# editfeatures
#	Implements all editing features for the text editor

# findText
#
# 	Search the text for the search string allowing
#	user to select case sensitive, direction and regexp
# 	matching
#
#

proc findText {} {
	global findwhat exact_match case_sensitive search_direction
	global match_length find_index
	set args " "
	if {$case_sensitive==1} {
		set args [lappend args -nocase]
	}
	if {$search_direction==0} {
		set args [lappend args  -forwards]
	} else {
		set args [lappend args  -backwards]
	}
	if {$exact_match==0} {
		set args [lappend  args  -exact]
	} else { 
		set args [lappend  args  -regexp]
	}
	set match_length 0
	set args [lappend args -count match_length]
	set args [lappend args --]
	set find_index 1.0
	catch { eval {.text search} $args $findwhat insert } find_index 
	focus .text
	if [catch {.text see $find_index}] {
		return
	}
	.menu.edit.m entryconfigure "Find Next" -state normal
	.text mark set insert $find_index 
}

# findnextText
#
#	Search for search string in the direction specified 
#	previously
#

proc findnextText {} {
	global search_direction 
	global match_length find_index
	if {$search_direction==0} {
		focus .text
		if [catch { .text mark set insert "$find_index + $match_length chars" }] {
	 		return
		}
	}
	findText 
}
 
# replaceText
#
#	Replace search text  with replace text string 
#	previously
#

proc replaceText {} {
	global replacewith 
	global match_length find_index
	findText 
	if [catch { .text delete $find_index "$find_index + $match_length chars"}] {
	  	return
	}
	.text insert $find_index $replacewith
}; #replaceText



# clearEditor
#
#	Delete all temporary files and set menu to default values
#	Clear the text editor of all contents
#
	

proc clearEditor {} {
	global pidVal
#		file delete -force tempold.$pidVal
#		file delete -force templatest.$pidVal
#		file delete -force patchfile.$pidVal
		.text delete 1.0 end	
		# Disable menu options as no file exists 	
#		.menu.file.m entryconfigure "Save" -state disabled
#		.menu.file.m entryconfigure "Close" -state disabled
		wm title . "Multi User Editor"
} 
	
# copy
#
# 	Clear buffer for cut text and get new selection and start and 
#	ending index of the selection. Clear the selection  and store 
#	new selection in buffer.
#


proc copy {} {
	global cut_text cut_index start_cut
 	set cut_text ""
	set cut_index 1.0
	.text mark set cut_index insert
	if [catch  {selection get}  cut_text ] {
		return 0
	}
	set start_cut [string length $cut_text]
	.menu.edit.m entryconfigure "Paste" -state normal
	return 1
}

# cut
#
# 	Clear buffer for cut text and get new selection and start and 
#	ending index of the selection. Clear the selection  and store 
#	new selection in buffer.
proc cut {} {
	global cut_text cut_index start_cut
	set retval [copy]
	if {$retval==1} {
		.text delete "insert - $start_cut chars" insert
	}
}
# paste
#
#	Paste the contents of buffer (cut_text) at current insertion
#	point ( where the cursor is situated).
#

proc paste {} {
	global cut_text 
	.text insert insert $cut_text
}
