0

I am trying to customize some added words to be colored differently from the default face-font colors.

This is what I am doing:

(defconst lconfig-font-lock-faces  
  (list

   '(font-lock-function-name-face
     ((((class color)) (:foreground "DarkBlue"  :bold t))))

   '(font-lock-constant-face
     ((((class color)) (:foreground "Black"     :bold t))))


   '(font-lock-builtin-face
     ((((class color)) (:foreground nil))))

   '(font-lock-preprocessor-face
     ((((class color)) (:foreground nil))))

   )
  )

(autoload 'custom-set-faces "font-lock" "Set the color scheme" t)
(autoload 'font-lock-fontify-buffer "font-lock" "Fontify Buffer" t)
(progn (apply 'custom-set-faces lconfig-font-lock-faces)
       (add-hook 'c-mode-common-hook 'font-lock-fontify-buffer)
       (add-hook 'emacs-lisp-mode-hook 'font-lock-fontify-buffer)
       )
(global-font-lock-mode t)

(font-lock-add-keywords
 'c-mode
 '(
   ("^#[ \t]*\\(ifdef\\|else\\|ifndef\\|if !?defined\\|if\\|elif\\|endif\\|ident\\).*$" 1 font-lock-constant-face)                  ;#defines
   ("\\(^#[ \t]*define\\|^#[ \t]*include\\|^#[ \t]*undef\\).*$" 1 font-lock-function-name-face)                                     ;other #s
 )
)

unfortunately when opening a c file I see #include and #define being black colored. Although they should match the regular expressions and turn to dark blue. Also #ifdef and #endif is in light dark color and not bold.

Any help is appreciated.

Drew
  • 29,895
  • 7
  • 74
  • 104
SFbay007
  • 1,917
  • 1
  • 20
  • 39
  • It looks like your are using `defconst` instead of `setq` or `custom-set-variables` to change existing variables. I could be wrong, but I don't think `defconst` is an acceptable substitute under those circumstances -- I'd be interested to know one way or the other. I've added a few samples of what does work within an answer below. – lawlist Jul 31 '13 at 23:36

1 Answers1

1

Here are some examples that you could modify -- e.g., change to c-mode instead of latex-mode, and modify it with your colors and regexp. As the definitions become more complex, the order in which they appear in the list may trump previous definitions -- so if what your doing should be working, then check the order in which they appear.

(defvar lawlist-super-HotPink1 (make-face 'lawlist-super-HotPink1))
(set-face-attribute 'lawlist-super-HotPink1 nil :background "white" :foreground "HotPink1" :bold t :underline nil :font "Courier" :height 200)

(defvar lawlist-super-SeaGreen (make-face 'lawlist-super-SeaGreen))
(set-face-attribute 'lawlist-super-SeaGreen nil :background "white" :foreground "SeaGreen" :bold t :underline nil :font "Courier" :height 200)

(defvar lawlist-keywords-01
    (concat "\\b\\(?:"
        (regexp-opt (list "INSERT" "CLIENT" "RECIPIENT" "document-name" ))
    "\\)\\b"))

(defvar lawlist-keywords-02
    (list (concat "\\b\\(?:"
        (regexp-opt (list "FIXME" "TO-DO" "BUGS" ))
    "\\)\\b") 0 'lawlist-red t))

(font-lock-add-keywords 'latex-mode (list

    (list (concat "\\(\{\\)\\(\\\\bf\\)\\(\\\\uline\\)\\(\{\\)\\(\\(.\\|\n\\)+?\\)\\(\}\\)\\(\}\\)")
        '(1 lawlist-regular t)
        '(2 lawlist-red t)
        '(3 lawlist-blue t)
        '(4 lawlist-regular t)
        '(5 lawlist-bold-underline t)
        '(7 lawlist-regular t)
        '(8 lawlist-regular t))

    (list (concat
        "\\("lawlist-keywords-01"\\)") 1 'lawlist-bumble-bee t)

    lawlist-keywords-02

    (list (concat "\\(\\blawlist\\b\\)") 1 'lawlist-pink t)

))


(font-lock-add-keywords 'latex-mode '(

("INCLUDE\\|REVISED" 0 lawlist-red t)

("\\(foo\\)-\\(bar\\)" (1 lawlist-super-orange t) (2 lawlist-super-cyan t))

("\\(hello\\) \\(World\\)" (1 lawlist-super-orange t) (2 lawlist-super-blue t))

) 'prepend)

EDIT

(font-lock-add-keywords 'c-mode '(

("^#[ \t]*\\(ifdef\\|else\\|ifndef\\|if !?defined\\|if\\|elif\\|endif\\|ident\\).*$" (1 lawlist-super-HotPink1 t) )

("\\(^#[ \t]*define\\|^#[ \t]*include\\|^#[ \t]*undef\\).*$" (1 lawlist-super-SeaGreen t))

) 'prepend)
lawlist
  • 13,099
  • 3
  • 49
  • 158
  • This did not work lawlist. I added my regex for #define/#include and rest of preprocessors but could not have each category has its own color. Any other suggestions? – SFbay007 Aug 12 '13 at 22:56
  • Did you try using your own font like the fist four to five lines of code in this example to create your own definition, or did you try using setq instead of defconst to modify the existing definitions? You can essentially define anything to get highlighted, unless the same keywords are defined somewhere else such that they are trumping your attempts to redefine it. If you are being trumped, then you will need to find where in the source code those same keyword definitions already exist. – lawlist Aug 12 '13 at 23:15
  • I did actually created a new dotemacs file that only has the code that I listed above. I can't seem to untangle preprocessor keywords from the major font-lock-preprocessor-face, even if I set it to nil. could you try it in your home directory and see if you could have #define and #if have separate colors? – SFbay007 Aug 12 '13 at 23:29
  • `#include` and `#define` both turn green in `c-mode` in the edited example above, using the two specially defined fonts also in the example. But I would need to really study the regex you have created to see if it is correct for all instances. `if` is pink, but not the `#` before `if`, so that would probably be an error as to the number sign in your regex. The number signs for `include` and `define` are green. – lawlist Aug 12 '13 at 23:41
  • Yes. You are actually right I did implement the edited code you suggested and it is indeed different. I am still wondering -as you are- why the # in the "if" is not the same color as "if" although the regex should have included it. The other question I have is why your implementation does have this effect where mine doesn't. Any pointers? – SFbay007 Aug 12 '13 at 23:59
  • Ok I fixed the #. Doing this: (defconst lconfig-font-lock-faces (list '(font-lock-preprocessor-face ((((class color)) (:foreground "Black" :bold t)))) ) ) – SFbay007 Aug 13 '13 at 00:25