2009-07-15

syntax coloring your own comment syntax in emacs

On Jul 15, 4:37 am, gento wrote:
> Hi All,
>
> I'm using emacs to edit journal text files *.jou, for which the
> commented lines
> starts with the character "/".
>
> I got two working solutions to have emacs recognize the "*.jou" files
> and
> apply the corresponding comment syntax automatically.
>
> Namely I add to the .emacs file either
>
> (require 'cl)
> (push '("\\.jou$" . (lambda () (text-mode) (setf comment-start "/")))
> auto-mode-alist)
>
> or
>
> (add-hook 'find-file-hooks
> (lambda ()
> (when (string-match "\\.jou$" (buffer-file-name))
> (setq comment-start "/")
> )
> )
> )
>
> For the moment I'm using the latter macro.
>
> Now I would also like to enable the syntax highlighting for comments.
>
> Does anybody know how I can modify one of the previous code snippets
> in order to turn the text of the commented lines into red ?
>
> I appreciate any help.

for syntax coloring comment, and if your comment syntax is simple as used in most popular langs, then, all you have to do is to setup emacs's syntax table for the comment chars, then syntax coloring will automatically work on comments.

like this (code untested):
(modify-syntax-entry ?/ "< b" jou-mode-syntax-table)
(modify-syntax-entry ?\n "> b" jou-mode-syntax-table)

for detail, see:

• How To Add Comment Handling In Your Major Mode
http://xahlee.org/emacs/elisp_comment_handling.html

The best thing i think in your case, is just to create a mode for your .jou file. It's pretty easy. This tutorial may help:

• How To Write A Emacs Major Mode For Syntax Coloring
http://xahlee.org/emacs/elisp_syntax_coloring.html

your code plus the syntax coloring and commenting command should be no more than 50 lines. Prob 20 will do. Once you have a mode, say jou-mode.el, than you can have this code associate the mode with .jou files:

(add-to-list 'auto-mode-alist '("\\.jou\\'" . jou-mode))

try my tutorial and let me know if it solves your problem.

Xah
∑ http://xahlee.org/

No comments:

Post a Comment