[gtksourceview/wip/undo-redo: 7/7] UndoManager: another batch of code clean-up



commit 28c77ede065a62ec742442b067047c3d4dbd9303
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Aug 22 19:03:08 2014 +0200

    UndoManager: another batch of code clean-up

 gtksourceview/gtksourceundomanagerdefault.c |   61 ++++++++++++++++++---------
 1 files changed, 41 insertions(+), 20 deletions(-)
---
diff --git a/gtksourceview/gtksourceundomanagerdefault.c b/gtksourceview/gtksourceundomanagerdefault.c
index 16294b8..f045c52 100644
--- a/gtksourceview/gtksourceundomanagerdefault.c
+++ b/gtksourceview/gtksourceundomanagerdefault.c
@@ -27,11 +27,8 @@
 #endif
 
 #include "gtksourceundomanagerdefault.h"
-
 #include <string.h>
-
 #include "gtksourceundomanager.h"
-#include "gtksourceview-i18n.h"
 
 #define DEFAULT_MAX_UNDO_LEVELS -1
 
@@ -103,13 +100,6 @@ struct _GtkSourceUndoAction
        guint modified : 1;
 };
 
-enum
-{
-       PROP_0,
-       PROP_BUFFER,
-       PROP_MAX_UNDO_LEVELS
-};
-
 struct _GtkSourceUndoManagerDefaultPrivate
 {
        /* Weak ref to the buffer. */
@@ -150,6 +140,13 @@ struct _GtkSourceUndoManagerDefaultPrivate
        GtkSourceUndoAction *modified_action;
 };
 
+enum
+{
+       PROP_0,
+       PROP_BUFFER,
+       PROP_MAX_UNDO_LEVELS
+};
+
 static void gtk_source_undo_manager_iface_init (GtkSourceUndoManagerIface *iface);
 
 G_DEFINE_TYPE_WITH_CODE (GtkSourceUndoManagerDefault, gtk_source_undo_manager_default, G_TYPE_OBJECT,
@@ -236,10 +233,14 @@ static GtkSourceUndoAction *
 action_list_nth_data (GPtrArray *array,
                       gint       n)
 {
-       if (n < 0 || n >= (gint)array->len)
+       if (n < 0 || (gint)array->len <= n)
+       {
                return NULL;
+       }
        else
+       {
                return array->pdata[array->len - 1 - n];
+       }
 }
 
 static void
@@ -459,17 +460,24 @@ merge_action (GtkSourceUndoManagerDefault *um,
 }
 
 static void
+remove_redo_actions (GtkSourceUndoManagerDefault *manager)
+{
+       if (manager->priv->next_redo >= 0)
+       {
+               free_first_n_actions (manager, manager->priv->next_redo + 1);
+       }
+
+       manager->priv->next_redo = -1;
+       update_can_undo_can_redo (manager);
+}
+
+static void
 add_action (GtkSourceUndoManagerDefault *um,
             const GtkSourceUndoAction   *undo_action)
 {
        GtkSourceUndoAction* action;
 
-       if (um->priv->next_redo >= 0)
-       {
-               free_first_n_actions (um, um->priv->next_redo + 1);
-       }
-
-       um->priv->next_redo = -1;
+       remove_redo_actions (um);
 
        if (!merge_action (um, undo_action))
        {
@@ -477,9 +485,14 @@ add_action (GtkSourceUndoManagerDefault *um,
                *action = *undo_action;
 
                if (action->action_type == GTK_SOURCE_UNDO_ACTION_INSERT)
-                       action->action.insert.text = g_strndup (undo_action->action.insert.text, 
undo_action->action.insert.length);
+               {
+                       action->action.insert.text = g_strndup (undo_action->action.insert.text,
+                                                               undo_action->action.insert.length);
+               }
                else if (action->action_type == GTK_SOURCE_UNDO_ACTION_DELETE)
+               {
                        action->action.delete.text = g_strdup (undo_action->action.delete.text);
+               }
                else
                {
                        g_free (action);
@@ -490,7 +503,9 @@ add_action (GtkSourceUndoManagerDefault *um,
                action->order_in_group = um->priv->actions_in_current_group;
 
                if (action->order_in_group == 1)
+               {
                        ++um->priv->num_of_groups;
+               }
 
                action_list_prepend (um->priv->actions, action);
        }
@@ -519,7 +534,9 @@ set_max_undo_levels (GtkSourceUndoManagerDefault *manager,
        manager->priv->max_undo_levels = max_undo_levels;
 
        if (max_undo_levels < 1)
+       {
                return;
+       }
 
        if (old_levels > max_undo_levels)
        {
@@ -986,15 +1003,20 @@ modified_changed_cb (GtkTextBuffer               *buffer,
        gint idx;
 
        if (manager->priv->actions->len == 0)
+       {
                return;
+       }
 
        idx = manager->priv->next_redo + 1;
        action = action_list_nth_data (manager->priv->actions, idx);
 
-       if (gtk_text_buffer_get_modified (buffer) == FALSE)
+       /* The buffer has been saved. */
+       if (!gtk_text_buffer_get_modified (buffer))
        {
                if (action != NULL)
+               {
                        action->mergeable = FALSE;
+               }
 
                if (manager->priv->modified_action != NULL)
                {
@@ -1008,7 +1030,6 @@ modified_changed_cb (GtkTextBuffer               *buffer,
        if (action == NULL)
        {
                g_return_if_fail (manager->priv->running_not_undoable_actions > 0);
-
                return;
        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]