Hacker News new | ask | show | jobs
by stiff 2799 days ago
To read a file into a string you actually do this:

  (with-temp-buffer
    (insert-file-contents filePath)
    (buffer-string))
but if you work with file contents as strings in Emacs Lisp, you are going to have a bad time, because performance of this is terrible enough for it to easily turn into a problem. So instead you insert-file-contents into the temporary buffer and then do a ton of low-level, error-prone imperative processing on the buffer contents itself using things like goto-char, search-forward, using markers etc. Most of the major emacs packages are littered with code like this, here is one random example:

https://github.com/magit/magit/blob/master/lisp/magit-git.el...

It reads like fortran77 to me.

3 comments

That's indeed completely unreadable and I wrote it. But it also isn't typical. In this particular case I replaced an earlier "functional" implementation with this abomination to fix a performance bottleneck. Though I think I might have gone overboard -- unfortunately the commit message isn't very good either and doesn't explain why it was necessary to switch to this style of doing things in addition to what the message actually talks about.

    (--map (list (substring it 3)
                 (aref it 0)
                 (aref it 1))
           (magit-git-items "status" "-z" "--porcelain" "-u" "--ignored"))))
Generally speaking you don't have write code like what you linked too unless you have a very good reason to do so (I doubt that I had in this case, but who knows).
Exactly my experience as well. Those are horrible patterns, that can only be excused by history.
I guess the problem is one of expectations. It is primarily a DSL for working with emacs, not a general purpose programming language like Common Lisp.