comparison lisp/font-lock.el @ 13701:1dab6f0d9239

Don't turn on in any buffer with a leading space in its name. When (un)fontifying, make sure we're doing it in the whole buffer. Make a command to refontify the window.
author Simon Marshall <simon@gnu.org>
date Sat, 09 Dec 1995 16:35:57 +0000
parents 1233bea2cb02
children baba81b17362
comparison
equal deleted inserted replaced
13700:7cbb1453aefd 13701:1dab6f0d9239
65 ;; less about, say, the structure of code, and more about, say, why the code 65 ;; less about, say, the structure of code, and more about, say, why the code
66 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep. 66 ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
67 ;; 67 ;;
68 ;; So, here are my opinions/advice/guidelines: 68 ;; So, here are my opinions/advice/guidelines:
69 ;; 69 ;;
70 ;; - Highlight conceptual objects, such as function and variable names, and
71 ;; different objects types differently, i.e., (a) and (b) above, highlight
72 ;; function names differently to variable names.
73 ;; - Keep the faces distinct from each other as far as possible.
74 ;; i.e., (a) above.
70 ;; - Use the same face for the same conceptual object, across all modes. 75 ;; - Use the same face for the same conceptual object, across all modes.
71 ;; i.e., (b) above, all modes that have items that can be thought of as, say, 76 ;; i.e., (b) above, all modes that have items that can be thought of as, say,
72 ;; keywords, should be highlighted with the same face, etc. 77 ;; keywords, should be highlighted with the same face, etc.
73 ;; - Keep the faces distinct from each other as far as possible.
74 ;; i.e., (a) above.
75 ;; - Make the face attributes fit the concept as far as possible. 78 ;; - Make the face attributes fit the concept as far as possible.
76 ;; i.e., function names might be a bold colour such as blue, comments might 79 ;; i.e., function names might be a bold colour such as blue, comments might
77 ;; be a bright colour such as red, character strings might be brown, because, 80 ;; be a bright colour such as red, character strings might be brown, because,
78 ;; err, strings are brown (that was not the reason, please believe me). 81 ;; err, strings are brown (that was not the reason, please believe me).
79 ;; - Don't use a non-nil OVERRIDE unless you have a good reason. 82 ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
352 355
353 Where modes support different levels of fontification, you can use the variable 356 Where modes support different levels of fontification, you can use the variable
354 `font-lock-maximum-decoration' to specify which level you generally prefer. 357 `font-lock-maximum-decoration' to specify which level you generally prefer.
355 When you turn Font Lock mode on/off the buffer is fontified/defontified, though 358 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
356 fontification occurs only if the buffer is less than `font-lock-maximum-size'. 359 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
357 To fontify a buffer without turning on Font Lock mode, and regardless of buffer 360
358 size, you can use \\[font-lock-fontify-buffer]." 361 To fontify a buffer, without turning on Font Lock mode and regardless of buffer
362 size, you can use \\[font-lock-fontify-buffer].
363 To fontify a window, perhaps because modification on the current line caused
364 syntactic change on other lines, you can use \\[font-lock-fontify-window]."
359 (interactive "P") 365 (interactive "P")
360 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode))) 366 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode)))
361 (maximum-size (if (not (consp font-lock-maximum-size)) 367 (maximum-size (if (not (consp font-lock-maximum-size))
362 font-lock-maximum-size 368 font-lock-maximum-size
363 (cdr (or (assq major-mode font-lock-maximum-size) 369 (cdr (or (assq major-mode font-lock-maximum-size)
364 (assq t font-lock-maximum-size)))))) 370 (assq t font-lock-maximum-size))))))
365 (if (equal (buffer-name) " *Compiler Input*") ; hack for bytecomp... 371 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
372 ;; batch job) or if the buffer is invisible (the name starts with a space).
373 (if (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
366 (setq on-p nil)) 374 (setq on-p nil))
367 (if (not on-p) 375 (if (not on-p)
368 (remove-hook 'after-change-functions 'font-lock-after-change-function 376 (remove-hook 'after-change-functions 'font-lock-after-change-function
369 t) 377 t)
370 (make-local-hook 'after-change-functions) 378 (make-local-hook 'after-change-functions)
387 (message "Fontifying %s... buffer too big." (buffer-name))))) 395 (message "Fontifying %s... buffer too big." (buffer-name)))))
388 (font-lock-fontified 396 (font-lock-fontified
389 (setq font-lock-fontified nil) 397 (setq font-lock-fontified nil)
390 (remove-hook 'before-revert-hook 'font-lock-revert-setup t) 398 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
391 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t) 399 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
392 (font-lock-unfontify-region (point-min) (point-max)) 400 ;; Make sure we unfontify etc. in the whole buffer.
393 (font-lock-thing-lock-cleanup)) 401 (save-restriction
402 (widen)
403 (font-lock-unfontify-region (point-min) (point-max))
404 (font-lock-thing-lock-cleanup)))
394 (t 405 (t
395 (remove-hook 'before-revert-hook 'font-lock-revert-setup t) 406 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
396 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t) 407 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
397 (font-lock-thing-lock-cleanup))) 408 (font-lock-thing-lock-cleanup)))
398 (force-mode-line-update))) 409 (force-mode-line-update)))
408 (interactive) 419 (interactive)
409 (let ((verbose (and (or font-lock-verbose (interactive-p)) 420 (let ((verbose (and (or font-lock-verbose (interactive-p))
410 (not (zerop (buffer-size)))))) 421 (not (zerop (buffer-size))))))
411 (set (make-local-variable 'font-lock-fontified) nil) 422 (set (make-local-variable 'font-lock-fontified) nil)
412 (if verbose (message "Fontifying %s..." (buffer-name))) 423 (if verbose (message "Fontifying %s..." (buffer-name)))
413 ;; Turn it on to run hooks and get the right `font-lock-keywords' etc. 424 ;; Make sure we have the right `font-lock-keywords' etc.
414 (or font-lock-mode (font-lock-set-defaults)) 425 (if (not font-lock-mode) (font-lock-set-defaults))
415 (condition-case nil 426 ;; Make sure we fontify etc. in the whole buffer.
416 (save-excursion 427 (save-restriction
417 (save-match-data 428 (widen)
418 (font-lock-fontify-region (point-min) (point-max) verbose) 429 (condition-case nil
419 (setq font-lock-fontified t))) 430 (save-excursion
420 ;; We don't restore the old fontification, so it's best to unfontify. 431 (save-match-data
421 (quit (font-lock-unfontify-region (point-min) (point-max)))) 432 (font-lock-fontify-region (point-min) (point-max) verbose)
422 (if verbose (message "Fontifying %s... %s." (buffer-name) 433 (setq font-lock-fontified t)))
423 (if font-lock-fontified "done" "aborted"))) 434 ;; We don't restore the old fontification, so it's best to unfontify.
424 (font-lock-after-fontify-buffer))) 435 (quit (font-lock-unfontify-region (point-min) (point-max))))
436 (if verbose (message "Fontifying %s... %s." (buffer-name)
437 (if font-lock-fontified "done" "aborted")))
438 (font-lock-after-fontify-buffer))))
439
440 (defun font-lock-fontify-window ()
441 "Fontify the current window the way `font-lock-mode' would."
442 (interactive)
443 (let ((font-lock-beginning-of-syntax-function nil))
444 (save-excursion
445 (save-match-data
446 (font-lock-fontify-region (window-start) (window-end))))))
447
448 (define-key global-map [?\C-\S-l] 'font-lock-fontify-window)
425 449
426 ;; Fontification functions. 450 ;; Fontification functions.
427 451
428 ;; We use this wrapper. However, `font-lock-fontify-region' used to be the 452 ;; We use this wrapper. However, `font-lock-fontify-region' used to be the
429 ;; name used for `font-lock-fontify-syntactically-region', so a change isn't 453 ;; name used for `font-lock-fontify-syntactically-region', so a change isn't
430 ;; back-compatible. But you shouldn't be calling these directly, should you? 454 ;; back-compatible. But you shouldn't be calling these directly, should you?
431 (defun font-lock-fontify-region (beg end &optional loudly) 455 (defun font-lock-fontify-region (beg end &optional loudly)
432 (let ((modified (buffer-modified-p)) 456 (let ((modified (buffer-modified-p))
433 (buffer-undo-list t) (inhibit-read-only t) 457 (buffer-undo-list t) (inhibit-read-only t)
434 (old-syntax-table (syntax-table)) 458 (old-syntax-table (syntax-table))
459 before-change-functions after-change-functions
435 buffer-file-name buffer-file-truename) 460 buffer-file-name buffer-file-truename)
436 (unwind-protect 461 (unwind-protect
437 (progn 462 (progn
438 ;; Use the fontification syntax table, if any. 463 ;; Use the fontification syntax table, if any.
439 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table)) 464 (if font-lock-syntax-table (set-syntax-table font-lock-syntax-table))
455 ; (font-lock-fontify-keywords-region beg end)) 480 ; (font-lock-fontify-keywords-region beg end))
456 481
457 (defun font-lock-unfontify-region (beg end) 482 (defun font-lock-unfontify-region (beg end)
458 (let ((modified (buffer-modified-p)) 483 (let ((modified (buffer-modified-p))
459 (buffer-undo-list t) (inhibit-read-only t) 484 (buffer-undo-list t) (inhibit-read-only t)
485 before-change-functions after-change-functions
460 buffer-file-name buffer-file-truename) 486 buffer-file-name buffer-file-truename)
461 (remove-text-properties beg end '(face nil)) 487 (remove-text-properties beg end '(face nil))
462 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))) 488 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil))))
463 489
464 ;; Called when any modification is made to buffer text. 490 ;; Called when any modification is made to buffer text.
762 (defun font-lock-revert-setup () 788 (defun font-lock-revert-setup ()
763 (setq font-lock-fontified nil)) 789 (setq font-lock-fontified nil))
764 790
765 ;; If the buffer has just been reverted, normally that turns off 791 ;; If the buffer has just been reverted, normally that turns off
766 ;; Font Lock mode. So turn the mode back on if necessary. 792 ;; Font Lock mode. So turn the mode back on if necessary.
767 (defalias 'font-lock-revert-cleanup 'turn-on-font-lock) 793 (defalias 'font-lock-revert-cleanup
794 'turn-on-font-lock)
768 795
769 (defun font-lock-compile-keywords (&optional keywords) 796 (defun font-lock-compile-keywords (&optional keywords)
770 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD 797 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
771 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string. 798 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
772 (let ((keywords (or keywords font-lock-keywords))) 799 (let ((keywords (or keywords font-lock-keywords)))
815 (or font-lock-keywords 842 (or font-lock-keywords
816 (let* ((defaults (or font-lock-defaults 843 (let* ((defaults (or font-lock-defaults
817 (cdr (assq major-mode font-lock-defaults-alist)))) 844 (cdr (assq major-mode font-lock-defaults-alist))))
818 (keywords (font-lock-choose-keywords 845 (keywords (font-lock-choose-keywords
819 (nth 0 defaults) font-lock-maximum-decoration))) 846 (nth 0 defaults) font-lock-maximum-decoration)))
820 ;; Keywords? 847 ;; Regexp fontification?
821 (setq font-lock-keywords (if (fboundp keywords) 848 (setq font-lock-keywords (if (fboundp keywords)
822 (funcall keywords) 849 (funcall keywords)
823 (eval keywords))) 850 (eval keywords)))
824 ;; Syntactic? 851 ;; Syntactic fontification?
825 (if (nth 1 defaults) 852 (if (nth 1 defaults)
826 (set (make-local-variable 'font-lock-keywords-only) t)) 853 (set (make-local-variable 'font-lock-keywords-only) t))
827 ;; Case fold? 854 ;; Case fold during regexp fontification?
828 (if (nth 2 defaults) 855 (if (nth 2 defaults)
829 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)) 856 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
830 ;; Syntax table? 857 ;; Syntax table for regexp and syntactic fontification?
831 (if (nth 3 defaults) 858 (if (nth 3 defaults)
832 (let ((slist (nth 3 defaults))) 859 (let ((slist (nth 3 defaults)))
833 (set (make-local-variable 'font-lock-syntax-table) 860 (set (make-local-variable 'font-lock-syntax-table)
834 (copy-syntax-table (syntax-table))) 861 (copy-syntax-table (syntax-table)))
835 (while slist 862 (while slist
836 (modify-syntax-entry (car (car slist)) (cdr (car slist)) 863 (modify-syntax-entry (car (car slist)) (cdr (car slist))
837 font-lock-syntax-table) 864 font-lock-syntax-table)
838 (setq slist (cdr slist))))) 865 (setq slist (cdr slist)))))
839 ;; Syntax function? 866 ;; Syntax function for syntactic fontification?
840 (if (nth 4 defaults) 867 (if (nth 4 defaults)
841 (set (make-local-variable 'font-lock-beginning-of-syntax-function) 868 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
842 (nth 4 defaults)))))) 869 (nth 4 defaults))))))
843 870
844 ;; Colour etc. support. 871 ;; Colour etc. support.
1038 ;; Everything else is a function declaration. 1065 ;; Everything else is a function declaration.
1039 "\\([^ \t\n\(\)]+\\)" 1066 "\\([^ \t\n\(\)]+\\)"
1040 "\\)\\)\\>" 1067 "\\)\\)\\>"
1041 ;; Any whitespace and declared object. 1068 ;; Any whitespace and declared object.
1042 "[ \t'\(]*" 1069 "[ \t'\(]*"
1043 "\\([^ \t\n\)]+\\)?") 1070 "\\(\\sw+\\)?")
1044 '(1 font-lock-keyword-face) 1071 '(1 font-lock-keyword-face)
1045 '(8 (cond ((match-beginning 3) font-lock-variable-name-face) 1072 '(8 (cond ((match-beginning 3) font-lock-variable-name-face)
1046 ((match-beginning 6) font-lock-type-face) 1073 ((match-beginning 6) font-lock-type-face)
1047 (t font-lock-function-name-face)) 1074 (t font-lock-function-name-face))
1048 nil t)) 1075 nil t))
1049 ) 1076 )
1050 "Subdued level highlighting Lisp modes.") 1077 "Subdued level highlighting for Lisp modes.")
1051 1078
1052 (defconst lisp-font-lock-keywords-2 1079 (defconst lisp-font-lock-keywords-2
1053 (append lisp-font-lock-keywords-1 1080 (append lisp-font-lock-keywords-1
1054 (list 1081 (list
1055 ;; 1082 ;;
1056 ;; Control structures. ELisp and CLisp combined. 1083 ;; Control structures. ELisp and CLisp combined.
1057 ; (make-regexp 1084 ; (make-regexp
1058 ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "catch" "throw" 1085 ; '("cond" "if" "while" "let\\*?" "prog[nv12*]?" "inline" "catch" "throw"
1059 ; "save-restriction" "save-excursion" "save-window-excursion" 1086 ; "save-restriction" "save-excursion" "save-window-excursion"
1060 ; "save-selected-window" "save-match-data" "unwind-protect" 1087 ; "save-selected-window" "save-match-data" "unwind-protect"
1061 ; "condition-case" "track-mouse" 1088 ; "condition-case" "track-mouse"
1062 ; "eval-after-load" "eval-and-compile" "eval-when-compile" 1089 ; "eval-after-load" "eval-and-compile" "eval-when-compile"
1063 ; "when" "unless" "do" "flet" "labels" "return" "return-from")) 1090 ; "when" "unless" "do" "flet" "labels" "return" "return-from"))
1064 (cons 1091 (cons
1065 (concat 1092 (concat
1066 "(\\(" 1093 "(\\("
1067 "\\(c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|" 1094 "\\(c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|do\\|"
1068 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|" 1095 "eval-\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\|flet\\|"
1069 "if\\|l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|return\\(\\|-from\\)\\|" 1096 "i\\(f\\|nline\\)\\|l\\(abels\\|et\\*?\\)\\|prog[nv12*]?\\|"
1097 "return\\(\\|-from\\)\\|"
1070 "save-\\(excursion\\|match-data\\|restriction\\|selected-window\\|" 1098 "save-\\(excursion\\|match-data\\|restriction\\|selected-window\\|"
1071 "window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|" 1099 "window-excursion\\)\\|t\\(hrow\\|rack-mouse\\)\\|"
1072 "un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)" 1100 "un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)"
1073 "\\)\\>") 1) 1101 "\\)\\>") 1)
1102 ;;
1103 ;; Feature symbols as references.
1104 '("(\\(featurep\\|provide\\|require\\)\\>[ \t']*\\(\\sw+\\)?"
1105 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1074 ;; 1106 ;;
1075 ;; Words inside \\[] tend to be for `substitute-command-keys'. 1107 ;; Words inside \\[] tend to be for `substitute-command-keys'.
1076 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend) 1108 '("\\\\\\\\\\[\\(\\sw+\\)]" 1 font-lock-reference-face prepend)
1077 ;; 1109 ;;
1078 ;; Words inside `' tend to be symbol names. 1110 ;; Words inside `' tend to be symbol names.
1219 ;; 1251 ;;
1220 ;; Fontify filenames in #include <...> preprocessor directives as strings. 1252 ;; Fontify filenames in #include <...> preprocessor directives as strings.
1221 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face) 1253 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
1222 ;; 1254 ;;
1223 ;; Fontify function macro names. 1255 ;; Fontify function macro names.
1224 '("^#[ \t]*define[ \t]+\\(\\(\\sw+\\)(\\)" 2 font-lock-function-name-face) 1256 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
1225 ;; 1257 ;;
1226 ;; Fontify symbol names in #if ... defined preprocessor directives. 1258 ;; Fontify symbol names in #if ... defined preprocessor directives.
1227 '("^#[ \t]*if\\>" 1259 '("^#[ \t]*if\\>"
1228 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil 1260 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1229 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))) 1261 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1230 ;; 1262 ;;
1231 ;; Fontify otherwise as symbol names, and the preprocessor directive names. 1263 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
1232 '("^\\(#[ \t]*[a-z]+\\)\\>[ \t]*\\(\\sw+\\)?" 1264 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
1233 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)) 1265 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
1234 )) 1266 ))
1235 1267
1236 (setq c-font-lock-keywords-2 1268 (setq c-font-lock-keywords-2
1237 (append c-font-lock-keywords-1 1269 (append c-font-lock-keywords-1
1244 ;; 1276 ;;
1245 ;; Fontify all builtin keywords (except case, default and goto; see below). 1277 ;; Fontify all builtin keywords (except case, default and goto; see below).
1246 (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face) 1278 (cons (concat "\\<\\(" c-keywords "\\)\\>") 'font-lock-keyword-face)
1247 ;; 1279 ;;
1248 ;; Fontify case/goto keywords and targets, and case default/goto tags. 1280 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1249 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?" 1281 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
1250 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) 1282 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1251 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face) 1283 '("^[ \t]*\\(\\sw+\\)[ \t]*:" 1 font-lock-reference-face)
1252 ))) 1284 )))
1253 1285
1254 (setq c-font-lock-keywords-3 1286 (setq c-font-lock-keywords-3
1315 ;; Fontify operator function name overloading. 1347 ;; Fontify operator function name overloading.
1316 '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?" 1348 '("\\<\\(operator\\)\\>[ \t]*\\([][)(><!=+-][][)(><!=+-]?\\)?"
1317 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t)) 1349 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
1318 ;; 1350 ;;
1319 ;; Fontify case/goto keywords and targets, and case default/goto tags. 1351 ;; Fontify case/goto keywords and targets, and case default/goto tags.
1320 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?" 1352 '("\\<\\(case\\|goto\\)\\>[ \t]*\\(\\sw+\\)?"
1321 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) 1353 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
1322 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face) 1354 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)
1323 ;; 1355 ;;
1324 ;; Fontify other builtin keywords. 1356 ;; Fontify other builtin keywords.
1325 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face) 1357 (cons (concat "\\<\\(" c++-keywords "\\)\\>") 'font-lock-keyword-face)