all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to enable the syntax highlighting for comments within files  having a specific extension?
@ 2009-07-15 11:37 gento
  2009-07-15 23:51 ` Xah Lee
  2009-08-27 20:57 ` pjb
  0 siblings, 2 replies; 6+ messages in thread
From: gento @ 2009-07-15 11:37 UTC (permalink / raw
  To: help-gnu-emacs

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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to enable the syntax highlighting for comments within files having a specific extension?
  2009-07-15 11:37 How to enable the syntax highlighting for comments within files having a specific extension? gento
@ 2009-07-15 23:51 ` Xah Lee
  2009-07-16 13:13   ` bj
  2009-08-27 20:57 ` pjb
  1 sibling, 1 reply; 6+ messages in thread
From: Xah Lee @ 2009-07-15 23:51 UTC (permalink / raw
  To: help-gnu-emacs

On Jul 15, 4:37 am, gento <gento_distef...@hotmail.com> 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/^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to enable the syntax highlighting for comments within files having a specific extension?
  2009-07-15 23:51 ` Xah Lee
@ 2009-07-16 13:13   ` bj
  2009-07-17  1:14     ` Xah Lee
  2009-08-27 22:22     ` A.Politz
  0 siblings, 2 replies; 6+ messages in thread
From: bj @ 2009-07-16 13:13 UTC (permalink / raw
  To: help-gnu-emacs

Dear Xah,

Thanks for your help.

I have partly reached my goals.

Here is the jou-mode.el file which I built on the basis of your
tutorials:

<--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---
><--8---><--8---><--8--->
;; the command to comment/uncomment text
(defun jou-comment-dwim (arg)
   "Comment or uncomment current line or region in a smart way. For
detail, see `comment-dwim'."
   (interactive "*P")
   (require 'newcomment)
   (let ((deactivate-mark nil) (comment-start "/") (comment-end ""))
     (comment-dwim arg)))

(setq myKeywords
 `(
  )
)

;; define the major mode.
(define-derived-mode jou-mode fundamental-mode
  "you are in jou-mode"
  (setq font-lock-defaults '(myKeywords))

  (setq comment-start "/")

  (define-key jou-mode-map [remap comment-dwim] 'jou-comment-dwim)

  (modify-syntax-entry ?/ "< b" jou-mode-syntax-table)
  (modify-syntax-entry ?\n "> b" jou-mode-syntax-table)
)
<--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---
><--8---><--8---><--8--->


The comment syntax highlighting works, but I still have few questions.


1) Is the line (setq comment-start "/") which I have added really
needed?

2) I don't need the keywords, but if I remove the relevant entries in
the jou-mode.el file
   (i.e. lines 9-12, 17), the commented lines are no longer in red.

3) if the comment string "/" is in the middle of a line, like the
division sign in an expression,
   then the characters to the right of it are turned into red. Do you
know how to avoid this?

Thanks for your help

gento




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to enable the syntax highlighting for comments within files having a specific extension?
  2009-07-16 13:13   ` bj
@ 2009-07-17  1:14     ` Xah Lee
  2009-08-27 22:22     ` A.Politz
  1 sibling, 0 replies; 6+ messages in thread
From: Xah Lee @ 2009-07-17  1:14 UTC (permalink / raw
  To: help-gnu-emacs

On Jul 16, 6:13 am, bj <gento_distef...@hotmail.com> wrote:
> Dear Xah,
>
> Thanks for your help.
>
> I have partly reached my goals.
>
> Here is the jou-mode.el file which I built on the basis of your
> tutorials:
>
> <--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8--->
>
> ;; the command to comment/uncomment text
> (defun jou-comment-dwim (arg)
>    "Comment or uncomment current line or region in a smart way. For
> detail, see `comment-dwim'."
>    (interactive "*P")
>    (require 'newcomment)
>    (let ((deactivate-mark nil) (comment-start "/") (comment-end ""))
>      (comment-dwim arg)))
>
> (setq myKeywords
>  `(
>   )
> )
>
> ;; define the major mode.
> (define-derived-mode jou-mode fundamental-mode
>   "you are in jou-mode"
>   (setq font-lock-defaults '(myKeywords))
>
>   (setq comment-start "/")
>
>   (define-key jou-mode-map [remap comment-dwim] 'jou-comment-dwim)
>
>   (modify-syntax-entry ?/ "< b" jou-mode-syntax-table)
>   (modify-syntax-entry ?\n "> b" jou-mode-syntax-table)
> )
> <--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---><--8---
>
> ><--8---><--8---><--8--->
>
> The comment syntax highlighting works, but I still have few questions.
>
> 1) Is the line (setq comment-start "/") which I have added really
> needed?

you don't need it.

> 2) I don't need the keywords, but if I remove the relevant entries in
> the jou-mode.el file
>    (i.e. lines 9-12, 17), the commented lines are no longer in red.

I'll have to take some time to know the tech details, but it's pretty
safe to jsut let your code be.

> 3) if the comment string "/" is in the middle of a line, like the
> division sign in an expression,
>    then the characters to the right of it are turned into red. Do you
> know how to avoid this?

if you want everything between / and newline char to be treated as
comment, than that is what your code has told emacs to do. If you want
to introduce some special case, then in short your comment syntax is a
complex one. As such, it cannot be implemented by using just syntax
table. You'll have to use emacs general mechanism for syntax coloring.
I'm not expert enough to give you solution off hand, and my
experiences with emacs syntax coloring mechanism is that it's quite
difficult and complex to learn.

Maybe someone else here can help.

Thanks & good luck.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to enable the syntax highlighting for comments within files having a specific extension?
  2009-07-15 11:37 How to enable the syntax highlighting for comments within files having a specific extension? gento
  2009-07-15 23:51 ` Xah Lee
@ 2009-08-27 20:57 ` pjb
  1 sibling, 0 replies; 6+ messages in thread
From: pjb @ 2009-08-27 20:57 UTC (permalink / raw
  To: help-gnu-emacs

On August 14, 2009 4:21:56 PM CEDT, gento
<gento_distef...@hotmail.com> wrote:
> I've made this modification to .emacs
>
> (add-hook 'find-file-hooks
>   (lambda ()
>      (when (string-match "\\.jou$" (buffer-file-name))
>         (setq comment-start "/")
>         (font-lock-add-keywords nil '("\\(/.*\\)$" (1 font-lock-comment-face)))
>      )
>   )
> )
>
> but I still have no coloring of the commented lines :(


It works for me (with emacs 22.3.1).  Have you evaluated the above
form?

--
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to enable the syntax highlighting for comments within files having a specific extension?
  2009-07-16 13:13   ` bj
  2009-07-17  1:14     ` Xah Lee
@ 2009-08-27 22:22     ` A.Politz
  1 sibling, 0 replies; 6+ messages in thread
From: A.Politz @ 2009-08-27 22:22 UTC (permalink / raw
  To: help-gnu-emacs

On Jul 16, 3:13 pm, bj <gento_distef...@hotmail.com> wrote:

>
> Here is the jou-mode.el file which I built on the basis of your
> tutorials:
[...]
>
> 2) I don't need the keywords, but if I remove the relevant entries in
> the jou-mode.el file
>    (i.e. lines 9-12, 17), the commented lines are no longer in red.
>
> 3) if the comment string "/" is in the middle of a line, like the
> division sign in an expression,
>    then the characters to the right of it are turned into red. Do you
> know how to avoid this?
>
> Thanks for your help
>
> gento


Use define-generic-mode, dude.

(define-generic-mode bj-journal-mode
  '("//") ;; use double backslash
  '("your" "keywords")
  nil
  '("\\.jou\\'")
  nil)

The comment/uncomment functions come for free, usually on M-; .

I doubt, that the comment starter really is a single backslash,
because that would be silly.

-ap





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-08-27 22:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-15 11:37 How to enable the syntax highlighting for comments within files having a specific extension? gento
2009-07-15 23:51 ` Xah Lee
2009-07-16 13:13   ` bj
2009-07-17  1:14     ` Xah Lee
2009-08-27 22:22     ` A.Politz
2009-08-27 20:57 ` pjb

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.