weechat/python/autoload/editor.py @ bfd9b1f910a3
Update
author |
Steve Losh <steve@stevelosh.com> |
date |
Wed, 11 Oct 2017 11:09:36 -0400 |
parents |
(none) |
children |
f433f96e0b21 |
import subprocess
import os
import tempfile
SCRIPT_NAME = 'editor'
SCRIPT_AUTHOR = 'Steve Losh <steve@stevelosh.com>'
SCRIPT_VERSION = '1.0'
SCRIPT_LICENSE = 'MIT/X11'
SCRIPT_DESC = 'Launch an external editor to compose a message'
SCRIPT_COMMAND = 'editor'
import_ok = True
EDITOR = os.environ.get('EDITOR','vim')
try:
import weechat
except ImportError:
print 'This is a weechat script, what are you doing, run it in weechat, jesus'
import_ok = False
weechat_version = 0
def get_data(initial_data):
with tempfile.NamedTemporaryFile(suffix=".tmp", mode="w+") as tf:
tf.write(initial_data)
tf.flush()
if subprocess.call([EDITOR, tf.name]) != 0:
return None
# Reopen, because most editors do atomic write-tmp+rename saves which
# fucks with Python here.
tf.file.close()
tf = file(tf.name)
return tf.readlines()
def editor(data, buffer, args):
line = weechat.buffer_get_string(buffer, "input")
data = get_data(line)
if data:
weechat.command(buffer, "/input delete_line")
for line in data:
weechat.command(buffer, line)
weechat.command("", "/window refresh")
return weechat.WEECHAT_RC_OK
if __name__ == '__main__' and import_ok:
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
weechat_version = weechat.info_get('version_number', '') or 0
weechat.hook_command(
SCRIPT_COMMAND,
'Open $EDITOR to compose a message',
'',
'',
'',
'editor',
'')