From mboxrd@z Thu Jan 1 00:00:00 1970 From: Diego Zamboni Subject: Vararg macros, and code block as macro? Date: Thu, 26 Jul 2018 16:33:31 +0200 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000274b4e0571e7e113" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:36016) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fihKj-00052B-5i for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 10:33:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fihKg-00067H-6L for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 10:33:37 -0400 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]:41734) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fihKf-00066V-SP for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 10:33:34 -0400 Received: by mail-ed1-x52f.google.com with SMTP id s24-v6so1609256edr.8 for ; Thu, 26 Jul 2018 07:33:33 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Org-mode --000000000000274b4e0571e7e113 Content-Type: text/plain; charset="UTF-8" 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 --000000000000274b4e0571e7e113 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

I have two somewhat related questio= ns:

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

2. Rela= ted 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 who= le elisp block into a single line for the macro definition, but I would lov= e to have it in a proper source block so I can edit it properly, have inden= tation and syntax highlighting, etc.

If anyone is = interested, here's my current code:=C2=A0https= ://raw.githubusercontent.com/zzamboni/zzamboni.org/master/content-org/zzamb= oni.org. You can see the "hsapi" macro, and a source block ri= ght below its definition, with the code, which for now has been joined toge= ther in the macro line.

Thanks!
--Diego<= /div>

--000000000000274b4e0571e7e113-- From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Re: Vararg macros, and code block as macro? Date: Thu, 26 Jul 2018 11:21:19 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000c497770571e88c7d" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:46975) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fii5D-0005tl-3o for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 11:21:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fii57-00041h-Cc for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 11:21:39 -0400 Received: from mail-lj1-x22f.google.com ([2a00:1450:4864:20::22f]:39189) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fii57-00041B-4C for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 11:21:33 -0400 Received: by mail-lj1-x22f.google.com with SMTP id l15-v6so1846157lji.6 for ; Thu, 26 Jul 2018 08:21:33 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Diego Zamboni Cc: Org-mode --000000000000c497770571e88c7d Content-Type: text/plain; charset="UTF-8" Hello Diego, I can try to answer your second question. On Thu, Jul 26, 2018 at 10:35 AM Diego Zamboni 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 --000000000000c497770571e88c7d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello Diego,

I can try to answer yo= ur second question.

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

2. Related to my worka= round 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 synt= ax highlighting, etc.

How about= adding this to your emacs config:

(defun my/org-m= acro-keys-code (str)
=C2=A0 "Split STR at spaces and wrap each elem= ent with `~' char, separated by `+'."
=C2=A0 (mapconcat (la= mbda (s)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 (concat "~" s "~"))
=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (split-stri= ng str)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 (concat (string ?\u200B) "+" (string ?\u200B))))
<= div>
Make sure that that always evaluates before you do Org e= xports, 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

--000000000000c497770571e88c7d-- From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Re: Vararg macros, and code block as macro? Date: Thu, 26 Jul 2018 11:39:54 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000527bb40571e8cf76" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:51029) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fiiN9-0004hk-9I for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 11:40:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fiiN7-0004DM-Rr for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 11:40:11 -0400 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]:42624) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fiiN7-0004AG-Jt for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 11:40:09 -0400 Received: by mail-lf1-x12a.google.com with SMTP id u202-v6so1509504lff.9 for ; Thu, 26 Jul 2018 08:40:09 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Diego Zamboni Cc: Org-mode --000000000000527bb40571e8cf76 Content-Type: text/plain; charset="UTF-8" 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 --000000000000527bb40571e8cf76 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Similary, how about this for your first question:

In your emacs config:

(defun= my/org-macro-hsapi-code (link anchor desc)
=C2=A0 (let* ((link-1 (if (o= rg-string-nw-p anchor)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (c= oncat link "#" anchor)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 link)= )
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (desc-1 (or (org-stri= ng-nw-p desc) link-1)))
=C2=A0=C2=A0=C2=A0 (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.notif= y,show)}}}
{{{hsapi(hs.notify,show,Foo)}}}
--

Kaushal Modi

--000000000000527bb40571e8cf76-- From mboxrd@z Thu Jan 1 00:00:00 1970 From: Diego Zamboni Subject: Re: Vararg macros, and code block as macro? Date: Thu, 26 Jul 2018 21:01:08 +0200 Message-ID: <19470276-0C3C-4140-8B79-2F21AD66910C@zzamboni.org> References: Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Content-Type: multipart/alternative; boundary="Apple-Mail=_BBECFAF9-F062-46D7-B4E5-56C8A237A943" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:38656) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1filVp-0001VP-N9 for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 15:01:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1filVn-0003XV-Vg for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 15:01:21 -0400 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]:46045) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1filVn-0003Vb-MV for emacs-orgmode@gnu.org; Thu, 26 Jul 2018 15:01:19 -0400 Received: by mail-ed1-x531.google.com with SMTP id s16-v6so2196046edq.12 for ; Thu, 26 Jul 2018 12:01:19 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Kaushal Modi Cc: Diego Zamboni , Org-mode --Apple-Mail=_BBECFAF9-F062-46D7-B4E5-56C8A237A943 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi Kaushal, > How about adding this to your emacs config: >=20 > =E2=80=A6 > 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=E2=80=99t 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: :)=20 Thanks! This is exactly what I=E2=80=99m 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=E2=80=99s macro language quite obscure. For example, here=E2=80=99= s the same =E2=80=9Chsapi=E2=80=9D shortcode in Hugo: = https://github.com/zzamboni/zzamboni.org/blob/master/layouts/shortcodes/hs= api.html = Cheers, =E2=80=94Diego --Apple-Mail=_BBECFAF9-F062-46D7-B4E5-56C8A237A943 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Hi = Kaushal,

How about adding this to your emacs config:

=E2=80=A6
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=E2=80=99t 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=E2=80=99m 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=E2=80=99s macro language quite obscure. For = example, here=E2=80=99s the same =E2=80=9Chsapi=E2=80=9D shortcode in = Hugo: https://github.com/zzamboni/zzamboni.org/blob/master/layouts/sh= ortcodes/hsapi.html

Cheers,
=E2=80=94Diego

= --Apple-Mail=_BBECFAF9-F062-46D7-B4E5-56C8A237A943--