elisp: Syntax Table Functions
34.3 Syntax Table Functions
===========================
In this section we describe functions for creating, accessing and
altering syntax tables.
-- Function: make-syntax-table &optional table
This function creates a new syntax table. If TABLE is non-‘nil’,
the parent of the new syntax table is TABLE; otherwise, the parent
is the standard syntax table.
In the new syntax table, all characters are initially given the
“inherit” (‘@’) syntax class, i.e., their syntax is inherited from
the parent table (Syntax Class Table).
-- Function: copy-syntax-table &optional table
This function constructs a copy of TABLE and returns it. If TABLE
is omitted or ‘nil’, it returns a copy of the standard syntax
table. Otherwise, an error is signaled if TABLE is not a syntax
table.
-- Command: modify-syntax-entry char syntax-descriptor &optional table
This function sets the syntax entry for CHAR according to
SYNTAX-DESCRIPTOR. CHAR must be a character, or a cons cell of the
form ‘(MIN . MAX)’; in the latter case, the function sets the
syntax entries for all characters in the range between MIN and MAX,
inclusive.
The syntax is changed only for TABLE, which defaults to the current
buffer’s syntax table, and not in any other syntax table.
The argument SYNTAX-DESCRIPTOR is a syntax descriptor, i.e., a
string whose first character is a syntax class designator and whose
second and subsequent characters optionally specify a matching
character and syntax flags. Syntax Descriptors. An error
is signaled if SYNTAX-DESCRIPTOR is not a valid syntax descriptor.
This function always returns ‘nil’. The old syntax information in
the table for this character is discarded.
Examples:
;; Put the space character in class whitespace.
(modify-syntax-entry ?\s " ")
⇒ nil
;; Make ‘$’ an open parenthesis character,
;; with ‘^’ as its matching close.
(modify-syntax-entry ?$ "(^")
⇒ nil
;; Make ‘^’ a close parenthesis character,
;; with ‘$’ as its matching open.
(modify-syntax-entry ?^ ")$")
⇒ nil
;; Make ‘/’ a punctuation character,
;; the first character of a start-comment sequence,
;; and the second character of an end-comment sequence.
;; This is used in C mode.
(modify-syntax-entry ?/ ". 14")
⇒ nil
-- Function: char-syntax character
This function returns the syntax class of CHARACTER, represented by
its designator character (Syntax Class Table). This
returns _only_ the class, not its matching character or syntax
flags.
The following examples apply to C mode. (We use ‘string’ to make
it easier to see the character returned by ‘char-syntax’.)
;; Space characters have whitespace syntax class.
(string (char-syntax ?\s))
⇒ " "
;; Forward slash characters have punctuation syntax.
;; Note that this char-syntax call does not reveal
;; that it is also part of comment-start and -end sequences.
(string (char-syntax ?/))
⇒ "."
;; Open parenthesis characters have open parenthesis syntax.
;; Note that this char-syntax call does not reveal that
;; it has a matching character, ‘)’.
(string (char-syntax ?\())
⇒ "("
-- Function: set-syntax-table table
This function makes TABLE the syntax table for the current buffer.
It returns TABLE.
-- Function: syntax-table
This function returns the current syntax table, which is the table
for the current buffer.
-- Command: describe-syntax &optional buffer
This command displays the contents of the syntax table of BUFFER
(by default, the current buffer) in a help buffer.
-- Macro: with-syntax-table table body...
This macro executes BODY using TABLE as the current syntax table.
It returns the value of the last form in BODY, after restoring the
old current syntax table.
Since each buffer has its own current syntax table, we should make
that more precise: ‘with-syntax-table’ temporarily alters the
current syntax table of whichever buffer is current at the time the
macro execution starts. Other buffers are not affected.