An Introduction to Programming in Emacs Lisp Chapter 17

終わりの時は近いと思いきや,おまけがあるのでもうちょい続きそう。

Chapter 17

  • デバッグ
    • debug-on-error
      • void-function とか
    • debug-on-entry
      • M-x debug-on-entry RET FUNCTION
      • d
      • q
      • 解除
        • M-x cancel-debug-on-entry RET FUNCTION
        • もしくは,もっかい登録
    • 無限ループ
      • C-g で止まるよ
    • (debug)
      • c で return
    • edebug-defun
      • ソースコードレベルでステップ実行
      • 実行中のソースコードの該当行に三角矢印が!!
      • SPC で潜っていく
      • minibuffer に評価中の sexpression の結果が表示される
      • gdb みたいな感じかな。

Exercise とか

;; Chapter 17
(defun triangle-bugged (arg)
  (let ((total 0))
    (while (> arg 0)
      (setq total (+ total arg))
      (setq arg (1= arg)))
    total))
(triangle-bugged 5)


;;; Exercise
(defun count-words-region (beg end)
  ""
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (let ((count 0))
      (while (and (< (point) end) (re-search-forward "\\w+\\W*" end t))
        (setq count (1+ count)))
      (cond ((zerop count)
             (message "The region does NOT have any words."))
            ((= 1 count)
             (message "The region has 1 word."))
            (t (message "The region has %d words." count))))))

;; count-words-region を試せぜよ。
first second

;; is a hook called after the command finishes?
;; => yes, some hooks were called on my system.

;; For example, these were called.
  ac-on-post-command()
* run-hooks(post-command-hook)

* run-hooks(elscreen-screen-update-hook)
* elscreen-run-screen-update-hook()
* run-hooks(post-command-hook)

;;; count-words-region を edebug-defun で試せぜよ。

;; defun 内で,M-x edebug-defun する。
(defun count-words-region (beg end)
  ""
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (let ((count 0))
      (while (and (< (point) end) (re-search-forward "\\w+\\W*" end t))
        (setq count (1+ count)))
      (cond ((zerop count)
             (message "The region does NOT have any words."))
            ((= 1 count)
             (message "The region has 1 word."))
            (t (message "The region has %d words." count))))))

;; んで,適当に region をアクティブにして,
;; M-x count-words-region っと。
;; SPC でひたすら進んでいきましょう。
;; 値が変わっていくのを見ているのは楽しいかも〜。


;; edebug 中にキーバインドを,`?' を押して調べてみましょう。
;; わー,いっぱいあるー。
The edebug buffer commands:
key             binding
---             -------

C-c             Prefix Command
TAB             lisp-indent-line
C-x             Prefix Command
ESC             Prefix Command
SPC             edebug-step-mode
-               negative-argument
=               edebug-temp-display-freq-count
?               edebug-help
B               edebug-next-breakpoint
C               edebug-Continue-fast-mode
E               edebug-visit-eval-list
G               edebug-Go-nonstop-mode
I               edebug-instrument-callee
P               edebug-view-outside
Q               edebug-top-level-nonstop
S               edebug-stop
T               edebug-Trace-fast-mode
W               edebug-toggle-save-windows
X               edebug-set-global-break-condition
a               abort-recursive-edit
b               edebug-set-breakpoint
c               edebug-continue-mode
d               edebug-backtrace
e               edebug-eval-expression
f               edebug-forward-sexp
g               edebug-go-mode
h               edebug-goto-here
i               edebug-step-in
n               edebug-next-mode
o               edebug-step-out
p               edebug-bounce-point
q               top-level
r               edebug-previous-result
t               edebug-trace-mode
u               edebug-unset-breakpoint
v               edebug-view-outside
w               edebug-where
x               edebug-set-conditional-breakpoint
DEL             backward-delete-char-untabify

C-c C-c         edebug-go-mode
C-c C-d         edebug-unset-breakpoint
C-c C-l         edebug-where
C-c C-n         edebug-next-mode
C-c C-s         edebug-step-mode
C-c C-t         ??

C-x C-e         edebug-eval-last-sexp
C-x SPC         edebug-set-breakpoint

M-TAB           lisp-complete-symbol
C-M-q           indent-pp-sexp
C-M-x           eval-defun

C-M-q           indent-sexp
  (that binding is currently shadowed by another mode)

;; edebug 中に `p' を押下して,今どこで count-words-region が動いているか,
;; 見てみましょう。

;; 見ました〜。ウィンドウを分割しておくと,ポイントが行って帰ってくるのが,
;; よくわかるね〜。


;; edebug 中に `h' を使って,`here!!' と指示しましょう。
;; おぉ〜。きちんと指示に従ってくれるよい子ですね。


;; edebug 中に,`t' (edebug-trace-mode) で散歩してみましょう。
;; ぬぉっー!!! (パパス談)
;; すげー。楽しいぞ。

;; edebug 中に,`T' (edebug-Trace-fast-mode) で爽快に走り抜けましょう。
;; ぬぉぅー!!!! (パパス談)
;; はえー。

;; edebug の trace-mode 中に,ブレイクポイントを指定して,
;; そこまで実行せよ。

;; M-x count-words-region して,まず edebug。
;; んで,カーソルを移動して,適当な場所で `b' して,
;; ブレイクポイントを設定。
;; さ,行ってらっしゃーい!! で,`T'
;; おぉー,止まるね。

;; ブレイクポイントの解除は,`u' でね。edebug-unset-breakpoint
;; ブレイクポイント関係のコマンド
B               edebug-next-breakpoint
b               edebug-set-breakpoint
u               edebug-unset-breakpoint
x               edebug-set-conditional-breakpoint
C-c C-d         edebug-unset-breakpoint
C-x SPC         edebug-set-breakpoint

;; ブレイクポイントをたくさん作っちゃったよ−。どざえも〜ん。
;; `B' でブレイクポイントにひょいひょいと移動できるので,
;; 移動する。んで,C-cC-d or `u' で 解除するといいと思うよ。

;; `x' すると条件を効いてくるので,適当に(= count 1) とか。
;; んで,`T' すると Break: (= count 1) => t で止まってくれる。
;; すげーぞ。がんばってるな,elisp。
;; なんでもデバッグできそうな気がしてきた。
;; あ,いいこと思いついた。
;; デバッグ練習帳みたいな本出してくれたら買うかも〜。
;; なんか今,デバッグ関連の本がでてるみたいだし,便乗で誰か出してくれないかなぁ。

おまけ

M-m (back-to-indentation) は便利だお。