(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.