From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: rgb Newsgroups: gmane.emacs.help 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) Organization: http://groups.google.com Message-ID: References: <5fd737ce-449e-4da0-bd54-a467d23faef2@w39g2000prb.googlegroups.com> <23da89fc-338e-4e3c-a3ea-998ec13dfb69@r36g2000prf.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1230662451 20312 80.91.229.12 (30 Dec 2008 18:40:51 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 30 Dec 2008 18:40:51 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Dec 30 19:41:58 2008 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 1LHjXO-000264-W6 for geh-help-gnu-emacs@m.gmane.org; Tue, 30 Dec 2008 19:41:55 +0100 Original-Received: from localhost ([127.0.0.1]:46248 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LHjWB-0000VG-9u for geh-help-gnu-emacs@m.gmane.org; Tue, 30 Dec 2008 13:40:39 -0500 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!o40g2000prn.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 111 Original-NNTP-Posting-Host: 24.131.174.213 Original-X-Trace: posting.google.com 1230662123 628 127.0.0.1 (30 Dec 2008 18:35:23 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 30 Dec 2008 18:35:23 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: o40g2000prn.googlegroups.com; posting-host=24.131.174.213; posting-account=c0t8LgoAAAAU6L5wu8lL6g4rkWKIgySB User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30428; .NET CLR 3.0.30422),gzip(gfe),gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:165649 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:60982 Archived-At: On Dec 24, 11:42=A0am, rgb 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. =A0I 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)))))) )