Barry's Emacs
User's Guide
Extensions Reference
MLisp Programmer's Guide
MLisp Reference
Barry's Emacs Programmer's Guide
This Guide describes how to program the Barry's Emacs editor. It contains a definition for the Mock Lisp programming language and describes, in detail, all the primitive Mock Lisp functions and system variables.
Emacs Usage
The Barry's Emacs software and documentation is Copyright © 1980-2016 Barry Scott.
Intended Audience
This manual is intended for programmers wishing to program Emacs or add extensions. For information on how to use Emacs as an editor, refer to the Barry's Emacs User's Guide.
Document Structure
The information in this Guide is structured as follows:
- Introduction provides an introduction to the programming facilities of Barry's Emacs.
- Macros describes the macro facilities for extending Emacs using keystroke sequences.
- Mock Lisp describes the Mock Lisp programming language used to write complex extensions.
- Extension Facilities describes the various facilities provided in Emacs to help you write extensions.
- Python in Emacs describes how Emacs and Python are integrated.
- Emacs database tools describes the tools to manipulate Emacs databases.
- Starting Up Emacs describes the process by which Emacs is activated and how control is passed to the user.
associated documents
The Barry's Emacs Mock Lisp Reference Manual contains an alphabetically ordered list of all Emacs functions and variables available in the Mock Lisp programming language.
The Barry's Emacs User's Guide describes how to use the editing features of Barry's Emacs.
The Barry's Emacs Extensions Reference Manual documents the Emacs extensions (packages, subsystems and libraries) that are distributed with Barry's Emacs.
conventions used in this document
Throughout this manual, key sequences are described as causing specific actions to occur (functions to run, for example). These key sequences are those set up when Emacs is started (they are the default key bindings).
Key sequences are displayed with each key separated by a dash. When using these sequences, do not include the dashes. The dashes are displayed for clarity only.
Many Emacs key sequences use Control keys. These are indicated by a caret character (^) appearing in the key sequence. For example, to type the ^O key sequence, press and hold down the Ctrl key and simultaneously press the O key. To type ^^, press and hold down the Ctrl key, then simultaneously type the ^ key.
Many other Emacs key sequences use Escape keys. These are indicated with an ESC- in the key sequence. For example, to type the ESC-e key sequence, press the key labelled ESC, then press the e key. Note that ESC- key sequences are case-sensitive; this means that ESC-e is not the same as ESC-E.
Different typefaces used in this book are used to distinguish between normal text and special symbols. These are described in the following table:
Return | The following special characters are displayed as shown: Return, Delete, Escape, Tab, Backspace, Linefeed and Space. |
This is an example |
All examples and system displays are shown like this. |
execute-mlisp-file | All keystroke sequences, function names, command names and variable names are displayed in this typeface. |
Introduction
Emacs provides an extremely comprehensive set of text editing features. These standard features will provide most novice Emacs users with all the editing capability they require. When a new user has had some experience with Emacs, they will often want to change the way that Emacs does certain things, or will want features that are not built in to the standard Emacs. The great thing about Emacs is that you can customise the manner in which standard features function or appear, or you can create new features that do not exist in the standard Emacs.
Simple Emacs extensions can be created using macros. These are key sequences remembered by Emacs which can be replayed at any time during the editing session. These so-called keyboard macros can be saved in a buffer and stored in a file, so you can save macro definitions and reload them in a subsequent editing session. Macros are described in detail in Macros on page 7.
Macros do not provide the generality required to perform complex operations. To create complex Emacs extensions, you must write programs in the Emacs programming language Mock Lisp (MLisp). As the name suggests, MLisp is a dialect of the Lisp programming language. The MLisp language itself is described in detail in Mock Lisp on page 9. Emacs provides a complete MLisp program development environment to aid programmers developing MLisp code. This environment includes debugging, online documentation, database access and process control facilities. These extension facilities are described in detail in Extension Facilities on page 25.
It is also possible for external applications written in other programming languages to make use of Emacs' powerful editing facilities, and for Emacs to execute code in executable images outside Emacs itself. These advanced facilities are described in Programming Emacs From Other Languages on page 35.
Command lines
Bemacs Command line
bemacs passes the whole command line to the Barry's Emacs application for processing.
Barry's Emacs then parses the command line into its component parts, called arguments. Having parsed the command line Barry's Emacs determines the package for the command line. The default package is called emacs. This can be overridden by using the -package switch.
Windows | Unix | Description |
---|---|---|
/package=package-name | -package=package-name | This causes emacs to execute the MLisp command package-name-com. If package-name-com is not yet defined then emacs loads the MLisp module named package-name.mlp. |
Writing a package
Emacs will automatically load a packages definition from an MLisp module named package from the file package.mlp. That module must contain atleast one function definition for the MLisp function package-com.
It is the job of the package to process the command line. Emacs has already parsed the command line into its component parts. Use the following functions to access the components of the command line:
- (argc)
- Returns the number of command line arguments. There is always one arguments.
- (argv position)
- Returns the text of the argument at position position. Position zero always returns the name of the package.
- (arg-is-qualifier position)
- Returns true if the argument at position position is a command line qualifier (also called a command line switch).
If the package does not use one of these functions then Emacs will call visit-file for each argument that is not a qualifier.
BemacsServer Command line
BEmacsServer supports a full command line. BemacsServer can be used directly if required. BEmacsServer supports the following command line options:
- /restore[=file]
- Restore the Emacs Environment from file.
- /norestore
- Do not restore any Emacs Environment file
- /package=package-file
- See above for details
- /RegServer
- register the OLE interface
- /embedding
- used by OLE
- /automation
- used by OLE
- /UnRegServer
- unregister the OLE interface (not implemented in this version)
Macros
This chapter describes the macro facilities available in Emacs. These facilities allow you to build macros on the fly, and then use them to replace multiple keystrokes. You can also use macros to write generalised facilities, map them into the function name-space and bind key sequences to them.
Macros differ from Mock Lisp (MLisp) functions because they are key sequences that are replayed into the Emacs keyboard processor and are subject to the current bindings. The effect of a macro will be unpredictable if the key bindings used in a macro change between the macro's definition and its use. There are no facilities to alter control flow while a macro is being executed.
keyboard macros
(start-remembering) ^X-( | Starts defining a keyboard macro. |
(stop-remembering) ^X-) | Ends the definition of a keyboard macro. |
(execute-keyboard-macro) ^X-e | Executes the most recently-defined keyboard macro. |
(define-keyboard-macro) | Turns the most recent keyboard macro into a named macro. |
A keyboard macro is a command defined by you to abbreviate a sequence of other commands. For example, if you discover that you are about to type ^U-^D forty times, you can define a keyboard macro to execute ^U-^D and call it with a repeat count of forty.
You define a keyboard macro while executing the commands which are part of the definition. In this way, you can see what the effects of your commands are---you do not have to work them out in your head. When you have finished, the keyboard macro is defined and has also been, in effect, executed once. You can execute the sequence of commands again by invoking the macro.
To start defining a keyboard macro, type the ^X-( (start-remembering) command. From then on, your commands continue to be executed, but also become part of the macro definition. When you have finished, the ^X-) (stop-remembering) command terminates the definition (without becoming part of it).
You can call the newly-defined macro the ^X-e (execute-keyboard-macro) command. You can give ^X-e a repeat count as a prefix argument to execute the macro several times.
There are two ways of using a keyboard macro to perform some action on each line in the buffer. The first is to start by positioning dot on the line above the first line to be processed, then begin the macro definition with a ^N (next-line). The second method is to start on the proper line, and end the macro with a ^N. Repeating the macro will operate on successive lines using either method.
Emacs can have only one current keyboard macro. When you define a new keyboard macro, the old macro definition is lost. To save a keyboard macro definition, you must give it a name. If you execute define-keyboard-macro, the last keyboard macro defined is turned into a named macro . When a named macro has been created, you can bind it to any key sequence in the usual way.
named macros
The named macro facility in Emacs allows you to specify a collection of keystrokes to act as an Emacs function, and then associate the function with another key sequence.
(define-buffer-macro) | Defines a macro using the current buffer's contents as the keystroke sequence. The name of the macro is obtained from the buffer-specific variable current-buffer-macro-name. |
(define-keyboard-macro function-name) | Defines a named macro with the current contents of the keyboard macro. |
(define-string-macro function-name keys) | Defines a macro given its name and the key sequence as arguments. |
ESC-x edit-macro | Inserts the body of the specified macro into the buffer Macro edit to allow editing of the keystroke sequence. |
(rename-macro old-name new-name) | Changes the name of a macro or function. |
You normally define a named macro by first defining it as a keyboard macro, then using ESC-x define-keyboard-macro to give the macro a name. When a macro has been given a name, you can execute it using ESC-x as if it were an ordinary Emacs function. The fact that it is a macro is transparent to ESC-x.
A quick way to define a short macro is to use ESC-x define-string-macro. This command prompts you for the name of the macro and the key sequence to use for the macro's body. Special characters have to be quoted, otherwise they will be executed as commands in the Mini-buffer. A disadvantage of this type of macro definition is that you must know, or be able to visualise, the key sequence while you are typing it in. ESC-x define-string-macro does not execute the macro key sequence while it is being defined.
If you discover a mistake in a macro, or you wish to change the macro's definition, use ESC-x edit-macro. This command generates a buffer called Macro edit with the body of the macro inserted in the buffer. The name of the macro is associated with the buffer, and the buffer's type is Macro. You can identify the name of the macro by looking at the variable current-buffer-macro-name. When you have finished editing the macro, the buffer contents can replace the body of the macro. Use ESC-x define-buffer-macro to re-define the macro.
If you wish to change the name of a macro (or any Emacs function, including MLisp and wired-in functions), use the rename-macro command. rename-macro asks you to specify the old name of the macro, followed by the new name you wish to give it. If the new name already exists, the existing MLisp or macro function will be overwritten. If the new function name already exists and is a wired-in function, an error message will be generated. This prevents you from removing wired-in parts of Emacs that packages may need.
Mock Lisp
The Mock Lisp (MLisp) programming language provides access to all the Emacs extension facilities. This chapter describes how to use MLisp to extend Emacs by writing private functions or shareable libraries and subsystems.
mlisp syntax
The syntax of MLisp is based on certain dialects of the full Lisp language. This is a highly structured syntax with few built-in constructs. This simple syntax means that an efficient interpreter can be written, so that executing MLisp code does not use significant CPU overhead.
expressions
An MLisp program consists of an expression. Each expression may contain further expressions to be evaluated.
Expressions fall into two categories:
- Function call expressions, which are introduced with the left parenthesis character. The left parenthesis is followed by the name of a function to be executed, followed by its arguments. Finally, the entire expression is completed with a right parenthesis. White space is used to separate the arguments from themselves and the function name.
Examples:
(setq left-margin 9) (forward-word) (fetch-database-entry "MLisp-library" "process")
- Evaluation expressions, which are specified simply by the presence of a constant or variable name.
Examples:
24 "abc" left-margin
names
MLisp names in Emacs are held in two name-spaces: one for function names, and the other for variable names. A name in one name-space is not related to the same name in the other name-space. There is never any confusion about which name-space a particular name belongs in, as this information can always be deduced from the syntactic positioning of the name. Whenever you need to pass the name of a function to another function, you must pass it as a string.
A name can consist of an arbitrary sequence of any characters excluding the MLisp syntax characters (, ) and ;. Names may not start with a digit or a minus sign. Upper-case letters are always distinguished from lowercase letters.
Examples of names:
execute-mlisp-file ~TAGS-replace-string ^K-character the-quick-brown-fox-jumps-over-a-lazy-dog ANAMEWITHCAPITALLETTERS
comments
Comments are introduced in MLisp using the ; character. The comment extends from the semicolon character to the end of the line. During MLisp compilation, all comments are removed.
constants
Constants are represented in MLisp by either writing their value in the MLisp expressions, or by referencing one of the named constants provided by Emacs for special purposes. Named constants are located in the variable name-space along with other named variables. You cannot alter the value of a named constant.
Numeric and Character Constants
You specify numeric constants using digit characters. An optional leading minus sign may be included to obtain negative values. Note, however, that you cannot specify a leading plus sign. Numeric constants are integers in the range 2,147,483,647 to -2,147,483,648. Overflow is not detected by the MLisp compiler, and merely results in wrap around.
Character constants are converted to their numeric equivalent during MLisp compilation; they are merely a convenient way of specifying the ASCII numeric value for a character.
Character constants are introduced and terminated with the single quote character, and can represent only one ASCII character. During MLisp compilation, the special sequences in Table 1 allow you to specify the control characters of the ASCII character set.
Table 1 MLisp Character Constants
Sequence | ASCII Character |
\n | Linefeed |
\b | Backspace |
\t | Tab |
\r | Return |
\e | Escape |
\' | Quote |
\\ | \ |
\xxx | Octal character xxx |
^X | Control X |
^? | Delete |
\x | character x |
It is best to use the \ sequences for the characters that are available using that mechanism, rather than using the equivalent ^ sequences because the \sequences are more meaningful.
String Constants
String constants are enclosed in double quote characters and can be up to 800 characters in length. Any ASCII character can be included in the string. An escape mechanism is provided using a scheme similar to that for character constants. The codes are described in Table 2.
Sequence | Character |
---|---|
\n | Linefeed |
\b | Backspace |
\t | Tab |
\r | Return |
\e | Escape |
\" | Double quotes |
\\ | \ |
\xxx | Octal character xxx |
\^X | Control X |
\^? | Delete |
\x | Character x |
\[key] | Characters for key |
variables
A variable is a name which is associated with a value, either a number, mark, string, window-ring or an array. Emacs uses many variables internally, and provides many others which allow the user to customise various Emacs features.
The easiest way to set a variable is to use the setq function. This function requires the name of the variable and the value to be assigned to the variable. Before the variable is set, the value being assigned may be checked so that you cannot set impossible values which might jeopardise other Emacs code. The variable to be set must already exist. You can set the value of a variable interactively using the ESC-x set command.
To fetch the value of a variable, simply reference it by name. You can also display the value of a single variable interactively with the ESC-x print command.
variable types
There are five different types of variable:
Numerics | which hold integer numbers in the range 2,147,483,647 to -2,147,483,648; |
Strings | which contain arbitrary length text; |
Markers | which contain a buffer name and positional information; |
Array | which contains up to a 10 dimension array of variables |
Window-rings | which contain a set of windows that make up a screen. |
You can also create arrays with up to 10 dimensions. A single array can contain elements of any of the basic types. Variable length system arrays conform to a convention where element zero contains the number of elements in the array excluding element zero.
When using variables in MLisp programs, the type of the variable is coerced to the type that a particular function expects. For example, if a variable holds a number and is supplied to a function expecting a string, the number is converted to a string when it is needed. When coercion takes place for markers, if a string is required, the value of the variable is the buffer name part; if a numeric is required, the position in the buffer is returned.
No coercion is available for window-rings. The only operation that can be performed on a window-ring is assignment.
variable classes
There are five different classes of variable as described below:
System Variables
System variables are used to set up a specific option upon which Emacs acts. They either modify the way Emacs presents information, or they contain some specific information. Some examples of system variables include pop-up-windows, which tells Emacs to pop a window up when one is needed, and window-size which tells you the size, in lines, of the currently selected window.
Buffer-Specific Variables
Buffer-specific variables are used to contain information which is private to each buffer. The name of the variable is the same for each buffer, but when its value is fetched or set, a private copy for the currently selected buffer is used. If you attempt to fetch the value of a buffer-specific variable before you have set it, the variable is preset with a default value. Buffer specific variables are created using the declare-buffer-specific function, and their default value is set using setq-default (or ESC-x set-default).
Global Variables
Global variables are used to contain information to be accessed by functions executed at any time. Once created, these variables remain in existence until Emacs exits.
Named Constants
Named constants are used by Emacs to provide users with useful information. Each named constant has a name in the variable name-space and has a read-only value. If you attempt to alter the value of a named constant, Emacs issues an error message.
Local Variables
Local variables are used to contain local information in a block inside an MLisp program. The lifetime of these variables is only while the block in which they are declared is executing. Local variables are declared by nominating them immediately after the block introducer (after progn and function names). For example:
(progn a b c d ... )
the scope of variables
The name scope for all variables is everywhere in Emacs, but the lifetime of the storage allocated to the variable depends on the class of variable. For example, once some MLisp has been compiled which declares a local variable, that variable's name is entered into the variable name-space and may be freely accessed. However, if you attempt to access the variable outside the block in which it is declared, no storage will have been allocated for it, and Emacs will issue an error message.
commands for manipulating variables
(declare-global name...)
Declares global variables and initialises their default value to zero.
(declare-buffer-specific name...)
Declares buffer-specific variables and initialises their default value to zero.
ESC-x set
Prompts for and sets the value of any variable interactively.
(setq name expression)
Sets the value of any variable in an MLisp program.
ESC-x set-default
Prompts for and sets the default value of a buffer-specific variable interactively.
(setq-default name expression)
Sets the default value of a buffer-specific variable in an MLisp program.
ESC-x print
Displays the current value of a variable in the Mini-buffer.
ESC-x print-default
Displays the default value of a buffer-specific variable in the Mini-buffer. If the variable is not buffer-specific, print-default functions the same way as the print command.
(use-variables-of-buffer buffer expression)
This function provides access to the buffer-specific variables of the buffer named buffer. When expression uses a buffer-specific variable, it will be the instance of the variable from buffer buffer, not from the current buffer. The following example inserts the file name of buffer fred into the current buffer joe:
(switch-to-buffer "joe") (use-variables-of-buffer "fred" (insert-string current-buffer-file-name) )
arrays
You create an array by using the array MLisp function, and assign the resulting structure to a variable. The target variable is then coerced into an array.
Once the array has been created, you can use all the functions defined below to manipulate the array contents.
You can finally free up the space allocated to array by assigning one of the basic types to the array variable.
(array low-bound high-bound...)
Creates an array with up to 10 dimensions. The array is initialised to integer zeroes in all elements.
(bounds-of-array array-name)
Analyses an array, and returns another array describing the bounds of the supplied array.
The output array is declared as (array 1 2 0 input-array-dimensions). Element [1,0] contains the number of dimensions of the array. Element [2,0]contains the total size of the array. Other elements contain the lower and upper bounds for each dimension of the array.
(fetch-array array index...)
Retrieves an element from the specified array.
(setq-array array index... value)
Assigns a value to an element of an array.
defining functions
A function is the object through which Emacs runs programs written in MLisp. A function is defined, then compiled, and may then be run simply by nominating it using the ESC-x command, or by calling it in an MLisp program. MLisp functions may also have key sequences bound to them. In this way, key bindings may be altered in a way suitable for a given application.
Functions are defined using the MLisp defun function using either of the following syntaxes:
(defun (name local... expression... ) )
This defines a function called name with local variables locals. The function executes expressions each time it is called. The local variables are separated by white-space. Any arguments that need to be passed to the function must be accessed using the arg MLisp function. All the components except name are optional. The result of the function is the result of the last expression executed.
(defun name (arg...) local... expression... )
This defines a function with arguments. The arguments are entered into the variable name-space, and may have optional default expressions, which are evaluated if the corresponding argument is not passed in. The initialising expressions are executed just before the function in invoked. The result of the function is the value of the last expression executed in the expression list.
Example of defun with arg1 defaulted to 6, arg2 defaulted to the string "hello" and arg3 defaulted to the value of (get-tty-string "arg3: "):
(defun name (arg1 6 arg2 "hello" arg3 (get-tty-string "arg3:")) local... expression... )
block structuring functions
This section describes all the block structuring functions in MLisp. All the block structuring functions introduce a block which may contain local variable declarations. The value of most blocks is the value of the last expression executed in the block.
(progn local... expression...)
Introduces a basic block.
(save-excursion local... expression...)
Introduces a block, and saves the current environment for restoration after the last expression has been executed. The items saved are the current position of dot, the current buffer name, the current mark (if set), the last specified search string, and the visibility of the current buffer on the current screen.
(save-window-excursion local... expression...)
Introduces a block, and saves the current environment for restoration after the last expression in the block has been executed. The items saved are the screen state, the current buffer, the current position of dot, the current mark (if set) and the last search string.
After the expression has been executed, the windows that were visible on the screen when the command was started will be restored in the correct order with each window's dot in the correct location.
(save-restriction local... expression...)
Introduces a block, and saves the current environment for restoration after the last expression has been executed. The items saved are the current buffer, the current position of dot, the current mark (if set), the current narrowing, the last search string, and the visibility of the current buffer on the screen.
(error-occurred local... expression...)
Introduces a block, and evaluates expressions until an error occurs. If no error occurs, then the value of the block is 0, otherwise, the value of the block is 1. When an error occurs, the text of the error message is available in the system variable error-message, but is not displayed in the Mini-buffer.
flow control functions
This section describes the functions provided to control the flow of execution of MLisp statements.
(while test-expression expression...)
This function implements a pre-tested while loop statement. The test-expression is evaluated, and if non-zero, the expressions are executed. When the expressions have been executed, control returns to the test expression. The test expression is re-evaluated at each iteration. The result of the expression is the result of the test expression at exit.
(if test-expression true-expression false-expression)
This function implements an if-then-else statement common in most high level languages. test-expression is evaluated, and if non-zero, true-expression is evaluated, otherwise false-expression is evaluated.
A multi-way if statement can be constructed by repeating test-expression and true-expression in place of the false expression. The last test in the list must have a false-expression. For example, for the variable a, the following prints out the power of ten if a is less than 1000:
(message (if (< a 0) "a is too small" (< a 10) "Zero" (< a 100) "One" (< a 1000) "Two" "a is too large" ) )
operator functions
Emacs has numerous operator functions specifically designed for use in MLisp programs.
There is no distinction between an operator function and an ordinary function as far as the syntax of calling the operators is concerned. They are called operator functions merely to distinguish them from functions whose values are of no interest to the caller.
arithmetic operator functions
The arithmetic operator functions are shown in Table 3.
Table 3 Arithmetic Operator Functions
+ | Addition |
-- | Subtraction |
/ | Division |
* | Multiplication |
! | Complimentation |
& | And |
| | Inclusive-or |
^ | Exclusive-or |
<< | Left shift |
>> | Right shift |
= | Equality |
!= | Non-equality |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
c= | Character equal |
All of these functions can be used with one or more arguments. Arguments are used singly or in pairs to form a running total. The only unary operator is ! (complimentation).
string operator functions
The string operator functions include:
(concat expression...)
This function returns the concatenation of all of expressions as a single string. The expressions are coerced into strings before the concatenation takes place.
(substr str pos n)
This function returns the substring of str starting at character pos of length n. If pos is negative, the length of the string is added to it before it is used.
(length expression)
This function returns the string length of expression. If the expression yields a numeric or mark value, it is coerced into a string.
(region-to-string)
This function returns the region in the current buffer as a string value.
(case-string-capitalize string)
This function converts the first letter of each word in string to upper-case and all other letters to lowercase and returns the result as a string.
(case-string-invert string)
This function inverts the case of each letter in string and returns the result as a string.
(case-string-upper string)
This function changes the case of each letter in string to upper-case and returns the result as a string.
(case-string-lower string)
This function changes the case of each letter in string to lowercase and returns the result as a string.
other useful operator functions
The following list includes other useful operator functions:
(dot)
dot returns a marker for the current position of dot in the current buffer.
(mark)
mark returns a marker for the current position of the current buffer's mark if the mark is set. An error is generated if no mark is set.
(preceding-char)
preceding-char returns the numeric value of the character to the left of dot in the current buffer. If dot is located at the beginning of the buffer, 0 is returned.
(following-char)
following-char returns the numeric value of the character to the right of dot in the current buffer. If dot is located at the end of the buffer, 0 is returned.
miscellaneous mlisp functions
Emacs contains a set of functions and variables which are primarily for use when programming in MLisp. Some of the functions actually check when they are called that they have not been executed interactively.
predicates
The following predicates are provided to test certain conditions in MLisp programs:
(eolp)
eolp returns true if dot is currently at the end of a line in the current buffer. Otherwise, it returns false.
(eobp)
eobp returns true if dot is at the end of the current buffer. Otherwise, it returns false.
(bolp)
bolp returns true if dot is currently at the beginning of a line in the current buffer. Otherwise, it returns false.
(bobp)
bobp returns true if dot is at the beginning of the current buffer. Otherwise, it returns false.
(dot-is-visible)
dot-is-visible returns true if the current location of dot is being displayed in a window on the screen. Otherwise, it returns false.
(interactive)
interactive returns true if the currently executing MLisp function was invoked either by using ESC-x, or as a result of a key binding. Otherwise, it returns false.
(is-bound name...)
is-bound returns true if all the specified variables currently bound to storage. Otherwise, it returns false.
accessing prefix and postfix arguments
Emacs has two argument systems: prefix arguments, which are always numerics; and post-fix arguments which may be numerics, strings, markers, arrays or window-rings. Prefix arguments are generally specified interactively from the keyboard using the ^U command, but may also be manipulated from within an MLisp program by using the following functions and variables:
(provide-prefix-argument prefix expression)
This function applies the numeric prefix argument prefix to the expression provided. The result of the function is the result of the execution of the expression. It is generally more efficient to use provide-prefix-argument to repeat a command many times than to use a while loop, or by repetition of the desired command. However, the command to be executed must interpret a prefix argument as a repetition count in order for this to work properly.
(prefix-argument-loop expression...)
prefix-argument-loop causes the expressions provided to be executed prefix argument number of times. If no prefix argument was provided to the MLisp function that calls prefix-argument-loop, the expressions will only be executed once. The result of the function is the result of the last expression in the expression list to be executed.
(return-prefix-argument expression)
This function sets up the prefix argument for the next function to be executed. The value of the function is the value of the expression returned as the prefix argument.
If you wish to provide a prefix argument for the next function to run after the current function has finished, return-prefix-argument must be the last expression to be executed.
(prefix-argument)
This variable contains the value of the prefix argument supplied to the current MLisp function. If no prefix argument was supplied, then its value is 1. Both positive and negative prefix arguments may be supplied.
prefix-argument-provided
This variable is true if an MLisp function has been called with a prefix argument, otherwise it is false. This variable is provided because there is no way to tell if a prefix argument has been provided by merely examining its value.
(arg num prompt)
This function causes the evaluation of the numth postfix argument provided to the function. If the required argument is an expression, then it will be re-evaluated every time arg accesses it. The left-most argument in a function call is argument number 1.
If the desired argument was not provided in the call, prompt is used as a terminal prompt string to read in the argument from the user. If prompt is omitted, this feature is disabled, and an error message is generated.
(nargs)
This function returns the number of arguments supplied to the called function. Arguments that are prompted for are not included in this count.
functions for obtaining user input
A number of functions exist to enable MLisp programmers to obtain input from the user. These functions generally take a string argument which is used as a prompt in the Mini-buffer. Specific functions may enable name completion.
The input functions are:
(expand-from-string-table prompt word-list-string)
This function provides access to the Emacs input expansion mechanism. The function has two parameters: the first is a prompt, the second is a string that contains a space-separated list of words from which the user can select a choice. The result of the function is the selected word. expand-from-string-table will only allow one of the words in the list to be returned. The following example, causes a Command: prompt to appear in the Mini-buffer; the user must then enter one of the four words from the supplied list:
(expand-from-string-table "Command: " "exit" "read" "write" "quit")
(get-tty-character)
This function waits for a single key to be pressed by the user. No prompt is available. The character that the user presses is returned as the value of the function. No keymaps are used to interpret the typed character.
(get-tty-command prompt)
get-tty-command issues the prompt in the Mini-buffer and then reads in a string. Function name completion is enabled during this operation, and the only valid input is an existing function name. The value of this function is a string containing the name of the function selected.
(get-tty-file prompt)
get-tty-file issues the prompt supplied in the Mini-buffer and then reads in a string. File name completion is enabled during this operation, although any string is valid when a Return is pressed. Multiple versions of the same file are ignored during completion. The value of this function is a string containing the name of the file selected.
(get-tty-string prompt)
This function issues the prompt in the Mini-buffer, then reads in a string from the user. This string is returned as the function's value.
(get-tty-variable prompt)
get-tty-variable issues the prompt in the Mini-buffer and then reads in the name of a variable. Variable name completion is enabled during this operation. The value of the function is a string containing the variable name selected.
(get-tty-buffer prompt)
This function issues the prompt and then obtains the name of a buffer using buffer name completion. The value of the function is a string containing the buffer name selected.
(process-keys keymap)
This function reads one key sequence from the keyboard by using the supplied keymap. The function selected by the key sequence is executed, and the result is returned as process-key's value.
functions for displaying messages
Two functions exist for displaying text. Both functions display the text in the Mini-buffer, and can take any number of arguments. The arguments are coerced into strings and concatenated in the order in which they appear as arguments.
(message string...)
Displays the concatenated strings in the Mini-buffer the next time the screen is updated.
(error-message string...)
Displays the concatenated strings in the Mini-buffer and declares an error. No further statements of an MLisp program will be executed, and the keyboard processor will resume operation (unless error-message has been issued from inside an error-occurred function).
(sit-for length)
This function causes the display to be updated, and executes a pause for length tenths of a second. The pause may be interrupted by the user pressing a key.
This function is useful when displaying status information to the user. For example, if a function takes some time to execute, it may be helpful to indicate what the function is currently doing. An argument of zero to sit-for will update the screen, but will not pause Emacs processing.
how to compile an mlisp program
Once you have written some MLisp, the code must be compiled before it can be executed. There are several ways to compile MLisp code:
- If the MLisp is located in a buffer in Emacs, make the buffer current and use the ESC-x execute-mlisp-buffer command. This command takes the Mlisp expressions in the buffer, compiles and executes them.
- If the MLisp is in a file, you can use the ESC-x execute-mlisp-file command. If no file type has been specified, then .ML is assumed. This command first searches for the specified file using the EMACS_PATH logical name. If no file is found, then a search is made in the database MLisp-library (if the file type is .ML, this is first removed). If no module is found in the database with the specified name, an error message is displayed.
When the file has been opened, all the MLisp expressions contained in the file are read in, compiled, and executed.
- If you wish to type a one-line MLisp program, you can use ESC-x execute-mlisp-line. This prompts in the Mini-buffer for a single MLisp expression to compile and execute. When the expression has been executed, execute-mlisp-line prints the returned value of the expression in the Mini-buffer.
execute-mlisp-line can also be used from MLisp programs to dynamically compile and execute a string. When called non-interactively, a single string argument can be specified. This string is compiled and executed as above, and the result returned as the result of the execute-mlisp-line function.
- If you wish to type a short MLisp program, you can use the MINIBUF package. See the Barry's Emacs Extensions Reference Manual for a description of the MINIBUF package.
libraries of commands
A considerable number of the functions in Emacs reside in libraries. A library function is not accessible unless the library that contains it is loaded. A number of libraries are provided with Emacs, and can be loaded automatically or on request to make their functions available.
To load a library, use execute-mlisp-file. Emacs loads the library by first looking in your own directory for the library file (or a directory you specify), then in the Emacs directory, then in the MLisp-library database search-list. Once the library has been loaded, all the functions defined in the library are then available for use.
For example, if you load the AREA library, you can then use the delete-area function to delete an area of the screen. delete-area is defined in the AREA library.
In addition to making functions accessible to ESC-x, the library may bind some functions to key sequences. This is done by the library loading another file called LIBRARY-NAME.KEY, where LIBRARY-NAME is the name of the library being loaded.
Some libraries are loaded automatically in the course of executing certain functions. You will not normally notice this. For example, the PROCESS library is automatically loaded when you use the shell function for the first time. This facility is known as autoloading. Autoloading is used to make library functions available without you having to know how to load the library. Autoloading also saves space in Emacs by loading libraries only when you need them.
Autoloading works by simply calling execute-mlisp-file on the required library. Autoloading is set up using the autoload function. For example, if you have a function named box-it which is defined in the file BOXIT.ML, then the command:
(autoload "box-it" "boxit.ml")
will define the box-it function, but will not actually load the definition from BOXIT.ML until you execute the box-it function.
debugging mlisp programs
Emacs provides a powerful debugging facility to help you develop your MLisp programs. This section describes the supplied MLisp debugger and the tools that Emacs provides to support the development of new debuggers.
the debug package
The DEBUG package is a powerful MLisp source-line debugger, providing the MLisp programmer with tracing, breakpointing, error trapping and stack-dumping facilities.
To start DEBUG, load the DEBUG library. Each time you want to enter DEBUG mode, use the ESC-^D command (or execute the ESC-x debug command). The debug function loads up the breakpoint, error and trace handlers, binds debug commands to keys and pops up the DEBUG windows. Source code and other information displayed by DEBUG will appear in these windows.
The following commands are available in the DEBUG window:
b (set breakpoint)
Sets a breakpoint on the specified function. When this function is executed after the breakpoint has been set, it will cause DEBUG to regain control, and DEBUG will display the decompiled source showing the call to the function.
B (remove breakpoint)
Clears the breakpoint flag for the specified function so that it will no longer cause a breakpoint when executed.
c (show calls)
Displays the current call stack in the DEBUG window.
. or KP5 (display current source)
Displays the current source line, in context, in the Debug window.
e (interrupt on error)
Causes DEBUG to trap error reports and display them and the function that generated them. Errors are not trapped by default.
E (kill error trapping)
Disables error-trapping.
g or KP, (go)
Continues the execution of a program without interference from DEBUG. The only way DEBUG will regain control is at a breakpoint or when an error is detected and error-trapping is enabled.
Use this function when you have finished examining execution at one point in your program, and you wish to continue the program.
h or ? or Help (help)
Displays help on DEBUG commands.
l (list breakpoints)
Lists in the DEBUG window all functions which have their breakpoint flag set.
o or KP0 (step over)
Executes the next MLisp expression and then stops in DEBUG afterwards. However, if the expression is a function call, DEBUG executes all of the expressions in the called function and any functions it calls.
p (print)
Prints the contents of a variable.
q (quit)
Exits DEBUG. The DEBUG windows are removed and all variables controlled by DEBUG are reset. Breakpoints remain intact, but will not cause DEBUG to be run. To restart debugging, use the ESC-^D command.
s (step)
Executes the next MLisp expression.
t (set trace mode)
Enables trace-mode. During trace-mode, every MLisp expression that is executed will be displayed in the DEBUG windows for debug-sit-for tenths of a second. Execution will continue until all MLisp expression have been executed.
T (Unset trace mode)
Unsets trace-mode.
v (View last expression)
Prints the value of the last executed expression.
DEBUG never displays debug information about any of the DEBUG functions or MLisp functions that are called while in the DEBUG window. This stops DEBUG from confusing you.
tools for building mlisp debuggers
This section describes the components which can be used to build MLisp tracing and debugging packages.
Four facilities are provided with Emacs for debugging programs written in MLisp: breakpoints, error-trapping, tracing and stack-trace generation.
Breakpoints
Each function within Emacs can have a breakpoint applied to it. If, during the course of executing functions, Emacs attempts to execute a function which has a breakpoint, it calls the breakpoint function specified in the breakpoint-hook variable.
Breakpoints are set for a function using the ESC-x breakpoint command. This command asks for the name of a function and the value to which the function's breakpoint flag should be set. The execution of a function causes a breakpoint if the function's breakpoint flag is set and if the variable breakpoint-hook contains the name of an Emacs function.
The breakpoint function is called just before the function that caused the breakpoint to occur is called.
decompile-current-line can be used to display the environment from which the breakpointing function was called.
Error Trapping
If an error occurs during the execution of MLisp code, the normal action is to display the error message in the Mini-buffer, and to abort the execution of all macros and MLisp code.
However, if the variable error-hook contains the name of a function, then this function is called instead. This means that a debugger can gain control when an error occurs. Further, during the execution of the error function, the error message that should have been displayed is available to the debugger in the variable error-message.
Error trapping does not occur for errors that happen in an error-occurred block.
Trace Mode
Tracing of MLisp programs is controlled with three variables:
· trace-into
· trace-mode
· trace-hook
When trace-mode is set, the function whose name is contained in trace-hook is called before each MLisp statement is executed. The decompiled form of the statement about to be executed is available using the decompile-current-line function.
On MLisp function calls, trace-into controls whether tracing continues into the called code. If trace-into is true, tracing continues into functions. Otherwise, tracing will be suspended until the inner function has completed execution.
Stack Tracing
The generation of an MLisp stack trace can be automatic when an error is detected, or can be generated by the user at any time from an MLisp function.
The variable stack-trace-on-error controls the generation of stack-traces when an error is detected. If set to true, when an error is generated, a buffer called Stack-trace is created, and the current MLisp stack is inserted into the buffer. No action is taken inside an error-occurred block. The first line of the stack trace contains the error message that caused the stack-trace to occur. The second and subsequent lines contain the decompiled expressions being executed when the error was detected. An end-of-stack marker is located at the end of the buffer.
The variable stack-maximum-depth limits the call depth of the running MLisp code.
For example, if the following function is executed with a parameter of 1:
(defun x (a) (if (> a 3) (error-message "Too deep") (x (+ a 1)) ) )
then the buffer Stack-trace would contain:
Message: Too deep Executing: (error-message "Too deep") (x (+ a 1)) (x (+ a 1)) (x (+ a 1)) (x 1) --- bottom of stack ---
The function dump-stack-trace can be used to generate a stack-trace at any time. The stack-trace will be located in buffer Stack-trace, which will be popped onto the screen. The stack-trace format is the same as that displayed for stack-traced errors, except that there will never be a Message: line.
Extension Facilities
This chapter describes the various useful facilities which enable complex extensions to be built using MLisp programs.
keymaps and key bindings
The facilities for binding command key sequences to functions are very general in nature. When Emacs is accepting commands from the terminal, it translates those commands into Emacs functions. Every command key sequence will result in the execution of one function---either MLisp, a macro, an autoloaded function or some wired-in code.
A keymap is the entity that tells Emacs what to do when a given key is typed by the user. It will either indicate that a function should be called, or that another keymap should be used to obtain another character in a multicharacter command sequence.
For every buffer, there are two routes to follow for command key decoding; the global keymap and the buffer's local keymap . Command key sequences that are defined by the global keymap may be used from any buffer in Emacs, while command key sequences defined in local keymaps may only be used from buffers that associate with the keymap.
When accepting commands, Emacs makes transitions across the two trees of keymaps as though they were finite state automata. Keymaps can be regarded as look-up tables to make transitions across these automata. Each table contains 256 entries---one entry for each eight-bit ASCII character.
Keymaps have names which are entered into the function name-space. This means that it is quite legal to execute a keymap as a function. If you do this, the next key sequence input by Emacs will be translated starting with the executed keymap. If you execute more than one keymap, the last keymap executed is used to start the keymap translation.
manipulating keymaps
Several functions and variables can be used to manipulate keymaps. These include the following:
(define-keymap name)
This function explicitly declares a keymap and initialises its contents. The keymap's name is entered into the function name-space. Once the keymap has been declared in this way, it can be explicitly nominated as a local or global keymap.
(use-global-map name)
Uses the nominated keymap as the global keymap for all subsequent command input. name must be an existing keymap.
(use-local-map name)
Uses the nominated keymap as the local keymap for the current buffer. Any previous local keymap association is removed (though the keymap itself will not be destroyed).
current-local-keymap
This variable contains the name of a buffer's current local keymap. The name it contains may be used with a use-local-map function.
default-global-keymap
This variable contains the default global keymap that Emacs initialises and uses when it starts up. It contains the standard command key sequences plus any global alterations the user has made.
command binding
Functions may be bound to any arbitrary key sequence using the functions described as follows:
(bind-to-key name keys)
Makes a global binding to a function. The function binding will be usable from any buffer. The function name must have already been declared in the function name-space. keys can be an arbitrary sequence of characters used to access the function. The following example binds the function set-mark to the Select key:
(bind-to-key "set-mark" "select)")
If keys is a character constant, then the binding is entered into the current global keymap. If keys is a string, then enough keymaps are automatically generated to define the entire path to the function. Automatically generated keymaps have names of the form auto-defined-keymap-n, where n is a sequence number.
If the key sequence specified is a control sequence, it must be a valid ISO control sequence. For example, the key sequence ESC-Select is not a valid ISO control sequence because it generates two consecutive ESC characters. However, Emacs allows you to bind commands to such sequences by providing the convert-key-string function. convert-key-string will convert a control sequence according to Emacs' control-string processing rules to a valid ISO control sequence. For example, to bind the unset-mark function to the ESC-Select key sequence, use the convert-key-string function as follows:
(bind-to-key "unset-mark"
(concat
""
(convert-key-string "select)")
)
)
(local-bind-to-key name keys)
Defines a local binding to a function. The binding is entered into the current buffer's local keymap. Character and string keys parameters are treated as described under bind-to-key. If a buffer does not have a local keymap, an error is reported.
(global-binding-of keys)
Returns a string containing the name of the function defined by the keys string using the current global keymap.
(local-binding-of keys)
Returns a string containing the name of the function defined by the keys string using the current buffer's local keymap.
(remove-all-local-bindings)
Removes all the local bindings in the current buffer's local keymap. The local keymap itself is not destroyed.
(remove-binding keys)
Removes the association of a function to a command key sequence from the global keymap.
(remove-local-binding keys)
Removes the association of a function to a command key sequence from the local keymap.
databases
Emacs provides a comprehensive database management system.
The key components of the database system are database search-lists. These are ordered lists of database files. When data is to be fetched from a database search-list, Emacs scans all the files in the list until it either finds a key match, or the list is exhausted. When Emacs writes data to a database search-list, the data is always inserted into the database at the top of the specified search-list.
You can use any text as the key for a database entry. However keys used in the MLisp-library should conform to the rules for file names and not use upper case letters.
Certain optimisations are made to ease the problems of multiple invocations of Emacs using the same library files. Library files in a database search-list will always be closed and re-opened between each library operation. This may slow down library searches, but it does ensure that data in the library is not cached inside Emacs, so that other Emacs users can access the library.
Database files are of two types: read-only, and read-write. You control which type of access is required by specifying parameters to Emacs. If a database file is protected against writing, read-only access will be granted if possible.
Emacs uses the following database search-lists:
info
This list is used by the INFO subsystem to provide pages of information. The INFO database describes which files are accessed.
language
This list is used by the language modes C Mode and Bliss Mode as the source of language construct templates. The templates supplied with Emacs are found in the file EMACS_LIBRARY:EMACSLANG.TLB. During initialisation of the language subsystems, your language template file SYS$LOGIN:LANGUAGE-TEMPLATES.TLB is added to the search-list so that you can override the system templates with your own templates.
MLisp-library
This list provides database access to stored MLisp programs. All the distributed MLisp functions are stored in the file EMACS_LIBRARY:EMACSLIB.TLB. This file is visible in the search-list. To provide for user extensions and customisations, Emacs will also add the file SYS$LOGIN:EMACSLIB.TLB (if it exists) to the search-list during initialisation.
describe
This search-list is used by the describe-command, describe-variable and Help functions. The files in the list contain the Emacs help database. This search-list can grow as new packages are added into the running Emacs.
subr-names
This search-list is used by the describe-word-in-buffer function as the source of one-line information about words. It contains the supplied quick-information database files and the user's SYS$LOGIN:SUBRNAMES.TLB database if it exists.
database functions
The following commands are provided to manipulate database search-lists and to access the data contained in them:
(extend-database-search-list dbname dbfile access)
This command adds the database file specified to the nominated database search-list. The file's writeability is checked and a flag is set to indicate the file's type. If the database search-list does not exist, it is created. If the file already exists in the search-list, it is not added again. access is an optional argument containing access flags. The flags are specified as a bit-mask with the following meaning:
- 1 (read-only) --- indicates that read-only access is required
- 2 (noclose) --- indicates that the library should remain open across library accesses.
(remove-database dbname dbfile)
Removes the dbfile database file from the dbname database search-list. No error message is generated if the file does not exist. The database search-list will remain even if the last file is removed.
(list-databases)
Creates a buffer called Database list and inserts the names and file names of all the database search-lists. This buffer is then displayed in a window.
(put-database-entry dbname key)
Inserts the current buffer into the first library in the designated database search-list using the specified key. You cannot use put-database-entry on a read-only database. Completion is enabled for both the database search-list name and the key.
(fetch-database-entry dbname key)
Fetches the contents of the designated module and inserts the data into the current buffer at dot. Dot remains in the same place. Completion is enabled for both the database search-list name and the key. If the key does not exist, an error is generated.
controlling processes from emacs
Emacs has a set of functions for controlling subprocesses. These processes are created by Emacs and can execute any shell to accept commands. You designate a name for each subprocess and use this name when you want to refer to it.
The number of concurrently running subprocesses is limited only by the resources available to you.
functions for controlling processes
(start-process process-name)
Creates the named subprocess. process-name must not already exist. The current process is changed to the newly-created process.
(kill-process process-name)
Deletes the named subprocess. If the name is null, the current process is deleted. When the process has actually terminated, the process termination procedure will be called. After the termination procedure has finished executing, the process name will be removed from the process name-space.
(list-processes)
Displays a buffer containing the name and state of each existing process. A time value is also displayed; this is set to the time the last state-transition occurred. The associated output buffer, input and output procedures are also displayed.
The possible process states include:
- RUNNING --- indicates that an image is running in the process
- PAUSED --- indicates that the process has been explicitly suspended
- IN-WAIT --- indicates that the process has asked for input
- OUT-WAIT --- indicates that the process has generated some output, and that Emacs is waiting to process it
- DEAD --- indicates that the process has terminated, and will soon be removed from the process list.
(pause-process process-name)
Allows a given process to be temporarily suspended by sending a VMS SUSPEND request to it. A paused process will remain paused until it is explicitly resumed or killed.
(resume-process process-name)
Resumes a process which has been paused with pause-process.
(force-exit-process process-name reason)
Causes the image running in the specified process to be terminated with the specified status code. Processes that are not running in user mode will not be affected by the exit until they resume execution in user mode. If the process is not running an image, the force exit will be issued on the next image run in the process.
(set-process-termination-procedure process-name function)
Associates an Emacs function with a process. The associated function will be called when the process terminates (not when an image exits). When the associated function is called, the current process is temporarily set to the associated process.
(current-process-name)
Returns a string containing the current process name. This name will be either the name of the last created process, the name of the process which was a target of the set-current-process function, or the name of the associated process for functions called as a result of process input, output or termination.
(set-current-process process-name)
Changes the current process to the process with the specified name. If you change the current process name in an associated input, output or termination function, the effect will only last for the invocation of that function.
input and output to processes
The following functions exist to make input and output to and from processes convenient.
For output, the data produced by the process can be inserted into a buffer associated with the process. This provides a convenient way for you to see the output. If, however, some processing of the output is required, then an Emacs function can be automatically called to obtain the output when it is available and perform the required processing.
The functions available for output include:
(set-process-output-buffer process-name buffer-name)
Associates a buffer with a process. When output is available from the process, it is inserted at the end of the buffer. The screen will be updated if the buffer is associated with a window.
If the number of characters exceeds the maximum for the process, characters will be deleted from the beginning of the buffer. This feature protects you from creating huge process buffers.
(set-process-output-procedure process-name function)
Associates an Emacs function with a process. This function will be called once for every line that is produced by the process. It is assumed that the function will obtain the output line and process it in some way. When the procedure is called, the current process is temporarily set to the process that produced the output line.
(process-output process-name)
Returns the next output line from the process.
For input, the user makes lines of input available to the process. If the process does not require input, an error is generated. To allow for co-ordination of input, the following functions are provided:
(set-process-input-procedure process-name function)
Associates an Emacs function with the process. When the process requests some input, the designated function is called. It is assumed that the function will provide some input to the process. During the call of the associated function, the current process is temporarily set to the associated process.
(send-string-to-process process-name input-line)
Sends some input to the designated process. If that process is not requesting input, an error is generated.
(wait-for-process-input-request process-name)
Suspends Emacs execution until the designated process requests some input. Keyboard input will also cause this function to return to prevent Emacs from becoming wedged. When a process asks for input, the function returns 1; otherwise, it returns 0, indicating that the user pressed a key on the keyboard.
minor modes
Minor modes are used to alter the way Emacs performs an operation. All minor modes are independent of each other and of the selected major mode. The statuses of some minor modes are displayed in the mode lines for each window. Each minor mode is controlled by a variable that can be turned on or off. A non zero-value always turns the mode on, and an explicit zero value always turns the mode off.
The following minor modes are defined by Emacs:
Auto-fill Mode
This mode allows you to type text endlessly without worrying about the width of your screen. Line separators are inserted as required to prevent lines from becoming too long.
Auto-fill Mode is controlled by setting the variable right-margin. When an attempt is made to insert a word character passed the right margin, auto-filling is invoked.
Checkpoint Mode
Checkpoint Mode protects you from system crashes by periodically saving the buffers you are working with. Whenever you visit a file, checkpointing is enabled if the variable checkpoint-frequency is non-zero.
Checkpointing for individual buffers is also controlled by the buffer-specific variable current-buffer-checkpointable.
Journal Mode
Journal Mode protects you from system crashes by writing a journal of all the changes that are made to modified buffers. When you visit a file, journalling is automatically enabled if the variable journal-frequency is non-zero.
Journalling for individual buffers is controlled by the buffer-specific variable current-buffer-journalled.
Replace Mode
Replace Mode causes ordinary printing characters to replace existing text rather than be inserted. For example, if dot is in front of the B in FOOBAR, then in Replace Mode, typing a G changes it to FOOGAR, instead of making it FOOGBAR as usual.
This mode is controlled with variable replace-mode.
Abbreviation Mode
Abbreviation Mode allows you to define abbreviations that automatically expand as you type them. For example, am might expand to Abbrev mode. The abbreviations also may depend on the active major mode.
This mode is controlled by the variable abbrev-mode.
Read-only Mode
Read-only Mode allows you to protect the data in a buffer from being altered. When Read-only Mode is enabled, attempts to insert characters into the buffer are ignored and cause the bell to be sounded.
This mode is controlled by the variable read-only-buffer.
the mode line
As with most things in Emacs, the information in the mode line is customisable. A buffer-specific variable mode-line-format exists for this purpose. mode-line-format contains a format string which governs the way that the mode line information is displayed. It contains literal text and embedded format effectors. The literal text will be displayed in the mode line as it is typed, while the format effectors will be expanded with the appropriate information. You normally alter the default value of this variable (default-mode-line-format) so that when a new buffer is created, it displays your customised mode line immediately.
The general form of the format effectors is %nx. n is the width of the string to be inserted and causes truncation or padding on the right. x is one of the letters in the following list:
a
Inserts the string Abbrev if Abbreviation Mode is on, otherwise it inserts nothing.
b
Inserts the current buffer's name.
c
Inserts the string Checkpoint if checkpointing is generally enabled and also enabled for the current buffer, otherwise it inserts nothing.
f
Inserts the current file name for buffers associated with files, or [No File] for scratch buffers.
h
Inserts the string <n< if the horizontal position of the window on the current buffer does not include column one. n indicates by how many characters column one is offset from the left-hand side of the window.
j
Inserts the string Journal if journalling is enabled for the buffer, otherwise nothing is inserted.
m
Inserts the value of the buffer-specific variable mode-string. This variable defines the major mode for the buffer.
M
Inserts the value of the global variable global-mode-string which defines the name of the currently running subsystem.
p
Inserts the position of dot in the current buffer as a percentage. If dot is at the beginning of the buffer, the string Top is inserted. If dot is at the end of the buffer, the string Bottom is inserted.
r
Inserts the string Overstrike if Replace Mode is currently enabled.
R
Inserts the string Readonly if Read-only Mode is currently enabled.
*
Inserts a asterisk if the current buffer has been modified since it was last written to disk, otherwise it inserts nothing.
[ and ]
Inserts zero or more square brackets depending on the depth of recursive editing. One bracket is inserted per level when the recursive edit depth is less than 5. When the edit depth is 5 or greater, the string [n] is inserted, where n is the recursive edit depth.
The default value of mode-line-format is:
%[%hBuffer: %b%* File: %f %M (%m%c%r%a) %p%]
Syntax Tables
The effects of all the Emacs commands which parse words or balance parentheses are controlled by syntax tables . Each ASCII character is represented by one entry in each syntax table. Each buffer has a syntax table associated with it. The default syntax table is called global-syntax-table. The name of the current syntax table (the syntax table associated with the current buffer) may be accessed through the variable current-buffer-syntax-table, and this table may be changed either by setting the variable, or by using the function use-syntax-table.
To alter a syntax table, the function modify-syntax-entry is provided. This command takes a single string argument which describes the attributes to be applied to the specified characters as follows:
· The first character is one of the following:
w
A word character, as used by such functions as forward-word and case-word-capitalize.
Space
A character with no special interpretation.
(
A open parenthesis. Typical candidates for this type are the characters (, [ and.
Characters of this type also have a matching closing parenthesis specified which appears as the second character of the parameter to modify-syntax-entry.
)
A close parenthesis. Typical candidates for this type are the characters ), ] and. Characters of this type also have a matching open parenthesis specified which appears as the second character of the parameter to modify-syntax-entry.
"
A double-quote character. The C string delimiters " and ' are usually given this class.
\
A prefix character, like \ in C.
· The second character of the string is the matching parenthesis if the first character is of the open or close parenthesis type. If you specify that ( is a left parenthesis matched by ), you should also specify that ) is a right parenthesis matched by (.
· The third character, if a , indicates that the characters described by this syntax entry begin a comment.
· The fourth character, if equal to , indicates that the character described by this syntax entry ends a comment.
· If either the beginning or ending comment sequence is two characters long, then the fifth character provides the second character of the comment sequence.
· The sixth and following characters specify which characters are described by the entry; a range of characters can be specified by using the sequence specifier a-b, where aand b are ASCII characters and a is less than b. -- can be described if it appears as the sixth character.
Attribute entries that are not relevant should be spaces.
For example:
(modify-syntax-entry "w -")
makes - behave as a word character;
(modify-syntax-entry "(] [")
makes [ behave as a left bracket which is matched by ].
The function list-syntax-tables is useful for finding out the names of all the syntax tables known to Emacs. Use dump-syntax-table to dump the contents of a syntax table. This function is very useful when trying to discover why a syntax table is not working as expected.
Emacs database files
Each Emacs database is stored in a set of three files whos name is the name of the database and whos extentions are .DIR, .PAG and .DAT.
The Emacs database commands use these database files.
You can maintain these database files using the Emacs support tools.
dbcreate db-name [-c]
This command will create a database named 'db-name'. Use the options -c parameter to force an empty database to be created. Normally dbcreate will report an error if you attempt to create a database that already exists.
dbadd db-name key <input-file
Add a new entry to database 'db-name'. The entry will have the key 'key' and the contents of the entry will be taken from standard input. Use redirection to as above to add the contents of a file.
dbdel db-name key
Delete the entry named 'key' from the database 'db-name'.
dbprint db-name key >output-file
Fetch the contents of entry 'key' from the database 'db-name'. The contents of the entry are output to standard output. Use redirection to save the contents to a file, as show above.
dblist db-name
Print a list of all the keys in the database names 'db-name'.
Emacs Files and logical names
Special Emacs Files
This section contains a list of all of the special files Emacs may access while it is running.
- sys$login:emacsinit.ml -- This is your private start-up file. The file is executed as a series of MLisp expressions as part of the Emacs initialisation process.
- emacs_path:emacsinit.ml -- Alternative location for the emacsinit.ml file.
- emacs_user:emacs7_nt.env -- This file contains your private saved Emacs environment saved under Windows NT.
- emacs_user:emacs7_95.env -- This file contains your private saved Emacs environment saved under Windows 95.
- sys$login:emacslib -- This file is a database which contains your private library of MLisp programs. When the ESC-x execute-mlisp-file command is issued, this text library will be included in the search for a program.
- sys$login:subrnames -- This file is a database which contains your one-line descriptions of special words for the ESC-x describe-word-in-buffer command.
-
emacs_library:emacs_profile.ml -- This file is the Emacs system start-up file. It is executed as a series of MLisp expressions every time Emacs is started with the /norestore qualifier.
- emacs_library:emacslib -- This file is a database containing all the MLisp programs and libraries supplied with Emacs.
- emacs_library:emacslang -- This is the Emacs language template database for use with c-mode and bliss-mode. The default templates are in this file. See Editing Programs for more information on language-specific editing.
- sys$login:language-templates -- This is your personal language template database file. You put your own customised language templates for use with c-mode and bliss-mode in this file. See Editing Programs for more information on language specific editing.
Emacs Logical Names
Emacs uses a number of logical names. These names point to directories. For example, the logical name emacs_user points to the users emacs library directory. A file in there called profile.ml could be referred to as follows:
emacs_user:profile.ml |
If you are familiar with OpenVMS, you will realise that these logical names are the equivalent of OpenVMS logical names.
The standard logical names include the following:
- sys$login -- This is your personal directory. Under Windows NT is login in directory as defined by the system administrator.
- sys$scratch -- This is the directory that emacs creates temporary files in. Typically this is C:\TEMP.
- emacs_user -- This is your personal emacs directory. Under Windows NT it defaults to sys$login:\bemacs.
- emacs_checkpoint -- Emacs writes all checkpoint files to the directory specified by this logical name.
- emacs_info -- This name is used by the INFO subsystem to access its database.
- emacs_journal -- Points to the directory that Emacs uses to store journal files.
- emacs_library -- Is used to access files and databases nessesary to run emacs. For example it contains: emacsinit.ml, emacs_profile.ml and the supplied libraries and packages.
- emacs_local_library -- This optional logical name may be used to point at a local library of emacs programs and library files.
- emacs_memory -- Points to the location of the Emacs memory file. By default, this is the current directory. Define emacs_memory to be the null device to disable memory files. NUL is the name of the null device on Windows.
- emacs_path -- This name is used when Emacs attempts to load an MLisp library from a file. Emacs automatically sets the value of this logical name.
Its value is typically .;emacs_user:;emacs_library:. If emacs_local_library has been defined then emacs_path is .;emacs_user:;emacs_local_library;emacs_library:.
You can add your own logical definitions by editing the [DeviceNames] section in the file emacs060.ini.
Starting Up Emacs
This chapter describes the events involved in starting up Emacs, and the various facilities that you can use to change the way Emacs starts.
The Emacs Start-Up Procedure describes the Emacs start-up procedure. Emacs Initialisation Files describes how to use initialisation files to customise EMACS. Emacs Environment Files describes how to save customisations in environment files.
The Emacs Start-Up Procedure
This section describes in detail what happens when Emacs is initially started and re-started after an ESC-x pause-emacs command. The steps that are executed when returning to a paused Emacs are specifically noted.
This section describes in detail what happens when Emacs is initially started.
The following steps are executed in order:
- If the /restore qualifier has been specified, Emacs restores the specified environment file and initialisation continues from step 4.
- If /norestore qualifier is specified, Emacs executes the file emacs_library:emacs_profile.ml as a series of Mock Lisp (MLisp) expressions.
- emacs_library:emacs_profile.ml causes the file sys$login:emacsinit.ml to be executed as a series of MLisp expressions. If this file does not exist emacs searches the directories named in emacs_path for emacsinit.ml and executes the first one found instead.
- If the variable user-interface-hook contains the name of an MLisp function, then this function is executed. Normally you will leave this variable alone and let Emacs set it up for you.
- If Emacs is starting and the variable enter-emacs-hook contains the name of an MLisp function, then this function is executed.
- This is the hook you may used to execute code each time emacs starts up.
- If the /package=package-name qualifier is specified on the
Emacs command line then the designated package is loaded and run otherwise the
emacs package is loaded and run.
This is achieved by taking the name specified and adding
.mlp to it.
The resulting name is then used as the name of a file to execute as a series of MLisp expressions. When the file has been completely executed, the function package-name-com is called.
For example, a package called dired will have a file called dired.mlp that contains an MLisp function called dired-com.
- If the Emacs functions argc and argv have not been used since the Emacs command was issued, all the files specified on the command line are automatically visited.
- If no arguments were specified on the command line, Emacs attempts to restore the files that were displayed on the screen when Emacs last exited completely. This information is maintained in the emacs memory file. The contents of this file are private to Emacs, and should never be altered or edited. If the file does not exist, no files will be automatically visited. Under Unix the Emacs memory file is named emacs_memory:.emacs_mem. Under Windows the Emacs memory file is named emacs_memory:__emacs_.mem.
- Emacs now calls its display processor to display the screen, and then calls its keyboard processor to process keyboard commands.
Emacs Initialisation Files
An initialisation file is an MLisp program which is executed whenever you start Emacs. Emacs looks for an initialisation file using the file name emacs_path:emacsinit.ml or the MLisp program EMACSINIT.
An initialisation file can ask you questions and do things, as well as just setting up commands for later use.
The example shows a sample initialisation file.
Example: Sample EMACSINIT.ML
; ; Setup EMACS in the windows UI style ; (default-emacs-init "windows,windows-extended,ere-searching") ; ; Set up some variables ; (setq scroll-step 1) (setq visible-bell 1) (setq silently-kill-processes 1) (setq checkpoint-frequency 500) (setq unlink-checkpoint-files 1) (setq default-case-fold-search 1) (setq default-comment-column 41) ; ; Make some functions automatically load a library or package ; (autoload "add-tag" "tags") (autoload "goto-tag" "tags") (autoload "email" "email") ; ; Associate some commands with file specifications ; (auto-execute "lisp-mode" "*.ml") (auto-execute "text-mode" "*.tex") (auto-execute "c-mode" "*.c") ; ; Some re-wired key bindings ; (bind-to-key "goto-percent" "\^Xg") (bind-to-key "add-tag" "\[F2]") (bind-to-key "goto-tag" "\^XL") (bind-to-key "visit-function" "\^XP")