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