(defun some-function (beginning end)
(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"
The use of
(interactive "r") means that the parameters
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
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
(defun use-region-p ()
(> (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
(defun region-active-p ()
We see that it returns true (non-
nil) when all of these are true:
transient-mark-mode is turned on.
- The mark is 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.