Allow tangling from other directories into `user-emacs-directory'

This commit is contained in:
Gerard Vermeulen 2024-02-07 17:07:11 +01:00
parent 84e3c44cd9
commit c1450ce0ff

View File

@ -2520,27 +2520,31 @@ Is subject to change by `toggle-post-tangle-hook-dir-usage'."
"Modes for which `dir+org-babel-post-tangle' does what it has to do."
:group 'org-babel)
(defun dir+org-babel-post-tangle ()
(defun org-babel-post-tangle-into ()
"Function to hook on `org-babel-post-tangle-hook'."
(when (and org-babel-post-tangle-dir
(derived-mode-p org-babel-post-tangle-dir-modes))
(let ((bfn (buffer-file-name)))
(unless (string-suffix-p "init.el" bfn)
(unless (or (string-suffix-p "init.el" bfn)
(string-suffix-p "dir-locals.el" bfn))
(message "Tangle file `%S' into `%S'"
(file-name-nondirectory bfn) org-babel-post-tangle-dir)
bfn org-babel-post-tangle-dir)
(rename-file bfn org-babel-post-tangle-dir t)))))
(defun toggle-post-tangle-hook-dir-usage ()
(defun org-babel-toggle-post-tangle-into ()
"Toggle tangling to `org-babel-post-tangle-dir'."
(interactive)
(if (member 'dir+org-babel-post-tangle org-babel-post-tangle-hook)
(if (member 'org-babel-post-tangle-into org-babel-post-tangle-hook)
(progn
(setq org-babel-post-tangle-dir nil)
(message "Disable tangling (into `%S')" org-babel-post-tangle-dir)
(remove-hook 'org-babel-post-tangle-hook #'dir+org-babel-post-tangle))
(setq org-babel-post-tangle-dir (org-entry-get nil "tangle-dir" t))
(remove-hook 'org-babel-post-tangle-hook #'org-babel-post-tangle-into))
(setq org-babel-post-tangle-dir
(expand-file-name (org-entry-get nil "tangle-dir" t)
user-emacs-directory))
(message "Enable tangling (into `%S')" org-babel-post-tangle-dir)
(add-hook 'org-babel-post-tangle-hook #'dir+org-babel-post-tangle))))
(add-hook 'org-babel-post-tangle-hook
#'org-babel-post-tangle-into))))
#+end_src
#+caption[Set =org-link= options]: