forked from dwachss/bililiteRange
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.vi.js
More file actions
502 lines (476 loc) · 16.3 KB
/
Copy pathjquery.vi.js
File metadata and controls
502 lines (476 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// moving toward an implementation of vi for jQuery
// (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/vi.html)
// depends: bililiteRange.ex.js and all its depends, jquery.keymap.js, jquery.savemonitor.js, jquery.status.js, jquery.livesearch.js
// documentation: to be created
// Version 0.9
// Copyright (c) 2014 Daniel Wachsstock
// MIT license:
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
(function($){
$.viClass = $.viClass || 'vi';
$.fn.vi = function(status, toolbar, exrc){
var self = this;
$(toolbar).click (function(evt){
$(evt.target).trigger('vi-click', [self]);
return false;
}).keydown({keys: /\w/}, function (evt){
// numbers/letters to activate the toolbar buttons (can tab/shift-tab over then enter, as normal)
self.each(function(){
bililiteRange(this).ex ('toolbar '+parseInt(evt.hotkeys, 36));
evt.preventDefault();
return false;
});
});
this.each(function(){
var rng = bililiteRange(this);
rng.undo(0); // initialize the undo handler (ex does this but we haven't called ex yet)
// track saving and writing
var state = rng.data();
var monitor = state.monitor = $(this).savemonitor();
state['save~state'] = monitor.state();
monitor.on($.savemonitor.states, function(evt){
state['save~state'] = evt.type;
});
if (exrc) $.get(exrc).then(function(commands){
// note that this is done asynchronously, so the user may be editing before this gets executed
rng.ex(commands);
});
});
return this.addClass($.viClass).data('vi.status', $(status)).data('vi.toolbar', $(toolbar));
}
// extensions to bililiteRange
bililiteRange.extend({
is: function (b){
return this.bounds()[0] == b[0] && this.bounds()[1] == b[1];
},
union: function (b){
this.bounds(Math.min (this.bounds()[0],b[0]), Math.max (this.bounds()[1],b[1]));
}
});
// create special events that let us check for vi-specific elements and modes
$.event.fixHooks['bililiteRangeData'] = { props:['detail'] }; // make sure it's copied over
$.event.fixHooks['vi-data'] = { props:['detail'] };
$.event.special['vi-data'] = {
delegateType: 'bililiteRangeData',
bindType: 'bililiteRangeData',
handle: function(evt){
if (!$(evt.target).hasClass($.viClass)) return;
var desiredoption = evt.data && evt.data.name;
if (desiredoption && evt.detail.name != desiredoption) return;
evt.rng = bililiteRange(evt.target);
return evt.handleObj.handler.apply(this, arguments);
}
}
$.event.special['vi-keydown'] = {
delegateType: 'keydown',
bindType: 'keydown',
handle: function(evt){
if (!$(evt.target).hasClass($.viClass)) return;
var mode = bililiteRange(evt.target).data().vimode;
var desiredmode = evt.data && evt.data.mode;
if (desiredmode && mode != desiredmode) return;
evt.rng = bililiteRange(evt.target);
return evt.handleObj.handler.apply(this, arguments);
}
};
$.event.special['vi-click'] = {
handle: function(evt, target){
var self = this, args = arguments;
target.each(function(){
evt.rng = bililiteRange(this);
var mode = evt.rng.data().vimode;
var desiredmode = evt.data && evt.data.mode;
if (desiredmode && mode != desiredmode) return;
return evt.handleObj.handler.apply(self, args);
});
}
};
function executeCommand (rng, command, defaultAddress){
// returns a function that will run command (if not defined, then will run whatever command is passed in when executed)
return function (text){
var data = rng.data();
rng.bounds('selection').ex(command || text, defaultAddress).select().scrollIntoView();
if (data.motionCommand && !rng.is(data.motionStart)){
rng.union(data.motionStart).ex(data.motionCommand);
data.motionCommand = undefined;
data.motionStart = undefined;
}
data.count = 0; // reset
data.register = undefined;
return rng.exMessage;
};
}
var body = $('body');
$.exmap = function(opts, defaults){
if ($.isArray(opts)){
var self = this;
opts.forEach(function(opt) {self.exmap(opt, defaults)});
return self;
}
opts = $.extend({}, opts, defaults);
if (!opts.name && typeof opts.command == 'string') opts.name = opts.command;
if (!opts.name && opts.monitor) opts.name = opts.monitor;
if (!opts.name && opts.keys) opts.name = opts.keys;
if (!opts.name) opts.name = Math.random().toString(); // need something!
if (!opts.command && opts.monitor) opts.command = opts.monitor+" toggle";
if (!opts.command && bililiteRange.ex.commands[opts.name]) opts.command = opts.name;
if (!opts.command) opts.command = 'sendkeys '+JSON.stringify(opts.name);
if ($.isFunction(opts.command)){
var commandName = bililiteRange.ex.toID(opts.name);
bililiteRange.ex.commands[commandName] = opts.command;
opts.command = commandName;
}
if (/([a-z]+)~motion/.test(opts.command)){
// replace this command with one that just pushes the name of the command, leaving the original command to be run after the motion is complete
var motionCommand = opts.command; // this is the real command
opts.command = RegExp.$1+'~motiondependent';
bililiteRange.ex.commands[opts.command] = function(){
this.data().motionStart = [this.bounds()[0], this.bounds()[1]];
this.data().motionCommand = motionCommand;
};
}
function run(event){
$($.data(event.rng.element(), 'vi.status')).status({
run: executeCommand(event.rng, opts.command, '%%')
});
event.preventDefault();
}
var button = $(); // make sure it exists
if (opts.buttonContainer){
button = $('button[name='+JSON.stringify(opts.name)+']', opts.buttonContainer);
if (button.length == 0) button = $('<button>').appendTo(opts.buttonContainer);
button.attr({
name: opts.name,
'class': opts.name.replace(/~/g,'-'),
title: opts.title
}).on('vi-click', {mode: opts.mode}, run);
}
if (opts.keys){
body.on('vi-keydown', {keys: opts.keys, mode: opts.mode}, function(event){
if (button){
// simulate a click
button.addClass('highlight')
setTimeout(function(){ button.removeClass('highlight') }, 400);
}
run(event);
});
}
if (opts.monitor) {
// TODO: remove the monitoring function; make a logical default
if (!opts.monitoringFunction) opts.monitoringFunction = function(option, value){
// assume we're looking at a binary option
this.removeClass('on off').addClass(value ? 'on' : 'off');
}
body.on('vi-data', {name: opts.monitor}, function(evt){
opts.monitoringFunction.call(button, evt.detail.option, evt.detail.value);
})
}
};
/*------------ Set up default options ------------ */
// unlike real vi, start in insert mode since that is more "natural" for a GUI
bililiteRange.data('vimode', {
value: 'INSERT',
enumerable: false
});
// RE's for searching
bililiteRange.ex.createOption('word', /^|$|\W+|\w+/);
bililiteRange.ex.createOption('bigword', /^|$|\s+|\S+/);
// for saving
bililiteRange.ex.createOption('directory', ''); // used as the $.post url for saving
bililiteRange.ex.createOption('file', 'Untitled');
// writing files. Assumes POSTing with {buffer: text-to-be-saved, file: filename}
// use mockjax to do something else.
bililiteRange.data('monitor', {
enumerable: false
});
bililiteRange.data('save~state', {
value: 'clean',
enumerable: false,
monitored: true
});
// a series of digits means a number of times to repeat a command
// 'count' is that number; some commands use that directly but text-entry commands can only repeat after the text is entered.
// so the count is saved in 'repeatcount' (since 'count' is reset after every command), and the present text is saved.
// When we return to visual mode, we see how that text has changed and insert the new text (at whereever the insertion point is!)
// 'repeatcount'-1 times ( since it's already been inserted once)
bililiteRange.data('count', { // a number preceding vi commands
value: 0,
enumerable: false
});
bililiteRange.data('oldtext', { // for repeated insertions, this is the original text to compare to
enumerable: false
});
bililiteRange.data('repeatcount', { // this is the number of times to repeat insertions
value: 0,
enumerable: false
});
body.on('vi-keydown', {keys: /\d/, mode: 'VISUAL'}, function (evt){
var state = bililiteRange(evt.target).data();
if (state.count == 0 && evt.hotkeys == '0') return; // 0 has a different meaning if not part of a number
state.count = state.count * 10 + parseInt(evt.hotkeys);
evt.preventDefault();
evt.stopImmediatePropagation();
});
// a double quote followed by a letter means store the result of the next command (if it removes text) into that register
bililiteRange.data('register', {enumerable: false});
body.on('vi-keydown', {keys: /" [A-Za-z]/, mode: 'VISUAL'}, function (evt){
evt.range.data().register = evt.hotkeys.charAt(2);
evt.preventDefault();
evt.stopImmediatePropagation();
});
// track tab size
bililiteRange.data ('tabSize', {monitored: true});
body.on('vi-data', {name: 'tabSize'}, function (evt){
var style = evt.rng.element().style;
style.tabSize =
style.OTabSize =
style.MozTabSize = evt.detail.value; // for browsers that support this.
});
/*------------ Set up generic commands ------------ */
$.exmap([
{
name: 'button',
command: function (parameter, variant){
var exmapparam = { buttonContainer: $.data(this.element(), 'vi.toolbar') };
if (variant){
// use the complex form
bililiteRange.ex.splitCommands(parameter, ' ').forEach(function(item){
var match = /(\w+)=(.+)/.exec(item);
if (!match) throw new Error('Bad syntax in button: '+item);
exmapparam[match[1]] = bililiteRange.ex.string(match[2]);
});
}else{
exmapparam.name = parameter;
}
$.exmap(exmapparam);
}
},{
name: 'console',
command: function (parameter, variant){
console.log(executeCommand(this)(parameter));
}
},{
name: 'map',
command: function (parameter, variant){
// The last word (either in a string or not containing spaces) is the replacement; the rest of
// the string at the beginning are the mapped key(s)
var match = /^(.+?)([^"\s]+|"(?:[^"]|\\")+")$/.exec(parameter);
if (!match) throw {message: 'Bad syntax in map: '+parameter};
var keys = match[1].trim();
var command = bililiteRange.ex.string(match[2]);
// TODO: remove any old key handlers
$.exmap({
keys: keys,
command: command,
mode: variant ? 'INSERT' : 'VISUAL'
});
}
},{
name: 'read',
command: function (parameter, variant){
var rng = this;
$.get(parameter, function (text) {
rng.text(text, 'end').select().element().focus();
});
}
},{
name: 'repeat',
command: function (parameter, variant){
var state = this.data();
for (var i = state.count || 1; i > 0; --i){
var result = executeCommand(this, parameter, '%%')();
}
return result;
}
},{
name: 'select',
command: function (parameter, variant){
this.bounds(parameter).select();
}
},{
name: 'sendkeys',
command: function (parameter, variant){
this.sendkeys(parameter).element().focus();
}
},{
name: 'toolbar',
command: function (parameter, variant){
var which = parseInt(parameter);
var target = $(this.element());
var toolbar = target.data('vi.toolbar');
if ($.isNumeric (which)){
var button = $('button', toolbar).eq(which);
button.trigger ('vi-click', [target]);
// TODO: refactor this with the keydown handler
button.addClass('highlight')
setTimeout(function(){ button.removeClass('highlight') }, 400);
}else{
$('button', toolbar).eq(0).focus();
}
}
},{
name: 'vi',
keys: '{esc}',
command: function (parameter, variant){
var state = this.data();
parameter = parameter || 'VISUAL';
// an insertion with a count means repeat the insertion when we return to visual mode
if (parameter == 'INSERT'){
state.repeatcount = Math.max(state.count - 1, 0); // -1 since we've done one insertion already
state.oldtext = this.all();
}else if (parameter == 'VISUAL' && state.repeatcount){
var text = bililiteRange.diff(state.oldtext, this.all()).data;
this.bounds('endbounds').text(text.repeat(state.repeatcount), 'end');
state.repeatcount = 0;
}
state.vimode = parameter;
}
},{
name: 'write',
command: function(parameter, variant){
var rng = this, state = this.data(), el = this.element();
if (parameter) state.file = parameter;
state.monitor.clean($.data(el, 'vi.status').status({
run: function() { return $.post(state.directory, {
buffer: rng.all(),
file: state.file
}).then(
function() { return state.file+' Saved' },
function() { return new Error(state.file+' Not saved') }
)},
returnPromise: true
}));
}
},{
keys: '^z', // not really part of vi, but too ingrained in my fingers
command: 'undo'
},{
keys: '^y',
command: 'redo'
}
]);
bililiteRange.ex.commands.w = 'write'; // shortcut
/*------------ Set up VISUAL mode commands ------------ */
$.exmap([
{
keys: ':',
command: function (){
var el = this.element();
$.data(el, 'vi.status').status({
prompt: ':',
run: executeCommand(this),
returnPromise: true
}).then( // make sure we return focus to the text! It would be nice to have a finally method
function(e) {el.focus()},
function(e) {el.focus()}
);
}
},{
keys: 'a',
command : "select endbounds | vi INSERT"
},{
keys: 'b', // end of previous word
command: function(){
var state = this.data();
for (var i = state.count || 1; i > 0; --i){
this.findBack(state.word, true);
}
this.bounds('endbounds');
}
},{
keys: 'B', // end of previous bigword
command: function(){
var state = this.data();
for (var i = state.count || 1; i > 0; --i){
this.findBack(state.bigword, true);
}
this.bounds('endbounds');
}
},{
keys: 'e', // end of next word
command: function(){
var state = this.data();
for (var i = state.count || 1; i > 0; --i){
this.find(state.word, true);
}
this.bounds('endbounds');
}
},{
keys: 'E', // end of next bigword
command: function(){
var state = this.data();
for (var i = state.count || 1; i > 0; --i){
this.find(state.bigword, true);
}
this.bounds('endbounds');
}
},{
keys: 'h',
command: "repeat sendkeys {leftarrow}"
},{
keys: 'i',
command: "select startbounds | vi INSERT"
},{
keys: 'l',
command: "repeat sendkeys {rightarrow}"
},{
keys: 'o',
command: ".a | vi INSERT"
},{
keys: '0',
command: "select BOL" // if part of a number, should have been handled above
},{
keys: '$',
command: 'select EOL'
},{
keys: '^',
command: function(){
this.bounds('BOL').find(/\S/).bounds('startbounds');
}
},{
keys: '>',
command: 'repeat >'
},{
keys: '<',
command: 'repeat <'
},{
keys: '/',
name: 'livesearch',
command: function(parameter, variant){
var rng = this, el = this.element(), $status = $.data(el, 'vi.status');
$status.status({
prompt: variant ? '?' : '/',
run: function(text){
rng.bounds('selection').find(new RegExp(text), undefined, variant).select().scrollIntoView();
if (!rng.match) throw new Error(text+' not found');
},
returnPromise: true
}).then( // make sure we return focus to the text! It would be nice to have a finally method
function(e) {el.focus()},
function(e) {el.focus()}
);
$status.off('.search').on('input.search focus.search focusout.search', 'input', $(el).livesearch(variant));
}
},{
keys: '?',
command: 'livesearch!'
}
], {mode: 'VISUAL'});
})(jQuery);