;******************************************************************************
;
; File name     : mkSearch.stk
; Creation date : Aug-3-1993
; Last update   : Aug-16-1993
;
;******************************************************************************
;
; STk adaptation of the Tk widget demo.
;
; mkTextSearch:
; Create a top-level window containing a text widget that allows you to load
; a file and highlight all instances of a given string.
;
;******************************************************************************

(provide "mkSearch")

(define fileName "")
(define searchString "")


(define (mkTextSearch)
  (catch (destroy .top-text-search))
  (toplevel ".top-text-search")
  (dpos .top-text-search)
  (wm 'title .top-text-search "Text Demonstration - Search and Highlight")
  (wm 'iconname .top-text-search "Text Search")

  (frame ".top-text-search.file")
  (label ".top-text-search.file.l" :text "File name:" :width 13 :anchor "w")
  (entry ".top-text-search.file.e"
	 :width 40 :relief "sunken" :bd 2 :textvar 'fileName)
  (button ".top-text-search.file.b"
	  :text "Load File"
	  :command '(TextLoadFile .top-text-search.t fileName))
  (pack .top-text-search.file.l .top-text-search.file.e :side "left")
  (pack .top-text-search.file.b :side "left" :pady 5 :padx 10)
  (bind .top-text-search.file.e "<Return>"
	'(begin
	   (TextLoadFile .top-text-search.t fileName)
	   (focus .top-text-search.string.e)))

  (frame ".top-text-search.string")
  (label ".top-text-search.string.l"
	 :text "Search string:" :width 13 :anchor "w")
  (entry ".top-text-search.string.e"
	 :width 40 :relief "sunken" :bd 2 :textvar 'searchString)
  (button ".top-text-search.string.b"
	  :text "Highlight"
	  :command '(TextSearch .top-text-search.t searchString 'search))
  (pack .top-text-search.string.l .top-text-search.string.e :side "left")
  (pack .top-text-search.string.b :side "left" :pady 5 :padx 10)
  (bind .top-text-search.string.e "<Return>"
	'(TextSearch .top-text-search.t searchString 'search))
  
  (button ".top-text-search.ok" :text "OK" :command '(destroy .top-text-search))
  (text ".top-text-search.t"
	:relief "raised" :bd 2 :setgrid "true"
	:yscroll ".top-text-search.s 'set")
  (scrollbar ".top-text-search.s"
	     :relief "flat" :command ".top-text-search.t 'yview")
  (pack .top-text-search.file .top-text-search.string :side "top" :fill "x")
  (pack .top-text-search.ok :side "bottom" :fill "x")
  (pack .top-text-search.s :side "right" :fill "y")
  (pack .top-text-search.t :expand "yes" :fill "both")

  ; Set up display styles for text highlighting.

  (if (equal? (tk 'colormodel .top-text-search) "color")
      (TextToggle
       '(.top-text-search.t 'tag 'config 'search
			  :background "SeaGreen4" :foreground "white") 800
       '(.top-text-search.t 'tag 'config 'search
			  :background "" :foreground "") 200)
      (TextToggle
       '(.top-text-search.t 'tag 'config 'search
			  :background "black" :foreground "white") 800
       '(.top-text-search.t 'tag 'config 'search
			  :background "" :foreground "") 200))
      
  (.top-text-search.t 'insert "0.0"
"This window demonstrates how to use the tagging facilities in text
widgets to implement a searching mechanism.  First, type a file name
in the top entry, then type <Return> or click on \"Load File\".  Then
type a string in the lower entry and type <Return> or click on
\"Load File\".  This will cause all of the instances of the string to
be tagged with the tag \"search\", and it will arrange for the tag's
display attributes to change to make all of the strings blink.
")
  (.top-text-search.t 'mark 'set "insert" "0.0")
  (bind .top-text-search "<Any-Enter>" '(focus .top-text-search.file.e)))


; The utility procedure below loads a file into a text widget,
; discarding the previous contents of the widget. Tags for the
; old widget are not affected, however.
; Arguments:
;
; w -	 The window into which to load the file.  Must be a text widget.
; file - The name of the file to load.  Must be readable.

(define (TextLoadFile w file)
  (let ((f (open-input-file file)))
    (w 'delete "1.0" "end")
    (do ((line (read-line f) (read-line f)))
	((eof-object? line) (close-input-port f))
	
	(w 'insert "end" (string-append line "\n")))))



; The utility procedure below searches for all instances of a
; given string in a text widget and applies a given tag to each
; instance found.
; Arguments:
;
; w -	   The window in which to search.  Must be a text widget.
; string - The string to search for.  The search is done using
;	   exact matching only (no special characters).
; tag -	   Tag to apply to each instance of a matching string.

(define (TextSearch w str tag)
  (w 'tag 'remove 'search "0.0" 'end)
  (let ((numLines (floor (string->number (w 'index 'end))))
	(l (string-length str)))
     (do ((i 1 (+ i 1)))
	 ((> i  numLines))
       (let* ((line (w 'get (format #f "~A.0" i) (format #f "~A.1000" i)))
	      (offset 0)
	      (pos  (string-index str line)))
	 (if pos
	     (do ((index pos (string-index str line)))
		 ((not index))
	       (set! offset (+ offset index))
	       (w 'tag 'add tag (format #f "~A.~A" i  offset) 
		                (format #f "~A.~A" i (+ offset l)))
	       (set! offset (+ offset l))
	       (set! line (substring line (+ index l) (string-length line)))))))))


; The procedure below is invoked repeatedly to invoke two commands
; at periodic intervals.  It normally reschedules itself after each
; execution but if an error occurs (e.g. because the window was
; deleted) then it doesn't reschedule itself.
; Arguments:
;
; cmd1 -   Command to execute when procedure is called.
; sleep1 - Ms to sleep after executing cmd1 before executing cmd2.
; cmd2 -   Command to execute in the *next* invocation of this procedure.
; sleep2 - Ms to sleep after executing cmd2 before executing cmd1 again.

(define (TextToggle cmd1 sleep1 cmd2 sleep2)
  (catch
   (eval cmd1)
   (after sleep1 `(TextToggle ',cmd2 ,sleep2 ',cmd1 ,sleep1))))
