Previous: Keymaps and Key Bindings, Up: Character Syntax and Key Bindings


6.1.2 Syntax

auto-completion-syntax-alist
Alist associating character syntax descriptors with completion functions. Used by the completion-self-insert function in auto-completion-mode to decide what to do based on a typed character's syntax.
auto-completion-override-syntax-alist
Alist associating characters with completion functions. Overrides the default function for a typed character's syntax. Used by completion-self-insert in auto-completion-mode.
completion-define-word-constituent-binding
Convenience command for use in your .emacs file. It is used to define key bindings that insert a character as though it had a different syntax. Usually used to allow punctuation characters to be inserted one-off as word-constituents. To define key as a binding to insert character char as though its were a word-constituent, use:
          (completion-define-word-constituent-binding key char)

To define key as a binding to insert character char as though its syntax class were syntax, use:

          (completion-define-word-constituent-binding key char syntax)

To ignore auto-completion-override-syntax-alist for this key binding, so that the behaviour is determined only by syntax, supply a non-nil third argument:

          (completion-define-word-constituent-binding key char syntax t)

When a character is typed in auto-completion-mode, predictive mode decides what to do based on that character's syntax, as defined by the current syntax table (see Syntax). All printable characters are bound by default to the function completion-self-insert, which inserts the character, looks up the character's syntax descriptor in auto-completion-syntax-alist, and carries out the associated actions. By default, all word-constituent characters (syntax descriptor w) insert the character and complete the new prefix, whilst anything else rejects any provisional completion and inserts the character.

Occasionally, the syntax-derived behaviour needs to be overridden for individual characters. The auto-completion-override-syntax-alist associates characters with completion behaviour and takes precedence over auto-completion-syntax-alist.

When customizing auto-completion-syntax-alist, some useful predefined settings are available. The ‘Acceptance behaviour’ can be set to ‘type normally’ or ‘punctuation accepts’. Surprisingly, the ‘type normally’ allows you to type normally: word-constituent characters are added to the current prefix and recompleted, anything else rejects any provisional completion and inserts the character. The ‘punctuation accepts’ setting causes whitespace and punctuation characters to instead accept the current completion and insert the character.

The ‘Completion behaviour’ can be set to ‘word’ or ‘string’. The differences between these settings is subtle, and mainly affect what happens when you start typing when the point is in the middle or end of an existing word. Essentially, the ‘word’ setting will try to intelligently decide whether you wanted to add characters to the existing word or start a new word depending on where the point is, whereas the ‘string’ setting will always behave as though you're starting a new word, wherever the point is.

For full control overlay syntax-related behaviour, you can manually customize the entries in auto-completion-syntax-alist. Each association in auto-completion-syntax-alist and auto-completion-override-syntax-alist is a two-element list of the form1:

     (<accept|reject|add> <string|word|none>)

The first element determines what happens if there is a provisional completion at the point: accept accepts it, reject rejects it, and add adds the typed character to the completion's prefix. The last element determines what kind of completion is done (if any) after the typed character is inserted: string and word complete the current prefix, whereas none doesn't do any completion. The string and word options differ in how they decide what prefix should be completed when no completion is in progress. string takes the prefix to be the prefix built up by typing sequential characters. word takes the prefix to be the part of the current word appearing before point2. The difference is only evident when typing a character in the middle or at the end of an existing word, in which case string completes the newly typed character whereas word completes the existing word plus the new character.

Finally, it is occasionally useful to be able to manually override a character's syntax, and have it treated one-off as though it had a different syntax class. A key binding to do this can be created using the completion-define-word-constituent-binding function in your .emacs file (after the line loading the predictive package, see Obtaining and Installing). This is most commonly used to allow punctuation characters to be inserted one-off as word-constituents. The bindings M-., M--, M-/ and M-S-<SPC> are defined by default in predictive mode to do precisely this for the punctuation characters ‘.’, ‘-’ and ‘/’.


Footnotes

[1] Lisp packages can additionally set a third element that controls whether the typed character is inserted, and can replace any element in the list with a function that returns one of the values described here or nil, but these features are beyond the scope of this user manual.

[2] The value of completion-word-thing determines what is considered a word. Logically enough, it defaults to word. Lisp packages can redefine it, or even override the whole prefix-finding mechanism with their own function, but this is again beyond the scope of this user manual.