关于elisp:Emacs和Emacs Lisp的故障排除技术

Troubleshooting techniques for Emacs and Emacs Lisp

我已经是相当普通的emacs用户,已经有4年了,但是在定制emacs和对elisp进行故障排除方面,我仍然是一个新手。 最近,我已经开始将emacs定制为我的红宝石开发环境,并且在StackOverflow中从这里的人们那里学到了一些技术。
例如,这里有人告诉我有关C-u C-M-x的知识,以使用edebug来检测一个功能,然后遍历代码。 我还发现,emacs中的大多数命令和模式都提供了大量的钩子(函数或正则表达式或可自定义的变量),这些钩子可以提供任何新手想要的东西。
现在,我很贪婪-我正在寻找您过去使用并发现有用的更多技术和技巧。


1
2
 (setq debug-on-error t)
 (setq debug-on-quit t)

当您要调试任何深层次的问题时,这些功能会有所帮助。您已经发现edebug(这是我确定其他人代码的首选工具)。 describe-function通常为您提供一个指向.el文件(以及行号)的链接,该文件被加载到该文件中。这对于跳到问题根源很有用。我经常这样做,复制该函数,进行一些message调用,然后重新评估C-x C-e以使其运行而不是原始运行。

更新:这是我从约翰·威格利的演讲中得到的东西。

1
2
3
4
(global-set-key (kbd"C-c C-d")
        (lambda () (interactive)
          (setq debug-on-error (if debug-on-error nil t))
          (message (format"debug-on-error : %s" debug-on-error))))

让我们通过一次按键切换debug-on-error


C-x Esc Esc可让您浏览已运行的M-x命令的历史记录,但会显示elisp代码。

IELM是emacs lisp的代表。

Speedbar是浏览.el文件的绝佳方法,我也经常使用C-h(浏览elisp手册)并使用speedbar浏览主题的节点树。

信息浏览器中的C-s / C-r增量搜索实际上将搜索过去的分页符。

我经常运行M-:来测试一些快速代码,而不必切换到我的* ielm *缓冲区。

对于特别棘手的代码,我在桌面上做了??一个快捷方式来运行emacs -q -l development-init.el(这是对于处理缓冲区和外部进程的低级操作的代码特别方便,这种类型的事情很容易挂起emacs或用segv杀死它。


如果黑客您的.emacs文件,请始终保持emacs进程运行,并通过启动第二个Emacs进程来测试更改(使用--debug-init)。这样,如果出现问题,您仍然可以使用编辑器。


我的init文件对调试工具进行了评论:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;;;; * Debugging, Tracing, and Profiling

;; M-: (info"(elisp) Debugging") RET

;; Standard debugger:
;; M-x debug-on-entry FUNCTION
;; M-x cancel-debug-on-entry &optional FUNCTION
;; debug &rest DEBUGGER-ARGS
;; M-x toggle-debug-on-error
;; M-x toggle-debug-on-quit
;; setq debug-on-signal
;; setq debug-on-next-call
;; setq debug-on-event
;; setq debug-on-message REGEXP

;; Edebug -- a source-level debugger for Emacs Lisp
;; M-x edebug-defun (C-u C-M-x) Cancel with eval-defun (C-M-x)
;; M-x edebug-all-defs -- Toggle edebugging of all definitions
;; M-x edebug-all-forms -- Toggle edebugging of all forms
;; M-x edebug-eval-top-level-form

;; Tracing:
;; M-x trace-function FUNCTION &optional BUFFER
;; M-x untrace-function FUNCTION
;; M-x untrace-all

;; Timing and benchmarking:
;; (benchmark-run &optional REPETITIONS &rest FORMS)

;; Emacs Lisp Profiler (ELP)
;; M-x elp-instrument-package
;; M-x elp-instrument-list
;; M-x elp-instrument-function
;; M-x elp-reset-*
;; M-x elp-results
;; M-x elp-restore-all
;;
;;"There's a built-in profiler called ELP. You can try something like
;; M-x elp-instrument-package, enter"vc", and then try finding a file
;; Afterwards, M-x elp-results will show you a profile report.
;; (Note that if the time is instead being spent in non-vc-related
;; functions, this technique will not show it, but you can instrument
;; further packages if you like.)" http://stackoverflow.com/a/6732810/324105

;; CPU & Memory Profiler ('Native Profiler')
;; M-x profiler-start
;; M-x profiler-report
;; M-x profiler-reset
;; M-x profiler-stop
;; M-x profiler-*

;; Dope ("DOtemacs ProfilEr. A per-sexp-evaltime profiler.")
;; https://raw.github.com/emacsmirror/dope/master/dope.el
;; M-x dope-quick-start will show a little introduction tutorial.

;; Spinning:
;; Set debug-on-quit to t
;; When the problem happens, hit C-g for a backtrace.

就我而言,相对于edebug,我更喜欢(推荐)debug,但这可能只是口味问题。

除了Noufal提到的关于debug-on-*变量的内容外,您还可以通过M-x debug-on-entry输入给定功能的调试器。

您可以在代码中的断点处对debug进行显式调用:(debug)(debug nil sexp-to-print)


欢迎来到启蒙的前几步。 ;)

首先,一些简单的快速提示:

1
(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)

这将给您关于迷你缓冲区的一些提示。非常方便!本技巧所涉及的不是故障排除,而更多的是使其易于编写,但仍然如此。

从MELPA获取erefactor软件包,然后看一下它的作用。我注意到对函数执行C-x C-e后,它将通过elint运行结果。节省了很多麻烦。

emacs入门套件中的这个钩子很棒。它删除所有无效的.elc文件。如果您不小心,旧的elc文件可能会对您不利。相反地??看一下自动编译。

1
2
3
4
5
6
7
8
9
(add-hook 'emacs-lisp-mode-hook 'starter-kit-remove-elc-on-save)

(defun starter-kit-remove-elc-on-save ()
 "If you're saving an elisp file, likely the .elc is no longer valid."
  (make-local-variable 'after-save-hook)
  (add-hook 'after-save-hook
            (lambda ()
              (if (file-exists-p (concat buffer-file-name"c"))
                  (delete-file (concat buffer-file-name"c"))))))

最后,在编辑lisp,emacs-lisp或scheme时,请确保尝试使用paredit。太棒了。