Skip to content

Latest commit

 

History

History
755 lines (589 loc) · 10.1 KB

introduction-to-emacs-lisp.org

File metadata and controls

755 lines (589 loc) · 10.1 KB

Introduction To Emacs Lisp

Notice that this is just a practice and it has no value. This is a way for me to get familiar with git/magit/elisp. The practices are based on the video series by System Crafters youtube channel. I just type what he was talking about to get more focused on the topic. For more details see: Learning Emacs Lisp (by System Crafters)

Learning Emacs Lisp #1

(defun the-meaning-of-life (answer)
  (message "The answer is %s" answer))
(list 1 2 3
      4 5 6
      7 8 9)

Lisp Types

  • Strings
  • Numbers (both integer and floeag)
  • Symbols
  • Cons Cells
  • Arrays and Vectors
  • etc…

Emacs Specific Types

  • Buffers
    (switch-to-buffer (other-buffer))
        
  • Windows
  • Frames
  • Threads
  • Keymaps
  • etc…

Forms and Evaluation

How evaluation works

Different for:

  • Lists
  • Symbols
  • Other objects

self evaluating

;number
777
;string
"This is a string"

not self evaluating

buffer-file-name

evaluates a function

(+ 300 11)
(300 11)

result: invalid function 300

Some things cannot be avaluated

Symbols

It is an object, but it’s not self-evaluating.

  • Convert from one type to another: bui-keyword->symbol
  • Define a namespace for the simbol: efs/some-name
  • Indicates a global variable: *pcache-repositories*
  • Check if something is equal to something else: string=

Evaluated symbol returns its value

buffer-file-name

Function names cannot be evaluated:

get-file-buffer

results: eval: Symbol’s value as variable is void: get-file-buffer

Infix vs Prefix

(+ 300 (- 12 1))

Compared to a conventional programming language: (300 + 11) * 50

Exercises

;number
7

;another list definition
(list 1 2 3)
(car (car '(1 2 3)))
;vector
[1 2 3]
(stringp "Is this a string?")

(eq 3.1 3.1)
(eq "string" "string")
(eq '(1 2 3) '(1 2 3))
(setq test-val '(1 2 3))
(eq test-val test-val)

eql

(eql 1 1)
(eql 3.1 3.1)
(eql "string" "string")
(eql '(1 2 3) '(1 2 3))
(setq test-val '(1 2 3))
(eql test-val test-val)

equal

(equal 1 1)
(equal 3.1 3.1)
(equal "string" "string")
(equal '(1 2 3) '(1 2 3))
(setq test-val '(1 2 3))
(eq test-val test-val)

(% 11 5)
(mod 11.1 5)

Incrementation function (for loops, e.g.)

(1+ 5)
(1- 5)

Convert between ingeters and floats:

  • truncate
  • round
  • floor
  • ceiling
(truncate 1.2)
(truncate -1.2)
(floor 1.2)
(floor -1.2)
(ceiling 1.2)
(round 1.5)

Predicates

(integerp 1)
(numberp "text")

Comparison

(= 1 1.0)
(max 1 5 6 4 2)
(min 34 65 2 -123)

Characters

?A

Unicode:

?\N{U+E0}

Control and meta char syntax:

?\C-c
(kbd "C-c")

Comparisons

(char-equal ?A ?A)

Sequences

Strings

(make-string 5 ?a)
(string ?H ?e ?l ?l ?o ?!)
(string= "hello" "hello")
(string< "bello" "hello")
(string> "hello" "bello")

Operations

(substring "hello" 0 4)
(concat "hello " "bello")

This is based on a pattern. ???

(split-string "Hello bello" "[o]")

Formatting

Cons Cells

(cons 1 2)
(cons 1 2)
(car '(1 . 2))

Arrays

(setq some-array [1 2 3 4])
(aset some-array 1 5)
some-array
(setq some-array "Hello!")
(aset some-array 3 ?b)
some-array

Logic Expressions

(if t 'true 'false)
(if 5 'true 'false)
(if "text" 'ture 'false)
(if nil 'ture 'false) ; false - nil
(not t)
(not 3)
(not nil)
(and t t t t 'foo) ; 'foo
(and t t t 'foo t)
(and nil 'something) ; nil
(or nil 'something) ; 'something
(or nil 'something t) ; 'something
(or (- 3 3) (+2 0))

Conditional Expression

(if t 5
  (message "true branch")
  (+ 2 2))
(if t ; nil
    (progn
      (message "test")
      5)
  (message "someting else")
  (+ 2 2))
(when (> 2 1) 'foo) ; foo
(unless (> 2 1) 'foo) ; nil
(user-login-name)
(setq a 1)
(setq a 2)
(setq a -1)

(cond ((eql a 1) "Equal to 1")
      ((> a 1)   "Greater than 1")
      (t         "Something else!"))

Loops

(setq my-loop-counter 0)

(while (< my-loop-counter 5)
  (message "I'm looping! %d" my-loop-counter)
  (setq my-loop-counter (1+ my-loop-counter)))
(dotimes (count 5)
        (message "I'm looping with dotimes. %d" count))
(dolist (item '("one" "two" "three" "four" "five"))
        (message "Item %s" item))
(defun efs/recursion-test (counter limit)
  (when (< counter limit)
    (message "Recursion: %d" counter)
    (efs/recursion-test (1+ counter) limit)))
(efs/recursion-test 0 5)

Defining Functions and Commands - Learning Emacs Lisp #3

Function is a reusable piece of code.

Defining a function

(defun do-some-math (x y)
  (* (+ x 20)
     (- y 10)))
(do-some-math 100 50)

Function arguments

Optional argument

(defun multiply-maybe (x &optional y z)
  (* x
     (or y 1)
     (or z 1)))
(multiply-maybe 5)
(multiply-maybe 5 2 10)

Rest arguments

(defun multiply-many(x &rest operands)
  (dolist (operand operands)
          (when operand
               (setq x (* x operand))))
  x)
(multiply-many 5 10)

Documentation function

after defun

Function without names

(lambda (x y)
  (+ 100 x y))
((lambda (x y)
  (+ 100 x y))
 10 20)

Invoking functions

Usual way:

(+ 2 2)
(funcall '+ 2 2)
(defun gimmie-function (fun x)
  (message "Function: %s -- Result: %d"
           fun
           (funcall fun x)))
(gimmie-function)