File: editorundo.c

package info (click to toggle)
manedit 0.8.1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,488 kB
  • ctags: 2,910
  • sloc: ansic: 49,339; cpp: 3,562; sh: 690; makefile: 80
file content (434 lines) | stat: -rw-r--r-- 9,564 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <gtk/gtk.h>

#include "../include/string.h"

#include "editor.h"
#include "editorop.h"
#include "editorundo.h"

#include "manedit.h"


gpointer EditorUndoNew(
	editor_struct *editor, editor_undo_type type,
	const gchar *comment
);
void EditorUndoDelete(editor_struct *editor, gpointer u);

static editor_undo_insert_chars_struct *EditorUndoApplyRemove(
	editor_undo_remove_chars_struct *u_remove
);
static editor_undo_remove_chars_struct *EditorUndoApplyInsert(
	editor_undo_insert_chars_struct *u_insert
);

gint EditorUndoDoApply(
	editor_struct *editor,
	gpointer **in_list, gint *in_total,
	gpointer **out_list, gint *out_total,
	gint max
);


#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))


/*
 *	Allocates a new undo structure with the given type and initial
 *	values.
 */
gpointer EditorUndoNew(
	editor_struct *editor, editor_undo_type type,
	const gchar *comment
)
{
	gint len = 0;
	gpointer u = NULL;

	switch(type)
	{
	  case EDITOR_UNDO_REMOVE_CHARS:
	    len = sizeof(editor_undo_remove_chars_struct);
	    break;

	  case EDITOR_UNDO_INSERT_CHARS:
	    len = sizeof(editor_undo_insert_chars_struct);
	    break;
	}

	if(len > 0)
	{
	    u = g_malloc0(len);
	    if(u != NULL)
	    {
		editor_undo_common_struct *common = EDITOR_UNDO_COMMON(u);

		/* Set common member values */
		common->type = type;
		common->comment = (comment != NULL) ?
		    g_strdup(comment) : NULL;
		common->editor_ptr = editor;
	    }
	}

	return(u);
}

/*
 *	Deletes the undo structure and all its allocated resources.
 */
void EditorUndoDelete(editor_struct *editor, gpointer u)
{
	editor_undo_remove_chars_struct *u_remove;
	editor_undo_insert_chars_struct *u_insert;
 	editor_undo_common_struct *u_common;

	if(u == NULL)
	    return;

	/* Delete type specific data */
	switch(EDITOR_UNDO_COMMON(u)->type)
	{
	  case EDITOR_UNDO_REMOVE_CHARS:
	    u_remove = EDITOR_UNDO_REMOVE_CHARS(u);
	    break;

	  case EDITOR_UNDO_INSERT_CHARS:
	    u_insert = EDITOR_UNDO_INSERT_CHARS(u);
	    g_free(u_insert->characters);
	    break;
	}

	/* Delete common members */
	u_common = EDITOR_UNDO_COMMON(u);
	g_free(u_common->comment);

	g_free(u);
}



/*
 *	Procedure to remove characters (undo insert characters).
 *
 *	Returns an inverse insert characters operation or NULL on error.
 *
 *	Given input undo structure will be deleted and should not be
 * 	referenced again after this call.
 */
static editor_undo_insert_chars_struct *EditorUndoApplyRemove(
	editor_undo_remove_chars_struct *u_remove
)
{
	editor_undo_insert_chars_struct *u_insert = NULL;
	editor_struct *editor;
	GtkEditable *editable;
	GtkText *text;
	gchar *text_buf = NULL;


	if(u_remove == NULL)
	    return(u_insert);

	editor = EDITOR(u_remove->editor_ptr);
	editable = (GtkEditable *)u_remove->w;
	text = (GtkText *)u_remove->w;
	if((editor == NULL) || (editable == NULL))
	    return(u_insert);

	/* Get characters from editable that we're about to remove */
	if(u_remove->length > 0)
	    text_buf = gtk_editable_get_chars(
		editable,
		u_remove->position,
		u_remove->position + u_remove->length
	    );

	/* Create inverse operation structure and set values */
	u_insert = EDITOR_UNDO_INSERT_CHARS(EditorUndoNew(
	    editor, EDITOR_UNDO_INSERT_CHARS,
	    u_remove->comment
	));
	if(u_insert != NULL)
	{
	    u_insert->w = GTK_WIDGET(editable);

	    u_insert->position = u_remove->position;
	    u_insert->length = (text_buf != NULL) ?
		u_remove->length : 0;

	    g_free(u_insert->characters);
	    u_insert->characters = (text_buf != NULL) ?
		g_strdup(text_buf) : NULL;
	}
	g_free(text_buf);
	text_buf = NULL;

	/* Now remove the characters */
	if(use_text_delete)
	{
	    gtk_text_freeze(text);
	    gtk_text_set_point(text, (guint)u_remove->position);
	    gtk_text_forward_delete(text, (guint)u_remove->length);
	    gtk_text_thaw(text);
	}
	else
	{
	    gtk_text_freeze(text);
	    gtk_editable_delete_text(
		editable,
		u_remove->position,
		u_remove->position + u_remove->length
	    );
	    gtk_text_thaw(text);
	}

	/* Delete the input structure */
	EditorUndoDelete(editor, u_remove);
	u_remove = NULL;

	return(u_insert);
}

/*
 *      Procedure to insert characters (undo remove characters).
 *
 *      Returns an inverse remove characters operation or NULL on error.
 * 
 *      Given input undo structure will be deleted and should not be
 *      referenced again after this call.
 */
static editor_undo_remove_chars_struct *EditorUndoApplyInsert(
	editor_undo_insert_chars_struct *u_insert
)
{
	editor_undo_remove_chars_struct *u_remove = NULL;
	editor_struct *editor;
	GtkEditable *editable;

	if(u_insert == NULL)
	    return(u_remove);

	editor = EDITOR(u_insert->editor_ptr);
	editable = (GtkEditable *)u_insert->w;
	if((editor == NULL) || (editable == NULL))
	    return(u_remove);

	/* Insert characters */
	if((u_insert->length > 0) && (u_insert->characters != NULL))
	{
	    medit_core_struct *core = editor->core;
	    medit_styles_list_struct *styles_list = &core->styles_list;
	    GtkStyle *style_ptr = styles_list->edit_text_standard;

	    gtk_text_set_point(GTK_TEXT(editable), u_insert->position);

	    gtk_text_freeze(GTK_TEXT(editable));
	    gtk_text_insert(
		GTK_TEXT(editable),
		(style_ptr != NULL) ? style_ptr->font : NULL,
		NULL,		/* Foreground color */
		NULL,		/* Background color */
		u_insert->characters,
		u_insert->length
	    );
	    gtk_text_thaw(GTK_TEXT(editable));
	    gtk_editable_select_region(
		editable,
		u_insert->position,
		u_insert->position + u_insert->length
	    );
	    gtk_text_set_point(
		GTK_TEXT(editable),
		u_insert->position + u_insert->length
	    );

	    /* Need to perform syntax highlighting, because the insert
	     * callback is not called.
	     */
	    EditorSyntaxHighlight(
		editor, GTK_WIDGET(editable),
		u_insert->position, u_insert->position,
		u_insert->position + u_insert->length
	    );
	}

	/* Create inverse operation structure and set values */
	u_remove = EDITOR_UNDO_REMOVE_CHARS(EditorUndoNew(
	    editor, EDITOR_UNDO_REMOVE_CHARS,
	    u_insert->comment
	));
	if(u_remove != NULL)
	{
	    u_remove->w = GTK_WIDGET(editable);

	    u_remove->position = u_insert->position;
	    u_remove->length = u_insert->length;
	}

	/* Delete the input structure */
	EditorUndoDelete(editor, u_insert);
	u_insert = NULL;

	return(u_remove);
}


/*
 *	Procedure to apply the first undo item in the in_list and then
 *	then convert it into the inverse operation and transfer it to
 *	the out_list.
 *
 *	If highest item on out_list would exceed the given max then
 *	it will be deleted.
 *
 *	Returns non-zero on error.
 */
gint EditorUndoDoApply(
	editor_struct *editor,
	gpointer **in_list, gint *in_total,
	gpointer **out_list, gint *out_total,
	gint max
)
{
	gint i, n;
	gpointer u_in, u_inverse = NULL;


	if((editor == NULL) || (in_list == NULL) || (in_total == NULL))
	    return(-1);

	/* Nothing to do? */
	if((*in_list == NULL) || (*in_total < 1))
	    return(0);


	/* Get first undo operation from input list, newest undo
	 * operation is always the first in the list
	 */
	u_in = (*in_list)[0];

	/* Reduce and shift input list */
	*in_total = (*in_total) - 1;
	for(i = 0; i < *in_total; i++)
	    (*in_list)[i] = (*in_list)[i + 1];

	if(*in_total > 0)
	{
	    *in_list = (gpointer *)g_realloc(
		*in_list, (*in_total) * sizeof(gpointer)
	    );
	    if(*in_list == NULL)
		*in_total = 0;
	}
	else
	{
	    g_free(*in_list);
	    *in_list = NULL;
	    *in_total = 0;
	}



	/* Handle input undo operation by its type, note that the
	 * given input undo structure will be deleted
	 */
	switch(EDITOR_UNDO_COMMON(u_in)->type)
	{
	  case EDITOR_UNDO_REMOVE_CHARS:
	    u_inverse = EditorUndoApplyRemove(
		EDITOR_UNDO_REMOVE_CHARS(u_in)
	    );
	    u_in = NULL;
	    break;

	  case EDITOR_UNDO_INSERT_CHARS:
	    u_inverse = EditorUndoApplyInsert(
		EDITOR_UNDO_INSERT_CHARS(u_in)
	    );
	    u_in = NULL;
	    break;
	}

	/* Undo not processed? */
	if(u_in != NULL)
	{
	    EditorUndoDelete(editor, u_in);
	    u_in = NULL;
	}



	/* Did we get an inverse operation? */
	if(u_inverse != NULL)
	{
	    /* Add it to the out list */
	    if((out_list != NULL) && (out_total != NULL))
	    {
		if(*out_total < 0)
		   *out_total = 0;

		*out_total = (*out_total) + 1;
		*out_list = (gpointer *)g_realloc(
		    *out_list,
		    (*out_total) * sizeof(gpointer)
		);
		if(*out_list == NULL)
		{
		    *out_total = 0;

		    EditorUndoDelete(editor, u_inverse);
		    u_inverse = NULL;
		}
		else
		{
		    /* Shift */
		    for(i = (*out_total) - 1; i > 0; i--)
			(*out_list)[i] = (*out_list)[i - 1];

		    /* Record newest undo inverse structure as first item */
		    (*out_list)[0] = u_inverse;
		    u_inverse = NULL;
		}

		/* Delete older undo structures on the out list */
		if(*out_total > max)
		{
		    n = MAX(max, 0);

		    /* Delete all undo structures greater than the max */
		    for(i = (*out_total) - 1; i >= n; i--)
			EditorUndoDelete(editor, (*out_list)[i]);

		    (*out_total) = n;
		    if((*out_total) > 0)
		    {
			(*out_list) = (gpointer*)g_realloc(
			    *out_list,
			    (*out_total) * sizeof(gpointer)
			);
			if((*out_list) == NULL)
			    (*out_total) = 0;
		    }
		    else
		    {
			g_free(*out_list);
			(*out_list) = NULL;
			(*out_total) = 0;
		    }
		}
	    }
	    else
	    {
		/* No out list available, delete inverse operation
		 * structure.
		 */
		EditorUndoDelete(editor, u_inverse);
		u_inverse = NULL;
	    }
	}

	return(0);
}