Skip to content

Commit 41aa6c7

Browse files
committed
[Fix #887] Do not extend region-to-kill before/after symbol.
Sometimes forward-word or backward-word skips further than the end of the symbol token that smartparens calculated. In this case we should not kill any further than the beg/end of this symbol as we can be sure this won't break any structure.
1 parent ec152cf commit 41aa6c7

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

smartparens.el

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8888,15 +8888,19 @@ See `sp-forward-symbol' for what constitutes a symbol."
88888888
(p (point)))
88898889
(when s
88908890
(sp-get s
8891-
(let ((delims (buffer-substring :beg-prf p)))
8891+
(cl-letf ((delims (buffer-substring :beg-prf p))
8892+
((symbol-function 'sp--get-kill-end)
8893+
(lambda (s)
8894+
(min (save-excursion (sp--forward-word) (point))
8895+
(sp-get s :end-suf)))))
88928896
(if (string-match-p "\\`\\(\\s.\\|\\s-\\)*\\'" delims)
88938897
(if word
8894-
(kill-region p (save-excursion (sp--forward-word) (point)))
8898+
(kill-region p (sp--get-kill-end s))
88958899
(kill-region p :end))
88968900
(let ((kill-from (if (> p :beg-prf) :beg :beg-prf)))
88978901
(goto-char kill-from)
88988902
(if word
8899-
(kill-region kill-from (save-excursion (sp--forward-word) (point)))
8903+
(kill-region kill-from (sp--get-kill-end s))
89008904
(kill-region kill-from :end)))))))))
89018905
(sp--cleanup-after-kill)
89028906
(setq arg (1- arg)))
@@ -8959,19 +8963,23 @@ See `sp-backward-symbol' for what constitutes a symbol."
89598963
(p (point)))
89608964
(when s
89618965
(sp-get s
8962-
(let ((delims (buffer-substring :end p)))
8966+
(cl-letf ((delims (buffer-substring :end p))
8967+
((symbol-function 'sp--get-kill-beg)
8968+
(lambda (s)
8969+
(max (save-excursion (sp--backward-word) (point))
8970+
(sp-get s :beg-prf)))))
89638971
(if (string-match-p "\\`\\(\\s.\\|\\s-\\)*\\'" delims)
89648972
;; Note: the arguments to kill-region are
89658973
;; "reversed" (end before beg) so that the
89668974
;; successive kills are prepended in the kill
89678975
;; ring. See the implementation of
89688976
;; `kill-region' for more info
89698977
(if word
8970-
(kill-region p (save-excursion (sp--backward-word) (point)))
8978+
(kill-region p (sp--get-kill-beg s))
89718979
(kill-region p :beg-prf))
89728980
(goto-char :end)
89738981
(if word
8974-
(kill-region :end (save-excursion (sp--backward-word) (point)))
8982+
(kill-region :end (sp--get-kill-beg s))
89758983
(kill-region :end :beg-prf))))))))
89768984
(sp--cleanup-after-kill)
89778985
(setq arg (1- arg)))

test/smartparens-commands-test.el

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
((not (boundp 'mode)) (emacs-lisp-mode))
66
((eq mode 'elisp) (emacs-lisp-mode))
77
((eq mode 'racket) (racket-mode))
8+
((eq mode 'clojure) (clojure-mode))
89
((eq mode 'c) (c-mode))
910
((eq mode 'js) (js2-mode))
1011
((eq mode 'python) (shut-up (python-mode))))
@@ -474,13 +475,34 @@ be."
474475
(((current-prefix-arg '(4)))
475476
("(foo bar (baz)| (quux) (blob bluq))" "(foo bar (baz| quux blob bluq))"))))
476477

478+
477479
(sp-test-command sp-kill-word
478480
((nil
479481
("| 'foo-bar-baz" "|-bar-baz")
480482
("|'foo-bar-baz" "|-bar-baz")
481483
("'|foo-bar-baz" "'|-bar-baz")
482484
("'f|oo-bar-baz" "'f|-bar-baz")
483-
("'foo-|bar-baz" "'foo-|-baz"))))
485+
("'foo-|bar-baz" "'foo-|-baz")
486+
("|(foo-bar-baz)" "(|-bar-baz)")
487+
("(foo-bar-baz|) foo" "(foo-bar-baz) |")
488+
)
489+
(((mode 'clojure))
490+
("|(-> (first (secord)))" "(| (first (secord)))" "( (| (secord)))" "( ( (|)))")
491+
("(|-> (first (secord)))" "(| (first (secord)))"))))
492+
493+
(sp-test-command sp-backward-kill-word
494+
((nil
495+
("'foo-bar-baz |" "'foo-bar-|" "'foo-|" "'|")
496+
("'foo-|bar-baz" "'|bar-baz")
497+
("'foo|-bar-baz" "'|-bar-baz")
498+
("'f|oo-bar-baz" "'|oo-bar-baz")
499+
("'foo-b|ar-baz" "'foo-|ar-baz")
500+
("(foo-bar-baz)|" "(foo-bar-|)")
501+
("foo (|foo-bar-baz)" "|(foo-bar-baz)")
502+
)
503+
(((mode 'clojure))
504+
("(-> (first (secord)))|" "(-> (first (|)))" "(-> (| ()))" "(| ( ()))")
505+
("(-> |(first (secord)))" "(|(first (secord)))"))))
484506

485507
(sp-test-command sp-kill-symbol
486508
((nil

0 commit comments

Comments
 (0)