Search code examples
emacselispemacs23xemacs

Simplest Emacs syntax highlighting tutorial?


I would like to create only a syntax highlighting minor mode for Emacs. I have the "Writing GNU Emacs Extensions" by O'Reilly, but it does not go into any depth of detail. Is there a simple tutorial for a real or fake programming language highlighting mode?

Thank you


Solution

  • Defining a custom Generic Mode is probably the best place to start. You can define basic syntax highlighting for a language as simply as the following snippet.

    (require 'generic-x)
    
    (define-generic-mode 
      'my-mode                          ;; name of the mode
      '("//")                           ;; comments delimiter
      '("function" "var" "return")      ;; some keywords
      '(("=" . 'font-lock-operator) 
        ("+" . 'font-lock-operator)     ;; some operators
        (";" . 'font-lock-builtin))     ;; a built-in 
      '("\\.myext$")                    ;; files that trigger this mode
       nil                              ;; any other functions to call
      "My custom highlighting mode"     ;; doc string
    )
    

    It's great for quickly defining some basic syntax highlighting for obscure languages. I even use it for log files in some cases.