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)
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.