Closed nickdrozd closed 6 years ago
Great write-up, thank you :)
Yeah, so functions in suggest-functions
need to be pure, and rot13
isn't pure -- it messes with the current buffer. It's safe to add rot13-string
to suggest-functions
, but that only rotates the text by exactly 13 letters. You need to rotate "hal"
by 1 letter to get "ibm"
.
Is there anything actionable here? I'm hesitant to add rot13-string
because I'm struggling to imagine any situation in which users are actually looking for its behaviour.
This was quite an issue, wasn't it? It was New Year's Day, I had blasted off into outer space, and because I had recently started using Helpful, a simple Suggest debugging session turned into an archaeological excavation.
There are three parts here:
A request for adding a some rot-N function, should one exist in any of the "standard libraries". I guess there isn't one, so nothing to be done there.
A bug report / general observation as to why adding rot13
to suggest-functions
didn't work. This is actually not true anymore; I just tried adding it and it ran fine:
;; Inputs (one per line):
"abc"
;; Desired output:
"nop"
;; Suggestions:
(rot13 "abc") ;=> "nop"
(apply #'rot13 (list "abc")) ;=> "nop"
(apply #'rot13 (cons "abc" nil)) ;=> "nop"
(apply #'rot13 (-repeat 2 "abc")) ;=> "nop"
(apply #'rot13 (make-list 1 "abc")) ;=> "nop"
I don't know if something changed with Suggest or if something changed from Emacs 25.2 to 26.1 to make this happen.
Looks like rot13
just calls rot13-string
on Emacs 26.1.
Anyway, I'm going to close this as I don't think there's anything to do :)
Wouldn't it be neat if
suggest
could come up with a function like???
?https://www.youtube.com/watch?v=qDrDUmuUBTo
Actually, Emacs does have a
rot13
function, so I tried adding it tosuggest-functions
and running this:I didn't get an answer. Instead, Emacs froze until I hit
C-g
. I checked*Messages*
to see what had happened, and it was completely filled up with these:Oh, and the
suggest
buffer had been wiped clean.Now where, you ask, did that message come from? Hey, it's
hs-show-all
:Wait, why would
rot13
triggerhs-show-all
? Isn't it just a string manipulation function? No, it turns out that things are a little more complicated:rot13
callsrot13-string
, which creates a temporary buffer with just the string inserted into it.rot13-string
then passes the entire contents of the buffer (as min and max points) torot13-region
, which mutates the string in the buffer according to ROT13, and then returns the whole contents of the buffer as a string.If you're anything like me, you looked at that code and thought: What the shit is this? Why does the simple string function call the elaborate buffer-mutating function as a subroutine rather than the other way around? What kind of idiot would design a package like this?
In fact, nobody designed the package like that. Way back in 1992 (and presumably as early as 1988),
rot13.el
didn't even have therot13
function. The only function it had wasrot13-other-window
, which causes another window to display the ROT13 contents of the current window's buffer. (Note that this ROT13-display is a condition of the window rather than the buffer, and it apparently cannot be undone except by deleting the window or callingtoggle-rot13-mode
.) Indeed, the package is a self-described "hack" designed "mainly to show off the char table stuff".Is there a moral to this story? Maybe we shouldn't judge the quality of code without knowing its etiology.
By the way, the manual and the source code commentary employ strikingly different examples to illustrate a common use of ROT13. According to the manual,
whereas the source code says that