Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use (interactive "r") function in this situation?

Tags:

emacs

elisp

I defined a function and wanted to ues the region as optional parameters.

(defun my-grep-select(&optional beg end)
  (interactive "r")
    (if mark-active
       (....)
      (....))

I wanted to grep the select chars in the buffer if the mark is active, or grep the word under the cursor in the buffer if the mark is not active.

But In the situation: I opened the file and haven't select anything, Then run the command my-grep-select, emacs complains:

The mark is not set now, so there is no region

How can I eliminate this complains? Thanks.

like image 882
yang wen Avatar asked Nov 08 '12 01:11

yang wen


2 Answers

The right way to do it might be:

(defun my-grep-select(&optional beg end)
  (interactive
   (if (use-region-p) (list (region-beginning) (region-end))
     (list <wordbegin> <wordend>)))
  ...)
like image 170
Stefan Avatar answered Sep 23 '22 15:09

Stefan


You don't need to use (interactive "r"). Instead, you could just check if region is active using (region-active-p) or similar then use (region-beginning) and (region-end) else do whatever else.

Perhaps there is choice to be made when region is active and a different set of parameters are passed...

like image 40
Miserable Variable Avatar answered Sep 23 '22 15:09

Miserable Variable