Browse Source

Added faster tangling function for literate config

master
Josh Wolfe 2 years ago
parent
commit
a8865e1c29
1 changed files with 48 additions and 1 deletions
  1. 48
    1
      emacs/.emacs.d/init.el

+ 48
- 1
emacs/.emacs.d/init.el View File

@@ -28,4 +28,51 @@
:pin org
:ensure org-plus-contrib)

(org-babel-load-file "~/.emacs.d/README.org"))
(defun init/tangle-section-canceled ()
"Return t if the current section header was CANCELED, else nil."
(save-excursion
(if (re-search-backward "^\\*+\\s-+\\(.*?\\)?\\s-*$" nil t)
(string-prefix-p "CANCELED" (match-string 1))
nil)))

(defun init/tangle-config-org (orgfile elfile)
"This function will write all source blocks from =config.org= into
=config.el= that are ...

- not marked as :tangle no
- have a source-code of =emacs-lisp=
- doesn't have the todo-marker CANCELED"
(let* ((body-list ())
(gc-cons-threshold most-positive-fixnum)
(org-babel-src-block-regexp (concat
;; (1) indentation (2) lang
"^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
;; (3) switches
"\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
;; (4) header arguments
"\\([^\n]*\\)\n"
;; (5) body
"\\([^\000]*?\n\\)??[ \t]*#\\+end_src")))
(with-temp-buffer
(insert-file-contents orgfile)
(goto-char (point-min))
(while (re-search-forward org-babel-src-block-regexp nil t)
(let ((lang (match-string 2))
(args (match-string 4))
(body (match-string 5))
(canc (init/tangle-section-canceled)))
(when (and (string= lang "emacs-lisp")
(not (string-match-p ":tangle\\s-+no" args))
(not canc))
(add-to-list 'body-list body)))))
(with-temp-file elfile
(insert (format ";; Don't edit this file, edit %s instead ...\n\n" orgfile))
(apply 'insert (reverse body-list)))
(message "Wrote %s ..." elfile)))

(let ((orgfile (concat user-emacs-directory "README.org"))
(elfile (concat user-emacs-directory "README.el")))
(when (or (not (file-exists-p elfile))
(file-newer-than-file-p orgfile elfile))
(init/tangle-config-org orgfile elfile))
(load-file elfile)))

Loading…
Cancel
Save