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 # 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
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"
57 SCRIPT_COMMAND
= SCRIPT_NAME
64 print("This script must be run under WeeChat.")
65 print("Get WeeChat now at: http://www.weechat.org/")
68 CONFIG_FILE_NAME
= "buffer_autoset"
70 # config file / options
75 # =================================[ config ]=================================
77 def bas_config_init():
79 Initialization of configuration file.
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
== "":
89 section_look
= weechat
.config_new_section(
90 bas_config_file
, "look", 0, 0, "", "", "", "", "", "", "", "", "", "")
92 weechat
.config_free(bas_config_file
)
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, "", "", "", "", "", "")
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, "", "", "", "", "", "")
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
)
116 def bas_config_buffer_create_option_cb(data
, config_file
, section
, option_name
,
118 option
= weechat
.config_search_option(config_file
, section
, option_name
)
120 return weechat
.config_option_set(option
, value
, 1)
122 option
= weechat
.config_new_option(config_file
, section
, option_name
,
123 "string", "", "", 0, 0, "",
124 value
, 0, "", "", "", "", "", "")
126 return weechat
.WEECHAT_CONFIG_OPTION_SET_ERROR
127 return weechat
.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE
130 def bas_config_reload_cb(data
, config_file
):
131 """Reload configuration file."""
132 return weechat
.config_reload(config_file
)
135 def bas_config_read():
136 """Read configuration file."""
137 global bas_config_file
138 return weechat
.config_read(bas_config_file
)
141 def bas_config_write():
142 """Write configuration file."""
143 global bas_config_file
144 return weechat
.config_write(bas_config_file
)
147 # ================================[ command ]=================================
149 def bas_cmd(data
, buffer, args
):
150 """Callback for /buffer_autoset command."""
153 weechat
.command("", "/set %s.buffer.*" % CONFIG_FILE_NAME
)
154 return weechat
.WEECHAT_RC_OK
155 argv
= args
.split(None, 3)
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":
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]))
170 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
171 return weechat
.WEECHAT_RC_OK
172 return weechat
.WEECHAT_RC_OK
175 def bas_completion_current_buffer_cb(data
, completion_item
, buffer,
178 Complete with current buffer name (plugin.name),
179 for command '/buffer_autoset'.
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
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
)
193 while weechat
.infolist_next(options
):
194 weechat
.hook_completion_list_add(
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
202 # ==========================[ timer/signal/option ]===========================
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
)
211 while weechat
.infolist_next(options
):
212 option
= weechat
.infolist_string(options
, "option_name")
213 value
= weechat
.infolist_string(options
, "value")
215 pos
= option
.rfind(".")
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
)
223 weechat
.infolist_free(options
)
226 def bas_timer_buffer_opened_cb(data
, remaining_calls
):
228 buffer = weechat
.buffer_search("==", full_name
)
230 return weechat
.WEECHAT_RC_OK
231 bas_apply_options_for_buffer(buffer)
232 return weechat
.WEECHAT_RC_OK
235 def bas_signal_buffer_opened_cb(data
, signal
, signal_data
):
238 timer
= weechat
.config_integer(bas_options
["look_timer"])
240 bas_apply_options_for_buffer(buffer)
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
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
252 if not weechat
.config_get(option
): # option was deleted
253 return weechat
.WEECHAT_RC_OK
255 option
= option
[len("%s.buffer." % CONFIG_FILE_NAME
):]
257 pos
= option
.rfind(".")
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
)
265 return weechat
.WEECHAT_RC_OK
267 while weechat
.infolist_next(buffers
):
268 buffer = weechat
.infolist_pointer(buffers
, "pointer")
269 weechat
.buffer_set(buffer, property, value
)
271 weechat
.infolist_free(buffers
)
273 return weechat
.WEECHAT_RC_OK
276 # ==================================[ main ]==================================
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
))
288 weechat
.hook_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 "
296 "property: buffer property\n"
297 " value: value for property\n"
298 " option: name of option from configuration file\n\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 "
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)",
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", "")
333 # apply settings to all already opened buffers
334 buffers
= weechat
.infolist_get("buffer", "", "")
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
)
342 # ==================================[ end ]===================================
344 def bas_unload_script():
345 """ Function called when script is unloaded. """
346 global bas_config_file
350 return weechat
.WEECHAT_RC_OK