10b135dc39c9

weechat quote script
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Thu, 18 Sep 2014 11:48:31 -0400
parents 4e2fe1ec0997
children cbcb1ac55566
branches/tags (none)
files weechat/python/autoload/quotes.py

Changes

--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/weechat/python/autoload/quotes.py	Thu Sep 18 11:48:31 2014 -0400
@@ -0,0 +1,92 @@
+import subprocess
+
+SCRIPT_NAME = 'quotes'
+SCRIPT_AUTHOR = 'Steve Losh <steve@stevelosh.com>'
+SCRIPT_VERSION = '1.0'
+SCRIPT_LICENSE = 'MIT/X11'
+SCRIPT_DESC = 'Grab quotes and shove them into a text file.'
+SCRIPT_COMMAND = 'quo'
+
+QUOTE_FILE = '/Users/sjl/Dropbox/quotes.txt'
+
+import_ok = True
+
+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 hd(fn, name, obj, attr):
+    return fn(weechat.hdata_get(name), obj, attr)
+
+def hdp(name, obj, attr):
+    return hd(weechat.hdata_pointer, name, obj, attr)
+
+def hds(name, obj, attr):
+    return hd(weechat.hdata_string, name, obj, attr)
+
+def hdgs(name, obj, attr):
+    return hd(weechat.hdata_get_string, name, obj, attr)
+
+def get_lines(buffer):
+    lines = hdp("buffer", buffer, "own_lines")
+    if not lines:
+        # null pointer wat do
+        return None
+
+    last_lines = []
+
+    line = hdp("lines", lines, "last_line")
+    for _ in range(15):
+        if not line:
+            # shit we're at the top of the buffer
+            break
+
+        data = hdp("line", line, "data")
+        msg = hds("line_data", data, "message")
+        pre = hds("line_data", data, "prefix")
+
+        msg = weechat.string_remove_color(msg, "")
+        pre = weechat.string_remove_color(pre, "")
+
+        last_lines.append("<%s> %s" % (pre.strip(), msg.strip()))
+
+        line = hdp("line", line, "prev_line")
+
+    last_lines.reverse()
+    return last_lines
+
+def quote_grab(data, buffer, args):
+    lines = get_lines(buffer)
+
+    with open(QUOTE_FILE, 'a') as f:
+        f.write("\n---\n")
+        f.write('\n'.join(lines))
+
+    subprocess.call(["vim", QUOTE_FILE,
+                     # start at the bottom of the file
+                     "+",
+                     # move up N lines, where N is how many we appended
+                     "-c", "normal! %dk" % len(lines)])
+    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,
+            'Appends the last 15 lines of the current buffer to your quotes '
+            'file and opens it in Vim so you can trim it.',
+            '',
+            '',
+            '',
+            'quote_grab',
+            '')
+        weechat.hook_command_run('/input complete*', 'completer', '')
+