script for redo/undo text?

David Epstein dfepstein at comcast.net
Wed Jul 10 19:59:26 EDT 2019


I cannot help with the project of "unlimited" undo/redo, but a simple one-layer undo for fields is not hard to implement.  

Sean Cole's suggestion that we should record the field state whenever a key is pressed I think does more than is wanted; if you type 10 characters, you don't want to choose Undo 10 times to get rid of them.  

My approach is to record the field state on keypresses ONLY if there is a text selection (about to be lost when the key is pressed) rather than an insertion point.  If text has been added but no text has been lost, you don’t need an Undo command to remove what was added.  If you also record the field's state before anything is typed in it, and before the Undo command itself is executed, and before any scripted handlers change the field, I think this will resemble ordinary Word Processor behavior.

on keyDown
  if the selectedText is not empty then uStoreFieldState the short id of the selectedField
  pass keyDown
end keyDown
-- and a similar handler for the other keys Sean mentions, although I don't think for arrow keys.  

on uStoreFieldState fID -- fID is the short id of the field
  global u
  put the selectedChunk into myChunk
  put fID into u["fieldModified"]
  put the htmlText of fld id fID into u[fID]
  put word 2 of myChunk into u["chunkA"]
  put word 4 of myChunk into u["chunkB"]
end uStoreFieldState

Then on undo, you read the values of u to revise the field, and reload u with the state of the field just before undoing.  I store the chunk information so that undo can restore the selection as well as the text to its prior state.

David Epstein



More information about the use-livecode mailing list