HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

Emacs: What's Region, Active Region, transient-mark-mode?

Advertise Here For Profit

Xah Lee, 2008-06, …, 2010-03-25

This page is a tutorial on emacs's concept of region, active region, transient-mark-mode, for those who want to write emacs lisp commands.

Region

The last Mark position to the current cursor position is called a Region.

Once a user sets a mark in a buffer 【Ctrl+Space】 or 【Alt+x set-mark-command】, a region exists. So, almost always, there exists a region in a buffer.

By convention, commands ending in the word “-region” acts on the region. For example: kill-region, comment-region, fill-region, indent-region.

You can get the positions of region by the functions “region-beginning” and “region-end”.

This system worked well until modern concept of highlighted text selection became popular. Emacs introduced the region highlighting feature around late 1990s, and as a consequence, we have the minor mode transient-mark-mode, and the new concept of Active Region.

transient-mark-mode, Active Region, Highlighting of Region

transient-mark-mode

Emacs has a minor mode called transient-mark-mode, introduced since emacs 19.x or earlier. It is on by default since Emacs 23. When on, it will highlight the Active Region.

Active Region

Because a region exists once a user set a mark, and always having a section of text highlighted to the cursor position is annoying, so there's a new concept of Active Region.

The active/inactive status of a region is controlled by the variable mark-active. When this variable has value of true, the region is active, if false then inactive.

When Should a Region Be Active?

Typically, when set-mark-command is called, the region becomes active (highlighted). When a command is called, typically the command will set the region status to inactive. This means, when you set mark using the keyboard or the mouse, text selection become highlighted, then after you called some command that do something, the region returns inactive again (and the highlighting goes away).

What's Text Selection?

Emacs's concept of “active region” is practically the same as the modern term “Text Selection”.

Typically, when you want your command to act on a text selection, do this check:

(and transient-mark-mode mark-active)

Or, use use a higher level function “region-active-p”, which does exactly the above. (lookup its inline doc)

Emacs 23 Changes

Starting with Emacs 23, transient-mark-mode is on by default, and some command behavior changed. If there is a text selection, the command acts on it, else it acts on the current word, line, paragraph, buffer (or whatever is its default input).

This change is good, because users don't need to think about whether he should choose the region or non-region version of the command. The command simply act on a text selection if there is one.

Commands with this new behavior includes: fill-paragraph, ispell-word, indent-for-tab-command, comment-dwim. The number of command that are sensitive to existence of text selection will probably increase.

Note that commands ending in “-region” still should act on region as before, regardless of the region activeness status.

(info "(elisp) The Mark")

blog comments powered by Disqus