From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: font-lock-defaults doesn't work?? Date: Sat, 14 Apr 2007 19:42:03 +1000 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <87fy73wavo.fsf@lion.rapttech.com.au> References: <1176478548.918147.146490@d57g2000hsg.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1176546978 22293 80.91.229.12 (14 Apr 2007 10:36:18 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 14 Apr 2007 10:36:18 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Apr 14 12:36:12 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Hcfbz-0001Q2-AN for geh-help-gnu-emacs@m.gmane.org; Sat, 14 Apr 2007 12:36:07 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HcfgL-0007B9-JF for geh-help-gnu-emacs@m.gmane.org; Sat, 14 Apr 2007 06:40:37 -0400 Original-Path: shelby.stanford.edu!newshub.stanford.edu!postnews.google.com!news3.google.com!out01b.usenetserver.com!news.usenetserver.com!in04.usenetserver.com!news.usenetserver.com!news-in-02.newsfeed.easynews.com!easynews.com!easynews!sn-xt-sjc-02!sn-xt-sjc-08!sn-post-sjc-01!supernews.com!corp.supernews.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.95 (gnu/linux) Cancel-Lock: sha1:dwWqDeUIoSdY0gBmalhpuedP1Mg= Original-X-Complaints-To: abuse@supernews.com Original-Lines: 143 Original-Xref: shelby.stanford.edu gnu.emacs.help:147064 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:42668 Archived-At: "Peter Tury" writes: > Hi, > > I am trying to create a new major mode from scratch. I found that > syntax parsing doesn't work properly sometimes. Why? Am I missed > something or this is a bug? > I suspect you have missed something. Many modes are doing what you are trying to do and successfully, so its unlikely you have found a bug. However, it is possible, especially since your using a devlopment code base (emacs 22)., but I think unlikely. You are also re-inventing a wheel to some extent. Use define-derived-mode and let emacs take care of some of the heavy lifting for you and build on work already done! Also, see the example provided on the emacs wiki site. ,----[ C-h f define-derived-mode RET ] | define-derived-mode is a Lisp macro in `derived.el'. | (define-derived-mode CHILD PARENT NAME &optional DOCSTRING &rest BODY) | | Create a new mode as a variant of an existing mode. | | The arguments to this command are as follow: | | CHILD: the name of the command for the derived mode. | PARENT: the name of the command for the parent mode (e.g. `text-mode') | or nil if there is no parent. | NAME: a string which will appear in the status line (e.g. "Hypertext") | DOCSTRING: an optional documentation string--if you do not supply one, | the function will attempt to invent something useful. | BODY: forms to execute just before running the | hooks for the new mode. Do not use `interactive' here. | | BODY can start with a bunch of keyword arguments. The following keyword | arguments are currently understood: | :group GROUP | Declare the customization group that corresponds to this mode. | The command `customize-mode' uses this. | :syntax-table TABLE | Use TABLE instead of the default. | A nil value means to simply use the same syntax-table as the parent. | :abbrev-table TABLE | Use TABLE instead of the default. | A nil value means to simply use the same abbrev-table as the parent. | | Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode: | | (define-derived-mode LaTeX-thesis-mode LaTeX-mode "LaTeX-Thesis") | | You could then make new key bindings for `LaTeX-thesis-mode-map' | without changing regular LaTeX mode. In this example, BODY is empty, | and DOCSTRING is generated by default. | | On a more complicated level, the following command uses `sgml-mode' as | the parent, and then sets the variable `case-fold-search' to nil: | | (define-derived-mode article-mode sgml-mode "Article" | "Major mode for editing technical articles." | (setq case-fold-search nil)) | | Note that if the documentation string had been left out, it would have | been generated automatically, with a reference to the keymap. | | The new mode runs the hook constructed by the function | `derived-mode-hook-name'. | | See Info node `(elisp)Derived Modes' for more details. | | [back] `---- > Details: > > I have the following defun for start. If I remove the "secretly > must-to-have parts" (see below) then M-: (syntax-ppss) returns lists > as if it would parse a lisp buffer: characters in a line after `;' > reported as in-comment chars, and real comments (= delimited by `/*' > and `*/' doesn't recognized as comments. However, fontification works > nicely -- most of the time, but not always: _sometimes_, if I put `;' > inside a /*-comment and then delete this `;', then comment-color is > removed... More interestingly once I got nil for the value of > font-lock-syntax-table (queried via C-h v font-lock-syntax-table from > the buffer where I activated this new mode via M-x t-mode > previously)!? > > So it seems syntax parsing is wrong and fontification is > indeterministic iff I set(??) font-lock-syntax-table via > font-lock-defaults, but everything works well if I directly set it via > set-syntax-table. > > Is this normal? Do you know the reason? Should I report it as a bug? > > (defun t-mode () > "test major mode" > (interactive) > > (kill-all-local-variables) > (setq major-mode (quote t-mode)) > (setq mode-name "t-mode") > > ;; secretly must-to-have parts -- start > (let ((t-syntax-table (make-syntax-table))) > (modify-syntax-entry ?/ ". 14" t-syntax-table) > (modify-syntax-entry ?* ". 23" t-syntax-table) > (set-syntax-table t-syntax-table)) > ;; secretly must-to-have parts -- end > > (set (make-local-variable 'font-lock-defaults) > '(nil nil t > ((?/ . ". 14") > (?* . ". 23")))) > from memory, I don't htink this bit is correct and you shouldn't need it. See the example on the eamcs wiki. Comment font locking is driven by the comment characters defined in the syntax table. You shouldn't need to also set them explicitly in the font-lock stuff (at least, I've not needed to in the derived modes I've been working on in the past). > (run-mode-hooks 't-mode-hook)) > > Note: emacs' help writes for font-lock-syntax-table: "this is normally > set via `font-lock-defaults'", while elisp manual writes for > make-syntax-table (in 35.3): "most major mode syntax tables are > created in this way" -- however I would think that make-syntax-table > is unusable if I set font-lock-syntax-table via font-lock- > defaults...?? > have a look at a fairly simple mode and see how they do the font-lock stuff. I found sql.el quite useful (part of emacs). Again, look at the emacs wikki, there is some really useful ifo there. I think you may be making things more complicated than necessary. Tim -- tcross (at) rapttech dot com dot au