Created
December 27, 2014 14:38
-
-
Save xuchunyang/7f02337a0ee3e9198a01 to your computer and use it in GitHub Desktop.
elisp-demo-01.el
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 1. (interactive) | |
;; "P": prefix argument | |
;; "B": Buffer name | |
;; "b": should be a >exiting buffer | |
;; "r": Point and the mark | |
;; "*": Signal an error if the current buffer is read-only. Special. | |
(defun add-by-two (number) | |
"Add NUMBER by two." | |
(interactive "P\nbF buffer: ") ; Enable the prefix argument | |
(message "The result is %d." (+ number 2))) | |
(add-by-two 3) | |
;; 2. (save-excursion ...) | |
;; Save mark, point and current buffer | |
(save-excursion | |
(switch-to-buffer-other-window "*test-buffer*") | |
(goto-char (point-min)) | |
(erase-buffer) | |
(insert ";;=>") | |
(other-window 1)) | |
(defun my-beginning-of-buffer () | |
"Move point to the beginning of the buffer; | |
leave mark at previous position." | |
(interactive) | |
(push-mark) | |
(goto-char (point-min))) | |
(defun my-mark-whole-buffer () | |
"Put point at beginning and mark at end of buffer." | |
(interactive) | |
(push-mark (point)) | |
(push-mark (point-max) nil t) | |
(goto-char (point-min))) | |
(defun my-append-to-buffer (buffer start end) | |
"Append to specified buffer the text of the region. | |
It is inserted into that buffer before its point. | |
When calling from a program, give these arguments: | |
BUFFER (of buffer name, START and END. | |
START and END specify the portion of the current buffer to copied." | |
(interactive | |
(list (read-buffer "Append to buffer: " (other-buffer | |
(current-buffer) t)) | |
(region-beginning) (region-end))) | |
(let ((oldbuf (current-buffer))) | |
(save-excursion | |
(let* ((append-to (get-buffer-create buffer)) | |
(windows (get-buffer-window-list append-to t t)) | |
point) | |
(set-buffer append-to) | |
(setq point (point)) | |
(barf-if-buffer-read-only) ;error message when finds read-only buffer | |
(insert-buffer-substring oldbuf start end) | |
(dolist (window windows) | |
(when (= (window-point window) point) | |
(set-window-point window (point)))))))) | |
(defun buffer-exist? (buffer-name) | |
"Use `if' and `get-buffer' to test if BUFFER exist." | |
(interactive) | |
(if (get-buffer buffer-name) | |
(message "Yes, the buffer '%s' exist." buffer-name) | |
(message "No, the buffer '%s' doesn't exist." buffer-name))) | |
(buffer-exist? (current-buffer)) | |
;; 3. with-current-buffer | |
;; tell computer to work on another buffer not user | |
;; is used to replace "save-excursion + set-buffer" | |
(defun copy-to-buffer (buffer start end) | |
"Copy to specified buffer the text of the region. | |
It is inserted into that buffer, replacing existing text there. | |
When calling from a program, give three arguments: | |
BUFFER (or buffer name), START and END. | |
START and END specify the portion of the current buffer to be copied." | |
(interactive "BCopy to buffer: \nr") | |
(let ((oldbuf (current-buffer))) ; user seem buffer now called old buffer | |
(with-current-buffer (get-buffer-create buffer) | |
(barf-if-buffer-read-only) | |
(erase-buffer) | |
(save-excursion ;with current user seen buffer | |
(insert-buffer-substring oldbuf start end))))) | |
(defun my-insert-buffer (buffer) | |
"Insert after point the contents of BUFFER. | |
Puts mark after the insert text. | |
BUFFER may be a buffer of a buffer name." | |
(interactive "*bInsert buffer: ") | |
(or (bufferp buffer) | |
(setq buffer (get-buffer buffer))) | |
(let (start end newmark) ;bind 3 local vars to nil | |
(save-excursion | |
(save-excursion ;go to source buffer | |
(set-buffer buffer) | |
(setq start (point-min) | |
end (point-max))) | |
(insert-buffer-substring buffer start end) ;go to destination buffer | |
(setq newmark (point))) | |
(push-mark newmark))) | |
;; 4. or | |
(or (bufferp (get-buffer "*scratch2*")) | |
"Nope") | |
(if (bufferp (get-buffer "*scratch2*")) | |
"Yep" | |
"Nope") | |
(unless (bufferp (get-buffer "*scratch2*")) | |
"Nope") | |
(when (not (bufferp (get-buffer "*scratch2*"))) | |
"Nope") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment