branch: externals/auctex-label-numbers commit 30919ac12f2dec24692dd206b94ae0d7a2114ec5 Author: Paul Nelson <ultr...@gmail.com> Commit: Paul Nelson <ultr...@gmail.com>
rename to auctex-label-numbers.el https://lists.gnu.org/archive/html/emacs-devel/2024-05/msg01170.html --- README.org | 12 +-- tex-numbers.el => auctex-label-numbers.el | 151 +++++++++++++++--------------- 2 files changed, 83 insertions(+), 80 deletions(-) diff --git a/README.org b/README.org index fb26c94ebd..ce60dd2a25 100644 --- a/README.org +++ b/README.org @@ -1,8 +1,8 @@ -#+title: tex-numbers.el: Numbering for LaTeX previews and folds +#+title: auctex-label-numbers.el: Numbering for LaTeX previews and folds #+author: Paul D. Nelson * Overview -The package provides a function, =tex-numbers-label-to-number=, that retrieves label numbers in LaTeX documents. This function is used to implement a global minor mode, =tex-numbers-mode=, that augments the preview and folding features of [[https://www.gnu.org/software/auctex/manual/auctex/Installation.html#Installation][AUCTeX]]: +The package provides a function, =auctex-label-numbers-label-to-number=, that retrieves label numbers in LaTeX documents. This function is used to implement a global minor mode, =auctex-label-numbers-mode=, that augments the preview and folding features of [[https://www.gnu.org/software/auctex/manual/auctex/Installation.html#Installation][AUCTeX]]: - Previews of labeled equations are numbered as in the compiled document. @@ -20,14 +20,14 @@ This package requires [[https://www.gnu.org/software/auctex/manual/auctex/Instal Download this repository, install using =M-x package-install-file= (or package-vc-install, straight, elpaca, ...), and add something like the following to your [[https://www.emacswiki.org/emacs/InitFile][init file]]: #+begin_src elisp - (use-package tex-numbers + (use-package auctex-label-numbers :after latex :config - (tex-numbers-mode 1)) + (auctex-label-numbers-mode 1)) #+end_src With this, the package activates automatically. -You could alternatively use simply =(use-package tex-numbers)= and activate via =M-x tex-numbers-mode= or =(tex-numbers-mode 1)= whenever you'd like (e.g., in the middle of some other customizations of =TeX-fold-mode=). If you'd like to enable some (but not all) of the provided functionality, then you can extract from the definition of =tex-numbers-mode= the pieces that you'd like and put those in your config. +You could alternatively use simply =(use-package auctex-label-numbers)= and activate via =M-x auctex-label-numbers-mode= or =(auctex-label-numbers-mode 1)= whenever you'd like (e.g., in the middle of some other customizations of =TeX-fold-mode=). If you'd like to enable some (but not all) of the provided functionality, then you can extract from the definition of =auctex-label-numbers-mode= the pieces that you'd like and put those in your config. * Usage The label numbers are retrieved from the aux file of the compiled document. To update them, one should compile the document, regenerate the previews and refresh the folds. @@ -37,4 +37,4 @@ The previews should be numbered automated. To activate the folds, you'll want t I use the packages [[https://github.com/ultronozm/tex-continuous.el][tex-continuous.el]] and [[https://github.com/ultronozm/preview-auto.el][preview-auto.el]] (with the variable =preview-auto-refresh-after-compilation= is set to its default value, =t=) to compile the document and regenerate the previews automatically, and refresh the folds as needed using =TeX-fold-section= (=C-c C-o C-s=). * Customization -By customizing the variable =tex-numbers-label-to-number-function=, one could specify a different way to retrieve label numbers, e.g., by querying an LSP server. +By customizing the variable =auctex-label-numbers-label-to-number-function=, one could specify a different way to retrieve label numbers, e.g., by querying an LSP server. diff --git a/tex-numbers.el b/auctex-label-numbers.el similarity index 58% rename from tex-numbers.el rename to auctex-label-numbers.el index 8c6e7d51de..1b294ec832 100644 --- a/tex-numbers.el +++ b/auctex-label-numbers.el @@ -1,10 +1,10 @@ -;;; tex-numbers.el --- Numbering for LaTeX previews and folds -*- lexical-binding: t; -*- +;;; auctex-label-numbers.el --- Numbering for LaTeX previews and folds -*- lexical-binding: t; -*- ;; Copyright (C) 2024 Paul D. Nelson ;; Author: Paul D. Nelson <nelson.paul.da...@gmail.com> ;; Version: 0.1 -;; URL: https://github.com/ultronozm/tex-numbers.el +;; URL: https://github.com/ultronozm/auctex-label-numbers.el ;; Package-Requires: ((emacs "27.1") (auctex "14.0.5")) ;; Keywords: tex @@ -23,10 +23,11 @@ ;;; Commentary: -;; This package provides a function, `tex-numbers-label-to-number', -;; that retrieves label numbers for LaTeX documents from the compiled -;; aux file. That function is used to implement a global minor mode, -;; `tex-numbers-mode', that augments many features of AUCTeX +;; This package provides a function, +;; `auctex-label-numbers-label-to-number', that retrieves label +;; numbers for LaTeX documents from the compiled aux file. That +;; function is used to implement a global minor mode, +;; `auctex-label-numbers-mode', that augments many features of AUCTeX ;; (previews, folding, ...). See README.org for details. ;;; Code: @@ -37,16 +38,16 @@ (require 'preview) (require 'reftex) -(defgroup tex-numbers nil +(defgroup auctex-label-numbers nil "Numbering for LaTeX previews and folds." :group 'AUCTeX) -(defvar tex-numbers-cache (make-hash-table :test 'equal) +(defvar auctex-label-numbers-cache (make-hash-table :test 'equal) "Cache of label numbers from aux files. The keys are aux file names. The values are hash tables, mapping label strings to label number strings.") -(defun tex-numbers-update-cache (aux-file) +(defun auctex-label-numbers-update-cache (aux-file) "Update the cache for AUX-FILE. Return the updated cache, or nil if the aux file does not exist." (when (file-exists-p aux-file) @@ -61,36 +62,37 @@ Return the updated cache, or nil if the aux file does not exist." (number (match-string 2))) (puthash label number cache)))) (puthash 'timestamp (current-time) cache) - (puthash aux-file cache tex-numbers-cache) + (puthash aux-file cache auctex-label-numbers-cache) cache)))) -(defcustom tex-numbers-label-to-number-function nil +(defcustom auctex-label-numbers-label-to-number-function nil "Function to retrieve label numbers. -If non-nil, `tex-numbers-label-to-number' delegates to this function. -The function should take a label string as its argument and return the -corresponding label number as a string, or nil if that number cannot be -retrieved." +If non-nil, `auctex-label-numbers-label-to-number' delegates to this +function. The function should take a label string as its argument and +return the corresponding label number as a string, or nil if that number +cannot be retrieved." :type '(choice (const :tag "Default" nil) function)) -(defconst tex-numbers--external-document-regexp +(defconst auctex-label-numbers--external-document-regexp "\\\\external\\(?:cite\\)?document\\(?:\\[[^]]+\\]\\)\\{0,2\\}{\\([^}]+\\)}") -(defun tex-numbers-label-to-number-helper (label aux-file) +(defun auctex-label-numbers-label-to-number-helper (label aux-file) "Get the number of LABEL from the AUX-FILE. Check the cache first, and update it if AUX-FILE has changed. Return the label number as a string, or nil if the label cannot be found." - (let ((cache (gethash aux-file tex-numbers-cache))) + (let ((cache (gethash aux-file auctex-label-numbers-cache))) (if (or (not cache) (time-less-p (gethash 'timestamp cache) (nth 5 (file-attributes aux-file)))) - (setq cache (tex-numbers-update-cache aux-file))) + (setq cache (auctex-label-numbers-update-cache aux-file))) (when cache (gethash label cache)))) -(defcustom tex-numbers-search-external-documents t +(defcustom auctex-label-numbers-search-external-documents t "Whether to search external documents for label numbers. -If non-nil, `tex-numbers-label-to-number' will search external documents -for label numbers if the label cannot be found in the current document. +If non-nil, `auctex-label-numbers-label-to-number' will search external +documents for label numbers if the label cannot be found in the current +document. The search is performed by looking for \\externaldocument{FILENAME} commands in the current document, and then looking for the label in @@ -99,29 +101,28 @@ buffers, so that it can use AUCTeX's function `TeX-master-output-file' to find the corresponding aux files." :type 'boolean) -(defun tex-numbers-label-to-number (label) +(defun auctex-label-numbers-label-to-number (label) "Get number of LABEL for current tex buffer. -If the buffer does not point to a file, or if the corresponding -aux file does not exist, or if the label cannot be found, then -return nil. Otherwise, return the label number as a string. If -the label is found in an external document, prefix the string -with \"X\"." - (if tex-numbers-label-to-number-function - (funcall tex-numbers-label-to-number-function label) +If the buffer does not point to a file, or if the corresponding aux file +does not exist, or if the label cannot be found, then return nil. +Otherwise, return the label number as a string. If the label is found +in an external document, prefix the string with \"X\"." + (if auctex-label-numbers-label-to-number-function + (funcall auctex-label-numbers-label-to-number-function label) (or (when-let* ((aux-file (TeX-master-output-file "aux"))) - (tex-numbers-label-to-number-helper label aux-file)) + (auctex-label-numbers-label-to-number-helper label aux-file)) ;; If we can't retrieve the label from the main file, then we look ;; at any external documents. (and - tex-numbers-search-external-documents + auctex-label-numbers-search-external-documents (save-excursion (save-restriction (widen) (goto-char (point-min)) (let (found) (while (and (null found) - (re-search-forward tex-numbers--external-document-regexp + (re-search-forward auctex-label-numbers--external-document-regexp nil t)) (let* ((tex-filename (concat (match-string 1) ".tex")) (tex-buffer (find-file-noselect tex-filename)) @@ -129,13 +130,14 @@ with \"X\"." (with-current-buffer tex-buffer (hack-local-variables) (TeX-master-output-file "aux")))) - (setq found (tex-numbers-label-to-number-helper label aux-filename)))) + (setq found (auctex-label-numbers-label-to-number-helper label aux-filename)))) (when found (concat "X" found))))))))) -(defun tex-numbers-preview-preprocessor (str) +(defun auctex-label-numbers-preview-preprocessor (str) "Preprocess STR for preview by adding tags to labels. -Uses `tex-numbers-label-to-number-function' to retrieve label numbers." +Uses `auctex-label-numbers-label-to-number-function' to retrieve label +numbers." (let ((buf (current-buffer)) (label-re (concat "\\(?:" (mapconcat #'identity reftex-label-regexps "\\|") "\\)"))) @@ -146,7 +148,7 @@ Uses `tex-numbers-label-to-number-function' to retrieve label numbers." (let ((label (match-string 1))) (if-let ((number (with-current-buffer buf - (tex-numbers-label-to-number label)))) + (auctex-label-numbers-label-to-number label)))) (when (let ((comment-start-skip (concat "\\(\\(^\\|[^\\\n]\\)\\(" @@ -158,51 +160,52 @@ Uses `tex-numbers-label-to-number-function' to retrieve label numbers." (insert "\\nonumber")))) (buffer-substring-no-properties (point-min) (point-max))))) -(defun tex-numbers-ref-helper (label default) - "Helper function for `tex-numbers-ref-display'. +(defun auctex-label-numbers-ref-helper (label default) + "Helper function for `auctex-label-numbers-ref-display'. Returns a fold display string for LABEL (retrieved via -`tex-numbers-label-to-number-function'), or DEFAULT if the label number cannot -be retrieved." - (format "[%s]" (or (tex-numbers-label-to-number label) default))) +`auctex-label-numbers-label-to-number-function'), or DEFAULT if the +label number cannot be retrieved." + (format "[%s]" (or (auctex-label-numbers-label-to-number label) default))) -(defun tex-numbers-ref-display (label &rest _args) +(defun auctex-label-numbers-ref-display (label &rest _args) "Fold display for a \\ref{LABEL} macro." - (tex-numbers-ref-helper label "r")) + (auctex-label-numbers-ref-helper label "r")) -(defun tex-numbers-eqref-display (label &rest _args) +(defun auctex-label-numbers-eqref-display (label &rest _args) "Fold display for a \\eqref{LABEL} macro." - (tex-numbers-ref-helper label "e")) + (auctex-label-numbers-ref-helper label "e")) -(defun tex-numbers-label-display (label &rest _args) +(defun auctex-label-numbers-label-display (label &rest _args) "Fold display for a \\label{LABEL} macro." - (tex-numbers-ref-helper label "l")) + (auctex-label-numbers-ref-helper label "l")) -(defvar tex-numbers--saved-spec-list nil +(defvar auctex-label-numbers--saved-spec-list nil "Saved values from `TeX-fold-macro-spec-list'.") -(defcustom tex-numbers-macro-list '("ref" "eqref" "label") +(defcustom auctex-label-numbers-macro-list '("ref" "eqref" "label") "List of macros to fold with theorem or equation numbers. Each element describes a LaTeX macro that takes a label as its argument. -There should be a corresponding function `tex-numbers-MACRO-display' -that returns a fold display string for that macro." +There should be a corresponding function +`auctex-label-numbers-MACRO-display' that returns a fold display string +for that macro." :type '(repeat string)) -(defun tex-numbers-label-annotation-advice (orig-fun label) +(defun auctex-label-numbers-label-annotation-advice (orig-fun label) "Return context for LABEL, augmented by the corresponding label number. Call ORIG-FUN with LABEL, and append the label number in parentheses if it can be retrieved." (concat (funcall orig-fun label) - (when-let ((number (tex-numbers-label-to-number label))) + (when-let ((number (auctex-label-numbers-label-to-number label))) (format " (%s)" number)))) -(defun tex-numbers--reftex-goto-label-advice (orig-fun &rest args) +(defun auctex-label-numbers--reftex-goto-label-advice (orig-fun &rest args) "Advice for `reftex-goto-label'. Call ORIG-FUN with ARGS, and add the label number to the annotation." (let* ((buf (current-buffer)) (fn (lambda (label) (if-let ((str (with-current-buffer buf - (funcall #'tex-numbers-label-to-number label)))) + (funcall #'auctex-label-numbers-label-to-number label)))) (format " [%s]" str) ""))) (completion-extra-properties @@ -210,23 +213,23 @@ Call ORIG-FUN with ARGS, and add the label number to the annotation." (apply orig-fun args))) ;;;###autoload -(define-minor-mode tex-numbers-mode - "Toggle `tex-numbers' mode." +(define-minor-mode auctex-label-numbers-mode + "Toggle `auctex-label-numbers' mode." :global t :lighter nil (cond - (tex-numbers-mode - (add-to-list 'preview-preprocess-functions #'tex-numbers-preview-preprocessor) + (auctex-label-numbers-mode + (add-to-list 'preview-preprocess-functions #'auctex-label-numbers-preview-preprocessor) (advice-add 'LaTeX-completion-label-annotation-function - :around #'tex-numbers-label-annotation-advice) - (advice-add 'reftex-goto-label :around #'tex-numbers--reftex-goto-label-advice) + :around #'auctex-label-numbers-label-annotation-advice) + (advice-add 'reftex-goto-label :around #'auctex-label-numbers--reftex-goto-label-advice) (require 'tex-fold) - (dolist (macro tex-numbers-macro-list) - (let ((func (intern (format "tex-numbers-%s-display" macro)))) + (dolist (macro auctex-label-numbers-macro-list) + (let ((func (intern (format "auctex-label-numbers-%s-display" macro)))) (dolist (spec TeX-fold-macro-spec-list) (when (and (member macro (cadr spec)) (not (eq (car spec) func))) - (push (cons macro (car spec)) tex-numbers--saved-spec-list) + (push (cons macro (car spec)) auctex-label-numbers--saved-spec-list) (setcdr spec (list (seq-remove (lambda (x) (equal x macro)) (cadr spec)))))) (add-to-list 'TeX-fold-macro-spec-list (list func (list macro))))) @@ -234,22 +237,22 @@ Call ORIG-FUN with ARGS, and add the label number to the annotation." (TeX-fold-mode 1))) (t (setq preview-preprocess-functions - (delete #'tex-numbers-preview-preprocessor preview-preprocess-functions)) + (delete #'auctex-label-numbers-preview-preprocessor preview-preprocess-functions)) (advice-remove 'LaTeX-completion-label-annotation-function - #'tex-numbers-label-annotation-advice) - (advice-remove 'reftex-goto-label #'tex-numbers--reftex-goto-label-advice) - (dolist (macro tex-numbers-macro-list) - (let ((func (intern (format "tex-numbers-%s-display" macro)))) + #'auctex-label-numbers-label-annotation-advice) + (advice-remove 'reftex-goto-label #'auctex-label-numbers--reftex-goto-label-advice) + (dolist (macro auctex-label-numbers-macro-list) + (let ((func (intern (format "auctex-label-numbers-%s-display" macro)))) (setq TeX-fold-macro-spec-list (seq-remove (lambda (elem) (eq (car elem) func)) TeX-fold-macro-spec-list))) - (when-let ((saved (assoc macro tex-numbers--saved-spec-list))) + (when-let ((saved (assoc macro auctex-label-numbers--saved-spec-list))) (dolist (spec TeX-fold-macro-spec-list) (when (eq (car spec) (cdr saved)) (push macro (cadr spec)))))) - (setq tex-numbers--saved-spec-list nil) + (setq auctex-label-numbers--saved-spec-list nil) (when TeX-fold-mode (TeX-fold-mode 1))))) -(provide 'tex-numbers) -;;; tex-numbers.el ends here +(provide 'auctex-label-numbers) +;;; auctex-label-numbers.el ends here