Tidy load-time specific source block evaluation

This commit is contained in:
Gerard Vermeulen 2022-04-17 19:05:06 +02:00
parent ec0210d63f
commit eeae503add
2 changed files with 52 additions and 25 deletions

View File

@ -1816,6 +1816,33 @@ variables in order to export the =info-org-link= types in this document to
#+caption: Evaluate specific source blocks at load-time.
#+name: lst:load-time-specific-source-block-evaluation
#+begin_src emacs-lisp
(with-eval-after-load 'emacs
(defun org-eval-infixed-blocks (infix)
"Evaluate all source blocks having INFIX in their name."
(when (eq major-mode 'org-mode)
(let ((blocks
(org-element-map
(org-element-parse-buffer 'greater-element nil) 'src-block
(lambda (block)
(when-let ((name (org-element-property :name block)))
(when (string-match-p infix name) block))))))
(dolist (block blocks)
(goto-char (org-element-property :begin block))
(org-babel-execute-src-block)))))
(defun org-eval-emacs-lisp-setup-blocks ()
"Evaluate all source blocks having \"emacs-lisp-setup\" in their name."
(org-eval-infixed-blocks "emacs-lisp-setup"))
(defun org-eval-python-setup-blocks ()
"Evaluate all source blocks having \"python-setup\" in their name."
(org-eval-infixed-blocks "python-setup"))
;; Emacs looks for "Local variables:" after the last "?\n?\f".
(add-to-list 'safe-local-eval-forms '(org-eval-emacs-lisp-setup-blocks))
(add-to-list 'safe-local-eval-forms '(org-eval-python-setup-blocks)))
(with-eval-after-load 'emacs
(defun my-org-eval-blocks-named (infix)
"Evaluate all source blocks having INFIX in their name."
(when (eq major-mode 'org-mode)
@ -1823,9 +1850,8 @@ variables in order to export the =info-org-link= types in this document to
(org-element-map
(org-element-parse-buffer 'greater-element nil) 'src-block
(lambda (block)
(let ((name (org-element-property :name block)))
(when (and name (string-match-p infix name))
block))))))
(when-let ((name (org-element-property :name block)))
(when (string-match-p infix name) block))))))
(dolist (block blocks)
(goto-char (org-element-property :begin block))
(org-babel-execute-src-block)))))
@ -1834,7 +1860,7 @@ variables in order to export the =info-org-link= types in this document to
(add-to-list 'safe-local-eval-forms
'(apply 'my-org-eval-blocks-named '("emacs-lisp-setup")))
(add-to-list 'safe-local-eval-forms
'(apply 'my-org-eval-blocks-named '("python-setup")))
'(apply 'my-org-eval-blocks-named '("python-setup"))))
#+end_src
*** [[info:org#LaTeX header and sectioning][Easy LaTeX preamble editing]]
@ -3596,6 +3622,6 @@ Only the [[info:org#Top][Org]] source file shows the local variables footer.
# Local Variables:
# compile-command: "latexmk -interaction=nonstopmode -lualatex -pvc -shell-escape README.tex"
# eval: (apply 'my-org-eval-blocks-named '("emacs-lisp-setup"))
# eval: (org-eval-emacs-lisp-setup-blocks)
# fill-column: 80
# End:

View File

@ -5,21 +5,22 @@ emacs -Q --batch --eval "
(require 'org)
(require 'ob-latex)
(require 'ox-latex)
(defun my-org-eval-blocks-named (infix)
(defun org-eval-infixed-blocks (infix)
(when (eq major-mode 'org-mode)
(let ((blocks
(org-element-map
(org-element-parse-buffer 'greater-element nil) 'src-block
(lambda (block)
(let ((name (org-element-property :name block)))
(when (and name (string-match-p infix name))
block))))))
(when-let ((name (org-element-property :name block)))
(when (string-match-p infix name) block))))))
(dolist (block blocks)
(goto-char (org-element-property :begin block))
(org-babel-execute-src-block)))))
(add-to-list 'safe-local-eval-forms
'(apply 'my-org-eval-blocks-named '(\"emacs-lisp-setup\")))
(defun org-eval-emacs-lisp-setup-blocks ()
(org-eval-infixed-blocks \"emacs-lisp-setup\"))
(add-to-list 'safe-local-eval-forms '(org-eval-emacs-lisp-setup-blocks))
(setq org-confirm-babel-evaluate nil)
(setq python-indent-guess-indent-offset nil)