A little trick in my literary programming
Hello everyone,
I'm not going to talk about literate programming, because there are already great articles about it, like this one: Introduction to Literate Programming by Howard the Geek.
Instead, I want to talk about my memory problem. Many times— and when I say many, I mean many —when I need to modify a script or a program, I just go to the code and make the change. As you’d expect. But sometimes I review the configuration in the org document where the script is located, and then I run C-c C-v C-t
to generate the code, and it overwrites the changes I made directly in the file: F**k!
So I decided to add this little snippet of code to a section in all my org files
that contain any script, so that it automatically inserts a header with information about where it came from.
* Export Data #+BEGIN_SRC elisp :var comment=";;" :results value :tangle no (format "%s Org-babel generated.\n%s Export date: %s\n%s File: %s" comment comment (format-time-string "%Y-%m-%d %H:%M:%S") comment (buffer-file-name)) #+END_SRC
This way, I can use it in any file like this:
* File =early-init.el= #+begin_src emacs-lisp :tangle ~/.config/emacs/early-init.el :noweb yes ;; -*- lexical-binding: t; -*- <<export-data()>> ;; Match theme color early on (smoother transition). ;; Theme loaded in features/ui.el. ... #+end_src
The function has a parameter comment
which defaults to ;;
, but we can use it to change the comment prefix by passing another one:
<<export-data(comment="#")>>
The result it's something like that:
;; -*- lexical-binding: t; -*- ;; Org-babel generated. ;; Export date: 2025-04-11 10:12:47 ;; File: /home/cnoceda/Qsync/config/emacs/config_files/emacs.org ;; Match theme color early on (smoother transition). ;; Theme loaded in features/ui.el. ...
Now, when I open the file, I see that comment and I use find-file-at-point
or embark
to open the org file.
As I write these lines, I realize that maybe this can already be done natively with org-babel
, but I don’t know how. In any case, here’s a trick we can always adapt to suit our needs.
Regards!
Note: My English isn’t very good; I use this blog so I don’t get rusty ☺️, but the important thing is the code, right?