unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: rgb <rbielaws@i1.net>
To: help-gnu-emacs@gnu.org
Subject: Re: how do I have a mode where '#' is a comment but '.#.' isn't?
Date: Tue, 30 Dec 2008 10:35:23 -0800 (PST)	[thread overview]
Message-ID: <af3d8c1b-d2ef-42cc-a661-8eafbf4999e3@o40g2000prn.googlegroups.com> (raw)
In-Reply-To: 23da89fc-338e-4e3c-a3ea-998ec13dfb69@r36g2000prf.googlegroups.com

On Dec 24, 11:42 am, rgb <rbiel...@i1.net> wrote:
> I've had similar problems with several major modes I've written.
>
> Cobol for example doesn't even have a comment character, anything
> after a particular column is a comment
> TAL uses ! as both begin and end and eol is also an implicit end....
>
> So ! this is a comment!but this isn't! and this is
> but this isn't
>
> Anyway, the only really good way to get useful results is by
> specifying
> font-lock-syntactic-keywords in your font-lock-defaults statement.
>
> It's not a terribly simple process.
> Some years ago, when I was writing all those modes, I was pretty
> fluent and could spout off just exactly how to do it. Fortunately I
> answered several how-to questions in several Emacs NGs so my notes are
> available.
>
> Try this thread.  I think it's pretty complete in covering what you
> need to know.
>
> http://groups.google.com/group/comp.emacs/browse_thread/thread/c1b7de...
>

Oddly enough I had to brush up on this myself just now.  CTP3 of
Powershell just came out and I needed to add support for an additional
comment syntax.

# to eol was the original comment syntax.
That was easily supported by the syntax table like this.
   (modify-syntax-entry ?#  "<" powershell-mode-syntax-table)
   (modify-syntax-entry ?\n ">" powershell-mode-syntax-table)

Now <# #> are are multi-line comment delimiters and, while I should be
able to support that via
   (modify-syntax-entry ?# ".23" powershell-mode-syntax-table)
   (modify-syntax-entry ?> ".4"  powershell-mode-syntax-table)
   (modify-syntax-entry ?< ".1"  powershell-mode-syntax-table)
it doesn't leave me with a mechanism for supporting for the original
syntax because # can only have ".23" or "<" syntax, not both
simultaneously.

As you can see, I wrote a function that returns match-data to find the
comment delimiters.
Then used font-lock-syntactic-keywords to give only those specific
characters comment delimiter syntax rather than all occurances like a
syntax-table does.

(defun powershell-find-syntactic-keywords (limit)
  "Finds PowerShell comment begin and comment end characters.
Returns match 1 or match 2 for <# #> comment sequences respectively.
Returns match 3 and match 4 for #/eol comments."
  (when (search-forward "#" limit t)
    (cond
     ((looking-back "<#")
      (set-match-data (list (match-beginning 0) (1+ (match-beginning
0))
                            (match-beginning 0) (1+ (match-beginning
0)))))
     ((looking-at ">")
      (set-match-data (list (match-beginning 0) (match-end 0)
                            nil nil
                            (match-beginning 0) (match-end 0)))
      (forward-char))
     (t
      (let ((start (point)))
        (if (search-forward "\n" limit t)
            (set-match-data (list (1- start) (match-end 0)
                                  nil nil nil nil
                                  (1- start) start
                                  (match-beginning 0) (match-end 0)))
          (set-match-data (list start (match-end 0)
                                nil nil nil nil
                                (1- start) start))))))
    t))

(defun powershell-setup-font-lock ()
  "Sets up the buffer local value for font-lock-defaults and
optionally
turns on font-lock-mode"
  ;; I use font-lock-syntactic-keywords to set some properties and I
  ;; don't want them ignored.
  (set (make-local-variable 'parse-sexp-lookup-properties) t)
  ;; I really can't imagine anyone wanting this off.
  (set (make-local-variable 'parse-sexp-ignore-comments) t)
  ;; This is where all the font-lock stuff actually gets set up.  Once
  ;; font-lock-defaults has it's value, setting font-lock-mode true
should
  ;; cause all your syntax highlighting dreams to come true.
  (setq font-lock-defaults
         ;; The first value is all the keyword expressions.
       '(powershell-font-lock-keywords
         ;; keywords-only means no strings or comments get fontified
         nil
         ;; case-fold (ignore case)
         nil
         ;; syntax-alist.  Nothing I can think of...
         nil
         ;; syntax-begin - no function defined to move outside
syntactic block
         nil
         ;; font-lock-syntactic-keywords
         ;; takes (matcher (match syntax override lexmatch) ...)...
         (font-lock-syntactic-keywords . ((powershell-find-syntactic-
keywords
                                           (1 "<" t t) (2 ">" t t)
                                           (3 "<b" t t) (4 ">b" t
t))))))
)


      reply	other threads:[~2008-12-30 18:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-16 23:54 how do I have a mode where '#' is a comment but '.#.' isn't? stuart
2008-12-17 12:18 ` Xah Lee
2008-12-17 14:06   ` Juanma Barranquero
2008-12-24 17:42 ` rgb
2008-12-30 18:35   ` rgb [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=af3d8c1b-d2ef-42cc-a661-8eafbf4999e3@o40g2000prn.googlegroups.com \
    --to=rbielaws@i1.net \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).