Xah Talk Show 2022-09-07 Emacs Lisp for Beginner

xah talk show 2022-09-07 2xQj
xah talk show 2022-09-07 2xQj

2 parts to emacs lisp learning.

  1. Emacs lisp items common to every programing language. e.g. string, variable, function, boolean, flow control, loop, datatypes, input/output.
  2. Emacs specific emacs lisp, such as text manipulation, cursor, buffer, major/minor mode, syntax coloring, gui window/frame, etc.

for Emacs specific emacs lisp, there are 2 parts:

  1. text processing. e.g. write command to manipulate text, insert, delete, move, refactor code, template, etc.
  2. emacs system. e.g. font, syntax coloring, minor mode, major mode, manipulate window/frame size, write a dir viewer, pdf viewer, image viewer, chat interface, email client, etc.

some syntax shortcuts

(equal 3 3)

(equal '(a b) (quote (a b)))

go thru the tutorial

lexical scope vs dynamic scope

Lexical scope, basically means, text based variable scope. Meaning, what you see is what you get. Meaning, that a variable's scope is inside a syntactic unit of the language, such as paren or curly bracket.

dynamic scope, is basically, a time based scope.

LISP, originally stand for list processor

;;; lexical-binding: t; -*-

lexical-binding

(+ 2 3)

2

(+ 2 (+ 3 1))

'(3 2)
(quote (3 2))

;; (f a b c)

(message "hello world factorial" )

; this is a comment

(message "hello world factorial" )

(insert "abc")
;; abc

(insert "something more fancy\nok?")
;; something more fancy
;; ok?

(length "abc")

(length "🤩")

;; python "double"
;; 'single'
;; """triple"""
;; '''triple'''
;; JavaScript template quote  `t`

(substring STRING FROM &optional TO)

(substring "abcdefg" "1" )

(substring "abcdefg" 1 )

(substring "abcdefg" 3 )

(equal
 (substring "abcdefg" 3)
 "defg"
 )

(format "hello" )

(format "hello i have %s cats" 34 )

(format "%s" 34 )

(string-to-number "3")

(number-to-string 3)

(/ 7 2)
(/ 7 2.0)

(eq nil
    (list))

(eq nil
    '()
)

nil
'nil

(eq nil 'nil)

(or nil 3)

(eq t 't )

(if t "yes" "no")

(if 0 "yes" "no")
(if "" "yes" "no")
(if [] "yes" "no")

;; variable

(setq x 3)

;; x

(equal x 3)

(let (x)
  (setq x 4)
  (message "%s" x))

(if (> x 2)
    (message "yes, bigger than 2")
  (insert "wow")
  (message "no. it is smaller than 2"))

(if (> x 2)
    (progn
      (message "yes, bigger than 2")
      (insert "wow"))
  (message "no. it is smaller than 2"))

;; loop

(while (< x 9)
  (message "%s" x)
  (setq x (+ x 1)))

(list 3 4 5)

(list 3 (list 3 4 5) 5)

(length (list 3 4 5))

(append
 (list 3 4 5)
 (list "a" "b"))

(equal
 (list 3 (list 3 4 5) 5)
 (list 3 (list 3 4 5) 5))

(mapcar
 (lambda (x) (+ x 1))
 (list 3 4 5))

(defun ff (x)
  "add 2 to x"
  (let ()
    (+ x 2)))

(ff 3)

byte compile emacs lisp