all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Vararg macros, and code block as macro?
@ 2018-07-26 14:33 Diego Zamboni
  2018-07-26 15:21 ` Kaushal Modi
  0 siblings, 1 reply; 4+ messages in thread
From: Diego Zamboni @ 2018-07-26 14:33 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 999 bytes --]

Hi,

I have two somewhat related questions:

1. Is there a way for macros to check how many arguments were passed, and
change its output depending on this? At the moment I found a solution using
an =(eval...)= macro, which checks the values of $1, $2, etc. and produces
the appropriate string. It works, but I wonder if there's a more org-native
way.

2. Related to my workaround above, I am wondering if there's a way to refer
to a source block as the macro definition. At the moment I just joined my
whole elisp block into a single line for the macro definition, but I would
love to have it in a proper source block so I can edit it properly, have
indentation and syntax highlighting, etc.

If anyone is interested, here's my current code:
https://raw.githubusercontent.com/zzamboni/zzamboni.org/master/content-org/zzamboni.org.
You can see the "hsapi" macro, and a source block right below its
definition, with the code, which for now has been joined together in the
macro line.

Thanks!
--Diego

[-- Attachment #2: Type: text/html, Size: 1291 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Vararg macros, and code block as macro?
  2018-07-26 14:33 Vararg macros, and code block as macro? Diego Zamboni
@ 2018-07-26 15:21 ` Kaushal Modi
  2018-07-26 15:39   ` Kaushal Modi
  2018-07-26 19:01   ` Diego Zamboni
  0 siblings, 2 replies; 4+ messages in thread
From: Kaushal Modi @ 2018-07-26 15:21 UTC (permalink / raw)
  To: Diego Zamboni; +Cc: Org-mode

[-- Attachment #1: Type: text/plain, Size: 1138 bytes --]

Hello Diego,

I can try to answer your second question.

On Thu, Jul 26, 2018 at 10:35 AM Diego Zamboni <diego@zzamboni.org> wrote:

>
> 2. Related to my workaround above, I am wondering if there's a way to
> refer to a source block as the macro definition. At the moment I just
> joined my whole elisp block into a single line for the macro definition,
> but I would love to have it in a proper source block so I can edit it
> properly, have indentation and syntax highlighting, etc.
>

How about adding this to your emacs config:

(defun my/org-macro-keys-code (str)
  "Split STR at spaces and wrap each element with `~' char, separated by
`+'."
  (mapconcat (lambda (s)
               (concat "~" s "~"))
             (split-string str)
             (concat (string ?\u200B) "+" (string ?\u200B))))

Make sure that that always evaluates before you do Org exports, and then
simply use this in your Org files:

#+macro: keys (eval (my/org-macro-keys-code $1))
{{{keys("Ctrl c Ctrl e H H")}}}

PS: Thanks for sharing that code! You are following the vision of ox-hugo..
replacing Hugo shortcodes with Org macros :+1: :)
-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 1810 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Vararg macros, and code block as macro?
  2018-07-26 15:21 ` Kaushal Modi
@ 2018-07-26 15:39   ` Kaushal Modi
  2018-07-26 19:01   ` Diego Zamboni
  1 sibling, 0 replies; 4+ messages in thread
From: Kaushal Modi @ 2018-07-26 15:39 UTC (permalink / raw)
  To: Diego Zamboni; +Cc: Org-mode

[-- Attachment #1: Type: text/plain, Size: 550 bytes --]

Similary, how about this for your first question:

In your emacs config:

(defun my/org-macro-hsapi-code (link anchor desc)
  (let* ((link-1 (if (org-string-nw-p anchor)
                     (concat link "#" anchor)
                   link))
         (desc-1 (or (org-string-nw-p desc) link-1)))
    (concat "[[http://www.hammerspoon.org/docs/" link-1 "][" desc-1 "]]")))

In your Org file:

#+macro: hsapi (eval (my/org-macro-hsapi-code $1 $2 $3))
{{{hsapi(hs.notify)}}}
{{{hsapi(hs.notify,show)}}}
{{{hsapi(hs.notify,show,Foo)}}}
-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 956 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Vararg macros, and code block as macro?
  2018-07-26 15:21 ` Kaushal Modi
  2018-07-26 15:39   ` Kaushal Modi
@ 2018-07-26 19:01   ` Diego Zamboni
  1 sibling, 0 replies; 4+ messages in thread
From: Diego Zamboni @ 2018-07-26 19:01 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: Diego Zamboni, Org-mode

[-- Attachment #1: Type: text/plain, Size: 1163 bytes --]

Hi Kaushal,

> How about adding this to your emacs config:
> 
> …
> Make sure that that always evaluates before you do Org exports, and then simply use this in your Org files:

Thanks for the tips! I hadn’t thought of adding the functions to my Emacs config, but that would work perfectly. On the other hand, it would be nice to make the org file fully self-contained :)

> PS: Thanks for sharing that code! You are following the vision of ox-hugo.. replacing Hugo shortcodes with Org macros :+1: :) 

Thanks! This is exactly what I’m trying to do as I migrate my existing Hugo posts to ox-hugo (I migrate them whenever I need to update them in any way). My idea is to eventually republish some of my posts in a different format (as an ebook, maybe), so I want the text to be as Hugo-independent as possible. And the code is even more readable - I find Hugo’s macro language quite obscure. For example, here’s the same “hsapi” shortcode in Hugo: https://github.com/zzamboni/zzamboni.org/blob/master/layouts/shortcodes/hsapi.html <https://github.com/zzamboni/zzamboni.org/blob/master/layouts/shortcodes/hsapi.html>

Cheers,
—Diego


[-- Attachment #2: Type: text/html, Size: 2115 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-07-26 19:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-26 14:33 Vararg macros, and code block as macro? Diego Zamboni
2018-07-26 15:21 ` Kaushal Modi
2018-07-26 15:39   ` Kaushal Modi
2018-07-26 19:01   ` Diego Zamboni

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.