Skip to content

Commit e406024

Browse files
committed
Ignore double backslashes (newlines) in Latex (#784)
1 parent 6db9904 commit e406024

1 file changed

Lines changed: 72 additions & 12 deletions

File tree

smartparens-latex.el

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,31 @@
8383
(delete-char 2)
8484
(forward-char 2))))
8585

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

94112
(defun sp-latex-point-before-word-p (id action context)
95113
"Return t if point is before a word while in navigate action.
@@ -98,7 +116,7 @@ ID, ACTION, CONTEXT."
98116
(looking-at-p "\\sw")))
99117

100118
(add-to-list 'sp-navigate-skip-match
101-
'((tex-mode plain-tex-mode latex-mode) . sp--backslash-skip-match))
119+
'((tex-mode plain-tex-mode latex-mode) . sp--latex-backslash-skip-match))
102120

103121
(sp-with-modes '(
104122
tex-mode
@@ -111,9 +129,11 @@ ID, ACTION, CONTEXT."
111129
:skip-match 'sp-latex-skip-match-apostrophe
112130
:unless '(sp-latex-point-after-backslash
113131
sp-latex-point-before-word-p))
132+
114133
;; math modes, yay. The :actions are provided automatically if
115134
;; these pairs do not have global definitions.
116-
(sp-local-pair "$" "$")
135+
(sp-local-pair "$" "$"
136+
:unless '(sp-latex-point-after-backslash))
117137
(sp-local-pair "\\[" "\\]"
118138
:unless '(sp-latex-point-after-backslash))
119139

@@ -131,64 +151,104 @@ ID, ACTION, CONTEXT."
131151
:post-handlers '(sp-latex-skip-double-quote))
132152

133153
;; add the prefix function sticking to {} pair
134-
(sp-local-pair "{" nil :prefix "\\\\\\(\\sw\\|\\s_\\)*")
135-
154+
(sp-local-pair "{" "}"
155+
:prefix "\\\\\\(\\sw\\|\\s_\\)*"
156+
:unless '(sp-latex-point-after-backslash))
157+
136158
;; do not add more space when slurping
137-
(sp-local-pair "{" "}")
138-
(sp-local-pair "(" ")")
139-
(sp-local-pair "[" "]")
159+
(sp-local-pair "\\{" "\\}"
160+
:unless '(sp-latex-point-after-backslash))
161+
(sp-local-pair "[" "]"
162+
:unless '(sp-latex-point-after-backslash))
163+
(sp-local-pair "(" ")"
164+
:unless '(sp-latex-point-after-backslash))
165+
(sp-local-pair "\\(" "\\)"
166+
:unless '(sp-latex-point-after-backslash))
140167

141168
;; pairs for big brackets. Needs more research on what pairs are
142169
;; useful to add here. Post suggestions if you know some.
143170
(sp-local-pair "\\left(" "\\right)"
144171
:trigger "\\l("
145172
:when '(sp-in-math-p)
173+
:unless '(sp-latex-point-after-backslash)
146174
:post-handlers '(sp-latex-insert-spaces-inside-pair))
147175
(sp-local-pair "\\left[" "\\right]"
148176
:trigger "\\l["
149177
:when '(sp-in-math-p)
178+
:unless '(sp-latex-point-after-backslash)
150179
:post-handlers '(sp-latex-insert-spaces-inside-pair))
151180
(sp-local-pair "\\left\\{" "\\right\\}"
152181
:trigger "\\l{"
153182
:when '(sp-in-math-p)
183+
:unless '(sp-latex-point-after-backslash)
154184
:post-handlers '(sp-latex-insert-spaces-inside-pair))
155185
(sp-local-pair "\\left|" "\\right|"
156186
:trigger "\\l|"
157187
:when '(sp-in-math-p)
188+
:unless '(sp-latex-point-after-backslash)
158189
:post-handlers '(sp-latex-insert-spaces-inside-pair))
159190
(sp-local-pair "\\bigl(" "\\bigr)"
191+
:when '(sp-in-math-p)
192+
:unless '(sp-latex-point-after-backslash)
160193
:post-handlers '(sp-latex-insert-spaces-inside-pair))
161194
(sp-local-pair "\\biggl(" "\\biggr)"
195+
:when '(sp-in-math-p)
196+
:unless '(sp-latex-point-after-backslash)
162197
:post-handlers '(sp-latex-insert-spaces-inside-pair))
163198
(sp-local-pair "\\Bigl(" "\\Bigr)"
199+
:when '(sp-in-math-p)
200+
:unless '(sp-latex-point-after-backslash)
164201
:post-handlers '(sp-latex-insert-spaces-inside-pair))
165202
(sp-local-pair "\\Biggl(" "\\Biggr)"
203+
:when '(sp-in-math-p)
204+
:unless '(sp-latex-point-after-backslash)
166205
:post-handlers '(sp-latex-insert-spaces-inside-pair))
167206
(sp-local-pair "\\bigl[" "\\bigr]"
207+
:when '(sp-in-math-p)
208+
:unless '(sp-latex-point-after-backslash)
168209
:post-handlers '(sp-latex-insert-spaces-inside-pair))
169210
(sp-local-pair "\\biggl[" "\\biggr]"
211+
:when '(sp-in-math-p)
212+
:unless '(sp-latex-point-after-backslash)
170213
:post-handlers '(sp-latex-insert-spaces-inside-pair))
171214
(sp-local-pair "\\Bigl[" "\\Bigr]"
215+
:when '(sp-in-math-p)
216+
:unless '(sp-latex-point-after-backslash)
172217
:post-handlers '(sp-latex-insert-spaces-inside-pair))
173218
(sp-local-pair "\\Biggl[" "\\Biggr]"
219+
:when '(sp-in-math-p)
220+
:unless '(sp-latex-point-after-backslash)
174221
:post-handlers '(sp-latex-insert-spaces-inside-pair))
175222
(sp-local-pair "\\bigl\\{" "\\bigr\\}"
223+
:when '(sp-in-math-p)
224+
:unless '(sp-latex-point-after-backslash)
176225
:post-handlers '(sp-latex-insert-spaces-inside-pair))
177226
(sp-local-pair "\\biggl\\{" "\\biggr\\}"
227+
:when '(sp-in-math-p)
228+
:unless '(sp-latex-point-after-backslash)
178229
:post-handlers '(sp-latex-insert-spaces-inside-pair))
179230
(sp-local-pair "\\Bigl\\{" "\\Bigr\\}"
231+
:when '(sp-in-math-p)
232+
:unless '(sp-latex-point-after-backslash)
180233
:post-handlers '(sp-latex-insert-spaces-inside-pair))
181234
(sp-local-pair "\\Biggl\\{" "\\Biggr\\}"
235+
:when '(sp-in-math-p)
236+
:unless '(sp-latex-point-after-backslash)
182237
:post-handlers '(sp-latex-insert-spaces-inside-pair))
183238
(sp-local-pair "\\lfloor" "\\rfloor"
239+
:when '(sp-in-math-p)
240+
:unless '(sp-latex-point-after-backslash)
184241
:post-handlers '(sp-latex-insert-spaces-inside-pair))
185242
(sp-local-pair "\\lceil" "\\rceil"
243+
:when '(sp-in-math-p)
244+
:unless '(sp-latex-point-after-backslash)
186245
:post-handlers '(sp-latex-insert-spaces-inside-pair))
187246
(sp-local-pair "\\langle" "\\rangle"
247+
:when '(sp-in-math-p)
248+
:unless '(sp-latex-point-after-backslash)
188249
:post-handlers '(sp-latex-insert-spaces-inside-pair))
189250

190251
;; some common wrappings
191-
(sp-local-tag "\"" "``" "''" :actions '(wrap))
192252
(sp-local-tag "\\b" "\\begin{_}" "\\end{_}")
193253
(sp-local-tag "bi" "\\begin{itemize}" "\\end{itemize}")
194254
(sp-local-tag "be" "\\begin{enumerate}" "\\end{enumerate}"))

0 commit comments

Comments
 (0)