• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • part
kateundo.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
3 Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
4 Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "kateundo.h"
22
23#include "katedocument.h"
24#include "kateview.h"
25#include "katecursor.h"
26
30 class KateUndo
31{
32 public:
41 KateUndo (KateUndoGroup::UndoType type, uint line, uint col, uint len, const TQString &text);
42
46 ~KateUndo ();
47
48 public:
55 bool isValid();
56
63 bool merge(KateUndo* u);
64
69 void undo (KateDocument *doc);
70
75 void redo (KateDocument *doc);
76
80 KateTextCursor cursorBefore() const;
81
85 KateTextCursor cursorAfter() const;
86
91 inline KateUndoGroup::UndoType type() const { return m_type; }
92
97 inline uint line () const { return m_line; }
98
103 inline uint col () const { return m_col; }
104
109 inline uint len() const { return m_len; }
110
115 inline const TQString& text() const { return m_text; };
116
117 private:
121 KateUndoGroup::UndoType m_type;
122
126 uint m_line;
127
131 uint m_col;
132
136 uint m_len;
137
141 TQString m_text;
142};
143
144KateUndo::KateUndo (KateUndoGroup::UndoType type, uint line, uint col, uint len, const TQString &text)
145: m_type (type),
146 m_line (line),
147 m_col (col),
148 m_len (len),
149 m_text (text)
150{
151}
152
153KateUndo::~KateUndo ()
154{
155}
156
157bool KateUndo::isValid()
158{
159 if (m_type == KateUndoGroup::editInsertText || m_type == KateUndoGroup::editRemoveText)
160 if (len() == 0)
161 return false;
162
163 return true;
164}
165
166bool KateUndo::merge(KateUndo* u)
167{
168 if (m_type != u->type())
169 return false;
170
171 if (m_type == KateUndoGroup::editInsertText
172 && m_line == u->line()
173 && (m_col + m_len) == u->col())
174 {
175 m_text += u->text();
176 m_len += u->len();
177 return true;
178 }
179 else if (m_type == KateUndoGroup::editRemoveText
180 && m_line == u->line()
181 && m_col == (u->col() + u->len()))
182 {
183 m_text.prepend(u->text());
184 m_col = u->col();
185 m_len += u->len();
186 return true;
187 }
188
189 return false;
190}
191
192void KateUndo::undo (KateDocument *doc)
193{
194 if (m_type == KateUndoGroup::editInsertText)
195 {
196 doc->editRemoveText (m_line, m_col, m_len);
197 }
198 else if (m_type == KateUndoGroup::editRemoveText)
199 {
200 doc->editInsertText (m_line, m_col, m_text);
201 }
202 else if (m_type == KateUndoGroup::editWrapLine)
203 {
204 doc->editUnWrapLine (m_line, (m_text == "1"), m_len);
205 }
206 else if (m_type == KateUndoGroup::editUnWrapLine)
207 {
208 doc->editWrapLine (m_line, m_col, (m_text == "1"));
209 }
210 else if (m_type == KateUndoGroup::editInsertLine)
211 {
212 doc->editRemoveLine (m_line);
213 }
214 else if (m_type == KateUndoGroup::editRemoveLine)
215 {
216 doc->editInsertLine (m_line, m_text);
217 }
218 else if (m_type == KateUndoGroup::editMarkLineAutoWrapped)
219 {
220 doc->editMarkLineAutoWrapped (m_line, m_col == 0);
221 }
222}
223
224void KateUndo::redo (KateDocument *doc)
225{
226 if (m_type == KateUndoGroup::editRemoveText)
227 {
228 doc->editRemoveText (m_line, m_col, m_len);
229 }
230 else if (m_type == KateUndoGroup::editInsertText)
231 {
232 doc->editInsertText (m_line, m_col, m_text);
233 }
234 else if (m_type == KateUndoGroup::editUnWrapLine)
235 {
236 doc->editUnWrapLine (m_line, (m_text == "1"), m_len);
237 }
238 else if (m_type == KateUndoGroup::editWrapLine)
239 {
240 doc->editWrapLine (m_line, m_col, (m_text == "1"));
241 }
242 else if (m_type == KateUndoGroup::editRemoveLine)
243 {
244 doc->editRemoveLine (m_line);
245 }
246 else if (m_type == KateUndoGroup::editInsertLine)
247 {
248 doc->editInsertLine (m_line, m_text);
249 }
250 else if (m_type == KateUndoGroup::editMarkLineAutoWrapped)
251 {
252 doc->editMarkLineAutoWrapped (m_line, m_col == 1);
253 }
254}
255
256KateTextCursor KateUndo::cursorBefore() const
257{
258 if (m_type == KateUndoGroup::editInsertLine || m_type == KateUndoGroup::editUnWrapLine)
259 return KateTextCursor(m_line+1, m_col);
260 else if (m_type == KateUndoGroup::editRemoveText)
261 return KateTextCursor(m_line, m_col+m_len);
262
263 return KateTextCursor(m_line, m_col);
264}
265
266KateTextCursor KateUndo::cursorAfter() const
267{
268 if (m_type == KateUndoGroup::editRemoveLine || m_type == KateUndoGroup::editWrapLine)
269 return KateTextCursor(m_line+1, m_col);
270 else if (m_type == KateUndoGroup::editInsertText)
271 return KateTextCursor(m_line, m_col+m_len);
272
273 return KateTextCursor(m_line, m_col);
274}
275
276KateUndoGroup::KateUndoGroup (KateDocument *doc)
277: m_doc (doc),m_safePoint(false)
278{
279 m_items.setAutoDelete (true);
280}
281
282KateUndoGroup::~KateUndoGroup ()
283{
284}
285
286void KateUndoGroup::undo ()
287{
288 if (m_items.count() == 0)
289 return;
290
291 m_doc->editStart (false);
292
293 for (KateUndo* u = m_items.last(); u; u = m_items.prev())
294 u->undo(m_doc);
295
296 if (m_doc->activeView())
297 {
298 for (uint z=0; z < m_items.count(); z++)
299 if (m_items.at(z)->type() != KateUndoGroup::editMarkLineAutoWrapped)
300 {
301 m_doc->activeView()->editSetCursor (m_items.at(z)->cursorBefore());
302 break;
303 }
304 }
305
306 m_doc->editEnd ();
307}
308
309void KateUndoGroup::redo ()
310{
311 if (m_items.count() == 0)
312 return;
313
314 m_doc->editStart (false);
315
316 for (KateUndo* u = m_items.first(); u; u = m_items.next())
317 u->redo(m_doc);
318
319 if (m_doc->activeView())
320 {
321 for (uint z=0; z < m_items.count(); z++)
322 if (m_items.at(z)->type() != KateUndoGroup::editMarkLineAutoWrapped)
323 {
324 m_doc->activeView()->editSetCursor (m_items.at(z)->cursorAfter());
325 break;
326 }
327 }
328
329 m_doc->editEnd ();
330}
331
332void KateUndoGroup::addItem (KateUndoGroup::UndoType type, uint line, uint col, uint len, const TQString &text)
333{
334 addItem(new KateUndo(type, line, col, len, text));
335}
336
337void KateUndoGroup::addItem(KateUndo* u)
338{
339 if (!u->isValid())
340 delete u;
341 else if (m_items.last() && m_items.last()->merge(u))
342 delete u;
343 else
344 m_items.append(u);
345}
346
347bool KateUndoGroup::merge(KateUndoGroup* newGroup,bool complex)
348{
349 if (m_safePoint) return false;
350 if (newGroup->isOnlyType(singleType()) || complex) {
351 // Take all of its items first -> last
352 KateUndo* u = newGroup->m_items.take(0);
353 while (u) {
354 addItem(u);
355 u = newGroup->m_items.take(0);
356 }
357 if (newGroup->m_safePoint) safePoint();
358 return true;
359 }
360 return false;
361}
362
363void KateUndoGroup::safePoint (bool safePoint) {
364 m_safePoint=safePoint;
365}
366
367KateUndoGroup::UndoType KateUndoGroup::singleType()
368{
369 KateUndoGroup::UndoType ret = editInvalid;
370
371 for (KateUndo* u = m_items.first(); u; u = m_items.next()) {
372 if (ret == editInvalid)
373 ret = u->type();
374 else if (ret != u->type())
375 return editInvalid;
376 }
377
378 return ret;
379}
380
381bool KateUndoGroup::isOnlyType(KateUndoGroup::UndoType type)
382{
383 if (type == editInvalid) return false;
384
385 for (KateUndo* u = m_items.first(); u; u = m_items.next())
386 if (u->type() != type)
387 return false;
388
389 return true;
390}
KateTextCursor
Simple cursor class with no document pointer.
Definition: katecursor.h:34
KateUndoGroup
Class to manage a group of undo items.
Definition: kateundo.h:34
KateUndoGroup::redo
void redo()
Redo the contained undo items.
Definition: kateundo.cpp:309
KateUndoGroup::~KateUndoGroup
~KateUndoGroup()
Destructor.
Definition: kateundo.cpp:282
KateUndoGroup::undo
void undo()
Undo the contained undo items.
Definition: kateundo.cpp:286
KateUndoGroup::addItem
void addItem(KateUndoGroup::UndoType type, uint line, uint col, uint len, const TQString &text)
add an item to the group
Definition: kateundo.cpp:332
KateUndoGroup::KateUndoGroup
KateUndoGroup(KateDocument *doc)
Constructor.
Definition: kateundo.cpp:276
KateUndoGroup::UndoType
UndoType
Types for undo items.
Definition: kateundo.h:63
KateUndoGroup::safePoint
void safePoint(bool safePoint=true)
set group as as savepoint.
Definition: kateundo.cpp:363
KateUndoGroup::merge
bool merge(KateUndoGroup *newGroup, bool complex)
merge this group with an other
Definition: kateundo.cpp:347
TDEStdAccel::redo
const TDEShortcut & redo()
TDEStdAccel::undo
const TDEShortcut & undo()

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kate

Skip menu "kate"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kate by doxygen 1.9.4
This website is maintained by Timothy Pearson.