Learning Emacs Lisp

Table of Contents
  1. 1. List Processing
    1. 1.1. Lisp List
    2. 1.2. Run a Program
    3. 1.3. Generate an Error Message
    4. 1.4. Functions and Arguments
    5. 1.5. The message function
    6. 1.6. Setting the Value of a Variable
  2. 2. Practicing Evaluation
  3. 3. How to Write Function Definitions
    1. 3.1. The defun Macro

List Processing

Lisp List

  • List definition: '(a, b, c, d, ...)
  • lists can be programs AND data
  • Atom: indivisible element
  • Atoms contain: numbers, symbols, …

    Run a Program

  • ' tells List to do nothing with the list, otherwise, use the first element in the list as the function
  • Use C-x C-e to evaluate a lisp expression in emacs

    Generate an Error Message

  • Evaluate (a, b, c, d) will generate an error, type q to quit

    Functions and Arguments

  • concat number-to-string

    The message function

  • (message "This is a message")
  • (message "The current buffer is %s" (buffer-name))

    Setting the Value of a Variable

  • Using set: (set 'flowers '(rose violet daisy buttercup))
  • Using setq: (setq flowers '(rose violet daisy buttercup))

    Practicing Evaluation

  • To view documentation: (info "(elisp)Files")
  • Each keystroke calls the self-insert-command, an interactive function
  • (buffer-size) (point)

    How to Write Function Definitions

    The defun Macro

  • A typical list function:
1
2
3
4
(defun function_name (args)
"documentation"
(interactive argument-passing-info) ; optional
body...)
  • interactive: (interactive "p") uses prefix
  • let:
1
2
3
4
(let ((var_name, var_value),
(var_name, var_value)...)
body...
)
  • if:
1
2
3
(if (> 5 4)
(message "5 is greater than 4")
(message "otherwise"))

-