aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorStefan Monnier2022-07-03 11:12:45 -0400
committerStefan Monnier2022-07-03 11:12:45 -0400
commitbfc9e7669688b3439cfdd2d5972065b95eae812f (patch)
treeaaaa50b0f7c9b3166aa376e1e42b1a3abbcdbd0a /lisp
parente41ba8ab89a125c91dee672845679f2dec19853a (diff)
downloademacs-bfc9e7669688b3439cfdd2d5972065b95eae812f.tar.gz
emacs-bfc9e7669688b3439cfdd2d5972065b95eae812f.zip
lisp/elec-pair.el: Simplify last change
* lisp/elec-pair.el (electric-pair--with-syntax): Rename from `electric-pair--with-text-syntax`. Make `start` mandatory. Run `body` in the normal syntax if `start` is nil. (electric-pair--with-syntax-1): New function, extracted from `electric-pair--with-text-syntax`. (electric-pair-syntax-info, electric-pair--balance-info): Adjust calls accordingly.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/elec-pair.el84
1 files changed, 40 insertions, 44 deletions
diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el
index 247e9b93ec2..4b901071cd9 100644
--- a/lisp/elec-pair.el
+++ b/lisp/elec-pair.el
@@ -188,24 +188,29 @@ be considered.")
188 ;; I also find it often preferable not to pair next to a word. 188 ;; I also find it often preferable not to pair next to a word.
189 (eq (char-syntax (following-char)) ?w))) 189 (eq (char-syntax (following-char)) ?w)))
190 190
191(cl-defmacro electric-pair--with-text-syntax ((&optional start) &rest body) 191(defmacro electric-pair--with-syntax (string-or-comment &rest body)
192 "Run BODY with `electric-pair-text-syntax-table' active. 192 "Run BODY with appropriate syntax table active.
193This ensures that all syntax related values are set properly and the 193STRING-OR-COMMENT is the start position of the string/comment
194`syntax-ppss' cache is cleared before and after. 194in which we are, if applicable.
195In particular, this must be used when BODY contains code which may 195Uses the text-mode syntax table if within a string or a comment."
196update the `syntax-ppss' cache. This includes calling 196 (declare (debug t) (indent 1))
197`parse-partial-sexp' and any sexp-based movement functions when 197 `(electric-pair--with-syntax-1 ,string-or-comment (lambda () ,@body)))
198`parse-sexp-lookup-properties' is non-nil. The cache is flushed from 198
199position START, defaulting to point." 199(defun electric-pair--with-syntax-1 (string-or-comment body-fun)
200 (declare (debug ((&optional form) body)) (indent 1)) 200 (if (not string-or-comment)
201 (let ((start-var (make-symbol "start"))) 201 (funcall body-fun)
202 `(let ((syntax-propertize-function nil) 202 ;; Here we assume that the `syntax-ppss' cache has already been filled
203 (,start-var ,(or start '(point)))) 203 ;; past `string-or-comment' with data corresponding to the "normal" syntax
204 (syntax-ppss-flush-cache ,start-var) 204 ;; (this should be the case because STRING-OR-COMMENT was returned
205 ;; in the `nth 8' of `syntax-ppss').
206 ;; Maybe we should narrow-to-region so that `syntax-ppss' uses the narrow
207 ;; cache?
208 (syntax-ppss-flush-cache string-or-comment)
209 (let ((syntax-propertize-function nil))
205 (unwind-protect 210 (unwind-protect
206 (with-syntax-table electric-pair-text-syntax-table 211 (with-syntax-table electric-pair-text-syntax-table
207 ,@body) 212 (funcall body-fun))
208 (syntax-ppss-flush-cache ,start-var))))) 213 (syntax-ppss-flush-cache string-or-comment)))))
209 214
210(defun electric-pair-syntax-info (command-event) 215(defun electric-pair-syntax-info (command-event)
211 "Calculate a list (SYNTAX PAIR UNCONDITIONAL STRING-OR-COMMENT-START). 216 "Calculate a list (SYNTAX PAIR UNCONDITIONAL STRING-OR-COMMENT-START).
@@ -222,13 +227,10 @@ inside a comment or string."
222 (string-or-comment (and post-string-or-comment 227 (string-or-comment (and post-string-or-comment
223 pre-string-or-comment)) 228 pre-string-or-comment))
224 (table-syntax-and-pair 229 (table-syntax-and-pair
225 (cl-flet ((f () 230 (electric-pair--with-syntax string-or-comment
226 (list (char-syntax command-event) 231 (list (char-syntax command-event)
227 (or (matching-paren command-event) 232 (or (matching-paren command-event)
228 command-event)))) 233 command-event))))
229 (if string-or-comment
230 (electric-pair--with-text-syntax () (f))
231 (f))))
232 (fallback (if string-or-comment 234 (fallback (if string-or-comment
233 (append electric-pair-text-pairs 235 (append electric-pair-text-pairs
234 electric-pair-pairs) 236 electric-pair-pairs)
@@ -275,7 +277,7 @@ when to fallback to `parse-partial-sexp'."
275 (skip-syntax-forward " >!") 277 (skip-syntax-forward " >!")
276 (point))))) 278 (point)))))
277 (if s-or-c-start 279 (if s-or-c-start
278 (electric-pair--with-text-syntax (s-or-c-start) 280 (electric-pair--with-syntax s-or-c-start
279 (parse-partial-sexp s-or-c-start pos)) 281 (parse-partial-sexp s-or-c-start pos))
280 ;; HACK! cc-mode apparently has some `syntax-ppss' bugs 282 ;; HACK! cc-mode apparently has some `syntax-ppss' bugs
281 (if (memq major-mode '(c-mode c++ mode)) 283 (if (memq major-mode '(c-mode c++ mode))
@@ -293,7 +295,8 @@ when to fallback to `parse-partial-sexp'."
293(defun electric-pair--balance-info (direction string-or-comment) 295(defun electric-pair--balance-info (direction string-or-comment)
294 "Examine lists forward or backward according to DIRECTION's sign. 296 "Examine lists forward or backward according to DIRECTION's sign.
295 297
296STRING-OR-COMMENT is info suitable for running `parse-partial-sexp'. 298STRING-OR-COMMENT is the position of the start of the comment/string
299in which we are, if applicable.
297 300
298Return a cons of two descriptions (MATCHED-P . PAIR) for the 301Return a cons of two descriptions (MATCHED-P . PAIR) for the
299innermost and outermost lists that enclose point. The outermost 302innermost and outermost lists that enclose point. The outermost
@@ -325,14 +328,11 @@ If point is not enclosed by any lists, return ((t) . (t))."
325 (cond ((< direction 0) 328 (cond ((< direction 0)
326 (condition-case nil 329 (condition-case nil
327 (eq (char-after pos) 330 (eq (char-after pos)
328 (cl-flet ((f () 331 (electric-pair--with-syntax
329 (matching-paren 332 string-or-comment
330 (char-before 333 (matching-paren
331 (scan-sexps (point) 1))))) 334 (char-before
332 (if string-or-comment 335 (scan-sexps (point) 1)))))
333 (electric-pair--with-text-syntax ()
334 (f))
335 (f))))
336 (scan-error nil))) 336 (scan-error nil)))
337 (t 337 (t
338 ;; In this case, no need to use 338 ;; In this case, no need to use
@@ -346,9 +346,8 @@ If point is not enclosed by any lists, return ((t) . (t))."
346 (opener (char-after start))) 346 (opener (char-after start)))
347 (and start 347 (and start
348 (eq (char-before pos) 348 (eq (char-before pos)
349 (or (if string-or-comment 349 (or (electric-pair--with-syntax
350 (electric-pair--with-text-syntax () 350 string-or-comment
351 (matching-paren opener))
352 (matching-paren opener)) 351 (matching-paren opener))
353 opener)))))))) 352 opener))))))))
354 (actual-pair (if (> direction 0) 353 (actual-pair (if (> direction 0)
@@ -361,14 +360,11 @@ If point is not enclosed by any lists, return ((t) . (t))."
361 (save-excursion 360 (save-excursion
362 (while (not outermost) 361 (while (not outermost)
363 (condition-case err 362 (condition-case err
364 (cl-flet ((f () 363 (electric-pair--with-syntax string-or-comment
365 (scan-sexps (point) (if (> direction 0) 364 (scan-sexps (point) (if (> direction 0)
366 (point-max) 365 (point-max)
367 (- (point-max)))) 366 (- (point-max))))
368 (funcall at-top-level-or-equivalent-fn))) 367 (funcall at-top-level-or-equivalent-fn))
369 (if string-or-comment
370 (electric-pair--with-text-syntax () (f))
371 (f)))
372 (scan-error 368 (scan-error
373 (cond ((or 369 (cond ((or
374 ;; some error happened and it is not of the "ended 370 ;; some error happened and it is not of the "ended