簡體   English   中英

Emacs和Emacs Lisp的故障排除技術

[英]Troubleshooting techniques for Emacs and Emacs Lisp

我已經是一個相當常規的emacs用戶大約4年了,但在自定義emacs和故障排除elisp方面,我仍然是新手。 最近,我開始將emacs定制為我的ruby開發環境,我從StackOverflow中的人員那里學到了一些技巧。 例如,這里的一些人告訴我有關Cu CMx用edebug檢測函數然后逐步執行代碼。 我還發現,emacs中的大多數命令和模式都提供了大量的鈎子(函數或正則表達式或可自定義的變量),這些鈎子將提供任何新手想要的大部分內容。
現在我很貪心 - 我正在尋找你曾經使用並且過去發現有用的更多技巧和技巧。

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

當你想調試任意深度問題時,這些幫助。 你已經發現了edebug (這是我找出其他人代碼的首選工具)。 describe-function通常會為您提供鏈接到.el文件的鏈接(以及行號)。 這對於跳轉到問題的根源非常有用。 我經常這樣做,復制函數,輸入一些message調用並重新評估Cx Ce以使其運行而不是原始運行。

更新:這是我在John Wiegley演講中提到的一些東西。

(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

Cx Esc Esc為您提供了已運行的Mx命令的可瀏覽歷史記錄,但顯示了elisp代碼。

IELM是emacs lisp的代表。

Speedbar是瀏覽.el文件的絕佳方式,我發現自己經常使用Ch i(瀏覽elisp手冊)並使用speedbar瀏覽主題的節點樹。

信息瀏覽器中的Cs / Cr增量搜索實際上將搜索過去的分頁符。

我經常運行M-:測試快速的代碼而不必切換到我的* ielm *緩沖區。

對於特別棘手的代碼,我在桌面上創建一個快捷方式來運行emacs -q -l development-init.el (這對於處理緩沖區和外部進程的低級操作的代碼特別方便,這種東西很容易掛起emacs或用segv殺死它。

如果黑客入侵.emacs文件,請始終保持emacs進程運行,並通過啟動第二個Emacs進程來測試更改(使用--debug-init )。 這樣,如果出現問題,您仍然可以使用編輯器。

我的init文件評論調試工具:

;;;; * 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.

就我而言,我更喜歡(推薦) debug edebug ,但這可能只是一個品味問題。

除了Noufal提到的有關debug-on-*變量的內容之外,您還可以通過Mx debug-on-entry給定函數的調試器。

你可以把顯式調用debug在代碼中的斷點: (debug)(debug nil sexp-to-print)

歡迎來到啟蒙的前幾步。 ;)

首先,一些簡單的快速點擊:

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

這將為您提供迷你緩沖區的一些提示。 它非常方便! 這個提示不是關於故障排除,而是關於使其易於編寫的更多信息,但仍然如此。

從MELPA獲取erefactor包,並查看它的功能。 我注意到在對函數執行Cx Ce之后,它將通過elint運行結果。 省去了很多麻煩。

來自emacs入門套件的這個鈎子很棒。 它會刪除任何無效的.elc文件。 如果你不小心,老elc文件可能是你身邊真正的刺。 反過來看看自動編譯。

(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。 太棒了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM