Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for external editing #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ environment variable, you can set it in weechat.
/set plugins.var.python.edit.editor "vim -f"
```

In case you want to run editor externally without blocking weechat (since
blocking weechat can break things), you can configure the plugin like this:

```
/set plugins.var.python.edit.editor "gvim -f"
/set plugins.var.python.edit.run_externally "true"
```

You can of course use any editor you want, you can even spawn a terminal and
use terminal vim if you prefer.

# Installation

Copy the script to `~/.weechat/python/autoload`
Expand Down
52 changes: 28 additions & 24 deletions edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,51 @@
#
# 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"
#
# If run_externally, the editor is spawned without blocking weechat. The
# process should not output to the terminal (use GUI program or spawn a new
# terminal). Otherwise the editor is executed in the current terminal (blocking
# weechat, which can break stuff).
#
# History:
# 2022-09-09
# Version 1.0.4: Use temporary files for the message
# Version 1.0.3: Drop terminal option and leave that up to the user
# 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

VERSION = "1.0.4"

import json
import os
import os.path
import shlex
import subprocess
import tempfile
import weechat


def weechat_config_dir():
return os.path.expanduser(os.environ.get("WEECHAT_HOME", "~/.weechat/"))


PATH = os.path.join(weechat_config_dir(), "message.txt")


def editor_process_cb(data, command, return_code, out, err):
buf = data
buf, path = json.loads(data)

if return_code != 0:
cleanup(PATH, buf)
cleanup(path, buf)
weechat.prnt("", "{}: {}".format(
err.strip(),
return_code
))
return weechat.WEECHAT_RC_ERROR

if return_code == 0:
read_file(PATH, buf)
cleanup(PATH, buf)
read_file(path, buf)
cleanup(path, buf)

return weechat.WEECHAT_RC_OK

Expand All @@ -57,7 +65,7 @@ def cleanup(path, buf):

def read_file(path, buf):
try:
with open(PATH) as f:
with open(path) as f:
text = f.read()

weechat.buffer_set(buf, "input", text)
Expand All @@ -69,13 +77,11 @@ def read_file(path, buf):
weechat.command(buf, "/window refresh")


def hook_editor_process(terminal, editor, path, buf):
term_cmd = "{} -e".format(terminal)
def hook_editor_process(editor, path, buf):
editor_cmd = "{} {}".format(editor, path)
weechat.hook_process("{} \"{}\"".format(
term_cmd,
editor_cmd
), 0, "editor_process_cb", buf)
data = json.dumps([buf, path])
weechat.hook_process(
shlex.join(shlex.split(editor) + [path]), 0, "editor_process_cb", data)


def run_blocking(editor, path, buf):
Expand All @@ -92,29 +98,27 @@ 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, path = tempfile.mkstemp(prefix = "weechat-edit-")
os.close(f)

with open(path, "w+") as f:
f.write(weechat.buffer_get_string(buf, "input"))

if run_externally:
hook_editor_process(terminal, editor, PATH, buf)
hook_editor_process(editor, path, buf)
else:
run_blocking(editor, PATH, buf)
run_blocking(editor, path, buf)

return weechat.WEECHAT_RC_OK


def main():
if not weechat.register("edit", "Keith Smiley", "1.0.0", "MIT",
if not weechat.register("edit", "Keith Smiley", VERSION, "MIT",
"Open your $EDITOR to compose a message", "", ""):
return weechat.WEECHAT_RC_ERROR

Expand Down