16

When learning a new programming language, "read source code" is a common advice received by the experts. However, with such a huge system like emacs, build over decades by many people, it is not so easy for the beginner to figure out which libraries are the best examples of idiomatic Elisp progamming. Therefore I would like to get some recommendations with regards to the following criteria:

  • idiomatic elisp programming style

  • adherence to emacs programming guidelines

  • clever use of the cl-package in accordance to emacs guidelines

  • clever use of eieio-package

  • exemplary organisation of a single .el file library

  • exemplary organisation of a multi-file/multi-package library

  • any criteria you consider important

Giving the reasons why you recommend certain libraries for auto-didactic studies would certainly be informative to beginners in Emacs lisp programming.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Thorsten
  • 3,451
  • 3
  • 20
  • 25
  • There are so many well-written Emacs packages, so it's almost impossible to pick one over the other. I would suggest that you first decide what kind of package you would like to write and then study packages that similar things. For example, if you plan to write a minor mode, study how other minor modes have been implemented. – Lindydancer Mar 13 '11 at 14:16
  • Maybe I should reformulate the question a bit: since the ultimate aim to learn Elisp programming might be the contribution of one's own major and minor modes, it would be nice to have one or two exemplary contributed modes of each kind as role models - with reassured code quality. Of course Emacs has a lot of quality modes, and picking one doesn't mean the others are less good. However, since the beginner doesn't know neither the programming people and their reputation nor the criteria to judge the code, a few recommendations by the experts would be helpful. – Thorsten Mar 13 '11 at 15:45

2 Answers2

4

As Trey said, use the elisp from Emacs itself to learn. I usually do one of two things:

1) If I'm trying to find out how to use a specific function, I'll bring up a dired window and do:

% g foobar

where foobar is the name of the function I'm interested in learning how to use. That will mark all of the files that use foobar and then I go and search through them to see how to call it, in what context it's being called, etc.

2) If I'm trying to figure out how to do something, and I know a mode that does something similar, I'll go look at that mode's source code to see how they do it. If you don't know where to start, but you know how to execute what it is you want to do, a handy thing to do is to look it up by key-binding. For example, in a dired buffer, do the following:

C-h k % g

and that will bring up:

% g runs the command dired-mark-files-containing-regexp, which is an
% interactive compiled Lisp function in `dired.el'.

It is bound to % g, <menu-bar> <regexp> <mark-cont>.
(dired-mark-files-containing-regexp regexp &optional marker-char)

Mark all files with contents containing regexp for use in later
commands. A prefix argument means to unmark them instead.
`.' and `..' are never marked.

Clicking on dired.el in the above text (in an emacs buffer) will open up the source code and take you right to that function definition. It's a great way to learn by example.

Don't forget the elisp debugger as a way to see exactly what's going on and following along as the code executes step-by-step. You can mark the code for debugging using edebug-defun and then invoke it as usual, at which point emacs will step you through the code.

Joe Casadonte
  • 15,888
  • 11
  • 45
  • 57
  • 1
    I begin to see how all the things that come handy for the Emacs user makes it a great IDE for the Elisp-programmer. – Thorsten Mar 13 '11 at 16:12
3

The source code I'd recommend would be that of Emacs itself. Start off with the simple stuff (pun intended), and look at other files as you are wont.

M-x find-library simple RET

I think it's self-explanatory as to why Emacs' own lisp code is a good example of Emacs lisp.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • I just discovered "An Introduction to Programming in Emacs Lisp" by Robert J. Chassell (available as info-node) that takes a similar approach and walks through some 'simple' elisp functions from Emacs own lisp code with a lot of explanations. – Thorsten Mar 13 '11 at 16:08
  • @Thorsten, sure that's a good introduction, but doesn't have a lot of source code to look through. For those looking for it, "An Introduction to Programming in Emacs Lisp" can be found here: http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/index.html – Trey Jackson Mar 13 '11 at 18:12
  • Are there any functions you'd point to as particularly good examples? After reading `describe-function-1` (in `help-fns.el`), for example, I could not advocate it as a model of programming style. It's worth studying, but I think that for new code, we can do better. – harpo Feb 11 '12 at 18:23