Revision of Twine macro: <<once>> from Sun, 05/12/2013 - 05:24

This little macro shows a span of text only the first time you encounter it - for all subsequent times you visit a passage containing it, it will be absent. This can be done with variables, but this form lets you use a single pair of macro tags. This can be good for, let's say, IF-style verbose opening descriptions of scenes or rooms.

(function(){version.extensions.onceMacro={major:1,minor:0,revision:0};
macros.once={handler:function(g,e,f,b){function tagcontents(starttag,endtag,k){var a=b.source.slice(k),l=0,c="";
for(var i=0;i<a.length;i++){var w=endtag.length;if(a.substr(i,w)==endtag){if(l==0){b.nextMatch=k+i+w;
return c;}else{l--;c+=a.charAt(i);}}else{if(a.substr(i,starttag.length)==starttag){l++;
}c+=a.charAt(i);}}return"";}var o="<<"+e,eo="<<end"+e+">>",k=b.source.indexOf(">>",b.matchStart)+2,d=tagcontents(o,eo,k),shv=state.history[0].variables,os="once seen";
shv[os]=shv[os]||[];if(d){if(shv[os].indexOf(d)==-1){new Wikifier(insertElement(g,"span",null,"once"),d);
shv[os].push(d);}}else{throwError(g,"can't find matching end"+e);
return;}}};macros.endonce={handler:function(){}};}());

Usage examples:
* <<once>>I'm going now. Goodbye.<<endonce>>

Notes:
* If many <<once>> macros containing exactly identical text are used in different passages, seeing one of them will hide the others.

Version history

  1. 12-5-13 - Initial.

Feel free to report any bugs to @webbedspace.

pensive-mosquitoes