+
+proc file-read-lines {lvar body} {
+ upvar 1 $lvar l
+ global logfile poll_after lastactivity bufdata
+
+#puts f-r-l
+ if {![info exists logfile]} {
+ return
+ }
+ while 1 {
+ if {[catch { read $logfile } got]} {
+ file-error $got
+ return
+ }
+#puts "f-r-l [string length $got]"
+ if {![string length $got] && [eof $logfile]} {
+ set ago [expr { [clock seconds] - $lastactivity }]
+ set interval [expr {( $ago < 10 ? 10 :
+ $ago > 3000 ? 3000 :
+ $ago ) * 10}]
+#puts "requeue filepoll $interval"
+ set poll_after [after $interval pollfile]
+ return
+ }
+ set lastactivity [clock seconds]
+
+ while {[regexp {^(.*?)[\r\n]+(.*)$} $got dummy lhs rhs]} {
+ set l "$bufdata$lhs"
+ set bufdata {}
+ set got $rhs
+#puts ">>$l<<"
+ uplevel 1 $body
+ }
+ append bufdata $got
+ }
+}
+
+proc file-error {emsg} {
+ global logfile
+
+ shownotice red "Error reading logfile $currentfile:\n$emsg"
+ catch { close $logfile }
+ catch { unset logfile }
+}
+
+proc pollfile {} {
+ global poll_after logfile currentfile
+ global errorCode errorInfo bufdata lastactivity
+
+ catch { after cancel $poll_after }
+ if {![string length $currentfile]} {
+ shownotice red "No log file selected. Use File / Open."
+ return
+ }
+ if {![info exists logfile]} {
+ set bufdata {}
+ set lastactivity [clock seconds]
+ if {[catch { set logfile [open $currentfile r] } emsg]} {
+ shownotice red "Error opening logfile $currentfile:\n$emsg"
+ return
+ }
+ shownotice \#000080 "Reading $currentfile"
+ if {[catch {
+ seek $logfile -1024 end
+ } emsg]} {
+ if {![string match {POSIX EINVAL *} $errorCode]} {
+ file-error $emsg
+ }
+ }
+ file-read-lines l { }
+ }
+ file-read-lines l {
+ hidenotice
+ if {[regexp {^\[\d+:\d+:\d+\] (.*)} $l dummy rhs]} {
+#puts PROCLINE
+ process-line $rhs
+ }
+ }
+}
+
+proc process-line {l} {
+ if {[regexp {^(\w+) tells ye, \"(.*)\"$} $l dummy pirate msg]} {
+#puts "MESSAGE $l"
+ message $pirate $msg
+ }
+}
+
+proc message {pirate msg} {
+ global times pirates messages height
+ global lw_ls lw_ws bell_again
+
+ set ix [lsearch -exact $pirates $pirate]
+ set now [clock seconds]
+
+ if {$bell_again > -2 &&
+ ($ix<0 || [lindex $times $ix] < $now-$bell_again)} {
+ bell -nice
+ }
+ if {$ix < 0} {
+ set cix 0
+ set oldest $now
+ foreach time $times {
+ if {$time < $oldest} {
+ set oldest $time
+ set ix $cix
+ }
+ incr cix
+ }
+ for_lw {
+ set $l [lreplace [set $l] $ix $ix]
+ lappend $l {}
+ $w delete $ix
+ $w insert end {}
+ }
+ set ix [expr {$height-1}]
+ }
+ for_lw new [list $now $pirate $msg] {
+ set $l [lreplace [set $l] $ix $ix $new]
+ $w delete $ix
+ $w insert $ix $new
+ }
+#puts "TIMES $times"
+ .pirate itemconfigure $ix -foreground white
+ retint
+}
+