comparison lisp/whitespace.el @ 92372:0418e6ff8eb2

New version 9.3.
author Vinicius Jose Latorre <viniciusjl@ig.com.br>
date Sat, 01 Mar 2008 19:00:24 +0000
parents 13eacee3408e
children 7b3262e9986b
comparison
equal deleted inserted replaced
92371:d442efe2d5a7 92372:0418e6ff8eb2
4 ;; Free Software Foundation, Inc. 4 ;; Free Software Foundation, Inc.
5 5
6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br> 6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br> 7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
8 ;; Keywords: data, wp 8 ;; Keywords: data, wp
9 ;; Version: 9.2 9 ;; Version: 9.3
10 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre 10 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
11 11
12 ;; This file is part of GNU Emacs. 12 ;; This file is part of GNU Emacs.
13 13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify 14 ;; GNU Emacs is free software; you can redistribute it and/or modify
160 ;; 160 ;;
161 ;; M-x global-whitespace-mode RET 161 ;; M-x global-whitespace-mode RET
162 ;; 162 ;;
163 ;; There are also the following useful commands: 163 ;; There are also the following useful commands:
164 ;; 164 ;;
165 ;; `whitespace-report'
166 ;; Report some blank problems in buffer.
167 ;;
168 ;; `whitespace-report-region'
169 ;; Report some blank problems in a region.
170 ;;
165 ;; `whitespace-cleanup' 171 ;; `whitespace-cleanup'
166 ;; Cleanup some blank problems in all buffer or at region. 172 ;; Cleanup some blank problems in all buffer or at region.
167 ;; 173 ;;
168 ;; `whitespace-cleanup-region' 174 ;; `whitespace-cleanup-region'
169 ;; Cleanup some blank problems at region. 175 ;; Cleanup some blank problems at region.
170 ;;
171 ;; `whitespace-buffer'
172 ;; Turn on `whitespace-mode' forcing some settings.
173 ;; 176 ;;
174 ;; The problems, which are cleaned up, are: 177 ;; The problems, which are cleaned up, are:
175 ;; 178 ;;
176 ;; 1. empty lines at beginning of buffer. 179 ;; 1. empty lines at beginning of buffer.
177 ;; 2. empty lines at end of buffer. 180 ;; 2. empty lines at end of buffer.
186 ;; If `whitespace-chars' includes the value `space-before-tab', 189 ;; If `whitespace-chars' includes the value `space-before-tab',
187 ;; replace SPACEs by TABs. 190 ;; replace SPACEs by TABs.
188 ;; 191 ;;
189 ;; 5. SPACEs or TABs at end of line. 192 ;; 5. SPACEs or TABs at end of line.
190 ;; If `whitespace-chars' includes the value `trailing', remove all 193 ;; If `whitespace-chars' includes the value `trailing', remove all
191 ;; SPACEs or TABs at end of line." 194 ;; SPACEs or TABs at end of line.
192 ;; 195 ;;
193 ;; 6. 8 or more SPACEs after TAB. 196 ;; 6. 8 or more SPACEs after TAB.
194 ;; If `whitespace-chars' includes the value `space-after-tab', 197 ;; If `whitespace-chars' includes the value `space-after-tab',
195 ;; replace SPACEs by TABs. 198 ;; replace SPACEs by TABs.
196 ;; 199 ;;
278 ;; 281 ;;
279 ;; `whitespace-global-modes' Modes for which global 282 ;; `whitespace-global-modes' Modes for which global
280 ;; `whitespace-mode' is automagically 283 ;; `whitespace-mode' is automagically
281 ;; turned on. 284 ;; turned on.
282 ;; 285 ;;
286 ;; `whitespace-action' Specify which action is taken when a
287 ;; buffer is visited, killed or written.
288 ;;
283 ;; 289 ;;
284 ;; Acknowledgements 290 ;; Acknowledgements
285 ;; ---------------- 291 ;; ----------------
292 ;;
293 ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook actions
294 ;; when buffer is written or killed as the original whitespace package had.
286 ;; 295 ;;
287 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long" 296 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
288 ;; lines tail. See EightyColumnRule (EmacsWiki). 297 ;; lines tail. See EightyColumnRule (EmacsWiki).
289 ;; 298 ;;
290 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting: 299 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
784 :group 'whitespace) 793 :group 'whitespace)
785 794
786 795
787 ;; Hacked from `visible-whitespace-mappings' in visws.el 796 ;; Hacked from `visible-whitespace-mappings' in visws.el
788 (defcustom whitespace-display-mappings 797 (defcustom whitespace-display-mappings
789 ;; Due to limitations of glyph representation, the char code can not
790 ;; be above ?\x1FFFF. Probably, this will be fixed after Emacs
791 ;; unicode merging.
792 '( 798 '(
793 (?\ [?\xB7] [?.]) ; space - centered dot 799 (?\ [?\xB7] [?.]) ; space - centered dot
794 (?\xA0 [?\xA4] [?_]) ; hard space - currency 800 (?\xA0 [?\xA4] [?_]) ; hard space - currency
795 (?\x8A0 [?\x8A4] [?_]) ; hard space - currency 801 (?\x8A0 [?\x8A4] [?_]) ; hard space - currency
796 (?\x920 [?\x924] [?_]) ; hard space - currency 802 (?\x920 [?\x924] [?_]) ; hard space - currency
797 (?\xE20 [?\xE24] [?_]) ; hard space - currency 803 (?\xE20 [?\xE24] [?_]) ; hard space - currency
798 (?\xF20 [?\xF24] [?_]) ; hard space - currency 804 (?\xF20 [?\xF24] [?_]) ; hard space - currency
799 ;; NEWLINE is displayed using the face `whitespace-newline' 805 ;; NEWLINE is displayed using the face `whitespace-newline'
800 (?\n [?$ ?\n]) ; end-of-line - dollar sign 806 (?\n [?\u21B5 ?\n] [?$ ?\n]) ; end-of-line - downwards arrow
801 ;; (?\n [?\u21B5 ?\n] [?$ ?\n]) ; end-of-line - downwards arrow 807 ;; (?\n [?$ ?\n]) ; end-of-line - dollar sign
802 ;; (?\n [?\xB6 ?\n] [?$ ?\n]) ; end-of-line - pilcrow 808 ;; (?\n [?\xB6 ?\n] [?$ ?\n]) ; end-of-line - pilcrow
803 ;; (?\n [?\x8AF ?\n] [?$ ?\n]) ; end-of-line - overscore 809 ;; (?\n [?\x8AF ?\n] [?$ ?\n]) ; end-of-line - overscore
804 ;; (?\n [?\x8AC ?\n] [?$ ?\n]) ; end-of-line - negation 810 ;; (?\n [?\x8AC ?\n] [?$ ?\n]) ; end-of-line - negation
805 ;; (?\n [?\x8B0 ?\n] [?$ ?\n]) ; end-of-line - grade 811 ;; (?\n [?\x8B0 ?\n] [?$ ?\n]) ; end-of-line - grade
806 ;; 812 ;;
861 867
862 (c-mode c++-mode) 868 (c-mode c++-mode)
863 869
864 means that `whitespace-mode' is turned on for buffers in C and 870 means that `whitespace-mode' is turned on for buffers in C and
865 C++ modes only." 871 C++ modes only."
866 :type '(choice (const :tag "None" nil) 872 :type '(choice :tag "Global Modes"
873 (const :tag "None" nil)
867 (const :tag "All" t) 874 (const :tag "All" t)
868 (set :menu-tag "Mode Specific" :tag "Modes" 875 (set :menu-tag "Mode Specific" :tag "Modes"
869 :value (not) 876 :value (not)
870 (const :tag "Except" not) 877 (const :tag "Except" not)
871 (repeat :inline t 878 (repeat :inline t
872 (symbol :tag "Mode")))) 879 (symbol :tag "Mode"))))
880 :group 'whitespace)
881
882
883 (defcustom whitespace-action nil
884 "*Specify which action is taken when a buffer is visited, killed or written.
885
886 It's a list containing some or all of the following values:
887
888 nil no action is taken.
889
890 cleanup cleanup any bogus whitespace always when local
891 whitespace is turned on.
892 See `whitespace-cleanup' and
893 `whitespace-cleanup-region'.
894
895 report-on-bogus report if there is any bogus whitespace always
896 when local whitespace is turned on.
897
898 auto-cleanup cleanup any bogus whitespace when buffer is
899 written or killed.
900 See `whitespace-cleanup' and
901 `whitespace-cleanup-region'.
902
903 abort-on-bogus abort if there is any bogus whitespace and the
904 buffer is written or killed.
905
906 Any other value is treated as nil."
907 :type '(choice :tag "Actions"
908 (const :tag "None" nil)
909 (repeat :tag "Action List"
910 (choice :tag "Action"
911 (const :tag "Cleanup When On" cleanup)
912 (const :tag "Report On Bogus" report-on-bogus)
913 (const :tag "Auto Cleanup" auto-cleanup)
914 (const :tag "Abort On Bogus" abort-on-bogus))))
873 :group 'whitespace) 915 :group 'whitespace)
874 916
875 917
876 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 918 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
877 ;;;; User commands - Local mode 919 ;;;; User commands - Local mode
891 :group 'whitespace 933 :group 'whitespace
892 (cond 934 (cond
893 (noninteractive ; running a batch job 935 (noninteractive ; running a batch job
894 (setq whitespace-mode nil)) 936 (setq whitespace-mode nil))
895 (whitespace-mode ; whitespace-mode on 937 (whitespace-mode ; whitespace-mode on
896 (whitespace-turn-on)) 938 (whitespace-turn-on)
939 (whitespace-action-when-on))
897 (t ; whitespace-mode off 940 (t ; whitespace-mode off
898 (whitespace-turn-off)))) 941 (whitespace-turn-off))))
899 942
900 943
901 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 944 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
916 (cond 959 (cond
917 (noninteractive ; running a batch job 960 (noninteractive ; running a batch job
918 (setq global-whitespace-mode nil)) 961 (setq global-whitespace-mode nil))
919 (global-whitespace-mode ; global-whitespace-mode on 962 (global-whitespace-mode ; global-whitespace-mode on
920 (save-excursion 963 (save-excursion
921 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled t) 964 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
922 (dolist (buffer (buffer-list)) ; adjust all local mode 965 (dolist (buffer (buffer-list)) ; adjust all local mode
923 (set-buffer buffer) 966 (set-buffer buffer)
924 (unless whitespace-mode 967 (unless whitespace-mode
925 (whitespace-turn-on-if-enabled))))) 968 (whitespace-turn-on-if-enabled)))))
926 (t ; global-whitespace-mode off 969 (t ; global-whitespace-mode off
1257 (when (memq 'indentation whitespace-chars) 1300 (when (memq 'indentation whitespace-chars)
1258 (goto-char rstart) 1301 (goto-char rstart)
1259 (while (re-search-forward 1302 (while (re-search-forward
1260 whitespace-indentation-regexp rend t) 1303 whitespace-indentation-regexp rend t)
1261 (setq tmp (current-indentation)) 1304 (setq tmp (current-indentation))
1305 (goto-char (match-beginning 0))
1262 (delete-horizontal-space) 1306 (delete-horizontal-space)
1263 (unless (eolp) 1307 (unless (eolp)
1264 (indent-to tmp)))) 1308 (indent-to tmp))))
1265 ;; problem 3: SPACEs or TABs at eol 1309 ;; problem 3: SPACEs or TABs at eol
1266 ;; action: remove all SPACEs or TABs at eol 1310 ;; action: remove all SPACEs or TABs at eol
1267 (when (memq 'trailing whitespace-chars) 1311 (when (memq 'trailing whitespace-chars)
1268 (let ((regexp (concat "\\(\\(" whitespace-trailing-regexp 1312 (let ((regexp (whitespace-trailing-regexp)))
1269 "\\)+\\)$")))
1270 (goto-char rstart) 1313 (goto-char rstart)
1271 (while (re-search-forward regexp rend t) 1314 (while (re-search-forward regexp rend t)
1272 (delete-region (match-beginning 1) (match-end 1))))) 1315 (delete-region (match-beginning 1) (match-end 1)))))
1273 ;; problem 4: 8 or more SPACEs after TAB 1316 ;; problem 4: 8 or more SPACEs after TAB
1274 ;; action: replace 8 or more SPACEs by TABs 1317 ;; action: replace 8 or more SPACEs by TABs
1298 (- scol (% scol 8))) ; prev start col 1341 (- scol (% scol 8))) ; prev start col
1299 8))))) 1342 8)))))
1300 1343
1301 1344
1302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1303 ;;;; User command - old whitespace compatibility 1346 ;;;; User command - report
1347
1348
1349 (defun whitespace-trailing-regexp ()
1350 "Make the `whitespace-trailing-regexp' regexp."
1351 (concat "\\(\\(" whitespace-trailing-regexp "\\)+\\)$"))
1352
1353
1354 (defconst whitespace-report-list
1355 (list
1356 (cons 'empty whitespace-empty-at-bob-regexp)
1357 (cons 'empty whitespace-empty-at-eob-regexp)
1358 (cons 'indentation whitespace-indentation-regexp)
1359 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1360 (cons 'trailing (whitespace-trailing-regexp))
1361 (cons 'space-after-tab whitespace-space-after-tab-regexp)
1362 )
1363 "List of whitespace bogus symbol and corresponding regexp.")
1364
1365
1366 (defconst whitespace-report-text
1367 "\
1368 Whitespace Report
1369
1370 Current Setting Whitespace Problem
1371
1372 empty [] [] empty lines at beginning of buffer.
1373 empty [] [] empty lines at end of buffer.
1374 indentation [] [] 8 or more SPACEs at beginning of line.
1375 space-before-tab [] [] SPACEs before TAB.
1376 trailing [] [] SPACEs or TABs at end of line.
1377 space-after-tab [] [] 8 or more SPACEs after TAB.\n\n"
1378 "Text for whitespace bogus report.")
1379
1380
1381 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1382 "The buffer name for whitespace bogus report.")
1304 1383
1305 1384
1306 ;;;###autoload 1385 ;;;###autoload
1307 (defun whitespace-buffer () 1386 (defun whitespace-report (&optional force report-if-bogus)
1308 "Turn on `whitespace-mode' forcing some settings. 1387 "Report some whitespace problems in buffer.
1309 1388
1310 It forces `whitespace-style' to have `color'. 1389 Return nil if there is no whitespace problem; otherwise, return
1311 1390 non-nil.
1312 It also forces `whitespace-chars' to have: 1391
1313 1392 If FORCE is non-nil or \\[universal-argument] was pressed just before calling
1314 trailing 1393 `whitespace-report' interactively, it forces `whitespace-chars' to
1394 have:
1395
1396 empty
1315 indentation 1397 indentation
1316 space-before-tab 1398 space-before-tab
1317 empty 1399 trailing
1318 space-after-tab 1400 space-after-tab
1319 1401
1320 So, it is possible to visualize the following problems: 1402 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1403 whitespace problems in buffer.
1404
1405 Report if some of the following whitespace problems exist:
1321 1406
1322 empty 1. empty lines at beginning of buffer. 1407 empty 1. empty lines at beginning of buffer.
1323 empty 2. empty lines at end of buffer. 1408 empty 2. empty lines at end of buffer.
1324 indentation 3. 8 or more SPACEs at beginning of line. 1409 indentation 3. 8 or more SPACEs at beginning of line.
1325 space-before-tab 4. SPACEs before TAB. 1410 space-before-tab 4. SPACEs before TAB.
1327 space-after-tab 6. 8 or more SPACEs after TAB. 1412 space-after-tab 6. 8 or more SPACEs after TAB.
1328 1413
1329 See `whitespace-chars' and `whitespace-style' for documentation. 1414 See `whitespace-chars' and `whitespace-style' for documentation.
1330 See also `whitespace-cleanup' and `whitespace-cleanup-region' for 1415 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1331 cleaning up these problems." 1416 cleaning up these problems."
1332 (interactive) 1417 (interactive (list current-prefix-arg))
1333 (whitespace-mode 0) ; assure is off 1418 (whitespace-report-region (point-min) (point-max)
1334 ;; keep original values 1419 force report-if-bogus))
1335 (let ((whitespace-style (copy-sequence whitespace-style)) 1420
1336 (whitespace-chars (copy-sequence whitespace-chars))) 1421
1337 ;; adjust options for whitespace bogus blanks 1422 ;;;###autoload
1338 (add-to-list 'whitespace-style 'color) 1423 (defun whitespace-report-region (start end &optional force report-if-bogus)
1339 (mapc #'(lambda (option) 1424 "Report some whitespace problems in a region.
1340 (add-to-list 'whitespace-chars option)) 1425
1341 '(trailing 1426 Return nil if there is no whitespace problem; otherwise, return
1342 indentation 1427 non-nil.
1343 space-before-tab 1428
1344 empty 1429 If FORCE is non-nil or \\[universal-argument] was pressed just before calling
1345 space-after-tab)) 1430 `whitespace-report-region' interactively, it forces `whitespace-chars'
1346 (whitespace-mode 1))) ; turn on 1431 to have:
1432
1433 empty
1434 indentation
1435 space-before-tab
1436 trailing
1437 space-after-tab
1438
1439 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1440 whitespace problems in buffer.
1441
1442 Report if some of the following whitespace problems exist:
1443
1444 empty 1. empty lines at beginning of buffer.
1445 empty 2. empty lines at end of buffer.
1446 indentation 3. 8 or more SPACEs at beginning of line.
1447 space-before-tab 4. SPACEs before TAB.
1448 trailing 5. SPACEs or TABs at end of line.
1449 space-after-tab 6. 8 or more SPACEs after TAB.
1450
1451 See `whitespace-chars' and `whitespace-style' for documentation.
1452 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1453 cleaning up these problems."
1454 (interactive "r")
1455 (setq force (or current-prefix-arg force))
1456 (save-excursion
1457 (save-match-data
1458 (let* (has-bogus
1459 (rstart (min start end))
1460 (rend (max start end))
1461 (bogus-list (mapcar
1462 #'(lambda (option)
1463 (when force
1464 (add-to-list 'whitespace-chars (car option)))
1465 (goto-char rstart)
1466 (and (re-search-forward (cdr option) rend t)
1467 (setq has-bogus t)))
1468 whitespace-report-list)))
1469 (when (if report-if-bogus has-bogus t)
1470 (with-current-buffer (get-buffer-create
1471 whitespace-report-buffer-name)
1472 (erase-buffer)
1473 (insert whitespace-report-text)
1474 (goto-char (point-min))
1475 (forward-line 3)
1476 (dolist (option whitespace-report-list)
1477 (forward-line 1)
1478 (whitespace-mark-x 22 (memq (car option) whitespace-chars))
1479 (whitespace-mark-x 7 (car bogus-list))
1480 (setq bogus-list (cdr bogus-list)))
1481 (when has-bogus
1482 (goto-char (point-max))
1483 (insert " Type `M-x whitespace-cleanup'"
1484 " to cleanup the buffer.\n\n")
1485 (insert " Type `M-x whitespace-cleanup-region'"
1486 " to cleanup a region.\n\n"))
1487 (whitespace-display-window (current-buffer))))
1488 has-bogus))))
1347 1489
1348 1490
1349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1491 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1350 ;;;; Internal functions 1492 ;;;; Internal functions
1351 1493
1390 1532
1391 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*" 1533 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1392 "The buffer name for whitespace toggle options.") 1534 "The buffer name for whitespace toggle options.")
1393 1535
1394 1536
1537 (defun whitespace-mark-x (nchars condition)
1538 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1539 (forward-char nchars)
1540 (insert (if condition "X" " ")))
1541
1542
1395 (defun whitespace-insert-option-mark (the-list the-value) 1543 (defun whitespace-insert-option-mark (the-list the-value)
1396 "Insert the option mark ('X' or ' ') in toggle options buffer." 1544 "Insert the option mark ('X' or ' ') in toggle options buffer."
1397 (forward-line 1) 1545 (forward-line 1)
1398 (dolist (sym the-list) 1546 (dolist (sym the-list)
1399 (forward-line 1) 1547 (forward-line 1)
1400 (forward-char 2) 1548 (whitespace-mark-x 2 (memq sym the-value))))
1401 (insert (if (memq sym the-value) "X" " "))))
1402 1549
1403 1550
1404 (defun whitespace-help-on (chars style) 1551 (defun whitespace-help-on (chars style)
1405 "Display the whitespace toggle options." 1552 "Display the whitespace toggle options."
1406 (unless (get-buffer whitespace-help-buffer-name) 1553 (unless (get-buffer whitespace-help-buffer-name)
1413 (goto-char (point-min)) 1560 (goto-char (point-min))
1414 (whitespace-insert-option-mark 1561 (whitespace-insert-option-mark
1415 whitespace-chars-value-list chars) 1562 whitespace-chars-value-list chars)
1416 (whitespace-insert-option-mark 1563 (whitespace-insert-option-mark
1417 whitespace-style-value-list style) 1564 whitespace-style-value-list style)
1418 (goto-char (point-min)) 1565 (whitespace-display-window buffer)))))
1419 (set-buffer-modified-p nil) 1566
1420 (let ((size (- (window-height) 1567
1421 (max window-min-height 1568 (defun whitespace-display-window (buffer)
1422 (1+ (count-lines (point-min) 1569 "Display BUFFER in a new window."
1423 (point-max))))))) 1570 (goto-char (point-min))
1424 (when (<= size 0) 1571 (set-buffer-modified-p nil)
1425 (kill-buffer buffer) 1572 (let ((size (- (window-height)
1426 (error "Frame height is too small; \ 1573 (max window-min-height
1574 (1+ (count-lines (point-min)
1575 (point-max)))))))
1576 (when (<= size 0)
1577 (kill-buffer buffer)
1578 (error "Frame height is too small; \
1427 can't split window to display whitespace toggle options")) 1579 can't split window to display whitespace toggle options"))
1428 (set-window-buffer (split-window nil size) buffer)))))) 1580 (set-window-buffer (split-window nil size) buffer)))
1429 1581
1430 1582
1431 (defun whitespace-help-off () 1583 (defun whitespace-help-off ()
1432 "Remove the buffer and window of the whitespace toggle options." 1584 "Remove the buffer and window of the whitespace toggle options."
1433 (let ((buffer (get-buffer whitespace-help-buffer-name))) 1585 (let ((buffer (get-buffer whitespace-help-buffer-name)))
1536 the-list) 1688 the-list)
1537 1689
1538 1690
1539 (defun whitespace-turn-on () 1691 (defun whitespace-turn-on ()
1540 "Turn on whitespace visualization." 1692 "Turn on whitespace visualization."
1693 (whitespace-add-local-hook)
1541 (setq whitespace-active-style (if (listp whitespace-style) 1694 (setq whitespace-active-style (if (listp whitespace-style)
1542 whitespace-style 1695 whitespace-style
1543 (list whitespace-style))) 1696 (list whitespace-style)))
1544 (setq whitespace-active-chars (if (listp whitespace-chars) 1697 (setq whitespace-active-chars (if (listp whitespace-chars)
1545 whitespace-chars 1698 whitespace-chars
1550 (whitespace-display-char-on))) 1703 (whitespace-display-char-on)))
1551 1704
1552 1705
1553 (defun whitespace-turn-off () 1706 (defun whitespace-turn-off ()
1554 "Turn off whitespace visualization." 1707 "Turn off whitespace visualization."
1708 (whitespace-remove-local-hook)
1555 (when (memq 'color whitespace-active-style) 1709 (when (memq 'color whitespace-active-style)
1556 (whitespace-color-off)) 1710 (whitespace-color-off))
1557 (when (memq 'mark whitespace-active-style) 1711 (when (memq 'mark whitespace-active-style)
1558 (whitespace-display-char-off))) 1712 (whitespace-display-char-off)))
1559 1713
1588 (when (memq 'trailing whitespace-active-chars) 1742 (when (memq 'trailing whitespace-active-chars)
1589 (font-lock-add-keywords 1743 (font-lock-add-keywords
1590 nil 1744 nil
1591 (list 1745 (list
1592 ;; Show trailing blanks 1746 ;; Show trailing blanks
1593 (list (concat "\\(\\(" whitespace-trailing-regexp "\\)+\\)$") 1747 (list (whitespace-trailing-regexp) 1 whitespace-trailing t))
1594 1 whitespace-trailing t))
1595 t)) 1748 t))
1596 (when (or (memq 'lines whitespace-active-chars) 1749 (when (or (memq 'lines whitespace-active-chars)
1597 (memq 'lines-tail whitespace-active-chars)) 1750 (memq 'lines-tail whitespace-active-chars))
1598 (font-lock-add-keywords 1751 (font-lock-add-keywords
1599 nil 1752 nil
1725 ;; Only insert face bits on NEWLINE char mapping to avoid 1878 ;; Only insert face bits on NEWLINE char mapping to avoid
1726 ;; obstruction of other faces like TABs and (HARD) SPACEs 1879 ;; obstruction of other faces like TABs and (HARD) SPACEs
1727 ;; faces, font-lock faces, etc. 1880 ;; faces, font-lock faces, etc.
1728 (when (memq 'color whitespace-active-style) 1881 (when (memq 'color whitespace-active-style)
1729 (dotimes (i (length vec)) 1882 (dotimes (i (length vec))
1730 ;; Due to limitations of glyph representation, the char
1731 ;; code can not be above ?\x1FFFF. Probably, this will
1732 ;; be fixed after Emacs unicode merging.
1733 (or (eq (aref vec i) ?\n) 1883 (or (eq (aref vec i) ?\n)
1734 (> (aref vec i) #x1FFFF)
1735 (aset vec i 1884 (aset vec i
1736 (make-glyph-code (aref vec i) 1885 (make-glyph-code (aref vec i)
1737 whitespace-newline))))) 1886 whitespace-newline)))))
1738 ;; Display mapping 1887 ;; Display mapping
1739 (aset buffer-display-table (car entry) vec)) 1888 (aset buffer-display-table (car entry) vec))
1750 (setq whitespace-display-table-was-local nil 1899 (setq whitespace-display-table-was-local nil
1751 buffer-display-table whitespace-display-table))) 1900 buffer-display-table whitespace-display-table)))
1752 1901
1753 1902
1754 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1903 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1904 ;;;; Hook
1905
1906
1907 (defun whitespace-action-when-on ()
1908 "Action to be taken always when local whitespace is turned on."
1909 (cond ((memq 'cleanup whitespace-action)
1910 (whitespace-cleanup))
1911 ((memq 'report-on-bogus whitespace-action)
1912 (whitespace-report nil t))))
1913
1914
1915 (defun whitespace-add-local-hook ()
1916 "Add some whitespace hooks locally."
1917 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
1918 (add-hook 'kill-buffer-hook 'whitespace-kill-buffer-hook nil t))
1919
1920
1921 (defun whitespace-remove-local-hook ()
1922 "Remove some whitespace hooks locally."
1923 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
1924 (remove-hook 'kill-buffer-hook 'whitespace-kill-buffer-hook t))
1925
1926
1927 (defun whitespace-write-file-hook ()
1928 "Action to be taken when buffer is written.
1929 It should be added buffer-locally to `write-file-functions'."
1930 (when (whitespace-action)
1931 (error "Abort write due to whitespace problems in %s"
1932 (buffer-name)))
1933 nil) ; continue hook processing
1934
1935
1936 (defun whitespace-kill-buffer-hook ()
1937 "Action to be taken when buffer is killed.
1938 It should be added buffer-locally to `kill-buffer-hook'."
1939 (whitespace-action)
1940 nil) ; continue hook processing
1941
1942
1943 (defun whitespace-action ()
1944 "Action to be taken when buffer is killed or written.
1945 Return t when the action should be aborted."
1946 (cond ((memq 'auto-cleanup whitespace-action)
1947 (whitespace-cleanup)
1948 nil)
1949 ((memq 'abort-on-bogus whitespace-action)
1950 (whitespace-report nil t))
1951 (t
1952 nil)))
1953
1954
1955 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1755 1956
1756 1957
1757 (defun whitespace-unload-function () 1958 (defun whitespace-unload-function ()
1758 "Unload the Whitespace library." 1959 "Unload the whitespace library."
1759 (let (whitespace-mode) ;; so g-w-m thinks it is nil in all buffers 1960 (global-whitespace-mode -1)
1760 (global-whitespace-mode -1)) 1961 ;; be sure all local whitespace mode is turned off
1761 ;; continue standard unloading 1962 (save-current-buffer
1762 nil) 1963 (dolist (buf (buffer-list))
1964 (set-buffer buf)
1965 (whitespace-mode -1)))
1966 nil) ; continue standard unloading
1967
1763 1968
1764 (provide 'whitespace) 1969 (provide 'whitespace)
1765 1970
1766 1971
1767 (run-hooks 'whitespace-load-hook) 1972 (run-hooks 'whitespace-load-hook)