#!/bin/sh
# the next line restarts using wish8.3 \
exec wish8.3 "$0" "$@"

# This is "ding", 
#  * a dictionary lookup program,
#  * DIctionary Nice Grep,
#  * a Front-End to [ae]grep, ispell, dict, ...
#  * Ding {n} :: thing
#
# Copyright (c) Frank Richter <Frank.Richter@hrz.tu-chemnitz.de> 1999 - 2002
# GNU public license
# Ding comes with ABSOLUTELY NO WARRANTY.
# 

set pinfo(pname)    {Ding: Dictionary Lookup}
set pinfo(version)  {1.2}
set pinfo(homepage) {http://www.tu-chemnitz.de/~fri/ding/}

set ding_usage(en) \
"    word\tStart seaching this word
   -x   or --selection\tStart searching for selected word (X selection)
   -m   or --mini\tStart with minimal size (search form only)\n
   -r   or --remote\tStart search in an already running program
   -R   or --remote-new\tStart search in an already running program
                       \tor start a new program\n
           --noconf\tDo not save preferences
   -D # or --debug #\tStart with debug output, # = number (1..15)\n
These options may be combined, such as:
   $argv0 -R -x\tStart searching for selected word in an already
   \trunning program or start a new program\n"

set ding_usage(de) "
   Suchwort\tStarte Suche nach Suchwort
   -x   oder --selection\tStarte Suche mit markiertem Text
   -m   oder --mini\tStarte in Mini-Größe (nur Sucheingabe)\n
   -r   oder --remote\tStarte Suche in bereits laufendem Programm
   -R   oder --remote-new\tStarte Suche in bereits laufendem Programm
   \toder starte neues Programm\n
             --noconf\tKein Speichern der Einstellungen
   -D # oder --debug #\tDebug-Ausschriften, # = Zahl (1..15)\n
Die Optionen können natürlich kombiniert werden, z.B.:
   $argv0 -R -x\tStarte Suche nach markiertem Wort in bereits
   \tlaufendem Programm oder starte neues Programm\n"

proc Usage {} {
    global pinfo argv0 env ding_usage
    puts "\n$pinfo(pname) version $pinfo(version)"

    if {[info exists env(LANG)] && [info exists ding_usage([string range $env(LANG) 0 1])]} {
        puts "Usage: $argv0 \[Optionen\] \[Suchwort\]"
        puts $ding_usage([string range $env(LANG) 0 1])
        puts "Siehe WWW-Homepage: $pinfo(homepage)"
    } else {
        puts "Usage: $argv0 \[options\] \[word\]"
        puts $ding_usage(en)
        puts "See WWW homepage: $pinfo(homepage)"
    }
}

if { ![info exists tk_version] } {
    Usage
    exit
}

if {$tk_version < 8.0} {
    tk_messageBox -type ok -icon error -message {
Sorry, wish version >= 8.0 is required.  Try to install a newer Tk version.

Die Version von Tcl/Tk ist zu alt, bitte Version 8.0 oder höher installieren.

Exiting ...}
    exit
}

set param(isunix) [expr {$tcl_platform(platform) == {unix}}]
set param(iswin)  [expr {$tcl_platform(platform) == {windows}}]

# Config options you may want to change:
# Startup file
set param(rcfile) [expr {$param(isunix) == 1 ? "$env(HOME)/.dingrc" : "dingrc.tcl"}]
set param(noticefile) [expr {$param(isunix) == 1 ? "$env(HOME)/.dingnotice.txt" : "dingntc.txt"}]

# The default search methods
# The provided German-English dictionary:
set default_searchmeth(0,name) {Dictionary}
set default_searchmeth(0,grepcmds) {agrep egrep internal_search}
set default_searchmeth(0,dictfile) [expr {$param(isunix) ?  "/usr/X11R6/share/ding/ger-eng.txt" : "ger-eng.txt"}]
set default_searchmeth(0,separator) { :: }
set default_searchmeth(0,language1) {Deutsch}
set default_searchmeth(0,language2) {English}

# spell checker
set default_searchmeth(1,name) {Spell check}
set default_searchmeth(1,grepcmds) {ispell aspell}
set default_searchmeth(1,language1)  "English"
set default_searchmeth(1,grepopts)  "-B -S -a -d english"

# spell checker
set default_searchmeth(2,name) {Rechtschreibung}
set default_searchmeth(2,grepcmds) {ispell aspell}
set default_searchmeth(2,language1)  "Deutsch"
set default_searchmeth(2,grepopts)  "-B -S -a -d german"

# dict - english ditionary
set default_searchmeth(3,name) {English}
set default_searchmeth(3,grepcmds) {dict}
set default_searchmeth(3,language1)  "English Dictionary"

# Fortunes
set default_searchmeth(4,name) {Epigram}
set default_searchmeth(4,grepcmds) {/usr/games/fortune}
set default_searchmeth(4,language1)  "Random English adage/epigram"
set default_searchmeth(4,grepopts)  ""
set default_searchmeth(4,minlength)  0

# check for these search commands
set default_grepcmds(agrep) "-h"
set default_grepcmds(egrep) "-h"
set default_grepcmds(dict) ""
set default_grepcmds(ispell) "-B -S -a"
set default_grepcmds(aspell) "-B -S -a"
set default_grepcmds(/usr/games/fortune) ""
set default_grepcmds(internal_search) ""

set default_searchopts(minlength)    2
set default_searchopts(maxlength)   30
set default_searchopts(maxresults) 200
set default_searchopts(shapedresult) 1

set default(maxhistory)    50
set default(autosave)       1
set default(params_as_menu) 0
set default(show_menu)      1
set default(umlaut_buttons) 0
set default(show_result)    1
set default(show_status)    1
set default(bcolor) [expr {$param(isunix) ? [. cget -background] : "#C0C0C0"}]
set default(fcolor)     black

set default(showBalloons)   1
set default(balloonDelay) 800
array set balloonHelp      {}
set gparam(balloonBackground)  LightGoldenrodYellow
set default(search_prop)  0
set default(win_prop)  0

if {$param(isunix)} {
    # set lfont "-*-Helvetica-Medium-R-Normal--*-100-*-*-*-*-*-*"
    # set bfont "-*-Helvetica-Bold-R-Normal--*-100-*-*-*-*-*-*"
    # set sfont "-*-Helvetica-Medium-R-Normal--*-100-*-*-*-*-*-*"
    # set default_tfont "-*-Courier-Medium-R-Normal--*-120-*-*-*-*-*-*"
    set default(lfont)        {helvetica 12 normal}
    set param(bfont)          {helvetica 12 bold}
    set param(sfont)          {helvetica 10 normal}
    set default(tfont)        {fixed 14 normal}

    # Font selection
    set param(tfonts) {courier fixed lucida lucidatypewriter helvetica times terminal}
} else {
    set default(lfont) {Arial 10 normal}
    set param(bfont)   {Arial 10 bold}
    set param(sfont)   {Arial 8 normal}
    set default(tfont) {Courier 10 normal}

    # Font selection
    # show all avail. font families
    set param(tfonts) {}
    # or show only this subset:
    # set param(tfonts) {Arial System Times Courier Verdana}
}

set param(tfont) $default(tfont)
set param(lfont) $default(lfont)

set opts(case)   0
set opts(word)   0
set opts(regex)  0
set opts(errors) 0

### don't change from here unless you know what you're doing .-)
# set defaults
set gparam(showBalloons)    $default(showBalloons)
set gparam(balloonDelay)    $default(balloonDelay)
set gparam(autosave)        $default(autosave)

set param(bcolor)           $default(bcolor)
set param(fcolor)           $default(fcolor)
set param(maxhistory)       $default(maxhistory)
set param(params_as_menu)   $default(params_as_menu)
set param(show_menu)        $default(show_menu)
set param(umlaut_buttons)   $default(umlaut_buttons)
set param(show_result)      $default(show_result)
set param(show_status)      $default(show_status)
set param(search_prop)      $default(search_prop)
set param(win_prop)         $default(win_prop)
set param(check_selection_active)  0

set curhistory 0
set inshistory 0
array set history_result {}
array set history_query {}
array set history_pos {}

set pinfo(authormail) {Frank.Richter@hrz.tu-chemnitz.de}
set pinfo(author)     "Frank Richter <$pinfo(authormail)>"
set pinfo(copyright) "Copyright (c) 1999 - 2002 $pinfo(author)"

set proc(pid) -1
set proc(pipe) {}
set proc(processlist) {}
set lines {} 
set sigchld 0

# interface language settings
set languages(en) {English}
set languages(de) {Deutsch}
set default(language) {en}
set gparam(lang) $default(language)
set defaultcursor [. cget -cursor]

# Debug level 
# 1 - main functions   2 - trace    4 - external process 8 - util
set param(debug) 0
# set param(debug) 15

# correction of height for some window managers
set param(add_geom_height)  0

# English
set s(en)(file) "File"
set s(en)(newwin) "New window"
set s(en)(quit) "Quit"
set s(en)(save) "Save"
set s(en)(saveall) "Save all"
set s(en)(mail) "Send corrections"
set s(en)(options) "Preferences"
set s(en)(params) "Search options"
set s(en)(soptions) "Search preferences..."
set s(en)(general) "General preferences..."
set s(en)(saveopts) "Save preferences"
set s(en)(help) "Help"
set s(en)(khelp) "Keys"
set s(en)(cmdline) "Start options"
set s(en)(about) "About"
set s(en)(ctl) "Ctrl"
set s(en)(space) "Space"
set s(en)(up) "Up"
set s(en)(down) "Down"
set s(en)(noback) "No previous search results."
set s(en)(noforw) "No more search results."
set s(en)(query) "Search word:"
set s(en)(searchmeth) "Search methods"
set s(en)(configured) "Configured"
set s(en)(search) "Search"
set s(en)(searchhelp) "Click left: Start search method\nClick middle: Search in result"
set s(en)(clear) "Clear"
set s(en)(minimize) "Minimize window"
set s(en)(normalsize) "Show full window"
set s(en)(word)(0) "full words"
set s(en)(word)(1) "partial search"
set s(en)(case)(0) "ignore case"
set s(en)(case)(1) "exact case"
set s(en)(errors) "errors"
set s(en)(error) "error"
set s(en)(closestmatch) "best match"
set s(en)(regex)(0) "simple search"
set s(en)(regex)(1) "reg. expression"
set s(en)(cmd) "Search with"
set s(en)(bbhistory) "Previous results"
set s(en)(bfhistory) "Next results"
set s(en)(bwords) "Search for full words\nor partial matches?"
set s(en)(bcase) "Search case insensitive or sensitive?"
set s(en)(berrors) "Try error correction?"
set s(en)(bregex) "Simple patterns with *\nor regular expressions?"
set s(en)(color) "Select color"
set s(en)(results) "results"
set s(en)(result) "result"
set s(en)(noresults) "No results for"
set s(en)(notfound) "Search word not found"
set s(en)(correct) "Correctly spelled, no suggestions"
set s(en)(suggestion) "Suggestions"
set s(en)(nosuggestion) "Spelling is wrong or unknown, no suggestions"
set s(en)(root) "word root"
set s(en)(searcho) "Set search options"
set s(en)(searchm) "Set search methods"
set s(en)(apply) "Apply"
set s(en)(default) "Default"
set s(en)(cancel) "Cancel"
set s(en)(dictfile) "Dictionary file"
set s(en)(nodictfile) "No Dictionary file found! See Options | Search options ..."
set s(en)(new)    "New"
set s(en)(edit)   "Change"
set s(en)(delete) "Delete"
set s(en)(autosave) "Save options whenever changed"
set s(en)(lang) "Language"
set s(en)(sep) "Separator"
set s(en)(maxresults) "Max. number of results"
set s(en)(minlength) "Min. length of search word"
set s(en)(maxlength) "Max. length of search word"
set s(en)(maxhistory) "Remember how many results"
set s(en)(params_as_menu) "Show search parameters"
set s(en)(show_menu)      "Show menu bar"
set s(en)(umlaut_buttons) "Show umlaut buttons"
set s(en)(show_result) "Show search form only "
set s(en)(show_status) "Show status line"
set s(en)(fg) "Foreground color..."
set s(en)(bg) "Background color..."
set s(en)(balloon) "Show balloon help"
set s(en)(after) "after"
set s(en)(ms) "msec"
set s(en)(mailtitle) "Send e-mail to"
set s(en)(send) "Send e-mail"
set s(en)(notext) "No text for your e-mail?\nNo message sent."
set s(en)(nomail) "Please send your suggestions with a mail program to"
set s(en)(notice) "Write a notice"
set s(en)(attachnotice) "Include notices"
set s(en)(nosave) "No search results to save!"
set s(en)(tooshort) "Search word too short!"
set s(en)(toolong) "Search word too long!"
set s(en)(more) "(found more, max limit reached)"
set s(en)(tfont) "Result font"
set s(en)(lfont) "Other font"
set s(en)(defaultdict) "De <-> En"
set s(en)(shaped) "Results shaped"
set s(en)(name) "Name"
set s(en)(grepcmd) "Search command"
set s(en)(grepopts) "Options"
set s(en)(noagrep) "The \"agrep\" command wasn't found on your system.
Some functions won't be available for searching.\n
As a recommendation - install agrep.
You'll find it in a special agrep package or within the Glimpse package.\n
We will use for now: "
set s(en)(change) "Some changes will come into effect after program restart."
set s(en)(kill) "Search process was stopped."
set s(en)(nokill) "Couldn't stop search process."
set s(en)(namerequired) "Please give this search method a name."
set s(en)(props) "Search behavior"
set s(en)(onrequest) "Search on input / middle mouse button"
set s(en)(onmouseover) "Search on mouse over"
set s(en)(onselection) "Search on new text selection"
set s(en)(min_focus) "Minimize window when mouse out"

# German
set s(de)(file) "Datei"
set s(de)(newwin) "Neues Fenster"
set s(de)(quit) "Beenden"
set s(de)(save) "Speichern"
set s(de)(saveall) "Alles speichern"
set s(de)(mail) "Korrektur senden"
set s(de)(options) "Einstellungen"
set s(de)(params) "Suchparameter"
set s(de)(soptions) "Suchmethoden..."
set s(de)(general) "Allgemein..."
set s(de)(saveopts) "Einstellungen speichern"
set s(de)(help) "Hilfe"
set s(de)(khelp) "Tastatur"
set s(de)(cmdline) "Startoptionen"
set s(de)(about) "Info"
set s(de)(ctl) "Strg"
set s(de)(space) "Leer"
set s(de)(up) "Hoch"
set s(de)(down) "Runter"
set s(de)(noback) "Keine früheren Suchergebnisse."
set s(de)(noforw) "Keine weiteren Suchergebnisse."
set s(de)(query) "Suchwort:"
set s(de)(searchmeth) "Suchmethoden"
set s(de)(configured) "Eingestellte"
set s(de)(search) "Suche"
set s(de)(searchhelp) "Klick links: Suchmethode starten\nKlick mittel: Suche in Ergebnis"
set s(de)(clear) "Löschen"
set s(de)(minimize) "Fenster minimieren"
set s(de)(normalsize) "Volles Fenster anzeigen"
set s(de)(word)(0) "ganze Wörter"
set s(de)(word)(1) "Teilsuche"
set s(de)(case)(0) "Groß/klein egal"
set s(de)(case)(1) "Groß/klein exakt"
set s(de)(errors) "Fehler"
set s(de)(error) "Fehler"
set s(de)(closestmatch) "bis Treffer"
set s(de)(regex)(0) "einfache Suche"
set s(de)(regex)(1) "reg. Ausdrücke"
set s(de)(cmd) "Suche mit"
set s(de)(bbhistory) "Frühere Suchergebnisse"
set s(de)(bfhistory) "Weitere Suchergebnisse"
set s(de)(bwords) "Suche nach vollständigen Wörtern\noder Muster in Wörtern?"
set s(de)(bcase) "Unterscheidung Groß-/Kleinschreibweise?"
set s(de)(berrors) "Versuche Fehlerkorrektur?"
set s(de)(bregex) "Einfache Muster mit * oder\nkomplexe reguläre Ausdrücke?"
set s(de)(color) "Farbauswahl"
set s(de)(results) "Ergebnisse"
set s(de)(result) "Ergebnis"
set s(de)(noresults) "Kein Ergebnis für"
set s(de)(notfound) "Suchbegriff nicht gefunden"
set s(de)(correct) "Richtige Schreibweise, keine Vorschläge"
set s(de)(suggestion) "Vorschläge"
set s(de)(nosuggestion) "Falsche oder unbekannte Schreibweise, keine Vorschläge"
set s(de)(root) "Wortstamm"
set s(de)(searcho) "Suchmethoden einstellen"
set s(de)(searchm) "Suchmethoden festlegen"
set s(de)(apply) "Übernehmen"
set s(de)(default) "Standard"
set s(de)(cancel) "Abbrechen"
set s(de)(dictfile) "Wörterbuch-Datei"
set s(de)(nodictfile) "Keine Wörterbuch-Datei gefunden! Siehe Einstellungen | Suchmethoden"
set s(de)(new)    "Neu"
set s(de)(edit)   "Ändern"
set s(de)(delete) "Entfernen"
set s(de)(autosave) "Einstellungen bei Änderung sofort speichern"
set s(de)(lang) "Sprache"
set s(de)(sep) "Trennzeichen"
set s(de)(maxresults) "Maximalanzahl von Ergebnissen"
set s(de)(minlength) "Minimale Länge des Suchwortes"
set s(de)(maxlength) "Maximale Länge des Suchwortes"
set s(de)(maxhistory) "Merken wievieler Ergebnisse"
set s(de)(params_as_menu) "Zeige Suchparameter"
set s(en)(show_menu)      "Zeige Menüleiste"
set s(de)(umlaut_buttons) "Zeige Umlautknöpfe"
set s(de)(show_result) "Zeige nur Suchmaske"
set s(de)(show_status) "Zeige Statuszeile"
set s(de)(fg) "Vordergrundfarbe..."
set s(de)(bg) "Hintergrundfarbe..."
set s(de)(balloon) "Hilfen anzeigen"
set s(de)(after) "nach"
set s(de)(ms) "ms"
set s(de)(mailtitle) "Sende E-Mail an"
set s(de)(send) "E-Mail absenden"
set s(de)(notext) "Kein Text in der E-Mail?\nKeine Nachricht gesendet."
set s(de)(nomail) "Bitte senden Sie Ihre E-Mail mit einem Mail-Programm
an"
set s(de)(notice) "Notiz schreiben"
set s(de)(attachnotice) "Notizen einlesen"
set s(de)(nosave) "Noch keine Suchergebnisse zum Abspeichern."
set s(de)(tooshort) "Suchbegriff zu kurz!"
set s(de)(toolong) "Suchbegriff zu lang!"
set s(de)(more) "(weitere vorhanden, Suche abgebrochen)"
set s(de)(tfont) "Schriftart für Resultat"
set s(de)(lfont) "Sonstige Schriftart"
set s(de)(defaultdict) "De <-> En"
set s(de)(shaped) "Resultate farblich abgesetzt"
set s(de)(name) "Name"
set s(de)(grepcmd) "Such-Kommando"
set s(de)(grepopts) "Optionen"
set s(de)(noagrep) "Das Kommando \"agrep\" wurde in Ihrem System nicht gefunden.
Dadurch stehen einige Funktionen nicht zur Verfügung.\n
Empfehlung: agrep installieren!
Sie finden es in einem speziellen agrep-Paket oder im glimpse-Paket.\n
Jetzt wird verwendet: "
set s(de)(change) "Einige Änderungen werden erst nach Neustart des Programmes sichtbar."
set s(de)(kill) "Suchvorgang wurde gestoppt."
set s(de)(nokill) "Suchvorgang konnte nicht gestoppt werden."
set s(de)(namerequired) "Bitte dieser Suchmethode einen Namen geben."
set s(de)(props) "Suchverhalten"
set s(de)(onrequest) "Suche erst nach Eingabe/mittl. Maustaste"
set s(de)(onmouseover) "Suche sobald Maus über Fenster"
set s(de)(onselection) "Suche sofort bei neuer Textauswahl"
set s(de)(min_focus) "Fenster minimieren, wenn Fokus woanders"

# Debug procedure
proc debug {level s} {
    global param
    if [expr $param(debug) & $level] { 
        puts stderr "$level $s"
    }
} 

# if Shell variable LANG set AND we've defined this language, 
# set this as default
if [info exists env(LANG)] {
    foreach l [array names languages] {
        if [string match "$l*" $env(LANG)] {
            set default(language) $l
            set gparam(lang) $l
            break
        }
    }
}

proc errorBox {msg} {
    tk_messageBox -message $msg -type ok -icon error
}

# for flag -remote: send a command to a running ding
proc remote {query} {
    debug 1 "remote $query"

    # my own application name
    regsub { .*} [tk appname] {} myname
    debug 8 "  myname = $myname"

    # find an alway running application ...
    foreach i [lsort [winfo interps]] {
        if [string match "${myname}*" $i] {
            debug 4 " send $i dictsearch $query"
            set result [catch [list send $i [concat dictsearch [list $query] 0]] msg]
            if {$result != 0} {
                # error - try next
                debug 4 " returns: $result"
            } else {
                # ok
                return 0
            }
        }
    }
    # error
    return 1
}

############### main

# check command line parameters
set mini 0
set gparam(noconf) 0
set remote 0
set new 0
set query {}
set last_selection {}

for {set a 0} {$a < $argc} {incr a} {
    set arg [lindex $argv $a]
    if {([string compare $arg "--help"] == 0) || 
        ([string compare $arg "-h"] == 0)} {
        Usage
        exit
    } elseif {([string compare $arg "--remote"] == 0) || 
        ([string compare $arg "-r"] == 0)} {
        set remote 1
    } elseif {([string compare $arg "--remote-new"] == 0) ||
              ([string compare $arg {-R}] == 0)} {
        set remote 1
        set new 1
    } elseif {([string compare $arg {--selection}] == 0) ||
              ([string compare $arg {-x}] == 0)} {
        # first search with current X selection
        catch {set q [selection get]}
        if {[info exists q] && [string length $q] > 0} {
            set query $q
            set last_selection $q
        }
    } elseif {([string compare $arg "--mini"] == 0) ||
              ([string compare $arg "-m"] == 0)} {
        set mini 1
    } elseif {([string compare $arg "--noconf"] == 0)} {
        set gparam(noconf) 1
        set gparam(autosave) 0
    } elseif {([string compare $arg "--debug"] == 0) ||
              ([string compare $arg "-D"] == 0)} {
        incr a
        if {$a < $argc} {
            set param(debug) [lindex $argv $a]
        }
    } elseif {! [string match {-*} $arg]} {
        # argument is a search query
        set query "$query $arg"
    }
}

debug 1 "end of argument parsing: remote = $remote new = $new, query = $query, mini = $mini"
if {$remote} {
    # send a command to a running ding
    set ret 1
    if [string length $query] {
        set ret [remote $query]
    }
    debug 1 "remote exit $ret"
    # if send was ok or no new window -> exit
    if {($ret == 0) || (! $new)} {
        exit $ret
    }
}

# read user's config file if existing
if [file readable $param(rcfile)] {
    set err [catch "source $param(rcfile)" errmsg]
    if $err {
        errorBox "Error in config file $param(rcfile):\n$errmsg"
        exit
    }
}


proc cmd_avail {cmd} {
    global env param

    debug 2 "cmd_avail $cmd"
    if [string match "internal_*" $cmd] {
        return 1
    } elseif {$param(isunix)} {
        if {[string index $cmd 0] == "/" && [file isfile $cmd] && [file executable $cmd]} {
            debug 8 "Found $cmd"
            return 1
        }
        if {[llength [array get env PATH]] < 1} {
            return 0
        }
        foreach p [split $env(PATH) :] {
            if {[file isfile "$p/$cmd"] && [file executable "$p/$cmd"]} {
                debug 8 "Found $p/$cmd"
                return 1
            }
        }
    } else {
        if {[file executable $cmd] || [file executable "$cmd.exe"]} {
            # errorBox "Found $cmd.exe"
            return 1
        }
    }
    return 0
}

# check available search commands
set gparam(grepcmd) {}
foreach c [array names default_grepcmds] {
    if [cmd_avail $c] {
        if {$gparam(grepcmd) == ""} {       # default search cmd
            set gparam(grepcmd) $c
        }
        set grepcmds($c) "$default_grepcmds($c)"
    }
}

set cmds 0

# 1.0 options - no prog_version used
# do backward compatibility

if { ! [info exists ding_version]} {
    # 1.0 options - no prog_version used
    # or new user
    set searchmeth(0,name) [set s(en)(defaultdict)]
    set searchmeth(0,type) 0
    foreach i [array names default_searchopts] {
        set searchmeth(0,$i) $default_searchopts($i)
    }
    if [info exists dictfile] {
        set searchmeth(0,dictfile) $dictfile
    } else {
        set searchmeth(0,dictfile) $default_searchmeth(0,dictfile)
    }
    set searchmeth(0,dictfiles) [glob -nocomplain $searchmeth(0,dictfile)]
    if [info exists language1] {
        set searchmeth(0,language1) $language1
    } else {
        set searchmeth(0,language1) $default_searchmeth(0,language1)
    }
    if [info exists language2] {
        set searchmeth(0,language2) $language2
    } else {
        set searchmeth(0,language2) $default_searchmeth(0,language2)
    }
    if [info exists separator] {
        set searchmeth(0,separator) $separator
    } elseif [info exists default_searchmeth(0,separator)] {
        set searchmeth(0,separator) $default_searchmeth(0,separator)
    } else {
        set searchmeth(0,separator) {}
    }
    if [info exists maxlength] {
        set searchmeth(0,maxlength) $maxlength
    } elseif [info exists default_searchmeth(0,maxlength)] {
        set searchmeth(0,maxlength) $default_searchmeth(0,maxlength)
    }
    if [info exists maxresults] {
        set searchmeth(0,maxresults) $maxresults
    } elseif [info exists default_searchmeth(0,maxresults)] {
        set searchmeth(0,maxresults) $default_searchmeth(0,maxresults)
    }
    if [info exists minlength] {
        set searchmeth(0,minlength) $minlength
    } elseif [info exists default_searchmeth(0,minlength)] {
        set searchmeth(0,minlength) $default_searchmeth(0,minlength)
    }
    if [info exists shapedresult] {
         set searchmeth(0,shapedresult) $shapedresult
    } elseif [info exists default_searchmeth(0,shapedresult)] {
        set searchmeth(0,shapedresult) $default_searchmeth(0,shapedresult)
    }
    if [info exists grepcmd] {
        set searchmeth(0,grepcmd) $grepcmd
    } else {
        # evtl. spezifisches grep cmd
        foreach c $default_searchmeth(0,grepcmds) {
            if [info exists grepcmds($c)] {
                set searchmeth(0,grepcmd) $c
                break
            }
        }
    }
    if [info exists grepopts] {
        set searchmeth(0,grepopts) $grepopts
    } else {
        set searchmeth(0,grepopts) "$grepcmds($searchmeth(0,grepcmd))"
    }

    set searchmeth(0,avail) 1
    set searchmpos 0

    # Enable other available search methods
    for {set m 1} {[info exists default_searchmeth($m,name)]} {incr m} {
        foreach c $default_searchmeth($m,grepcmds) {
            if [info exists grepcmds($c)] {
                set searchmeth($m,name) $default_searchmeth($m,name)
                set searchmeth($m,type) $m
                set searchmeth($m,grepcmd) $c
                if [info exists default_searchmeth($m,grepopts)] {
                    set searchmeth($m,grepopts) $default_searchmeth($m,grepopts)
                } else {
                    set searchmeth($m,grepopts) $grepcmds($c)
                }
                foreach i [array names default_searchopts] {
                    set searchmeth($m,$i) $default_searchopts($i)
                }
                if [info exists default_searchmeth($m,dictfile)] {
                    set searchmeth($m,dictfile) $default_searchmeth($m,dictfile)
                } else {
                    set searchmeth($m,dictfile) {}
                }
                set searchmeth($m,dictfiles) [glob -nocomplain $searchmeth($m,dictfile)]
                if [info exists default_searchmeth($m,language1)] {
                    set searchmeth($m,language1) $default_searchmeth($m,language1)
                } else {
                    set searchmeth($m,language1) {}
                }
                if [info exists default_searchmeth($m,language2)] {
                    set searchmeth($m,language2) $default_searchmeth($m,language2)
                } else {
                    set searchmeth($m,language2) {}
                }
                if [info exists default_searchmeth($m,separator)] {
                    set searchmeth($m,separator) $default_searchmeth($m,separator)
                } else {
                    set searchmeth($m,separator) {}
                }
                if [info exists default_searchmeth($m,maxlength)] {
                    set searchmeth($m,maxlength) $default_searchmeth($m,maxlength)
                }
                if [info exists default_searchmeth($m,maxresults)] {
                    set searchmeth($m,maxresults) $default_searchmeth($m,maxresults)
                }
                if [info exists default_searchmeth($m,minlength)] {
                    set searchmeth($m,minlength) $default_searchmeth($m,minlength)
                }
                if [info exists default_searchmeth($m,shapedresult)] {
                    set searchmeth($m,shapedresult) $default_searchmeth($m,shapedresult)
                }
                set searchmeth($m,avail) 1
                lappend searchmpos $m
    
                break
            }
        }
    }

    if [info exists maxhistory] {
        set param(maxhistory) $maxhistory
    }
    if [info exists fcolor] {
        set param(fcolor) $fcolor
    }
    if [info exists bcolor] {
        set param(bcolor) $bcolor
    }
    if [info exists tfont] {
        set param(tfont) $tfont
    }
    if [info exists lang] {
        set gparam(lang) $lang
    }

} else {
    if {$searchmpos == ""} {
        set searchmpos 0
    }
    foreach i $searchmpos {
        if [cmd_avail $searchmeth($i,grepcmd)] {
            incr cmds
            set searchmeth($i,avail) 1
        } else {
            set searchmeth($i,avail) 0
        }

        # Change path of default ger-eng.txt when upgrading from version 1.1
        if {$searchmeth($i,dictfile) == {/usr/dict/ger-eng.txt} &&
            $ding_version == {1.1}} {
            set searchmeth($i,dictfile) $default_searchmeth(0,dictfile)
            debug 2 "New path of ger-eng.txt configured"
        }

        set searchmeth($i,dictfiles) [glob -nocomplain $searchmeth($i,dictfile)]
    }
}
if [expr $param(debug) & 8] {
    foreach m [lsort [array names searchmeth]] {
        puts stderr " $m: $searchmeth($m)"
    }
                }
set param(cursearchmeth) 0

tk_setPalette foreground $param(fcolor) background $param(bcolor)

if { ![string compare $gparam(grepcmd) ""] && $cmds < 1} {
    # no search command found ...
    errorBox "No grep commands like [array names default_grepcmds] found.
Check your PATH and/or install a grep command.
Exiting ..."
    exit
}

# Define Font names
proc defFonts {} {
    global param
    # Pixel size for Unix - looks better
    set pixel [expr {$param(isunix) ? "-" : ""}]

    if [string length [lindex $param(lfont) 2]] {
        set weight [lindex $param(lfont) 2]
    } else {
        set weight {normal}
    }
    font create lfont -family [lindex $param(lfont) 0] \
        -size $pixel[lindex $param(lfont) 1] -weight $weight

    if [string length [lindex $param(bfont) 2]] {
        set weight [lindex $param(bfont) 2]
    } else {
        set weight {bold}
    }
    font create bfont -family [lindex $param(bfont) 0] \
        -size $pixel[lindex $param(bfont) 1] -weight $weight

    if [string length [lindex $param(sfont) 2]] {
        set weight [lindex $param(sfont) 2]
    } else {
        set weight {normal}
    }
    font create sfont -family [lindex $param(sfont) 0] \
        -size $pixel[lindex $param(sfont) 1] -weight $weight

    if [string length [lindex $param(tfont) 2]] {
        set weight [lindex $param(tfont) 2]
    } else {
        set weight {normal}
    }
    font create tfont -family [lindex $param(tfont) 0] \
        -size $pixel[lindex $param(tfont) 1] -weight $weight
}

proc chFont {font} {
    global param
    # Pixel size for Unix - looks better
    set pixel [expr {$param(isunix) ? "-" : ""}]

    if [string length [lindex $param($font) 2]] {
        set weight [lindex $param($font) 2]
    } else {
        set weight {normal}
    }
    font configure $font -family [lindex $param($font) 0] \
        -size $pixel[lindex $param($font) 1] -weight $weight
}

defFonts

# if {![string compare $searchmeth($param(cursearchmeth),grepcmd) ""]} {
# } else {
#     set default_searchopts(grepcmd) $searchmeth($param(cursearchmeth),grepcmd)
# }

if {[winfo depth .] > 1} {      # Color display
    set param(errcolor) red
} else {
    set param(errcolor) black
}

#########################################################################
# Balloon help, by John Haxby <jch@pwd.hp.com>, with slight changes
# by Axel Boldt <boldt@math.ucsb.edu>.

proc BalloonInit {} {
    global gparam

    
bind balloon <Enter> {
    if { [info exists balloonHelp(%W)] && [%W cget -state] != "disabled"} {
        set balloonHelp(%W,after) [after $gparam(balloonDelay) {showBalloonHelp %W}]
    }
}

bind balloon <Leave> { unShowBalloonHelp %W }
bind balloon <Any-KeyPress> { unShowBalloonHelp %W }
bind balloon <Any-Button> { unShowBalloonHelp %W }

proc showBalloonHelp {w} {
    global balloonHelp param gparam
    if {![info exists balloonHelp($w)] || ! $gparam(showBalloons) } {
        return
    }
    update idletasks
    set curpos [winfo pointerxy $w]
    set curwin [eval winfo containing $curpos]
    if { $w == $curwin } {
        if ![winfo exists .balloon] {
            toplevel .balloon
            wm overrideredirect .balloon true
            pack [label .balloon.l -font lfont \
                    -foreground black \
                    -background $gparam(balloonBackground) \
                    -highlightthickness 1 \
                    -highlightbackground black]
            wm withdraw .balloon
        }
        .balloon.l configure -text $balloonHelp($w)
        set x [expr [lindex $curpos 0] - 14]
        set y [expr [lindex $curpos 1] + 19]
        wm geometry .balloon +$x+$y
        # This update is important to have the geometry command take 
        # effect in all cases (A.B.)
        update idletasks
        raise .balloon
        wm deiconify .balloon
    }
}
proc unShowBalloonHelp {w} {
    global balloonHelp
    if [info exists balloonHelp($w,after)] {
        after cancel $balloonHelp($w,after)
        unset balloonHelp($w,after)
    }
    catch {wm withdraw .balloon}
}

# end of proc BalloonInit
} 

BalloonInit
###############

proc shadeColor {fcolor bcolor} {
    
    debug 2 "shadeColor $fcolor $bcolor"
    # the higher the more different is the shaded color, max 127
    set dc1 16
    if {[scan $bcolor "#%2x%2x%2x" red green blue] != 3} {
        set rgb [winfo rgb . $bcolor]
        set red   [expr {[lindex $rgb 0]/0x100}]
        set green [expr {[lindex $rgb 1]/0x100}]
        set blue  [expr {[lindex $rgb 2]/0x100}]
    }
    debug 8 "  old bcolor = $red $green $blue"
    set red   [expr $red < (255 - $dc1) ? [expr $red + $dc1] : [expr $red - $dc1]]
    set green [expr $green < (255 - $dc1) ? [expr $green + $dc1] : [expr $green - $dc1]]
    set blue  [expr $blue < (255 - $dc1) ? [expr $blue + $dc1] : [expr $blue - $dc1]]
    set bc [format "#%02x%02x%02x" $red $green $blue]
    set ec [format "#%02x%02x%02x" [expr $red < 128 ? 255 : $red] [expr $green < 128 ? 128 : 0] [expr $blue < 128 ? 128 : 0]]
    # [expr 255 - $green] [expr 255 - $blue]]

    if {[scan $fcolor "#%2x%2x%2x" red green blue] != 3} {
        set rgb [winfo rgb . $fcolor]
        set red   [expr {[lindex $rgb 0]/0x100}]
        set green [expr {[lindex $rgb 1]/0x100}]
        set blue  [expr {[lindex $rgb 2]/0x100}]
    }
    debug 8 "  old fcolor = $red $green $blue"
    # set red [expr $red < 128 ? 255 : 0]
    # set green [expr $green < 128 ? 255 : 0]
    set blue [expr $blue < 128 ? 255 : 0]
    set fc [format "#%02x%02x%02x" $red $green $blue]

    debug 8 "  new colors = $bc $fc $ec"
    return [list $bc $fc $ec]
}

proc setSearchBg {color} {
    .search.s configure -background $color
    .search.s.label configure -background $color
    .search.l configure -background $color
    .search.l.logo configure -background $color
    .search configure -background $color
}

# updates the menu for search methods when changed
proc update_searchmeth_menu {} {
    global searchmeth searchmpos param gparam s 

    debug 2 "update_searchmeth_menu"
    set lang $gparam(lang)
    variable i
    .search.s.searchmeth.m delete 0 end
    set n 0
    foreach i $searchmpos {
        if $searchmeth($i,avail) {
            incr n
            set cmd "
                set param(cursearchmeth) $i
                .search.s.searchmeth configure -text {$searchmeth($i,name)}
                .statusBar.lab config -foreground $param(fcolor) \
                    -text {[set s($lang)(cmd)] $searchmeth($i,grepcmd)}
                .statusBar.file config -foreground $param(fcolor) \
                    -text {$searchmeth($i,dictfile)}
                set_opts_errors {$i}
            "
            .search.s.searchmeth.m add command -font lfont \
                -accelerator "[set s($lang)(ctl)]+$n" \
                -label [set searchmeth($i,name)] -command $cmd
            bind .search.s.entry <Control-KeyPress-$n> "$cmd"
         }
    }
    .search.s.searchmeth configure -text $searchmeth($param(cursearchmeth),name)
    .statusBar.file config -foreground $param(fcolor) \
                        -text "$searchmeth($param(cursearchmeth),dictfile)"
}

# manipulates menu for opts(errors)
# - active only for search methods which support error correction
proc set_opts_errors {i} {
    global searchmeth opts param

    debug 2 "set_opts_errors"
    if {  ([string match "*grep*" $searchmeth($i,grepcmd)] && \
      [string match "agrep*" $searchmeth($i,grepcmd)]) ||
      [string match "dict*" $searchmeth($i,grepcmd)]} {
    # only agrep and dict (limited) have error correction
        .search.opts.errors configure -state normal
        .menuBar.params entryconfigure 8 -state normal
        .menuBar.params entryconfigure 9 -state normal
        .menuBar.params entryconfigure 10 -state normal
        .menuBar.params entryconfigure 11 -state normal
        .menuBar.params entryconfigure 12 -state normal
    } else {
        # save old state ???
        set opts(errors) 0
        .search.opts.errors configure -state disabled
        .menuBar.params entryconfigure 8 -state disabled
        .menuBar.params entryconfigure 9 -state disabled
        .menuBar.params entryconfigure 10 -state disabled
        .menuBar.params entryconfigure 11 -state disabled
        .menuBar.params entryconfigure 12 -state disabled
    }
}

# Escape: reset interface (kill search process, reset cursor, stop anim logo)
proc Reset {} {
    global proc param gparam s

    debug 2 "Reset"
    set lang $gparam(lang)
    if {$proc(pid) > 0} {
        debug 4 "  kill $proc(pid)"
        if [catch {exec kill $proc(pid)} err] {
            .statusBar.lab config -foreground $param(errcolor) -text "[set s($lang)(nokill)] $err"
        } else {
            set $proc(pid) -1
            .statusBar.lab config -foreground $param(fcolor) -text [set s($lang)(kill)]
        }
        interface_reset 0
    } elseif {$proc(pid) == 0} {
        # internal search
        debug 4 "Stopping internal"
        set proc(pid) -1
        interface_reset 0
    } else {
        interface_reset 1
    }
}

# Scrollbars when needed, http://mini.net/cgi-bin/wikit/BagOfTkAlgorithms
# scrollbars come and go as needed -- see Welch 8.0/347f.
proc yscroll {bar cmd offset size} {
    # debug 8 "yscroll $bar \"$cmd\" $offset $size"
    if {$offset == 0 && $size == 0} {
        return
    }
    if {$offset != 0.0 || $size != 1.0} {
        # grid configure .result.text -rowspan 1
        eval $cmd
    }
    $bar set $offset $size
}
proc xscroll {bar cmd offset size} {
    # debug 8 "xscroll $bar \"$cmd\" $offset $size"
    if {$offset == 0 && $size == 0} {
        return
    }
    if {$offset != 0.0 || $size != 1.0} {
        grid configure .result.text -columnspan 1
        eval $cmd
    }
    $bar set $offset $size
}

# show (full window) or hide (minimize window) the result frame
# type  0 = hide, 1 = show, -1 = toggle

proc hideResult {type} {
    global param s lang balloonHelp
    debug 2 "hideResult $type"
    set x 0
    set y 0
    # update
    set geo [wm geometry .]
    scan $geo "%dx%d+%d+%d" w h x y
    set where {}
    set wx [winfo x .]
    set wy [winfo y .]
    if {$x == 0 && $y == 0} {
        if {${wx} != 0 && ${wy} != 0} {
            # set where "+${wx}+${wy}"
        }
    } else {
        set where "+$x+$y"
    }
    debug 8 "  toplevel geometry: $geo (= $w $h $x $y) winfo: $wx $wy -> $where"

    set visible [winfo viewable .result]
    # show
    if {($type == 1 && ! $visible) || ($type == -1 && ! $visible)} {
        pack .result -fill both -expand yes
        debug 8 "  show result"
        set param(show_result) 1
        hideMenuBar 1
        hideStatusBar 1
        if {[info exists param(width)] && [info exists param(height)]} {
            set height [expr $param(height) + $param(add_geom_height)]
            debug 8 "  setting geometry: $param(width)x$height$where"
            bind dlookup <Configure> {}

            wm geometry . "$param(width)x$height$where"
            update
            debug 8 "  after setting: [wm geometry .] [winfo height .] "
            set wh [winfo height .]
            if {$height != $wh} {
                # correction of height for some window managers
                debug 8 "Window manager sets false height: should $height, is $wh."
                set param(add_geom_height) [expr $param(height) - $wh]
                ## should be: requested height + correction
                set height [expr $param(height) + $param(add_geom_height)]
                # for KDE this works: setting requested height again
                set height $param(height)
                debug 8 "  setting geometry: $param(width)x$height$where"
                wm geometry . "$param(width)x$height$where"
                update
                debug 8 "  after setting: [wm geometry .] [winfo height .] "
            }
            bind dlookup <Configure> catch_resize
        } else {    
            wm geometry . {}
        }
        set balloonHelp(.search.l.logo) [set s($lang)(minimize)]
        .popup entryconfigure 3 -label [set s($lang)(minimize)]
        return
    }
    # hide
    if {($type == 0 && $visible) || ($type == -1 && $visible)} {
        pack forget .result
        debug 8 "  hide result"
        set param(show_result) 0
        hideMenuBar 0
        hideStatusBar 0
        if [info exists param(width)] {
            set w $param(width)
        } else {
            set w [winfo width .search]
        }
        debug 8 "  setting geometry: ${w}x[winfo height .search]$where"
        wm geometry . "${w}x[winfo height .search]$where"
        # wm geometry . {}
        set balloonHelp(.search.l.logo) [set s($lang)(normalsize)]
        .popup entryconfigure 3 -label [set s($lang)(normalsize)]
        return
    }
    debug 2 "hideResult - status ok, nothing done"
}

# proc to show or hide the menu bar
# type  0 = hide, 1 = show, -1 = toggle

proc hideMenuBar {type} {
    global param
    debug 8 "hideMenuBar $type"

    # show
    if {$type == 1} {
        . configure -menu .menuBar
        set param(show_menu) 1
        debug 8 "  show menu"
    } else {
    # hide
        . configure -menu .nomenuBar
        set param(show_menu) 0
        debug 8 "  hide menu"
    }
}
# 0 - off, 1 = on, -1 = show if wanted to see
proc hideStatusBar {type} {
    global param

    debug 8 "hideStatusBar $type"
    # show
    if {$type == 1} {
        pack .statusBar -side bottom -fill x -pady 1 -padx 1 -before .result
        set param(show_status) 1
        debug 8 "  show status"
    } elseif {$type == 0} {
        # hide
        pack forget .statusBar
        set param(show_status) 0
        debug 8 "  hide status"
    } elseif {$param(show_status) == 1 && ![winfo viewable .statusBar]} {
        pack .statusBar -side bottom -fill x -pady 1 -padx 1 -before .result
        debug 8 "  show status"
    } else {
        # hide
        pack forget .statusBar
        debug 8 "  hide status"
    }
}

proc welcome {start} {
    global param searchmpos searchmeth s lang pinfo

    set t .result.text
    set big {Helvetica 18 bold}
    $t tag configure h1 -background $param(shadedcolor) -foreground $param(fcolor) -font $big -justify center
    .result.text tag configure cmd -background $param(bcolor) \
        -relief groove -borderwidth 1 -lmargin1 5 -spacing1 3 -spacing3 3 \
        -font lfont
    .result.text tag configure th -background $param(shadedcolor) -font bfont \
        -relief groove -borderwidth 1 -lmargin1 5 -spacing1 3 -spacing3 3
    $t tag configure c -justify center -font bfont
    $t tag configure infolink -foreground $param(highcolor) -underline 1 \
        -justify center -font bfont
    $t tag bind infolink <ButtonRelease-1> { aboutBox }
    $t tag bind infolink <Enter> " $t config -cursor hand2"
    $t tag bind infolink <Leave> " $t config -cursor xterm"
    $t configure -state normal

    $t delete 0.0 end
    if {$start} {
        .result.text insert end "$pinfo(pname)\n" h1
        .result.text insert end "\nVersion $pinfo(version)  -  " c
        .result.text insert end "Info" infolink
        .result.text insert end "\n\n" c
    }
    # compute tab stop
    set width [winfo width $t]
    $t configure -tabs [expr round(($width / 2) - 2)]

    .result.text insert end "[set s($lang)(configured)] [set s($lang)(searchmeth)]\t" th
    .result.text insert end "[set s($lang)(grepcmd)] / [set s($lang)(dictfile)]\n" th
    foreach i $searchmpos {
        set what "$searchmeth($i,language1)"
        if [string length $searchmeth($i,language2)] {
            set what "$what $searchmeth($i,language2)"
        }
        if $searchmeth($i,avail) {
            .result.text insert end [format "%s (%s)\t%s %s %s\n" \
                $searchmeth($i,name) $what \
                $searchmeth($i,grepcmd) \
                $searchmeth($i,grepopts) \
                $searchmeth($i,dictfile) ] cmd
        }
    }
    .result.text configure -state disabled

    if {$start} {
        animlogo .search.l.logo
        after 1500 {
            after cancel animlogo .search.l.logo
            .search.l.logo configure -image "logo1"
        }
    }
}

# proc mainWindow {w} {
#     global pinfo argv0 argv param gparam s lang
#     global searchmeth opts defaultcursor
#     global grepcmds

# now build the user interface
wm title . $pinfo(pname)
wm iconname . $pinfo(pname)
wm command . [concat $argv0 $argv] 
wm client . [info hostname]
# wm group . .

# Icon not used ...
# wm iconbitmap .  @mini-dbook.xbm
# wm focusmodel . active

set lang $gparam(lang)

menu .menuBar -font lfont -tearoff 0 -relief groove -bd 1
menu .menuBar.file -tearoff 0 -font lfont
.menuBar.file add command -label "[set s($lang)(newwin)]..." \
    -underline 0 -accelerator "[set s($lang)(ctl)]+N" -command startNew 
.menuBar.file add command -label "[set s($lang)(save)]..." -command {save 0}  \
    -underline 0 -accelerator "[set s($lang)(ctl)]+S"
.menuBar.file add command -label "[set s($lang)(saveall)]..." -command {save 1} \
    -underline 0 -accelerator "[set s($lang)(ctl)]+L"
.menuBar.file add command -label "[set s($lang)(notice)]..." \
    -command {notice $param(noticefile)} \
    -underline 1 -accelerator "[set s($lang)(ctl)]+O"
.menuBar.file add command -label "[set s($lang)(mail)]..." -command sendMail \
    -underline 0 -accelerator "[set s($lang)(ctl)]+M"
.menuBar.file add command -label [set s($lang)(quit)] -command {
    if {$gparam(autosave) != 0} {
        saveOptions
    }
    exit } \
    -underline 0 -accelerator "[set s($lang)(ctl)]+Q"
.menuBar add cascade -menu .menuBar.file -label [set s($lang)(file)] -underline 0

menu .menuBar.opts -tearoff 0  -font lfont
.menuBar.opts add checkbutton -label [set s($lang)(params_as_menu)] \
        -variable param(params_as_menu) -command {
                if $param(params_as_menu) {
                    pack .search.opts -pady 2
                } else {
                    pack forget .search.opts
                }
            }
.menuBar.opts add checkbutton -label [set s($lang)(umlaut_buttons)] \
        -variable param(umlaut_buttons) -command {
                if $param(umlaut_buttons) {
                    pack .search.u -pady 2
                } else {
                    pack forget .search.u
                }
            }
.menuBar.opts add checkbutton -label [set s($lang)(show_result)] \
        -variable param(show_result) -command {hideResult -1} \
        -accelerator "[set s($lang)(ctl)]+[set s($lang)(space)]"

.menuBar.opts add checkbutton -label [set s($lang)(show_status)] \
        -variable param(show_status) -command \
            { hideStatusBar $param(show_status)}

.menuBar.opts add separator
.menuBar.opts add command -label [set s($lang)(general)] -command "setGeneral" -underline 0 
.menuBar.opts add command -label [set s($lang)(soptions)] -command "setSearch" -underline 0 
.menuBar.opts add command -label [set s($lang)(saveopts)] -command "saveOptions" -underline 0 

if {$gparam(noconf)} {
    .menuBar.opts entryconfigure 7 -state disabled
}

.menuBar add cascade -menu .menuBar.opts -label [set s($lang)(options)] -underline 0

# Search parameters as menu?
    menu .menuBar.params -tearoff 1 -font lfont 

    .menuBar.params add radiobutton -label [set s($lang)(word)(0)] \
        -value 0 -variable opts(word)
    .menuBar.params add radiobutton -label [set s($lang)(word)(1)] \
        -value 1 -variable opts(word)

    .menuBar.params add separator
    .menuBar.params add radiobutton -label [set s($lang)(case)(0)] \
        -value 0 -variable opts(case)
    .menuBar.params add radiobutton -label [set s($lang)(case)(1)] \
        -value 1 -variable opts(case)
    
    .menuBar.params add separator
    for {set x 0} {$x < 5} {incr x} {
        set e [expr $x == 1 ? {[set s($lang)(error)]} : {[set s($lang)(errors)]}]
        .menuBar.params add radiobutton -label "$x $e" \
            -value $x -variable opts(errors)
    }
    .menuBar.params add radiobutton -label [set s($lang)(closestmatch)] \
        -value -1 -variable opts(errors)
    
    .menuBar.params add separator
    .menuBar.params add radiobutton -label [set s($lang)(regex)(0)] \
        -value 0 -variable opts(regex)
    .menuBar.params add radiobutton -label [set s($lang)(regex)(1)] \
        -value 1 -variable opts(regex)

.menuBar add cascade -menu .menuBar.params -label  [set s($lang)(params)] \
    -underline 0

# Program behavior menu
    menu .menuBar.props -tearoff 1 -font lfont 

    .menuBar.props add radiobutton -label [set s($lang)(onrequest)] \
        -value 0 -variable param(search_prop) \
        -command {after cancel check_selection}
    .menuBar.props add radiobutton -label [set s($lang)(onmouseover)] \
        -value 1 -variable param(search_prop) \
        -command {after cancel check_selection}
    .menuBar.props add radiobutton -label [set s($lang)(onselection)] \
        -value 2 -variable param(search_prop) \
        -command {after 500 check_selection}

    .menuBar.props add separator
    .menuBar.props add checkbutton -label [set s($lang)(min_focus)] \
        -onvalue 1 -offvalue 0 -variable param(win_prop)

.menuBar add cascade -menu .menuBar.props -label  [set s($lang)(props)] \
    -underline 0

menu .menuBar.help -tearoff 0  -font lfont
.menuBar.help add command -label  [set s($lang)(help)] -command "helpGeneral" \
    -underline 0 -accelerator "F1"
.menuBar.help add command -label  [set s($lang)(khelp)] -command "helpKeys" \
    -underline 0
.menuBar.help add command -label  [set s($lang)(cmdline)] \
    -command "helpCmdline" -underline 0
.menuBar.help add command -label  [set s($lang)(searchmeth)] -command "welcome 0" \
    -underline 0
.menuBar.help add sep
.menuBar.help add command -label "Homepage" -command "urlOpen $pinfo(homepage)"
.menuBar.help add sep
.menuBar.help add command -label  [set s($lang)(about)] -command "aboutBox" \
    -underline 0
.menuBar add cascade -menu .menuBar.help -label  [set s($lang)(help)] -underline 0

# the "no menu bar"
menu .nomenuBar -bd 0

if {(! $mini) && $param(show_menu) == 1} {
    . configure -menu .menuBar
} else {
    . configure -menu .nomenuBar
}

# Pop up menu
menu .popup -tearoff 0 -font lfont
.popup add command -label [set s($lang)(bbhistory)] -command { history back } \
    -accelerator "[set s($lang)(ctl)]+[set s($lang)(up)]" -state disabled
.popup add command -label [set s($lang)(bfhistory)] -command { history forward } \
    -accelerator "[set s($lang)(ctl)]+[set s($lang)(down)]" -state disabled

.popup add separator
.popup add command -label [set s($lang)(minimize)] -command { hideResult -1 } \
    -accelerator "[set s($lang)(ctl)]+[set s($lang)(space)]"
.popup add separator
.popup add command -label [set s($lang)(save)] -command {save 0}  \
    -underline 0 -accelerator "[set s($lang)(ctl)]+S"


frame .search -background white
pack .search -side top -fill x -expand 0

frame .search.l

pack .search.l -side right -fill y -padx 2
image create photo logo1 -data {
R0lGODdhIAAgAMIAAMDAwAAAAP///4CAgPJ8Dv///////////ywAAAAAIAAgAAADxRi63P5N
yEmrvVIJwLv/YDhoYWl6Y7CdLJiubQy8ALR0tsLRKiYENd9vpyH0MDWAMECYFY+X5LL5Mv4C
g4EgK5Eustwf9SmU+phOlVWiBXeBE7dWnBaskfDzWA21mDFoVX0VXlhbYYFkV1hhfxeJfGV5
gHt2UHMTZmCYdIJxh3OOFpCWkkqHGQyVa5sUokFXq4Ouk29DpHdRtRs6NbI2vEo5siZAxsXE
xb0luDlMBEbDddDU1dRg1tmVMjIpm9/g4eLO5MAJADs=}
image create photo logo2 -data {
R0lGODdhIAAgAMIAAMDAwAAAAP///4CAgPJ8Dv///////////ywAAAAAIAAgAAAD2Bi63P5N
yEmrvVIJwLv/nwIOGmiCgRCM5emmKpu6Jxx/5AwtnTY1nNzGdlkRB6pfUEMgWgCrCTKZIQCE
TYwECpBMqSorNqUYIM3bKAwZmAbETGeF67S9r3GpwDzl6sF3Y1pDXXs/bnApWVp+FCQ/iQKL
GHR6dpGLC3xphRRtVXiKchSNbpehkkdzUW6AmDZnaIQwOmiBeYxRVLphqIt8X4RKvLeig40q
vL2CuYUiFcWpO8I70U0vdC+vNDzavtVvBE3VmOLm5+J86OuRNO4mOcDy8/T14PfTCQA7}
image create photo logo3 -data {
R0lGODdhIAAgAMIAAMDAwAAAAP///4CAgPJ8Dv///////////ywAAAAAIAAgAAADzxi63P5N
yEmrvVIJwLv/QAB+gzaeytmVwaZ6rfiy7htmNh1CactCHF0LIxgWMTIawXjxHZsEgJJpCQwy
1Ex0WrReB9fnTwAOB7aaJfF4zWqlaeMXnJloquiWmuj0zt9cfGZIeQJ7SHaEcHpDC2V1XV2P
RYWHTUdulIuGTGF2MTd4m4dznkVBdGRtlZkZJQAVnmejQ2WmKRKpFLOBSAOwfKxrVsCKUzwb
IjwKrCrKryjNzinOrMsE2Mu8adjd3thl3+KFNuUkCrbp6uvs2u7ICQA7}
image create photo logo4 -data {
R0lGODdhIAAgAMIAAMDAwAAAAP///4CAgPJ8Dv///////////ywAAAAAIAAgAAAD2xi63P5N
yEmrvVIJwDsPXih6gzYCyqkCZbCNLriK7Su6gjx3NfpkHciC08NNUBOQ8SKrEZYbDRCTIbA0
zwoSB8owrc5YYDBAJgE5DXlQvbqyFDO7Sw2AsUZ2TVBCSyt2bgJwRzFTfGSIbWFaOHNodXdv
UD5nVDmSg1wphjl+aWuLeIBJnpeBjBQubFN/qpmEJROsdKGKqKNJtaYVrJiCcGuVj4i+SbCU
H4cYuJMXynTMyEJKfkLIKkM6N9jZKdnT1wRP1LDj5+jja+nsmTvvPApr8/T19vTU+fkCCQA7}
image create photo logo5 -data {
R0lGODdhIAAgAMIAAMDAwAAAAP///4CAgPJ8Dv///////////ywAAAAAIAAgAAAD3Ri63P5N
yEmrvVIJwLnqYCh2gxYGQjCuYYmeGSu7G4jG8kgD0fR5kN/uxktRVDeMCkAjEG9JHgCTITA1
TkmxIs1cAtYmCikYSMzdRXlgToWxT1R70KWCr6jsZkwpTTUWd2JaUBN1bGZsVXgCeltlhFN2
b3lPRpB1SpSNUIWRRgqIi4N7Pp+TjI5yFGlJR5uqZ4YqZ4prbqlJSW2ZE224pHSue1OiFYJw
rBpotKikhlu9gbBBL7TV1CxIUivIlTIL4NTYBE7YqeXp6uWI6+6bOfEtoYj19vf49dX7+wIJ
ADs=}
image create bitmap left -foreground $param(fcolor) -data {
#define l_width 9
#define l_height 13
static char l_bits[] = {
 0x80,0x00,0xc0,0x00,0xe0,0x00,0x70,0x00,0x38,0x00,0x1c,0x00,0x0e,0x00,0x1c,
 0x00,0x38,0x00,0x70,0x00,0xe0,0x00,0xc0,0x00,0x80,0x00};
}
image create bitmap right -foreground $param(fcolor) -data {
#define r_width 9
#define r_height 13
static char r_bits[] = {
 0x02,0x00,0x06,0x00,0x0e,0x00,0x1c,0x00,0x38,0x00,0x70,0x00,0xe0,0x00,0x70,
 0x00,0x38,0x00,0x1c,0x00,0x0e,0x00,0x06,0x00,0x02,0x00};
}
image create bitmap up -foreground $param(fcolor) -data {
#define u_width 13
#define u_height 9
static char u_bits[] = {
 0x00,0x00,0x40,0x00,0xe0,0x00,0xf0,0x01,0xb8,0x03,0x1c,0x07,0x0e,0x0e,0x07,
 0x1c,0x00,0x00};
}
image create bitmap down -foreground $param(fcolor) -data {
#define d_width 13
#define d_height 9
static char d_bits[] = {
 0x00,0x00,0x07,0x1c,0x0e,0x0e,0x1c,0x07,0xb8,0x03,0xf0,0x01,0xe0,0x00,0x40,
 0x00,0x00,0x00};
}

button .search.l.logo -image logo1 -bd 1 -relief raised -command Reset
pack .search.l.logo -side right -expand 1 -anchor n

set balloonHelp(.search.l.logo) [set s($lang)(minimize)]
bindtags .search.l.logo [list balloon .search.l.logo Button all]

set logoanim 1
proc animlogo {t} {
    global logoanim

    # debug 2 "animlogo $t"
    if ![winfo exists $t] {
        after cancel animlogo $t
        return
    }
    if {$logoanim >= 5} {
        set logoanim 1
    } else {
        incr logoanim
    }
    $t configure -image "logo$logoanim"
    after 100 "animlogo $t"
}

frame .search.s -background white
pack .search.s -side top -expand 0 -padx 5
button .search.s.back -state disabled -height 15 -width 15 -image left \
    -command { history back }
set balloonHelp(.search.s.back) [set s($lang)(bbhistory)]
bindtags .search.s.back [list balloon .search.s.back Button all]

button .search.s.forw -state disabled -height 15 -width 15 -image right \
    -command { history forward }
set balloonHelp(.search.s.forw) [set s($lang)(bfhistory)]
bindtags .search.s.forw [list balloon .search.s.forw Button all]

label .search.s.label -text " [set s($lang)(query)]" -font lfont
entry .search.s.entry -width 17 -selectbackground blue -selectforeground white \
    -textvariable query -relief sunken -font tfont

button .search.s.button -text [set s($lang)(search)] -font lfont \
    -command "dictsearch \$query"

bind .search.s.button <2> {
    # internal search = search in result text
    display $curhistory $query 0
}
set balloonHelp(.search.s.button) [set s($lang)(searchhelp)]
bindtags .search.s.button [list balloon .search.s.button Button all]

menubutton .search.s.searchmeth -text $searchmeth($param(cursearchmeth),name) \
    -menu .search.s.searchmeth.m -indicatoron 1 -relief raised -anchor c \
    -direction flush -font lfont -width 12
set balloonHelp(.search.s.searchmeth) [set s($lang)(searchmeth)]
bindtags .search.s.searchmeth [list balloon .search.s.searchmeth Menubutton all]
menu .search.s.searchmeth.m -font lfont -tearoff 0

button .search.s.clear -text [set s($lang)(clear)] -font lfont \
    -command {set query ""}

pack .search.s.back .search.s.forw .search.s.label .search.s.entry -side left
pack .search.s.searchmeth -side left -pady 5 -padx 5
pack .search.s.button -side left -pady 5
pack .search.s.clear -side left -pady 5 -padx 5


# adjust size of history buttons
set h [winfo reqheight .search.s.label]

if {$h > 0} {
    debug 8 "new history button size: $h"
    .search.s.back configure -height $h -width $h
    .search.s.forw configure -height $h -width $h
}

# Umlaut buttons?
frame .search.u
button .search.u.aumlaut -text "ä" -font lfont -command {.search.s.entry insert insert "ä"} 
button .search.u.oumlaut -text "ö" -font lfont -command {.search.s.entry insert insert "ö"}
button .search.u.uumlaut -text "ü" -font lfont -command {.search.s.entry insert insert "ü"}
button .search.u.bigaumlaut -text "Ä" -font lfont -command {.search.s.entry insert insert "Ä"}
button .search.u.bigoumlaut -text "Ö" -font lfont -command {.search.s.entry insert insert "Ö"}
button .search.u.biguumlaut -text "Ü" -font lfont -command {.search.s.entry insert insert "Ü"}
button .search.u.eszet -text "ß" -font lfont -command {.search.s.entry insert insert "ß"}

pack .search.u.aumlaut .search.u.oumlaut .search.u.uumlaut .search.u.bigaumlaut .search.u.bigoumlaut .search.u.biguumlaut .search.u.eszet -side left

if {$param(umlaut_buttons) == 1} {
    pack .search.u -side top -pady 2 
}

# Search parameters as multiple menus?
    frame .search.opts
    menubutton .search.opts.optword -textvariable wlabel -menu .search.opts.optword.menu \
        -indicatoron 1 -relief raised -anchor c -direction flush -font lfont \
        -width 11
    set balloonHelp(.search.opts.optword) [set s($lang)(bwords)]
    bindtags .search.opts.optword [list balloon .search.opts.optword Menubutton all]
    menu .search.opts.optword.menu -font lfont -tearoff 0
    .search.opts.optword.menu add command -label [set s($lang)(word)(0)] -font lfont -command \
        {set opts(word) 0; set wlabel [set s($lang)(word)(0)]}
    .search.opts.optword.menu add command -label [set s($lang)(word)(1)] -font lfont -command \
        {set opts(word) 1; set wlabel [set s($lang)(word)(1)]}
    set wlabel [set s($lang)(word)($opts(word))]

    menubutton .search.opts.optcase -textvariable clabel -menu .search.opts.optcase.menu \
        -indicatoron 1 -relief raised -anchor c -direction flush -font lfont \
        -width 13
    set balloonHelp(.search.opts.optcase) [set s($lang)(bcase)]
    bindtags .search.opts.optcase [list balloon .search.opts.optcase Menubutton all]
    
    menu .search.opts.optcase.menu -font lfont -tearoff 0
    .search.opts.optcase.menu add command -label [set s($lang)(case)(0)] -font lfont \
        -command {set opts(case) 0; set clabel [set s($lang)(case)(0)]}
    .search.opts.optcase.menu add command -label [set s($lang)(case)(1)] -font lfont \
        -command {set opts(case) 1; set clabel [set s($lang)(case)(1)]}
    
    set clabel [set s($lang)(case)($opts(case))]
    
    menubutton .search.opts.errors -textvariable elabel -menu .search.opts.errors.menu \
        -indicatoron 1 -relief raised -anchor c -direction flush -font lfont \
        -width 9
    set balloonHelp(.search.opts.errors) [set s($lang)(berrors)]
    bindtags .search.opts.errors [list balloon .search.opts.errors Menubutton all]
    menu .search.opts.errors.menu -font lfont -tearoff 0
    
    # set opts(errors) 0
    if {[expr $opts(errors) == -1]} {
        set elabel [set s($lang)(closestmatch)]
    } else {
        set elabel "$opts(errors) [expr $opts(errors) == 1 ? {[set s($lang)(error)]} : {[set s($lang)(errors)]}]"
    }
    
    for {set x 0} {$x < 5} {incr x} {
        set e [expr $x == 1 ? {[set s($lang)(error)]} : {[set s($lang)(errors)]}]
        .search.opts.errors.menu add command -label "$x $e" -font lfont \
            -command "
                set opts(errors) $x; set elabel \"$x $e\""
    }
    .search.opts.errors.menu add command -label [set s($lang)(closestmatch)] -font lfont \
        -command {set opts(errors) -1; set elabel [set s($lang)(closestmatch)]}
    
    
    menubutton .search.opts.regex -textvariable rlabel -menu .search.opts.regex.menu \
        -indicatoron 1 -relief raised -anchor c -direction flush -font lfont \
        -width 13
    set balloonHelp(.search.opts.regex) [set s($lang)(bregex)]
    bindtags .search.opts.regex [list balloon .search.opts.regex Menubutton all]
    menu .search.opts.regex.menu -font lfont -tearoff 0
    .search.opts.regex.menu add command -label [set s($lang)(regex)(0)] -font lfont -command \
        {set opts(regex) 0; set rlabel [set s($lang)(regex)(0)]}
    .search.opts.regex.menu add command -label [set s($lang)(regex)(1)] -font lfont -command \
        {set opts(regex) 1; set rlabel [set s($lang)(regex)(1)]}
    set rlabel [set s($lang)(regex)($opts(regex))]
    
    pack .search.opts.optword .search.opts.optcase .search.opts.errors \
        .search.opts.regex -side left -fill x -padx 2

if {$param(params_as_menu) == 1} {
    pack .search.opts -side top -pady 2
}

####### Results
frame .result 
if {! $mini} {
    # pack .result -side top -fill both -expand yes -padx 0 -pady 0
}
text .result.text -bd 1 -state disabled -wrap none -relief groove \
     -padx 2 -pady 2 -font tfont -width 76 -height 16 \
     -yscrollcommand ".result.yscroll set"  \
     -xscrollcommand [list xscroll .result.xscroll \
        [list grid .result.xscroll  -in .result -row 1 -column 0 -sticky ew]]
 
     # -yscrollcommand [list yscroll .result.yscroll 
     #     [list grid .result.yscroll  -in .result -row 0 -column 1 -sticky ns]] 
 
scrollbar .result.yscroll -orient vertical -command { .result.text yview }
scrollbar .result.xscroll -orient horizontal -command { .result.text xview }
if {$param(isunix)} {          # I like smaller scrollbars
    .result.yscroll configure -width 10 -bd 1
    .result.xscroll configure -width 10 -bd 1
}

grid .result.text -in .result -row 0 -column 0 -sticky news -padx 0 -pady 0 -ipadx 0 -ipady 0
grid .result.yscroll  -in .result -row 0 -column 1 -sticky ns
# grid .result.xscroll  -in .result -row 1 -column 0 -sticky we
# grid .result.yscroll -sticky news
# grid .result.xscroll -sticky news
grid rowconfig    .result 0 -weight 1 -minsize 0
grid columnconfig .result 0 -weight 1 -minsize 0
# grid remove .result.xscroll
# grid remove .result.yscroll
# grid configure .result.text -columnspan 2
grid configure .result.text -rowspan 2


####### Status bar
frame .statusBar
# hideStatusBar $param(show_status)

label .statusBar.lab -width 46 -relief sunken -bd 1 -height 1 \
    -text "[set s($lang)(cmd)] $searchmeth($param(cursearchmeth),grepcmd)" \
    -font sfont -anchor w
label .statusBar.file -width 30 -relief sunken -bd 1 -height 1 \
    -font sfont -anchor w -justify right \
    -text $searchmeth($param(cursearchmeth),dictfile)

pack .statusBar.lab  -side left -padx 1 -pady 1 -expand yes -fill x
pack .statusBar.file -side left -padx 1 -pady 1 -expand yes -fill x


update_searchmeth_menu
set_opts_errors $param(cursearchmeth) 

# Key bindings
bind .search.s.entry <Return> "dictsearch \$query"
bind .search.s.entry <Shift-Return> {display $curhistory $query 0}
bind .search.s.entry <Shift-BackSpace> {set query ""}
bind .search.s.entry <Control-Up>   [list history back]
bind .search.s.entry <Control-Down> [list history forward]
bind .search.s.entry <Escape> Reset

# These key bindings look a bit strange: scroll result's text with key bindings
# to the entry. But the entry always has the focus...

bind .search.s.entry <Up>         [list .result.text yview scroll -1 units]
bind .search.s.entry <Down>       [list .result.text yview scroll 1 units]
bind .search.s.entry <Shift-Up>   [list .result.text yview scroll -5 units]
bind .search.s.entry <Shift-Down> [list .result.text yview scroll 5 units]
bind .search.s.entry <Prior>      [list .result.text yview scroll -1 pages]
bind .search.s.entry <Next>       [list .result.text yview scroll 1 pages]
bind .search.s.entry <Home>       [list .result.text yview moveto 0]
bind .search.s.entry <End>        [list .result.text yview moveto 1.0]
bind .search.s.entry <Shift-Left>  [list .result.text xview scroll -5 units]
bind .search.s.entry <Shift-Right> [list .result.text xview scroll 5 units]
bind .search.s.entry <F1> helpGeneral
bind .search.s.entry <Control-s> {save 0}
bind .search.s.entry <Control-l> {save 1}
bind .search.s.entry <Control-q> {
    if {$gparam(autosave) != 0} {
        saveOptions
    }
    exit
}
bind .search.s.entry <Control-m> sendMail
bind .search.s.entry <Control-o> {notice $param(noticefile)}
bind .search.s.entry <Control-n> startNew
bind .search.s.entry <Control-space> {hideResult -1}
bind .search.s.entry <Control-Shift-space> {hideMenuBar [expr $param(show_menu) == 1 ? 0 : 1]}

# change font on the fly
bind .search.s.entry <Control-plus> {
    set param(tfont) [list [lindex $param(tfont) 0] [expr [lindex $param(tfont) 1] + 1] [lindex $param(tfont) 2]]
     chFont tfont
    .statusBar.lab config -foreground $param(fcolor) -text "$param(tfont)"
}
bind .search.s.entry <Control-minus> {
    set fs [lindex $param(tfont) 1]
    if {$fs > 4} {
        set param(tfont) [list [lindex $param(tfont) 0] [expr $fs - 1] [lindex $param(tfont) 2]]
        chFont tfont
        .statusBar.lab config -foreground $param(fcolor) -text "$param(tfont)"
    }
}
bind .search.s.entry <Control-equal> {
    set fs [lindex $default(tfont) 1]
    set param(tfont) [list [lindex $param(tfont) 0] $fs [lindex $param(tfont) 2]]
    chFont tfont
    .statusBar.lab config -foreground $param(fcolor) -text "$param(tfont)"
}

bind Text <Double-1> {
    global query
    set tkPriv(selectMode) word
    tkTextSelectTo %W %x %y
    catch {%W mark set insert sel.last}
    catch {%W mark set anchor sel.first}
    catch {set q [selection get -displayof %W]}
    if {[string length $q] > 0 && [string compare $query $q]} {
        set query $q 
        dictsearch $query
    }
}

bind . <<PasteSelection>> { 
    global query
    set tkPriv(x) %x
    set tkPriv(y) %y 
    debug 8 "  Event <<PasteSelection>>: "
    catch {set q [selection get]}
    if {[info exists q] && [string length $q] > 0 && [string compare $query $q]} {
         debug 8 "     $q"
        set query $q
        dictsearch $query
    }
}
# right mouse button == popup menu
bind . <3> {
    debug 8 "  Event <3>: .popup"
    tk_popup .popup %X %Y 
}

# Polling if global selection changes - for "search on text selection"
proc check_selection {} {
    global last_selection param query

    debug 2 "check_selection: "
    set param(check_selection_active) 1
    catch {set q [selection get]}
    if {[info exists q] && [string length $q] && \
        [string compare $last_selection $q] && 
        [string compare $query $q]} {
        debug 2 "New selection: $q"
        set last_selection $q
        dictsearch $last_selection
    }
    after 500 check_selection
}


bind . <FocusIn> {
    global query param
    debug 2 "FocusIn %W"
    if {[string compare %W "."] == 0} {
    if {$param(search_prop) == 2} {
        debug 2 "canceling check_selection"
        set param(check_selection_active) 0
        after cancel check_selection
    }
        if {$param(search_prop) == 1} {
            # Search when we have Focus and a selection
            catch {set q [selection get]}
            if {[info exists q] && [string length $q] > 0 && [string compare $query $q]} {
                debug 8 "     $q"
                set query $q
                catch [dictsearch $query]
            }
        }
        if {$param(win_prop) == 1} {
            # full window when focus in
            hideResult 1
        }
    }
}

bind . <FocusOut> {
    global param
    debug 2  "FocusOut %W"
    if {[string compare %W "."] == 0} {
    if {$param(search_prop) == 2} {
        if {$param(check_selection_active) == 0} {
            after 500 check_selection
        }
    }
        if {$param(win_prop) == 1} {
            # mini window when focus out
            hideResult 0
        }
    }
}

proc catch_resize {} {
    global param curhistory history_result
    debug 8  "catch_resize"
    # update

    if { $curhistory > 0 && [string length $history_result($curhistory)] > 0} {
        display $curhistory "" 1
    }
    scan [wm geometry .] {%dx%d+} param(width) param(height)
    debug 8 " catch_resize - new size: $param(width) x $param(height)"
}

# catch resize event to redisplay result text
bind dlookup <Configure> catch_resize

# bind .search.s.entry <Delete> {set query ""}

bindtags .search.s.entry [list .search.s.entry Entry all]

# set tags for result text
if {[winfo depth .] > 1} {      # Color display
    set cols [shadeColor $param(fcolor) $param(bcolor)]
    set param(shadedcolor) [lindex $cols 0]
    set param(highcolor) [lindex $cols 1]
    set param(errcolor) [lindex $cols 2]
    .result.text tag configure bg1 -background $param(shadedcolor)
    .result.text tag configure matchfg -foreground $param(highcolor)
    .result.text tag configure search -background $param(highcolor) -foreground $param(bcolor)
    .result.text tag configure u -background $param(fcolor) -foreground $param(bcolor)
} else {
    set param(shadedcolor) white
    set param(highcolor) white
    .result.text tag configure bg1 -background $param(shadedcolor) -foreground black
    .result.text tag configure matchfg -background black -foreground white
    .result.text tag configure search -background black -foreground white
    .result.text tag configure u -underline 1
}

if 0 {
# commented out
if {![info exists grepcmds(agrep)]} {
    .result.text configure -state normal
    .result.text insert end [set s($lang)(noagrep)]
    .result.text insert end $gparam(grepcmd)
    .result.text configure -state disabled
}
}

setSearchBg $param(shadedcolor)

if ($mini) {
    wm geometry . {}
    hideResult 0
    set balloonHelp(.search.l.logo) [set s($lang)(normalsize)]
    .popup entryconfigure 3 -label [set s($lang)(normalsize)]
    update
        if [info exists param(width)] {
            set w $param(width)
        } else {
            set w [winfo width .search]
        }
        debug 8 "  setting geometry: ${w}x[winfo height .search]"
        wm geometry . "${w}x[winfo height .search]"
        # wm geometry . {}
} else {
    wm geometry . {}
    update
    hideResult 1
}

# if {(! $mini) && [info exists param(width)] && [info exists param(height)]} {
#     wm geometry . "$param(width)x$param(height)"
# } else {
#     wm geometry . {}
# }

update
set geo [wm geometry .]
scan $geo {%dx%d+%d+%d} w h x y
debug 8 "toplevel geometry: $geo, winfo: [winfo width .] [winfo height .] [winfo x .] [winfo y .], correcting height: $param(add_geom_height)"

if {! $mini} {
    set param(width) $w
    set param(height) $h
}

if {[string length $query] == 0 && ! $mini} {
    # welcome message if not started with search query and not mini size
    welcome 1
}
bindtags .result.text {Text . all dlookup}

focus .search.s.entry

# }
# mainWindow {}

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

# Handling of external processes
proc ExecCmd {cmd stderr_to_stdout} {
    global proc lines param

    debug 1 "ExecCmd $cmd $stderr_to_stdout"
    if {$proc(pid) > 0} {
        .statusBar.lab config -foreground $param(errcolor) -text \
            "Search process is still running. Wait or hit Escape."
        return
    }
    if {$stderr_to_stdout == 1} {
        set cmd [concat $cmd {2>@stdout}]
    }
    set lines {}
    if [catch {set proc(pipe) [open "|$cmd" r+]} err] {
        if [string match "child process exited abnormally" $err] {
            .statusBar.lab config -foreground $param(errcolor) -text "[set s($lang)(noresults)] $query"
        } else {
            .statusBar.lab config -foreground $param(errcolor) -text $err
        }
        return
    }
    debug 4 "Pipe: $proc(pipe)"
    set proc(pid) [pid $proc(pipe)]
    set proc(processlist) [list $proc(pid) $cmd]
    # fconfigure $proc(pipe) -buffering none -blocking 0
    fileevent $proc(pipe) readable "CmdOut"
}

proc CmdOut {} {
    global proc sigchld lines param searchmeth

    debug 2 "CmdOut"
    set r1 [catch {eof $proc(pipe)} res]
    if {$r1 || $res} {
        debug 4 "  CmdOut: End of $proc(pipe): $proc(processlist)"
        catch {close $proc(pipe)} err
        debug 4 "  CmdOut: Close pipe: $err"
        set sigchld 1
        # mark as finished
        set proc(processlist) {}
        set proc(pid) -1
        return
    }
    # else read from the pipe
    set line [read $proc(pipe)]
    if {[llength $lines] > 10 * $searchmeth($param(cursearchmeth),maxresults)} {
        # Reset
        return
    }
    # if {$line != ""}i
    foreach l [split $line "\n"] {
        lappend lines $l
        debug 4 "  CmdOut: read [llength $lines] .$l."
        if {[llength $lines] > 10 * $searchmeth($param(cursearchmeth),maxresults)} {
            debug 4 "  CmdOut: more than $searchmeth($param(cursearchmeth),maxresults) read, stopping"
            Reset
            break
        }
    }
}

proc dictsearch {query args} {
    global param gparam searchmeth result opts
    global curhistory inshistory 
    global history_result history_query history_searchm history_pos
    global s logoanim defaultcursor lines sigchld

    debug 1 "dictsearch $query"
    set lang $gparam(lang)

    # search method specified
    if [llength $args] {
        set c [lindex $args 0]
        if [info exists searchmeth($c,name)] {
            set param(cursearchmeth) $c
            .search.s.searchmeth configure -text $searchmeth($c,name)
            set_opts_errors $c
        }
    }
    set curr $param(cursearchmeth)
    debug 1 "  using search method $curr ($searchmeth($curr,name))"

    if {[string length $searchmeth($curr,dictfiles)] <= 0} {
        .statusBar.lab config -foreground $param(errcolor) -text [set s($lang)(nodictfile)]
        return
    }

    # clean up query
    # remove spaces and so at the beginning and the end
    regsub -all {^[ ,;:+'\n\t]*} $query {} query
    regsub -all {[ ,:-;'\n\t]*$} $query {} query
    # remove duplicate spaces
    regsub -all {   *} $query { } query
    # remove newline, tab, \
    regsub -all "\[\n\r\t\]$" $query {} query
    regsub -all "\[\n\r\t\]" $query { } query
    regsub -all {\\$} $query {} query

    if {[string match "*spell" $searchmeth($curr,grepcmd)]} {
        set squery $query
        regsub -all {ä} $squery {a"} squery
        regsub -all {Ä} $squery {A"} squery
        regsub -all {ö} $squery {o"} squery
        regsub -all {Ö} $squery {O"} squery
        regsub -all {ü} $squery {u"} squery
        regsub -all {Ü} $squery {U"} squery
        regsub -all {ß} $squery {s"} squery
    } else {
        regsub -all {"a} $query {ä} query
        regsub -all {"A} $query {Ä} query
        regsub -all {"o} $query {ö} query
        regsub -all {"O} $query {Ö} query
        regsub -all {"u} $query {ü} query
        regsub -all {"U} $query {Ü} query
        regsub -all {"s} $query {ß} query
        regsub -all {"}  $query "" query
        set squery $query
    }
    debug 8 "    query = $query, squery = $squery"

    .search.s.entry delete 0 end
    .search.s.entry insert 0 $query
    # .search.s.entry selection range 0 [string length $query]

    if {[string length $squery] < $searchmeth($curr,minlength)} {
        .statusBar.lab config -foreground $param(errcolor) -text [set s($lang)(tooshort)]
        return
    }
    if {[string length $squery] > $searchmeth($curr,maxlength)} {
        .statusBar.lab config -foreground $param(errcolor) -text \
            "[set s($lang)(toolong)] ([string length $squery], max. $searchmeth($curr,maxlength))"
        return
    }
    # escape shell meta chars -> \
    #
    regsub -all {([]\[\{\} `&$])} $squery {\\\1} squery

    if [string match "agrep*" $searchmeth($curr,grepcmd)] {
        # search with agrep
        if {$opts(regex) == 0} {
            # prepare simple pattern for agrep
            # simple pattern
            regsub -all {\*} $squery "#" squery
            # AND
            # delete lonesome + at the end
            regsub -all {\+$} $query "" query
            regsub -all {\+$} $squery "" squery
            regsub -all { *\+ *} $squery ";" squery
        } elseif [regexp {[*?.|]} $squery] {
            # reg expression - switch off word searching
            set opts(word) 1
        }
    }
    # evaluate options
    set isgrep 0
    set isspell 0
    set opt $searchmeth($curr,grepopts)
    set internal 0
    set q $query
    set cmd {}
    set stderr_to_stdout 1

    if [string match "internal_*" $searchmeth($curr,grepcmd)] {
        # internal search
        set cmd [list $searchmeth($curr,grepcmd) $query $searchmeth($curr,dictfiles) $searchmeth($curr,maxresults)]
        set internal 1
        # for sorting
        set isgrep 1
    } elseif {[string match "*grep" $searchmeth($curr,grepcmd)]} {
        # grep family
        set isgrep 1
        if {$opts(word) == 0 && ![regexp { } $squery] && $opts(errors) != -1} {
            # words only search - makes no sense if search string contains
            # white space, or when "best match" used
            set opt "$opt -w"
        }
        if {$opts(case) == 0} {
            set opt "$opt -i"
        }
        if ($opts(errors)) {
            if {$opts(errors) == -1} {
                set opt "$opt -B -y"
            } else {
                set opt "$opt -$opts(errors)"
            }
        }
        if {$opts(regex) == 0} {
            if [string match "agrep*" $searchmeth($curr,grepcmd)] {
            # prepare simple pattern for agrep
                regsub -all {\*} $squery "#" squery
            }
            # q is for priority sorting
            regsub -all {[[\*+)(]} $query "" q
        }
        set opt "$opt -e"
        set cmd [concat "$searchmeth($curr,grepcmd) $opt \"$squery\" $searchmeth($curr,dictfiles)"]
    } elseif {[string match "*spell" $searchmeth($curr,grepcmd)]} {
        # spell family
        set isspell 1
        set cmd [concat "echo \'$squery\' | $searchmeth($curr,grepcmd) $opt"]
    } elseif {[string match "*fortune" $searchmeth($curr,grepcmd)]} {
        # fortune
        set stderr_to_stdout 0
        if {[string length $squery] > 0} {
            # fortune with pattern
            if {$opts(case) == 0} {
                set opt "$opt -i"
            }
            if {$opts(word) == 0} {
                # words only search - add a space after word...
                # set squery "$squery\[ !?.,\]"
                set squery "$squery\[ !?.,\]"
            }
            set opt "$opt -m \"$squery\""
        }
        set cmd [concat "$searchmeth($curr,grepcmd) $opt"]

    } else {
        # this is for dict: 
        # setting more than one search strategy is useless
        if {$opts(word) == 1 || [regexp { } $squery]} {
            set opt "$opt -s substring"
        }
        if {$opts(errors) >= 1} {
            set opt "$opt -s lev"
        } elseif {$opts(errors) == -1} {
            set opt "$opt -s soundex"
        }
        if {$opts(regex) == 1} {
            set opt "$opt -s re"
        }
        
        set cmd [concat "$searchmeth($curr,grepcmd) $opt \"$squery\""]
    }

    .statusBar.lab config -foreground $param(fcolor) -text "$cmd"

    . configure -cursor watch
    .result.text configure -cursor watch
    .search.s.entry configure -cursor watch
    update
    
    # Suche!
    set res ""
    set logoanim 1
    animlogo .search.l.logo

    debug 1 "dictsearch: Starting $cmd"
    if ($internal) {
        set lines [eval $cmd]
    } else {
        set sigchld 0
        ExecCmd $cmd $stderr_to_stdout
        tkwait variable sigchld
    }
    # ok, read results
    update idletasks
    set prio 0
    set resl {}
    set c 0
    foreach line $lines {
    # foreach line [split $lines "\n"]
        update idletasks
        incr c
        debug 2 "dictsearch: result $c: $line." 
        set p 0
        if {!$isspell && [regexp "^#" $line]} {      # skip comments
            continue
        }
        # sorting of *grep
        if {$isgrep} {
            if {$line == ""} {continue}
            if [string match "Grand Total*" $line] {
                # get rid of Windows agrep's last line ...
                continue
            }
            # find good matches (word without context, exact order etc.)
            if {[regexp {[ +]} $query]} {
                set p [regexp -nocase $q $line]
            } else {
                if {[regexp {\|} $line]} {
                    if {[regexp -nocase ":: (to )?$q \\|" $line] ||
                        [regexp -nocase "\[^|\]*$q\[^|\]*\\|.+ ::" $line] } {
                        set p 1
                    }
                } else {
                    if {[regexp -nocase ":: (to )?$q$" $line] ||
                        [regexp -nocase "^$q (\[\[\{(\].*\[\]\})\] )?::" $line]} {
                        set p 1
                    }
                }
            }
        }
        # set p 0
        if $p {
            debug 8 "dictsearch: good match: $prio: $line"
            set resl [linsert $resl $prio $line]
            incr prio
        } else {
            lappend resl $line
        }
    }
    set res [join $resl "\n"]
    if [string length $res] {
        # results found
        if {$inshistory >= $param(maxhistory)} {
            set inshistory 1
        } else {
            incr inshistory
        }
        .search.s.forw configure -state disabled
        .popup entryconfigure 1 -state disabled
        if {$curhistory != 0} {
            .search.s.back configure -state normal
            .popup entryconfigure 0 -state normal
            # mark the current scroll position
            set history_pos($curhistory) [lindex [.result.text yview] 0]
        }
        set curhistory $inshistory
        set history_query($inshistory) $query
        set history_searchm($inshistory) $curr
        set history_result($inshistory) $res
        set history_pos($inshistory) 0
        display $inshistory "" 1
    } else {
        # no results found
        .search.s.entry delete 0 end
        .search.s.entry insert 0 $query
        # .search.s.entry selection range 0 [string length $query]
        .statusBar.lab config -foreground $param(errcolor) -text \
            "[set s($lang)(noresults)] $query"
        .result.text configure -state normal
        .result.text yview moveto 0
        .result.text configure -state disabled
    }

    catch {close $in}
    after 400 {
        after cancel animlogo .search.l.logo
        .search.l.logo configure -image "logo1"
    }

    . configure -cursor $defaultcursor
    .result.text configure -cursor $defaultcursor
    .search.s.entry configure -cursor $defaultcursor
}

# display: shows search results:
#      num    - show result for number in history
#      search - highlight this string in result
#      status - update status line, remove scrollbars?

proc display {num search status} {
    global searchmeth maxresults param gparam s
    global history_query history_searchm history_result history_pos history_spos

    debug 1 "display num = $num search = $search status = $status"

    # make result visible
    hideResult 1
    wm deiconify .

    if ![info exists history_query($num)] {
        return
    }

    set lang $gparam(lang)
    set count 0
    set more ""
    set shape 0

    if {[string length $search] == 0} {
        .search.s.entry delete 0 end
        .search.s.entry insert 0 $history_query($num)
    }

    # selection to the query in search entry when no other selection active
    catch {set q [selection get]}
    if {![info exists q] || \
        ([string length $q] > 0 && 
         [string compare $q $history_query($num)] == 0)} {
        .search.s.entry selection range 0 end
    }

    set t .result.text
    $t configure -state normal
    $t delete 0.0 end
    # remove scrollbars
    if {$status == 1} {
        ### fri
        grid forget .result.xscroll
        # grid forget .result.yscroll
        # grid configure .result.text -columnspan 2
        grid configure .result.text -rowspan 2
    }

    # compute tab stop in the middle
    # set width [$t cget -width]
    set width [winfo width $t]
    set w [expr round(($width / 2) - 2)]
    $t configure -tabs $w


    if {[string compare $searchmeth($history_searchm($num),language1) ""] || \
        [string compare $searchmeth($history_searchm($num),language2) ""]} {
        $t insert end \
        "$searchmeth($history_searchm($num),language1)\t  $searchmeth($history_searchm($num),language2)\n" u
    }

    set isdict [expr [string compare $searchmeth($history_searchm($num),grepcmd) "dict"] ? 0 : 1]
    set isfortune [string match "*fortune" $searchmeth($history_searchm($num),grepcmd)]
    set isspell [string match "*spell" $searchmeth($history_searchm($num),grepcmd)]
    if {$isspell} {     ; # an empty line ...
        $t insert end "\n"
    }
    set spell_version {}
    set ftext 0
    set ffirst 1
    set xscroll 0
    foreach l [split $history_result($num) "\n"] {
        if {!$isspell && [regexp "^#" $l]} {      # skip comments
            continue
        }
        if {$count >= $searchmeth($history_searchm($num),maxresults)} {
            set more [set s($lang)(more)]
            break
        }
        if {$isdict == 1} {
            # dict - Find a "NUM definition found" l
            if {[regexp {^([0-9]+) definitions? found} $l b c]} {
                set count $c
                continue
            }
        } elseif {$isfortune == 1} {
            # fortune 
            if {! $ftext && [regexp {^$} $l]} {
                continue
            }
            if {[regexp {^\(.*\)$} $l]} {
                set ftext 0
                continue
            }
            if {[regexp "^%$" $l]} {
                set l ""
                if {$searchmeth($history_searchm($num),shapedresult)} {
                    set shape [expr ! $shape]
                }
                if $ftext {
                    incr count
                    $t insert end "\n"
                }
                continue
            } else {    
                # fortune text
                # substitute tabs
                if $ffirst {
                    incr count
                    set ffirst 0
                    $t insert end "\n"
                }
                set ftext 1
                regsub "\t" $l "    " l
            }
        } elseif {$isspell == 1} {
          # ispell -a results:
          # fist line: @(#) Version
          # * ok
          # + <root word>
          # - <concatenation>,
          # & <original>  <count>  <offset>:  <miss>,  <miss>, ..., <guess>, ...
          # ? <original> 0 <offset>: <guess>, <guess>, ...
          # # <original> <offset>

            if {[regexp {^@\(#\) (.+)} $l b spell_version]} {
                continue
            } elseif {[regexp {^$} $l]} {
                continue
            } elseif {[regexp {^\*} $l]} {
                set l [set s($lang)(correct)]
            } elseif {[regexp {^\+} $l]} {
                regsub {^\+} $l "[set s($lang)(root)]:" l
            } elseif {[regexp {^\#} $l]} {
                set l [set s($lang)(nosuggestion)]
            } else {
                regsub {.+:} $l "[set s($lang)(suggestion)]: " l
            }
            regsub -all {a"} $l {ä} l
            regsub -all {A"} $l {Ä} l
            regsub -all {o"} $l {ö} l
            regsub -all {O"} $l {Ö} l
            regsub -all {u"} $l {ü} l
            regsub -all {U"} $l {Ü} l
            regsub -all {s"} $l {ß} l

            incr count 
        } else {
            # not dict: 1 line == 1 match
            incr count 
            # ignore <...> - old/wrong spelling
            regsub -all {<[^>]*>} $l {} l
        }
        if {[string compare $searchmeth($history_searchm($num),separator) ""] && \
            [regexp "$searchmeth($history_searchm($num),separator)" $l]} {
            regsub "$searchmeth($history_searchm($num),separator).*" $l "" lang1
            regsub ".*$searchmeth($history_searchm($num),separator)" $l "" lang2
            if {[regexp {\|} "$lang1"]} {
                # contains additional entries: separate on different lines
                set output {}
                set l1 [split $lang1 |]
                set l2 [split $lang2 |]
                for {set i 0} {$i < [llength $l1]} {incr i} {
                    if {$i > 0} {
                        set output "$output\n"
                    }
                    set output "$output[lindex $l1 $i]\t  [lindex $l2 $i]"
                }
            } else {
                set output "$lang1\t  $lang2"
            }
        } else {
            set output $l
        }
        
        # regsub ".\{40\}" $lang1
        if {$searchmeth($history_searchm($num),shapedresult)} {
            if {$isdict == 1} {
                if {[regexp "^From " $l]} {
                    set shape 1
                } else {
                    set shape 0
                }
            } elseif {! $isfortune} {
                if {[expr $count % 2]} {
                    set shape 1
                } else {
                    set shape 0
                }
            }
        }
        if {$shape} {
            $t insert end "$output\n" bg1
        } else {
            $t insert end "$output\n"
        }

    }

    # if {$xscroll == 1} {
    #     grid .result.xscroll -in .result -row 1 -column 0 -sticky we
    #     debug 4 "placing xscrollbar"
    # } else {
    #     grid forget .result.xscroll
    #     debug 4 "removing xscrollbar"
    # }

    if {$status == 1} {
        set erg [expr $count == 1 ? {[set s($lang)(result)]} : {[set s($lang)(results)]}]
        if {$isdict == 1 && $count == 0} {
            set count "??"
        }
        .statusBar.lab config -foreground $param(fcolor) -text "$count $erg $more"
        if {$isspell == 1} {
            .statusBar.file config -foreground $param(fcolor) -text $spell_version
        }
    }
    # find and mark match pattern
    if {[string length $history_query($num)] > 0} {
        regsub -all {[,+]} $history_query($num) "|" mquery
        regsub -all {[[\*+)(]} $mquery "" mquery
        regsub -all {  } $mquery " " mquery

        # mark query
        set cur 1.0
        while 1 {
            set cur [.result.text search -nocase -count length -regexp -- $mquery $cur end]
            if {$cur == ""} {
                break
            }
            .result.text tag add matchfg $cur "$cur + $length char"
            set cur [.result.text index "$cur + $length char"]
        }
    }
    # search pattern ?
    if {[string length $search] > 0} {
        # remove special chars
        regsub -all {\*} $search {} search
        if {[string length $search] <= 0} {
            return
        }
        if {$status == 1 || ![info exists history_spos] || $history_spos == 0} {
            set history_spos 1.0
        }
        set found 0
        set first {}
        # mark search words
        set cur 1.0
        while 1 {
            set cur [.result.text search -nocase -count length -exact -- $search $cur end]
            if {$cur == {}} {
                break
            }
            if {$first == {}} {
                set first $cur
            }
            .result.text tag add search $cur "$cur + $length char"
            set new [.result.text index "$cur + $length char"]
            if {$found == 0} {
                if {$status == 0 && $history_spos != 0} {
                    if [.result.text compare $cur > $history_spos] {
                        set history_spos $new
                        set found 1
                    }
                } else {
                    set history_spos $new
                    set found 1
                }
            }
            set cur $new
        }
        if {$found == 0} {
            # no next search results
            if {$first != {}} {
                # jump to the first
                set history_spos $first
            } else {
                # no search result
                .statusBar.lab configure -foreground $param(errcolor) \
                    -text "[set s($lang)(notfound)]"
            }
        }
        debug 8 "Go to position $history_spos"
        .result.text see $history_spos
    }
    if {$isdict == 1} {
        # mark word in dict results - synonyms, antonyms etc.
        set cur 1.0
        set pattern {\{[^\}]*\}}
        while 1 {
            set cur [.result.text search -nocase -count length -regexp -- $pattern $cur end]
            if {$cur == ""} {
                break
            }
            set length [expr $length - 2]
            .result.text delete $cur
            .result.text delete "$cur + $length char"
            .result.text tag add matchfg $cur "$cur + $length char"
            set cur [.result.text index "$cur + $length char"]
        }
    }
    if {$history_pos($num) > 0} {
        #scroll
        .result.text yview moveto $history_pos($num)
    }
    .result.text configure -state disabled

    # raise is slowly with KDE/Gnome :-/
    # raise .

    debug 1 "display return"
}


#### main

# if query is set via command line - search it:
if {[string length $query] > 0} {
    dictsearch $query
}


proc save {what} {
    global inshistory history_result param gparam s

    debug 2 "save $what"
    set lang $gparam(lang)
    if { $inshistory <= 0 || [string length $history_result($inshistory)] <= 0} {
	tk_messageBox -icon info -type ok -parent . -message \
        	[set s($lang)(nosave)]
	return
    }
    set f [tk_getSaveFile]
    if {$f == ""} {
        return
    }
    set err [catch "set fd \[open $f w\]"]
    if $err {
	tk_messageBox -icon error -type ok -parent . -message \
        	"Couldn't open $f for writing!"
        return
    }
    if {$what == 0} {		# current result
        puts $fd $history_result($inshistory)
    } else {                    # all results in history
        for {set h $inshistory} {$h <= [array size history_result]} {incr h} {
            puts $fd $history_result($h)
        }
        for {set h 1} {$h <= $inshistory} {incr h} {
            puts $fd $history_result($h)
        }
    }
    catch {close $fd}
}

proc interface_reset {toggle_result} {
    global defaultcursor

    debug 2 "interface_reset"
    after cancel animlogo .search.l.logo
    .search.l.logo configure -image "logo1"
    . configure -cursor $defaultcursor
    .result.text configure -cursor $defaultcursor
    .search.s.entry configure -cursor $defaultcursor

    if {$toggle_result == 1} {
        hideResult -1
    }
}

# places a window on the desktop
proc placeWin {win diff} {
    debug 8 "placeWin $win $diff"

    scan [wm geometry .] "%dx%d+%d+%d" w h x y
    set x [expr $x + $diff]
    set y [expr $y + $diff]
    debug 8 "   -> +$x+$y"
    wm geometry $win +$x+$y
}

proc aboutBox {} {
    global param gparam pinfo s defaultcursor

    debug 2 "aboutBox"
    set lang $gparam(lang)
    # just in case ...
    interface_reset 0

    if ![winfo exists .about] {
        toplevel .about
        wm group .about .
        wm title .about "$pinfo(pname) - [set s($lang)(about)]"
        wm resizable .about 0 0
        placeWin .about 50

        text .about.text -font lfont -wrap word -width 60 -height 26 \
            -relief groove -padx 8 -pady 8
        button .about.ok -text "Ok" -width 8 -command {
            after cancel animlogo .about.text.logo
            destroy .about
        }
        pack .about.text -side top -expand 1 -fill x -fill y -ipady 10 -ipadx 10
        pack .about.ok -side bottom -pady 8
        label .about.text.logo -image logo1 -bd 1
        animlogo .about.text.logo
        after 1500 {
            after cancel animlogo .about.text.logo
            .about.text.logo configure -image "logo1"
        }

        .about.text tag configure center -justify center
        .about.text tag configure bfont -font bfont -justify center
        .about.text tag configure homepage -foreground $param(highcolor) -underline 1 \
                -font bfont
        .about.text tag bind homepage <ButtonRelease-1> { urlOpen $pinfo(homepage)  }
        .about.text tag bind homepage <Enter> { .about.text config -cursor hand2}
        .about.text tag bind homepage <Leave> { .about.text config -cursor xterm}

        .about.text insert end " " center
        # .about.text image create end -image logo1 -pady 10 -align center
        .about.text window create end -window .about.text.logo -pady 10 -align center
        .about.text insert end " \n$pinfo(pname)\nVersion $pinfo(version)\n\n" bfont
        .about.text insert end "$pinfo(copyright)\n\nOnline: " center
        .about.text insert end "$pinfo(homepage)" homepage

        if {$gparam(lang) == "de"} {
        .about.text insert end "\nKommentare sind sehr willkommen!\n" center

        .about.text insert end "
Dieses Programm ist freie Software. Sie können es unter \
den Bedingungen der GNU General Public License, wie von der \
Free Software Foundation herausgegeben, weitergeben und/oder \
modifizieren, entweder unter Version 2 der Lizenz oder (wenn \
Sie es wünschen) jeder späteren Version.

Die Veröffentlichung dieses Programms erfolgt in der \
Hoffnung, dass es Ihnen von Nutzen sein wird, aber OHNE JEDE \
GEWÄHRLEISTUNG - sogar ohne die implizite Gewährleistung \
der MARKTREIFE oder der EIGNUNG FÜR EINEN BESTIMMTEN ZWECK. \
Details finden Sie in der GNU General Public License.

Sie sollten eine Kopie der GNU General Public License zusammen \
mit diesem Programm erhalten haben. Falls nicht, schreiben Sie \
an die Free Software Foundation, Inc., 675 Mass Ave, Cambridge, \
MA 02139, USA.
"

        } else {
            .about.text insert end "\nComments are welcome!\n" center
            .about.text insert end "
This program is free software; you can redistribute it and/or modify it \
under the terms of the GNU General Public License as published by the \
Free Software Foundation; either version 2 of the License, or (at your \
option) any later version.

This program is distributed in the hope that it will be useful, \
but WITHOUT ANY WARRANTY; without even the implied warranty of \
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU \
General Public License for more details.

You should have received a copy of the GNU General Public License \
along with this program; if not, write to the Free Software \
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
"
        }
        .about.text configure -state disabled
    } else {
        wm deiconify .about
        raise .about 
    }
}

proc openHelpWindow {} {
    global param gparam s pinfo

    debug 2 "openHelpWindow"
    set lang $gparam(lang)
    if ![winfo exists .help] {
        toplevel .help
        wm group .help .
        wm title .help "$pinfo(pname) - [set s($lang)(help)]"
        # wm resizable .help 0 0
        placeWin .help 50

        text .help.text -font lfont -wrap word -width 80 -height 24 \
            -relief groove -yscrollcommand ".help.yscroll set" \
            -padx 4 -pady 4 -wrap none
        scrollbar .help.yscroll -orient vertical \
            -command { .help.text yview }         
        if {$param(isunix)} {          # I like smaller scrollbars
            .help.yscroll configure -width 10 -bd 1
        }
        button .help.ok -text "Ok" -width 8 -command {destroy .help}
        pack .help.ok -side bottom -pady 8 
        pack .help.yscroll -side right -fill y
        pack .help.text -expand 1 -fill both

        .help.text tag configure center -justify center
        .help.text tag configure big -font bfont -justify center
        .help.text tag configure bfont -font bfont
        .help.text tag configure maillink -foreground $param(highcolor) -underline 1
        .help.text tag bind maillink <ButtonRelease-1> { sendMail }
        .help.text tag bind maillink <Enter> { .help.text config -cursor hand2}
        .help.text tag bind maillink <Leave> { .help.text config -cursor xterm}

        .help.text tag configure keylink -foreground $param(highcolor) -underline 1
        .help.text tag bind keylink <ButtonRelease-1> { helpKeys }
        .help.text tag bind keylink <Enter> { .help.text config -cursor hand2}
        .help.text tag bind keylink <Leave> { .help.text config -cursor xterm}

        .help.text tag configure flaglink -foreground $param(highcolor) -underline 1
        .help.text tag bind flaglink <ButtonRelease-1> { helpCmdline }
        .help.text tag bind flaglink <Enter> { .help.text config -cursor hand2}
        .help.text tag bind flaglink <Leave> { .help.text config -cursor xterm}

        set w [expr round([winfo reqwidth .help.text] / 2) - 30]
        .help.text configure -tabs $w

    } else {
        wm deiconify .help
        # raise .help 
        .help.text configure -state normal
        .help.text delete 0.0 end
    }
}

proc helpGeneral {} {
    global s gparam pinfo

    debug 2 "helpGeneral"
    set lang $gparam(lang)
    openHelpWindow

    .help.text insert end "\n$pinfo(pname) - [set s($lang)(help)]\nVersion $pinfo(version)\n\n" big

    if {$lang == "de"} {
        .help.text insert end {Suche starten:} bfont
        .help.text insert end {
   * Suchwort eingeben, Suchmethode auswählen, ENTER oder auf "Suche" drücken, oder
   * Doppelklick mit linker Maustaste auf Wort in Ergebnisfenster, oder
   * in anderem Fenser selektiertes Wort mit mittlerer Maustaste über 
     Ergebnisfenster "fallenlassen"
   * Suche im Ergebnis: Suchwort eingeben, Umschalt+ENTER oder
     mit mittlerer Maustaste auf "Suche" drücken
}
        .help.text insert end {
Suchworte angeben: } bfont
        .help.text insert end {
   * ein Suchwort oder
   * eins aus mehreren Wörtern (ODER-Verknüpfung): }
        .help.text insert end {Wort1,Wort2} bfont
        .help.text insert end {
   * alle Wörter (UND-Verknüpfung): }
        .help.text insert end {Wort1+Wort2} bfont
        .help.text insert end {
   * exakte Wortfolge: }
        .help.text insert end {Wort1 Wort2} bfont
        .help.text insert end {
   * }
        .help.text insert end {Umlaute: } bfont
        .help.text insert end {Falls Sie keine Umlaute auf der Tastatur haben, benutzen Sie:
        }
        .help.text insert end {"a = ä  "A = Ä  "o = ö  "O = Ö  "u = ü "U = Ü  "s = ß} bfont

        .help.text insert end {

Suchparameter: } bfont
        .help.text insert end {
   * Suche nach vollständigen Wörtern oder nach Muster in Wörtern?
   * Groß-/Kleinschreibweise ignorieren oder exakt beachten?
   * Korrekte Schreibweise oder, falls agrep benutzt wird, Fehlerkorrektur 
     versuchen?
   * Einfache Suche (* als Platzhalter für beliebige Zeichen) oder 
     reguläre Ausdrücke zulassen?
}

        .help.text insert end {
Andere Suchverhalten } bfont
        .help.text insert end { sind über das Menü einstellbar: }
        .help.text insert end {
   Suche, sobald Maus über Fenster: } bfont
        .help.text insert end {
     * Selektieren Sie in einem anderen Fenster ein Wort (linke Maustaste)
     * Sobald Sie mit der Maus über das Ding-Fenster kommen, wird dieses 
       Wort mit aktueller Suchmethode gesucht.
        }
        .help.text insert end {
   Suche sofort bei neuer Textauswahl: } bfont
        .help.text insert end {
     * Sobald Sie in einem anderen Fenster ein Wort selektieren 
       (linke Maustaste), wird dieses Wort mit aktueller Suchmethode gesucht.
        } 

        .help.text insert end {
History-Funktion: } bfont
        .help.text insert end {
   * Frühere Suchergebnisse lassen sich wieder anzeigen (Knöpfe "<" bzw.  ">")
}
        .help.text insert end {
Zur Funktionsweise: } bfont
        .help.text insert end "
   * \"$pinfo(pname)\" ist kein intelligentes Dolmetscherprogramm, 
     sondern letztlich nur ein Front-End zur Suche in Dateien.
   * Die eigentliche Suche führt ein dafür existierendes Unix-Kommando aus der 
     \"grep\"-Famile durch. Hat man \"agrep\" installiert, kann man die Funktion 
     der \"fehlertoleranten Suche\" nutzen.
   * Die Ergebnisse sind nur so gut, wie die zugrundeliegende Wörterbuch-Datei.
     Zur Verbesserung dieser Datenbasis können Sie beitragen. 
     Senden Sie einfach "
        .help.text insert end {eine E-Mail an den Autor.} maillink

        .help.text insert end "\n\nWeitere Informationen:\n" bfont
        .help.text insert end "   * "
        .help.text insert end "Hilfe zu Tastaturkürzeln" keylink
        .help.text insert end "\n   * "
        .help.text insert end "Hilfe zu Startoptionen" flaglink
        .help.text insert end "
   * Unix-Manuals zu agrep, egrep, ispell, dict\n\n"

    } else {                      # english
        .help.text insert end {Start search:} bfont
        .help.text insert end {
   * Type in search word, select a search method,
     then press ENTER or press the "Search" button, or
   * Double click on a word in the result window with left mouse button 1, or
   * Select a word in another X window, drop it by clicking with middle mouse
     button 2 over the result window.
   * Search in result: Type in search word, then press Shift+ENTER or
     press the "Search" button with mouse middle button 2
}
        .help.text insert end {
Specify your search words: } bfont
        .help.text insert end {
   * Just one word, or
   * Find one of many words: }
        .help.text insert end {word1,word2} bfont
        .help.text insert end {
   * Find all words: }
        .help.text insert end {word1+word2} bfont
        .help.text insert end {
   * Find exact phrase: }
        .help.text insert end {word1 word2} bfont
        .help.text insert end {
   * }
        .help.text insert end {Umlauts: } bfont
        .help.text insert end {If you don't have umlaut keys on your keyboard, please use:
        }
        .help.text insert end {"a = ä  "A = Ä  "o = ö  "O = Ö  "u = ü  "U = Ü  "s = ß} bfont

        .help.text insert end {

Search options: } bfont
        .help.text insert end {
   * Search for full words or partial matches within words?
   * Ignore case or search case sensitive?
   * If agrep is used, try error correction if nothing is found?
   * Simple search (* is wildcard for any character) or search with
     regular expressions?
        }

        .help.text insert end {
Another search behavior } bfont
        .help.text insert end { can be selected via the menu: }
        .help.text insert end {
   Search on mouse over: } bfont
        .help.text insert end {
     * Select a word in another window (left mouse button)
     * As soon as you move the mouse over the Ding window
       this word will be searched with the current search method.
        }
        .help.text insert end {
   Search on new text selection: } bfont
        .help.text insert end {
     * As soon as you select a word in another window (left mouse button)
       this word will be searched with the current search method.
        } 

        .help.text insert end {
History function: } bfont
        .help.text insert end {
   * Previous search results could be displayed again ("<" and ">" buttons)
        }
        .help.text insert end {
How it works: } bfont
        .help.text insert end "
   * \"$pinfo(pname)\" is not an intelligent translator system, but \"only\"
     a front-end to search quickly in files.
   * The search itself is done by a dedicated Unix command  ala \"grep\".
     If you have installed \"agrep\" you are able to use the approximate
     matching feature to correct spelling errors.
   * Well, the results are as good as the dictionary wordlist. 
     You could contribute to improve this list!
     Simply send "
        .help.text insert end {an email to the author.} maillink

        .help.text insert end "\n\nFurther information:\n" bfont
        .help.text insert end "   * "
        .help.text insert end "Help on Keyboard shortcuts" keylink
        .help.text insert end "\n   * "
        .help.text insert end "Help on start options" flaglink
        .help.text insert end "
   * Unix manual pages for agrep, egrep, ispell, dict \n\n"
    }
    .help.text configure -state disabled
}


proc helpKeys {} {
    global s gparam pinfo

    debug 2 "helpKeys"
    set lang $gparam(lang)
    openHelpWindow
    .help.text insert end "\n$pinfo(pname) - [set s($lang)(help)]\nVersion $pinfo(version)\n\n" big

    if {$lang == "de"} {
        .help.text insert end {Viele Funktionen lassen sich einfach und schnell über }
        .help.text insert end {Tastaturkürzel} bfont
        .help.text insert end " erreichen.\n\n"
        .help.text insert end {Im Suchwort ändern:} bfont
        .help.text insert end "
   Kursur links (rechts)\tÄndern im Suchwort
   Umschalt + Zurück\tSuchwort löschen
   Strg+a\tan den Anfang des Suchwortes
   Strg+e\tans Ende des Suchwortes
   Strg+k\tLöschen von aktueller Position bis Ende\n\n"
        .help.text insert end {Suchmethode ändern:} bfont
        .help.text insert end "
   Strg + 1\tSuche mit Methode 1, 2 usw.
   Strg + 2 ...\t ... je nachdem, wieviele definiert\n\n"
        .help.text insert end {Im Ergebnis blättern:} bfont
        .help.text insert end "
   Kursur hoch (runter) / Rollmaus\t1 Zeile hoch (runter)
   Umschalt + Kursur hoch (runter) / Rollmaus\t5 Zeilen hoch (runter)
   Bild hoch (runter) / Alt + Rollmaus\t1 Seite hoch (runter)
   Pos 1\tan den Anfang
   Ende\tans Ende
   Umschalt + Kursur links (rechts)\tnach links (rechts) verschieben (bei langen Zeilen)

   Strg + Kursur hoch (runter)\tin früheren Suchergebnissen
   Strg + Rad der Rollmaus\tblättern (History)
\n"

        .help.text insert end {Menü-Funktionen:} bfont
        .help.text insert end "
   Strg + q\tProgramm beenden
   Strg + n\tneues Programmfenster starten
   Strg + s\tAktuelles Ergebnis speichern
   Strg + l\tAlle Ergebnisse speichern
   Strg + m\tE-Mail an den Autor senden (nur Unix)\n\n"

        .help.text insert end {Sonstiges:} bfont
        .help.text insert end " 
   F1\tHilfe
   Strg + +\tSchrift vergrößern
   Strg + -\tSchrift verkleinern
   Strg + =\tStandard-Schriftgröße
   Esc\tevtl. Suche stoppen, Oberfläche rücksetzen
      \tFenster minimieren oder normale Größe\n\n"

    } else {                      # english
        .help.text insert end {Many functions are easily available by }
        .help.text insert end {Keyboard shortcuts} bfont
        .help.text insert end ".\n\n"
        .help.text insert end {To change the search word:} bfont
        .help.text insert end "
   Cursor left (right)\tEdit in the current word
   Shift + Backspace\tErase search word
   Ctrl+a\tGo to the begin of the search word
   Ctrl+e\tGo to end of the search word
   Ctrl+k\tErase from current position to the end\n\n"
        .help.text insert end {To change the search method:} bfont
        .help.text insert end "
   Ctrl + 1\tSearch with method 1, 2 etc..
   Ctrl + 2 ...\t ... if defined\n\n"
        .help.text insert end {Scroll in the results:} bfont
        .help.text insert end "
   Cursor up (down) / wheel mouse\t1 line up (down)
   Shift + Cursor up (down) / wheel mouse\t5 lines up (down)
   Page up (down) / Alt + wheel mouse\t1 page up (down)
   Pos 1\tto the beginning
   End\tto the end
   Shift + Cursor left (right)\tscroll left (right) (for long lines)

   Ctrl + Cursur up (down)\tbrowse through previous
   Ctrl + wheel of mouse\tsearch results (History)\n\n"

        .help.text insert end {Menu functions:} bfont
        .help.text insert end "
   Ctrl + q\tQuit program
   Strg + n\tStart new program window
   Ctrl + s\tSave current result
   Ctrl + l\tSave all results
   Ctrl + m\tSend an e-mail to the author (Unix only)\n\n"
        .help.text insert end {Others:} bfont
        .help.text insert end " 
   F1\tHelp
   Strg + +\tIncrease font size
   Strg + -\tDecrease font size
   Strg + =\tStandard font size
   Esc\tStop search, Reset interface
       \tminimize window or set normal size\n\n"
        .help.text insert end {Umlauts: } bfont
        .help.text insert end {If you don't have umlaut keys on your keyboard, please use:
        }
        .help.text insert end {"a = ä  "A = Ä  "o = ö  "O = Ö  "u = ü  "U = Ü  "s = ß
        
        } bfont

    }
    .help.text configure -state disabled
}

proc helpCmdline {} {
    global s gparam pinfo argv0 ding_usage

    debug 2 "helpCmdline"
    set lang $gparam(lang)
    openHelpWindow
    .help.text insert end "\n$pinfo(pname) - [set s($lang)(help)]\nVersion $pinfo(version)\n\n" big

    if {$lang == "de"} {
        .help.text insert end "Das Programm $argv0 kann mit verschiedenen Optionen (Flags) gestartet werden:\n"
        .help.text insert end "$ding_usage(de)"
    } else {                      # english
        .help.text insert end "The program $argv0 may be started with different options (command line flags):\n"
        .help.text insert end "$ding_usage(en)"
    }
    .help.text configure -state disabled
}

# updates the list of search methods in configuration
proc update_searchmeth {num} {
    global searchmeth searchmpos

    debug 2 "update_searchmeth $num"
    set sel [.ssearch.m.list curselection]
    set size [.ssearch.m.list size]
    .ssearch.m.list delete 0 $size
    foreach i $searchmpos {
        if {$searchmeth($i,name) != ""} {
            .ssearch.m.list insert end $searchmeth($i,name)
        }
    }
    if {$sel != ""} {
        set s [expr $sel + $num]
        if {($s <= $size && $s >= 0)} {
            .ssearch.m.list selection set $s
        }
    }
    # update
}

proc setSearch {} {
    global s pinfo default_searchmeth searchmeth searchmpos gparam param grepcmds

    debug 2 "setSearch"
    set lang $gparam(lang)
    if ![winfo exists .ssearch] {
        toplevel .ssearch
        wm group .ssearch .
        wm title .ssearch "$pinfo(pname): [set s($lang)(searchm)]"
        wm iconname .ssearch "dict"
        placeWin .ssearch 50

        frame .ssearch.buttons
        pack .ssearch.buttons -side bottom -fill x
        button .ssearch.buttons.ok -text [set s($lang)(apply)] -command {
            update_searchmeth_menu
            if {$gparam(autosave) != 0} {
                saveOptions
            }
            destroy .ssearch
        }
        button .ssearch.buttons.cancel -text [set s($lang)(cancel)] -command "destroy .ssearch"
        pack .ssearch.buttons.ok .ssearch.buttons.cancel \
            -side left -expand 1 -pady 8 -padx 8

        # Methods
        frame .ssearch.m -bd 2 -relief groove
        pack .ssearch.m -side top -fill x -ipady 10 -ipady 10
        scrollbar .ssearch.m.scroll -command ".ssearch.m.list yview"
        if {$param(isunix)} {          # I like smaller scrollbars
            .ssearch.m.scroll configure -width 10 -bd 1
        }
        listbox .ssearch.m.list -yscroll ".ssearch.m.scroll set" \
            -setgrid 1 -height 4
        menubutton .ssearch.m.new -text [set s($lang)(new)] -width 8 \
            -menu .ssearch.m.new.menu -indicatoron 1 -relief raised \
            -anchor c -direction flush
        
        menu .ssearch.m.new.menu -font lfont -tearoff 0
        for {set j 0} {[info exists default_searchmeth($j,name)]} {incr j} {
            # check for available search cmds
            set state "disabled"
            foreach c $default_searchmeth($j,grepcmds) {
                if [info exists grepcmds($c)] {
                    set state "active"
                    break
                }
            }
            .ssearch.m.new.menu add command -label "$default_searchmeth($j,name)" \
                -state $state -command "setSearchOptions {} $j"
        }
        button .ssearch.m.edit -text [set s($lang)(edit)] -width 8 \
            -state disabled -command {
            if {[.ssearch.m.list curselection] != {}} {
                setSearchOptions [lindex $searchmpos [.ssearch.m.list curselection]] {}
            }
        }
        button .ssearch.m.delete -text [set s($lang)(delete)] -width 8 \
            -state disabled -command {
            if {[.ssearch.m.list curselection] != {}} {
                set sel [.ssearch.m.list curselection]
                set searchmeth([lindex $searchmpos $sel],name) ""
                set searchmpos [lreplace $searchmpos $sel $sel]
                update_searchmeth 0
            }
        }

        button .ssearch.m.up -image up -state disabled -command {
            if {[.ssearch.m.list curselection] != {}} {
                set sel [.ssearch.m.list curselection]
                if {$sel > 0} {
                    set pos [lindex $searchmpos $sel]
                    set searchmpos [lreplace $searchmpos $sel $sel]
                    set searchmpos [linsert $searchmpos [expr $sel - 1] $pos]
                    update_searchmeth -1
                }
            }
        }
        button .ssearch.m.down -image down -state disabled -command {
            if {[.ssearch.m.list curselection] != {}} {
                set sel [.ssearch.m.list curselection]
                if {$sel < [expr [.ssearch.m.list size] - 1]} {
                    set pos [lindex $searchmpos $sel]
                    set searchmpos [lreplace $searchmpos $sel $sel]
                    set searchmpos [linsert $searchmpos [expr $sel + 1] $pos]
                    update_searchmeth 1
                }
            }
        }
        grid .ssearch.m.list -in .ssearch.m -row 0 -column 0 -rowspan 5 \
            -sticky w,n,s -pady 4
        grid .ssearch.m.scroll -in .ssearch.m -row 0 -column 1 -rowspan 5 \
            -sticky w,n,s -pady 4
        grid .ssearch.m.up -in .ssearch.m -row 1 -column 2 -rowspan 1 \
             -padx 4 -sticky n
        grid .ssearch.m.down -in .ssearch.m -row 1 -column 2 -rowspan 1 \
             -padx 4 -sticky s
        grid .ssearch.m.new -in .ssearch.m -row 0 -column 3 -rowspan 1 \
             -padx 8 -pady 4
        grid .ssearch.m.edit -in .ssearch.m -row 1 -column 3 -rowspan 1 \
             -padx 8 -pady 4
        grid .ssearch.m.delete -in .ssearch.m -row 2 -column 3 -rowspan 1 \
             -padx 8 -pady 4

        # grid .ssearch.m.down -in .ssearch.m -row 1 -column 2 -rowspan 1 \
        #      -padx 8 -pady 4

        bind .ssearch.m.list <Double-1> {
            setSearchOptions [lindex $searchmpos [.ssearch.m.list curselection]] {}
        }
        bind .ssearch.m.list <Return> {
            setSearchOptions [lindex $searchmpos [.ssearch.m.list curselection]] {}
        }
        bind .ssearch.m.list <1> {
            .ssearch.m.up configure -state normal
            .ssearch.m.down configure -state normal
            .ssearch.m.edit configure -state normal
            .ssearch.m.delete configure -state normal
        }
        update_searchmeth 0

    } else {
        wm deiconify .ssearch
        raise .ssearch
    }
}

proc setSearchOptions {num type} {
    global searchmeth default_searchmeth default_searchopts searchmpos grepcmds
    global param gparam pinfo s

    debug 1 "setSearchOptions $num $type"
    set lang $gparam(lang)
    variable smeth
    if [array exists smeth] {
        unset smeth
    }
    variable new 0
    variable n $num
    if {$num != ""} {
        foreach i [array names searchmeth "$num,*"] {
            regsub "$num," $i "" j
            set smeth($j) $searchmeth($i)
        }
    } else {
        set n 0
        set new 1
        array set smeth [array get default_searchopts]
        foreach i [array names default_searchmeth "$type,*"] {
            regsub "$type," $i "" j
            set smeth($j) $default_searchmeth($i)
        }
        # set smeth(grepopts) $grepcmds($smeth(grepcmd))
        # set smeth(name) ""
        foreach i [array names searchmeth "*,name"] {
            incr n
        }
        set smeth(type) $type
    }
    if [winfo exists .sopts] {
        wm deiconify .sopts
        raise .sopts
    } else {
        toplevel .sopts
        wm group .sopts .
        wm title .sopts "$pinfo(pname): [set s($lang)(searcho)]"
        wm iconname .sopts "dict"
        placeWin .sopts 70

        frame .sopts.buttons
        pack .sopts.buttons -side bottom -fill x
        button .sopts.buttons.ok -text [set s($lang)(apply)] -command {
            if {$smeth(name) == ""} {
                tk_messageBox -icon info -type ok -parent .sopts \
                    -message [set s($lang)(namerequired)]
                focus .sopts.c.n
                return
            }
            set searchmeth($n,name)      $smeth(name)
            set searchmeth($n,type)      $smeth(type)
            set searchmeth($n,grepcmd)   $smeth(grepcmd)
            set searchmeth($n,grepopts)  $smeth(grepopts)
            set searchmeth($n,dictfile)  $smeth(dictfile)
            set searchmeth($n,dictfiles) [glob -nocomplain $smeth(dictfile)]
            set searchmeth($n,separator) $smeth(separator)
            set searchmeth($n,language1) $smeth(language1)
            set searchmeth($n,language2) $smeth(language2)
            set searchmeth($n,minlength) $smeth(minlength)
            set searchmeth($n,maxlength) $smeth(maxlength)
            set searchmeth($n,maxresults) $smeth(maxresults)
            set searchmeth($n,shapedresult) $smeth(shapedresult)
            set searchmeth($n,avail) 1
            if {$new == 1} {           # new
                lappend searchmpos $n
            }
            update_searchmeth 0
            unset smeth
            destroy .sopts
        }
        button .sopts.buttons.st -text [set s($lang)(default)] -command {
            if {$new == 1} {           # new
                array set smeth [array get default_searchopts]
            } else {
                foreach i [array names searchmeth "$n,*"] {
                    regsub "$n," $i "" j
                    set smeth($j) $searchmeth($i)
                }
            }
        }
        button .sopts.buttons.cancel -text [set s($lang)(cancel)] \
            -command {
                unset smeth
                destroy .sopts
            }
        pack .sopts.buttons.ok .sopts.buttons.st .sopts.buttons.cancel \
            -side left -expand 1 -pady 8

        # Command
        frame .sopts.c -bd 2 -relief groove
        pack .sopts.c -side top -fill x -ipady 10 -ipady 10
        label .sopts.c.ln -text "[set s($lang)(name)]:" -font bfont
        entry .sopts.c.n -textvariable smeth(name) -font bfont -width 15
        label .sopts.c.l -text "[set s($lang)(grepcmd)]:" -font lfont

        menubutton .sopts.c.m -textvariable smeth(grepcmd) -menu .sopts.c.m.m \
            -indicatoron 1 -relief raised -anchor c -direction flush \
            -font lfont -width 13
        menu .sopts.c.m.m -font lfont -tearoff 0

        foreach c $default_searchmeth($smeth(type),grepcmds) {
            # all defined commands for this method
            if {![info exists grepcmds($c)]} {
                 # not available in current system
                 continue
            }
            if {$smeth(grepcmd) == ""} {    # no default command yet
                set smeth(grepcmd) $c
                if ![info exists smeth(grepopts)] {
                    # don't override specific options
                    set smeth(grepopts) "$grepcmds($c)"
                }
            }
            if {$new == 1 || [string compare $c $searchmeth($n,grepcmd)] != 0} {
                # no options set so far
                if {[info exists default_searchmeth($smeth(type),grepopts)]} {
                        # special options for this method
                    set cmd "set smeth(grepopts) \"$default_searchmeth($smeth(type),grepopts)\""
                } else {
                    # default options for this command
                    set cmd "set smeth(grepopts) \"$grepcmds($c)\""
                }
            } else {
                    # set default options
                set cmd "set smeth(grepopts) \"$searchmeth($n,grepopts)\""
            }
            set cmd "$cmd; set smeth(grepcmd) $c"

            .sopts.c.m.m add command -label $c -font lfont -command $cmd
        }
        label .sopts.c.l2 -text "[set s($lang)(grepopts)]:" -font lfont
        entry .sopts.c.o -textvariable smeth(grepopts) -font lfont -width 13
        grid .sopts.c.ln -in .sopts.c -row 0 -column 0 -rowspan 1 -sticky e \
            -padx 4 -pady 4
        grid .sopts.c.n -in .sopts.c -row 0 -column 1 -rowspan 1 -sticky w \
            -padx 4 -pady 4
        grid .sopts.c.l -in .sopts.c -row 1 -column 0 -rowspan 1 -sticky e \
            -padx 4 -pady 4
        grid .sopts.c.m -in .sopts.c -row 1 -column 1 -rowspan 1 -sticky w \
            -padx 4 -pady 4
        grid .sopts.c.l2 -in .sopts.c -row 2 -column 0 -rowspan 1 -sticky e \
            -padx 4 -pady 4
        grid .sopts.c.o -in .sopts.c -row 2 -column 1 -rowspan 1 -sticky w \
            -padx 4 -pady 4

        # File
        frame .sopts.file -bd 2 -relief groove 
        pack .sopts.file -side top -fill x -ipadx 10 -ipady 10 -pady 10

        label .sopts.file.l -text "[set s($lang)(dictfile)]: " \
            -font lfont
        entry .sopts.file.e -width 36 -font lfont \
            -textvariable smeth(dictfile)
        variable types {
            {"Text files" {.txt .vok .dic}}
            {"All files" *}
        }

        button .sopts.file.b -font lfont -text "Browse ..." \
            -command {
                set f [tk_getOpenFile -initialdir [file dirname $smeth(dictfile)] \
                    -filetypes $types -parent .sopts]
                if [string compare $f ""] {
                    set smeth(dictfile) $f
                }
            }
        grid .sopts.file.l -in .sopts.file -row 0 -column 0 -columnspan 6
        grid .sopts.file.e -in .sopts.file -row 1 -column 0 -columnspan 5 -sticky e
        grid .sopts.file.b -in .sopts.file -row 1 -column 6 

        label .sopts.file.l1 -text "[set s($lang)(lang)] 1:" -font lfont
        label .sopts.file.lt -text "[set s($lang)(sep)]:" -font lfont
        label .sopts.file.l2 -text "[set s($lang)(lang)] 2:" -font lfont
        entry .sopts.file.e1 -font lfont -textvariable smeth(language1) -width 10
        entry .sopts.file.et -font lfont -textvariable smeth(separator) -width 5
        entry .sopts.file.e2 -font lfont -textvariable smeth(language2) -width 10


        grid .sopts.file.l1 -in .sopts.file -row 2 -column 0 -padx 4 -columnspan 2
        grid .sopts.file.lt -in .sopts.file -row 2 -column 2 -padx 4 -columnspan 2
        grid .sopts.file.l2 -in .sopts.file -row 2 -column 4 -padx 4 -columnspan 2
        grid .sopts.file.e1 -in .sopts.file -row 3 -column 0 -padx 4 -columnspan 2
        grid .sopts.file.et -in .sopts.file -row 3 -column 2 -padx 4 -columnspan 2
        grid .sopts.file.e2 -in .sopts.file -row 3 -column 4 -padx 4 -columnspan 2
        

        frame .sopts.search -bd 2 -relief groove
        pack .sopts.search -side top -fill x -ipadx 10 -ipady 10
        label .sopts.search.rl -text "[set s($lang)(maxresults)]:" -font lfont
        entry .sopts.search.re -justify right -width 5 -font lfont -textvariable smeth(maxresults)
        label .sopts.search.ll -text "[set s($lang)(minlength)]:" -font lfont
        entry .sopts.search.le -justify right -width 5 -font lfont -textvariable smeth(minlength)
        label .sopts.search.ml -text "[set s($lang)(maxlength)]:" -font lfont
        entry .sopts.search.me -justify right -width 5 -font lfont -textvariable smeth(maxlength)
        checkbutton .sopts.search.shaped -text [set s($lang)(shaped)] -font lfont \
            -variable smeth(shapedresult)

        grid .sopts.search.rl -in .sopts.search -row 0 -column 0 -rowspan 1 -sticky e -padx 0
        grid .sopts.search.re -in .sopts.search -row 0 -column 1 -rowspan 1 -sticky w -padx 0
        grid .sopts.search.ll -in .sopts.search -row 1 -column 0 -rowspan 1 -sticky e -padx 0
        grid .sopts.search.le -in .sopts.search -row 1 -column 1 -rowspan 1 -sticky w -padx 0
        grid .sopts.search.ml -in .sopts.search -row 2 -column 0 -rowspan 1 -sticky e -padx 0
        grid .sopts.search.me -in .sopts.search -row 2 -column 1 -rowspan 1 -sticky w -padx 0
        grid .sopts.search.shaped -in .sopts.search -row 3 -column 0 -rowspan 1 -sticky e -padx 0
    }

    if {! [info exists default_searchmeth($smeth(type),dictfile)]} {
        .sopts.file.e configure -state disabled
        .sopts.file.b configure -state disabled
        .sopts.file.l configure -foreground darkgray
    }    
    if {! [info exists default_searchmeth($smeth(type),separator)]} {
        .sopts.file.et configure -state disabled
        .sopts.file.lt configure -foreground darkgray
    }    
    if {! [info exists default_searchmeth($smeth(type),language2)]} {
        .sopts.file.e2 configure -state disabled
        .sopts.file.l2 configure -foreground darkgray
    }    
}

proc setGeneral {} {
    global default
    global pinfo s languages
    global param gparam

    debug 1 "setGeneral"
    set lang $gparam(lang)
    if ![winfo exists .general] {
        toplevel .general
        wm group .general .
        wm title .general "$pinfo(pname): [set s($lang)(general)]"
        wm iconname .general "ding"
        placeWin .general 50

        variable fc "$param(fcolor)"
        variable bc "$param(bcolor)"
        variable ll $lang
        variable changes_restart 0
        variable ffamily
        variable fsize
        variable fstyle
        variable lffamily
        variable lfsize
        variable lfstyle
        set ffamily [lindex $param(tfont) 0]
        set fsize [lindex $param(tfont) 1]
        set fstyle [lindex $param(tfont) 2]
        set lffamily [lindex $param(lfont) 0]
        set lfsize [lindex $param(lfont) 1]
        set lfstyle [lindex $param(lfont) 2]

        variable omaxhistory $param(maxhistory)

        frame .general.buttons
        pack .general.buttons -side bottom -fill x

        button .general.buttons.ok -text [set s($lang)(apply)] -command {
            set gparam(lang) $ll
            set tf [list $ffamily $fsize $fstyle]
            if [string compare "$tf" "$param(tfont)"] {
                set param(tfont) $tf
                chFont tfont
                # .result.text configure -font $param(tfont)
                # .search.s.entry configure -font $param(tfont)
            }
            set lf [list $lffamily $lfsize $lfstyle]
            if [string compare "$lf" "$param(lfont)"] {
                set param(lfont) $lf
                set param(bfont)  [list $lffamily $lfsize bold $lfstyle]
                set param(sfont) [list $lffamily [expr $lfsize - 2] $lfstyle]
                chFont lfont
                chFont bfont
                chFont sfont
            }
            if {[string compare $param(fcolor) $fc] || [string compare $param(bcolor) $bc]} {
                # color change
                set param(fcolor) "$fc"
                set param(bcolor) "$bc"
                tk_setPalette foreground "$fc" background "$bc" 

                left configure -foreground $param(fcolor)
                right configure -foreground $param(fcolor)
                up configure -foreground $param(fcolor)
                down configure -foreground $param(fcolor)

                if {[winfo depth .] > 1} {      # Color display
                    # other background color for result text
                    set cols [shadeColor $param(fcolor) $param(bcolor)]
                    set param(shadedcolor) [lindex $cols 0]
                    set param(highcolor) [lindex $cols 1]
                    set param(errcolor) [lindex $cols 2]
                    .result.text tag configure bg1 -background $param(shadedcolor)
                    .result.text tag configure matchfg -foreground $param(highcolor)
                    .result.text tag configure search -background $param(highcolor) -foreground $param(bcolor)
                    .result.text tag configure u -background $param(fcolor) \
                        -foreground $param(bcolor)
                    setSearchBg $param(shadedcolor)
                }
                if {($curhistory <= 0) || ([string length $query] == 0 && ! $mini)} {
                    welcome 1
                }
            }
            if {$changes_restart == 1} {
                tk_messageBox -icon info -type ok -parent .general -message \
                    [set s($lang)(change)]
            }
            if {$gparam(autosave) != 0} {
                saveOptions
            }
            destroy .general
        }
        button .general.buttons.st -text [set s($lang)(default)] -command {
            set lang $default(language)
            set llabel $languages($lang)
            set fc $default(fcolor)
            set bc $default(bcolor)
            .general.c.fl configure -background $fc
            .general.c.bl configure -background $bc
            set ffamily [lindex $default(tfont) 0]
            set fsize [lindex $default(tfont) 1]
            set lffamily [lindex $default(lfont) 0]
            set lfsize [lindex $default(lfont) 1]
            set gparam(balloonDelay) $default(balloonDelay)
            set gparam(showBalloons) $default(showBalloons)
            set param(maxhistory)   $default(maxhistory)
            set gparam(autosave) $default(autosave)
        }
        button .general.buttons.cancel -text [set s($lang)(cancel)] \
            -command "destroy .general"
        pack .general.buttons.ok .general.buttons.st .general.buttons.cancel \
            -side left -expand 1 -pady 8 -padx 8

        # language
        frame .general.l -bd 2 -relief groove
        pack .general.l -side top -fill x -ipady 10 -ipady 10
        label .general.l.l -text "[set s($lang)(lang)]:" -font lfont
        variable llabel
        set llabel $languages($lang)
        menubutton .general.l.m -textvariable llabel -menu .general.l.m.m \
            -indicatoron 1 -relief raised -anchor c -direction flush \
            -font lfont -width 13
        menu .general.l.m.m -font lfont -tearoff 0
        foreach i [array names languages] {
            .general.l.m.m add command -label $languages($i) -font lfont \
                -command "set ll $i; set llabel $languages($i)"
        }
        grid .general.l.l -in .general.l -row 0 -column 0 -rowspan 1 -sticky e \
            -padx 4 -pady 4
        grid .general.l.m -in .general.l -row 0 -column 1 -rowspan 1 -sticky w \
            -padx 4 -pady 4

        # colors
        frame .general.c -bd 2 -relief groove
        pack .general.c -side top -fill x -ipady 10 -ipady 10  -pady 10

        button .general.c.fb -text [set s($lang)(fg)] -width 18 -font lfont \
            -command {
                set fc [selectColor $fc fg]
                if [string compare $fc ""] {
                    .general.c.fl configure -background $fc
                }
            }
        button .general.c.bb -text [set s($lang)(bg)] -width 18 -font lfont \
            -command {
                set bc [selectColor $bc bg]
                if [string compare $bc ""] {
                    .general.c.bl configure -background $bc
                }
            }
        label .general.c.fl -background $fc -width 13 -relief groove
        label .general.c.bl -background $bc -width 13 -relief groove
        grid .general.c.fb -in .general.c -row 0 -column 0 -sticky e \
            -padx 4 -pady 4
        grid .general.c.fl -in .general.c -row 0 -column 1 -sticky w \
            -padx 4 -pady 4
        grid .general.c.bb -in .general.c -row 1 -column 0 -sticky e \
            -padx 4 -pady 4
        grid .general.c.bl -in .general.c -row 1 -column 1 -sticky w \
            -padx 4 -pady 4

        # Fonts
        # frame .general.f -bd 2 -relief groove
        pack .general.c -side top -fill x -ipady 10 -ipady 10 -pady 10
        label .general.c.l -text "[set s($lang)(tfont)]: " -font lfont
        label .general.c.l2 -text "[set s($lang)(lfont)]: " -font lfont

        menubutton .general.c.f -textvariable ffamily -menu .general.c.f.m \
            -indicatoron 1 -relief raised -anchor c -direction flush \
            -font lfont -width 13
        menu .general.c.f.m -font lfont -tearoff 0
        menubutton .general.c.f2 -textvariable lffamily -menu .general.c.f2.m \
            -indicatoron 1 -relief raised -anchor c -direction flush \
            -font lfont -width 13
        menu .general.c.f2.m -font lfont -tearoff 0

        set allfams [font families]
        if {[llength $param(tfonts)] > 0} {
            foreach i $param(tfonts) {
                if {[lsearch $allfams $i] != -1} {
                # if {[lsearch $allfams [string tolower $i]] != -1} 
                    .general.c.f.m add command -label $i -font lfont \
                        -command "set ffamily {$i}"
                    .general.c.f2.m add command -label $i -font lfont \
                        -command "set lffamily {$i}"
                }
            }
        } else {        # use all available fonts
            foreach i [lsort $allfams] {
                .general.c.f.m add command -label $i -font lfont \
                    -command "set ffamily {$i}"
                .general.c.f2.m add command -label $i -font lfont \
                    -command "set lffamily {$i}"
            }
        }
        menubutton .general.c.s -textvariable fsize -menu .general.c.s.m \
            -indicatoron 1 -relief raised -anchor c -direction flush \
            -font lfont -width 4
        menu .general.c.s.m -font lfont -tearoff 0
        menubutton .general.c.s2 -textvariable lfsize -menu .general.c.s2.m \
            -indicatoron 1 -relief raised -anchor c -direction flush \
            -font lfont -width 4
        menu .general.c.s2.m -font lfont -tearoff 0
        foreach i {7 8 10 12 14 15 16 18 24} {
            .general.c.s.m add command -label "$i" -font lfont \
                -command "set fsize \"$i\""
            .general.c.s2.m add command -label "$i" -font lfont \
                -command "set lfsize \"$i\""
        }

        grid .general.c.l -in .general.c -row 2 -column 0 -sticky e \
            -padx 4 -pady 4
        grid .general.c.f -in .general.c -row 2 -column 1 -sticky w \
            -padx 4 -pady 4
        grid .general.c.s -in .general.c -row 2 -column 2 -sticky w \
            -padx 4 -pady 4
        grid .general.c.l2 -in .general.c -row 3 -column 0 -sticky e \
            -padx 4 -pady 4
        grid .general.c.f2 -in .general.c -row 3 -column 1 -sticky w \
            -padx 4 -pady 4
        grid .general.c.s2 -in .general.c -row 3 -column 2 -sticky w \
            -padx 4 -pady 4

        # History, Parameters menue, Balloon help, autosave
        frame .general.b -bd 2 -relief groove
        pack .general.b -side top -fill x -ipady 10 -ipady 10 
        label .general.b.hl -text "[set s($lang)(maxhistory)]:" -font lfont
        entry .general.b.he -justify right -width 5 -font lfont \
            -textvariable param(maxhistory)

        checkbutton .general.b.help -text [set s($lang)(balloon)] -font lfont \
            -variable gparam(showBalloons) -command {
                if {$gparam(showBalloons) == 1} {
                    .general.b.e configure -state normal -foreground black
                    .general.b.l1 configure -foreground black
                    .general.b.l2 configure -foreground black
                } else {
                    .general.b.e configure -state disabled -foreground darkgray
                    .general.b.l1 configure -foreground darkgray
                    .general.b.l2 configure -foreground darkgray
                }
             }

        label .general.b.l1 -text [set s($lang)(after)] -font lfont
        entry .general.b.e -width 5 -textvariable gparam(balloonDelay) \
            -font lfont -justify right
        label .general.b.l2 -text [set s($lang)(ms)] -font lfont

        checkbutton .general.b.as -text [set s($lang)(autosave)] -font lfont \
            -variable gparam(autosave)

        grid .general.b.hl -in .general.b -row 0 -column 0 -columnspan 2 \
            -sticky w -padx 4 -pady 4
        grid .general.b.he -in .general.b -row 0 -column 2 \
            -sticky e -padx 4 -pady 4
        grid .general.b.help -in .general.b -row 1 -column 0 \
            -sticky w -padx 4 -pady 4
        grid .general.b.l1 -in .general.b -row 1 -column 1 \
            -sticky w -padx 4 -pady 4
        grid .general.b.e -in .general.b -row 1 -column 2 \
            -sticky e -padx 4 -pady 4
        grid .general.b.l2 -in .general.b -row 1 -column 3 \
            -sticky w -padx 4 -pady 4
        grid .general.b.as -in .general.b -row 2 -column 0 -columnspan 3 \
            -sticky w -padx 4 -pady 4

    } else {
        wm deiconify .general
        raise .general
    }
}

proc selectColor {color name} {
    global s gparam 

    debug 2 "selectColor $color $name"
    set lang $gparam(lang)
    grab .
    set color [tk_chooseColor -title "[set s($lang)(color)] [set s($lang)($name)]" \
        -initialcolor $color]
    grab release .
    if [string compare $color ""] {
        return $color
    }
}

# Save options 
proc saveOptions {} {
    global opts pinfo
    global param gparam
    global pinfo env s searchmeth searchmpos

    debug 1 "saveOptions"
    if {$gparam(noconf) == 1} {
        return
    }
    set lang $gparam(lang)
    if {[file readable $param(rcfile)] &&
        ![file isfile $param(rcfile)]} {
       errorBox "$param(rcfile) isn't a regular file!"
       return
    }
    set err [catch "set fd \[open $param(rcfile) w\]"]
    if $err {
        errorBox "Couldn't open $param(rcfile) for writing!"
        return
    }
    puts $fd "# Options for $pinfo(pname) - do not edit!"
    puts $fd "# General options"
    puts $fd "set ding_version {$pinfo(version)}"
    puts $fd "set gparam(autosave) {$gparam(autosave)}"
    puts $fd "set gparam(lang) {$lang}"
    puts $fd "set gparam(showBalloons) {$gparam(showBalloons)}"
    puts $fd "set gparam(balloonDelay) {$gparam(balloonDelay)}"
    puts $fd "set param(params_as_menu) {$param(params_as_menu)}"
    puts $fd "set param(show_menu) {$param(show_menu)}"
    puts $fd "set param(umlaut_buttons) {$param(umlaut_buttons)}"
    puts $fd "set param(show_result) {$param(show_result)}"
    puts $fd "set param(show_status) {$param(show_status)}"
    puts $fd "set param(fcolor) {$param(fcolor)}"
    puts $fd "set param(bcolor) {$param(bcolor)}"
    puts $fd "set param(lfont) {$param(lfont)}"
    puts $fd "set param(bfont) {$param(bfont)}"
    puts $fd "set param(sfont) {$param(sfont)}"
    puts $fd "set param(tfont) {$param(tfont)}"
    puts $fd "set param(maxhistory) {$param(maxhistory)}"
    puts $fd "set param(width) {$param(width)}"
    puts $fd "set param(height) {$param(height)}"
    puts $fd "set param(search_prop) {$param(search_prop)}"
    puts $fd "set param(win_prop) {$param(win_prop)}"

    puts $fd "set opts(word) {$opts(word)}"
    puts $fd "set opts(case) {$opts(case)}"
    puts $fd "set opts(errors) {$opts(errors)}"
    puts $fd "set opts(regex) {$opts(regex)}"

    puts $fd "\n# Dictionaries and search methods"
    set n 0
    set so ""
    foreach i $searchmpos {
        set so "$so $n"
        incr n
    }
    puts $fd "set searchmpos {$so}"

    set n 0
    foreach i $searchmpos {
        puts $fd "set searchmeth($n,name) {$searchmeth($i,name)}"
        puts $fd "set searchmeth($n,type) {$searchmeth($i,type)}"
        puts $fd "set searchmeth($n,dictfile) {$searchmeth($i,dictfile)}"
        puts $fd "set searchmeth($n,separator) {$searchmeth($i,separator)}"
        puts $fd "set searchmeth($n,language1) {$searchmeth($i,language1)}"
        puts $fd "set searchmeth($n,language2) {$searchmeth($i,language2)}"
        puts $fd "set searchmeth($n,grepcmd) {$searchmeth($i,grepcmd)}"
        puts $fd "set searchmeth($n,grepopts) {$searchmeth($i,grepopts)}"
        puts $fd "set searchmeth($n,maxlength) {$searchmeth($i,maxlength)}"
        puts $fd "set searchmeth($n,maxresults) {$searchmeth($i,maxresults)}"
        puts $fd "set searchmeth($n,minlength) {$searchmeth($i,minlength)}"
        puts $fd "set searchmeth($n,shapedresult) {$searchmeth($i,shapedresult)}"
        incr n
    }

    catch {close $fd}
    .statusBar.lab configure -foreground $param(fcolor) -text "[set s($lang)(saveopts)] ok"
}

# History function
proc history {where} {
    global curhistory inshistory history_query history_result history_pos
    global param gparam s

    debug 1 "history $where"
    set lang $gparam(lang)
    set len [array size history_result]

    if ![string compare $where "back"] { # back
        if {$curhistory == 0} {
            set curhistory $inshistory
        }
        debug 8 "history: back: max $param(maxhistory) cur $curhistory ins $inshistory len $len"
        if {($curhistory <= 0) ||
            ($curhistory == 1 && $len < $param(maxhistory)) ||
            ($curhistory == 1 && $inshistory == $param(maxhistory)) ||
            ($curhistory == [expr $inshistory + 1])} {
                # no more history entries
                .statusBar.lab config -foreground $param(fcolor) \
                    -text [set s($lang)(noback)]
                return
        }
        # mark the current scroll position
        set history_pos($curhistory) [lindex [.result.text yview] 0]
        if {$curhistory == 1} {
            set curhistory $param(maxhistory)
        } else {
            set curhistory [expr $curhistory - 1]
        }
        display $curhistory "" 1
        if {($curhistory == 1 && ($len < $param(maxhistory) || $inshistory == $param(maxhistory))) || 
            ($inshistory == [expr $curhistory - 1])} {
            .search.s.back configure -state disabled
            .popup entryconfigure 0 -state disabled
        }
        .search.s.forw configure -state normal
        .popup entryconfigure 1 -state normal
    } else {            # forward
        debug 8 "history: forw: max $param(maxhistory) cur $curhistory ins $inshistory len $len"
        if {($curhistory <= 0) || ($len < $curhistory) ||
             ($len == $curhistory && $len < $param(maxhistory)) ||
             ($curhistory == $inshistory)} {
                # no more history entries
                .statusBar.lab config -foreground $param(fcolor) \
                    -text [set s($lang)(noforw)]
                return
        }
        # mark the current scroll position
        set history_pos($curhistory) [lindex [.result.text yview] 0]
        if {$curhistory == $param(maxhistory)} {
            set curhistory 1
        } else {
            incr curhistory
        }
        display $curhistory "" 1
        if {($curhistory >= $param(maxhistory) && $param(maxhistory) < $len) || 
            $curhistory == $inshistory} {
            .search.s.forw configure -state disabled
            .popup entryconfigure 1 -state disabled
        }
        .search.s.back configure -state normal
        .popup entryconfigure 0 -state normal
    }
}

proc sendMail {} {
    global param gparam pinfo s

    debug 1 "sendMail"
    set lang $gparam(lang)
    variable subject "$pinfo(pname) $pinfo(version): "
    variable nf $param(noticefile)

    if {! $param(isunix)} {
        # don't know how to send mail on Non-Unix ...
        tk_messageBox -icon error -type ok -parent . -message \
                    "[set s($lang)(nomail)] $pinfo(author)"
        return
    }

    if ![winfo exists .mail] {
        toplevel .mail
        wm group .mail .
        wm title .mail "[set s($lang)(mailtitle)] $pinfo(author)"
        wm resizable .mail 0 0
        placeWin .mail 50

        frame .mail.buttons
        button .mail.buttons.ok -text [set s($lang)(send)] -width 15 -command {
            set text [.mail.t.text get 1.0 end]
            if {[string length $text] <= 3} {
	        tk_messageBox -icon warning -type ok -parent .mail -message \
                    [set s($lang)(notext)]
        
                return
            }
            catch {set fd [open "|mail -s \"$subject\" $pinfo(authormail)" w]} em
            if ![info exists fd] {
                catch {set fd [open "|mail $pinfo(authormail)" w]} em
            }
            if ![info exists fd] {
                tk_messageBox -icon error -type ok -parent .mail -message \
                    [set s($lang)(notext)]
            } else {
                puts -nonewline $fd $text
                close $fd
                .statusBar.lab config -foreground $param(fcolor) \
                    -text "[set s($lang)(send)] ok."
            }
            destroy .mail
        }
        button .mail.buttons.cancel -text [set s($lang)(cancel)] -width 15 \
            -command {destroy .mail}
        pack .mail.buttons -side bottom -expand 1 -padx 8 -pady 8
        pack .mail.buttons.ok .mail.buttons.cancel -side left -padx 8

        frame .mail.s
        label .mail.s.label -text "Subject: " -font bfont
        entry .mail.s.subject -bg $param(shadedcolor) -textvariable subject \
            -relief sunken -font lfont

        label .mail.s.flabel -text "[set s($lang)(file)]: " -font bfont
        entry .mail.s.fname -bg $param(shadedcolor) -font lfont \
            -textvariable nf -relief sunken
        variable types {
            {"Text files" {.txt .vok .dic}}
            {"All files" *}
        }
        button .mail.s.fb -font lfont -text "[set s($lang)(attachnotice)] ..." \
            -command {
                if [string compare $nf ""] {
                    set err [catch "set fd \[open $nf r\]"]
                    if {! $err} {
                        .mail.t.text insert end [read $fd]
                        close $fd
                    }
                }
            }
        pack .mail.s -side top -fill x -expand 1 -padx 8 -pady 8
        grid columnconfig .mail.s 1 -weight 1
        grid .mail.s.label -row 0 -column 0 -sticky e
        grid .mail.s.subject -row 0 -column 1 -sticky news
        # grid x -in .mail.s 
        grid .mail.s.flabel -row 1 -column 0 -sticky e
        grid .mail.s.fname -row 1 -column 1 -sticky news
        grid .mail.s.fb -in .mail.s -row 1 -column 2 -sticky w -padx 4

        # pack .mail.s.label -side left
        # pack .mail.s.subject -side left -fill x -expand 1
        
        frame .mail.t
        text .mail.t.text -wrap word -width 70 -height 15 \
            -relief sunken -bg $param(shadedcolor) \
            -yscrollcommand ".mail.t.yscroll set"

        scrollbar .mail.t.yscroll -orient vertical \
                -command { .mail.t.text yview }
        if {$param(isunix)} {          # I like smaller scrollbars
            .mail.t.yscroll configure -width 10 -bd 1
        }
        pack .mail.t -fill x -expand 1 -padx 8 -pady 8
        pack .mail.t.text .mail.t.yscroll -side left -fill both -expand 1

    } else {
        wm deiconify .mail
        raise .mail 
    }
}

proc notice {file} {
    global param gparam s

    debug 1 "notice file"
    set lang $gparam(lang)

    if ![winfo exists .notice] {
        toplevel .notice
        wm group .notice .
        wm title .notice "[set s($lang)(notice)]"
        wm resizable .notice 0 0
        placeWin .notice 50

        variable nf $file

        frame .notice.buttons
        button .notice.buttons.ok -text [set s($lang)(save)] -width 15 -command {
            set text [.notice.t.text get 1.0 end]
            if {[string length $text] <= 0} {
	        tk_messageBox -icon warning -type ok -parent .notice -message \
                    [set s($lang)(notext)]
                return
            }
            set err [catch "set fd \[open $nf w\]"]
            if $err {
	            tk_messageBox -icon error -type ok -parent . -message \
        	            "Couldn't open $nf for writing!"
                return
            } else {
                puts -nonewline $fd $text
                close $fd
            }
            set $param(noticefile) $nf
            .statusBar.lab config -foreground $param(fcolor) \
                -text "[set s($lang)(save)] ok."
            destroy .notice
        }
        button .notice.buttons.cancel -text [set s($lang)(cancel)] -width 15 \
            -command {destroy .notice}
        pack .notice.buttons -side bottom -expand 1 -padx 8 -pady 8
        pack .notice.buttons.ok .notice.buttons.cancel -side left -padx 8

        frame .notice.f
        label .notice.f.label -text "[set s($lang)(file)]:"
        entry .notice.f.name -bg $param(shadedcolor) -font lfont \
            -textvariable nf -relief sunken
        variable types {
            {"Text files" {.txt .vok .dic}}
            {"All files" *}
        }

        button .notice.f.b -font lfont -text "Browse ..." \
            -command {
                set f [tk_getSaveFile -initialdir [file dirname $nf] \
                    -filetypes $types -parent .notice]
                if [string compare $f ""] {
                    set nf $f
                }
            }
 
        pack .notice.f -side top -fill x -expand 1 -padx 8 -pady 8
        pack .notice.f.label -side left
        pack .notice.f.name -side left -fill x -expand 1
        pack .notice.f.b -side left
        frame .notice.t
        text .notice.t.text -wrap word -width 70 -height 15 \
            -relief sunken -bg $param(shadedcolor) \
            -yscrollcommand ".notice.t.yscroll set"

        scrollbar .notice.t.yscroll -orient vertical \
                -command { .notice.t.text yview }
        if {$param(isunix)} {          # I like smaller scrollbars
            .notice.t.yscroll configure -width 10 -bd 1
        }
        pack .notice.t -fill x -expand 1 -padx 8 -pady 8
        pack .notice.t.text .notice.t.yscroll -side left -fill both -expand 1

        set err [catch "set fd \[open $nf r\]"]
        if {! $err} {
            .notice.t.text insert end [read $fd]
            close $fd
        }
    } else {
        wm deiconify .notice
        raise .notice 
    }
    focus .notice.t.text
}

proc startNew {} {
    global param gparam argv0

    set prog "$argv0 --noconf"
    debug 1 "new win: $prog"
    if [catch {exec $argv0 --noconf &} err] {
        .statusBar.lab config -foreground $param(errcolor) -text \
            "Error starting $prog: $err"
    } else {
        .statusBar.lab config -foreground $param(fcolor) -text "$prog started"
    }
}

# Settings for scrolling with a wheel mouse
# See http://www.inria.fr/koala/colas/mouse-wheel-scroll/
proc mscroll {bindtag} {
    bind $bindtag <Button-4> [list %W yview scroll -1 units]
    bind $bindtag <Button-5> [list %W yview scroll 1 units]
    bind $bindtag <Shift-Button-4> [list %W yview scroll -5 units]
    bind $bindtag <Shift-Button-5> [list %W yview scroll 5 units]
    bind $bindtag <Alt-Button-4> [list %W yview scroll -1 pages]
    bind $bindtag <Alt-Button-5> [list %W yview scroll 1 pages]
    bind $bindtag <Control-Button-4> { history back }
    bind $bindtag <Control-Button-5> { history forward }
}
mscroll Text

# Thanks to Holger Trapp <hot@hrz.tu-chemnitz.de>
# enforce loading the Tcl procedures "tcl_wordBreakBefore" and
# "tcl_wordBreakAfter" by calling them with senseless args
# tcl_wordBreakBefore "" 0
# tcl_wordBreakAfter "" 0

# now the global strings "tcl_nonwordchars" and "tcl_wordchars" are initialized
# and we can modify them as needed to specify the chars belonging or not
# belonging to words respectively
# set tcl_wordchars {[a-zA-Z0-9_äöüÄÖÜß]}
# set tcl_nonwordchars {[^a-zA-Z0-9_äöüÄÖÜß]}

# Invoking browsers
# http://mini.net/cgi-bin/wikit/InvokingBrowsers

proc urlOpen {url} {
    global param
    switch $::tcl_platform(platform) {
       "unix" {
            if {0 == [info exists ::env(BROWSER)]} {
               if { [ string length [ auto_execok mozilla ] ] } {
                  set ::env(BROWSER) [ auto_execok mozilla ]
               } elseif { [ string length [ auto_execok netscape ] ] } {
                  set ::env(BROWSER) [ auto_execok netscape ]
               } elseif {[info exists ::env(NETSCAPE)]} {
                  # Some of us use this envvar instead... - DKF
                  set ::env(BROWSER) $::env(NETSCAPE)
               } elseif { [ string length [ auto_execok lynx ] ] } {
                  # lynx can also output formatted text to a variable
                  # with the -dump option, as a last resort:
                  # set formatted_text [ exec lynx -dump $url ] - PSE
                  set ::env(BROWSER) [ auto_execok lynx ]
               }
            }
            debug 4 "Invoking $::env(BROWSER) -remote openURL($url)"
            .statusBar.lab config -foreground $param(fcolor) \
                -text "$::env(BROWSER) -remote openURL($url) ..."
            if {[catch {exec $::env(BROWSER) -remote openURL($url)}]} {
               # perhaps browser doesn't understand -remote flag
                debug 4 "Invoking $::env(BROWSER) $url"
                .statusBar.lab config -foreground $param(fcolor) \
                    -text "$::env(BROWSER) $url ..."
               if {[catch {exec $::env(BROWSER) $url &} emsg]} {
                  error "Error displaying $url in browser\n$emsg"
                  # Another possibility is to just pop a window up with
                  # the URL to visit in it. - DKF
               }
            }
      }
      "windows" {
          if {$::tcl_platform(os) == "Windows NT"} {
             set rc [catch {exec $::env(COMSPEC) /c start $url &} emsg]
          } else {
             # Windows 95/98
             set rc [catch {exec start $url} emsg]
          }
          if {$rc} {
             error "Error displaying $url in browser\n$emsg"
          }
      }
      "macintosh" {
          # AppleScript should be able to provide the default browser,
          # but I'm not enough of an AppleScript hacker to figure out.
          # Help!
          if {0 == [info exists ::env(BROWSER)]} {
             error "Couldn't find a usable browser.  Set env(BROWSER) in your application's preferences file."
          }
          if {[catch {
             AppleScript execute "tell application \"$::env(BROWSER)\"
             open url \"$url\"
             end tell
             "} emsg]
          } then {
             error "Error displaying $url in browser\n$emsg"
          }
       }
    } ;## end of switch
}


### internal search - search in dict file without an external grep
# Thanks to Jens Poenisch <jens@ruessel.in-chemnitz.de>

array set internal_search_Dict {}
array set internal_search_Dict {}
array set internal_search_init {}

proc internal_search {query dictfile max} {
    global internal_search_init internal_search_Dict internal_search_dict
    global param proc opts tcl_nonwordchars
    debug 4 "internal_search $query $dictfile $max"

    if {! [info exists internal_search_init($dictfile)]} {
        set nr [array size internal_search_init]
        incr nr
        debug 4 "Initialising $dictfile ($nr) ..."
        set s0 [clock seconds]
        .statusBar.lab config -foreground $param(fcolor) \
            -text "Initializing $dictfile ..."
        update idletasks
        set fd [open $dictfile r]
        fconfigure $fd -buffersize 1024000
        set Text [read $fd]
        close $fd

        # exact case
        set internal_search_Dict($nr) [split $Text \n]
        # lower case
        set text [string tolower $Text]
        set internal_search_dict($nr) [split $text \n]

        set internal_search_init($dictfile) $nr

        .statusBar.lab config -foreground $param(fcolor) \
            -text "[expr [clock seconds] - $s0] sec"
        debug 4 "done: [expr [clock seconds] - $s0] sec"
    }
    set f 0
    set num 0
    set result {}
    set proc(pid) 0
    set search {-regexp}
    if {$opts(regex) == 0} {        ; # simple query
        regsub -all {\*} $query {.*} query
    }
    if {$opts(case) == 0} {
        set query [string tolower $query]
    }
    if {$opts(word) == 0} {
        set searchquery "(^|.*${tcl_nonwordchars})${query}(${tcl_nonwordchars}.*|$)"
    } else {
        set searchquery "${query}"
    }
    # case exact ?
    set in $internal_search_init($dictfile)
    if {$opts(case)} {
        set d $internal_search_Dict($in)
    } else {
        set d $internal_search_dict($in)
    }
    debug 8 "searchquery = $searchquery in $in"
    while {$proc(pid) == 0 && $num <= $max && \
           [set i [lsearch -regexp [lrange $d $f end] $searchquery]] >= 0} {
        update
        lappend result [lindex $internal_search_Dict($in) [expr $f+$i]]
        debug 4 "$num: [lindex $internal_search_Dict($in) [expr $f+$i]]"
        set f [expr $f+$i+1]
        incr num
    }
    debug 4 "internal_search ended - $num results"
    return $result
}

