Mercurial > emacs
comparison lisp/font-lock.el @ 12385:c80966dc0b60
Specify in font-lock-defaults/font-lock-defaults-alist which keywords we can
use based on font-lock-maximum-decoration.
author | Simon Marshall <simon@gnu.org> |
---|---|
date | Mon, 26 Jun 1995 11:55:25 +0000 |
parents | f22803cfdc5c |
children | bf638e2849d5 |
comparison
equal
deleted
inserted
replaced
12384:13dd2463be12 | 12385:c80966dc0b60 |
---|---|
120 "If set by a major mode, should be the defaults for Font Lock mode. | 120 "If set by a major mode, should be the defaults for Font Lock mode. |
121 The value should look like the `cdr' of an item in `font-lock-defaults-alist'.") | 121 The value should look like the `cdr' of an item in `font-lock-defaults-alist'.") |
122 | 122 |
123 (defvar font-lock-defaults-alist | 123 (defvar font-lock-defaults-alist |
124 (let ((tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\"")))) | 124 (let ((tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\"")))) |
125 (lisp-mode-defaults '(lisp-font-lock-keywords | 125 (c-mode-defaults |
126 nil nil ((?: . "w") (?- . "w") (?* . "w"))))) | 126 '((c-font-lock-keywords c-font-lock-keywords-1 c-font-lock-keywords-2) |
127 nil nil ((?_ . "w")))) | |
128 (c++-mode-defaults | |
129 '((c++-font-lock-keywords c++-font-lock-keywords-1 | |
130 c++-font-lock-keywords-2) | |
131 nil nil ((?_ . "w")))) | |
132 (lisp-mode-defaults | |
133 '((lisp-font-lock-keywords lisp-font-lock-keywords-1 | |
134 lisp-font-lock-keywords-2) | |
135 nil nil ((?: . "w") (?- . "w") (?* . "w"))))) | |
127 (list | 136 (list |
128 (cons 'bibtex-mode tex-mode-defaults) | 137 (cons 'bibtex-mode tex-mode-defaults) |
129 '(c++-c-mode . (c-font-lock-keywords nil nil ((?_ . "w")))) | 138 (cons 'c++-c-mode c-mode-defaults) |
130 '(c++-mode . (c++-font-lock-keywords nil nil ((?_ . "w")))) | 139 (cons 'c++-mode c++-mode-defaults) |
131 '(c-mode . (c-font-lock-keywords nil nil ((?_ . "w")))) | 140 (cons 'c-mode c-mode-defaults) |
132 (cons 'emacs-lisp-mode lisp-mode-defaults) | 141 (cons 'emacs-lisp-mode lisp-mode-defaults) |
133 (cons 'latex-mode tex-mode-defaults) | 142 (cons 'latex-mode tex-mode-defaults) |
134 (cons 'lisp-mode lisp-mode-defaults) | 143 (cons 'lisp-mode lisp-mode-defaults) |
135 (cons 'plain-tex-mode tex-mode-defaults) | 144 (cons 'plain-tex-mode tex-mode-defaults) |
136 (cons 'scheme-mode lisp-mode-defaults) | 145 (cons 'scheme-mode lisp-mode-defaults) |
137 (cons 'slitex-mode tex-mode-defaults) | 146 (cons 'slitex-mode tex-mode-defaults) |
138 (cons 'tex-mode tex-mode-defaults))) | 147 (cons 'tex-mode tex-mode-defaults))) |
139 "*Alist of default major mode and Font Lock defaults. | 148 "*Alist of default major mode and Font Lock defaults. |
140 Each item should be a list of the form: | 149 Each item should be a list of the form: |
141 (MAJOR-MODE . (FONT-LOCK-KEYWORDS KEYWORDS-ONLY CASE-FOLD FONT-LOCK-SYNTAX)) | 150 (MAJOR-MODE . (FONT-LOCK-KEYWORDS KEYWORDS-ONLY CASE-FOLD FONT-LOCK-SYNTAX)) |
142 where both MAJOR-MODE and FONT-LOCK-KEYWORDS are symbols. If KEYWORDS-ONLY is | 151 where MAJOR-MODE is a symbol, and FONT-LOCK-KEYWORDS may be a symbol or a list |
143 non-nil, syntactic fontification (strings and comments) is not performed. | 152 of symbols. If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and |
144 If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying. | 153 comments) is not performed. If CASE-FOLD is non-nil, the case of the keywords |
145 FONT-LOCK-SYNTAX should be a list of cons pairs of the form (CHAR . STRING), it | 154 is ignored when fontifying. FONT-LOCK-SYNTAX should be a list of cons pairs of |
146 is used to set the local Font Lock syntax table for keyword fontification.") | 155 the form (CHAR . STRING), it is used to set the local Font Lock syntax table |
156 for keyword fontification.") | |
147 | 157 |
148 (defvar font-lock-keywords-case-fold-search nil | 158 (defvar font-lock-keywords-case-fold-search nil |
149 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.") | 159 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.") |
150 | 160 |
151 (defvar font-lock-syntax-table nil | 161 (defvar font-lock-syntax-table nil |
156 "*Non-nil means `font-lock-fontify-buffer' should print status messages.") | 166 "*Non-nil means `font-lock-fontify-buffer' should print status messages.") |
157 | 167 |
158 ;;;###autoload | 168 ;;;###autoload |
159 (defvar font-lock-maximum-decoration nil | 169 (defvar font-lock-maximum-decoration nil |
160 "Non-nil means use the maximum decoration for fontifying. | 170 "Non-nil means use the maximum decoration for fontifying. |
161 If a number, means use that level of decoration (or, if that is not available, | 171 If nil, use the default decoration (typically the minimum available). |
162 the maximum). If t, use the maximum decoration available. | 172 If t, use the maximum decoration available. |
163 | 173 If a number, use that level of decoration (or if not available the maximum).") |
164 It is up to packages defining Font Lock keywords to respect this variable.") | |
165 | 174 |
166 (defvar font-lock-maximum-size | 175 (defvar font-lock-maximum-size |
167 (if font-lock-maximum-decoration (* 150 1024) (* 300 1024)) | 176 (if font-lock-maximum-decoration (* 150 1024) (* 300 1024)) |
168 "*If non-nil, the maximum size for buffers. | 177 "*If non-nil, the maximum size for buffers. |
169 Only buffers less than this can be fontified when Font Lock mode is turned on. | 178 Only buffers less than this can be fontified when Font Lock mode is turned on. |
609 The default Font Lock mode faces and their attributes are defined in the | 618 The default Font Lock mode faces and their attributes are defined in the |
610 variable `font-lock-face-attributes', and Font Lock mode default settings in | 619 variable `font-lock-face-attributes', and Font Lock mode default settings in |
611 the variable `font-lock-defaults-alist'. | 620 the variable `font-lock-defaults-alist'. |
612 | 621 |
613 Where modes support different levels of fontification, you can use the variable | 622 Where modes support different levels of fontification, you can use the variable |
614 `font-lock-maximum-decoration' to specify which you generally prefer. | 623 `font-lock-maximum-decoration' to specify which level you generally prefer. |
615 When you turn Font Lock mode on/off the buffer is fontified/defontified, though | 624 When you turn Font Lock mode on/off the buffer is fontified/defontified, though |
616 fontification occurs only if the buffer is less than `font-lock-maximum-size'. | 625 fontification occurs only if the buffer is less than `font-lock-maximum-size'. |
617 To fontify a buffer without turning on Font Lock mode, and regardless of buffer | 626 To fontify a buffer without turning on Font Lock mode, and regardless of buffer |
618 size, you can use \\[font-lock-fontify-buffer]." | 627 size, you can use \\[font-lock-fontify-buffer]." |
619 (interactive "P") | 628 (interactive "P") |
725 (list (concat "^(\\(def[^ \t\n\(\)]+\\|eval-" | 734 (list (concat "^(\\(def[^ \t\n\(\)]+\\|eval-" |
726 "\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\)\\>" | 735 "\\(a\\(fter-load\\|nd-compile\\)\\|when-compile\\)\\)\\>" |
727 "[ \t']*\\([^ \t\n\(\)]+\\)?") | 736 "[ \t']*\\([^ \t\n\(\)]+\\)?") |
728 '(1 font-lock-keyword-face) '(4 font-lock-function-name-face nil t)) | 737 '(1 font-lock-keyword-face) '(4 font-lock-function-name-face nil t)) |
729 ) | 738 ) |
730 "For consideration as a value of `lisp-font-lock-keywords'. | 739 "Subdued level highlighting Lisp modes.") |
731 This does fairly subdued highlighting.") | |
732 | 740 |
733 (defconst lisp-font-lock-keywords-2 | 741 (defconst lisp-font-lock-keywords-2 |
734 (append lisp-font-lock-keywords-1 | 742 (append lisp-font-lock-keywords-1 |
735 (let ((word-char "[-+a-zA-Z0-9_:*]")) | 743 (let ((word-char "[-+a-zA-Z0-9_:*]")) |
736 (list | 744 (list |
737 ;; | 745 ;; |
738 ;; Control structures. | 746 ;; Control structures. |
739 ;; ELisp: | 747 ;; ELisp: |
740 ; ("cond" "if" "while" "let\\*?" "prog[nv12*]?" "catch" "throw" | 748 ; ("cond" "if" "while" "let\\*?" "prog[nv12*]?" "catch" "throw" |
741 ; "save-restriction" "save-excursion" | 749 ; "save-restriction" "save-excursion" "save-window-excursion" |
742 ; "save-window-excursion" "save-match-data" "unwind-protect" | 750 ; "save-selected-window" "save-match-data" "unwind-protect" |
743 ; "condition-case" "track-mouse") | 751 ; "condition-case" "track-mouse") |
744 (cons | 752 (cons |
745 (concat | 753 (concat |
746 "(\\(" | 754 "(\\(" |
747 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|if\\|let\\*?\\|prog[nv12*]?\\|" | 755 "c\\(atch\\|ond\\(\\|ition-case\\)\\)\\|if\\|let\\*?\\|prog[nv12*]?\\|" |
748 "save-\\(excursion\\|match-data\\|restriction\\|window-excursion\\)\\|" | 756 "save-\\(excursion\\|match-data\\|restriction\\|" |
757 "selected-window\\|window-excursion\\)\\|" | |
749 "t\\(hrow\\|rack-mouse\\)\\|unwind-protect\\|while" | 758 "t\\(hrow\\|rack-mouse\\)\\|unwind-protect\\|while" |
750 "\\)\\>") 1) | 759 "\\)\\>") 1) |
751 ;; CLisp: | 760 ;; CLisp: |
752 ; ("when" "unless" "do" "flet" "labels" "return" "return-from") | 761 ; ("when" "unless" "do" "flet" "labels" "return" "return-from") |
753 '("(\\(do\\|flet\\|labels\\|return\\(\\|-from\\)\\|unless\\|when\\)\\>" | 762 '("(\\(do\\|flet\\|labels\\|return\\(\\|-from\\)\\|unless\\|when\\)\\>" |
766 1 'font-lock-reference-face t) | 775 1 'font-lock-reference-face t) |
767 ;; | 776 ;; |
768 ;; & keywords as types | 777 ;; & keywords as types |
769 '("\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face) | 778 '("\\&\\(optional\\|rest\\|whole\\)\\>" . font-lock-type-face) |
770 ))) | 779 ))) |
771 "For consideration as a value of `lisp-font-lock-keywords'. | 780 "Gaudy level highlighting for Lisp modes.") |
772 This does a lot more highlighting.") | 781 |
773 | 782 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1 |
774 (defvar lisp-font-lock-keywords (if font-lock-maximum-decoration | 783 "Default expressions to highlight in Lisp modes.") |
775 lisp-font-lock-keywords-2 | |
776 lisp-font-lock-keywords-1) | |
777 "Additional expressions to highlight in Lisp modes.") | |
778 | 784 |
779 | 785 |
780 (defconst c-font-lock-keywords-1 nil | 786 (defconst c-font-lock-keywords-1 nil |
781 "For consideration as a value of `c-font-lock-keywords'. | 787 "Subdued level highlighting for C modes.") |
782 This does fairly subdued highlighting.") | |
783 | 788 |
784 (defconst c-font-lock-keywords-2 nil | 789 (defconst c-font-lock-keywords-2 nil |
785 "For consideration as a value of `c-font-lock-keywords'. | 790 "Gaudy level highlighting for C modes.") |
786 This does a lot more highlighting.") | |
787 | 791 |
788 (defconst c++-font-lock-keywords-1 nil | 792 (defconst c++-font-lock-keywords-1 nil |
789 "For consideration as a value of `c++-font-lock-keywords'. | 793 "Subdued level highlighting for C++ modes.") |
790 This does fairly subdued highlighting.") | |
791 | 794 |
792 (defconst c++-font-lock-keywords-2 nil | 795 (defconst c++-font-lock-keywords-2 nil |
793 "For consideration as a value of `c++-font-lock-keywords'. | 796 "Gaudy level highlighting for C++ modes.") |
794 This does a lot more highlighting.") | |
795 | 797 |
796 (let ((c-keywords | 798 (let ((c-keywords |
797 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while") | 799 ; ("break" "continue" "do" "else" "for" "if" "return" "switch" "while") |
798 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while") | 800 "break\\|continue\\|do\\|else\\|for\\|if\\|return\\|switch\\|while") |
799 (c-type-types | 801 (c-type-types |
906 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?" | 908 '("\\<\\(case\\|goto\\)\\>[ \t]*\\([^ \t\n:;]+\\)?" |
907 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) | 909 (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)) |
908 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)))) | 910 '("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-reference-face)))) |
909 ) | 911 ) |
910 | 912 |
911 (defvar c-font-lock-keywords (if font-lock-maximum-decoration | 913 (defvar c-font-lock-keywords c-font-lock-keywords-1 |
912 c-font-lock-keywords-2 | 914 "Default expressions to highlight in C mode.") |
913 c-font-lock-keywords-1) | 915 |
914 "Additional expressions to highlight in C mode.") | 916 (defvar c++-font-lock-keywords c++-font-lock-keywords-1 |
915 | 917 "Default expressions to highlight in C++ mode.") |
916 (defvar c++-font-lock-keywords (if font-lock-maximum-decoration | |
917 c++-font-lock-keywords-2 | |
918 c++-font-lock-keywords-1) | |
919 "Additional expressions to highlight in C++ mode.") | |
920 | 918 |
921 (defvar tex-font-lock-keywords | 919 (defvar tex-font-lock-keywords |
922 ;; '("\\(\\\\\\([a-zA-Z@]+\\|.\\)\\)" 1 font-lock-keyword-face t) | 920 ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>. |
923 ;; '("{\\\\em\\([^}]+\\)}" 1 font-lock-comment-face t) | |
924 ;; '("{\\\\bf\\([^}]+\\)}" 1 font-lock-keyword-face t) | |
925 ;; '("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face t) | |
926 ;; '("\\\\\\(begin\\|end\\){\\([a-zA-Z0-9\\*]+\\)}" | |
927 ;; 2 font-lock-function-name-face t) | |
928 ;; '("\\(^\\|[^\\\\]\\)\\$\\([^$]*\\)\\$" 2 font-lock-string-face t) | |
929 ;;; '("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t) | |
930 ;; Regexps updated by simon@gnu with help from Ulrik Dickow <dickow@nbi.dk>. | |
931 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" | 921 '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" |
932 2 font-lock-function-name-face) | 922 2 font-lock-function-name-face) |
933 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" | 923 ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}" |
934 2 font-lock-reference-face) | 924 2 font-lock-reference-face) |
935 ;; It seems a bit dubious to use `bold' and `italic' faces since we might | 925 ;; It seems a bit dubious to use `bold' and `italic' faces since we might |
938 ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep) | 928 ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep) |
939 ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face) | 929 ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face) |
940 ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep)) | 930 ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep)) |
941 "Additional expressions to highlight in TeX mode.") | 931 "Additional expressions to highlight in TeX mode.") |
942 | 932 |
933 (defun font-lock-choose-keywords (keywords level) | |
934 ;; Return evaled LEVELth element of KEYWORDS. A LEVEL of nil is equal to a | |
935 ;; LEVEL of 0, a LEVEL of t is equal to (1- (length KEYWORDS)). | |
936 (eval (cond ((symbolp keywords) | |
937 keywords) | |
938 ((numberp level) | |
939 (or (nth level keywords) (car (reverse keywords)))) | |
940 ((eq level t) | |
941 (car (reverse keywords))) | |
942 (t | |
943 (car keywords))))) | |
944 | |
943 (defun font-lock-set-defaults () | 945 (defun font-lock-set-defaults () |
944 "Set fontification defaults appropriately for this mode. | 946 "Set fontification defaults appropriately for this mode. |
945 Sets `font-lock-keywords', `font-lock-no-comments', `font-lock-syntax-table' | 947 Sets `font-lock-keywords', `font-lock-no-comments', `font-lock-syntax-table' |
946 and `font-lock-keywords-case-fold-search' using `font-lock-defaults-alist'." | 948 and `font-lock-keywords-case-fold-search' using `font-lock-defaults' (or, if |
949 nil, using `font-lock-defaults-alist') and `font-lock-maximum-decoration'." | |
947 ;; Set face defaults. | 950 ;; Set face defaults. |
948 (font-lock-make-faces) | 951 (font-lock-make-faces) |
949 ;; Set fontification defaults. | 952 ;; Set fontification defaults. |
950 (or font-lock-keywords | 953 (or font-lock-keywords |
951 (let ((defaults (or font-lock-defaults | 954 (let ((defaults (or font-lock-defaults |
952 (cdr (assq major-mode font-lock-defaults-alist))))) | 955 (cdr (assq major-mode font-lock-defaults-alist))))) |
953 ;; Keywords? | 956 ;; Keywords? |
954 (setq font-lock-keywords (eval (nth 0 defaults))) | 957 (setq font-lock-keywords (font-lock-choose-keywords (nth 0 defaults) |
958 font-lock-maximum-decoration)) | |
955 ;; Syntactic? | 959 ;; Syntactic? |
956 (if (nth 1 defaults) | 960 (if (nth 1 defaults) |
957 (set (make-local-variable 'font-lock-no-comments) t)) | 961 (set (make-local-variable 'font-lock-no-comments) t)) |
958 ;; Case fold? | 962 ;; Case fold? |
959 (if (nth 2 defaults) | 963 (if (nth 2 defaults) |
960 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)) | 964 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)) |
961 ;; Syntax table? | 965 ;; Syntax table? |
962 (if (nth 3 defaults) | 966 (if (nth 3 defaults) |
963 (let ((slist (nth 3 defaults))) | 967 (let ((slist (nth 3 defaults))) |
964 (make-local-variable 'font-lock-syntax-table) | 968 (set (make-local-variable 'font-lock-syntax-table) |
965 (setq font-lock-syntax-table (copy-syntax-table (syntax-table))) | 969 (copy-syntax-table (syntax-table))) |
966 (while slist | 970 (while slist |
967 (modify-syntax-entry (car (car slist)) (cdr (car slist)) | 971 (modify-syntax-entry (car (car slist)) (cdr (car slist)) |
968 font-lock-syntax-table) | 972 font-lock-syntax-table) |
969 (setq slist (cdr slist)))))))) | 973 (setq slist (cdr slist)))))))) |
970 | 974 |