emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* stderr patch
@ 2014-03-22  3:59 Ian Kelling
  2014-03-22  8:45 ` Bastien
  2014-03-22  9:43 ` Michael Brand
  0 siblings, 2 replies; 8+ messages in thread
From: Ian Kelling @ 2014-03-22  3:59 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

I use babel mostly for shell scripts. I wrote a patch to allow toggling the
handling of errors & std err. I prefer standard error just get printed with
everything else, the same as calling a script from a terminal. Doing this properly
with header arguments etc. has been discussed before (google org-babel stderr),
and just hasn't gotten done yet. Until then, this works great, so I'm putting it
out there in case anyone wants to use it.



---
 lisp/ob-eval.el |   18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lisp/ob-eval.el b/lisp/ob-eval.el
index 057590f..fdc6da5 100644
--- a/lisp/ob-eval.el
+++ b/lisp/ob-eval.el
@@ -31,6 +31,14 @@
 (eval-when-compile (require 'cl))

 (defvar org-babel-error-buffer-name "*Org-Babel Error Output*")
+(defcustom org-babel-use-error-buffer t
+  "When evaluating a code block, if nil and an error is returned
+,no error buffer is created and. Standard error
+is redirected to standard out."
+:group 'org-babel
+:version "24.1"
+:type 'boolean)
+
 (declare-function org-babel-temp-file "ob-core" (prefix &optional suffix))

 (defun org-babel-eval-error-notify (exit-code stderr)
@@ -46,14 +54,16 @@
   "Run CMD on BODY.
 If CMD succeeds then return its results, otherwise display
 STDERR with `org-babel-eval-error-notify'."
-  (let ((err-buff (get-buffer-create " *Org-Babel Error*")) exit-code)
-    (with-current-buffer err-buff (erase-buffer))
+  (let ((err-buff (if org-babel-use-error-buffer
+		    (get-buffer-create " *Org-Babel Error*")))
+	exit-code)
+    (if err-buff (with-current-buffer err-buff (erase-buffer)))
     (with-temp-buffer
       (insert body)
       (setq exit-code
 	    (org-babel--shell-command-on-region
 	     (point-min) (point-max) cmd err-buff))
-      (if (or (not (numberp exit-code)) (> exit-code 0))
+      (if (and err-buff (or (not (numberp exit-code)) (> exit-code 0)))
 	  (progn
 	    (with-current-buffer err-buff
 	      (org-babel-eval-error-notify exit-code (buffer-string)))
@@ -90,7 +100,7 @@ function in various versions of Emacs.
     ;; This is fixed in Emacs trunk as of 2012-12-21; let's use this
     ;; workaround for now.
     (unless (file-remote-p default-directory)
-      (delete-file error-file))
+      (if error-file (delete-file error-file)))
     ;; we always call this with 'replace, remove conditional
     ;; Replace specified region with output from command.
     (let ((swap (< start end)))
-- 
1.7.10.4

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

* Re: stderr patch
  2014-03-22  3:59 stderr patch Ian Kelling
@ 2014-03-22  8:45 ` Bastien
  2014-03-22  8:50   ` Ian Kelling
  2014-03-22  9:43 ` Michael Brand
  1 sibling, 1 reply; 8+ messages in thread
From: Bastien @ 2014-03-22  8:45 UTC (permalink / raw)
  To: Ian Kelling; +Cc: emacs-orgmode@gnu.org

Hi Ian,

the idea behind the patch looks good, although the patch needs
some clean-up (adding a Changelog entry, fixing the typos in the
docstring, etc.)

Also, the patch goes beyond the 15 lines: we cannot accept it
until you sign the FSF copyright assignment.  If you're willing
to do this, please fill this form:

http://orgmode.org/cgit.cgi/org-mode.git/plain/request-assign-future.txt

Eric, can you have a look and let us know if this is good to
apply to master in a rewritten version?

Thanks,

-- 
 Bastien

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

* Re: stderr patch
  2014-03-22  8:45 ` Bastien
@ 2014-03-22  8:50   ` Ian Kelling
  2014-03-22  9:07     ` Bastien
  0 siblings, 1 reply; 8+ messages in thread
From: Ian Kelling @ 2014-03-22  8:50 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode@gnu.org

On 03/22/2014 01:45 AM, Bastien wrote:
> Hi Ian,
> 
> the idea behind the patch looks good, although the patch needs
> some clean-up (adding a Changelog entry, fixing the typos in the
> docstring, etc.)
> 
> Also, the patch goes beyond the 15 lines: we cannot accept it
> until you sign the FSF copyright assignment.  If you're willing
> to do this, please fill this form:
> 
> http://orgmode.org/cgit.cgi/org-mode.git/plain/request-assign-future.txt
> 
> Eric, can you have a look and let us know if this is good to
> apply to master in a rewritten version?
> 
> Thanks,
> 


I'd be happy to clean it up if there is interest in merging it. Also I've done the
FSF paperwork a while ago, I'm just waiting on hearing confirmation.

- Ian

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

* Re: stderr patch
  2014-03-22  8:50   ` Ian Kelling
@ 2014-03-22  9:07     ` Bastien
  2014-03-22  9:10       ` Ian Kelling
  0 siblings, 1 reply; 8+ messages in thread
From: Bastien @ 2014-03-22  9:07 UTC (permalink / raw)
  To: Ian Kelling; +Cc: emacs-orgmode@gnu.org

Hi Ian,

Ian Kelling <ianowl@gmail.com> writes:

> I'd be happy to clean it up if there is interest in merging it. Also I've done the
> FSF paperwork a while ago, I'm just waiting on hearing confirmation.

Was it more than a month ago?  If so, I'll ping them.

Let's wait for Eric's answer about whether the patch
should be applied or not, but for me yes.

-- 
 Bastien

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

* Re: stderr patch
  2014-03-22  9:07     ` Bastien
@ 2014-03-22  9:10       ` Ian Kelling
  2014-03-22  9:23         ` Bastien
  0 siblings, 1 reply; 8+ messages in thread
From: Ian Kelling @ 2014-03-22  9:10 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode@gnu.org

> Was it more than a month ago?  If so, I'll ping them.

We've been in email contact less than a week ago. It shouldn't be too long.

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

* Re: stderr patch
  2014-03-22  9:10       ` Ian Kelling
@ 2014-03-22  9:23         ` Bastien
  0 siblings, 0 replies; 8+ messages in thread
From: Bastien @ 2014-03-22  9:23 UTC (permalink / raw)
  To: Ian Kelling; +Cc: emacs-orgmode@gnu.org

Ian Kelling <ianowl@gmail.com> writes:

> We've been in email contact less than a week ago. It shouldn't be
> too long.

Okay, this takes usually up to 6 weeks, so don't hold you breath!

-- 
 Bastien

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

* Re: stderr patch
  2014-03-22  3:59 stderr patch Ian Kelling
  2014-03-22  8:45 ` Bastien
@ 2014-03-22  9:43 ` Michael Brand
  2014-03-22 10:58   ` Ian Kelling
  1 sibling, 1 reply; 8+ messages in thread
From: Michael Brand @ 2014-03-22  9:43 UTC (permalink / raw)
  To: Ian Kelling; +Cc: emacs-orgmode@gnu.org

Hi Ian

On Sat, Mar 22, 2014 at 4:59 AM, Ian Kelling <ianowl@gmail.com> wrote:
> I use babel mostly for shell scripts. I wrote a patch to allow toggling the
> handling of errors & std err. I prefer standard error just get printed with
> everything else, the same as calling a script from a terminal. Doing this properly
> with header arguments etc. has been discussed before (google org-babel stderr),
> and just hasn't gotten done yet. Until then, this works great, so I'm putting it
> out there in case anyone wants to use it.

I am generally concerned about respecting the exit status [1] and
stderr of a process, also in Org babel [2]. Thank you for taking care
of it.

> +(defcustom org-babel-use-error-buffer t
> +  "When evaluating a code block, if nil and an error is returned
> +,no error buffer is created and. Standard error
> +is redirected to standard out."
> +:group 'org-babel
> +:version "24.1"
> +:type 'boolean)

For two reasons I would much prefer if there would be not yet another
variable org-* but a header argument or a new value for e. g.
":results":
- When someone would use org-babel-use-error-buffer then sharing Org
  source blocks would be less easy.
- With a header argument it would be possible to have different
  behaviors in different Org source blocks which is also a requirement
  for backward compatibility of existing source blocks (I would want
  them to let babel execution still fail in case of a problem).

There are also use cases where I would want to capture stderr but let
babel execution fail when the exit status is non-zero, see [2].

Michael

[1] My shell prompt ($PS1) contains the exit status ($?), my shell
    scripts contain "set -o errexit; set -o pipefail".
[2] See my lengthy post here:
    http://thread.gmane.org/gmane.emacs.orgmode/45828/focus=46415

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

* Re: stderr patch
  2014-03-22  9:43 ` Michael Brand
@ 2014-03-22 10:58   ` Ian Kelling
  0 siblings, 0 replies; 8+ messages in thread
From: Ian Kelling @ 2014-03-22 10:58 UTC (permalink / raw)
  To: Michael Brand; +Cc: emacs-orgmode@gnu.org

On 03/22/2014 02:43 AM, Michael Brand wrote:
> 
> For two reasons I would much prefer if there would be not yet another
> variable org-* but a header argument or a new value for e. g.
> ":results":
> - When someone would use org-babel-use-error-buffer then sharing Org
>   source blocks would be less easy.
> - With a header argument it would be possible to have different
>   behaviors in different Org source blocks which is also a requirement
>   for backward compatibility of existing source blocks (I would want
>   them to let babel execution still fail in case of a problem).
> 
> There are also use cases where I would want to capture stderr but let
> babel execution fail when the exit status is non-zero, see [2].

I had the similar thoughts. I just wanted to share in case someone out there
wanted to apply the patch for themselves until it's done right within org mode. I
started on making a header argument, but it will be a while before I finish.

- Ian

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

end of thread, other threads:[~2014-03-22 10:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-22  3:59 stderr patch Ian Kelling
2014-03-22  8:45 ` Bastien
2014-03-22  8:50   ` Ian Kelling
2014-03-22  9:07     ` Bastien
2014-03-22  9:10       ` Ian Kelling
2014-03-22  9:23         ` Bastien
2014-03-22  9:43 ` Michael Brand
2014-03-22 10:58   ` Ian Kelling

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