# jviewers.tcl - view URLs and man pages
#
######################################################################
# Copyright 1992-1999 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.     #
######################################################################

package provide aq 0.1

# following has side-effect of making sure ::aq namespace is defined
namespace eval ::aq {
  namespace export {[a-z]*}
  catch {
    package require jldb
    # following likely to fail, because done by other libraries too
    namespace import ::jldb::shortcuts::*
  }
}

######################################################################
# ::aq::view_url url - view URL with user's preferred browser
#   this is what applications should call
######################################################################

proc ::aq::view_url { url } {
  global J_PREFS
  ::aq::default J_PREFS(web_browser) mosaic
  
  ::aq::_view_url_$J_PREFS(web_browser) $url
}

######################################################################
# ::aq::_view_url_mosaic url - view URL with Mosaic
######################################################################

proc ::aq::_view_url_mosaic { url } {
  exec Mosaic $url &
}

######################################################################
# ::aq::_view_url_netscape url - view URL with Netscape
######################################################################

# borrowed from EXMH; credits:
# martin hamilton <martin@mrrl.lut.ac.uk>
# &
# John Robert LoVerso <loverso@osf.org>
# Toivo Pedaste <toivo@ucs.uwa.edu.au>
# Fred Douglis <douglis@research.att.com>

proc ::aq::_view_url_netscape { url } {
  if [ catch { exec netscape -remote openURL($url) } tmp ] {
    if { [ string first "not running on" $tmp ] != -1 } {
      exec netscape $url &
    }
  }
}
######################################################################
# ::aq::_view_url_arena url - view URL with arena
######################################################################

# piping into jmore is a kludge to allow us to display the text that
# arena wants to print to standard out when it quits.

proc ::aq::_view_url_arena { url } {
  exec arena $url |& jmore &
}

######################################################################
# ::aq::_view_url_lynx url - view URL with lynx in an xterm
######################################################################

proc ::aq::_view_url_lynx { url } {
  exec xterm -e lynx $url &
}

######################################################################
# ::aq::view_man manpage - view man page with user's preferred browser
#   this is what applications should call
######################################################################

proc ::aq::view_man { manpage } {
  global J_PREFS
  ::aq::default J_PREFS(man_viewer) builtin
  
  ::aq::_view_man_$J_PREFS(man_viewer) $manpage
}

######################################################################
# ::aq::_view_man_builtin manpage -
#   view man page by postprocessing man(1) output
######################################################################

proc ::aq::_view_man_builtin { manpage } {
  catch {exec man $manpage |& ul -i} mantext
  ::aq::more -height 30 \
    -title [subst -nocommands -nobackslashes {Manual page for `$manpage'}] \
    -text $mantext
}

######################################################################
# ::aq::_view_man_tkman manpage - tell tkman to show us manpage
#   taken from contrib/remote.tcl in TkMan 1.7.3 distribution
######################################################################

proc ::aq::_view_man_tkman { manpage } {
  if {[set found [lsearch [winfo interps] tkman*]]==-1} {
    # if TkMan doesn't already exist, start one up
    if {[catch {exec tkman &}]} {
      ::aq::alert -text [= viewer:missing_tkman]
      return
    }
    
    # wait for it to be registered
    for {set found -1} {$found==-1} {after 200} {
      set found [lsearch [winfo interps] tkman*]
    }
    
    # wait for it to initialize
    for {set ready 0} {!$ready} {after 200} {
      catch {if {[send tkman set manx(init)]=="1"} {set ready 1}}
    }
  }
  set tkman [lindex [winfo interps] $found]
  
  # .man is the main window, guaranteed to exist
  set w .man
  send $tkman raise $w
  send $tkman wm deiconify $w
  send $tkman manShowMan $manpage

  return
}

######################################################################
# ::aq::_view_man_man manpage - view man page in an xterm
######################################################################

proc ::aq::_view_man_man { manpage } {
  exec xterm -e man $manpage &
}





