;;;; ;;;; STk adaptation of the Tk widget demo. ;;;; ;;;; This demonstration script creates a collection of widgets that ;;;; allow you to load a file into a text widget, then perform searches ;;;; on that file. ;;;; (define Wsearch-name "") (define Wsearch-string "") (define (demo-search) ;; ;; Functions used by this demo ;; (define (load-file t file) (text-delete t 0.0 "end") (with-input-from-file file (lambda () (do ((l (read-line) (read-line))) ((eof-object? l)) (text-insert t "end" l) (text-insert t "end" "\n"))))) (define (search t str tag) ; Search for all instances of a given string in a text widget and ; apply a given tag to each instance found. (tag-remove tag 0.0 "end") (let ((length (string-length str))) (when (> length 0) (let loop ((cur (text-search t str 1.0 "end"))) (when cur (let ((last (cons (car cur) (+ (cdr cur) length)))) (tag-add tag cur last) (loop (text-search t str last "end")))))))) (define (toggle t tag) (catch (let ((bg (slot-ref tag 'background)) (fg (slot-ref tag 'foreground))) (slot-set! tag 'foreground bg) (slot-set! tag 'background fg) (update) (after t (lambda () (toggle t tag)))))) (let* ((w (make-demo-toplevel "search" "Text Demonstration - Search and Highlight" "")) (t (make :wrap "word" :parent w)) (tag (make :parent t :foreground "IndianRed1"))) (when (= (winfo 'depth w) 1) (slot-set! tag 'background "white") (slot-set! tag 'foreground "black")) (let* ((f1 (make :parent w)) (l1 (make