Êíèãà: Learning GNU Emacs, 3rd Edition
9.8 The Lisp Modes
Emacs has three Lisp modes, listed here by their command names:
emacs-lisp-mode
Used for editing Emacs Lisp code, as covered in Chapter 11 (filename .emacs or suffix .el).
lisp-mode
Used for editing Lisp code intended for another Lisp system (suffix .l or .lisp).
lisp-interaction-mode
Used for editing and running Emacs Lisp code.
All three modes have the same basic functionality; they differ only in the support they give to running Lisp code.
All three Lisp modes understand the basic syntax elements common to all language modes. In addition, they have various commands that apply to the more advanced syntactic concepts of S-expressions, lists, and defuns. An S-expression (or syntactic expression) is any syntactically correct Lisp expression, be it an atom (number, symbol, variable, etc.), or parenthesized list. Lists are special cases of S-expressions, and defuns (function definitions) are special cases of lists. Several commands deal with these syntactic concepts; you will most likely become comfortable with a subset of them.
Table 9-11 shows the commands that handle S-expressions.
Table 9-11. S-expression commands
Keystrokes | Command name | Action |
---|---|---|
C-M-b | backward-sexp | Move backward by one S-expression. |
C-M-f | forward-sexp | Move forward by one S-expression. |
C-M-t | transpose-sexps | Transpose the two S-expressions around the cursor. |
C-M-@ | mark-sexp | Set mark to the end of the current S-expression; set the cursor to the beginning. |
C-M-k | kill-sexp | Delete the S-expression following the cursor. |
(none) | backward-kill-sexp | Delete the S-expression preceding the cursor. |
Since an S-expression can be a wide variety of things, the actions of commands that handle S-expressions are determined by where your cursor is when you invoke them. If your cursor is on a ( or on a space preceding one, the S-expression in question is taken to be the list that starts with that (. If your cursor is on some other character such as a letter or number (or preceding whitespace), the S-expression is taken to be an atom (symbol, variable, or constant).
For example, suppose your cursor is in this position:
(mary bob (dave (pete)) ed)
If you type C-M-f, the cursor moves like this:
(mary bob (dave (pete)) ed)
That is, the cursor moves forward past the S-expression (dave (pete)), which is a list. However, say your cursor is positioned like this:
(mary bob (dave (pete)) ed)
When you type C-M-f, it moves here:
(mary bob (dave (pete)) ed)
In this case, the S-expression is the atom bob.
The commands moving in lists are shown in Table 9-12.
Table 9-12. Commands for moving in lists
Keystrokes | Command name | Action |
---|---|---|
C-M-n | forward-list | Move forward by one list. |
C-M-p | backward-list | Move backward by one list. |
C-M-d | down-list | Move forward and down one parenthesis level. |
(none) | up-list | Move forward out of one parenthesis level. |
C-M-u | backward-up-list | Move backward out of one parenthesis level. |
As a mnemonic device, you can think of lists as analogous to lines and S-expressions as analogous to characters; thus, C-n and C-p appear in list motion commands, whereas C-f and C-b appear in S-expression motion commands. C-M-n and C-M-p work similarly to C-M-f and C-M-b, respectively, except that you must position the cursor so that there is a list in front or back of it to move across—that is, there must be an opening or closing parenthesis on, after, or before the cursor. If there is no parenthesis, Emacs signals an error. For example, if your cursor is positioned like this:
(fred bob (dave (pete)) ed)
and you type C-M-n, Emacs complains with the message:
Containing expression ends prematurely
However, if your cursor is here:
(fred
_bob (dave (pete)) ed)
the "next list" is actually (dave (pete)), and the cursor ends up like this if you type C-M-n:
(fred bob (dave (pete))
_ed)
The commands for moving up or down lists enable you to get inside or outside them. For example, say your cursor is here:
(fred bob (dave (pete)) ed)
typing C-M-d moves the cursor here:
(fred bob (dave (pete)) ed)
This is the result because fred is the next level down after its enclosing list. Typing C-M-d again has this result:
(fred bob (dave (pete)) ed)
You are now inside the list (dave (pete)). At this point, typing C-M-u does the opposite of what C-M-d does: it moves the cursor back and outside of the two lists. But if you type M-x up-list Enter, you will move forward as well as out, resulting in this:
(fred bob (dave (pete))
_ed)
The commands for defuns listed in Table 9-13 are more straightforward.
Table 9-13. Commands for working with functions
Keystrokes | Command name | Action |
---|---|---|
C-M-a | beginning-of-defun | Move to the beginning of the current function. |
C-M-e | end-of-defun | Move to the end of the current function. |
C-M-h | mark-defun | Put the cursor at the beginning of the function, put the mark at the end. |
These commands work properly only when the (defun that starts the current function is at the beginning of a line.
- 1.3 A Word About Modes
- 10.7 Starting Modes via Auto-Mode Customization
- Chapter 11. Emacs Lisp Programming
- 1.5.3 The Mode Line
- 9.8.1 Indentation in Lisp Modes
- 9.8.2 Comments in Lisp Modes
- 4.4.4 The Dispatcher
- About the author
- Chapter 7. The state machine
- Appendix E. Other resources and links
- Example NAT machine in theory
- The final stage of our NAT machine