emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org babel before excute hook
@ 2013-10-11 13:56 Henning Redestig
  2013-10-11 20:31 ` John Kitchin
  0 siblings, 1 reply; 11+ messages in thread
From: Henning Redestig @ 2013-10-11 13:56 UTC (permalink / raw)
  To: emacs-orgmode

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

I collaborate with different people on the same orgfile which contains many
source blocks that generate graphics by e.g. :results graphics abc.pdf

It can happen that I or someone else accidentally create another
independent source block that overwrites my abc.pdf which is of course very
bad.

I would like to add functionality so that org-babel-execute-src-block
checks if in :results graphics FILE, FILE is already referred to by another
source block and if so refuse to evaluate.

However, I only see a org-babel-after-execute-hook but no
org-babel-before-execute-hook

any reason for this? I could try to write a patch but thought I'd ask
first.

....or if there is an even better approach to avoid overwriting output from
different source blocks..

//Henning

[-- Attachment #2: Type: text/html, Size: 905 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-11 13:56 org babel before excute hook Henning Redestig
@ 2013-10-11 20:31 ` John Kitchin
  2013-10-12  2:28   ` Charles Berry
  0 siblings, 1 reply; 11+ messages in thread
From: John Kitchin @ 2013-10-11 20:31 UTC (permalink / raw)
  Cc: emacs-orgmode@gnu.org

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

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.

John

John

-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu



On Fri, Oct 11, 2013 at 9:56 AM, Henning Redestig <henning.red@gmail.com>wrote:

> I collaborate with different people on the same orgfile which contains
> many source blocks that generate graphics by e.g. :results graphics abc.pdf
>
> It can happen that I or someone else accidentally create another
> independent source block that overwrites my abc.pdf which is of course very
> bad.
>
> I would like to add functionality so that org-babel-execute-src-block
> checks if in :results graphics FILE, FILE is already referred to by another
> source block and if so refuse to evaluate.
>
> However, I only see a org-babel-after-execute-hook but no
> org-babel-before-execute-hook
>
> any reason for this? I could try to write a patch but thought I'd ask
> first.
>
> ....or if there is an even better approach to avoid overwriting output
> from different source blocks..
>
> //Henning
>

[-- Attachment #2: Type: text/html, Size: 2151 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-11 20:31 ` John Kitchin
@ 2013-10-12  2:28   ` Charles Berry
  2013-10-12 13:11     ` Henning Redestig
  2013-10-13 21:22     ` Henning Redestig
  0 siblings, 2 replies; 11+ messages in thread
From: Charles Berry @ 2013-10-12  2:28 UTC (permalink / raw)
  To: emacs-orgmode

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 
> 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]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-12  2:28   ` Charles Berry
@ 2013-10-12 13:11     ` Henning Redestig
  2013-10-13 21:22     ` Henning Redestig
  1 sibling, 0 replies; 11+ messages in thread
From: Henning Redestig @ 2013-10-12 13:11 UTC (permalink / raw)
  Cc: emacs-orgmode

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

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 <ccberry@ucsd.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
> > 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]
>
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 3519 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-12  2:28   ` Charles Berry
  2013-10-12 13:11     ` Henning Redestig
@ 2013-10-13 21:22     ` Henning Redestig
  2013-10-13 21:35       ` Samuel Wales
  2013-10-14 13:29       ` Eric Schulte
  1 sibling, 2 replies; 11+ messages in thread
From: Henning Redestig @ 2013-10-13 21:22 UTC (permalink / raw)
  To: emacs-orgmode

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

Is it possible to add a function to org-ctrl-c-ctrl-c-hook without patching
ob-core.el? If I just add something like

(add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-stop-if-file-collision)

in my .emacs I notice that my addition gets overwritten later via the
autoloads (I think) that are defined in ob-core.el..



2013/10/12 Charles Berry <ccberry@ucsd.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
> > 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]
>
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 2495 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-13 21:22     ` Henning Redestig
@ 2013-10-13 21:35       ` Samuel Wales
  2013-10-14  6:45         ` Henning Redestig
  2013-10-14 13:24         ` Eric Schulte
  2013-10-14 13:29       ` Eric Schulte
  1 sibling, 2 replies; 11+ messages in thread
From: Samuel Wales @ 2013-10-13 21:35 UTC (permalink / raw)
  To: Henning Redestig; +Cc: emacs-orgmode

In case it helps, there is org-confirm-babel-evaluate.

(But I have not found it to be useful, because it does not seem to
place point in a place where you can check properties, etc.)

Samuel

-- 
The Kafka Pandemic: http://thekafkapandemic.blogspot.com

The disease DOES progress.  MANY people have died from it.  ANYBODY can get it.

Denmark: free Karina Hansen NOW.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-13 21:35       ` Samuel Wales
@ 2013-10-14  6:45         ` Henning Redestig
  2013-10-14  8:11           ` [babel] Feature request - WAS: " Rainer M Krug
  2013-10-14 13:24         ` Eric Schulte
  1 sibling, 1 reply; 11+ messages in thread
From: Henning Redestig @ 2013-10-14  6:45 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: John Kitchin

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

if anyone is interested in this, a simple defadvice appears to be a good
option, I put

(defadvice org-babel-execute-maybe (around org-babel-stop-on-collision)
  "stop execution of result file defined more than once"
  (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))
        (error (concat result-file " defined in more than one source
block"))
      ad-do-it)))
(ad-activate 'org-babel-execute-maybe)


in my .emacs and appear to get the desired functionality




2013/10/13 Samuel Wales <samologist@gmail.com>

> In case it helps, there is org-confirm-babel-evaluate.
>
> (But I have not found it to be useful, because it does not seem to
> place point in a place where you can check properties, etc.)
>
> Samuel
>
> --
> The Kafka Pandemic: http://thekafkapandemic.blogspot.com
>
> The disease DOES progress.  MANY people have died from it.  ANYBODY can
> get it.
>
> Denmark: free Karina Hansen NOW.
>

[-- Attachment #2: Type: text/html, Size: 1734 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [babel] Feature request - WAS: org babel before excute hook
  2013-10-14  6:45         ` Henning Redestig
@ 2013-10-14  8:11           ` Rainer M Krug
  0 siblings, 0 replies; 11+ messages in thread
From: Rainer M Krug @ 2013-10-14  8:11 UTC (permalink / raw)
  To: emacs-orgmode

At the moment, I have the feeling that there is no solution to the
actual problem (avoiding duplicate :file names) without doctoring in an
unsatisfying (and potentially fragile?) way.

I would therefore formulate a feature request:

It is a common error to accidentally use the same :file name and to have
consequently the wrong file in the exported file.

I would therefore suggest a property with the following possible values:

,----
| file_unique
|
| - false :: the actual behaviour, i.e. consequtive :file overwrite the
| original ones *without* warning
| - true :: if more then one :file exist, an consecutive number is automatically
| to the :file and cached to avoid having leftover files (as in temp files)
| - warn :: give a warning on export, so that this error can be fixed
| manually.
`----

I would see this as a valuable addition to the export feature as it
avoids errors.

Cheers,

Rainer


Henning Redestig <henning.red@gmail.com> writes:

> if anyone is interested in this, a simple defadvice appears to be a good
> option, I put
>
> (defadvice org-babel-execute-maybe (around org-babel-stop-on-collision)
>   "stop execution of result file defined more than once"
>   (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))
>         (error (concat result-file " defined in more than one source
> block"))
>       ad-do-it)))
> (ad-activate 'org-babel-execute-maybe)
>
>
> in my .emacs and appear to get the desired functionality
>
>
>
>
> 2013/10/13 Samuel Wales <samologist@gmail.com>
>
>> In case it helps, there is org-confirm-babel-evaluate.
>>
>> (But I have not found it to be useful, because it does not seem to
>> place point in a place where you can check properties, etc.)
>>
>> Samuel
>>
>> --
>> The Kafka Pandemic: http://thekafkapandemic.blogspot.com
>>
>> The disease DOES progress.  MANY people have died from it.  ANYBODY can
>> get it.
>>
>> Denmark: free Karina Hansen NOW.
>>
<#secure method=pgpmime mode=sign>

-- 
Rainer M. Krug

email: RMKrug<at>gmail<dot>com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-13 21:35       ` Samuel Wales
  2013-10-14  6:45         ` Henning Redestig
@ 2013-10-14 13:24         ` Eric Schulte
  2013-10-26 22:05           ` Samuel Wales
  1 sibling, 1 reply; 11+ messages in thread
From: Eric Schulte @ 2013-10-14 13:24 UTC (permalink / raw)
  To: Samuel Wales; +Cc: emacs-orgmode, Henning Redestig

Samuel Wales <samologist@gmail.com> writes:

> In case it helps, there is org-confirm-babel-evaluate.
>
> (But I have not found it to be useful, because it does not seem to
> place point in a place where you can check properties, etc.)
>

I just pushed up a change so that `org-confirm-babel-evaluate' will
always be called from the head of the code block being evaluated.  So
the info can always be reached with something like the following.

    (setf org-confirm-babel-evaluate
          (lambda (&rest args)
            (message "info: %S" (org-babel-get-src-block-info 'light))
            nil))

>
> Samuel

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-13 21:22     ` Henning Redestig
  2013-10-13 21:35       ` Samuel Wales
@ 2013-10-14 13:29       ` Eric Schulte
  1 sibling, 0 replies; 11+ messages in thread
From: Eric Schulte @ 2013-10-14 13:29 UTC (permalink / raw)
  To: Henning Redestig; +Cc: emacs-orgmode

Henning Redestig <henning.red@gmail.com> writes:

> Is it possible to add a function to org-ctrl-c-ctrl-c-hook without patching
> ob-core.el? If I just add something like
>

Yes, see the documentation of `org-confirm-babel-evaluate'.  So the
function posted in your previous email could be changed to something
like...

    (setf org-confirm-babel-evaluate
          (lambda (&rest args)
            (let* ((info (org-babel-get-src-block-info))
                   (result-file (cdr (assoc :file (nth 2 info))))
                   (duplicat-file-p
                    (save-excursion
                      (goto-char 0)
                      (re-search-forward (concat ":file +" result-file) nil t)
                      (re-search-forward (concat ":file +" result-file) nil t)))))
            (if duplicate-file-p
                (prog1 t (message "duplicate result file"))
                nil)))

>
> (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-stop-if-file-collision)
>
> in my .emacs I notice that my addition gets overwritten later via the
> autoloads (I think) that are defined in ob-core.el..
>
>
>
> 2013/10/12 Charles Berry <ccberry@ucsd.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
>> > 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]
>>
>>
>>
>>
>>

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: org babel before excute hook
  2013-10-14 13:24         ` Eric Schulte
@ 2013-10-26 22:05           ` Samuel Wales
  0 siblings, 0 replies; 11+ messages in thread
From: Samuel Wales @ 2013-10-26 22:05 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode, Henning Redestig

I have confirmed that this works.  Thank you.

On 10/14/13, Eric Schulte <schulte.eric@gmail.com> wrote:
> I just pushed up a change so that `org-confirm-babel-evaluate' will
> always be called from the head of the code block being evaluated.  So
> the info can always be reached with something like the following.
>
>     (setf org-confirm-babel-evaluate
>           (lambda (&rest args)
>             (message "info: %S" (org-babel-get-src-block-info 'light))
>             nil))

-- 
The Kafka Pandemic: http://thekafkapandemic.blogspot.com

The disease DOES progress.  MANY people have died from it.  ANYBODY can get it.

Denmark: free Karina Hansen NOW.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-10-26 22:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-11 13:56 org babel before excute hook Henning Redestig
2013-10-11 20:31 ` John Kitchin
2013-10-12  2:28   ` Charles Berry
2013-10-12 13:11     ` Henning Redestig
2013-10-13 21:22     ` Henning Redestig
2013-10-13 21:35       ` Samuel Wales
2013-10-14  6:45         ` Henning Redestig
2013-10-14  8:11           ` [babel] Feature request - WAS: " Rainer M Krug
2013-10-14 13:24         ` Eric Schulte
2013-10-26 22:05           ` Samuel Wales
2013-10-14 13:29       ` Eric Schulte

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).