7

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
sandric
  • 1,221
  • 9
  • 19

1 Answers1

14

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)))
fengqi
  • 103
  • 4
sandric
  • 1,221
  • 9
  • 19
  • 2
    @Jules I can mark my own answer as accepted only in 2 days unfortunately. – sandric Nov 02 '16 at 18:45
  • Oh well the more you know! – Jules Nov 02 '16 at 20:26
  • 1
    Please consider providing the updated code, and linking to the Elisp manual, node [The Mark](http://www.gnu.org/software/emacs/manual/html_node/elisp/The-Mark.html). – Drew Nov 02 '16 at 21:49