1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009-2011 by Elián Hanisch <lambdae2@gmail.com>
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 # Search in Weechat buffers and logs (for Weechat 0.3.*)
22 # Inspired by xt's grep.py
23 # Originally I just wanted to add some fixes in grep.py, but then
24 # I got carried away and rewrote everything, so new script.
28 # Search in logs or buffers, see /help grep
30 # Lists logs in ~/.weechat/logs, see /help logs
33 # * plugins.var.python.grep.clear_buffer:
34 # Clear the results buffer before each search. Valid values: on, off
36 # * plugins.var.python.grep.go_to_buffer:
37 # Automatically go to grep buffer when search is over. Valid values: on, off
39 # * plugins.var.python.grep.log_filter:
40 # Coma separated list of patterns that grep will use for exclude logs, e.g.
41 # if you use '*server/*' any log in the 'server' folder will be excluded
42 # when using the command '/grep log'
44 # * plugins.var.python.grep.show_summary:
45 # Shows summary for each log. Valid values: on, off
47 # * plugins.var.python.grep.max_lines:
48 # Grep will only print the last matched lines that don't surpass the value defined here.
50 # * plugins.var.python.grep.size_limit:
51 # Size limit in KiB, is used for decide whenever grepping should run in background or not. If
52 # the logs to grep have a total size bigger than this value then grep run as a new process.
53 # It can be used for force or disable background process, using '0' forces to always grep in
54 # background, while using '' (empty string) will disable it.
56 # * plugins.var.python.grep.default_tail_head:
57 # Config option for define default number of lines returned when using --head or --tail options.
58 # Can be overriden in the command with --number option.
62 # * try to figure out why hook_process chokes in long outputs (using a tempfile as a
64 # * possibly add option for defining time intervals
68 # 2014-03-29, Felix Eckhofer <felix@tribut.de>
69 # version 0.7.3: fix typo
72 # version 0.7.2: bug fixes
76 # * use TempFile so temporal files are guaranteed to be deleted.
77 # * enable Archlinux workaround.
82 # * using --only-match shows only unique strings.
83 # * fixed bug that inverted -B -A switches when used with -t
86 # version 0.6.8: by xt <xt@bash.no>
87 # * supress highlights when printing in grep buffer
90 # version 0.6.7: by xt <xt@bash.no>
91 # * better temporary file:
92 # use tempfile.mkstemp. to create a temp file in log dir,
93 # makes it safer with regards to write permission and multi user
96 # version 0.6.6: bug fixes
97 # * use WEECHAT_LIST_POS_END in log file completion, makes completion faster
98 # * disable bytecode if using python 2.6
99 # * use single quotes in command string
100 # * fix bug that could change buffer's title when using /grep stop
103 # version 0.6.5: disable bytecode is a 2.6 feature, instead, resort to delete the bytecode manually
106 # version 0.6.4: bug fix
107 # version 0.6.3: added options --invert --only-match (replaces --exact, which is still available
108 # but removed from help)
109 # * use new 'irc_nick_color' info
110 # * don't generate bytecode when spawning a new process
111 # * show active options in buffer title
114 # version 0.6.2: removed 2.6-ish code
115 # version 0.6.1: fixed bug when grepping in grep's buffer
118 # version 0.6.0: implemented grep in background
119 # * improved context lines presentation.
120 # * grepping for big (or many) log files runs in a weechat_process.
121 # * added /grep stop.
122 # * added 'size_limit' option
123 # * fixed a infolist leak when grepping buffers
124 # * added 'default_tail_head' option
125 # * results are sort by line count
126 # * don't die if log is corrupted (has NULL chars in it)
127 # * changed presentation of /logs
128 # * log path completion doesn't suck anymore
129 # * removed all tabs, because I learned how to configure Vim so that spaces aren't annoying
130 # anymore. This was the script's original policy.
133 # version 0.5.5: rename script to 'grep.py' (FlashCode <flashcode@flashtux.org>).
136 # version 0.5.4.1: fix index error when using --after/before-context options.
139 # version 0.5.4: new features
140 # * added --after-context and --before-context options.
141 # * added --context as a shortcut for using both -A -B options.
144 # version 0.5.3: improvements for long grep output
145 # * grep buffer input accepts the same flags as /grep for repeat a search with different
147 # * tweaks in grep's output.
148 # * max_lines option added for limit grep's output.
149 # * code in update_buffer() optimized.
150 # * time stats in buffer title.
151 # * added go_to_buffer config option.
152 # * added --buffer for search only in buffers.
156 # version 0.5.2: made it python-2.4.x compliant
159 # version 0.5.1: some refactoring, show_summary option added.
162 # version 0.5: rewritten from xt's grep.py
163 # * fixed searching in non weechat logs, for cases like, if you're
164 # switching from irssi and rename and copy your irssi logs to %h/logs
165 # * fixed "timestamp rainbow" when you /grep in grep's buffer
166 # * allow to search in other buffers other than current or in logs
167 # of currently closed buffers with cmd 'buffer'
168 # * allow to search in any log file in %h/logs with cmd 'log'
169 # * added --count for return the number of matched lines
170 # * added --matchcase for case sensible search
171 # * added --hilight for color matches
172 # * added --head and --tail options, and --number
173 # * added command /logs for list files in %h/logs
174 # * added config option for clear the buffer before a search
175 # * added config option for filter logs we don't want to grep
176 # * added the posibility to repeat last search with another regexp by writing
177 # it in grep's buffer
178 # * changed spaces for tabs in the code, which is my preference
183 import sys
, getopt
, time
, os
, re
, tempfile
187 from weechat
import WEECHAT_RC_OK
, prnt
, prnt_date_tags
193 SCRIPT_AUTHOR
= "Elián Hanisch <lambdae2@gmail.com>"
194 SCRIPT_VERSION
= "0.7.3"
195 SCRIPT_LICENSE
= "GPL3"
196 SCRIPT_DESC
= "Search in buffers and logs"
197 SCRIPT_COMMAND
= "grep"
199 ### Default Settings ###
201 'clear_buffer' : 'off',
203 'go_to_buffer' : 'on',
204 'max_lines' : '4000',
205 'show_summary' : 'on',
206 'size_limit' : '2048',
207 'default_tail_head' : '10',
210 ### Class definitions ###
211 class linesDict(dict):
213 Class for handling matched lines in more than one buffer.
214 linesDict[buffer_name] = matched_lines_list
216 def __setitem__(self
, key
, value
):
217 assert isinstance(value
, list)
219 dict.__setitem
__(self
, key
, value
)
221 dict.__getitem
__(self
, key
).extend(value
)
223 def get_matches_count(self
):
224 """Return the sum of total matches stored."""
225 if dict.__len
__(self
):
226 return sum(map(lambda L
: L
.matches_count
, self
.itervalues()))
231 """Return the sum of total lines stored."""
232 if dict.__len
__(self
):
233 return sum(map(len, self
.itervalues()))
238 """Returns buffer count or buffer name if there's just one stored."""
241 return self
.keys()[0]
248 """Returns a list of items sorted by line count."""
249 items
= dict.items(self
)
250 items
.sort(key
=lambda i
: len(i
[1]))
253 def items_count(self
):
254 """Returns a list of items sorted by match count."""
255 items
= dict.items(self
)
256 items
.sort(key
=lambda i
: i
[1].matches_count
)
259 def strip_separator(self
):
260 for L
in self
.itervalues():
263 def get_last_lines(self
, n
):
264 total_lines
= len(self
)
265 #debug('total: %s n: %s' %(total_lines, n))
269 for k
, v
in reversed(self
.items()):
274 v
.stripped_lines
= l
-n
280 class linesList(list):
281 """Class for list of matches, since sometimes I need to add lines that aren't matches, I need an
282 independent counter."""
284 def __init__(self
, *args
):
285 list.__init
__(self
, *args
)
286 self
.matches_count
= 0
287 self
.stripped_lines
= 0
289 def append(self
, item
):
290 """Append lines, can be a string or a list with strings."""
291 if isinstance(item
, str):
292 list.append(self
, item
)
296 def append_separator(self
):
297 """adds a separator into the list, makes sure it doen't add two together."""
299 if (self
and self
[-1] != s
) or not self
:
307 def count_match(self
, item
=None):
308 if item
is None or isinstance(item
, str):
309 self
.matches_count
+= 1
311 self
.matches_count
+= len(item
)
313 def strip_separator(self
):
314 """removes separators if there are first or/and last in the list."""
322 ### Misc functions ###
326 return os
.stat(f
).st_size
330 sizeDict
= {0:'b', 1:'KiB', 2:'MiB', 3:'GiB', 4:'TiB'}
331 def human_readable_size(size
):
336 return '%.2f %s' %(size
, sizeDict
.get(power
, ''))
338 def color_nick(nick
):
339 """Returns coloured nick, with coloured mode if any."""
340 if not nick
: return ''
341 wcolor
= weechat
.color
342 config_string
= lambda s
: weechat
.config_string(weechat
.config_get(s
))
343 config_int
= lambda s
: weechat
.config_integer(weechat
.config_get(s
))
345 prefix
= config_string('irc.look.nick_prefix')
346 suffix
= config_string('irc.look.nick_suffix')
347 prefix_c
= suffix_c
= wcolor(config_string('weechat.color.chat_delimiters'))
348 if nick
[0] == prefix
:
351 prefix
= prefix_c
= ''
352 if nick
[-1] == suffix
:
354 suffix
= wcolor(color_delimiter
) + suffix
356 suffix
= suffix_c
= ''
360 mode
, nick
= nick
[0], nick
[1:]
361 mode_color
= wcolor(config_string('weechat.color.nicklist_prefix%d' \
362 %(modes
.find(mode
) + 1)))
364 mode
= mode_color
= ''
366 nick_color
= weechat
.info_get('irc_nick_color', nick
)
368 # probably we're in WeeChat 0.3.0
369 #debug('no irc_nick_color')
370 color_nicks_number
= config_int('weechat.look.color_nicks_number')
371 idx
= (sum(map(ord, nick
))%color
_nicks
_number
) + 1
372 nick_color
= wcolor(config_string('weechat.color.chat_nick_color%02d' %idx))
373 return ''.join((prefix_c
, prefix
, mode_color
, mode
, nick_color
, nick
, suffix_c
, suffix
))
375 ### Config and value validation ###
376 boolDict
= {'on':True, 'off':False}
377 def get_config_boolean(config
):
378 value
= weechat
.config_get_plugin(config
)
380 return boolDict
[value
]
382 default
= settings
[config
]
383 error("Error while fetching config '%s'. Using default value '%s'." %(config
, default
))
384 error("'%s' is invalid, allowed: 'on', 'off'" %value
)
385 return boolDict
[default
]
387 def get_config_int(config
, allow_empty_string
=False):
388 value
= weechat
.config_get_plugin(config
)
392 if value
== '' and allow_empty_string
:
394 default
= settings
[config
]
395 error("Error while fetching config '%s'. Using default value '%s'." %(config
, default
))
396 error("'%s' is not a number." %value
)
399 def get_config_log_filter():
400 filter = weechat
.config_get_plugin('log_filter')
402 return filter.split(',')
407 home
= weechat
.config_string(weechat
.config_get('logger.file.path'))
408 return home
.replace('%h', weechat
.info_get('weechat_dir', ''))
410 def strip_home(s
, dir=''):
411 """Strips home dir from the begging of the log path, this makes them sorter."""
421 script_nick
= SCRIPT_NAME
422 def error(s
, buffer=''):
424 prnt(buffer, '%s%s %s' %(weechat
.prefix('error'), script_nick
, s
))
425 if weechat
.config_get_plugin('debug'):
427 if traceback
.sys
.exc_type
:
428 trace
= traceback
.format_exc()
431 def say(s
, buffer=''):
433 prnt_date_tags(buffer, 0, 'no_highlight', '%s\t%s' %(script_nick
, s
))
437 ### Log files and buffers ###
438 cache_dir
= {} # note: don't remove, needed for completion if the script was loaded recently
439 def dir_list(dir, filter_list
=(), filter_excludes
=True, include_dir
=False):
440 """Returns a list of files in 'dir' and its subdirs."""
443 from fnmatch
import fnmatch
444 #debug('dir_list: listing in %s' %dir)
445 key
= (dir, include_dir
)
447 return cache_dir
[key
]
451 filter_list
= filter_list
or get_config_log_filter()
455 file = file[dir_len
:] # pattern shouldn't match home dir
456 for pattern
in filter_list
:
457 if fnmatch(file, pattern
):
458 return filter_excludes
459 return not filter_excludes
461 filter = lambda f
: not filter_excludes
464 extend
= file_list
.extend
467 for basedir
, subdirs
, files
in walk(dir):
469 # subdirs = map(lambda s : join(s, ''), subdirs)
470 # files.extend(subdirs)
471 files_path
= map(lambda f
: join(basedir
, f
), files
)
472 files_path
= [ file for file in files_path
if not filter(file) ]
476 cache_dir
[key
] = file_list
477 #debug('dir_list: got %s' %str(file_list))
480 def get_file_by_pattern(pattern
, all
=False):
481 """Returns the first log whose path matches 'pattern',
482 if all is True returns all logs that matches."""
483 if not pattern
: return []
484 #debug('get_file_by_filename: searching for %s.' %pattern)
485 # do envvar expandsion and check file
486 file = path
.expanduser(pattern
)
487 file = path
.expandvars(file)
488 if path
.isfile(file):
490 # lets see if there's a matching log
492 file = path
.join(home_dir
, pattern
)
493 if path
.isfile(file):
496 from fnmatch
import fnmatch
498 file_list
= dir_list(home_dir
)
500 for log
in file_list
:
502 if fnmatch(basename
, pattern
):
504 #debug('get_file_by_filename: got %s.' %file)
510 def get_file_by_buffer(buffer):
511 """Given buffer pointer, finds log's path or returns None."""
512 #debug('get_file_by_buffer: searching for %s' %buffer)
513 infolist
= weechat
.infolist_get('logger_buffer', '', '')
514 if not infolist
: return
516 while weechat
.infolist_next(infolist
):
517 pointer
= weechat
.infolist_pointer(infolist
, 'buffer')
518 if pointer
== buffer:
519 file = weechat
.infolist_string(infolist
, 'log_filename')
520 if weechat
.infolist_integer(infolist
, 'log_enabled'):
521 #debug('get_file_by_buffer: got %s' %file)
524 # debug('get_file_by_buffer: got %s but log not enabled' %file)
526 #debug('infolist gets freed')
527 weechat
.infolist_free(infolist
)
529 def get_file_by_name(buffer_name
):
530 """Given a buffer name, returns its log path or None. buffer_name should be in 'server.#channel'
531 or '#channel' format."""
532 #debug('get_file_by_name: searching for %s' %buffer_name)
533 # common mask options
534 config_masks
= ('logger.mask.irc', 'logger.file.mask')
535 # since there's no buffer pointer, we try to replace some local vars in mask, like $channel and
536 # $server, then replace the local vars left with '*', and use it as a mask for get the path with
537 # get_file_by_pattern
538 for config
in config_masks
:
539 mask
= weechat
.config_string(weechat
.config_get(config
))
540 #debug('get_file_by_name: mask: %s' %mask)
542 mask
= mask
.replace('$name', buffer_name
)
543 elif '$channel' in mask
or '$server' in mask
:
544 if '.' in buffer_name
and \
545 '#' not in buffer_name
[:buffer_name
.find('.')]: # the dot isn't part of the channel name
546 # ^ I'm asuming channel starts with #, i'm lazy.
547 server
, channel
= buffer_name
.split('.', 1)
549 server
, channel
= '*', buffer_name
550 if '$channel' in mask
:
551 mask
= mask
.replace('$channel', channel
)
552 if '$server' in mask
:
553 mask
= mask
.replace('$server', server
)
554 # change the unreplaced vars by '*'
555 from string
import letters
557 # vars for time formatting
558 mask
= mask
.replace('%', '$')
560 masks
= mask
.split('$')
561 masks
= map(lambda s
: s
.lstrip(letters
), masks
)
562 mask
= '*'.join(masks
)
565 #debug('get_file_by_name: using mask %s' %mask)
566 file = get_file_by_pattern(mask
)
567 #debug('get_file_by_name: got file %s' %file)
572 def get_buffer_by_name(buffer_name
):
573 """Given a buffer name returns its buffer pointer or None."""
574 #debug('get_buffer_by_name: searching for %s' %buffer_name)
575 pointer
= weechat
.buffer_search('', buffer_name
)
578 infolist
= weechat
.infolist_get('buffer', '', '')
579 while weechat
.infolist_next(infolist
):
580 short_name
= weechat
.infolist_string(infolist
, 'short_name')
581 name
= weechat
.infolist_string(infolist
, 'name')
582 if buffer_name
in (short_name
, name
):
583 #debug('get_buffer_by_name: found %s' %name)
584 pointer
= weechat
.buffer_search('', name
)
587 weechat
.infolist_free(infolist
)
588 #debug('get_buffer_by_name: got %s' %pointer)
591 def get_all_buffers():
592 """Returns list with pointers of all open buffers."""
594 infolist
= weechat
.infolist_get('buffer', '', '')
595 while weechat
.infolist_next(infolist
):
596 buffers
.append(weechat
.infolist_pointer(infolist
, 'pointer'))
597 weechat
.infolist_free(infolist
)
598 grep_buffer
= weechat
.buffer_search('python', SCRIPT_NAME
)
599 if grep_buffer
and grep_buffer
in buffers
:
600 # remove it from list
601 del buffers
[buffers
.index(grep_buffer
)]
605 def make_regexp(pattern
, matchcase
=False):
606 """Returns a compiled regexp."""
607 if pattern
in ('.', '.*', '.?', '.+'):
608 # because I don't need to use a regexp if we're going to match all lines
610 # matching takes a lot more time if pattern starts or ends with .* and it isn't needed.
611 if pattern
[:2] == '.*':
612 pattern
= pattern
[2:]
613 if pattern
[-2:] == '.*':
614 pattern
= pattern
[:-2]
617 regexp
= re
.compile(pattern
, re
.IGNORECASE
)
619 regexp
= re
.compile(pattern
)
621 raise Exception, 'Bad pattern, %s' %e
624 def check_string(s
, regexp
, hilight
='', exact
=False):
625 """Checks 's' with a regexp and returns it if is a match."""
630 matchlist
= regexp
.findall(s
)
632 if isinstance(matchlist
[0], tuple):
633 # join tuples (when there's more than one match group in regexp)
634 return [ ' '.join(t
) for t
in matchlist
]
638 matchlist
= regexp
.findall(s
)
640 if isinstance(matchlist
[0], tuple):
642 matchlist
= [ item
for L
in matchlist
for item
in L
if item
]
643 matchlist
= list(set(matchlist
)) # remove duplicates if any
645 color_hilight
, color_reset
= hilight
.split(',', 1)
647 s
= s
.replace(m
, '%s%s%s' % (color_hilight
, m
, color_reset
))
650 # no need for findall() here
651 elif regexp
.search(s
):
654 def grep_file(file, head
, tail
, after_context
, before_context
, count
, regexp
, hilight
, exact
, invert
):
655 """Return a list of lines that match 'regexp' in 'file', if no regexp returns all lines."""
657 tail
= head
= after_context
= before_context
= False
660 before_context
= after_context
= False
664 #debug(' '.join(map(str, (file, head, tail, after_context, before_context))))
667 # define these locally as it makes the loop run slightly faster
668 append
= lines
.append
669 count_match
= lines
.count_match
670 separator
= lines
.append_separator
673 if check_string(s
, regexp
, hilight
, exact
):
678 check
= lambda s
: check_string(s
, regexp
, hilight
, exact
)
681 file_object
= open(file, 'r')
685 if tail
or before_context
:
686 # for these options, I need to seek in the file, but is slower and uses a good deal of
687 # memory if the log is too big, so we do this *only* for these options.
688 file_lines
= file_object
.readlines()
691 # instead of searching in the whole file and later pick the last few lines, we
692 # reverse the log, search until count reached and reverse it again, that way is a lot
695 # don't invert context switches
696 before_context
, after_context
= after_context
, before_context
699 before_context_range
= range(1, before_context
+ 1)
700 before_context_range
.reverse()
705 while line_idx
< len(file_lines
):
706 line
= file_lines
[line_idx
]
712 for id in before_context_range
:
714 context_line
= file_lines
[line_idx
- id]
715 if check(context_line
):
716 # match in before context, that means we appended these same lines in a
717 # previous match, so we delete them merging both paragraphs
719 del lines
[id - before_context
- 1:]
729 while id < after_context
+ offset
:
732 context_line
= file_lines
[line_idx
+ id]
733 _context_line
= check(context_line
)
736 context_line
= _context_line
# so match is hilighted with --hilight
743 if limit
and lines
.matches_count
>= limit
:
753 for line
in file_object
:
756 count
or append(line
)
760 while id < after_context
+ offset
:
763 context_line
= file_object
.next()
764 _context_line
= check(context_line
)
767 context_line
= _context_line
769 count
or append(context_line
)
770 except StopIteration:
773 if limit
and lines
.matches_count
>= limit
:
779 def grep_buffer(buffer, head
, tail
, after_context
, before_context
, count
, regexp
, hilight
, exact
,
781 """Return a list of lines that match 'regexp' in 'buffer', if no regexp returns all lines."""
784 tail
= head
= after_context
= before_context
= False
787 before_context
= after_context
= False
788 #debug(' '.join(map(str, (tail, head, after_context, before_context, count, exact, hilight))))
790 # Using /grep in grep's buffer can lead to some funny effects
791 # We should take measures if that's the case
792 def make_get_line_funcion():
793 """Returns a function for get lines from the infolist, depending if the buffer is grep's or
795 string_remove_color
= weechat
.string_remove_color
796 infolist_string
= weechat
.infolist_string
797 grep_buffer
= weechat
.buffer_search('python', SCRIPT_NAME
)
798 if grep_buffer
and buffer == grep_buffer
:
799 def function(infolist
):
800 prefix
= infolist_string(infolist
, 'prefix')
801 message
= infolist_string(infolist
, 'message')
802 if prefix
: # only our messages have prefix, ignore it
806 infolist_time
= weechat
.infolist_time
807 def function(infolist
):
808 prefix
= string_remove_color(infolist_string(infolist
, 'prefix'), '')
809 message
= string_remove_color(infolist_string(infolist
, 'message'), '')
810 date
= infolist_time(infolist
, 'date')
811 return '%s\t%s\t%s' %(date
, prefix
, message
)
813 get_line
= make_get_line_funcion()
815 infolist
= weechat
.infolist_get('buffer_lines', buffer, '')
817 # like with grep_file() if we need the last few matching lines, we move the cursor to
818 # the end and search backwards
819 infolist_next
= weechat
.infolist_prev
820 infolist_prev
= weechat
.infolist_next
822 infolist_next
= weechat
.infolist_next
823 infolist_prev
= weechat
.infolist_prev
826 # define these locally as it makes the loop run slightly faster
827 append
= lines
.append
828 count_match
= lines
.count_match
829 separator
= lines
.append_separator
832 if check_string(s
, regexp
, hilight
, exact
):
837 check
= lambda s
: check_string(s
, regexp
, hilight
, exact
)
840 before_context_range
= range(1, before_context
+ 1)
841 before_context_range
.reverse()
843 while infolist_next(infolist
):
844 line
= get_line(infolist
)
845 if line
is None: continue
851 for id in before_context_range
:
852 if not infolist_prev(infolist
):
854 for id in before_context_range
:
855 context_line
= get_line(infolist
)
856 if check(context_line
):
858 del lines
[id - before_context
- 1:]
862 infolist_next(infolist
)
863 count
or append(line
)
867 while id < after_context
+ offset
:
869 if infolist_next(infolist
):
870 context_line
= get_line(infolist
)
871 _context_line
= check(context_line
)
873 context_line
= _context_line
878 # in the main loop infolist_next will start again an cause an infinite loop
880 infolist_next
= lambda x
: 0
882 if limit
and lines
.matches_count
>= limit
:
884 weechat
.infolist_free(infolist
)
890 ### this is our main grep function
891 hook_file_grep
= None
892 def show_matching_lines():
894 Greps buffers in search_in_buffers or files in search_in_files and updates grep buffer with the
897 global pattern
, matchcase
, number
, count
, exact
, hilight
, invert
898 global tail
, head
, after_context
, before_context
899 global search_in_files
, search_in_buffers
, matched_lines
, home_dir
901 matched_lines
= linesDict()
902 #debug('buffers:%s \nlogs:%s' %(search_in_buffers, search_in_files))
906 if search_in_buffers
:
907 regexp
= make_regexp(pattern
, matchcase
)
908 for buffer in search_in_buffers
:
909 buffer_name
= weechat
.buffer_get_string(buffer, 'name')
910 matched_lines
[buffer_name
] = grep_buffer(buffer, head
, tail
, after_context
,
911 before_context
, count
, regexp
, hilight
, exact
, invert
)
915 size_limit
= get_config_int('size_limit', allow_empty_string
=True)
917 if size_limit
or size_limit
== 0:
918 size
= sum(map(get_size
, search_in_files
))
919 if size
> size_limit
* 1024:
921 elif size_limit
== '':
926 regexp
= make_regexp(pattern
, matchcase
)
927 for log
in search_in_files
:
928 log_name
= strip_home(log
)
929 matched_lines
[log_name
] = grep_file(log
, head
, tail
, after_context
, before_context
,
930 count
, regexp
, hilight
, exact
, invert
)
933 # we hook a process so grepping runs in background.
934 #debug('on background')
935 global hook_file_grep
, script_path
, bytecode
936 timeout
= 1000*60*5 # 5 min
938 quotify
= lambda s
: '"%s"' %s
939 files_string
= ', '.join(map(quotify
, search_in_files
))
942 # we keep the file descriptor as a global var so it isn't deleted until next grep
943 tmpFile
= tempfile
.NamedTemporaryFile(prefix
=SCRIPT_NAME
,
944 dir=weechat
.info_get('weechat_dir', ''))
945 cmd
= grep_process_cmd
%dict
(logs
=files_string
, head
=head
, pattern
=pattern
, tail
=tail
,
946 hilight
=hilight
, after_context
=after_context
, before_context
=before_context
,
947 exact
=exact
, matchcase
=matchcase
, home_dir
=home_dir
, script_path
=script_path
,
948 count
=count
, invert
=invert
, bytecode
=bytecode
, filename
=tmpFile
.name
,
949 python
=weechat
.info_get('python2_bin', '') or 'python')
952 hook_file_grep
= weechat
.hook_process(cmd
, timeout
, 'grep_file_callback', tmpFile
.name
)
955 buffer_create("Searching for '%s' in %s worth of data..." %(pattern_tmpl
,
956 human_readable_size(size
)))
960 # defined here for commodity
961 grep_process_cmd
= """%(python)s -%(bytecode)sc '
962 import sys, cPickle, os
963 sys.path.append("%(script_path)s") # add WeeChat script dir so we can import grep
964 from grep import make_regexp, grep_file, strip_home
967 regexp = make_regexp("%(pattern)s", %(matchcase)s)
970 log_name = strip_home(log, "%(home_dir)s")
971 lines = grep_file(log, %(head)s, %(tail)s, %(after_context)s, %(before_context)s,
972 %(count)s, regexp, "%(hilight)s", %(exact)s, %(invert)s)
974 fd = open("%(filename)s", "wb")
975 cPickle.dump(d, fd, -1)
978 print >> sys.stderr, e'
981 grep_stdout
= grep_stderr
= ''
982 def grep_file_callback(filename
, command
, rc
, stdout
, stderr
):
983 global hook_file_grep
, grep_stderr
, grep_stdout
985 #debug("rc: %s\nstderr: %s\nstdout: %s" %(rc, repr(stderr), repr(stdout)))
987 grep_stdout
+= stdout
989 grep_stderr
+= stderr
992 def set_buffer_error():
993 grep_buffer
= buffer_create()
994 title
= weechat
.buffer_get_string(grep_buffer
, 'title')
995 title
= title
+ ' %serror' %color
_title
996 weechat
.buffer_set(grep_buffer
, 'title', title
)
1004 elif path
.exists(filename
):
1008 fd
= open(filename
, 'rb')
1009 d
= cPickle
.load(fd
)
1010 matched_lines
.update(d
)
1012 except Exception, e
:
1020 grep_stdout
= grep_stderr
= ''
1021 hook_file_grep
= None
1022 return WEECHAT_RC_OK
1024 def get_grep_file_status():
1025 global search_in_files
, matched_lines
, time_start
1026 elapsed
= now() - time_start
1027 if len(search_in_files
) == 1:
1028 log
= '%s (%s)' %(strip_home(search_in_files
[0]),
1029 human_readable_size(get_size(search_in_files
[0])))
1031 size
= sum(map(get_size
, search_in_files
))
1032 log
= '%s log files (%s)' %(len(search_in_files
), human_readable_size(size
))
1033 return 'Searching in %s, running for %.4f seconds. Interrupt it with "/grep stop" or "stop"' \
1034 ' in grep buffer.' %(log
, elapsed
)
1037 def buffer_update():
1038 """Updates our buffer with new lines."""
1039 global pattern_tmpl
, matched_lines
, pattern
, count
, hilight
, invert
, exact
1042 buffer = buffer_create()
1043 if get_config_boolean('clear_buffer'):
1044 weechat
.buffer_clear(buffer)
1045 matched_lines
.strip_separator() # remove first and last separators of each list
1046 len_total_lines
= len(matched_lines
)
1047 max_lines
= get_config_int('max_lines')
1048 if not count
and len_total_lines
> max_lines
:
1049 weechat
.buffer_clear(buffer)
1051 def _make_summary(log
, lines
, note
):
1052 return '%s matches "%s%s%s"%s in %s%s%s%s' \
1053 %(lines
.matches_count
, color_summary
, pattern_tmpl
, color_info
,
1054 invert
and ' (inverted)' or '',
1055 color_summary
, log
, color_reset
, note
)
1058 make_summary
= lambda log
, lines
: _make_summary(log
, lines
, ' (not shown)')
1060 def make_summary(log
, lines
):
1061 if lines
.stripped_lines
:
1063 note
= ' (last %s lines shown)' %len(lines
)
1065 note
= ' (not shown)'
1068 return _make_summary(log
, lines
, note
)
1070 global weechat_format
1072 # we don't want colors if there's match highlighting
1073 format_line
= lambda s
: '%s %s %s' %split
_line
(s
)
1076 global nick_dict
, weechat_format
1077 date
, nick
, msg
= split_line(s
)
1080 nick
= nick_dict
[nick
]
1083 nick_c
= color_nick(nick
)
1084 nick_dict
[nick
] = nick_c
1086 return '%s%s %s%s %s' %(color_date
, date
, nick
, color_reset
, msg
)
1092 print_line('Search for "%s%s%s"%s in %s%s%s.' %(color_summary
, pattern_tmpl
, color_info
,
1093 invert
and ' (inverted)' or '', color_summary
, matched_lines
, color_reset
),
1095 # print last <max_lines> lines
1096 if matched_lines
.get_matches_count():
1098 # with count we sort by matches lines instead of just lines.
1099 matched_lines_items
= matched_lines
.items_count()
1101 matched_lines_items
= matched_lines
.items()
1103 matched_lines
.get_last_lines(max_lines
)
1104 for log
, lines
in matched_lines_items
:
1105 if lines
.matches_count
:
1109 weechat_format
= True
1114 if line
== linesList
._sep
:
1116 prnt(buffer, context_sep
)
1120 error("Found garbage in log '%s', maybe it's corrupted" %log
)
1121 line
= line
.replace('\x00', '')
1122 prnt_date_tags(buffer, 0, 'no_highlight', format_line(line
))
1125 if count
or get_config_boolean('show_summary'):
1126 summary
= make_summary(log
, lines
)
1127 print_line(summary
, buffer)
1130 if not count
and lines
:
1133 print_line('No matches found.', buffer)
1139 time_total
= time_end
- time_start
1140 # percent of the total time used for grepping
1141 time_grep_pct
= (time_grep
- time_start
)/time_total
*100
1142 #debug('time: %.4f seconds (%.2f%%)' %(time_total, time_grep_pct))
1143 if not count
and len_total_lines
> max_lines
:
1144 note
= ' (last %s lines shown)' %len(matched_lines
)
1147 title
= "Search in %s%s%s %s matches%s | pattern \"%s%s%s\"%s %s | %.4f seconds (%.2f%%)" \
1148 %(color_title
, matched_lines
, color_reset
, matched_lines
.get_matches_count(), note
,
1149 color_title
, pattern_tmpl
, color_reset
, invert
and ' (inverted)' or '', format_options(),
1150 time_total
, time_grep_pct
)
1151 weechat
.buffer_set(buffer, 'title', title
)
1153 if get_config_boolean('go_to_buffer'):
1154 weechat
.buffer_set(buffer, 'display', '1')
1156 # free matched_lines so it can be removed from memory
1160 """Splits log's line 's' in 3 parts, date, nick and msg."""
1161 global weechat_format
1162 if weechat_format
and s
.count('\t') >= 2:
1163 date
, nick
, msg
= s
.split('\t', 2) # date, nick, message
1165 # looks like log isn't in weechat's format
1166 weechat_format
= False # incoming lines won't be formatted
1167 date
, nick
, msg
= '', '', s
1170 msg
= msg
.replace('\t', ' ')
1171 return date
, nick
, msg
1173 def print_line(s
, buffer=None, display
=False):
1174 """Prints 's' in script's buffer as 'script_nick'. For displaying search summaries."""
1176 buffer = buffer_create()
1177 say('%s%s' %(color_info
, s
), buffer)
1178 if display
and get_config_boolean('go_to_buffer'):
1179 weechat
.buffer_set(buffer, 'display', '1')
1181 def format_options():
1182 global matchcase
, number
, count
, exact
, hilight
, invert
1183 global tail
, head
, after_context
, before_context
1185 append
= options
.append
1186 insert
= options
.insert
1188 for i
, flag
in enumerate((count
, hilight
, matchcase
, exact
, invert
)):
1193 n
= get_config_int('default_tail_head')
1207 if before_context
and after_context
and (before_context
== after_context
):
1209 append(before_context
)
1213 append(before_context
)
1216 append(after_context
)
1218 s
= ''.join(map(str, options
)).strip()
1219 if s
and s
[0] != '-':
1223 def buffer_create(title
=None):
1224 """Returns our buffer pointer, creates and cleans the buffer if needed."""
1225 buffer = weechat
.buffer_search('python', SCRIPT_NAME
)
1227 buffer = weechat
.buffer_new(SCRIPT_NAME
, 'buffer_input', '', '', '')
1228 weechat
.buffer_set(buffer, 'time_for_each_line', '0')
1229 weechat
.buffer_set(buffer, 'nicklist', '0')
1230 weechat
.buffer_set(buffer, 'title', title
or 'grep output buffer')
1231 weechat
.buffer_set(buffer, 'localvar_set_no_log', '1')
1233 weechat
.buffer_set(buffer, 'title', title
)
1236 def buffer_input(data
, buffer, input_data
):
1237 """Repeats last search with 'input_data' as regexp."""
1239 cmd_grep_stop(buffer, input_data
)
1241 return WEECHAT_RC_OK
1243 global search_in_buffers
, search_in_files
1246 if pattern
and (search_in_files
or search_in_buffers
):
1247 # check if the buffer pointers are still valid
1248 for pointer
in search_in_buffers
:
1249 infolist
= weechat
.infolist_get('buffer', pointer
, '')
1251 del search_in_buffers
[search_in_buffers
.index(pointer
)]
1252 weechat
.infolist_free(infolist
)
1254 cmd_grep_parsing(input_data
)
1255 except Exception, e
:
1256 error('Argument error, %s' %e, buffer=buffer)
1257 return WEECHAT_RC_OK
1259 show_matching_lines()
1260 except Exception, e
:
1263 error("There isn't any previous search to repeat.", buffer=buffer)
1264 return WEECHAT_RC_OK
1268 """Resets global vars."""
1269 global home_dir
, cache_dir
, nick_dict
1270 global pattern_tmpl
, pattern
, matchcase
, number
, count
, exact
, hilight
, invert
1271 global tail
, head
, after_context
, before_context
1273 head
= tail
= after_context
= before_context
= invert
= False
1274 matchcase
= count
= exact
= False
1275 pattern_tmpl
= pattern
= number
= None
1276 home_dir
= get_home()
1277 cache_dir
= {} # for avoid walking the dir tree more than once per command
1278 nick_dict
= {} # nick cache for don't calculate nick color every time
1280 def cmd_grep_parsing(args
):
1281 """Parses args for /grep and grep input buffer."""
1282 global pattern_tmpl
, pattern
, matchcase
, number
, count
, exact
, hilight
, invert
1283 global tail
, head
, after_context
, before_context
1284 global log_name
, buffer_name
, only_buffers
, all
1285 opts
, args
= getopt
.gnu_getopt(args
.split(), 'cmHeahtivn:bA:B:C:o', ['count', 'matchcase', 'hilight',
1286 'exact', 'all', 'head', 'tail', 'number=', 'buffer', 'after-context=', 'before-context=',
1287 'context=', 'invert', 'only-match'])
1288 #debug(opts, 'opts: '); debug(args, 'args: ')
1290 if args
[0] == 'log':
1292 log_name
= args
.pop(0)
1293 elif args
[0] == 'buffer':
1295 buffer_name
= args
.pop(0)
1297 def tmplReplacer(match
):
1298 """This function will replace templates with regexps"""
1299 s
= match
.groups()[0]
1300 tmpl_args
= s
.split()
1301 tmpl_key
, _
, tmpl_args
= s
.partition(' ')
1303 template
= templates
[tmpl_key
]
1304 if callable(template
):
1305 r
= template(tmpl_args
)
1307 error("Template %s returned empty string "\
1308 "(WeeChat doesn't have enough data)." %t
)
1315 args
= ' '.join(args
) # join pattern for keep spaces
1318 pattern
= _tmplRe
.sub(tmplReplacer
, args
)
1319 debug('Using regexp: %s', pattern
)
1321 raise Exception, 'No pattern for grep the logs.'
1323 def positive_number(opt
, val
):
1334 raise Exception, "argument for %s must be a positive integer." %opt
1336 for opt
, val
in opts
:
1337 opt
= opt
.strip('-')
1338 if opt
in ('c', 'count'):
1340 elif opt
in ('m', 'matchcase'):
1341 matchcase
= not matchcase
1342 elif opt
in ('H', 'hilight'):
1343 # hilight must be always a string!
1347 hilight
= '%s,%s' %(color_hilight
, color_reset
)
1348 # we pass the colors in the variable itself because check_string() must not use
1349 # weechat's module when applying the colors (this is for grep in a hooked process)
1350 elif opt
in ('e', 'exact', 'o', 'only-match'):
1353 elif opt
in ('a', 'all'):
1355 elif opt
in ('h', 'head'):
1358 elif opt
in ('t', 'tail'):
1361 elif opt
in ('b', 'buffer'):
1363 elif opt
in ('n', 'number'):
1364 number
= positive_number(opt
, val
)
1365 elif opt
in ('C', 'context'):
1366 n
= positive_number(opt
, val
)
1369 elif opt
in ('A', 'after-context'):
1370 after_context
= positive_number(opt
, val
)
1371 elif opt
in ('B', 'before-context'):
1372 before_context
= positive_number(opt
, val
)
1373 elif opt
in ('i', 'v', 'invert'):
1377 if number
is not None:
1386 n
= get_config_int('default_tail_head')
1392 def cmd_grep_stop(buffer, args
):
1393 global hook_file_grep
, pattern
, matched_lines
, tmpFile
1396 weechat
.unhook(hook_file_grep
)
1397 hook_file_grep
= None
1398 s
= 'Search for \'%s\' stopped.' %pattern
1400 grep_buffer
= weechat
.buffer_search('python', SCRIPT_NAME
)
1402 weechat
.buffer_set(grep_buffer
, 'title', s
)
1406 say(get_grep_file_status(), buffer)
1409 def cmd_grep(data
, buffer, args
):
1410 """Search in buffers and logs."""
1411 global pattern
, matchcase
, head
, tail
, number
, count
, exact
, hilight
1413 cmd_grep_stop(buffer, args
)
1415 return WEECHAT_RC_OK
1418 weechat
.command('', '/help %s' %SCRIPT_COMMAND
)
1419 return WEECHAT_RC_OK
1422 global log_name
, buffer_name
, only_buffers
, all
1423 log_name
= buffer_name
= ''
1424 only_buffers
= all
= False
1428 cmd_grep_parsing(args
)
1429 except Exception, e
:
1430 error('Argument error, %s' %e)
1431 return WEECHAT_RC_OK
1434 log_file
= search_buffer
= None
1436 log_file
= get_file_by_pattern(log_name
, all
)
1438 error("Couldn't find any log for %s. Try /logs" %log_name
)
1439 return WEECHAT_RC_OK
1441 search_buffer
= get_all_buffers()
1443 search_buffer
= get_buffer_by_name(buffer_name
)
1444 if not search_buffer
:
1445 # there's no buffer, try in the logs
1446 log_file
= get_file_by_name(buffer_name
)
1448 error("Logs or buffer for '%s' not found." %buffer_name
)
1449 return WEECHAT_RC_OK
1451 search_buffer
= [search_buffer
]
1453 search_buffer
= [buffer]
1456 global search_in_files
, search_in_buffers
1457 search_in_files
= []
1458 search_in_buffers
= []
1460 search_in_files
= log_file
1461 elif not only_buffers
:
1462 #debug(search_buffer)
1463 for pointer
in search_buffer
:
1464 log
= get_file_by_buffer(pointer
)
1465 #debug('buffer %s log %s' %(pointer, log))
1467 search_in_files
.append(log
)
1469 search_in_buffers
.append(pointer
)
1471 search_in_buffers
= search_buffer
1475 show_matching_lines()
1476 except Exception, e
:
1478 return WEECHAT_RC_OK
1480 def cmd_logs(data
, buffer, args
):
1481 """List files in Weechat's log dir."""
1484 sort_by_size
= False
1488 opts
, args
= getopt
.gnu_getopt(args
.split(), 's', ['size'])
1491 for opt
, var
in opts
:
1492 opt
= opt
.strip('-')
1493 if opt
in ('size', 's'):
1495 except Exception, e
:
1496 error('Argument error, %s' %e)
1497 return WEECHAT_RC_OK
1499 # is there's a filter, filter_excludes should be False
1500 file_list
= dir_list(home_dir
, filter, filter_excludes
=not filter)
1502 file_list
.sort(key
=get_size
)
1506 file_sizes
= map(lambda x
: human_readable_size(get_size(x
)), file_list
)
1507 # calculate column lenght
1512 column_len
= len(bigest
) + 3
1516 buffer = buffer_create()
1517 if get_config_boolean('clear_buffer'):
1518 weechat
.buffer_clear(buffer)
1519 file_list
= zip(file_list
, file_sizes
)
1520 msg
= 'Found %s logs.' %len(file_list
)
1522 print_line(msg
, buffer, display
=True)
1523 for file, size
in file_list
:
1524 separator
= column_len
and '.'*(column_len
- len(file))
1525 prnt(buffer, '%s %s %s' %(strip_home(file), separator
, size
))
1527 print_line(msg
, buffer)
1528 return WEECHAT_RC_OK
1532 def completion_log_files(data
, completion_item
, buffer, completion
):
1533 #debug('completion: %s' %', '.join((data, completion_item, buffer, completion)))
1536 completion_list_add
= weechat
.hook_completion_list_add
1537 WEECHAT_LIST_POS_END
= weechat
.WEECHAT_LIST_POS_END
1538 for log
in dir_list(home_dir
):
1539 completion_list_add(completion
, log
[l
:], 0, WEECHAT_LIST_POS_END
)
1540 return WEECHAT_RC_OK
1542 def completion_grep_args(data
, completion_item
, buffer, completion
):
1543 for arg
in ('count', 'all', 'matchcase', 'hilight', 'exact', 'head', 'tail', 'number', 'buffer',
1544 'after-context', 'before-context', 'context', 'invert', 'only-match'):
1545 weechat
.hook_completion_list_add(completion
, '--' + arg
, 0, weechat
.WEECHAT_LIST_POS_SORT
)
1546 for tmpl
in templates
:
1547 weechat
.hook_completion_list_add(completion
, '%{' + tmpl
, 0, weechat
.WEECHAT_LIST_POS_SORT
)
1548 return WEECHAT_RC_OK
1552 # template placeholder
1553 _tmplRe
= re
.compile(r
'%\{(\w+.*?)(?:\}|$)')
1554 # will match 999.999.999.999 but I don't care
1555 ipAddress
= r
'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
1556 domain
= r
'[\w-]{2,}(?:\.[\w-]{2,})*\.[a-z]{2,}'
1557 url
= r
'\w+://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?' % (domain
, ipAddress
)
1559 def make_url_regexp(args
):
1560 #debug('make url: %s', args)
1562 words
= r
'(?:%s)' %'|'.join(map(re
.escape
, args
.split()))
1563 return r
'(?:\w+://|www\.)[^\s]*%s[^\s]*(?:/[^\])>\s]*)?' %words
1567 def make_simple_regexp(pattern
):
1580 'url': make_url_regexp
,
1581 'escape': lambda s
: re
.escape(s
),
1582 'simple': make_simple_regexp
,
1587 def delete_bytecode():
1589 bytecode
= path
.join(script_path
, SCRIPT_NAME
+ '.pyc')
1590 if path
.isfile(bytecode
):
1592 return WEECHAT_RC_OK
1594 if __name__
== '__main__' and import_ok
and \
1595 weechat
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
, SCRIPT_LICENSE
, \
1596 SCRIPT_DESC
, 'delete_bytecode', ''):
1597 home_dir
= get_home()
1599 # for import ourselves
1601 script_path
= path
.dirname(__file__
)
1602 sys
.path
.append(script_path
)
1605 # check python version
1608 if sys
.version_info
> (2, 6):
1614 weechat
.hook_command(SCRIPT_COMMAND
, cmd_grep
.__doc
__,
1615 "[log <file> | buffer <name> | stop] [-a|--all] [-b|--buffer] [-c|--count] [-m|--matchcase] "
1616 "[-H|--hilight] [-o|--only-match] [-i|-v|--invert] [(-h|--head)|(-t|--tail) [-n|--number <n>]] "
1617 "[-A|--after-context <n>] [-B|--before-context <n>] [-C|--context <n> ] <expression>",
1620 log <file>: Search in one log that matches <file> in the logger path.
1621 Use '*' and '?' as wildcards.
1622 buffer <name>: Search in buffer <name>, if there's no buffer with <name> it will
1623 try to search for a log file.
1624 stop: Stops a currently running search.
1625 -a --all: Search in all open buffers.
1626 If used with 'log <file>' search in all logs that matches <file>.
1627 -b --buffer: Search only in buffers, not in file logs.
1628 -c --count: Just count the number of matched lines instead of showing them.
1629 -m --matchcase: Don't do case insensible search.
1630 -H --hilight: Colour exact matches in output buffer.
1631 -o --only-match: Print only the matching part of the line (unique matches).
1632 -v -i --invert: Print lines that don't match the regular expression.
1633 -t --tail: Print the last 10 matching lines.
1634 -h --head: Print the first 10 matching lines.
1635 -n --number <n>: Overrides default number of lines for --tail or --head.
1636 -A --after-context <n>: Shows <n> lines of trailing context after matching lines.
1637 -B --before-context <n>: Shows <n> lines of leading context before matching lines.
1638 -C --context <n>: Same as using both --after-context and --before-context simultaneously.
1639 <expression>: Expression to search.
1642 Input line accepts most arguments of /grep, it'll repeat last search using the new
1643 arguments provided. You can't search in different logs from the buffer's input.
1644 Boolean arguments like --count, --tail, --head, --hilight, ... are toggleable
1646 Python regular expression syntax:
1647 See http://docs.python.org/lib/re-syntax.html
1650 %{url [text]}: Matches anything like an url, or an url with text.
1651 %{ip}: Matches anything that looks like an ip.
1652 %{domain}: Matches anything like a domain.
1653 %{escape text}: Escapes text in pattern.
1654 %{simple pattern}: Converts a pattern with '*' and '?' wildcards into a regexp.
1657 Search for urls with the word 'weechat' said by 'nick'
1658 /grep nick\\t.*%{url weechat}
1659 Search for '*.*' string
1662 # completion template
1663 "buffer %(buffers_names) %(grep_arguments)|%*"
1664 "||log %(grep_log_files) %(grep_arguments)|%*"
1666 "||%(grep_arguments)|%*",
1668 weechat
.hook_command('logs', cmd_logs
.__doc
__, "[-s|--size] [<filter>]",
1669 "-s --size: Sort logs by size.\n"
1670 " <filter>: Only show logs that match <filter>. Use '*' and '?' as wildcards.", '--size', 'cmd_logs', '')
1672 weechat
.hook_completion('grep_log_files', "list of log files",
1673 'completion_log_files', '')
1674 weechat
.hook_completion('grep_arguments', "list of arguments",
1675 'completion_grep_args', '')
1678 for opt
, val
in settings
.iteritems():
1679 if not weechat
.config_is_set_plugin(opt
):
1680 weechat
.config_set_plugin(opt
, val
)
1683 color_date
= weechat
.color('brown')
1684 color_info
= weechat
.color('cyan')
1685 color_hilight
= weechat
.color('lightred')
1686 color_reset
= weechat
.color('reset')
1687 color_title
= weechat
.color('yellow')
1688 color_summary
= weechat
.color('lightcyan')
1689 color_delimiter
= weechat
.color('chat_delimiters')
1690 color_script_nick
= weechat
.color('chat_nick')
1693 script_nick
= '%s[%s%s%s]%s' %(color_delimiter
, color_script_nick
, SCRIPT_NAME
, color_delimiter
,
1695 script_nick_nocolor
= '[%s]' %SCRIPT_NAME
1696 # paragraph separator when using context options
1697 context_sep
= '%s\t%s--' %(script_nick
, color_info
)
1699 # -------------------------------------------------------------------------
1702 if weechat
.config_get_plugin('debug'):
1704 # custom debug module I use, allows me to inspect script's objects.
1706 debug
= pybuffer
.debugBuffer(globals(), '%s_debug' % SCRIPT_NAME
)
1708 def debug(s
, *args
):
1709 if not isinstance(s
, basestring
):
1713 prnt('', '%s\t%s' %(script_nick
, s
))
1718 # vim:set shiftwidth=4 tabstop=4 softtabstop=4 expandtab textwidth=100: