Can anyone provide me with a hello world example for a major mode in emacs? I guess it's a beginner question, still I really like to write a major mode, both to learn emacs and elisp, as to be able to use customization to the fullest.
What I have done so far (and is working) :
(require 'sample-mode)
(provide 'sample-mode)
But still it doesn't seem to be activated, I cannot call it with M-sample-mode.
So how to do that? And can anyone provide me with a very very simple Hello World like working sample?
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.
Modes are what make one editor (Emacs) equally useful for writing documentation, programming in a variety of languages (C, C++, Perl, Python, Java, and many more), creating a home page, sending E-Mail, reading Usenet news, keeping track of your appointments, and even playing games.
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.
Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.
Well, let's start with this answer, which uses define-generic-mode
.
Flesh it out with some comment characters like: /* */
, some keywords: hello
hi
etc., re-use the face from the original answer, a file extension .hello
, and a function call to do further customization.
There's the additional line to get autoloading working, but you have to generate the loaddefs.el file. That's more advanced than hello world.
And, you end up with this:
(make-face 'my-date-face)
(set-face-attribute 'my-date-face nil :underline t)
(set-face-attribute 'my-date-face nil :family "times")
(set-face-attribute 'my-date-face nil :slant 'normal)
(set-face-attribute 'my-date-face nil :height '340)
;;;###autoload
(define-generic-mode hello-world
'(("/*" . "*/")) ; comment characters
'("hello" "hi" "howdy" "greetings" "hola") ; keywords
'(("\\([0-9]+/[0-9]+/[0-9]+\\)"
(1 'my-date-face))) ; font lock
'("\\.hello$") ; auto-mode-alist
'(hello-world-special-setup) ; function-list
"An example major mode.
We have comments, keywords, a special face for dates, and recognize .hello files.")
(defun hello-world-special-setup ()
"Some custom setup stuff done here by mode writer."
(message "You've just enabled the most amazing mode ever."))
The Elisp manual introduces major modes pretty well, and it includes a node that presents "hello-world" examples. At least that is the intention, I think.
Those examples might not cover everything you are looking for. In that case, consider requesting whatever you think is missing that would help users more. To do that, use M-x report-emacs-bug
(that is for enhancement requests also).
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