unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How do I do syntax highlighting when writing a major mode
@ 2003-05-23 15:04 John Massingham
  0 siblings, 0 replies; 2+ messages in thread
From: John Massingham @ 2003-05-23 15:04 UTC (permalink / raw)



[-- Attachment #1.1: Type: text/plain, Size: 5101 bytes --]

Hi

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

I am trying to write my own major mode and pretty much the most important
thing at the moment is syntax highlighting, I will get round to indentation
later...

I have set up some key words to parse on and they get set in their given
colours but I want to have the equivilent to the C mode whereby

int abc;

the "int" gets the font-lock-keyword-face and the "abc" gets the
font-lock-variable-name-face

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

iclass
reference
schedule
semantic

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!!

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

can anyone help?

cheers

John

PS. Dont laugh, but here is a copy what I have done so far

;;
;; 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)))


;;
;; Here, we append a definition to auto-mode-alist.
;; This tells emacs that when a buffer with a name ending with .tie is
opened,
;; then tie-mode should be started in that buffer
;;
(setq auto-mode-alist
      (append
       '(("\\.tie\\'" . tie-mode))
       auto-mode-alist))


;;
;; Here we have defined our minimal set of keywords for emacs to highlight
;;
;; TIE keywords...
;; coprocessor ctype field iclass interface opcode operand proto reference
regfile
;; schedule semantic state table user_register
;;
;; Because font-lock is so resource-intensive, providing optimized regexps
to
;; font-lock should provide a boost in performance. Not done here as it is
still in the
;; "get it working phase!"
;;
(defconst tie-font-lock-keywords-1
  (list
   ;;'("^;.*$" . font-lock-preprocessor-face)
   '("^//.*$" . font-lock-comment-face)
   '("[^$]//.*$" . font-lock-comment-face)

'("\\<\\(coprocessor\\|ctype\\|def\\|field\\|interface\\|opcode\\|operand\\|
proto\\|regfile\\|state\\|table\\|use\\|user_register\\)\\>" .
font-lock-keyword-face)
   '("\\<\\(iclass\\|reference\\|schedule\\|semantic\\)\\>" .
font-lock-preprocessor-face)
   ;;'("\\( \\|state\\)[_A-Za-z0-9]*" . font-lock-function-name-face)  ;;
PUTS EVERYTHING IN BLUE
   ;;'("\\(state\\|opcode\\)\\b[_A-Za-z0-9]+\\b" .
font-lock-function-name-face)
   ;;'("state\b[_A-Za-z0-9]*\b" . font-lock-function-name-face)
   ;;'("state[\t\s\w]*[_A-Za-z0-9]+" . font-lock-function-name-face)
   ;;'("state [_A-Za-z0-9]+" . font-lock-function-name-face)
   ;;'("\\(state\\)[_A-Za-z0-9]+[ \t]*\+?=" . font-lock-function-name-face)
   ;;'("\\( \\|state\\)[_A-Za-z0-9]+\?\\sw+\\" .
font-lock-function-name-face)
   )
  "Minimal highlighting expressions for TIE mode")

;;
;; Now I've defined the second level of highlighting. Note that the second
level is appended to the first level,
;; resulting in a single keyword variable that matches everything in both
levels.
;;
;; More TIE key words
;; assign in inout out wire
;; TIEadd TIEaddn TIEcmp TIEcsa TIEmac TIEmul TIEmulpp TIEmux TIEpsel TIEsel
;;
(defconst tie-font-lock-keywords-2
  (append tie-font-lock-keywords-1
   (list
    '("\\<\\(assign\\|in\\|inout\\|out\\|wire\\)\\>" . font-lock-type-face)

'("\\<\\(TIE\\(add\\|addn\\|cmp\\|csa\\|mac\\|mul\\|mulpp\\|mux\\|psel\\|sel
\\)\\)\\>" . font-lock-string-face)
    ))
  "Additional Keywords to highlight in TIE mode")


;;
;; I've now defined more TIE constants. This completes the list of TIE
keywords.
;;
;; Even more TIE key words
;; VAddr MemDataIn8 MemDataIn16 MemDataIn32 MemDataIn64 MemDataIn128
;; MemDataOut8 MemDataOut16 MemDataOut32 MemDataOut64 MemDataOut128
;; LoadByteDisable StoreByteDisable
;;
(defconst tie-font-lock-keywords-3
  (append tie-font-lock-keywords-2
   (list

'("\\<\\(VAddr\\|MemDataIn\\(8\\|16\\|32\\|64\\|128\\)\\|MemDataOut\\(8\\|16
\\|32\\|64\\|128\\)\\|LoadByteDisable\\|StoreByteDisable\\)\\>" .
font-lock-variable-name-face)
    ))
  "All-out highlighting in TIE mode")


;;
;; Here I've defined the default level of highlighting to be the maximum
which is my preference - and it is my mode!!
;;
(defvar tie-font-lock-keywords tie-font-lock-keywords-3
  "Default highlighting expressions for TIE mode")


(defconst tie-comment-start-regexp "//\\|/\\*"
  "Dual comment value for `comment-start-regexp'.")



;;
;; 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))

(provide 'tie-mode)



[-- Attachment #1.2: Type: text/html, Size: 12472 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: John W Massingham (E-mail).vcf --]
[-- Type: text/x-vcard; name="John W Massingham (E-mail).vcf", Size: 497 bytes --]

BEGIN:VCARD
VERSION:2.1
N:Massingham;John
FN:John W Massingham (E-mail)
ORG:Tensilica
TITLE:European FAE
TEL;WORK;VOICE:+44 (0) 1844 355612
TEL;CELL;VOICE:+44 (0) 7748 938303
ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;Sanderum House=0D=0AOakley Road;Chinnor;Oxfordshire;OX9 4TW;United Kingdom
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:Sanderum House=0D=0AOakley Road=0D=0AChinnor, Oxfordshire OX9 4TW=0D=0AUnite=
d Kingdom
EMAIL;PREF;INTERNET:johnm@tensilica.com
REV:20000904T084229Z
END:VCARD

[-- Attachment #3: Type: text/plain, Size: 151 bytes --]

_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://mail.gnu.org/mailman/listinfo/help-gnu-emacs

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How do I do syntax highlighting when writing a major mode
       [not found] <mailman.6644.1053703895.21513.help-gnu-emacs@gnu.org>
@ 2003-05-23 17:07 ` Oliver Scholz
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Scholz @ 2003-05-23 17:07 UTC (permalink / raw)


"John Massingham" <johnm@tensilica.com> 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

[keywords]  

> 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
expression).

(defvar jm-example-font-lock-keywords
  '(("\\(\\<iclass\\>\\)\\s-+\\([[:alnum:]]+\\)?" 
     (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
      http://two-wugs.net/emacs/mode-tutorial.html

    - Generic mode: for simple modes
      http://www.emacswiki.org/cgi-bin/wiki.pl?GenericMode

    - derived modes
      http://www.emacswiki.org/cgi-bin/wiki.pl?DerivedMode

[...]
> ;; 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 ....)
    ...
    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)))

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-05-23 17:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-23 15:04 How do I do syntax highlighting when writing a major mode John Massingham
     [not found] <mailman.6644.1053703895.21513.help-gnu-emacs@gnu.org>
2003-05-23 17:07 ` Oliver Scholz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).