aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Marshall1996-07-12 07:27:55 +0000
committerSimon Marshall1996-07-12 07:27:55 +0000
commit98f84f52d43a887e63f85a0a1f80fe2987a1db92 (patch)
tree633e4342f4b671cd1dd22e94ce6204fc0550abf0
parent8d47e7e5246700bfdd46918c8a1f2a2c265ecdc9 (diff)
downloademacs-98f84f52d43a887e63f85a0a1f80fe2987a1db92.tar.gz
emacs-98f84f52d43a887e63f85a0a1f80fe2987a1db92.zip
1. Use new font-lock-comment-start-regexp for matching a comment---it can be faster than comment-start-skip.
2. Don't treat ; as an item separator.
-rw-r--r--lisp/font-lock.el111
1 files changed, 91 insertions, 20 deletions
diff --git a/lisp/font-lock.el b/lisp/font-lock.el
index baeaf1e4f04..2e840bfc7d1 100644
--- a/lisp/font-lock.el
+++ b/lisp/font-lock.el
@@ -49,7 +49,56 @@
49;; it takes to fontify. See the variable `font-lock-maximum-decoration', and 49;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
50;; also the variable `font-lock-maximum-size'. Support modes for Font Lock 50;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
51;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'. 51;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
52
53;; Constructing patterns:
54;;
55;; See the documentation for the variable `font-lock-keywords'.
56;;
57;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
58;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
59;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
60;; archive.cis.ohio-state.edu for this and other functions.
61
62;; Adding patterns for modes that already support Font Lock:
63;;
64;; Font Lock mode uses the buffer local variable `font-lock-keywords' for the
65;; highlighting patterns. This variable is set by Font Lock mode from (a) the
66;; buffer local variable `font-lock-defaults', if non-nil, or (b) the global
67;; variable `font-lock-defaults-alist', if the major mode has an entry.
68;; Font Lock mode is set up via (a) where a mode's patterns are distributed
69;; with the mode's package library, (b) where a mode's patterns are distributed
70;; with font-lock.el itself. An example of (a) is Pascal mode, an example of
71;; (b) is C/C++ modes. (Normally, the mechanism is (a); (b) is used where it
72;; is not clear which package library should contain the pattern definitions.)
73;;
74;; If, for a particular mode, mechanism (a) is used, you need to add your
75;; patterns after that package library has loaded, e.g.:
76;;
77;; (eval-after-load "pascal" '(add-to-list 'pascal-font-lock-keywords ...))
78;;
79;; (Note that only one pattern can be added with `add-to-list'. For multiple
80;; patterns, use one `eval-after-load' form with one `setq' and `append' form,
81;; or multiple `eval-after-load' forms each with one `add-to-list' form.)
82;; If mechanism (b) is used, you need to add your patterns after font-lock.el
83;; itself has loaded, e.g.:
84;;
85;; (eval-after-load "font-lock" '(add-to-list 'c-font-lock-keywords ...))
86;;
87;; Which variable you should add to depends on what level of fontification you
88;; choose and what level is supported. If you choose the maximum level, by
89;; setting the variable `font-lock-maximum-decoration', you change a different
90;; variable. Maximum level patterns for C are `c-font-lock-keywords-3', so:
91;;
92;; (setq font-lock-maximum-decoration t)
93;; (eval-after-load "font-lock"
94;; '(add-to-list 'c-font-lock-keywords-3
95;; '("\\<FILE\\>" . font-lock-type-face)))
96;;
97;; To see which variable to set, see the buffer's value of `font-lock-defaults'
98;; or the mode's entry in the global value of `font-lock-defaults-alist'.
52 99
100;; Adding patterns for modes that do not support Font Lock:
101;;
53;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which 102;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which
54;; you don't want syntactic fontification to occur, you can make Font Lock mode 103;; you don't want syntactic fontification to occur, you can make Font Lock mode
55;; use your regexps when turning on Font Lock by adding to `foo-mode-hook': 104;; use your regexps when turning on Font Lock by adding to `foo-mode-hook':
@@ -57,12 +106,7 @@
57;; (add-hook 'foo-mode-hook 106;; (add-hook 'foo-mode-hook
58;; '(lambda () (make-local-variable 'font-lock-defaults) 107;; '(lambda () (make-local-variable 'font-lock-defaults)
59;; (setq font-lock-defaults '(foo-font-lock-keywords t)))) 108;; (setq font-lock-defaults '(foo-font-lock-keywords t))))
60;; 109
61;; Nasty regexps of the form "bar\\(\\|lo\\)\\|f\\(oo\\|u\\(\\|bar\\)\\)\\|lo"
62;; are made thusly: (make-regexp '("foo" "fu" "fubar" "bar" "barlo" "lo")) for
63;; efficiency. See /pub/gnu/emacs/elisp-archive/functions/make-regexp.el.Z on
64;; archive.cis.ohio-state.edu for this and other functions.
65
66;; What is fontification for? You might say, "It's to make my code look nice." 110;; What is fontification for? You might say, "It's to make my code look nice."
67;; I think it should be for adding information in the form of cues. These cues 111;; I think it should be for adding information in the form of cues. These cues
68;; should provide you with enough information to both (a) distinguish between 112;; should provide you with enough information to both (a) distinguish between
@@ -238,31 +282,36 @@ The value should be like the `cdr' of an item in `font-lock-defaults-alist'.")
238 '((c-font-lock-keywords c-font-lock-keywords-1 282 '((c-font-lock-keywords c-font-lock-keywords-1
239 c-font-lock-keywords-2 c-font-lock-keywords-3) 283 c-font-lock-keywords-2 c-font-lock-keywords-3)
240 nil nil ((?_ . "w")) beginning-of-defun 284 nil nil ((?_ . "w")) beginning-of-defun
285 (font-lock-comment-start-regexp . "/[*/]")
241 (font-lock-mark-block-function . mark-defun))) 286 (font-lock-mark-block-function . mark-defun)))
242 (c++-mode-defaults 287 (c++-mode-defaults
243 '((c++-font-lock-keywords c++-font-lock-keywords-1 288 '((c++-font-lock-keywords c++-font-lock-keywords-1
244 c++-font-lock-keywords-2 c++-font-lock-keywords-3) 289 c++-font-lock-keywords-2 c++-font-lock-keywords-3)
245 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun 290 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun
291 (font-lock-comment-start-regexp . "/[*/]")
246 (font-lock-mark-block-function . mark-defun))) 292 (font-lock-mark-block-function . mark-defun)))
247 (lisp-mode-defaults 293 (lisp-mode-defaults
248 '((lisp-font-lock-keywords 294 '((lisp-font-lock-keywords
249 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2) 295 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
250 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun 296 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
297 (font-lock-comment-start-regexp . ";")
251 (font-lock-mark-block-function . mark-defun))) 298 (font-lock-mark-block-function . mark-defun)))
252 (scheme-mode-defaults 299 (scheme-mode-defaults
253 '(scheme-font-lock-keywords 300 '(scheme-font-lock-keywords
254 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun 301 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
302 (font-lock-comment-start-regexp . ";")
255 (font-lock-mark-block-function . mark-defun))) 303 (font-lock-mark-block-function . mark-defun)))
256 ;; For TeX modes we could use `backward-paragraph' for the same reason. 304 ;; For TeX modes we could use `backward-paragraph' for the same reason.
257 ;; But we don't, because paragraph breaks are arguably likely enough to 305 ;; But we don't, because paragraph breaks are arguably likely enough to
258 ;; occur within a genuine syntactic block to make it too risky. 306 ;; occur within a genuine syntactic block to make it too risky.
259 ;; However, we do specify a MARK-BLOCK function as that cannot result 307 ;; However, we do specify a MARK-BLOCK function as that cannot result
260 ;; in a mis-fontification even if it might not fontify enough. --sm. 308 ;; in a mis-fontification even if it might not fontify enough. --sm.
261 (tex-mode-defaults '(tex-font-lock-keywords nil nil ((?$ . "\"")) nil 309 (tex-mode-defaults
262 (font-lock-mark-block-function . mark-paragraph))) 310 '(tex-font-lock-keywords nil nil ((?$ . "\"")) nil
311 (font-lock-comment-start-regexp . "%")
312 (font-lock-mark-block-function . mark-paragraph)))
263 ) 313 )
264 (list 314 (list
265 (cons 'bibtex-mode tex-mode-defaults)
266 (cons 'c++-c-mode c-mode-defaults) 315 (cons 'c++-c-mode c-mode-defaults)
267 (cons 'c++-mode c++-mode-defaults) 316 (cons 'c++-mode c++-mode-defaults)
268 (cons 'c-mode c-mode-defaults) 317 (cons 'c-mode c-mode-defaults)
@@ -317,7 +366,8 @@ around a text block relevant to that mode).
317Other variables include those for buffer-specialised fontification functions, 366Other variables include those for buffer-specialised fontification functions,
318`font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function', 367`font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
319`font-lock-fontify-region-function', `font-lock-unfontify-region-function', 368`font-lock-fontify-region-function', `font-lock-unfontify-region-function',
320`font-lock-maximum-size' and `font-lock-inhibit-thing-lock'.") 369`font-lock-comment-start-regexp', `font-lock-inhibit-thing-lock' and
370`font-lock-maximum-size'.")
321 371
322(defvar font-lock-keywords-only nil 372(defvar font-lock-keywords-only nil
323 "*Non-nil means Font Lock should not fontify comments or strings. 373 "*Non-nil means Font Lock should not fontify comments or strings.
@@ -347,6 +397,13 @@ When called with no args it should leave point at the beginning of any
347enclosing textual block and mark at the end. 397enclosing textual block and mark at the end.
348This is normally set via `font-lock-defaults'.") 398This is normally set via `font-lock-defaults'.")
349 399
400(defvar font-lock-comment-start-regexp nil
401 "*Regexp to match the start of a comment.
402This need not discriminate between genuine comments and quoted comment
403characters or comment characters within strings.
404If nil, `comment-start-skip' is used instead; see that variable for more info.
405This is normally set via `font-lock-defaults'.")
406
350(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer 407(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
351 "Function to use for fontifying the buffer. 408 "Function to use for fontifying the buffer.
352This is normally set via `font-lock-defaults'.") 409This is normally set via `font-lock-defaults'.")
@@ -783,12 +840,18 @@ delimit the region to fontify."
783(defun font-lock-fontify-syntactically-region (start end &optional loudly) 840(defun font-lock-fontify-syntactically-region (start end &optional loudly)
784 "Put proper face on each string and comment between START and END. 841 "Put proper face on each string and comment between START and END.
785START should be at the beginning of a line." 842START should be at the beginning of a line."
786 (let ((synstart (if comment-start-skip 843 (let ((synstart (cond (font-lock-comment-start-regexp
787 (concat "\\s\"\\|" comment-start-skip) 844 (concat "\\s\"\\|" font-lock-comment-start-regexp))
788 "\\s\"")) 845 (comment-start-skip
789 (comstart (if comment-start-skip 846 (concat "\\s\"\\|" comment-start-skip))
790 (concat "\\s<\\|" comment-start-skip) 847 (t
791 "\\s<")) 848 "\\s\"")))
849 (comstart (cond (font-lock-comment-start-regexp
850 font-lock-comment-start-regexp)
851 (comment-start-skip
852 (concat "\\s<\\|" comment-start-skip))
853 (t
854 "\\s<")))
792 state prev prevstate) 855 state prev prevstate)
793 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name))) 856 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
794 (goto-char start) 857 (goto-char start)
@@ -852,7 +915,7 @@ START should be at the beginning of a line."
852 (if (or (nth 4 state) (nth 7 state)) 915 (if (or (nth 4 state) (nth 7 state))
853 ;; 916 ;;
854 ;; We found a real comment start. 917 ;; We found a real comment start.
855 (let ((beg (match-beginning 0))) 918 (let ((beg (or (match-end 1) (match-beginning 0))))
856 (goto-char beg) 919 (goto-char beg)
857 (save-restriction 920 (save-restriction
858 (narrow-to-region (point-min) end) 921 (narrow-to-region (point-min) end)
@@ -868,7 +931,7 @@ START should be at the beginning of a line."
868 (if (nth 3 state) 931 (if (nth 3 state)
869 ;; 932 ;;
870 ;; We found a real string start. 933 ;; We found a real string start.
871 (let ((beg (match-beginning 0))) 934 (let ((beg (or (match-end 1) (match-beginning 0))))
872 (while (and (re-search-forward "\\s\"" end 'move) 935 (while (and (re-search-forward "\\s\"" end 'move)
873 (nth 3 (parse-partial-sexp here (point) 936 (nth 3 (parse-partial-sexp here (point)
874 nil nil state)))) 937 nil nil state))))
@@ -1488,7 +1551,15 @@ the face is also set; its value is the face name."
1488 ;; The expect syntax of an item is "word" or "word::word", possibly ending 1551 ;; The expect syntax of an item is "word" or "word::word", possibly ending
1489 ;; with optional whitespace and a "(". Everything following the item (but 1552 ;; with optional whitespace and a "(". Everything following the item (but
1490 ;; belonging to it) is expected to by skip-able by `forward-sexp', and items 1553 ;; belonging to it) is expected to by skip-able by `forward-sexp', and items
1491 ;; are expected to be separated with a "," or ";". 1554 ;; are expected to be separated with a ",".
1555 ;;
1556 ;; The regexp matches: word::word (
1557 ;; ^^^^ ^^^^ ^
1558 ;; Match subexps are: 1 3 4
1559 ;;
1560 ;; So, the item is delimited by (match-beginning 1) and (match-end 1).
1561 ;; If (match-beginning 3) is non-nil, that part of the item follows a ":".
1562 ;; If (match-beginning 4) is non-nil, the item is followed by a "(".
1492 (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?") 1563 (if (looking-at "[ \t*&]*\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*\\((\\)?")
1493 (save-match-data 1564 (save-match-data
1494 (condition-case nil 1565 (condition-case nil
@@ -1497,7 +1568,7 @@ the face is also set; its value is the face name."
1497 (narrow-to-region (point-min) limit) 1568 (narrow-to-region (point-min) limit)
1498 (goto-char (match-end 1)) 1569 (goto-char (match-end 1))
1499 ;; Move over any item value, etc., to the next item. 1570 ;; Move over any item value, etc., to the next item.
1500 (while (not (looking-at "[ \t]*\\([,;]\\|$\\)")) 1571 (while (not (looking-at "[ \t]*\\(,\\|$\\)"))
1501 (goto-char (or (scan-sexps (point) 1) (point-max)))) 1572 (goto-char (or (scan-sexps (point) 1) (point-max))))
1502 (goto-char (match-end 0))) 1573 (goto-char (match-end 0)))
1503 (error t))))) 1574 (error t)))))