Mercurial > emacs
comparison lisp/whitespace.el @ 109903:3ea5c230ba38
Fix slow cursor movement.
author | Vinicius Jose Latorre <viniciusjl@ig.com.br |
---|---|
date | Sat, 21 Aug 2010 01:43:04 -0300 |
parents | 904ccd8f2acb |
children | e49f8c56fca8 |
comparison
equal
deleted
inserted
replaced
109902:df9d0b151262 | 109903:3ea5c230ba38 |
---|---|
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: 12.1 | 9 ;; Version: 13.0 |
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 |
810 :type '(cons (regexp :tag "Indentation SPACEs") | 810 :type '(cons (regexp :tag "Indentation SPACEs") |
811 (regexp :tag "Indentation TABs")) | 811 (regexp :tag "Indentation TABs")) |
812 :group 'whitespace) | 812 :group 'whitespace) |
813 | 813 |
814 | 814 |
815 (defcustom whitespace-empty-at-bob-regexp "\\`\\(\\([ \t]*\n\\)+\\)" | 815 (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)" |
816 "Specify regexp for empty lines at beginning of buffer. | 816 "Specify regexp for empty lines at beginning of buffer. |
817 | 817 |
818 If you're using `mule' package, there may be other characters besides: | 818 If you're using `mule' package, there may be other characters besides: |
819 | 819 |
820 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | 820 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ |
825 Used when `whitespace-style' includes `empty'." | 825 Used when `whitespace-style' includes `empty'." |
826 :type '(regexp :tag "Empty Lines At Beginning Of Buffer") | 826 :type '(regexp :tag "Empty Lines At Beginning Of Buffer") |
827 :group 'whitespace) | 827 :group 'whitespace) |
828 | 828 |
829 | 829 |
830 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)\\'" | 830 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)" |
831 "Specify regexp for empty lines at end of buffer. | 831 "Specify regexp for empty lines at end of buffer. |
832 | 832 |
833 If you're using `mule' package, there may be other characters besides: | 833 If you're using `mule' package, there may be other characters besides: |
834 | 834 |
835 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | 835 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ |
1225 Used by `whitespace-trailing-regexp' function (which see).") | 1225 Used by `whitespace-trailing-regexp' function (which see).") |
1226 | 1226 |
1227 (defvar whitespace-font-lock-refontify nil | 1227 (defvar whitespace-font-lock-refontify nil |
1228 "Used to save locally the font-lock refontify state. | 1228 "Used to save locally the font-lock refontify state. |
1229 Used by `whitespace-post-command-hook' function (which see).") | 1229 Used by `whitespace-post-command-hook' function (which see).") |
1230 | |
1231 (defvar whitespace-bob-marker nil | |
1232 "Used to save locally the bob marker value. | |
1233 Used by `whitespace-post-command-hook' function (which see).") | |
1234 | |
1235 (defvar whitespace-eob-marker nil | |
1236 "Used to save locally the eob marker value. | |
1237 Used by `whitespace-post-command-hook' function (which see).") | |
1238 | |
1239 (defvar whitespace-buffer-changed nil | |
1240 "Used to indicate locally if buffer changed. | |
1241 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed' | |
1242 functions (which see).") | |
1230 | 1243 |
1231 | 1244 |
1232 ;;;###autoload | 1245 ;;;###autoload |
1233 (defun whitespace-toggle-options (arg) | 1246 (defun whitespace-toggle-options (arg) |
1234 "Toggle local `whitespace-mode' options. | 1247 "Toggle local `whitespace-mode' options. |
1461 ;; ACTION: remove all empty lines at bob and/or eob | 1474 ;; ACTION: remove all empty lines at bob and/or eob |
1462 (when (memq 'empty whitespace-style) | 1475 (when (memq 'empty whitespace-style) |
1463 (let (overwrite-mode) ; enforce no overwrite | 1476 (let (overwrite-mode) ; enforce no overwrite |
1464 (goto-char (point-min)) | 1477 (goto-char (point-min)) |
1465 (when (re-search-forward | 1478 (when (re-search-forward |
1466 whitespace-empty-at-bob-regexp nil t) | 1479 (concat "\\`" whitespace-empty-at-bob-regexp) nil t) |
1467 (delete-region (match-beginning 1) (match-end 1))) | 1480 (delete-region (match-beginning 1) (match-end 1))) |
1468 (when (re-search-forward | 1481 (when (re-search-forward |
1469 whitespace-empty-at-eob-regexp nil t) | 1482 (concat whitespace-empty-at-eob-regexp "\\'") nil t) |
1470 (delete-region (match-beginning 1) (match-end 1))))))) | 1483 (delete-region (match-beginning 1) (match-end 1))))))) |
1471 ;; PROBLEM 3: 8 or more SPACEs at bol | 1484 ;; PROBLEM 3: 8 or more SPACEs at bol |
1472 ;; PROBLEM 4: SPACEs before TAB | 1485 ;; PROBLEM 4: SPACEs before TAB |
1473 ;; PROBLEM 5: SPACEs or TABs at eol | 1486 ;; PROBLEM 5: SPACEs or TABs at eol |
1474 ;; PROBLEM 6: 8 or more SPACEs after TAB | 1487 ;; PROBLEM 6: 8 or more SPACEs after TAB |
2144 (copy-sequence font-lock-keywords))) | 2157 (copy-sequence font-lock-keywords))) |
2145 ;; save current point and refontify when necessary | 2158 ;; save current point and refontify when necessary |
2146 (set (make-local-variable 'whitespace-point) | 2159 (set (make-local-variable 'whitespace-point) |
2147 (point)) | 2160 (point)) |
2148 (set (make-local-variable 'whitespace-font-lock-refontify) | 2161 (set (make-local-variable 'whitespace-font-lock-refontify) |
2162 0) | |
2163 (set (make-local-variable 'whitespace-bob-marker) | |
2164 (point-min-marker)) | |
2165 (set (make-local-variable 'whitespace-eob-marker) | |
2166 (point-max-marker)) | |
2167 (set (make-local-variable 'whitespace-buffer-changed) | |
2149 nil) | 2168 nil) |
2150 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t) | 2169 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t) |
2170 (add-hook 'before-change-functions #'whitespace-buffer-changed nil t) | |
2151 ;; turn off font lock | 2171 ;; turn off font lock |
2152 (set (make-local-variable 'whitespace-font-lock-mode) | 2172 (set (make-local-variable 'whitespace-font-lock-mode) |
2153 font-lock-mode) | 2173 font-lock-mode) |
2154 (font-lock-mode 0) | 2174 (font-lock-mode 0) |
2155 ;; add whitespace-mode color into font lock | 2175 ;; add whitespace-mode color into font lock |
2156 (when (memq 'spaces whitespace-active-style) | 2176 (when (memq 'spaces whitespace-active-style) |
2157 (font-lock-add-keywords | 2177 (font-lock-add-keywords |
2158 nil | 2178 nil |
2159 (list | 2179 (list |
2160 ;; Show SPACEs | 2180 ;; Show SPACEs |
2161 (list #'whitespace-space-regexp 1 whitespace-space t) | 2181 (list whitespace-space-regexp 1 whitespace-space t) |
2162 ;; Show HARD SPACEs | 2182 ;; Show HARD SPACEs |
2163 (list whitespace-hspace-regexp 1 whitespace-hspace t)) | 2183 (list whitespace-hspace-regexp 1 whitespace-hspace t)) |
2164 t)) | 2184 t)) |
2165 (when (memq 'tabs whitespace-active-style) | 2185 (when (memq 'tabs whitespace-active-style) |
2166 (font-lock-add-keywords | 2186 (font-lock-add-keywords |
2167 nil | 2187 nil |
2168 (list | 2188 (list |
2169 ;; Show TABs | 2189 ;; Show TABs |
2170 (list #'whitespace-tab-regexp 1 whitespace-tab t)) | 2190 (list whitespace-tab-regexp 1 whitespace-tab t)) |
2171 t)) | 2191 t)) |
2172 (when (memq 'trailing whitespace-active-style) | 2192 (when (memq 'trailing whitespace-active-style) |
2173 (font-lock-add-keywords | 2193 (font-lock-add-keywords |
2174 nil | 2194 nil |
2175 (list | 2195 (list |
2294 (defun whitespace-color-off () | 2314 (defun whitespace-color-off () |
2295 "Turn off color visualization." | 2315 "Turn off color visualization." |
2296 ;; turn off font lock | 2316 ;; turn off font lock |
2297 (when (whitespace-style-face-p) | 2317 (when (whitespace-style-face-p) |
2298 (font-lock-mode 0) | 2318 (font-lock-mode 0) |
2299 (remove-hook 'post-command-hook #'whitespace-post-command-hook) | 2319 (remove-hook 'post-command-hook #'whitespace-post-command-hook t) |
2320 (remove-hook 'before-change-functions #'whitespace-buffer-changed t) | |
2300 (when whitespace-font-lock | 2321 (when whitespace-font-lock |
2301 (setq whitespace-font-lock nil | 2322 (setq whitespace-font-lock nil |
2302 font-lock-keywords whitespace-font-lock-keywords)) | 2323 font-lock-keywords whitespace-font-lock-keywords)) |
2303 ;; restore original font lock state | 2324 ;; restore original font lock state |
2304 (font-lock-mode whitespace-font-lock-mode))) | 2325 (font-lock-mode whitespace-font-lock-mode))) |
2315 | 2336 |
2316 | 2337 |
2317 (defun whitespace-empty-at-bob-regexp (limit) | 2338 (defun whitespace-empty-at-bob-regexp (limit) |
2318 "Match spaces at beginning of buffer which do not contain the point at \ | 2339 "Match spaces at beginning of buffer which do not contain the point at \ |
2319 beginning of buffer." | 2340 beginning of buffer." |
2320 (and (/= whitespace-point 1) | 2341 (let ((b (point)) |
2321 (re-search-forward whitespace-empty-at-bob-regexp limit t))) | 2342 r) |
2343 (cond | |
2344 ;; at bob | |
2345 ((= b 1) | |
2346 (setq r (and (/= whitespace-point 1) | |
2347 (looking-at whitespace-empty-at-bob-regexp))) | |
2348 (if r | |
2349 (set-marker whitespace-bob-marker (match-end 1)) | |
2350 (set-marker whitespace-bob-marker b))) | |
2351 ;; inside bob empty region | |
2352 ((<= limit whitespace-bob-marker) | |
2353 (setq r (looking-at whitespace-empty-at-bob-regexp)) | |
2354 (if r | |
2355 (when (< (match-end 1) limit) | |
2356 (set-marker whitespace-bob-marker (match-end 1))) | |
2357 (set-marker whitespace-bob-marker b))) | |
2358 ;; intersection with end of bob empty region | |
2359 ((<= b whitespace-bob-marker) | |
2360 (setq r (looking-at whitespace-empty-at-bob-regexp)) | |
2361 (if r | |
2362 (set-marker whitespace-bob-marker (match-end 1)) | |
2363 (set-marker whitespace-bob-marker b))) | |
2364 ;; it is not inside bob empty region | |
2365 (t | |
2366 (setq r nil))) | |
2367 ;; move to end of matching | |
2368 (and r (goto-char (match-end 1))) | |
2369 r)) | |
2370 | |
2371 | |
2372 (defsubst whitespace-looking-back (regexp limit) | |
2373 (save-excursion | |
2374 (when (/= 0 (skip-chars-backward " \t\n" limit)) | |
2375 (unless (bolp) | |
2376 (forward-line 1)) | |
2377 (looking-at regexp)))) | |
2322 | 2378 |
2323 | 2379 |
2324 (defun whitespace-empty-at-eob-regexp (limit) | 2380 (defun whitespace-empty-at-eob-regexp (limit) |
2325 "Match spaces at end of buffer which do not contain the point at end of \ | 2381 "Match spaces at end of buffer which do not contain the point at end of \ |
2326 buffer." | 2382 buffer." |
2327 (and (/= whitespace-point (1+ (buffer-size))) | 2383 (let ((b (point)) |
2328 (re-search-forward whitespace-empty-at-eob-regexp limit t))) | 2384 (e (1+ (buffer-size))) |
2329 | 2385 r) |
2330 | 2386 (cond |
2331 (defun whitespace-space-regexp (limit) | 2387 ;; at eob |
2332 "Match spaces." | 2388 ((= limit e) |
2333 (setq whitespace-font-lock-refontify t) | 2389 (when (/= whitespace-point e) |
2334 (re-search-forward whitespace-space-regexp limit t)) | 2390 (goto-char limit) |
2335 | 2391 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))) |
2336 | 2392 (if r |
2337 (defun whitespace-tab-regexp (limit) | 2393 (set-marker whitespace-eob-marker (match-beginning 1)) |
2338 "Match tabs." | 2394 (set-marker whitespace-eob-marker limit) |
2339 (setq whitespace-font-lock-refontify t) | 2395 (goto-char b))) ; return back to initial position |
2340 (re-search-forward whitespace-tab-regexp limit t)) | 2396 ;; inside eob empty region |
2397 ((>= b whitespace-eob-marker) | |
2398 (goto-char limit) | |
2399 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)) | |
2400 (if r | |
2401 (when (> (match-beginning 1) b) | |
2402 (set-marker whitespace-eob-marker (match-beginning 1))) | |
2403 (set-marker whitespace-eob-marker limit) | |
2404 (goto-char b))) ; return back to initial position | |
2405 ;; intersection with beginning of eob empty region | |
2406 ((>= limit whitespace-eob-marker) | |
2407 (goto-char limit) | |
2408 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)) | |
2409 (if r | |
2410 (set-marker whitespace-eob-marker (match-beginning 1)) | |
2411 (set-marker whitespace-eob-marker limit) | |
2412 (goto-char b))) ; return back to initial position | |
2413 ;; it is not inside eob empty region | |
2414 (t | |
2415 (setq r nil))) | |
2416 r)) | |
2417 | |
2418 | |
2419 (defun whitespace-buffer-changed (beg end) | |
2420 "Set `whitespace-buffer-changed' variable to t." | |
2421 (setq whitespace-buffer-changed t)) | |
2341 | 2422 |
2342 | 2423 |
2343 (defun whitespace-post-command-hook () | 2424 (defun whitespace-post-command-hook () |
2344 "Save current point into `whitespace-point' variable. | 2425 "Save current point into `whitespace-point' variable. |
2345 Also refontify when necessary." | 2426 Also refontify when necessary." |
2346 (setq whitespace-point (point)) | 2427 (setq whitespace-point (point)) ; current point position |
2347 (let ((refontify (or (eolp) ; end of line | 2428 (let ((refontify |
2348 (= whitespace-point 1)))) ; beginning of buffer | 2429 (or |
2349 (when (or whitespace-font-lock-refontify refontify) | 2430 ;; it is at end of line ... |
2350 (setq whitespace-font-lock-refontify refontify) | 2431 (and (eolp) |
2432 ;; ... with trailing SPACE or TAB | |
2433 (or (= (preceding-char) ?\ ) | |
2434 (= (preceding-char) ?\t))) | |
2435 ;; it is at beginning of buffer (bob) | |
2436 (= whitespace-point 1) | |
2437 ;; the buffer was modified and ... | |
2438 (and whitespace-buffer-changed | |
2439 (or | |
2440 ;; ... or inside bob whitespace region | |
2441 (<= whitespace-point whitespace-bob-marker) | |
2442 ;; ... or at bob whitespace region border | |
2443 (and (= whitespace-point (1+ whitespace-bob-marker)) | |
2444 (= (preceding-char) ?\n)))) | |
2445 ;; it is at end of buffer (eob) | |
2446 (= whitespace-point (1+ (buffer-size))) | |
2447 ;; the buffer was modified and ... | |
2448 (and whitespace-buffer-changed | |
2449 (or | |
2450 ;; ... or inside eob whitespace region | |
2451 (>= whitespace-point whitespace-eob-marker) | |
2452 ;; ... or at eob whitespace region border | |
2453 (and (= whitespace-point (1- whitespace-eob-marker)) | |
2454 (= (following-char) ?\n))))))) | |
2455 (when (or refontify (> whitespace-font-lock-refontify 0)) | |
2456 (setq whitespace-buffer-changed nil) | |
2457 ;; adjust refontify counter | |
2458 (setq whitespace-font-lock-refontify | |
2459 (if refontify | |
2460 1 | |
2461 (1- whitespace-font-lock-refontify))) | |
2462 ;; refontify | |
2351 (jit-lock-refontify)))) | 2463 (jit-lock-refontify)))) |
2352 | 2464 |
2353 | 2465 |
2354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 2466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2355 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>) | 2467 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>) |
2384 (let (vecs vec) | 2496 (let (vecs vec) |
2385 ;; Remember whether a buffer has a local display table. | 2497 ;; Remember whether a buffer has a local display table. |
2386 (unless whitespace-display-table-was-local | 2498 (unless whitespace-display-table-was-local |
2387 (setq whitespace-display-table-was-local t | 2499 (setq whitespace-display-table-was-local t |
2388 whitespace-display-table | 2500 whitespace-display-table |
2501 (copy-sequence buffer-display-table)) | |
2502 ;; asure `buffer-display-table' is unique | |
2503 ;; when two or more windows are visible. | |
2504 (setq buffer-display-table | |
2389 (copy-sequence buffer-display-table))) | 2505 (copy-sequence buffer-display-table))) |
2390 ;; asure `buffer-display-table' is unique | |
2391 ;; when two or more windows are visible. | |
2392 (set (make-local-variable 'buffer-display-table) | |
2393 (copy-sequence buffer-display-table)) | |
2394 (unless buffer-display-table | 2506 (unless buffer-display-table |
2395 (setq buffer-display-table (make-display-table))) | 2507 (setq buffer-display-table (make-display-table))) |
2396 (dolist (entry whitespace-display-mappings) | 2508 (dolist (entry whitespace-display-mappings) |
2397 ;; check if it is to display this mark | 2509 ;; check if it is to display this mark |
2398 (when (memq (car entry) whitespace-style) | 2510 (when (memq (car entry) whitespace-style) |