#!/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 nonprofit, noncommercial use.

catch {tk colormodel . color}		;# colour even on a 2-bit display
set tk_strictMotif 1


global VERSION
set VERSION {3.6/2.0}

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

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

global PREFS MOREPREFS          ;# user preferences

set NAME $env(USER)
set HOME $env(HOME)

# check for $HOME/.tk/jlibrary.tcl and read it in if it exists.
# this contains library procedures.  it would normally be in $tk_library,
# but we check for its presence here for people who can't put things in
# $tk_library.
#
if {[file isfile "$HOME/.tk/jlibrary.tcl"]} then {
  source "$HOME/.tk/jlibrary.tcl"
}
# now check for some other library files which might be there (again,
# these would normally be in $tk_library):
j:source_config jrichtext.tcl
j:source_config jabout.tcl
j:source_config jbindings.tcl

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

proc jmore:cmd:prefs {} {
  global 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
}

######################################################################
# read in user's text bindings.
######################################################################

j:source_config -directory $HOME .textbindings.tcl

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

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

switch -exact $PREFS(bindings) {
  basic {
    j:eb:basic_bind Entry
    j:tb:basic_bind Text
  }
  emacs {
    j:eb:emacs_bind Entry
    j:tb:emacs_bind Text
  }
}

wm withdraw .

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

if {$argc > 0} {
  foreach filename $argv {
    set file [open $filename r]
    set t [j:more \
      -height $MOREPREFS(textheight) \
      -width $MOREPREFS(textwidth) \
      -font $MOREPREFS(textfont) \
      -wrap $MOREPREFS(textwrap) \
      -title "File `$filename':" \
      -text [read $file]]
    close $file
    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}
  "
}
