Define org mode macro with line breaks

前端 未结 4 1661
后悔当初
后悔当初 2021-02-07 07:20

It is possible to define a macro in an org file as follow:

#+MACRO: macroname 

Is it possi

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 07:55

    Not possible with standard tools at the moment, as org-macro--collect-macros only looks at single line definitions.

    Here's a work-around:

    * 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}}}
    

    This approach favors convention over customization, so each macro has to be a level 2 child of level 1 heading with name "Macros". No additional config necessary: a plain export should work.

    The Setup heading should be copied to files where you want this to work. Or you can add the defadvice to your config to have this behavior everywhere.

提交回复
热议问题