From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "John Massingham" Newsgroups: gmane.emacs.help Subject: How do I do syntax highlighting when writing a major mode Date: Fri, 23 May 2003 16:04:33 +0100 Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <006701c3213c$9f638640$050d10ac@johnm> Reply-To: johnm@tensilica.com NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0068_01C32145.0127EE40" X-Trace: main.gmane.org 1053704589 18804 80.91.224.249 (23 May 2003 15:43:09 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Fri, 23 May 2003 15:43:09 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Fri May 23 17:43:04 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 19JEYv-00048Y-00 for ; Fri, 23 May 2003 17:34:30 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.20) id 19JEX5-0003nt-Jo for gnu-help-gnu-emacs@m.gmane.org; Fri, 23 May 2003 11:32:35 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.20) id 19JESm-00021R-5a for help-gnu-emacs@gnu.org; Fri, 23 May 2003 11:28:08 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.20) id 19JEPO-0000yB-IA for help-gnu-emacs@gnu.org; Fri, 23 May 2003 11:24:42 -0400 Original-Received: from hq.tensilica.com ([65.205.227.29] helo=mail-in.hq.tensilica.com) by monty-python.gnu.org with esmtp (Exim 4.20) id 19JE2D-0008Q8-3t for help-gnu-emacs@gnu.org; Fri, 23 May 2003 11:00:41 -0400 Original-Received: from johnm ([172.16.13.5]) by mail-in.hq.tensilica.com (8.11.6/8.9.3) with SMTP id h4NF0T623008 for ; Fri, 23 May 2003 08:00:29 -0700 Original-To: X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook CWS, Build 9.0.2416 (9.0.2910.0) X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Importance: Normal X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:10121 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:10121 This is a multi-part message in MIME format. ------=_NextPart_000_0068_01C32145.0127EE40 Content-Type: multipart/alternative; boundary="----=_NextPart_001_0069_01C32145.0127EE40" ------=_NextPart_001_0069_01C32145.0127EE40 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit 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) ------=_NextPart_001_0069_01C32145.0127EE40 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi
 
If I = am going to be=20 flamed please do it quickly....
 
I am = trying to write=20 my own major mode and pretty much the most important thing at the moment = is=20 syntax highlighting, I will get round to indentation=20 later...
 
I have = set up some=20 key words to parse on and they get set in their given colours but I want = to have=20 the equivilent to the C mode whereby
 
int=20 abc;
 
the = "int" gets the=20 font-lock-keyword-face and the "abc" gets the=20 font-lock-variable-name-face
 
I have = a number of=20 key words (equivilent to int / char / etc) and it is quite a limited=20 list
 
iclass
reference
schedule
semantic
 
These I can=20 search for and set in the keyword face but I am unsure how to find the = second=20 word, whatever it may be, and I obviously dont want to highlight = every=20 second word!!
 
I dont know=20 lisp, I am just hacking about, looking at various modes, perl, make, etc = and=20 cutting and pasting untill it seems to work! However I suspec a syntax = table or=20 something has to be built up but with my lack of lisp knowledge I cant = follow=20 whats actually going on
 
can anyone=20 help?
 
cheers
 
John
 
PS. Dont=20 laugh, but here is a copy what I have done so = far
 
;;
;; Here we define some variables that = all major=20 modes should define
;;
(defvar tie-mode-hook nil)
(defvar=20 tie-mode-map  nil
  "Keymap for TIE major=20 mode")
 

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

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

;;
;;=20 Here we have defined our minimal set of keywords for emacs to=20 highlight
;;
;; TIE keywords...
;; coprocessor ctype field = iclass=20 interface opcode operand proto reference regfile
;; schedule semantic = state=20 table user_register
;;
;; Because font-lock is so = resource-intensive,=20 providing optimized regexps to
;; font-lock should provide a boost = in=20 performance. Not done here as it is still in the
;; "get it working=20 phase!"
;;
(defconst tie-font-lock-keywords-1
 =20 (list
   ;;'("^;.*$" . = font-lock-preprocessor-face)
  =20 '("^//.*$" . font-lock-comment-face)
   '("[^$]//.*$" .=20 font-lock-comment-face)
   '("
\\<\\(coprocessor\\|ctype\\|def\\|field\\|interface\\|opcode\\|op= erand\\|proto\\|regfile\\|state\\|table\\|use\\|user_register\\)\\= >" . = font-lock-keyword-face)
  =20 '("\\<\\(iclass\\|reference\\|schedule\\|semantic\\)\\>" .=20 font-lock-preprocessor-face)
   ;;'("\\( \\|state\\)[_A-Za-z0-9]*" . font-lock-function-name-face)  ;; PUTS = EVERYTHING IN=20 BLUE
   ;;'("
\\(state\\|opcode\\)\\b[_A-Za-z0-9]+\\b" .=20 font-lock-function-name-face)
   = ;;'("state\b[_A-Za-z0-9]*\b" .=20 font-lock-function-name-face)
   = ;;'("state[\t\s\w]*[_A-Za-z0-9]+"=20 . font-lock-function-name-face)
   ;;'("state = [_A-Za-z0-9]+" .=20 font-lock-function-name-face)
   ;;'("\\(state\\)[_A-Za-z0-9]+[ \t]*\+?=3D" . = font-lock-function-name-face)
  =20 ;;'("\\(
\\|state\\)[_A-Za-z0-9]+\?\\sw+\\" .=20 font-lock-function-name-face)
   )
  "Minimal = highlighting=20 expressions for TIE mode")
 
;;
;; Now I've = defined the second=20 level of highlighting. Note that the second level is appended to the = first=20 level,
;; resulting in a single keyword variable that matches = everything in=20 both levels.
;;
;; More TIE key words
;; assign in inout out = wire=20
;; TIEadd TIEaddn TIEcmp TIEcsa TIEmac TIEmul TIEmulpp TIEmux = TIEpsel=20 TIEsel
;;
(defconst tie-font-lock-keywords-2
  (append=20 tie-font-lock-keywords-1
   (list
   =20 '("
\\<\\(assign\\|in\\|inout\\|out\\|wire\\)\\>" .=20 font-lock-type-face)
    '("\\<\\(TIE\\(add\\|addn\\|cmp\\|csa\\|mac\\|mul\\|mulpp\\|mux\\|ps= el\\|sel\\)\\)\\>" .=20 font-lock-string-face)
    ))
  "Additional = Keywords=20 to highlight in TIE mode")
 

;;
;; I've now = defined more=20 TIE constants. This completes the list of TIE keywords.
;;
;; = Even more=20 TIE key words
;; VAddr MemDataIn8 MemDataIn16 MemDataIn32 MemDataIn64 = MemDataIn128
;; MemDataOut8 MemDataOut16 MemDataOut32 MemDataOut64=20 MemDataOut128
;; LoadByteDisable StoreByteDisable
;;
(defconst=20 tie-font-lock-keywords-3
  (append=20 tie-font-lock-keywords-2
   (list
   =20 '("
\\<\\(VAddr\\|MemDataIn\\(8\\|16\\|32\\|64\\|128\\)\\|MemDataOut\= \(8\\|16\\|32\\|64\\|128\\)\\|LoadByteDisable\\|StoreByteDisable\\)\\>" .=20 font-lock-variable-name-face)
    ))
  = "All-out=20 highlighting in TIE mode")
 

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

(defconst=20 tie-comment-start-regexp "//\\|/\\*"
  "Dual comment value for=20 `comment-start-regexp'.")
 
 
 
;;
;; And now to = launch the major=20 mode....
;;
(defun tie-mode ()
  "Major mode for editing = TIE=20 files."
  (interactive)
  = (kill-all-local-variables)
 =20 (make-local-variable 'font-lock-defaults)
  (setq=20 font-lock-defaults
 '(tie-font-lock-keywords))
 
  (setq major-mode=20 'tie-mode)
  (setq mode-name "Tie")
  (run-hooks=20 'tie-mode-hooks))
 
(provide = 'tie-mode)
 
 
 
------=_NextPart_001_0069_01C32145.0127EE40-- ------=_NextPart_000_0068_01C32145.0127EE40 Content-Type: text/x-vcard; name="John W Massingham (E-mail).vcf" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="John W Massingham (E-mail).vcf" 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=3DQUOTED-PRINTABLE:;;Sanderum House=3D0D=3D0AOakley = Road;Chinnor;Oxfordshire;OX9 4TW;United Kingdom LABEL;WORK;ENCODING=3DQUOTED-PRINTABLE:Sanderum House=3D0D=3D0AOakley = Road=3D0D=3D0AChinnor, Oxfordshire OX9 4TW=3D0D=3D0AUnite=3D d Kingdom EMAIL;PREF;INTERNET:johnm@tensilica.com REV:20000904T084229Z END:VCARD ------=_NextPart_000_0068_01C32145.0127EE40 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit _______________________________________________ Help-gnu-emacs mailing list Help-gnu-emacs@gnu.org http://mail.gnu.org/mailman/listinfo/help-gnu-emacs ------=_NextPart_000_0068_01C32145.0127EE40--