Skip to content

Latest commit

 

History

History
236 lines (211 loc) · 5.91 KB

20210903123810-emacs_lisp.org

File metadata and controls

236 lines (211 loc) · 5.91 KB

Emacs Lisp

Characteristic

  • Only one data structure, list
  • Mutable Dynamic value, can has side-effects with some functions (like push)

Special forms

’ or quote

Any literal to quote will not evaluated and return as is

hello                                   ;void error
'hello
(quote hello)

Can be used to indicate list as a collection not a function call.

(1 2 3)                                 ;Invalid function: 1
'(1 2 3)
(quote (1 2 3))

It often use to determine a function/symbol/variable name and list collection (lisp’s data structure)

` (backquote)

` is similar to ’ but can be except for certain places where expressions are evaluated and inserted or spliced in.

(setq b `(ba bb bc))                    ; assume b has this value

Templates

Turn on/off

(defun turn-on-function function-name 1) ;off -1

Core functions

append and setq

Note that append doesn’t modify the original list - it creates a new list with t ed in a variable you need to assign back the new list: **** Welcome to IELM *** Type (describe-mode) for help. ELISP> (setq x (list 1 2 3)) (1 2 3)

ELISP> (setq y (list 4 5 6)) (4 5 6)

ELISP> (append x y) (1 2 3 4 5 6)

ELISP> x (1 2 3)

ELISP> (setq x (append x y)) (1 2 3 4 5 6)

ELISP> x (1 2 3 4 5 6)

list

Can have any arguments including 0

(list)
(list 1 2 3)

erase-buffer

(erase-buffer)                          ; *BECAREFUL*, your data may lost!

examples

;; Function that say hello to everyone in the list and return to source window
(setq list-of-names '("Sarah" "Chloe" "Mathilde"))
(defun greeting ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (mapcar 'hello list-of-names)
    (other-window 1))

(greeting)

Data Manipulation

Get the first element of this list with `car’:

(setq list-of-names '("Sarah" "Chloe" "Mathilde"))
(car list-of-names)

Get a list of all but the first element with `cdr’:

(cdr list-of-names)

Add an element to the beginning of a list with `push’:

(push "Stephanie" list-of-names)
list-of-names

Apply functions to each elements with `mapcar`

;; Let's call `hello' for each element in `list-of-names':
(mapcar 'hello list-of-names)
Hello Stephanie!
Hello Sarah!
Hello Chloe!
Hello Mathilde!

Search && replace

(defun replace-hello-by-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (goto-char (point-min))            ; goes to the beginning of the buffer.
    (while (search-forward "Hello")    ; searches for the string "Hello".
      (replace-match "Bonjour"))       ; replace it with "Bonjour", continue.
    (other-window 1))

(replace-hello-by-bonjour)

;; You should also get an error: “Search failed: Hello”. ;; ;; To avoid this error, you need to tell `search-forward’ whether it ;; should stop searching at some point in the buffer, and whether it ;; should silently fail when nothing is found:

;; (search-forward “Hello” nil t) does the trick:

;; The `nil’ argument says: the search is not bound to a position. ;; The `’t’ argument says: silently fail when nothing is found.

;; We use this sexp in the function below, which doesn’t throw an error:

(defun hello-to-bonjour ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    ;; Say hello to names in `list-of-names'
    (mapcar 'hello list-of-names)
    (goto-char (point-min))
    ;; Replace "Hello" by "Bonjour"
    (while (search-forward "Hello" nil t)
      (replace-match "Bonjour"))
    (other-window 1))

(hello-to-bonjour)

Docs

;; If you want to know more about a variable or a function: ;; ;; C-h v a-variable RET ;; C-h f a-function RET ;; ;; To read the Emacs Lisp manual with Emacs: ;; ;; C-h i m elisp RET ;; ;; To read an online introduction to Emacs Lisp: ;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html