It is possible and relatively easy to create a new major mode, deriving from c-mode, and just add two dashes '--' to work as comments too? I have checked many resources, but couldn't find anything similar. I tried some approaches, but I'm pretty novice with emacs-lisp programming, and c-mode seemed overwhelming for my knowledge.

Any hints will be appreciated.


I asked this question in Xah Lee emacs blog entry, and he gave me these suggestions:

i think it can be done, without much trouble. Read the elisp chapter on syntax table. basically, add the -- to the syntax table as comment. Note: syntax table is hard to work with, especially when specifying comment chars. Also, by default it only supports a few. I'm not sure you can have -- // /* */ all together. See my tutorial about comment linked on this page. But, if it can be done, then that's all you need to do. But if it cannot be done, you can still do so by trying to add the syntax for comment as text properties. (you'll need to learn about text properties)

I went through the elisp syntax table chapter, that I found pretty hard to grasp, and through Xah Lee comments tutorial and tried these lines, just like his lines for regular // c++ style comment.

    (define-derived-mode my-mode c-mode
      "my mode"
      (modify-syntax-entry ?- ". 12b" c-mode-syntax-table)
      (modify-syntax-entry ?\n "> b" c-mode-syntax-table)
    (provide 'my-mode)

It seems that this was enough for what I needed.

  • 2
    Note that you are actually changing the the syntax table for c-mode as well. – nispio Jun 8 '16 at 22:02
  • Note too that my-mode “has its own syntax table” predefined as my-mode-syntax-table. Therefore you could simply replace c-mode-syntax-table with my-mode-syntax-table in your code, and that way avoid changing c-mode’s syntax (@nispio), which might have unwanted effects. – Michael Allan Jul 7 '19 at 11:29

In order for your derived mode to not modify the c-mode syntax table directly, you should define your own syntax table which is based on the c-mode syntax table. Also, it is often helpful to modify the comment-start and comment-end variables to match the preferred comment style for the mode:

(require 'cc-langs)

(defvar c-like-syntax-table
  (let ((table (make-syntax-table)))
    (c-populate-syntax-table table)
    (modify-syntax-entry ?- ". 12b" table)
  "The syntax table for my mode called `c-like-mode'")

(define-derived-mode c-like-mode c-mode "C-Like"
  "Major mode that is like `c-mode' but with \"--\" as a comment"
  :syntax-table c-like-syntax-table
  (setq comment-start "-- ")
  (setq comment-end ""))

(provide 'c-like-mode)

It is worth noting that, given this syntax table, comments that start with -- are of the same comment style as comments that start with //. A side effect of this is that any combination of those characters will result in a comment. In other words, //, --, /-, and -/ will all mark the beginning of a comment.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.