Skip to content

Commit 3ed29e9

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

File tree

1 file changed

+77
-18
lines changed

1 file changed

+77
-18
lines changed

smartparens-latex.el

Lines changed: 77 additions & 18 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 sp-in-math-p))
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

@@ -117,72 +136,112 @@ ID, ACTION, CONTEXT."
117136
:post-handlers '(sp-latex-skip-double-quote))
118137

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

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

127153
;; pairs for big brackets. Needs more research on what pairs are
128154
;; useful to add here. Post suggestions if you know some.
129155
(sp-local-pair "\\left(" "\\right)"
130156
:trigger "\\l("
131157
:when '(sp-in-math-p)
158+
:unless '(sp-latex-point-after-backslash)
132159
:post-handlers '(sp-latex-insert-spaces-inside-pair))
133160
(sp-local-pair "\\left[" "\\right]"
134161
:trigger "\\l["
135162
:when '(sp-in-math-p)
163+
:unless '(sp-latex-point-after-backslash)
136164
:post-handlers '(sp-latex-insert-spaces-inside-pair))
137165
(sp-local-pair "\\left\\{" "\\right\\}"
138166
:trigger "\\l{"
139167
:when '(sp-in-math-p)
168+
:unless '(sp-latex-point-after-backslash)
140169
:post-handlers '(sp-latex-insert-spaces-inside-pair))
141170
(sp-local-pair "\\left|" "\\right|"
142171
:trigger "\\l|"
143172
:when '(sp-in-math-p)
173+
:unless '(sp-latex-point-after-backslash)
144174
:post-handlers '(sp-latex-insert-spaces-inside-pair))
145175
(sp-local-pair "\\bigl(" "\\bigr)"
176+
:when '(sp-in-math-p)
177+
:unless '(sp-latex-point-after-backslash)
146178
:post-handlers '(sp-latex-insert-spaces-inside-pair))
147179
(sp-local-pair "\\biggl(" "\\biggr)"
180+
:when '(sp-in-math-p)
181+
:unless '(sp-latex-point-after-backslash)
148182
:post-handlers '(sp-latex-insert-spaces-inside-pair))
149183
(sp-local-pair "\\Bigl(" "\\Bigr)"
184+
:when '(sp-in-math-p)
185+
:unless '(sp-latex-point-after-backslash)
150186
:post-handlers '(sp-latex-insert-spaces-inside-pair))
151187
(sp-local-pair "\\Biggl(" "\\Biggr)"
188+
:when '(sp-in-math-p)
189+
:unless '(sp-latex-point-after-backslash)
152190
:post-handlers '(sp-latex-insert-spaces-inside-pair))
153191
(sp-local-pair "\\bigl[" "\\bigr]"
192+
:when '(sp-in-math-p)
193+
:unless '(sp-latex-point-after-backslash)
154194
:post-handlers '(sp-latex-insert-spaces-inside-pair))
155195
(sp-local-pair "\\biggl[" "\\biggr]"
196+
:when '(sp-in-math-p)
197+
:unless '(sp-latex-point-after-backslash)
156198
:post-handlers '(sp-latex-insert-spaces-inside-pair))
157199
(sp-local-pair "\\Bigl[" "\\Bigr]"
200+
:when '(sp-in-math-p)
201+
:unless '(sp-latex-point-after-backslash)
158202
:post-handlers '(sp-latex-insert-spaces-inside-pair))
159203
(sp-local-pair "\\Biggl[" "\\Biggr]"
204+
:when '(sp-in-math-p)
205+
:unless '(sp-latex-point-after-backslash)
160206
:post-handlers '(sp-latex-insert-spaces-inside-pair))
161207
(sp-local-pair "\\bigl\\{" "\\bigr\\}"
208+
:when '(sp-in-math-p)
209+
:unless '(sp-latex-point-after-backslash)
162210
:post-handlers '(sp-latex-insert-spaces-inside-pair))
163211
(sp-local-pair "\\biggl\\{" "\\biggr\\}"
212+
:when '(sp-in-math-p)
213+
:unless '(sp-latex-point-after-backslash)
164214
:post-handlers '(sp-latex-insert-spaces-inside-pair))
165215
(sp-local-pair "\\Bigl\\{" "\\Bigr\\}"
216+
:when '(sp-in-math-p)
217+
:unless '(sp-latex-point-after-backslash)
166218
:post-handlers '(sp-latex-insert-spaces-inside-pair))
167219
(sp-local-pair "\\Biggl\\{" "\\Biggr\\}"
220+
:when '(sp-in-math-p)
221+
:unless '(sp-latex-point-after-backslash)
168222
:post-handlers '(sp-latex-insert-spaces-inside-pair))
169223
(sp-local-pair "\\lfloor" "\\rfloor"
224+
:when '(sp-in-math-p)
225+
:unless '(sp-latex-point-after-backslash)
170226
:post-handlers '(sp-latex-insert-spaces-inside-pair))
171227
(sp-local-pair "\\lceil" "\\rceil"
228+
:when '(sp-in-math-p)
229+
:unless '(sp-latex-point-after-backslash)
172230
:post-handlers '(sp-latex-insert-spaces-inside-pair))
173231
(sp-local-pair "\\langle" "\\rangle"
232+
:when '(sp-in-math-p)
233+
:unless '(sp-latex-point-after-backslash)
174234
:post-handlers '(sp-latex-insert-spaces-inside-pair))
175235
(sp-local-pair "\\lVert" "\\rVert"
176-
:when '(sp-in-math-p)
177-
:trigger "\\lVert"
178-
:post-handlers '(sp-latex-insert-spaces-inside-pair))
236+
:when '(sp-in-math-p)
237+
:trigger "\\lVert"
238+
:post-handlers '(sp-latex-insert-spaces-inside-pair))
179239
(sp-local-pair "\\lvert" "\\rvert"
180-
:when '(sp-in-math-p)
181-
:trigger "\\lvert"
182-
:post-handlers '(sp-latex-insert-spaces-inside-pair))
240+
:when '(sp-in-math-p)
241+
:trigger "\\lvert"
242+
:post-handlers '(sp-latex-insert-spaces-inside-pair))
183243

184244
;; some common wrappings
185-
(sp-local-tag "\"" "``" "''" :actions '(wrap))
186245
(sp-local-tag "\\b" "\\begin{_}" "\\end{_}")
187246
(sp-local-tag "bi" "\\begin{itemize}" "\\end{itemize}")
188247
(sp-local-tag "be" "\\begin{enumerate}" "\\end{enumerate}"))

0 commit comments

Comments
 (0)