An Introduction to Programming in Emacs Lips Chapter 08

Chapter 08

  • zap-to-char
    • (interactive "*p\ncZap to char: "
  • search-forward
  • progn
    • special form
  • kill-region
    • VAR
    • BODYFORM
      • delete-and-extract-region
        • [START, END]を削除
        • del_range_1とかを呼んでいく
      • if (eq this-command 'kill-regoin)
        • kill-append
          • kill-new
        • kill-new
    • HANDLERS
      • buffer-read-only or text-read-only
      • copy-region-as-kill
        • kill-append
          • kill-new
        • kill-new
  • kill-new
    • kill-ring を直接いじっている
    • nthcdr
      • リストの最初の n 個を取り除いた物を返す
  • conditon-case
    • VAR
    • BODYFORM
    • ERROR HANDLERS
    • お試し
  • defvar
    • special form
    • 「*」
      • edit-option コマンドで編集できるらしいが・・・。
  • interactive
    • c
      • character
    • v
      • variable
    • M
      • any string
    • s

お試し

;; search-forward のお試し
(search-forward (char-to-string ?x) nil nil 2);; ここで,C-xC-eすると,カーソルが 最初の y へ飛ぶ
;; abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz

;; zap-to-char のお試し
(defun my-zap-to-char (arg char)
  ""
  (interactive "*p\ncZap to char: ")
  (kill-region (point)                  ; 現在のポイント
               (progn
                 (search-forward
                  (char-to-string char) nil nil arg)
                 (point))))             ; 見つかった文字のポイント
;; お試し
(my-zap-to-char 1 ?q)
;; この行の終わりで,M-x my-zap-to-char RET q RET すると,
;; abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz

;; 下のようになる。
;; この行の終わりで,M-x my-zap-to-char RET q RET すると,rstuvwxyz abcdefghijklmnopqrstuvwxyz


;; condition-case のお試し
;; まずは,シンプルに。
(condition-case nil "Hello, condition-case!!" nil)
; => "Hello, condition-case!!"


;; 次に,ちょっこす工夫。
(condition-case
    nil
    (let ()
      (setq buffer-read-only t)
      (erase-buffer))
  ((buffer-read-only)
   "Hello, condition-case from an error handler!"))

;; この S-expression を評価すると,バッファが read-only になり,
;; echo エリアに,"Hello, condition-case from an error handler!" と
;; 表示される。
;; read-only になったら,C-q (toggle-read-only) で,read-only を解除。

;; もひとつ error-handler のところがわからんが,
;; とりあえず,拾えているようなので,よしとする。

Exercises

;;; Exercises 8.7
(defun test-search (needle)
  "Search forward for NEEDLE from the current point"
  (interactive "sString: ")
  ;; 見つからなかった場合に,nil を返してもらうために,
  ;; search-forward の third argument として,t を渡す。
  (if (search-forward needle nil t 1)
      (message "Found!")
    (message "Not Found")))


(defun third-in-kill-ring ()
  "Display the third element in the kill ring, if any."
  (interactive)
  (if (>= (length kill-ring) 3)
      ;; Zero origin in kill-ring.
      ;; Use substring-no-properties to remove text properties.
      (message "%s" (substring-no-properties (nth 2 kill-ring)))
    (message "kill-ring does not have the third element. (Its length is %d)" (length kill-ring))))

;; お試し
(third-in-kill-ring)


(defun n-elem-in-kill-ring (n)
  "Display the nth element in kill ring, if any."
  (interactive "p")
  (if (>= (length kill-ring) n)
      (message "%s" (substring-no-properties (nth (1- n) kill-ring)))
    (message "kill-ring does not have the %dth element. (Its length is %d)" n (length kill-ring))))

;; お試し
(n-elem-in-kill-ring 10)

(n-elem-in-kill-ring 90)
; => "kill-ring does not have the 90th element. (Its length is 60)"