Changeset 7461


Ignore:
Timestamp:
Oct 17, 2007, 2:15:46 PM (14 years ago)
Author:
jaj
Message:

First pass at implementing undo. There is still no redo or grouping.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/cocoa-ide/cocoa-editor.lisp

    r7438 r7461  
    415415         (hi::*buffer-gap-context* (hi::buffer-gap-context buffer))
    416416         (font (buffer-active-font buffer))
    417          (document (#/document self)))
     417         (document (#/document self))
     418         (undo-mgr (and document (#/undoManager document))))
    418419    #+debug
    419420    (#_NSLog #@"insert: pos = %ld, n = %ld" :long pos :long n)
     
    425426      (ns:with-ns-range (replacerange pos 0)
    426427        (#/replaceCharactersInRange:withString:
    427          mirror replacerange replacestring)))
     428         mirror replacerange replacestring))
     429      (when (and undo-mgr (not (#/isUndoing undo-mgr)))
     430        (#/replaceCharactersAtPosition:length:withString:
     431         (#/prepareWithInvocationTarget: undo-mgr self)
     432         pos n #@"")))
    428433    (#/setAttributes:range: mirror font (ns:make-ns-range pos n))   
    429     (textstorage-note-insertion-at-position self pos n)
    430     ;; Arguably, changecount stuff should happen via the document's NSUndoManager.
    431     ;; At some point in time, we'll know whether or not we have and are using
    432     ;; an NSUndoManager; while we're in limbo about that, do it here.
    433     (#/updateChangeCount: document #$NSChangeDone)))
    434 
     434    (textstorage-note-insertion-at-position self pos n)))
    435435
    436436(objc:defmethod (#/noteHemlockDeletionAtPosition:length: :void) ((self hemlock-text-storage)
     
    439439                                                                 (extra :<NSI>nteger))
    440440  (declare (ignorable extra))
     441  #+debug
     442  (#_NSLog #@"delete: pos = %ld, n = %ld" :long pos :long n)
    441443  (ns:with-ns-range (range pos n)
    442     ;; It seems to be necessary to call #/edited:range:changeInLength: before
    443     ;; deleting from the mirror attributed string.  It's not clear whether this
    444     ;; is also true of insertions and modifications.
    445     (#/edited:range:changeInLength: self (logior #$NSTextStorageEditedCharacters
    446                                                  #$NSTextStorageEditedAttributes)
    447                                     range (- n))
    448     (#/deleteCharactersInRange: (#/mirror self) range))
    449   (let* ((display (hemlock-buffer-string-cache (#/hemlockString self))))
    450     (reset-buffer-cache display)
    451     (update-line-cache-for-index display pos))
    452   ;; Arguably, changecount stuff should happen via the document's NSUndoManager.
    453   ;; At some point in time, we'll know whether or not we have and are using
    454   ;; an NSUndoManager; while we're in limbo about that, do it here.
    455   (#/updateChangeCount: (#/document self) #$NSChangeDone))
    456  
     444    (let* ((mirror (#/mirror self))
     445           (deleted-string (#/substringWithRange: (#/string mirror) range))
     446           (document (#/document self))
     447           (undo-mgr (and document (#/undoManager document)))
     448           (display (hemlock-buffer-string-cache (#/hemlockString self))))
     449      ;; It seems to be necessary to call #/edited:range:changeInLength: before
     450      ;; deleting from the mirror attributed string.  It's not clear whether this
     451      ;; is also true of insertions and modifications.
     452      (#/edited:range:changeInLength: self (logior #$NSTextStorageEditedCharacters
     453                                                   #$NSTextStorageEditedAttributes)
     454                                      range (- n))
     455      (#/deleteCharactersInRange: mirror range)
     456      (when (and undo-mgr (not (#/isUndoing undo-mgr)))
     457        (#/replaceCharactersAtPosition:length:withString:
     458         (#/prepareWithInvocationTarget: undo-mgr self)
     459         pos 0 deleted-string))
     460      (reset-buffer-cache display)
     461      (update-line-cache-for-index display pos))))
    457462
    458463(objc:defmethod (#/noteHemlockModificationAtPosition:length: :void) ((self hemlock-text-storage)
     
    461466                                                                     (extra :<NSI>nteger))
    462467  (declare (ignorable extra))
    463   (let* ((hemlock-string (#/hemlockString self))
    464          (mirror (#/mirror self)))
    465     (ns:with-ns-range (range pos n)
     468  #+debug
     469  (#_NSLog #@"modify: pos = %ld, n = %ld" :long pos :long n)
     470  (ns:with-ns-range (range pos n)
     471    (let* ((hemlock-string (#/hemlockString self))
     472           (mirror (#/mirror self))
     473           (deleted-string (#/substringWithRange: (#/string mirror) range))
     474           (document (#/document self))
     475           (undo-mgr (and document (#/undoManager document))))
    466476      (#/replaceCharactersInRange:withString:
    467477       mirror range (#/substringWithRange: hemlock-string range))
    468478      (#/edited:range:changeInLength: self (logior #$NSTextStorageEditedCharacters
    469                                                    #$NSTextStorageEditedAttributes) range 0)))
    470   ;; Arguably, changecount stuff should happen via the document's NSUndoManager.
    471   ;; At some point in time, we'll know whether or not we have and are using
    472   ;; an NSUndoManager; while we're in limbo about that, do it here.
    473   (#/updateChangeCount: (#/document self) #$NSChangeDone))
    474 
     479                                                   #$NSTextStorageEditedAttributes) range 0)
     480      (when (and undo-mgr (not (#/isUndoing undo-mgr)))
     481        (#/replaceCharactersAtPosition:length:withString:
     482         (#/prepareWithInvocationTarget: undo-mgr self)
     483         pos n deleted-string)))))
    475484
    476485(objc:defmethod (#/noteHemlockAttrChangeAtPosition:length: :void) ((self hemlock-text-storage)
     
    603612      attrs)))
    604613
    605 000
     614(objc:defmethod (#/replaceCharactersAtPosition:length:withString: :void)
     615    ((self hemlock-text-storage) (pos <NSUI>nteger) (len <NSUI>nteger) string)
     616  (ns:with-ns-range (r pos len)
     617    (#/replaceCharactersInRange:withString: self r string)))
     618
    606619(objc:defmethod (#/replaceCharactersInRange:withString: :void)
    607620    ((self hemlock-text-storage) (r :<NSR>ange) string)
     
    14991512    ((textstorage :foreign-type :id))
    15001513  (:metaclass ns:+ns-object))
     1514
     1515(objc:defmethod (#/undoManager :<BOOL>) ((self echo-area-document))
     1516  nil) ;For now, undo is not supported for echo-areas
    15011517
    15021518(defmethod update-buffer-package ((doc echo-area-document) buffer)
Note: See TracChangeset for help on using the changeset viewer.