Christian Moe <[email protected]> writes:

I'll give the macro thing a rest for a bit, but since you're already writing a good deal of Elisp in the macros anyway, I'd like to mention another possible way of doing conditional content that avoids the hassle
with escaping commas: inline Babel calls.

This can be done in many different ways. Here are some ideas:

Use a single named source block just to return the product code for this
document. Change it for each product version.

#+NAME: product
#+BEGIN_SRC elisp
  "DEVICE2"
#+END_SRC

Use others, referencing the product block, to pick the product name and
other information.

#+NAME: prodname
#+BEGIN_SRC elisp :var p=product
  (pcase p
   ("DEVICE1" "TurboDrive")
   ("DEVICE2" "UltraDrive"))
#+END_SRC

#+NAME: howtofix
#+BEGIN_SRC elisp :var p=product
  (pcase p
   ("DEVICE1" "press the green button")
   ("DEVICE2" "turn the blue knob"))
#+END_SRC

Now you can invoke these blocks via inline ~call~ syntax:

Congratulations on your new call_prodname(). If it doesn't work, you
  can try to call_howtofix().

If you don't like monotype results and being asked to confirm all the
time, you may want to set these variables locally:

  org-babel-inline-result-wrap: "%s"
  org-confirm-babel-evaluate: nil

You may want to keep all the setup blocks in a =:noexport:= entry.

To be more systematic and write fewer source blocks -- at the cost of cluttering your ~call~ objects with arguments -- you could instead organize the snippets in a table, and write a source block to look them up by name. You could add some options for formatting at the same time.
E.g.:

#+NAME: snippets
| Snippet      | DEVICE1                | DEVICE2            |
|--------------+------------------------+--------------------|
| product-name | TurboDrive             | UltraDrive         |
| howto-fix    | press the green button | turn the blue knob |

Then you can write a single callable lookup function that will look up the snippet by identifier (left column) and product code (first row). You can also add an option to format the result in complicated ways.
For example:

#+NAME: snip
#+HEADER: :var p=product :var data=snippets
#+HEADER: :var s="product-name" :var f='nil #+HEADER: :colnames no :hlines no
#+BEGIN_SRC elisp :exports code
  (let* ((column (or (seq-position (car data) p)
                   (error "Product %s not found" p)))
         (result (or (nth column (assoc s data))
                   (error "Snippet %s not found" s))))
    ;; Format the result if F is specified
    (pcase f
      ('small-caps
(format "@@html:<span style=\"font-variant-caps: small-caps;\">%s</span>@@"
             result))
      ('all-caps (upcase result))
      (_ result)))
#+END_SRC

Use:

  To fix the problem with your call_snip(s="product-name",
  f='small-caps), first try to call_snip(s="howto-fix").

Regards,
Christian

+1

Babel is a good alternative to macros, IMO. The babel :noweb facility is possibly useful here, too.

All the best,
Tom

--
Thomas S. Dye
https://tsdye.online/tsdye

Reply via email to