Tuesday, February 22, 2011

Re: better browsing of IRC logs

" Functions and data for uniq looking names
let b:names = []

function! Hash(str)
let num = 0
for chr in split(a:str, '\zs')
let num = (num+19+char2nr(chr)) % 216
endfor
return num+16
endfunction

function! DoName(name)
if index(b:names, a:name) >= 0 | return | endif
call add(b:names, a:name)
let key = substitute(a:name, '[^a-zA-Z0-9_@. ]', '', 'g')
let ptrn = substitute(a:name, '[^a-zA-Z0-9_@. ]', '\\S\\?', 'g')
let clr = Hash(key)
exec 'syn match c'.clr.' "\<'.ptrn.'\>"'
exec 'syn cluster ircNames add=c' . clr
exec 'hi def c'.clr.' ctermfg=' . clr
endfunction

function! DoNames()
for line in getline(0, '$')

let name = matchstr(line, '\v([\(\[]?[0-9-]* *\d\d:\d\d(:\d\d)? *[APM]*[)\]]?|^)\s*(\<[-+*@ ]?)?#?\zs[a-zA-Z0-9#|_@. \\]+\ze[:>]\s', '\1', '')
if name != ''
call DoName(name)
endif
endfor
endfunction

" Syntax
syn match ircFile "\v(\d{4}-\d{2}-\d{2}\.\d{6}([-+]\d{4}\u{3})?.txt:|^)"he=e-1 nextgroup=ircDate
syn match ircDate "\v([\(\[]?[0-9-]* *\d\d:\d\d(:\d\d)? *[APM]*[)\]]?|^)" contained skipwhite nextgroup=ircSys,ircSpeaker
syn match ircSys "\v(\w|\>|-)+[^:<]\s.*" contained contains=@ircNames
syn match ircSys "\v^Conversation.*" contained
syn match ircSpeaker "\v(\<[-+*@ ]*)?[a-zA-Z0-9#|_@. ]+[:>]\s" contained skipwhite nextgroup=ircName contains=@ircNames
syn match ircName "\v[a-zA-Z0-9#|_@.]+:\s" contained skipwhite nextgroup=ircName contains=@ircNames
syn match ircURL "\v(http://|wwww.)\S+"
syn cluster ircNames contains=NONE
call DoNames()
syn match ircIncr "\w\+++" contains=@ircNames
syn match ircDecr "\w\+--" contains=@ircNames
syn cluster ircColors contains=ircDecr,ircIncr

" Highlight
hi def link ircFile Type
hi def link ircSys Comment
hi def link ircDate Number
hi def link ircSpeaker Label
hi def link ircName Identifier
hi def link ircURL Tag
hi def ircIncr ctermfg=green
hi def ircDecr ctermfg=red

let b:current_syntax = "irc"
On 2011-02-22 12:31, Adam Monsen wrote:
> I use irssi for IRC. I like to quickly browse (local) logs (in vim)
> sometimes before sending a message. I've got a rather clunky way of
> browsing them, and I'm fishing for ideas on how to improve the process.

A while back I started a vim syntax file for IRC like logs.. There's not
really any standard log format though, so it's full of horrible regular
expression and could certainly be improved. Separating out different log
formats would be a good start.. It works in most cases though, and with
a little tweaking it appears to work on your example as well.

Anyway, it does a search and tries to color each name uniquely, which I
find quite helpful when browsing log files. It requires a 256-color
terminal though and I haven't bothered to add support for GVim yet.

Here's a TOhtml'd example from a recent trip to #vim:

http://vpaste.net/Pb1Z6?

I've attached the syntax file (vim license) if anyone's interested in
using it and/or fixing all my buggy code ;)

No comments: