[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I do syntax highlighting when writing a major mode

From: Oliver Scholz
Subject: Re: How do I do syntax highlighting when writing a major mode
Date: Fri, 23 May 2003 19:07:05 +0200
User-agent: Gnus/5.090019 (Oort Gnus v0.19) Emacs/21.3.50 (windows-nt)

"John Massingham" <address@hidden> writes:

> If I am going to be flamed please do it quickly....

Ts, ts, are we monsters? :-)

> I have a number of key words (equivilent to int / char / etc) and it is quite
> a limited list


> These I can search for and set in the keyword face but I am unsure
> how to find the second word, whatever it may be, and I obviously
> dont want to highlight every second word!!

You can use subexpressions for this. Group parts of your regexp with
"\\(" and "\\)" and reference them by number (0 stands for the whole

(defvar jm-example-font-lock-keywords
     (1 font-lock-keyword-face)
     (2 font-lock-function-name-face))))

(define-derived-mode jm-example-mode text-mode "EXAMPLE"
  "Major mode to demonstrate & test font-lock-keywords."
  (setq font-lock-defaults '(jm-example-font-lock-keywords)))

> I dont know lisp, I am just hacking about, looking at various modes,
> perl, make, etc and cutting and pasting untill it seems to work!
> However I suspec a syntax table or something has to be built up but
> with my lack of lisp knowledge I cant follow whats actually going on

Have a look at some web ressources for writing major modes:

    - Tutorial

    - Generic mode: for simple modes

    - derived modes

> ;; Here we define some variables that all major modes should define
> ;;
> (defvar tie-mode-hook nil)
> (defvar tie-mode-map  nil
>   "Keymap for TIE major mode")

> ;; Here we're defining a default keymap if the user doesn't already have one
> ;;
> (if tie-mode-map nil
>   (setq tie-mode-map (make-keymap)))

(defvar tie-mode-map (make-keymap))

Or (if you are going to define something in it):

(defvar tie-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map ....)
    (define-key map ....)
  "Keymap for TIE major mode")

> ;;
> ;; And now to launch the major mode....
> ;;
> (defun tie-mode ()
>   "Major mode for editing TIE files."
>   (interactive)
>   (kill-all-local-variables)
>   (make-local-variable 'font-lock-defaults)
>   (setq font-lock-defaults
>  '(tie-font-lock-keywords))
>   (setq major-mode 'tie-mode)
>   (setq mode-name "Tie")
>   (run-hooks 'tie-mode-hooks))

(define-derived-mode tie-mode fundamental-mode "TIE"
  "Major mode for editing TIE files."
  (setq font-lock-defaults '(tie-font-lock-keywords)))

4 Prairial an 211 de la Révolution
Liberté, Egalité, Fraternité!

reply via email to

[Prev in Thread] Current Thread [Next in Thread]