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