3

As I only use kill-ring-save after I used set-mark-command, I wanted to try using its binding also for another command. However I'm not able to get this to work with an additional if. I don't know how to pass the arguments to avy-goto-char-2 with the interactive stuff.

How can I determine if the mark is active, and use that information to pick a function to call?

1 Answer 1

3

The function region-active-p will tell you if the region is active. You can use this to determine which function to call. Both of the functions you want behave differently when called interactively vs when called as lisp functions, so you need to tell Emacs to treat them as interactive function calls with the (call-interactively ...) form. So:

(defun my-double ()
  (interactive)
  (if (region-active-p)
      (call-interactively 'kill-ring-save)
    (call-interactively 'avy-goto-char-2)))
3
  • 1
    (defun my-double (beg end) (interactive "r") (if (use-region-p) (kill-ring-save beg end 'REGION) (call-interactively #'avy-goto-char-2)))
    – Drew
    Commented Jul 4, 2016 at 22:37
  • @Drew that looks better. Any benefit, beyond style, to not using call-interactively?
    – Tyler
    Commented Jul 4, 2016 at 23:11
  • Not really. I just thought I'd mention it as an alternative.
    – Drew
    Commented Jul 5, 2016 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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