代码块内部的org-mode宏和使用babel

时间:2014-07-23 11:58:42

标签: emacs elasticsearch org-mode

受此great post的启发,我尝试使用org-modebabel的组合向elasticsearch发出查询。例如,计算索引中的条目数:

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

当点在块中时,可以使用C-c C-c来评估上述代码。 1

另一方面,可以在组织文档中定义macros。我的问题是:是否可以定义一个宏

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

并重写代码块如下:

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

开箱即用,对我来说,它不起作用......似乎babel在评估块之前没有扩展宏。想法?

修改

现在,一旦我了解到我可以使用es-mode,我就不会对我的问题进行微调。请考虑以下两个请求:

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

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

他们只是在URL上有所不同。我想定义两个宏:

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

然后使用宏作为块的变量。有可能吗?


1 确保启用sh的评估。添加如下内容:

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

.emacs

1 个答案:

答案 0 :(得分:4)

执行代码块时,本机不支持

宏扩展,但支持的Noweb reference syntax功能更强大。

但是,我怀疑它是否可以使用es-mode,因为它传递的是header argument而不是variable的网址。

这是sh代码块的一个简单示例:

#+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
相关问题