elisp

elisp

August 25, 2023 | emacs, language, permanent, seedling

Summary #

tags
Emacs Lisp

Basics #

() #

anything between braces is evaluated as a function

(message "Her age is: %d" 16)        ; %d is for number

list and cons #

cons pair (aka cons cell or just cons) is a value type, and is a fundamental data structure in emacs lisp. List is made of cons pairs.

(A 2 "A")            ; A list of three elements.
()                   ; A list of no elements (the empty list).
nil                  ; A list of no elements (the empty list).
("A ()")             ; A list of one element: the string "A ()".
(A ())               ; A list of two elements: A and the empty list.
(A nil)              ; Equivalent to the previous.
((A B C))            ; A list of one element
                     ;   (which is a list of three elements).

cons #

append list at index 0

(cons 'pine '(fir oak maple))
;; returns a list, it is like append a list
(pine fir oak maple)

Roughly speaking: (a . b) is a simplified version of (cons a b) reddit ref

car and cdr #

to get first and last of cons pair

car -> first element of the list cdr -> list with first element removed

(car '(rose violet daisy buttercup))
;; returns first element
;; rose

(cdr '(rose violet daisy buttercup))
;; result
;; (violet daisy buttercup)

ref

Setting variable #

(setq my-name "Bastien")
;; (setq my-name-1 ("one" "two")) ;; invalid because () evaluates
(setq my-name-2 '("one" "two"))
(message my-name-2)

Property list or plist #

Property List

Property list (in short, plist) is a List , but to be interpreted as list of pairs, like this: ‘(key1 val1 ky2 val2 etc)

ref


;; with cl library
(require 'cl)
(getf '(:a 1 :b 2) :a)

Association list or alist #

(c-mode . ((c-file-style . "BSD")
             (subdirs . nil)))

is an association list (also known as an alist). It associates the value (c-file-style . "BSD") with the key in c-mode

Alist vs Plist #

Alist refers to an AssociationList, plist refers to a PropertyList.

An alist associates keys with values. This is done using a list of cons cells:

((key1 . value1)
 (key2 . value2)
 (key3 . value3))

A plist does the same thing, but instead of using cons cells, the key and value pairs appear in the same list, one after another:

(key1 value1
 key2 value2
 key3 value3)

ref

foo vs ‘foo vs #‘foo #

stackexchange if foo is a symbol, then ‘foo and #‘foo are completely equivalent.

Learning #

converting list to string #

(setq vetted-human-kfupm-emails
      '("hali" "mamohsin")
      )

(defvar vetted-human-kfupm-emails
      '("hali" "mamohsin")
      )

(format "%s" vetted-human-kfupm-emails) ;; to print
(message "%s" vetted-human-kfupm-emails) ;; to print
(setq query (concat "from:" (mapconcat 'identity vetted-human-kfupm-emails " OR from:")))

(setq vetted-query (concat "(maildir:/kfupm/Inbox) AND " "from:" (mapconcat 'identity vetted-human-kfupm-emails " OR from:")))
(message "%s" query)
(message "%s" vetted-query)

interactive #

to make function visible be called from emac ui

(defun jak/add-to-kfupm-vetted-emails-list (username)
  (interactive "sUSERNAME: ") ;; s -> string n-> number
;; USERNAME will be displayed in buffer
  (add-to-list 'vetted-human-kfupm-emails username)
  )

append #


(defcustom jak/boss-emails nil
  ""
  :type '(repeat string)
  )

(defcustom jak/people-i-know-at-kfupm  nil
  ""
  :type '(repeat string)
  )

(defcustom jak/ictc-colleagues-emails nil
  ""
  :type '(repeat string)
  )

(defcustom jak/kfupm-colleagues-emails nil
  ""
  :type '(repeat string)
  )
  (setq jak/people-i-know-at-kfupm (append jak/boss-emails jak/ictc-colleagues-emails jak/kfupm-colleagues-emails))

(message "%s" jak/people-i-know-at-kfupm)

(defun jak/set-people-i-know-at-kfupm()
  (setq jak/people-i-know-at-kfupm (append jak/boss-emails jak/ictc-colleagues-emails jak/kfupm-colleagues-emails))
(message "%s" jak/people-i-know-at-kfupm)
  )
(jak/set-people-i-know-at-kfupm)

saving variable pro grammatically to custom-file #

(defun jak/add-to-boss-emails (username)
  (interactive "sBOSS_USERNAME: ")
  (add-to-list 'jak/boss-emails username)
  (customize-save-variable 'jak/boss-emails jak/boss-emails) ;; ***** setq and
;; from here:https://stackoverflow.com/a/17473323/5305401
;; tried setq and other ways of settings variables
;; they did work: user variables were not getting stored
;; https://macowners.club/posts/setq-vs-customize-set-variable/
  (jak/set-people-i-know-at-kfupm)
  (jak/set-boss-query)
  )

Custom variables #

init configuration variables that can be edited and Emacs stores the customization.

defcustom #

defcustom builds on defvar. It tells emacs that it is a variable, and it allows the developer to create a custom interface to set the value. The developer can say, things like “this variable can contain only the value ‘foo or ‘bar”. more ways to setting variables how to customize using easy customization interface

Common Constructs and usages #

Expanding file names #

(setq notdeft-xapian-program  (expand-file-name ".emacs.d/straight/repos/notdeft/xapian/notdeft-xapian" "~"))
(message (expand-file-name ".emacs.d/straight/repos/notdeft/xapian/notdeft-xapian" "~"))

Formatting strings #

(setq org-default-notes-file-path (format "%s/%s" org-base-path "notes.org")
      todo-file-path              (format "%s/%s" org-base-path "gtd.org")
      journal-file-path           (format "%s/%s" org-base-path "journal.org")
      today-file-path             (format "%s/%s" org-base-path "2010.org"))

Dictionary or hastable #

http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node154.html

(setq a (make-hash-table))
(setf (gethash 'color a) 'brown)
(setf (gethash 'name a) 'fred)
(gethash 'color a) => brown
(gethash 'name a) => fred
(gethash 'pointy a) => nil

4 ways to implement dictionary #

stackoverflow Common Lisp has at least four different ways to do that (key value storage):

property lists #

(:foo 1 :bar 2)

assoc lists #

((:foo . 1) (:bar . 2))

hash tables #

(setq a (make-hash-table))
(setf (gethash 'color a) 'brown)
(setf (gethash 'name a) 'fred)
(gethash 'color a) => brown
(gethash 'name a) => fred
(gethash 'pointy a) => nil

CLOS objects #

CLOS objects (slot-value foo ‘bar) to get and (setf (slot-value foo ‘bar) 42) to set. The slot name can be stored in a variable: (let ((name ‘bar)) (slot-value foo name)) .


Go to random page

Previous Next