examples/regexp-replace.rkt
#lang racket/base
(require racket/gui framework/gui-utils)

;;; Laurent Orseau <laurent orseau gmail com> -- 2012-04-23

;; Performs a (extended) regexp-replace* on the selection.
;; The "from" and "to" patterns are asked in a dialog box.
;; If protect? is checked, the "from" pattern is regexp-quoted.
(provide item-callback)
(define (item-callback str) 
  (define str-out #f)
  (define f (new dialog% [label "Regexp Replace"]
                 [min-width 500]))
  (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
      ; apply the regexes only per line
      (for/list ([line (regexp-split #rx"\n" str)])
        (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)
  (send f show #t)
  str-out)

#|
(item-callback "See the manual in the Script/Help \s* menu for \nmore information.")
; for protect, test with \s* and \1
;|#