views:

385

answers:

3

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?

+8  A: 
Peter
+1 for very nice website
dfa
Xah's website is not a nice resource for emacs users; it's a nice resource for very strange people that like to spend more time talking than implementing their ideas.
jrockway
@jrockway : not agreeing here, I learned a lot from him, including writing major modesAlthough he might himself not have any spare time left th include his own ideas, you have a point there
Peter
@jrockaway: that is the second time I see you criticising Xah... why do you hate him so much?
Vivi
+5  A: 

there are several examples around the Web like this. I can also recommend you several Emacs book:

  • Learning GNU Emacs (the best imho)
  • Writing GNU Emacs Extensions
  • the official GNU Emacs lisp reference/manual
dfa
+1 and thanks for the link, but in the whole emacs scene, sometimes a lack of simple examples bothers me a bit. One notable exception the link provided earlier : xahlee.org
Peter
check also emacswiki
dfa
+2  A: 

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."))
Trey Jackson