#!/usr/local/bin/wish4.0
# jmore - a tk-based analogue to the more(1) command
#
# Copyright 1992-1995 by Jay Sekora.  This file may be freely distributed,
# modified or unmodified, for any purpose, provided that this copyright
# notice is retained verbatim in all copies and no attempt is made to
# obscure the authorship of this file.  If you distribute any modified
# versions, I ask, but do not require, that you clearly mark any changes
# you make as such and that you provide your users with instructions for
# getting the original sources.
## begin boiler_header

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 jmore		;# prefs, libraries, bindings...

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

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

  j:ldb:set_defaults {
    {JMtitle:more_prefs {More Preferences}}
    {JMpref:dont_wrap {Don't wrap lines}}
    {JMpref:char_wrap {Wrap lines on character boundaries}}
    {JMpref:word_wrap {Wrap lines at word boundaries}}
    {{Font:} {Font:}}
    {JMpref:font_default {Default}}
    {JMpref:font_choose {Choose...}}
    {{Width:} {Width:}}
    {{Height:} {Height:}}
    {{Save} {Save}}
    {{Global Preferences} {Global Preferences}}
  }
  
  toplevel .more_prefs
  wm title .more_prefs [j:ldb JMtitle:more_prefs]

  frame .more_prefs.wrap
  radiobutton .more_prefs.wrap.none -anchor w \
    -text [j:ldb JMpref:dont_wrap] \
    -variable MOREPREFS(textwrap) -value none
  radiobutton .more_prefs.wrap.char -anchor w \
    -text [j:ldb JMpref:char_wrap] \
    -variable MOREPREFS(textwrap) -value char
  radiobutton .more_prefs.wrap.word -anchor w \
    -text [j:ldb JMpref:word_wrap] \
    -variable MOREPREFS(textwrap) -value word
  frame .more_prefs.font
  frame .more_prefs.font.top
  label .more_prefs.font.top.l -text [j:ldb {Font:}]
  button .more_prefs.font.top.default -width 8 \
    -text [j:ldb JMpref:font_default] -command {
    set MOREPREFS(textfont) {default}
  }
  button .more_prefs.font.top.choose -width 8\
    -text [j:ldb JMpref:font_choose] -command {
    set MOREPREFS(textfont) [j:prompt_font]
  }
  frame .more_prefs.font.bot
  entry .more_prefs.font.bot.e -width 50 \
    -textvariable MOREPREFS(textfont)
  frame .more_prefs.size
  label .more_prefs.size.wl -text [j:ldb {Width:}]
  entry .more_prefs.size.we -width 5 \
    -textvariable MOREPREFS(textwidth)
  label .more_prefs.size.hl -text [j:ldb {Height:}]
  entry .more_prefs.size.he -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}
}

j:ldb:set_defaults {
  {JMerror:cant_read... {Unable to open `$filename' for reading.}}
  {JMerror:...is_directory {`$filename' is a directory, not a regular file.}}
  {JMtitle:file... {File `$filename'}}
  {JMtitle:stdin {Standard Input}}
}

######################################################################
######################################################################
### 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} {
  global JMORE_COUNT
  set JMORE_COUNT 0
  foreach filename $argv {
    if [catch {open $filename r} file_or_error] {
      set text [j:ldb JMerror:cant_read...]
    } else {
      set file $file_or_error
      if [file isdirectory $filename] {
        set text [j:ldb JMerror:...is_directory]
      } else {
        if {[string match *.jrt $filename] ||
            [string match *.jdoc $filename]} {	;# SHOULDN'T BE HARDCODED
          set contents [read $file]
          set text [lindex $contents 0]
          set annotation [lindex $contents 1]
          set wrap word
        } else {
          set text [read $file]
          set annotation ""
          set wrap $MOREPREFS(textwrap)
        }
      }
      close $file
    }
    
    set t [j:more \
      -height $MOREPREFS(textheight) \
      -width $MOREPREFS(textwidth) \
      -font $MOREPREFS(textfont) \
      -wrap $wrap \
      -title [j:ldb JMtitle:file...] \
      -text $text \
      -annotation $annotation]
    incr JMORE_COUNT
    bind $t <Destroy> {jmore:close_window}
  }
} else {
  global JMORE_COUNT
  set JMORE_COUNT 1
  set t [j:more \
    -height $MOREPREFS(textheight) \
    -width $MOREPREFS(textwidth) \
    -font $MOREPREFS(textfont) \
    -wrap $MOREPREFS(textwrap) \
    -title [j:ldb JMtitle:stdin] \
    -text [read stdin]]
  bind $t <Destroy> {jmore:close_window}
}

proc jmore:close_window {} {
  global JMORE_COUNT
  incr JMORE_COUNT -1
  if {$JMORE_COUNT == 0} {
    j:tk4 {
      exit 0
    }
    j:tk3 {
      after 1000 {exit 0}
    }
  }
}









