Quickly Tangling
reddit / emacs
----------------------------------------------------------------------
AMLeaves · 2022-02-15 21:46
I've started putting some of the programs I've written for my own use
over the years up on GitHub. I've also written corresponding blog
posts that provide a less formal introduction to the programs and
examples of how they can be used.
This blog post is a brief one about a small sed script that can be
used to very quickly tangle certain types of org mode files:
[https://blog0.steelcandy.org/2022-02-12-quickly-
tangling.html](https://blog0.steelcandy.org/2022-02-12-quickly-
tangling.html)
That post also contains a link to the GitHub repository containing the
script.
----------------------------------------------------------------------
arthurno1 · 2022-02-16 21:04
> While it should work to tangle any source code within the
limitations outlined above, the primary reason I created it — and the
only thing I use it for currently — is to generate an Emacs Lisp file
from my literate Emacs configuration.
Sed was very fast indeed, but it is relatively trivial to write elisp
to tangle an org file unconditionally as your script does:
(defun my-tangle (to-file)
(if (file-exists-p to-file) (delete-file to-file))
(save-excursion
(goto-char 1)
(while (re-search-forward "begin_src[ \t]*emacs-lisp" nil t)
(unless (re-search-forward ":tangle[ \t]*no" (line-end-
position) t)
(let ((beg (line-end-position))
(end (progn (search-forward "end_src" nil t)
(line-beginning-position))))
(if end (append-to-file
(buffer-substring-no-properties beg end)
nil to-file)))))))
I don't know if [my literal
setup](https://github.com/amno1/.emacs.d/blob/main/init.org) is big or
small compared to what is normal, but this function takes like almost
no time. When I M-: (my-tangle (expand-file-name "tangled.el"
"~/repos/")) the Emacs seems to be done instantaneously. Benchmark-run
says 1.2 sec (1.1822892680000001 0 0.0), but that does not feel like
true. Emacs just printed the message immediately as I tapped the enter
key.
Anyway, even if Sed is faster, the convenience of having it in elisp
for debugging, calling from other scripts, running directly from
Emacs, no command prompt involved, just overweights any speed
advantage. I could also make it interactive and do some file-path
extraction to create .el file name automatically, so I don't have to
use M-: and pass a file name, but I was lazy for this example. If it
was as you say in your blog 2 minutes vs 40 milliseconds, I understand
that.
Try the above and see what you get, if it is not too much to ask. I am
just curious. You can just paste it into the scratch buffer. Or if you
can upload your setup in org file to GitHub.
Also, I don't think it is your script but my terminal (*term* in
Emacs) that does something with the output.
time ./org-quick-tangle ~/.emacs.d/init.org > ./tangled.el
I got a bunch of this formatting stuff in the output file:
(... )
(#859900setq
package-archives (nreverse package-archives))
(#859900unless
(file-directory-p init-file-modules-directory)
(make-directory init-file-modules-directory))
(#859900unless
(boundp 'init-file-hooks)
(make-variable-buffer-local 'init-file-hooks))
(#859900unless
(boundp 'init-file-package-list)
(make-variable-buffer-local 'init-file-package-list))
(...)
I just tested and got same thing in st as well (st = suckless
terminal). About first half of the file has that formatting, while the
second half comes out normal.
----------------------------------------------------------------------
paretoOptimalDev · 2022-02-16 21:52
There is way faster tangling coming and now on org 9.6 dev. My config
went from 10 seconds to 0.5.
https://lists.gnu.org/archive/html/emacs-orgmode/2022-01/msg00503.html
Graph:
https://lists.gnu.org/archive/html/emacs-
orgmode/2022-01/pngnQKAFMqFd0.png
----------------------------------------------------------------------
paretoOptimalDev · 2022-02-16 21:55
> I don't know if [my literal
setup](https://github.com/amno1/.emacs.d/blob/main/init.org) is big or
small compared to what is normal, but this function takes like almost
no time.
It looks like you don't use noweb, that's why.
----------------------------------------------------------------------
arthurno1 · 2022-02-16 22:23
Ah the utf-8 encoding. I got it now.
No I don't. I don't need it, Normally I don't even tangle that file, I
generate my init file from it. Not by just tangling but I do some
transformations on it too. You can check the generator part in the
linked file.
----------------------------------------------------------------------
CMDR_Rockman · 2022-02-22 06:01
This is cool! Thanks for sharing. Amazing that such a short sed script
can get the job done. I was looking for a faster method for tangling
as well and ended up with a gawk script. https://www.reddit.com/r/emac
s/comments/n6amm7/tangle_org_files_without_emacs_installed/