branch: externals/tex-parens commit e8c794cb10a35176a951673812bc232bba925195 Author: Paul Nelson <ultr...@gmail.com> Commit: Paul Nelson <ultr...@gmail.com>
New minor mode `tex-parens-mode' * tex-parens.el (tex-parens-mode): New minor mode, featuring a keymap that remaps the main commands. (tex-parens--saved-beginning-of-defun-function, tex-parens--saved-transpose-sexps-function, tex-parens--saved-end-of-defun-function): New variables. (tex-parens-setup): Use the new variables. * README.org: Simplified configuration, using the new minor mode. --- README.org | 29 +++++++++----------------- tex-parens.el | 65 ++++++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/README.org b/README.org index b3a8e3cde5..74500321c1 100644 --- a/README.org +++ b/README.org @@ -31,29 +31,17 @@ This package provides analogous commands adapted for tex buffers, with the class There is some support in this direction in the built-in [[https://www.gnu.org/software/emacs/manual/html_node/emacs/TeX-Mode.html][tex-mode]], in [[https://www.gnu.org/software/auctex/][AUCTeX]] and in [[https://github.com/Malabarba/latex-extra][latex-extra]]. There are many packages, such as [[https://paredit.org/][paredit]] and [[https://github.com/abo-abo/lispy][lispy]], that add further useful commands and bindings to lisp modes, and many other packages, such as [[https://github.com [...] * Configuration -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]], adjusting the binds according to preference (and replacing =LaTeX-mode= with =latex-mode= or =tex-mode= or =plain-tex-mode=, whichever is appropriate, if you don't use AUCTeX): +This package is available via GNU ELPA, so you can install it using =package-install= or =list-packages=. You can activate it via =M-x tex-parens-mode=, which remaps binds for the commands listed above to their =tex-parens-*= counterparts. + +To activate automatically, add the following lines to your config (the first hook applies if use the built-in [[https://www.gnu.org/software/emacs/manual/html_node/emacs/TeX-Mode.html][tex-mode]], the second if you use [[https://www.gnu.org/software/auctex/][AUCTeX]]): #+begin_src elisp (use-package tex-parens - :bind - (:map LaTeX-mode-map - ([remap forward-sexp] . tex-parens-forward-sexp) - ([remap backward-sexp] . tex-parens-backward-sexp) - ([remap forward-list] . tex-parens-forward-list) - ([remap backward-list] . tex-parens-backward-list) - ([remap backward-up-list] . tex-parens-backward-up-list) - ([remap up-list] . tex-parens-up-list) - ([remap down-list] . tex-parens-down-list) - ([remap delete-pair] . tex-parens-delete-pair) - ([remap mark-sexp] . tex-parens-mark-sexp) - ([remap kill-sexp] . tex-parens-kill-sexp) - ([remap transpose-sexps] . transpose-sexps) - ([remap backward-kill-sexp] . tex-parens-backward-kill-sexp) - ([remap raise-sexp] . tex-parens-raise-sexp)) :hook - (LaTeX-mode . tex-parens-setup)) + (tex-mode . tex-parens-mode) + (TeX-mode . tex-parens-mode)) #+end_src -With this setup, whatever binds you generally use for the indicated list commands will be forwarded to =tex-parens= in =LaTeX-mode=. If you want to specify the keys more directly, use instead something like the following: +For finer control over how keys are bound, you can avoid =tex-parens-mode= and instead use something like the following: #+begin_src elisp (use-package tex-parens :bind @@ -72,12 +60,13 @@ With this setup, whatever binds you generally use for the indicated list command ("C-M-<backspace>" . tex-parens-backward-kill-sexp) ("M-+" . tex-parens-raise-sexp)) :hook - (LaTeX-mode . tex-parens-setup)) + (tex-mode . tex-parens-setup) + (TeX-mode . tex-parens-setup)) #+end_src The precise use-package declaration that I use may be found in [[https://github.com/ultronozm/emacsd/blob/main/init-latex.el][the LaTeX part of my config]] (elpaca branch). -Use =M-x customize-group tex-parens= to configure further. If you tweak the customization variables concerning delimiters and modifiers, then you'll need to reload your tex file or =M-: (tex-parens-setup)= for the changes to take effect. +Use =M-x customize-group tex-parens= to configure further. If you tweak the customization variables concerning delimiters and modifiers, then you'll need to reload your tex file, reset the minor mode, or do =M-: (tex-parens-setup)= for the changes to take effect. * Variants This package contains the additional functions =tex-parens-burp-left=, =tex-parens-burp-right=, =tex-parens-mark-inner=, =tex-parens-beginning-of-list= and =tex-parens-end-of-list=, which are defined in terms of the sexp/list primitives; see the =C-h f= documentation for details. diff --git a/tex-parens.el b/tex-parens.el index f5259380dc..b45556d090 100644 --- a/tex-parens.el +++ b/tex-parens.el @@ -30,26 +30,13 @@ ;; langle/rangle), together with their tex modifiers (e.g., ;; left/right, bigl/bigr). See README.org for more details. ;; -;; Sample configuration: +;; You can activate it via `M-x tex-parens-mode'. To activate +;; automatically, add the following to your init file: ;; ;; (use-package tex-parens -;; :bind -;; (:map LaTeX-mode-map -;; ([remap forward-sexp] . tex-parens-forward-sexp) -;; ([remap backward-sexp] . tex-parens-backward-sexp) -;; ([remap forward-list] . tex-parens-forward-list) -;; ([remap backward-list] . tex-parens-backward-list) -;; ([remap backward-up-list] . tex-parens-backward-up-list) -;; ([remap up-list] . tex-parens-up-list) -;; ([remap down-list] . tex-parens-down-list) -;; ([remap delete-pair] . tex-parens-delete-pair) -;; ([remap mark-sexp] . tex-parens-mark-sexp) -;; ([remap kill-sexp] . tex-parens-kill-sexp) -;; ([remap transpose-sexps] . transpose-sexps) -;; ([remap backward-kill-sexp] . tex-parens-backward-kill-sexp) -;; ([remap raise-sexp] . tex-parens-raise-sexp)) ;; :hook -;; (LaTeX-mode . tex-parens-setup)) +;; (tex-mode . tex-parens-mode) +;; (TeX-mode . tex-parens-mode)) ;;; Code: @@ -173,6 +160,10 @@ form delimiters which are visibly `left'/`opening' or (defvar tex-parens--regexp-reverse nil) (defvar tex-parens--regexp-reverse+ nil) +(defvar tex-parens--saved-beginning-of-defun-function nil) +(defvar tex-parens--saved-transpose-sexps-function nil) +(defvar tex-parens--saved-end-of-defun-function nil) + (defun tex-parens-setup () "Set up tex-parens. Intended as a hook for `LaTeX-mode'." (dolist (func '(tex-parens-down-list @@ -187,10 +178,16 @@ form delimiters which are visibly `left'/`opening' or tex-parens-forward-sexp tex-parens-backward-sexp)) (add-to-list 'TeX-fold-auto-reveal-commands func)) + (setq-local tex-parens--saved-beginning-of-defun-function + beginning-of-defun-function) (setq-local beginning-of-defun-function #'tex-parens--beginning-of-defun) - (setq-local transpose-sexps-default-function + (setq-local tex-parens--saved-transpose-sexps-function + transpose-sexps-function) + (setq-local transpose-sexps-function #'tex-parens-transpose-sexps-default-function) - (setq end-of-defun-function #'tex-parens--end-of-defun) + (setq-local tex-parens--saved-end-of-defun-function + end-of-defun-function) + (setq-local end-of-defun-function #'tex-parens--end-of-defun) (setq tex-parens--pairs (tex-parens--generate-pairs)) (setq tex-parens--pairs-swap (mapcar (lambda (x) (cons (cdr x) (car x))) tex-parens--pairs)) @@ -241,6 +238,36 @@ form delimiters which are visibly `left'/`opening' or ;; (setq-local forward-sexp-function #'tex-parens-forward-sexp) ) +;;;###autoload +(define-minor-mode tex-parens-mode + "Toggle tex-parens mode. +Tex Parens mode is a minor mode in which lisp/sexp/defun-based commands +are adapted to tex environments and math delimiters. The affected +commands include, for instance, `forward-sexp', `forward-list' and +`beginning-of-defun'." + :lighter nil + :keymap (let ((map (make-sparse-keymap))) + (define-key map [remap forward-sexp] #'tex-parens-forward-sexp) + (define-key map [remap backward-sexp] #'tex-parens-backward-sexp) + (define-key map [remap forward-list] #'tex-parens-forward-list) + (define-key map [remap backward-list] #'tex-parens-backward-list) + (define-key map [remap backward-up-list] #'tex-parens-backward-up-list) + (define-key map [remap up-list] #'tex-parens-up-list) + (define-key map [remap down-list] #'tex-parens-down-list) + (define-key map [remap delete-pair] #'tex-parens-delete-pair) + (define-key map [remap mark-sexp] #'tex-parens-mark-sexp) + (define-key map [remap kill-sexp] #'tex-parens-kill-sexp) + (define-key map [remap backward-kill-sexp] #'tex-parens-backward-kill-sexp) + (define-key map [remap raise-sexp] #'tex-parens-raise-sexp) + map) + (cond + (tex-parens-mode + (tex-parens-setup)) + (t + (setq-local beginning-of-defun-function tex-parens--saved-beginning-of-defun-function) + (setq-local transpose-sexps-function tex-parens--saved-transpose-sexps-function) + (setq-local end-of-defun-function tex-parens--saved-end-of-defun-function)))) + (defcustom tex-parens-search-limit 10000 "How far to search for a delimiter, in either direction. This should exceed the length, in characters, of the longest