Macro in Variable in Regexp in Filter...

43 views
Skip to first unread message

David Szego

unread,
Feb 1, 2017, 4:14:56 PM2/1/17
to TiddlyWikiDev
Hi all,

This works to find a Tiddler created on a certain date of the month (in this case, the 1st):

\define regExDateOfMonth() ^\d{6}01 />

<$list filter="[regexp:created<regExDateOfMonth>]">
...

Either of these returns the correct string (^\d{6}01) but doesn't work:

\define regExDateOfMonth() ^\d{6}<<now 0DD>> />

<$list filter="[regexp:created<regExDateOfMonth>]">
...

\define dateOfMonth() <<now 0DD>>
\define regExDateOfMonth() ^\d{6}<<dateOfMonth>> />

<$list filter="[regexp:created<regExDateOfMonth>]">
...

So, how do I find <<now 0DD>> at the 7th character of a field, in a regexp filter?

Thanks! =-)

David.

Eric Shulman

unread,
Feb 1, 2017, 6:24:59 PM2/1/17
to TiddlyWikiDev
Keep in mind that macros are not "functions"... they don't "evaluate and return" the result... rather, macro processing only does 3 things:
  A) replace instances of $param$ using values passed to the macro as arguments
  B) replace instances of $(varname)$ using values defined outside the macro as variables
  C) return the resulting content for possible further processing in the calling context

It is up to that calling context to determine if the macro result should be parsed further.

Thus, when you wrote:
\define regExDateOfMonth() ^\d{6}<<now 0DD>>
the result was actually the literal value, "^\d{6}<<now 0DD>>", which is then *rendered* by the calling context, causing the <<now>> macro to be processed, producing the *displayed* output of "^\d{6}01".

However, when that same macro result is used within the filter syntax:
... filter="[regexp:created<regExDateOfMonth>]" ...
the returned macro value is NOT rendered, but simply used as a parameter value for the regexp filter operator.  Thus, the <<now>> macro it contains is left as a literal value.

Try this instead:

\define regExDateOfMonth() ^\d{6}$(dateOfMonth)$

<$vars dateOfMonth=<<now 0DD>> >
<$list filter="[regexp:created<regExDateOfMonth>]"\>
</$vars>

The main points:

1) First, $vars is used to store the *output* of the <<now>> macro (i.e., "01") in a variable, "dateOfMonth".

2) In the regExDateOfMonth() macro, the dateOfMonth variable is referenced using $(varname)$ syntax, which performs a *lexical substitution* of the stored value before returning the desired result (i.e., the regexp pattern using the actual month number in place of <<now 0DD>>).

That should do it.  Let me know how it goes.

enjoy,

-e
Eric Shulman
TiddlyTools: Small Tools for Big Ideas!
InsideTiddlyWiki: The Missing Manuals




David Szego

unread,
Feb 1, 2017, 7:14:58 PM2/1/17
to TiddlyWikiDev

Eric,
The solution was simple and elegant, but the lesson was invaluable!
Thank you!
D.
Reply all
Reply to author
Forward
0 new messages