Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define comment syntax for a major mode?

I'd like to add comments to a major mode that I use that doesn't currently support them. The only examples I can find online show how to write single-line comments, but I need paired delimiters.

What do I need to change?

like image 853
Patrick Collins Avatar asked Aug 20 '14 08:08

Patrick Collins


1 Answers

I ended up digging in to the elisp of the package that I was working with. The problem is that if you add

(setq comment-start "FOO")
(setq comment-end "BAR")

to your-mode-hook, then, when you switch to another language that defines comment-start but not comment-end, then you end up with comment-end sticking from the other mode. For example, your python-mode comments look like:

# def my_func(): BAR

which definitely isn't what you want. In order to fix that, use the following:

(add-hook 'your-mode-hook
          (lambda ()
            (set (make-local-variable 'comment-start) "FOO")
            (set (make-local-variable 'comment-end) "BAR")))

and it won't clobber other modes' comment-end.

like image 105
Patrick Collins Avatar answered Oct 05 '22 07:10

Patrick Collins