开发者

Simplest Emacs syntax highlighting tutorial? [closed]

开发者 https://www.devze.com 2023-01-19 17:17 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 8 years ago.

Improve this question

I would like to create only a syntax highlig开发者_高级运维hting 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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号