UP | HOME

ELisp

1 syntax

2 writing interactive functions

From this useful Utah resource:

(defun multiply-by-seven (number)       ; Interactive version.
  "Multiply NUMBER by seven."
  (interactive "p")
  (message "The result is %d" (* 7 number)))
  • Use C-x C-e to execute the function
  • Now, I can call it with M-x multiply-by-seven

A nice list of examples here.

The first ELisp functions I ever wrote:

(require 'subr-x)
(defun format-daily-link (pathName fileName)
  "format a daily notes link"
  (format "[[%s][%s]]" (concat pathName "/" fileName) (file-name-sans-extension fileName)))
(defun get-daily-description (pathName filePath)
  "Return a list of lines of a file at filePath."
  (with-temp-buffer
    (insert-file-contents (concat pathName "/" filePath))
    (mapconcat (lambda (x) (concat "-" (string-trim x "*+")) ) (cl-remove-if-not (lambda (x) (string-match "^\*" x)) (split-string (buffer-string) "\n" t)) "\n")))
(defun insert-daily-links ()
 "Insert a list of all daily notes with links. For some reason there needs to be text in the file when this is called. 
 I think that has something to do with it being interactive."
 (interactive)
 (let* ((path "/home/czw/org-roam/daily/lab")
         (dailies (cl-remove-if-not (lambda (x) (string-match "^[0-9].*\.org$" x)) (directory-files path))))
    (mapconcat (lambda (x) (concat (format-daily-link path x) 
                                   "\n"
                                   (get-daily-description path x)) dailies) "\n"))

3 using with-temp-buffer

Use this macro to set up a temporary buffer and fill it with file contents. (from here)

(defun read-lines (filePath)
  "Return a list of lines of a file at filePath."
  (with-temp-buffer
    (insert-file-contents filePath)
    (split-string (buffer-string) "\n" t)))

4 see also

Created: 2021-09-16 Thu 00:50