You can also put a buffer directly into SQL mode with M-x sql-mode. This provides some assistance for motion and composition of SQL statements, but mostly it's there to let you build complex statements and then ship them to the interactive buffer for execution. Table 9-10shows how to send various segments of the buffer to the database.
Table 9-10. SQL mode send commands
Keystroke |
Command name |
Action |
C-c C-c |
sql-send-paragraph |
Send the paragraph the cursor is on. A paragraph is defined by the particular database client. For the sql-mysqlprocess, for example, a paragraph begins with a statement like select or update and ends with a semicolon. Any number of lines can intervene. |
C-c C-r |
sql-send-region |
Send the marked region. |
C-c C-b |
sql-send-buffer |
Send the entire buffer. |
The output of all of these send commands shows up in your interactive buffer. Nothing changes in the editing buffer so you should feel free to experiment. That's what these modes are here for!
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-11shows 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-nand C-pappear in list motion commands, whereas C-fand C-bappear in S-expression motion commands. C-M-nand C-M-pwork similarly to C-M-fand 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 (pe te) ) ed)
and you type C-M-n, Emacs complains with the message:
Containing expression ends prematurely
However, if your cursor is here:
(fred
_ b o b (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-dmoves the cursor here:
( fred bob (dave (pete)) ed)
This is the result because fredis the next level down after its enclosing list. Typing C-M-dagain has this result:
(fred bob ( dave (pete)) ed)
You are now inside the list (dave (pete)). At this point, typing C-M-udoes the opposite of what C-M-ddoes: 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-13are 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 (defunthat starts the current function is at the beginning of a line.
Читать дальше