Fix link errors in the Mac OS build that were caused by variables being marked as...
[bpt/emacs.git] / lisp / whitespace.el
index 56eebd7..8d25a16 100644 (file)
@@ -1,12 +1,11 @@
 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
 
 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
 
-;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 2000-2011  Free Software Foundation, Inc.
 
 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
 ;; Keywords: data, wp
 
 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
 ;; Keywords: data, wp
-;; Version: 12.0
+;; Version: 13.2
 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
 
 ;; This file is part of GNU Emacs.
 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
 
 ;; This file is part of GNU Emacs.
   "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
   :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el")
   :version "23.1"
   "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
   :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el")
   :version "23.1"
-  :group 'wp
-  :group 'data)
+  :group 'convenience)
 
 
 (defcustom whitespace-style
 
 
 (defcustom whitespace-style
-  '(tabs spaces trailing lines space-before-tab newline
-        indentation empty space-after-tab
-        space-mark tab-mark newline-mark)
+  '(face
+    tabs spaces trailing lines space-before-tab newline
+    indentation empty space-after-tab
+    space-mark tab-mark newline-mark)
   "Specify which kind of blank is visualized.
 
 It's a list containing some or all of the following values:
 
   "Specify which kind of blank is visualized.
 
 It's a list containing some or all of the following values:
 
+   face                        enable all visualization via faces (see below).
+
    trailing            trailing blanks are visualized via faces.
    trailing            trailing blanks are visualized via faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
    tabs                        TABs are visualized via faces.
 
    tabs                        TABs are visualized via faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
    spaces              SPACEs and HARD SPACEs are visualized via
                        faces.
 
    spaces              SPACEs and HARD SPACEs are visualized via
                        faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
 
-   lines               lines whose have columns beyond
+   lines               lines which have columns beyond
                        `whitespace-line-column' are highlighted via
                        `whitespace-line-column' are highlighted via
-                       faces .
+                       faces.
                        Whole line is highlighted.
                        It has precedence over `lines-tail' (see
                        below).
                        Whole line is highlighted.
                        It has precedence over `lines-tail' (see
                        below).
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
 
-   lines-tail          lines whose have columns beyond
+   lines-tail          lines which have columns beyond
                        `whitespace-line-column' are highlighted via
                        faces.
                        But only the part of line which goes
                        beyond `whitespace-line-column' column.
                        It has effect only if `lines' (see above)
                        `whitespace-line-column' are highlighted via
                        faces.
                        But only the part of line which goes
                        beyond `whitespace-line-column' column.
                        It has effect only if `lines' (see above)
-                       is not present in `whitespace-style'.
+                       is not present in `whitespace-style'
+                       and if `face' (see above) is present in
+                       `whitespace-style'.
 
    newline             NEWLINEs are visualized via faces.
 
    newline             NEWLINEs are visualized via faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
    empty               empty lines at beginning and/or end of buffer
                        are visualized via faces.
 
    empty               empty lines at beginning and/or end of buffer
                        are visualized via faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
    indentation::tab    8 or more SPACEs at beginning of line are
                        visualized via faces.
 
    indentation::tab    8 or more SPACEs at beginning of line are
                        visualized via faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
    indentation::space  TABs at beginning of line are visualized via
                        faces.
 
    indentation::space  TABs at beginning of line are visualized via
                        faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
    indentation         8 or more SPACEs at beginning of line are
                        visualized, if `indent-tabs-mode' (which see)
                        is non-nil; otherwise, TABs at beginning of
                        line are visualized via faces.
 
    indentation         8 or more SPACEs at beginning of line are
                        visualized, if `indent-tabs-mode' (which see)
                        is non-nil; otherwise, TABs at beginning of
                        line are visualized via faces.
+                       It has effect only if `face' (see above)
+                       is present in `whitespace-style'.
 
    space-after-tab::tab                8 or more SPACEs after a TAB are
                                visualized via faces.
 
    space-after-tab::tab                8 or more SPACEs after a TAB are
                                visualized via faces.
+                               It has effect only if `face' (see above)
+                               is present in `whitespace-style'.
 
    space-after-tab::space      TABs are visualized when 8 or more
                                SPACEs occur after a TAB, via faces.
 
    space-after-tab::space      TABs are visualized when 8 or more
                                SPACEs occur after a TAB, via faces.
+                               It has effect only if `face' (see above)
+                               is present in `whitespace-style'.
 
    space-after-tab             8 or more SPACEs after a TAB are
                                visualized, if `indent-tabs-mode'
                                (which see) is non-nil; otherwise,
                                the TABs are visualized via faces.
 
    space-after-tab             8 or more SPACEs after a TAB are
                                visualized, if `indent-tabs-mode'
                                (which see) is non-nil; otherwise,
                                the TABs are visualized via faces.
+                               It has effect only if `face' (see above)
+                               is present in `whitespace-style'.
 
    space-before-tab::tab       SPACEs before TAB are visualized via
                                faces.
 
    space-before-tab::tab       SPACEs before TAB are visualized via
                                faces.
+                               It has effect only if `face' (see above)
+                               is present in `whitespace-style'.
 
    space-before-tab::space     TABs are visualized when SPACEs occur
                                before TAB, via faces.
 
    space-before-tab::space     TABs are visualized when SPACEs occur
                                before TAB, via faces.
+                               It has effect only if `face' (see above)
+                               is present in `whitespace-style'.
 
    space-before-tab            SPACEs before TAB are visualized, if
                                `indent-tabs-mode' (which see) is
                                non-nil; otherwise, the TABs are
                                visualized via faces.
 
    space-before-tab            SPACEs before TAB are visualized, if
                                `indent-tabs-mode' (which see) is
                                non-nil; otherwise, the TABs are
                                visualized via faces.
+                               It has effect only if `face' (see above)
+                               is present in `whitespace-style'.
 
    space-mark          SPACEs and HARD SPACEs are visualized via
                        display table.
 
    space-mark          SPACEs and HARD SPACEs are visualized via
                        display table.
@@ -462,7 +495,7 @@ Any other value is ignored.
 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
 via display table.
 
 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
 via display table.
 
-There is an evaluation order for some values, if some values are
+There is an evaluation order for some values, if they are
 included in `whitespace-style' list.  For example, if
 indentation, indentation::tab and/or indentation::space are
 included in `whitespace-style' list.  The evaluation order for
 included in `whitespace-style' list.  For example, if
 indentation, indentation::tab and/or indentation::space are
 included in `whitespace-style' list.  The evaluation order for
@@ -487,9 +520,16 @@ So, for example, if indentation and indentation::space are
 included in `whitespace-style' list, the indentation value is
 evaluated instead of indentation::space value.
 
 included in `whitespace-style' list, the indentation value is
 evaluated instead of indentation::space value.
 
+One reason for not visualize spaces via faces (if `face' is not
+included in `whitespace-style') is to use exclusively for
+cleanning up a buffer.  See `whitespace-cleanup' and
+`whitespace-cleanup-region' for documentation.
+
 See also `whitespace-display-mappings' for documentation."
   :type '(repeat :tag "Kind of Blank"
                 (choice :tag "Kind of Blank Face"
 See also `whitespace-display-mappings' for documentation."
   :type '(repeat :tag "Kind of Blank"
                 (choice :tag "Kind of Blank Face"
+                        (const :tag "(Face) Face visualization"
+                               face)
                         (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
                                trailing)
                         (const :tag "(Face) SPACEs and HARD SPACEs"
                         (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
                                trailing)
                         (const :tag "(Face) SPACEs and HARD SPACEs"
@@ -522,9 +562,9 @@ Used when `whitespace-style' includes the value `spaces'."
 
 (defface whitespace-space
   '((((class color) (background dark))
 
 (defface whitespace-space
   '((((class color) (background dark))
-     (:background "grey20"      :foreground "aquamarine3"))
+     (:background "grey20"      :foreground "darkgray"))
     (((class color) (background light))
     (((class color) (background light))
-     (:background "LightYellow" :foreground "aquamarine3"))
+     (:background "LightYellow" :foreground "lightgray"))
     (t (:inverse-video t)))
   "Face used to visualize SPACE."
   :group 'whitespace)
     (t (:inverse-video t)))
   "Face used to visualize SPACE."
   :group 'whitespace)
@@ -540,9 +580,9 @@ Used when `whitespace-style' includes the value `spaces'."
 
 (defface whitespace-hspace             ; 'nobreak-space
   '((((class color) (background dark))
 
 (defface whitespace-hspace             ; 'nobreak-space
   '((((class color) (background dark))
-     (:background "grey24"        :foreground "aquamarine3"))
+     (:background "grey24"        :foreground "darkgray"))
     (((class color) (background light))
     (((class color) (background light))
-     (:background "LemonChiffon3" :foreground "aquamarine3"))
+     (:background "LemonChiffon3" :foreground "lightgray"))
     (t (:inverse-video t)))
   "Face used to visualize HARD SPACE."
   :group 'whitespace)
     (t (:inverse-video t)))
   "Face used to visualize HARD SPACE."
   :group 'whitespace)
@@ -558,9 +598,9 @@ Used when `whitespace-style' includes the value `tabs'."
 
 (defface whitespace-tab
   '((((class color) (background dark))
 
 (defface whitespace-tab
   '((((class color) (background dark))
-     (:background "grey22" :foreground "aquamarine3"))
+     (:background "grey22" :foreground "darkgray"))
     (((class color) (background light))
     (((class color) (background light))
-     (:background "beige"  :foreground "aquamarine3"))
+     (:background "beige"  :foreground "lightgray"))
     (t (:inverse-video t)))
   "Face used to visualize TAB."
   :group 'whitespace)
     (t (:inverse-video t)))
   "Face used to visualize TAB."
   :group 'whitespace)
@@ -813,7 +853,7 @@ Used when `whitespace-style' includes `indentation',
   :group 'whitespace)
 
 
   :group 'whitespace)
 
 
-(defcustom whitespace-empty-at-bob-regexp "\\`\\(\\([ \t]*\n\\)+\\)"
+(defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
   "Specify regexp for empty lines at beginning of buffer.
 
 If you're using `mule' package, there may be other characters besides:
   "Specify regexp for empty lines at beginning of buffer.
 
 If you're using `mule' package, there may be other characters besides:
@@ -828,7 +868,7 @@ Used when `whitespace-style' includes `empty'."
   :group 'whitespace)
 
 
   :group 'whitespace)
 
 
-(defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)\\'"
+(defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
   "Specify regexp for empty lines at end of buffer.
 
 If you're using `mule' package, there may be other characters besides:
   "Specify regexp for empty lines at end of buffer.
 
 If you're using `mule' package, there may be other characters besides:
@@ -867,8 +907,13 @@ Used when `whitespace-style' includes `space-after-tab',
 (defcustom whitespace-line-column 80
   "Specify column beyond which the line is highlighted.
 
 (defcustom whitespace-line-column 80
   "Specify column beyond which the line is highlighted.
 
+It must be an integer or nil.  If nil, the `fill-column' variable value is
+used.
+
 Used when `whitespace-style' includes `lines' or `lines-tail'."
 Used when `whitespace-style' includes `lines' or `lines-tail'."
-  :type '(integer :tag "Line Length")
+  :type '(choice :tag "Line Length Limit"
+                (integer :tag "Line Length")
+                (const :tag "Use fill-column" nil))
   :group 'whitespace)
 
 
   :group 'whitespace)
 
 
@@ -1057,7 +1102,7 @@ See also `whitespace-newline' and `whitespace-display-mappings'."
   :init-value nil
   :global     nil
   :group      'whitespace
   :init-value nil
   :global     nil
   :group      'whitespace
-  (let ((whitespace-style '(newline-mark newline)))
+  (let ((whitespace-style '(face newline-mark newline)))
     (whitespace-mode whitespace-newline-mode)
     ;; sync states (running a batch job)
     (setq whitespace-newline-mode whitespace-mode)))
     (whitespace-mode whitespace-newline-mode)
     ;; sync states (running a batch job)
     (setq whitespace-newline-mode whitespace-mode)))
@@ -1134,7 +1179,7 @@ otherwise, turn off visualization.
 Use `global-whitespace-newline-mode' only for NEWLINE
 visualization exclusively.  For other visualizations, including
 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
 Use `global-whitespace-newline-mode' only for NEWLINE
 visualization exclusively.  For other visualizations, including
 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
-please, use `global-whitespace-mode'.
+please use `global-whitespace-mode'.
 
 See also `whitespace-newline' and `whitespace-display-mappings'."
   :lighter    " NL"
 
 See also `whitespace-newline' and `whitespace-display-mappings'."
   :lighter    " NL"
@@ -1142,7 +1187,8 @@ See also `whitespace-newline' and `whitespace-display-mappings'."
   :global     t
   :group      'whitespace
   (let ((whitespace-style '(newline-mark newline)))
   :global     t
   :group      'whitespace
   (let ((whitespace-style '(newline-mark newline)))
-    (global-whitespace-mode global-whitespace-newline-mode)
+    (global-whitespace-mode (if global-whitespace-newline-mode
+                                1 -1))
     ;; sync states (running a batch job)
     (setq global-whitespace-newline-mode global-whitespace-mode)))
 
     ;; sync states (running a batch job)
     (setq global-whitespace-newline-mode global-whitespace-mode)))
 
@@ -1152,7 +1198,8 @@ See also `whitespace-newline' and `whitespace-display-mappings'."
 
 
 (defconst whitespace-style-value-list
 
 
 (defconst whitespace-style-value-list
-  '(tabs
+  '(face
+    tabs
     spaces
     trailing
     lines
     spaces
     trailing
     lines
@@ -1177,7 +1224,8 @@ See also `whitespace-newline' and `whitespace-display-mappings'."
 
 
 (defconst whitespace-toggle-option-alist
 
 
 (defconst whitespace-toggle-option-alist
-  '((?t    . tabs)
+  '((?f    . face)
+    (?t    . tabs)
     (?s    . spaces)
     (?r    . trailing)
     (?l    . lines)
     (?s    . spaces)
     (?r    . trailing)
     (?l    . lines)
@@ -1229,6 +1277,19 @@ Used by `whitespace-trailing-regexp' function (which see).")
   "Used to save locally the font-lock refontify state.
 Used by `whitespace-post-command-hook' function (which see).")
 
   "Used to save locally the font-lock refontify state.
 Used by `whitespace-post-command-hook' function (which see).")
 
+(defvar whitespace-bob-marker nil
+  "Used to save locally the bob marker value.
+Used by `whitespace-post-command-hook' function (which see).")
+
+(defvar whitespace-eob-marker nil
+  "Used to save locally the eob marker value.
+Used by `whitespace-post-command-hook' function (which see).")
+
+(defvar whitespace-buffer-changed nil
+  "Used to indicate locally if buffer changed.
+Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
+functions (which see).")
+
 
 ;;;###autoload
 (defun whitespace-toggle-options (arg)
 
 ;;;###autoload
 (defun whitespace-toggle-options (arg)
@@ -1244,6 +1305,7 @@ Interactively, it reads one of the following chars:
 
   CHAR MEANING
   (VIA FACES)
 
   CHAR MEANING
   (VIA FACES)
+   f   toggle face visualization
    t   toggle TAB visualization
    s   toggle SPACE and HARD SPACE visualization
    r   toggle trailing blanks visualization
    t   toggle TAB visualization
    s   toggle SPACE and HARD SPACE visualization
    r   toggle trailing blanks visualization
@@ -1272,6 +1334,7 @@ Interactively, it reads one of the following chars:
 Non-interactively, ARG should be a symbol or a list of symbols.
 The valid symbols are:
 
 Non-interactively, ARG should be a symbol or a list of symbols.
 The valid symbols are:
 
+   face                        toggle face visualization
    tabs                        toggle TAB visualization
    spaces              toggle SPACE and HARD SPACE visualization
    trailing            toggle trailing blanks visualization
    tabs                        toggle TAB visualization
    spaces              toggle SPACE and HARD SPACE visualization
    trailing            toggle trailing blanks visualization
@@ -1321,6 +1384,7 @@ Interactively, it accepts one of the following chars:
 
   CHAR MEANING
   (VIA FACES)
 
   CHAR MEANING
   (VIA FACES)
+   f   toggle face visualization
    t   toggle TAB visualization
    s   toggle SPACE and HARD SPACE visualization
    r   toggle trailing blanks visualization
    t   toggle TAB visualization
    s   toggle SPACE and HARD SPACE visualization
    r   toggle trailing blanks visualization
@@ -1349,6 +1413,7 @@ Interactively, it accepts one of the following chars:
 Non-interactively, ARG should be a symbol or a list of symbols.
 The valid symbols are:
 
 Non-interactively, ARG should be a symbol or a list of symbols.
 The valid symbols are:
 
+   face                        toggle face visualization
    tabs                        toggle TAB visualization
    spaces              toggle SPACE and HARD SPACE visualization
    trailing            toggle trailing blanks visualization
    tabs                        toggle TAB visualization
    spaces              toggle SPACE and HARD SPACE visualization
    trailing            toggle trailing blanks visualization
@@ -1464,10 +1529,10 @@ documentation."
          (let (overwrite-mode)         ; enforce no overwrite
            (goto-char (point-min))
            (when (re-search-forward
          (let (overwrite-mode)         ; enforce no overwrite
            (goto-char (point-min))
            (when (re-search-forward
-                  whitespace-empty-at-bob-regexp nil t)
+                  (concat "\\`" whitespace-empty-at-bob-regexp) nil t)
              (delete-region (match-beginning 1) (match-end 1)))
            (when (re-search-forward
              (delete-region (match-beginning 1) (match-end 1)))
            (when (re-search-forward
-                  whitespace-empty-at-eob-regexp nil t)
+                  (concat whitespace-empty-at-eob-regexp "\\'") nil t)
              (delete-region (match-beginning 1) (match-end 1)))))))
     ;; PROBLEM 3: 8 or more SPACEs at bol
     ;; PROBLEM 4: SPACEs before TAB
              (delete-region (match-beginning 1) (match-end 1)))))))
     ;; PROBLEM 3: 8 or more SPACEs at bol
     ;; PROBLEM 4: SPACEs before TAB
@@ -1587,12 +1652,12 @@ documentation."
            (whitespace-replace-action
             (if whitespace-indent-tabs-mode 'tabify 'untabify)
             rstart rend whitespace-space-before-tab-regexp
            (whitespace-replace-action
             (if whitespace-indent-tabs-mode 'tabify 'untabify)
             rstart rend whitespace-space-before-tab-regexp
-            (if whitespace-indent-tabs-mode 1 2)))
+            (if whitespace-indent-tabs-mode 0 2)))
           ;; ACTION: replace SPACEs before TAB by TABs.
           ((memq 'space-before-tab::tab whitespace-style)
            (whitespace-replace-action
             'tabify rstart rend
           ;; ACTION: replace SPACEs before TAB by TABs.
           ((memq 'space-before-tab::tab whitespace-style)
            (whitespace-replace-action
             'tabify rstart rend
-            whitespace-space-before-tab-regexp 1))
+            whitespace-space-before-tab-regexp 0))
           ;; ACTION: replace TABs by SPACEs.
           ((memq 'space-before-tab::space whitespace-style)
            (whitespace-replace-action
           ;; ACTION: replace TABs by SPACEs.
           ((memq 'space-before-tab::space whitespace-style)
            (whitespace-replace-action
@@ -1878,9 +1943,10 @@ cleaning up these problems."
 
 (defconst whitespace-help-text
   "\
 
 (defconst whitespace-help-text
   "\
- Whitespace Toggle Options
-
- FACES
+ Whitespace Toggle Options                  | scroll up  :  SPC   or > |
+                                            | scroll down:  M-SPC or < |
+ FACES                                      \\__________________________/
+ []  f   - toggle face visualization
  []  t   - toggle TAB visualization
  []  s   - toggle SPACE and HARD SPACE visualization
  []  r   - toggle trailing blanks visualization
  []  t   - toggle TAB visualization
  []  s   - toggle SPACE and HARD SPACE visualization
  []  r   - toggle trailing blanks visualization
@@ -1954,15 +2020,13 @@ cleaning up these problems."
   "Display BUFFER in a new window."
   (goto-char (point-min))
   (set-buffer-modified-p nil)
   "Display BUFFER in a new window."
   (goto-char (point-min))
   (set-buffer-modified-p nil)
-  (let ((size (- (window-height)
-                (max window-min-height
-                     (1+ (count-lines (point-min)
-                                      (point-max)))))))
-    (when (<= size 0)
-      (kill-buffer buffer)
-      (error "Frame height is too small; \
+  (when (< (window-height) (* 2 window-min-height))
+    (kill-buffer buffer)
+    (error "Window height is too small; \
 can't split window to display whitespace toggle options"))
 can't split window to display whitespace toggle options"))
-    (set-window-buffer (split-window nil size) buffer)))
+  (let ((win (split-window)))
+    (set-window-buffer win buffer)
+    (shrink-window-if-larger-than-buffer win)))
 
 
 (defun whitespace-kill-buffer (buffer-name)
 
 
 (defun whitespace-kill-buffer (buffer-name)
@@ -1978,6 +2042,24 @@ can't split window to display whitespace toggle options"))
   (whitespace-kill-buffer whitespace-help-buffer-name))
 
 
   (whitespace-kill-buffer whitespace-help-buffer-name))
 
 
+(defun whitespace-help-scroll (&optional up)
+  "Scroll help window, if it exists.
+
+If UP is non-nil, scroll up; otherwise, scroll down."
+  (condition-case data-help
+      (let ((buffer (get-buffer whitespace-help-buffer-name)))
+       (if buffer
+           (with-selected-window (get-buffer-window buffer)
+             (if up
+                 (scroll-up 3)
+               (scroll-down 3)))
+         (ding)))
+    ;; handler
+    ((error)
+     ;; just ignore error
+     )))
+
+
 (defun whitespace-interactive-char (local-p)
   "Interactive function to read a char and return a symbol.
 
 (defun whitespace-interactive-char (local-p)
   "Interactive function to read a char and return a symbol.
 
@@ -1988,6 +2070,7 @@ It accepts one of the following chars:
 
   CHAR MEANING
   (VIA FACES)
 
   CHAR MEANING
   (VIA FACES)
+   f   toggle face visualization
    t   toggle TAB visualization
    s   toggle SPACE and HARD SPACE visualization
    r   toggle trailing blanks visualization
    t   toggle TAB visualization
    s   toggle SPACE and HARD SPACE visualization
    r   toggle trailing blanks visualization
@@ -2037,16 +2120,20 @@ See also `whitespace-toggle-option-alist'."
                         (cdr
                          (assq ch whitespace-toggle-option-alist)))))
              ;; while body
                         (cdr
                          (assq ch whitespace-toggle-option-alist)))))
              ;; while body
-             (if (eq ch ?\?)
-                 (whitespace-help-on style)
-               (ding)))
+             (cond
+              ((eq ch ?\?)   (whitespace-help-on style))
+              ((eq ch ?\ )   (whitespace-help-scroll t))
+              ((eq ch ?\M- ) (whitespace-help-scroll))
+              ((eq ch ?>)    (whitespace-help-scroll t))
+              ((eq ch ?<)    (whitespace-help-scroll))
+              (t             (ding))))
            (whitespace-help-off)
            (message " "))              ; clean echo area
        ;; handler
        ((quit error)
         (whitespace-help-off)
         (error (error-message-string data)))))
            (whitespace-help-off)
            (message " "))              ; clean echo area
        ;; handler
        ((quit error)
         (whitespace-help-off)
         (error (error-message-string data)))))
-    (list sym)))                       ; return the apropriate symbol
+    (list sym)))                       ; return the appropriate symbol
 
 
 (defun whitespace-toggle-list (local-p arg the-list)
 
 
 (defun whitespace-toggle-list (local-p arg the-list)
@@ -2118,22 +2205,23 @@ resultant list will be returned."
 
 (defun whitespace-style-face-p ()
   "Return t if there is some visualization via face."
 
 (defun whitespace-style-face-p ()
   "Return t if there is some visualization via face."
-  (or (memq 'tabs                    whitespace-active-style)
-      (memq 'spaces                  whitespace-active-style)
-      (memq 'trailing                whitespace-active-style)
-      (memq 'lines                   whitespace-active-style)
-      (memq 'lines-tail              whitespace-active-style)
-      (memq 'newline                 whitespace-active-style)
-      (memq 'empty                   whitespace-active-style)
-      (memq 'indentation             whitespace-active-style)
-      (memq 'indentation::tab        whitespace-active-style)
-      (memq 'indentation::space      whitespace-active-style)
-      (memq 'space-after-tab         whitespace-active-style)
-      (memq 'space-after-tab::tab    whitespace-active-style)
-      (memq 'space-after-tab::space  whitespace-active-style)
-      (memq 'space-before-tab        whitespace-active-style)
-      (memq 'space-before-tab::tab   whitespace-active-style)
-      (memq 'space-before-tab::space whitespace-active-style)))
+  (and (memq 'face whitespace-active-style)
+       (or (memq 'tabs                    whitespace-active-style)
+          (memq 'spaces                  whitespace-active-style)
+          (memq 'trailing                whitespace-active-style)
+          (memq 'lines                   whitespace-active-style)
+          (memq 'lines-tail              whitespace-active-style)
+          (memq 'newline                 whitespace-active-style)
+          (memq 'empty                   whitespace-active-style)
+          (memq 'indentation             whitespace-active-style)
+          (memq 'indentation::tab        whitespace-active-style)
+          (memq 'indentation::space      whitespace-active-style)
+          (memq 'space-after-tab         whitespace-active-style)
+          (memq 'space-after-tab::tab    whitespace-active-style)
+          (memq 'space-after-tab::space  whitespace-active-style)
+          (memq 'space-before-tab        whitespace-active-style)
+          (memq 'space-before-tab::tab   whitespace-active-style)
+          (memq 'space-before-tab::space whitespace-active-style))))
 
 
 (defun whitespace-color-on ()
 
 
 (defun whitespace-color-on ()
@@ -2147,8 +2235,15 @@ resultant list will be returned."
     (set (make-local-variable 'whitespace-point)
         (point))
     (set (make-local-variable 'whitespace-font-lock-refontify)
     (set (make-local-variable 'whitespace-point)
         (point))
     (set (make-local-variable 'whitespace-font-lock-refontify)
+        0)
+    (set (make-local-variable 'whitespace-bob-marker)
+        (point-min-marker))
+    (set (make-local-variable 'whitespace-eob-marker)
+        (point-max-marker))
+    (set (make-local-variable 'whitespace-buffer-changed)
         nil)
     (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
         nil)
     (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
+    (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
     ;; turn off font lock
     (set (make-local-variable 'whitespace-font-lock-mode)
         font-lock-mode)
     ;; turn off font lock
     (set (make-local-variable 'whitespace-font-lock-mode)
         font-lock-mode)
@@ -2159,7 +2254,7 @@ resultant list will be returned."
        nil
        (list
        ;; Show SPACEs
        nil
        (list
        ;; Show SPACEs
-       (list #'whitespace-space-regexp  1 whitespace-space  t)
+       (list whitespace-space-regexp  1 whitespace-space  t)
        ;; Show HARD SPACEs
        (list whitespace-hspace-regexp 1 whitespace-hspace t))
        t))
        ;; Show HARD SPACEs
        (list whitespace-hspace-regexp 1 whitespace-hspace t))
        t))
@@ -2168,7 +2263,7 @@ resultant list will be returned."
        nil
        (list
        ;; Show TABs
        nil
        (list
        ;; Show TABs
-       (list #'whitespace-tab-regexp 1 whitespace-tab t))
+       (list whitespace-tab-regexp 1 whitespace-tab t))
        t))
     (when (memq 'trailing whitespace-active-style)
       (font-lock-add-keywords
        t))
     (when (memq 'trailing whitespace-active-style)
       (font-lock-add-keywords
@@ -2184,14 +2279,16 @@ resultant list will be returned."
        (list
        ;; Show "long" lines
        (list
        (list
        ;; Show "long" lines
        (list
-        (format
-         "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
-         whitespace-tab-width (1- whitespace-tab-width)
-         (/ whitespace-line-column whitespace-tab-width)
-         (let ((rem (% whitespace-line-column whitespace-tab-width)))
-           (if (zerop rem)
-               ""
-             (format ".\\{%d\\}" rem))))
+        (let ((line-column (or whitespace-line-column fill-column)))
+          (format
+           "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
+           whitespace-tab-width
+           (1- whitespace-tab-width)
+           (/ line-column whitespace-tab-width)
+           (let ((rem (% line-column whitespace-tab-width)))
+             (if (zerop rem)
+                 ""
+               (format ".\\{%d\\}" rem)))))
         (if (memq 'lines whitespace-active-style)
             0                          ; whole line
           2)                           ; line tail
         (if (memq 'lines whitespace-active-style)
             0                          ; whole line
           2)                           ; line tail
@@ -2297,7 +2394,8 @@ resultant list will be returned."
   ;; turn off font lock
   (when (whitespace-style-face-p)
     (font-lock-mode 0)
   ;; turn off font lock
   (when (whitespace-style-face-p)
     (font-lock-mode 0)
-    (remove-hook 'post-command-hook #'whitespace-post-command-hook)
+    (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
+    (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
     (when whitespace-font-lock
       (setq whitespace-font-lock nil
            font-lock-keywords   whitespace-font-lock-keywords))
     (when whitespace-font-lock
       (setq whitespace-font-lock nil
            font-lock-keywords   whitespace-font-lock-keywords))
@@ -2318,37 +2416,128 @@ resultant list will be returned."
 (defun whitespace-empty-at-bob-regexp (limit)
   "Match spaces at beginning of buffer which do not contain the point at \
 beginning of buffer."
 (defun whitespace-empty-at-bob-regexp (limit)
   "Match spaces at beginning of buffer which do not contain the point at \
 beginning of buffer."
-  (and (/= whitespace-point 1)
-       (re-search-forward whitespace-empty-at-bob-regexp limit t)))
+  (let ((b (point))
+       r)
+    (cond
+     ;; at bob
+     ((= b 1)
+      (setq r (and (/= whitespace-point 1)
+                  (looking-at whitespace-empty-at-bob-regexp)))
+      (if r
+         (set-marker whitespace-bob-marker (match-end 1))
+       (set-marker whitespace-bob-marker b)))
+     ;; inside bob empty region
+     ((<= limit whitespace-bob-marker)
+      (setq r (looking-at whitespace-empty-at-bob-regexp))
+      (if r
+         (when (< (match-end 1) limit)
+           (set-marker whitespace-bob-marker (match-end 1)))
+       (set-marker whitespace-bob-marker b)))
+     ;; intersection with end of bob empty region
+     ((<= b whitespace-bob-marker)
+      (setq r (looking-at whitespace-empty-at-bob-regexp))
+      (if r
+         (set-marker whitespace-bob-marker (match-end 1))
+       (set-marker whitespace-bob-marker b)))
+     ;; it is not inside bob empty region
+     (t
+      (setq r nil)))
+    ;; move to end of matching
+    (and r (goto-char (match-end 1)))
+    r))
+
+
+(defsubst whitespace-looking-back (regexp limit)
+  (save-excursion
+    (when (/= 0 (skip-chars-backward " \t\n" limit))
+      (unless (bolp)
+       (forward-line 1))
+      (looking-at regexp))))
 
 
 (defun whitespace-empty-at-eob-regexp (limit)
   "Match spaces at end of buffer which do not contain the point at end of \
 buffer."
 
 
 (defun whitespace-empty-at-eob-regexp (limit)
   "Match spaces at end of buffer which do not contain the point at end of \
 buffer."
-  (and (/= whitespace-point (1+ (buffer-size)))
-       (re-search-forward whitespace-empty-at-eob-regexp limit t)))
-
-
-(defun whitespace-space-regexp (limit)
-  "Match spaces."
-  (setq whitespace-font-lock-refontify t)
-  (re-search-forward whitespace-space-regexp limit t))
-
-
-(defun whitespace-tab-regexp (limit)
-  "Match tabs."
-  (setq whitespace-font-lock-refontify t)
-  (re-search-forward whitespace-tab-regexp limit t))
+  (let ((b (point))
+       (e (1+ (buffer-size)))
+       r)
+    (cond
+     ;; at eob
+     ((= limit e)
+      (when (/= whitespace-point e)
+       (goto-char limit)
+       (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
+      (if r
+         (set-marker whitespace-eob-marker (match-beginning 1))
+       (set-marker whitespace-eob-marker limit)
+       (goto-char b)))                 ; return back to initial position
+     ;; inside eob empty region
+     ((>= b whitespace-eob-marker)
+      (goto-char limit)
+      (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
+      (if r
+         (when (> (match-beginning 1) b)
+           (set-marker whitespace-eob-marker (match-beginning 1)))
+       (set-marker whitespace-eob-marker limit)
+       (goto-char b)))                 ; return back to initial position
+     ;; intersection with beginning of eob empty region
+     ((>= limit whitespace-eob-marker)
+      (goto-char limit)
+      (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
+      (if r
+         (set-marker whitespace-eob-marker (match-beginning 1))
+       (set-marker whitespace-eob-marker limit)
+       (goto-char b)))                 ; return back to initial position
+     ;; it is not inside eob empty region
+     (t
+      (setq r nil)))
+    r))
+
+
+(defun whitespace-buffer-changed (beg end)
+  "Set `whitespace-buffer-changed' variable to t."
+  (setq whitespace-buffer-changed t))
 
 
 (defun whitespace-post-command-hook ()
   "Save current point into `whitespace-point' variable.
 Also refontify when necessary."
 
 
 (defun whitespace-post-command-hook ()
   "Save current point into `whitespace-point' variable.
 Also refontify when necessary."
-  (setq whitespace-point (point))
-  (let ((refontify (or (eolp)                   ; end of line
-                      (= whitespace-point 1)))) ; beginning of buffer
-    (when (or whitespace-font-lock-refontify refontify)
-      (setq whitespace-font-lock-refontify refontify)
+  (setq whitespace-point (point))      ; current point position
+  (let ((refontify
+        (or
+         ;; it is at end of line ...
+         (and (eolp)
+              ;; ... with trailing SPACE or TAB
+              (or (= (preceding-char) ?\ )
+                  (= (preceding-char) ?\t)))
+         ;; it is at beginning of buffer (bob)
+         (= whitespace-point 1)
+         ;; the buffer was modified and ...
+         (and whitespace-buffer-changed
+              (or
+               ;; ... or inside bob whitespace region
+               (<= whitespace-point whitespace-bob-marker)
+               ;; ... or at bob whitespace region border
+               (and (= whitespace-point (1+ whitespace-bob-marker))
+                    (= (preceding-char) ?\n))))
+         ;; it is at end of buffer (eob)
+         (= whitespace-point (1+ (buffer-size)))
+         ;; the buffer was modified and ...
+         (and whitespace-buffer-changed
+              (or
+               ;; ... or inside eob whitespace region
+               (>= whitespace-point whitespace-eob-marker)
+               ;; ... or at eob whitespace region border
+               (and (= whitespace-point (1- whitespace-eob-marker))
+                    (= (following-char) ?\n)))))))
+    (when (or refontify (> whitespace-font-lock-refontify 0))
+      (setq whitespace-buffer-changed nil)
+      ;; adjust refontify counter
+      (setq whitespace-font-lock-refontify
+           (if refontify
+               1
+             (1- whitespace-font-lock-refontify)))
+      ;; refontify
       (jit-lock-refontify))))
 
 \f
       (jit-lock-refontify))))
 
 \f
@@ -2387,6 +2576,10 @@ Also refontify when necessary."
       (unless whitespace-display-table-was-local
        (setq whitespace-display-table-was-local t
              whitespace-display-table
       (unless whitespace-display-table-was-local
        (setq whitespace-display-table-was-local t
              whitespace-display-table
+             (copy-sequence buffer-display-table))
+       ;; asure `buffer-display-table' is unique
+       ;; when two or more windows are visible.
+       (setq buffer-display-table
              (copy-sequence buffer-display-table)))
       (unless buffer-display-table
        (setq buffer-display-table (make-display-table)))
              (copy-sequence buffer-display-table)))
       (unless buffer-display-table
        (setq buffer-display-table (make-display-table)))
@@ -2475,5 +2668,4 @@ It should be added buffer-locally to `write-file-functions'."
 (run-hooks 'whitespace-load-hook)
 
 
 (run-hooks 'whitespace-load-hook)
 
 
-;; arch-tag: 1b1e2500-dbd4-4a26-8f7a-5a5edfd3c97e
 ;;; whitespace.el ends here
 ;;; whitespace.el ends here