auctex-devel
[Top][All Lists]
Advanced

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

Support for LaTeX hooks


From: Arash Esbati
Subject: Support for LaTeX hooks
Date: Wed, 12 May 2021 13:23:54 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50

Hi all,

next LaTeX release will have more support for hooks.  Try

    texdoc lthooks-doc
    texdoc ltfilehook-doc
    texdoc ltcmdhooks-doc

I think a preliminary support for hooks can be added to AUCTeX like
this: We need a new variable `TeX-global-input-files-with-extension', a
function which reads and returns the hook (see below) and another
function which inserts the hook (tbd).  The one below currently inserts
some text in the buffer for checking.  You can play with the code below
by eval'ing the forms and in a .tex file, eval

    (TeX-read-hook nil)

WDYT?

Any comments welcome.

--8<---------------cut here---------------start------------->8---
(defvar TeX-global-input-files-with-extension nil
  "List of the non-local TeX input files with extension.
Initialized once at the first time you prompt for an input file
inside a file hook command.  May be reset with
`\\[universal-argument] \\[TeX-normal-mode]'.")

(defun TeX-read-hook (_optional)
  "Read a LaTeX hook."
  (let* ((hook (completing-read
                (TeX-argument-prompt nil nil "Hook")
                '("cmd"
                  "env"
                  "begindocument"
                  "begindocument/before"
                  "begindocument/end"
                  "enddocument"
                  "enddocument/afterlastpage"
                  "enddocument/afteraux"
                  "enddocument/info"
                  "enddocument/end"
                  "rmfamily"       "sffamily"
                  "ttfamily"       "normalfont"
                  "bfseries"       "bfseries/defaults"
                  "mdseries"       "mdseries/defaults"
                  "file/before"    "file/after"
                  "package/before" "package/after"
                  "class/before"   "class/after"
                  "include/before" "include/end" "include/after")))
         (func (lambda ()
                 (completing-read
                  (TeX-argument-prompt nil nil "Where")
                  (if (string= hook "cmd")
                      '("after" "before")
                    '("before" "begin" "end" "after")))))
         (search (if (eq TeX-arg-input-file-search 'ask)
                     (not (y-or-n-p "Find file yourself? "))
                   TeX-arg-input-file-search))
         name where files result)
    (cond ((string= hook "cmd")
           ;; cmd/<name>/<where>
           (setq name (completing-read
                       (TeX-argument-prompt nil nil "Command")
                       (TeX-symbol-list)))
           (setq where (funcall func)))

          ;; env/<name>/<where>
          ((string= hook "env")
           (setq name (completing-read
                       (TeX-argument-prompt nil nil "Environment")
                       (LaTeX-environment-list)))
           (setq where (funcall func)))

          ;; file/(before|after)/<file-name.xxx> where <file-name> is
          ;; optional and must be with extension
          ((string-match "\\`file" hook)
           (if search
               (unless TeX-global-input-files-with-extension
                 (setq TeX-global-input-files-with-extension
                       (prog2
                           (message "Searching for files...")
                           (mapcar #'list
                                   (TeX-search-files-by-type 'texinputs
                                                             'global
                                                             t nil))
                         (message "Searching for files...done")))))
           (setq name
                 (completing-read
                  (TeX-argument-prompt t nil "File")
                  TeX-global-input-files-with-extension)))

          ;; include/(before|after|end)/<file-name> where <file-name>
          ;; is optional
          ((string-match "\\`include" hook)
           (if search
               (setq files
                     (prog2
                         (message "Searching for files...")
                         (TeX-search-files-by-type 'texinputs 'local t t)
                       (message "Searching for files...done"))))
           (setq name (completing-read
                       (TeX-argument-prompt t nil "File")
                       files)))

          ;; class/before|after/<doc-class> where <doc-class> is
          ;; optional
          ((string-match "\\`class" hook)
           (let* ((TeX-file-extensions '("cls")))
             (unless LaTeX-global-class-files
               (setq LaTeX-global-class-files
                     (if search
                         (prog2
                             (message "Searching for LaTeX classes...")
                             (mapcar #'list
                                     (TeX-search-files-by-type 'texinputs
                                                               'global
                                                               t t))
                           (message "Searching for LaTeX classes...done"))
                       LaTeX-style-list))))
           (setq name (completing-read
                       (TeX-argument-prompt t nil "Document class")
                       LaTeX-global-class-files)))

          ;; package/before|after/<pack-name> where
          ;; <pack-name> is optional
          ((string-match "\\`package" hook)
           (let* ((TeX-file-extensions '("sty")))
             (unless LaTeX-global-package-files
               (setq LaTeX-global-package-files
                     (if search
                         (prog2
                             (message "Searching for LaTeX packages...")
                             (mapcar #'list
                                     (TeX-search-files-by-type 'texinputs
                                                               'global
                                                               t t))
                           (message "Searching for LaTeX packages...done"))))))
           (setq name (completing-read
                       (TeX-argument-prompt t nil "Package")
                       LaTeX-global-package-files)))

          ;; User specific input or others, do nothing and just
          ;; insert `hook' later:
          (t nil))
    (if (member hook '("cmd" "env"))
        (setq result (mapconcat #'identity
                                `(,hook ,name ,where)
                                "/"))

      (push hook result)
      (when (and name (not (string= name "")))
        (push name result))
      (setq result
            (mapconcat #'identity
                       (reverse result)
                       "/")))
    (insert "\n" "\\AddToHook{" result "}")))
--8<---------------cut here---------------end--------------->8---

Best, Arash



reply via email to

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