Skip to content
Newer
Older
100644 188 lines (151 sloc) 4.67 KB
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
1 ;;; rudel-operations.el --- Rudel domain operations
2 ;;
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 2, 2010
3 ;; Copyright (C) 2009, 2010 Jan Moringen
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
4 ;;
5 ;; Author: Jan Moringen <scymtym@users.sourceforge.net>
6 ;; Keywords: Rudel, operations
7 ;; X-RCS: $Id:$
8 ;;
f2f18f2 * obby/rudel-obby.el (header): fixed license text
jan authored Jun 8, 2009
9 ;; This file is part of Rudel.
10 ;;
11 ;; Rudel is free software: you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15 ;;
f2f18f2 * obby/rudel-obby.el (header): fixed license text
jan authored Jun 8, 2009
16 ;; Rudel is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
20 ;;
21 ;; You should have received a copy of the GNU General Public License
f2f18f2 * obby/rudel-obby.el (header): fixed license text
jan authored Jun 8, 2009
22 ;; along with Rudel. If not, see <http://www.gnu.org/licenses>.
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
23
24
25 ;;; Commentary:
26 ;;
27 ;; This file contains classes representing operations like insertions
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 1, 2010
28 ;; and deletions, that are important for Rudel's domain of
29 ;; collaborative editing.
30 ;;
31 ;; Currently the following operations are available:
32 ;;
33 ;; + `rudel-operation' (abstract base class)
34 ;; + `rudel-insert-op'
35 ;; + `rudel-delete-op'
36 ;; + `rudel-move-cursor-op'
37 ;; + `rudel-move-selection-op'
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
38
39
40 ;;; History:
41 ;;
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 1, 2010
42 ;; 0.2 - Added cursor and selection operations
43 ;;
44 ;; 0.1 - Initial version
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
45
46
47 ;;; Code:
48 ;;
49
50 (require 'eieio)
51
52
53 ;;; Class rudel-operation
54 ;;
55
56 (defclass rudel-operation ()
57 ()
58 "Abstract base class for operations."
59 :abstract t)
60
61 (defgeneric rudel-apply ((this rudel-operation) object)
62 "Apply the change represented by THIS to OBJECT.")
63
64
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 1, 2010
65 ;;; Class rudel-point-operation
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
66 ;;
67
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 1, 2010
68 (defclass rudel-point-operation (rudel-operation)
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
69 ((from :initarg :from
70 :type (or null (integer 0))
71 :documentation
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 1, 2010
72 "Position affected by this operation or nil. nil means
73 end of buffer"))
74 ""
75 :abstract t)
76
77
78 ;;; Class rudel-range-operation
79 ;;
80
81 (defclass rudel-range-operation (rudel-operation)
82 ((from :initarg :from
83 :type (integer 0)
84 :documentation
85 "Start of the region affected by this operation.")
86 (to :initarg :to
87 :type (integer 0)
88 :documentation
89 "End of the region affected by this operation."))
90 ""
91 :abstract t)
92
93 (defmethod slot-missing ((this rudel-range-operation)
94 slot-name operation &optional new-value)
95 "Simulate slot :length"
96 (cond
97 ;; Slot :length
98 ((or (eq slot-name :length)
99 (eq slot-name 'length))
100 (with-slots (from to) this
101 (if (eq operation 'oref)
102 (- to from)
103 (setq to (+ from new-value)))))
104 ;; Call next method
105 (t (call-next-method)))
106 )
107
108
109 ;;; Class rudel-insert-op
110 ;;
111
112 ;; TODO should be rudel-insert but there is a method of the same name
113 (defclass rudel-insert-op (rudel-point-operation)
114 ((data :initarg :data
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
115 :type string
116 :documentation
117 "The inserted string."))
118 "Objects of this class represent insertion operations.")
119
120 (defmethod rudel-apply ((this rudel-insert-op) object)
121 "Apply THIS to OBJECT by inserting the associated data."
122 (with-slots (from data) this
123 (rudel-insert object from data)))
124
125 (defmethod slot-missing ((this rudel-insert-op)
126 slot-name operation &optional new-value)
127 "Simulate read-only slots :length and :to."
128 (cond
129 ;; Slot :length
130 ((and (or (eq slot-name :length)
131 (eq slot-name 'length))
132 (eq operation 'oref))
133 (with-slots (data) this
134 (length data)))
135 ;; Slot :to
136 ((and (or (eq slot-name :to)
137 (eq slot-name 'to))
138 (eq operation 'oref))
139 (with-slots (from length) this
140 (+ from length)))
141 ;; Call next method
142 (t (call-next-method)))
143 )
144
145
146 ;;; Class rudel-delete-op
147 ;;
148
149 ;; TODO should be rudel-delete but there is a method of the same name
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 1, 2010
150 (defclass rudel-delete-op (rudel-range-operation)
151 ()
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
152 "Objects of this class represent deletion operations.")
153
154 (defmethod rudel-apply ((this rudel-delete-op) object)
155 "Apply THIS to OBJECT by deleting the associated region."
156 (with-slots (from length) this
157 (rudel-delete object from length)))
158
789c675 @scymtym Added operation classes for point, range, cursor and selection
authored Feb 1, 2010
159
160 ;;; Class rudel-move-cursor-op
161 ;;
162
163 (defclass rudel-move-cursor-op (rudel-point-operation)
164 ()
165 "Objects of this class represent cursor movements.")
166
167 (defmethod rudel-apply ((this rudel-move-cursor-op) object)
168 "Apply THIS to OBJECT by changing the position of one user's cursor."
169 (with-slots (from) this
170 (rudel-move-cursor object from)))
171
172
173 ;;; Class rudel-move-selection-op
174 ;;
175
176 (defclass rudel-move-selection-op (rudel-range-operation)
177 ()
178 "Objects of this class represent changes of users'
179 selections.")
180
181 (defmethod rudel-apply ((this rudel-move-selection-op) object)
182 "Apply THIS to OBJECT by changing one user's selection."
183 (with-slots (from to) this
184 (rudel-move-selection object from to)))
5f857eb * obby/rudel-obby-server.el
jan authored Feb 2, 2009
185
186 (provide 'rudel-operations)
187 ;;; rudel-operations.el ends here
Something went wrong with that request. Please try again.