auctex-diffs
[Top][All Lists]
Advanced

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

master e0bc78a5: Support in-buffer completion for class/package names/op


From: Arash Esbati
Subject: master e0bc78a5: Support in-buffer completion for class/package names/options
Date: Sat, 13 May 2023 03:43:29 -0400 (EDT)

branch: master
commit e0bc78a500c43ba3c9f6eb04ee104ab64f6d90bb
Author: Arash Esbati <arash@gnu.org>
Commit: Arash Esbati <arash@gnu.org>

    Support in-buffer completion for class/package names/options
    
    * latex.el (LaTeX-completion-documentclass-usepackage): New
    function providing in-buffer completion for class/package names
    and options.
    (LaTeX--arguments-completion-at-point): Use function accordingly.
---
 latex.el | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 92 insertions(+), 1 deletion(-)

diff --git a/latex.el b/latex.el
index cf2518b2..b287604e 100644
--- a/latex.el
+++ b/latex.el
@@ -7597,6 +7597,91 @@ COLLECTION is an list of strings."
                    (lambda (_)
                      collection)))))
 
+(defun LaTeX-completion-documentclass-usepackage (entry)
+  "Return completion candidates for arguments of \\usepackage macro.
+ENTRY is the value returned by `LaTeX-what-macro'.  This function
+provides completion for package names if point is inside the
+mandatory argument and package options if inside the first
+optional argument."
+  (let ((cls-or-sty (if (member (car entry) '("usepackage" "RequirePackage"
+                                              "RequirePackageWithOptions"))
+                        'sty
+                      'cls)))
+    (cond ((and (eq (nth 3 entry) 'mandatory)
+                (eq TeX-arg-input-file-search t))
+           (if (eq cls-or-sty 'cls)
+               (progn
+                 (unless LaTeX-global-class-files
+                   (let ((TeX-file-extensions '("cls")))
+                     (message "Searching for LaTeX classes...")
+                     (setq LaTeX-global-class-files
+                           (mapcar #'list (TeX-search-files-by-type 'texinputs 
'global t t)))
+                     (message "Searching for LaTeX classes...done")))
+                 (LaTeX-completion-candidates-completing-read
+                  LaTeX-global-class-files))
+             (unless LaTeX-global-package-files
+               (let ((TeX-file-extensions '("sty")))
+                 (message "Searching for LaTeX packages...")
+                 (setq LaTeX-global-package-files
+                       (mapcar #'list (TeX-search-files-by-type 'texinputs 
'global t t)))
+                 (message "Searching for LaTeX packages...done")))
+             (LaTeX-completion-candidates-completing-read-multiple
+              LaTeX-global-package-files)))
+          ;; We have to be more careful for the optional argument
+          ;; since the macros can look like this:
+          ;; \usepackage[opt1]{mand}[opt2].  So we add an extra check
+          ;; if we are inside the first optional arg:
+          ((and (eq (nth 3 entry) 'optional)
+                (= (nth 2 entry) 1))
+           (let ((syntax (TeX-search-syntax-table ?\[ ?\]))
+                 style style-opts)
+             ;; We have to find out about the package/class name:
+             (save-excursion
+               (with-syntax-table syntax
+                 (condition-case nil
+                     (let ((forward-sexp-function nil))
+                       (up-list))
+                   (error nil)))
+               (skip-chars-forward "^[:alnum:]")
+               (setq style (thing-at-point 'symbol t)))
+             ;; Load the style file; may fail but that's Ok for us
+             (TeX-load-style style)
+             ;; Now we have to find out how the options are available:
+             ;; This is usually a variable called
+             ;; `LaTeX-<class|package>-package-options'.  If it is a
+             ;; function, then the options are stored either in a
+             ;; variable or a function called
+             ;; `LaTeX-<class|package>-package-options-list:'
+             (when (setq style-opts
+                         (intern-soft (format
+                                       (concat "LaTeX-%s-"
+                                               (if (eq cls-or-sty 'cls)
+                                                   "class"
+                                                 "package")
+                                               "-options")
+                                       style)))
+               (cond ((and (boundp style-opts)
+                           (symbol-value style-opts))
+                      (LaTeX-completion-candidates-completing-read-multiple
+                       (symbol-value style-opts)))
+                     ((and (setq style-opts
+                                 (intern-soft (format
+                                               (concat "LaTeX-%s-"
+                                                       (if (eq cls-or-sty 'cls)
+                                                           "class"
+                                                         "package")
+                                                       "-options-list")
+                                               style)))
+                           (boundp style-opts)
+                           (symbol-value style-opts))
+                      (LaTeX-completion-candidates-key-val
+                       (symbol-value style-opts)))
+                     ((fboundp style-opts)
+                      (LaTeX-completion-candidates-key-val
+                       (funcall style-opts)))
+                     (t nil)))))
+          (t nil))))
+
 (defun LaTeX-completion-parse-args (entry)
   "Return the match of buffer position ENTRY with AUCTeX macro definitions.
 ENTRY is generated by the function `LaTeX-what-macro'.  This
@@ -7811,7 +7896,13 @@ function `TeX--completion-at-point' which should come 
first in
   (when (and (LaTeX-completion-find-argument-boundries)
              (not (nth 4 (syntax-ppss))))
     (let ((entry (LaTeX-what-macro)))
-      (cond ((or (and entry
+      (cond ((and entry
+                  (member (car entry) '("usepackage" "RequirePackage"
+                                        "RequirePackageWithOptions"
+                                        "documentclass" "LoadClass"
+                                        "LoadClassWithOptions")))
+             (LaTeX-completion-documentclass-usepackage entry))
+            ((or (and entry
                       (eq (nth 1 entry) 'mac)
                       (assoc (car entry) (TeX-symbol-list)))
                  (and entry



reply via email to

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