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