`with-editor-sequence`

I almost can't believe this isn't in the standard library, so here it is:

(define (call-with-editor-sequence editor f)
  (send editor begin-edit-sequence)
  (f)
  (send editor end-edit-sequence))

(require syntax/parse/define)

(define-syntax-parse-rule (with-editor-sequence editor:expr body:expr ...+)
  (call-with-editor-sequence editor (λ () body ...)))

Example use:

(define t (new text%))
(send t begin-edit-sequence)
(send t insert "a")
(send t insert "b")
(send t insert "c" 0 0)
(send t end-edit-sequence)
;; becomes
(with-editor-sequence t
  (send t insert "a")
  (send t insert "b")
  (send t insert "c" 0 0))

It would be natural to add support for the arguments to begin-edit-sequence. I opted not to do anything crazy (anaphoric or otherwise send*-like) with the editor because (in my case) I need to trigger the editor sequence on an editor that is held by an editor-canvas% subclass before operating on the subclass instance and nodes in its object graph, not the editor itself.

2 Likes

I wonder about exceptions / continuations, maybe using dynamic-wind would be better?
But I don't know the details about begin/end-edit-sequence and whether it would make sense in this context / in the context of your application.