emacs-learning-2

Emacs lisp

Emacs lisp is a dialect of Lisp language family used for script language in Emacs. For the pragraming process, it has similar machanism with lisp or mit-scheme (another dialect of Lisp). Since emacs lisp (Elisp for short) functions as a script language inside Emacs, it is necessary to learn Elisp for deeper understanding of Emacs and further hacking purpose.

Inside the emacs, the lisp repl can be invoked by switching to *scratch* buffer. (C-x b searches for the specific buffer. Default buffer will be *scratch*)

While interacting with Elisp, we can use C-x C-e or C-j to compile our procedure.

A useful reference provides more details and basic knowledge about Elisp.

Reverse polish notation is used for number calculations in Elisp:

1
2
3
4
(+ 2 2)
4
(+ 2 (* 3 4))
14

setq is used to create a new variable. insert is able to insert the string.

1
2
3
(setq my-name "Yang")
"Yang"
(insert "Hello " my-name)

Here, when we input C-x C-e, the lisp will insert string Hello Yang in the following of the insertcommand.

1
(insert "Hello " my-name)Hello Yang

defun can define a new function in the Elisp.

1
2
(defun func () (insert "Hello"))
(func)

switch-to-buffer-other-window is used to create a new window with specific buffer.

1
(switch-to-buffer-other-window "*test*") ;; switch to a buffer called *test*

progn combines multiple expressions into one expression.

1
2
3
4
(progn
(switch-to-buffer-other-window "*test*")
(hello)
(other-window 1))

let binds a value to a local variable.

1
2
(let ((local-name "yang"))
(hello local-name))

format is able to format a string with given format formula:

1
(format "hello %s!\n" "visitor")

Of course, there are some interative commands like read-from-minibuffer:

1
2
3
4
5
6
7
8
9
10
11
(read-from-minibuffer "Enter your name:")
;;; you can get variable name from prompt, such as
(defun greeting (from-name)
(let ((your-name (read-from-minibuffer "Enter your name: ")))
(insert (format "Hello!\n\nI am %s and you are %s."
from-name ; the argument of the function
your-name ; the let-bound var, entered at prompt
))))
;;; evaluate this command
(greeting "Jack")

In this post, it just summaries the basic opeartions from the link mentioned in the first paragraph. More functions or data-structure operations can be discovered in the future code reading.