-
Notifications
You must be signed in to change notification settings - Fork 12
/
prompt.nim
351 lines (336 loc) · 10.1 KB
/
prompt.nim
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
proc singleQuoted(s: string): string =
if find(s, {'\L', '\C'}) >= 0:
result = escape(s)
elif find(s, {' ', '\t'}) >= 0:
result = '\'' & s.replace("'", "''") & '\''
else:
result = s
proc openCmd(ed: Editor) =
when defined(windows):
if ed.prompt.fullText == "o ":
let previousLocation =
if ed.main.filename.len > 0: ed.main.filename.splitFile.dir
else: ""
let toOpen = chooseFilesToOpen(nil, previousLocation)
for p in toOpen:
ed.openTab(p)
focus = ed.main
return
let prompt = ed.prompt
focus = prompt
prompt.clear()
prompt.insert "o "
const
saveChanges = "Closing tab: Save changes? [yes|no|abort]"
askForReplace = "Replace? [yes|no|abort|all]"
proc askForQuitTab(ed: Editor) =
let prompt = ed.prompt
prompt.clear()
ed.sh.statusMsg = saveChanges
focus = prompt
proc findAll(ed: Editor; searchPhrase: string; searchOptions: SearchOptions;
toReplaceWith: string = "") =
for it in allBuffers(ed):
it.findNext(searchPhrase, searchOptions, toReplaceWith)
if onlyCurrentFile in searchOptions: break
it.activeMarker = 0
proc gotoFirstMarker(ed: Editor; stayInFile: bool): bool =
for b in allBuffers(ed):
if b.activeMarker < b.markers.len:
gotoPos(b, b.markers[b.activeMarker].b+1)
result = true
break
elif stayInFile:
break
proc gotoNextMarker(ed: Editor; stayInFile: bool) =
var b = ed.main
inc b.activeMarker
if b.activeMarker >= b.markers.len:
b.activeMarker = 0
if not stayInFile:
let start = b
while true:
b = b.next
if b == start: break
if b.activeMarker < b.markers.len:
ed.main = b
break
if b.activeMarker < b.markers.len:
gotoPos(b, b.markers[b.activeMarker].b+1)
proc gotoPrevMarker(ed: Editor; stayInFile: bool) =
var b = ed.main
dec b.activeMarker
if b.activeMarker < 0:
b.activeMarker = b.markers.high
if not stayInFile:
let start = b
while true:
b = b.prev
if b == start: break
if b.activeMarker < b.markers.len:
ed.main = b
break
if b.activeMarker < b.markers.len:
gotoPos(b, b.markers[b.activeMarker].b+1)
proc smartOpen(ed: Editor; p: var string): bool {.discardable.} =
if p.len > 0:
if not ed.openTab(p, true):
# don't give up, do "what I mean":
p = findFileAbbrev(ed, p)
if p.len > 0:
result = ed.openTab(p, true)
else:
result = true
proc unmark(ed: Editor) =
ed.sh.focus = ed.main
ed.sh.state = requestedNothing
ed.sh.statusMsg = readyMsg
for x in allBuffers(ed):
x.markers.setLen 0
proc runCmd(ed: Editor; cmd: string; shiftPressed: bool): bool =
let prompt = ed.prompt
let sh = ed.sh
ed.promptCon.hist[""].addCmd(cmd)
template success() =
prompt.clear()
sh.focus = ed.main
var action = ""
var i = parseWord(cmd, action, 0, true)
case action
of "": focus = ed.main
of "exec", "e":
var procName = ""
i = parseWord(cmd, procName, i)
if procname.len > 0:
if supportsAction(procName):
let x = runTransformator(procName, ed.main.getSelectedText())
if not x.len == 0:
inc ed.main.version
ed.main.removeSelectedText()
dec ed.main.version
ed.main.insert(x)
else:
sh.statusMsg = "Unknown command: " & procname
success()
sh.state = requestedNothing
of "yes", "y":
case sh.state
of requestedShutdown, requestedCloseTab:
ed.main.save()
ed.removeBuffer(ed.main)
success()
sh.statusMsg = readyMsg
sh.state = if sh.state==requestedShutdown: requestedShutdownNext
else: requestedNothing
of requestedReplace:
if ed.main.doReplace():
ed.gotoNextMarker(onlyCurrentFile in sh.searchOptions)
else:
sh.statusMsg = readyMsg
sh.state = requestedNothing
focus = ed.main
of requestedReload:
loadFromFile(ed.main, ed.main.filename)
success()
sh.statusMsg = readyMsg
else: discard
of "no", "n":
case sh.state
of requestedShutdown, requestedCloseTab:
ed.main.changed = false
ed.removeBuffer(ed.main)
success()
sh.statusMsg = readyMsg
sh.state = if sh.state==requestedShutdown: requestedShutdownNext
else: requestedNothing
of requestedReplace:
ed.gotoNextMarker(onlyCurrentFile in sh.searchOptions)
of requestedReload:
sh.state = requestedNothing
sh.statusMsg = readyMsg
success()
else: discard
of "abort", "a":
success()
sh.statusMsg = readyMsg
sh.state = requestedNothing
of "all":
if sh.state == requestedReplace:
ed.main.activeMarker = 0
while ed.main.doReplace():
ed.gotoNextMarker(onlyCurrentFile in sh.searchOptions)
sh.statusMsg = readyMsg
ed.prompt.clear()
focus = ed.main
sh.state = requestedNothing
of "quit", "q": result = true
of "find", "findall", "f", "filter":
var searchPhrase = ""
i = parseWord(cmd, searchPhrase, i)
if searchPhrase.len > 0:
var searchOptions = ""
i = parseWord(cmd, searchOptions, i)
sh.searchOptions = parseSearchOptions searchOptions
if action != "findall": sh.searchOptions.incl onlyCurrentFile
ed.findAll(searchPhrase, sh.searchOptions)
if ed.gotoFirstMarker(onlyCurrentFile in sh.searchOptions):
ed.prompt.clear()
if action == "filter":
filterOccurances(ed.main)
focus = ed.main
else:
ed.prompt.insert("next")
ed.prompt.selected.a = 0
ed.prompt.selected.b = len"next" - 1
else:
sh.statusMsg = "Match not found."
else:
unmark(ed)
if action == "filter":
ed.main.filterLines = false
of "next":
if not shiftPressed:
ed.gotoNextMarker(onlyCurrentFile in sh.searchOptions)
else:
ed.gotoPrevMarker(onlyCurrentFile in sh.searchOptions)
of "prev", "v":
if not shiftPressed:
ed.gotoPrevMarker(onlyCurrentFile in sh.searchOptions)
else:
ed.gotoNextMarker(onlyCurrentFile in sh.searchOptions)
of "replace", "r", "replaceall":
var searchPhrase = ""
i = parseWord(cmd, searchPhrase, i)
if searchPhrase.len > 0:
var toReplaceWith = ""
i = parseWord(cmd, toReplaceWith, i)
var searchOptions = ""
i = parseWord(cmd, searchOptions, i)
sh.searchOptions = parseSearchOptions searchOptions
if action != "replaceall": sh.searchOptions.incl onlyCurrentFile
ed.findAll(searchPhrase, sh.searchOptions)
ed.main.findNext(searchPhrase, sh.searchOptions,
toReplaceWith)
if ed.gotoFirstMarker(onlyCurrentFile in sh.searchOptions):
ed.prompt.clear()
sh.state = requestedReplace
sh.statusMsg = askForReplace
else:
sh.statusMsg = "Match not found."
else:
unmark(ed)
of "goto", "g":
var dest = ""
i = parseWord(cmd, dest, i, true)
if dest.len > 0:
var p = ""
i = parseWord(cmd, p, i)
if smartOpen(ed, p):
var lineAsInt = -1
discard parseutils.parseInt(dest, lineAsInt)
if lineAsInt >= 0:
var col = -1
i = parseWord(cmd, dest, i, true)
discard parseutils.parseInt(dest, col)
ed.main.gotoLine(lineAsInt, col)
else:
# search for declaration of this identifier:
ed.main.filterMinimap()
lineAsInt = gotoNextDeclaration(ed.main, dest)
if lineAsInt > 0:
ed.main.gotoLine(lineAsInt, -1)
focus = ed.main
prompt.clear()
of "save", "s":
var p = ""
i = parseWord(cmd, p, i)
if not p.isAbsolute:
if ed.main.filename.len == 0:
p = os.getCurrentDir() / p
else:
p = ed.main.filename.splitFile.dir / p
if p.len > 0:
sh.statusMsg = readyMsg
var answer = ""
i = parseWord(cmd, answer, i, true)
if cmpPaths(ed.main.filename, p) == 0 or
not os.fileExists(p) or answer[0] == 'y':
ed.main.saveAs(p)
try:
ed.main.filename = expandFilename(p)
except OSError:
discard
success()
elif answer[0] == 'n':
success()
else:
sh.statusMsg = "File already exists. Overwrite? [yes|no]"
ed.prompt.insert(" no")
else:
ed.main.save()
success()
of "open", "o":
var p = ""
i = parseWord(cmd, p, i)
smartOpen(ed, p)
success()
of "lang":
var lang = ""
i = parseWord(cmd, lang, i)
ed.main.lang = getSourceLanguage(lang)
highlightEverything(ed.main)
success()
of "config", "conf", "cfg", "colors":
openTab(ed, sh.cfgColors.string, true)
success()
of "script", "scripts", "actions":
openTab(ed, sh.cfgActions.string, true)
success()
of "cr":
ed.main.lineending = "\C"
success()
of "lf":
ed.main.lineending = "\L"
success()
of "crlf":
ed.main.lineending = "\C\L"
success()
of "tab", "tabsize", "tabs":
var x = ""
i = parseWord(cmd, x, i)
var xx: int
discard parseutils.parseInt(x, xx)
if xx > 0 and xx <= 127:
ed.main.tabSize = xx.int8
success()
of "setproject", "proj", "project":
sh.project = ""
i = parseWord(cmd, sh.project, i)
if sh.project.len == 0:
sh.setTitle(windowTitle)
else:
let p = findFile(ed, sh.project.addFileExt("nim"))
if p.len != 0: sh.project = p
sh.setTitle(windowTitle & " - " & sh.project.extractFilename)
success()
of "nimsug", "nimsuggest", "sug":
var a = ""
i = parseWord(cmd, a, i, true)
case a
of "shutdown", "stop", "quit", "halt", "exit":
nimsuggestclient.shutdown()
of "restart", "start":
if not startup(sh.theme.nimsuggestPath, sh.project, sh.nimsuggestDebug):
sh.statusMsg = "Nimsuggest failed for: " & sh.project
of "debug":
var onoff = ""
i = parseWord(cmd, onoff, i, true)
sh.nimsuggestDebug = onoff != "off"
else:
sh.statusMsg = "wrong command, try: start|stop|debug"
success()
of "help":
openDefaultBrowser getAppDir() / "docs.html"
success()
else:
sh.statusMsg = "wrong command, try: help|open|save|find|..."