(module mred-state mzscheme
(require (lib "class.ss")
(lib "etc.ss")
(lib "struct.ss")
(lib "mred.ss" "mred")
(lib "errortrace-lib.ss" "errortrace")
"dot-processing.ss"
"utilities.ss"
"structures.ss"
"rope.ss")
(provide MrEd-state%)
(define diva-debug false)
(define (diva-printf text . args)
(when diva-debug
(apply printf text args)))
(define ((a title) a)
(diva-printf "A: ~a: ~a~n" title a)
a)
(define (b b)
(diva-printf "B: ~a ~n" b)
b)
(define MrEd-state%
(class object%
(super-instantiate ())
(init diva-message-init window-text-init)
(define diva-message- diva-message-init)
(define window-text window-text-init)
(define (diva-message text . args)
(apply diva-message- text args))
(define/public (critical-error exn)
(let ([err-msg (format "DivaScheme Error: ~a" exn)])
(print-error-trace (current-error-port) exn)
(diva-message err-msg))
(raise exn))
(define/public (error-message str)
(and str (diva-message str)))
(define/public (message str)
(diva-message str))
(define/public (get-rope)
(send window-text diva:-get-rope))
(define/public (update-text rope)
(send window-text diva:-update-text rope))
(define/public (get-cursor-position)
(index->pos (send window-text get-start-position)))
(define/public (set-cursor-position pos)
(send window-text diva:set-selection-position (pos->index pos)))
(define/public (set-selection pos len)
(if (<= 0 len)
(begin (send window-text set-position
(pos->index pos)
(+ len (pos->index pos))
#f #f 'local)
(send window-text scroll-to-position
(pos->index pos)
#f
(+ len (pos->index pos))
'start))
(set-selection (+ pos len) (- len))))
(define/public (get-selection-len)
(let ([start-pos (send window-text get-start-position)]
[end-pos (send window-text get-end-position)])
(- end-pos start-pos)))
(define/public (get-mark-position)
(index->pos (send window-text diva:-get-mark-start-position)))
(define/public (get-mark-length)
(let ([mark-start-pos (send window-text diva:-get-mark-start-position)]
[mark-end-pos (send window-text diva:-get-mark-end-position)])
(- mark-end-pos mark-start-pos)))
(define/public (set-mark pos len)
(if (>= len 0)
(send window-text diva:-set-mark (pos->index pos) (+ (pos->index pos) len))
(set-mark (+ pos len) (- len))))
(define/public (update-world world)
(update-world-path
(update-world-mark
(update-world-select
(update-world-text world)))))
(define (update-world-path world)
(copy-struct World world
[World-path (send window-text get-filename)]))
(define/public (update-world-text world)
(cond
[(rope=? (World-rope world) (get-rope))
(copy-struct World world
[World-rope (get-rope)])]
[else
(copy-struct World world
[World-rope (get-rope)]
[World-syntax-list/lazy #f])]))
(define (update-world-select world)
(copy-struct World world
[World-cursor-position (get-cursor-position)]
[World-selection-length (get-selection-len)]))
(define (update-world-mark world)
(copy-struct World world
[World-mark-position (get-mark-position)]
[World-mark-length (get-mark-length)]))
(define/public (update-mred world)
(local
( (define (update-mred-text world)
(unless (rope=? (World-rope world) (get-rope))
(update-text (World-rope world)))
world)
(define (select-mred world)
(set-selection (World-cursor-position world) (World-selection-length world))
world)
(define (mark-mred world)
(set-mark (World-mark-position world) (World-mark-length world))
world))
(select-mred
(mark-mred
(update-mred-text world))))))))