BetterThanTomorrow / calva

Clojure & ClojureScript Interactive Programming for VS Code
https://marketplace.visualstudio.com/items?itemName=betterthantomorrow.calva
Other
1.68k stars 217 forks source link

Add paredit function `paredit-backward-kill-word` #2003

Open aeberts opened 1 year ago

aeberts commented 1 year ago

As an Emacs user, I am accustomed to using the "paredit-backwards-kill-word" function to delete words backwards while preserving parentheses. This function doesn't appear to be available in Calva (unless I'm missing something?).

My VSCode-fu is not up to the task of porting the Emacs code to VSCode but I've included the Emacs implementation below for reference.

Many thanks!

http://paredit.org/releases/26/paredit.el

(defun paredit-backward-kill-word ()
  "Kill a word backward, skipping over any intervening delimiters."
  (interactive)
  (if (not (or (bobp)
               (eq (char-syntax (char-before)) ?w)))
      (let ((end (point)))
        (backward-word 1)
        (forward-word 1)
        (goto-char (min end (point)))
        (let* ((parse-state (paredit-current-parse-state))
               (state
                (paredit-kill-word-state parse-state 'char-before)))
          (while (and (< (point) end)
                      (progn
                        (setq parse-state
                              (parse-partial-sexp (point) (1+ (point))
                                                  nil nil parse-state))
                        (or (eq state
                                (paredit-kill-word-state parse-state
                                                         'char-before))
                            (progn (backward-char 1) nil)))))
          (if (and (eq state 'comment)
                   (eq ?\# (char-after (point)))
                   (eq ?\| (char-before (point))))
              (backward-char 1)))))
  (backward-kill-word 1))
PEZ commented 1 year ago

Hi! Thanks for this issue. I don't quite understand what's supposed to happen. Can you describe it with a few before/after examples, something like:

before 1:

(a b c (d e)| f)

after 1:

what-it-should-look-like-after-1

...

The vertical bar | denotes the cursor.

aeberts commented 1 year ago

Hi @PEZ many thanks for your great work on Calva!

re: paredit-backward-kill-word here's the before and after:

Before:

(paredit-kill-word kills by word but (preserves) parentheses|)

After executing paredit-backward-kill-word 1 time:

(paredit-kill-word kills by word but (preserves) |)

After executing paredit-backward-kill-word 2 times:

(paredit-kill-word kills by word but (|))

After executing paredit-backward-kill-word 3 times:

(paredit-kill-word kills by word |())

Here's an animated gif which shows both deleting backward by character and then by word:

paredit-delete-backward

Source: http://danmidwood.com/content/2014/11/21/animated-paredit.html

Hope that is clearer!

Note: Edited to correct the examples.

PEZ commented 1 year ago

Thanks. Looks funny with the word ”parentheses” reappearing after the second use of the command. Is that a typo?

aeberts commented 1 year ago

Yup, good catch - that's a copy and paste error. I'll correct it.