Emacs for beginners: elisp
Introduction
I immediately warn that I am not going to write either a textbook or an
introductory Lisp course and do not pretend to be any description complete. And
I'm going to give some useful information in my opinion that
will help beginners to use emacs to configure it as they
wish and write simple functions that can also be
used in the process of using emacs.
Emacs, as has been repeatedly mentioned everywhere, is a
combination of a text editor and a Lisp interpreter. You only need to
be able to use it.
Workplace preparation
To begin with, we will configure our emacs so that we would be comfortable
working in it .
Insert the following lines into our .emacs:
( setq auto-mode-alist
( append
' (
( "\\. El $". Lisp-mode ) ) ) ))
( global-font-lock-mode 1 )
The first set of brackets will do this that when opening a file with the
extension .el, the mode of working with
texts of Lisp programs is automatically turned on in the buffer . The last line turns on
syntax highlighting. Colors the language operators in different nice colors. It
really helps to read the program text. Now if we edited
.emacs in emacs, restart it so that it re-reads the settings file.
Here, experienced comrades suggested that the above operations are not necessary, since the necessary modes are enabled by default in modern versions of emacs.
We will try to do everything from emax. So, we need to create a
directory for our experimental files. And the file.
Suppose we are in the home directory (we mean that
we are on some kind of UNIX). Push:
| Keys | Action performed | | ----------------------- + ------------------------- ---------------------- | | Cx Cf RET | Go to dired mode in the home directory | | + Directory name RET | Create a new directory | | RET | Go to the created directory | | Cx Cf myfile.el RET | Open the file myfile.el |
The most common concepts and techniques of work
In Lisp, you need to know two concepts, variables and functions. In general, a
variable can also be considered as a function that returns the
value assigned to this variable. Variables and functions have
names - a sequence of characters. If the program is enclosed in
parentheses, it will be executed as a function. True, if this is a
variable name , the lisp interpreter will throw an error. In Lisp, names are usually
written down, separating the logical parts of the name through a hyphen. For example,
my-cool-function
Now about how functions are called. Unlike, for example, C,
functions are called as follows:
(name-of-the-function arg1 arg2 arg3 ......)
Those. function call is enclosed in brackets. The first name in parentheses is the name of the
function, the remaining names are arguments. As in C, the
arguments can be expressions that return values, for example,
calls to other functions, for example:
(name-of-the-function arg1 (name-of-the-function) arg3 ......)
This is all good, but how to work with it? In emacs, working with this is
very easy and simple. Let's try to add 2 and
2 first. Let's write the following expression in our myfile.el file:
(+ 2 2)
Let's try to decrypt it. The "+" symbol is a function that
adds up its two arguments. the next two deuces are
function arguments . Let's try to execute it. To do this, put the emacs cursor to the right of the
right bracket of our expression (close) and press Cx e. If everything is
done correctly, the number "4" will appear in the minibuffer. Hooray, we have
fulfilled our expression.
For full programming, it is customary to use
variables. Let's try to do it. First you need to assign
a value to a variable.
(set 'myvar-variable 4)
What do we have here?
| Name | Comment | | ---------------- + -------------------------------- ----------------------------------- | | set | A function that sets the first argument to the value of the 2nd | | myvar-variable | The name of the variable to which you want to assign a value | | 4 | Assigned Value | | '| Icon that indicates that the name should not return a value |
Pay attention to the stash (single quote), it does not allow the
variable name to return a value. Without it, the Lisp interpreter will
complain about the error. There is another variant of the assignment function, in
which you do not need to escape the variable name with quotation mark, setq. For this
function, the entry will look:
(setq myvar-variable 4)
Now, another convenient trick. How to see the value
assigned to a variable? To do this, put (you can directly in the
program text) the cursor to the right of the variable name and press Cx e. The
minibuffer will show the value of the variable. Please note that
if you try to do this with a variable that has a
escape quotation mark, the value will not be displayed, the escaped name
does not return a value.
Assigning a string value to a variable. Very simple. The string
is enclosed in quotation marks.
( setq myvar-str-variable "My string" )
Output the message to the minibuffer.
(message "My message")
Insert a line into the buffer at the current cursor position:
(insert "My string")
Executing a sequence of functions:
( progn
( setq my-var "Hello" )
( message my-var ) )
Defining your own functions
There is nothing easier than defining your own function. The
definition syntax is:
( defun myf-test ( ab ... )
"Documentation line"
( expression )
...
( expression ) )
In this definition:
| what | Comment | | ----------------------- + ------------------------- ----------------------- | | defun | Keyword for function definition | | myf-test | Function Name | | (ab) | Parameter List | | "Documentation line" | Documentation line. It is issued at Ch myf-test | | (expression) | Expressions performed in body functions |
Note. The function returns the value returned by its last expression.
Let's try to define our own function as an example:
( progn
( setq my-var "Hello" )
( message my-var ) )
Check:
(myf-test 1 2)
Reception of work: To minimize fuss, when debugging functions, it is convenient to use the following technique:
( progn
( defun myf-test ( ab )
"Add first argument to second. Return result"
( + ab ) )
( myf-test 1 2 )
)
If you remember, the progn statement executes a list of
expressions in its body. In our case, the function definition is first performed
, then a test case is called for execution. By changing something
in the definition, you can instantly check it.
But, there is one catch, functions defined in this way, you are not
You can call the Mx command "function name" for execution. To
ensure that it would be possible to do so, in the definition of the functions you need
to insert the statement "interactive":
( defun myf-test-1 ( )
"the Add first argument to: second Return of result."
( Interactive )
( message "the Hello" ) )
After completing this definition, and by issuing the command Mx myf-test-1 RET, you
will receive the word Hello in the minibuffer.
Note: For the names of your functions and variables, it is advisable to select
and use your unique prefix, since emacs has a global
namespace. I use eik-, first name, middle name, last name. You
you need to invent something of your own.
Conclusion
The described information from Lisp, in my opinion, is enough to start
experiments with emacs customization. Basically, customization
comes down to assigning values to emacs variables and writing
your own functions.
Literature
- On Ubuntu, the emacs22 package includes the Elisp
reference guide. Mx info, Cs elisp - Google
- Emacs Lisp Programming