ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

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

Xah Lee, , …,

This page is a tutorial on emacs's concept of {region, active region, transient-mark-mode}. These are very important concepts with subtle differences. If you want to write interactive emacs commands, you should understand them well.

Region & Mark

Region = The last Mark position to the current cursor position.

Call set-mark-commandCtrl+Space】 to set a mark.

Once a user sets a mark in a buffer, 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, region-end}.

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

Active Region

Because a region exists once a user sets 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. (in elisp, nil and () are false, everything else is true. true is represented by t)

transient-mark-mode

Emacs has a minor mode called transient-mark-mode. When on, it will highlight the Active Region.

transient-mark-mode is introduced in emacs 19 (1994). transient-mark-mode is on by default since Emacs 23.1.

When Should a Region Be Active?

Typically, when set-mark-command is called, the region becomes active (highlighted). When a command is called, it typically 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, the region returns to 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:

(if (and transient-mark-mode mark-active)
    (progn ; there is a text selection
      …
      )
  (progn ; user did not have text selection feature on
    …
    )
  )

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 〔☛ New Features in 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 commands 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