From mboxrd@z Thu Jan 1 00:00:00 1970 From: Aaron Ecay 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 Message-ID: <1398196476-4773-1-git-send-email-aaronecay@gmail.com> Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:35003) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcgmI-00061P-8V for emacs-orgmode@gnu.org; Tue, 22 Apr 2014 15:54:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wcgm9-0004Ym-5B for emacs-orgmode@gnu.org; Tue, 22 Apr 2014 15:54:50 -0400 Received: from mail-qc0-x22b.google.com ([2607:f8b0:400d:c01::22b]:61755) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wcgm9-0004Yc-0x for emacs-orgmode@gnu.org; Tue, 22 Apr 2014 15:54:41 -0400 Received: by mail-qc0-f171.google.com with SMTP id c9so6006888qcz.30 for ; Tue, 22 Apr 2014 12:54:40 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org * 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