diff options
author | bptato <nincsnevem662@gmail.com> | 2023-09-09 01:44:58 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-09-09 01:52:24 +0200 |
commit | 11219c7d21c8ec7f438d52de063c4ac25f84711d (patch) | |
tree | 36b1308d747cc467dc7dad2713eb2cc546390026 /src/extern/editor.nim | |
parent | 6c435aa6c186c2fe7ec72cee2a5ae2f1ae8b22a7 (diff) | |
download | chawan-11219c7d21c8ec7f438d52de063c4ac25f84711d.tar.gz |
add extern, refactor some term functions
* Add an extern() call. Maybe it should be defined on client. It certainly should accept a dictionary instead of the enum type we use now. Perhaps it should return the error code? I'll leave it undocumented until I figure this out. * Refactor enableRawMode, unblockStdin, etc. so that they operate on the term object instead of global state. * Move editor to a separate folder, and factor out runprocess into a different module.
Diffstat (limited to 'src/extern/editor.nim')
-rw-r--r-- | src/extern/editor.nim | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/extern/editor.nim b/src/extern/editor.nim new file mode 100644 index 00000000..58f3d199 --- /dev/null +++ b/src/extern/editor.nim @@ -0,0 +1,57 @@ +import os + +import config/config +import display/term +import extern/runproc +import io/tempfile + +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) + return runProcess(term, cmd) + +proc openInEditor*(term: Terminal, config: Config, input: var string): bool = + try: + let tmpdir = config.external.tmpdir + let tmpf = getTempFile(tmpdir) + 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 |