Search code examples
emacsfont-lock

Emacs Specifying Rules for font-lock


I'm putting the parsing techniques I'm learning into practice. I'm trying to write a programming mode in Emacs,so I can take advantage of syntax highlighting and the like. unfortunately, font-lock isn't working. Searching on Google and following the tutorials found there yielded no results. Below is my code. Any advice is appreciated.

;;;###autoload
(defgroup use-mode nil
  "Mode for editing Use source files."
  :group 'languages)

;;;###autoload
(defcustom use-mode-hook nil
  "Hook run when use-mode is started.")

(defvar use-mode-map (make-sparse-keymap)
"Keymap for use-mode.")

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.use\\'" . use-mode))

(defvar use-keywords-1 '("use" . font-lock-keyword-face)
  "First level of font-lock in Use")

(defvar  use-font-lock-keywords use-keywords-1
  "Code highlighting.")

;;;###autoload
(define-derived-mode use-mode prog-mode "Use"
  "Major mode for editing Use source files."
  (setq font-lock-defaults '(use-font-lock-keywords)))

(provide 'use-mode)

Solution

  • When you define font-lock keywords, you should supply a list of entries. So, if you replace:

    (defvar use-keywords-1 '("use" . font-lock-keyword-face)
      "First level of font-lock in Use")
    

    With the following, it will work:

    (defvar use-keywords-1 '(("use" . font-lock-keyword-face))
      "First level of font-lock in Use")
    

    Note that unless you plan to write a really advanced system, you don't need to provide different levels, so you can drop the *-1 variables.

    (Just in case you haven't seen it already, I would like to recommend one of my packages, font-lock-studio. It is a interactive debugger for font-lock keywords.)