G4C

; $VER: FSearch.gc 2.0
; by D.Keletsekis June 1999

; GUI for the FSearch command
; You must have FSearch 2.0 in the "c:" or "guis:c" dirs

; This gui can be called as :
; GUILOAD fsearch.gc SearchPath Text2Find

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									  Globals
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

WINBIG 103 22 413 159 'FSearch.gc by D.Keletsekis'
winsmall -1 20 150 -1
WinType 11110001
resinfo 8 660 270

BOX 2 13 410 128 OUT button
	gadid 988 ; for gadredraw
	attr resize 0022

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									 System events
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

xOnLoad searchpath findtxt
	setgad FSearch.gc 101  hide
	setgad FSearch.gc 201/250 hide
	setgad FSearch.gc 300/350 hide
	setgad FSearch.gc 325/333 off   ; size
	setgad FSearch.gc 305/306 off   ; date
	; the default values
	file = '#?'
	subdirs = ALL
	case = ''
	strsearch = On
	output = gui
	format = 'VERBOSE'
	nobin = ''
	nopath = ''
	details = ''
	highlight = HL
	min_size = ''
	max_size = ''
	from = ''
	to = ''
	header = ''
	guimode = PAR
	GuiOpen FSearch.gc
	update fsearch.gc 2 $searchpath
	update fsearch.gc 7 $findtxt
	setgad fsearch.gc 7 on

	; run fsearch..
	extract fsearch.gc guipath fsprog
	joinfile $fsprog fsearch fsprog
	ifexists file $fsprog
		; nop
	elseifexists file c:fsearch
		fsprog = c:FSearch
	elseifexists file guis:c/fsearch
		fsprog = guis:c/fsearch
	else
		ezreq 'Could not find the FSearch binary\nYou *must* copy it to C:' 'OK' ''
	endif
	guiload :fsearch.rmb		; load pop-up guis
	guiload :fsearch.pop
	guiload :fsearch.ed		; gui to interface with editor (CEd in my case)

xOnClose
	GuiQuit FSearch.gc
	GuiQuit fsearch.ft		; filetypes gui
	GuiQuit fsearch.rmb
	GuiQuit fsearch.pop
	GuiQuit fsearch.ed
	guiquit fsearch.rep

xONRMB
	if $guimode = RES
		guiopen fsearch.rmb
	endif

xOnHelp OFF
	gadid 905
	gosub #this dohelp

xRoutine dohelp
	if '$$help.text' > ''
		setwintitle fsearch.gc '$$help.text'
	else
		setwintitle fsearch.gc 'No help here, but it should be obvious..'
	endif

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									Gui Mode cycler
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; ---- a cycler to change the gui to its various modes

XCYCLER 2 0 188 13 "" guimode
	gadhelp 'Click to change the GUI mode - use "m" for shortcut'
	gadid 500
	attr resize 0010
	gadkey m       ; use 'm' to cycle it
	CStr "FSearch Parameters"	PAR
	CStr "FSearch Settings" SET
	cstr "Date, Size & Type"  DATE
	cstr "FSearch Results" RES
	gosub FSearch.gc guichange

xroutine guichange
	docase $guimode
		case = PAR
			setgad FSearch.gc 1/13	 show
			setgad FSearch.gc 21/24 show
			setgad FSearch.gc 101	 hide
			setgad FSearch.gc 300/350	hide
			setgad FSearch.gc 201/250	hide
			break
		case = SET
			setgad FSearch.gc 1/13	 hide
			setgad FSearch.gc 21/24 hide
			setgad FSearch.gc 101	 hide
			setgad FSearch.gc 300/350	hide
			setgad FSearch.gc 201/250	show
			break
		case = DATE
			setgad FSearch.gc 1/13		hide
			setgad FSearch.gc 21/24 hide
			setgad FSearch.gc 101		hide
			setgad FSearch.gc 201/250	hide
			setgad FSearch.gc 300/350	show
			break
		case = RES
			setgad FSearch.gc 1/13	 hide
			setgad FSearch.gc 21/24 hide
			setgad FSearch.gc 101	 show
			setgad FSearch.gc 300/350	hide
			setgad FSearch.gc 201/250	hide
			break
	endcase

	; redraw FSearch.gc
	; NEW - using PARTREDRAW - looks great eh?
	; partredraw FSearch.gc 4 15 405 124

	; redraw 1 pixel inside the box (top of file)
	gadredraw #this 988 988 988 988 1

	if $guimode != RES
		SetWintitle FSearch.gc	'FSearch by D. Keletsekis'
	endif


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									Parameters
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; -------------------------- choose searchpath(s)

XTEXTBOX 10 20 394 64 '' ''
	attr tbox 1/0/3/button/in
	attr resize 0021
	gadid 12

XTEXTIN 18 37 356 13 "" searchpath sys: 100
	gadhelp 'The path FSearch will start searching from'
	attr title '2/1/shadow/left/above/Search in:'
	attr resize 0120
	gadid 2
	setgad FSearch.gc 5 on			; set cursor to next textin gad

XBUTTON 376 37 18 13 "<"			; choose a searchpath
	gadhelp 'Choose a path from a requester'
	ATTR resize 2100
	gadid 3
	ReqFile -1 -1 300 200 'Choose SearchPath:' DIR searchpath $searchpath
	update FSearch.gc 2 $searchpath

; ------------------------ filename pattern

XTEXTIN 18 65 356 13 "" file '#?' 100
	gadhelp 'Enter the filepattern - #? means all files'
	attr title '2/1/shadow/left/above/File name pattern:'
	attr resize 0120
	gadid 5
	setgad FSearch.gc 7 on			 ; set cursor to next textin gad

XBUTTON 376 65 18 13 "<"
	gadhelp 'Choose from various common patterns'
	ATTR resize 2100
	gadid 4
	guiopen fsearch.ft :fsearch.pat 5

XCHECKBOX 370 24 24 12 "" subdirs ALL "" ON
	gadhelp 'Check to recurse through all subdirectories'
	attr title "2/0/plain/lside/bottom/Include sub-Directories"
	ATTR resize 2100
	gadid 11

; ---------------------- text search

XTEXTBOX 9 92 395 37 '' ''
	attr tbox 1/0/3/button/in
	attr resize 0121
	gadid 8

XTEXTIN 18 111 377 13 "" findtxt '' 100
	gadhelp 'Enter text to find - (nothing = no text search)'
	attr title '2/1/shadow/left/above/Find text:'
	attr resize 0120
	gadid 7

XCHECKBOX 369 96 24 12 "" case CS "" OFF
	gadhelp 'Check for Case Sensitive search'
	attr title "2/0/plain/lside/bottom/Case sensitive"
	ATTR resize 2100
	gadid 9


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;								Search results listview
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

XLISTVIEW 4 15 406 124 "" resfile "" 10 MULTI
	gadhelp "Display search results - Click RMB for options"
	gadid 101
	attr resize 0022
	gadfont #mono 8 001

	; -------- If its a LINE - go to it..
 
	if $resfile[0][1] = ' '
	or $resfile[0][4] = '›33m'

		if $resfile[0][4] = '›33m'						; cut off highlighting
			cutvar resfile[4] cut word  1 resnum
		else
			cutvar resfile cut word 1 resnum			; get the line number
		endif
		extract resnum clean resnum
		cutvar resnum	cut char -1 ''
		while $$lv.rec[0][1] == '	'					; get the parent file
		or $$lv.rec[0][4] == '›33m'
		and $$lv.line > ''
			lvgo	prev
		endwhile
		resfile	= $$lv.rec
		if $resfile[0][4] = '›32m'	; cut off highlighting
			cutvar resfile[4] cut word 1	resfile
			cutvar resfile cut char -4 temp	  		; trim the csi
		endif

		gosub fsearch.ed LoadFile $fsearch.gc/resfile $fsearch.gc/resnum

	; ------------ If its a FILE - use popup window to deal with it..

	elseif $resfile[0][1] != '*'

		if $resfile[0][4] =	'›32m'	; cut off highlighting
			cutvar resfile[4] cut word 1 resfile
			cutvar resfile cut char -4 temp	  		; trim the csi
		endif
		guiopen fsearch.pop $resfile 1

	endif 


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									 FSearch settings
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; ------- output mode

BOX 23 28 177 98 in button
	gadid 201

XRADIO 162 36 18 9 output 2
	gadhelp 'Choose where you want the output from FSearch to go to'
	gadid 202
	rstr 'Output to Gui'   gui
	RStr 'Output to Shell' cli

; ----------- Output format 

XRADIO 162 66 19 9 format 2
	gadhelp 'Says it all really..'
	gadid 203
	RStr	'Show text found' 'VERBOSE'
	RStr	'Filenames only'	''

; ----------- LINE switch

XRADIO 162 96 19 9 lineswitch 2
	gadhelp 'Show Line or Byte No of text found - Byte is faster!'
	gadid 203 ; same as above
	RSTR 'Show Byte No' ''
	RSTR 'Show Line No' LINE

; ------------ Settings - checkboxes

BOX 218 28 178 73 IN BUTTON
	gadid 208

XCHECKBOX 366 36 22 12 'Highlight results' highlight "HL" "" ON
	gadhelp 'Bask in the glory of colored ansi text'
	gadid 205

XCHECKBOX 366 51 22 12 "Check binary files" nobin "" "NOBIN" ON
	gadhelp 'Also search binary (non-text) files'
	gadid 211

XCHECKBOX 366 66 22 12 "Show File Path" nopath "" "NOPATH" ON
	gadhelp 'Print the full path of the files found'
	gadid 212

XCHECKBOX 366 81 22 12 "Show File Details" details "INFO" "" OFF
	gadhelp 'Also print file info - Disables REPLACE!'
	gadid 213

; ------- help & save settings

XCHECKBOX 391 1 20 11 'OnLine help' helpon "ON" "OFF" OFF
	gadhelp 'Start/Stop showing these help messages'
	attr resize 2000
	if $helpon = ON
		setgad #this 905 ON
		setgad fsearch.rmb 905 on
	else
		setwintitle #this 'FSearch.gc by D.Keletsekis'
		setgad #this 905 OFF
		setgad fsearch.rmb 905 off
	endif

XBUTTON 261 111 91 13 "Help.."
	gadhelp 'Load & display the FSearch.readme file'
	gadid 220
	update FSearch.gc 500 3        ; change gui to show results listview
	guimode = RES
	gosub FSearch.gc guichange
	lvuse #this 101
	lvchange :fsearch.readme

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;										  DO IT!
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; This is where the commandline is formed and launched. The name of
; the variable which holds it is 'options'


XBUTTON 20 143 91 13 "_Search"
	gadhelp 'Start the search!'
	gadid 501
	attr resize 1210
	if $searchpath < ' '
		setwintitle FSearch.gc 'Wrong parameters!'
		stop
	endif
	SetWintitle FSearch.gc Searching...

	; --- construct the command line

	options = '$searchpath '
	if $file != '#?'
	and $file > ' '
		appvar options 'PAT=$file	'
	endif
	if $findtxt > ' '
		appvar options 'TXT=\"$findtxt\" $case '
	endif
	if $size = SIZE
		if $min_size  > ' '
		and $min_size > 0
			min = $min_size
			if $size_type = mb
				min == $min *	1000
			endif
			appvar options 'MIN=$min '
		endif
		if $max_size  > ' '
		and $max_size > 0
			max = $max_size
			if $size_type = mb
				max == $max *	1000
			endif
			appvar options 'MAX=$max '
		endif
		if $min_size > $max_size
		and $max_size != ''
			ezreq 'Minimum size is more than\nMaximum size..' Ooops! ''
			stop
		endif
	endif
	if $date = DATED
		if $from_date > ' '
			appvar options 'FROM=$from_date '
		endif
		if $to_date > ' '
			appvar options 'TO=$to_date '
		endif
	endif
	extract header clean header
	if $header > ' '
	and $header != NONE
		appvar options 'HEADER=$header'
		appvar options '#? '
	endif
	appvar options '$format $nobin $nopath $details $highlight $subdirs $lineswitch'

	; disable gadgets
	setgad FSearch.gc 501/502 off

	; --- launch according to where it should output

	if $output = cli
		say '\n-------------- Search results :\n'
		launch 1 '$fsprog $options'
	else
		launch 2 '$fsprog >pipe:findres $options'
		update FSearch.gc 500 3        ; change gui to show results listview
		guimode = RES
		gosub FSearch.gc guichange
	endif

; ---------- xONRETURN commands - handle returns according to launches

; -- return from shell launch

xonreturn 1   ; to the shell
	say '-------------- Search finished.\n'
	setgad FSearch.gc 501/502 on
	SetWintitle FSearch.gc 'Search finished.'

; -- return from gui output mode launch

xonreturn 2   ; to gui or file
	lvuse FSearch.gc 101
	setgad FSearch.gc 501/502 on
	SetWintitle FSearch.gc 'Search finished.'

; -- the pipe event where the output is piped at

xpipe pipe:findres ON
	gadid 999
	lvuse FSearch.gc 101      ; whenever a line is available
	lvadd $$pipe				  ; append it to our listv

; -- We're all human..

xonfail
	setwintitle FSearch.gc '** Error **'
	setgad FSearch.gc 501/502 on


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;								 Clear & cancel buttons
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; --------------- The CLEAR button - reset all settings

XBUTTON 209 143 91 13 "C_lear"
	gadhelp 'Clear the current gadgets - will not change hidden gadgets'
	gadid 502
	attr resize 1210
	docase $guimode
	case =	PAR
		file	= #?
		update #this 5 $file
		findtxt = ''
		update #this 7 ''
		break
	case =	SET
		break
	case =	DATE
		date	= ANY
		update FSearch.gc 301 0
			from_date = ''
			update FSearch.gc 305 ''
			to_date	= ''
			update FSearch.gc 306 ''
			size	= ANY
			update FSearch.gc 321 0
			min_size =	''
			update FSearch.gc 325 ''
			max_size =	''
			update FSearch.gc 330 ''
			size_type =	kb
			update FSearch.gc 329 0
			; update FSearch.gc 333 0
			setgad FSearch.gc 325/333	off	; size
			setgad FSearch.gc 305/306	off	; date
			header = NONE
			update FSearch.gc 341 'All files'
			break
	case = RES
			lvuse FSearch.gc	101	; clear the lv
			lvclear
			break
	endcase

; --------------- The CANCEL button

XBUTTON 304 143 91 13 "_Cancel"
	gadhelp 'Abort the Search!'
	gadid 503
	attr resize 1210
	breaktask $fsprog C		 ; break fsearch
	setgad FSearch.gc 999 CLEAN	 ; stop & flush pipe output
	lvuse FSearch.gc 101
	lvadd "**** BREAK ****"


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;							  SET DATE, SIZE & TYPE
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; ----- Date

XTEXTBOX 13 17 388 41 '' Date:
	attr tbstyle 2/1/shadow/left
	attr tbox 2/1/3/button/in
	attr resize 0021
	gadid 335

XRADIO 159 27 17 9 date 4
	gadhelp 'Use the from & to dates entered here'
	attr resize 1100
	gadid 301
	RStr 'Any date'	 ANY
	RStr 'Files dated' DATED
	if $date = ANY
		setgad FSearch.gc 305/306 off
	else
		setgad FSearch.gc 305/306 on
	endif

XTEXTIN 312 24 77 12 "From:" from_date '' 100
	gadhelp 'Show files created/changed after this date'
	attr resize 2100
	gadid 305
	setgad FSearch.gc 306 on

XTEXTIN 312 39 77 12 "To:" to_date '' 100
	gadhelp 'Show files created/changed before this date'
	attr resize 2100
	gadid 306
	setgad FSearch.gc 305 on

; ----- Size

XTEXTBOX 12 63 388 54 '' Size:
	attr tbstyle 2/1/shadow/left
	attr tbox 2/1/3/button/in
	attr resize 0121
	gadid 335

XRADIO 159 78 18 9 size 4
	gadhelp 'Use the file size settings entered here'
	gadid 321
	attr resize 1100
	RStr 'Any size' ANY
	RStr 'Files sized' SIZE
	if $size = SIZE
		setgad FSearch.gc 325/333 on
	else
		setgad FSearch.gc 325/333 off
	endif

XTEXTIN 309 69 47 12 Over min_size "" 100
	gadhelp 'Show files larger than this'
	attr resize 2100
	gadid 325

XBUTTON 357 69 15 12 "<"
	gadid 327
	attr resize 2100
	if $min_size > 0
		counter min_size dec 1
		update FSearch.gc 325 $min_size
	endif

XBUTTON 372 69 15 12 ">"
	gadid 328
	attr resize 2100
	counter min_size inc 1
	update FSearch.gc 325 $min_size

XCYCLER 309 99 78 12 "" size_type
	CStr Kb kb
	CStr Mb mb
	gadhelp 'Express size in Kilobytes (Kb) or MegaBytes (Mb)'
	attr resize 2100
	gadid 329

XTEXTIN 309 84 47 12 Under max_size "" 100
	gadhelp 'Show files smaller than this'
	attr resize 2100
	gadid 330

XBUTTON 357 84 15 12 "<"
	gadid 331
	attr resize 2100
	if $size_size	> 0
		counter max_size dec 1
		update FSearch.gc 330 $max_size
	endif

XBUTTON 372 84 15 12 ">"
	gadid 332
	attr resize 2100
	counter max_size inc 1
	update FSearch.gc 330 $max_size

;XCYCLER 334 99 52 12 "" max_type
;	CStr Kb kb
;	CStr Mb mb
;	attr resize 2100
;	gadid 333

; ----- File type

XTEXTBOX 66 120 296 14 '' "All files"
	attr title 2/1/shadow/lside/middle/Type:
	attr tbox 2/1/0/button/out
	attr resize 0220
	gadid 341

XBUTTON 363 120 22 14 "<"
	gadhelp 'Choose a file type from a pop-up list'
	attr resize 2200
	gadid 342
	guiopen fsearch.ft :fsearch.types 341


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;								ROUTINES

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

xRoutine NextFile
	lvuse fsearch.gc 101
	lvmulti first	; 1st every time since we set item off at end

	; Skip all lines ..
 	while $resfile[0][1] = ' '		; normal line
	or $resfile[0][4] = '›33m'		; highlighted
	or $resfile[0][1] = '*'			; break
		lvmulti off
		lvmulti next
	endwhile

	if $$lv.line = ''					; end of list
		resfile = ''
	else									; cut off esc seq
		if $resfile[0][4] = '›32m'
			cutvar resfile cut char 4 ''
			cutvar resfile cut char -4 ''
		endif
		lvmulti off
	endif 


; ##################################################################
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									  FSearch.ft
;
;			 Gui with listview for choosing file header type
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ##################################################################

NEWFILE	fsearch.ft

WINBIG 245 109 179 116	''
WinType 00001000
winonmouse 100 50
; varpath FSearch.gc

xonopen list id
	lvuse fsearch.ft 1
	lvchange $list

xoninactive
	guiclose fsearch.ft

xonrmb
	guiclose fsearch.ft

; start off with empty listview - load the fsearch.types file in the
; fsearch.gc xonload event

XLISTVIEW 0 0 179 116  "" ftype '' 10 TXT
	gadid 1
	gadfont #mono 8 000
	guiclose fsearch.ft
	if $id = 341	; file type
		cutvar ftype cut word -1 fsearch.gc/header
		update FSearch.gc $id $ftype
	else		; search pattern
		cutvar ftype cut word -1 fsearch.gc/file
		extract fsearch.gc/file clean fsearch.gc/file
		update FSearch.gc $id $fsearch.gc/file
	endif





