1

I'm using smartparens-strict-mode and trying to get it so that C-w kills a region if selected and the whole line that point is on if not (like this but working with smartparens strict mode).

This is what I have so far:

(defun sp-kill-line-or-region (orig-fun &rest args)
    "Kill whole line if region not selected."
    (if (region-active-p)
        (apply orig-fun args)
      (kill-whole-line)))
  (advice-add 'sp-kill-region :around 'sp-kill-line-or-region)

But it doesn't work. If I just do C-w it gives me the error command-execute: The mark is not set now, so there is no region. However, if I mark something, then C-w, then I can use it like I want (i.e. I can then not mark anything and it will kill the whole line).

I can't figure this one out for whatever reason. Help? Thanks!

2

Advices suck. Much easier is to just define a new function and bind it to C-w. This is what I use:

(defun my-kill-region-or-word (&optional arg)
  "Kill active region or one word backward."
  (interactive "p")
  (if (use-region-p)
      (sp-kill-region (region-beginning) (region-end))
    (if smartparens-strict-mode
        (sp-backward-kill-word arg)
      (backward-kill-word arg))))

Replace the else branch with sp-kill-whole-line, so this code should work

(defun my-kill-region-or-line (&optional arg)
  "Kill active region or current line."
  (interactive "p")
  (if (use-region-p)
      (sp-kill-region (region-beginning) (region-end)) ;; strict-mode version of kill-region
    (sp-kill-whole-line))) ;; strict-mode version of kill-whole-line

You can of course make it strict-mode aware by wrapping the commands with if as I do in my function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.