2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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.
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.
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.
20 /* code ported from gedit */
21 /* This is for my patient girlfirend Regina */
29 #include <string.h> /* for strlen */
30 #include <stdlib.h> /* for mbstowcs */
34 #include "prefs_common.h"
36 typedef struct _UndoInfo UndoInfo;
44 gfloat window_position;
48 static void undo_free_list (GList **list_pointer);
49 static void undo_check_size (UndoMain *undostruct);
50 static gint undo_merge (GList *list,
55 static void undo_add (const gchar *text,
59 UndoMain *undostruct);
60 static gint undo_get_selection (GtkTextView *textview,
63 static void undo_insert_text_cb (GtkTextBuffer *textbuf,
67 UndoMain *undostruct);
68 static void undo_delete_text_cb (GtkTextBuffer *textbuf,
71 UndoMain *undostruct);
73 static void undo_paste_clipboard_cb (GtkTextView *textview,
74 UndoMain *undostruct);
76 void undo_undo (UndoMain *undostruct);
77 void undo_redo (UndoMain *undostruct);
80 UndoMain *undo_init(GtkWidget *text)
83 GtkTextView *textview = GTK_TEXT_VIEW(text);
84 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
86 g_return_val_if_fail(text != NULL, NULL);
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;
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);
106 void undo_destroy (UndoMain *undostruct)
108 undo_free_list(&undostruct->undo);
109 undo_free_list(&undostruct->redo);
113 static UndoInfo *undo_object_new(gchar *text, gint start_pos, gint end_pos,
114 UndoAction action, gfloat window_position)
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;
126 static void undo_object_free(UndoInfo *undo)
134 * @list_pointer: list to be freed
136 * frees and undo structure list
138 static void undo_free_list(GList **list_pointer)
141 GList *cur, *list = *list_pointer;
143 if (list == NULL) return;
145 for (cur = list; cur != NULL; cur = cur->next) {
146 undo = (UndoInfo *)cur->data;
147 undo_object_free(undo);
151 *list_pointer = NULL;
154 void undo_set_change_state_func(UndoMain *undostruct, UndoChangeStateFunc func,
157 g_return_if_fail(undostruct != NULL);
159 undostruct->change_state_func = func;
160 undostruct->change_state_data = data;
165 * @compose: document to check
167 * Checks that the size of compose->undo does not excede settings->undo_levels and
168 * frees any undo level above sett->undo_level.
171 static void undo_check_size(UndoMain *undostruct)
176 if (prefs_common.undolevels < 1) return;
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);
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
199 * Return Value: TRUE is merge was sucessful, FALSE otherwise
201 static gint undo_merge(GList *list, guint start_pos, guint end_pos,
202 gint action, const guchar *text)
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
220 if (list == NULL) return FALSE;
222 last_undo = list->data;
224 if (!last_undo->mergeable) return FALSE;
226 if (end_pos - start_pos != 1 ||
228 action != last_undo->action ||
229 action == UNDO_ACTION_REPLACE_INSERT ||
230 action == UNDO_ACTION_REPLACE_DELETE) {
231 last_undo->mergeable = FALSE;
235 if (action == UNDO_ACTION_DELETE) {
236 gboolean checkit = TRUE;
238 if (last_undo->start_pos != end_pos &&
239 last_undo->start_pos != start_pos) {
240 last_undo->mergeable = FALSE;
242 } else if (last_undo->start_pos == start_pos) {
243 /* Deleted with the delete key */
244 temp_string = g_strdup_printf("%s%s", last_undo->text, text);
245 last_undo->end_pos++;
246 g_free(last_undo->text);
247 last_undo->text = temp_string;
249 /* Deleted with the backspace key */
250 temp_string = g_strdup_printf("%s%s", text, last_undo->text);
251 last_undo->start_pos = start_pos;
252 g_free(last_undo->text);
253 last_undo->text = temp_string;
255 } else if (action == UNDO_ACTION_INSERT) {
256 if (last_undo->end_pos != start_pos) {
257 last_undo->mergeable = FALSE;
260 temp_string = g_strdup_printf("%s%s", last_undo->text, text);
261 g_free(last_undo->text);
262 last_undo->end_pos = end_pos;
263 last_undo->text = temp_string;
266 debug_print("Unknown action [%i] inside undo merge encountered", action);
276 * @action: either UNDO_ACTION_INSERT or UNDO_ACTION_DELETE
278 * @view: The view so that we save the scroll bar position.
280 * Adds text to the undo stack. It also performs test to limit the number
281 * of undo levels and deltes the redo list
284 static void undo_add(const gchar *text,
285 gint start_pos, gint end_pos,
286 UndoAction action, UndoMain *undostruct)
291 g_return_if_fail(text != NULL);
292 g_return_if_fail(end_pos >= start_pos);
294 undo_free_list(&undostruct->redo);
296 /* Set the redo sensitivity */
297 undostruct->change_state_func(undostruct,
298 UNDO_STATE_UNCHANGED, UNDO_STATE_FALSE,
299 undostruct->change_state_data);
301 if (undostruct->paste != 0) {
302 if (action == UNDO_ACTION_INSERT)
303 action = UNDO_ACTION_REPLACE_INSERT;
305 action = UNDO_ACTION_REPLACE_DELETE;
306 undostruct->paste = undostruct->paste + 1;
307 if (undostruct->paste == 3)
308 undostruct->paste = 0;
311 if (undo_merge(undostruct->undo, start_pos, end_pos, action, text))
314 undo_check_size(undostruct);
316 vadj = GTK_ADJUSTMENT(GTK_TEXT_VIEW(undostruct->textview)->vadjustment);
317 undoinfo = undo_object_new(g_strdup(text), start_pos, end_pos, action,
320 if (end_pos - start_pos != 1 || text[0] == '\n')
321 undoinfo->mergeable = FALSE;
323 undoinfo->mergeable = TRUE;
325 undostruct->undo = g_list_prepend(undostruct->undo, undoinfo);
327 undostruct->change_state_func(undostruct,
328 UNDO_STATE_TRUE, UNDO_STATE_UNCHANGED,
329 undostruct->change_state_data);
337 * Executes an undo request on the current document
339 void undo_undo(UndoMain *undostruct)
342 GtkTextView *textview;
343 GtkTextBuffer *buffer;
344 GtkTextIter iter, start_iter, end_iter;
347 g_return_if_fail(undostruct != NULL);
349 if (undostruct->undo == NULL) return;
351 /* The undo data we need is always at the top op the
352 stack. So, therefore, the first one */
353 undoinfo = (UndoInfo *)undostruct->undo->data;
354 g_return_if_fail(undoinfo != NULL);
355 undoinfo->mergeable = FALSE;
356 undostruct->redo = g_list_prepend(undostruct->redo, undoinfo);
357 undostruct->undo = g_list_remove(undostruct->undo, undoinfo);
359 textview = undostruct->textview;
360 buffer = gtk_text_view_get_buffer(textview);
362 undo_block(undostruct);
364 /* Check if there is a selection active */
365 mark = gtk_text_buffer_get_insert(buffer);
366 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
367 gtk_text_buffer_place_cursor(buffer, &iter);
369 /* Move the view (scrollbars) to the correct position */
370 gtk_adjustment_set_value
371 (GTK_ADJUSTMENT(textview->vadjustment),
372 undoinfo->window_position);
374 switch (undoinfo->action) {
375 case UNDO_ACTION_DELETE:
376 gtk_text_buffer_get_iter_at_offset(buffer, &iter, undoinfo->start_pos);
377 gtk_text_buffer_insert(buffer, &iter, undoinfo->text, -1);
379 case UNDO_ACTION_INSERT:
380 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, undoinfo->start_pos);
381 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, undoinfo->end_pos);
382 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
384 case UNDO_ACTION_REPLACE_INSERT:
385 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, undoinfo->start_pos);
386 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, undoinfo->end_pos);
387 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
388 /* "pull" another data structure from the list */
389 undoinfo = (UndoInfo *)undostruct->undo->data;
390 g_return_if_fail(undoinfo != NULL);
391 undostruct->redo = g_list_prepend(undostruct->redo, undoinfo);
392 undostruct->undo = g_list_remove(undostruct->undo, undoinfo);
393 g_return_if_fail(undoinfo->action == UNDO_ACTION_REPLACE_DELETE);
394 gtk_text_buffer_insert(buffer, &start_iter, undoinfo->text, -1);
396 case UNDO_ACTION_REPLACE_DELETE:
397 g_warning("This should not happen. UNDO_REPLACE_DELETE");
400 g_assert_not_reached();
404 undostruct->change_state_func(undostruct,
405 UNDO_STATE_UNCHANGED, UNDO_STATE_TRUE,
406 undostruct->change_state_data);
408 if (undostruct->undo == NULL)
409 undostruct->change_state_func(undostruct,
411 UNDO_STATE_UNCHANGED,
412 undostruct->change_state_data);
414 undo_unblock(undostruct);
422 * executes a redo request on the current document
424 void undo_redo(UndoMain *undostruct)
427 GtkTextView *textview;
428 GtkTextBuffer *buffer;
429 GtkTextIter iter, start_iter, end_iter;
432 g_return_if_fail(undostruct != NULL);
434 if (undostruct->redo == NULL) return;
436 redoinfo = (UndoInfo *)undostruct->redo->data;
437 g_return_if_fail (redoinfo != NULL);
438 undostruct->undo = g_list_prepend(undostruct->undo, redoinfo);
439 undostruct->redo = g_list_remove(undostruct->redo, redoinfo);
441 textview = undostruct->textview;
442 buffer = gtk_text_view_get_buffer(textview);
444 undo_block(undostruct);
446 /* Check if there is a selection active */
447 mark = gtk_text_buffer_get_insert(buffer);
448 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
449 gtk_text_buffer_place_cursor(buffer, &iter);
451 /* Move the view to the right position. */
452 gtk_adjustment_set_value(textview->vadjustment,
453 redoinfo->window_position);
455 switch (redoinfo->action) {
456 case UNDO_ACTION_INSERT:
457 gtk_text_buffer_get_iter_at_offset(buffer, &iter, redoinfo->start_pos);
458 gtk_text_buffer_insert(buffer, &iter, redoinfo->text, -1);
460 case UNDO_ACTION_DELETE:
461 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, redoinfo->start_pos);
462 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, redoinfo->end_pos);
463 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
465 case UNDO_ACTION_REPLACE_DELETE:
466 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, redoinfo->start_pos);
467 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, redoinfo->end_pos);
468 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
469 debug_print("UNDO_ACTION_REPLACE %s\n", redoinfo->text);
470 /* "pull" another data structure from the list */
471 redoinfo = (UndoInfo *)undostruct->redo->data;
472 g_return_if_fail(redoinfo != NULL);
473 undostruct->undo = g_list_prepend(undostruct->redo, redoinfo);
474 undostruct->redo = g_list_remove(undostruct->undo, redoinfo);
475 g_return_if_fail(redoinfo->action == UNDO_ACTION_REPLACE_DELETE);
476 gtk_text_buffer_insert(buffer, &start_iter, redoinfo->text, -1);
478 case UNDO_ACTION_REPLACE_INSERT:
479 g_warning("This should not happen. Redo: UNDO_REPLACE_INSERT");
482 g_assert_not_reached();
486 undostruct->change_state_func(undostruct,
487 UNDO_STATE_TRUE, UNDO_STATE_UNCHANGED,
488 undostruct->change_state_data);
490 if (undostruct->redo == NULL)
491 undostruct->change_state_func(undostruct,
492 UNDO_STATE_UNCHANGED,
494 undostruct->change_state_data);
496 undo_unblock(undostruct);
499 void undo_block(UndoMain *undostruct)
501 GtkTextBuffer *buffer;
503 g_return_if_fail(GTK_IS_TEXT_VIEW(undostruct->textview));
505 buffer = gtk_text_view_get_buffer(undostruct->textview);
506 g_signal_handlers_block_by_func(buffer, undo_insert_text_cb, undostruct);
507 g_signal_handlers_block_by_func(buffer, undo_delete_text_cb, undostruct);
508 g_signal_handlers_block_by_func(buffer, undo_paste_clipboard_cb,
512 void undo_unblock(UndoMain *undostruct)
514 GtkTextBuffer *buffer;
516 g_return_if_fail(GTK_IS_TEXT_VIEW(undostruct->textview));
518 buffer = gtk_text_view_get_buffer(undostruct->textview);
519 g_signal_handlers_unblock_by_func(buffer, undo_insert_text_cb, undostruct);
520 g_signal_handlers_unblock_by_func(buffer, undo_delete_text_cb, undostruct);
521 g_signal_handlers_unblock_by_func(buffer, undo_paste_clipboard_cb,
525 void undo_wrapping(UndoMain *undostruct, gboolean wrap)
527 debug_print("undo wrapping now %d\n", wrap);
528 undostruct->wrap = wrap;
531 void undo_insert_text_cb(GtkTextBuffer *textbuf, GtkTextIter *iter,
532 gchar *new_text, gint new_text_length,
533 UndoMain *undostruct)
535 gchar *text_to_insert;
537 if (prefs_common.undolevels <= 0) return;
539 pos = gtk_text_iter_get_offset(iter);
540 if (undostruct->wrap && undostruct->undo) {
541 UndoInfo *last_undo = undostruct->undo->data;
542 if (last_undo && last_undo->action == UNDO_ACTION_INSERT
543 && last_undo->start_pos < pos && last_undo->end_pos > pos) {
544 GtkTextIter start,end;
545 last_undo->end_pos += g_utf8_strlen(new_text, -1);
546 gtk_text_buffer_get_iter_at_offset(textbuf, &start, last_undo->start_pos);
547 gtk_text_buffer_get_iter_at_offset(textbuf, &end, last_undo->end_pos);
548 g_free(last_undo->text);
549 last_undo->text = gtk_text_buffer_get_text(textbuf, &start, &end, FALSE);
550 debug_print("add:undo upd %d-%d\n", last_undo->start_pos, last_undo->end_pos);
552 } else debug_print("add:last: %d, %d-%d (%d)\n", last_undo->action,
553 last_undo->start_pos, last_undo->end_pos, pos);
556 Xstrndup_a(text_to_insert, new_text, new_text_length, return);
557 debug_print("add:undo add %d-%ld\n", pos, pos + g_utf8_strlen(text_to_insert, -1));
558 undo_add(text_to_insert, pos, pos + g_utf8_strlen(text_to_insert, -1),
559 UNDO_ACTION_INSERT, undostruct);
562 void undo_delete_text_cb(GtkTextBuffer *textbuf, GtkTextIter *start,
563 GtkTextIter *end, UndoMain *undostruct)
565 gchar *text_to_delete;
566 gint start_pos, end_pos;
568 if (prefs_common.undolevels <= 0) return;
570 text_to_delete = gtk_text_buffer_get_text(textbuf, start, end, FALSE);
571 if (!text_to_delete || !*text_to_delete) return;
573 start_pos = gtk_text_iter_get_offset(start);
574 end_pos = gtk_text_iter_get_offset(end);
576 if (undostruct->wrap && undostruct->undo) {
577 UndoInfo *last_undo = undostruct->undo->data;
578 if (last_undo && last_undo->action == UNDO_ACTION_INSERT
579 && last_undo->start_pos < start_pos && last_undo->end_pos > end_pos) {
580 GtkTextIter start,end;
581 last_undo->end_pos -= g_utf8_strlen(text_to_delete, -1);
582 gtk_text_buffer_get_iter_at_offset(textbuf, &start, last_undo->start_pos);
583 gtk_text_buffer_get_iter_at_offset(textbuf, &end, last_undo->end_pos);
584 g_free(last_undo->text);
585 last_undo->text = gtk_text_buffer_get_text(textbuf, &start, &end, FALSE);
586 debug_print("del:undo upd %d-%d\n", last_undo->start_pos, last_undo->end_pos);
588 } else debug_print("del:last: %d, %d-%d (%d)\n", last_undo->action,
589 last_undo->start_pos, last_undo->end_pos, start_pos);
592 debug_print("del:undo add %d-%d\n", start_pos, end_pos);
593 undo_add(text_to_delete, start_pos, end_pos, UNDO_ACTION_DELETE,
595 g_free(text_to_delete);
598 void undo_paste_clipboard_cb(GtkTextView *textview, UndoMain *undostruct)
600 if (prefs_common.undolevels > 0)
601 if (undo_get_selection(textview, NULL, NULL))
602 undostruct->paste = TRUE;
606 * undo_get_selection:
607 * @text: Text to get the selection from
608 * @start: return here the start position of the selection
609 * @end: return here the end position of the selection
611 * Gets the current selection for View
613 * Return Value: TRUE if there is a selection active, FALSE if not
615 static gint undo_get_selection(GtkTextView *textview, guint *start, guint *end)
617 GtkTextBuffer *buffer;
618 GtkTextIter start_iter, end_iter;
619 guint start_pos, end_pos;
621 buffer = gtk_text_view_get_buffer(textview);
622 gtk_text_buffer_get_selection_bounds(buffer, &start_iter, &end_iter);
624 start_pos = gtk_text_iter_get_offset(&start_iter);
625 end_pos = gtk_text_iter_get_offset(&end_iter);
627 /* The user can select from end to start too. If so, swap it*/
628 if (end_pos < start_pos) {
632 start_pos = swap_pos;
641 if ((start_pos > 0 || end_pos > 0) && (start_pos != end_pos))