Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define org mode macro with line breaks

Tags:

org-mode

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

#+MACRO: macroname <here comes the body of the macro>

Is it possible to define a macro that contains line breaks? I.e., something like:

#+MACRO: macroname line 1 of macro
                   line 2 of macro

In particular, this macro will be expanded to

line 1 of macro
line 2 of macro

My motivation is to have a macro that expands to a block of text which contains, for instance, two paragraphs.

like image 984
Dror Avatar asked Mar 02 '14 19:03

Dror


2 Answers

Not only is this possible, it's quite easy to do. You just need to be creative about how you insert the newline. I make use of Org Babel to hack the macros. The following works for me:

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

So, given this document:

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

{{{macroname}}}

Exporting to Org mode gives the following

# 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

You just have to be aware that some export formats will transform that newline to a single line when it wraps the text. So, you probably want something like this to get two paragraphs:

#+MACRO: newline   src_emacs-lisp[:results raw]{"\n"}
#+MACRO: macroname line 1 of macro {{{newline}}} {{{newline}}}line 2 of macro
like image 138
emacs_ftw Avatar answered Oct 19 '22 03:10

emacs_ftw


There is another solution which seems to work correctly. From org-macro.el source code:

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

Thus, you may define macro like this:

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

Substitutions are also supported. The macro

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

which is called as

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

will be replaced with:

#+CAPTION: hello world!
file:./img1.jpg
like image 30
Evgenii Lepikhin Avatar answered Oct 19 '22 04:10

Evgenii Lepikhin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!