Create a new mode in Emacs

I don't know anything about Emacs Lisp (or any Lisp for that matter). I want to do something very simple, but I had no luck with online guides. I want to create "packet-mode.el" for files .packet

. I want to do the following:

  • Enable C ++ Mode
  • Make a keyword packet

    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 also tried to switch the order of statements in batch, but then the C ++ highlighting gets highlighted.

I would like to packet

behave like struct

in the sense that

packet foo {
  int bar;
}

      

is highlighted in the same way as if used packet

instead .packet

struct

+3


source to share


1 answer


Here's what you need to put in 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

in a directory in load-path

and (optionally) byte compiles it.

Now add (require 'packet-mode)

to your .emacs.el

.

+7


source







All Articles