#!/usr/local/bin/wish
global env gv

set gv(version)		"Visual Edit (Version 1.3 10dec96)"
set gv(program)		vedit
set gv(iconname)	[string toupper $gv(program)]
set gv(top)		.$gv(program)
set gv(author)		"John van Gulik"

proc errmess { pw {msg ""} } {
	global env gv
	pack [label .errmess -text $msg] -side top -expand true -fill both
	pack [button .ok -text "OK" -command "destroy .ok"] -side top
	tkwait window .ok
}

##############################################################################
#####	Set the required environment variables
##############################################################################

if {[info exists env(HOME)] && $env(HOME)=="/"} {set env(HOME) "/root"}

if {$tcl_platform(platform)=="windows"} {
	if {![info exists env(TKAPPS)]} {
		set env(TKAPPS) /TKAPPS
	}
	if {![file isdirectory $env(TKAPPS)]} {
		errmess . "Directory $env(TKAPPS) not found"
		exit 1
	}
} elseif {$tcl_platform(platform)=="unix"} {
	if {![info exists env(TKAPPS)]} {
		if {[file isdirectory $env(HOME)/TKAPPS]} {
			set env(TKAPPS) $env(HOME)/TKAPPS
		} elseif {[file isdirectory /opt/TKAPPS]} {
			set env(TKAPPS) /opt/TKAPPS
		} elseif {[file isdirectory /apps/TKAPPS]} {
			set env(TKAPPS) /apps/TKAPPS
		} else {
			set env(TKAPPS) /usr/local/TKAPPS
		}
	}
	if {![file isdirectory $env(TKAPPS)]} {
		errmess . "Directory $env(TKAPPS) not found"
		exit 1
	}
} else {
	errmess . "This application is not supported under $tcl_platform(platform)"
	exit 1
}

set auto_path [linsert $auto_path 0 $env(TKAPPS)/lib]

#wm withdraw .

standard_startup $gv(program) $gv(version)

##############################################################################
#####	Set required variables and options
##############################################################################

global textedit_global

##############################################################################
#####	SUBROUTINES
##############################################################################

proc vedit_loadfile { filename } {
	global env gv

	regsub -all { } $filename {} filename

	if {$filename==""} {return 0}

	set gv(filename) $filename
	wm iconname $gv(top) [file tail $gv(filename)]

	if {![text_load $gv(top).view.txt $gv(filename)]} {return 0}
	return 1
}

proc vedit_savefile { filename } {
	global env gv

	regsub -all { } $filename {} filename

	if {$filename==""} {return 0}

	set gv(filename) $filename
	wm iconname $gv(top) [file tail $gv(filename)]

	if {![text_save $gv(top).view.txt $gv(filename)]} {return 0}
	return 1
}

proc text_html {tw action args} {
	global env gv

	if {$action=="add"} {
		if {[catch {set i [$tw index sel.first]}]} {
			$tw insert insert [join $args {}]
		} else {
			if {[llength $args]>1} {
				$tw insert sel.last [join [lrange $args 1 end] {}]
			}
			if {[llength $args]>0} {
				$tw insert $i [lindex $args 0]
			}
		}
	} elseif {$action=="table"} {
		if {[catch {set i [$tw index sel.first]}]} {
			$tw insert insert {<table border>
<th>header1</th><th>header2</th><th>header3</th>
<tr><td>row1col1</td><td>row1col2</td><td>row1col3</td></tr>
<tr><td>row2col1</td><td>row2col2</td><td>row2col3</td></tr>
</table border>
}
		} else {
			set res [$tw get sel.first sel.last]
			$tw delete sel.first sel.last
			set table "<table border>\n"
			set linenum 0
			foreach line [split $res "\n"] {
				incr linenum
				if {$linenum==1} {
					foreach field [split $line "\t"] {
						append table "<th>$field</th>\n"
					}
				} else {
					append table "<tr>\n"
					foreach field [split $line "\t"] {
						append table "	<td>$field</td>\n"
					}
					append table "</tr>\n"
				}
			}
			$tw insert $i "$table</table border>"
		}
	} elseif {$action=="show"} {
		if {![vedit_savefile $gv(filename)]} {
			errmess $gv(top) "You must save the file before it can be viewed"
			return
		}
		if {[file tail $gv(filename)]==$gv(filename)} {
			viewfile [pwd]/$gv(filename)
		} else {
			viewfile $gv(filename)
		}
	} else {
		errmess $gv(top) "Unknown HTML action $action"
	}
	return
}

##############################################################################
#####	MAIN PROGRAM
##############################################################################

toplevel	$gv(top)
wm iconname	$gv(top) $gv(iconname)
wm iconbitmap	$gv(top) @$env(TKAPPS)/bitmaps/$gv(program).xbm
wm iconmask	$gv(top) @$env(TKAPPS)/bitmaps/$gv(program).msk
wm title	$gv(top) $gv(version)
wm minsize	$gv(top) 100 100
wm protocol	$gv(top) WM_DELETE_WINDOW exit

pack [label $gv(top).help] \
	-side bottom -fill x
bind_help $gv(top).help $gv(top).help \
	[lang "Help text" "Texte d'aide"]

pack [frame $gv(top).b1] \
        -side top -fill x
pack [menubutton $gv(top).b1.file -menu $gv(top).b1.file.m \
	-text [lang "File..." "Fichier..."] ] \
        -side left -fill y
pack [menubutton $gv(top).b1.edit -menu $gv(top).b1.edit.m \
	-text [lang "Edit..." "Modifier..."] ] \
	-side left -fill y
menubutton $gv(top).b1.html -menu $gv(top).b1.html.m \
	-text [lang "HTML..." "FRENCH"]
pack [button $gv(top).b1.print -text [lang "Print" "FRENCH"] \
	-command "if \{\[print_select $gv(top)\]\} \{print_text \"\[$gv(top).view.txt get 1.0 end\]\" \"Printed by: $env(USER)\" \"\$gv(filename)\"\}" ] \
	-side left
pack [button $gv(top).b1.spell -text [lang "Spell Check" "Orthographe"] \
	-command "tkispell_text $gv(top).view.txt" ] \
	-side left
pack [entry $gv(top).b1.filename -textvariable gv(filename) ] \
	-side left -expand true -fill x
pack [button $gv(top).b1.save -text [lang "Save & Exit" "Sauvegarder et quitter"] \
	-command "if \{\[vedit_savefile \$gv(filename)\]\} \{exit\}" ] \
	-side left
pack [button $gv(top).b1.iconify -text [lang "Iconify" "Minimiser"] \
	-command "wm iconify $gv(top)"] \
        -side left
pack [button $gv(top).b1.exit -text [lang "Exit" "Abandonner"] \
	-command "exit"] \
        -side left

bind_help $gv(top).b1.file $gv(top).help \
        [lang "Standard file menu" "Menu standard du fichier"]
bind_help $gv(top).b1.edit $gv(top).help \
        [lang "Extra edit functions" "FRENCH"]
bind_help $gv(top).b1.html $gv(top).help \
        [lang "HTML edit functions" "FRENCH"]
bind_help $gv(top).b1.spell	$gv(top).help \
	[lang "Check spelling" "Vrifier l'orthographe"]
bind_help $gv(top).b1.save $gv(top).help \
        [lang "Save current file and exit program" "FRENCH"]
bind_help $gv(top).b1.iconify $gv(top).help \
        [lang "Iconify window" "Minimiser cette fentre"]
bind_help $gv(top).b1.exit $gv(top).help \
        [lang "Close this window" "Fermer cette fentre"]

menu $gv(top).b1.file.m
$gv(top).b1.file.m add command -label [lang "Load file ..." "Chargement de fichier..."] \
	-command "prompt -master $gv(top) -directory \[pwd\] -command vedit_loadfile"
$gv(top).b1.file.m add command -label [lang "Save changes" "Sauvegarder les changements"] \
	-command "vedit_savefile \$gv(filename)"
$gv(top).b1.file.m add command -label [lang "Save as ..." "Sauvegarder sous ..."] \
	-command "prompt -master $gv(top) -directory \[pwd\] -access w -command vedit_savefile"
$gv(top).b1.file.m add separator
$gv(top).b1.file.m add checkbutton -label [lang "HTML Functions" "FRENCH"] \
	-variable gv(mode) -onvalue "html" -offvalue "text" \
	-command {
		if {$gv(mode)=="html"} {
			pack $gv(top).b1.html -after $gv(top).b1.edit \
				-side left -fill y
		} else {
			pack forget $gv(top).b1.html
		}
	}
$gv(top).b1.file.m add separator
$gv(top).b1.file.m add command -label [lang "Help" "Aide"] \
	-command "help $env(TKAPPS)/help/$gv(program)"
$gv(top).b1.file.m add command -label [lang "Fonts/Colors" "Fontes/Couleurs"] \
	-command "background tkcustom $gv(program)"
$gv(top).b1.file.m add command -label [lang "Iconify" "Minimiser"] \
	-command "wm iconify $gv(top)"
$gv(top).b1.file.m add command -label [lang "Exit" "Abandonner"] \
	-command "exit"

menu $gv(top).b1.edit.m
$gv(top).b1.edit.m add command -label [lang "Cut" "Couper"] \
	-command "text_cut $gv(top).view.txt"
$gv(top).b1.edit.m add command -label [lang "Copy" "Copier"] \
	-command "text_copy $gv(top).view.txt"
$gv(top).b1.edit.m add command -label [lang "Paste" "Coller"] \
	-command "text_paste $gv(top).view.txt"
$gv(top).b1.edit.m add separator
$gv(top).b1.edit.m add command -label [lang "Lower case" "Minuscule"] \
	-command "text_lower $gv(top).view.txt"
$gv(top).b1.edit.m add command -label [lang "Upper case" "Majuscule"] \
	-command "text_upper $gv(top).view.txt"
$gv(top).b1.edit.m add separator
$gv(top).b1.edit.m add command -label [lang "Word wrap and indent text" "FRENCH"] \
                -command "text_quote $gv(top).view.txt \"\[getstring $gv(top) {Indent string :}\]\""
$gv(top).b1.edit.m add command -label [lang "Word wrap" "Retour de chariot"] \
	-command "text_wordwrap $gv(top).view.txt"
$gv(top).b1.edit.m add command -label [lang "Join lines" "Combinaison de ligne"] \
	-command "text_join $gv(top).view.txt"

menu $gv(top).b1.html.m
$gv(top).b1.html.m add command -label [lang "Save changes and View HTML" "FRENCH"] \
	-command "text_html $gv(top).view.txt show"
$gv(top).b1.html.m add command -label [lang "New HTML Document" "FRENCH"] \
	-command "text_html $gv(top).view.txt add {<html>\n<head>\n<title>} \"</title>\n</head>\n\n<body>\n\n\n\n\n<hr>\n\[clock format \[clock seconds\]\] - $env(FULLNAME)\n</body></html>\""
$gv(top).b1.html.m add cascade -label [lang "Headers..." "FRENCH"] \
	-menu $gv(top).b1.html.m.h
$gv(top).b1.html.m add cascade -label [lang "Markup..." "FRENCH"] \
	-menu $gv(top).b1.html.m.m
$gv(top).b1.html.m add cascade -label [lang "Lists..." "FRENCH"] \
	-menu $gv(top).b1.html.m.l
$gv(top).b1.html.m add cascade -label [lang "Netscape Extensions..." "FRENCH"] \
	-menu $gv(top).b1.html.m.n
$gv(top).b1.html.m add cascade -label [lang "Extras..." "FRENCH"] \
	-menu $gv(top).b1.html.m.e


menu $gv(top).b1.html.m.h
$gv(top).b1.html.m.h add command -label [lang "Header 1" "FRENCH"] \
	-accelerator {<h1>} \
	-command "text_html $gv(top).view.txt add {<h1>} {</h1>}"
$gv(top).b1.html.m.h add command -label [lang "Header 2" "FRENCH"] \
	-accelerator {<h2>} \
	-command "text_html $gv(top).view.txt add {<h2>} {</h2>}"
$gv(top).b1.html.m.h add command -label [lang "Header 3" "FRENCH"] \
	-accelerator {<h3>} \
	-command "text_html $gv(top).view.txt add {<h3>} {</h3>}"
$gv(top).b1.html.m.h add command -label [lang "Header 4" "FRENCH"] \
	-accelerator {<h4>} \
	-command "text_html $gv(top).view.txt add {<h4>} {</h4>}"
$gv(top).b1.html.m.h add command -label [lang "Header 5" "FRENCH"] \
	-accelerator {<h5>} \
	-command "text_html $gv(top).view.txt add {<h5>} {</h5>}"
$gv(top).b1.html.m.h add command -label [lang "Header 6" "FRENCH"] \
	-accelerator {<h6>} \
	-command "text_html $gv(top).view.txt add {<h6>} {</h6>}"

menu $gv(top).b1.html.m.m
$gv(top).b1.html.m.m add command -label [lang "Bold" "FRENCH"] \
	-accelerator {<b>} \
	-command "text_html $gv(top).view.txt add {<b>} {</b>}"
$gv(top).b1.html.m.m add command -label [lang "Italic" "FRENCH"] \
	-accelerator {<i>} \
	-command "text_html $gv(top).view.txt add {<i>} {</i>}"
$gv(top).b1.html.m.m add command -label [lang "Underline" "FRENCH"] \
	-accelerator {<u>} \
	-command "text_html $gv(top).view.txt add {<u>} {</u>}"
$gv(top).b1.html.m.m add command -label [lang "Cite" "FRENCH"] \
	-accelerator {<cite>} \
	-command "text_html $gv(top).view.txt add {<cite>} {</cite>}"
$gv(top).b1.html.m.m add command -label [lang "Emphasis" "FRENCH"] \
	-accelerator {<em>} \
	-command "text_html $gv(top).view.txt add {<em>} {</em>}"
$gv(top).b1.html.m.m add command -label [lang "Strong" "FRENCH"] \
	-accelerator {<strong>} \
	-command "text_html $gv(top).view.txt add {<strong>} {</strong>}"
$gv(top).b1.html.m.m add command -label [lang "Typewriter font" "FRENCH"] \
	-accelerator {<tt>} \
	-command "text_html $gv(top).view.txt add {<tt>} {</tt>}"

menu $gv(top).b1.html.m.l
$gv(top).b1.html.m.l add command -label [lang "Ordered List" "FRENCH"] \
	-accelerator {<ol>} \
	-command "text_html $gv(top).view.txt add {<ol>\n<li>	} {\n</ol>\n}"
$gv(top).b1.html.m.l add command -label [lang "Unordered List" "FRENCH"] \
	-accelerator {<ul>} \
	-command "text_html $gv(top).view.txt add {<ul>\n<li>	} {\n</ul>\n}"
$gv(top).b1.html.m.l add command -label [lang "List Item" "FRENCH"] \
	-accelerator {<li>} \
	-command "text_html $gv(top).view.txt add {<li>	} {\n}"
$gv(top).b1.html.m.l add separator
$gv(top).b1.html.m.l add command -label [lang "Definition List" "FRENCH"] \
	-accelerator {<dl>} \
	-command "text_html $gv(top).view.txt add {<dl>\n<dt>	} {\n<dd>	\n</dl>\n}"
$gv(top).b1.html.m.l add command -label [lang "Definition Title" "FRENCH"] \
	-accelerator {<dt>} \
	-command "text_html $gv(top).view.txt add {<dt>	} {\n}"
$gv(top).b1.html.m.l add command -label [lang "Definition Description" "FRENCH"] \
	-accelerator {<dd>} \
	-command "text_html $gv(top).view.txt add {<dd>	} {\n}"

menu $gv(top).b1.html.m.n
$gv(top).b1.html.m.n add command -label [lang "Table" "FRENCH"] \
	-accelerator {<table>} \
	-command "text_html $gv(top).view.txt table"

menu $gv(top).b1.html.m.e
$gv(top).b1.html.m.e add command -label [lang "Comment" "FRENCH"] \
	-accelerator {<!-->} \
	-command "text_html $gv(top).view.txt add {<!--} {>}"
$gv(top).b1.html.m.e add command -label [lang "HTML Reference" "FRENCH"] \
	-accelerator {<a href>} \
	-command "text_html $gv(top).view.txt add {<a href=change.me>} {</a>}"
$gv(top).b1.html.m.e add command -label [lang "Inline Image" "FRENCH"] \
	-accelerator {<img>} \
	-command "text_html $gv(top).view.txt add {<img src=change.me alt=change.me>}"
$gv(top).b1.html.m.e add command -label [lang "Pre Formatted" "FRENCH"] \
	-accelerator {<pre>} \
	-command "text_html $gv(top).view.txt add {<pre>} {</pre>}"
$gv(top).b1.html.m.e add command -label [lang "Block Quote" "FRENCH"] \
	-accelerator {<quote>} \
	-command "text_html $gv(top).view.txt add {<quote>} {</quote>}"
$gv(top).b1.html.m.e add command -label [lang "Break" "FRENCH"] \
	-accelerator {<br>} \
	-command "text_html $gv(top).view.txt add {<br>}"
$gv(top).b1.html.m.e add command -label [lang "Paragraph" "FRENCH"] \
	-accelerator {<p>} \
	-command "text_html $gv(top).view.txt add {<p>}"
$gv(top).b1.html.m.e add command -label [lang "Horizontal line" "FRENCH"] \
	-accelerator {<hr>} \
	-command "text_html $gv(top).view.txt add {<hr>\n}"
$gv(top).b1.html.m.e add command -label [lang "Insert current Date/Time" "FRENCH"] \
	-accelerator {} \
	-command "text_html $gv(top).view.txt add \"\[clock format \[clock seconds\]\]\""

pack [ frame $gv(top).view ] \
	-side top -expand true -fill both
pack [ scrollbar $gv(top).view.yscroll \
	-command "$gv(top).view.txt yview" ] \
	-side left -fill y
pack [ text $gv(top).view.txt -yscroll "$gv(top).view.yscroll set" \
	-wrap word ] \
	-expand true -fill both
bind_help $gv(top).view $gv(top).help \
	[lang "Text window" "FRENCH"]
bind $gv(top).view.txt <Control-e> {
	set filename [string trim [selection get]]
	if {$filename==""} {break}
	if {[file exists $filename]} {
		background vedit -mode $gv(mode) $filename
	} elseif {[file exists [file dirname $gv(filename)]/$filename]} {
		background vedit [file dirname $gv(filename)]/$filename
	} else {
		errmess $gv(top) "Sorry...file $filename does not exist"
	}
	break
}

set mode "text"
set argv [topgetopt -any {mode} $argv]
if {$mode=="html"} {
	set gv(mode) "html"
	pack $gv(top).b1.html -after $gv(top).b1.edit \
		-side left -fill y
}

if {[llength $argv]>0} {
	set infile [lindex $argv 0]
} else {
	set infile ""
}
if {[llength $argv]>1} {
	set filename [lindex $argv 1]
} else {
	set filename $infile
}

if {$infile=="-"} {
	$gv(top).view.txt insert insert [read stdin]
} elseif {$infile!=""} {
	vedit_loadfile $filename
}

if {$filename=="-"} {
	set gv(filename) ""
} else {
	set gv(filename) "$filename"
}

wm withdraw .
if {[winfo exists .startup]} {destroy .startup}
if {[info exists gv(startup.id)]} {image delete $gv(startup.id)}


