2005-10-01 [paul] 1.9.14cvs64
[claws.git] / src / undo.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /* code ported from gedit */
21 /* This is for my patient girlfirend Regina */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <glib.h>
28
29 #include <string.h> /* for strlen */
30 #include <stdlib.h> /* for mbstowcs */
31
32 #include "undo.h"
33 #include "utils.h"
34 #include "prefs_common.h"
35
36 typedef struct _UndoInfo UndoInfo;
37
38 struct _UndoInfo 
39 {
40         UndoAction action;
41         gchar *text;
42         gint start_pos;
43         gint end_pos;
44         gfloat window_position;
45         gint mergeable;
46 };
47
48 static void undo_free_list      (GList         **list_pointer);
49 static void undo_check_size     (UndoMain       *undostruct);
50 static gint undo_merge          (GList          *list,
51                                  guint           start_pos,
52                                  guint           end_pos,
53                                  gint            action,
54                                  const guchar   *text);
55 static void undo_add            (const gchar    *text,
56                                  gint            start_pos,
57                                  gint            end_pos,
58                                  UndoAction      action,
59                                  UndoMain       *undostruct);
60 static gint undo_get_selection  (GtkTextView    *textview,
61                                  guint          *start,
62                                  guint          *end);
63 static void undo_insert_text_cb (GtkTextBuffer  *textbuf,
64                                  GtkTextIter    *iter,
65                                  gchar          *new_text,
66                                  gint           new_text_length,
67                                  UndoMain       *undostruct);
68 static void undo_delete_text_cb (GtkTextBuffer  *textbuf,
69                                  GtkTextIter    *start,
70                                  GtkTextIter    *end,
71                                  UndoMain       *undostruct);
72
73 static void undo_paste_clipboard_cb     (GtkTextView    *textview,
74                                          UndoMain       *undostruct);
75
76 void undo_undo                  (UndoMain       *undostruct);
77 void undo_redo                  (UndoMain       *undostruct);
78
79
80 UndoMain *undo_init(GtkWidget *text) 
81 {
82         UndoMain *undostruct;
83         GtkTextView *textview = GTK_TEXT_VIEW(text); 
84         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
85
86         g_return_val_if_fail(text != NULL, NULL);
87
88         undostruct = g_new(UndoMain, 1);
89         undostruct->textview = textview;
90         undostruct->undo = NULL;
91         undostruct->redo = NULL;
92         undostruct->paste = 0;
93         undostruct->undo_state = FALSE;
94         undostruct->redo_state = FALSE;
95
96         g_signal_connect(G_OBJECT(textbuf), "insert-text",
97                          G_CALLBACK(undo_insert_text_cb), undostruct);
98         g_signal_connect(G_OBJECT(textbuf), "delete-range",
99                          G_CALLBACK(undo_delete_text_cb), undostruct);
100         g_signal_connect(G_OBJECT(textview), "paste-clipboard",
101                          G_CALLBACK(undo_paste_clipboard_cb), undostruct);
102
103         return undostruct;
104 }
105
106 void undo_destroy (UndoMain *undostruct) 
107 {
108         undo_free_list(&undostruct->undo);
109         undo_free_list(&undostruct->redo);
110         g_free(undostruct);
111 }
112
113 static UndoInfo *undo_object_new(gchar *text, gint start_pos, gint end_pos, 
114                                  UndoAction action, gfloat window_position) 
115 {
116         UndoInfo *undoinfo;
117         undoinfo = g_new (UndoInfo, 1);
118         undoinfo->text      = text;
119         undoinfo->start_pos = start_pos;
120         undoinfo->end_pos   = end_pos;
121         undoinfo->action    = action;
122         undoinfo->window_position = window_position;
123         return undoinfo;
124 }
125
126 static void undo_object_free(UndoInfo *undo) 
127 {
128         g_free (undo->text);
129         g_free (undo);
130 }
131
132 /**
133  * undo_free_list:
134  * @list_pointer: list to be freed
135  *
136  * frees and undo structure list
137  **/
138 static void undo_free_list(GList **list_pointer) 
139 {
140         UndoInfo *undo;
141         GList *cur, *list = *list_pointer;
142
143         if (list == NULL) return;
144
145         for (cur = list; cur != NULL; cur = cur->next) {
146                 undo = (UndoInfo *)cur->data;
147                 undo_object_free(undo);
148         }
149
150         g_list_free(list);
151         *list_pointer = NULL;
152 }
153
154 void undo_set_change_state_func(UndoMain *undostruct, UndoChangeStateFunc func,
155                                 gpointer data)
156 {
157         g_return_if_fail(undostruct != NULL);
158
159         undostruct->change_state_func = func;
160         undostruct->change_state_data = data;
161 }
162
163 /**
164  * undo_check_size:
165  * @compose: document to check
166  *
167  * Checks that the size of compose->undo does not excede settings->undo_levels and
168  * frees any undo level above sett->undo_level.
169  *
170  **/
171 static void undo_check_size(UndoMain *undostruct) 
172 {
173         UndoInfo *last_undo;
174         guint length;
175
176         if (prefs_common.undolevels < 1) return;
177
178         /* No need to check for the redo list size since the undo
179            list gets freed on any call to compose_undo_add */
180         length = g_list_length(undostruct->undo);
181         if (length >= prefs_common.undolevels && prefs_common.undolevels > 0) {
182                 last_undo = (UndoInfo *)g_list_last(undostruct->undo)->data;
183                 undostruct->undo = g_list_remove(undostruct->undo, last_undo);
184                 undo_object_free(last_undo);
185         }
186 }
187
188 /**
189  * undo_merge:
190  * @last_undo:
191  * @start_pos:
192  * @end_pos:
193  * @action:
194  *
195  * This function tries to merge the undo object at the top of
196  * the stack with a new set of data. So when we undo for example
197  * typing, we can undo the whole word and not each letter by itself
198  *
199  * Return Value: TRUE is merge was sucessful, FALSE otherwise
200  **/
201 static gint undo_merge(GList *list, guint start_pos, guint end_pos,
202                        gint action, const guchar *text) 
203 {
204         guchar *temp_string;
205         UndoInfo *last_undo;
206
207         /* This are the cases in which we will NOT merge :
208            1. if (last_undo->mergeable == FALSE)
209            [mergeable = FALSE when the size of the undo data was not 1.
210            or if the data was size = 1 but = '\n' or if the undo object
211            has been "undone" already ]
212            2. The size of text is not 1
213            3. If the new merging data is a '\n'
214            4. If the last char of the undo_last data is a space/tab
215            and the new char is not a space/tab ( so that we undo
216            words and not chars )
217            5. If the type (action) of undo is different from the last one
218            Chema */
219
220         if (list == NULL) return FALSE;
221
222         last_undo = list->data;
223
224         if (!last_undo->mergeable) return FALSE;
225
226         if (end_pos - start_pos != 1 ||
227             text[0] == '\n' ||
228             action != last_undo->action ||
229             action == UNDO_ACTION_REPLACE_INSERT ||
230             action == UNDO_ACTION_REPLACE_DELETE) {
231                 last_undo->mergeable = FALSE;
232                 return FALSE;
233         }
234
235         if (action == UNDO_ACTION_DELETE) {
236                 gboolean checkit = TRUE;
237
238                 if (last_undo->start_pos != end_pos &&
239                     last_undo->start_pos != start_pos) {
240                         last_undo->mergeable = FALSE;
241                         return FALSE;
242                 } else if (last_undo->start_pos == start_pos) {
243                         /* Deleted with the delete key */
244                         if (text[0] != ' ' && text[0] != '\t' &&
245                             (last_undo->text[last_undo->end_pos - last_undo->start_pos - 1] == ' ' ||
246                              last_undo->text[last_undo->end_pos - last_undo->start_pos - 1] == '\t'))
247                                 checkit = FALSE;
248
249                         temp_string = g_strdup_printf("%s%s", last_undo->text, text);
250                         last_undo->end_pos++;
251                         g_free(last_undo->text);
252                         last_undo->text = temp_string;
253                 } else {
254                         /* Deleted with the backspace key */
255                         if (text[0] != ' ' && text[0] != '\t' &&
256                             (last_undo->text[0] == ' ' ||
257                              last_undo->text[0] == '\t'))
258                                 checkit = FALSE;
259
260                         temp_string = g_strdup_printf("%s%s", text, last_undo->text);
261                         last_undo->start_pos = start_pos;
262                         g_free(last_undo->text);
263                         last_undo->text = temp_string;
264                 }
265
266                 if (!checkit) {
267                         last_undo->mergeable = FALSE;
268                         return FALSE;
269                 }
270         } else if (action == UNDO_ACTION_INSERT) {
271                 if (last_undo->end_pos != start_pos) {
272                         last_undo->mergeable = FALSE;
273                         return FALSE;
274                 } else {
275                         temp_string = g_strdup_printf("%s%s", last_undo->text, text);
276                         g_free(last_undo->text);
277                         last_undo->end_pos = end_pos;
278                         last_undo->text = temp_string;
279                 }
280         } else
281                 debug_print("Unknown action [%i] inside undo merge encountered", action);
282
283         return TRUE;
284 }
285
286 /**
287  * compose_undo_add:
288  * @text:
289  * @start_pos:
290  * @end_pos:
291  * @action: either UNDO_ACTION_INSERT or UNDO_ACTION_DELETE
292  * @compose:
293  * @view: The view so that we save the scroll bar position.
294  *
295  * Adds text to the undo stack. It also performs test to limit the number
296  * of undo levels and deltes the redo list
297  **/
298
299 static void undo_add(const gchar *text, 
300                      gint start_pos, gint end_pos,
301                      UndoAction action, UndoMain *undostruct) 
302 {
303         UndoInfo *undoinfo;
304         GtkAdjustment *vadj;
305
306         g_return_if_fail(text != NULL);
307         g_return_if_fail(end_pos >= start_pos);
308
309         undo_free_list(&undostruct->redo);
310
311         /* Set the redo sensitivity */
312         undostruct->change_state_func(undostruct,
313                                       UNDO_STATE_UNCHANGED, UNDO_STATE_FALSE,
314                                       undostruct->change_state_data);
315
316         if (undostruct->paste != 0) {
317                 if (action == UNDO_ACTION_INSERT) 
318                         action = UNDO_ACTION_REPLACE_INSERT;
319                 else 
320                         action = UNDO_ACTION_REPLACE_DELETE;
321                 undostruct->paste = undostruct->paste + 1;
322                 if (undostruct->paste == 3) 
323                         undostruct->paste = 0;
324         }
325
326         if (undo_merge(undostruct->undo, start_pos, end_pos, action, text))
327                 return;
328
329         undo_check_size(undostruct);
330
331         vadj = GTK_ADJUSTMENT(GTK_TEXT_VIEW(undostruct->textview)->vadjustment);
332         undoinfo = undo_object_new(g_strdup(text), start_pos, end_pos, action,
333                                    vadj->value);
334
335         if (end_pos - start_pos != 1 || text[0] == '\n')
336                 undoinfo->mergeable = FALSE;
337         else
338                 undoinfo->mergeable = TRUE;
339
340         undostruct->undo = g_list_prepend(undostruct->undo, undoinfo);
341
342         undostruct->change_state_func(undostruct,
343                                       UNDO_STATE_TRUE, UNDO_STATE_UNCHANGED,
344                                       undostruct->change_state_data);
345 }
346
347 /**
348  * undo_undo:
349  * @w: not used
350  * @data: not used
351  *
352  * Executes an undo request on the current document
353  **/
354 void undo_undo(UndoMain *undostruct) 
355 {
356         UndoInfo *undoinfo;
357         GtkTextView *textview;
358         GtkTextBuffer *buffer;
359         GtkTextIter iter, start_iter, end_iter;
360         GtkTextMark *mark;
361
362         g_return_if_fail(undostruct != NULL);
363
364         if (undostruct->undo == NULL) return;
365
366         /* The undo data we need is always at the top op the
367            stack. So, therefore, the first one */
368         undoinfo = (UndoInfo *)undostruct->undo->data;
369         g_return_if_fail(undoinfo != NULL);
370         undoinfo->mergeable = FALSE;
371         undostruct->redo = g_list_prepend(undostruct->redo, undoinfo);
372         undostruct->undo = g_list_remove(undostruct->undo, undoinfo);
373
374         textview = undostruct->textview;
375         buffer = gtk_text_view_get_buffer(textview);
376
377         undo_block(undostruct);
378
379         /* Check if there is a selection active */
380         mark = gtk_text_buffer_get_insert(buffer);
381         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
382         gtk_text_buffer_place_cursor(buffer, &iter);
383
384         /* Move the view (scrollbars) to the correct position */
385         gtk_adjustment_set_value
386                 (GTK_ADJUSTMENT(textview->vadjustment),
387                  undoinfo->window_position);
388
389         switch (undoinfo->action) {
390         case UNDO_ACTION_DELETE:
391                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, undoinfo->start_pos);
392                 gtk_text_buffer_insert(buffer, &iter, undoinfo->text, -1);
393                 break;
394         case UNDO_ACTION_INSERT:
395                 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, undoinfo->start_pos);
396                 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, undoinfo->end_pos);
397                 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
398                 break;
399         case UNDO_ACTION_REPLACE_INSERT:
400                 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, undoinfo->start_pos);
401                 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, undoinfo->end_pos);
402                 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
403                 /* "pull" another data structure from the list */
404                 undoinfo = (UndoInfo *)undostruct->undo->data;
405                 g_return_if_fail(undoinfo != NULL);
406                 undostruct->redo = g_list_prepend(undostruct->redo, undoinfo);
407                 undostruct->undo = g_list_remove(undostruct->undo, undoinfo);
408                 g_return_if_fail(undoinfo->action == UNDO_ACTION_REPLACE_DELETE);
409                 gtk_text_buffer_insert(buffer, &start_iter, undoinfo->text, -1);
410                 break;
411         case UNDO_ACTION_REPLACE_DELETE:
412                 g_warning("This should not happen. UNDO_REPLACE_DELETE");
413                 break;
414         default:
415                 g_assert_not_reached();
416                 break;
417         }
418
419         undostruct->change_state_func(undostruct,
420                                       UNDO_STATE_UNCHANGED, UNDO_STATE_TRUE,
421                                       undostruct->change_state_data);
422
423         if (undostruct->undo == NULL)
424                 undostruct->change_state_func(undostruct,
425                                               UNDO_STATE_FALSE,
426                                               UNDO_STATE_UNCHANGED,
427                                               undostruct->change_state_data);
428
429         undo_unblock(undostruct);
430 }
431
432 /**
433  * undo_redo:
434  * @w: not used
435  * @data: not used
436  *
437  * executes a redo request on the current document
438  **/
439 void undo_redo(UndoMain *undostruct) 
440 {
441         UndoInfo *redoinfo;
442         GtkTextView *textview;
443         GtkTextBuffer *buffer;
444         GtkTextIter iter, start_iter, end_iter;
445         GtkTextMark *mark;
446
447         g_return_if_fail(undostruct != NULL);
448
449         if (undostruct->redo == NULL) return;
450
451         redoinfo = (UndoInfo *)undostruct->redo->data;
452         g_return_if_fail (redoinfo != NULL);
453         undostruct->undo = g_list_prepend(undostruct->undo, redoinfo);
454         undostruct->redo = g_list_remove(undostruct->redo, redoinfo);
455
456         textview = undostruct->textview;
457         buffer = gtk_text_view_get_buffer(textview);
458
459         undo_block(undostruct);
460
461         /* Check if there is a selection active */
462         mark = gtk_text_buffer_get_insert(buffer);
463         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
464         gtk_text_buffer_place_cursor(buffer, &iter);
465
466         /* Move the view to the right position. */
467         gtk_adjustment_set_value(textview->vadjustment, 
468                                  redoinfo->window_position);
469
470         switch (redoinfo->action) {
471         case UNDO_ACTION_INSERT:
472                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, redoinfo->start_pos);
473                 gtk_text_buffer_insert(buffer, &iter, redoinfo->text, -1);
474                 break;
475         case UNDO_ACTION_DELETE:
476                 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, redoinfo->start_pos);
477                 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, redoinfo->end_pos);
478                 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
479                 break;
480         case UNDO_ACTION_REPLACE_DELETE:
481                 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, redoinfo->start_pos);
482                 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, redoinfo->end_pos);
483                 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
484                 debug_print("UNDO_ACTION_REPLACE %s\n", redoinfo->text);
485                 /* "pull" another data structure from the list */
486                 redoinfo = (UndoInfo *)undostruct->redo->data;
487                 g_return_if_fail(redoinfo != NULL);
488                 undostruct->undo = g_list_prepend(undostruct->redo, redoinfo);
489                 undostruct->redo = g_list_remove(undostruct->undo, redoinfo);
490                 g_return_if_fail(redoinfo->action == UNDO_ACTION_REPLACE_DELETE);
491                 gtk_text_buffer_insert(buffer, &start_iter, redoinfo->text, -1);
492                 break;
493         case UNDO_ACTION_REPLACE_INSERT:
494                 g_warning("This should not happen. Redo: UNDO_REPLACE_INSERT");
495                 break;
496         default:
497                 g_assert_not_reached();
498                 break;
499         }
500
501         undostruct->change_state_func(undostruct,
502                                       UNDO_STATE_TRUE, UNDO_STATE_UNCHANGED, 
503                                       undostruct->change_state_data);
504
505         if (undostruct->redo == NULL)
506                 undostruct->change_state_func(undostruct,
507                                               UNDO_STATE_UNCHANGED,
508                                               UNDO_STATE_FALSE,
509                                               undostruct->change_state_data);
510
511         undo_unblock(undostruct);
512 }
513
514 void undo_block(UndoMain *undostruct)
515 {
516         GtkTextBuffer *buffer;
517
518         g_return_if_fail(GTK_IS_TEXT_VIEW(undostruct->textview));
519
520         buffer = gtk_text_view_get_buffer(undostruct->textview);
521         g_signal_handlers_block_by_func(buffer, undo_insert_text_cb, undostruct);
522         g_signal_handlers_block_by_func(buffer, undo_delete_text_cb, undostruct);
523         g_signal_handlers_block_by_func(buffer, undo_paste_clipboard_cb,
524                                           undostruct);
525 }
526
527 void undo_unblock(UndoMain *undostruct)
528 {
529         GtkTextBuffer *buffer;
530
531         g_return_if_fail(GTK_IS_TEXT_VIEW(undostruct->textview));
532
533         buffer = gtk_text_view_get_buffer(undostruct->textview);
534         g_signal_handlers_unblock_by_func(buffer, undo_insert_text_cb, undostruct);
535         g_signal_handlers_unblock_by_func(buffer, undo_delete_text_cb, undostruct);
536         g_signal_handlers_unblock_by_func(buffer, undo_paste_clipboard_cb,
537                                           undostruct);
538 }
539
540 void undo_insert_text_cb(GtkTextBuffer *textbuf, GtkTextIter *iter,
541                          gchar *new_text, gint new_text_length,
542                          UndoMain *undostruct) 
543 {
544         gchar *text_to_insert;
545         gint pos;
546
547         if (prefs_common.undolevels <= 0) return;
548
549         pos = gtk_text_iter_get_offset(iter);
550
551         Xstrndup_a(text_to_insert, new_text, new_text_length, return);
552         undo_add(text_to_insert, pos, pos + g_utf8_strlen(text_to_insert, -1),
553                  UNDO_ACTION_INSERT, undostruct);
554 }
555
556 void undo_delete_text_cb(GtkTextBuffer *textbuf, GtkTextIter *start,
557                          GtkTextIter *end, UndoMain *undostruct) 
558 {
559         gchar *text_to_delete;
560         gint start_pos, end_pos;
561
562         if (prefs_common.undolevels <= 0) return;
563
564         text_to_delete = gtk_text_buffer_get_text(textbuf, start, end, FALSE);
565         if (!text_to_delete || !*text_to_delete) return;
566
567         start_pos = gtk_text_iter_get_offset(start);
568         end_pos   = gtk_text_iter_get_offset(end);
569
570         undo_add(text_to_delete, start_pos, end_pos, UNDO_ACTION_DELETE,
571                  undostruct);
572         g_free(text_to_delete);
573 }
574
575 void undo_paste_clipboard_cb(GtkTextView *textview, UndoMain *undostruct)
576 {
577 #if 0
578         if (editable->clipboard_text == NULL) return;
579 #endif
580
581         if (prefs_common.undolevels > 0)
582                 if (undo_get_selection(textview, NULL, NULL))
583                         undostruct->paste = TRUE;
584 }
585
586 /**
587  * undo_get_selection:
588  * @text: Text to get the selection from
589  * @start: return here the start position of the selection
590  * @end: return here the end position of the selection
591  *
592  * Gets the current selection for View
593  *
594  * Return Value: TRUE if there is a selection active, FALSE if not
595  **/
596 static gint undo_get_selection(GtkTextView *textview, guint *start, guint *end) 
597 {
598         GtkTextBuffer *buffer;
599         GtkTextIter start_iter, end_iter;
600         guint start_pos, end_pos;
601
602         buffer = gtk_text_view_get_buffer(textview);
603         gtk_text_buffer_get_selection_bounds(buffer, &start_iter, &end_iter);
604
605         start_pos = gtk_text_iter_get_offset(&start_iter);
606         end_pos   = gtk_text_iter_get_offset(&end_iter);
607
608         /* The user can select from end to start too. If so, swap it*/
609         if (end_pos < start_pos) {
610                 guint swap_pos;
611                 swap_pos  = end_pos;
612                 end_pos   = start_pos;
613                 start_pos = swap_pos;
614         }
615
616         if (start != NULL)
617                 *start = start_pos;
618                 
619         if (end != NULL)
620                 *end = end_pos;
621
622         if ((start_pos > 0 || end_pos > 0) && (start_pos != end_pos))
623                 return TRUE;
624         else
625                 return FALSE;
626 }