#lang scheme
(require scheme/gui/base)
(provide define/provide)
(define-syntax define/provide
(syntax-rules ()
[(_ (name args ... . l) body ...)
(begin (provide name)
(define (name args ... . l) body ...))]
[(_ name val)
(begin (provide name)
(define name val))]
))
(define/provide application-version-maj 3)
(define/provide application-version-min 10)
(define/provide application-version (format "~a.~a" application-version-maj application-version-min))
(define/provide application-name "MrEd Designer")
(define/provide application-name-version
(string-append application-name " " application-version))
(provide define/provide-mock)
(define-syntax-rule (define/provide-mock (name args ...) body ...)
(begin (provide name)
(define (name args ...)
(printf "~a: NOT IMPLEMENTED ; arg-list: ~a\n"
'name
(list args ...)))))
(define/provide debug #f)
(define/provide (set-debug dbg) (set! debug dbg))
(define/provide (debug-printf . r)
(when debug
(apply printf r)))
(define image-hash (make-hash))
(define/provide (image-file->bitmap filename)
(hash-ref! image-hash filename
(make-object bitmap% (build-path "images" filename))))
(define/provide (atom? val)
(or (number? val) (symbol? val) (string? val) (boolean? val)))
(define/provide (->string x)
(cond [(string? x) x]
[(number? x) (number->string x)]
[(symbol? x) (symbol->string x)]
[(path? x) (path->string x)]
[else (format "~a" x)]))
(define/provide to-string ->string)
(define/provide (string-append* . l)
(apply string-append (map ->string l)))
(define/provide (symbol-append* . l)
(string->unreadable-symbol (apply string-append* l)))
(define/provide (symbol->keyword sym)
(string->keyword (symbol->string sym)))
(define/provide (assoc-ref l key [default-val
(λ()(error "key not found in assoc-ref:" key))])
(let ([v (assoc key l)])
(if v
(second v)
(if (procedure? default-val)
(default-val)
default-val)
)))
(define/provide (assoc-remove lst id-list)
(cond
((null? lst) '())
((and (list? (car lst)) (member (caar lst) id-list))
(assoc-remove (cdr lst) id-list)
)
(else
(cons (car lst) (assoc-remove (cdr lst) id-list))
)
)
)
(define/provide (assoc-change lst old-id new-id)
(cond
((null? lst) '())
((and (list? (car lst)) (equal? (caar lst) old-id))
(cons (cons new-id (cdar lst)) (assoc-change (cdr lst) old-id new-id))
)
(else
(cons (car lst) (assoc-change (cdr lst) old-id new-id))
)
)
)
(define/provide (hash-keys-values h)
(match h
[(hash-table [keys vals] ...)
(values keys vals)]))
(define/provide (hash-keys h)
(match h
[(hash-table [keys values] ...)
keys]))
(define/provide (hash-values h)
(match h
[(hash-table [keys values] ...)
values]))
(define/provide (member? x l)
(if (member x l) #t #f))
(define/provide (list-pos l e)
(for/first ([x l]
[i (in-naturals)]
#:when (equal? e x))
i))
(define/provide (split-at-element l e [compare? equal?])
(split-at l (list-pos l e)))
(define/provide (list-move-left l e)
(let*-values ([(left right) (split-at-element l e)]
[(rleft) (reverse left)])
(if (empty? left)
l (append (reverse (rest rleft))
(list e)
(cons (first rleft) (rest right))))))
(define/provide (list-move-right l e)
(reverse (list-move-left (reverse l) e)))
(define/provide (text-split-with-empty str ch empty)
(let*
((idx (string-length str))
(last #f)
(slist '())
)
(do () ( (not (>= idx 0)) )
(set! last idx)
(do () ( (not (and (> idx 0)
(not (or (and (char? ch)
(char=? (string-ref str (- idx 1)) ch))
(and (list? ch)
(member (string-ref str (- idx 1)) ch))
)
)
)
) )
(set! idx (- idx 1))
)
(when (>= idx 0)
(when (or empty
(and (not empty) (> (- last idx) 0)) )
(set! slist (cons (substring str idx last) slist))
)
(set! idx (- idx 1))
)
)
slist
)
)
(define/provide (path-string->string pstr)
(if (path? pstr)
(path->string pstr)
pstr))
(define/provide (most-common-prefix l1 l2)
(let loop ([prefix '()]
[l1 l1]
[l2 l2])
(cond [(or (empty? l1) (empty? l2)
(not (equal? (first l1) (first l2))))
(values prefix l1 l2)]
[else (loop (append prefix (list (first l1)))
(rest l1)
(rest l2))])))
(define/provide (relative-path base-dir path)
(let ([lbase (explode-path (normal-case-path (simple-form-path base-dir)))]
[lpath (explode-path (normal-case-path (simple-form-path path)))])
(let-values ([(common rest-base rest-path)
(most-common-prefix lbase lpath)])
(apply build-path (append (map (λ _ 'up) rest-base) rest-path))
)))
(define/provide (write-path p)
(cons 'build-path
(map (λ(p-elt)(cond [(symbol? p-elt) (list 'quote p-elt)]
[(absolute-path? p-elt) (path->string p-elt)]
[else (path-element->string p-elt)]))
(explode-path p))))
(define/provide use-runtime-paths? (make-parameter #f))
(define/provide current-property-mred-id (make-parameter #f))
(define/provide (generate-begin-code codes)
(cond [(not codes) #f]
[(empty? codes) #f]
[(empty? (rest codes)) (first codes)]
[else (cons 'begin codes)]
))
(provide try)
(define-syntax try
(syntax-rules (catch finally)
[(try
try-body ...
(catch
[exn exn-handler] ...
)
(finally
final-body ...))
(with-handlers ([exn exn-handler] ...)
(with-handlers ([exn? (λ(e)final-body ...
(raise e))])
try-body ...))
]
[(try t ... (finally f ...))
(try t ... (catch) (finally f ...))]
))
(provide map-send)
(define-syntax map-send
(syntax-rules ()
[(_ (arg ...) l)
(map (λ(x)(send x arg ...)) l)]
[(_ id l)
(map (λ(x)(send x id)) l)]
))
(provide for-each-send)
(define-syntax for-each-send
(syntax-rules ()
[(_ (arg ...) l)
(for-each (λ(x)(send x arg ...)) l)]
[(_ id l)
(for-each (λ(x)(send x id)) l)]
))
(define-for-syntax (->string x)
(cond [(syntax? x) (->string (syntax->datum x))]
[else (format "~a" x)]
))
(define-for-syntax (symbol-append* . args)
(string->symbol
(apply string-append (map ->string args))))
(provide getter)
(define-syntax (getter stx)
(syntax-case stx ()
[(_ id)
(with-syntax ([get-id (symbol-append* "get-" #'id)])
#'(define/public (get-id) id))]
[(_ id1 id2 ...)
#'(begin (getter id1)
(getter id2 ...))]
))
(provide setter)
(define-syntax (setter stx)
(syntax-case stx ()
[(_ id)
(with-syntax ([get-id (symbol-append* "set-" #'id)])
#'(define/public (get-id val) (set! id val))
)]
[(_ id1 id2 ...)
#'(begin (setter id1)
(setter id2 ...))]
))
(provide getter/setter)
(define-syntax-rule (getter/setter arg ...)
(begin (getter arg ...)
(setter arg ...)))
(define/provide (close-window tlw)
(when (send tlw can-close?)
(send tlw on-close)
(send tlw show #f)))