# Xah Teaches Emacs Lisp

By Xah Lee. Date: .

Learn elisp with me.

go here: Practical Emacs Lisp

start at the elisp basics.

if you have question, comment, i'll help out.

next few weeks, i'll go thru them systematically, and add/enhance stuff. (they are up-to-date as is, because i update them every week.)

Learn elisp with Xah. Today, read this. Elisp: Overview of Coding Emacs Lisp

today, read this. Elisp: Simple Code Examples

try some of the examples. And try to lookup some of the function's doc.

### elisp exercise: print prime

I'm going to start to give quiz. Today's quiz:

write a function that prints the first 10 prime numbers in messages buffer. (e.g. 2, 3, 5, 7, 11, 13, 17, 19, 23…)

you should know how to do this now. Do it.

if stuck, ask me question at some page's comment.

Elisp: Show Function/Command Documentation

Quiz of the day: how many functions contains the word “inline” in their names?

### solution to print list of prime numbers

Bobby Casey gave a very nice solution to the list of prime quiz.

```;; quick solution to print first 10 primes
;; by Bobby Casey, 2016-09-03

(require 'cl-seq)

(defun prime-p (num)
(if (zerop (count-if 'zerop
(mapcar (lambda (a) (mod num a))
(number-sequence 2 (sqrt num)))))
num))

(delq nil (mapcar 'prime-p (number-sequence 2 30)))```

This is not prime sieve as i'd expected, but still, nice simple solution good for this exercise.

Can you modify the code so it doesn't call Common Lisp, or, use the more efficient prime sieve algorithm?

the prime testing function can be written this way, without the Common Lisp lib:

```(defun prime2-p (num)
"Returns t if num is a prime number."
(catch 'bbb
(mapc
(lambda (x)
(when (zerop (mod num x))
(throw 'bbb nil)))
(number-sequence 2 (sqrt num)))
t
))```

today's #Emacs Lisp is to learn a few most useful ways to search elisp doc Elisp: Search Documentation

exercise 1: using emacs, find the elisp manual page for the function “point”.

exercise 2: there's a function you can't remember about checking if a file exist. Find it by using emacs search features.

exercise 3: hooks are variables. List all variables whose name contains “hook”.

### Prime Sieve in Emacs Lisp, Common Lisp Style

professor John Kitchin gave a helping hand and wrote a version of prime sieve for our elisp exercise.

```;; print primes, using sieve algorithm
;; code by John Kitchin , 2016-09-06

(require 'cl-macs)

(let* ((N 100)
(A (loop for i from 0 to N collect t))
j)
(loop for i from 2 to (sqrt N)
if (elt A i)
do
(catch 'done
(loop for k from 0
do
(setq j (+ (* i i) (* k i)))
(if (<= j N)
(setf (elt A j) nil)
(throw 'done nil)))))

(setq x (loop for i in (number-sequence 2 N) if (elt A i) collect i))
)

(prin1 x)```

It is heavily Common Lisp style.

If you know elisp but not Common Lisp, you can learn something from it, its use of CL loop.

but for beginner emacs lisp programers, you may want to try writing a more basic emacs lisp version. Try it. You'll learn a lot.

Emacs: How to Edit Lisp Code

today's elisp lesson, an easy and fun one. How to Edit Lisp Code with Emacs. Try, experiment with it.

### elisp lesson: common functions

today's elisp learning is to read these pages:

you get to be familiar of the very basic functions you need to call.

again, try to evaluate many of them. Also, from previous day's lesson, you know how to find their doc string or in manual.

Ok, here's exercise:

exercise 1: read the doc string for the function `region-beginning`.

exercise 2: find the page in manual of the function `region-beginning`, using elisp doc searching techniques. Not Google! Read that page.

exercise 3: write a command my-forward-2-words that moves cursor forward by 2 words. (bind it to key M-f or M-right) Use it, and see if you like it better.

exercise 4: write a command open-my-file that opens a file you use frequently. Give this command a key F8. Use it for a week and see if you like it.

ok. quite a few exercises. They should be not hard. Review the pages covered in previous days if you have trouble. Again, ask question on twitter, Google Plus, or comment on page, i'll help out.

today's emacs lisp, read this. Elisp: Mark, Region, Active Region

It's critical to understand Region, Active Region, transient-mark-mode. For writing many commands, you'll be working with text selection.

today's exercise.

exercise 1: write a command “my-select-line” that select the current line. Hint: go to beginning of line (`beginning-of-line`), mark, then move cursor to end of line.

exercise 2: write a command “select-text-in-quotes” that select text in quotes. Hint: use `search-backward` to go to left quote, mark, `search-forward` to move cursor to right quote.

if you get stuck, remember to look at: Elisp: Simple Code Examples

Elisp: How to Write Commands

today's elisp lesson, read that page.

Elisp: Cut Copy Paste, kill-ring

that's today's elisp lesson. read that.