emacs

solf 2025-02-06T18:33:20.064479Z

Two hours spent chasing a bug :') Finally fixed it with:

(let ((data (match-data)))
                  (unwind-protect
                      (my-original-function-that-i-didn't-knew-ran-regexp-searches)
                    (set-match-data data)))
Now back to the immutable comfort of clojure

ag 2025-02-06T20:42:04.060539Z

So, why did it take you so long? How did you triage it? Did you use the built-in profiler? Did you use edebug? These days I rarely find myself in a situation where a seemingly simple bug takes hours to look for the culprit. I think once someone learns how to use profiler, debugger, helpful.el, consult-info, jumping to the source, etc. - that changes the way how you deal with Emacs. It really becomes extremelly productive tool for solving problems. Most of the time, for me, something usually breaks due to an upstream change - recent example? Magit's update broke a few things for me. It took me less than a minute to find what was up and for now I simply disabled the package that broke (since I don't have time to deal with it at the moment)

solf 2025-02-06T20:57:05.919189Z

I did all of that. Here's the error message:

Debugger entered--Lisp error: (args-out-of-range 47 68)
Annoyingly instrumenting the function that errored with edebug didn't trigger the error. I don't know enough about the internals of emacs to know why, maybe the debugger wraps each sexp in something similar to unwind-protect? As for the error itself, it happened very rarely, I managed to narrow it to html files with charset=... somewhere in them, because in auto-coding-functions there's a function that tries helpfully to set the coding-system to of html files, and it uses regexps for that: sgml-xml-auto-coding-function. So: • instrumenting with edebug hided the bug • helpful isn't that helpful when it's something hidden like auto-coding-functions • I triaged by bisecting my inputs until I found that it was charset that trigger the bug

ag 2025-02-06T21:14:24.105349Z

> edebug didn't trigger the error. • yeah, edebug sometimes fails to instrument byte-compiled functions, or macro-generated functions, of cl-defun functions. Often, I would explicitly write something like: (when condition (edebug)) to trigger the breakpoint. • Indeed, the bug does look like a tricky one, I'm glad you were able to fix it.

solf 2025-02-06T21:22:18.539389Z

It was a bug that didn't affect me at all, but it was puzzling... so I had to keep going 😅

tomd 2025-02-07T09:11:50.381269Z

I think you could simplify that with save-match-data, unless I'm missing something..?

💯 2
solf 2025-02-07T09:21:55.110959Z

I think so, now that you mention it that function does look familiar, I probably used and forgot about it years ago. When I was looking at restoring match data I read this diagonally: https://www.gnu.org/software/emacs/manual/html_node/elisp/Saving-Match-Data.html And for some reason they offer the unwind-protect as an alternative to save-match-data, but the font is bigger so my eyes skipped the rest 🙂

🙂 1
a13 2025-02-07T11:11:47.242289Z

also, there's the amazing s.el by @magnars that takes care of everything under the hood so you don't have to.