summaryrefslogtreecommitdiffstats
path: root/emacs.d/lisp/rudel/jupiter/jupiter-insert.el
blob: 339270ea4af7088b9aebaba65f27a2006cdaebc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
;;; jupiter-insert.el --- Jupiter insert operation
;;
;; Copyright (C) 2009 Jan Moringen
;;
;; Author: Jan Moringen <scymtym@users.sourceforge.net>
;; Keywords: jupiter, operation, insert
;; X-RCS: $Id:$
;;
;; This file is part of Rudel.
;;
;; Rudel is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; Rudel is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Rudel. If not, see <http://www.gnu.org/licenses>.


;;; Commentary:
;;
;; Class `jupiter-insert' implements an insert operation for the
;; Jupiter algorithm.


;;; History:
;;
;; 0.1 - Initial revision


;;; Code:
;;

(require 'eieio)

(require 'rudel-operations)
(require 'jupiter-operation)


;;; Class jupiter-insert
;;

(defclass jupiter-insert (jupiter-operation
			  rudel-insert-op)
  ()
  "Objects of this class represent insertions into buffers.")

(defmethod jupiter-transform ((this jupiter-insert) other)
  "Transform OTHER using THIS."
  (cond

   ;;
   ;; Transform an insert operation
   ;;
   ((jupiter-insert-p other)
    (with-slots ((this-from   :from)
		 (this-to     :to)
		 (this-length :length)
		 (this-data   :data)) this
      (with-slots ((other-from   :from)
		   (other-to     :to)
		   (other-length :length)
		   (other-data   :data)) other
	(cond
	 ;;
	 ;; <other>
	 ;;         <this>
	 ;; No need to do anything in this case.
	 ;; ((< other-from this-from))

	 ;;
	 ;;        <other>
	 ;; <this>
	 ;;
	 ((> other-from this-from)
	  (incf other-from this-length))

	 ;;
	 ;; <other>
	 ;; <this>
	 ;; OTHER inserted at the same start position as we did. Since
	 ;; the situation is symmetric in the location properties of
	 ;; OTHER and THIS, we use the inserted data to construct an
	 ;; ordering.
	 ((= other-from this-from)
	  (when (string< this-data other-data)
	    (incf other-from this-length)))))))

   ;;
   ;; Transform a delete operation
   ;;
   ((jupiter-delete-p other)
    (with-slots ((this-from   :from)
		 (this-to     :to)
		 (this-length :length)) this
      (with-slots ((other-from   :from)
		   (other-to     :to)
		   (other-length :length)) other
	(cond

	 ;;
	 ;; <other>
	 ;;         <this>
	 ;; just keep OTHER

	 ;;
	 ;; <other> and   <other> and        <other>
	 ;; <this>      <this>        <this>
	 ((>= other-from this-from)
	  (incf other-from this-length)
	  (incf other-to   this-length))

	 ;;
	 ;; <  other  >
	 ;;   <this>
	 ;; OTHER deleted a region that includes the point at which THIS
	 ;; inserted in its interior. OTHER has to be split into one
	 ;; deletion before and one delete after the inserted data.
	 ((and (< other-from this-from) (> other-to this-to))
	  (setq other
		(jupiter-compound "compound"
	         :children (list (jupiter-delete "delete-left"
				  :from other-from
				  :to   this-from)
				 (jupiter-delete "delete-right"
				  :from this-to
				  :to   (+ other-to this-length))))))
	 ))))

   ;;
   ;; Transform a compound operation
   ;;
   ((jupiter-compound-p other) ;; TODO encapsulation violation
    (with-slots (children) other
      (dolist (child children)
	(setf child (jupiter-transform this child)))))

   ;;
   ;; Transform a nop operation
   ;;
   ((jupiter-nop-p other))

   ;; TODO this is for debugging
   (t (error "Cannot transform operation of type `%s'"
	     (object-class other))))
  other)

(defmethod object-print ((this jupiter-insert) &rest strings)
  "Add from, to, length and data to string representation of THIS."
  (with-slots (from to length data) this
    (call-next-method
     this
     (format " from %d" from)
     (format " to %d" to)
     (format " length %d" length)
     (format " data \"%s\"" data)))
  )

(provide 'jupiter-insert)
;;; jupiter-insert.el ends here