The Time Poor Blogger has a post about creating a new emacs major mode – article mode. For implementing a major mode, the wiki provides some useful hints but I think this is a useful case study. The mode adds word counting and keyword density checking and is designed for writing search engine optimised articles.
Major modes are generally for significant pieces of functionality, for example helping with editing a new type of file or providing a unified mechanism for editing directories (dired).
Article mode doesn’t really fit into either category which indicates that perhaps it would be better implemented as a minor mode. Also it might be useful in conjuction with a number of different major modes, e.g. HTML mode, Text mode or Latex mode.
As major modes are mutually exclusive it wouldn’t be possible to use Article mode in this way with the current implementation. I would recommend to change this to a minor mode.
The mode demonstrates the following interesting emacs features:
- adding text properties – it provides a mechanism for highlighting keywords
- timers – it only updates the information buffer when emacs has been idle for half a second
- hooks – it demonstrates a slightly unusual technique of waiting for a second keypress after a particular keychord using post-command-hook.
Finally, I like the fact that it uses (define-derived-mode CHILD PARENT NAME <DOCSTRING>).
(define-derived-mode article-mode text-mode "Article" "Major mode for editing articles Special commands: \\{article-mode-map}")