comparison lisp/packages/font-lock.el @ 76:c0c698873ce1 r20-0b33

Import from CVS: tag r20-0b33
author cvs
date Mon, 13 Aug 2007 09:05:10 +0200
parents 131b0175ea99
children 821dec489c24
comparison
equal deleted inserted replaced
75:a4e0195b387b 76:c0c698873ce1
74 ;; 74 ;;
75 ;; The same is true of `c-font-lock-keywords-1' and `c-font-lock-keywords-2'; 75 ;; The same is true of `c-font-lock-keywords-1' and `c-font-lock-keywords-2';
76 ;; the former is subdued, the latter is loud. 76 ;; the former is subdued, the latter is loud.
77 ;; 77 ;;
78 ;; You can make font-lock default to the gaudier variety of keyword 78 ;; You can make font-lock default to the gaudier variety of keyword
79 ;; highlighting by setting the variable `font-lock-use-maximal-decoration' 79 ;; highlighting by setting the variable `font-lock-maximum-decoration'
80 ;; before loading font-lock, or by calling the functions 80 ;; before loading font-lock, or by calling the functions
81 ;; `font-lock-use-default-maximal-decoration' or 81 ;; `font-lock-use-default-maximal-decoration' or
82 ;; `font-lock-use-default-minimal-decoration'. 82 ;; `font-lock-use-default-minimal-decoration'.
83 ;; 83 ;;
84 ;; On a Sparc10, the initial fontification takes about 6 seconds for a typical 84 ;; On a Sparc10, the initial fontification takes about 6 seconds for a typical
137 ;; Don't use it to, say, highlight keywords in commented out code or strings. 137 ;; Don't use it to, say, highlight keywords in commented out code or strings.
138 ;; - Err, that's it. 138 ;; - Err, that's it.
139 139
140 140
141 ;;; Code: 141 ;;; Code:
142
143 (require 'fontl-hooks)
142 144
143 ;;;;;;;;;;;;;;;;;;;;;; user variables ;;;;;;;;;;;;;;;;;;;;;; 145 ;;;;;;;;;;;;;;;;;;;;;; user variables ;;;;;;;;;;;;;;;;;;;;;;
144 146
145 (defvar font-lock-verbose t 147 (defvar font-lock-verbose t
146 "*If non-nil, means show status messages when fontifying. 148 "*If non-nil, means show status messages when fontifying.
1511 c++-font-lock-keywords-3) 1513 c++-font-lock-keywords-3)
1512 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun)) 1514 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun))
1513 1515
1514 (put 'java-mode 'font-lock-defaults 1516 (put 'java-mode 'font-lock-defaults
1515 '((java-font-lock-keywords 1517 '((java-font-lock-keywords
1516 java-font-lock-keywords-1 java-font-lock-keywords-2) 1518 java-font-lock-keywords-1 java-font-lock-keywords-2
1517 nil nil ((?_ . "w")) beginning-of-defun)) 1519 java-font-lock-keywords-3)
1520 nil nil ((?_ . "w")) beginning-of-defun
1521 (font-lock-mark-block-function . mark-defun)))
1518 1522
1519 (put 'lisp-mode 'font-lock-defaults 1523 (put 'lisp-mode 'font-lock-defaults
1520 '((lisp-font-lock-keywords 1524 '((lisp-font-lock-keywords
1521 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2) 1525 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
1522 nil nil 1526 nil nil
2174 ; ) 2178 ; )
2175 ; 2179 ;
2176 ;(defconst c++-font-lock-keywords c++-font-lock-keywords-1 2180 ;(defconst c++-font-lock-keywords c++-font-lock-keywords-1
2177 ; "Additional expressions to highlight in C++ mode.") 2181 ; "Additional expressions to highlight in C++ mode.")
2178 2182
2183 ;; Java support from Anders Lindgren and Bob Weiner
2184
2179 (defconst java-font-lock-keywords-1 nil 2185 (defconst java-font-lock-keywords-1 nil
2180 "For consideration as a value of `java-font-lock-keywords'. 2186 "For consideration as a value of `java-font-lock-keywords'.
2181 This does fairly subdued highlighting.") 2187 This does fairly subdued highlighting.")
2182 2188
2183 (defconst java-font-lock-keywords-2 nil 2189 (defconst java-font-lock-keywords-2 nil
2184 "For consideration as a value of `java-font-lock-keywords'. 2190 "For consideration as a value of `java-font-lock-keywords'.
2185 This does a lot more highlighting.") 2191 This adds highlighting of types and identifier names.")
2186 2192
2187 (let ((storage (concat "static\\|abstract\\|const\\|final\\|" 2193 (defconst java-font-lock-keywords-3 nil
2188 "synchronized\\|threadsafe\\|transient\\|native")) 2194 "For consideration as a value of `java-font-lock-keywords'.
2189 (types (concat 2195 This adds highlighting of Java documentation tags, such as @see.")
2190 "boolean\\|int\\|char\\|byte\\|short\\|long\\|" 2196
2191 "float\\|double\\|void")) 2197 (defvar java-font-lock-type-regexp
2192 (reserved-words 2198 (concat "\\<\\(boolean\\|byte\\|char\\|double\\|float\\|int"
2193 '("private" "protected" "public" "break" "byvalue" 2199 "\\|long\\|short\\|void\\)\\>")
2194 "case" "catch" "class" 2200 "Regexp which should match a primitive type.")
2195 "continue" "default" "do" "else if" 2201
2196 "else" "extends" "false" "finally" 2202 (let ((capital-letter "A-Z\300-\326\330-\337")
2197 "for" "if" "implements" "import" 2203 (letter "a-zA-Z_$\300-\326\330-\366\370-\377")
2198 "instanceof" "interface" 2204 (digit "0-9"))
2199 "new" "null" "package" "return" 2205 (defvar java-font-lock-identifier-regexp
2200 "super" "switch" 2206 (concat "\\<\\([" letter "][" letter digit "]*\\)\\>")
2201 "this" "throw" "throws" 2207 "Regexp which should match all Java identifiers.")
2202 "true" "try" "synchronize" "while")) 2208
2203 2209 (defvar java-font-lock-class-name-regexp
2204 ; (java-token "\\w+") 2210 (concat "\\<\\([" capital-letter "][" letter digit "]*\\)\\>")
2205 (java-token "[a-zA-Z0-9_\.]+") 2211 "Regexp which should match a class or an interface name.
2206 (java-modifying-token "[a-zA-Z0-9_\.]+\\([ \t]*\\[\\]\\)?") 2212 The name is assumed to begin with a capital letter.")
2207 ) 2213 )
2214
2215
2216 (let ((java-modifier-regexp
2217 (concat "\\<\\(abstract\\|const\\|final\\|native\\|"
2218 "private\\|protected\\|public\\|"
2219 "static\\|synchronized\\|transient\\|volatile\\)\\>")))
2220
2221 ;; Basic font-lock support:
2208 (setq java-font-lock-keywords-1 2222 (setq java-font-lock-keywords-1
2209 (list 2223 (list
2210 ;;------------------------------------------------------------------ 2224 ;; Keywords:
2211 ;; fontify C++-style comments as comments. 2225 (list
2212 ;;------------------------------------------------------------------ 2226 (concat
2213 2227 "\\<\\("
2214 '("//.*" . font-lock-comment-face) 2228 "break\\|byvalue\\|"
2215 2229 "case\\|cast\\|catch\\|class\\|continue\\|"
2216 ;;------------------------------------------------------------------ 2230 "do\\|else\\|extends\\|"
2217 ;; I think static deserves special attention 2231 "finally\\|for\\|future\\|"
2218 ;;------------------------------------------------------------------ 2232 "generic\\|goto\\|"
2219 2233 "if\\|implements\\|import\\|"
2220 '("static" . font-lock-keyword-face) 2234 "instanceof\\|interface\\|"
2221 2235 "new\\|package\\|return\\|switch\\|"
2222 ;;------------------------------------------------------------------ 2236 "throws?\\|try\\|while\\)\\>")
2223 ;; Make the "public" keyword standout (should we do private instead?) 2237 1 'font-lock-keyword-face)
2224 ;;------------------------------------------------------------------ 2238
2225 2239 ;; Modifiers:
2226 ;; these depend on some personal SMF faces 2240 (list java-modifier-regexp 1 font-lock-type-face)
2227 2241
2228 ;; I wish I knew elisp, etc enough to know if it's 2242 ;; Special constants:
2229 ;; faster to use 1 regex for n words, or n 2243 '("\\<\\(this\\|super\\)\\>" (1 font-lock-reference-face))
2230 ;; unglobbed entries in this list... 2244 '("\\<\\(false\\|null\\|true\\)\\>" (1 font-lock-keyword-face))
2231 2245
2232 ;; '("private" . font-lock-pale-face) 2246 ;; Class names:
2233 ;; '("protected" . font-lock-pale-face) 2247 (list (concat "\\<class\\>\\s *" java-font-lock-identifier-regexp)
2234 2248 1 'font-lock-function-name-face)
2235 ;;------------------------------------------------------------------ 2249
2236 ;; special case so "new Foo();" doesn't map to method declaration 2250 ;; Package declarations:
2237 ;;------------------------------------------------------------------ 2251 (list (concat "\\<\\(package\\|import\\)\\>\\s *"
2238 2252 java-font-lock-identifier-regexp)
2239 (list (concat 2253 '(2 font-lock-reference-face)
2240 "^.*[ \t]+\\(new\\|return\\)[ \t]+" 2254 (list (concat
2241 "\\(" java-token "\\)") 2255 "\\=\\.\\(" java-font-lock-identifier-regexp "\\)")
2242 2 'default) 2256 nil nil '(1 (if (= (char-after (match-end 0)) ?.)
2243 2257 'font-lock-reference-face
2244 ;;------------------------------------------------------------------ 2258 'font-lock-type-face))))
2245 ;; special case so "else if();" doesn't map to method declaration 2259
2246 ;;------------------------------------------------------------------ 2260 ;; Constructors:
2247 2261 (list (concat
2248 (list "^.*[ \t]+else[ \t]+\\(if\\)" 2262 "^\\s *\\(" java-modifier-regexp "\\s +\\)*"
2249 1 'default) 2263 java-font-lock-class-name-regexp "\\s *\(")
2250 2264 (list 3
2251 ;;------------------------------------------------------------------ 2265 '(condition-case nil
2252 ;; METHOD IDENTIFICATION 2266 (save-excursion
2253 ;; 2267 (goto-char (scan-sexps (- (match-end 0) 1) 1))
2254 ;; fontify the (first word of) names of methods being defined. 2268 (parse-partial-sexp (point) (point-max) nil t)
2255 ;;------------------------------------------------------------------ 2269 (and (looking-at "\\($\\|\\<throws\\>\\|{\\)")
2256 2270 'font-lock-function-name-face))
2257 (list (concat 2271 (error 'font-lock-function-name-face))))
2258 "^[ \t]+" ;; indent of line 2272
2259 2273 ;; Methods:
2260 ;;------------------------------------------------------- 2274 (list (concat "\\(" java-font-lock-type-regexp "\\|"
2261 ;; Demanding a token 1st doesn't recognize constructors 2275 java-font-lock-class-name-regexp "\\)"
2262 ;; w/out any access specifiers. Unfortunately, that also 2276 "\\s *\\(\\[\\s *\\]\\s *\\)*"
2263 ;; looks like many other things, including "if (foo) {", 2277 java-font-lock-identifier-regexp "\\s *\(")
2264 ;; so it's not an easy case to detect -- I'm just going 2278 5
2265 ;; to live w/out it for now... 2279 'font-lock-function-name-face)
2266 ;;------------------------------------------------------- 2280
2267 2281 ;; Labels:
2268 "\\(" java-modifying-token "[ \t]+\\)" 2282 (list ":"
2269 2283 (list
2270 ; "\\(" 2284 (concat "^\\s *" java-font-lock-identifier-regexp "\\s *:")
2271 ; "\\(^[ \t]+\\(" java-token "[ \t]+\\)\\)" 2285 '(beginning-of-line) '(end-of-line)
2272 ; "\\|" 2286 '(1 font-lock-reference-face)))
2273 ; "\\( \\)" 2287
2274 ; "\\)" 2288 ;; `break' and continue' destination labels:
2275 ; failed attempt to hack in ^ followed by exactly 4 spaces allowance to 2289 (list (concat "\\<\\(break\\|continue\\)\\>\\s *"
2276 ; recognize constructions with no access specified 2290 java-font-lock-identifier-regexp)
2277 2291 2 'font-lock-reference-face)
2278 "\\(" java-modifying-token "[ \t]+\\)?" 2292
2279 "\\(" java-modifying-token "[ \t]+\\)?" 2293 ;; Case statements:
2280 "\\(" java-modifying-token "[ \t]+\\)?" 2294 ;; In Java, any constant expression is allowed.
2281 "\\(" java-modifying-token "[ \t]+\\)?" 2295 '("\\<case\\>\\s *\\(.*\\):" 1 font-lock-reference-face)))
2282 "\\(" java-modifying-token "[ \t]+\\)?" 2296
2283 "\\(" java-token "\\)[ \t]*(") 2297 ;; Types and declared variable names:
2284
2285 ; "\\(" java-token "\\)[ \t]*(.*{")
2286
2287 ;; SMF: while demanding { at EOL is stylistic,
2288 ;; it DOESN'T hilite the likes of:
2289 ;; return new Dimension()
2290 ;; "\\(" java-token "\\)[ \t]*(")
2291 ;; PROBLEM -- it leaves out abstract and native methods!
2292
2293 13 'font-lock-function-name-face)
2294
2295 ;;------------------------------------------------------------------
2296 ;; Fontify class names ...
2297 ;; (info-node is another personal SMF face)
2298 ;;------------------------------------------------------------------
2299
2300 (list (concat
2301 "^[ \t]*\\([a-z]+[ \t]+\\)?\\([a-z]+[ \t]+\\)?class[ \t]+\\("
2302 java-token "\\)")
2303 3 'font-lock-reference-face)
2304
2305 ;;------------------------------------------------------------------
2306 ;; Package names
2307 ;;------------------------------------------------------------------
2308
2309 (list (concat
2310 "package[ \t]+\\(" java-token "\\)")
2311 1 'font-lock-reference-face)
2312
2313 ;;
2314 ;; Fontify case clauses. This is fast because its anchored on the left.
2315
2316 '("case[ \t]+\\(\\(\\sw\\|\\s_\\)+\\):". 1)
2317 '("\\<\\(default\\):". 1)
2318 ))
2319
2320 (setq java-font-lock-keywords-2 2298 (setq java-font-lock-keywords-2
2321 (append java-font-lock-keywords-1 2299 (append
2322 (list 2300
2323 ;; 2301 java-font-lock-keywords-1
2324 ;; fontify all storage classes and type specifiers 2302 (list
2325 (cons (concat "\\<\\(" storage "\\)\\>") 'font-lock-type-face) 2303 ;; Keywords followed by a type:
2326 (cons (concat "\\<\\(" types "\\)\\>") 'font-lock-type-face) 2304 (list (concat "\\<\\(extends\\|instanceof\\|new\\)\\>\\s *"
2327 2305 java-font-lock-identifier-regexp)
2328 ;; 2306 '(2 (if (= (char-after (match-end 0)) ?.)
2329 ;; fontify all builtin tokens 2307 'font-lock-reference-face 'font-lock-type-face))
2330 (cons (concat 2308 (list (concat "\\=\\." java-font-lock-identifier-regexp)
2331 "[ \t]\\(" 2309 '(goto-char (match-end 0)) nil
2332 (mapconcat 'identity reserved-words "\\|") 2310 '(1 (if (= (char-after (match-end 0)) ?.)
2333 "\\)[ \t\n(){};,]") 2311 'font-lock-reference-face 'font-lock-type-face))))
2334 1) 2312
2335 (cons (concat 2313 ;; Keywords followed by a type list:
2336 "^\\(" 2314 (list (concat "\\<\\(implements\\|throws\\)\\>\\ s*"
2337 (mapconcat 'identity reserved-words "\\|") 2315 java-font-lock-identifier-regexp)
2338 "\\)[ \t\n(){};,]") 2316 '(2 (if (= (char-after (match-end 0)) ?.)
2339 1) 2317 font-lock-reference-face font-lock-type-face))
2340 ))) 2318 (list (concat "\\=\\(\\.\\|\\s *\\(,\\)\\s *\\)"
2319 java-font-lock-identifier-regexp)
2320 '(goto-char (match-end 0)) nil
2321 '(3 (if (= (char-after (match-end 0)) ?.)
2322 font-lock-reference-face font-lock-type-face))))
2323
2324 ;; primitive types, can't be confused with anything else.
2325 (list java-font-lock-type-regexp
2326 '(1 font-lock-type-face)
2327 '(font-lock-match-java-declarations
2328 (goto-char (match-end 0))
2329 (goto-char (match-end 0))
2330 (0 font-lock-variable-name-face)))
2331
2332 ;; Declarations, class types and capitalized variables:
2333 ;;
2334 ;; Declarations are easy to recognize. Capitalized words
2335 ;; followed by a closing parenthesis are treated as casts if they
2336 ;; also are followed by an expression. Expressions beginning with
2337 ;; a unary numerical operator, e.g. +, can't be cast to an object
2338 ;; type.
2339 ;;
2340 ;; The path of a fully qualified type, e.g. java.lang.Foo, is
2341 ;; fontified in the reference face.
2342 ;;
2343 ;; An access to a static field, e.g. System.out.println, is
2344 ;; not fontified since it can't be distinguished from the
2345 ;; usage of a capitalized variable, e.g. Foo.out.println.
2346
2347 (list (concat java-font-lock-class-name-regexp
2348 "\\s *\\(\\[\\s *\\]\\s *\\)*"
2349 "\\(\\<\\|$\\|)\\s *\\([\(\"]\\|\\<\\)\\)")
2350 '(1 (save-match-data
2351 (save-excursion
2352 (goto-char
2353 (match-beginning 3))
2354 (if (not (looking-at "\\<instanceof\\>"))
2355 'font-lock-type-face))))
2356 (list (concat "\\=" java-font-lock-identifier-regexp "\\.")
2357 '(progn
2358 (goto-char (match-beginning 0))
2359 (while (or (= (preceding-char) ?.)
2360 (= (char-syntax (preceding-char)) ?w))
2361 (backward-char)))
2362 '(goto-char (match-end 0))
2363 '(1 font-lock-reference-face)
2364 '(0 nil)) ; Workaround for bug in XEmacs.
2365 '(font-lock-match-java-declarations
2366 (goto-char (match-end 1))
2367 (goto-char (match-end 0))
2368 (1 font-lock-variable-name-face))))))
2369
2370 ;; Modifier keywords and Java doc tags
2371 (setq java-font-lock-keywords-3
2372 (append
2373
2374 '(
2375 ;; Feature scoping:
2376 ;; These must come first or the Modifiers from keywords-1 will
2377 ;; catch them. We don't want to use override fontification here
2378 ;; because then these terms will be fontified within comments.
2379 ("\\<private\\>" 0 font-lock-string-face)
2380 ("\\<protected\\>" 0 font-lock-preprocessor-face)
2381 ("\\<public\\>" 0 font-lock-reference-face))
2382 java-font-lock-keywords-2
2383
2384 (list
2385
2386 ;; Java doc tags
2387 '("@\\(author\\|exception\\|param\\|return\\|see\\|version\\)\\s "
2388 0 font-lock-keyword-face t)
2389
2390 ;; Doc tag - Parameter identifiers
2391 (list (concat "@param\\s +" java-font-lock-identifier-regexp)
2392 1 'font-lock-variable-name-face t)
2393
2394 ;; Doc tag - Exception types
2395 (list (concat "@exception\\ s*"
2396 java-font-lock-identifier-regexp)
2397 '(1 (if (= (char-after (match-end 0)) ?.)
2398 font-lock-reference-face font-lock-type-face) t)
2399 (list (concat "\\=\\." java-font-lock-identifier-regexp)
2400 '(goto-char (match-end 0)) nil
2401 '(1 (if (= (char-after (match-end 0)) ?.)
2402 'font-lock-reference-face 'font-lock-type-face) t)))
2403
2404 ;; Doc tag - Cross-references, usually to methods
2405 '("@see\\s +\\(\\S *[^][ \t\n\r\f(){},.;:]\\)"
2406 1 font-lock-function-name-face t)
2407
2408 )))
2341 ) 2409 )
2342 2410
2343 (defvar java-font-lock-keywords java-font-lock-keywords-1 2411 (defvar java-font-lock-keywords java-font-lock-keywords-1
2344 "Additional expressions to highlight in Java mode.") 2412 "Additional expressions to highlight in Java mode.")
2413
2414 ;; Match and move over any declaration/definition item after
2415 ;; point. Does not match items which look like a type declaration
2416 ;; (primitive types and class names, i.e. capitalized words.)
2417 ;; Should the variable name be followed by a comma, we reposition
2418 ;; the cursor to fontify more identifiers.
2419 (defun font-lock-match-java-declarations (limit)
2420 "Match and skip over variable definitions."
2421 (if (looking-at "\\s *\\(\\[\\s *\\]\\s *\\)*")
2422 (goto-char (match-end 0)))
2423 (and
2424 (looking-at java-font-lock-identifier-regexp)
2425 (save-match-data
2426 (not (string-match java-font-lock-type-regexp
2427 (buffer-substring (match-beginning 1)
2428 (match-end 1)))))
2429 (save-match-data
2430 (save-excursion
2431 (goto-char (match-beginning 1))
2432 (not (looking-at
2433 (concat java-font-lock-class-name-regexp
2434 "\\s *\\(\\[\\s *\\]\\s *\\)*\\<")))))
2435 (save-match-data
2436 (condition-case nil
2437 (save-restriction
2438 (narrow-to-region (point-min) limit)
2439 (goto-char (match-end 0))
2440 ;; Note: Both `scan-sexps' and the second goto-char can
2441 ;; generate an error which is caught by the
2442 ;; `condition-case' expression.
2443 (while (not (looking-at "\\s *\\(\\(,\\)\\|;\\|$\\)"))
2444 (goto-char (or (scan-sexps (point) 1) (point-max))))
2445 (goto-char (match-end 2))) ; non-nil
2446 (error t)))))
2447
2345 2448
2346 (defvar tex-font-lock-keywords 2449 (defvar tex-font-lock-keywords
2347 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>. 2450 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
2348 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}" 2451 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
2349 ; 2 font-lock-function-name-face) 2452 ; 2 font-lock-function-name-face)