1 /*
2  * Copyright (c) 2004 by Alexander V. Lukyanov (lav@yars.free.net)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include "edit.h"
20 #include <stdlib.h>
21 
22 class Undo
23 {
24    struct GroupHead
25    {
26       offs pos;
27       num stdcol;
28       offs block_begin;
29       offs block_end;
30       bool block_hide;
31 
32       GroupHead();
33       void Undo();
34    };
35    class Change
36    {
37       friend class Undo;
38       unsigned group;
39       GroupHead *group_head;
40       Change *next;
41       Change *prev;
42       bool Join(const Change *);
43    protected:
44       enum type_t { INSERT, DELETE, REPLACE } type;
45       offs pos;
46       char *left;
47       num left_size;
48       char *right;
49       num right_size;
50       bool old_modified;
51    public:
52       Change(type_t t,const char *l,num ls,const char *r,num rs);
~Change()53       ~Change() { free(left); free(right); }
GetSize()54       size_t GetSize()
55 	 {
56 	    return (left?left_size:0)
57 		  +(right?right_size:0)
58 		  +(group_head?sizeof(*group_head):0)
59 		  +sizeof(Change);
60 	 }
61       void Undo();
62       void Redo();
63    };
64 
65    Change *chain_head;
66    Change *chain_ptr;
67    Change *chain_tail;
68    int group_open;
69    unsigned current_group;
70    GroupHead *group_head;
71 
72    bool locked;
73    bool enabled;
74 
75    time_t last_change_time;
76 
77    bool glue_changes;
78    int glue_max_time;
79    num max_size;
80    int min_groups;
81 
82    void CheckSize();
83 
84 public:
85    struct Delete : public Change {
DeleteDelete86       Delete(const char *l,num ls,const char *r,num rs) : Change(DELETE,l,ls,r,rs) {} };
87    struct Insert : public Change {
InsertInsert88       Insert(const char *l,num ls,const char *r,num rs) : Change(INSERT,l,ls,r,rs) {} };
89    struct Replace : public Change {
ReplaceReplace90       Replace(const char *l,num ls,const char *r,num rs) : Change(REPLACE,l,ls,r,rs) {} };
91 
92    void BeginUndoGroup();
93    void AddChange(Change *);
94    void EndUndoGroup();
95 
96    void UndoGroup();
97    void RedoGroup();
98    void UndoOne();
99    void RedoOne();
100    void FileSaved();
101 
102    void Clear();
103 
104    Undo();
105    ~Undo();
106 
Enabled()107    bool Enabled() { return enabled&&!locked; }
Locked()108    bool Locked()  { return locked; }
109 
SetMaxSize(num ms)110    void SetMaxSize(num ms) { max_size=ms; }
SetMinGroups(int mg)111    void SetMinGroups(int mg) { min_groups=mg; }
SetEnable(bool en)112    void SetEnable(bool en) { if(enabled!=en) Clear(); enabled=en; }
SetGlue(bool g)113    void SetGlue(bool g) { glue_changes=g; }
114 };
115 
116 extern Undo undo;
117