emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [RFC] [PATCH] ob-core.el: allow the auto-generation of output file n


From: Aaron Ecay
Subject: [O] [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

* 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 via email to

[Prev in Thread] Current Thread [Next in Thread]