blob: 0e2c91c8a7e9b7cab8150a69a037ef60c10df7eb (
plain) (
blame)
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
|
import os
import config/config
import display/term
func formatEditorName(editor, file: string, line: int): string =
result = newStringOfCap(editor.len + file.len)
var i = 0
var filefound = false
while i < editor.len:
if editor[i] == '%' and i < editor.high:
if editor[i + 1] == 's':
result &= file
filefound = true
i += 2
continue
elif editor[i + 1] == 'd':
result &= $line
i += 2
continue
elif editor[i + 1] == '%':
result &= '%'
i += 2
continue
result &= editor[i]
inc i
if not filefound:
if result[^1] != ' ':
result &= ' '
result &= file
proc openEditor*(term: Terminal, config: Config, file: string, line = 1): bool =
var editor = config.external.editor
if editor == "":
editor = getEnv("EDITOR")
if editor == "":
editor = "vi %s +%d"
let cmd = formatEditorName(editor, file, line)
term.quit()
result = execShellCmd(cmd) == 0
term.restart()
var tmpf_seq: int
proc openInEditor*(term: Terminal, config: Config, input: var string): bool =
try:
let tmpdir = config.external.tmpdir
if not dirExists(tmpdir):
createDir(tmpdir)
var tmpf = tmpdir / "chatmp" & $tmpf_seq
while fileExists(tmpf):
inc tmpf_seq
tmpf = tmpdir / "chatmp" & $tmpf_seq
inc tmpf_seq
if input != "":
writeFile(tmpf, input)
if openEditor(term, config, tmpf):
if fileExists(tmpf):
input = readFile(tmpf)
removeFile(tmpf)
return true
else:
return false
except IOError:
discard
|