2   var slice = Array.prototype.slice;
 
   4   function update(array, args) {
 
   5     var arrayLength = array.length, length = args.length;
 
   6     while (length--) array[arrayLength + length] = args[length];
 
  10   function merge(array, args) {
 
  11     array = slice.call(array, 0);
 
  12     return update(array, args);
 
  15   Function.prototype.bind = function(context) {
 
  16     if (arguments.length < 2 && typeof arguments[0] === 'undefined') {
 
  20     var args = slice.call(arguments, 1);
 
  22       var a = merge(args, arguments);
 
  23       return __method.apply(context, a);
 
  29 function nblck_each(array, body, after) {
 
  30         $.each(array, function(i) {
 
  37 function nblck_map(array, func, after) {
 
  40         nblck_each(array, function(elem, index) {
 
  41                 acc.push(func(elem, index));
 
  47 function ScriptletCenter()
 
  51     this.scriptlets['insert_tag'] = function(context, params, done)
 
  53         var text = this.XMLEditorSelectedText(context);
 
  54         var start_tag = '<'+params.tag;
 
  55         var move_cursor = false;
 
  57         for (var attr in params.attrs) {
 
  58             start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
 
  62         var end_tag = '</'+params.tag+'>';
 
  68             for(var index=0; index < text.length; index++)
 
  70                 if (text[index].match(/\s/)) { // whitespace
 
  75                     if(output == token) output += start_tag;
 
  77                     output += text[index];
 
  81             if( output[output.length-1] == '\\' ) {
 
  82                 output = output.substr(0, output.length-1) + end_tag + '\\';
 
  89             if(params.nocontent) {
 
  90                 output = "<"+params.tag +" />";
 
  93                 output = start_tag + end_tag;
 
  98         this.XMLEditorReplaceSelectedText(context, output);
 
 102                 this.XMLEditorMoveCursorForward(context, params.tag.length+2);
 
 109     this.scriptlets['lineregexp'] = function(context, params, done) {
 
 112         var exprs = $.map(params.exprs, function(expr) {
 
 114             if(expr.length > 2) {
 
 117                 rx: new RegExp(expr[0], opts),
 
 123         var text = this.XMLEditorSelectedText(context);
 
 124         if(!text) return done();
 
 127         var lines = text.split('\n');
 
 129                 nblck_map(lines, function(line, index) {
 
 131             $(exprs).each(function() {
 
 133                 line = line.replace(expr.rx, expr.repl);
 
 136                         $progress.html(index);
 
 138             if(old_line != line) changed += 1;
 
 140         }, function(newlines) {
 
 142                                 self.XMLEditorReplaceSelectedText(context, newlines.join('\n') );
 
 149     this.scriptlets['fulltextregexp'] = function(context, params, done) {
 
 152         var exprs = $.map(params.exprs, function(expr) {
 
 154             if(expr.length > 2) {
 
 158                 rx: new RegExp(expr[0], opts),
 
 163         var text = this.XMLEditorSelectedText(context);
 
 164         if(!text) return done();
 
 165         var original = text;$
 
 167                 nblck_each(exprs, function(expr, index) {
 
 168                         $progress.html(600 + index);
 
 169             text = text.replace(expr.rx, expr.repl);
 
 171                         if( original != text) {
 
 172                    self.XMLEditorReplaceSelectedText(context, text);
 
 179     this.scriptlets['macro'] = function(context, params, done) {
 
 184                 if (i < params.length) {
 
 187                                 self.scriptlets[e[0]](context, e[1], next);
 
 197     this.scriptlets['lowercase'] = function(context, params, done)
 
 199         var text = this.XMLEditorSelectedText(context);
 
 204         var lcase = text.toLowerCase();
 
 205         var ucase = text.toUpperCase();
 
 207         if(lcase == text) repl = ucase; /* was lowercase */
 
 208         else if(ucase != text) repl = lcase; /* neither lower- or upper-case */
 
 209         else { /* upper case -> camel-case */
 
 210             var words = $(lcase.split(/\s/)).map(function() {
 
 211                 if(this.length > 0) {
 
 212                     return this[0].toUpperCase() + this.slice(1);
 
 217             repl = words.join(' ');
 
 220         if(repl != text) this.XMLEditorReplaceSelectedText(context, repl);
 
 226     this.scriptlets["insert_stanza"] = function(context, params, done) {
 
 227         var text = this.XMLEditorSelectedText(context);
 
 230             var verses = text.split('\n');
 
 231             text = ''; var buf = ''; var ebuf = '';
 
 234             for(var i=0;  i < verses.length; i++) {
 
 235                 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
 
 237                     text += (buf ? buf + '/\n' : '') + ebuf;
 
 238                     buf = (first ? '<strofa>\n' : '') + verses[i];
 
 242                     ebuf += '\n' + verses[i];
 
 245             text = text + buf + '\n</strofa>' + ebuf;
 
 246             this.XMLEditorReplaceSelectedText(context, text);
 
 250             this.XMLEditorMoveCursorForward(context, params.tag.length + 2);
 
 258 ScriptletCenter.prototype.callInteractive = function(opts) {
 
 259         $progress = $('<span>Executing script</span>');
 
 262         /* This won't work, 'cause the JS below might be synchronous :( */
 
 263         /* var timer = setTimeout(function() {
 
 264             $.blockUI({message: $progress});
 
 268         $.blockUI({message: $progress, showOverlay: false});
 
 270         self.scriptlets[opts.action](opts.context, opts.extra, function(){
 
 274             $.unblockUI(); // done
 
 278 ScriptletCenter.prototype.XMLEditorSelectedText = function(editor) {
 
 280     return editor.selection();
 
 283 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(editor, replacement)
 
 285         $progress.html("Replacing text");       
 
 286         editor.replaceSelection(replacement);
 
 289 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
 
 290     var pos = panel.cursorPosition();
 
 291     panel.selectLines(pos.line, pos.character + n);
 
 297     scriptletCenter = new ScriptletCenter();