minara selection – nearly there

;; Too long! Decompose!

(define (update-selection-transform buffer sel x y)
(let-values (((prev-from prev-to)
(sexp-before buffer (picking-hit-from sel)))
((next-from next-to)
(sexp-after buffer (picking-hit-to sel))))
(let ((offset 0)
(start-index 0)
(prev-symbol (sexp-symbol-string buffer prev-from))
(next-symbol (sexp-symbol-string buffer next-from)))
;; What if it's a rotate but preceded by a translate? Later.
(if (not (and (string= prev-symbol "translate")
(string= next-symbol "pop-matrix")))
(let ((prefix
(format
#f
"(push-matrix)~%(translate~a ~a)~%" x y))
(suffix "(pop-matrix)\n"))
;; Wrap the selection in a new transform
(buffer-insert-undoable buffer
(picking-hit-to sel)
suffix)
(buffer-insert-undoable buffer
(picking-hit-from sel)
prefix)
(set! start-index
(+ (picking-hit-index sel)
1))
(set! offset (+ (string-length prefix)
(string-length suffix))))
;; else
(let* ((new-translate (format #f "(translate ~a ~a)"
(+ x old-x)
(+ y old-y))))
;; Update the existing transform very inefficiently
;; Delete the current transform
;; Insert new transform
(buffer-insert-undoable buffer
(- (picking-hit-from sel)
1)
new-translate)
(set! offset (- (string-length new-translate)
(- prev-from
prev-to))))
(set! start-index
(picking-hit-index sel)))

;; Roll the other selections down or up
(update-selection-ranges (selections-var buffer)
start-index
offset))))

Posted in Generative Art