forked from jakub-m/vim-in-textarea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsvim.js
1217 lines (1043 loc) · 32.9 KB
/
jsvim.js
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* (c) [email protected] 2012, 2013 */
/* commented parts starting with //ext// should make crossrider extension out
* of this script */
//ext//appAPI.ready(function($) {
/*===================================================================*/
/* Command tree */
function Node(data) {
this.set_choice = function( option, node, data ) {
this.nodes[ option ] = {node: node, data: data}
return this
}
this.get_choice = function( option ) {
return this.nodes[option]
}
this.get_choice_node = function( option ) {
var x = this.get_choice( option )
return (x === undefined) ? undefined : x.node
}
this.is_leaf = function() {
return is_empty_object( this.nodes )
}
this.set_choices = function( choices ) {
for (var key in choices)
if (choices.hasOwnProperty(key)) {
var val = choices[key]
if ( val instanceof Array ) {
this.set_choice( key, val[0], val[1] )
} else {
this.set_choice( key, val )
}
}
return this
}
this.data = data
this.nodes = {}
}
var merge_dict = function( dict_a, dict_b ) {
var r = {}
var clone = function( src ) {
for (var p in src)
if (src.hasOwnProperty(p))
r[p] = src[p]
}
clone(dict_a)
if (dict_b !== undefined) clone(dict_b)
return r
}
var is_empty_object = function(e) {
for (var prop in e) if (e.hasOwnProperty(prop)) return false;
return true;
}
/*===================================================================*/
/* VIM class and other */
var COMMAND = 'COMMAND'
var INSERT = 'INSERT'
var VISUAL = 'VISUAL'
var NREPEATS_LIMIT = 100
var UNDO_DEPTH = 100
var _proxy = function( func, context ) {
return function(){
return func.apply( context, arguments )
}
}
var __special_keys = {
8:'Backspace',
9:'Tab',
13:'Enter',
27:'Escape',
33:'PageUp',
34:'PageDown',
35:'End',
36:'Home',
37:'Left',
38:'Up',
39:'Right',
40:'Down',
45:'Insert',
46:'Delete',
}
function VIM(ctrees) {
this.attach_to = function(m_selector) {
this.m_selector = m_selector
m_selector.onkeydown = _proxy( this.on_keydown, this)
m_selector.onkeypress = _proxy( this.on_keypress, this)
this.reset()
//ext// window.__refocus = true
}
this.on_keydown = function(event){
var p
var m = __special_keys[ event.keyCode ]
if (undefined === m ) {
p = true
} else {
p = this.on_key( '<'+m+'>', event )
}
return p
}
this.on_keypress = function(event){
var m = String.fromCharCode( event.keyCode )
var p = this.on_key( m, event )
return p
}
this.on_key = function(c, event) {
this.log('"' + c + '"')
var pass_keys
if ('<Escape>' === c) {
this.reset()
pass_keys = false
}
else if ( this.is_mode(INSERT) ) {
if (c === '<Enter>') {
this.enter_auto_indent()
pass_keys = false
} else {
pass_keys = true
}
}
else if (c !== null) {
this.accept_key( c )
pass_keys = false
}
if ( false === pass_keys ) {
event.preventDefault()
event.stopPropagation()
}
return pass_keys
}
this.enter_auto_indent = function() {
var text = this.get_text()
var pos = this.get_pos()
var xs = select_line(text, pos)
var n_spaces = count_space_from( text, xs[0] )
var local_pos = pos - xs[0]
n_spaces = (local_pos < n_spaces) ? local_pos : n_spaces
var t = "\n" + repeat_str(' ', n_spaces)
text = insert_at( text, t, pos )
this.set_text( text )
this.set_pos( pos + t.length )
}
this.set_mode = function(mode) {
this.log("set_mode " + mode)
if (this.m_mode === COMMAND && mode === VISUAL) {
// do not change when comming from PENDING - would affect 'viw'
this.m_selection_from = this.get_pos()
}
this.m_mode = mode
if (this.on_set_mode !== undefined) this.on_set_mode(this)
}
this.insert_to_clipboard = function(text) {
this.log('insert_to_clipboard len: ' + text.length)
if (this.m_allow_clipboard_reset) {
this.m_allow_clipboard_reset = false
this.m_buffer = ''
}
this.m_buffer += text
}
/* This is used to distinguish between "2daw" and "dawdaw". In first case,
* both lines should be put into the buffer, and in the second case - only
* the line deleted at second "daw", overwriting the line from first "daw" */
this.allow_clipboard_reset = function(text) {
this.m_allow_clipboard_reset = true
}
this.get_clipboard = function() {
return this.m_buffer
}
this.save_undo_state = function() {
var curr_text = this.get_text()
var last_text = last( this.m_undo_stack )
last_text = (last_text === undefined) ? '' : last_text.text
if ( curr_text !== last_text ) {
var t = {text: curr_text, pos: this.get_pos()}
this.m_undo_stack.push( t )
if (this.m_undo_stack.length > UNDO_DEPTH ) {
this.m_undo_stack.shift()
}
this.log('undo depth ' + this.m_undo_stack.length )
}
}
this.is_mode = function(mode) {
return this.m_mode === mode
}
this.reset = function() {
this.reset_mode()
this.reset_commands()
}
this.reset_mode = function() {
this.set_mode( COMMAND )
}
this.reset_commands = function() {
this.m_cnode = this.m_ctrees[ this.m_mode ]
this.m_cdata = {}
this.m_current_sequence = ''
}
this.get_pos = function() {
return getCaret( document.activeElement )
}
this.get_text = function() {
return this.m_selector.value
}
this.set_text = function(tx) {
this.m_selector.value = tx
}
this.set_pos = function(k) {
_select_pos( this.m_selector, k )
if (this.on_set_pos !== undefined) this.on_set_pos(this)
}
this.select_current_line = function() {
return select_line( this.get_text(), this.get_pos() )
}
this.accept_key = function(c) {
this.m_current_sequence += c
var x = this.m_cnode.get_choice(c)
if (x === undefined) {
this.log('(unknown sequence "'+this.m_current_sequence+'"')
this.reset_commands()
}
else {
this.m_cdata = merge_dict( this.m_cdata, x.data )
this.m_cdata = merge_dict( this.m_cdata, x.node.data )
this.m_cnode = x.node
if (this.m_cnode.is_leaf()) {
var nrepeats
if (this.m_cdata.digit === undefined) {
nrepeats = ( (this.m_digit_buffer === '') ? 1
: parseInt(this.m_digit_buffer))
nrepeats = (nrepeats > NREPEATS_LIMIT) ? NREPEATS_LIMIT :
( (nrepeats < 1) ? 1 : nrepeats)
this.m_digit_buffer = ''
}
else {
nrepeats = 1
}
if (nrepeats !== 1) { this.log('n. repeats: ' + nrepeats) }
this.allow_clipboard_reset()
if ( ! this.m_cdata.dont_save_undo_state ) {
this.save_undo_state()
}
for (var i = 0; i < nrepeats; i++ ) {
this.execute_leaf()
}
this.reset_commands()
}
}
}
this.execute_leaf = function() {
var fn = this.m_cdata.action
if ( fn === undefined ) {
this.log('ERROR: could not execute leaf!')
} else {
//fn.apply( this, [this, this.m_cdata] )
fn( this, this.m_cdata )
}
}
this.log = function(message){
if (this.on_log !== undefined) {
this.on_log(message)
}
}
this.m_ctrees = build_sequence_trees() // this can be shared among all VIMs
this.m_selector = null
this.m_mode = undefined
this.m_selection_from = undefined
this.m_cnode = undefined
this.m_current_sequence = ''
this.m_digit_buffer = ''
this.m_cdata = {} /* data merged during ctree traversal */
this.m_undo_stack = []
this.m_buffer = ''
// callbacks, for extenal functions; e.g., for nice formatting
this.on_set_mode = undefined
this.on_set_pos = undefined
this.on_log = undefined
} /* VIM END */
//==============================================================================
// http://stackoverflow.com/questions/263743/how-to-get-caret-position-in-textarea
function getCaret(el) {
if (el.selectionStart) {
return el.selectionStart;
} else if (document.selection) {
el.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = el.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
return 0;
}
/*
based on
http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
*/
var _select_pos = function(el, pos) {
if (el.setSelectionRange) {
el.focus();
el.setSelectionRange(pos, pos);
} else if (el.createTextRange) {
var range = el.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
};
//=============================================================================
var node = function(x) {
return new Node(x)
}
var build_sequence_trees = function() {
var tree_command = build_tree_command_mode()
var tree_visual = build_tree_visual_mode()
return {COMMAND: tree_command,
VISUAL: tree_visual}
}
var build_tree_command_mode = function() {
var d_inside = function(fn) {
return function(text, pos) {
var xs = fn(text, pos)
return (xs[1] === 0) ? xs : [xs[0] + 1, xs[1] - 2]
}
}
var d_in_line = function(fn){
return function(text, pos) {
var iline = select_line(text, pos)
var line_pos = iline[0], line_len = iline[1]
var line = text.substr( line_pos, line_len )
var xs = fn( line, pos - line_pos )
return [ xs[0] + line_pos, xs[1] ]
}
}
var choices_ai = {
'a': node()
.set_choice('p', node({ select_func: d_with_whitespaces_after(select_paragraph) }))
.set_choice('w', node({ select_func: find_word_with_spaces_after }))
.set_choice('W', node({ select_func: d_with_spaces_after(find_word_plus) }))
.set_choice("'", node({ select_func: d_in_line( select_quotes.partial("'")) }))
.set_choice('"', node({ select_func: d_in_line( select_quotes.partial('"')) }))
.set_choice('(', node({ select_func: select_bounds.partial('()') }))
.set_choice(')', node({ select_func: select_bounds.partial('()') }))
.set_choice('{', node({ select_func: select_bounds.partial('{}') }))
.set_choice('}', node({ select_func: select_bounds.partial('{}') }))
.set_choice('<', node({ select_func: select_bounds.partial('<>') }))
.set_choice('>', node({ select_func: select_bounds.partial('<>') }))
.set_choice('[', node({ select_func: select_bounds.partial('[]') }))
.set_choice(']', node({ select_func: select_bounds.partial('[]') })) ,
'i': node()
.set_choice('p', node({ select_func: select_paragraph }))
.set_choice('w', node({ select_func: find_word }))
.set_choice('W', node({ select_func: find_word_plus }))
.set_choice("'", node({ select_func: d_inside( d_in_line( select_quotes.partial("'"))) }))
.set_choice('"', node({ select_func: d_inside( d_in_line( select_quotes.partial('"'))) }))
.set_choice('(', node({ select_func: d_inside( select_bounds.partial('()')) }))
.set_choice(')', node({ select_func: d_inside( select_bounds.partial('()')) }))
.set_choice('{', node({ select_func: d_inside( select_bounds.partial('{}')) }))
.set_choice('}', node({ select_func: d_inside( select_bounds.partial('{}')) }))
.set_choice('<', node({ select_func: d_inside( select_bounds.partial('<>')) }))
.set_choice('>', node({ select_func: d_inside( select_bounds.partial('<>')) }))
.set_choice('[', node({ select_func: d_inside( select_bounds.partial('[]')) }))
.set_choice(']', node({ select_func: d_inside( select_bounds.partial('[]')) })) ,
}
var _select_line = node( {select_func: select_line } )
var _c = node()
.set_choices( make_choices_for_navigation() )
.set_choices( choices_ai )
.set_choice('c', _select_line )
.set_choice('w', node( {select_func: till_right_word_bound} ))
.set_choice('W', node( {select_func: till_right_word_bound_plus} ))
var _d = make_node_dy('d')
.set_choices( choices_ai )
var _y = make_node_dy('y')
.set_choices( choices_ai )
var _indent_inc = make_node_indent('>')
.set_choices( choices_ai )
var _indent_dec = make_node_indent('<')
.set_choices( choices_ai )
var _r = node()
.set_choices( make_choices_for_navigation({action: act_move}))
.set_choices( make_choices_for_digits() )
.set_choice('a', node({action: act_append}))
.set_choice('A', node({action: act_append_to_end}))
.set_choice('c', _c, {action: act_delete_range, mode: INSERT})
.set_choice('C', node({action: act_delete_range, mode: INSERT,
move_func: move_to_line_end}))
.set_choice('d', _d, {action: act_delete_range, mode: COMMAND})
.set_choice('D', node({action: act_delete_range, mode: COMMAND,
move_func: move_to_line_end}))
.set_choice('i', node({action: act_insert}))
.set_choice('J', node({action: act_merge_lines}))
.set_choice('o', node({action: act_insert_line_after}))
.set_choice('O', node({action: act_insert_line_before}))
.set_choice('p', node({action: act_paste_after}))
.set_choice('P', node({action: act_paste_before}))
.set_choice('s', node({action: act_delete_char, mode: INSERT}))
.set_choice('u', node({action: act_undo, dont_save_undo_state: true}))
.set_choice('v', node({action: act_visual_mode}))
.set_choice('x', node({action: act_delete_char}))
.set_choice('y', _y, {action: act_yank_range})
.set_choice('>', _indent_inc, {action: act_indent_increase} )
.set_choice('<', _indent_dec, {action: act_indent_decrease} )
return _r
}
var make_node_dy = function( line_char ) {
var e = node()
.set_choices( make_choices_for_navigation() )
.set_choice( line_char, node( {select_func: select_line_nl} ) )
.set_choice('w', node( {select_func: till_next_word} ))
.set_choice('W', node( {select_func: till_next_word_plus} ))
return e
}
var make_node_indent = function( line_char ) {
var e = node()
.set_choices( make_choices_for_navigation() )
.set_choice( line_char, node( {select_func: select_line} ) )
return e
}
var build_tree_visual_mode = function() {
var _r = node()
.set_choices( make_choices_for_navigation({action: act_move}) )
.set_choices( make_choices_for_digits() )
.set_choice('d', node({action: act_delete_current_selection, mode: COMMAND}) )
.set_choice('c', node({action: act_delete_current_selection, mode: INSERT}) )
return _r
}
var make_choices_for_navigation = function(params) {
params = (params===undefined) ? {} : params
var N = function(d) { return node( merge_dict( params, d) ) }
var _g = node()
.set_choice('g', N({move_func: move_to_very_beginning}))
var ch = {
'0': N({move_func: move_to_line_start}),
'$': N({move_func: move_to_line_end}),
'<Left>': N({move_func: move_left}),
'<Down>': N({move_func: move_down}),
'<Up>': N({move_func: move_up}),
'<Right>': N({move_func: move_right}),
'h': N({move_func: move_left}),
'j': N({move_func: move_down}),
'k': N({move_func: move_up}),
'l': N({move_func: move_right}),
'<Enter>': N({move_func: move_to_word_in_next_line}),
'<Backspace>': N({move_func: move_left}),
'w': N({move_func: move_to_next_word}),
'W': N({move_func: move_to_next_word_plus}),
'e': N({move_func: move_to_end_of_word}),
'E': N({move_func: move_to_end_of_word_plus}),
'b': N({move_func: move_to_prev_word}),
'B': N({move_func: move_to_prev_word_plus}),
'g': _g,
'G': N({move_func: move_to_very_end}),
}
return ch
}
var make_choices_for_digits = function(){
var xs = {}
for (var i = 1; i < 10; i++) {
var c = i.toString()
xs[c] = node({action: act_accept_digit, digit: c})
}
xs['0'] = node({action: act_zero, digit: '0'})
return xs
}
//=============================================================================
/* find_... , till_... and other selection functions return [pos,len] where pos
* is position of the element and len is length of element. len can be 0 */
var find_word = function( text, pos ) {
var m = '(\\s(?=\\S))|([^W](?=[W]))|(\\S(?=\\s))|([W](?=[^W]))'.replace(/W/g,'\\u0000-\\u002f\\u003a-\\u0040\\u005b-\\u0060\\u007b-\\u00bf')
var p = new RegExp( m,'g' )
return __find_regex_break( p, text, pos )
}
// non ascii unicode
///[\u0000-\u002f\u003a-\u0040\u005b-\u0060\u007b-\u00bf]/
var find_word_plus = function(text, pos) {
var p = /(\s(?=\S))|(\S(?=\s))/g
return __find_regex_break(p, text, pos)
}
var find_word_with_spaces_after = function(text, pos) {
return d_with_spaces_after(find_word)(text, pos)
}
var d_with_spaces_after = function(fn) {
return d_with_characters_after(fn, ' ')
}
var d_with_whitespaces_after = function(fn) {
return d_with_characters_after(fn, '\n\s')
}
var d_with_characters_after = function(fn, characters){
return function(text, pos) {
var g = fn(text, pos)
var n = count_space_from(text, g[0]+g[1], characters)
return [ g[0], g[1] + n ]
}
}
//var find_word_with_spaces_before = function(text, pos) {
// var xs = find_word(text, pos)
// var n = count_space_to(text, xs[0])
// return [ xs[0]-n, xs[1]+n ]
//}
var find_word_plus_with_trailing_spaces = function(text, pos) {
var g = find_word_plus(text, pos)
var n = count_space_from(text, g[0]+g[1])
return [ g[0], g[1] + n ]
}
var till_right_word_bound = function( text, pos ) {
var xs = find_word( text, pos )
return [ pos, xs[0] + xs[1] - pos ]
}
var till_right_word_bound_plus = function( text, pos ) {
var xs = find_word_plus( text, pos )
return [ pos, xs[0] + xs[1] - pos ]
}
//var till_left_word_bound = function( text, pos ) {
// var xs = find_word( text, pos )
// var len = pos - xs[0]
// return [ xs[0], len ]
//}
var till_next_word = function(text, pos) {
var g = find_word_with_spaces_after(text,pos)
return [ pos, g[0] + g[1] - pos]
}
var till_next_word_plus = function(text, pos) {
var g = find_word_plus_with_trailing_spaces(text,pos)
return [ pos, g[0] + g[1] - pos ]
}
///* decorator - find a pattern with fn_find and latter find the trailing spaces */
//var with_trailing_spaces = function( fn_find ) { // todo: refactor, remove?
// return function( text, pos ) {
// var xs = fn_find(text, pos)
// var t = regex_end_pos( /\s*/, text, {from: xs[1]} )
// xs[1] = t
// return xs
// }
//}
var regex_end_pos = function( regex, text, opts ) {
opts = (opts === undefined) ? {from:0} : opts
var tx = text.substr( opts.from )
var m = regex.exec(tx)
var g = 0 // group === undefined ? 0 : group
return m === null ? null : m.index + m[g].length + opts.from
}
var cut_slice = function( text, i0, i1 ) {
var from, to
if (i0 < i1 ) { from = i0; to = i1 }
else { from = i1; to = i0 }
return { text: text.substr(0, from) + text.substr(to),
cut: text.substr(from, (to-from)),
from: from, to: to }
}
/* return [start of the line, length] */
var select_line = function( text, pos ) {
var ileft = trailing_nl( text, pos )
var iright = leading_nl( text, pos )
return [ileft, iright - ileft]
}
var select_line_nl = function( text, pos ) {
var ileft = trailing_nl( text, pos )
var iright = leading_nl( text, pos )
return [ileft, iright - ileft + 1 ]
}
var select_next_line = function( text, pos ) {
var xs = select_line( text, pos )
var k = xs[0]+xs[1]+1
return ( k < text.length ) ? select_line( text, k ) : undefined
}
var select_prev_line = function( text, pos ) {
var xs = select_line( text, pos )
var k = xs[0] - 1
return (k >= 0) ? select_line(text, k) : undefined
}
var select_bounds = function(bounds, text, pos) {
var m_left = bounds.charAt(0)
var m_right = bounds.charAt(1)
var i_left, i_right
if ( text.charAt(pos) == m_left ) {
i_left = pos
} else {
var k = 1
for( var i = pos - 1; i >= 0; i-- ) {
var c = text.charAt(i)
k += ((c === m_left) ? -1 : 0) + ((c === m_right) ? 1 : 0)
if (k === 0 ) {
i_left = i
break
}
}
}
if ( text.charAt(pos) == m_right ) {
i_right = pos
} else {
var k = 1
for( var i = pos + 1; i < text.length; i++ ) {
var c = text.charAt(i)
k += ((c === m_left) ? 1 : 0) + ((c === m_right) ? -1 : 0)
if (k === 0) {
i_right = i
break
}
}
}
return ((i_right === undefined) || (i_left === undefined)) ?
[pos,0] :
[i_left, i_right - i_left + 1]
}
var select_quotes = function(quote, text, pos) {
var xs = __select_quotes(quote, text, pos)
var i_left = xs[0], i_right = xs[1]
return ( (i_left === undefined) || (i_right === undefined) ) ?
[pos,0] :
[i_left, i_right - i_left + 1]
}
var __select_quotes = function(quote, text, pos) {
var i_left, i_right
for(var i = pos; i >= 0; i--) {
//if (text.charAt(i) === quote) {
if (text.substr(i, quote.length) === quote) {
i_left = i
break
}
}
for(var i = pos + 1; i < text.length; i++) {
//if (text.charAt(i) === quote) {
if (text.substr(i, quote.length) === quote) {
i_right = i
break
}
}
return [i_left, i_right]
}
var select_paragraph = function(text, pos) {
var regex = /\n\s*\n/g
return __find_regex(regex, text, pos)
}
var __find_regex = function(regex, text, pos ) {
var m, ileft = 0, iright
while ( (m=regex.exec(text)) !== null ) {
var i = m.index + m[0].length
if ((ileft === undefined) || (i <= pos)) {
ileft = i
}
i = m.index
if ((iright === undefined) && (i > pos)) {
iright = i
}
}
iright = (iright === undefined) ? (text.length) : iright
return [ileft, iright - ileft]
}
/* todo: merge __find_regex and __find_regex_break */
var __find_regex_break = function( regex, text, pos ) {
var m, ileft = 0, iright
while ( (m=regex.exec(text)) !== null ) {
var i = m.index + 1
if ((ileft === undefined) || (i <= pos)) {
ileft = i
}
if ((iright === undefined) && (i > pos)) {
iright = i
}
}
iright = (iright === undefined) ? (text.length) : iright
return [ileft, iright - ileft]
}
var trailing_nl = function ( text, pos ) {
var i = trailing_char( text, pos, "\n" )
return (i===undefined) ? 0 : i
}
var leading_nl = function ( text, pos ) {
var i = leading_char( text, pos, "\n" )
return (i===undefined) ? text.length : i
}
var trailing_char = function( text, pos, character ) {
var t
for ( var i = (pos-1); i >= 0; i-- ) {
if ( text.charAt(i) == character ) {
t = i + 1
break
}
}
return t
}
var leading_char = function(text, pos, character) {
var t
for (var i = pos; i < text.length; i++ ) {
if (text.charAt(i) == character) {
t = i
break
}
}
return t
}
/* Decorator, skips spaces at position and starts searching from after the
* spaces */
var skip_spaces = function(search_func) {
return function(text, pos) {
var k = count_space_from( text, pos )
var xs = search_func( text, k + pos )
return xs
}
}
var count_space_from = function(text, pos, characters) {
characters = (characters===undefined) ? (' ') : characters
var r = new RegExp('^['+characters+']+')
var m = r.exec(text.substr(pos))
return (m===null) ? 0 : m[0].length
}
var count_space_to = function(text,pos) {
var k
if (pos > text.length) {
k = 0
} else {
var m = /[ ]+$/.exec(text.substr(0,pos))
k = (m===null) ? 0 : m[0].length
}
return k
}
var insert_at = function( base, chunk, pos ) {
return base.slice(0,pos).concat( chunk ).concat( base.slice(pos) )
}
var repeat_str = function( str, n_repeats ) {
return (new Array(n_repeats+1)).join( str )
}
var last = function(xs, def) {
return (xs.length > 0) ? ( xs[xs.length-1] ) : def
}
var insert_line_after_auto_indent = function(text, pos) {
var xs = select_line( text, pos )
var k = count_space_from( text, xs[0] )
var t = "\n" + repeat_str(" ", k)
var q = {}
q.text = insert_at( text, t, xs[0] + xs[1] )
q.pos = xs[0] + xs[1] + k + 1
return q
}
Function.prototype.partial = function() {
var base_args = to_array(arguments),
base_fn = this
return function(){
var next_args = to_array(arguments)
return base_fn.apply(this, base_args.concat(next_args))
}
}
var to_array = function( args ) {
return Array.prototype.slice.call(args, 0)
}
/* ACTIONS -- various commands */
var act_insert_line_after = function(vim, cdata) {
var text = vim.get_text()
var pos = vim.get_pos()
var q = insert_line_after_auto_indent( text, pos )
vim.set_text( q.text )
vim.set_pos( q.pos )
vim.set_mode( INSERT )
}
var act_insert_line_before = function(vim, cdata) {
var text = vim.get_text()
var pos = vim.get_pos()
var xs = select_line( text, pos )
var k = count_space_from( text, xs[0] )
var t = repeat_str(" ", k) + "\n"
text = insert_at( text, t, xs[0] )
vim.set_text( text )
vim.set_pos( xs[0] + k )
vim.set_mode( INSERT )
}
var act_move = function(vim, cdata) {
var p = cdata.move_func.apply( vim, [vim.get_text(), vim.get_pos()] )
vim.set_pos( p )
}
var act_accept_digit = function(vim, cdata) {
vim.m_digit_buffer += cdata.digit
}
/* zero is handled separately, because it can be both "go to start of the
* line" and "insert 0 to digit buffer" */
var act_zero = function(vim, cdata) {
if (vim.m_digit_buffer === '') {
var s = vim.select_current_line()
vim.set_pos( s[0] )
} else {
vim.m_digit_buffer += cdata.digit
}
}
var act_delete_range = function(vim, cdata) {
vim.log('delete range')
var t = __yank(vim, cdata)
vim.set_text( t.text )
vim.set_pos( t.from )
vim.set_mode( cdata.mode )
}
var act_yank_range = function(vim, cdata) {
vim.log('yank range')
var t = __yank(vim, cdata)
vim.set_pos( t.from )
}
var __yank = function(vim, cdata) {
var xs = selection_with.apply( vim, [ cdata, vim.get_text(), vim.get_pos() ] ) // todo, clean
var t = cut_slice( vim.get_text(), xs[0], xs[0] + xs[1] )
vim.insert_to_clipboard( t.cut )
return t
}
/* use either select_func or move_func parameter */
var selection_with = function( cdata, text, pos ) {
var fn, xs
fn = cdata.select_func
if (fn === undefined) {
fn = cdata.move_func
var k = fn.apply( this, [ text, pos ] )
var len = k - pos
xs = (len >= 0) ? [pos, len] : [pos + len, -len]
}
else {
xs = fn.apply( this, [ text, pos ] )
}
return xs
}
var act_delete_char = function(vim, cdata) {
var p = vim.get_pos()
var t = vim.get_text()
vim.set_text( t.substr(0,p) + t.substr(p+1) )
vim.set_pos( p )
vim.insert_to_clipboard( t.substr(p,1) )
if (cdata.mode !== undefined) {
vim.set_mode(INSERT)
}
}
var act_append = function(vim, cdata) {
vim.log('append')
var xs = vim.select_current_line()
var p = vim.get_pos()
vim.set_pos( p + (p == (xs[0] + xs[1]) ? 0 : 1) ) /* don't move at the end of line*/
vim.set_mode(INSERT)
}
var act_append_to_end = function(vim, cdata) {
vim.log('append to end')
var xs = vim.select_current_line()
vim.set_pos( xs[0] + xs[1] )
vim.set_mode(INSERT)
}
var act_insert = function(vim, cdata) {
vim.log('insert')
vim.set_mode(INSERT)
}
var act_delete_current_selection = function(vim, cdata) {
vim.log('delete current selection')
var t = cut_slice( vim.get_text(), vim.m_selection_from, vim.get_pos() )
vim.set_text( t.text)
vim.set_pos( t.from )
vim.set_mode(cdata.mode)
vim.insert_to_clipboard( t.cut )
}
var act_visual_mode = function(vim, cdata) {
vim.log('act_visual_mode')
vim.set_mode( VISUAL )
}