branch: externals/auctex
commit 436371c63da26af56ea076bce8636dcb41fcdfd1
Author: Tassilo Horn <[email protected]>
Commit: Tassilo Horn <[email protected]>

    Add completion-at-point support
    
    * doc/auctex.texi (Completion): Document completion-at-point support.
    * doc/changes.texi: Mention completion-at-point support.
    * tex.el (TeX--completion-at-point): New function.
    (VirTeX-common-initialization): Add TeX--completion-at-point to
    completion-at-point-functions in TeX buffers if that's bound.
---
 doc/auctex.texi  |   62 ++++++++++++++++++++++++++++++++++++++++++++++++------
 doc/changes.texi |    7 ++++++
 tex.el           |   35 ++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+), 7 deletions(-)

diff --git a/doc/auctex.texi b/doc/auctex.texi
index 09322f1..87233f1 100644
--- a/doc/auctex.texi
+++ b/doc/auctex.texi
@@ -1216,24 +1216,72 @@ If non-nil, insert braces after typing @key{^} and 
@key{_} in math mode.
 @cindex Arguments to @TeX{} macros
 
 Emacs lisp programmers probably know the @code{lisp-complete-symbol}
-command, usually bound to @kbd{M-@key{TAB}}.  Users of the wonderful
-ispell mode know and love the @code{ispell-complete-word} command from
-that package.  Similarly, @AUCTeX{} has a @code{TeX-complete-symbol}
-command, by default bound to @kbd{M-@key{TAB}} which is equivalent to
-@kbd{M-C-i}.  Using @code{TeX-complete-symbol} makes it easier to type
-and remember the names of long @LaTeX{} macros.
+command which was bound to @kbd{M-@key{TAB}} until completion-at-point
+became the new standard completion facility (see below).  Users of the
+wonderful ispell mode know and love the @code{ispell-complete-word}
+command from that package.  Similarly, @AUCTeX{} has a
+@code{TeX-complete-symbol} command, by default bound to
+@kbd{M-@key{TAB}} which is equivalent to @kbd{M-C-i}.  Using
+@code{TeX-complete-symbol} makes it easier to type and remember the
+names of long @LaTeX{} macros.
 
 In order to use @code{TeX-complete-symbol}, you should write a backslash
 and the start of the macro.  Typing @kbd{M-@key{TAB}} will now complete
 as much of the macro, as it unambiguously can.  For example, if you type
 `@samp{\renewc}' and then @kbd{M-@key{TAB}}, it will expand to
-`@samp{\renewcommand}'.
+`@samp{\renewcommand}'.  But there's more: if point is just after
+@samp{\begin@{}, then @code{TeX-complete-symbol} will complete @LaTeX{}
+environments, etc.  This is controlled by @code{TeX-complete-list}.
 
 @deffn Command TeX-complete-symbol
 @kindex M-@key{TAB}
 (@kbd{M-@key{TAB}})  Complete @TeX{} symbol before point.
 @end deffn
 
+@defvar TeX-complete-list
+List of ways to complete the preceding text.
+
+Each entry is a list with the following elements:
+
+@enumerate
+@item
+Regexp matching the preceding text.
+@item
+A number indicating the subgroup in the regexp containing the text.
+@item
+A function returning an alist of possible completions.
+@item
+Text to append after a succesful completion.
+@end enumerate
+
+Or alternatively:
+
+@enumerate
+@item
+Regexp matching the preceding text.
+@item
+Function to do the actual completion.
+@end enumerate
+@end defvar
+
+More recent Emacs versions have a new completion mechanism.  Modes may
+define and register custom completion-at-point functions and when the
+user invokes @code{completion-at-point} (usually bound to
+@kbd{M-@key{TAB}}), all such registered functions are consulted for
+checking for possible completions.  Modern completion UIs like
+@i{company-mode} support this completion-at-point facility.
+
+@deffn TeX--completion-at-point
+@AUCTeX{}'s completion-at-point function which is automatically added to
+@code{completion-at-point-functions} in @TeX{} and @LaTeX{} buffers.
+
+It offers the same completion candidates as would
+@code{TeX-complete-symbol} (and is also controlled by
+@code{TeX-complete-list}) except that it doesn't fall back on
+@code{ispell-complete-word} which would be awkward with completion UIs
+like @i{company-mode}.
+@end deffn
+
 A more direct way to insert a macro is with @code{TeX-insert-macro},
 bound to @kbd{C-c C-m} which is equivalent to @kbd{C-c @key{RET}}.  It
 has the advantage over completion that it knows about the argument of
diff --git a/doc/changes.texi b/doc/changes.texi
index 74eabe0..e756d96 100644
--- a/doc/changes.texi
+++ b/doc/changes.texi
@@ -12,6 +12,13 @@
 
 @itemize @bullet
 @item
+In addition to the completion performed by @code{TeX-complete-symbol},
+@AUCTeX{} now also supports the new Emacs standard completion-at-point
+facility (see the Emacs command @code{completion-at-point}).  This also
+means that modern completion UIs like @i{company-mode} work out of the
+box in @TeX{} and @LaTeX{} buffers.
+
+@item
 @AUCTeX{} is able to display several levels of super- and subscripts,
 each one raised above and a bit smaller than its basis.  For this
 feature, have a look at the customize options
diff --git a/tex.el b/tex.el
index ab301fd..ac81b74 100644
--- a/tex.el
+++ b/tex.el
@@ -3202,6 +3202,29 @@ Or alternatively:
                   (message "Making completion list...done")))))
       (funcall (nth 1 entry)))))
 
+(defun TeX--completion-at-point ()
+  "(La)TeX completion at point function.
+See `completion-at-point-functions'."
+  (let ((list TeX-complete-list)
+       entry)
+    (while list
+      (setq entry (car list)
+           list (cdr list))
+      (if (TeX-looking-at-backward (car entry) 250)
+         (setq list nil)))
+    (if (numberp (nth 1 entry))
+       (let* ((sub (nth 1 entry))
+              (begin (match-beginning sub))
+              (end (match-end sub))
+              (symbol (buffer-substring-no-properties begin end))
+              (list (funcall (nth 2 entry))))
+         (list begin end (all-completions symbol list)))
+      ;; We intentionally don't call the fallback completion functions because
+      ;; they do completion on their own and don't work too well with things
+      ;; like company-mode.  And the default function `ispell-complete-word'
+      ;; isn't so useful anyway.
+      nil)))
+
 (defcustom TeX-default-macro "ref"
   "*The default macro when creating new ones with `TeX-insert-macro'."
   :group 'TeX-macro
@@ -3730,6 +3753,18 @@ The algorithm is as follows:
       (set (make-local-variable 'prettify-symbols-compose-predicate)
           #'TeX--prettify-symbols-compose-p)))
 
+  ;; Standard Emacs completion-at-point support
+  (when (boundp 'completion-at-point-functions)
+    (add-hook 'completion-at-point-functions
+             #'TeX--completion-at-point nil t)
+
+    ;; Support for company-mode
+    (when (fboundp 'company-mode)
+      ;; By default, company completions kick in after a prefix of 3 chars has
+      ;; been typed.  Since we don't have too many completions, that's too
+      ;; much.
+      (set (make-local-variable 'company-minimum-prefix-length) 1)))
+
   ;; Let `TeX-master-file' be called after a new file was opened and
   ;; call `TeX-update-style' on any file opened.  (The addition to the
   ;; hook has to be made here because its local value will be deleted

Reply via email to