branch: externals/tex-parens
commit 82eff30aa59f64437c9c68f9c471918b86866bf5
Author: Paul Nelson <[email protected]>
Commit: Paul Nelson <[email protected]>
ignore dollar delimiters that do not demarcate math mode
---
README.org | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tex-parens.el | 32 ++++++++++++++++---------------
2 files changed, 78 insertions(+), 15 deletions(-)
diff --git a/README.org b/README.org
new file mode 100644
index 0000000000..f7501cd297
--- /dev/null
+++ b/README.org
@@ -0,0 +1,61 @@
+#+title: tex-parens.el: like lisp.el but for tex
+#+author: Paul Nelson
+
+* Overview
+Emacs comes with the following useful commands for working with lists, sexps
and defuns (see the info nodes
[[https://www.gnu.org/software/emacs/manual/html_node/emacs/Expressions.html][Expressions]]
and
[[https://www.gnu.org/software/emacs/manual/html_node/emacs/Defuns.html][Defuns]]):
+
+| bind | command | docstring
|
+|-----------------+--------------------+--------------------------------------------------------------------|
+| C-M-@ | mark-sexp | Set mark ARG sexps from point or move
mark one sexp. |
+| C-M-a | beginning-of-defun | Move backward to the beginning of a
defun. |
+| C-M-b | backward-sexp | Move backward across one balanced
expression (sexp). |
+| C-M-d | down-list | Move forward down one level of
parentheses. |
+| C-M-e | end-of-defun | Move forward to next end of defun.
|
+| C-M-f | forward-sexp | Move forward across one balanced
expression (sexp). |
+| C-M-h | mark-defun | Put mark at end of this defun, point
at beginning. |
+| C-M-k | kill-sexp | Kill the sexp (balanced expression)
following point. |
+| C-M-n | forward-list | Move forward across one balanced
group of parentheses. |
+| C-M-p | backward-list | Move backward across one balanced
group of parentheses. |
+| C-M-t | transpose-sexps | Like C-t (‘transpose-chars’), but
applies to sexps. |
+| C-M-u | backward-up-list | Move backward out of one level of
parentheses. |
+| C-M-<backspace> | backward-kill-sexp | Kill the sexp (balanced expression)
preceding point. |
+| - | up-list | Move forward out of one level of
parentheses. |
+| - | delete-pair | Delete a pair of characters enclosing
ARG sexps that follow point. |
+| - | raise-sexp | Raise N sexps one level higher up the
tree. |
+
+This packages provides analogous commands adapted for tex buffers, with the
role of parentheses =()=, brackets =[]= and braces ={}= played by the following:
+- =\begin{...}= - =\end{...}= pairs
+- math environment delimiters =$...$=, =\(...\)=, =\[...\]=, =$$...$$=
+- mathematical delimiters: parentheses, brackets, braces, =\langle= -
=\rangle=, =\lvert= - =\rvert=, etc., and compositions of these with modifiers
such as =\left= - =\right=, =\Bigg=, etc. See =M-x customize-group tex-parens=
for details.
+
+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]],
and also in [[https://www.gnu.org/software/auctex/][AUCTeX]]. 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/Fuco1/smartparens][smartparens,]] [[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=, or whatever it is, if you don't
use AUCTeX):
+#+begin_src elisp
+(use-package tex-parens
+ :bind
+ (:map LaTeX-mode-map
+ ("C-M-f" . tp-forward-sexp)
+ ("C-M-b" . tp-backward-sexp)
+ ("C-M-n" . tp-forward-list)
+ ("C-M-p" . tp-backward-list)
+ ("C-M-u" . tp-backward-up-list)
+ ("M-u" . tp-up-list)
+ ("C-M-g" . tp-down-list)
+ ("M-_" . tp-delete-pair)
+ ("C-M-SPC" . tp-mark-sexp)
+ ("C-M-k" . tp-kill-sexp)
+ ("C-M-t" . transpose-sexps)
+ ("C-M-<backspace>" . tp-backward-kill-sexp)
+ ("M-+" . tp-raise-sexp))
+ :hook
+ (LaTeX-mode . tp-setup))
+#+end_src
+
+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-: (tp-setup)= for the changes to take affect.
+
+* Issues
+1. This isn't really an issue in the sense that it doesn't affect usage, but
it shouldn't have been necessary to define the functions =tp-mark-sexp=,
=tp-kill-sexp=, =tp-backward-kill-sexp=, =tp-raise-sexp= -- it should suffice
to use the
+built-in functions =mark-sexp=, =kill-sexp=, =backward-kill-sexp=,
=raise-sexp= with the local variable =forward-sexp-function= set to
=#'tp-forward-sexp=. Unfortunately, Emacs freezes when I attempt to do so, for
reasons that I haven't been able to debug.
+
diff --git a/tex-parens.el b/tex-parens.el
index 74f0eb17c3..1bf371c172 100644
--- a/tex-parens.el
+++ b/tex-parens.el
@@ -233,17 +233,16 @@ defun-based commands."
;; ignore double prime in math-mode
(equal m-str "''")
(> (tp--math-face-p) 0))
- ;; (and
- ;; ;; ignore dollar signs that don't matter for whatever reason
- ;; (member m-str '("$" "$$"))
- ;; (equal
- ;; (save-excursion
- ;; (goto-char (1- m-begin))
- ;; (tp--math-face-p))
- ;; (save-excursion
- ;; (goto-char m-end)
- ;; (tp--math-face-p))))
- ))
+ (and
+ ;; ignore dollar delimiters that do not demarcate math mode
+ (member m-str '("$" "$$"))
+ (equal
+ (save-excursion
+ (goto-char (1- m-begin))
+ (tp--math-face-p))
+ (save-excursion
+ (goto-char m-end)
+ (tp--math-face-p))))))
(defun tp--search-forward (regexp bound)
(let (success done)
@@ -258,6 +257,8 @@ defun-based commands."
(when success
(match-string 0))))
+;; TODO (not essential): speed this up
+
(defun tp--search-backward (regexp bound)
(let* ((text (buffer-substring bound (point)))
(buf (current-buffer))
@@ -271,13 +272,13 @@ defun-based commands."
(if (re-search-forward regexp bound t)
(when (not
(let ((new-point (point))
- (m-string (match-string 0))
- (m-begin (match-beginning 0))
- (m-end (match-end 0)))
+ (m-string (match-string 0)))
(with-current-buffer buf
(save-excursion
(backward-char (1- new-point))
- (tp--ignore m-string m-begin m-end)))))
+ (tp--ignore m-string
+ (point)
+ (+ (point) (length m-string)))))))
(setq done t
success t))
(setq done t)))
@@ -287,6 +288,7 @@ defun-based commands."
(let ((new-point (car result))
(match (cdr result)))
(backward-char (1- new-point))
+ ;; (goto-char (- (point) (1- new-point)))
(reverse match)))))
(defun tp--forward-bound ()