Patrick Collins
Patrick Collins

Reputation: 10584

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?

Upvotes: 6

Views: 1298

Answers (1)

Patrick Collins
Patrick Collins

Reputation: 10584

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.

Upvotes: 4

Related Questions