Last active
August 29, 2015 14:04
-
-
Save xuchunyang/fe947fccefd69bfec26d to your computer and use it in GitHub Desktop.
Play with Emacs Lisp
This file contains 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
;;; filename: emacs-lisp-basic.el | |
(+ 23 3) | |
(message "hi") | |
;; store variable | |
(setq my-name "Chunyang Xu") | |
(setq my-age 21) | |
(insert "hello" " world") | |
;; define function | |
(defun hello (name) | |
(insert "hi" " " name) | |
) | |
;; call it | |
(hello "you")hi you | |
(switch-to-buffer-other-window "xcy.org") | |
;; combine statements | |
(progn | |
(hello "you") | |
(message "date")) | |
(progn | |
(switch-to-buffer-other-window "xcy.org") | |
(erase-buffer) | |
(hello "there") | |
) | |
;; local variable | |
(let ((local-name "you")) | |
(switch-to-buffer-other-window "xcy.org") ;no need for progn here, let does it | |
(hello local-name) | |
(other-window 1) | |
) | |
;; format string | |
(format "I'm %s, %d old.\n" my-name my-age) | |
(defun hello (name) | |
(insert (format "hi %s\n" name))) | |
(hello "xcy")hi xcy | |
;; create a function use 'let' | |
(defun say-hi (name) | |
(let ((tmp-message "你好")) | |
(insert (format "%s, %s!" | |
name ;function's argument | |
tmp-message ;let-bound variable | |
))) | |
) | |
(say-hi "徐春阳") | |
;; 交互式的函数 | |
(read-from-minibuffer "姓名: ") | |
;; within function | |
(defun say-hi2 () | |
(let ((name (read-from-minibuffer "姓名 "))) | |
(insert (format "\nhi, %s\n" name)))) | |
(say-hi2) | |
hi, xcy | |
(defun say-hi-on-other-window () | |
(let (your-name (read-from-minibuffer "姓名 ")) | |
(other-window 1) | |
(insert (format "\nhi, %s" your-name)) | |
(other-window 1) | |
)) | |
(say-hi-on-other-window) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; 列表 | |
(setq list-of-numbers '(-2 23 1024)) | |
(setq list-of-names '("me" "my broken ❤")) | |
;; 第一个 | |
(car list-of-numbers) | |
(car list-of-names) | |
;; 剩下的 | |
(cdr list-of-numbers) | |
(cdr list-of-names) | |
;; push 添加到开头 | |
(push "chunyang" list-of-names) | |
;; 注意,car 和 cdr 不会改动列表,而 push 会 | |
;; 映射 | |
(mapcar 'hello list-of-names) | |
(defun replace-chunyang-by-xcy () | |
(get-buffer-create "new-buffer") | |
(switch-to-buffer-other-window "new-buffer") | |
(goto-char (point-min)) | |
(while (search-forward "chunyang") | |
(replace-match "xcy")) | |
(other-window 1) | |
) | |
(replace-chunyang-by-xcy) | |
(defun boldfy-names () | |
(switch-to-buffer-other-window "new-buffer") | |
(goto-char (point-min)) | |
(while (re-search-forward "xcy" nil t) | |
(add-text-properties (match-beginning 1) | |
(match-end 1) | |
(list 'face 'bold))) | |
(other-window 1) | |
) | |
(boldfy-names) | |
;; lambda | |
((lambda (name) | |
(message (format "hi, %s" name))) | |
"Chunyang Xu" | |
) | |
(mapcar (lambda (s) (insert s "\n" )) | |
(split-string (shell-command-to-string "ls .")) | |
) | |
;; fix the mac PATH variable | |
(defun ome-set-exec-path-from-shell-PATH () | |
(let ((path-from-shell (shell-command-to-string "$SHELL -i -c 'echo $PATH'"))) | |
(setenv "PATH" path-from-shell) | |
(setq exec-path (split-string path-from-shell path-separator)))) | |
(shell-command-to-string "$SHELL -i -c 'echo $PATH'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment