Topics include: programming, Apple, Unix, gadgets, large-scale web sites and other nerdy stuff.

Tutorial on making an emacs major mode.

2007-01-17 11:29:03

At work, we have these message declaration files for localizing the application. I wanted to make an emacs mode where at least the message identifiers were highlighted in a different color. I found this nice tutorial on getting started with the hooks.

It only took me 10 minutes to get my mode working well enough. Here is my mode, as an example.

(defvar r9msg-mode-hook nil)

(defvar r9msg-mode-map
  (let ((r9msg-mode-map (make-keymap)))
    ;;(define-key r9msg-mode-map "\C-j" 'newline-and-indent)
  "Keymap for r9msg majo mode")

(add-to-list 'auto-mode-alist '("\\.msg\\'" . r9msg-mode))

; comments begin with #
; msg ids begin with *
; directives begin with @
(defvar r9msg-font-lock-keywords-1
   '("^#.*" . font-lock-comment-face)              
   '("^\\*.*" . font-lock-constant-face)
   '("^@.*" . font-lock-variable-name-face)
  "Minimal highlighting expressions for r9msg mode")

(defvar r9msg-font-lock-keywords 

(defun r9msg-mode ()
  "Major mode for editing msg files"
  (set (make-local-variable 'font-lock-defaults)
  ;;(set-syntax-table r9msg-mode-syntax-table)
  (use-local-map r9msg-mode-map))