;;; -*- mode: Lisp; Syntax: Common-Lisp; -*- ;;; ;;; Copyright (c) 2009 by the authors. ;;; ;;; See LICENCE for details. (in-package :hu.dwim.projectional-editor) ;;;;;; ;;; Operation API ;;; ;;; An operation represents a change in domain data, selections or any other editor state. (def (generic e) operation? (object) (:documentation "Returns TRUE if OBJECT is an operation, otherwise returns FALSE. Purely functional.")) (def (generic e) redo-operation (operation document) (:documentation "Redoes all side effects of OPERATION on DOCUMENT. Has side effects on DOCUMENT.")) (def (generic e) undo-operation (operation document) (:documentation "Undoes all side effects of OPERATION on DOCUMENT. Has side effects on DOCUMENT.")) ;;;;;; ;;; Operation classes (def class* operation-tree () ((operations :type sequence)) (:documentation "Represents the operations that were executed.")) (def class* operation () () (:documentation "Base class for operations.")) (def class* operation/compound (operation) ((elements :type sequence)) (:documentation "A sequence of operations carried out in the order they appear in elements.")) (def class* operation/quit (operation) () (:documentation "An operation that quits the editor.")) (def class* operation/undo (operation) () (:documentation "An operation that undoes the effect of the last operation.")) (def class* operation/replace-selection (operation) ((selection :type selection)) (:documentation "An operation that replaces the selection of a document.")) ;;;;;; ;;; Operation constructors (def (function e) make-operation/compound (elements) (make-instance 'operation/compound :elements elements)) (def (function e) make-operation/quit () (make-instance 'operation/quit)) (def (function e) make-operation/undo () (make-instance 'operation/undo)) (def (function e) make-operation/replace-selection (selection) (make-instance 'operation/replace-selection :selection selection)) ;;;;;; ;;; Operation API implementation (def method operation? (object) (typep object 'operation)) (def method redo-operation ((operation operation/compound) document) (iter (for element :in-sequence (elements-of operation)) (redo-operation element document))) (def method redo-operation ((operation operation/quit) document) (throw :quit-editor nil)) (def method redo-operation ((operation operation/undo) document) (not-yet-implemented)) (def method redo-operation ((operation operation/replace-selection) document) (setf (selection-of document) (selection-of operation)))