Skip to content

Completion

Apart from real-time validation, the most important feature that nXML mode provides for assisting in document creation is "completion". Completion assists the user in inserting characters at point, based on knowledge of the schema and on the contents of the buffer before point.

nXML mode adapts the standard GNU Emacs command for completion in a buffer: completion-at-point, which is bound to C-M-i and M-TAB. Note that many window systems and window managers use M-TAB themselves (typically for switching between windows) and do not pass it to applications. In that case, you should type C-M-i or ESC TAB for completion, or bind completion-at-point to a key that is convenient for you. In the following, I will assume that you type C-M-i.

nXML mode completion works by examining the symbol preceding point. This is the symbol to be completed. The symbol to be completed may be the empty. Completion considers what symbols starting with the symbol to be completed would be valid replacements for the symbol to be completed, given the schema and the contents of the buffer before point. These symbols are the possible completions. An example may make this clearer. Suppose the buffer looks like this (where ∗ indicates point):

bash
<html xmlns="http://www.w3.org/1999/xhtml">
<h∗

and the schema is XHTML. In this context, the symbol to be completed is ‘h’. The possible completions consist of just ‘head’. Another example, is

bash
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<

In this case, the symbol to be completed is empty, and the possible completions are ‘base’, ‘isindex’, ‘link’, ‘meta’, ‘script’, ‘style’, ‘title’. Another example is:

<html xmlns="∗

In this case, the symbol to be completed is empty, and the possible completions are just ‘http://www.w3.org/1999/xhtml’.

When you type C-M-i, what happens depends on what the set of possible completions are.

  • If the set of completions is empty, nothing happens.

  • If there is one possible completion, then that completion is inserted, together with any following characters that are required. For example, in this case:

    bash
    <html xmlns="http://www.w3.org/1999/xhtml">
    <

    C-M-i will yield

    bash
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head∗
  • If there is more than one possible completion, but all possible completions share a common non-empty prefix, then that prefix is inserted. For example, suppose the buffer is:

    <html x∗

    The symbol to be completed is ‘x’. The possible completions are ‘xmlns’ and ‘xml:lang’. These share a common prefix of ‘xml’. Thus, C-M-i will yield:

    <html xml∗

    Typically, you would do C-M-i again, which would have the result described in the next item.

  • If there is more than one possible completion, but the possible completions do not share a non-empty prefix, then Emacs will prompt you to input the symbol in the minibuffer, initializing the minibuffer with the symbol to be completed, and popping up a buffer showing the possible completions. You can now input the symbol to be inserted. The symbol you input will be inserted in the buffer instead of the symbol to be completed. Emacs will then insert any required characters after the symbol. For example, if it contains:

    <html xml∗

    Emacs will prompt you in the minibuffer with

    Attribute: xml∗

    and the buffer showing possible completions will contain

    Possible completions are:
    xml:lang                           xmlns

    If you input xmlns, the result will be:

    lisp
    <html xmlns="∗"

    (If you do C-M-i again, the namespace URI will be inserted. Should that happen automatically?)