emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: stardiviner <numbchild@gmail.com>
To: Org Mode <emacs-orgmode@gnu.org>
Subject: Re: [Proposal] Make header argument :mkdirp yes work for other header arguments not just :tangle
Date: Sun, 03 Mar 2019 12:32:34 +0800	[thread overview]
Message-ID: <875zt0h925.fsf@gmail.com> (raw)
In-Reply-To: <87ftsk9w03.fsf@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 987 bytes --]


Christopher M. Miles <numbchild@gmail.com> writes:

> I hope `:mkdirp` header argument can also work for other related header
> arguments like `:dir`, `:file` etc not just `:tangle`. Like following
> example.
>
> #+begin_src sh :mkdirp yes :dir "data/code/mkdirp/dir" :file "test" :results file link
> echo "hello"
> #+end_src
>
> Do you have any idea about this?

I figured out previous question now, it's because ~expand-file-name~ on ~nil~.

    #+begin_example
    Debugger entered--Lisp error: (wrong-type-argument stringp nil)
      expand-file-name(nil)
      (file-name-as-directory (expand-file-name dir))
      (let ((mkdirp (cdr (assq :mkdirp params))) (fnd (file-name-as-directory (expand-file-name dir)))) (if (and (string= mkdirp "yes") fnd) (make-directory fnd 'parents)) (or (and dir (file-name-as-directory (expand-file-name dir))) default-directory))'
    #+end_example

After fix, the patch works fine and passed all tests include my write corresponding test.
      

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-core.el-Make-mkdirp-work-for-dir-too.patch --]
[-- Type: text/x-patch, Size: 4340 bytes --]

From 53103450181c86c5b5fe07e7d4222618f9d1d8f8 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
Date: Sat, 2 Mar 2019 12:11:47 +0800
Subject: [PATCH] ob-core.el: 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-manualo.rg (mkdirp): declare new change in manual.

* etc/ORG-NEWS: declare changes in ORG-NEWS.

* testing/lisp/test-ob-core.el: Add a specific testing file for
  ob-core.el, and add a testing for :mkdir yes work with :dir header
  argument usage.
---
 doc/org-manual.org           |  7 ++++---
 etc/ORG-NEWS                 |  6 ++++++
 lisp/ob-core.el              |  7 ++++++-
 testing/lisp/test-ob-core.el | 30 ++++++++++++++++++++++++++++++
 4 files changed, 46 insertions(+), 4 deletions(-)
 create mode 100644 testing/lisp/test-ob-core.el

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 00e5e1072..30c797ad7 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17476,9 +17476,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
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 057440a71..cd5e4d900 100644
--- a/etc/ORG-NEWS
+++ b/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
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index e6f0edba5..b2a5ee5cb 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -677,8 +677,13 @@ 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
+			    ;; Possibly create the parent directories for file.
+			    (let (fnd (file-name-as-directory (expand-file-name dir)))
+			      (if (and (string= mkdirp "yes") fnd)
+				  (make-directory fnd 'parents))))
 		       default-directory))
 		 (cmd (intern (concat "org-babel-execute:" lang)))
 		 result)
diff --git a/testing/lisp/test-ob-core.el b/testing/lisp/test-ob-core.el
new file mode 100644
index 000000000..c601e44eb
--- /dev/null
+++ b/testing/lisp/test-ob-core.el
@@ -0,0 +1,30 @@
+;;; test-ob-core.el --- tests for ob-core.el
+
+;; Copyright (c) 2018-2022 Free Software Foundation, Inc.
+;; Authors: stardiviner
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+(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"))))
+
+(provide 'test-ob-core)
+
+;;; test-ob-core.el ends here
-- 
2.21.0


[-- Attachment #3: Type: text/plain, Size: 247 bytes --]


-- 
[ stardiviner ]
       I try to make every word tell the meaning what I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

      parent reply	other threads:[~2019-03-03  4:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-19  5:40 [Proposal] Make header argument :mkdirp yes work for other header arguments not just :tangle Christopher M. Miles
2019-03-02  4:55 ` stardiviner
2019-03-03  7:23   ` [FEATURE] " stardiviner
2019-03-03  8:03     ` Nicolas Goaziou
2019-03-03 15:15       ` stardiviner
2019-03-04 22:21         ` Nicolas Goaziou
2019-03-05  5:13           ` stardiviner
2019-03-05  6:02           ` [Discuss] make :tangle header argument respect :dir could save info typing stardiviner
2019-03-19 13:08             ` Sean O'Halpin
2019-03-20  5:47               ` stardiviner
2019-03-03  4:32 ` stardiviner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875zt0h925.fsf@gmail.com \
    --to=numbchild@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).