aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpoljar <poljar@termina.org.uk>2019-03-02 20:44:45 +0100
committerKeith Smiley <keithbsmiley@gmail.com>2019-03-02 11:44:45 -0800
commitc1f6966d32f8c54a480fa60b20eb9e82c4a16a33 (patch)
tree5641da5daf0383f5761672931561fb6c495fc39e
parentd9cd83f6ca08e9ed91ca0cdafd23a5ecd671bae9 (diff)
downloadedit-c1f6966d32f8c54a480fa60b20eb9e82c4a16a33.tar.gz
edit-c1f6966d32f8c54a480fa60b20eb9e82c4a16a33.zip
Allow the editor to be opened in a separate terminal. (#7)
This avoids blocking weechat while editing a message.
-rw-r--r--edit.py96
1 files changed, 81 insertions, 15 deletions
diff --git a/edit.py b/edit.py
index 71333dc..ca6ab3f 100644
--- a/edit.py
+++ b/edit.py
@@ -5,9 +5,12 @@
#
# Optional settings:
# /set plugins.var.python.edit.editor "vim -f"
+# /set plugins.var.python.edit.terminal "xterm"
+# /set plugins.var.python.edit.run_externally "false"
#
# History:
# 10-18-2015
+# Version 1.0.2: Add the ability to run the editor in a external terminal
# Version 1.0.1: Add configurable editor key
# Version 1.0.0: initial release
@@ -22,29 +25,91 @@ def weechat_config_dir():
return os.path.expanduser(os.environ.get("WEECHAT_HOME", "~/.weechat/"))
-def edit(data, buf, args):
- editor = (weechat.config_get_plugin("editor") or
- os.environ.get("EDITOR", "vim -f"))
- config_dir = weechat_config_dir()
- path = os.path.join(config_dir, "message.txt")
- with open(path, "w+") as f:
- f.write(weechat.buffer_get_string(buf, "input"))
+PATH = os.path.join(weechat_config_dir(), "message.txt")
- cmd = shlex.split(editor) + [path]
- code = subprocess.Popen(cmd).wait()
- if code != 0:
- os.remove(path)
- weechat.command(buf, "/window refresh")
+
+def editor_process_cb(data, command, return_code, out, err):
+ buf = data
+
+ if return_code != 0:
+ cleanup(PATH, buf)
+ weechat.prnt("", "{}: {}".format(
+ err.strip(),
+ return_code
+ ))
return weechat.WEECHAT_RC_ERROR
- with open(path) as f:
- text = f.read()
+ if return_code == 0:
+ read_file(PATH, buf)
+ cleanup(PATH, buf)
+
+ return weechat.WEECHAT_RC_OK
+
+
+def cleanup(path, buf):
+ try:
+ os.remove(path)
+ except (OSError, IOError):
+ pass
+
+ weechat.command(buf, "/window refresh")
+
+
+def read_file(path, buf):
+ try:
+ with open(PATH) as f:
+ text = f.read()
+
weechat.buffer_set(buf, "input", text)
weechat.buffer_set(buf, "input_pos", str(len(text)))
- os.remove(path)
+ except (OSError, IOError):
+ pass
+
weechat.command(buf, "/window refresh")
+
+def hook_editor_process(terminal, editor, path, buf):
+ term_cmd = "{} -e".format(terminal)
+ editor_cmd = "{} {}".format(editor, path)
+ weechat.hook_process("{} \"{}\"".format(
+ term_cmd,
+ editor_cmd
+ ), 0, "editor_process_cb", buf)
+
+
+def run_blocking(editor, path, buf):
+ cmd = shlex.split(editor) + [path]
+ code = subprocess.Popen(cmd).wait()
+
+ if code != 0:
+ cleanup(path, buf)
+
+ read_file(path, buf)
+
+
+def edit(data, buf, args):
+ editor = (weechat.config_get_plugin("editor")
+ or os.environ.get("EDITOR", "vim -f"))
+
+ terminal = (weechat.config_get_plugin("terminal")
+ or os.getenv("TERMCMD"))
+
+ terminal = terminal or "xterm"
+
+ run_externally = weechat.config_string_to_boolean(
+ weechat.config_get_plugin("run_externally")
+ )
+ run_externally = bool(run_externally)
+
+ with open(PATH, "w+") as f:
+ f.write(weechat.buffer_get_string(buf, "input"))
+
+ if run_externally:
+ hook_editor_process(terminal, editor, PATH, buf)
+ else:
+ run_blocking(editor, PATH, buf)
+
return weechat.WEECHAT_RC_OK
@@ -56,5 +121,6 @@ def main():
weechat.hook_command("edit", "Open your $EDITOR to compose a message", "",
"", "", "edit", "")
+
if __name__ == "__main__":
main()