From c2b673308f5d8ef7a295d03d1a1fa72b8b947c12 Mon Sep 17 00:00:00 2001 From: Mathieu Pasquet Date: Thu, 28 Nov 2013 21:22:17 +0100 Subject: =?UTF-8?q?Add=20an=20=E2=80=9Cautocorrect=E2=80=9D=20plugin=C2=A0?= =?UTF-8?q?with=20a=20sed-like=20syntax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO (eventually): - correct words when using “word*” or “*word” using levenshtein distance - allow regular expressions for replacement --- doc/source/plugins/autocorrect.rst | 6 ++++ doc/source/plugins/index.rst | 8 +++++ plugins/autocorrect.py | 67 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 doc/source/plugins/autocorrect.rst create mode 100644 plugins/autocorrect.py diff --git a/doc/source/plugins/autocorrect.rst b/doc/source/plugins/autocorrect.rst new file mode 100644 index 00000000..f637678e --- /dev/null +++ b/doc/source/plugins/autocorrect.rst @@ -0,0 +1,6 @@ +.. _autocorrect-plugin: + +Autocorrect +=========== + +.. automodule:: autocorrect diff --git a/doc/source/plugins/index.rst b/doc/source/plugins/index.rst index ce758537..e8307e7e 100644 --- a/doc/source/plugins/index.rst +++ b/doc/source/plugins/index.rst @@ -212,6 +212,13 @@ Plugin index Insert dots in your messages. + Autocorrect + :ref:`Documentation ` + + Add new ways to correct messages. + + + .. toctree:: :hidden: @@ -246,3 +253,4 @@ Plugin index iq_show regex_admin pointpoint + autocorrect diff --git a/plugins/autocorrect.py b/plugins/autocorrect.py new file mode 100644 index 00000000..61795b79 --- /dev/null +++ b/plugins/autocorrect.py @@ -0,0 +1,67 @@ +""" +This plugin lets you perform simple replacements on the last message. + +Installation +------------ + +Load the plugin:: + + /load autocorrect + +Usage +----- + +.. note:: This plugin only performs *simple* replacements, not with + regular expressions, despite the syntax. Although it would be + possible, that would be even less useful. + +Once the plugin is loaded, any message matching the following regex:: + + ^s/(.+?)/(.*?)(/|/g)?$ + +will be interpreted as a replacement, and the substitution will be +applied to the last sent message. + +For example, if you sent the message:: + + This tab lists all public rooms on a MUC service. It is currently very limited but will be improved in the future. There currently is no way to search a room. + +And you now want to replace “MUC” with “multi-user chat”, you input:: + + s/MUC/multi-user chat + +And poezio will correct the message for you. +""" + +from plugin import BasePlugin +import re + +sed_re = re.compile('^s/(.+?)/(.*?)(/|/g)?$') + +class Plugin(BasePlugin): + def init(self): + self.api.add_event_handler('muc_say', self.sed_fix) + self.api.add_event_handler('conversation_say', self.sed_fix) + self.api.add_event_handler('private_say', self.sed_fix) + + def sed_fix(self, msg, tab): + if not tab.last_sent_message: + return + body = tab.last_sent_message['body'] + match = sed_re.match(msg['body']) + if not match: + return + remove, put, matchall = match.groups() + + replace_all = False + if matchall == '/g': + replace_all = True + + if replace_all: + new_body = body.replace(remove, put) + else: + new_body = body.replace(remove, put, 1) + + if body != new_body: + msg['body'] = new_body + msg['replace']['id'] = tab.last_sent_message['id'] -- cgit v1.2.3