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 # 2017-06-21, Sébastien Helleu <flashcode@flashtux.org>:
26 # version 1.0: rename command /autosetbuffer to /buffer_autoset
27 # 2015-09-28, Simmo Saan <simmo.saan@gmail.com>:
28 # version 0.9: instantly apply properties
29 # 2015-07-12, Sébastien Helleu <flashcode@flashtux.org>:
30 # version 0.8: add option buffer_autoset.look.timer to add a small timer
31 # before setting buffer properties
32 # 2015-04-05, Nils Görs <freenode@#weechat>:
33 # version 0.7: increase priority of hook_signal('buffer_opened')
34 # 2012-12-09, Nils Görs <freenode@#weechat>:
35 # version 0.6: add support of core buffer
36 # 2012-03-09, Sébastien Helleu <flashcode@flashtux.org>:
37 # version 0.5: fix reload of config file
38 # 2012-01-03, Sébastien Helleu <flashcode@flashtux.org>:
39 # version 0.4: make script compatible with Python 3.x
40 # 2010-12-02, Sébastien Helleu <flashcode@flashtux.org>:
41 # version 0.3: "no_highlight_nicks" replaced by "hotlist_max_level_nicks"
42 # 2010-10-11, Sébastien Helleu <flashcode@flashtux.org>:
43 # version 0.2: add example in /help autosetbuffer with new buffer
44 # property "no_highlight_nicks"
45 # 2010-04-19, Sébastien Helleu <flashcode@flashtux.org>:
46 # version 0.1: initial release
49 SCRIPT_NAME
= "buffer_autoset"
50 SCRIPT_AUTHOR
= "Sébastien Helleu <flashcode@flashtux.org>"
51 SCRIPT_VERSION
= "1.0"
52 SCRIPT_LICENSE
= "GPL3"
53 SCRIPT_DESC
= "Auto-set buffer properties when a buffer is opened"
55 SCRIPT_COMMAND
= SCRIPT_NAME
62 print("This script must be run under WeeChat.")
63 print("Get WeeChat now at: http://www.weechat.org/")
66 CONFIG_FILE_NAME
= "buffer_autoset"
68 # config file / options
73 # =================================[ config ]=================================
75 def bas_config_init():
77 Initialization of configuration file.
80 global bas_config_file
, bas_options
81 bas_config_file
= weechat
.config_new(CONFIG_FILE_NAME
,
82 "bas_config_reload_cb", "")
83 if bas_config_file
== "":
87 section_look
= weechat
.config_new_section(
88 bas_config_file
, "look", 0, 0, "", "", "", "", "", "", "", "", "", "")
90 weechat
.config_free(bas_config_file
)
93 # options in section "look"
94 bas_options
["look_timer"] = weechat
.config_new_option(
95 bas_config_file
, section_look
, "timer", "integer",
96 "Timer used to delay the set of properties (in milliseconds, "
97 "0 = don't use a timer)",
98 "", 0, 2147483647, "1", "1", 0, "", "", "", "", "", "")
100 bas_options
["look_instant"] = weechat
.config_new_option(
101 bas_config_file
, section_look
, "instant", "boolean",
102 "Instantly apply properties to buffers affected",
103 "", 0, 0, "on", "on", 0, "", "", "", "", "", "")
106 section_buffer
= weechat
.config_new_section(
107 bas_config_file
, "buffer", 1, 1, "", "", "", "", "", "",
108 "bas_config_buffer_create_option_cb", "", "", "")
109 if not section_buffer
:
110 weechat
.config_free(bas_config_file
)
114 def bas_config_buffer_create_option_cb(data
, config_file
, section
, option_name
,
116 option
= weechat
.config_search_option(config_file
, section
, option_name
)
118 return weechat
.config_option_set(option
, value
, 1)
120 option
= weechat
.config_new_option(config_file
, section
, option_name
,
121 "string", "", "", 0, 0, "",
122 value
, 0, "", "", "", "", "", "")
124 return weechat
.WEECHAT_CONFIG_OPTION_SET_ERROR
125 return weechat
.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE
128 def bas_config_reload_cb(data
, config_file
):
129 """Reload configuration file."""
130 return weechat
.config_reload(config_file
)
133 def bas_config_read():
134 """Read configuration file."""
135 global bas_config_file
136 return weechat
.config_read(bas_config_file
)
139 def bas_config_write():
140 """Write configuration file."""
141 global bas_config_file
142 return weechat
.config_write(bas_config_file
)
145 # ================================[ command ]=================================
147 def bas_cmd(data
, buffer, args
):
148 """Callback for /buffer_autoset command."""
151 weechat
.command("", "/set %s.buffer.*" % CONFIG_FILE_NAME
)
152 return weechat
.WEECHAT_RC_OK
153 argv
= args
.split(None, 3)
157 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
158 return weechat
.WEECHAT_RC_OK
159 weechat
.command("", "/set %s.buffer.%s.%s \"%s\""
160 % (CONFIG_FILE_NAME
, argv
[1], argv
[2], argv
[3]))
161 elif argv
[0] == "del":
163 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
164 return weechat
.WEECHAT_RC_OK
165 weechat
.command("", "/unset %s.buffer.%s"
166 % (CONFIG_FILE_NAME
, argv
[1]))
168 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
169 return weechat
.WEECHAT_RC_OK
170 return weechat
.WEECHAT_RC_OK
173 def bas_completion_current_buffer_cb(data
, completion_item
, buffer,
176 Complete with current buffer name (plugin.name),
177 for command '/buffer_autoset'.
179 name
= "%s.%s" % (weechat
.buffer_get_string(buffer, "plugin"),
180 weechat
.buffer_get_string(buffer, "name"))
181 weechat
.hook_completion_list_add(completion
, name
,
182 0, weechat
.WEECHAT_LIST_POS_BEGINNING
)
183 return weechat
.WEECHAT_RC_OK
186 def bas_completion_options_cb(data
, completion_item
, buffer, completion
):
187 """Complete with config options, for command '/buffer_autoset'."""
188 options
= weechat
.infolist_get("option", "",
189 "%s.buffer.*" % CONFIG_FILE_NAME
)
191 while weechat
.infolist_next(options
):
192 weechat
.hook_completion_list_add(
194 weechat
.infolist_string(options
, "option_name"),
195 0, weechat
.WEECHAT_LIST_POS_SORT
)
196 weechat
.infolist_free(options
)
197 return weechat
.WEECHAT_RC_OK
200 # ==========================[ timer/signal/option ]===========================
202 def bas_apply_options_for_buffer(buffer):
203 full_name
= weechat
.buffer_get_string(buffer, "full_name")
204 options
= weechat
.infolist_get("option", "",
205 "%s.buffer.*" % CONFIG_FILE_NAME
)
209 while weechat
.infolist_next(options
):
210 option
= weechat
.infolist_string(options
, "option_name")
211 value
= weechat
.infolist_string(options
, "value")
213 pos
= option
.rfind(".")
215 buffer_mask
= option
[0:pos
]
216 property = option
[pos
+1:]
217 if buffer_mask
and property:
218 if weechat
.string_match(full_name
, buffer_mask
, 1):
219 weechat
.buffer_set(buffer, property, value
)
221 weechat
.infolist_free(options
)
224 def bas_timer_buffer_opened_cb(data
, remaining_calls
):
226 buffer = weechat
.buffer_search("==", full_name
)
228 return weechat
.WEECHAT_RC_OK
229 bas_apply_options_for_buffer(buffer)
230 return weechat
.WEECHAT_RC_OK
233 def bas_signal_buffer_opened_cb(data
, signal
, signal_data
):
236 timer
= weechat
.config_integer(bas_options
["look_timer"])
238 bas_apply_options_for_buffer(buffer)
240 weechat
.hook_timer(timer
, 0, 1,
241 "bas_timer_buffer_opened_cb",
242 weechat
.buffer_get_string(buffer, "full_name"))
243 return weechat
.WEECHAT_RC_OK
246 def bas_config_option_cb(data
, option
, value
):
247 if not weechat
.config_boolean(bas_options
["look_instant"]):
248 return weechat
.WEECHAT_RC_OK
250 if not weechat
.config_get(option
): # option was deleted
251 return weechat
.WEECHAT_RC_OK
253 option
= option
[len("%s.buffer." % CONFIG_FILE_NAME
):]
255 pos
= option
.rfind(".")
257 buffer_mask
= option
[0:pos
]
258 property = option
[pos
+1:]
259 if buffer_mask
and property:
260 buffers
= weechat
.infolist_get("buffer", "", buffer_mask
)
263 return weechat
.WEECHAT_RC_OK
265 while weechat
.infolist_next(buffers
):
266 buffer = weechat
.infolist_pointer(buffers
, "pointer")
267 weechat
.buffer_set(buffer, property, value
)
269 weechat
.infolist_free(buffers
)
271 return weechat
.WEECHAT_RC_OK
274 # ==================================[ main ]==================================
276 if __name__
== "__main__" and import_ok
:
277 if weechat
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
,
278 SCRIPT_LICENSE
, SCRIPT_DESC
, "bas_unload_script", ""):
279 version
= weechat
.info_get("version_number", "") or 0
280 if int(version
) < 0x01000000:
281 weechat
.prnt("", "%s%s: WeeChat 1.0 is required for this script."
282 % (weechat
.prefix("error"), SCRIPT_NAME
))
286 weechat
.hook_command(
288 "Auto-set buffer properties when a buffer is opened",
289 "[add buffer property value] | [del option]",
290 " add: add a buffer/property/value in configuration file\n"
291 " del: delete an option from configuration file\n"
292 " buffer: name of a buffer (can start or end with \"*\" as "
294 "property: buffer property\n"
295 " value: value for property\n"
296 " option: name of option from configuration file\n\n"
298 " disable timestamp on channel #weechat:\n"
299 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#weechat "
300 "time_for_each_line 0\n"
301 " add word \"weechat\" in highlight list on channel "
303 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#savannah "
304 "highlight_words_add weechat\n"
305 " disable highlights from nick \"mike\" on freenode server, "
306 "channel #weechat (requires WeeChat >= 0.3.4):\n"
307 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#weechat "
308 "hotlist_max_level_nicks_add mike:2\n"
309 " disable hotlist changes for nick \"bot\" on freenode "
310 "server (all channels) (requires WeeChat >= 0.3.4):\n"
311 " /" + SCRIPT_COMMAND
+ " add irc.freenode.* "
312 "hotlist_max_level_nicks_add bot:-1",
313 "add %(buffers_plugins_names)|"
314 "%(buffer_autoset_current_buffer) "
315 "%(buffer_properties_set)"
316 " || del %(buffer_autoset_options)",
318 weechat
.hook_completion(
319 "buffer_autoset_current_buffer",
320 "current buffer name for buffer_autoset",
321 "bas_completion_current_buffer_cb", "")
322 weechat
.hook_completion(
323 "buffer_autoset_options",
324 "list of options for buffer_autoset",
325 "bas_completion_options_cb", "")
326 weechat
.hook_signal("9000|buffer_opened",
327 "bas_signal_buffer_opened_cb", "")
328 weechat
.hook_config("%s.buffer.*" % CONFIG_FILE_NAME
,
329 "bas_config_option_cb", "")
331 # core buffer is already open on script startup, check manually!
332 bas_signal_buffer_opened_cb("", "", weechat
.buffer_search_main())
335 # ==================================[ end ]===================================
337 def bas_unload_script():
338 """ Function called when script is unloaded. """
339 global bas_config_file
343 return weechat
.WEECHAT_RC_OK