2020-06-08 Emacs as a (very simple) CAT

From time to time, I need to translate something (usually from English to Polish). Being an Emacs user, I obviously do the typing in Emacs. However, translating – as opposed to writing – has its own set of challenges. One of them is that I need to have two texts on the screen, and track my positions in both. While the “current position” in the active window is clearly marked with the point, this is not that helpful in the “other window” – while the point is visible there, it is usually not very prominent (even if you customize the cursor-in-non-selected-windows variable, which see).

You probably expect where this is going to. Yes, I’m going to write such a tool myself. ;-)

So, here’s my simplistic take on this. At first, I just wrote the functions defining and moving around the overlay which highlighted my “current” translated sentence. Of course, for this feature to be really useful, what I needed was a minor mode with a suitable keymap. This turned out to pose a few difficulties.

First of all, I wanted the canonical way to define a minor mode which installs its keymap under some prefix key. Emacs has a similar thing, so I needed to look there and mimick its code. (I made an embarassing mistake along the way, but it works now.)

Then, I discovered that what I really want is a global minor mode. If I have the source text in one buffer and my translation in another one, I want to be able to move my overlay from both the source and destination buffers. The easiest way to accomplish this is via a global minor mode. (This has the drawback that I cannot have two texts under translation in an Emacs session at the same time. This is not a big problem, and if it were, I would have to somehow establish the link between the “source” and “destination” buffers. For instance, I could have a buffer-local variable in either of them pointing to the other one. Of course, the variable holding the overlay would also have to be buffer-local then. All of this is doable, but would make the code more complicated for little gain.)

One interesting part of this endeavor is that when I asked some questions on the Emacs mailing list, a very interesting discussion started and I learned a lot of things. (In particular, I modified my code thanks to suggestions from Emanuel and Eric (and it turned out that Eric has some similar tool in the pipeline – I can’t wait to see it in action!)

So, without further ado, here is the GitHub repository with the code. (When I’m writing these words, there is only one commit representing the more-or-less initial state, but this could change in the future, of course.)

CategoryEnglish, CategoryBlog, CategoryEmacs