Extensions and modes
Lem contribs are not loaded at startup.
M-x load-library
and choose a library (for example, tetris). Then, you can call this contrib’s commands (in that case, M-x tetris
).
A major mode is associated to a keymap and commands. It is typically used for a programming language mode, but also for non-programming modes (directory-mode, tetris-mode…), or to complement a programming mode keymap (paredit).
If you want to add support for another programming language, please see Lem’s modes/
directory (and get in touch!).
Also, remember that you don’t need to create a major mode to add custom commands to Lem. A define-command
in that case is enough, please see configuring Lem.
But, how do we create a new major mode?
Create a .lisp file and create a new package. You’ll want to “use” :lem
:
(defpackage :lem-awesome-mode
(:use :cl :lem)
(:export :awesome-command ;; <-- we'll write them
:awesome-mode))
(in-package :lem-awesome-mode)
Name the major mode to be created and name its keymap:
(define-major-mode awesome-mode ()
(:name "Awesome mode"
:keymap *awesome-mode-keymap*)
;; you can do write more startup code here.
)
You can also create a minor mode with
lem:define-minor-mode
. A minor mode will only complement a major mode and won’t overwrite all its keybindings.
It’s time to write your code and define commands.
(define-command awesome-command () ()
(message "Hello Lem!"))
(define-key *awesome-mode-keymap* "!" 'awesome-command)
Don’t forget to export both :awesome-mode
and :awesome-command
in your package definition.
Now, open any file and activate your mode with M-x awesome-mode
. Try to write a “!”: you should see the “Hello Lem!” message pop-up. You’ll have to re-activate the mode you were in to stop this functionnality.
You can take inspiration from all Lem modes in modes/
or src/ext/
. Do a grep
for “define-major-mode” or “define-minor-mode”.
It is natural that when you open a “.lisp” file Lem enables the lisp-mode. There is a mechanism for that.
You can tell Lem to associate one or many file extensions with your mode.
After your mode definition, use define-file-type
. This is the declaration for all lisp files:
(define-file-type ("lisp" "asd" "cl" "lsp" "ros") lisp-mode)
Adapt accordingly with your major mode.
A “Makefile” file has no extension, but we want to open it in makefile-mode anyways. Use define-file-associations
:
(define-file-associations makefile-mode
((:file-namestring "Makefile")
(:file-namestring "makefile")))
Adapt accordingly.
- Pareto - A mode, complement to the built-in Paredit mode, similar to Emacs’ Lispy-mode. Makes Lisp code editing even more efficient!