-
Notifications
You must be signed in to change notification settings - Fork 1
/
regex_highlight.rb
153 lines (130 loc) · 4.72 KB
/
regex_highlight.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# regex_highlight.rb
# Copyright (c) 2014 Les Aker <me@lesaker.org>
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Changelog:
# 0.0.1 - Initial functionality
# 0.0.2 - Cleaned up code
# 0.1.0 - Classified, cleaned up bugs
require 'yaml'
##
# Config object for regex highlighting
class RegexConfig
attr_reader :path, :rules
HELPTEXT = '[save] | [load] | [list] | [[add|del] channel pattern]'.freeze
def initialize
@path = Weechat.info_get('weechat_dir', '') + '/regex_highlight.conf'
@rules = Hash.new { |h, k| h[k] = [] }
reload if File.exist?(@path)
load_hooks
end
def command_hook(_, _, args) # rubocop:disable Metrics/MethodLength
case args
when 'save' then save
when 'load' then reload
when 'list' then list
when /^add #?(?<channel>[\w]+) (?<regex>.*)/
add Regexp.last_match['channel'], Regexp.last_match['regex']
when /^del #?(?<channel>[\w]+) (?<regex>.*)/
remove Regexp.last_match['channel'], Regexp.last_match['regex']
else
Weechat.print('', "Syntax: #{HELPTEXT}")
end
Weechat::WEECHAT_RC_OK
end
def highlight_hook(_, _, modifier_data, string)
return string unless modifier_data =~ /irc;\w+\.[#\w]+;.+/
server, channel, tags = parse_modifiers(modifier_data)
return string unless match channel, string
new_tags = tags.join(',').sub 'notify_message', 'notify_highlight'
buffer = Weechat.info_get('irc_buffer', "#{server},#{channel}")
Weechat.print_date_tags(buffer, 0, new_tags, string)
''
end
private
def load_hooks
Weechat.hook_command(
'regex',
'Control regex highlights',
HELPTEXT,
'save/load dump to and load from the config file; list shows current patterns; add/del add and remove patterns', # rubocop:disable Metrics/LineLength
'save || load || list || add %(irc_channels) %- %S || del %(irc_channels) %- %S',
'command_hook',
''
)
Weechat.hook_modifier('weechat_print', 'highlight_hook', '')
end
def parse_modifiers(modifiers)
data = modifiers.split ';'
tags = data.last.split ','
server, channel = data[1].split '.', 2
[server, channel, tags]
end
def save
File.open(@path, 'w') { |fh| fh << @rules.to_yaml }
Weechat.print('', "Saved regex_highlight config to #{@path}")
end
def reload
@rules.clear
@rules.merge! File.open(@path) { |file| YAML.safe_load file }
Weechat.print('', "Loaded regex_highlight config from #{@path}")
end
def list
Weechat.print('', 'No rules loaded') if @rules.empty?
@rules.each do |channel, ruleset|
Weechat.print('', "Patterns for #{channel}")
ruleset.each { |rule| Weechat.print('', rule) }
end
end
def add(channel, regex)
if @rules[channel].include? regex
Weechat.print('', "Regex already configured for ##{channel}: #{regex}")
else
@rules[channel] << regex
Weechat.print('', "Added regex for ##{channel}: #{regex}")
end
end
def remove(channel, regex)
if @rules[channel].include? regex
@rules[channel].delete(regex)
Weechat.print('', "Removed regex for channel ##{channel}: #{regex}")
else
Weechat.print('', "This regex isn't in the list")
end
end
def match(channel, string)
@rules.each do |rule_channel, ruleset|
next unless ['ALL', channel].include? rule_channel
next unless ruleset.find { |rule| string.match rule }
return true
end
false
end
end
def weechat_init
Weechat.register(
'regex_highlight',
'Les Aker <me@lesaker.org>',
'0.1.0',
'MIT',
'Provides more flexible highlighting rules',
'', ''
)
$Regex_Highlight = RegexConfig.new
Weechat::WEECHAT_RC_OK
end
require 'forwardable'
extend Forwardable
def_delegators :$Regex_Highlight, :highlight_hook, :command_hook