I have been learning elisp for couple of weeks just before Steve Yegge came up with his blog on Emergency Elisp. It is a good introduction to a programming language which is bound to an editor (or the other way around?). His blog talks about the basics of elisp in a nut shell, although lot of people have pointed out mistakes in it, but I feel it must have helped people like me who are learning elisp or will do so in the future. Also if you have looked for elisp tutorials in Google you must have bumped on to “An Introduction to Programming in Emacs Lisp” or “Emacs lisp Tutorial” . The elisp manual is good for people who have played with the language for some time; but for people who want to start learning elisp it is may not be a good place to be.
So why am I writing this blog? I found no mention of buffers and windows related stuffs in Steve’s post; as a programmer I felt I should keep notes of my work (as I do in some of my blogs).
When we configure SLIME and Emacs we add some lines to our init.el file which reads some thing like:
(add-to-list ‘load-path ….)
So the first thing is how to inspect a variable in Emacs, C-h v will prompt for the name of the variable you want to check and as soon as you enter the name of the variable it shows you the content of it. Like if I have a variable foo and want to see what the value of foo is.
Similarly if we want to see the documentation of any function we can use C-h f and enter the name of the function to see its documentation.
But don’t be fooled by these “control h” stuffs as they are nothing but either elisp functions or commands which are bound to some key combinations and keymaps. One can make a function interactive with the user by a using a built in function called (interactive).
Buffers and Windows
Buffers are lisp objects which hold texts for future editing. So as soon as you start Emacs it starts in a buffer. The most famous buffer is *scratch* buffer. If you do not have any customizations or filename mentioned while opening Emacs, it opens in this buffer by default. Let’s check some buffer related functions.
Let’s try out a mini project, suppose I have to create a function which will create documents in a predefined manner. How do I do that? The format is as follows:
From there on the author will create the rest of the document.
(Note here that I am not going into a lot of details in this and please do take care of the details required before creating a real project.)
Here is a way how we can do it elisp (please feel free to comment if you have any other idea i.e. more lispy way of doing this).
C: printf(“Author:%s”, auth);
Java: System.out.println(“Author:” + auth);
Common Lisp: (format t “Author:~a~%” auth)
Same applies here also but we do the formatting as following:
(format "Author:%s " auth)
I will just quickly note what all the in build functions does and how I have used them.
- insert: This function inserts a formatted string in to the current buffer.
- newline: As the name suggest it appends a new line.
- switch-to-buffer: This function switches to a buffer and displays the buffer.
- create-file-buffer: This will create a buffer with the filename (if the buffer exists it appends some extras apropos) and returns that buffer.
- save-buffer: This function will prompt the user to save the buffer.
(Refer to elisp manual for details of these function)
Here is the code:(defun create-the-doc-template (auth &optional email company)
(insert (format "Author:%s " auth))
(if (eq email nil)
(insert "Author Email:")
(insert (format "Author Email:%s " email)))
(if (eq company nil)
(insert (format "Company:%s " email)))
(defun create-file-as-doc(filename Author &optional Auth-Email Comp)
(switch-to-buffer (create-file-buffer filename))
(create-the-doc-template Author Auth-Email Comp)
(create-file-as-doc "hello-emacs.txt" "samik" "email@example.com" "ABC")
(create-file-as-doc "hello-emacs.txt" “samik”)
Now once you execute the first call it should look like the following:
The best thing about elisp is that it allows you to hack Emacs completely and you can record a keyboard macro and use it by using a set functions and variables.
The variable last-kbd-board macro holds the last macro that you have defined and execute-kbd-macro does repeat the last macro defined by you in the current line. This can help you build a function that will repeat the macro in vary large file.
So that’s all for now…
Thanks for reading.