#!/usr/bin/env wish8.3
# jmore - a tk-based analogue to the more(1) command
#
###############################################################################
#  Copyright 1992-2001 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(AQTOOLS_LIB)]} {
  set aq_library $env(AQTOOLS_LIB)
  set aq_pkg [file join $env(AQTOOLS_LIB) pkg]
} else {
  set aq_library /usr/lib/aq
  set aq_pkg [file join $aq_library pkg]
}

# add the aq library to the library search path:

set auto_path  [concat  [list $aq_pkg]  [list $aq_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 aq libraries.

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

## end boiler_header

# the import IS A BIG PROBLEM until this whole file gets wrapped in its own
# namespace:

catch {
  package require jldb
  # following likely to fail, because done by other libraries too
  namespace import ::jldb::shortcuts::*
}

catch {
  package require aq
  namespace eval ::aq { }			;# so namespace is defined
}

######################################################################
# all procedure definitions are autoloaded
######################################################################

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

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

global J_PREFS MOREPREFS	;# user preferences

::aq::aq_init aqmore		;# prefs, libraries, bindings...

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

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

  ::jldb::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 [::jldb::long_text JMtitle:more_prefs]

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

  ::aq::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} \
    [::aq::rule .more_prefs] {top fillx} \
    .more_prefs.font {top fillx} \
    [::aq::rule .more_prefs] {top fillx} \
    .more_prefs.size {top fillx} \
    [::aq::rule .more_prefs] {top fillx} \
    .more_prefs.b {top fillx}

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

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

  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}
}

::jldb::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 ::aq:aq_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 {
    set rich 0				;# not a rich-text file
    set wrap char
    set text {}
    set annotation {}
    if [catch {open $filename r} file_or_error] {
      ::aq::alert \
        -title "Error from [file tail $argv0]" \
        -text [subst -nocommands -nobackslashes \
          [::jldb::long_text JMerror:cant_read...]]
      exit 1
    } else {
      set file $file_or_error
      if [file isdirectory $filename] {
        ::aq::alert \
          -title "Error from [file tail $argv0]" \
          -text [subst -nocommands -nobackslashes \
            [::jldb::long_text JMerror:...is_directory]]
        exit 1
      } else {
        if {[string match *.jrt $filename] ||
            [string match *.jdoc $filename] ||
            [string match *.jhtml $filename]} {	;# SHOULDN'T BE HARDCODED
          set rich 1
          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 [::aq::more \
      -height $MOREPREFS(textheight) \
      -width $MOREPREFS(textwidth) \
      -font $MOREPREFS(textfont) \
      -wrap $wrap \
      -title [::jldb::long_text JMtitle:file...] \
      -text $text \
      -annotation $annotation]
    incr JMORE_COUNT
    bind $t <Destroy> {jmore:close_window}
    if {$rich} {
      jedit:format:configure_all_list_tags $t	;# NEEDS TO MOVE TO GENERAL LIB.
    }
  }
} else {
  global JMORE_COUNT
  set JMORE_COUNT 1
  set t [::aq::more \
    -height $MOREPREFS(textheight) \
    -width $MOREPREFS(textwidth) \
    -font $MOREPREFS(textfont) \
    -wrap $MOREPREFS(textwrap) \
    -title [::jldb::long_text 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} {
    exit 0
  }
}

