Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new mode in Emacs

Tags:

emacs

elisp

I know nothing about Emacs Lisp (or any Lisp, for that matter). I want to do something that seems very simple, yet I have had no luck with online guides. I want to create "packet-mode.el" for .packet files. I want to do the following:

  • Enable C++ mode
  • Make packet a keyword, while leaving the rest of C++ mode unchanged
(define-derived-mode packet-mode fundamental-mode
  (font-lock-add-keywords 'c++-mode `(("packet" . font-lock-keyword-face)))
  (c++-mode))

  (add-to-list 'auto-mode-alist '("\\.packet\\'" . packet-mode)
  (provide 'packet-mode)

I've also tried switching the order of the statements in packet mode, but then the C++ highlighting breaks.

I would like packet to behave like struct in the sense that

packet foo {
  int bar;
}

is highlighted the same way it would be if struct were used in place of packet.

like image 793
Nick Avatar asked Feb 14 '13 23:02

Nick


People also ask

How do I set mode in Emacs?

Usually, the major mode is automatically set by Emacs, when you first visit a file or create a buffer (see Choosing File Modes). You can explicitly select a new major mode by using an M-x command.

What is a mode in Emacs?

A mode is a set of definitions that customize Emacs behavior in useful ways. There are two varieties of modes: minor modes, which provide features that users can turn on and off while editing; and major modes, which are used for editing or interacting with a particular kind of text.

What is the default mode in Emacs?

The standard default value is fundamental-mode . If the default value is nil , then whenever Emacs creates a new buffer via a command such as C-x b ( switch-to-buffer ), the new buffer is put in the major mode of the previously current buffer.

How do I enable minor mode in Emacs?

If you want to enable/disable minor-modes on the whole session, press [e] or [d] on any minor-mode to keep its status even if you change major-modes, buffers, files. It continues until stopping emacs. This enable/disable list is stored in the global of manage-minor-mode-default .


1 Answers

Here is what you need to put into packet-mode.el:

(defvar packet-mode-font-lock-keywords
  '(("\\<packet\\>" . font-lock-keyword-face)))
(define-derived-mode packet-mode c++-mode "Packet"
  "A major mode to edit GNU ld script files."
  (font-lock-add-keywords nil packet-mode-font-lock-keywords))
(add-to-list 'auto-mode-alist '("\\.packet\\'" . packet-mode))
(provide 'packet-mode)

Place packet-mode.el into a directory in your load-path and (optionally) byte compile it.

Now, add (require 'packet-mode) into your .emacs.el.

like image 192
sds Avatar answered Sep 24 '22 23:09

sds