index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

02 March 2016 Emacs lisp snippets

cl-loop for in

(cl-loop for day in '("mon" "tue" "wed" "thu" "fri" "sat" "sun")
         do (print day))

"mon"

"tue"

"wed"

"thu"

"fri"

"sat"

"sun"

cl-loop for from to

(cl-loop for x from 1 to 5
         do (print x))

1

2

3

4

5

pcase literal matching

(pcase "word"
  ('word (message "Matched 'word symbol"))
  ("word" (message "Matched \"word\" string")))
Matched "word" string

Avoid nesting with the help of thread-first and thread-last.

(thread-last "12.....34"
  (string-remove-prefix "1")
  (string-remove-suffix "4"))
2.....3

Find file upwards, up parents, up hierarchy

(locate-dominating-file FILE NAME)

Find executable in PATH

(executable-find COMMAND)

Read string with completion (helm/ido/ivy friendly)

(completing-read PROMPT COLLECTION &optional PREDICATE REQUIRE-MATCH INITIAL-INPUT HIST DEF INHERIT-INPUT-METHOD)

Execute command/process and return list (similar to shell-command-to-string)

(process-lines PROGRAM &rest ARGS)

Iterating org buffer

(org-element-map (org-element-parse-buffer) '(headline link)
  (lambda (element)
    (cond
     ((and (eq (org-element-type element) 'headline)
           (= (org-element-property :level element) 1))
      (print "headline"))
     ((eq (org-element-type element) 'link)
      (print "link")))
    nil))