home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / bug-lucid-emacs / text0082.txt < prev    next >
Encoding:
Text File  |  1993-07-04  |  6.3 KB  |  180 lines

  1. In article <9304191814.AA21726@Advent.com>, jduhamel@Advent.com (Joseph Duhamel) writes:
  2. > Using Lemasc-19.6 I am having trouble getting the c++ / c keywords to
  3. > highlight. Is this just me or is anyone else haveing similar problems?
  4.  
  5. Here is my setup that seems to work very nicely. I'd requested c++ font lock
  6. ideas a while back and here is the sum of them.  Note also the bug in
  7. c++-mode.el whereby # is treated as a comment.  Barry says it should be fixed,
  8. but my version still has it.
  9.  
  10. Also note that font-lock-any-extents-p has a bug to do with back-to-back 
  11. extents.  It must be in the font-lock.el file to be any good since it is a 
  12. defsubst (i think).
  13.  
  14. (defsubst font-lock-any-extents-p (start end)
  15.   (catch 'done
  16.     (map-extents (function (lambda (extent ignore)
  17.                  (if (and (eq 'font-lock (extent-data extent))
  18.                       (/= (extent-end-position extent) start))
  19.                  (throw 'done t))))
  20.          (current-buffer) start end nil)
  21.     nil))
  22.  
  23. (defconst c-font-lock-keywords-1 nil
  24.  "For consideration as a value of `c-font-lock-keywords'.
  25. This does fairly subdued highlighting.")
  26.  
  27. (defconst c-font-lock-keywords-2 nil
  28.  "For consideration as a value of `c-font-lock-keywords'.
  29. This does a lot more highlighting.")
  30.  
  31. (defconst c-font-lock-keywords-3 nil
  32.  "For consideration as a value of `c-font-lock-keywords'.
  33. This does a lot more highlighting.")
  34.  
  35. (defconst c++-font-lock-keywords-1 nil
  36.  "For consideration as a value of `c++-font-lock-keywords'.
  37. This does a lot more highlighting.")
  38.  
  39. (let ((storage "auto\\|extern\\|register\\|static\\|volatile\\|const")
  40.       (prefixes "unsigned\\|short\\|long")
  41.       (types (concat "int\\|char\\|float\\|double\\|void\\|struct\\|"
  42.              "union\\|enum\\|typedef"))
  43.       (c++-types "public\\|private\\|protected\\|virtual\\|friend\\|inline")
  44.       (ctoken "\\(\\sw\\|\\s_\\|[~*]\\)+")
  45.       )
  46.   (setq c-font-lock-keywords-1
  47.    (list
  48.      ;; fontify preprocessor directives as comments.
  49.      '("^#[a-z]+" . font-lock-doc-string-face)
  50.  
  51.     ;;
  52.     ;; fontify names being defined.
  53.     '("^#[ \t]*\\(define\\|undef\\)[ \t]+\\(\\(\\sw\\|\\s_\\)+\\)" 2
  54.       font-lock-function-name-face)
  55.     ;;
  56.     ;; fontify other preprocessor lines.
  57.     '("^#[ \t]*\\(if\\|ifn?def\\)[ \t]+\\([^\n]+\\)"
  58.       2 font-lock-function-name-face t)
  59.     ;;
  60.     ;; fontify the filename in #include <...>
  61.     ;; don't need to do this for #include "..." because those were
  62.     ;; already fontified as strings by the syntactic pass.
  63.     '("^#[ \t]*include[ \t]+<\\([^>\"\n]+\\)>" 1 font-lock-string-face)
  64.     ;;
  65.     ;; fontify the names of functions being defined.
  66.     ;; I think this should be fast because it's anchored at bol, but it's not.
  67.     (list (concat
  68.        "^\\(" ctoken "[ \t]+\\)?"    ; type specs; there can be no
  69.        "\\(" ctoken "[ \t]+\\)?"    ; more than 3 tokens, right?
  70.        "\\(" ctoken "[ \t]+\\)?"
  71.        "\\(\\*+[ \t]*\\)?"        ; pointer
  72.        ctoken "[ \t]*(")        ; name
  73.       8 'font-lock-function-name-face)
  74.     ;;
  75.     ;; This is faster but not by much.  I don't see why not.
  76. ;    (list (concat "^" ctoken "[ \t]*(") 1 'font-lock-function-name-face)
  77.     ;;
  78.     ;; Fontify structure names (in structure definition form).
  79.     (list (concat "^\\(typedef[ \t]+struct\\|struct\\|static[ \t]+struct\\)"
  80.           "[ \t]+" ctoken "[ \t]*\\(\{\\|$\\)")
  81.       2 'font-lock-function-name-face)
  82.     ;;
  83.     ;; Fontify case clauses.  This is fast because its anchored on the left.
  84.     '("case[ \t]+\\(\\sw\\|\\s_\\)+:". 1)
  85.     '("\\<\\(default\\):". 1)
  86.     ))
  87.  
  88.   (setq c-font-lock-keywords-2
  89.      (list
  90.      ;;
  91.      ;; fontify all storage classes and type specifiers
  92.      (cons (concat "\\<\\(" storage "\\)\\>") 'font-lock-type-face)
  93.      (cons (concat "\\<\\(" types "\\)\\>") 'font-lock-type-face)
  94.      (cons (concat "\\<\\(" prefixes "[ \t]+" types "\\)\\>")
  95.        'font-lock-type-face)
  96.      ;;
  97.      ;; fontify all builtin tokens
  98.      (cons (concat
  99.         "[ \t]\\("
  100.         (mapconcat 'identity
  101.          '("for" "while" "do" "return" "goto" "case" "break" "switch"
  102.            "if" "then" "else if" "else" "return" "default" "continue"
  103.            "default"
  104.            )
  105.          "\\|")
  106.         "\\)[ \t\n(){};,]")
  107.        1)))
  108.  
  109.   (setq c-font-lock-keywords-3
  110.     (list
  111.      ;;
  112.      ;; fontify case targets and goto-tags.  This is slow because the
  113.      ;; expression is anchored on the right.
  114.      "\\(\\sw\\|\\s_\\)+:"
  115.  
  116.      ;;
  117.      ;; Fontify variables declared with structures, or typedef names.
  118.      '("}[ \t*]*\\(\\sw\\|\\s_\\)+[ \t]*[,;]" 1 font-lock-function-name-face)
  119.      ;;
  120.      ;; Fontify global variables without a type.
  121.      '("^\\([_a-zA-Z0-9:~*]+\\)[ \t]*[[;={]" 1 font-lock-function-name-face)
  122.  
  123.      ))
  124. (setq c++-font-lock-keywords-1
  125.       (list
  126.        ;;
  127.        ;; fontify extra c++ storage classes and type specifiers
  128.        (cons (concat "\\<\\(" c++-types "\\)\\>") 'font-lock-type-face)
  129.  
  130.        ;;special check for class
  131.        '("^\\(\\<\\|template[ \t]+<[ \t]*\\)\\(class\\)[ \t\n]+" 2 font-lock-type-face)
  132.  
  133.        ;; fonify the class names only in the definition
  134.        (list (concat "^class[ \t]+" ctoken "[ \t\n{: ;]") 1 'font-lock-function-name-face)
  135.  
  136.        (list (concat
  137.           "^\\(" ctoken "[ \t]+\\)?" ; type specs; there can be no
  138.           "\\(" ctoken "[ \t]+\\)?" ; more than 3 tokens, right?
  139.           "\\(" ctoken "[ \t]+\\)?"
  140.           "\\(\\*+[ \t]*\\)?"    ; pointer
  141.           "\\(" ctoken "\\(::\\)?~?\\(\\(operator[ \t]*[^ \ta-zA-Z]+\\)\\|" ctoken "\\)\\)[ \t]*(") ; name
  142.          8 'font-lock-function-name-face t)
  143.  
  144.        ;; special handling of template
  145.        "^\\(template\\)\\>"
  146.        ;; fontify extra c++ builtin tokens
  147.        (cons (concat
  148.           "[ \t]\\("
  149.           (mapconcat 'identity
  150.              '("asm" "catch" "throw" "try" "delete" "new" "operator"
  151.                "sizeof" "this"
  152.                )
  153.              "\\|")
  154.           "\\)[ \t\n(){};,]")
  155.          1)
  156.        ))
  157.   
  158. )
  159.  
  160. (defvar c-font-lock-keywords (append c-font-lock-keywords-1
  161.                      c-font-lock-keywords-2)
  162.   "Additional expressions to highlight in C mode.")
  163.  
  164. (defvar c++-font-lock-keywords (append c-font-lock-keywords-1
  165.                      c-font-lock-keywords-2
  166.                      c++-font-lock-keywords-1)
  167.   "Additional expressions to highlight in C++ mode.")
  168.  
  169. -- 
  170. -------------------------------------------------------------------------------
  171. | Tibor L. Polgar    |  (408) 746-8649     | tlp00@climb.ras.amdahl.com
  172. | Amdahl Corporation
  173. | 1250 East Arques Avenue (M/S 148)
  174. | P.O. Box 3470
  175. | Sunnyvale, CA 94088-3470
  176. -------------------------------------------------------------------------------
  177.  
  178.