#!/usr/bin/wish

# This program is part of Geoduck, a set of graphical user
# interfaces for UNIX programs.
# Copyright (C) 2002 Allen B. Downey
#
# 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.
#
# For a copy of the GNU General Public License, go to
# www.gnu.org/copyleft/gpl.html
# or write to the Free Software Foundation, Inc.
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Contact the author: me@allendowney.com

set dir [file dirname [info script]]
source [file join $dir geoduck.tcl]

proc getCommand {} {
    global st widget

    set cmd "grep[getFlags]"
    append cmd [checkContext]

    # get pattern from the "file or dir" entry
    if {$st(rb_pattern) == "pattern"} {
	set pattern [$widget(en_pattern) get]
	if {[string range $pattern 0 0] == "-"} {
	    append cmd " -e $pattern"
	} else {
	    append cmd " $pattern"
	}
    } else {
	set file [$widget(en_patfile) get]
	append cmd " -f $file"
    }

    # get arguments from the "file or dir" entry
    set args [$widget(en_args) get]
    append cmd " $args"
}

proc checkContext {} {
    global st widget opt

    if {$st(context) == "none"} {return ""}
    set lines [$widget(en_context) get]
    if {$lines == ""} {return ""}

    return " $opt($st(context))$lines"
}

proc test {} {
    global env widget

    cursorWatch
    set pattern [$widget(en_pattern) get]
    set args [$widget(en_args) get]
    
    if {$pattern == "" || $args == ""} {
	clearText $widget(text)
	return
    }

    # execute the command and put the result in the text window
    set cmd [$widget(en_command) get]
    fillText text [execCatch $cmd]
    cursorNormal
}


topFrame
set st(autotest) 1
fr -pady 5


# COLUMNS 1 and 2

fr -side left -padx 5

la "Search for pattern:"
rb "on command line"   rb_pattern  pattern
en 15 en_pattern
fillEntry en_pattern pattern

rb "from file"     rb_pattern  file
en 15 en_patfile "" browse

la  "Search which files or dirs?"
en 15 en_args "" browse
fillEntry en_args filename


fr -side left

la  ""
cb  "inverse matching?"        cb_inverse     -v
cb  "pattern must match word?" cb_word        -w
cb  "pattern must match line?" cb_line        -x

la ""
la ""
fr
la "Pattern type:" -side left
mb "Extended"                    mb_type  extended  ""
mb "Basic"                       mb_type    basic   -G
mb "Perl"                        mb_type    perl    -P
mb "Fixed strings"               mb_type    fixed   -F
endfr

la ""
fr
la "Maximum matches" -side left
en 5 en_max -m
endfr

la ""
cb "Use mmap (possibly faster)?"            cb_mmap    --mmap

endfr



# COLUMN 2

frSpace -side left -padx 2
fr -side left

la ""
cb "skip directories?"                cb_skipd     "-d skip"
cb "skip devices?"                    cb_skipdev   "-D skip"
la ""
cb "search directories recursively..."  cb_recurse -R

fr
la "...including files that match:"
en 15 en_include --include=
la "...skipping files that match:"
en 15 en_exclude --exclude=
endfr

la ""
fr
la "Binary files:" -side left
mb "treat as binary"                            mb_binary   binary   ""
mb "treat as text"           mb_binary   text     -a
mb "assume no match"                   mb_binary   nomatch  -I
endfr

endfr

endfr

# COLUMN 3

fr -side left -padx 5

fr
la "What kind of output?" -side left
mb "Lines that match"            mb_output   lines   ""
mb "Number of matches"           mb_output   count   -c
mb "Files that contain matches"  mb_output  files     -l
mb "Files without matches"       mb_output  notfiles  -L
mb "Nothing"                     mb_output  quiet     -q
mb "Help information"            mb_output  help      --help
mb "Version information"         mb_output  version   -V
endfr

la ""
cb "Suppress error messages?"        cb_suppress -s
cb "Output only the matching part?"      cb_match    -o
cb "Include line number of matches?"     cb_lineno    -n
cb "Include byte offset of matches?"     cb_offset    -b
cb "Unix compatible byte offsets?"        cb_unixoff   -u
config cb_unixoff -anchor e
cb "Insert zero byte after file name?"   cb_zero     -Z

la ""
cb "Include file names containing matches?"     cb_names  -H
cb "Suppress file names with matches?"          cb_noname -h


la ""

fr

fr -side left
la "Lines of output context" 
en 8 en_context
fillEntry en_context 1


rb "No context"      context   none
rb "# lines before"  context   before   -B  
rb "# lines after"   context   after    -A
rb "# lines around"  context   around   -C

endfr

fr
#repack frame -anchor s
colorMenu
endfr

endfr
endfr
endfr


# ROW 3
fr top -pady 5
te text
endfr

bind . <Return> "+test"

showCommand

