1 # vim: set noet nosta sw=4 ts=4 :
4 # Michael B. Hix <m@hix.io>
5 # http://code.hix.io/projects/colorizer
7 # Color certain parts of text in certain buffers based on rules.
13 # plugins.var.ruby.colorizer.buffer_regex
14 # Buffers with names matching this regex are colorized. All buffers are
15 # colorized if this option is empty.
17 # plugins.var.ruby.colorizer.rule.count
18 # This is the maximum number of rules to load.
20 # plugins.var.ruby.colorizer.rule.X
21 # X is zero or a positive integer. Rules are strings consisting of a regular
22 # expression followed immediately by a slash and a Weechat color name. The
23 # regular expressions are case-insensitive.
25 # Text matching the regular expression is colored with the given color. The
26 # last match "wins" and overlapping matches are not detected.
28 # For example: "strelka|mongrel2/lightgreen"
34 # 0.1: Initial release.
35 # 0.2: Add compatibility with new weechat_print modifier data (WeeChat >= 2.9).
36 # 0.3: Fix the compatibility checker
38 SCRIPT_NAME
= 'colorizer'
39 SCRIPT_AUTHOR
= 'Michael B. Hix'
40 SCRIPT_DESC
= 'Colorize text in buffers based on rules.'
41 SCRIPT_VERSION
= '0.3'
42 SCRIPT_LICENSE
= 'BSD'
44 # A default coloring rule.
48 :description => 'A colorizing rule of the form: <regular_expression>/<weechat_color_name> Empty rules are ignored.',
51 # Configuration defaults are supplied and set for the user if they're not already set.
54 'rule.0' => DEFAULT_RULE
,
55 'rule.1' => DEFAULT_RULE
,
56 'rule.2' => DEFAULT_RULE
,
57 'rule.3' => DEFAULT_RULE
,
58 'rule.4' => DEFAULT_RULE
,
61 :description => 'The maximum number of rules to look for in your config.',
65 :description => 'Only colorize text in buffers with names that match this regex. Leaving this empty matches all buffer names.',
69 ########################################################################
71 ########################################################################
74 Weechat
.register SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
, SCRIPT_LICENSE
, SCRIPT_DESC
, '', ''
76 Weechat
.hook_modifier( 'weechat_print', 'colorize_cb', '' )
78 DEFAULTS
.each_pair
do |option
, opts
|
80 description
= opts
[:description]
82 cur_value
= Weechat
.config_get_plugin( option
)
84 if cur_value
.nil? || cur_value
.empty
?
85 Weechat
.config_set_plugin( option
, value
.to_s
)
88 Weechat
.config_set_desc_plugin( option
, description
)
93 Weechat
.hook_config( "plugins.var.ruby.#{SCRIPT_NAME}.*", 'config_cb', '' )
95 return Weechat
::WEECHAT_RC_OK
98 ################################################################################
100 ################################################################################
102 # Provide a way to print legible stack traces.
104 def pp_error( e
, message
= '' )
105 return unless e
.is_a
? Exception
106 unless message
.nil? or message
.empty
?
107 Weechat
.print( '', '%s%s' % [Weechat
.prefix('error'), message
] )
109 Weechat
.print( '', '%s%s: %s' % [Weechat
.prefix( 'error' ), SCRIPT_NAME
, e
.to_s
] )
110 e
.backtrace
.each
do |line
|
111 Weechat
.print( '', '%s%s' % [Weechat
.prefix( 'error' ), line
] )
115 # Re-build rules and any regular expressions when the config changes.
119 count
= Weechat
::config_get_plugin( 'rule.count' ).to_i
||
120 DEFAULTS
['rule.count']
124 next unless Weechat
::config_is_set_plugin( key
)
126 conf
= Weechat
::config_get_plugin( key
)
127 regex
,color
,_
= conf
.split( /(?<!\\)\//, 3 )
129 next if regex
.nil? or regex
.empty
? or color
.nil? or color
.empty
?
132 rules
[/(#{regex})/i
] = color
133 rescue Exception
=> e
134 pp_error( e
, 'There was a problem with rule %d:' % [i
] )
141 @buffer_regex = /#{Weechat::config_get_plugin( 'buffer_regex' )}/i
142 rescue Exception
=> e
143 pp_error( e
, 'There was a problem with buffer_regex:' )
147 ################################################################################
148 ### C A L L B A C K S
149 ################################################################################
151 # Handle configuration changes.
153 def config_cb( data, option
, value
)
155 return Weechat
::WEECHAT_RC_OK
158 # Handle message printing.
160 def colorize_cb( data, modifier
, modifier_data
, message
)
161 if modifier_data
.start_with
?('0x')
163 buffer
, tags
= modifier_data
.split( ';' )
164 buffer_name
= Weechat
.buffer_get_string(buffer
, 'name')
167 plugin
, buffer_name
, tags
= modifier_data
.split( ';' )
170 return message
unless @buffer_regex =~ buffer_name
172 reset
= Weechat
.color( 'reset' )
173 @rules.each
do |reg
, color_str
|
174 color
= Weechat
.color( color_str
)
175 message
.gsub!
( reg
, '%s\1%s' % [color
,reset
] )
184 Copyright (c
) 2014 Michael B
. Hix
187 Redistribution
and use
in source
and binary forms
, with
or without
188 modification
, are permitted provided that the following conditions are met
:
190 1. Redistributions of source code must retain the above copyright notice
,
191 this list of conditions
and the following disclaimer
.
193 2. Redistributions
in binary form must reproduce the above copyright notice
,
194 this list of conditions
and the following disclaimer
in the documentation
195 and/or other materials provided with the distribution
.
197 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS"
198 AND ANY EXPRESS OR IMPLIED WARRANTIES
, INCLUDING
, BUT NOT LIMITED TO
, THE
199 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200 ARE DISCLAIMED
. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
201 LIABLE FOR ANY DIRECT
, INDIRECT
, INCIDENTAL
, SPECIAL
, EXEMPLARY
, OR
202 CONSEQUENTIAL
DAMAGES (INCLUDING
, BUT NOT LIMITED TO
, PROCUREMENT OF
203 SUBSTITUTE GOODS OR SERVICES
; LOSS OF USE
, DATA, OR PROFITS
; OR BUSINESS
204 INTERRUPTION
) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY
, WHETHER IN
205 CONTRACT
, STRICT LIABILITY
, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE
)
206 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE
, EVEN IF ADVISED OF THE
207 POSSIBILITY OF SUCH DAMAGE
.