#!/usr/local/bin/wish -f
# jmore - a tk-based analogue to the more(1) command
# 
# Copyright 1993-1994 by Jay Sekora.  All rights reserved, except 
# that this file may be freely redistributed in whole or in part 
# for non-profit, noncommercial use.

## begin boiler_header

global VERSION
set VERSION {3.6/3.0}

if {[info exists env(JSTOOLS_LIB)]} {
  set jstools_library $env(JSTOOLS_LIB)
} else {
  set jstools_library /usr/local/lib/jstools
}

# add the jstools library to the library search path:

set auto_path [concat [list $jstools_library] $auto_path]

# check for ~/.tk and prepend it to the auto_path if it exists.
# that way the user can override and customise the jstools libraries.

if {[file isdirectory ~/.tk]} then {
  set auto_path [concat [list [glob ~/.tk]] $auto_path]
}

## end boiler_header

######################################################################

global NAME			;# user's login name
global HOME			;# user's home directory

global J_PREFS MOREPREFS	;# user preferences

j:jstools_init			;# prefs, libraries, bindings...

######################################################################
# jmore:cmd:prefs - preferences panel
######################################################################

proc jmore:cmd:prefs {} {
  global J_PREFS MOREPREFS env tk_strictMotif

  toplevel .more_prefs

  frame .more_prefs.wrap
  radiobutton .more_prefs.wrap.none -relief flat -anchor w \
    -text {Don't wrap lines} \
    -variable MOREPREFS(textwrap) -value none
  radiobutton .more_prefs.wrap.char -relief flat -anchor w \
    -text {Wrap lines on character boundaries} \
    -variable MOREPREFS(textwrap) -value char
  radiobutton .more_prefs.wrap.word -relief flat -anchor w \
    -text {Wrap lines at word boundaries} \
    -variable MOREPREFS(textwrap) -value word
  frame .more_prefs.font
  frame .more_prefs.font.top
  label .more_prefs.font.top.l -text {Font:}
  button .more_prefs.font.top.default -width 8 -text {Default} -command {
    set MOREPREFS(textfont) {default}
  }
  button .more_prefs.font.top.choose -text {Choose . . .} -command {
    set MOREPREFS(textfont) [j:prompt_font]
  }
  frame .more_prefs.font.bot
  entry .more_prefs.font.bot.e -relief sunken -width 50 \
    -textvariable MOREPREFS(textfont)
  frame .more_prefs.size
  label .more_prefs.size.wl -text {Width:}
  entry .more_prefs.size.we -relief sunken -width 5 \
    -textvariable MOREPREFS(textwidth)
  label .more_prefs.size.hl -text {Height:}
  entry .more_prefs.size.he -relief sunken -width 5 \
    -textvariable MOREPREFS(textheight)

  j:buttonbar .more_prefs.b -default save -buttons {
    {
      save Save {
        if {$MOREPREFS(textwidth) < 20} {set MOREPREFS(textwidth) 20}
        if {$MOREPREFS(textheight) < 4} {set MOREPREFS(textheight) 4}
        j:write_prefs -array MOREPREFS -file jmore-defaults
        exit 0
      }
    } {
      global "Global Preferences" {j:global_pref_panel}
    }
  }
  
  pack append .more_prefs.wrap \
    .more_prefs.wrap.none {top expand fillx} \
    .more_prefs.wrap.char {top expand fillx} \
    .more_prefs.wrap.word {top expand fillx}
  pack append .more_prefs.font.top \
    .more_prefs.font.top.l {left} \
    .more_prefs.font.top.choose {right padx 10 pady 5} \
    .more_prefs.font.top.default {right pady 5}
  pack append .more_prefs.font.bot \
    .more_prefs.font.bot.e {left padx 10 pady 5}
  pack append .more_prefs.font \
    .more_prefs.font.top {top expand fillx} \
    .more_prefs.font.bot {top expand fillx}
  pack append .more_prefs.size \
    .more_prefs.size.wl {left fillx} \
    .more_prefs.size.we {left} \
    .more_prefs.size.hl {left fillx} \
    .more_prefs.size.he {left}

  pack append .more_prefs \
    .more_prefs.wrap {top fillx} \
    [j:rule .more_prefs] {top fillx} \
    .more_prefs.font {top fillx} \
    [j:rule .more_prefs] {top fillx} \
    .more_prefs.size {top fillx} \
    [j:rule .more_prefs] {top fillx} \
    .more_prefs.b {top fillx}

  j:dialogue .more_prefs		;# position in centre of screen

  focus .more_prefs
  j:default_button .more_prefs.b.save \
    .more_prefs.font.bot.e \
    .more_prefs.size.we \
    .more_prefs.size.he \
    .more_prefs

  j:tab_ring \
    .more_prefs.font.bot.e \
    .more_prefs.size.we \
    .more_prefs.size.he
    
  bind .more_prefs <Key-Tab> {focus .more_prefs.font.bot.e}
  grab .more_prefs
  tkwait window .more_prefs
}


######################################################################
# FINAL SETUP
######################################################################

# read in user's .tk/jmorerc.tcl and .tk/jmore-defaults
#
j:source_config jmorerc.tcl			;# just source the file, if any
j:read_prefs -array MOREPREFS -file jmore-defaults {
  {textfont default}
  {textwidth 80}
  {textheight 30}
  {textwrap char}
}

######################################################################
######################################################################
### WHY DOES THIS HAVE TO BE HERE?  IT'S CALLED IN j:jstools_init! ###
######################################################################
######################################################################

                        # set user's text bindings:
                        global J_PREFS
                        switch -exact $J_PREFS(bindings) {
                          basic {
                            j:eb:basic_bind Entry
                          }
                          emacs {
                            j:eb:emacs_bind Entry
                          }
                          vi {
                            j:eb:vi_bind Entry
                          }
                        }

wm withdraw .

if [string match "-pref*" $argv] {
  jmore:cmd:prefs
  exit 0
}

if {$argc > 0} {
  foreach filename $argv {
    if [catch {open $filename r} file_or_error] {
      set text "Unable to open $filename for reading."
    } else {
      set file $file_or_error
      set text [read $file]
      close $file
    }
    
    set t [j:more \
      -height $MOREPREFS(textheight) \
      -width $MOREPREFS(textwidth) \
      -font $MOREPREFS(textfont) \
      -wrap $MOREPREFS(textwrap) \
      -title "File `$filename':" \
      -text $text]
    after 1 "
      tkwait window $t
      if {\[winfo children .\] == {}} {exit 0}
    "
  }
} else {
  set t [j:more \
    -height $MOREPREFS(textheight) \
    -width $MOREPREFS(textwidth) \
    -font $MOREPREFS(textfont) \
    -wrap $MOREPREFS(textwrap) \
    -title "Standard Input" \
    -text [read stdin]]
  after 1 "				;# don't need to be this elaborate...
    tkwait window $t
    if {\[winfo children .\] == {}} {exit 0}
  "
}
