BlankMode

This page describes package Lisp:blank-mode.el, by ViniciusJoseLatorre.

.:: v9.1 :: part of GNU Emacs ::.
It runs on Emacs 21, 22 and 23.
Note: This package was replaced by WhiteSpace.

Screenshot

The screenshots below show when BlankMode is turned on using the default setting.

BlankModeOn
BlankModeOnDark

About blank-mode

This package is a minor mode to visualize blanks (TAB, (HARD) SPACE and NEWLINE).

BlankMode uses two ways to visualize blanks: Faces and display table.

The ‘blank-style’ and ‘blank-chars’ variables are used to select which way should be used to visualize blanks.

Note that when BlankMode is turned on, BlankMode saves the font-lock state, that is, if font-lock is on or off. And BlankMode restores the font-lock state when it is turned off. So, if BlankMode is turned on and font-lock is off, BlankMode also turns on the font-lock to highlight blanks, but the font-lock will be turned off when BlankMode is turned off. Thus, turn on font-lock before BlankMode is on, if you want that font-lock continues on after BlankMode is turned off.

When BlankMode is on, it takes care of highlighting some special characters over the default mechanism of ‘nobreak-char-display’ (which see) and ‘show-trailing-whitespace’ (which see).

There are two ways of using BlankMode: local and global.

You can mix the local and global usage without any conflict. But local BlankMode has priority over global BlankMode. BlankMode is active in a buffer if you have enabled it in that buffer or if you have enabled it globally.

When global and local BlankMode are on:

To use BlankMode, insert in your ~/.emacs:

   (require 'blank-mode)

Or autoload at least one of the commands‘blank-mode’, ‘blank-toggle-options’, ‘global-blank-mode’ or ‘global-blank-toggle-options’. For example:

   (autoload 'blank-mode           "blank-mode" "Toggle blank visualization."        t)
   (autoload 'blank-toggle-options "blank-mode" "Toggle local `blank-mode' options." t)

BlankMode was inspired by:

whitespace.el – Rajesh Vaidheeswarran <rv@gnu.org>
Warn about and clean bogus whitespaces in the file
(inspired the idea to warn and clean some blanks)
show-whitespace-mode.el – Aurélien Tisné <aurelien.tisne@free.fr>
Simple mode to highlight whitespaces
(inspired the idea to use font-lock)
whitespace-mode.elLawrenceMitchell
Major mode for editing Whitespace
(inspired the idea to use display table)
visws.elMilesBader
Make whitespace visible
(handle display table, his code was modified, but the main idea was kept)

For good performance, be sure to byte-compile BlankMode, e.g.

   M-x byte-compile-file <give the path to blank-mode.el when prompted>

This will generate blank-mode.elc, which will be loaded instead of Lisp:blank-mode.el.

BlankMode was tested with GNU Emacs 22 and 23.

Using blank-mode

There is no problem if you mix local and global minor mode usage.

LOCAL BlankMode:

To toggle BlankMode options locally, type:
M-x blank-toggle-options RET
To activate BlankMode locally, type:
C-u 1 M-x blank-mode RET
To deactivate BlankMode locally, type:
C-u 0 M-x blank-mode RET
To toggle BlankMode locally, type:
M-x blank-mode RET

GLOBAL BlankMode:

To toggle BlankMode options globally, type:
M-x global-blank-toggle-options RET
To activate BlankMode globally, type:
C-u 1 M-x global-blank-mode RET
To deactivate BlankMode globally, type:
C-u 0 M-x global-blank-mode RET
To toggle BlankMode globally, type:
M-x global-blank-mode RET

You can also bind ‘blank-mode’, ‘global-blank-mode’, ‘blank-toggle-options’ and ‘global-blank-toggle-options’ to some key, like:

   (global-set-key "\C-c_b" 'blank-mode)
   (global-set-key "\C-c_t" 'blank-toggle-options)
   (global-set-key "\C-c=b" 'global-blank-mode)
   (global-set-key "\C-c=t" 'global-blank-toggle-options)

There are also the following useful commands:

‘blank-cleanup’
Cleanup some blank problems in all buffer or at region.
‘blank-cleanup-region’
Cleanup some blank problems at region.

The problems, which are cleaned up, are:

1. empty lines at beginning of buffer.
2. empty lines at end of buffer.
If ‘blank-chars’ has ‘empty’ as an element, remove all empty lines at beginning and/or end of buffer.
3. 8 or more SPACEs at beginning of line.
If ‘blank-chars’ has ‘indentation’ as an element, replace 8 or more SPACEs at beginning of line by TABs.
4. SPACEs before TAB.
If ‘blank-chars’ has ‘space-before-tab’ as an element, replace SPACEs by TABs.
5. SPACEs or TABs at end of line.
If ‘blank-chars’ has ‘trailing’ as an element, remove all SPACEs or TABs at end of line.”
6. 8 or more SPACEs after TAB.
If ‘blank-chars’ has ‘space-after-tab’ as an element, replace SPACEs by TABs.

Hooks

BlankMode has the following hook variables:

‘blank-mode-hook’
It is evaluated always when BlankMode is turned on.
‘global-blank-mode-hook’
It is evaluated always when BlankMode is turned on globally.
‘blank-load-hook’
It is evaluated after BlankMode package is loaded.

Options

Below it’s shown a brief description of BlankMode options, please, see the options declaration in the code for a long documentation.

‘blank-style’
Specify the visualization style.
‘blank-chars’
Specify which kind of blank is visualized.
‘blank-space’
Face used to visualize SPACE.
‘blank-hspace’
Face used to visualize HARD SPACE.
‘blank-tab’
Face used to visualize TAB.
‘blank-newline’
Face used to visualize NEWLINE char mapping.
‘blank-trailing’
Face used to visualize trailing blanks.
‘blank-line’
Face used to visualize “long” lines.
‘blank-space-before-tab’
Face used to visualize SPACEs before TAB.
‘blank-indentation’
Face used to visualize 8 or more SPACEs at beginning of line.
‘blank-empty’
Face used to visualize empty lines at beginning and/or end of buffer.
‘blank-space-after-tab’
Face used to visualize 8 or more SPACEs after TAB.
‘blank-space-regexp’
Specify SPACE characters regexp.
‘blank-hspace-regexp’
Specify HARD SPACE characters regexp.
‘blank-tab-regexp’
Specify TAB characters regexp.
‘blank-trailing-regexp’
Specify trailing characters regexp.
‘blank-space-before-tab-regexp’
Specify SPACEs before TAB regexp.
‘blank-indentation-regexp’
Specify regexp for 8 or more SPACEs at beginning of line.
‘blank-empty-at-bob-regexp’
Specify regexp for empty lines at beginning of buffer.
‘blank-empty-at-eob-regexp’
Specify regexp for empty lines at end of buffer.
‘blank-space-after-tab-regexp’
Specify regexp for 8 or more SPACEs after TAB.
‘blank-line-column’
Specify column beyond which the line is highlighted.
‘blank-display-mappings’
Specify an alist of mappings for displaying characters.
‘blank-global-modes’
Modes for which global BlankMode is automagically turned on.

To set the above options you may:

a. insert the code in your ~/.emacs, like:

   (setq blank-space 'underline)

This way always keep your default settings when you enter a new Emacs session.

b. or use ‘set-variable’ in your Emacs session, like:

   M-x set-variable RET blank-space RET underline RET

This way keep your settings only during the current Emacs session.

c. or use customization, for example:

In Emacs 21 or lower:

  1. click on MenuBar Help option,
  2. then click on Customize,
  3. then click on Browse Customization Groups,
  4. expand Data group,
  5. expand Blank group
  6. and then customize BlankMode options.

In Emacs 22 or higher:

  1. click on MenuBar Options option,
  2. then click on Customize Emacs,
  3. then click on Browse Customization Groups,
  4. expand Data group,
  5. expand Blank group
  6. and then customize BlankMode options.

Through this way, you may choose if the settings are kept or not when you leave out the current Emacs session.

d. or see the option value:

   C-h v blank-space RET

and click the customize hypertext button.

Through this way, you may choose if the settings are kept or not when you leave out the current Emacs session.

Acknowledgements

Thanks to nschum for the idea about highlight “long” lines tail. See EightyColumnRule.

Thanks to JuriLinkov for suggesting:

Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.

Thanks to DrewAdams for toggle commands suggestion.

Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for helping to fix ‘find-file-hooks’ reference.

Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for indicating defface byte-compilation warnings.

Thanks to TimOCallaghan for the idea about highlight “long” lines. See EightyColumnRule.

Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new newline character mapping.

Thanks to Pete Forman <pete.forman@westgeo.com> for indicating whitespace-mode on XEmacs.

Thanks to:

Rajesh Vaidheeswarran <rv@gnu.org> – whitespace.el
Aurélien Tisné <aurelien.tisne@free.fr> – show-whitespace-mode.el (ShowWhitespaceMode)
LawrenceMitchellwhitespace-mode.el (WhitespaceMode)
MilesBadervisws.el (Emacs 21)

And to all people who contributed with them.

TAB & Display Table

When BlankMode is on and a TAB occupies exactly one column, it will display the character ?\xBB at that column followed by a TAB which goes to the next TAB column. This is due the way the Emacs display engine works.

The situation above is illustrated by the following images:

BlankModeOffTab BlankModeOnTab

There are the following possibilities to deal with the situation above:

        (setq blank-display-mappings
          '((?\    [?\xB7]     [?.])		; space
            (?\xA0 [?\xA4]     [?_])		; hard space
            (?\n   [?\xB6 ?\n] [?$ ?\n])	; end-of-line
            ))
The setting above doesn’t have any TAB mapping.
        (defcustom blank-display-mappings
          '((?\    [?\xB7]     [?.])		; space
            (?\xA0 [?\xA4]     [?_])		; hard space
            (?\n   [?\xB6 ?\n] [?$ ?\n])	; end-of-line
            ;; WARNING: the mapping below has a problem.
            ;; When a TAB occupies exactly one column, it will display the character
            ;; ?\xBB at that column followed by a TAB which goes to the next TAB
            ;; column.
            ;; If this is a problem for you, please, comment the line below.
            ;;(?\t   [?\xBB ?\t] [?\\ ?\t])	; tab
            )
            ......
        (setq blank-style '(color))
That is, don’t use display table.

See Also


Inbox

I have a question about this mode running under XEmacs, in case anyone has solved it:

  • When I turn BlankMode on, only the visible part of the buffer is highlighted. Is this normal?

Thanks, – BillBarnard

just use whitespace-mode (WhitespaceMode) on XEmacs – ClausBrunzema

Use whitespace-visual-mode for recent releases of the text-modes (sources) XEmacs package (Quickstart Package Guide) – AdrianAichner

See also: ShowWhiteSpace for several other ways to do what blank-mode does.

Well, BlankMode does not run on XEmacs. It is welcome any help from anyone who knows XEmacs. – ViniciusJoseLatorre

The :stipple face attribute can do some cool things

    (defface blank-tab
      `((t (:stipple
            ;; 88 = 8 * (frame-char-width)
            ;; 18 =     (frame-char-height)
            (88 18 ,
                (string
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 #b01000000    0    0    0    0    0    0    0    0    0    0
                 #b10000000    0    0    0    0    0    0    0    0    0    0
                 #b11111100 #x01 #x01 #x08 #x40 #x00 #x02 #x10 #x80 #x00 #x04
                 #b10000000    0    0    0    0    0    0    0    0    0    0
                 #b01000000    0    0    0    0    0    0    0    0    0    0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0
                 0 0 0 0 0 0 0 0 0 0 0))
            :foreground "cyan4")))
      "")

bojohan-stippled-tab

bojohan

Cool! It doesn’t have the TAB problem which display table has. :-)

But :stipple depends on your current (frame-char-width) and (frame-char-height). :-(

From my Emacs session:
(frame-char-width) = 6
(frame-char-height) = 13
ViniciusJoseLatorre

I have been successfully using blank-mode.el and whitespace.el for a while now on Windows-based GNU Emacs, including 22.3. I appreciate it very much. Thank you!

I just tried using blank-mode.el on x86-64 Fedora 9-based GNU Emacs 22.3. It behaves as if it is not loaded at all, except that BL and/or bl appears in the modeline, depending on whether I have it in global-blank-mode and/or blank-mode. AFAIK, I have it installed and configured and loaded via .emacs the same way as I have successfully used on my Windows-based GNU Emacs 22.3. (I am having a different problem with whitespace.el that I have described over on that wikipage.) – Dan’l Miller

Well, I answered in the WhiteSpace page. – ViniciusJoseLatorre

CategoryCode, CategoryModes, CategoryFaces