Skip to content

Commit

Permalink
[Fix #932] Update sp-local-pairs caches when removing pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuco1 committed Oct 27, 2018
1 parent 33ce3c7 commit 20d9e3b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
34 changes: 24 additions & 10 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,12 @@ See `sp--init'."
(unless sp-pair-list
(sp--init)))

(defun sp--remove-local-pair (open)
"Remove OPEN from `sp-local-pairs'."
(setq sp-local-pairs
(--remove (equal (plist-get it :open) open)
sp-local-pairs)))

(defun sp--update-sp-pair-list ()
"Update `sp-pair-list' according to current value of `sp-local-pairs'."
(setq sp-pair-list
Expand Down Expand Up @@ -880,6 +886,14 @@ or a list of such property lists."
(setf (sp-state-local-pairs sp-state) sp-local-pairs)
(setf (sp-state-pair-list sp-state) sp-pair-list))

(defmacro sp-with-buffers-using-mode (mode &rest body)
"Execute BODY in every existing buffer using `major-mode' MODE."
(declare (indent 1))
`(--each (buffer-list)
(with-current-buffer it
(when (derived-mode-p ,mode)
,@body))))

(defun sp--update-local-pairs-everywhere (&rest modes)
"Run `sp--update-local-pairs' in all buffers.

Expand Down Expand Up @@ -2397,7 +2411,9 @@ a similar lambda manually. To only bind this in specific major
modes, use this property on `sp-local-pair' instead."
(if (eq actions :rem)
(let ((global-list (assq t sp-pairs)))
(setcdr global-list (--remove (equal (plist-get it :open) open) (cdr global-list))))
(setcdr global-list (--remove (equal (plist-get it :open) open) (cdr global-list)))
(--each (buffer-list)
(with-current-buffer it (sp--remove-local-pair open))))
(let ((pair nil))
(setq pair (plist-put pair :open open))
(when close (plist-put pair :close close))
Expand Down Expand Up @@ -2508,15 +2524,13 @@ markdown when it signifies list item instead of emphasis. In
addition, there is a global per major-mode option, see
`sp-navigate-skip-match'."
(if (eq actions :rem)
(let ((remove ""))
(dolist (m (-flatten (list modes)))
(setq remove (concat remove
(sp-get-pair-definition open m :open)
(sp-get-pair-definition open m :close)))
(let ((mode-pairs (assq m sp-pairs)))
(setcdr mode-pairs
(--remove (equal (plist-get it :open) open)
(cdr mode-pairs))))))
(dolist (m (-flatten (list modes)))
(let ((mode-pairs (assq m sp-pairs)))
(setcdr mode-pairs
(--remove (equal (plist-get it :open) open)
(cdr mode-pairs))))
(sp-with-buffers-using-mode m
(sp--remove-local-pair open)))
(dolist (m (-flatten (list modes)))
(let* ((pair nil))
(setq pair (plist-put pair :open open))
Expand Down
15 changes: 15 additions & 0 deletions test/smartparens-config-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,18 @@
(sp-update-local-pairs 'foo)
(should (sp-test-config--equal-sets
sp-pair-list '(("(" . ")") ("[" . "]") ("'" . "'")))))))

;; #932
(ert-deftest sp-test-config--remove-local-pair ()
(let ((sp-pairs (-clone sp-pairs)))
(sp-test-with-temp-elisp-buffer ""
(should (--some? (equal (plist-get it :open) "[") sp-local-pairs))
(sp-local-pair 'emacs-lisp-mode "[" nil :actions :rem)
(should-not (--some? (equal (plist-get it :open) "[") sp-local-pairs)))))

(ert-deftest sp-test-config--remove-global-pair ()
(let ((sp-pairs (-clone sp-pairs)))
(sp-test-with-temp-elisp-buffer ""
(should (--some? (equal (plist-get it :open) "[") sp-local-pairs))
(sp-pair "[" nil :actions :rem)
(should-not (--some? (equal (plist-get it :open) "[") sp-local-pairs)))))

0 comments on commit 20d9e3b

Please sign in to comment.