forked from ericu/jv-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjv.js
5759 lines (5355 loc) · 182 KB
/
jv.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
/* ***** BEGIN LICENSE BLOCK *****
Version: GPL 2.0
This license covers jV, a text editor browser extension.
Copyright (C) 2013
Eric Uhrhane,
Google Inc.,
Marsette Vona ([email protected])
Copyright (C) 2012-2013 Eric Uhrhane and Google Inc.
Copyright (C) 2007-2011 Eric Uhrhane
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***** END LICENSE BLOCK *****/
/*
Feature requests:
change textarea cursor
: commands
work around Firefox spellchecker not doing anything until space after word
Any j/DOWN/ENTER that can't move should prompt a scroll in the appropriate
direction [to the max?], to handle wrapped lines at the bottom.
Add a menu item for preferences to the context menu.
Commands in command mode:
+ - % , [[ ]] { } H L M >> << U m <motion >motion { }
gq, gk, gj, gI [same as gi?]
z. z<CR> z-
gqmotion
Commands in insert mode:
C-w C-space (C-@) C-v ^u
Colon commands:
:mode
Preferences:
allow backspace out of added region (bs=2 or bs=indent,eol,start).
shiftwidth
textwidth [just for gq at first]
no flash on backspace at start of buffer
*/
// Implemented:
// hjklbBeEwWiIJaAoOpPdcsyDCSYxX0$.RrtTfF;/?nN~|^vV, home, end, del, arrow
// keys, numbers, escape, ^c, u, ^r, ^[, "[a-z.+*/], G, gg, {^b,^d,^e,^f,^u,^y}
// [normal mode only].
// Top TODOs [beyond bugs]:
// >>, << [by how far?]
// For chrome: clipboard support, now that the API's out of experimental
// Nice-to-haves:
// Detect if an outside event [such as a paste from the edit menu] has altered
// the textarea, and do something about it to clean up the undo stack.
// q<reg>, @, ^a, ^x, \,.
// Preferences for whitelisting, ts, sw, et.
// Optional case-insensitive search.
// Much later:
// advanced search [*, #]
// m, '
// :<number>
// :'<'>s/// or :%s///
// z*
// %
// Known incompatibilities:
// Search strings are Javascript regular expressions, not Vi[m] syntax.
//
// Scrolling commands scroll by a number of visible lines, not newlines.
//
// W, w, e don't complain at EOF.
//
// Ctrl+{home, end} do the wrong thing.
//
// When using arrow keys, home, and end in insert mode, the multiplier
// should be ignored, but stored for repeats IFF no text is typed after
// the arrow keys but before Escape. We currently ignore it entirely.
//
// Commands dw, cw on empty line do nothing but flash. Should delete the
// whole line [including preceding spaces!] for some reason.
//
// Redo of an undone 'O' will leave the cursor in the place it was before the
// original action was done, but measured by #chars-from-beginning-of-file,
// whereas vim measures it by line-and-#chars-from-beginning-of-line.
//
// When attempting an illegal or unsupported command in visual mode, visual
// mode is cancelled. Vim merely beeps.
//
// Vim accepts 5"a6"sdd, deleting 30 lines and storing it in s. I abort when
// presented with more than one register in a command.
// element is null in the extension case, and in that of the hack holder below.
function JsVim(element) {
// Registers are shared among all the textareas in a given window.
this.regs = new Object();
this.regsLinewise = new Object();
this.e = element;
this.lenabled = true;
return this;
}
// This global variable holds a reference to e, the element affected by the
// current event. It starts with no element, and will have its element replaced
// as needed as events come in. It's the [per-window in FF, per-iframe in
// Chrome] extension object in the extension case.
var jsvim = new JsVim();
(function () {
/* Persistent variable names.
In command 55"a66d5l, the elements are N0(55), REG(a), N1(66), CMD(d),
N2(5), MOTION(l). N[012] are multiplied together to get MUL.
*/
var VarNames = {
MODE : "MODE",
COL : "COL", // for use in moving up+down, when past/at $ on current line
MUL : "MUL",
REG : "REG",
CMD : "CMD",
MOTION : "MOTION",
COMBO_CMD_CHAR : "COMBO_CMD_CHAR", // So far only g, for gg, but z,] later.
INPUT_CHAR_CODE : "INPUT_CHAR_CODE", // So far used only by r.
OVER : 'OVER', // stores text overwritten by R
OVER_EXTEND_CHARS : 'OVER_EXTEND_CHARS', // num chars added at EOLN by R
SEEK : "SEEK", // tTfF
SEEK_CHAR : "SEEK_CHAR", // the char being sought
SEARCH : "SEARCH", // ?/
SEARCH_STR : "SEARCH_STR",
SEARCH_START_POS : "SEARCH_START_POS",
FLICKER_START_SEL : "FLICKER_START_SEL",
FLICKER_END_SEL : "FLICKER_END_SEL",
LAST_MUL : "LAST_MUL",
LAST_REG : "LAST_REG",
LAST_CMD : "LAST_CMD",
LAST_MOTION : "LAST_MOTION",
LAST_INPUT_CHAR_CODE : "LAST_INPUT_CHAR_CODE", // So far used only by r.
LAST_SEEK : "LAST_SEEK",
LAST_SEEK_CHAR : "LAST_SEEK_CHAR",
LAST_SEARCH : "LAST_SEARCH",
LAST_SEARCH_STR : "LAST_SEARCH_STR",
LAST_DEL_CHARS : "LAST_DEL_CHARS",
LAST_VISUAL_DX : "LAST_VISUAL_DX", // Stored for "." after visual action.
LAST_VISUAL_DY : "LAST_VISUAL_DY", // Stored for "." after visual action.
CUR_NUM : "CUR_NUM", // In-progress number so far, if any.
BEEPING : "BEEPING",
VISUAL : "VISUAL", // vV
VISUAL_USED : "VISUAL_USED", // vV; held here past clear of VISUAL for DOT.
VISUAL_START_POS : "VISUAL_START_POS",
VISUAL_END_POS : "VISUAL_END_POS",
VISUAL_DX : "VISUAL_DX", // Stored for "." after visual action.
VISUAL_DY : "VISUAL_DY", // Stored for "." after visual action.
UNDO_START : "UNDO_START",
UNDO_END : "UNDO_END",
UNDO_TEXT : "UNDO_TEXT",
UNDO_DEL_CHARS : "UNDO_DEL_CHARS",
// If the undo record that of an 'o' or 'O', this is set to the cursor
// position current before the action, otherwise null.
UNDO_O : "UNDO_O",
// In-progress undo record, for compound actions that make non-contiguous
// changes, otherwise null. Currently only used for 'J'.
UNDO_RECORD : "UNDO_RECORD",
LINE_HEIGHT : "LINE_HEIGHT", // Currently only used as a backup.
STATUS_BAR : "STATUS_BAR",
UPDATE_EPOCH : "UPDATE_EPOCH", // Used to detect preference changes.
// A variable on the element, not the jsvim object
JV_REMOVAL_FUNCTION : "JV_REMOVAL_FUNCTION",
};
// Names of modes
// We may need one for "Got reg".
var ModeNames = {
COMMAND : "COMMAND", // Used before and between states; may have CMD.
INSERT : "INSERT", // In insert mode.
IN_REG : "IN_REG", // We just got a ".
IN_NUM : "IN_NUM", // Gotten at least one digit, not starting with 0.
OVERWRITE : "OVERWRITE", // Got R.*.
SEEK : "SEEK", // tTfF
SEARCH : "SEARCH", // ?/
};
// Names of named registers
var RegNames = {
DEF : '"',
INS : '.',
CLIP : '+', // System clipboard on any OS.
SEL : '*', // X Selection on systems that support it.
SEEK : 'SEEK', // Not retrievable directly.
SEEK_CHAR : 'SEEK_CHAR', // Not retrievable directly.
SEARCH : 'SEARCH', // Not retrievable directly.
SEARCH_STR : 'SEARCH_STR', // User types '/', though.
};
var Consts = {
EOLN : '\n',
EMPTY : "",
SPACE : ' ',
BACKSLASH : '\\',
WHITE : "white",
BLACK : "black",
DONE : "DONE", // Thrown by abortCommand.
SEEK : "SEEK", // so that code looking at MOTION doesn't get confused
SEARCH : "SEARCH", // so that code looking at MOTION doesn't get confused
VISUAL : "VISUAL", // so that code looking at MOTION doesn't get confused
KEYCODE : "KEYCODE",
CHARCODE : "CHARCODE",
BGCOLOR : "#faf6f2" //fae2c8 fff9f9 fff7f7
};
var Combos = {
gg : "gg",
};
var Keys = {
CTRL_B : 2,
CTRL_C : 3,
CTRL_D : 4,
CTRL_E : 5,
CTRL_F : 6,
BS : 8,
TAB : 9, // Same as the KeyCode.
LF : 10,
CR : 13,
CTRL_R : 18,
CTRL_T : 20,
CTRL_U : 21,
CTRL_V : 22,
CTRL_Y : 25,
ESC : 27, // Same as the KeyCode.
SPACE : 32,
QUOTES : 34,
DOLLAR : 36,
PERCENT : 37,
L_PAREN : 40,
R_PAREN : 41,
STAR : 42,
PLUS : 43,
DOT : 46,
SLASH : 47,
N_0 : 48,
N_1 : 49,
N_2 : 50,
N_3 : 51,
N_4 : 52,
N_5 : 53,
N_6 : 54,
N_7 : 55,
N_8 : 56,
N_9 : 57,
SEMI : 59,
QUEST : 63,
A : 65,
B : 66,
C : 67,
D : 68,
E : 69,
F : 70,
G : 71,
H : 72,
I : 73,
J : 74,
K : 75,
L : 76,
M : 77,
N : 78,
O : 79,
P : 80,
Q : 81,
R : 82,
S : 83,
T : 84,
U : 85,
V : 86,
W : 87,
X : 88,
Y : 89,
Z : 90,
L_BRAC : 91,
R_BRAC : 93,
CARET : 94,
a : 97,
b : 98,
c : 99,
d : 100,
e : 101,
f : 102,
g : 103,
h : 104,
i : 105,
j : 106,
k : 107,
l : 108,
m : 109,
n : 110,
o : 111,
p : 112,
q : 113,
r : 114,
s : 115,
t : 116,
u : 117,
v : 118,
w : 119,
x : 120,
y : 121,
z : 122,
L_BRACE : 123,
PIPE : 124,
R_BRACE : 125,
TILDE : 126,
};
var KeyCodes = {
BS : 8, // Same as the Key.
TAB : 9, // Same as the Key.
SHIFT : 16,
CTRL : 17,
ALT : 18, // Beware! This looks like CTRL_R after we remap it!
// TODO: In chrome, we think CTRL+ALT is CTLR_R.
PAUSE : 19,
CAPS_LK : 20,
ESC : 27, // Same as the Key.
ARROW_L : 37,
ARROW_U : 38,
ARROW_R : 39,
ARROW_D : 40,
PAGE_U : 33,
PAGE_D : 34,
END : 35,
HOME : 36,
INS : 45,
DEL : 46,
NUM_LK : 144,
L_BRAC : 219,
R_BRAC : 221,
};
function getQueue() {
if (!this.e.queue) {
this.e.queue = new Queue(20);
}
return this.e.queue;
}
function getVar(key, def) {
if (!this.e.vars) {
this.e.vars = new Object();
}
var ret = this.e.vars[key];
if (ret == null) {
ret = def;
}
return ret;
}
function setVar(key, value) {
if (!this.e.vars) {
this.e.vars = new Object();
}
this.e.vars[key] = value;
}
function clearVar(key) {
if (!this.e.vars) {
this.e.vars = new Object();
}
this.e.vars[key] = null;
}
function getReg(reg) {
if (reg == RegNames.SEL && this.getXSelection) {
return this.getXSelection();
} else if (reg == RegNames.CLIP && this.getClipboard) {
return this.getClipboard();
}
if (reg == null) {
reg = RegNames.DEF;
}
var ret = this.regs[reg];
if (ret == null) {
ret = Consts.EMPTY;
}
return ret;
}
function regIsLinewise(reg) {
if (reg == null) {
reg = RegNames.DEF;
}
return this.regsLinewise[reg];
}
function setReg(reg, value, linewise) {
if (reg == RegNames.SEL && this.setXSelection) {
this.setXSelection(value);
} else if (reg == RegNames.CLIP && this.setClipboard) {
this.setClipboard(value);
} else {
this.regs[reg] = value;
}
// This can get out of sync with the system clipboard, but there isn't
// really much we can do about it, and it's just not that big a deal.
this.regsLinewise[reg] = linewise;
}
function getMode() {
var mode = this.getVar(VarNames.MODE);
if (!mode) {
if (this.defaultModeInsert) {
mode = ModeNames.INSERT;
this.setVar(VarNames.CMD, Keys.i);
this.endNonTextCommand(true, mode);
this.setMode(ModeNames.INSERT);
} else {
mode = ModeNames.COMMAND;
}
}
return mode;
}
function setMode(mode) {
if (mode == ModeNames.INSERT || mode == ModeNames.OVERWRITE) {
this.clearVar(VarNames.COL);
}
this.setVar(VarNames.MODE, mode);
this.updateStatusBar(mode);
}
var flickering = false;
var flickerOffset;
function doFlicker(element) {
try {
if (flickering) {
popup("doFlicker was REENTRANT!");
return;
} else if (!element) {
popup("doFlicker: element was null!");
return;
} else if (!flickerTimeoutId) {
popup("doFlicker: no flickerTimeoutId implies failed cancel!");
return;
}
this.setTextArea(element);
flickering = true;
var start = this.getSelectionStart();
var end = this.getSelectionEnd();
var pos = this.getCursorPos();
if (pos == this.getMaxPos()) {
scrollToBottom(this.e);
return; // No need to flicker!
}
this.setVar(VarNames.FLICKER_START_SEL, start);
this.setVar(VarNames.FLICKER_END_SEL, end);
// Note the use of setSelection, not setCursorPos, so as to get around any
// special variables.
this.setSelection(pos + 1, pos + 1);
var keyCode = KeyCodes.ARROW_L;
var charCode = 0;
this.sendKeyEvent(element, keyCode, charCode);
//element.focus();
} catch (ex) {
popup("Error in doFlicker:\n" + stringifyObj(ex));
} finally {
flickering = false;
}
}
var flickerTimeoutId = null;
function flicker(element) {
if (flickerTimeoutId) {
clearTimeout(flickerTimeoutId);
if (!flickerTimeoutId) {
// This should be impossible, as javascript isn't preemptive.
popup("FAILED to flicker due to race.");
return;
} else {
// Successfully cancelled
flickerTimeoutId = null;
}
}
var temp = this;
flickerTimeoutId = setTimeout(
function () {
temp.doFlicker(element);
flickerTimeoutId = null;
},
100); // Wait until idle for a bit before flickering.
}
function sendKeyEvent(element, keyCode, charCode) {
var e = document.createEvent("KeyboardEvent");
var type = "keypress";
var bubbles = true;
var cancellable = true;
var view = null;
var ctrl = false;
var alt = false;
var shift = false;
var meta = false;
// These really only work for a small range--zero-padding should be
// adaptive, and alpha values need to be case-folded. But this function's
// only ever called for left-arrow, so no biggie.
var keyLocation = '0x00';
var keyIdentifier = 'U+00' + keyCode.toString(16).toUpperCase();
eventToIgnore = e;
if (e.initKeyEvent) {
e.initKeyEvent(type, bubbles, cancellable, view, ctrl, alt, shift, meta,
keyCode, charCode);
} else {
// Focus/blur pair courtesy of
// http://stackoverflow.com/questions/2692009/move-caret-to-the-end-of-a-text-input-field-and-make-the-end-visible.
if (!element.jv_divhack) {
this.e.blur();
this.e.focus();
} //else divhackDBG("killing focus/blur!");
e.initKeyboardEvent(type, bubbles, cancellable, view, keyIdentifier,
keyLocation, ctrl, alt, shift, meta);
}
element.dispatchEvent(e);
}
function beepOn() {
if (!this.getVar(VarNames.BEEPING)) {
this.setVar(VarNames.BEEPING, 1);
var style = this.e.style;
var temp = style.color;
style.color = style.backgroundColor;
style.backgroundColor = temp;
if (!style.color) {
style.color = Consts.WHITE;
}
if (!style.backgroundColor) {
style.backgroundColor = Consts.BLACK;
}
}
}
function beepOff() {
if (this.getVar(VarNames.BEEPING)) {
this.setVar(VarNames.BEEPING, 0);
var style = this.e.style;
var temp = style.color;
style.color = style.backgroundColor;
style.backgroundColor = temp;
}
}
function beep() {
if (!this.inhibitVisualBell) {
this.beepOn();
temp = this;
setTimeout(
function() {
temp.beepOff();
}, 75);
}
}
function clearVisualVars() {
this.clearVar(VarNames.VISUAL);
this.clearVar(VarNames.VISUAL_START_POS);
this.clearVar(VarNames.VISUAL_END_POS);
}
// This doesn't clear undo vars, but so far there's nothing that would set
// them that could be aborted.
function clearCmdVars(clearVisual) {
this.clearVar(VarNames.MUL);
this.clearVar(VarNames.REG);
this.clearVar(VarNames.CMD);
this.clearVar(VarNames.MOTION);
this.clearVar(VarNames.CUR_NUM);
this.clearVar(VarNames.COMBO_CMD_CHAR);
this.clearVar(VarNames.SEARCH_START_POS);
if (clearVisual) {
this.clearVisualVars();
}
}
function storeCmdVars() {
var cmd = this.getVar(VarNames.CMD);
var reg;
var mul;
var motion;
var inputCharCode;
var seek;
var seekChar;
var search;
var searchStr;
var delChars;
var visualDX;
var visualDY;
if (cmd == Keys.DOT) {
cmd = this.getVar(VarNames.LAST_CMD);
reg = this.getVar(VarNames.LAST_REG);
mul = this.getVar(VarNames.MUL, this.getVar(VarNames.LAST_MUL));
motion = this.getVar(VarNames.LAST_MOTION);
inputCharCode = this.getVar(VarNames.LAST_INPUT_CHAR_CODE);
seek = this.getVar(VarNames.LAST_SEEK);
seekChar = this.getVar(VarNames.LAST_SEEK_CHAR);
search = this.getVar(VarNames.LAST_SEARCH);
searchStr = this.getVar(VarNames.LAST_SEARCH_STR);
delChars = this.getVar(VarNames.LAST_DEL_CHARS);
visualDX = this.getVar(VarNames.LAST_VISUAL_DX);
visualDY = this.getVar(VarNames.LAST_VISUAL_DY);
visual = this.getVar(VarNames.LAST_VISUAL_USED);
} else {
reg = this.getVar(VarNames.REG);
mul = this.getVar(VarNames.MUL);
motion = this.getVar(VarNames.MOTION);
inputCharCode = this.getVar(VarNames.INPUT_CHAR_CODE);
seek = this.getVar(VarNames.SEEK);
seekChar = this.getVar(VarNames.SEEK_CHAR);
search = this.getVar(VarNames.SEARCH);
searchStr = this.getVar(VarNames.SEARCH_STR);
visualDX = this.getVar(VarNames.VISUAL_DX);
visualDY = this.getVar(VarNames.VISUAL_DY);
visual = this.getVar(VarNames.VISUAL_USED);
// We don't store DEL_CHARS here, since we don't know it yet. The user
// has typed e.g. 'cw', but hasn't gotten to the DEL chars yet. Therefore
// we only ever use LAST_DEL_CHARS, and we set it directly as it's
// discovered. However, if the user's using DOT, we know to keep
// LAST_DEL_CHARS around.
}
this.setVar(VarNames.LAST_REG, reg);
this.setVar(VarNames.LAST_MUL, mul);
this.setVar(VarNames.LAST_CMD, cmd);
this.setVar(VarNames.LAST_MOTION, motion);
this.setVar(VarNames.LAST_INPUT_CHAR_CODE, inputCharCode);
this.setVar(VarNames.LAST_SEEK, seek);
this.setVar(VarNames.LAST_SEEK_CHAR, seekChar);
this.setVar(VarNames.LAST_SEARCH, search);
this.setVar(VarNames.LAST_SEARCH_STR, searchStr);
this.setVar(VarNames.LAST_DEL_CHARS, delChars);
this.setVar(VarNames.LAST_VISUAL_DX, visualDX);
this.setVar(VarNames.LAST_VISUAL_DY, visualDY);
this.setVar(VarNames.LAST_VISUAL_USED, visual);
}
// This is the normal end of a command [where you end up if you're going back
// to command mode, but not via a repeated insert or overwrite].
function endNonTextCommand(repeatable, mode) {
var wasUndoRedo;
if (repeatable) {
this.storeCmdVars();
}
var cmd = this.getVar(VarNames.CMD);
if (cmd == Keys.u || cmd == Keys.CTRL_R) {
wasUndoRedo = true;
}
this.clearCmdVars();
if (mode == ModeNames.INSERT) {
this.setReg(RegNames.INS, Consts.EMPTY);
} else if (mode == ModeNames.OVERWRITE) {
this.setReg(RegNames.INS, Consts.EMPTY);
this.setVar(VarNames.OVER, Consts.EMPTY);
this.setVar(VarNames.OVER_EXTEND_CHARS, 0);
}
return wasUndoRedo;
}
function endCommand(mode, repeatable, special) {
var curMode = this.getMode();
var wasUndoRedo;
// TODO: This comment is somewhat out of date.
// special is the flag that we're using arrow keys in insert mode, so
// we're going to skip the multiplier, but save the multiplied command that
// got us into insert mode as the command to remember. The subsequent stuff
// gets forgotten [repeatable == false when that comes through].
if (curMode == ModeNames.INSERT || curMode == ModeNames.OVERWRITE) {
// Vars are already cleared, but we may have more to do.
var oldCmd = this.getVar(VarNames.LAST_CMD);
var mul = this.getVar(VarNames.LAST_MUL, 1) - 1;
var start = this.getCursorPos();
var end = start;
if (!special && mul > 0 && oldCmd && isRepeatableInsertCommand(oldCmd)) {
var newText = this.getReg(RegNames.INS);
if (newText.length > 0) {
if (oldCmd == Keys.O || oldCmd == Keys.o) {
newText = Consts.EOLN + newText;
}
var single = newText;
for (var i=1; i < mul; ++i) {
newText += single;
}
var endPos = start + newText.length;
if (curMode == ModeNames.OVERWRITE) {
end = endPos;
var oldText = this.getRange(start, end);
var eoln = oldText.indexOf(Consts.EOLN);
if (eoln != -1) {
if (curMode == ModeNames.OVERWRITE) {
end = start + eoln;
} else {
// Undo the already-done deletion.
// Looks like this is invoked in "2[Rr]<x>[<Esc>].".
// So you're trying to do a multi-char replace/overwrite at the
// end of a line, but there isn't space, so we abort.
this.replaceRange(start - 1, start, this.getVar(VarNames.OVER));
this.setCursorPos(start - 1);
this.abortCommand();
}
}
}
this.replaceRange(start, end, newText);
this.setCursorPos(endPos);
}
}
if (!special) {
this.setCursorPos(this.safeBackUp());
}
this.clearVar(VarNames.COL);
if (special) {
// Nuke the mul. We should really keep it around for some rare
// circumstances, but that can wait.
this.clearVar(VarNames.LAST_MUL);
}
} else {
wasUndoRedo = this.endNonTextCommand(repeatable, mode);
}
this.setMode(mode);
if (mode == ModeNames.COMMAND && !wasUndoRedo) {
this.pushUndoState();
}
}
function abortCommand(quiet) {
if (!quiet) {
this.beep();
}
this.clearCmdVars(true);
this.setCursorPos(this.getCursorPos()); // Clear highlight.
this.setMode(ModeNames.COMMAND);
throw Consts.DONE;
}
function handleUnrecognizedChar() {
// Used to do something different here, but it ended up not needed. todo:
// figure out something better to do, or just inline this.
this.abortCommand();
}
function setCursorPos(pos) {
var end = this.getVar(VarNames.VISUAL_END_POS);
if (end != null) {
this.setVar(VarNames.VISUAL_END_POS, pos);
this.highlightVisualRange();
} else {
this.setSelection(pos, pos);
}
}
function setSelection(start, end) {
if (null == start) {
popup("Bad selection pos!");
}
if (null == end) {
end = start;
}
this.e.selectionStart = start;
this.e.selectionEnd = end;
if (this.e.jv_divhack) this.divhackUpdateRangeFromSelection();
}
function getCursorPos() {
return this.getVar(VarNames.VISUAL_END_POS, this.getSelectionEnd());
}
function getSelectionStart() {
//TODO(vona) maybe ensure sync with range in div?
//for now we try to do it actively in event listeners vs passively here
return this.e.selectionStart;
}
function getSelectionEnd() {
//TODO(vona) maybe ensure sync with range in div?
//for now we try to do it actively in event listeners vs passively here
return this.e.selectionEnd;
}
function getSelectionText() {
return this.getText(this.getSelectionStart(), this.getSelectionEnd());
}
function getText(start, end) {
return this.getElementText().slice(start, end);
}
function getMaxPos() {
return this.getElementText().length;
}
function getCharAtPos(pos) {
return this.getElementText().charAt(pos);
}
function getCharCodeAtPos(pos) {
return this.getElementText().charCodeAt(pos);
}
var hack = 0;
// There are a variety of techniques here, for timing tests. They're all
// about the same, it turns out.
function replaceRangeNoUndo(start, end, newText, saveCol) {
if (!saveCol) {
this.clearVar(VarNames.COL);
}
var element = this.e;
if (element.jv_divhack) {
divhackUpdateValueFromDiv();
}
var scrollPos = getScrollPos(element);
var oldText;
//pushTimingEvent("Extraction " + (hack % 2));
switch (hack % 2) { // How to extract the old text.
case 0:
// the original, simple method
prevText = element.value.slice(0, start);
if (end == start) {
oldText = Consts.EMPTY;
} else {
oldText = element.value.slice(start, end);
}
postText = element.value.slice(end);
break;
case 1:
// substring instead of slice
prevText = element.value.substring(0, start);
if (end == start) {
oldText = Consts.EMPTY;
} else {
oldText = element.value.substring(start, end);
}
postText = element.value.substring(end);
break;
}
//pushTimingEvent("Done extraction " + (hack % 2));
//pushTimingEvent("Combination " + (hack % 3));
switch (hack % 3) {
case 0:
// The original, simple method.
element.value = prevText + newText + postText;
break;
case 1:
// string.concat
element.value = prevText.concat(newText, postText);
break;
case 2:
// array join
element.value = [prevText, newText, postText].join(Consts.EMPTY);
break;
}
//pushTimingEvent("Done combination " + (hack % 3));
if (element.jv_divhack) {
divhackUpdateDivFromValue();
}
setScrollPos(element, scrollPos);
++hack;
return oldText;
}
function replaceRange(start, end, newText, isSpecialDel) {
var oldText = this.replaceRangeNoUndo(start, end, newText);
if (isSpecialDel) {
this.addUndoDelChars(start, oldText);
} else {
this.addUndoInfo(start, start + newText.length, oldText);
}
return oldText;
}
function deleteChars(start, count) {
if (count) {
var pos = this.getCursorPos();
this.replaceRange(start, Math.min(start+count, this.getMaxPos()),
Consts.EMPTY, true);
this.setCursorPos(pos); // Seems to be required.
}
}
function getRange(start, end) {
return this.getElementText().slice(start, end);
}
function alertCharCode(str, charCode) {
popup(str + "(" + charCode + "):'" + String.fromCharCode(charCode) + "'");
}
function dumpRegs(jsvim) {
_debug("Regs: ");
_debug(stringifyObj(jsvim.regs));
}
function dumpObj(obj) {
_debug("Debugging:");
for (i in obj) {
_debug(i + ":\t" + obj[i]);
}
}
// Persistent variable names
var CharTypes = {
ALNUM : 1,
PUNC : 2,
WS : 3,
}
function isWhiteSpace(charCode) {
return charCode <= 32;
}
function isDigit(charCode) {
return (Keys.N_0 <= charCode) && (charCode <= Keys.N_9);
}
function isComboCommandChar(charCode) {
return charCode == Keys.g;
}
function isRepeatableInsertCommand(charCode) {
switch (charCode) {
case Keys.a:
case Keys.A:
case Keys.I:
case Keys.i:
case Keys.o:
case Keys.O:
case Keys.R:
return true;
default:
return false;
}
}
function isSeek(charCode) {
switch (charCode) {
case Keys.t:
case Keys.T:
case Keys.f:
case Keys.F:
return true;
default:
return false;
}
}
function isSearch(charCode) {
switch (charCode) {
case Keys.QUEST:
case Keys.SLASH:
return true;
default:
return false;
}
}
function invertSearch(charCode) {
switch (charCode) {
case Keys.QUEST:
return Keys.SLASH;
case Keys.SLASH:
return Keys.QUEST;
default:
throw "Bad search char code: " + charCode;
}
}
function isVisual(charCode) {
switch (charCode) {
case Keys.V:
case Keys.v:
return true;
default:
return false;
}
}
function isCompleteCommand(charCode) {
switch (charCode) {
case Keys.CTRL_B:
case Keys.CTRL_D:
case Keys.CTRL_E: