Search code examples
emacscommentselispmode

Making a new emacs mode: simple comment dwim


I'm writing a simple major mode for a language, and am having trouble setting up comments. I've been following the tutorial:

http://www.emacswiki.org/emacs/ModeTutorial

However, I can't figure out how to adapt their example comment dwim to my needs because the way comments are defined seems pretty cryptic. In this language, comments are simply made using hashes (#). This is the same as Perl or Bash. I have the syntax highlighting understanding comments, but when I am trying to use the mode so I can comment-dwim I get a "no comment syntax defined" error. How can I make a fairly smart (indentation, blocks, uncommenting) comment-dwim that just uses hash-marks instead of any of the C-style fanciness?


Solution

  • In order to get comment-dwim working you need to specify comment-start and comment-end at the least. You can do this in your mode declaration body.

    (setq comment-start "#")
    (setq comment-end "")
    

    Documentation on the variables:

    comment-start:

    "String to insert to start a new comment, or nil if no comment syntax.")

    comment-end:

    "String to insert to end a new comment. Should be an empty string if comments are terminated by end-of-line."

    These variables are defined in newcomment.el the same builtin package that defines comment-dwim and the like.

    To read more: M-x find-library enter newcomment enter

    Also, if you didn't pick up enough about syntax tables form the tutorial, yours should specify ?\# for "<" and ?\n for ">", this tells emacs that # starts a comment, and a newline ends a comment.