Browse Source

ob-core: Make :mkdirp work for :dir too

* lisp/ob-core.el (org-babel-execute-src-block): Make directory if
  ":dir path" does not exist when ":mkdirp yes" exist.

* doc/org-manual.org (Header arguments): Document it.

* testing/lisp/test-ob.el: Add a specific testing file for ob-core.el,
  and add a testing for ":mkdir yes" work with :dir header argument
  usage.
stardiviner 6 năm trước cách đây
mục cha
commit
8b5941330b
4 tập tin đã thay đổi với 26 bổ sung4 xóa
  1. 4 3
      doc/org-manual.org
  2. 6 0
      etc/ORG-NEWS
  3. 8 1
      lisp/ob-core.el
  4. 8 0
      testing/lisp/test-ob.el

+ 4 - 3
doc/org-manual.org

@@ -17484,9 +17484,10 @@ to source file(s).
   location.  Example: =:tangle FILENAME=.
 
 #+cindex: @samp{mkdirp}, header argument
-The =mkdirp= header argument creates parent directories for tangled
-files if the directory does not exist.  =yes= enables directory
-creation and =no= inhibits directory creation.
+The =mkdirp= header argument creates parent directories for =dir=
+header argument specified path and tangled files if the directory does
+not exist.  =yes= enables directory creation and =no= inhibits
+directory creation.
 
 #+cindex: @samp{comments}, header argument
 The =comments= header argument controls inserting comments into

+ 6 - 0
etc/ORG-NEWS

@@ -157,6 +157,12 @@ A call of ~org-set-tags-command~ with prefix argument C-u C-u avoids
 the fast tag selection interface and instead offers the plain
 interface.
 
+*** ~:mkdirp~ now supports create directory for ~:dir~ path
+
+The ~:mkdirp~ header argument used to only work for ~:tangle~ tangle
+files. Now ~:mkdirp~ works for ~:dir~ too. This is more convenient for
+specify default directory and with ~:file~ header argument.
+
 * Version 9.2
 ** Incompatible changes
 *** Removal of OrgStruct mode mode and radio lists

+ 8 - 1
lisp/ob-core.el

@@ -677,8 +677,15 @@ block."
 		      (replace-regexp-in-string
 		       (org-src-coderef-regexp coderef) "" expand nil nil 1))))
 		 (dir (cdr (assq :dir params)))
+		 (mkdirp (cdr (assq :mkdirp params)))
 		 (default-directory
-		   (or (and dir (file-name-as-directory (expand-file-name dir)))
+		   (or (and dir
+			    (not (member mkdirp '("no" "nil" nil)))
+			    (progn
+			      (let ((d (file-name-as-directory
+					(expand-file-name dir))))
+				(make-directory d 'parents)
+				d)))
 		       default-directory))
 		 (cmd (intern (concat "org-babel-execute:" lang)))
 		 result)

+ 8 - 0
testing/lisp/test-ob.el

@@ -1607,6 +1607,14 @@ echo \"$data\"
 		   (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
     ))
 
+(ert-deftest test-ob-core/dir-mkdirp ()
+  (org-test-with-temp-text
+   "#+begin_src sh :mkdirp yes :dir \"data/code\"
+pwd
+#+end_src"
+   (org-babel-execute-src-block))
+  (should (file-directory-p "data/code")))
+
 (ert-deftest test-ob/script-escape ()
   ;; Delimited lists of numbers
   (should (equal '(1 2 3)