#lang racket/base
(require racket/gui
framework/gui-utils
racket/help)
(define str-out #f)
(define str-in #f)
(define f (new dialog% [label "Regexp Replace"]
[min-width 500]))
(define hp-help (new horizontal-panel% [parent f]))
(new message% [parent hp-help] [label "Replace the selected text using an extended regular expression"])
(define bt-help (new button% [parent hp-help] [label "Help"]
[callback (thunk* (help "Regular expressions N:Printing N:Reading"))]))
(define hp1 (new horizontal-panel% [parent f]))
(define t1 (new text-field% [parent hp1] [label "Replace:"]))
(define cb1 (new check-box% [parent hp1] [label "protect?"]))
(define hp2 (new horizontal-panel% [parent f]))
(define t2 (new text-field% [parent hp2] [label "\tBy:"]))
(define cb2 (new check-box% [parent hp2] [label "protect?"]))
(define (ok-pressed b ev)
(send f show #f)
(define t1-re ((if (send cb1 get-value) regexp-quote pregexp)
(send t1 get-value)))
(define t2-re ((if (send cb2 get-value) regexp-replace-quote values)
(send t2 get-value)))
(define new-lines
(for/list ([line (regexp-split #rx"\n" str-in)])
(regexp-replace* t1-re line t2-re)))
(set! str-out (string-join new-lines "\n"))
)
(define (cancel-pressed b ev)
(send f show #f)
)
(gui-utils:ok/cancel-buttons f ok-pressed cancel-pressed)
(provide item-callback)
(define (item-callback str)
(set! str-in str)
(set! str-out #f)
(send t1 focus)
(send (send t1 get-editor) select-all)
(send f show #t)
str-out)