This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
/
vibreoffice.vbs
1037 lines (830 loc) · 28.9 KB
/
vibreoffice.vbs
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
' vibreoffice - Vi Mode for LibreOffice/OpenOffice
'
' The MIT License (MIT)
'
' Copyright (c) 2014 Sean Yeh
'
' 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.
Option Explicit
' --------
' Globals
' --------
global VIBREOFFICE_STARTED as boolean ' Defaults to False
global VIBREOFFICE_ENABLED as boolean ' Defaults to False
global oXKeyHandler as object
' Global State
global MODE as string
global VIEW_CURSOR as object
global MULTIPLIER as integer
' -----------
' Singletons
' -----------
Function getCursor
getCursor = VIEW_CURSOR
End Function
Function getTextCursor
dim oTextCursor
On Error Goto ErrorHandler
oTextCursor = getCursor().getText.createTextCursorByRange(getCursor())
getTextCursor = oTextCursor
Exit Function
ErrorHandler:
' Text Cursor does not work in some instances, such as in Annotations
getTextCursor = Nothing
End Function
' -----------------
' Helper Functions
' -----------------
Sub restoreStatus 'restore original statusbar
dim oLayout
oLayout = thisComponent.getCurrentController.getFrame.LayoutManager
oLayout.destroyElement("private:resource/statusbar/statusbar")
oLayout.createElement("private:resource/statusbar/statusbar")
End Sub
Sub setRawStatus(rawText)
thisComponent.Currentcontroller.StatusIndicator.Start(rawText, 0)
End Sub
Sub setStatus(statusText)
setRawStatus(MODE & " | " & statusText & " | special: " & getSpecial() & " | " & "modifier: " & getMovementModifier())
End Sub
Sub setMode(modeName)
MODE = modeName
setRawStatus(modeName)
End Sub
Function gotoMode(sMode)
Select Case sMode
Case "NORMAL":
setMode("NORMAL")
setMovementModifier("")
Case "INSERT":
setMode("INSERT")
Case "VISUAL":
setMode("VISUAL")
dim oTextCursor
oTextCursor = getTextCursor()
' Deselect TextCursor
oTextCursor.gotoRange(oTextCursor.getStart(), False)
' Show TextCursor selection
thisComponent.getCurrentController.Select(oTextCursor)
End Select
End Function
Sub cursorReset(oTextCursor)
oTextCursor.gotoRange(oTextCursor.getStart(), False)
oTextCursor.goRight(1, False)
oTextCursor.goLeft(1, True)
thisComponent.getCurrentController.Select(oTextCursor)
End Sub
Function samePos(oPos1, oPos2)
samePos = oPos1.X() = oPos2.X() And oPos1.Y() = oPos2.Y()
End Function
Function genString(sChar, iLen)
dim sResult, i
sResult = ""
For i = 1 To iLen
sResult = sResult & sChar
Next i
genString = sResult
End Function
' Yanks selection to system clipboard.
' If bDelete is true, will delete selection.
Sub yankSelection(bDelete)
dim dispatcher As Object
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, ".uno:Copy", "", 0, Array())
If bDelete Then
getTextCursor().setString("")
End If
End Sub
Sub pasteSelection()
dim oTextCursor, dispatcher As Object
' Deselect if in NORMAL mode to avoid overwriting the character underneath
' the cursor
If MODE = "NORMAL" Then
oTextCursor = getTextCursor()
oTextCursor.gotoRange(oTextCursor.getStart(), False)
thisComponent.getCurrentController.Select(oTextCursor)
End If
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, ".uno:Paste", "", 0, Array())
End Sub
' -----------------------------------
' Special Mode (for chained commands)
' -----------------------------------
global SPECIAL_MODE As string
global SPECIAL_COUNT As integer
Sub setSpecial(specialName)
SPECIAL_MODE = specialName
If specialName = "" Then
SPECIAL_COUNT = 0
Else
SPECIAL_COUNT = 2
End If
End Sub
Function getSpecial()
getSpecial = SPECIAL_MODE
End Function
Sub delaySpecialReset()
SPECIAL_COUNT = SPECIAL_COUNT + 1
End Sub
Sub resetSpecial(Optional bForce)
If IsMissing(bForce) Then bForce = False
SPECIAL_COUNT = SPECIAL_COUNT - 1
If SPECIAL_COUNT <= 0 Or bForce Then
setSpecial("")
End If
End Sub
' -----------------
' Movement Modifier
' -----------------
'f,i,a
global MOVEMENT_MODIFIER As string
Sub setMovementModifier(modifierName)
MOVEMENT_MODIFIER = modifierName
End Sub
Function getMovementModifier()
getMovementModifier = MOVEMENT_MODIFIER
End Function
' --------------------
' Multiplier functions
' --------------------
Sub _setMultiplier(n as integer)
MULTIPLIER = n
End Sub
Sub resetMultiplier()
_setMultiplier(0)
End Sub
Sub addToMultiplier(n as integer)
dim sMultiplierStr as String
dim iMultiplierInt as integer
' Max multiplier: 10000 (stop accepting additions after 1000)
If MULTIPLIER <= 1000 then
sMultiplierStr = CStr(MULTIPLIER) & CStr(n)
_setMultiplier(CInt(sMultiplierStr))
End If
End Sub
' Should only be used if you need the raw value
Function getRawMultiplier()
getRawMultiplier = MULTIPLIER
End Function
' Same as getRawMultiplier, but defaults to 1 if it is unset (0)
Function getMultiplier()
If MULTIPLIER = 0 Then
getMultiplier = 1
Else
getMultiplier = MULTIPLIER
End If
End Function
' -------------
' Key Handling
' -------------
Sub sStartXKeyHandler
sStopXKeyHandler()
oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
thisComponent.CurrentController.AddKeyHandler(oXKeyHandler)
End Sub
Sub sStopXKeyHandler
thisComponent.CurrentController.removeKeyHandler(oXKeyHandler)
End Sub
Sub XKeyHandler_Disposing(oEvent)
End Sub
' --------------------
' Main Key Processing
' --------------------
function KeyHandler_KeyPressed(oEvent) as boolean
dim oTextCursor
' Exit if plugin is not enabled
If IsMissing(VIBREOFFICE_ENABLED) Or Not VIBREOFFICE_ENABLED Then
KeyHandler_KeyPressed = False
Exit Function
End If
' Exit if TextCursor does not work (as in Annotations)
oTextCursor = getTextCursor()
If oTextCursor Is Nothing Then
KeyHandler_KeyPressed = False
Exit Function
End If
dim bConsumeInput, bIsMultiplier, bIsModified, bIsControl, bIsSpecial
bConsumeInput = True ' Block all inputs by default
bIsMultiplier = False ' reset multiplier by default
bIsModified = oEvent.Modifiers > 1 ' If Ctrl or Alt is held down. (Shift=1)
bIsControl = (oEvent.Modifiers = 2) or (oEvent.Modifiers = 8)
bIsSpecial = getSpecial() <> ""
' --------------------------
' Process global shortcuts, exit if matched (like ESC)
If ProcessGlobalKey(oEvent) Then
' Pass
' If INSERT mode, allow all inputs
ElseIf MODE = "INSERT" Then
bConsumeInput = False
' If Change Mode
' ElseIf MODE = "NORMAL" And Not bIsSpecial And getMovementModifier() = "" And ProcessModeKey(oEvent) Then
ElseIf ProcessModeKey(oEvent) Then
' Pass
' Replace Key
ElseIf getSpecial() = "r" And Not bIsModified Then
dim iLen
iLen = Len(getCursor().getString())
getCursor().setString(genString(oEvent.KeyChar, iLen))
' Multiplier Key
ElseIf ProcessNumberKey(oEvent) Then
bIsMultiplier = True
delaySpecialReset()
' Normal Key
ElseIf ProcessNormalKey(oEvent.KeyChar, oEvent.Modifiers) Then
' Pass
' If is modified but doesn't match a normal command, allow input
' (Useful for built-in shortcuts like Ctrl+a, Ctrl+s, Ctrl+w)
ElseIf bIsModified Then
' Ctrl+a (select all) sets mode to VISUAL
If bIsControl And oEvent.KeyChar = "a" Then
gotoMode("VISUAL")
End If
bConsumeInput = False
' Movement modifier here?
ElseIf ProcessMovementModifierKey(oEvent.KeyChar) Then
delaySpecialReset()
' If standard movement key (in VISUAL mode) like arrow keys, home, end
ElseIf MODE = "VISUAL" And ProcessStandardMovementKey(oEvent) Then
' Pass
' If bIsSpecial but nothing matched, return to normal mode
ElseIf bIsSpecial Then
gotoMode("NORMAL")
' Allow non-letter keys if unmatched
ElseIf asc(oEvent.KeyChar) = 0 Then
bConsumeInput = False
End If
' --------------------------
' Reset Special
resetSpecial()
' Reset multiplier if last input was not number and not in special mode
If not bIsMultiplier and getSpecial() = "" and getMovementModifier() = "" Then
resetMultiplier()
End If
setStatus(getMultiplier())
KeyHandler_KeyPressed = bConsumeInput
End Function
Function KeyHandler_KeyReleased(oEvent) As boolean
dim oTextCursor
' Show terminal-like cursor
oTextCursor = getTextCursor()
If oTextCursor Is Nothing Then
' Do nothing
ElseIf oEvent.Modifiers = 2 Or oEvent.Modifiers = 8 And oEvent.KeyChar = "c" Then
' Allow Ctrl+c for Copy, so don't change cursor
' Pass
ElseIf MODE = "NORMAL" Then
cursorReset(oTextCursor)
ElseIf MODE = "INSERT" Then
oTextCursor.gotoRange(oTextCursor.getStart(), False)
thisComponent.getCurrentController.Select(oTextCursor)
End If
KeyHandler_KeyReleased = (MODE = "NORMAL") 'cancel KeyReleased
End Function
' ----------------
' Processing Keys
' ----------------
Function ProcessGlobalKey(oEvent)
dim bMatched, bIsControl
bMatched = True
bIsControl = (oEvent.Modifiers = 2) or (oEvent.Modifiers = 8)
' PRESSED ESCAPE (or ctrl+[)
if oEvent.KeyCode = 1281 Or (oEvent.KeyCode = 1315 And bIsControl) Then
' Move cursor back if was in INSERT (but stay on same line)
If MODE <> "NORMAL" And Not getCursor().isAtStartOfLine() Then
getCursor().goLeft(1, False)
End If
resetSpecial(True)
gotoMode("NORMAL")
Else
bMatched = False
End If
ProcessGlobalKey = bMatched
End Function
Function ProcessStandardMovementKey(oEvent)
dim c, bMatched
c = oEvent.KeyCode
bMatched = True
If MODE <> "VISUAL" Then
bMatched = False
'Pass
ElseIf c = 1024 Then
ProcessMovementKey("j", True)
ElseIf c = 1025 Then
ProcessMovementKey("k", True)
ElseIf c = 1026 Then
ProcessMovementKey("h", True)
ElseIf c = 1027 Then
ProcessMovementKey("l", True)
ElseIf c = 1028 Then
ProcessMovementKey("^", True)
ElseIf c = 1029 Then
ProcessMovementKey("$", True)
Else
bMatched = False
End If
ProcessStandardMovementKey = bMatched
End Function
Function ProcessNumberKey(oEvent)
dim c
c = CStr(oEvent.KeyChar)
If c >= "0" and c <= "9" Then
addToMultiplier(CInt(c))
ProcessNumberKey = True
Else
ProcessNumberKey = False
End If
End Function
Function ProcessModeKey(oEvent)
dim bIsModified
bIsModified = oEvent.Modifiers > 1 ' If Ctrl or Alt is held down. (Shift=1)
' Don't change modes in these circumstances
If MODE <> "NORMAL" Or bIsModified Or getSpecial <> "" Or getMovementModifier() <> "" Then
ProcessModeKey = False
Exit Function
End If
' Mode matching
dim bMatched
bMatched = True
Select Case oEvent.KeyChar
' Insert modes
Case "i", "a", "I", "A", "o", "O":
If oEvent.KeyChar = "a" Then getCursor().goRight(1, False)
If oEvent.KeyChar = "I" Then ProcessMovementKey("^")
If oEvent.KeyChar = "A" Then ProcessMovementKey("$")
If oEvent.KeyChar = "o" Then
ProcessMovementKey("$")
ProcessMovementKey("l")
getCursor().setString(chr(13))
If Not getCursor().isAtStartOfLine() Then
getCursor().setString(chr(13) & chr(13))
ProcessMovementKey("l")
End If
End If
If oEvent.KeyChar = "O" Then
ProcessMovementKey("^")
getCursor().setString(chr(13))
If Not getCursor().isAtStartOfLine() Then
ProcessMovementKey("h")
getCursor().setString(chr(13))
ProcessMovementKey("l")
End If
End If
gotoMode("INSERT")
Case "v":
gotoMode("VISUAL")
Case Else:
bMatched = False
End Select
ProcessModeKey = bMatched
End Function
Function ProcessNormalKey(keyChar, modifiers)
dim i, bMatched, bIsVisual, iIterations, bIsControl
bIsControl = (modifiers = 2) or (modifiers = 8)
bIsVisual = (MODE = "VISUAL") ' is this hardcoding bad? what about visual block?
' ----------------------
' 1. Check Movement Key
' ----------------------
iIterations = getMultiplier()
bMatched = False
For i = 1 To iIterations
dim bMatchedMovement
' Movement Key
bMatchedMovement = ProcessMovementKey(KeyChar, bIsVisual, modifiers)
bMatched = bMatched or bMatchedMovement
' If Special: d/c + movement
If bMatched And (getSpecial() = "d" Or getSpecial() = "c" Or getSpecial() = "y") Then
yankSelection((getSpecial() <> "y"))
End If
Next i
' Reset Movement Modifier
setMovementModifier("")
' Exit already if movement key was matched
If bMatched Then
' If Special: d/c : change mode
If getSpecial() = "d" Or getSpecial() = "y" Then gotoMode("NORMAL")
If getSpecial() = "c" Then gotoMode("INSERT")
ProcessNormalKey = True
Exit Function
End If
' --------------------
' 2. Undo/Redo
' --------------------
If keyChar = "u" Or (bIsControl And keyChar = "r") Then
For i = 1 To iIterations
Undo(keyChar = "u")
Next i
ProcessNormalKey = True
Exit Function
End If
' --------------------
' 3. Paste
' Note: in vim, paste will result in cursor being over the last character
' of the pasted content. Here, the cursor will be the next character
' after that. Fix?
' --------------------
If keyChar = "p" or keyChar = "P" Then
' Move cursor right if "p" to paste after cursor
If keyChar = "p" Then
ProcessMovementKey("l", False)
End If
For i = 1 To iIterations
pasteSelection()
Next i
ProcessNormalKey = True
Exit Function
End If
' --------------------
' 4. Check Special/Delete Key
' --------------------
' There are no special/delete keys with modifier keys, so exit early
If modifiers > 1 Then
ProcessNormalKey = False
Exit Function
End If
' Only 'x' or Special (dd, cc) can be done more than once
If keyChar <> "x" and getSpecial() = "" Then
iIterations = 1
End If
For i = 1 To iIterations
dim bMatchedSpecial
' Special/Delete Key
bMatchedSpecial = ProcessSpecialKey(keyChar)
bMatched = bMatched or bMatchedSpecial
Next i
ProcessNormalKey = bMatched
End Function
' Function for both undo and redo
Sub Undo(bUndo)
On Error Goto ErrorHandler
If bUndo Then
thisComponent.getUndoManager().undo()
Else
thisComponent.getUndoManager().redo()
End If
Exit Sub
' Ignore errors from no more undos/redos in stack
ErrorHandler:
Resume Next
End Sub
Function ProcessSpecialKey(keyChar)
dim oTextCursor, bMatched, bIsSpecial, bIsDelete
bMatched = True
bIsSpecial = getSpecial() <> ""
If keyChar = "d" Or keyChar = "c" Or keyChar = "s" Or keyChar = "y" Then
bIsDelete = (keyChar <> "y")
' Special Cases: 'dd' and 'cc'
If bIsSpecial Then
dim bIsSpecialCase
bIsSpecialCase = (keyChar = "d" And getSpecial() = "d") Or (keyChar = "c" And getSpecial() = "c")
If bIsSpecialCase Then
ProcessMovementKey("^", False)
ProcessMovementKey("j", True)
oTextCursor = getTextCursor()
thisComponent.getCurrentController.Select(oTextCursor)
yankSelection(bIsDelete)
Else
bMatched = False
End If
' Go to INSERT mode after 'cc', otherwise NORMAL
If bIsSpecialCase And keyChar = "c" Then
gotoMode("INSERT")
Else
gotoMode("NORMAL")
End If
' visual mode: delete selection
ElseIf MODE = "VISUAL" Then
oTextCursor = getTextCursor()
thisComponent.getCurrentController.Select(oTextCursor)
yankSelection(bIsDelete)
If keyChar = "c" Or keyChar = "s" Then gotoMode("INSERT")
If keyChar = "d" Or keyChar = "y" Then gotoMode("NORMAL")
' Enter Special mode: 'd', 'c', or 'y' ('s' => 'cl')
ElseIf MODE = "NORMAL" Then
' 's' => 'cl'
If keyChar = "s" Then
setSpecial("c")
gotoMode("VISUAL")
ProcessNormalKey("l", 0)
Else
setSpecial(keyChar)
gotoMode("VISUAL")
End If
End If
' If is 'r' for replace
ElseIf keyChar = "r" Then
setSpecial("r")
' Otherwise, ignore if bIsSpecial
ElseIf bIsSpecial Then
bMatched = False
ElseIf keyChar = "x" Then
oTextCursor = getTextCursor()
thisComponent.getCurrentController.Select(oTextCursor)
yankSelection(True)
' Reset Cursor
cursorReset(oTextCursor)
' Goto NORMAL mode (in the case of VISUAL mode)
gotoMode("NORMAL")
ElseIf keyChar = "D" Or keyChar = "C" Then
If MODE = "VISUAL" Then
ProcessMovementKey("^", False)
ProcessMovementKey("$", True)
ProcessMovementKey("l", True)
Else
' Deselect
oTextCursor = getTextCursor()
oTextCursor.gotoRange(oTextCursor.getStart(), False)
thisComponent.getCurrentController.Select(oTextCursor)
ProcessMovementKey("$", True)
End If
yankSelection(True)
If keyChar = "D" Then
gotoMode("NORMAL")
ElseIf keyChar = "C" Then
gotoMode("INSERT")
End IF
' S only valid in NORMAL mode
ElseIf keyChar = "S" And MODE = "NORMAL" Then
ProcessMovementKey("^", False)
ProcessMovementKey("$", True)
yankSelection(True)
gotoMode("INSERT")
Else
bMatched = False
End If
ProcessSpecialKey = bMatched
End Function
Function ProcessMovementModifierKey(keyChar)
dim bMatched
bMatched = True
Select Case keyChar
Case "f", "t", "F", "T", "i", "a":
setMovementModifier(keyChar)
Case Else:
bMatched = False
End Select
ProcessMovementModifierKey = bMatched
End Function
Function ProcessSearchKey(oTextCursor, searchType, keyChar, bExpand)
'-----------
' Searching
'-----------
dim bMatched, oSearchDesc, oFoundRange, bIsBackwards, oStartRange
bMatched = True
bIsBackwards = (searchType = "F" Or searchType = "T")
If Not bIsBackwards Then
' VISUAL mode will goRight AFTER the selection
If MODE <> "VISUAL" Then
' Start searching from next character
oTextCursor.goRight(1, bExpand)
End If
oStartRange = oTextCursor.getEnd()
' Go back one
oTextCursor.goLeft(1, bExpand)
Else
oStartRange = oTextCursor.getStart()
End If
oSearchDesc = thisComponent.createSearchDescriptor()
oSearchDesc.setSearchString(keyChar)
oSearchDesc.SearchCaseSensitive = True
oSearchDesc.SearchBackwards = bIsBackwards
oFoundRange = thisComponent.findNext( oStartRange, oSearchDesc )
If not IsNull(oFoundRange) Then
dim oText, foundPos, curPos, bSearching
oText = oTextCursor.getText()
foundPos = oFoundRange.getStart()
' Unfortunately, we must go go to this "found" position one character at
' a time because I have yet to find a way to consistently move the
' Start range of the text cursor and leave the End range intact.
If bIsBackwards Then
curPos = oTextCursor.getEnd()
Else
curPos = oTextCursor.getStart()
End If
do until oText.compareRegionStarts(foundPos, curPos) = 0
If bIsBackwards Then
bSearching = oTextCursor.goLeft(1, bExpand)
curPos = oTextCursor.getStart()
Else
bSearching = oTextCursor.goRight(1, bExpand)
curPos = oTextCursor.getEnd()
End If
' Prevent infinite if unable to find, but shouldn't ever happen (?)
If Not bSearching Then
bMatched = False
Exit Do
End If
Loop
If searchType = "t" Then
oTextCursor.goLeft(1, bExpand)
ElseIf searchType = "T" Then
oTextCursor.goRight(1, bExpand)
End If
Else
bMatched = False
End If
' If matched, then we want to select PAST the character
' Else, this will counteract some weirdness. hack either way
If Not bIsBackwards And MODE = "VISUAL" Then
oTextCursor.goRight(1, bExpand)
End If
ProcessSearchKey = bMatched
End Function
Function ProcessInnerKey(oTextCursor, movementModifier, keyChar, bExpand)
dim bMatched, searchType1, searchType2, search1, search2
' Setting searchType
If movementModifier = "i" Then
searchType1 = "T" : searchType2 = "t"
ElseIf movementModifier = "a" Then
searchType1 = "F" : searchType2 = "f"
Else ' Shouldn't happen
ProcessInnerKey = False
Exit Function
End If
Select Case keyChar
Case "(", ")", "{", "}", "[", "]", "<", ">", "t", "'", """":
Select Case keyChar
Case "(", ")":
search1 = "(" : search2 = ")"
Case "{", "}":
search1 = "{" : search2 = "}"
Case "[", "]":
search1 = "[" : search2 = "}"
Case "<", ">":
search1 = "<" : search2 = ">"
Case "t":
search1 = ">" : search2 = "<"
Case "'":
search1 = "'" : search2 = "'"
Case """":
' Matches "smart" quotes, which is default in libreoffice
search1 = "“" : search2 = "”"
End Select
dim bMatched1, bMatched2
bMatched1 = ProcessSearchKey(oTextCursor, searchType1, search1, False)
bMatched2 = ProcessSearchKey(oTextCursor, searchType2, search2, True)
bMatched = (bMatched1 And bMatched2)
Case Else:
bMatched = False
End Select
ProcessInnerKey = bMatched
End Function
' -----------------------
' Main Movement Function
' -----------------------
' Default: bExpand = False, keyModifiers = 0
Function ProcessMovementKey(keyChar, Optional bExpand, Optional keyModifiers)
dim oTextCursor, bSetCursor, bMatched
oTextCursor = getTextCursor()
bMatched = True
If IsMissing(bExpand) Then bExpand = False
If IsMissing(keyModifiers) Then keyModifiers = 0
' Check for modified keys (Ctrl, Alt, not Shift)
If keyModifiers > 1 Then
dim bIsControl
bIsControl = (keyModifiers = 2) or (keyModifiers = 8)
' Ctrl+d and Ctrl+u
If bIsControl and keyChar = "d" Then
getCursor().ScreenDown(bExpand)
ElseIf bIsControl and keyChar = "u" Then
getCursor().ScreenUp(bExpand)
Else
bMatched = False
End If
ProcessMovementKey = bMatched
Exit Function
End If
' Set global cursor to oTextCursor's new position if moved
bSetCursor = True
' ------------------
' Movement matching
' ------------------
' ---------------------------------
' Special Case: Modified movements
If getMovementModifier() <> "" Then
Select Case getMovementModifier()
' f,F,t,T searching
Case "f", "t", "F", "T":
bMatched = ProcessSearchKey(oTextCursor, getMovementModifier(), keyChar, bExpand)
Case "i", "a":
bMatched = ProcessInnerKey(oTextCursor, getMovementModifier(), keyChar, bExpand)
Case Else:
bSetCursor = False
bMatched = False
End Select
If Not bMatched Then
bSetCursor = False
End If
' ---------------------------------
ElseIf keyChar = "l" Then
oTextCursor.goRight(1, bExpand)
ElseIf keyChar = "h" Then
oTextCursor.goLeft(1, bExpand)
' oTextCursor.goUp and oTextCursor.goDown SHOULD work, but doesn't (I dont know why).
' So this is a weird hack
ElseIf keyChar = "k" Then
'oTextCursor.goUp(1, False)
getCursor().goUp(1, bExpand)
bSetCursor = False
ElseIf keyChar = "j" Then
'oTextCursor.goDown(1, False)
getCursor().goDown(1, bExpand)
bSetCursor = False
' ----------
ElseIf keyChar = "^" Then
getCursor().gotoStartOfLine(bExpand)
bSetCursor = False
ElseIf keyChar = "$" Then
dim oldPos, newPos
oldPos = getCursor().getPosition()
getCursor().gotoEndOfLine(bExpand)
newPos = getCursor().getPosition()
' If the result is at the start of the line, then it must have
' jumped down a line; goLeft to return to the previous line.
' Except for: Empty lines (check for oldPos = newPos)
If getCursor().isAtStartOfLine() And oldPos.Y() <> newPos.Y() Then
getCursor().goLeft(1, bExpand)
End If
' maybe eventually cursorGoto... should return True/False for bsetCursor
bSetCursor = False
ElseIf keyChar = "w" or keyChar = "W" Then
oTextCursor.gotoNextWord(bExpand)
ElseIf keyChar = "b" or keyChar = "B" Then
oTextCursor.gotoPreviousWord(bExpand)
ElseIf keyChar = "e" Then
oTextCursor.gotoEndOfWord(bExpand)
ElseIf keyChar = ")" Then
oTextCursor.gotoNextSentence(bExpand)
ElseIf keyChar = "(" Then
oTextCursor.gotoPreviousSentence(bExpand)
ElseIf keyChar = "}" Then
oTextCursor.gotoNextParagraph(bExpand)
ElseIf keyChar = "{" Then
oTextCursor.gotoPreviousParagraph(bExpand)
Else
bSetCursor = False
bMatched = False
End If
' If oTextCursor was moved, set global cursor to its position
If bSetCursor Then
getCursor().gotoRange(oTextCursor.getStart(), False)
' ---- REALLY BAD HACK
' I can't seem to get the View Cursor (getCursor()) to update its
' position without calling its own movement functions.
' Theoretically, the above call to gotoRange should work, but I don't
' know why it doesn't. Visually it works, but its X position is reset
' when you move lines. Bug??
dim oTempPos
oTempPos = getCursor().getPosition()
' Move left 1 and then right 1 to stay in same position
getCursor().goLeft(1, False)
If Not samePos(oTempPos, getCursor().getPosition()) Then
getCursor().goRight(1, False)
End If
End If
' If oTextCursor was moved and is in VISUAL mode, update selection
if bSetCursor and bExpand then
thisComponent.getCurrentController.Select(oTextCursor)
end if