Version 2 of A little man page viewer

Updated 2004-01-21 21:02:09

if 0 {Richard Suchenwirth 2004-01-21 - As a little evening project, here's a viewer for man pages that understands (some of) the roff format. Highly experimental, nothing guaranteed, but a nice distraction :}

http://mini.net/files/man2text.jpg


 package require Tk

 set font {Times 10}
 set file [lindex $argv 0]
 wm title . "man [file tail  [file root $file]]"
 pack [scrollbar .s -command ".t yview"] -side right -fill y
 pack [text .t -wrap word -yscrollcommand ".s set" \
        -padx 10 -font $font] \
    -side left -fill both -expand 1
 foreach style {bold italic} {
    .t tag config $style -font [concat $font $style]
 }
 .t tag config right -justify right

 set fp [open $file]
 set tab ""
 foreach line [split [read $fp] \n] {
     set tag ""
     if {$line eq ".EN"} {continue}
     if {$line eq ".PP"} {set line \n\n}
     if {$line eq ".br"} {set line \n}
     if {$line eq ".ti 8"} {set line \n\t}
     if {$line eq ".in 8"} {set tab \t; continue}
     if {$line eq ".in 0"} {set tab ""; continue}
     if [regexp  {^\.SH (.+)} $line -> line] {
         set line \n\n$line
         set tag bold
     }
     if [regexp  {^\.TH (.+)} $line -> line] {
         set tag right
     }
     if [regexp  {^\.I (.+)} $line -> line] {
         set tag italic
     }
     if [regexp  {^\.B (.+)} $line -> line] {
         set tag bold
     }
     if [regexp  {^\.BI (.+)} $line -> line] {
         set tag {bold italic}
     }
     if [regexp  {^\.TP} $line] {
         set line \n\t
     }
     set line [string map {\\- - \\fB "" \\fR "" \\^ ""} $line]
    .t insert end "$tab$line " $tag
 }
 close $fp