[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/auctex 436371c 31/57: Add completion-at-point support

From: Tassilo Horn
Subject: [elpa] externals/auctex 436371c 31/57: Add completion-at-point support
Date: Wed, 11 Jan 2017 18:01:39 +0000 (UTC)

branch: externals/auctex
commit 436371c63da26af56ea076bce8636dcb41fcdfd1
Author: Tassilo Horn <address@hidden>
Commit: Tassilo Horn <address@hidden>

    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 @address@hidden  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 @address@hidden which is equivalent to
address@hidden  Using @code{TeX-complete-symbol} makes it easier to type
-and remember the names of long @LaTeX{} macros.
+command which was bound to @address@hidden 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
address@hidden command, by default bound to
address@hidden@key{TAB}} which is equivalent to @kbd{M-C-i}.  Using
address@hidden 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 @address@hidden will now complete
 as much of the macro, as it unambiguously can.  For example, if you type
 address@hidden' and then @address@hidden, it will expand to
address@hidden'.  But there's more: if point is just after
address@hidden@{}, then @code{TeX-complete-symbol} will complete @LaTeX{}
+environments, etc.  This is controlled by @code{TeX-complete-list}.
 @deffn Command TeX-complete-symbol
 @kindex address@hidden
 (@address@hidden)  Complete @TeX{} symbol before point.
 @end deffn
address@hidden TeX-complete-list
+List of ways to complete the preceding text.
+Each entry is a list with the following elements:
+Regexp matching the preceding text.
+A number indicating the subgroup in the regexp containing the text.
+A function returning an alist of possible completions.
+Text to append after a succesful completion.
address@hidden enumerate
+Or alternatively:
+Regexp matching the preceding text.
+Function to do the actual completion.
address@hidden enumerate
address@hidden 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
address@hidden@key{TAB}}), all such registered functions are consulted for
+checking for possible completions.  Modern completion UIs like
address@hidden support this completion-at-point facility.
address@hidden TeX--completion-at-point
address@hidden's completion-at-point function which is automatically added to
address@hidden in @TeX{} and @LaTeX{} buffers.
+It offers the same completion candidates as would
address@hidden (and is also controlled by
address@hidden) except that it doesn't fall back on
address@hidden which would be awkward with completion UIs
+like @i{company-mode}.
address@hidden 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
+In addition to the completion performed by @code{TeX-complete-symbol},
address@hidden 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.
 @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)
+  ;; 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

[Prev in Thread] Current Thread [Next in Thread]