home *** CD-ROM | disk | FTP | other *** search
- In article <9304191814.AA21726@Advent.com>, jduhamel@Advent.com (Joseph Duhamel) writes:
- >
- > Using Lemasc-19.6 I am having trouble getting the c++ / c keywords to
- > highlight. Is this just me or is anyone else haveing similar problems?
- >
-
- Here is my setup that seems to work very nicely. I'd requested c++ font lock
- ideas a while back and here is the sum of them. Note also the bug in
- c++-mode.el whereby # is treated as a comment. Barry says it should be fixed,
- but my version still has it.
-
- Also note that font-lock-any-extents-p has a bug to do with back-to-back
- extents. It must be in the font-lock.el file to be any good since it is a
- defsubst (i think).
-
- (defsubst font-lock-any-extents-p (start end)
- (catch 'done
- (map-extents (function (lambda (extent ignore)
- (if (and (eq 'font-lock (extent-data extent))
- (/= (extent-end-position extent) start))
- (throw 'done t))))
- (current-buffer) start end nil)
- nil))
-
- (defconst c-font-lock-keywords-1 nil
- "For consideration as a value of `c-font-lock-keywords'.
- This does fairly subdued highlighting.")
-
- (defconst c-font-lock-keywords-2 nil
- "For consideration as a value of `c-font-lock-keywords'.
- This does a lot more highlighting.")
-
- (defconst c-font-lock-keywords-3 nil
- "For consideration as a value of `c-font-lock-keywords'.
- This does a lot more highlighting.")
-
- (defconst c++-font-lock-keywords-1 nil
- "For consideration as a value of `c++-font-lock-keywords'.
- This does a lot more highlighting.")
-
- (let ((storage "auto\\|extern\\|register\\|static\\|volatile\\|const")
- (prefixes "unsigned\\|short\\|long")
- (types (concat "int\\|char\\|float\\|double\\|void\\|struct\\|"
- "union\\|enum\\|typedef"))
- (c++-types "public\\|private\\|protected\\|virtual\\|friend\\|inline")
- (ctoken "\\(\\sw\\|\\s_\\|[~*]\\)+")
- )
- (setq c-font-lock-keywords-1
- (list
- ;; fontify preprocessor directives as comments.
- '("^#[a-z]+" . font-lock-doc-string-face)
-
- ;;
- ;; fontify names being defined.
- '("^#[ \t]*\\(define\\|undef\\)[ \t]+\\(\\(\\sw\\|\\s_\\)+\\)" 2
- font-lock-function-name-face)
- ;;
- ;; fontify other preprocessor lines.
- '("^#[ \t]*\\(if\\|ifn?def\\)[ \t]+\\([^\n]+\\)"
- 2 font-lock-function-name-face t)
- ;;
- ;; fontify the filename in #include <...>
- ;; don't need to do this for #include "..." because those were
- ;; already fontified as strings by the syntactic pass.
- '("^#[ \t]*include[ \t]+<\\([^>\"\n]+\\)>" 1 font-lock-string-face)
- ;;
- ;; fontify the names of functions being defined.
- ;; I think this should be fast because it's anchored at bol, but it's not.
- (list (concat
- "^\\(" ctoken "[ \t]+\\)?" ; type specs; there can be no
- "\\(" ctoken "[ \t]+\\)?" ; more than 3 tokens, right?
- "\\(" ctoken "[ \t]+\\)?"
- "\\(\\*+[ \t]*\\)?" ; pointer
- ctoken "[ \t]*(") ; name
- 8 'font-lock-function-name-face)
- ;;
- ;; This is faster but not by much. I don't see why not.
- ; (list (concat "^" ctoken "[ \t]*(") 1 'font-lock-function-name-face)
- ;;
- ;; Fontify structure names (in structure definition form).
- (list (concat "^\\(typedef[ \t]+struct\\|struct\\|static[ \t]+struct\\)"
- "[ \t]+" ctoken "[ \t]*\\(\{\\|$\\)")
- 2 'font-lock-function-name-face)
- ;;
- ;; Fontify case clauses. This is fast because its anchored on the left.
- '("case[ \t]+\\(\\sw\\|\\s_\\)+:". 1)
- '("\\<\\(default\\):". 1)
- ))
-
- (setq c-font-lock-keywords-2
- (list
- ;;
- ;; fontify all storage classes and type specifiers
- (cons (concat "\\<\\(" storage "\\)\\>") 'font-lock-type-face)
- (cons (concat "\\<\\(" types "\\)\\>") 'font-lock-type-face)
- (cons (concat "\\<\\(" prefixes "[ \t]+" types "\\)\\>")
- 'font-lock-type-face)
- ;;
- ;; fontify all builtin tokens
- (cons (concat
- "[ \t]\\("
- (mapconcat 'identity
- '("for" "while" "do" "return" "goto" "case" "break" "switch"
- "if" "then" "else if" "else" "return" "default" "continue"
- "default"
- )
- "\\|")
- "\\)[ \t\n(){};,]")
- 1)))
-
- (setq c-font-lock-keywords-3
- (list
- ;;
- ;; fontify case targets and goto-tags. This is slow because the
- ;; expression is anchored on the right.
- "\\(\\sw\\|\\s_\\)+:"
-
- ;;
- ;; Fontify variables declared with structures, or typedef names.
- '("}[ \t*]*\\(\\sw\\|\\s_\\)+[ \t]*[,;]" 1 font-lock-function-name-face)
- ;;
- ;; Fontify global variables without a type.
- '("^\\([_a-zA-Z0-9:~*]+\\)[ \t]*[[;={]" 1 font-lock-function-name-face)
-
- ))
- (setq c++-font-lock-keywords-1
- (list
- ;;
- ;; fontify extra c++ storage classes and type specifiers
- (cons (concat "\\<\\(" c++-types "\\)\\>") 'font-lock-type-face)
-
- ;;special check for class
- '("^\\(\\<\\|template[ \t]+<[ \t]*\\)\\(class\\)[ \t\n]+" 2 font-lock-type-face)
-
- ;; fonify the class names only in the definition
- (list (concat "^class[ \t]+" ctoken "[ \t\n{: ;]") 1 'font-lock-function-name-face)
-
- (list (concat
- "^\\(" ctoken "[ \t]+\\)?" ; type specs; there can be no
- "\\(" ctoken "[ \t]+\\)?" ; more than 3 tokens, right?
- "\\(" ctoken "[ \t]+\\)?"
- "\\(\\*+[ \t]*\\)?" ; pointer
- "\\(" ctoken "\\(::\\)?~?\\(\\(operator[ \t]*[^ \ta-zA-Z]+\\)\\|" ctoken "\\)\\)[ \t]*(") ; name
- 8 'font-lock-function-name-face t)
-
- ;; special handling of template
- "^\\(template\\)\\>"
- ;; fontify extra c++ builtin tokens
- (cons (concat
- "[ \t]\\("
- (mapconcat 'identity
- '("asm" "catch" "throw" "try" "delete" "new" "operator"
- "sizeof" "this"
- )
- "\\|")
- "\\)[ \t\n(){};,]")
- 1)
- ))
-
- )
-
- (defvar c-font-lock-keywords (append c-font-lock-keywords-1
- c-font-lock-keywords-2)
- "Additional expressions to highlight in C mode.")
-
- (defvar c++-font-lock-keywords (append c-font-lock-keywords-1
- c-font-lock-keywords-2
- c++-font-lock-keywords-1)
- "Additional expressions to highlight in C++ mode.")
-
- --
- -------------------------------------------------------------------------------
- | Tibor L. Polgar | (408) 746-8649 | tlp00@climb.ras.amdahl.com
- | Amdahl Corporation
- | 1250 East Arques Avenue (M/S 148)
- | P.O. Box 3470
- | Sunnyvale, CA 94088-3470
- -------------------------------------------------------------------------------
-
-