Zamansky: Learning Elisp #12

Mike Zamansky just published the latest video in his Learning Elisp series. This is the second video is his project to insert emojis into a buffer by replacing keywords with the corresponding emoji. In his previous post, Zamansky showed how to write a function to replace the keywords with the appropriate emojis. This video considers how to make the substitution happen automatically.

Experienced Elispers won’t be surprised that the vehicle Zamansky used for this was hook functions. In particular, he used the after-change-functions hook. The functions in his hook get called every time the buffer changes. That means that it gets called whenever a character is inserted. The rest is easy.

Every time a character is typed, Zamansky’s function is called by the hook. The first thing it does is check if the character is a space. If it’s not, the function simply returns. If it is, the function checks for a keyword—delimited with colons—and if found replaces the keyword with its emoji. The function is essentially the same as that from the previous video except for the checking if the current character is a space.

The thing that strikes me is how easy all this is. The function itself is short and simple and automating it is simply a matter of calling add-hook. I can’t imagine how one would do this in another editor, if you could do it at all.

The video is 13 minutes, 46 seconds so you’ll probably have to schedule some time. If you aren’t familiar with hook functions, this video is a good introduction. As usual, the video is worth your time.

This entry was posted in General and tagged , . Bookmark the permalink.