Emacs lisp tuto

Table of Contents

This file contains some examples of Emacs lisp prog. Main informations are comming from An Introduction to Programming in Emacs Lisp

For memo: After :results place "value" to catch the returned value (with "output" the evaluation of the code returns nothing or errors when it's the case)

Interactive Lisp mode

Alt-X ielm : enter in interactive list mode Scatch buffer : after an expression type C-j to execute last expression.

Some basic tests

Simple test with text

(print '(Hello world!))

(Hello world!)

Simple test with numbers

(princ (+ 1 2))
3

Some complemenary and elementary tests

CAR

(princ (car '(rose violet daisy buttercup)))
rose

CDR

(princ (cdr '(rose violet daisy buttercup)))
(violet daisy buttercup)

CONS

(princ (cons 'pine '(fir oak maple)))
(pine fir oak maple)

Global test

(let ((dog (sqrt 2))
      (cat 7))
  (print (format "%s %f" "Dog: " (eval dog)))
  (print (format "%s %d" "Cat: " (eval cat)) nil)
  (print "Fish."))

"Dog:  1.414214"

"Cat:  7"

"Fish."

Other tests

Set

Setq is to avoid to use '

(set 'flowers '(rose violet daisy buttercup))
(print flowers)
flowers

(rose violet daisy buttercup)

Buffers name

(buffer-file-name)
;; (buffer-name)
;; (current-buffer)

Switch buffer

(switch-to-buffer (other-buffer))

Buffer size

(buffer-size)

Functions

Defintion

(defun FUNCTION-NAME (ARGUMENTS…) "OPTIONAL-DOCUMENTATION…" (interactive ARGUMENT-PASSING-INFO) ; optional BODY…)

(defun multiply-by-seven (number)
      "Multiply NUMBER by seven."
      (* 7 number))
(multiply-by-seven 7)

Interactvie

(defun multiply-by-seven (number)       ; Interactive version.
  "Multiply NUMBER by seven."
  (interactive "p")
  (message "The result is %d" (* 7 number)))
Mon Blog — généré avec Emacs Org-mode