📜 ⬆️ ⬇️

Emacs for beginners: elisp

Introduction


Immediately I warn you that I am not going to write either a textbook or
introductory course Lisp and do not pretend to have any kind of description. BUT
I am going to give some useful information in my opinion
help beginners use emacs to customize it
discretion and write simple functions that can also be
use in the process of using emacs.

Emacs, as has been repeatedly mentioned elsewhere, is
a combination of a text editor and a Lisp interpreter. Only need
be able to use it.

Workplace preparation

')
To begin with, we will tune our emacs so that we would be comfortable in it.
work.

Paste 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 make it so that when opening a file with
extension .el, the buffer mode automatically turns on
Lisp texts. The last line turns on the backlight
syntax. Paints language operators in different nice colors. it
really helps to read the text of the program. Now if we edited
.emacs in emacs, restart it to reread the settings file.

Here experienced comrades suggested that these operations are not necessary, since the necessary modes are included in modern versions of emacs by default.


We will try to do everything from emax. So, we need to create
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). Click:

 |  Keys |  Produced action |
 | ----------------------- + ------------------------- ---------------------- |
 |  Cx Cf RET |  Switch to dired mode in your home directory |
 |  + Directory name RET |  Create a new directory |
 |  RET |  Go to the directory created |
 |  Cx Cf myfile.el RET |  Open the file myfile.el |


The most common concepts and methods of work


In Lisp you need to know two concepts, variables and functions. Actually
a variable can also be viewed as a function that returns
the value assigned to this variable. Variables and functions have
Names - a sequence of characters. If the program enclose the name in
parentheses, it will execute as a function. True if this name
variable, the Lisp interpreter will generate an error. In lispe names taken
write, separating the logical parts of the name with a hyphen. for example
my-cool-function

Now how functions are caused. Unlike, for example, C,
functions are called as follows:

 (name-of-the-function arg1 arg2 arg3 ......)


Those. the function call is enclosed in brackets. The first name in brackets is the name
functions, other names are arguments. Just as in C, as
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 ......)


It's all good, but how to work with it? In emacs work with this
very easy and simple. Let's first try to add 2 and
2. Write the following expression in our file myfile.el:

 (+ 2 2)


Let's try to decrypt it. The "+" symbol is a function,
folding her two arguments. the next two twos are arguments
functions. Let's try to execute it. To do this, put the cursor emacs right
right bracket of our expression (close) and click Cx e. If everyone
done correctly, the number "4" will appear in the minibuffer. Hooray we
fulfilled our expression.


For full programming, it is customary to use
variables. Let's try to do it. First you need to assign
variable value


 (set 'myvar-variable 4)


What do we have here?


 |  Name |  Comment |
 | ---------------- + -------------------------------- ----------------------------------- |
 |  set |  Function assigning the first argument to the value of the 2nd |
 |  myvar-variable |  The name of the variable to which you want to assign the value |
 |  4 |  Assigned value |
 |  '|  Icon that indicates that the name should not return value |


Pay attention to the stash (single quote), it does not give a name
variable return value. Without it, the Lisp interpreter will
complain about an error. There is another variant of the assignment function, with
which does not need to escape the variable name with a quotation mark, setq. For this
Record function will look like:


 (setq myvar-variable 4)


Now, another convenient way to work. How to see the value
assigned to a variable? For this you need to put (you can right in
program text) the cursor to the right of the variable name and press Cx e. AT
the value of the variable will be shown to the minibuffer. note that
if you try this with a variable that has
escape quote, value will not be shown, escaped name
value does not return.

Assigning a variable to a string value. Very simple. Line
is in quotes.

( setq myvar-str-variable "My string" )

Displays a message in the minibu er.

 (message "My message")


Inserting a string into the buffer at the current cursor position:

 (insert "My string")


The sequence of functions:

( progn
( setq my-var "Hello" )
( message my-var ) )

Definition of eigenfunctions


Nothing is easier than defining your own function. Syntax
The definitions are:

( defun myf-test ( a b ... )
"Documentation String"
( expression )
...
( expression ) )

In this definition:

 |  what |  Comment |
 | ----------------------- + ------------------------- ----------------------- |
 |  defun |  Keyword to determine the function |
 |  myf-test |  Function Name |
 |  (ab) |  List of parameters |
 |  "Documentation String" |  A string of documentation.  Issued when Ch myf-test |
 |  (expression) |  Expressions executed in the function body |


Note. The function returns the value returned by its last expression.

For example, let's try to define our own function:

( progn
( setq my-var "Hello" )
( message my-var ) )

Check:

   (myf-test 1 2)


Reception of work: In order to have less fuss, when debugging functions, it is convenient to use the following technique:


( progn
( defun myf-test ( a b )
"Add first argument to second. Return result"
( + a b ) )
( myf-test 1 2 )
)

If you remember, the "progn" statement executes the list
expressions in his body. In our case, we first define
function, then the test example is called for execution. Changing something
in the definition, you can instantly check it out.

But, there is one odd thing, the functions defined in this way, you do not
You can invoke the command Mx "function name". For
In order to do this, the function definition needs
insert operator "interactive":

( defun myf-test- 1 ( )
"Add first argument to second. Return result"
( interactive )
( message "Hello" ) )

By completing this definition and submitting the command Mx myf-test-1 RET, you
get Hello in the minibuffer

Note: For the names of your functions and variables, it is advisable to select
and use your unique prefix, since in emacs the global
namespace. I use eik-, by first name, last name. You
need to invent something of their own.

Conclusion


The information from Lisp, in my opinion, is enough for a start.
experimenting with emacs customization. Mostly customization and
comes down to assigning values ​​to emacs variables and writing
own functions.

Literature


  1. In Ubuntu, the emacs22 package comes with
    Elisp reference manual. Mx info, Cs elisp
  2. Google
  3. Emacs Lisp Programming

Source: https://habr.com/ru/post/61992/


All Articles