RE: Help using font-lock

"Dan Bar Dov" <[email protected]> Thu, 22 Jun 2006 16:29:44 +0300
Newsgroups gmane.emacs.xemacs.general
Message-ID <[email protected]>
Thanks for all the replies, here's what I ended up with (if anyone's interested)
(no comments though ...)
----------------------

(defface twiki-header1-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:bold t :foreground "red" :background "yellow" :size 12pt))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki keywords."
  :group 'font-lock-twiki-faces)

(defface twiki-header2-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:bold t :foreground "red" :background "moccasin"))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki keywords."
  :group 'font-lock-twiki-faces)

(defface twiki-header3-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:bold t :foreground "red" :background "ivory"))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki keywords."
  :group 'font-lock-twiki-faces)

(defface twiki-bullet-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:foreground "darkred"))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki bullets."
  :group 'font-lock-twiki-faces)

(defface twiki-pre-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:foreground "darkgoldenrod"))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki bullets."
  :group 'font-lock-twiki-faces)

(defface twiki-table-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:background "green"))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki bullets."
  :group 'font-lock-twiki-faces)

(defface twiki-bold-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:bold t))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki bullets."
  :group 'font-lock-twiki-faces)

(defface twiki-italic-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:italic t))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki bullets."
  :group 'font-lock-twiki-faces)

(defface twiki-link-face
  '((((class color) (background dark)) (:foreground "cyan"))
    ;; red4 is hardly different from black on windows.
    (((class color) (background light) (type mswindows)) (:foreground "blue" :underline t))
    (((class color) (background light)) (:foreground "red4"))
    (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
    (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
    (t (:bold t)))
  "Font Lock mode face used to highlight twiki bullets."
  :group 'font-lock-twiki-faces)

(defvar twiki-font-lock-keywords
  (list
   '("---\\+.*$" .              twiki-header1-face)
   '("---\\+\\+.*$" .           twiki-header2-face)
   '("---\\+\\+\\+.*$" .        twiki-header3-face)
   '("   \\* .*$" .              twiki-bullet-face)
   '("<\\(pre\\|verbatim\\)>" .  twiki-pre-face)
   '("</\\(pre\\|verbatim\\)>" . twiki-pre-face)
   '("|" .                       twiki-table-face)
   '("\\*.*\\*" .                twiki-bold-face)
   '("_.*_" .                    twiki-italic-face)
   '("\\[.*\\]" .                twiki-link-face)
   )
  "Faces and what they look like.")

(defvar twiki-mode-map nil
  "Keymap for twiki display mode.")

(defun twiki-mode ()
  "Major mode for viewing emacs face formats.
Easiest way is to open ~load\rwiki-mode.el, then executing twiki-mode on it.
You should then see how the various formats look."

 (interactive)
  (kill-all-local-variables)
  (use-local-map twiki-mode-map)
  (setq mode-name "twiki")
  (setq major-mode 'twiki-mode)
  (set-variable 'tab-width 3)
  (set-variable 'indent-tabs-mode nil)
  (set-variable 'truncate-partial-width-windows nil) ;line wrap on.
  (set-variable 'comment-column 42)
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults '(twiki-font-lock-keywords t))
  (font-lock-mode)
  (run-hooks 'twiki-mode-hook)
)
 

> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of Andy Sawyer
> Sent: Saturday, June 17, 2006 12:39 PM
> To: [email protected]
> Subject: Re: Help using font-lock
> 
> "Dan Bar Dov" <[email protected]> writes:
> 
> > I'm having a problem understanding how to use font-lock-mode 
> > without using any major mode.
> >
> > Actually, I'm editing some text in text-mode and I'd like several 
> > forms to be highlighted.
> >
> > I tried to set the following in my init.el, but this does 
> not seem to work
> >
> > (setq font-lock-keywords
> >       '(("   \\*.*" . 'font-lock-string-face)
> > 	("---+ .*" . 'font-lock-string-face)
> > ))
> >
> > I do not want to write a new major mode or anything fancy, something
> > very simple.
> 
>  Writing a new major mode to do this is one of those things that turns
> out to be very simple, once you know how :). Try this:
> 
> ,----
> | (define-derived-mode dbd-mode
> |   text-mode
> |   "DBD"
> |   "Highlight for DBD."
> |   (font-lock-mode 1))
> | 
> | (defconst dbd-mode-font-lock-keywords
> |   '(("   \\*.*" . font-lock-string-face)
> |     ("---+ .*" . font-lock-string-face))
> |     "`font-lock-keywords' for `dbd-mode'.")
> | 
> | (put 'dbd-mode 'font-lock-defaults
> |      '(dbd-mode-font-lock-keywords))
> `----
> 
> then when you're editing your text just M-x dbd-mode.
> 
> Hope this helps,
>  '(andys)
> 
>