17

Say, I have some code like this:

(defun some-function ()
  (interactive)
  ;; do something
  )

Now I want some-function to operate only on the selected region in the buffer ? How can I do that ? Also, can there be two separate code path for doing something if I have selected some region and doing something else If I haven't selected.

22
(defun some-function (beginning end)
  (interactive "r")
  (if (use-region-p)
      (message "The region is active, and is from %d to %d" beginning end)
    (message "The region is still there (from % d to %d), but it is inactive" 
             beginning end)))

The use of (interactive "r") means that the parameters beginning and end will automatically receive the values of the region beginning and the region end, respectively, when the function is invoked. (You can also get those values anytime using the functions region-beginning and region-end, respectively.)

The region is always present (if there is a mark in the current buffer), but you might not think it is. The region is active when it is highlighted (text is selected). You can test whether the region is active using predicate region-active-p. But the better test is usually use-region-p, because (by default) it returns true (non-nil) only when the region is both active and nonempty (point and mark are different).

Originally, there was no highlighting of the region. And for a long time, even though highlighting was available, it was not the default behavior. For highlighting to occur, you must have transient-mark-mode turned on (or turned on at least temporarily). By default, transient-mark-mode is turned on in recent Emacs versions.

It's helpful to look at the definition of predicate use-region-p:

(defun use-region-p ()
  (and (region-active-p)
       (or use-empty-active-region
           (> (region-end) (region-beginning)))))

use-empty-active-region is a user option, whose default value is nil (true), meaning that by default use-region-p will not return true if the region is empty. In that case, for it to return true, the end of the region must be greater than the beginning (the behavior I described above).

And if we look at the definition of region-active-p:

(defun region-active-p ()
  (and transient-mark-mode
       mark-active
       (mark)))

We see that it returns true (non-nil) when all of these are true:

  • transient-mark-mode is turned on.
  • The mark is active (mark-active).
  • There is a mark in the current buffer.

The mark being active is really what the region being active is all about. When it is active, assuming that transient-mark-mode is on and there is a mark in the current buffer, the region is highlighted.

  • AFAICS if mark is set to point, i.e. region has not extend, mark-active is nil. So mark-active and transient-mark-mode are the only things adding value in these fairly redundant functions. – Andreas Röhler May 12 '15 at 12:32
  • 1
    @AndreasRöhler: Nope. C-SPC M-: mark-active => t. You can definitely have an empty region that is nevertheless active. – Drew May 12 '15 at 13:41
  • Okay, thanks. Still think mark-active and region-active-p --defined as extend greater zero-- should be all needed. – Andreas Röhler May 12 '15 at 15:26

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.