On Sunday, February 28, 2016 at 10:18:37 AM UTC-8, Mat wrote:
>
> It just seem so fundamental and it has popped up before... but I can't 
> find any solution:
>

Your confusion stems from the fact that, despite the ability to have 
parameters, macros are not "functions" in the usual sense.

Macros don't "run" their content and "return the output".  A macro is 
simply a text substitution mechanism, and the ONLY action it performs is to 
replace any embedded parameters and/or variables -- the $param$ and 
$(variable)$ syntax -- and then return the modified content for further 
handling by the calling context. Other than this replacement processing, 
macros *never* "evaluate" (aka, "wikify") their output; that is *always* 
the responsibility of the context in which the macro is invoked.

The stored result (regardless if using a direct macro call or via the set 
> variable) gives the result to be a copy of the literal macro string rather 
> than the *evaluated *macro result.
>
> \define c() <$count filter="[prefix[New]]"/>
>
> <$set name="c2" value=<<c>>>
> <$button>
> <$action-setfield result=<<c>> result2=<<c2>>/>
> x
> </$button>
> </$set>
>
> What can be done? Couldn't there be some generic macro parameter to state 
> if the return value should be the evaluated result?
>

As noted above, the "evaluation" of the macro results depends entirely on 
the context into which it is returned: the macro content *might* be 
rendered (and thus, "wikified"), or it might be used as the value of a 
widget parameter, or perhaps saved as-is to a tiddler field, etc.

The immediate problem here is that you are trying to use the $count widget 
to get a value that you want to store in a variable.  This won't work, 
because the $count widgets job is to simply display the total number of 
items in the filter result.  Thus, while it produces rendered output, it 
doesn't actually "return" a value that can be stored.

What you really need is a [count[]] *filter*... then, you could use the 
$set widget to store the results of the filter.

Here's the code for a simple "count" filter:
/*\
title: filters/count.js
type: application/javascript
module-type: filteroperator
author: EricShulman
description: Filter operator for getting the number of items in a list
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports.count = function(source,operator,options)
   { var c=0; source(function(tiddler,title) { c++; }); return [c.toString
()]; };

})();

Put the above code into the text field of a tiddler, and set fields
   type="application/javascript"
and
   module-type="filteroperator"
Then save-and-reload for the new filter to be available.

Here's how you would use it:
<$set name="c" filter="[prefix[New]count[]]"
   <$button> X
      <$action-setfield result=<<c>>/>
   </$button>
</$set>

enjoy,
-e

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywiki+unsubscr...@googlegroups.com.
To post to this group, send email to tiddlywiki@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/3167eaa4-a67a-4e0c-8f22-9940f4e896cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to