Re: [O] babel header arguments tutorial?

2015-09-27 Thread Lawrence Bottorff
I guess from a purely elisp perspective I'm baffled. How is

#+begin_src emacs-lisp
  org-babel-default-header-args:Python
#+end_src

supposed to produce

#+RESULTS:
| (:session . foo) |

as it supposedly does in the doc? It doesn't for me. (Where, e.g., would
"foo" be coming from?) If it had worked, does it make an addition to some
hash table, or an alist somewhere? But then running this as an elisp code
block is cool from a declarative programming standpoint (see this
) . So this

# Local Variables:
# eval: (setq-local org-babel-default-header-args:Python '((:session .
"foo")))
# End:

is called when you eval-buffer or open the file?

Ancillary questions:
1. can babel elisp (or CL) blocks be assigned/associated to a specifically
named session? That would enable various elisp code blocks to have separate
"session spaces" (as does the geiser/scheme babel). If so, I'm guessing the
block-session communication could also be remote? Again, with babel
geiser/scheme sessions, can they call "cached" things from each other's
different sessions?

2. . . . which makes me wonder how code blocks in a buffer can be run
besides manually C-c C-c ing them. For example,


#+name: myexptdouble
#+begin_src emacs-lisp :session
(defun myexptdouble (x y)
  (* (myexpt x y) (myexpt x y)))
#+end_src

#+RESULTS: myexptdouble
: myexptdouble

#+name: myexpt
#+begin_src emacs-lisp :session
(defun myexpt (x y)
  (expt x y))
#+end_src

#+BEGIN_SRC emacs-lisp :results output raw
(myexptdouble 2 3)
#+END_SRC

Even if I manually evaluate myexptdouble, running the last block gives an
error about not knowing what myexpt is. Is there something in the last
block that can be told to evaluate all the dependent functions? Perhaps if
my blocks are named the same as the function name? How tangling and
preserving sessions is also an interesting question, IHMO.


Re: [O] babel header arguments tutorial?

2015-09-27 Thread Thomas S . Dye
Aloha Nick,

Nick Dokos  writes:

> Thomas S. Dye  writes:
>
>> Aloha Nick,
>>
>> Nick Dokos  writes:
>>
>>> The implementation on the page you linked contains a typo (capital P
>>> instead of lower-case p in python) and is much more prolix than it needs
>>> to be. You can get the same effect with
>>>
>>> # Local Variables:
>>> # org-babel-default-header-args:python: ((:session . "foo")))
>>> # End:
>>>
>>> This form should be preferred for just setting variables. The eval
>>> mechanism should be used only when absolutely necessary.
>>
>> Can you elaborate why the eval mechanism should be used only when
>> absolutely necessary?  You've mentioned that a few times but I haven't
>> run across similar warnings elsewhere.
>>
>
> It's a matter of safety: eval allows you to evaluate arbitrary lisp
> code. Doing that in a local-variables block which is run when you
> open the file can lead to all kinds of damage. If you get a file
> with an eval in the local variables section, you'd better be very
> sure before opening the file in emacs.
>
> Here's one warning:
>
> http://www.gnu.org/software/emacs/manual/html_node/emacs/Safe-File-Variables.html

Thanks for the clarification.  Emacs' built-in paranoia seems
appropriate, I guess.  I get asked if I want to evaluate local variables
that might not be safe and one of my choices is to say no, in which case
the file is opened without evaluating them.  At that point I can look at
the code they propose to run and convince myself there is no mischief.

All the best,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com



Re: [O] babel header arguments tutorial?

2015-09-27 Thread Nick Dokos
Lawrence Bottorff  writes:

> I guess from a purely elisp perspective I'm baffled. How is
>
> #+begin_src emacs-lisp
>   org-babel-default-header-args:Python
> #+end_src
>
> supposed to produce
>
> #+RESULTS:
> | (:session . foo) |
>
> as it supposedly does in the doc? It doesn't for me. (Where, e.g., would 
> "foo" be coming from?) If it had worked, does it make an addition to some 
> hash table, or an alist somewhere?
> But then running this as an elisp code block is cool from a declarative 
> programming standpoint (see this) . So this
>
> # Local Variables:
> # eval: (setq-local org-babel-default-header-args:Python '((:session . 
> "foo")))
> # End:
>
> is called when you eval-buffer or open the file? 
>

When you open the file. But see some caveats in my reply. Charles
Berry's reply shows the preferred method today, using only what's
available in org, but does not shed any light on the local file
variables question (which is a more general mechanism, available
throughout emacs, not just with org files).

Assuming that you want to learn about file variables, we will forget
temporarily about Charles's reply.

So when you open the file, the local variables section is found and
evaluated. Any variable settings are automatically buffer-local.
That's what sets the org-babel-default-header-args:python variable
to '((:session . "foo")) - in that buffer only.

Then when you go evaluate the code block

#+begin_src emacs-lisp
org-babel-default-header-args:python
#+end_src

the variable has the give value and produces a result

#+RESULTS:
: ((:session . foo))

But if you evaluate the same code block in some other buffer, the
result will be nil (unless you have initialized in some other way
of course, e.g. by adding the initialization to your .emacs).

> Ancillary questions:
> 1. can babel elisp (or CL) blocks be assigned/associated to a
> specifically named session?
> That would enable various elisp code blocks to have separate "session
> spaces" (as does the geiser/scheme babel). If so, I'm guessing the
> block-session communication could also be remote? Again, with babel
> geiser/scheme sessions, can they call "cached" things from each
> other's different sessions?
>

Theoretically, maybe (how's that for exactness?), but there are babel
implementations that don't support sessions at all and (depending on the
underlying interpreter), there may be subtle (or perhaps not so subtle)
differences in evaluation semantics. Some experimentation should provide
answers, but N.B. that the answers may be accidents of implementation
(which some might call "bugs"). I don't think there is a strictly
defined session model that applies to all languages: the implementations
do the best they can under these circumstances.

Now this is more a hunch than anything else: I *think* that's the way
things are but I may be wrong, out-of-date, or both. A set of
well-designed experiments in as many languages as possible would clarify
the situation, and perhaps lead to improvements (they could also be
turned into unit tests, which would be very desirable) - I won't
volunteer, but maybe you would?

> 2. . . . which makes me wonder how code blocks in a buffer can be run besides 
> manually C-c C-c ing them. For example,
>
> #+name: myexptdouble
> #+begin_src emacs-lisp :session
> (defun myexptdouble (x y)
>   (* (myexpt x y) (myexpt x y)))
> #+end_src
>
> #+RESULTS: myexptdouble
> : myexptdouble
>
> #+name: myexpt
> #+begin_src emacs-lisp :session
> (defun myexpt (x y)
>   (expt x y))
> #+end_src
>
> #+BEGIN_SRC emacs-lisp :results output raw
> (myexptdouble 2 3)
> #+END_SRC
>
> Even if I manually evaluate myexptdouble, running the last block gives an 
> error about not knowing what myexpt is. Is there something in the last block 
> that can be told to evaluate all
> the dependent functions? Perhaps if my blocks are named the same as the 
> function name? How tangling and preserving sessions is also an interesting 
> question, IHMO.
>

That's not answering any of your deeper questions, but the problem here
is that the last code block evaluates a function that produces no
output: it produces a return value though, so try

#+BEGIN_SRC emacs-lisp :results value raw
 (myexptdouble 2 3)
#+END_SRC

-- 
Nick




Re: [O] babel header arguments tutorial?

2015-09-26 Thread Nick Dokos
Lawrence Bottorff  writes:

> I see this and find the bottom section ("Setting language and file
> specific default header argument value") intriguing, however too
> cryptic. Can someone explain what's going on here and how to use it?
>

Each language defines a variable where you can set header args that will
apply to *all* code block for that language (and there is a
language-independent variable too: org-babel-default-header-args). What
the document you linked to suggests is that you can set such variables
as local file variables (i.e. they are set as part of opening the file
and they are set for that file only).

The mechanism is bog-standard emacs: see (info "(emacs) file variables")
for the details.

The implementation on the page you linked contains a typo (capital P
instead of lower-case p in python) and is much more prolix than it needs
to be. You can get the same effect with

# Local Variables:
# org-babel-default-header-args:python: ((:session . "foo")))
# End:

This form should be preferred for just setting variables. The eval
mechanism should be used only when absolutely necessary.

HTH
-- 
Nick




Re: [O] babel header arguments tutorial?

2015-09-26 Thread Thomas S . Dye
Aloha Nick,

Nick Dokos  writes:

> The implementation on the page you linked contains a typo (capital P
> instead of lower-case p in python) and is much more prolix than it needs
> to be. You can get the same effect with
>
> # Local Variables:
> # org-babel-default-header-args:python: ((:session . "foo")))
> # End:
>
> This form should be preferred for just setting variables. The eval
> mechanism should be used only when absolutely necessary.

Can you elaborate why the eval mechanism should be used only when
absolutely necessary?  You've mentioned that a few times but I haven't
run across similar warnings elsewhere.

Thanks,
Tom
-- 
Thomas S. Dye
http://www.tsdye.com



Re: [O] babel header arguments tutorial?

2015-09-26 Thread Charles C. Berry

On Sat, 26 Sep 2015, Lawrence Bottorff wrote:


I see this  and
find the bottom section ("Setting language and file specific default header
argument value") intriguing, however too cryptic. Can someone explain
what's going on here and how to use it?



I'd say "don't use it". The source is several years old and filed under 
the FIXME worg directory.


header-arg handling has improved lately.

See (info "(org) Header arguments in Org mode properties") for a good 
appproach that uses this one line


#+PROPERTY: header-args:python  :session foo

to do what is in the example you cite.

HTH,

Chuck



Re: [O] babel header arguments tutorial?

2015-09-26 Thread Nick Dokos
Thomas S. Dye  writes:

> Aloha Nick,
>
> Nick Dokos  writes:
>
>> The implementation on the page you linked contains a typo (capital P
>> instead of lower-case p in python) and is much more prolix than it needs
>> to be. You can get the same effect with
>>
>> # Local Variables:
>> # org-babel-default-header-args:python: ((:session . "foo")))
>> # End:
>>
>> This form should be preferred for just setting variables. The eval
>> mechanism should be used only when absolutely necessary.
>
> Can you elaborate why the eval mechanism should be used only when
> absolutely necessary?  You've mentioned that a few times but I haven't
> run across similar warnings elsewhere.
>

It's a matter of safety: eval allows you to evaluate arbitrary lisp
code. Doing that in a local-variables block which is run when you
open the file can lead to all kinds of damage. If you get a file
with an eval in the local variables section, you'd better be very
sure before opening the file in emacs.

Here's one warning:

http://www.gnu.org/software/emacs/manual/html_node/emacs/Safe-File-Variables.html

-- 
Nick




Re: [O] babel, header arguments.

2014-11-21 Thread jenia.ivlev
Sebastien Vauban sva-n...@mygooglest.com
writes:

 jenia.ivlev wrote:
 So lets say there's this function:

 #+name: my-plus
 #+begin_src scheme
 (define my-plus
   (lambda (x y) (+ x y)))
 #+end_src

 And i want to call it from another source block, like so:

 #+tblname: addition
 | sum|
 ||
 | #ERROR |
 #+TBLFM: @2$1='(org-sbe my-plus (33 22))

 As you can see, I get an error. How do I write these header-arguments
 (i think they are called) to achieve calling one src-block from another.

 This should do it (untested):

 (org-sbe my-plus (x 33) (y 22))

 Also, second scenario, can I somehow call my-plus from a source-block as
 so:

  #+begin_src scheme
  (my-plus 3 4)
  #+end_src

 I think you must also call `org-sbe'.

 Best regards,
   Seb

Thanks a lot Seb. It worked perfectly for the table.
For the second scenario I don't understand how to import the results
of my-plus function. The only thing I can think of is something like 
this (it doesnt work by the way):

#+begin_src scheme
(+ z 1)
#+end_src
#+ z='(org-sbe my-plus (x 33) (y 22))

Also, what if I want to import the actual function defintion into another src
block:

#+begin_src scheme
(+ (my-plus 3 4) 1)
#+end_src
something here that import the previous function definitions

Is that possible?









Re: [O] babel, header arguments.

2014-11-21 Thread Thomas S. Dye
Aloha,

jenia.iv...@gmail.com (jenia.ivlev) writes:

 Also, what if I want to import the actual function defintion into another src
 block:

 #+begin_src scheme
 (+ (my-plus 3 4) 1)
 #+end_src
 something here that import the previous function definitions

 Is that possible?

Yes, see section 14.10 Noweb reference syntax in the manual.

You'll have something that looks like this:

#+header: :noweb yes
#+begin_src scheme
  previous-function-definition
  (+ (previous-function 3 4) 1)
#+end_src

hth,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com



Re: [O] babel, header arguments.

2014-11-21 Thread jenia.ivlev
t...@tsdye.com (Thomas S. Dye) writes:

 Aloha,

 jenia.iv...@gmail.com (jenia.ivlev) writes:

 Also, what if I want to import the actual function defintion into
 another src block:

 #+begin_src scheme
 (+ (my-plus 3 4) 1)
 #+end_src
 something here that import the previous function definitions

 Is that possible?

 Yes, see section 14.10 Noweb reference syntax in the manual.

 You'll have something that looks like this:

 #+header: :noweb yes
 #+begin_src scheme
   previous-function-definition
   (+ (previous-function 3 4) 1)
 #+end_src

 hth,
 Tom

What do you mean? previous-function-definition should be replaced
with the actual function definition? But I use babel-mode so that I can
interlace code in a natural language document. I want these src blocks
to be separate.




Re: [O] babel, header arguments.

2014-11-21 Thread jenia.ivlev
jenia.iv...@gmail.com (jenia.ivlev) writes:

 t...@tsdye.com (Thomas S. Dye) writes:

 Aloha,

 jenia.iv...@gmail.com (jenia.ivlev) writes:

 Also, what if I want to import the actual function defintion into
 another src block:

 #+begin_src scheme
 (+ (my-plus 3 4) 1)
 #+end_src
 something here that import the previous function definitions

 Is that possible?

 Yes, see section 14.10 Noweb reference syntax in the manual.

 You'll have something that looks like this:

 #+header: :noweb yes
 #+begin_src scheme
   previous-function-definition
   (+ (previous-function 3 4) 1)
 #+end_src

 hth,
 Tom

 What do you mean? previous-function-definition should be replaced
 with the actual function definition? But I use babel-mode so that I can
 interlace code in a natural language document. I want these src blocks
 to be separate.





Tom, thanks so so much.
If someone is interested: 

#+name: my-plus
#+begin_src scheme :noweb-ref my-plus
(define my-plus
  (lambda (x y) (+ x y)))
(my-plus 3 3)
#+end_src

#+RESULTS: my-plus
: 6


#+name: my-plus2
#+header: :noweb yes 
#+begin_src scheme
my-plus
(define my-plus2
  (lambda () (+ (my-plus 3 4) 1)))
(my-plus2)
#+end_src

#+RESULTS: my-plus2
: 8




Re: [O] babel, header arguments.

2014-11-21 Thread Thomas S. Dye
jenia.iv...@gmail.com (jenia.ivlev) writes:

 t...@tsdye.com (Thomas S. Dye) writes:

 Aloha,

 jenia.iv...@gmail.com (jenia.ivlev) writes:

 Also, what if I want to import the actual function defintion into
 another src block:

 #+begin_src scheme
 (+ (my-plus 3 4) 1)
 #+end_src
 something here that import the previous function definitions

 Is that possible?

 Yes, see section 14.10 Noweb reference syntax in the manual.

 You'll have something that looks like this:

 #+header: :noweb yes
 #+begin_src scheme
   previous-function-definition
   (+ (previous-function 3 4) 1)
 #+end_src

 hth,
 Tom

 What do you mean? previous-function-definition should be replaced
 with the actual function definition? But I use babel-mode so that I can
 interlace code in a natural language document. I want these src blocks
 to be separate.



Sorry, it refers to the name of the source code block.

,-
| The “noweb” (see http://www.cs.tufts.edu/~nr/noweb/) Literate 
| Programming system allows named blocks of code to be referenced by using
| the familiar Noweb syntax:  
| 
|  code-block-name
`-

hth,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com



Re: [O] babel, header arguments.

2014-11-21 Thread jenia.ivlev
t...@tsdye.com (Thomas S. Dye) writes:

 jenia.iv...@gmail.com (jenia.ivlev) writes:

 t...@tsdye.com (Thomas S. Dye) writes:

 Aloha,

 jenia.iv...@gmail.com (jenia.ivlev) writes:

 Also, what if I want to import the actual function defintion into
 another src block:

 #+begin_src scheme
 (+ (my-plus 3 4) 1)
 #+end_src
 something here that import the previous function definitions

 Is that possible?

 Yes, see section 14.10 Noweb reference syntax in the manual.

 You'll have something that looks like this:

 #+header: :noweb yes
 #+begin_src scheme
   previous-function-definition
   (+ (previous-function 3 4) 1)
 #+end_src

 hth,
 Tom

 What do you mean? previous-function-definition should be replaced
 with the actual function definition? But I use babel-mode so that I can
 interlace code in a natural language document. I want these src blocks
 to be separate.



 Sorry, it refers to the name of the source code block.

 ,-
 | The “noweb” (see http://www.cs.tufts.edu/~nr/noweb/) Literate 
 | Programming system allows named blocks of code to be referenced by using
 | the familiar Noweb syntax:  
 | 
 |  code-block-name
 `-

 hth,
 Tom


I have one last quick question, I have an exmaple here with no-web that
I think should work but doesnt. I wonder why:

#+BEGIN_SRC C :noweb-ref begin
int main() {
  printf(Line 1\n);

#+END_SRC

#+BEGIN_SRC C  :noweb-ref middle
printf(Second\n);
#+END_SRC

#+BEGIN_SRC C  :noweb-ref end
}
main();
#+END_SRC


#+BEGIN_SRC C  :noweb yes

begin
middle
end
printf(some appropriate debug word);

 
#+END_SRC

And the output is:
/tmp/babel-15080Ms4/C-src-150805OE.c: In function ‘main’:
/tmp/babel-15080Ms4/C-src-150805OE.c:6:7: warning: incompatible implicit 
declaration of built-in function ‘printf’
   printf(Line 1\n);
   ^
/tmp/babel-15080Ms4/C-src-150805OE.c: At top level:
/tmp/babel-15080Ms4/C-src-150805OE.c:10:5: warning: data definition has no 
type or storage class
 main();
 ^
/tmp/babel-15080Ms4/C-src-150805OE.c:12:8: error: expected declaration 
specifiers or ‘...’ before string constant
 printf(asti);
^
/bin/bash: /tmp/babel-15080Ms4/C-bin-15080GZK: Permission denied

Or if I dont include the 

printf(some appropriate debug word);

then, I get no output at all.

`Code block produced no output.`


So my question is how do I make this work. lol.


Thanks in advance again




Re: [O] babel, header arguments.

2014-11-20 Thread Sebastien Vauban
jenia.ivlev wrote:
 So lets say there's this function:

 #+name: my-plus
 #+begin_src scheme
 (define my-plus
   (lambda (x y) (+ x y)))
 #+end_src

 And i want to call it from another source block, like so:

 #+tblname: addition
 | sum|
 ||
 | #ERROR |
 #+TBLFM: @2$1='(org-sbe my-plus (33 22))

 As you can see, I get an error. How do I write these header-arguments
 (i think they are called) to achieve calling one src-block from another.

This should do it (untested):

(org-sbe my-plus (x 33) (y 22))

 Also, second scenario, can I somehow call my-plus from a source-block as
 so:

  #+begin_src scheme
  (my-plus 3 4)
  #+end_src

I think you must also call `org-sbe'.

Best regards,
  Seb

-- 
Sebastien Vauban




Re: [O] [Babel] Header arguments

2011-05-14 Thread Eric Schulte
t...@tsdye.com (Thomas S. Dye) writes:

 Aloha all,

 I have a little function that graphs two 14C dates (below).  The data
 are held in tables produced by a software package that I access on the
 web.  I read these into Org-mode and give them a #+tblname:, as shown
 below.  I'd like to have one function that will graph any number of tables
 but I don't know how to package up the table references and get them
 inside the function.  If I put them in a table, they end up as strings
 inside the function.


Hi Tom,

The following example might work, it uses an emacs-lisp code block to
wrap any number of tables into a single list of tables which could then
be passed to R.

#+data: table-names
- first-table
- second-table
- third-table

#+data: first-table
| a | 1 |
| b | 2 |

#+data: second-table
| c | 3 |
| d | 4 |

#+data: third-table
| e | 5 |
| f | 6 |

#+begin_src emacs-lisp :var table-names=table-names
  (mapcar #'org-babel-ref-resolve table-names)
#+end_src

#+results:
| (a 1) | (b 2) |
| (c 3) | (d 4) |
| (e 5) | (f 6) |

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/



Re: [O] [Babel] Header arguments

2011-05-13 Thread Nick Dokos
Thomas S. Dye t...@tsdye.com wrote:

 Aloha all,
 
 I have a little function that graphs two 14C dates (below).  The data
 are held in tables produced by a software package that I access on the
 web.  I read these into Org-mode and give them a #+tblname:, as shown
 below.  I'd like to have one function that will graph any number of tables
 but I don't know how to package up the table references and get them
 inside the function.  If I put them in a table, they end up as strings
 inside the function.
 
 I suspect I'm being thick about this.  Can someone give me a pointer to
 how this might be done?
 
 All the best,
 Tom
 
 #+tblname: theta-one-no-rat
 | cal BP | Posterior probability |
 |+---|
 |  -1520 | 1.8353001633417145E-5 |
 ...
 
 
  Two dates
 #+srcname: two-dated-events
 #+header: :file ~/org/tsdye/two-dates.pdf
 #+header: :var xlab=theta_1
 #+header: :var x=theta-one-no-rat 
 #+header: :var ylab=theta_4
 #+header: :var y=theta-four-no-rat 
 #+header:  :width 6 :height 4 :results output graphics
 #+begin_src R 
   library(ggplot2)  
   res - 
 data.frame(cal.BP=numeric(0),Posterior.probability=numeric(0),label=character(0))
   res - rbind(res,cbind(x,label=rep(xlab,dim(x)[1])))
   res - rbind(res,cbind(y,label=rep(ylab,dim(y)[1])))
   theme_set(theme_bw(base_size=11))
   g -  ggplot(res, aes(x=1950 + cal.BP, y=Posterior.probability))
   g + geom_bar(stat='identity') + xlab(Year AD) +
   ylab(Probability) + facet_wrap(~ label)
 #+end_src
 

In a similar situation, I was able to do it as follows:

,
| #+srcname: org2cw
| #+begin_src python :results output :exports none
|   s = 
|   for row in table:
|   ...do something with row and modify s...
|   print s
| 
| #+end_src
| 
| #+call: org2cw(table=support.obs) :file support.obs.cwiki
| 
| #+call: org2cw(table=support) :file support.cwiki
| 
`

Each #+call is given a different table as an argument:

,
| #+tblname: support.obs
| table elided
| 
| #+tblname: support
| table elided
`

Note the role of ``table'' as the parameter name in the #+call:s
and its use as a variable in the source block.

HTH,
Nick