Zamansky: Learning Elisp #13

Mike Zamansky has published the latest video in his emoji project. It’s a short video but, for me, the most useful so far. That’s because it’s about something I didn’t understand very well: overlays.

In the last video, Zamansky showed how to automatically replace an emoji keyword with the corresponding emoji. That worked pretty well but the problem is that it permanently changes the buffer and, if saved, the file. This episode shows how to change the display to show the emoji without altering the buffer.

That works by placing an overlay containing the emoji over the keyword. Then the display shows the emoji but the underlying buffer is unchanged. If it is saved and examined, the keyword, not the emoji, will be there. I always thought that the overlay system was complicated and obscure but it’s actually just another text property and handled the same way as the others.

To overlay whatever is at point positions BEG to END with Irreal, you simply make the call

(put-text-property BEG END 'display "Irreal")

If BEG and END mark the position of the keyword and Irreal is replaced by the emoji, the display will show the emoji without changing the buffer. The only change required from the previous code was to replace the code that killed the keyword and inserted with emoji with the code above. It could hardly be simpler.

The video is only 9 minutes, 12 seconds long so it should be easy to fit in. If you’ve ever wondered how overlays work, this video is just what you need to explain it all.

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