1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2010-2015 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 # 2015-09-28, Simmo Saan <simmo.saan@gmail.com>:
26 # version 0.9: instantly apply properties
27 # 2015-07-12, Sébastien Helleu <flashcode@flashtux.org>:
28 # version 0.8: add option buffer_autoset.look.timer to add a small timer
29 # before setting buffer properties
30 # 2015-04-05, Nils Görs <freenode@#weechat>:
31 # version 0.7: increase priority of hook_signal('buffer_opened')
32 # 2012-12-09, Nils Görs <freenode@#weechat>:
33 # version 0.6: add support of core buffer
34 # 2012-03-09, Sébastien Helleu <flashcode@flashtux.org>:
35 # version 0.5: fix reload of config file
36 # 2012-01-03, Sébastien Helleu <flashcode@flashtux.org>:
37 # version 0.4: make script compatible with Python 3.x
38 # 2010-12-02, Sébastien Helleu <flashcode@flashtux.org>:
39 # version 0.3: "no_highlight_nicks" replaced by "hotlist_max_level_nicks"
40 # 2010-10-11, Sébastien Helleu <flashcode@flashtux.org>:
41 # version 0.2: add example in /help autosetbuffer with new buffer
42 # property "no_highlight_nicks"
43 # 2010-04-19, Sébastien Helleu <flashcode@flashtux.org>:
44 # version 0.1: initial release
47 SCRIPT_NAME
= "buffer_autoset"
48 SCRIPT_AUTHOR
= "Sébastien Helleu <flashcode@flashtux.org>"
49 SCRIPT_VERSION
= "0.9"
50 SCRIPT_LICENSE
= "GPL3"
51 SCRIPT_DESC
= "Auto-set buffer properties when a buffer is opened"
53 SCRIPT_COMMAND
= "autosetbuffer"
60 print("This script must be run under WeeChat.")
61 print("Get WeeChat now at: http://www.weechat.org/")
64 CONFIG_FILE_NAME
= "buffer_autoset"
66 # config file / options
71 # =================================[ config ]=================================
73 def bas_config_init():
75 Initialization of configuration file.
78 global bas_config_file
, bas_options
79 bas_config_file
= weechat
.config_new(CONFIG_FILE_NAME
,
80 "bas_config_reload_cb", "")
81 if bas_config_file
== "":
85 section_look
= weechat
.config_new_section(
86 bas_config_file
, "look", 0, 0, "", "", "", "", "", "", "", "", "", "")
88 weechat
.config_free(bas_config_file
)
91 # options in section "look"
92 bas_options
["look_timer"] = weechat
.config_new_option(
93 bas_config_file
, section_look
, "timer", "integer",
94 "Timer used to delay the set of properties (in milliseconds, "
95 "0 = don't use a timer)",
96 "", 0, 2147483647, "1", "1", 0, "", "", "", "", "", "")
98 bas_options
["look_instant"] = weechat
.config_new_option(
99 bas_config_file
, section_look
, "instant", "boolean",
100 "Instantly apply properties to buffers affected",
101 "", 0, 0, "on", "on", 0, "", "", "", "", "", "")
104 section_buffer
= weechat
.config_new_section(
105 bas_config_file
, "buffer", 1, 1, "", "", "", "", "", "",
106 "bas_config_buffer_create_option_cb", "", "", "")
107 if not section_buffer
:
108 weechat
.config_free(bas_config_file
)
112 def bas_config_buffer_create_option_cb(data
, config_file
, section
, option_name
,
114 option
= weechat
.config_search_option(config_file
, section
, option_name
)
116 return weechat
.config_option_set(option
, value
, 1)
118 option
= weechat
.config_new_option(config_file
, section
, option_name
,
119 "string", "", "", 0, 0, "",
120 value
, 0, "", "", "", "", "", "")
122 return weechat
.WEECHAT_CONFIG_OPTION_SET_ERROR
123 return weechat
.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE
126 def bas_config_reload_cb(data
, config_file
):
127 """Reload configuration file."""
128 return weechat
.config_reload(config_file
)
131 def bas_config_read():
132 """Read configuration file."""
133 global bas_config_file
134 return weechat
.config_read(bas_config_file
)
137 def bas_config_write():
138 """Write configuration file."""
139 global bas_config_file
140 return weechat
.config_write(bas_config_file
)
143 # ================================[ command ]=================================
145 def bas_cmd(data
, buffer, args
):
146 """Callback for /autosetbuffer command."""
149 weechat
.command("", "/set %s.buffer.*" % CONFIG_FILE_NAME
)
150 return weechat
.WEECHAT_RC_OK
151 argv
= args
.split(None, 3)
155 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
156 return weechat
.WEECHAT_RC_OK
157 weechat
.command("", "/set %s.buffer.%s.%s \"%s\""
158 % (CONFIG_FILE_NAME
, argv
[1], argv
[2], argv
[3]))
159 elif argv
[0] == "del":
161 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
162 return weechat
.WEECHAT_RC_OK
163 weechat
.command("", "/unset %s.buffer.%s"
164 % (CONFIG_FILE_NAME
, argv
[1]))
166 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
167 return weechat
.WEECHAT_RC_OK
168 return weechat
.WEECHAT_RC_OK
171 def bas_completion_current_buffer_cb(data
, completion_item
, buffer,
174 Complete with current buffer name (plugin.name),
175 for command '/autosetbuffer'.
177 name
= "%s.%s" % (weechat
.buffer_get_string(buffer, "plugin"),
178 weechat
.buffer_get_string(buffer, "name"))
179 weechat
.hook_completion_list_add(completion
, name
,
180 0, weechat
.WEECHAT_LIST_POS_BEGINNING
)
181 return weechat
.WEECHAT_RC_OK
184 def bas_completion_options_cb(data
, completion_item
, buffer, completion
):
185 """Complete with config options, for command '/autosetbuffer'."""
186 options
= weechat
.infolist_get("option", "",
187 "%s.buffer.*" % CONFIG_FILE_NAME
)
189 while weechat
.infolist_next(options
):
190 weechat
.hook_completion_list_add(
192 weechat
.infolist_string(options
, "option_name"),
193 0, weechat
.WEECHAT_LIST_POS_SORT
)
194 weechat
.infolist_free(options
)
195 return weechat
.WEECHAT_RC_OK
198 # ==========================[ timer/signal/option ]===========================
200 def bas_apply_options_for_buffer(buffer):
201 full_name
= weechat
.buffer_get_string(buffer, "full_name")
202 options
= weechat
.infolist_get("option", "",
203 "%s.buffer.*" % CONFIG_FILE_NAME
)
207 while weechat
.infolist_next(options
):
208 option
= weechat
.infolist_string(options
, "option_name")
209 value
= weechat
.infolist_string(options
, "value")
211 pos
= option
.rfind(".")
213 buffer_mask
= option
[0:pos
]
214 property = option
[pos
+1:]
215 if buffer_mask
and property:
216 if weechat
.string_match(full_name
, buffer_mask
, 1):
217 weechat
.buffer_set(buffer, property, value
)
219 weechat
.infolist_free(options
)
222 def bas_timer_buffer_opened_cb(data
, remaining_calls
):
224 buffer = weechat
.buffer_search("==", full_name
)
226 return weechat
.WEECHAT_RC_OK
227 bas_apply_options_for_buffer(buffer)
228 return weechat
.WEECHAT_RC_OK
231 def bas_signal_buffer_opened_cb(data
, signal
, signal_data
):
234 timer
= weechat
.config_integer(bas_options
["look_timer"])
236 bas_apply_options_for_buffer(buffer)
238 weechat
.hook_timer(timer
, 0, 1,
239 "bas_timer_buffer_opened_cb",
240 weechat
.buffer_get_string(buffer, "full_name"))
241 return weechat
.WEECHAT_RC_OK
243 def bas_config_option_cb(data
, option
, value
):
244 if not weechat
.config_boolean(bas_options
["look_instant"]):
245 return weechat
.WEECHAT_RC_OK
247 if not weechat
.config_get(option
): # option was deleted
248 return weechat
.WEECHAT_RC_OK
250 option
= option
[len("%s.buffer." % CONFIG_FILE_NAME
):]
252 pos
= option
.rfind(".")
254 buffer_mask
= option
[0:pos
]
255 property = option
[pos
+1:]
256 if buffer_mask
and property:
257 buffers
= weechat
.infolist_get("buffer", "", buffer_mask
)
260 return weechat
.WEECHAT_RC_OK
262 while weechat
.infolist_next(buffers
):
263 buffer = weechat
.infolist_pointer(buffers
, "pointer")
264 weechat
.buffer_set(buffer, property, value
)
266 weechat
.infolist_free(buffers
)
268 return weechat
.WEECHAT_RC_OK
270 # ==================================[ main ]==================================
272 if __name__
== "__main__" and import_ok
:
273 if weechat
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
,
274 SCRIPT_LICENSE
, SCRIPT_DESC
, "bas_unload_script", ""):
275 version
= weechat
.info_get("version_number", "") or 0
276 if int(version
) < 0x01000000:
277 weechat
.prnt("", "%s%s: WeeChat 1.0 is required for this script."
278 % (weechat
.prefix("error"), SCRIPT_NAME
))
282 weechat
.hook_command(
284 "Auto-set buffer properties when a buffer is opened",
285 "[add buffer property value] | [del option]",
286 " add: add a buffer/property/value in configuration file\n"
287 " del: delete an option from configuration file\n"
288 " buffer: name of a buffer (can start or end with \"*\" as "
290 "property: buffer property\n"
291 " value: value for property\n"
292 " option: name of option from configuration file\n\n"
294 " disable timestamp on channel #weechat:\n"
295 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#weechat "
296 "time_for_each_line 0\n"
297 " add word \"weechat\" in highlight list on channel "
299 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#savannah "
300 "highlight_words_add weechat\n"
301 " disable highlights from nick \"mike\" on freenode server, "
302 "channel #weechat (requires WeeChat >= 0.3.4):\n"
303 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#weechat "
304 "hotlist_max_level_nicks_add mike:2\n"
305 " disable hotlist changes for nick \"bot\" on freenode "
306 "server (all channels) (requires WeeChat >= 0.3.4):\n"
307 " /" + SCRIPT_COMMAND
+ " add irc.freenode.* "
308 "hotlist_max_level_nicks_add bot:-1",
309 "add %(buffers_plugins_names)|"
310 "%(buffer_autoset_current_buffer) "
311 "%(buffer_properties_set)"
312 " || del %(buffer_autoset_options)",
314 weechat
.hook_completion(
315 "buffer_autoset_current_buffer",
316 "current buffer name for buffer_autoset",
317 "bas_completion_current_buffer_cb", "")
318 weechat
.hook_completion(
319 "buffer_autoset_options",
320 "list of options for buffer_autoset",
321 "bas_completion_options_cb", "")
322 weechat
.hook_signal("9000|buffer_opened",
323 "bas_signal_buffer_opened_cb", "")
324 weechat
.hook_config("%s.buffer.*" % CONFIG_FILE_NAME
,
325 "bas_config_option_cb", "")
327 # core buffer is already open on script startup, check manually!
328 bas_signal_buffer_opened_cb("", "", weechat
.buffer_search_main())
331 # ==================================[ end ]===================================
333 def bas_unload_script():
334 """ Function called when script is unloaded. """
335 global bas_config_file
339 return weechat
.WEECHAT_RC_OK