1

I'm trying to wrap my head around why the following macro does not work:

#+MACRO: INCLUDE_ORG #+INCLUDE: "$1::$2" :minlevel 3 :only-contents t

This is how I'm using it:

{{{INCLUDE_ORG(./somefile.org,Some long heading with spacesj)}}}

I was experimenting with putting quotes here and there without any progress. Using regular include without macro works just fine.

Could you please help me understand what causes the issue?

3
  • I have three questions: (1) Why do you think it is not working as expected? (2) What's the behavior you expect? and (3) what's the behavior you are getting?
    – doltes
    Nov 2 '20 at 20:36
  • Did you try escaping the spaces, e.g. ` ` with \ ?
    – Melioratus
    Nov 2 '20 at 23:01
  • Following NickD's answer, I would rather use something like yasnippet to expand manually your macro. Nov 3 '20 at 0:29
3

It does not work because macro expansion happens after include processing: by the time the macro is expanded and you have #+INCLUDE: in the output, it is too late.

The code is in the function org-export-as in file ox.el, ll.2948-2953:

...
(org-export-with-buffer-copy
     ;; Run first hook with current back-end's name as argument.
     (run-hook-with-args 'org-export-before-processing-hook
                 (org-export-backend-name backend))
     (org-export-expand-include-keyword)
     (org-export--delete-comment-trees)
     (org-macro-initialize-templates)
     (org-macro-replace-all (append org-macro-templates
                    org-export-global-macros)
                parsed-keywords)
...
1
  • Thank you so much!
    – FieryCod
    Nov 3 '20 at 8:53
0

Fixed that behavior by applying following fix to org-export-as function in ox.el.

   (org-export-expand-include-keyword)
   (org-export--delete-comment-trees)
   (org-macro-initialize-templates)
   (org-macro-replace-all (append org-macro-templates
                                            org-export-global-macros)
                                  parsed-keywords)

   ;; Duplicate include, initialize, replace before refresh
   (org-export-expand-include-keyword)
   (org-export--delete-comment-trees)
   (org-macro-initialize-templates)
   (org-macro-replace-all (append org-macro-templates
                                            org-export-global-macros)
                                  parsed-keywords)
3
  • I did not suggest that (I just diagnosed the problem), but if it works for you, I'm OK with it. But note that this is a change that you will need to carry forward every time that the Org mode code is updated. That's not too bad with git but it can become a chore. Note also that if you ever want a macro that expands to a macro that expands to an include then you will have to repeat the code one more time - and so on :-). My suggestion would be to examine alternative implementations of what you are trying to do and see if you can limit yourself to what the current code allows.
    – NickD
    Nov 3 '20 at 13:55
  • 1
    Yep sorry for that. I've edited the post so that it does not include misinformation. I will think about alternative solutions.
    – FieryCod
    Nov 4 '20 at 8:02
  • I would be very interested to hear what solution you came up with
    – Jeff Trull
    Jul 27 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.