0

The proper way to deal with region is using (interactive "r"). But in my case, (interactive "r") always complaints 'The mark is not set now, so there is no region' after fresh Emacs startup. This problem occurred if we never invoke mark region before. After invoking mark region, problem solved. Am I missing something ?

take this mwe:

(defun try-region (begin end)
  (interactive "r")
  (if (use-region-p)
      (message "Region Active")
    (message "Region not active")))

It always complaints 'The mark is not set now, so there is no region' if I never invoked mark region before. So I need to modify my code to use (buffer-substring-no-properties (region-beginning) (region-end) to deal with region instead.

Related: How to use (interactive “r”) function in this situation?

Thanks.

1

The use-region-p test should be within the interactive spec.

"r" is for functions that always use the region. You probably want to use something like:

(defun try-region (&optional begin end)
  (interactive (if (use-region-p) (list (region-beginning) (region-end))))
  (message (if begin "Region Active" "Region not active")))
  • I really was expecting answer from you, but I don't hope a lot cause your last seen was months ago. I thought it was a bug, turns out not. The key is " 'r' is for functions that always use the region". I keep trying using "r" for that case cause I saw a lot of project did the same.I don't know if they aware of this or not. Searching for examples, turns out I found them in emacs source. Thanks a lot. – azzamsa Feb 7 at 15: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.