elisp


General

Type conversions

Check value of a type:

Type conversions:

funcall and apply

(funcall '+ 2 3)
; 5

(apply '+ '(2 3))
; 5

Emacs terminology

Point

https://www.gnu.org/software/emacs/manual/html_node/elisp/Point.html

Associated functions:

Kill ring

See:

Single characters

Preceded by a ? for syntax.

; ?a is like 'a' in C
?a
97

See:

Lists

nth element of a list

(nth 0 '(0 1 2 3 4 5))
0

(nth 3 '(0 1 2 3 4 5))
3
(nthcdr 3 '(0 1 2 3 4 5))
(3 4 5)

(nthcdr 10 '(0 1 2 3 4 5))
nil

https://www.gnu.org/software/emacs/manual/html_node/eintr/nth.html

car and cdr

(car '("a" "b" "c"))
; "a"

(car '(0 1 2))
; 0

(car '())
; nil
(cdr '("a" "b" "c"))
; ("b" "c")

(cdr '(0 1 2))
; (1 2)

(cdr '())
; nil

https://www.gnu.org/software/emacs/manual/html_node/eintr/car-_0026-cdr.html

Strings

See:

Uppercase and lowercase

If argument is char, return value is int for both downcase and upcase.

Splitting a string

split-string

(split-string "1.2.3" "\\.")
; ("1" "2" "3")

(split-string "a-b c" "-")
; ("a" "b c")

https://stackoverflow.com/questions/6236196/elisp-split-string-function-to-split-a-string-by-character

Functions

defun foo () (5)
(foo)  ;5
(defun fact (a) (cond
    ((= a 0) 1)
    (t (* a
          (fact (- a 1))
       )
    )))

(fact 5)  ; 120
(defun gcd (a b)
  (cond
    ((< a b) (gcd b a))
    ((= (% a b) 0) b)
    (t (gcd b (% a b)))
  ))

(gcd 1000 10)  ; 10
(atom 1)  ; t
(atom (list 1 2))  ; nil
;; NOT WORKING
; find sqrt of a.
; x is starting value
; e is permissible error
(defun sqrt_nr (a x e)
  (cond
    ((< (abs (- (* x x) a)) e) x)
    (t (sqrt_nr a (* (/ 1 2) (+ x (/ a x))) e))
  ))

Plist (Property list)

Functions:

In:

(pine cones numbers (1 2 3) color "blue")

it's like:

Examples:

(setq-local myplist '("pine" "cones" "numbers" (1 2 3) "color" "blue"))
; ("pine" "cones" "numbers" (1 2 3) "color" "blue")

(setq-local myplist (plist-put myplist "vive" "la"))
; ("pine" "cones" "numbers" (1 2 3) "color" "blue" "vive" "la")

(setq-local l myplist)
; ("pine" "cones" "numbers" (1 2 3) "color" "blue" "vive" "la")

(plist-get '(foo 4) 'foo)
; 4

Alist (Association list)

(setq-local myalist
  '(("name" . "joe")
   ("age" . 50)
   ("planet" . "earth")))
; (("name" . "joe") ("age" . 50) ("planet" . "earth"))

Functions:

Folding over an alist (courtesy of chatgpt):

(require 'cl-lib)

(setq-local my-alist '((a . 1) (b . 2) (c . 3)))
((a . 1) (b . 2) (c . 3))

(cl-reduce (lambda (acc pair)
            (+ acc (cdr pair)))  ;; Accumulate the values (cdr of each pair)
           my-alist
           :initial-value 0)
; 6

Hash table

Creating hash tables

#s(hash-table data (key1 val1 key2 300))
; #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 data (key1 val1 key2 300 ...))

#s(hash-table (key1 val1 key2 300))
; #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 data ( ...))

#s(key1 val1 key2 300)
; #s(key1 val1 key2 300)

Example usage:

(make-hash-table)
; #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 data ( ...))

#s(hash-table size 30 data (key1 val1 key2 300))
; #s(hash-table size 30 test eql rehash-size 1.5 rehash-threshold 0.8125 data (key1 val1 key2 300 ...))

(setq-local myht #s(hash-table size 30 data ("a" 10 "b" 20)))
; #s(hash-table size 30 test eql rehash-size 1.5 rehash-threshold 0.8125 data ("a" 10 "b" 20 ...))

Functions

https://www.gnu.org/software/emacs/manual/html_mono/elisp.html#Hash-Access

(hash-table-keys #s(hash-table data ("a" 1 "b" 2)))
; ("a" "b")

; No unique keys ???
(hash-table-keys #s(hash-table data ("a" 1 "b" 2 "a" 3)))
; ("a" "b" "a")

(hash-table-p #s("a" 1))
; nil

(hash-table-p #s(hash-table "a" 1))
; t

(hash-table-p (make-hash-table))
; t

Text properties in strings

https://www.gnu.org/software/emacs/manual/html_mono/elisp.html#Text-Props-and-Strings

For example, the following means:

#("foo bar" 0 3 (face bold) 3 4 nil 4 7 (face italic))
Part of string Slice Property
"foo" str[0: 3] bold
" " str[3: 4] NA
"bar" str[4: 7] italic

Conditionals

Equality check

See:

eq

(setq-local a 3)
(setq-local b 3)

(eq 'a 'a)
; t

(eq 'a 'b)
; nil

(eq 'a '3)
; nil

equal

(eq "2" "2")
; nil

(equal "2" "2")
; t

(equal 2 2)
; nil

Built-in functions

Math functions:

Advising functions

https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html

Change the default behaviour of an existing function ('by giving some advice') rather than redefining the whole function.

Codes for interactive prompts

First character of argument of (interactive).

https://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html

Read input from user interactively:

Function Description
read-char Reads a character. Returns int
read-int Reads int
read-string Reads string
read-key
read-char-choice
yes-or-no-p Ask a yes/no answer
y-or-n-p Ask a y/n answer

dotimes and dolist

dotimes (counter limit result) body

dolist is looping on a list.

(dolist list-name
  (operation list-elem)

is like the following python:

for list_elem in list_name:
  operation(list_elem)

Append a string to a file

Use write-region.

(write-region <string> nil <filename> 'append)

https://stackoverflow.com/questions/17376706/in-emacs-lisp-how-can-i-append-a-string-to-a-file-that-i-dont-like-to-open

Run an external command and get its output

(message (shell-command-to-string "/bin/echo -n hello"))
; Output: hello

True and false

(and t nil)
; nil

(or t nil)
; t

(if '() "T" "F")
; "F"

(if () "T" "F")
; "F"

Managing parenthesis

See:

Handling time and date

(current-time)
; (26423 47784 405993 901000)

(decode-time)
; (42 48 2 16 11 2024 6 nil 19800)
(format-time-string "%Y-%m-%d")
"2024-11-16"

(format-time-string "%Y-%m-%d" (decode-time))
"1970-01-07"
(decode-time)
; (59 6 3 16 11 2024 6 nil 19800)

(encode-time (decode-time))
; (26423 48961)

(format-time-string "%Y-%m-%d" (encode-time (decode-time)))
; "2024-11-16"

See:

car and cdr

(car '(0 1 2 3))
; 0

(car nil)
; nil

(cdr '(0 1 2 3))
; (1 2 3)

(cdr (cdr '(0 1 2 3)))
; (2 3)

nth element in a list/tuple

(nth 2 '(1 2 3))
; 3

let expressions

See: https://www.gnu.org/software/emacs/manual/html_node/eintr/let.html

let

(let 
  ((x 1)
   (y 2))
  (message (format "x=%d, y=%d" x y)))
; x=1, y=2

Each binding should be defined independent of the other.

Otherwise it will give error. For example:

(let
  ((x 1)
   (y (+ x 2)))
  (message (format "x=%d, y=%d" x y)))

; Debugger entered--Lisp error: (void-variable x)
; (+ x 2)
; (let ((x 1) (y (+ x 2))) (message (format "x=%d, y=%d" x y)))
; eval((let ((x 1) (y (+ x 2))) (message (format "x=%d, y=%d" x y))) t)
; eval-expression((let ((x 1) (y (+ x 2))) (message (format "x=%d, y=%d" x y))) nil nil 127)
; funcall-interactively(eval-expression (let ((x 1) (y (+ x 2))) (message (format "x=%d, y=%d" x y))) nil nil 127)
; call-interactively(eval-expression nil nil)
; command-execute(eval-expression)

let*

Unlike let, bindings can be defined dependent on other values mentioned in the same let* expression.

(let*
  ((x 1)
   (y (+ x 2)))
  (message (format "x=%d, y=%d" x y)))
; x=1, y=3

https://emacs.stackexchange.com/questions/42449/what-does-let-let-asterisk-mean-in-elisp

Copy whole buffer

These functions update the kill ring. To just delete text without disturbing kill ring use functions that start with kill-.

https://www.gnu.org/software/emacs/manual/html_node/elisp/Kill-Functions.html

Writing to file

https://www.gnu.org/software/emacs/manual/html_node/elisp/Writing-to-Files.html

Lisp interaction mode

Handy to try out lisp snippets. Maybe in *scratch* buffer.

https://www.gnu.org/software/emacs/manual/html_node/emacs/Lisp-Interaction.html


References:

Common lisp

May not be true for elisp.

elisp libraries

File system management

https://www.gnu.org/software/emacs/manual/html_node/elisp/Contents-of-Directories.html

References