I'm trying to write my first (very simple) major mode for Emacs. My goal is to have multiple comment types, but I can't find a way to implement this. Basically, I want my major mode to make highlighted comments as follows
- line that comes after
#
, exactly like in Python - line that comes after
!
, same as above, but with a different color. Also, I want to be able to add a#
comment at the end of this line. - a code block starting with
%
and ending inend
, again with a different color
So far, I have the following in my .el file:
(defvar my-mode-hook nil)
(defvar my-mode-map
(let ((map (make-keymap)))
map)
(defvar my-mode-syntax-table
(let ((st (make-syntax-table)))
;; comment style: "# ..."
(modify-syntax-entry ?# "<" st)
(modify-syntax-entry ?\n ">" st)
(setq comment-start "# ")
(setq comment-end "")
st))
(defvar my-highlights
'(("!\\|" . font-lock-function-name-face)
("!\\([^<]+?\\)\n" . (1 font-lock-function-name-face))))
(define-derived-mode my-mode prog-mode "my-mode"
(interactive)
;; (kill-all-local-variables)
(use-local-map my-mode-map)
(set-syntax-table my-mode-syntax-table)
(setq font-lock-defaults (list nil nil))
(set (make-local-variable 'font-lock-defaults) '(my-highlights))
;; (set (make-local-variable 'indent-line-function) 'my-indent-line)
(setq major-mode 'my-mode)
(setq mode-name "MY MODE")
(run-hooks 'my-mode-hook))
This works somewhat for #
and !
, but the text highlight for !
comments does not work if I put a #
at the end of the line. Also, it only works after I hit enter at the end of the line, not straight after the !
as I write.