-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
math-delimiters.el
275 lines (249 loc) · 10.7 KB
/
math-delimiters.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
;;; math-delimiters.el --- Insert math delimiters in TeX, LaTeX and Org buffers -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Omar Antolín Camarena
;; Author: Omar Antolín Camarena <omar@matem.unam.mx>
;; Keywords: convenience
;; Version: 0.1
;; Package-Requires: ((emacs "24.3"))
;; Homepage: https://github.com/oantolin/math-delimiters
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides the command `math-delimiters-insert' to
;; insert TeX/LaTeX math delimiters. This command is meant to be bound
;; to $ in buffers where the user wants to use it. The author, for
;; example, binds it in `LaTeX-mode-map' and in `org-mode-map'.
;;
;; Note that the excellent `cdlatex' includes a `cdlatex-dollar'
;; command that it binds to $. Users of both packages will probably
;; want to unbind $ in `cdlatex-mode-map'.
;;
;; The delimiters used for inline math and display math by
;; `math-delimiters-insert' are customizable, defaulting to \(...\) and
;; \[...\] respectively. A command `math-delimiters-toggle' is
;; provided to quickly toggle between $...$ and \(...\) for inline
;; math and between $$...$$ and \[...\] for display math.
;;
;; By default, inline math is translated into display math with no
;; additional line breaks. This can be modified by setting
;; `math-delimiters-compressed-display-math' to `nil'. For example,
;; expression of the form \(...\) will then be translated to
;; \n\[\n...\n\]\n.
;;
;; When translating inline math to display math, often it is desirable
;; to include punctuation into the display math and exclude it from of
;; the inline math environment. For that purpose, the variable
;; `math-delimiters-include-characters' is available; it takes a list of
;; characters to act upon. For example, setting it to (list ?.) and
;; then toggling to display math on the expression "\(\eta\)." would
;; turn it into "\[\eta.\]" .
;;
;; Finally, a command `math-delimiters-no-dollars' is provided to
;; replace all $...$ and $$...$$ delimiters with \(...\) and \[...\],
;; respectively.
;;; Code:
(require 'cl-lib)
;;;###autoload
(defun math-delimiters-no-dollars (&optional beg end)
"Convert math formulas in buffer from dollars to \\(\\) and \\=\\[\\].
If a region is active, convert all formulas from BEG to END instead."
(interactive
(when (region-active-p)
(list (region-beginning) (region-end))))
(cl-flet ((replace-all (a b &optional c)
(goto-char (or beg (point-min)))
(while (search-forward a end t)
(replace-match b t t)
(when c
(search-forward a)
(replace-match c t t)))))
(save-excursion
(replace-all "\\$" "\\dollar ")
(replace-all "$$" "\\[" "\\]")
(replace-all "$" "\\(" "\\)")
(replace-all "\\dollar " "\\$"))))
(defgroup math-delimiters nil
"Delimiters for LaTeX math mode."
:group 'tex)
(defcustom math-delimiters-inline
'("\\(" . "\\)")
"Delimiters to use for inline math mode."
:group 'math-delimiters
:type '(choice
(const :tag "Parentheses" ("\\(" . "\\)"))
(const :tag "Dollars" ("$" . "$"))
(cons :tag "Other" string string)))
(defcustom math-delimiters-display
'("\\[" . "\\]")
"Delimiters to use for display math mode."
:group 'math-delimiters
:type '(choice
(const :tag "Brackets" ("\\[" . "\\]"))
(const :tag "Dollars" ("$$" . "$$"))
(cons :tag "Other" string string)))
(defcustom math-delimiters-include-characters ",.;"
"Characters to include in display math mode.
If any of them are right behind an inline math equation, slurp
them into the display math environment and barf them back out
when converting to inline math."
:group 'math-delimiters
:type 'string)
(defcustom math-delimiters-compressed-display-math t
"Whether display math should be compressed.
A value of `t' means that, when transforming from inline to
display math, no additional line breaks are inserted. For
example, we go from
text-before \\(1 + 1\\) text-after
to
text-before \\=\\[1 + 1\\] text-after.
A value of `nil', however, would switch between the above inline
math version and
text-before
\\=\\[
1 + 1
\\]
text-after."
:group 'math-delimiters
:type 'boolean)
(defun math-delimiters--slurp-or-barf-characters (from-close)
"Slurp or barf characters.
The characters considered are the ones in the string
`math-delimiters-include-characters'. Thus, include these
characters in display math should they come right after an inline
math statement, and exclude them in the other direction.
The FROM-CLOSE parameter indicates the closing delimiter
currently at point and is used to decide whether to slurp or
barf."
(if (equal from-close (cdr math-delimiters-inline)) ; from inline math
(when (cl-find (char-after) math-delimiters-include-characters)
(forward-char))
(let ((orig-pos (point))) ; from display math
(skip-chars-backward " \t")
(when (bolp) (backward-char 1) (skip-chars-backward " \t"))
(cond ((cl-find (char-before) math-delimiters-include-characters)
(backward-char))
(math-delimiters-compressed-display-math
(goto-char orig-pos))))))
(defun math-delimiters--toggle-line-breaks (open close)
(let* ((to-display (equal (car math-delimiters-display) open)))
(cl-flet ((toggle-close ()
(if to-display
(progn
(newline-and-indent)
(search-backward close)
(newline-and-indent))
(join-line -1)
(join-line -1)
(backward-char (1+ (length close)))))
(toggle-open ()
(if to-display
(progn (newline-and-indent)
(forward-char (length open))
(newline-and-indent))
(join-line)
(join-line -1)
;; If necessary, like when the inline delimiters are
;; Dollars, fix `join-line's whitespace insertion.
(when (equal (char-after) ?\s)
(delete-char 1)))))
(toggle-close)
(search-backward open)
(toggle-open)
(search-forward close))))
;;;###autoload
(defun math-delimiters-toggle (arg)
"Toggle between $...$ and \\(...\\) for inline math.
When ARG is non-nil (interactively, if called with universal
prefix argument), also toggle the display math delimiters between
$$...$$ and \\=\\[...\\]."
(interactive "P")
(setf math-delimiters-inline
(if (equal math-delimiters-inline '("\\(" . "\\)"))
'("$" . "$")
'("\\(" . "\\)")))
(when arg
(setf math-delimiters-display
(if (equal math-delimiters-display '("\\[" . "\\]"))
'("$$" . "$$")
'("\\[" . "\\]"))))
(message "%sinline%s and %sdisplay%s"
(car math-delimiters-inline)
(cdr math-delimiters-inline)
(car math-delimiters-display)
(cdr math-delimiters-display)))
;;;###autoload
(defun math-delimiters-insert ()
"Insert math delimiters.
If region is active surround it. When repeated, toggle between
display and inline math. Also toggle between display and inline
if called from inside empty math delimiters, or just after math
delimiters."
(interactive)
(if (and (eq major-mode 'org-mode)
(save-excursion
(beginning-of-line)
(looking-at "^\\s-*#\\+TBLFM: ")))
(insert "$")
(let ((open (car math-delimiters-inline))
(close (cdr math-delimiters-inline))
(display-open (car math-delimiters-display))
(display-close (cdr math-delimiters-display)))
(cl-flet* ((after-p (close)
(looking-back (regexp-quote close)
(- (point) (length close))))
(flip-after (from-open from-close to-open to-close)
(delete-char (- (length from-close)))
(math-delimiters--slurp-or-barf-characters from-close)
(insert to-close)
(let ((end (point)))
(backward-char (length to-close))
(search-backward from-open)
(delete-char (length from-open))
(insert to-open)
(goto-char (+ end (- (length to-open)
(length from-open)))))
(unless math-delimiters-compressed-display-math
(math-delimiters--toggle-line-breaks to-open to-close)))
(middle-p (open close)
(and (save-excursion
(skip-chars-forward " \t\n")
(looking-at (regexp-quote close)))
(save-excursion
(skip-chars-backward " \t\n")
(looking-back (regexp-quote open)
(- (point) (length open))))))
(flip-middle (from-open from-close to-open to-close)
(search-forward from-close)
(flip-after from-open from-close to-open to-close)
(backward-char (length to-close))
(unless (or math-delimiters-compressed-display-math
(equal to-close close))
(beginning-of-line)
(backward-char 1))))
(cond
((use-region-p)
(let ((end (region-end)))
(goto-char (region-beginning))
(insert open)
(goto-char (+ end (length open)))
(insert close)))
((middle-p display-open display-close)
(flip-middle display-open display-close open close))
((after-p display-close)
(flip-after display-open display-close open close))
((middle-p open close)
(flip-middle open close display-open display-close))
((after-p close)
(flip-after open close display-open display-close))
(t
(insert open close)
(backward-char (length close))))))))
(provide 'math-delimiters)
;;; math-delimiters.el ends here