Emacs 菜鸡教程

Cutting and Storing Text

list

nthcdr: taking cdr n times.

(nthcdr 3 '(1 2 3 4 5 6))
    => (4 5 6 7)

zap-to-char

剪切到指定字符,放到kill-ring中。 使用C-y(yank)回来。

implementation

(kill-region (point) (progn
                         (search-forward (char-to-string char)
                                         nil nil arg)
                         (point)))

search-forward: 搜索指定字符

(search-forward "target-string"
                limit-of-search
                what-to-do-if-search-fails
                repeat-count)
;; 
(search-forward (char-to-string char) nil nil arg)

kill-region

剪切region到kill-ring

copy-region-as-kill

和kill-region几乎一模一样,只是不剪切。

kill-ring

kill-ring就是个list。

(setq kill-ring '("1" "2" "3"))
kill-ring 
;;  => ("1" "2" "3")
(setcar kill-ring "head")
;;  => ("head" "2" "3")
(push "ass" kill-ring)
;;  => ("ass" "head" "2" "3")
(length kill-ring)
;; 4 ..

exercises

Write an interactive function that searches for a string. If the search finds the string, leave point after it and display a message that says “Found!”.

(defun test-search (str)                  
    "finds a string, display 'Found!'"    
  (interactive "sfind the String: ")      
  (progn (search-forward str nil nil 1)   
         (message (concat "Found " str))))

Write a function that prints the third element of the kill ring in the echo area, if any; if the kill ring does not contain a third element, print an appropriate message.

(defun third-element-of-kill-ring ()
  "the third elemnt of kill ring"
  (interactive)
  (if (< (length kill-ring) 3)
      (message "the length of kill-ring < 3")
    (let ((ele (car (nthcdr 2 kill-ring))))
      (message (concat "the third element of kill-ring is "  ele)))))