14

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

zetetic
  • 47,184
  • 10
  • 111
  • 119
Eli Schneider
  • 4,903
  • 3
  • 28
  • 50
  • possible duplicate of [How to write an emacs mode for a new language?](http://stackoverflow.com/questions/91201/how-to-write-an-emacs-mode-for-a-new-language) – dmckee --- ex-moderator kitten Oct 08 '10 at 03:25
  • @dmckee: I don't think this is a duplicate of that question. It is true that some of the *answers* there cover this subject somewhat. You would be better off linking to whatever you think answers this question, rather than signaling that this *question* duplicates the major-mode *question*. – Drew Dec 04 '14 at 03:14

3 Answers3

23

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.

Colin Cochrane
  • 2,565
  • 1
  • 19
  • 20
  • 1
    would one save this is a file like my-syntax.el and add something like `(require 'my-syntax)` in .emacs? Thank you – Eli Schneider Oct 08 '10 at 03:15
  • 4
    You can do that, or put it directly in your .emacs if you prefer. If you put it in a seperate file, ensure it is in your load-path and that you include the line (provide 'my-syntax) at the end if you want to use (require 'my-syntax). – Colin Cochrane Oct 08 '10 at 03:22
  • 1
    @ColinCochrane how do you use it for log files? – Andrii Tykhonov Oct 14 '13 at 14:02
5

EmacsWiki's Mode tutorial has a little more information on creating a major mode, in case you want to expand from syntax highlighting only.

Joakim Hårsman
  • 1,824
  • 13
  • 14
3

You also might find it useful to look at this answer, which has a pointer to code that defines a minor mode to highlight certain key words - but only in strings and comments.

A minor mode is nicer if all you want is highlights - less baggage.

The relevant portions of the manual are for the function 'font-lock-add-keywords and the variable font-lock-keywords.

Community
  • 1
  • 1
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229