An Introduction to Programming in Emacs Lisp Chapter 13

Info の使い方を覚えてきた。
「l」「r」が便利だね。
複数の Info のドキュメントを行き来するのは面倒だったけど,
Bookmarks というものを知り,快適になったぞ,こんにゃろめ。

Chapter 13

  • regexp
    • \W
      • not a word constituent
  • Count Words Region
    • (re-search-forward REGEXP end t)
    • リージョンだから,end を指定するのね
  • Count Words Recursively
    • helper function
    • recursive function tha calls itself
  • 出力用の関数は,別にしたいね
    • 引数でカウントする関数をもらう?

お試し

Emacs の? 正規表現では,[](characte alternative)を使用する場合は,
バックスラッシュは`スペシャル'ではないのね。へぇ〜。
これを知らずにちょっこすはまりました。
Elisp Info から引用。

;;    As a `\' is not special inside a character alternative, it can never
;; remove the special meaning of `-' or `]'.  So you should not quote
;; these characters when they have no special meaning either.
;; それぞれで C-xC-e してお試しね。
(re-search-forward "[.]")
(re-search-forward "[,]")
(re-search-forward "[;]")
(re-search-forward "[:]")
(re-search-forward "[!]")
(re-search-forward "[?]")
(re-search-forward "[\\]")
a. b, c; d: e! f? \g

Exercise

;;; Exercise
;; while 版
(defun count-punctuation-marks (beg end)
  "Print the number of punctuation marks in the region.
A punctuation mark is period, comma, semicolon, colon,
exlamation mark, or question mark."
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (let ((count 0))
      (while (and (< (point) end)
                  (re-search-forward "[.,;:!?]" end t))
        (setq count (1+ count)))
      (message "There are %d punctuation marks." count))))

;; 再帰版
(defun recursive-count-punctuation-marks (beg end)
  "Print the number of punctuation marks in the region.
A punctuation mark is period, comma, semicolon, colon,
exlamation mark, or question mark."
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (message "There are %d punctuation marks." (count-puncs-recursively end))))
(re-search-forward "[.]")

(defun count-puncs-recursively (end)
  (if (and (< (point) end)
           (re-search-forward "[.,;:!?]" end t))
      (1+ (count-puncs-recursively end))
    0))