all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Aaron Ecay <aaronecay@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [RFC] [PATCH] ob-core.el: allow the auto-generation of output file names for src blocks.
Date: Tue, 22 Apr 2014 15:54:36 -0400	[thread overview]
Message-ID: <1398196476-4773-1-git-send-email-aaronecay@gmail.com> (raw)

* lisp/ob-core.el (org-babel-generate-file-param): New function.
(org-babel-get-src-block-info): Use it.
---
 lisp/ob-core.el | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

This patch allows the autogeneration of result file names from source
block names.  Thus, instead of:

#+name: one
#+begin_src R :results file graphics :file one.png
  ...
#+end_src

One can write (with the same result, that one.png is generated):

#+name: one
#+begin_src R :results file graphics :file png
  ...
#+end_src

The benefit comes from the reduced duplication of information in the
file.  It also becomes possible to use the usual property inheritance
mechanisms (via #+PROPERTY lines or :PROPERTY: drawers) to specify
result file types for multiple source blocks at once.

This patch also introduces the :output-dir property, which can be used
to redirect all the output from blocks in a (file/subtree) to a subdirectory.

The patch treats any :file args containing a period as before, so
backwards compatibility with old files should in most cases be maintained.

If this patch looks good, I can provide tests and documentation.

Aaron

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 1348f04..2defabf 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -282,7 +282,9 @@ Returns a list
 	(setq info (org-babel-parse-inline-src-block-match))))
     ;; resolve variable references and add summary parameters
     (when (and info (not light))
-      (setf (nth 2 info) (org-babel-process-params (nth 2 info))))
+      (setf (nth 2 info) (org-babel-generate-file-param
+			  name
+			  (org-babel-process-params (nth 2 info)))))
     (when info (append info (list name indent head)))))

 (defvar org-babel-exp-reference-buffer nil
@@ -2890,6 +2892,35 @@ For the format of SAFE-LIST, see `org-babel-safe-header-args'."
 		      (member (cdr pair) (cdr entry)))
 		     (t nil)))))))

+(defun org-babel-generate-file-param (src-name params)
+  "Calculate the filename for source block results.
+
+The directory is calculated from the :output-dir property of the
+source block; if not specified, use the current directory.
+
+If the source block has a #+NAME and the :file parameter does not
+contain any period characters, then the :file parameter is
+treated as an extension, and the output file name is the
+concatenation of the directory (as calculated above), the block
+name, a period, and the parameter value as a file extension.
+Otherwise, the :file parameter is treated as a full file name,
+and the output file name is the directory (as calculated above)
+plus the parameter value."
+  (let* ((file-cons (assq :file params))
+	 (file (cdr-safe file-cons))
+	 (dir (or (cdr-safe (assq :output-dir params)) "")))
+    (when (and file (not (string= dir "")))
+      (make-directory dir t)
+      (unless (string-match "/\\'" dir)
+	(setq dir (concat dir "/"))))
+    (cond
+     ((not file) nil)
+     ((or (string-match "\\." file) (not src-name))
+      ;; Either the file name already has an extension, or there is no
+      ;; name for this source block -> we cannot do anything
+      (setcdr file-cons (concat dir file)))
+     (t (setcdr file-cons (concat dir src-name "." file))))
+    params))

 ;;; Used by backends: R, Maxima, Octave.
 (defun org-babel-graphical-output-file (params)
--
1.9.2

             reply	other threads:[~2014-04-22 19:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-22 19:54 Aaron Ecay [this message]
2014-04-22 21:22 ` [RFC] [PATCH] ob-core.el: allow the auto-generation of output file names for src blocks Bastien
2014-04-23  0:04   ` Aaron Ecay
2014-04-23  1:35     ` Eric Schulte
2014-04-23 14:58       ` Bastien
2014-04-28  2:18       ` Aaron Ecay
2014-04-28  6:20         ` Achim Gratz
2014-04-29 13:25         ` Bastien
2014-05-04 13:55         ` Eric Schulte
2014-05-11 20:38           ` Aaron Ecay
2014-05-14 17:46             ` Achim Gratz
2014-05-15 10:05               ` Bastien
2014-05-16  3:28               ` Aaron Ecay
2014-05-17  6:20                 ` Achim Gratz
2014-04-23  6:27     ` Bastien
2014-04-23 11:07       ` Eric Schulte
2014-04-23 19:44     ` Achim Gratz

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

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

  git send-email \
    --in-reply-to=1398196476-4773-1-git-send-email-aaronecay@gmail.com \
    --to=aaronecay@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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.