Skip to content

Commit

Permalink
Generalize deletion of delimiter-preceding syntax
Browse files Browse the repository at this point in the history
Now any characters, including read macro syntax, just before an opening
delimiter will be deleted when splicing or deleting a sexp.

Fixes purcell#272
  • Loading branch information
noctuid authored and abo-abo committed May 13, 2016
1 parent d9b058b commit 573900e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
30 changes: 29 additions & 1 deletion lispy-test.el
Expand Up @@ -601,6 +601,13 @@ Insert KEY if there's no command."
(should (string= (lispy-with "((a) |\"foo\" (c))"
(lispy-delete -1))
"(|\"foo\" (c))"))
;; test that quotes also get deleted
(should (string= (lispy-with "'|()" "\C-d")
"|"))
(should (string= (lispy-with "(,@|())" "\C-d")
"(|)"))
(should (string= (lispy-with "#2A|((a b) (0 1))" "\C-d")
"|"))
(let ((lispy-safe-delete t))
;; region is already safe
(should (string= (lispy-with "((a) ~(b (c (d)))|)" "\C-d")
Expand Down Expand Up @@ -1092,7 +1099,14 @@ Insert KEY if there's no command."
(should (string= (lispy-with "(asdf)\n(progn\n |(foo)\n (bar))\n(asdf)" "//")
"(asdf)\n|(progn\n foo\n bar)\n(asdf)"))
(should (string= (lispy-with "(asdf)\n(progn\n (foo)\n (bar)|)\n(asdf)" "//")
"(asdf)\n(progn\n foo\n bar)|\n(asdf)")))
"(asdf)\n(progn\n foo\n bar)|\n(asdf)"))
;; test that quotes also get deleted
(should (string= (lispy-with "'|(a)" "/")
"|a"))
(should (string= (lispy-with "(,@|(a))" "/")
"|(a)"))
(should (string= (lispy-with "#2A|((a b) (0 1))" "/")
"|(a b) (0 1)")))

(ert-deftest lispy-barf-to-point ()
(should (string= (lispy-with "((a) (b)| (c))" (lispy-barf-to-point nil))
Expand Down Expand Up @@ -2409,6 +2423,13 @@ Insert KEY if there's no command."
;; space cleanup
(should (string= (lispy-with "(foo (| (bar) baz))" (kbd "DEL"))
"(foo |(bar) baz)"))
;; test that quotes also get deleted
(should (string= (lispy-with "'(|)" (kbd "DEL"))
"|"))
(should (string= (lispy-with "(,@(|))" (kbd "DEL"))
"(|)"))
(should (string= (lispy-with "#2A(|(a b) (0 1))" (kbd "DEL"))
"|(a b) (0 1)"))
(lispy-set-key-theme '(special lispy c-digits oleh)))

(ert-deftest lispy-delete-or-splice-or-slurp ()
Expand Down Expand Up @@ -2441,6 +2462,13 @@ Insert KEY if there's no command."
"\"a b c\"|"))
(should (string= (lispy-with "|\"a b c\"" (kbd "C-d"))
"|"))
;; test that quotes also get deleted
(should (string= (lispy-with "'|()" (kbd "C-d"))
"|"))
(should (string= (lispy-with "(,@|())" (kbd "C-d"))
"(|)"))
(should (string= (lispy-with "#2A|((a b) (0 1))" (kbd "C-d"))
"|(a b) (0 1)"))
(lispy-set-key-theme '(special lispy c-digits oleh)))

;;* Paredit compatibility tests
Expand Down
31 changes: 13 additions & 18 deletions lispy.el
Expand Up @@ -1144,7 +1144,7 @@ If position isn't special, move to previous or error."
(lispy-left 1))

((lispy-left-p)
(lispy--delete-quote-garbage)
(lispy--delete-leading-garbage)
(lispy-dotimes arg
(lispy--delete)))

Expand All @@ -1162,20 +1162,15 @@ If position isn't special, move to previous or error."
(t
(delete-char arg)))))

(defvar lispy-quote-regexp-alist
'((emacs-lisp-mode . "`',@")
(t . "`',@"))
"An alist of `major-mode' to a string.
The regexp describes the possible quoting characters in front of
the list. When the list is deleted, these quotes, that are
assumed to belong to the list are also deleted.")

(defun lispy--delete-quote-garbage ()
"Delete any combination of `',@ preceeding point."
(let ((str (or (cdr (assoc major-mode lispy-quote-regexp-alist))
(cdr (assoc t lispy-quote-regexp-alist))))
(pt (point)))
(skip-chars-backward str)
(defun lispy--delete-leading-garbage ()
"Delete any syntax before an opening delimiter such as '.
Delete backwards to the closest whitespace char or opening delimiter or to the
beginning of the line."
(let ((pt (point)))
(re-search-backward (concat "[[:space:]]" "\\|"
lispy-left "\\|"
"^"))
(goto-char (match-end 0))
(delete-region (point) pt)))

(defun lispy--delete-whitespace-backward ()
Expand Down Expand Up @@ -2505,7 +2500,7 @@ When lispy-left, will slurp ARG sexps forwards.
(save-excursion
(goto-char (cdr bnd))
(delete-char -1))
(lispy--delete-quote-garbage)
(lispy--delete-leading-garbage)
(delete-char 1)
(lispy-forward 1)
(lispy-backward 1))
Expand Down Expand Up @@ -7859,7 +7854,7 @@ quote of a string, move backward."
(save-excursion
(lispy-different)
(delete-char -1))
(lispy--delete-quote-garbage)
(lispy--delete-leading-garbage)
(delete-char 1))
((looking-back lispy-right (1- (point)))
(let ((tick (buffer-chars-modified-tick)))
Expand Down Expand Up @@ -7892,7 +7887,7 @@ quote of a string, move forward."
(save-excursion
(lispy-different)
(delete-char -1))
(lispy--delete-quote-garbage)
(lispy--delete-leading-garbage)
(delete-char 1))
((looking-at lispy-right)
(let ((tick (buffer-chars-modified-tick)))
Expand Down

0 comments on commit 573900e

Please sign in to comment.