Learning Elisp 15 - cleaning up our minor mode
source link: https://cestlaz.github.io/post/learning-elisp-15/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Learning Elisp 15 - cleaning up our minor mode
Today we're finishing off our emoji project. This video covers two quick things.
The first is how we can make the mode use either of our replacement methods - one that actually replaces the text with the emoji and the other that uses text properties to overlay the emoji without changing the text.
This is done with a variable that we can set telling us which function
to call. We used one named emoji-replace-by-insert
. If it's t
(true)
we use the replace method, if it's nil
we use the overlay one.
We could just use an if statement in our define minor mode but I decided to use this as an opportunity to show how we can assign a function to a variable which might be new to some readers/viewers.
Let's say we want to create a variable named my_function_variable
and have it stand in for, let's say the built in 1+
function that
increments a value. We could do a simple assignment:
(setq my_function_variable #'1+)
Unfortunately, in Elisp we can't just call our new variable as a function straight out but rather must call it using funcall, like this:
(funcall my_variable_function 5) ;; this returns 6, just like (1+ 5) would
In other languages we could just call the function directly. In the case of defining our mode, we can just throw in the variable once we assign it. Here's the code:
(define-minor-mode emoji-replace-mode
"fill in the docstring later"
:lighter " ER"
(let ((func (if emoji-replace-by-insert
#'emoji-replace-insert
#'emoji-replace-overlay)))
(if emoji-replace-mode
(add-hook 'after-change-functions func nil t)
(remove-hook 'after-change-functions func))))
We use the let
form to assign our variable func
to the appropriate
function and then just pass along func
when we use add-hook
and
remove-hook
.
That's it.
The other thing we cover involves cleaning up our variables. In
earlier videos we used setq
but also showed defvar
with the
practical difference being that defvar
had a docstring.
In this video, we look at defcustom
which looks like defvar
but
also lets you change the variable value using emacs'
customize-variable
command and interface. Further, if we do change
the variable this way and save it through the interface, it will save
the change in your init.el
file for future use. It's a nice touch
when making a complete "package."
That's it for this project. I might do one more on setting things up for installation using straight or something similar. If not, it'll be on to the next project which I think will be a thesaurus moed.
Code:
The code for the series is still up here:
The videos:
Video link: https://www.youtube.com/watch?v=Y9TPHo33Y1Y
Recommend
-
143
IMPORTANT This package is not maintained anymore. User should go and checkout weirdNox/org-noter as an alternative. Interleaved Textbooks? Some history, w...
-
52
README.md org-trello
-
12
gorepl-mode A minor emacs mode for Go REPL. Synopsis gorepl-mode is a Go REPL interaction library for Emacs. It's built on top of gore. Gorepl pack...
-
16
yard-mode Rudimentary support for fontifying YARD tags and directives in ruby comments. Installation Use MELPA: M-x package-install yard-mode. Ad...
-
4
Files Permalink Latest commit message Commit time
-
7
Learning Elisp 5 - Rot13 parts 2 and 3Rather than one long boring video, I thought it would be better to split up the remainder of the Rot13 project into 4 shorter boring ones :-).Here are the first two.Befo...
-
6
Learning Elisp 4 - Rot13 part 1It's been a couple of weeks since my last post. Sorry - been repainting the apartment so I was limited to my laptop for a while.Now that we've gone over a few basics we can explore ne...
-
5
Learning Elisp 10 - elisp data structuresI'm leaving our next elisp project for when I get back from my vacation but realized I could make a short video with some preliminaries - some elisp tools that we'll need that we c...
-
5
Learning Elisp 14 - defining a minor modeAnother 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 o...
-
5
Learning Elisp 17 - a thesaurus modeWhen we last left our heroes we had made the API call to dictionaryapi.com, gotten the response and coverted the json string into elisp data. Specifically an array that contained a hash...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK