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);
30 function ScriptletCenter()
34 this.scriptlets['insert_tag'] = function(context, params)
36 var text = this.XMLEditorSelectedText(context);
37 var start_tag = '<'+params.tag;
38 var move_cursor = false;
40 for (var attr in params.attrs) {
41 start_tag += ' '+attr+'="' + params.attrs[attr] + '"';
45 var end_tag = '</'+params.tag+'>';
51 for(var index=0; index < text.length; index++)
53 if (text[index].match(/\s/)) { // whitespace
58 if(output == token) output += start_tag;
60 output += text[index];
64 if( output[output.length-1] == '\\' ) {
65 output = output.substr(0, output.length-1) + end_tag + '\\';
72 if(params.nocontent) {
73 output = "<"+params.tag +" />";
76 output = start_tag + end_tag;
81 this.XMLEditorReplaceSelectedText(context, output);
85 this.XMLEditorMoveCursorForward(context, params.tag.length+2);
91 this.scriptlets['lineregexp'] = function(context, params) {
93 var exprs = $.map(params.exprs, function(expr) {
98 rx: new RegExp(expr[0], opts),
104 var text = this.XMLEditorSelectedText(context);
108 var lines = text.split('\n');
109 lines = $.map(lines, function(line) {
111 $(exprs).each(function() {
113 line = line.replace(expr.rx, expr.repl);
116 if(old_line != line) changed += 1;
121 this.XMLEditorReplaceSelectedText(context, lines.join('\n') );
125 this.scriptlets['fulltextregexp'] = function(context, params) {
126 var exprs = $.map(params.exprs, function(expr) {
128 if(expr.length > 2) {
132 rx: new RegExp(expr[0], opts),
137 var text = this.XMLEditorSelectedText(context);
140 $(exprs).each(function() {
141 text = text.replace(this.rx, this.repl);
144 if( original != text) {
145 this.XMLEditorReplaceSelectedText(context, text);
149 this.scriptlets['macro'] = function(context, params) {
152 $(params).each(function() {
153 $.log(this[0], this[1]);
154 self.scriptlets[this[0]](context, this[1]);
158 this.scriptlets['lowercase'] = function(context, params)
160 var text = this.XMLEditorSelectedText(context);
165 var lcase = text.toLowerCase();
166 var ucase = text.toUpperCase();
168 if(lcase == text) repl = ucase; /* was lowercase */
169 else if(ucase != text) repl = lcase; /* neither lower- or upper-case */
170 else { /* upper case -> camel-case */
171 var words = $(lcase.split(/\s/)).map(function() {
172 if(this.length > 0) {
173 return this[0].toUpperCase() + this.slice(1);
178 repl = words.join(' ');
181 if(repl != text) this.XMLEditorReplaceSelectedText(context, repl);
185 this.scriptlets["insert_stanza"] = function(context, params) {
186 var text = this.XMLEditorSelectedText(context);
189 var verses = text.split('\n');
190 text = ''; var buf = ''; var ebuf = '';
193 for(var i=0; i < verses.length; i++) {
194 var verse = verses[i].replace(/^\s+/, "").replace(/\s+$/, "");
196 text += (buf ? buf + '/\n' : '') + ebuf;
197 buf = (first ? '<strofa>\n' : '') + verses[i];
201 ebuf += '\n' + verses[i];
204 text = text + buf + '\n</strofa>' + ebuf;
205 this.XMLEditorReplaceSelectedText(context, text);
209 this.XMLEditorMoveCursorForward(context, params.tag.length + 2);
216 ScriptletCenter.prototype.XMLEditorSelectedText = function(panel) {
217 return panel.selection();
220 ScriptletCenter.prototype.XMLEditorReplaceSelectedText = function(panel, replacement)
222 panel.replaceSelection(replacement);
223 // Tell XML view that it's data has changed
224 // panel.contentView.editorDataChanged();
227 ScriptletCenter.prototype.XMLEditorMoveCursorForward = function(panel, n) {
228 var pos = panel.cursorPosition();
229 panel.selectLines(pos.line, pos.character + n);
235 scriptletCenter = new ScriptletCenter();