This chapter describes the commands available in Emacs for editing specific types of file, such as text memos or program source code:
Emacs has many different major modes, each of which customises Emacs for editing text of a particular file type. The major modes are mutually exclusive -- that is, only one major mode can be active at a time. When at top level, Emacs indicates in the mode line which major mode is active for each buffer displayed on the screen.
When Emacs creates a buffer, it is in what is called Normal Mode , which means that the character commands are defined so as to be convenient for general use. More precisely, in Normal Mode every Emacs binding is set to its default state. For editing any specific type of text, such as Lisp code or English text, you should switch to the appropriate major mode. This tells Emacs to change the meanings of a few commands to become more specifically adapted to the language being edited. Most commands remain unchanged.
Selecting a new major mode is done with an ESC-x command. Each major mode is the name of the function that selects that mode. Thus, you can enter Lisp Mode by typing ESC-x lisp-mode. Emacs can be made to enter the correct major mode for a file simply based on the file's type, so you do not have to worry about selecting a mode. This is achieved by using the auto-execute function.
The ESC-x auto-execute command causes the nominated command to be executed every time a file is manipulated which matches the specified file pattern. The file pattern is specified as a wildcard pattern.
The "*" and "?" characters are treated specially, all other characters stand for themselves. A "*" match zero or more characters and a "?" matches a single character.
Here are some example patterns:
Pattern | Explanation | Examples |
---|---|---|
*.cxx | Matches all files whoes names end in .cxx. | emacs.cxx
search.cxx .cxx |
*?.cxx | Matches all files whoes names has atleast one characters before ending with .cxx. | emacs.cxx
search.cxx a.cxx |
readme* | Matches all files that start with readme. | readme.txt
readme.1st |
The command is executed whenever the file name matches the pattern. If you wish to use Lisp Mode when visiting files with a type of .ML, use the command ESC-x auto-execute lisp-mode *.ml.
Syntax tables define how Emacs commands (such as forward-word) react to the characters in a buffer. Many major modes alter syntax tables so that characters are treated in the most suitable way for the particular mode. For example, most programming language major modes specify that only blank lines separate paragraphs. This is so that the paragraph commands remain useful when writing program source code rather than natural language text.
To find out which keys a particular major mode redefines, use the command ESC-x describe-bindings while the required mode is in effect. This command displays all the key bindings for the current buffer. The specific bindings for the current major mode will be displayed under the heading Local bindings, along with any other local bindings you have defined yourself.
Emacs enables you to easily manipulate words, sentences, or paragraphs of text. In addition, there are commands to fill text and convert character cases.
Emacs has commands for moving over or operating on words. By convention, they are all Escape prefixed commands. All of these commands may be used in any major mode.
Move forward over a word. | ESC-f or Ctrl-Right |
Move backward over a word. | ESC-b or Ctrl-Left |
Delete up to the end of a word. | ESC-d or Ctrl-Delete |
Delete back to the beginning of a word. | ESC-h |
Notice how these commands form a group that parallels the character-based commands ^F, ^B, ^D and Delete.
The commands ESC-f and ESC-b move forward and backward over words. They are analogous to ^F and ^B, which move over single characters. Like their Control- analogues, ESC-f and ESC-b move over several words if they are given a prefix argument. ESC-f with a negative argument moves backwards like ESC-b, and ESC-b with a negative argument moves forwards. Forward motion stops exactly after the last letter of the word, while backward motion stops exactly before the first letter.
It is easy to delete a word at a time. ESC-d deletes the word after dot. To be precise, it deletes everything from dot to the place that ESC-f would move dot. Therefore, if dot is in the middle of a word, only the part of the word after dot is deleted. If some punctuation comes after dot and before the next word, it is deleted along with the word. If you wish to delete only the next word but not the punctuation, use ESC-f to go to the end, then delete the word backwards with ESC-h. ESC-d takes arguments just like ESC-f.
ESC-h deletes the word before dot. It deletes everything from dot back to where ESC-b would move dot. If dot is after the space in FOO, BAR, then FOO, is deleted. If you wish to delete just FOO, use ESC-b then ESC-d instead of an ESC-h.
The definition of a word as used by the word commands is completely controlled by the current syntax table. Any character can, for example, be declared as a word-delimiter.
The Emacs commands for manipulating sentences and paragraphs are mostly Escape prefixed commands so as to resemble the word-handling commands.
Move back to the beginning of a sentence. | ESC-a |
Move forward to the end of a sentence. | ESC-e |
Move back to the beginning of the previous paragraph. | ESC-( |
Move forward to the end of the next paragraph. | ESC-) |
The commands ESC-a and ESC-e move to the beginning and end of the current sentence respectively. They were choosen to resemble ^A and ^E, which move to the beginning and end of the current line. Emacs considers a sentence to end wherever there is a ., ? or ! character followed by white-space or an end-of-line. Neither ESC-a nor ESC-e move past the white-space which delimits the sentence.
The variable sentence-delimiters is a regular expression search-string composed of the various strings that define a sentence boundary. The default value of this variable is:
[.!?][\040\t\n][\040\t\n]*
ESC-( moves to the beginning of the current or previous paragraph, while ESC-) moves to the end of the current or next paragraph. Blank lines and text formatter command lines are not considered part of any paragraph. Also, an indented line starts a new paragraph.
The variable paragraph-delimiters is a regular expression search string composed of the various strings that define a paragraph boundary. The default value of this variable is:
\f\n\|[\040\t]*\n
Tab | Indents appropriately in a mode-dependent fashion. |
Linefeed | Performs the same action as Return followed by Tab. |
Use the Tab command to request indentation. Its precise effect depends on the major mode. In Text and Normal Modes, it indents to the next tab-stop.
When you type normal text, if you use the Linefeed key to terminate a line, the indentation of the current line will be duplicated at the start of the next line. So to type in several lines of text all starting at a specified indent, use the Linefeed key to terminate each line.
Emacs has a major mode called Text Mode which provides functions for setting margins, filling, justifying and centering text. Use the ESC-x text-mode command to enter Text Mode.
The Text Mode functions can be used without needing to switch to Text Mode -- they can be used from any major mode. This is usually more convenient than having to change the major mode to Text Mode when you need to format some text. Text Mode is included in Emacs for backwards compatibility only.
If you enable the Text Mode functions explicitly using ESC-x text-mode, the Text Mode functions are bound to keys. If you do not switch to Text Mode to use the functions, you must either provide your own bindings for the functions, or execute the functions as extended commands using ESC-x.
The Text Mode functions and key bindings are:
Set the left margin. | ESC-1 |
Set the right margin. | ESC-^H |
Set the margins to the values indicated by text in the current paragraph. The right margin is defined as the right-most column in which text appears for paragraphs with a ragged-right margin. | (set-margins) |
Set the paragraph indent offset. | ESC-i |
Fill or justifies the current paragraph between the currently set margins. | ESC-j |
Fill or justifies the region between the currently set margins. | ESC-J |
Toggle the current justify mode to either a ragged or straight right margin. | ESC-m |
Centre the current line between the currently set margins. | ESC-c |
The fill-and-justify functions will fill the current paragraph between the current margin settings. If you edit text in the middle of a paragraph, the paragraph may no longer be correctly filled -- just type ESC-j to fill the paragraph. The effect of ESC-j can be undone with ^X-^U (undo). See Undoing Changes to the Buffer for more information on undoing changes.
Paragraphs can be indented using ESC-i to set an indent offset. A positive value for the indent offset causes the first line of the paragraph to be indented by the specified number of characters. For example, with an indent offset of 5, paragraphs will appear as:
Here is the beginning of the paragraph. Here is some more text, and here is yet more.
A negative indent offset enables you to create hanging paragraphs; with an indent of -5 and a left margin of 5, text will be filled as follows:
1. text....text ....text more text..... text text more text..... text text more text..... text text 2. text....text ....text more text..... text text more text..... text text Here is the beginning of the paragraph Here is some more text, and here is yet more.
The Text Mode functions access several buffer-specific variables. You can use either use the Text Mode bindings listed above to set the values of these variables, or you can set them directly using ESC-x set. If you want to change their default values, use ESC-x set-default.
The Text Mode variables are:
left-margin
Contains the left margin setting for the current buffer. The default setting for the left margin is 1.
right-margin
Contains the right margin setting for the current buffer. The default setting for the right margin is 1000 -- if you use the Text Mode fill functions from any mode other than Text Mode, be sure to set right-margin to a reasonable value.
index-offset
Contains the index offset for the current buffer.
buffer-display-margins
Controls whether the current margin settings are displayed in the buffer's mode line. This variable is set to 1 by default, so margins are displayed. If you do not want margins displayed, set the variable to 0.
autowrap-mode-flag
Controls whether text is automatically wrapped as you type it. This is set to 1 by default, so that when you type text beyond the right margin, a line separator is automatically inserted, and you can continue typing without worrying about breaking the line yourself. This feature is known as auto-filling.
buffer-justify-flag
Controls the appearance of text at the right margin. If set to 1 (the default), text will be be filled with a straight right margin; if buffer-display-margins is set to 1, the right margin indicator in the mode line will be a J. If buffer-justify-flag is set to 0, text will be filled with a ragged right margin; if buffer-display-margins is set to 1, the right margin indicator in the mode line will be an R.
Emacs has commands for converting either a single word or any arbitrary range of text to be capitalized, upper-case, lower-case or to invert the case.
Convert the current word or region to lower-case. | ESC-l (case-lower) |
Convert the current word or region to upper-case. | ESC-u (case-upper) |
Invert the case of the current word or region. | ESC-i (case-invert) |
Capitalize the current word or region. | ESC-C (case-capitalize) |
If a case conversion command is given in the middle of a word, it applies to the whole word.
To convert the case of part of a word set the region around the part to be converted.
Special features for editing programs include automatic indentation, comment alignment, parenthesis matching, and the ability to move over balanced, parenthesised expressions. Many of these features are generalised so that they can work for any programming language.
For some languages, there is a special major mode which customises Emacs to be better-suited to editing programs written in that language.
The languages supported by major modes include:
C | C Mode or Electric C Mode |
C++ | C Mode or Electric C Mode |
MLisp | Lisp Mode |
Lisp | Lisp Mode |
PASCAL | PASCAL Mode |
Bliss | Bliss Mode |
The TAGS package can be used to remember all the labels or functions in a multi-file program, and to find any one of them quickly.
The command ^X-^E (compile-it) is used to compile a program within Emacs. Normally, ^X-^E uses the make command to compile a program. make knows how to build your program based on a description file that you give it.
The first thing ^X-^E does is to write out all modified files. This is because it is likely that other buffers contain parts of the same program that you are about to compile.
Then ^X-^E invokes the make command. All output from make goes into the empty buffer Error log, which will be displayed in a window on the screen. Messages that the compilation generates are displayed in the Error Log buffer as they are generated.
When the compilation is complete, the function parse-error-messages-in-region is applied to the entire buffer. This command uses the function specified in the variable error-message-parser to find all the compiler error messages. For each error message generated, the source file in which the error was detected is visited and the line on which the compilation detected an error is marked. This process generates a list of marks in various buffers for viewing with the ^X-^N (next-error) command. When all errors have been visited, ^X-^N says so.
In summary, a typical use of the compilation facility is as follows:
You can also compile programs with the ESC-x shell command. In shell, you are put into shell window in Emacs where every line that you type and end with a Return is executed as a DCL command. The output from the commands is displayed in the shell window. Several key sequences are re-bound in shell to be more useful. ^R is bound to insert the last command sent to DCL into the buffer at dot. So, if you make a mistake, you can easily edit the command and try it again. The last 20 commands are remembered and can be recalled with successive ^R commands. The shell command is described more fully in the Barry's Emacs Extensions Reference Manual.
Indent the current line. | Tab |
Equivalent to Return followed by Tab. | Linefeed |
Delete all spaces and tabs around dot. | ESC-x delete-white-space |
Most programming languages have some indentation convention. For Lisp, lines are indented according to their nesting in parentheses. For assembler code, almost all lines start with a single tab, but some have one or more spaces as well.
Whatever the language, to indent a line, use the Tab command. Each major mode defines this command to perform the sort of indentation appropriate for the particular language. In Lisp Mode, Tab aligns the line according to its depth in parentheses. This only happens if the tab is the first character on a line.
The command Linefeed does a Return, then does a Tab on the next line. Thus, Linefeed at the end of the line makes a following blank line and supplies it with the appropriate amount of indentation. Linefeed in the middle of a line breaks the line and supplies the indentation in front of the new line.
To delete just the indentation of a line, go to the beginning of the line and use ESC-x delete-white-space, which deletes all spaces and tabs around the cursor.
To insert an indented line before the current line, use ^A-^O-Tab. To make an indented line after the current one, use ^E-Linefeed.
The Emacs parenthesis matching feature shows automatically how parentheses balance as you enter text. When this feature is enabled, after a closing parenthesis or other close-bracket character is inserted, the cursor automatically moves for an instant to the open parenthesis which balances the newly-inserted character. The cursor stays at the open parenthesis for a second before returning to the close parenthesis.
The parenthesis-matching feature is enabled by using Fundamental Mode rather than the default Normal Mode, and may be enabled by language modes as required. It is also available on its own as the MLisp package FLASHER. You can load the FLASHER package yourself to have parenthesis-matching in a specific buffer. Several language major modes also load FLASHER to provide this feature when you edit program source code.
It is worth emphasising that the location of dot (the place where your text will be inserted) is not affected by the parenthesis-matching feature. Dot stays after the close parenthesis, where it ought to be -- only the cursor on the screen moves away and back. You can type ahead freely as if the parenthesis display feature was not enabled. In fact, if you type fast enough, you will not see the cursor move -- you must pause after typing a close parenthesis to let the cursor move to the open parenthesis.
If you type in an un-matched parenthesis, Emacs rings the bell and tells you there is an error.
Lisp's simple syntax makes it easy for an editor to understand. As a result, Emacs can do more for Lisp, and with less work, than for any other language.
Paragraphs are defined to start only with blank lines so that the paragraph commands can be useful. Auto-fill Mode indents the new lines which it creates. Comments start with a semicolon ";". For example:
; This is a comment
The command ` (expand-mlisp-word) helps you to insert Emacs function names; it gives you name completion, which saves a lot of typing.
Lisp Mode commands include:
Move back one s-expression. | ESC-( |
Move forward one s-expression. | ESC-) |
Compile and execute the first MLisp expression in the current buffer. | ESC-c |
Re-indent the current MLisp function. | ESC-j |
Re-indent the current MLisp line. | ESC-i |
Insert a Lisp comment. | ; |
ESC-c can be used to take the single MLisp expression in the current buffer and compile it in Emacs as MLisp. The compiled MLisp is then executed. Thus, if you write some MLisp, the easiest way to test and debug it is to run it using ESC-c.
To re-indent the current function (as defined by lines staring with (defun) use the ESC-j command. This command uses the same indenting algorithm as the Linefeed and Tab commands.
Lisp Mode works in exactly the right way only for the MLisp dialect of Lisp. For other Lisps, the syntax tables may have to be modified.
C Mode is a major programming mode for writing C programs. It provides optional automatic expansion of C keywords, formatting them according to Software Engineering standards.
When you type a C keyword in C Mode and complete it with a non-word character, C Mode automatically recognises the word, and performs the expansion. If a word is not recognised, it is not expanded.
If you do not require automatic expansion, set the variable c-auto-expansion to zero. The function expand-c-keyword can then be used directly after each keyword to force the expansion to take place.
C Mode works by reading templates from the Emacs language template library accessed through the language database search-list.
This search-list contains the provided template library database, and also the database sys$login:language-template. The database language-template is searched first, so your customised templates are used before the system's templates.
To cause the expansion for module and function headers, enter the appropriate word and use the expand-c-keyword function.
If you try to expand a C word that C Mode does not understand, an error message is issued. All C keywords, braces, comments and tertiary operators are recognised by C Mode.
PASCAL Mode is a simple major mode for writing PASCAL programs. PASCAL Mode works by automatically upper-casing all PASCAL reserved words as they are entered. Table 3 shows which
PASCAL Keywords Recognised by PASCAL Mode
AND | ARRAY | BEGIN | BOOLEAN | CAND | CASE |
CHR | CONST | COR | DIV | DO | DOWNTO |
ELSE | END | EXIT | EXPORTS | FALSE | FILE |
FOR | FORWARD | FROM | FUNCTION | GET | GOTO |
IF | IMPORTS | IN | INPUT | INTEGER | LABEL |
LONG | MOD | MODULE | NEW | NIL | NOT |
OF | OR | ORD | OTHERWISE | OUTPUT | PACKED |
PRIVATE | PROCEDURE | PROGRAM | PUT | READ | READLN |
REAL | RECORD | REPEAT | RESET | REWRITE | SET |
SET | STRING | TEXT | THEN | TO | TRUE |
TYPE | UNTIL | VAR | WHILE | WITH | WRITE |
WRITELN |
Two types of PASCAL comment are recognised: (* with *) and { with }.
The function Pascal-Skeleton can be used to create a function or procedure template and fills in the appropriate parameters and types.
Bliss Mode
Bliss Mode is a major programming mode for writing any dialect of Bliss code. It provides optional automatic expansion of Bliss keywords, formatting them according to Software Engineering standards.
When you type a Bliss keyword in Bliss Mode and complete it with a non-word character, Bliss Mode automatically recognises the word, and performs the expansion. If a word is not recognised by Bliss Mode, it will not be expanded. Some words in Bliss may be grouped together to form strings of keywords (for example, EXTERNAL ROUTINE). To solve the problem of whether or not to expand a word, Bliss Mode looks at the word separator. If a word may form a string of words, Bliss Mode assumes that there are no more words to be input if the separator is a Return. So, to define a GLOBAL BIND ROUTINE, type GLOBAL BIND ROUTINE Return.
If you do not require automatic expansion, set the variable bliss-auto-expansion to zero. The function expand-bliss-keyword can then be used directly after each keyword to be expanded to force the expansion to take place.
Bliss Mode works by reading templates from the Emacs language template library accessed through the language database search-list.
This search-list contains the provided template library database, and also the database sys$login:language-template. The database language-template is searched first, so your customised templates are used before the system's templates.
For example, when you need to write a new Bliss module, do the following:
If you try to expand a Bliss word that Bliss Mode does not understand, an error message is issued. All Bliss reserved keywords and comment characters are recognised.