An Introduction to Programming in Emacs Lisp Chapter 12

Chapter 12

  • regular expression
    • \n
      • new line
      • 使うときはそのまま
      • \r はないの?
    • \t
      • タブ
      • 使うときはそのまま
    • \\(...\\)
      • 「\|」で「または」を作るため
      • 繰り返し用(* とか +)
      • マッチした表現を記録して,あとで参照するため
        • \\1 とか
    • \w
      • any word-constituent character
        • 使う場合はクォート
        • \\w
  • re-search-forward
  • forward-sentence
  • forward-paragraph
    • BODY なし while の登場!!
  • looking-at
  • eobp
    • End Of Buffer Predicate
  • prog1
    • なんて発音するの?
      • ぷろぐわん?
    • progn
      • これは,「ぷろぐえぬ」?
  • let*
    • let とほとんど同じ
    • 先にバインドした変数を使えるぞ
(let ((foo 1) (bar (1+ foo)) ...))
  • match-beginning
    • Elisp の Info で丁寧に解説されているお
  • TAGS
    • etags
    • M-x visit-tags-table
    • M-.
      • find-tag
    • M-*
      • pop-tag-mark

弄ってみる。

ま,Info に詳しく書いてあるので,それを見るのがなによりですが,
試してみないと,おぉー,と言えないのでね。

(string-match "\\(ha\\)\\(rd\\)"
              "It's hard to learn by yourself how to write in English.")
               01234567890

;; 与えた正規表現全体
(match-string 0 "It's hard to learn by yourself how to write in English.")
; => "hard"

;; 与えた正規表現の1個目
(match-string 1 "It's hard to learn by yourself how to write in English.")
; => "ha"

;; 与えた正規表現の2個目
(match-string 2 "It's hard to learn by yourself how to write in English.")
; => "rd"

(match-end 0) ; => 9
(match-end 1) ; => 7
(match-end 2) ; => 9

Exercise

;; いいかどうかは知らんが,上も下もみるようにしてみました。
;; make it look high and low みたいな!
(defun search-for-blank-lines ()
  "Search for two or more consecutive blank lines."
  (interactive)
  (let ((blines "\\(^[ \t]*\n\\)\\{2,\\}"))
    (unless (or (re-search-forward blines nil t 1)
                (re-search-backward blines nil t 1))
      (message "not found."))))

;; 2回以上連続出現する単語を検索
(defun the-the ()
  "Search forward for duplicated words from the current position."
  (interactive)
  (let ((needle "[ \t\n]+\\(\\w+\\)[ \t\n]+\\1[ \t\n]+"))
    (if (re-search-forward needle nil t 1)
        (progn
          (push-mark)
          (goto-char (match-beginning 1)))
      (message "No duplicated word."))))

おまけ

新しく知った Emacs の便利機能

  • C-M-o
    • カーソルの位置以降の文字をカラム位置はおんなじで,1行下におろす。わかりにく?
    • C-o してずらす感じ。
  • M-x string-insert-rectangle
    • メールとかコメントに返信する場合に,「> 」を行頭に入れたい。
    • んな場合は,対象の開始位置をマーク。
    • 対象の最後の行の末尾にカーソルを。
    • で,M-x string-insert-rectangle
    • 「> 」を入力。
    • おぉー。パチパチパチ。
    • だが,王道ではない気がする。なんか他にうまい方法ってあるのかなぁ?