(module install mzscheme
(require (lib "util.ss" "planet")
(lib "list.ss")
(lib "plt-match.ss")
(prefix preferences: "diva-preferences.ss"))
(define (print-installation-finished-msg)
(printf "DivaScheme should now be installed.~n~nTo finish the installation, please restart DrScheme.~nOnce restarted, F4 will toggle DivaScheme on and off."))
(define (get-planet-package-installed-versions owner pkg)
(cond
[(assoc owner (current-cache-contents))
=> (lambda (d-owner)
(cond
[(assoc pkg (rest d-owner)) => rest]
[else '()]))]
[else '()]))
(define (package-version-installed? owner package major minor)
(cond
[(assoc major (get-planet-package-installed-versions owner package))
=>
(lambda (major&minors)
(cond
[(member minor (second major&minors)) #t]
[else #f]))]
[else #f]))
(define (no-newer-package-version? owner package major minor)
(let ([major&minors (get-planet-package-installed-versions owner package)])
(let loop ([major&minors major&minors])
(cond
[(empty? major&minors) #t]
[else
(let ([installed-major (first (first major&minors))]
[installed-minors (second (first major&minors))])
(cond
[(< installed-major major)
(loop (rest major&minors))]
[(= installed-major major)
(<= (apply max installed-minors) minor)]
[else #f]))]))))
(define (list-replace* lst x y)
(let loop ([lst lst])
(cond
[(empty? lst) lst]
[(equal? (first lst) x)
(cons y (loop (rest lst)))]
[else (cons (first lst)
(loop (rest lst)))])))
(define (update-2.1->2.2)
(define (has-no-contextual-square-open?)
(and (member '("[" "diva:open")
(preferences:get-command-mode-bindings))
(member '("[" "diva:open-square")
(preferences:get-insert-mode-bindings))))
(define (control-open-square-defined?)
(cond
[(assoc "c:[" (preferences:get-command-mode-bindings)) #t]
[else #f]))
(define (eligible-for-contextual-square-keybinding-update?)
(and (no-newer-package-version? "divascheme" "divascheme.plt" 1 1)
(package-version-installed? "divascheme" "divascheme.plt" 1 0)
(has-no-contextual-square-open?)))
(define (upgrade-preferences-with-magic-square-keybinding)
(preferences:set-command-mode-bindings
(list-replace* (preferences:get-command-mode-bindings)
'("[" "diva:open")
'("[" "diva:open-square/contextual")))
(preferences:set-insert-mode-bindings
(list-replace* (preferences:get-insert-mode-bindings)
'("[" "diva:open-square")
'("[" "diva:open-square/contextual"))))
(define (upgrade-preferences-with-control-open-square)
(preferences:set-command-mode-bindings
(cons '("c:[" "diva:open-square")
(preferences:get-command-mode-bindings)))
(preferences:set-insert-mode-bindings
(cons '("c:[" "diva:open-square")
(preferences:get-insert-mode-bindings))))
(define (print-open-square/contextual-upgraded)
(printf "We've automatically updated your DivaScheme's keymap~nto assign '[' to diva:open-square/contextual.~nSee http://list.cs.brown.edu/pipermail/plt-scheme/2006-April/012537.html~nfor details.~n~n"))
(when (eligible-for-contextual-square-keybinding-update?)
(upgrade-preferences-with-magic-square-keybinding)
(print-open-square/contextual-upgraded))
(when (not (control-open-square-defined?))
(upgrade-preferences-with-control-open-square)))
(update-2.1->2.2)
(print-installation-finished-msg))