aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancisco Lopes <francisco@nosubstance.me>2020-11-12 21:55:59 -0300
committerFrancisco Lopes <francisco@nosubstance.me>2020-11-12 22:34:41 -0300
commit3cf2b087559f3d9778bb30883968eaa8fb8f9e38 (patch)
tree26206fd4c973adf30c762ad310ce9d9906e98249
parentc1f6966d32f8c54a480fa60b20eb9e82c4a16a33 (diff)
downloadedit-3cf2b087559f3d9778bb30883968eaa8fb8f9e38.tar.gz
edit-3cf2b087559f3d9778bb30883968eaa8fb8f9e38.zip
Add file type and code fences support
Also fix inconsistency on usage of global variables
-rw-r--r--README.md25
-rw-r--r--edit.py68
2 files changed, 63 insertions, 30 deletions
diff --git a/README.md b/README.md
index 4318c52..3cb64df 100644
--- a/README.md
+++ b/README.md
@@ -1,15 +1,28 @@
# edit-weechat
This simple [weechat](https://weechat.org/) plugin allows you to
-compose messages in your `$EDITOR`.
+compose messages in your `$EDITOR`, optionally with a file type.
# Usage
-```sh
-/edit
-# Type some stuff
-# Save and quit
-```
+- Markdown message (it's the default, same as `/edit md`)
+ ```sh
+ /edit
+ # Type some stuff
+ # Save and quit
+ ```
+- Plain text message
+ ```sh
+ /edit txt
+ # Type some stuff
+ # Save and quit
+ ```
+- Code message with fences added automatically
+ ```sh
+ /fenced cpp
+ # Type some code
+ # Save and quit
+ ```
# Configuration
diff --git a/edit.py b/edit.py
index ca6ab3f..14fbaaa 100644
--- a/edit.py
+++ b/edit.py
@@ -1,7 +1,8 @@
# Open your $EDITOR to compose a message in weechat
#
# Usage:
-# /edit
+# /edit [extension]
+# /fenced [extension]
#
# Optional settings:
# /set plugins.var.python.edit.editor "vim -f"
@@ -21,18 +22,19 @@ import subprocess
import weechat
-def weechat_config_dir():
- return os.path.expanduser(os.environ.get("WEECHAT_HOME", "~/.weechat/"))
+FILE = ""
+FENCED = False
-PATH = os.path.join(weechat_config_dir(), "message.txt")
+def weechat_config_dir():
+ return os.path.expanduser(os.environ.get("WEECHAT_HOME", "~/.weechat/"))
def editor_process_cb(data, command, return_code, out, err):
buf = data
if return_code != 0:
- cleanup(PATH, buf)
+ cleanup(buf)
weechat.prnt("", "{}: {}".format(
err.strip(),
return_code
@@ -40,26 +42,27 @@ def editor_process_cb(data, command, return_code, out, err):
return weechat.WEECHAT_RC_ERROR
if return_code == 0:
- read_file(PATH, buf)
- cleanup(PATH, buf)
+ read_file(buf)
+ cleanup(buf)
return weechat.WEECHAT_RC_OK
-def cleanup(path, buf):
+def cleanup(buf):
try:
- os.remove(path)
+ os.remove(FILE)
except (OSError, IOError):
pass
weechat.command(buf, "/window refresh")
-def read_file(path, buf):
+def read_file(buf):
try:
- with open(PATH) as f:
+ with open(FILE) as f:
text = f.read()
-
+ if FENCED:
+ text = "```\n" + text.strip() + "\n```"
weechat.buffer_set(buf, "input", text)
weechat.buffer_set(buf, "input_pos", str(len(text)))
@@ -69,26 +72,26 @@ def read_file(path, buf):
weechat.command(buf, "/window refresh")
-def hook_editor_process(terminal, editor, path, buf):
+def hook_editor_process(terminal, editor, buf):
term_cmd = "{} -e".format(terminal)
- editor_cmd = "{} {}".format(editor, path)
+ editor_cmd = "{} {}".format(editor, FILE)
weechat.hook_process("{} \"{}\"".format(
term_cmd,
editor_cmd
), 0, "editor_process_cb", buf)
-def run_blocking(editor, path, buf):
- cmd = shlex.split(editor) + [path]
+def run_blocking(editor, buf):
+ cmd = shlex.split(editor) + [FILE]
code = subprocess.Popen(cmd).wait()
if code != 0:
- cleanup(path, buf)
+ cleanup(buf)
- read_file(path, buf)
+ read_file(buf)
-def edit(data, buf, args):
+def edit(data, buf, args, fenced=False):
editor = (weechat.config_get_plugin("editor")
or os.environ.get("EDITOR", "vim -f"))
@@ -102,24 +105,41 @@ def edit(data, buf, args):
)
run_externally = bool(run_externally)
- with open(PATH, "w+") as f:
+ global FILE, FENCED
+
+ FILE = os.path.join(weechat_config_dir(), "message." + ("md" if not args else args))
+
+ FENCED = fenced
+
+ with open(FILE, "w+") as f:
f.write(weechat.buffer_get_string(buf, "input"))
if run_externally:
- hook_editor_process(terminal, editor, PATH, buf)
+ hook_editor_process(terminal, editor, buf)
else:
- run_blocking(editor, PATH, buf)
+ run_blocking(editor, buf)
return weechat.WEECHAT_RC_OK
+def fenced(data, buf, args):
+ return edit(data, buf, args, fenced=True)
+
+
def main():
if not weechat.register("edit", "Keith Smiley", "1.0.0", "MIT",
"Open your $EDITOR to compose a message", "", ""):
return weechat.WEECHAT_RC_ERROR
- weechat.hook_command("edit", "Open your $EDITOR to compose a message", "",
- "", "", "edit", "")
+ weechat.hook_command("edit", "Open your $EDITOR to compose a message",
+ "[extension]",
+ "extension: extension for temporary composing file",
+ "extension", "edit", "")
+ weechat.hook_command("fenced", "Open your $EDITOR to compose a message"
+ " with automatic code fences",
+ "[extension]",
+ "extension: extension for temporary composing file",
+ "extension", "fenced", "")
if __name__ == "__main__":