Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org-mode macros inside code blocks and using babel

Inspired by this great post, I'm trying to use the combination of org-mode and babel for issuing queries to elasticsearch. For example, counting the number of entries in an index:

#+BEGIN_SRC sh
curl -XGET 'http://my.uri.example:8080/index/_count'
#+END_SRC

The above code can be evaluated using C-c C-c when the point is in the block.1

On the other hand, one can define macros in the org document. My question is: is it possible to define a macro

#+MACRO: live-db http://my.uri.example:8080

and rewrite the code block as follow:

#+BEGIN_SRC sh
curl -XGET '{{{live-db}}}/index/_count'
#+END_SRC

Out of the box, for me, it didn't work... It seems like babel is not expanding the macro before the evaluation of the block. Ideas?

Edit

Now, once I learned that I can use es-mode, I won't to fine tune my question. Consider the following two requests:

#+BEGIN_SRC es :url http://mu.uri.stage:8080
GET /users/_search?pretty
{
  "query": {
    "match_all":{}
  }
}
#+END_SRC

and

#+BEGIN_SRC es :url http://mu.uri.live:8080
GET /users/_search?pretty
{
  "query": {
    "match_all":{}
  }
}
#+END_SRC

They merely differ in the URL. I would like to define two macros:

#+MACRO staging http://my.uri.stage:8080
#+MACRO live http://my.uri.live:8080

and then use the macros as the variables of the blocks. Is it possible?


1 Make sure you enable the evaluation of sh. Add something like:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((sh . t)))

to your .emacs.

like image 485
Dror Avatar asked Jul 23 '14 11:07

Dror


1 Answers

macro expansion is not natively supported when executing code blocks, but the Noweb reference syntax which is supported is much more powerful.

However, I doubt that it will work using es-mode, since it passes the url in a header argument and not a variable.

This is a simple example for a sh code block:

#+name: staging
: http://my.uri.stage:8080

#+name: live
: http://my.uri.live:8080

#+name: test
#+begin_src sh :var url=staging
echo $url
#+end_src

#+call: test(live)

#+RESULTS:
: http://my.uri.live:8080

#+call: test(staging)

#+RESULTS:
: http://my.uri.stage:8080
like image 95
david-hoze Avatar answered Nov 11 '22 04:11

david-hoze