expez / evil-smartparens

Evil integration for Smartparens
GNU General Public License v3.0
134 stars 17 forks source link

That would leave the buffer unbalanced #52

Closed c02y closed 5 years ago

c02y commented 5 years ago

Unable to delete a single parentheses when it it not balanced

For instance, I want to turn:

(setq a t)
(setq b t)

into

(setq a t
      b t)

This is not possible since when I backspace or C-d in emacs state or x in evil normal state, or mark the extra text to delete, it will fail and display

That would leave the buffer unbalanced

Or sometime this kind of unbalanced code would exist after I copy/paste:

(
 (
 ))  <-- a
)  <-- b

I need to delete the unbalanced parentheses, a or b, but deleting any of them would not be possible since it would fail and display

That would leave the buffer unbalanced

Currently I use M-x delete-char to manually delete it.

So how can I delete the single unbalanced parentheses using normal key like x or C-d or backspace?

expez commented 5 years ago

https://github.com/expez/evil-smartparens#escape-hatches

expez commented 5 years ago

FWIW you first example I'd solve by calling sp-forward-slurp-sexp to slurp the second sexp into the first one and then calling sp-splice-sexp-killing-backward-or-around to get rid of the setq and the surrounding parens.

You could also call sp-join-sexp and then delete the extra setq in the joined expression.

I would never solve this by manually deleting parens.

c02y commented 5 years ago

Thank you, https://github.com/expez/evil-smartparens#escape-hatches works great, I just didn't get the meaning of "Escape hatches", so I didn't take that part seriously when I was reading the readme file.

As for your second reply, sp-join-sexp +sp-splice-sexp-killing-backward-or-around or sp-join-sexp+delete the extra setq don't work as expected, maybe I just didn't use correctly.

Anyway, the setq code snippet is just a simple example, I couldn't just think up a specfic way for any specfic situation, the simple way of just marking and deleting the extra text is my best choice, and I never use sp-*sexp* functions directly.

expez commented 5 years ago

Glad to hear it worked out :)

[..] the simple way of just marking and deleting the extra text is my best choice, and I never use sp-*sexp* functions directly.

The entire point of evil-smartparens and smartparens itself is to never do this, because it's usually less efficient and can get you into trouble with an unbalanced buffer. Once that happens it can take some time to recover IME, and flow is broken. One lifesaver in that situation is to use the built-in check-parens function.

As a friendly tip I would like to encourage you to dig into a few of the functions in smartparens. You can add say one a day or one a week and you'll quickly cover the most important ones.

The ones I use the most are (pasted form my config):

(defun my-smartparens-mode-hook ()
  (evil-smartparens-mode 1)
  (fill-keymap evil-normal-state-local-map
               (kbd "C-t") 'sp-transpose-sexp
               "[" (lambda (&optional arg) (interactive "P") (sp-wrap-with-pair "["))
               "{" (lambda (&optional arg) (interactive "P") (sp-wrap-with-pair "{"))
               "(" (lambda (&optional arg) (interactive "P") (sp-wrap-with-pair "("))
               "H" 'sp-backward-up-sexp
               "L" 'sp-up-sexp
               "C-9" 'sp-backward-barf-sexp
               "C-0" 'sp-forward-barf-sexp
               "M-9" 'sp-backward-slurp-sexp
               "M-0" 'sp-forward-slurp-sexp))

(fill-keymap sp-keymap
             "M-s" 'sp-splice-sexp
             "M-S" 'sp-split-sexp
             "M-j" 'sp-join-sexp

             "M-o" 'sp-down-sexp
             "M-u" 'sp-backward-down-sexp

             "M-l" 'sp-forward-sexp
             "M-h" 'sp-backward-sexp
             "M-k" 'sp-splice-sexp-killing-backward-or-around
             "M-K" 'sp-splice-sexp-killing-forward
             "C-k" 'sp-kill-sexp
             "M-c" 'sp-convolute-sexp)
c02y commented 5 years ago

I never use sp-*-sexp-* functions because they look complicated so I never spend time learning them .

You are right, I really should spend time doing that.

Thanks for your advice.