]> git.rmz.io Git - dotfiles.git/blob - weechat/python/buffer_autoset.py
305bd6b2d24268c1ff17afdf8b791a1008603af7
[dotfiles.git] / weechat / python / buffer_autoset.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2010-2015 Sébastien Helleu <flashcode@flashtux.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 #
20 # Auto-set buffer properties when a buffer is opened.
21 # (this script requires WeeChat 1.0 or newer)
22 #
23 # History:
24 #
25 # 2015-09-28, Simmo Saan <simmo.saan@gmail.com>:
26 # version 0.9: instantly apply properties
27 # 2015-07-12, Sébastien Helleu <flashcode@flashtux.org>:
28 # version 0.8: add option buffer_autoset.look.timer to add a small timer
29 # before setting buffer properties
30 # 2015-04-05, Nils Görs <freenode@#weechat>:
31 # version 0.7: increase priority of hook_signal('buffer_opened')
32 # 2012-12-09, Nils Görs <freenode@#weechat>:
33 # version 0.6: add support of core buffer
34 # 2012-03-09, Sébastien Helleu <flashcode@flashtux.org>:
35 # version 0.5: fix reload of config file
36 # 2012-01-03, Sébastien Helleu <flashcode@flashtux.org>:
37 # version 0.4: make script compatible with Python 3.x
38 # 2010-12-02, Sébastien Helleu <flashcode@flashtux.org>:
39 # version 0.3: "no_highlight_nicks" replaced by "hotlist_max_level_nicks"
40 # 2010-10-11, Sébastien Helleu <flashcode@flashtux.org>:
41 # version 0.2: add example in /help autosetbuffer with new buffer
42 # property "no_highlight_nicks"
43 # 2010-04-19, Sébastien Helleu <flashcode@flashtux.org>:
44 # version 0.1: initial release
45 #
46
47 SCRIPT_NAME = "buffer_autoset"
48 SCRIPT_AUTHOR = "Sébastien Helleu <flashcode@flashtux.org>"
49 SCRIPT_VERSION = "0.9"
50 SCRIPT_LICENSE = "GPL3"
51 SCRIPT_DESC = "Auto-set buffer properties when a buffer is opened"
52
53 SCRIPT_COMMAND = "autosetbuffer"
54
55 import_ok = True
56
57 try:
58 import weechat
59 except ImportError:
60 print("This script must be run under WeeChat.")
61 print("Get WeeChat now at: http://www.weechat.org/")
62 import_ok = False
63
64 CONFIG_FILE_NAME = "buffer_autoset"
65
66 # config file / options
67 bas_config_file = ""
68 bas_options = {}
69
70
71 # =================================[ config ]=================================
72
73 def bas_config_init():
74 """
75 Initialization of configuration file.
76 Sections: buffer.
77 """
78 global bas_config_file, bas_options
79 bas_config_file = weechat.config_new(CONFIG_FILE_NAME,
80 "bas_config_reload_cb", "")
81 if bas_config_file == "":
82 return
83
84 # section "look"
85 section_look = weechat.config_new_section(
86 bas_config_file, "look", 0, 0, "", "", "", "", "", "", "", "", "", "")
87 if not section_look:
88 weechat.config_free(bas_config_file)
89 return
90
91 # options in section "look"
92 bas_options["look_timer"] = weechat.config_new_option(
93 bas_config_file, section_look, "timer", "integer",
94 "Timer used to delay the set of properties (in milliseconds, "
95 "0 = don't use a timer)",
96 "", 0, 2147483647, "1", "1", 0, "", "", "", "", "", "")
97
98 bas_options["look_instant"] = weechat.config_new_option(
99 bas_config_file, section_look, "instant", "boolean",
100 "Instantly apply properties to buffers affected",
101 "", 0, 0, "on", "on", 0, "", "", "", "", "", "")
102
103 # section "buffer"
104 section_buffer = weechat.config_new_section(
105 bas_config_file, "buffer", 1, 1, "", "", "", "", "", "",
106 "bas_config_buffer_create_option_cb", "", "", "")
107 if not section_buffer:
108 weechat.config_free(bas_config_file)
109 return
110
111
112 def bas_config_buffer_create_option_cb(data, config_file, section, option_name,
113 value):
114 option = weechat.config_search_option(config_file, section, option_name)
115 if option:
116 return weechat.config_option_set(option, value, 1)
117 else:
118 option = weechat.config_new_option(config_file, section, option_name,
119 "string", "", "", 0, 0, "",
120 value, 0, "", "", "", "", "", "")
121 if not option:
122 return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR
123 return weechat.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE
124
125
126 def bas_config_reload_cb(data, config_file):
127 """Reload configuration file."""
128 return weechat.config_reload(config_file)
129
130
131 def bas_config_read():
132 """Read configuration file."""
133 global bas_config_file
134 return weechat.config_read(bas_config_file)
135
136
137 def bas_config_write():
138 """Write configuration file."""
139 global bas_config_file
140 return weechat.config_write(bas_config_file)
141
142
143 # ================================[ command ]=================================
144
145 def bas_cmd(data, buffer, args):
146 """Callback for /autosetbuffer command."""
147 args = args.strip()
148 if args == "":
149 weechat.command("", "/set %s.buffer.*" % CONFIG_FILE_NAME)
150 return weechat.WEECHAT_RC_OK
151 argv = args.split(None, 3)
152 if len(argv) > 0:
153 if argv[0] == "add":
154 if len(argv) < 4:
155 weechat.command("", "/help %s" % SCRIPT_COMMAND)
156 return weechat.WEECHAT_RC_OK
157 weechat.command("", "/set %s.buffer.%s.%s \"%s\""
158 % (CONFIG_FILE_NAME, argv[1], argv[2], argv[3]))
159 elif argv[0] == "del":
160 if len(argv) < 2:
161 weechat.command("", "/help %s" % SCRIPT_COMMAND)
162 return weechat.WEECHAT_RC_OK
163 weechat.command("", "/unset %s.buffer.%s"
164 % (CONFIG_FILE_NAME, argv[1]))
165 else:
166 weechat.command("", "/help %s" % SCRIPT_COMMAND)
167 return weechat.WEECHAT_RC_OK
168 return weechat.WEECHAT_RC_OK
169
170
171 def bas_completion_current_buffer_cb(data, completion_item, buffer,
172 completion):
173 """
174 Complete with current buffer name (plugin.name),
175 for command '/autosetbuffer'.
176 """
177 name = "%s.%s" % (weechat.buffer_get_string(buffer, "plugin"),
178 weechat.buffer_get_string(buffer, "name"))
179 weechat.hook_completion_list_add(completion, name,
180 0, weechat.WEECHAT_LIST_POS_BEGINNING)
181 return weechat.WEECHAT_RC_OK
182
183
184 def bas_completion_options_cb(data, completion_item, buffer, completion):
185 """Complete with config options, for command '/autosetbuffer'."""
186 options = weechat.infolist_get("option", "",
187 "%s.buffer.*" % CONFIG_FILE_NAME)
188 if options:
189 while weechat.infolist_next(options):
190 weechat.hook_completion_list_add(
191 completion,
192 weechat.infolist_string(options, "option_name"),
193 0, weechat.WEECHAT_LIST_POS_SORT)
194 weechat.infolist_free(options)
195 return weechat.WEECHAT_RC_OK
196
197
198 # ==========================[ timer/signal/option ]===========================
199
200 def bas_apply_options_for_buffer(buffer):
201 full_name = weechat.buffer_get_string(buffer, "full_name")
202 options = weechat.infolist_get("option", "",
203 "%s.buffer.*" % CONFIG_FILE_NAME)
204 if not options:
205 return
206
207 while weechat.infolist_next(options):
208 option = weechat.infolist_string(options, "option_name")
209 value = weechat.infolist_string(options, "value")
210 if option:
211 pos = option.rfind(".")
212 if pos > 0:
213 buffer_mask = option[0:pos]
214 property = option[pos+1:]
215 if buffer_mask and property:
216 if weechat.string_match(full_name, buffer_mask, 1):
217 weechat.buffer_set(buffer, property, value)
218
219 weechat.infolist_free(options)
220
221
222 def bas_timer_buffer_opened_cb(data, remaining_calls):
223 full_name = data
224 buffer = weechat.buffer_search("==", full_name)
225 if not buffer:
226 return weechat.WEECHAT_RC_OK
227 bas_apply_options_for_buffer(buffer)
228 return weechat.WEECHAT_RC_OK
229
230
231 def bas_signal_buffer_opened_cb(data, signal, signal_data):
232 global bas_options
233 buffer = signal_data
234 timer = weechat.config_integer(bas_options["look_timer"])
235 if timer == 0:
236 bas_apply_options_for_buffer(buffer)
237 else:
238 weechat.hook_timer(timer, 0, 1,
239 "bas_timer_buffer_opened_cb",
240 weechat.buffer_get_string(buffer, "full_name"))
241 return weechat.WEECHAT_RC_OK
242
243 def bas_config_option_cb(data, option, value):
244 if not weechat.config_boolean(bas_options["look_instant"]):
245 return weechat.WEECHAT_RC_OK
246
247 if not weechat.config_get(option): # option was deleted
248 return weechat.WEECHAT_RC_OK
249
250 option = option[len("%s.buffer." % CONFIG_FILE_NAME):]
251
252 pos = option.rfind(".")
253 if pos > 0:
254 buffer_mask = option[0:pos]
255 property = option[pos+1:]
256 if buffer_mask and property:
257 buffers = weechat.infolist_get("buffer", "", buffer_mask)
258
259 if not buffers:
260 return weechat.WEECHAT_RC_OK
261
262 while weechat.infolist_next(buffers):
263 buffer = weechat.infolist_pointer(buffers, "pointer")
264 weechat.buffer_set(buffer, property, value)
265
266 weechat.infolist_free(buffers)
267
268 return weechat.WEECHAT_RC_OK
269
270 # ==================================[ main ]==================================
271
272 if __name__ == "__main__" and import_ok:
273 if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
274 SCRIPT_LICENSE, SCRIPT_DESC, "bas_unload_script", ""):
275 version = weechat.info_get("version_number", "") or 0
276 if int(version) < 0x01000000:
277 weechat.prnt("", "%s%s: WeeChat 1.0 is required for this script."
278 % (weechat.prefix("error"), SCRIPT_NAME))
279 else:
280 bas_config_init()
281 bas_config_read()
282 weechat.hook_command(
283 SCRIPT_COMMAND,
284 "Auto-set buffer properties when a buffer is opened",
285 "[add buffer property value] | [del option]",
286 " add: add a buffer/property/value in configuration file\n"
287 " del: delete an option from configuration file\n"
288 " buffer: name of a buffer (can start or end with \"*\" as "
289 "wildcard)\n"
290 "property: buffer property\n"
291 " value: value for property\n"
292 " option: name of option from configuration file\n\n"
293 "Examples:\n"
294 " disable timestamp on channel #weechat:\n"
295 " /" + SCRIPT_COMMAND + " add irc.freenode.#weechat "
296 "time_for_each_line 0\n"
297 " add word \"weechat\" in highlight list on channel "
298 "#savannah:\n"
299 " /" + SCRIPT_COMMAND + " add irc.freenode.#savannah "
300 "highlight_words_add weechat\n"
301 " disable highlights from nick \"mike\" on freenode server, "
302 "channel #weechat (requires WeeChat >= 0.3.4):\n"
303 " /" + SCRIPT_COMMAND + " add irc.freenode.#weechat "
304 "hotlist_max_level_nicks_add mike:2\n"
305 " disable hotlist changes for nick \"bot\" on freenode "
306 "server (all channels) (requires WeeChat >= 0.3.4):\n"
307 " /" + SCRIPT_COMMAND + " add irc.freenode.* "
308 "hotlist_max_level_nicks_add bot:-1",
309 "add %(buffers_plugins_names)|"
310 "%(buffer_autoset_current_buffer) "
311 "%(buffer_properties_set)"
312 " || del %(buffer_autoset_options)",
313 "bas_cmd", "")
314 weechat.hook_completion(
315 "buffer_autoset_current_buffer",
316 "current buffer name for buffer_autoset",
317 "bas_completion_current_buffer_cb", "")
318 weechat.hook_completion(
319 "buffer_autoset_options",
320 "list of options for buffer_autoset",
321 "bas_completion_options_cb", "")
322 weechat.hook_signal("9000|buffer_opened",
323 "bas_signal_buffer_opened_cb", "")
324 weechat.hook_config("%s.buffer.*" % CONFIG_FILE_NAME,
325 "bas_config_option_cb", "")
326
327 # core buffer is already open on script startup, check manually!
328 bas_signal_buffer_opened_cb("", "", weechat.buffer_search_main())
329
330
331 # ==================================[ end ]===================================
332
333 def bas_unload_script():
334 """ Function called when script is unloaded. """
335 global bas_config_file
336
337 if bas_config_file:
338 bas_config_write()
339 return weechat.WEECHAT_RC_OK