[cp-chronometrist] add tangling functions

This commit is contained in:
contrapunctus 2021-02-15 16:46:34 +05:30
parent 27673a62e5
commit 1399161e20
1 changed files with 39 additions and 0 deletions

View File

@ -214,6 +214,45 @@ Return nil (and run `magit-status') if the user answers no."
do (cl-incf count)
finally do (message "%s" count))))
;; Wrote these two as potential alternatives to `org-babel-tangle',
;; which was far slower than I'd like (took around 20s for
;; chronometrist.org when I checked during the migration process, and
;; 43s after the migration was complete.) These, on the other hand,
;; are almost instant, but I don't use them anywhere because I run a
;; sed script as a file local variable.
(defun chronometrist-tangle ()
(goto-char (point-min))
(cl-loop with source
while (not (eobp))
when (looking-at-p (rx (and line-start (zero-or-more blank) "#+BEGIN_SRC emacs-lisp" line-end)))
concat (progn
(forward-line 1)
(buffer-substring-no-properties
(point)
(cl-loop while (not (eobp))
if (looking-at-p (rx (and line-start
(zero-or-more blank)
"#+END_SRC"
line-end)))
do (cl-return (point))
else do (forward-line 1)))) into source
do (forward-line 1)
finally do
(with-current-buffer (find-file-noselect "chronometrist.el")
(delete-region (point-min) (point-max))
(insert source)
(save-buffer))))
(defun chronometrist-tangle-sed ()
(let* ((file-path (buffer-file-name
(current-buffer)))
(base (file-name-base file-path)))
(when (equal "chronometrist.org" (file-name-nondirectory file-path))
(start-process-shell-command
"sed-tangle"
(generate-new-buffer-name "sed-tangle")
(format "sed -n '/#+BEGIN_SRC emacs-lisp$/,/#+END_SRC$/{//!p;}' ~s.org > ~s.el" base base)))))
(provide 'cp-chronometrist)
;; Local Variables: