summaryrefslogtreecommitdiff
path: root/devdocs/elisp/abbrev-expansion.html
blob: 1fa4655d1eb03eae6faedc72158405a27f372a07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 <h3 class="section">Looking Up and Expanding Abbreviations</h3>    <p>Abbrevs are usually expanded by certain interactive commands, including <code>self-insert-command</code>. This section describes the subroutines used in writing such commands, as well as the variables they use for communication. </p> <dl> <dt id="abbrev-symbol">Function: <strong>abbrev-symbol</strong> <em>abbrev &amp;optional table</em>
</dt> <dd><p>This function returns the symbol representing the abbrev named <var>abbrev</var>. It returns <code>nil</code> if that abbrev is not defined. The optional second argument <var>table</var> is the abbrev table in which to look it up. If <var>table</var> is <code>nil</code>, this function tries first the current buffer’s local abbrev table, and second the global abbrev table. </p></dd>
</dl> <dl> <dt id="abbrev-expansion">Function: <strong>abbrev-expansion</strong> <em>abbrev &amp;optional table</em>
</dt> <dd><p>This function returns the string that <var>abbrev</var> would expand into (as defined by the abbrev tables used for the current buffer). It returns <code>nil</code> if <var>abbrev</var> is not a valid abbrev. The optional argument <var>table</var> specifies the abbrev table to use, as in <code>abbrev-symbol</code>. </p></dd>
</dl> <dl> <dt id="expand-abbrev">Command: <strong>expand-abbrev</strong>
</dt> <dd>
<p>This command expands the abbrev before point, if any. If point does not follow an abbrev, this command does nothing. To do the expansion, it calls the function that is the value of the <code>abbrev-expand-function</code> variable, with no arguments, and returns whatever that function does. </p> <p>The default expansion function returns the abbrev symbol if it did expansion, and <code>nil</code> otherwise. If the abbrev symbol has a hook function that is a symbol whose <code>no-self-insert</code> property is non-<code>nil</code>, and if the hook function returns <code>nil</code> as its value, then the default expansion function returns <code>nil</code>, even though expansion did occur. </p>
</dd>
</dl> <dl> <dt id="abbrev-insert">Function: <strong>abbrev-insert</strong> <em>abbrev &amp;optional name start end</em>
</dt> <dd><p>This function inserts the abbrev expansion of <code>abbrev</code>, replacing the text between <code>start</code> and <code>end</code>. If <code>start</code> is omitted, it defaults to point. <code>name</code>, if non-<code>nil</code>, should be the name by which this abbrev was found (a string); it is used to figure out whether to adjust the capitalization of the expansion. The function returns <code>abbrev</code> if the abbrev was successfully inserted, otherwise it returns <code>nil</code>. </p></dd>
</dl> <dl> <dt id="abbrev-prefix-mark">Command: <strong>abbrev-prefix-mark</strong> <em>&amp;optional arg</em>
</dt> <dd>
<p>This command marks the current location of point as the beginning of an abbrev. The next call to <code>expand-abbrev</code> will use the text from here to point (where it is then) as the abbrev to expand, rather than using the previous word as usual. </p> <p>First, this command expands any abbrev before point, unless <var>arg</var> is non-<code>nil</code>. (Interactively, <var>arg</var> is the prefix argument.) Then it inserts a hyphen before point, to indicate the start of the next abbrev to be expanded. The actual expansion removes the hyphen. </p>
</dd>
</dl> <dl> <dt id="abbrev-all-caps">User Option: <strong>abbrev-all-caps</strong>
</dt> <dd><p>When this is set non-<code>nil</code>, an abbrev entered entirely in upper case is expanded using all upper case. Otherwise, an abbrev entered entirely in upper case is expanded by capitalizing each word of the expansion. </p></dd>
</dl> <dl> <dt id="abbrev-start-location">Variable: <strong>abbrev-start-location</strong>
</dt> <dd><p>The value of this variable is a buffer position (an integer or a marker) for <code>expand-abbrev</code> to use as the start of the next abbrev to be expanded. The value can also be <code>nil</code>, which means to use the word before point instead. <code>abbrev-start-location</code> is set to <code>nil</code> each time <code>expand-abbrev</code> is called. This variable is also set by <code>abbrev-prefix-mark</code>. </p></dd>
</dl> <dl> <dt id="abbrev-start-location-buffer">Variable: <strong>abbrev-start-location-buffer</strong>
</dt> <dd><p>The value of this variable is the buffer for which <code>abbrev-start-location</code> has been set. Trying to expand an abbrev in any other buffer clears <code>abbrev-start-location</code>. This variable is set by <code>abbrev-prefix-mark</code>. </p></dd>
</dl> <dl> <dt id="last-abbrev">Variable: <strong>last-abbrev</strong>
</dt> <dd><p>This is the <code>abbrev-symbol</code> of the most recent abbrev expanded. This information is left by <code>expand-abbrev</code> for the sake of the <code>unexpand-abbrev</code> command (see <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Expanding-Abbrevs.html#Expanding-Abbrevs">Expanding Abbrevs</a> in <cite>The GNU Emacs Manual</cite>). </p></dd>
</dl> <dl> <dt id="last-abbrev-location">Variable: <strong>last-abbrev-location</strong>
</dt> <dd><p>This is the location of the most recent abbrev expanded. This contains information left by <code>expand-abbrev</code> for the sake of the <code>unexpand-abbrev</code> command. </p></dd>
</dl> <dl> <dt id="last-abbrev-text">Variable: <strong>last-abbrev-text</strong>
</dt> <dd><p>This is the exact expansion text of the most recent abbrev expanded, after case conversion (if any). Its value is <code>nil</code> if the abbrev has already been unexpanded. This contains information left by <code>expand-abbrev</code> for the sake of the <code>unexpand-abbrev</code> command. </p></dd>
</dl> <dl> <dt id="abbrev-expand-function">Variable: <strong>abbrev-expand-function</strong>
</dt> <dd><p>The value of this variable is a function that <code>expand-abbrev</code> will call with no arguments to do the expansion. The function can do anything it wants before and after performing the expansion. It should return the abbrev symbol if expansion took place. </p></dd>
</dl> <p>The following sample code shows a simple use of <code>abbrev-expand-function</code>. It assumes that <code>foo-mode</code> is a mode for editing certain files in which lines that start with ‘<samp>#</samp>’ are comments. You want to use Text mode abbrevs for those lines. The regular local abbrev table, <code>foo-mode-abbrev-table</code> is appropriate for all other lines. See <a href="standard-abbrev-tables">Standard Abbrev Tables</a>, for the definitions of <code>local-abbrev-table</code> and <code>text-mode-abbrev-table</code>. See <a href="advising-functions">Advising Functions</a>, for details of <code>add-function</code>. </p> <div class="example"> <pre class="example">(defun foo-mode-abbrev-expand-function (expand)
  (if (not (save-excursion (forward-line 0) (eq (char-after) ?#)))
      ;; Performs normal expansion.
      (funcall expand)
    ;; We're inside a comment: use the text-mode abbrevs.
    (let ((local-abbrev-table text-mode-abbrev-table))
      (funcall expand))))

(add-hook 'foo-mode-hook
          (lambda ()
            (add-function :around (local 'abbrev-expand-function)
                          #'foo-mode-abbrev-expand-function)))
</pre>
</div><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Abbrev-Expansion.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Abbrev-Expansion.html</a>
  </p>
</div>