-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVIM.bas
More file actions
206 lines (196 loc) · 5.81 KB
/
VIM.bas
File metadata and controls
206 lines (196 loc) · 5.81 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
Attribute VB_Name = "VIM"
Option Explicit
Declare PtrSafe Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
'Declare PtrSafe Function GetKeyState Lib "User32.dll" (ByVal vKey As Long) As Long
Const KEYUP = &H2
Const RIGHT = arr(0, 1)
Global tail_cell As Range
Global current_mode As Integer
Enum mode
normal_mode = 0
insert_mode = 1
visual_mode = 2
End Enum
Function const_mode_label()
const_mode_label = Array("-- NORMAL --", "-- INSERT --", "-- VISUAL --")
End Function
Sub Auto_Open()
current_mode = mode.normal_mode
Call CreateShortcut
Call NShortcuts
End Sub
Public Sub toggle_v_mode()
If current_mode <> mode.visual_mode Then
Set tail_cell = ActiveCell
current_mode = mode.visual_mode
Call VShortcuts
Call expand_selection
Call statusbar_change_mode
Else
current_mode = mode.normal_mode
Call NShortcuts
Call statusbar_change_mode
End If
End Sub
Sub expand_selection()
Dim active_cell As Range
Set active_cell = ActiveCell
Range(active_cell, tail_cell).Select 'with that active cell change position
active_cell.Activate 'activate initial cell again
End Sub
Sub toggle_i_mode()
On Error GoTo ErrorHandler
keybd_event vbKeyF2, 0, 0, 0
keybd_event vbKeyF2, 0, KEYUP, 0
current_mode = mode.insert_mode
Call NShortcuts
Call statusbar_change_mode ' TODO change back no NORMAL with Esc and Ctrl+[
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub NShortcuts()
' movement
Application.OnKey "{h}", "move_left"
Application.OnKey "{l}", "move_right"
Application.OnKey "{j}", "move_down"
Application.OnKey "{k}", "move_up"
End Sub
Sub VShortcuts()
' movement
Application.OnKey "{h}", "move_left_tail"
Application.OnKey "{l}", "move_right_tail"
Application.OnKey "{j}", "move_down_tail"
Application.OnKey "{k}", "move_up_tail"
End Sub
Sub CreateShortcut()
Application.OnKey "{i}", "toggle_i_mode"
Application.OnKey "{v}", "toggle_v_mode"
' increment
Application.OnKey "^{a}", "C_a"
' append bottom value
Application.OnKey "{J}", "J"
End Sub
Sub DeleteShortcut()
'Application.OnKey "{i}"
End Sub
Sub move_right()
On Error GoTo ErrorHandler
ActiveCell.Offset(0, 1).Activate
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub move_left()
On Error GoTo ErrorHandler
ActiveCell.Offset(0, -1).Activate
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub move_up()
On Error GoTo ErrorHandler
ActiveCell.Offset(-1, 0).Activate
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub move_down()
On Error GoTo ErrorHandler
ActiveCell.Offset(1, 0).Activate
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub move_right_tail()
On Error GoTo ErrorHandler
Set tail_cell = tail_cell.Offset(0, 1)
Call expand_selection
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub move_left_tail()
On Error GoTo ErrorHandler
Set tail_cell = tail_cell.Offset(0, -1)
Call expand_selection
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub move_up_tail()
On Error GoTo ErrorHandler
Set tail_cell = tail_cell.Offset(-1, 0)
Call expand_selection
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub move_down_tail()
On Error GoTo ErrorHandler
Set tail_cell = tail_cell.Offset(1, 0)
Call expand_selection
Exit Sub
ErrorHandler:
Debug.Print Err.Number & vbNewLine & Err.Description
End Sub
Sub C_a()
' increment action
' get selection string
' cut rightmost number
' number+=1
' glue number back
' aaa1a -> aaa2a
' a3aa9a -> a3aa10a
' -00003 -> -00004
' -1 -> 0
' -5 -> -4
' -100 -> -99 [fail] now -099
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
Dim matchobj ' as
Dim replace_str As String
With regex
.Pattern = "((-(?!0))?\d+)(?!.*\d)"
.Global = True
.MultiLine = False
End With
Set matchobj = regex.Execute(ActiveCell.Value)
'matchobj.count
'Dim mtc As Match
'Set mtc = matchobj(0)
If regex.test(ActiveCell.Value) Then
replace_str = matchobj(0).Value
End If
Dim digits As Integer
digits = Len(replace_str)
' if negative number cut "-" position from "000" text format
If InStr(1, replace_str, "-") = 1 Then
digits = digits - 1
End If
If regex.test(ActiveCell.Value) Then
Dim fmt As String
fmt = String$(digits, "0")
Dim return_int As Integer
return_int = CInt(replace_str) + 1 'TODO -1 for C_x
Dim return_str As String
return_str = Format(CStr(return_int), fmt)
ActiveCell.Value = regex.Replace(ActiveCell.Value, return_str)
End If
End Sub
Sub J()
' activecell value += bottomcell value
' clear bottomcell value
If ActiveCell.Offset(1, 0).Value <> "" Then
If ActiveCell.Value <> "" Then
ActiveCell.Value = Join(Array(ActiveCell.Value, ActiveCell.Offset(1, 0).Value))
Else
ActiveCell.Value = ActiveCell.Offset(1, 0).Value
End If
ActiveCell.Offset(1, 0).ClearContents
End If
End Sub
Function statusbar_change_mode()
Application.DisplayStatusBar = True
Application.statusbar = const_mode_label(current_mode)
End Function