-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathemojis.py
236 lines (182 loc) · 6.99 KB
/
emojis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# !/usr/bin/env python
# Copyright (c) 2013 Alexandre Gauthier
#
# Some rights reserved.
#
# Redistribution and use in source and binary forms of the software as well
# as documentation, with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
import os
import re
import sys
# I'm sorry.
try:
import weechat
except ImportError:
print "This is a WeeChat Script - http://www.weechat.org"
print "It makes no sense to run it on its own."
sys.exit(1)
# Global / Static script values
SCRIPT_NAME = "emojis"
SCRIPT_AUTHOR = "Alexandre Gauthier <[email protected]>"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "BSD"
SCRIPT_DESCRIPTION = "Allows you to spam emojis based on :triggers:"
# Emojis cache
EMOJIS = {}
# Decode required for proper calculation of string
# length, and to avoid some bugs when concatenating
# unicode/normal strings.
#
# FIXME: I don't like that this is a thing. Weechat supposedly uses
# UTF-8 internally exclusively, i'm not sure why we need to transit
# through ascii.
def decode(s):
""" Decode utf-8 to ascii """
if isinstance(s, str):
s = s.decode('utf-8')
return s
def encode(u):
""" Encode ascii as utf-8 """
if isinstance(u, unicode):
u = u.encode('utf-8')
return u
def load_emojis(dbfile):
""" Load emojis from file """
global EMOJIS
with open(dbfile) as f:
for line in f:
if line.startswith(':'):
EMOJIS[line.rstrip()] = f.next().rstrip()
else:
weechat.prnt(
"",
"%s%s: Malformed line in %s: %s"
% (weechat.prefix("error"), SCRIPT_NAME, f.name, line))
weechat.prnt(
"", "%s: Loaded %d knifaisms."
% (SCRIPT_NAME, len(EMOJIS)))
def reload_emojis():
""" Reload emojis from currently configured database """
global EMOJIS
weechat.prnt(
"", "%s: Reloading emojis from %s"
% (SCRIPT_NAME, weechat.config_get_plugin("dbfile")))
# Clear out emojis before reload.
EMOJIS = {}
load_emojis(weechat.config_get_plugin("dbfile"))
def transform_cb(data, bufferptr, command):
""" Apply transformation to input line in specified buffer """
if command == "/input return":
# Get input line from buffer. This is where we'll apply the
# transformation right when the user hits enter. To be brutally
# honest, I had no idea how else to make it happen.
line = weechat.buffer_get_string(bufferptr, 'input')
# Ignore commands
if line.startswith('/'):
return weechat.WEECHAT_RC_OK
# Apply transforms.
# This could probably be optimized.
for key, value in EMOJIS.iteritems():
if key in line:
line = line.replace(key, value)
# Poot transformed line back into buffer's input line.
weechat.buffer_set(bufferptr, 'input', line)
return weechat.WEECHAT_RC_OK
def complete_cb(data, bufferptr, command):
""" Apply transformation to input line in specified buffer """
if command != "/input complete_next":
return weechat.WEECHAT_RC_OK
line = decode(weechat.buffer_get_string(bufferptr, 'input'))
caret_pos = weechat.buffer_get_integer(bufferptr, 'input_pos')
match = re.search('(:\w+$)', line[:caret_pos])
if not match:
return weechat.WEECHAT_RC_OK
# tw = tabbed word
tw = match.group(0)
tw_length = len(tw)
tw_start = caret_pos - tw_length
tw_end = caret_pos
completion = ""
for key, value in EMOJIS.iteritems():
if key.startswith(tw):
completion = decode(value)
break
if completion:
line = line[:tw_start] + completion + line[tw_end:]
new_caret_pos = caret_pos - tw_length + len(completion)
weechat.buffer_set(bufferptr, 'input', encode(line))
weechat.buffer_set(bufferptr, 'input_pos', str(new_caret_pos))
return weechat.WEECHAT_RC_OK
def configuration_cb(data, option, value):
""" Configuration change callback """
weechat.prnt("", "%s: Configuration change detected." % (SCRIPT_NAME))
reload_emojis()
return weechat.WEECHAT_RC_OK
def reload_emojis_cb(data, bufferptr, args):
""" Command callback wrapper around reload_emojis() """
reload_emojis()
return weechat.WEECHAT_RC_OK
def main():
""" Entry point, initializes everything """
weechat.register(
SCRIPT_NAME,
SCRIPT_AUTHOR,
SCRIPT_VERSION,
SCRIPT_LICENSE,
SCRIPT_DESCRIPTION,
"", # Shutdown callback function
"", # Charset (blank for utf-8)
)
# Default values for settings
default_settings = {
'dbfile': os.path.join(
weechat.info_get("weechat_dir", ""), "emojis-db.dat")
}
# Apply default configuration values if anything is unset
for option, default in default_settings.items():
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, default)
# Hook callbacks
weechat.hook_config(
"plugins.var.python." + SCRIPT_NAME + ".*",
"configuration_cb", "")
weechat.hook_command_run("/input return", "transform_cb", "")
weechat.hook_command_run("/input complete*", "complete_cb", "")
# Command callbacks
weechat.hook_command(
"reloademojis", "reload emojis from file",
"", "", "", "reload_emojis_cb", "")
dbfile = weechat.config_get_plugin("dbfile")
weechat.prnt("", "%s: Loading emojis from %s" % (SCRIPT_NAME, dbfile))
try:
load_emojis(dbfile)
except IOError as e:
weechat.prnt(
"",
"%s%s: Database file %s is missing or inaccessible."
% (weechat.prefix("error"), SCRIPT_NAME, dbfile))
raise e # TODO: handle this better instead of brutally aborting
if __name__ == '__main__':
main()