简体   繁体   English

代码块内并使用babel的组织模式宏

[英]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 . 受此启发伟大的职位 ,我想使用的组合org-modebabel发出查询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 Cc Cc when the point is in the block. 当点在块中时,可以使用Cc Cc评估以上代码。 1 1个

On the other hand, one can define macros in the org document. 另一方面,可以在org文档中定义 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. 开箱即用,对我来说,它不起作用...似乎babel在评估块之前没有扩展宏。 Ideas? 有想法吗?

Edit 编辑

Now, once I learned that I can use es-mode , I won't to fine tune my question. 现在,一旦得知可以使用es-mode ,就不会微调我的问题了。 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. 它们只是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 . 1确保启用sh的评估。 Add something like: 添加类似的内容:

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

to your .emacs . .emacs

macro expansion is not natively supported when executing code blocks, but the Noweb reference syntax which is supported is much more powerful. 执行代码块时,本机不支持宏扩展,但是受支持的Noweb参考语法功能更强大。

However, I doubt that it will work using es-mode , since it passes the url in a header argument and not a variable . 但是,我怀疑它是否可以在es-mode ,因为它在标头参数而不是变量中传递url。

This is a simple example for a sh code block: 这是一个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM