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.
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>)))
...)
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With