13

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?

4 Answers 4

11

Ok, after some more googling I am at least one step furhter :

(define-derived-mode sample-mode ...) 

since the providing isn't defining the mode as I thought first.. This I found on :

http://xahlee.org/emacs/elisp_syntax_coloring.html

A very very nice site for emacs-lovers.

With the help of that : I have made a HelloWorld example myself now : It's a (as small as possible) Csharp mode. I have used Euler1 as example rather than HelloWorld. The files you need to know about are :

  • the file the mode will be applied on Euler1.cs
  • the .emacs
  • and of course the mode itself

Since a pic is worth, at least a bunch of words : all files on 1 screen :

alt text

But since this nice pic seems to disappear half the time (anyone a clue? Open in new tab always brings it on, and the url is ok) some words too :-) :

  1. The mode : cs-mode.el

    (setq myKeywords 
     '(("WriteLine" . font-lock-function-name-face)
       ("public\\|static\\|void\\|int\\|for\\|if\\|class"
    . font-lock-constant-face)))
    
    (define-derived-mode cs-mode fundamental-mode
      (setq font-lock-defaults '(myKeywords)))
    
    (provide 'cs-mode)
    
  2. The .emacs, that makes the .cs files open in the right mode :

;; cs
(require 'cs-mode)
(add-to-list 'auto-mode-alist '("\\.cs\\'" . cs-mode))

And thats all : the cs-code itself is useless her, cause it won't show the effect of coloring the key-words. To see that see the pic, or open the pic in another tab/window.

Cheers, ph

5
  • 1
    @jrockway : not agreeing here, I learned a lot from him, including writing major modes Although he might himself not have any spare time left th include his own ideas, you have a point there
    – Peter
    Jul 2, 2009 at 7:55
  • 1
    @jrockaway: that is the second time I see you criticising Xah... why do you hate him so much?
    – Vivi
    Jun 10, 2010 at 12:51
  • ps, there is a more full-featured C# mode: emacswiki.org/emacs/CSharpMode
    – Cheeso
    Apr 28, 2011 at 17:51
  • @Vivi Xah is well-known in Emacs circles, and not in a good way. Nov 15, 2012 at 0:39
  • @R.P.Dillon , imho this asks for some elaboration, otherwise your comment seems too nonbinding. I don't know Xah, but learned a lot from his site.
    – Peter
    Nov 15, 2012 at 9:05
6

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
1
  • 2
    +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
    Jun 30, 2009 at 11:44
6

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."))
1
  • Shouldn't this be called ` hello-world-mode` ?
    – ideasman42
    Jun 6, 2020 at 2:43
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.