[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how do I have a mode where '#' is a comment but '.#.' isn't?
From: |
rgb |
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) |
User-agent: |
G2/1.0 |
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))))))
)