ModeLinePosition

Mode-Line Position

2021-11-06: The library this page describes has been superseded by library ModeLineRegion (modeline-region.el), which is more correct, more featureful, and cleaner.

(For now, this page’s description is still useful, including the screenshots. The info here is largely applicable also to ModeLineRegion.)


This page describes library modeline-posn.el, which provides extensions to the standard Emacs variable ‘mode-line-position’ for Emacs 22 and later. That variable governs the behavior of minor modes ‘line-number-mode’, ‘column-number-mode’, and ‘size-indication-mode’, which show/hide the current line number, column number, and buffer size, respectively, in the mode line.

The following extensions are provided by library modeline-posn.el:

1. Whenever the current column is greater than ‘modelinepos-column-limit’, the column-number indication in the mode-line is highlighted in face ‘font-lock-function-name-face’. This lets you know when you’ve gone past a certain number of characters in any line. You can customize user option ‘modelinepos-column-limit’, and you can bind it to different values in different modes.

2. In TransientMarkMode, when the region is active, the size indicated is that of the region, not the buffer. This portion of the mode-line is then highlighted with face ‘region’. (Option ‘modelinepos-empty-region-flag’ determines whether to also indicate an empty active region.)

3. The region highlighting in the mode-line is changed to a different face when you invoke a command that behaves specially for an active region. Currently, this applies to replacement commands such as ‘query-replace’. If you use library Isearch+ then it also applies to Isearch and to replacement commands invoked from Isearch.

For #2: When the region is active, the mode line displays some information that you can customize — see options ‘modelinepos-style’, ‘modelinepos-rectangle-style’ (Emacs 26+), and ‘modelinepos-region-style’.

Customization choices for ‘modelinepos-style’ include: (a) the number of chars, (b) the number of bytes, (c) the number of rows or lines and number of columns, (d) the number of rows or lines, columns, words, and characters, and (e) anything else you might want. Choice (c) is the default.

For (c) and (d), if a rectangle is selected then the numbers are limited to its boundaries. For (d), only words entirely inside the rectangle are counted.

For (e), you provide a ‘format’ expression as separate components: the format string and the sexp arguments to be evaluated and plugged into the string. The number of sexp args depends on the format string that you use: one for each `%’ construct.

Choice (e) is provided so that you can choose alternative formatting styles. For example, instead of ` 256 ch, 13 l’, you could show ` (256 chars, 13 lines)’. But (e) can really show information at all. It need not have anything to do with the region, but it is nevertheless shown when the region is active.

Option ‘modelinepos-empty-region-flag’ determines whether to show the active-region indication when the active region is empty. By default it is ‘t’, meaning indicate an empty active region.

For #3: Certain standard Emacs commands restrict their scope to the region when it is active. Some of these are handled here by highlighting the region indication in the mode line specially (using face ‘modelinepos-region-acting-on’ instead of face ‘modelinepos-region’). The region-restricted commands defined in standard library `replace.el’ are handled this way.

If you also use library Isearch+ (isearch+.el) then (for Emacs 24.3+) Isearch commands too can optionally be restricted to the active region, and they too are handled by the special mode-line highlighting. This includes `M-%’ and `C-M-%’ during Isearch, which invoke query-replacement. Not only is the Isearch region indicated in the mode line, but the query-replacement command is invoked from Isearch with that region active, so it too is limited to that scope.

Here are some screenshots to show the effect.

‘modelinepos-column-limit’ = 70, past and not past the limit

mode-line-col-limit-past

mode-line-col-limit-not-past

Region highlighting and limits

mode-line-region

Region highlighting and limits during Isearch restricted to region

mode-line-region-restricted-cmd

Discussion

 

 

This is really handy. Thanks! One request for enhancement: add a customization variable to allow us to use a face other than ‘font-lock-function-name-face’. My modeline is blue and my ‘font-lock-function-name-face’ is also blue, so that particular face isn’t a good choice for me. --AmitPatel

Done; please try the latest. Thx for the suggestion. – DrewAdams

Thanks! – AmitPatel


Would it be possible to optionally display the absolute value of point even while not in TransientMarkMode? – priyadarshan

Sorry, but I think that if you want something like that it should be as an extension of ‘line-number-mode’ and/or ‘column-number-mode’. Showing point as an option makes sense regardless of whether the region is active.

Of course, as soon as point is shown someone will also want to see mark

If you want to do this kind of thing, just take a look at the code in modeline-posn.el, in particular the redefinition of ‘mode-line-position’. The parts to tweak are those for ‘line-number-mode’ and ‘column-number-mode’ (one or both, depending on what you want). HTH – DrewAdams

Thank you for the pointers! I shall try and let you know. – priyadarshan


CategoryDisplay CategoryEditing CategoryRegion CategoryWindows CategoryModeLine CategoryModeLineConfiguration