Introduction
In this lab we'll go through some of the special things that can be done in Elisp. More infprmation about it here.
Ex. 1. a. Macros
Open an Emacs window and select the buffer "scratch". Verify that you are in Lisp Interaction mode.
Write a small function that takes 1 parameter and adds 1 to it, then assigns the result to it (with setq). Define a variable having an integer value. Call this function on it, then check the content of the variable afterwards (no surprise here).
Let's start with the following macro:
(defmacro inc (var) (list 'setq var (list '1+ var)))Evaluate this macro. Call this macro with the variable used above as a parameter (just like any other function). What happened to the value of the variable afterwards?
Check what the symbol function is for the macro.
Define a second macro with a simple (setq ...) statement (as in the first function) instead of the list. Redo the experiment wit the variable using it. What is the result?
More operations to evaluate. Try to understand the logic of each result.
Call the function (macroexpand '(inc n)). What is the result? What does this function do?
b. Interacyive functions
Define a simple function called "hello" with no parameters and make it print "Hello World" or any other message. You can use princ for it. Make this function interactive by adding (interactive) after the header of the function, in the place where you would add its description. Evaluate and call this function first, then invoke it with Meta-x followed by its name.
Define the following interactive function:
(defun move2 () (interactive) (forward-word 2))Evaluate it and use it interactively (you may have to move the cursor up in the browser to see the result). What does it do?
Repeat the exercise with the following function:
(defun move-line (L) (interactive "nNr of lines:") (forward-line L))
c. Buffers Save the scratch file as a regular lisp file, like lab2.el or macro.el. Switch back to interactive mode.
Copy and evaluate the following expression:
(setq B (current-buffer))
Call the functions buffer-name, buffer-modified-p, and save-buffer with this variable as the parameter. What happens after you call the last one and why does the buffer still look modified after that?
Call the function buffer-list with no parameters. What do you get?
Add the following function:
(defun save-all ()
(interactive)
(let ((BL (buffer-list)))
(mapc 'save-buffer BL)))
On your own: the function string-match taking 2 parameters, both of
them strings, tries to match the first one in the second. If there is
a match, then it returns the first index of the match starting from
0. If there is no match, it returns nil. For example,
(string-match "el" "macro.el")
returns 6.
Note that the first string in this function is in fact a regular expression. More on the regular expressions here
Modify the function above so that it only saves the files having an extension, meaning those for which the name contains "." as a substring. For this purpose, replace 'save-buffer' in the above with a lambda expression, verifying the condition, and only calling save-buffer if the condition is true. Invoke the function in interactive mode. Your buffer should be saved now.
Turn in: Save the file containing the Lisp code and the results of the evaluation. You can send it by email for 2 extra credit points.