-- Find Script By String v.1.0 [2009-06-27]
-- by Karabakalov (Anubis) anubiss@abv.bg
if findScriptDlg != undefined then destroyDialog findScriptDlg
rollout findScriptDlg "Find Script By String" width:500 --height:200
(
	local allMSfiles = #()
	local result = #()
	-- UI items
	edittext ed_currDir "" width:446 across:2
	button bt_srcDir "..." height:18 align:#right
	edittext ed_search "" width:473
	multiListBox mlb_files "" width:473
	label lb_result "Enter string to search..."
	-- Functions
	fn getFilesRecursive root pattern =
	(
		dir_array = GetDirectories (root+"/*")
		for d in dir_array do
			join dir_array (GetDirectories (d+"/*"))
		join dir_array (GetDirectories root)
		my_files = #()
		for f in dir_array do
			join my_files (getFiles (f + pattern))
		my_files
	)
	fn searchFn = (
		result = #()
		setWaitCursor()
		for f in allMSfiles do
		(
			s = openFile f
			while not eof s do
			(
				find = readLine s
				if findString find ed_search.text != undefined then
				(append result f ; exit )
			)
			close s
			result
		)
		lb_result.text = (result.count as string) + " files found"
		if result.count > 0 then
		(mlb_files.items = result ; lb_result.text += " -- Double Click to Edit...")
		setArrowCursor()
	)
	-- Events
	on ed_search entered txt do
	(
		if txt != "" then searchFn()
		else lb_result.text = "Enter string to search..."
	)
	on bt_srcDir pressed do
	(
		d = getSavePath caption:"Where to Search..." initialDir:"$scripts"
		if d != undefined then
		(
			ed_currDir.text = d
			allMSfiles = getFilesRecursive ed_currDir.text "*.ms"
		)
	)
	on mlb_files doubleClicked itm do
		if mlb_files.selection != 0 then edit mlb_files.items[itm]
	on findScriptDlg open do
	(
		ed_currDir.text = (getDir #scripts)
		allMSfiles = getFilesRecursive (getDir #scripts) "*.ms"
	)
)
createDialog findScriptDlg style:#(#style_titleBar, #style_border, #style_sysMenu, #style_minimizeBox)
