3

I wish to remove selection programatically, what is the proper way of doing this? Specifically I wish to write a function to make swiper search in buffer with selected text, but it needs to remove selection before open swiper window. Here's the function

(defun sandric/swiper-or-region (beg end)
  "Swiper region or 'empty string' if none highlighted."
  (interactive (if (use-region-p)
                   (list (region-beginning) (region-end))
                 (list nil nil)))
  (if (and beg end)
      (progn
        (keyboard-quit)
        (swiper (buffer-substring-no-properties beg end)))
    (swiper)))

As you can see there's a (keyboard-quit) call before swiper command to open swiper buffer, but with that it simply discards swiper buffer as if ESC was called already after swiper buffer appeared. So I need to find a better way.

7

Ok, I found the answer almost immediately after posting question. The function name is deactivate-mark if anyone has the same question. Its described here. Updated code:

(defun sandric/swiper-or-region (beg end)
  "Swiper region or 'empty string' if none highlighted."
  (interactive (if (use-region-p)
                   (list (region-beginning) (region-end))
                 (list nil nil)))
  (if (and beg end)
      (progn
        (deactivate-mark)
        (swiper (buffer-substring-no-properties beg end)))
    (swiper)))
  • 2
    @Jules I can mark my own answer as accepted only in 2 days unfortunately. – sandric Nov 2 '16 at 18:45
  • Oh well the more you know! – Jules Nov 2 '16 at 20:26
  • 1
    Please consider providing the updated code, and linking to the Elisp manual, node The Mark. – Drew Nov 2 '16 at 21:49

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.