ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

How to Name Your Emacs Major Mode

Xah Lee, , …,

When coding emacs lisp to create a new major mode, there are several issues in naming your mode. This page gives you some tips about how to name your mode. For the basics of writing a major mode, see How to Write a Emacs Major Mode for Syntax Coloring.

Value of Variable “major-mode”

“major-mode” is a built-in buffer local variable.

For example, call describe-variable then type “major-mode”, and emacs will show the variable's value of the current buffer's major mode.

This variable is the most important, technical, name for a major mode. This value is usually the same as the command name user types to activate the mode, but not always. (for example, the command to invoke dired-mode is dired.)

You do not need to set this variable. This variable is automatically set when you use define-derived-mode. Emacs will use the first argument of define-derived-mode as the value.

Choosing Your Mode's Name

When choosing a name, try to come up with a name so that user can tell what it is for, but also unique from other modes for the same language. For example, if your mode is for HTML, you don't want just name it html-mode, because many other people probably have created a mode of the same name, causing confusion and name conflict.

Here are some example names from existing modes for consideration: for Perl, there are modes named perl-mode and cperl-mode. For XML, there are modes named xml-mode and nxml-mode. For HTML, there are html-mode, html-helper-mode, nxhtml-mode. For JavaScript: javascript-mode, js-mode, js2-mode, espresso-mode. For IRC, there are rcirc-mode, erc-mode. For email, there's rmail, vm, gnus.

If you are out of ideas, you can simply name it with your name plus the language's name, such as “david-html-mode” or “d-html-mode”.

Prefix Mode Name to All Names

Emacs lisp does not support namespace, nor does it support lexical scope. Practically speaking, all variables and function in emacs are all in a global namespace.

The symbols in your package should have unique names to avoid multiple packages having the same name that can override each other. The conventional practice is that all your symbol's names in your mode should be prefixed by your mode name, or a abbreviation of your mode name.

This is also important reason that you should choose a unique name for your mode.

For example, if you look at the source code of cperl-mode, all their names starts with “cperl”. For html-mode, all names starts with “sgml”. For python-mode, all names starts with “python”.

Value of Variable “mode-name”

mode-name is a built-in buffer local variable. When in a major mode, the value of the variable mode-name is displayed in the status bar (the “mode line”). This value should be a short label that conveys to users the purpose of the mode. For example, in html-mode, the value is “HTML”. In perl-mode, the value is “Perl”. In cperl-mode, the value is “CPerl”. In nxml-mode, the value “nXML”. (try calling describe-variable to see what is the current value)

You need to set this variable, in the body of define-derived-mode or your (defun my-html-mode …). For example:

(define-derived-mode dv-html-mode fundamental-mode
  …
  (setq mode-name "HTML")
  …
)

The Elisp File Name

Emacs and Elisp does not enforce a relation of your mode's name and the file name. For example, your can have your mode named “mylsl-mode”, while the file name can be “lsl_mode_by_David.el” or “lsl_mode_v1.4.el”.

Normally, you just name your file the same as the value of the variable major-mode defined in your package. So, for example, if you have “mylsl-mode”, then the file can be “mylsl-mode.el”. This is how majority of major mode package's files are named.

Elisp file's name can be a little flexible. A version number in the file name is common. If your package has more than one file, you can name them anything appropriate to the file's purpose.

What's the difference between library, package, features? And what's the difference between load-file, load, require, autoload?

See: Emacs Lisp's Library System.

blog comments powered by Disqus