From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Oliver Scholz Newsgroups: gmane.emacs.help Subject: Re: How do I do syntax highlighting when writing a major mode Date: Fri, 23 May 2003 19:07:05 +0200 Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: main.gmane.org 1053712389 4761 80.91.224.249 (23 May 2003 17:53:09 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Fri, 23 May 2003 17:53:09 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Fri May 23 19:53:05 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 19JGeN-0000SG-00 for ; Fri, 23 May 2003 19:48:15 +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 19JG4B-00088L-59 for gnu-help-gnu-emacs@m.gmane.org; Fri, 23 May 2003 13:10:51 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!dialin-145-254-191-146.arcor-ip.NET!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 100 Original-NNTP-Posting-Host: dialin-145-254-191-146.arcor-ip.net (145.254.191.146) Original-X-Trace: fu-berlin.de 1053709628 1228861 145.254.191.146 (16 [87814]) X-Attribution: os X-Face: "HgH2sgK|bfH$; PiOJI6|qUCf.ve<51_Od(%ynHr?=>znn#~#oS>",F%B8&\vus),2AsPYb -n>PgddtGEn}s7kH?7kH{P_~vu?]OvVN^qD(L)>G^gDCl(U9n{:d>'DkilN!_K"eNzjrtI4Ya6;Td% IZGMbJ{lawG+'J>QXPZD&TwWU@^~A}f^zAb[Ru;CT(UA]c& User-Agent: Gnus/5.090019 (Oort Gnus v0.19) Emacs/21.3.50 (windows-nt) Cancel-Lock: sha1:Kh9kMaMtsrPbTiZTAVXjV2TgsYM= Original-Xref: shelby.stanford.edu gnu.emacs.help:113630 Original-To: help-gnu-emacs@gnu.org 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:10126 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:10126 "John Massingham" 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 '(("\\(\\\\)\\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é!