help-gnu-emacs
[Top][All Lists]
Advanced

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

How do I do syntax highlighting when writing a major mode


From: John Massingham
Subject: How do I do syntax highlighting when writing a major mode
Date: Fri, 23 May 2003 16:04:33 +0100

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: John W Massingham (E-mail).vcf
Description: Vcard


reply via email to

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