(f90-mode-map): Don't bind \t and \r.
[bpt/emacs.git] / lisp / whitespace.el
index 8053483..56d4e58 100644 (file)
@@ -6,7 +6,7 @@
 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
 ;; Keywords: data, wp
-;; Version: 11.1
+;; Version: 11.2.1
 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
 
 ;; This file is part of GNU Emacs.
 ;;
 ;; There are also the following useful commands:
 ;;
+;; `whitespace-newline-mode'
+;;    Toggle NEWLINE minor mode visualization ("nl" on modeline).
+;;
+;; `global-whitespace-newline-mode'
+;;    Toggle NEWLINE global minor mode visualization ("NL" on modeline).
+;;
 ;; `whitespace-report'
 ;;    Report some blank problems in buffer.
 ;;
 ;; Acknowledgements
 ;; ----------------
 ;;
+;; Thanks to David Reitter <david.reitter@gmail.com> for suggesting a
+;; `whitespace-newline' initialization with low contrast relative to
+;; the background color.
+;;
 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
 ;; `indent-tabs-mode' usage suggestion.
 ;;
 ;; "long" lines.  See EightyColumnRule (EmacsWiki).
 ;;
 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
-;; newline character mapping.
+;; NEWLINE character mapping.
 ;;
 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
 ;; whitespace-mode.el on XEmacs.
@@ -564,10 +574,10 @@ and `newline'."
 
 (defface whitespace-newline
   '((((class color) (background dark))
-     (:background "grey26" :foreground "aquamarine3" :bold t))
+     (:foreground "darkgray" :bold nil))
     (((class color) (background light))
-     (:background "linen"  :foreground "aquamarine3" :bold t))
-    (t (:bold t :underline t)))
+     (:foreground "lightgray" :bold nil))
+    (t (:underline t :bold nil)))
   "Face used to visualize NEWLINE char mapping.
 
 See `whitespace-display-mappings'."
@@ -979,6 +989,10 @@ It's a list containing some or all of the following values:
    abort-on-bogus      abort if there is any bogus whitespace and the
                        buffer is written or killed.
 
+   warn-read-only      give a warning if `cleanup' or `auto-cleanup'
+                       is included in `whitespace-action' and the
+                       buffer is read-only.
+
 Any other value is treated as nil."
   :type '(choice :tag "Actions"
                 (const :tag "None" nil)
@@ -987,7 +1001,8 @@ Any other value is treated as nil."
                          (const :tag "Cleanup When On" cleanup)
                          (const :tag "Report On Bogus" report-on-bogus)
                          (const :tag "Auto Cleanup" auto-cleanup)
-                         (const :tag "Abort On Bogus" abort-on-bogus))))
+                         (const :tag "Abort On Bogus" abort-on-bogus)
+                         (const :tag "Warn Read-Only" warn-read-only))))
   :group 'whitespace)
 
 \f
@@ -1002,7 +1017,10 @@ Any other value is treated as nil."
 If ARG is null, toggle whitespace visualization.
 If ARG is a number greater than zero, turn on visualization;
 otherwise, turn off visualization.
-Only useful with a windowing system."
+Only useful with a windowing system.
+
+See also `whitespace-style', `whitespace-newline' and
+`whitespace-display-mappings'."
   :lighter    " ws"
   :init-value nil
   :global     nil
@@ -1016,6 +1034,31 @@ Only useful with a windowing system."
    (t                                  ; whitespace-mode off
     (whitespace-turn-off))))
 
+
+;;;###autoload
+(define-minor-mode whitespace-newline-mode
+  "Toggle NEWLINE minor mode visualization (\"nl\" on modeline).
+
+If ARG is null, toggle NEWLINE visualization.
+If ARG is a number greater than zero, turn on visualization;
+otherwise, turn off visualization.
+Only useful with a windowing system.
+
+Use `whitespace-newline-mode' only for NEWLINE visualization
+exclusively.  For other visualizations, including NEWLINE
+visualization together with (HARD) SPACEs and/or TABs, please,
+use `whitespace-mode'.
+
+See also `whitespace-newline' and `whitespace-display-mappings'."
+  :lighter    " nl"
+  :init-value nil
+  :global     nil
+  :group      'whitespace
+  (let ((whitespace-style '(newline-mark newline)))
+    (whitespace-mode whitespace-newline-mode)
+    ;; sync states (running a batch job)
+    (setq whitespace-newline-mode whitespace-mode)))
+
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;; User commands - Global mode
@@ -1028,7 +1071,10 @@ Only useful with a windowing system."
 If ARG is null, toggle whitespace visualization.
 If ARG is a number greater than zero, turn on visualization;
 otherwise, turn off visualization.
-Only useful with a windowing system."
+Only useful with a windowing system.
+
+See also `whitespace-style', `whitespace-newline' and
+`whitespace-display-mappings'."
   :lighter    " WS"
   :init-value nil
   :global     t
@@ -1074,6 +1120,31 @@ Only useful with a windowing system."
        ;; Otherwise, turn on whitespace mode.
        (whitespace-turn-on)))))
 
+
+;;;###autoload
+(define-minor-mode global-whitespace-newline-mode
+  "Toggle NEWLINE global minor mode visualization (\"NL\" on modeline).
+
+If ARG is null, toggle NEWLINE visualization.
+If ARG is a number greater than zero, turn on visualization;
+otherwise, turn off visualization.
+Only useful with a windowing system.
+
+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'.
+
+See also `whitespace-newline' and `whitespace-display-mappings'."
+  :lighter    " NL"
+  :init-value nil
+  :global     t
+  :group      'whitespace
+  (let ((whitespace-style '(newline-mark newline)))
+    (global-whitespace-mode global-whitespace-newline-mode)
+    ;; sync states (running a batch job)
+    (setq global-whitespace-newline-mode global-whitespace-mode)))
+
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;; User commands - Toggle
@@ -1362,18 +1433,23 @@ The problems cleaned up are:
 
 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
 documentation."
-  (interactive "@*")
-  (if (and (or transient-mark-mode
-              current-prefix-arg)
-          mark-active)
-      ;; region active
-      ;; PROBLEMs 1 and 2 are not handled in region
-      ;; PROBLEM 3: 8 or more SPACEs at bol
-      ;; PROBLEM 4: SPACEs before TAB
-      ;; PROBLEM 5: SPACEs or TABs at eol
-      ;; PROBLEM 6: 8 or more SPACEs after TAB
-      (whitespace-cleanup-region (region-beginning) (region-end))
-    ;; whole buffer
+  (interactive "@")
+  (cond
+   ;; read-only buffer
+   (buffer-read-only
+    (whitespace-warn-read-only "cleanup"))
+   ;; region active
+   ((and (or transient-mark-mode
+            current-prefix-arg)
+        mark-active)
+    ;; PROBLEMs 1 and 2 are not handled in region
+    ;; PROBLEM 3: 8 or more SPACEs at bol
+    ;; PROBLEM 4: SPACEs before TAB
+    ;; PROBLEM 5: SPACEs or TABs at eol
+    ;; PROBLEM 6: 8 or more SPACEs after TAB
+    (whitespace-cleanup-region (region-beginning) (region-end)))
+   ;; whole buffer
+   (t
     (save-excursion
       (save-match-data
        ;; PROBLEM 1: empty lines at bob
@@ -1392,7 +1468,7 @@ documentation."
     ;; PROBLEM 4: SPACEs before TAB
     ;; PROBLEM 5: SPACEs or TABs at eol
     ;; PROBLEM 6: 8 or more SPACEs after TAB
-    (whitespace-cleanup-region (point-min) (point-max))))
+    (whitespace-cleanup-region (point-min) (point-max)))))
 
 
 ;;;###autoload
@@ -1435,85 +1511,89 @@ The problems cleaned up are:
 
 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
 documentation."
-  (interactive "@*r")
-  (let ((rstart           (min start end))
-       (rend             (copy-marker (max start end)))
-       (indent-tabs-mode whitespace-indent-tabs-mode)
-       (tab-width        whitespace-tab-width)
-       overwrite-mode                  ; enforce no overwrite
-       tmp)
-    (save-excursion
-      (save-match-data
-       ;; PROBLEM 1: 8 or more SPACEs at bol
-       (cond
-        ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
-        ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
-        ;; SPACEs.
-        ((memq 'indentation whitespace-style)
-         (let ((regexp (whitespace-indentation-regexp)))
-           (goto-char rstart)
-           (while (re-search-forward regexp rend t)
-             (setq tmp (current-indentation))
-             (goto-char (match-beginning 0))
-             (delete-horizontal-space)
-             (unless (eolp)
-               (indent-to tmp)))))
-        ;; ACTION: replace 8 or more SPACEs at bol by TABs.
-        ((memq 'indentation::tab whitespace-style)
-         (whitespace-replace-action
-          'tabify rstart rend
-          (whitespace-indentation-regexp 'tab) 0))
-        ;; ACTION: replace TABs by SPACEs.
-        ((memq 'indentation::space whitespace-style)
-         (whitespace-replace-action
-          'untabify rstart rend
-          (whitespace-indentation-regexp 'space) 0)))
-       ;; PROBLEM 3: SPACEs or TABs at eol
-       ;; ACTION: remove all SPACEs or TABs at eol
-       (when (memq 'trailing whitespace-style)
-         (whitespace-replace-action
-          'delete-region rstart rend
-          whitespace-trailing-regexp 1))
-       ;; PROBLEM 4: 8 or more SPACEs after TAB
-       (cond
-        ;; ACTION: replace 8 or more SPACEs by TABs, if
-        ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
-        ;; SPACEs.
-        ((memq 'space-after-tab whitespace-style)
-         (whitespace-replace-action
-          (if whitespace-indent-tabs-mode 'tabify 'untabify)
-          rstart rend (whitespace-space-after-tab-regexp) 1))
-        ;; ACTION: replace 8 or more SPACEs by TABs.
-        ((memq 'space-after-tab::tab whitespace-style)
-         (whitespace-replace-action
-          'tabify rstart rend
-          (whitespace-space-after-tab-regexp 'tab) 1))
-        ;; ACTION: replace TABs by SPACEs.
-        ((memq 'space-after-tab::space whitespace-style)
-         (whitespace-replace-action
-          'untabify rstart rend
-          (whitespace-space-after-tab-regexp 'space) 1)))
-       ;; PROBLEM 2: SPACEs before TAB
-       (cond
-        ;; ACTION: replace SPACEs before TAB by TABs, if
-        ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
-        ;; SPACEs.
-        ((memq 'space-before-tab whitespace-style)
-         (whitespace-replace-action
-          (if whitespace-indent-tabs-mode 'tabify 'untabify)
-          rstart rend whitespace-space-before-tab-regexp
-          (if whitespace-indent-tabs-mode 1 2)))
-        ;; 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))
-        ;; ACTION: replace TABs by SPACEs.
-        ((memq 'space-before-tab::space whitespace-style)
-         (whitespace-replace-action
-          'untabify rstart rend
-          whitespace-space-before-tab-regexp 2)))))
-    (set-marker rend nil)))            ; point marker to nowhere
+  (interactive "@r")
+  (if buffer-read-only
+      ;; read-only buffer
+      (whitespace-warn-read-only "cleanup region")
+    ;; non-read-only buffer
+    (let ((rstart           (min start end))
+         (rend             (copy-marker (max start end)))
+         (indent-tabs-mode whitespace-indent-tabs-mode)
+         (tab-width        whitespace-tab-width)
+         overwrite-mode                ; enforce no overwrite
+         tmp)
+      (save-excursion
+       (save-match-data
+         ;; PROBLEM 1: 8 or more SPACEs at bol
+         (cond
+          ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
+          ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
+          ;; by SPACEs.
+          ((memq 'indentation whitespace-style)
+           (let ((regexp (whitespace-indentation-regexp)))
+             (goto-char rstart)
+             (while (re-search-forward regexp rend t)
+               (setq tmp (current-indentation))
+               (goto-char (match-beginning 0))
+               (delete-horizontal-space)
+               (unless (eolp)
+                 (indent-to tmp)))))
+          ;; ACTION: replace 8 or more SPACEs at bol by TABs.
+          ((memq 'indentation::tab whitespace-style)
+           (whitespace-replace-action
+            'tabify rstart rend
+            (whitespace-indentation-regexp 'tab) 0))
+          ;; ACTION: replace TABs by SPACEs.
+          ((memq 'indentation::space whitespace-style)
+           (whitespace-replace-action
+            'untabify rstart rend
+            (whitespace-indentation-regexp 'space) 0)))
+         ;; PROBLEM 3: SPACEs or TABs at eol
+         ;; ACTION: remove all SPACEs or TABs at eol
+         (when (memq 'trailing whitespace-style)
+           (whitespace-replace-action
+            'delete-region rstart rend
+            whitespace-trailing-regexp 1))
+         ;; PROBLEM 4: 8 or more SPACEs after TAB
+         (cond
+          ;; ACTION: replace 8 or more SPACEs by TABs, if
+          ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
+          ;; by SPACEs.
+          ((memq 'space-after-tab whitespace-style)
+           (whitespace-replace-action
+            (if whitespace-indent-tabs-mode 'tabify 'untabify)
+            rstart rend (whitespace-space-after-tab-regexp) 1))
+          ;; ACTION: replace 8 or more SPACEs by TABs.
+          ((memq 'space-after-tab::tab whitespace-style)
+           (whitespace-replace-action
+            'tabify rstart rend
+            (whitespace-space-after-tab-regexp 'tab) 1))
+          ;; ACTION: replace TABs by SPACEs.
+          ((memq 'space-after-tab::space whitespace-style)
+           (whitespace-replace-action
+            'untabify rstart rend
+            (whitespace-space-after-tab-regexp 'space) 1)))
+         ;; PROBLEM 2: SPACEs before TAB
+         (cond
+          ;; ACTION: replace SPACEs before TAB by TABs, if
+          ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
+          ;; by SPACEs.
+          ((memq 'space-before-tab whitespace-style)
+           (whitespace-replace-action
+            (if whitespace-indent-tabs-mode 'tabify 'untabify)
+            rstart rend whitespace-space-before-tab-regexp
+            (if whitespace-indent-tabs-mode 1 2)))
+          ;; 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))
+          ;; ACTION: replace TABs by SPACEs.
+          ((memq 'space-before-tab::space whitespace-style)
+           (whitespace-replace-action
+            'untabify rstart rend
+            whitespace-space-before-tab-regexp 2)))))
+      (set-marker rend nil))))         ; point marker to nowhere
 
 
 (defun whitespace-replace-action (action rstart rend regexp index)
@@ -2323,21 +2403,28 @@ It should be added buffer-locally to `write-file-functions'."
 (defun whitespace-kill-buffer-hook ()
   "Action to be taken when buffer is killed.
 It should be added buffer-locally to `kill-buffer-hook'."
-  (whitespace-action)
+  (whitespace-action t)
   nil)                                 ; continue hook processing
 
 
-(defun whitespace-action ()
+(defun whitespace-action (&optional is-killing-buffer)
   "Action to be taken when buffer is killed or written.
 Return t when the action should be aborted."
   (cond ((memq 'auto-cleanup whitespace-action)
-        (whitespace-cleanup)
+        (unless is-killing-buffer
+          (whitespace-cleanup))
         nil)
        ((memq 'abort-on-bogus whitespace-action)
         (whitespace-report nil t))
        (t
         nil)))
 
+
+(defun whitespace-warn-read-only (msg)
+  "Warn if buffer is read-only."
+  (when (memq 'warn-read-only whitespace-action)
+    (message "Can't %s: %s is read-only" msg (buffer-name))))
+
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;