1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2010-2017 Sébastien Helleu <flashcode@flashtux.org>
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.
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.
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/>.
20 # Auto-set buffer properties when a buffer is opened.
21 # (this script requires WeeChat 1.0 or newer)
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
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"
59 SCRIPT_COMMAND
= SCRIPT_NAME
66 print("This script must be run under WeeChat.")
67 print("Get WeeChat now at: http://www.weechat.org/")
70 CONFIG_FILE_NAME
= "buffer_autoset"
72 # config file / options
77 # =================================[ config ]=================================
79 def bas_config_init():
81 Initialization of configuration file.
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
== "":
91 section_look
= weechat
.config_new_section(
92 bas_config_file
, "look", 0, 0, "", "", "", "", "", "", "", "", "", "")
94 weechat
.config_free(bas_config_file
)
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, "", "", "", "", "", "")
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, "", "", "", "", "", "")
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
)
118 def bas_config_buffer_create_option_cb(data
, config_file
, section
, option_name
,
120 option
= weechat
.config_search_option(config_file
, section
, option_name
)
122 return weechat
.config_option_set(option
, value
, 1)
124 option
= weechat
.config_new_option(config_file
, section
, option_name
,
125 "string", "", "", 0, 0, "",
126 value
, 0, "", "", "", "", "", "")
128 return weechat
.WEECHAT_CONFIG_OPTION_SET_ERROR
129 return weechat
.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE
132 def bas_config_reload_cb(data
, config_file
):
133 """Reload configuration file."""
134 return weechat
.config_reload(config_file
)
137 def bas_config_read():
138 """Read configuration file."""
139 global bas_config_file
140 return weechat
.config_read(bas_config_file
)
143 def bas_config_write():
144 """Write configuration file."""
145 global bas_config_file
146 return weechat
.config_write(bas_config_file
)
149 # ================================[ command ]=================================
151 def bas_cmd(data
, buffer, args
):
152 """Callback for /buffer_autoset command."""
155 weechat
.command("", "/set %s.buffer.*" % CONFIG_FILE_NAME
)
156 return weechat
.WEECHAT_RC_OK
157 argv
= args
.split(None, 3)
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":
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]))
172 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
173 return weechat
.WEECHAT_RC_OK
174 return weechat
.WEECHAT_RC_OK
177 def bas_completion_current_buffer_cb(data
, completion_item
, buffer,
180 Complete with current buffer name (plugin.name),
181 for command '/buffer_autoset'.
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
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
)
195 while weechat
.infolist_next(options
):
196 weechat
.hook_completion_list_add(
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
204 # ==========================[ timer/signal/option ]===========================
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
)
213 while weechat
.infolist_next(options
):
214 option
= weechat
.infolist_string(options
, "option_name")
215 value
= weechat
.infolist_string(options
, "value")
217 pos
= option
.rfind(".")
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
)
225 weechat
.infolist_free(options
)
228 def bas_timer_buffer_opened_cb(data
, remaining_calls
):
230 buffer = weechat
.buffer_search("==", full_name
)
232 return weechat
.WEECHAT_RC_OK
233 bas_apply_options_for_buffer(buffer)
234 return weechat
.WEECHAT_RC_OK
237 def bas_signal_buffer_opened_cb(data
, signal
, signal_data
):
240 timer
= weechat
.config_integer(bas_options
["look_timer"])
242 bas_apply_options_for_buffer(buffer)
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
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
254 if not weechat
.config_get(option
): # option was deleted
255 return weechat
.WEECHAT_RC_OK
257 option
= option
[len("%s.buffer." % CONFIG_FILE_NAME
):]
259 pos
= option
.rfind(".")
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
)
267 return weechat
.WEECHAT_RC_OK
269 while weechat
.infolist_next(buffers
):
270 buffer = weechat
.infolist_pointer(buffers
, "pointer")
271 weechat
.buffer_set(buffer, property, value
)
273 weechat
.infolist_free(buffers
)
275 return weechat
.WEECHAT_RC_OK
278 # ==================================[ main ]==================================
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
))
290 weechat
.hook_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"
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 "
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)",
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", "")
334 # apply settings to all already opened buffers
335 buffers
= weechat
.infolist_get("buffer", "", "")
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
)
343 # ==================================[ end ]===================================
345 def bas_unload_script():
346 """ Function called when script is unloaded. """
347 global bas_config_file
351 return weechat
.WEECHAT_RC_OK