emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* ob-shell: using a table variable with bash
@ 2017-03-03 12:40 George Kettleborough (EI)
  2017-03-08  8:24 ` Nicolas Goaziou
  0 siblings, 1 reply; 3+ messages in thread
From: George Kettleborough (EI) @ 2017-03-03 12:40 UTC (permalink / raw)
  To: emacs-orgmode

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

Hello,

I recently updated my org-mode from version 7 (I think) to the latest. 
The behaviour of using a table as a variable in a shell code source 
block has changed. I use to use it like this:

#+BEGIN_SRC sh :results output :var table=synteny-names :separator ,
   IFS=','
   while read col1 col2; do
       # do stuff
   done <<EOF
   $table
   EOF
#+END_SRC

This just stopped working with the new version and I could not figure 
out why for a long time. But I looked in the new ob-shell.el code and 
figured out that when the shell is bash it now makes an associative 
array if the variable is a table. I couldn't seem to find this 
documented anywhere.

This new behaviour seems like it would be useful in a lot of cases, but 
in my case, the tables are not key-value pairs, they are merely things I 
want to iterate over. I wonder if simply checking to see if :separator 
is set and using the old behaviour if so would be better? This would 
seem to be a fine fix which maintains backwards compatibility unless 
there is a reason to set :separator but still expect the new behaviour.

Thanks,

George.

[-- Attachment #2: ob-shell.patch --]
[-- Type: text/x-patch, Size: 661 bytes --]

diff --git a/site-lisp/org/ob-shell.el b/site-lisp/org/ob-shell.el
index 9c22af8..6f5f76e 100644
--- a/site-lisp/org/ob-shell.el
+++ b/site-lisp/org/ob-shell.el
@@ -140,7 +140,7 @@ This function is called by `org-babel-execute-src-block'."
 
 (defun org-babel--variable-assignments:bash (varname values &optional sep hline)
   "Represents the parameters as useful Bash shell variables."
-  (if (listp values)
+  (if (and (listp values) (null sep))
       (if (and (listp (car values)) (= 1 (length (car values))))
 	  (org-babel--variable-assignments:bash_array varname values sep hline)
 	(org-babel--variable-assignments:bash_assoc varname values sep hline))

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

* Re: ob-shell: using a table variable with bash
  2017-03-03 12:40 ob-shell: using a table variable with bash George Kettleborough (EI)
@ 2017-03-08  8:24 ` Nicolas Goaziou
  2017-03-08 11:27   ` George Kettleborough
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Goaziou @ 2017-03-08  8:24 UTC (permalink / raw)
  To: George Kettleborough (EI); +Cc: emacs-orgmode

Hello,

"George Kettleborough (EI)" <george.kettleborough@earlham.ac.uk> writes:

> I recently updated my org-mode from version 7 (I think) to the latest. 
> The behaviour of using a table as a variable in a shell code source 
> block has changed. I use to use it like this:
>
> #+BEGIN_SRC sh :results output :var table=synteny-names :separator ,
>    IFS=','
>    while read col1 col2; do
>        # do stuff
>    done <<EOF
>    $table
>    EOF
> #+END_SRC
>
> This just stopped working with the new version and I could not figure 
> out why for a long time. But I looked in the new ob-shell.el code and 
> figured out that when the shell is bash it now makes an associative 
> array if the variable is a table. I couldn't seem to find this 
> documented anywhere.
>
> This new behaviour seems like it would be useful in a lot of cases, but 
> in my case, the tables are not key-value pairs, they are merely things I 
> want to iterate over. I wonder if simply checking to see if :separator 
> is set and using the old behaviour if so would be better? This would 
> seem to be a fine fix which maintains backwards compatibility unless 
> there is a reason to set :separator but still expect the new
> behaviour.

I have no objection to this patch, but I think it needs to be
documented, if only as a code comment. IIRC, there is also some
documentation about "ob-shell" on Worg. It would be nice to document
this feature.

Also, could you provide a proper commit message?

Thank you.

Regards,

-- 
Nicolas Goaziou

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

* Re: ob-shell: using a table variable with bash
  2017-03-08  8:24 ` Nicolas Goaziou
@ 2017-03-08 11:27   ` George Kettleborough
  0 siblings, 0 replies; 3+ messages in thread
From: George Kettleborough @ 2017-03-08 11:27 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

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

On 08/03/17 08:24, Nicolas Goaziou wrote:
> I have no objection to this patch, but I think it needs to be
> documented, if only as a code comment. IIRC, there is also some
> documentation about "ob-shell" on Worg. It would be nice to document
> this feature.

I can't actually find any documentation for ob-shell. I had to read the 
source code. I have documented it as a code comment in my patch (with 
proper commit message).

Thanks,

George.


[-- Attachment #2: 0001-ob-shell-use-old-table-var-behaviour-when-sep-is-use.patch --]
[-- Type: text/x-patch, Size: 1431 bytes --]

From 97dadaf0ba0f18772d4d8ac968ea63374937b179 Mon Sep 17 00:00:00 2001
From: George Kettleborough <george.kettleborough@earlham.ac.uk>
Date: Wed, 8 Mar 2017 11:25:37 +0000
Subject: [PATCH] ob-shell: use old table var behaviour when sep is used

When using the bash shell and an org table is used as a variable, the
default behaviour is to declare it as a bash array. The generic
behaviour is to convert the table to a string with columns separated by
a given separator string. This allows a user to choose the generic
behaviour by declaring the separator using a :separator option to the
code block.
---
 lisp/ob-shell.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-shell.el b/lisp/ob-shell.el
index 9c22af8..8c7b4f1 100644
--- a/lisp/ob-shell.el
+++ b/lisp/ob-shell.el
@@ -140,7 +140,9 @@ This function is called by `org-babel-execute-src-block'."
 
 (defun org-babel--variable-assignments:bash (varname values &optional sep hline)
   "Represents the parameters as useful Bash shell variables."
-  (if (listp values)
+  ;; we declare org tables as bash array types, unless the user has
+  ;; set the :separator option
+  (if (and (listp values) (null sep))
       (if (and (listp (car values)) (= 1 (length (car values))))
 	  (org-babel--variable-assignments:bash_array varname values sep hline)
 	(org-babel--variable-assignments:bash_assoc varname values sep hline))
-- 
2.10.1.502.g6598894


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

end of thread, other threads:[~2017-03-08 20:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-03 12:40 ob-shell: using a table variable with bash George Kettleborough (EI)
2017-03-08  8:24 ` Nicolas Goaziou
2017-03-08 11:27   ` George Kettleborough

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