2014-03-02 15 views

回答

1

目前不可能使用標準工具,因爲 org-macro--collect-macros只能查看單行定義。

這裏有一個解決方法:

* Setup                    :noexport: 
#+begin_src elisp :exports results :results silent 
(setq my-macros 
     (mapcar 
     (lambda (x) 
     (string-match "\\*\\* \\([^\n]+\\)\n\\(.*\\)" x) 
     (cons (match-string 1 x) 
       (substring x (match-beginning 2)))) 
     (org-element-map (org-element-parse-buffer) 'headline 
     (lambda (x) 
      (and (= (org-element-property :level x) 2) 
       (string= 
       (org-element-property 
        :raw-value 
        (org-element-property :parent x)) "Macros") 
       (buffer-substring-no-properties 
       (org-element-property :begin x) 
       (org-element-property :end x))))) 
     headings)) 

(defadvice org-macro--collect-macros (around add-macros activate) 
    (let ((r ad-do-it)) 
    (setq ad-return-value 
      (append my-macros r)))) 
#+end_src 
* Macros 
** foobar 
line 1 of macro 

line 2 of macro 

** bar 
line 1 of macro 

line 2 of macro 
line 3 of macro 
* Test 
{{{foobar}}} 

{{{bar}}} 

這種做法有利於約定優於定製,所以每個宏是 1級2級的孩子名爲「宏」的標題。 無需額外的配置:一個普通的導出應該工作。

安裝標題應該被複制到你想要它工作的文件。 或者,您可以將defadvice添加到您的配置中,以便在任何地方都有此行爲。

+0

這更接近我想要/需要,但它是非常麻煩:宏

#+MACRO: img (eval "#+CAPTION: $1\nfile:$2") 

將其稱作

{{{img(hello world!, ./img1.jpg)}}} 

將被替換。我希望有一個更簡單的解決方案。 – Dror

1

我可以給你的最好的建議是:按照Nicolas Goaziou的建議(即,不要使用宏作爲你的用法),並找到另一種解決方案,以確保它現在可以永久工作。

在這種情況下,我要麼使用YASnippets(如果只是輸入時的助手),要麼使用Org Babel(如果需要在寫入文檔後進行自定義)。

+0

我有'foo.org'和'bar.org',這樣'foo'包含在'bar'中。不過,我希望能夠在'bar'中定義一些將標題添加到'foo'中的內容。所以我認爲'YAS'不是一種選擇。 'Babel'可能是.. – Dror

7

這不僅是可能的,它很容易做到。您只需要創建關於如何插入換行符的 廣告素材。我利用Org Babel來破解這些宏。鑑於此文件

#+MACRO: newline src_emacs-lisp[:results raw]{"\n"} 
#+MACRO: macroname line 1 of macro {{{newline}}}line 2 of macro 

所以:我下面的作品

#+MACRO: newline src_emacs-lisp[:results raw]{"\n"} 
#+MACRO: macroname line 1 of macro {{{newline}}}line 2 of macro 

{{{macroname}}} 

導出到組織模式提供了以下

# Created 2015-11-03 Tue 15:27 
#+TITLE: 
#+AUTHOR: 
#+MACRO: newline src_emacs-lisp[:results raw]{"\n"} 
#+MACRO: macroname line 1 of macro {{{newline}}}line 2 of macro 

line 1 of macro 
line 2 of macro 

你一定要知道,有些導出格式將包裝文本時將換行 換行爲單行。所以,你可能想要這樣的兩個段落:

#+MACRO: newline src_emacs-lisp[:results raw]{"\n"} 
#+MACRO: macroname line 1 of macro {{{newline}}} {{{newline}}}line 2 of macro 
+0

這很好,但要求在{{{{newline}}}之前加空格。您可以進一步將新行宏修改爲'@@ html:@@ src_emacs-lisp ...',以確保'src_emacs-lisp'塊之前有一個空格。 – Mark

2

還有另一種解決方案似乎正常工作。來自org-macro。EI源代碼:

;; VALUE starts with "(eval": it is a s-exp, `eval' it. 
     (when (string-match "\\`(eval\\>" value) 
      (setq value (eval (read value)))) 

因此,你可以定義宏是這樣的:

#+MACRO: newline (eval "\n") 

替換也支持。

#+CAPTION: hello world! 
file:./img1.jpg 
相關問題