1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2010-2012 Sebastien 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 0.3.2 or newer)
25 # 2012-12-09, Nils Görs <freenode@#weechat>:
26 # version 0.6: add support of core buffer
27 # 2012-03-09, Sebastien Helleu <flashcode@flashtux.org>:
28 # version 0.5: fix reload of config file
29 # 2012-01-03, Sebastien Helleu <flashcode@flashtux.org>:
30 # version 0.4: make script compatible with Python 3.x
31 # 2010-12-02, Sebastien Helleu <flashcode@flashtux.org>:
32 # version 0.3: "no_highlight_nicks" replaced by "hotlist_max_level_nicks"
33 # 2010-10-11, Sebastien Helleu <flashcode@flashtux.org>:
34 # version 0.2: add example in /help autosetbuffer with new buffer
35 # property "no_highlight_nicks"
36 # 2010-04-19, Sebastien Helleu <flashcode@flashtux.org>:
37 # version 0.1: initial release
40 SCRIPT_NAME
= "buffer_autoset"
41 SCRIPT_AUTHOR
= "Sebastien Helleu <flashcode@flashtux.org>"
42 SCRIPT_VERSION
= "0.6"
43 SCRIPT_LICENSE
= "GPL3"
44 SCRIPT_DESC
= "Auto-set buffer properties when a buffer is opened"
46 SCRIPT_COMMAND
= "autosetbuffer"
53 print("This script must be run under WeeChat.")
54 print("Get WeeChat now at: http://www.weechat.org/")
57 CONFIG_FILE_NAME
= "buffer_autoset"
62 # =================================[ config ]=================================
64 def bas_config_init():
66 Initialization of configuration file.
69 global bas_config_file
70 bas_config_file
= weechat
.config_new(CONFIG_FILE_NAME
,
71 "bas_config_reload_cb", "")
72 if bas_config_file
== "":
76 section_buffer
= weechat
.config_new_section(
77 bas_config_file
, "buffer", 1, 1, "", "", "", "", "", "",
78 "bas_config_buffer_create_option_cb", "", "", "")
79 if section_buffer
== "":
80 weechat
.config_free(bas_config_file
)
83 def bas_config_buffer_create_option_cb(data
, config_file
, section
, option_name
, value
):
84 option
= weechat
.config_search_option(config_file
, section
, option_name
)
86 return weechat
.config_option_set (option
, value
, 1)
88 option
= weechat
.config_new_option (config_file
, section
, option_name
, "string",
89 "", "", 0, 0, "", value
, 0,
90 "", "", "", "", "", "")
92 return weechat
.WEECHAT_CONFIG_OPTION_SET_ERROR
93 return weechat
.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE
95 def bas_config_reload_cb(data
, config_file
):
96 """ Reload configuration file. """
97 return weechat
.config_reload(config_file
)
99 def bas_config_read():
100 """ Read configuration file. """
101 global bas_config_file
102 return weechat
.config_read(bas_config_file
)
104 def bas_config_write():
105 """ Write configuration file. """
106 global bas_config_file
107 return weechat
.config_write(bas_config_file
)
109 # ================================[ command ]=================================
111 def bas_cmd(data
, buffer, args
):
112 """ Callback for /autosetbuffer command. """
115 weechat
.command("", "/set %s.buffer.*" % CONFIG_FILE_NAME
)
116 return weechat
.WEECHAT_RC_OK
117 argv
= args
.split(None, 3)
121 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
122 return weechat
.WEECHAT_RC_OK
123 weechat
.command("", "/set %s.buffer.%s.%s \"%s\""
124 % (CONFIG_FILE_NAME
, argv
[1], argv
[2], argv
[3]))
125 elif argv
[0] == "del":
127 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
128 return weechat
.WEECHAT_RC_OK
129 weechat
.command("", "/unset %s.buffer.%s"
130 % (CONFIG_FILE_NAME
, argv
[1]))
132 weechat
.command("", "/help %s" % SCRIPT_COMMAND
)
133 return weechat
.WEECHAT_RC_OK
134 return weechat
.WEECHAT_RC_OK
136 def bas_completion_current_buffer_cb(data
, completion_item
, buffer, completion
):
137 """ Complete with current buffer name (plugin.name), for command '/autosetbuffer'. """
138 name
= "%s.%s" % (weechat
.buffer_get_string(buffer, "plugin"),
139 weechat
.buffer_get_string(buffer, "name"))
140 weechat
.hook_completion_list_add(completion
, name
,
141 0, weechat
.WEECHAT_LIST_POS_BEGINNING
)
142 return weechat
.WEECHAT_RC_OK
144 def bas_completion_options_cb(data
, completion_item
, buffer, completion
):
145 """ Complete with config options, for command '/autosetbuffer'. """
146 options
= weechat
.infolist_get("option", "", "%s.buffer.*" % CONFIG_FILE_NAME
)
148 while weechat
.infolist_next(options
):
149 weechat
.hook_completion_list_add(completion
,
150 weechat
.infolist_string(options
, "option_name"),
151 0, weechat
.WEECHAT_LIST_POS_SORT
)
152 weechat
.infolist_free(options
)
153 return weechat
.WEECHAT_RC_OK
155 # =================================[ signal ]=================================
157 def bas_signal_buffer_opened_cb(data
, signal
, signal_data
):
159 name
= "%s.%s" % (weechat
.buffer_get_string(buffer, "plugin"),
160 weechat
.buffer_get_string(buffer, "name"))
161 options
= weechat
.infolist_get("option", "", "%s.buffer.*" % CONFIG_FILE_NAME
)
163 while weechat
.infolist_next(options
):
164 option
= weechat
.infolist_string(options
, "option_name")
165 value
= weechat
.infolist_string(options
, "value")
167 pos
= option
.rfind(".")
169 buffer_mask
= option
[0:pos
]
170 property = option
[pos
+1:]
171 if buffer_mask
and property:
172 if weechat
.string_match(name
, buffer_mask
, 1):
173 weechat
.buffer_set(buffer, property, value
)
174 weechat
.infolist_free(options
)
175 return weechat
.WEECHAT_RC_OK
177 # ==================================[ main ]==================================
179 if __name__
== "__main__" and import_ok
:
180 if weechat
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
, SCRIPT_LICENSE
,
181 SCRIPT_DESC
, "bas_unload_script", ""):
182 version
= weechat
.info_get("version_number", "") or 0
183 if int(version
) < 0x00030200:
184 weechat
.prnt("", "%s%s: WeeChat 0.3.2 is required for this script."
185 % (weechat
.prefix("error"), SCRIPT_NAME
))
189 weechat
.hook_command(SCRIPT_COMMAND
,
190 "Auto-set buffer properties when a buffer is opened",
191 "[add buffer property value] | [del option]",
192 " add: add a buffer/property/value in configuration file\n"
193 " del: delete an option from configuration file\n"
194 " buffer: name of a buffer (can start or end with \"*\" as wildcard)\n"
195 "property: buffer property\n"
196 " value: value for property\n"
197 " option: name of option from configuration file\n\n"
199 " disable timestamp on channel #weechat:\n"
200 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#weechat time_for_each_line 0\n"
201 " add word \"weechat\" in highlight list on channel #savannah:\n"
202 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#savannah highlight_words_add weechat\n"
203 " disable highlights from nick \"mike\" on freenode server, channel #weechat (requires WeeChat >= 0.3.4):\n"
204 " /" + SCRIPT_COMMAND
+ " add irc.freenode.#weechat hotlist_max_level_nicks_add mike:2\n"
205 " disable hotlist changes for nick \"bot\" on freenode server (all channels) (requires WeeChat >= 0.3.4):\n"
206 " /" + SCRIPT_COMMAND
+ " add irc.freenode.* hotlist_max_level_nicks_add bot:-1",
207 "add %(buffers_plugins_names)|%(buffer_autoset_current_buffer) %(buffer_properties_set)"
208 " || del %(buffer_autoset_options)",
210 weechat
.hook_completion("buffer_autoset_current_buffer", "current buffer name for buffer_autoset",
211 "bas_completion_current_buffer_cb", "")
212 weechat
.hook_completion("buffer_autoset_options", "list of options for buffer_autoset",
213 "bas_completion_options_cb", "")
214 weechat
.hook_signal("buffer_opened", "bas_signal_buffer_opened_cb", "")
216 # core buffer is already open on script startup, check manually!
217 bas_signal_buffer_opened_cb("", "", weechat
.buffer_search_main())
218 # ==================================[ end ]===================================
220 def bas_unload_script():
221 """ Function called when script is unloaded. """
222 global bas_config_file
226 return weechat
.WEECHAT_RC_OK