Skip to content

Commit 3d3c536

Browse files
committed
Ignore double backslashes (newlines) in Latex (#784)
1 parent b8fdee3 commit 3d3c536

File tree

1 file changed

+72
-13
lines changed

1 file changed

+72
-13
lines changed

smartparens-latex.el

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,34 @@
7777
(delete-char 2)
7878
(forward-char 2))))
7979

80-
(defun sp-latex-point-after-backslash (id action _context)
81-
"Return t if point follows a backslash, nil otherwise.
82-
This predicate is only tested on \"insert\" action.
80+
(defun sp-number-of-backslashes-before-point ()
81+
(let ((p (point)))
82+
(while (and (> p 0) (equal (char-before p) ?\\ ))
83+
(setq p (- p 1)))
84+
(- (point) p)))
85+
86+
(defun sp--latex-backslash-skip-match (ms mb _me)
87+
"Skips a match if preceeded by uneven number of backslashes."
88+
(and ms
89+
(save-excursion
90+
(goto-char mb)
91+
(not (evenp (sp-number-of-backslashes-before-point))))))
92+
93+
(defun sp-latex-point-after-backslash (id action context)
94+
"Return t if point follows an uneven number of backslashes (a
95+
double backslash is a newline; we'd like to ignore those), nil
96+
otherwise. This predicate is only tested on \"insert\" action.
8397
ID, ACTION, CONTEXT."
8498
(when (eq action 'insert)
85-
(let ((trigger (sp-get-pair id :trigger)))
86-
(looking-back (concat "\\\\" (regexp-quote (if trigger trigger id))) nil))))
99+
(let* ((trigger (sp-get-pair id :trigger))
100+
(start (- (point) (length (if trigger trigger id)))))
101+
(when (> start 1)
102+
(save-excursion
103+
(goto-char start)
104+
(not (evenp (sp-number-of-backslashes-before-point))))))))
87105

88106
(add-to-list 'sp-navigate-skip-match
89-
'((tex-mode plain-tex-mode latex-mode) . sp--backslash-skip-match))
107+
'((tex-mode plain-tex-mode latex-mode) . sp--latex-backslash-skip-match))
90108

91109
(sp-with-modes '(
92110
tex-mode
@@ -100,7 +118,8 @@ ID, ACTION, CONTEXT."
100118
:unless '(sp-latex-point-after-backslash))
101119
;; math modes, yay. The :actions are provided automatically if
102120
;; these pairs do not have global definitions.
103-
(sp-local-pair "$" "$")
121+
(sp-local-pair "$" "$"
122+
:unless '(sp-latex-point-after-backslash))
104123
(sp-local-pair "\\[" "\\]"
105124
:unless '(sp-latex-point-after-backslash))
106125

@@ -118,60 +137,101 @@ ID, ACTION, CONTEXT."
118137
:post-handlers '(sp-latex-skip-double-quote))
119138

120139
;; add the prefix function sticking to {} pair
121-
(sp-local-pair "{" nil :prefix "\\\\\\(\\sw\\|\\s_\\)*")
140+
(sp-local-pair "{" "}"
141+
:prefix "\\\\\\(\\sw\\|\\s_\\)*"
142+
:unless '(sp-latex-point-after-backslash))
122143

123144
;; do not add more space when slurping
124-
(sp-local-pair "{" "}")
125-
(sp-local-pair "(" ")")
126-
(sp-local-pair "[" "]")
145+
(sp-local-pair "\\{" "\\}"
146+
:unless '(sp-latex-point-after-backslash))
147+
(sp-local-pair "[" "]"
148+
:unless '(sp-latex-point-after-backslash))
149+
(sp-local-pair "(" ")"
150+
:unless '(sp-latex-point-after-backslash))
151+
(sp-local-pair "\\(" "\\)"
152+
:unless '(sp-latex-point-after-backslash))
127153

128154
;; pairs for big brackets. Needs more research on what pairs are
129155
;; useful to add here. Post suggestions if you know some.
130156
(sp-local-pair "\\left(" "\\right)"
131157
:trigger "\\l("
132158
:when '(sp-in-math-p)
159+
:unless '(sp-latex-point-after-backslash)
133160
:post-handlers '(sp-latex-insert-spaces-inside-pair))
134161
(sp-local-pair "\\left[" "\\right]"
135162
:trigger "\\l["
136163
:when '(sp-in-math-p)
164+
:unless '(sp-latex-point-after-backslash)
137165
:post-handlers '(sp-latex-insert-spaces-inside-pair))
138166
(sp-local-pair "\\left\\{" "\\right\\}"
139167
:trigger "\\l{"
140168
:when '(sp-in-math-p)
169+
:unless '(sp-latex-point-after-backslash)
141170
:post-handlers '(sp-latex-insert-spaces-inside-pair))
142171
(sp-local-pair "\\left|" "\\right|"
143172
:trigger "\\l|"
144173
:when '(sp-in-math-p)
174+
:unless '(sp-latex-point-after-backslash)
145175
:post-handlers '(sp-latex-insert-spaces-inside-pair))
146176
(sp-local-pair "\\bigl(" "\\bigr)"
177+
:when '(sp-in-math-p)
178+
:unless '(sp-latex-point-after-backslash)
147179
:post-handlers '(sp-latex-insert-spaces-inside-pair))
148180
(sp-local-pair "\\biggl(" "\\biggr)"
181+
:when '(sp-in-math-p)
182+
:unless '(sp-latex-point-after-backslash)
149183
:post-handlers '(sp-latex-insert-spaces-inside-pair))
150184
(sp-local-pair "\\Bigl(" "\\Bigr)"
185+
:when '(sp-in-math-p)
186+
:unless '(sp-latex-point-after-backslash)
151187
:post-handlers '(sp-latex-insert-spaces-inside-pair))
152188
(sp-local-pair "\\Biggl(" "\\Biggr)"
189+
:when '(sp-in-math-p)
190+
:unless '(sp-latex-point-after-backslash)
153191
:post-handlers '(sp-latex-insert-spaces-inside-pair))
154192
(sp-local-pair "\\bigl[" "\\bigr]"
193+
:when '(sp-in-math-p)
194+
:unless '(sp-latex-point-after-backslash)
155195
:post-handlers '(sp-latex-insert-spaces-inside-pair))
156196
(sp-local-pair "\\biggl[" "\\biggr]"
197+
:when '(sp-in-math-p)
198+
:unless '(sp-latex-point-after-backslash)
157199
:post-handlers '(sp-latex-insert-spaces-inside-pair))
158200
(sp-local-pair "\\Bigl[" "\\Bigr]"
201+
:when '(sp-in-math-p)
202+
:unless '(sp-latex-point-after-backslash)
159203
:post-handlers '(sp-latex-insert-spaces-inside-pair))
160204
(sp-local-pair "\\Biggl[" "\\Biggr]"
205+
:when '(sp-in-math-p)
206+
:unless '(sp-latex-point-after-backslash)
161207
:post-handlers '(sp-latex-insert-spaces-inside-pair))
162208
(sp-local-pair "\\bigl\\{" "\\bigr\\}"
209+
:when '(sp-in-math-p)
210+
:unless '(sp-latex-point-after-backslash)
163211
:post-handlers '(sp-latex-insert-spaces-inside-pair))
164212
(sp-local-pair "\\biggl\\{" "\\biggr\\}"
213+
:when '(sp-in-math-p)
214+
:unless '(sp-latex-point-after-backslash)
165215
:post-handlers '(sp-latex-insert-spaces-inside-pair))
166216
(sp-local-pair "\\Bigl\\{" "\\Bigr\\}"
217+
:when '(sp-in-math-p)
218+
:unless '(sp-latex-point-after-backslash)
167219
:post-handlers '(sp-latex-insert-spaces-inside-pair))
168220
(sp-local-pair "\\Biggl\\{" "\\Biggr\\}"
221+
:when '(sp-in-math-p)
222+
:unless '(sp-latex-point-after-backslash)
169223
:post-handlers '(sp-latex-insert-spaces-inside-pair))
170224
(sp-local-pair "\\lfloor" "\\rfloor"
225+
:when '(sp-in-math-p)
226+
:unless '(sp-latex-point-after-backslash)
171227
:post-handlers '(sp-latex-insert-spaces-inside-pair))
172228
(sp-local-pair "\\lceil" "\\rceil"
229+
:when '(sp-in-math-p)
230+
:unless '(sp-latex-point-after-backslash)
173231
:post-handlers '(sp-latex-insert-spaces-inside-pair))
174232
(sp-local-pair "\\langle" "\\rangle"
233+
:when '(sp-in-math-p)
234+
:unless '(sp-latex-point-after-backslash)
175235
:post-handlers '(sp-latex-insert-spaces-inside-pair))
176236
(sp-local-pair "\\lVert" "\\rVert"
177237
:when '(sp-in-math-p)
@@ -181,9 +241,8 @@ ID, ACTION, CONTEXT."
181241
:when '(sp-in-math-p)
182242
:trigger "\\lvert"
183243
:post-handlers '(sp-latex-insert-spaces-inside-pair))
184-
244+
185245
;; some common wrappings
186-
(sp-local-tag "\"" "``" "''" :actions '(wrap))
187246
(sp-local-tag "\\b" "\\begin{_}" "\\end{_}")
188247
(sp-local-tag "bi" "\\begin{itemize}" "\\end{itemize}")
189248
(sp-local-tag "be" "\\begin{enumerate}" "\\end{enumerate}"))

0 commit comments

Comments
 (0)