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

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

Re: Font-lock with newcomment.el


From: Stefan Monnier
Subject: Re: Font-lock with newcomment.el
Date: 30 May 2003 12:34:41 -0400
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

>   (setq font-lock-defaults '((nrnhoc-font-lock-keywords)
>                              nil ; do not do string/comment highlighting
>                              nil ; keywords are case sensitive.
>                              nil
>                              ))
>   (modify-syntax-entry ?_  "w")
>   (modify-syntax-entry ?\n  "> b")
>   (modify-syntax-entry ?/  (
>                             if (string-match "XEmacs" (emacs-version))
>                                ". 1456"
>                              ". 124b"))
>   (modify-syntax-entry ?*  ". 23")
>   (modify-syntax-entry ?\^m  "> b")

Look at the SampleMode code in the www.emacswiki.org.  It shows the
canonical way to setup a mode's syntax-table.  It's generally better
to leave this stuff outside of the major-mode function itself.

>> Oh, and David, regarding your mode: do a C-h f looking-at, that
>> will save you a bunch of buffer-substrings.

> Thanks for looking at the code.  From the looking-at documentation,
> it seems like it only matches regexps after the point, whereas in the
> current way of doing indentation I need to match symbols anywhere in
> the line, in most cases at least.  So it's not clear to me how
> looking-at will help, unless I rethink the indentation code.  

Then use re-search-forward (with a (line-end-position) limit if necessary).
Also, rather than using regexp-matching to find { and } and comment-markers,
it's a lot more robust to use sexp-based operations, such as
(forward-sexp -1) and/or (up-list -1).  If you (buffer-locally) set
parse-sexp-ignore-comments (which you should) they will automatically skip
comments (i.e. they will properly handle the case where you have a { inside
a comment).  Indentation in Emacs is still a black art, sadly, but
most of the indentation functions I've written have at their core a loop
that looks like

    (let ((outside-pos nil)
          (start pos (point)))
      (condition-case err
           (while
               (progn
                 (forward-sexp -1)
                 ;; Are we at the beginning of the line?
                 (save-excursion (skip-chars-backward " \t") (not (bolp)))))
         (scan-error
          (setq outside-pos (nth 2 err))))
      ;; Now we're either at the beginning of a previous line at the same
      ;; parenthesis level (in which case `outside-pos' is nil) or we're
      ;; looking at the first element after the open paren in which the
      ;; line we're coming from is enclosed (in which case `outside-pos'
      ;; is the position of that paren).
      (if (or (null outside-pos) (< (point) start))
          ;; We're at the start of a previous line at the same paren level.
          (current-indentation)
        ;; We're the first thing after the open paren:
        ;; indent relative to open-paren.
        (goto-char outside-pos)
        (+ basic-indent (current-indentation))))


        Stefan

PS: You might also like to use `regexp-opt' to build the regexps matching
a list of keywords.  Makes the code more readable.


reply via email to

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