An Introduction to Programming in Emacs Lisp Chapter 05
Chapter 05
- copy-to-buffer
- current-bufferからコピー
- insert-buffer
- current-bufferへ挿入
- save-excursionして行った先で,start, endをsetq
- (interactive "*b")
- b:バッファは存在すべし
- *:read-onlyなバッファには何もしない
- orの使い方
- beginning-of-buffer
- optional argument
- (&optional arg)
- (interactive "P")
- Prefix argument
- C-u
- M-7とかも
- prefix-numeric-value
- 文字列与えると1になる
- C-u 7 M-
- バッファの先頭から70%いったところへ移動
- overflowを鑑みて移動場所を計算
- bufferサイズが小さい場合は,先に乗算しとかないと0になる場合がある
- prefix argumentに10以上を与えると,バッファの末尾へ移動
- optional argument
M-x describe-function RET what-line をすると,
Print the current buffer line number and narrowed line number of point.
おっ,narrowing されている場合は,narrowing された状態での,
ポイントのある行番号も表示するとのこと。
んで,simple.el をみてみる。
コメントを勝手につけました。
(defun what-line () "Print the current buffer line number and narrowed line number of point." (interactive) (let ((start (point-min)) (n (line-number-at-pos))) ;; line-number-at-pos はポイントのある行の行番号を返す。 ;; narrowing されてない場合は,そのまま表示。 (if (= start 1) (message "Line %d" n) ;; narrowing されている場合 (save-excursion (save-restriction (widen) ;; ポイントである start がある行を, ;; 重複してカウントしているので,-1。 (message "line %d (narrowed line %d)" (+ n (line-number-at-pos start) -1) n))))))
感想
ドキュメントをすぐに見られると言うことは,
すごく便利だなぁと,また,それゆえに,
すごく大事だなぁと,思った次第。
Exercises
;;; Exercise 6.3 ;; buffer-substring だと,text-properties を含めて ;; 結果として返すってあるから, ;; buffer-substring-no-properties を使った。 ;; goto-char と point-min を使えってあるけど, ;; なくてもいいんじゃね? ;; widen しているなら,(buffer-substring-no-properties 1 60) ;; で済みそうだけど。。。だめな理由があるのかなぁ? (defun first-60-chars () "Print the first 60 characters even if the buffer is narrowed." (interactive) (save-restriction (widen) (save-excursion (goto-char (point-min)) (message "%s" (buffer-substring-no-properties (point-min) (+ 60 (point-min))))))) ;; お試し (first-60-chars) ;; C-u C-x C-e ;; => "*[Emacs][Lisp][Meadow] An Introduction to Programming in Ema"