Feature Request: List Macro to select multiple Tags

10 views
Skip to first unread message

Jim Barr

unread,
Aug 10, 2005, 4:57:52 PM8/10/05
to TiddlyWikiDev
OK, these new Macros are getting REALLY fun! Here's a suggestion for
yet another incarnation:

How about creating a Macro that will list Tiddlers having two or more
specified Tags?

For example, say I have several Tiddlers Tagged as "Project1", and
several Tagged "Project2". Some of those Tiddlers are also Tagged
"Pending", and some others are Tagged "Completed".

Using the listAll Macro in the form <<listTags Project1>> I get a list
of all Project1 Tiddlers, but the list also contains both Pending and
Completed Tiddlers.

OK, so I could use <<listTags Pending>> to view all Pending Tiddlers,
but the list now contains Tiddlers for both Project1 and Project2.

So, my request is to be able to view a list of Tiddlers that are (in
this example) Pending and part of Project1 (ei: Tagged with both
Project1 and Pending.)

Ideally, something like: <<listTags sometag1 sometag2>> would be nice,
but the problem is that the current configuration of listAll (which is
fantastic, by the way) provides a second parameter for sorting. So it
would obviously require a re-work.

Any takers?!?

-Jim Barr
http://tiddlywikitips.com

sobaboy

unread,
Aug 19, 2005, 3:12:25 AM8/19/05
to TiddlyWikiDev
I have posted a TiddlyWiki with a listOnlyTags macro at
http://www.sobaboy.com/listOnlyTags/

It combines the features of the two listTags macros and the listTagsAnd
macro.

It will accept multiple Tags, an option to sort by title or modified,
and output as a numbered/bulleted list.

And I hope it will run in better than O(n^^2) :)

See the page for more info, the macro, and some examples.

Thanks to the TiddlyWiki Community for a great package, adaptations,
stylesheets, and plugins/macros.

takeshi yano

AlanH

unread,
Aug 19, 2005, 6:52:56 AM8/19/05
to TiddlyWikiDev
Thanks for this contribution. It occurs to me, however, that such a
macro (allowing multiple tags) should have either/or or both/and
capability. If I list two tags with the current version of
listOnlyTags can I stipulate that a tiddler should get included if it
has either one of these two tags (and not necessarily both)?

Paul Petterson

unread,
Aug 19, 2005, 10:25:52 AM8/19/05
to Tiddly...@googlegroups.com
you mean like:
<<listWithTag tag1>>
<<listWithTag tag2>>?

The difference between this and <<listWithTag tag1 OR tag2>> would simply be ordering correct?

AlanH

unread,
Aug 19, 2005, 10:41:54 AM8/19/05
to TiddlyWikiDev
Yes, for instance: if you wanted to merge two tiddler topics together
via the tags they each use, but you want them all sorted as one list by
title, etc.
~AH

sobaboy

unread,
Aug 19, 2005, 7:36:53 PM8/19/05
to TiddlyWikiDev
listOnlyTags was written to allow me to filter by tags.

<<listOnlyTags client-X project-Y stage-Z title *>>
<<listOnlyTags contacts client-X title *>>

So listOnlyTags is an AND only design.

I would suggest another separate macro which would OR the tags and take
the sorting/formatting parameters, or a better named macro which has
options for AND/OR matching, sorting and a formatting prefix.

Something along the lines of listSwissArmyKnifeTags. :)

takeshi yano

Paul Petterson

unread,
Aug 19, 2005, 11:46:13 PM8/19/05
to Tiddly...@googlegroups.com, Jim Barr
Okey Dokey - I whipped up a whopper of a list macro... I've tested it on windows in firefox and IE 6.

Let's you do stuff like this:

<<list withTags project AND (important OR urgent)>>

Interested? :)

// // ''Plugin Name:'' list //withTags// <criteria>
// // ''Author:'' PaulPetterson
// // ''Purpose:'' extends the TiddlyWiki list macro with support for listing tiddlers with specified tags
// // ''Parameters:'' 1..N: tag selection criteria - //tag//, AND, OR, NOT, and/or Parentheses
// // ''Usage:'' insert <list withTags systemConfig OR systemTiddlers> to list out all tiddlers with a systemConfig or systemTiddlers tag
// //  ''Notes:'' it must be a valid boolean expression, an invalid expression will return a macro error. Follows standard operator precedence so use parentheses to disambiguate the criteria.  You can substitute the JavaScript standard symbols &&, ||, and ! for AND, OR, and NOT.
// // ''Examples:'' <list withTags project and (urgent or important)> - will list all tiddlers with a project tag and either an urgent or important tag.

config.macros.list.withTags = {}
config.macros.list.withTags.handler = function(params)
{
    if ( !params || !params[1] )
        return config.macros.list.all.handler(params);

    // build us a regex of all our tags as a big-old regex that OR's the tags (tag1|tag2|tag3...)
    var tags = store.getTags();
    var exp = "(" + tags.join("|") + ")" ;
    exp = exp.replace( /(,[\d]+)/g, "" ) ;
    var regex = new RegExp( exp, "ig" );

    // build us string such that an expression that looks like this tag1 AND tag2 OR NOT tag3
    // turns into /tag1/.test(...) && /tag2/.test(...) || ! /tag2/.test(...)
    var cond = params.slice(1).join(" ");
    cond = cond.replace( regex, "/$1/.test( tiddlerTags )" );
    cond = cond.replace( /\sand\s/ig, " && " ).replace( /\sor\s/ig, " || " ).replace( /\snot\s/ig, " ! " ) ;

    // look through the tiddlers, make a string of the tags in the tiddler
    // and eval the 'cond' string we made against that string - if it's TRUE then the tiddler qualifies!
    var results = [];
    for( var t in store.tiddlers ) {
        var tiddler = store.tiddlers[t];
        var tiddlerTags = tiddler.tags.join() ;
        if ( eval( cond )  )  results.push( tiddler );
    }
    results.sort(function (a,b) {if(a["title"] == b["title"]) return(0); else return (a["title"] < b["title"]) ? -1 : +1; });
    return results;

kamvik

unread,
Aug 20, 2005, 1:41:14 AM8/20/05
to TiddlyWikiDev
This looks really good, but it gives me a syntax error:
missing ; before statement

If I cut the commented examples-part out , it gives me another syntax
error:
missing name after . operator

Kamilla

sobaboy

unread,
Aug 20, 2005, 2:06:18 AM8/20/05
to TiddlyWikiDev
if you did what I did, and cut and paste out of the web page you will
have some clean up work to do on the code.

Look for lines after comment tags // that may have come from the above
lines and gotten chopped in two.

Also, the line:
cond = cond.replace( /\sand\s/ig, " && " ).replace( /\sor\s/ig, " || "
).replace( /\snot\s/ig, " ! " ) ;

should be all one line.

Paul Petterson, I can post this on my TiddlyWIki if you want.

takeshi yano

Jim Barr

unread,
Aug 20, 2005, 2:11:37 AM8/20/05
to TiddlyWikiDev
Very cool! This solution is EXACTLY what I was looking for. It adds the
Boolean logic that I need, yet is simple enough

I think this could also be a nice replacement for the listAll Plugin
because it works with only one Tag also. That'll keep things more
consistent.

GREAT job!

-Jim Barr
http://TiddlyWikiTips.com

AlanH

unread,
Aug 21, 2005, 6:49:29 AM8/21/05
to TiddlyWikiDev
Very nice! And I like being able to have one plugin that does it all
instead of one for AND, etc.
Anymore, it seems that stuff will be added as plugins instead of into
the base code, but this could be a good candidate for a base code
addition. ~AlanH

Alvin

unread,
Aug 21, 2005, 11:18:08 AM8/21/05
to TiddlyWikiDev
sobaboy wrote:
> I have posted a TiddlyWiki with a listOnlyTags macro at
> http://www.sobaboy.com/listOnlyTags/

Wonderful job, Takeshi. Kudos.

But please help me with your code formatting. When I look at your
macro tiddler:

http://www.sobaboy.com/listOnlyTags/index.html#%5B%5BlistOnlyTags%20Macro%5D%5D

I see you're using monospaced text blocking:

{{{monospaced text here}}}

And your opening set of curly brackets comes AFTER your opening comment
string (/*), which makes it difficult to just copy the code without
going into tiddler edit-mode, which I'm presuming was your intention.

It sure makes the tiddler look good, but the monospaced text blocking
isn't necessary, is it?

Clint Checketts

unread,
Sep 8, 2005, 3:42:09 PM9/8/05
to Tiddly...@googlegroups.com
Paul,

How does the "list withTags" macro handle it if I ask for a tag that doesn't exist? Or if the tag has spaces in it? Here are two examples to help you understand where I'm coming from:

<<list withTags myTag and notes>>
This one gives me an error if the 'myTag' tag hasn't been applied to any tiddlers.

<<list withTags 'myTag with spaces' and notes>>
I get a very wierd error in this case. Maybe tags with are intended to be queried by

<<list withTags [[myTag with spaces]] and notes>>

Any suggestions?

-Clint


Paul Petterson

unread,
Sep 8, 2005, 5:26:22 PM9/8/05
to Tiddly...@googlegroups.com
There's a bug in handling tags that don't exist :(  There's a newer version on my ziddlywiki at http://phpetterson.objectis.net/ that will simply not return anything instead of throwing an error.  I'm not having problems with spaces in the criteria...  My ZW has an example on it's 'To Read' page - the macro is:

<<list withTags To Read and not Complete>> but <<list withTags [[To Read]] and not Complete>> also appears to work.

I'll get a fix for the tag not existing problem soon I hope... The problem is that I'm building a regular expression with all the criteria to be applied at once, that way I only have 1 loop, the loop through the tiddlers.  I build the criteria by substituting all the tags in the string with regular expression statements that match them, in other words ToDo -> /ToDo/.test( tiddlerTags ).  Well - if the tag doesn't exist then the substitution does nothing, leaving this weird old string, the eval fails and nothing is returned.  I need scrub the expression on the way in...

Paul

Clint Checketts

unread,
Sep 8, 2005, 5:33:46 PM9/8/05
to Tiddly...@googlegroups.com
Could you wrap the pluging at http://phpetterson.objectis.net/#%5B%5BPlugin%20list.withTags%5D%5D in {{{ }}} tags so I can select the code?  ZW doesn't allow viewing of source.

I'm not having problems with spaces in the criteria...  My ZW has an example on it's 'To Read' page - the macro is:

<<list withTags To Read and not Complete>> but <<list withTags [[To Read]] and not Complete>> also appears to work.

Okay. Maybe I'm creating another error... Would <<list withTags 'To Read' and not Complete>> cause an error?

-Clint
Reply all
Reply to author
Forward
0 new messages