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:
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
.
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.
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.
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.
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 .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With