In general, given a major mode with a defined comment syntax, what do I have to define or change to add a new comment syntax? By "adding", I mean setting some new characters so that they

  • work with commands such as comment-line, comment-dwim, comment-indent-new-line
  • are preserved nicely by fill-paragraph
  • get in all other ways treated as comments usually work in programming modes, including the proper spacing after the comment string

(Syntax highlighting will be specific to the mode, I guess, but if there is something more general -- please tell.)

Also, of course I'd like to have a function (or functions?) to apply the new comment syntax like with comment-line, which I can then bind to some keys, without having to implement all by myself, if possible.

There are description of some commands and variables here, but that doesn't answer all questions completely. For examle, some modes seem to include the space after the actual comment character into comment-start, while others don't -- why? Does it make a difference?

The only similar question I could find was this, but it's a bit more specific, and I don't know really what the provided answers do. For C and C++, there must certainly be a way that handles both // and /**/ comments, but that's not elaborated there... Also, I don't want to define a whole new major mode just for comments. This question is close, but unanswered.

My concrete use case: I'm working with julia-mode, where there normally are line comments starting with #. The syntax handling of that is, I think, defined here by setting comment-start to "# " and comment-start-skip to "#+\\s-*". What would I have to do to make line comments starting with #' and #+ be handled the same, preserving the standard comments? (For the curious, these kinds of comments are used in Weave.)

I set

(setq comment-start "#' "
      comment-start-skip "#+'?\\s-*")

but that doesn't quite do what I want:

  • comment-line now inserts comments starting with #', but I'd like it to keep using standard comments (just #). It manages to uncomment lines starting with both # and #', though.
  • fill-paragraph basically ignores the ', and behaves as with normal # (ie., #' bla\n #' blub becomes #' bla ' blub instead of #' bla blub, while doing the right thing without the single quotes).

What I want is to keep everything related to only # the same, but respect the compounds #' and #+ in formatting and uncommenting. And have extra commands to insert them, equivalent to comment-line.

  • To add to @Drew's remark: what happens if you (setq comment-start "#'") in julia-mode? Does it do what you want? if not, tell us what doesn't work the way you want it.
    – Stefan
    Feb 13 '18 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.