Another short one today. We're taking the code we already wrote and making a minor mode out of it.
In Emacs, a minor mode is a set of functionality that you can turn on
(or off) in a buffer (or globally). For example, the built in
auto-fill-mode can be turned on in a buffer will automatically add
newlines when your line gets "too long." You can see the modes that
you currently have on using the
describe-mode function, usually
C-h m. I also currently have
Hungry-Delete mode as well
Flyspell and a few others. Hungry-Delete automatically deletes
multiple whitespace characters all at once so if I have five spaces
between words, I can just delete or kill once and they'll all go
away. Flyspell adds auto spell checking.
Minor modes can be turned on manually using
command which toggles whatever mode on and off or automatically based
on hooks. For example, when you load a C file, I go into c mode which
is a major mode (more about them some other time) as well as these
Here's specific code we go over in the video to turn on and off our new mode:
"fill in the docstring later"
:lighter " ER"
(add-hook 'after-change-functions #'emoji-replace-insert nil t)
(remove-hook 'after-change-functions #'emoji-replace-insert)))
define-minor-mode does all the magic. The key is that it
defines a "mode variable" - in this case named
which tells us if we're turning the mode on or off. Based on that, we
either add or remove our hook. The :lighter " ER" sets what to show
in the mode line.
We're just scratching the surface today - just setting up the basics. Later we'll see how to clean up the variables we need for the mode - specifically our list of emojis as well as how we can select either of our emoji replacement methods - overlay a text property or replace the text. We'll also see about setting up key combos for a mode in our next elisp project.
That's it for today.
The code for the series is still up here:
Video link: https://www.youtube.com/watch?v=YBV9czwq-nw