From mboxrd@z Thu Jan 1 00:00:00 1970 From: Henning Redestig Subject: Re: org babel before excute hook Date: Sat, 12 Oct 2013 15:11:24 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=e89a8ff24d9dda597a04e88af8f6 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:44130) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUyyf-0000Bn-3R for emacs-orgmode@gnu.org; Sat, 12 Oct 2013 09:11:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VUyyb-0007cZ-UA for emacs-orgmode@gnu.org; Sat, 12 Oct 2013 09:11:29 -0400 Received: from mail-ie0-x22e.google.com ([2607:f8b0:4001:c03::22e]:57466) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUyyb-0007cV-M1 for emacs-orgmode@gnu.org; Sat, 12 Oct 2013 09:11:25 -0400 Received: by mail-ie0-f174.google.com with SMTP id qd12so5178558ieb.19 for ; Sat, 12 Oct 2013 06:11:25 -0700 (PDT) In-Reply-To: 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 Cc: emacs-orgmode@gnu.org --e89a8ff24d9dda597a04e88af8f6 Content-Type: text/plain; charset=ISO-8859-1 I thought about temp files, but that makes the file names cryptic which is inconvenient for communication without people outside org.. also, I tend recompile figures many times which would cause a lot of cluttering files that are hard to delete since you don't know which file is the one currently linked in your org file... Anyway, I made this instead which appears to work as I intended (defun org-babel-about-to-overwrite-file () (let ((info (org-babel-get-src-block-info))) (setq result-file (cdr (assoc :file (nth 2 info)))) (if (save-excursion (goto-char 0) (re-search-forward (concat ":file +" result-file) nil t) (re-search-forward (concat ":file +" result-file) nil t)) (message (concat result-file " defined in more than one source block")) (org-babel-execute-maybe)))) (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-about-to-overwrite-file) 2013/10/12 Charles Berry > John Kitchin andrew.cmu.edu> writes: > > > > > > > > > I have a related kind of problem. When preparing notes > > for a class, I may end up with 70 code blocks in an org file, many of > > which create graphics. I am always worried about accidentally using the > > same filename and overwriting a graphic from an earlier block. A unique, > > but reproducible filename would be sufficient for my needs. > > > > Header arg values can be elisp calls. You can use `make-temp-file'. > > So every time this block is executed, a new file is created and the > file link is added to the results. > > #+BEGIN_SRC R :results output append :file (make-temp-file "temp") > cat(date(),"\n") > #+END_SRC > > #+RESULTS: > [[file:/var/folders/kb/2hchpbyj7lb6z76l0q73w_fh0000gn/T/temp302IjV]] > [[file:/var/folders/kb/2hchpbyj7lb6z76l0q73w_fh0000gn/T/temp3028Lu]] > > See `temporary-file-directory', too, if you want to use this, as the > default may not be what you intend. > > > You might want to use this: > > #+BEGIN_SRC emacs-lisp > (defun local-tfile (file) > (let ((temporary-file-directory ".")) > (make-temp-file file))) > #+END_SRC > > Then the files go in the local directory when this is executed: > > #+BEGIN_SRC R :file (local-tfile "tfile") :results output append > cat(date(),"\n") > #+END_SRC > > You might not want `append' in this case. > > > HTH, > > Chuck > > [rest deleted] > > > > > --e89a8ff24d9dda597a04e88af8f6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
I thought about temp files, but that makes the file names = cryptic which is inconvenient for communication without people outside org.= . also, I tend recompile figures many times which would cause a lot of clut= tering files that are hard to delete since you don't know which file is= the one currently linked in your org file...

Anyway, I made this instead which appears to work as I inten= ded


(defun org-babel-about-to-overwrite-file ()
=A0 (let ((info = (org-babel-get-src-block-info)))
=A0 =A0 (setq result-file (cdr (= assoc :file (nth 2 info))))
=A0 =A0 (if (save-excursion
=A0(goto-char 0)
=A0(re-search-forw= ard (concat ":file +" result-file) nil t)
=A0(re-search-forward (concat &quo= t;:file +" result-file) nil t))
(message (concat re= sult-file " defined in more than one source block"))
= =A0 =A0 =A0 (org-babel-execute-maybe))))

(add-hook= 'org-ctrl-c-ctrl-c-hook 'org-babel-about-to-overwrite-file)





2013/10/12 Charles Berry <ccberry@ucs= d.edu>
John Kitchin <jkitchin <at> andrew.cmu.edu> writes:

>
>
>
> I have a related kind of problem. When preparing notes
> for a class, I may end up with 70 code blocks in an org file, many of<= br> > which create graphics. I am always worried about accidentally using th= e
> same filename and overwriting a graphic from an earlier block. A uniqu= e,
> =A0but reproducible filename would be sufficient for my needs.
>

Header arg values can be elisp calls. You can use `make-temp-file'= ;.

So every time this block is executed, a new file is created and the
file link is added to the results.

#+BEGIN_SRC R :results output append :file (make-temp-file "temp"= )
cat(date(),"\n")
#+END_SRC

#+RESULTS:
[[file:/var/folders/kb/2hchpbyj7lb6z76l0q73w_fh0000gn/T/temp302IjV]]
[[file:/var/folders/kb/2hchpbyj7lb6z76l0q73w_fh0000gn/T/temp3028Lu]]

See `temporary-file-directory', too, if you want to use this, as the default may not be what you intend.


You might want to use this:

#+BEGIN_SRC emacs-lisp
=A0 (defun local-tfile (file)
=A0 =A0 (let ((temporary-file-directory "."))
=A0 =A0 =A0 (make-temp-file file)))
#+END_SRC

Then the files go in the local directory when this is executed:

#+BEGIN_SRC R :file (local-tfile "tfile") :results output append<= br> cat(date(),"\n")
#+END_SRC

You might not want `append' in this case.


HTH,

Chuck

[rest deleted]





--e89a8ff24d9dda597a04e88af8f6--