Mike Zamansky has posted the 6th episode of his Learning Elisp video series. Again, he has split the content into two videos (9 minutes, 4 seconds and 11 minutes, 22 seconds) so as not to have a single overly long video. The subject is the conclusion of his discussion of implementing a Rot13 function.
The first video discusses the use of the interactive
declaration to provide an optional argument for a function. He uses it to introduce an (optional) argument for the amount to rotate the text. He shows how to test if the argument was specified and to use the default of 13 otherwise.
The interactive
declaration’s interaction with optional arguments is surprisingly rich. You can specify all sort of expected inputs such as numbers, symbols, directories, strings, file names, and many other possibilities. Zamansky mentions of few of these before settling on the appropriate choice.
He wants, of course, to input a number but for various reasons that he explains, he settles on the P
. I have a small quibble with this part of the video. Zamansky says that Ctrl and a single digit will set the argument to 0-9, while Meta and any number of digits will set the argument to whatever number you input. On my system, Ctrl and Meta both support the any number of digits: indeed, they both call digit-argument
. As far as I can see, there’s nothing special in my init=el
mandating this behavior so you may want to check what happens on your system.
In the second video, he extends rot13
to support rotating regions. The main thing here is to detect if a region is active. Zamansky does this by checking if the mark is active. That’s okay but the recommended way is to use use-region-p
, which checks the mark but also makes sure transient-mark-mode
is set and checks for a few edge cases.
Similarly, he use (mark)
and (point)
to delimit the beginning and end of the region. The better way is to use (region-beginning)
and (region-end)
because, for example, (region-beginning)
will use the lesser of the values of the mark and point and similarly for (region-end)
.
The choice of Rot13 was a good one because its (incremental) definition demonstrates several important aspects of Elisp and its run time library. After these 6 episodes, the Elisp n00b should be in a position to start experimenting productively with Elisp.