Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A 'hello world' example for a major mode in Emacs?

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) :

  • wrote a file sample-mode.el and put it in a lisp dir
  • called in .emacs (require 'sample-mode)
  • wrote some defuns in it, and provided it at the end (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?

like image 238
Peter Avatar asked Jun 30 '09 11:06

Peter


People also ask

How do I make Emacs major mode?

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 are modes in Emacs?

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.

What is the default mode of 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.

What is Emacs Lisp used for?

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.


2 Answers

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."))
like image 89
Trey Jackson Avatar answered Sep 25 '22 23:09

Trey Jackson


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).

like image 26
Drew Avatar answered Sep 24 '22 23:09

Drew