xed-sort-plugin.c: Clean up code styling
This commit is contained in:
parent
26f8139317
commit
3a6de664e1
|
@ -58,397 +58,372 @@ enum
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
GtkWidget *dialog;
|
GtkWidget *dialog;
|
||||||
GtkWidget *col_num_spinbutton;
|
GtkWidget *col_num_spinbutton;
|
||||||
GtkWidget *reverse_order_checkbutton;
|
GtkWidget *reverse_order_checkbutton;
|
||||||
GtkWidget *ignore_case_checkbutton;
|
GtkWidget *ignore_case_checkbutton;
|
||||||
GtkWidget *remove_dups_checkbutton;
|
GtkWidget *remove_dups_checkbutton;
|
||||||
|
|
||||||
XedDocument *doc;
|
XedDocument *doc;
|
||||||
|
|
||||||
GtkTextIter start, end; /* selection */
|
GtkTextIter start, end; /* selection */
|
||||||
} SortDialog;
|
} SortDialog;
|
||||||
|
|
||||||
struct _XedSortPluginPrivate
|
struct _XedSortPluginPrivate
|
||||||
{
|
{
|
||||||
GtkWidget *window;
|
GtkWidget *window;
|
||||||
|
|
||||||
GtkActionGroup *ui_action_group;
|
GtkActionGroup *ui_action_group;
|
||||||
guint ui_id;
|
guint ui_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
gboolean ignore_case;
|
gboolean ignore_case;
|
||||||
gboolean reverse_order;
|
gboolean reverse_order;
|
||||||
gboolean remove_duplicates;
|
gboolean remove_duplicates;
|
||||||
gint starting_column;
|
gint starting_column;
|
||||||
} SortInfo;
|
} SortInfo;
|
||||||
|
|
||||||
static void sort_cb (GtkAction *action, XedSortPlugin *plugin);
|
static void sort_cb (GtkAction *action,
|
||||||
|
XedSortPlugin *plugin);
|
||||||
static void sort_real (SortDialog *dialog);
|
static void sort_real (SortDialog *dialog);
|
||||||
|
|
||||||
static const GtkActionEntry action_entries[] =
|
static const GtkActionEntry action_entries[] =
|
||||||
{
|
{
|
||||||
{ "Sort",
|
{ "Sort",
|
||||||
"view-sort-ascending-symbolic",
|
"view-sort-ascending-symbolic",
|
||||||
N_("S_ort..."),
|
N_("S_ort..."),
|
||||||
NULL,
|
NULL,
|
||||||
N_("Sort the current document or selection"),
|
N_("Sort the current document or selection"),
|
||||||
G_CALLBACK (sort_cb) }
|
G_CALLBACK (sort_cb)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sort_dialog_destroy (GObject *obj,
|
sort_dialog_destroy (GObject *obj,
|
||||||
gpointer dialog_pointer)
|
gpointer dialog_pointer)
|
||||||
{
|
{
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
g_slice_free (SortDialog, dialog_pointer);
|
g_slice_free (SortDialog, dialog_pointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sort_dialog_response_handler (GtkDialog *widget,
|
sort_dialog_response_handler (GtkDialog *widget,
|
||||||
gint res_id,
|
gint res_id,
|
||||||
SortDialog *dialog)
|
SortDialog *dialog)
|
||||||
{
|
{
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
switch (res_id)
|
switch (res_id)
|
||||||
{
|
{
|
||||||
case GTK_RESPONSE_OK:
|
case GTK_RESPONSE_OK:
|
||||||
sort_real (dialog);
|
sort_real (dialog);
|
||||||
gtk_widget_destroy (dialog->dialog);
|
gtk_widget_destroy (dialog->dialog);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case GTK_RESPONSE_HELP:
|
case GTK_RESPONSE_HELP:
|
||||||
xed_help_display (GTK_WINDOW (widget),
|
xed_help_display (GTK_WINDOW (widget), NULL, "xed-sort-plugin");
|
||||||
NULL,
|
break;
|
||||||
"xed-sort-plugin");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GTK_RESPONSE_CANCEL:
|
case GTK_RESPONSE_CANCEL:
|
||||||
gtk_widget_destroy (dialog->dialog);
|
gtk_widget_destroy (dialog->dialog);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* NOTE: we store the current selection in the dialog since focusing
|
/* NOTE: we store the current selection in the dialog since focusing
|
||||||
* the text field (like the combo box) looses the documnent selection.
|
* the text field (like the combo box) looses the documnent selection.
|
||||||
* Storing the selection ONLY works because the dialog is modal */
|
* Storing the selection ONLY works because the dialog is modal */
|
||||||
static void
|
static void
|
||||||
get_current_selection (XedWindow *window, SortDialog *dialog)
|
get_current_selection (XedWindow *window,
|
||||||
|
SortDialog *dialog)
|
||||||
{
|
{
|
||||||
XedDocument *doc;
|
XedDocument *doc;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
doc = xed_window_get_active_document (window);
|
doc = xed_window_get_active_document (window);
|
||||||
|
|
||||||
if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc),
|
if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc), &dialog->start, &dialog->end))
|
||||||
&dialog->start,
|
{
|
||||||
&dialog->end))
|
/* No selection, get the whole document. */
|
||||||
{
|
gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc), &dialog->start, &dialog->end);
|
||||||
/* No selection, get the whole document. */
|
}
|
||||||
gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc),
|
|
||||||
&dialog->start,
|
|
||||||
&dialog->end);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SortDialog *
|
static SortDialog *
|
||||||
get_sort_dialog (XedSortPlugin *plugin)
|
get_sort_dialog (XedSortPlugin *plugin)
|
||||||
{
|
{
|
||||||
XedWindow *window;
|
XedWindow *window;
|
||||||
SortDialog *dialog;
|
SortDialog *dialog;
|
||||||
GtkWidget *error_widget;
|
GtkWidget *error_widget;
|
||||||
gboolean ret;
|
gboolean ret;
|
||||||
gchar *data_dir;
|
gchar *data_dir;
|
||||||
gchar *ui_file;
|
gchar *ui_file;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
window = XED_WINDOW (plugin->priv->window);
|
window = XED_WINDOW (plugin->priv->window);
|
||||||
|
|
||||||
dialog = g_slice_new (SortDialog);
|
dialog = g_slice_new (SortDialog);
|
||||||
|
|
||||||
data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (plugin));
|
data_dir = peas_extension_base_get_data_dir (PEAS_EXTENSION_BASE (plugin));
|
||||||
ui_file = g_build_filename (data_dir, "sort.ui", NULL);
|
ui_file = g_build_filename (data_dir, "sort.ui", NULL);
|
||||||
g_free (data_dir);
|
g_free (data_dir);
|
||||||
ret = xed_utils_get_ui_objects (ui_file,
|
ret = xed_utils_get_ui_objects (ui_file,
|
||||||
NULL,
|
NULL,
|
||||||
&error_widget,
|
&error_widget,
|
||||||
"sort_dialog", &dialog->dialog,
|
"sort_dialog", &dialog->dialog,
|
||||||
"reverse_order_checkbutton", &dialog->reverse_order_checkbutton,
|
"reverse_order_checkbutton", &dialog->reverse_order_checkbutton,
|
||||||
"col_num_spinbutton", &dialog->col_num_spinbutton,
|
"col_num_spinbutton", &dialog->col_num_spinbutton,
|
||||||
"ignore_case_checkbutton", &dialog->ignore_case_checkbutton,
|
"ignore_case_checkbutton", &dialog->ignore_case_checkbutton,
|
||||||
"remove_dups_checkbutton", &dialog->remove_dups_checkbutton,
|
"remove_dups_checkbutton", &dialog->remove_dups_checkbutton,
|
||||||
NULL);
|
NULL);
|
||||||
g_free (ui_file);
|
g_free (ui_file);
|
||||||
|
|
||||||
if (!ret)
|
if (!ret)
|
||||||
{
|
{
|
||||||
const gchar *err_message;
|
const gchar *err_message;
|
||||||
|
|
||||||
err_message = gtk_label_get_label (GTK_LABEL (error_widget));
|
err_message = gtk_label_get_label (GTK_LABEL (error_widget));
|
||||||
xed_warning (GTK_WINDOW (window),
|
xed_warning (GTK_WINDOW (window), "%s", err_message);
|
||||||
"%s", err_message);
|
|
||||||
|
|
||||||
g_slice_free (SortDialog, dialog);
|
g_slice_free (SortDialog, dialog);
|
||||||
gtk_widget_destroy (error_widget);
|
gtk_widget_destroy (error_widget);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog),
|
gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog), GTK_RESPONSE_OK);
|
||||||
GTK_RESPONSE_OK);
|
|
||||||
|
|
||||||
g_signal_connect (dialog->dialog,
|
g_signal_connect (dialog->dialog, "destroy", G_CALLBACK (sort_dialog_destroy), dialog);
|
||||||
"destroy",
|
g_signal_connect (dialog->dialog, "response", G_CALLBACK (sort_dialog_response_handler), dialog);
|
||||||
G_CALLBACK (sort_dialog_destroy),
|
|
||||||
dialog);
|
|
||||||
|
|
||||||
g_signal_connect (dialog->dialog,
|
get_current_selection (window, dialog);
|
||||||
"response",
|
|
||||||
G_CALLBACK (sort_dialog_response_handler),
|
|
||||||
dialog);
|
|
||||||
|
|
||||||
get_current_selection (window, dialog);
|
return dialog;
|
||||||
|
|
||||||
return dialog;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sort_cb (GtkAction *action,
|
sort_cb (GtkAction *action,
|
||||||
XedSortPlugin *plugin)
|
XedSortPlugin *plugin)
|
||||||
{
|
{
|
||||||
XedWindow *window;
|
XedWindow *window;
|
||||||
XedDocument *doc;
|
XedDocument *doc;
|
||||||
GtkWindowGroup *wg;
|
GtkWindowGroup *wg;
|
||||||
SortDialog *dialog;
|
SortDialog *dialog;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
window = XED_WINDOW (plugin->priv->window);
|
window = XED_WINDOW (plugin->priv->window);
|
||||||
|
|
||||||
doc = xed_window_get_active_document (window);
|
doc = xed_window_get_active_document (window);
|
||||||
g_return_if_fail (doc != NULL);
|
g_return_if_fail (doc != NULL);
|
||||||
|
|
||||||
dialog = get_sort_dialog (plugin);
|
dialog = get_sort_dialog (plugin);
|
||||||
g_return_if_fail (dialog != NULL);
|
g_return_if_fail (dialog != NULL);
|
||||||
|
|
||||||
wg = xed_window_get_group (window);
|
wg = xed_window_get_group (window);
|
||||||
gtk_window_group_add_window (wg,
|
gtk_window_group_add_window (wg, GTK_WINDOW (dialog->dialog));
|
||||||
GTK_WINDOW (dialog->dialog));
|
|
||||||
|
|
||||||
dialog->doc = doc;
|
dialog->doc = doc;
|
||||||
|
|
||||||
gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
|
gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog), GTK_WINDOW (window));
|
||||||
GTK_WINDOW (window));
|
gtk_window_set_modal (GTK_WINDOW (dialog->dialog), TRUE);
|
||||||
|
|
||||||
gtk_window_set_modal (GTK_WINDOW (dialog->dialog),
|
gtk_widget_show (GTK_WIDGET (dialog->dialog));
|
||||||
TRUE);
|
|
||||||
|
|
||||||
gtk_widget_show (GTK_WIDGET (dialog->dialog));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Compares two strings for the sorting algorithm. Uses the UTF-8 processing
|
/* Compares two strings for the sorting algorithm. Uses the UTF-8 processing
|
||||||
* functions in GLib to be as correct as possible.*/
|
* functions in GLib to be as correct as possible.*/
|
||||||
static gint
|
static gint
|
||||||
compare_algorithm (gconstpointer s1,
|
compare_algorithm (gconstpointer s1,
|
||||||
gconstpointer s2,
|
gconstpointer s2,
|
||||||
gpointer data)
|
gpointer data)
|
||||||
{
|
{
|
||||||
gint length1, length2;
|
gint length1, length2;
|
||||||
gint ret;
|
gint ret;
|
||||||
gchar *string1, *string2;
|
gchar *string1, *string2;
|
||||||
gchar *substring1, *substring2;
|
gchar *substring1, *substring2;
|
||||||
gchar *key1, *key2;
|
gchar *key1, *key2;
|
||||||
SortInfo *sort_info;
|
SortInfo *sort_info;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
sort_info = (SortInfo *) data;
|
sort_info = (SortInfo *) data;
|
||||||
g_return_val_if_fail (sort_info != NULL, -1);
|
g_return_val_if_fail (sort_info != NULL, -1);
|
||||||
|
|
||||||
if (!sort_info->ignore_case)
|
if (!sort_info->ignore_case)
|
||||||
{
|
{
|
||||||
string1 = *((gchar **) s1);
|
string1 = *((gchar **) s1);
|
||||||
string2 = *((gchar **) s2);
|
string2 = *((gchar **) s2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string1 = g_utf8_casefold (*((gchar **) s1), -1);
|
string1 = g_utf8_casefold (*((gchar **) s1), -1);
|
||||||
string2 = g_utf8_casefold (*((gchar **) s2), -1);
|
string2 = g_utf8_casefold (*((gchar **) s2), -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
length1 = g_utf8_strlen (string1, -1);
|
length1 = g_utf8_strlen (string1, -1);
|
||||||
length2 = g_utf8_strlen (string2, -1);
|
length2 = g_utf8_strlen (string2, -1);
|
||||||
|
|
||||||
if ((length1 < sort_info->starting_column) &&
|
if ((length1 < sort_info->starting_column) &&
|
||||||
(length2 < sort_info->starting_column))
|
(length2 < sort_info->starting_column))
|
||||||
{
|
{
|
||||||
ret = 0;
|
ret = 0;
|
||||||
}
|
}
|
||||||
else if (length1 < sort_info->starting_column)
|
else if (length1 < sort_info->starting_column)
|
||||||
{
|
{
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
else if (length2 < sort_info->starting_column)
|
else if (length2 < sort_info->starting_column)
|
||||||
{
|
{
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
}
|
||||||
else if (sort_info->starting_column < 1)
|
else if (sort_info->starting_column < 1)
|
||||||
{
|
{
|
||||||
key1 = g_utf8_collate_key (string1, -1);
|
key1 = g_utf8_collate_key (string1, -1);
|
||||||
key2 = g_utf8_collate_key (string2, -1);
|
key2 = g_utf8_collate_key (string2, -1);
|
||||||
ret = strcmp (key1, key2);
|
ret = strcmp (key1, key2);
|
||||||
|
|
||||||
g_free (key1);
|
g_free (key1);
|
||||||
g_free (key2);
|
g_free (key2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* A character column offset is required, so figure out
|
/* A character column offset is required, so figure out
|
||||||
* the correct offset into the UTF-8 string. */
|
* the correct offset into the UTF-8 string. */
|
||||||
substring1 = g_utf8_offset_to_pointer (string1, sort_info->starting_column);
|
substring1 = g_utf8_offset_to_pointer (string1, sort_info->starting_column);
|
||||||
substring2 = g_utf8_offset_to_pointer (string2, sort_info->starting_column);
|
substring2 = g_utf8_offset_to_pointer (string2, sort_info->starting_column);
|
||||||
|
|
||||||
key1 = g_utf8_collate_key (substring1, -1);
|
key1 = g_utf8_collate_key (substring1, -1);
|
||||||
key2 = g_utf8_collate_key (substring2, -1);
|
key2 = g_utf8_collate_key (substring2, -1);
|
||||||
ret = strcmp (key1, key2);
|
ret = strcmp (key1, key2);
|
||||||
|
|
||||||
g_free (key1);
|
g_free (key1);
|
||||||
g_free (key2);
|
g_free (key2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do the necessary cleanup. */
|
/* Do the necessary cleanup. */
|
||||||
if (sort_info->ignore_case)
|
if (sort_info->ignore_case)
|
||||||
{
|
{
|
||||||
g_free (string1);
|
g_free (string1);
|
||||||
g_free (string2);
|
g_free (string2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sort_info->reverse_order)
|
if (sort_info->reverse_order)
|
||||||
{
|
{
|
||||||
ret = -1 * ret;
|
ret = -1 * ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gchar *
|
static gchar *
|
||||||
get_line_slice (GtkTextBuffer *buf,
|
get_line_slice (GtkTextBuffer *buf,
|
||||||
gint line)
|
gint line)
|
||||||
{
|
{
|
||||||
GtkTextIter start, end;
|
GtkTextIter start, end;
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
gtk_text_buffer_get_iter_at_line (buf, &start, line);
|
gtk_text_buffer_get_iter_at_line (buf, &start, line);
|
||||||
end = start;
|
end = start;
|
||||||
|
|
||||||
if (!gtk_text_iter_ends_line (&start))
|
if (!gtk_text_iter_ends_line (&start))
|
||||||
gtk_text_iter_forward_to_line_end (&end);
|
{
|
||||||
|
gtk_text_iter_forward_to_line_end (&end);
|
||||||
|
}
|
||||||
|
|
||||||
ret= gtk_text_buffer_get_slice (buf,
|
ret= gtk_text_buffer_get_slice (buf, &start, &end, TRUE);
|
||||||
&start,
|
|
||||||
&end,
|
|
||||||
TRUE);
|
|
||||||
|
|
||||||
g_assert (ret != NULL);
|
g_assert (ret != NULL);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sort_real (SortDialog *dialog)
|
sort_real (SortDialog *dialog)
|
||||||
{
|
{
|
||||||
XedDocument *doc;
|
XedDocument *doc;
|
||||||
GtkTextIter start, end;
|
GtkTextIter start, end;
|
||||||
gint start_line, end_line;
|
gint start_line, end_line;
|
||||||
gint i;
|
gint i;
|
||||||
gchar *last_row = NULL;
|
gchar *last_row = NULL;
|
||||||
gint num_lines;
|
gint num_lines;
|
||||||
gchar **lines;
|
gchar **lines;
|
||||||
SortInfo *sort_info;
|
SortInfo *sort_info;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
doc = dialog->doc;
|
doc = dialog->doc;
|
||||||
g_return_if_fail (doc != NULL);
|
g_return_if_fail (doc != NULL);
|
||||||
|
|
||||||
sort_info = g_new0 (SortInfo, 1);
|
sort_info = g_new0 (SortInfo, 1);
|
||||||
sort_info->ignore_case = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->ignore_case_checkbutton));
|
sort_info->ignore_case = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->ignore_case_checkbutton));
|
||||||
sort_info->reverse_order = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->reverse_order_checkbutton));
|
sort_info->reverse_order = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->reverse_order_checkbutton));
|
||||||
sort_info->remove_duplicates = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->remove_dups_checkbutton));
|
sort_info->remove_duplicates = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->remove_dups_checkbutton));
|
||||||
sort_info->starting_column = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (dialog->col_num_spinbutton)) - 1;
|
sort_info->starting_column = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (dialog->col_num_spinbutton)) - 1;
|
||||||
|
|
||||||
start = dialog->start;
|
start = dialog->start;
|
||||||
end = dialog->end;
|
end = dialog->end;
|
||||||
start_line = gtk_text_iter_get_line (&start);
|
start_line = gtk_text_iter_get_line (&start);
|
||||||
end_line = gtk_text_iter_get_line (&end);
|
end_line = gtk_text_iter_get_line (&end);
|
||||||
|
|
||||||
/* if we are at line start our last line is the previus one.
|
/* if we are at line start our last line is the previus one.
|
||||||
* Otherwise the last line is the current one but we try to
|
* Otherwise the last line is the current one but we try to
|
||||||
* move the iter after the line terminator */
|
* move the iter after the line terminator */
|
||||||
if (gtk_text_iter_get_line_offset (&end) == 0)
|
if (gtk_text_iter_get_line_offset (&end) == 0)
|
||||||
end_line = MAX (start_line, end_line - 1);
|
{
|
||||||
else
|
end_line = MAX (start_line, end_line - 1);
|
||||||
gtk_text_iter_forward_line (&end);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gtk_text_iter_forward_line (&end);
|
||||||
|
}
|
||||||
|
|
||||||
num_lines = end_line - start_line + 1;
|
num_lines = end_line - start_line + 1;
|
||||||
lines = g_new0 (gchar *, num_lines + 1);
|
lines = g_new0 (gchar *, num_lines + 1);
|
||||||
|
|
||||||
xed_debug_message (DEBUG_PLUGINS, "Building list...");
|
xed_debug_message (DEBUG_PLUGINS, "Building list...");
|
||||||
|
|
||||||
for (i = 0; i < num_lines; i++)
|
for (i = 0; i < num_lines; i++)
|
||||||
{
|
{
|
||||||
lines[i] = get_line_slice (GTK_TEXT_BUFFER (doc), start_line + i);
|
lines[i] = get_line_slice (GTK_TEXT_BUFFER (doc), start_line + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
lines[num_lines] = NULL;
|
lines[num_lines] = NULL;
|
||||||
|
|
||||||
xed_debug_message (DEBUG_PLUGINS, "Sort list...");
|
xed_debug_message (DEBUG_PLUGINS, "Sort list...");
|
||||||
|
|
||||||
g_qsort_with_data (lines,
|
g_qsort_with_data (lines, num_lines, sizeof (gpointer), compare_algorithm, sort_info);
|
||||||
num_lines,
|
|
||||||
sizeof (gpointer),
|
|
||||||
compare_algorithm,
|
|
||||||
sort_info);
|
|
||||||
|
|
||||||
xed_debug_message (DEBUG_PLUGINS, "Rebuilding document...");
|
xed_debug_message (DEBUG_PLUGINS, "Rebuilding document...");
|
||||||
|
|
||||||
gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (doc));
|
gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (doc));
|
||||||
|
|
||||||
gtk_text_buffer_delete (GTK_TEXT_BUFFER (doc),
|
gtk_text_buffer_delete (GTK_TEXT_BUFFER (doc), &start, &end);
|
||||||
&start,
|
|
||||||
&end);
|
|
||||||
|
|
||||||
for (i = 0; i < num_lines; i++)
|
for (i = 0; i < num_lines; i++)
|
||||||
{
|
{
|
||||||
if (sort_info->remove_duplicates &&
|
if (sort_info->remove_duplicates && last_row != NULL && (strcmp (last_row, lines[i]) == 0))
|
||||||
last_row != NULL &&
|
{
|
||||||
(strcmp (last_row, lines[i]) == 0))
|
continue;
|
||||||
continue;
|
}
|
||||||
|
|
||||||
gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc),
|
gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc), &start, lines[i], -1);
|
||||||
&start,
|
gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc), &start, "\n", -1);
|
||||||
lines[i],
|
|
||||||
-1);
|
|
||||||
gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc),
|
|
||||||
&start,
|
|
||||||
"\n",
|
|
||||||
-1);
|
|
||||||
|
|
||||||
last_row = lines[i];
|
last_row = lines[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (doc));
|
gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (doc));
|
||||||
|
|
||||||
g_strfreev (lines);
|
g_strfreev (lines);
|
||||||
g_free (sort_info);
|
g_free (sort_info);
|
||||||
|
|
||||||
xed_debug_message (DEBUG_PLUGINS, "Done.");
|
xed_debug_message (DEBUG_PLUGINS, "Done.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -457,9 +432,9 @@ xed_sort_plugin_set_property (GObject *object,
|
||||||
const GValue *value,
|
const GValue *value,
|
||||||
GParamSpec *pspec)
|
GParamSpec *pspec)
|
||||||
{
|
{
|
||||||
XedSortPlugin *plugin = XED_SORT_PLUGIN (object);
|
XedSortPlugin *plugin = XED_SORT_PLUGIN (object);
|
||||||
|
|
||||||
switch (prop_id)
|
switch (prop_id)
|
||||||
{
|
{
|
||||||
case PROP_OBJECT:
|
case PROP_OBJECT:
|
||||||
plugin->priv->window = GTK_WIDGET (g_value_dup_object (value));
|
plugin->priv->window = GTK_WIDGET (g_value_dup_object (value));
|
||||||
|
@ -476,9 +451,9 @@ xed_sort_plugin_get_property (GObject *object,
|
||||||
GValue *value,
|
GValue *value,
|
||||||
GParamSpec *pspec)
|
GParamSpec *pspec)
|
||||||
{
|
{
|
||||||
XedSortPlugin *plugin = XED_SORT_PLUGIN (object);
|
XedSortPlugin *plugin = XED_SORT_PLUGIN (object);
|
||||||
|
|
||||||
switch (prop_id)
|
switch (prop_id)
|
||||||
{
|
{
|
||||||
case PROP_OBJECT:
|
case PROP_OBJECT:
|
||||||
g_value_set_object (value, plugin->priv->window);
|
g_value_set_object (value, plugin->priv->window);
|
||||||
|
@ -493,16 +468,16 @@ static void
|
||||||
update_ui (XedSortPluginPrivate *data)
|
update_ui (XedSortPluginPrivate *data)
|
||||||
{
|
{
|
||||||
XedWindow *window;
|
XedWindow *window;
|
||||||
XedView *view;
|
XedView *view;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
window = XED_WINDOW (data->window);
|
window = XED_WINDOW (data->window);
|
||||||
view = xed_window_get_active_view (window);
|
view = xed_window_get_active_view (window);
|
||||||
|
|
||||||
gtk_action_group_set_sensitive (data->ui_action_group,
|
gtk_action_group_set_sensitive (data->ui_action_group,
|
||||||
(view != NULL) &&
|
(view != NULL) &&
|
||||||
gtk_text_view_get_editable (GTK_TEXT_VIEW (view)));
|
gtk_text_view_get_editable (GTK_TEXT_VIEW (view)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -511,39 +486,36 @@ xed_sort_plugin_activate (PeasActivatable *activatable)
|
||||||
XedSortPlugin *plugin;
|
XedSortPlugin *plugin;
|
||||||
XedSortPluginPrivate *data;
|
XedSortPluginPrivate *data;
|
||||||
XedWindow *window;
|
XedWindow *window;
|
||||||
GtkUIManager *manager;
|
GtkUIManager *manager;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
plugin = XED_SORT_PLUGIN (activatable);
|
plugin = XED_SORT_PLUGIN (activatable);
|
||||||
data = plugin->priv;
|
data = plugin->priv;
|
||||||
window = XED_WINDOW (data->window);
|
window = XED_WINDOW (data->window);
|
||||||
|
|
||||||
manager = xed_window_get_ui_manager (window);
|
manager = xed_window_get_ui_manager (window);
|
||||||
|
|
||||||
data->ui_action_group = gtk_action_group_new ("XedSortPluginActions");
|
data->ui_action_group = gtk_action_group_new ("XedSortPluginActions");
|
||||||
gtk_action_group_set_translation_domain (data->ui_action_group,
|
gtk_action_group_set_translation_domain (data->ui_action_group, GETTEXT_PACKAGE);
|
||||||
GETTEXT_PACKAGE);
|
gtk_action_group_add_actions (data->ui_action_group,
|
||||||
gtk_action_group_add_actions (data->ui_action_group,
|
action_entries,
|
||||||
action_entries,
|
G_N_ELEMENTS (action_entries),
|
||||||
G_N_ELEMENTS (action_entries),
|
plugin);
|
||||||
plugin);
|
|
||||||
|
|
||||||
gtk_ui_manager_insert_action_group (manager,
|
gtk_ui_manager_insert_action_group (manager, data->ui_action_group, -1);
|
||||||
data->ui_action_group,
|
|
||||||
-1);
|
|
||||||
|
|
||||||
data->ui_id = gtk_ui_manager_new_merge_id (manager);
|
data->ui_id = gtk_ui_manager_new_merge_id (manager);
|
||||||
|
|
||||||
gtk_ui_manager_add_ui (manager,
|
gtk_ui_manager_add_ui (manager,
|
||||||
data->ui_id,
|
data->ui_id,
|
||||||
MENU_PATH,
|
MENU_PATH,
|
||||||
"Sort",
|
"Sort",
|
||||||
"Sort",
|
"Sort",
|
||||||
GTK_UI_MANAGER_MENUITEM,
|
GTK_UI_MANAGER_MENUITEM,
|
||||||
FALSE);
|
FALSE);
|
||||||
|
|
||||||
update_ui (data);
|
update_ui (data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -551,33 +523,31 @@ xed_sort_plugin_deactivate (PeasActivatable *activatable)
|
||||||
{
|
{
|
||||||
XedSortPluginPrivate *data;
|
XedSortPluginPrivate *data;
|
||||||
XedWindow *window;
|
XedWindow *window;
|
||||||
GtkUIManager *manager;
|
GtkUIManager *manager;
|
||||||
|
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
data = XED_SORT_PLUGIN (activatable)->priv;
|
data = XED_SORT_PLUGIN (activatable)->priv;
|
||||||
window = XED_WINDOW (data->window);
|
window = XED_WINDOW (data->window);
|
||||||
|
|
||||||
manager = xed_window_get_ui_manager (window);
|
manager = xed_window_get_ui_manager (window);
|
||||||
|
|
||||||
gtk_ui_manager_remove_ui (manager,
|
gtk_ui_manager_remove_ui (manager, data->ui_id);
|
||||||
data->ui_id);
|
gtk_ui_manager_remove_action_group (manager, data->ui_action_group);
|
||||||
gtk_ui_manager_remove_action_group (manager,
|
|
||||||
data->ui_action_group);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xed_sort_plugin_update_state (PeasActivatable *activatable)
|
xed_sort_plugin_update_state (PeasActivatable *activatable)
|
||||||
{
|
{
|
||||||
xed_debug (DEBUG_PLUGINS);
|
xed_debug (DEBUG_PLUGINS);
|
||||||
|
|
||||||
update_ui (XED_SORT_PLUGIN (activatable)->priv);
|
update_ui (XED_SORT_PLUGIN (activatable)->priv);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xed_sort_plugin_init (XedSortPlugin *plugin)
|
xed_sort_plugin_init (XedSortPlugin *plugin)
|
||||||
{
|
{
|
||||||
xed_debug_message (DEBUG_PLUGINS, "XedSortPlugin initializing");
|
xed_debug_message (DEBUG_PLUGINS, "XedSortPlugin initializing");
|
||||||
|
|
||||||
plugin->priv = XED_SORT_PLUGIN_GET_PRIVATE (plugin);
|
plugin->priv = XED_SORT_PLUGIN_GET_PRIVATE (plugin);
|
||||||
}
|
}
|
||||||
|
@ -585,7 +555,7 @@ xed_sort_plugin_init (XedSortPlugin *plugin)
|
||||||
static void
|
static void
|
||||||
xed_sort_plugin_dispose (GObject *object)
|
xed_sort_plugin_dispose (GObject *object)
|
||||||
{
|
{
|
||||||
XedSortPlugin *plugin = XED_SORT_PLUGIN (object);
|
XedSortPlugin *plugin = XED_SORT_PLUGIN (object);
|
||||||
|
|
||||||
xed_debug_message (DEBUG_PLUGINS, "XedSortPlugin disposing");
|
xed_debug_message (DEBUG_PLUGINS, "XedSortPlugin disposing");
|
||||||
|
|
||||||
|
@ -601,15 +571,15 @@ xed_sort_plugin_dispose (GObject *object)
|
||||||
plugin->priv->ui_action_group = NULL;
|
plugin->priv->ui_action_group = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
G_OBJECT_CLASS (xed_sort_plugin_parent_class)->dispose (object);
|
G_OBJECT_CLASS (xed_sort_plugin_parent_class)->dispose (object);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xed_sort_plugin_class_init (XedSortPluginClass *klass)
|
xed_sort_plugin_class_init (XedSortPluginClass *klass)
|
||||||
{
|
{
|
||||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||||
|
|
||||||
object_class->dispose = xed_sort_plugin_dispose;
|
object_class->dispose = xed_sort_plugin_dispose;
|
||||||
object_class->set_property = xed_sort_plugin_set_property;
|
object_class->set_property = xed_sort_plugin_set_property;
|
||||||
object_class->get_property = xed_sort_plugin_get_property;
|
object_class->get_property = xed_sort_plugin_get_property;
|
||||||
|
|
||||||
|
@ -637,7 +607,7 @@ peas_register_types (PeasObjectModule *module)
|
||||||
{
|
{
|
||||||
xed_sort_plugin_register_type (G_TYPE_MODULE (module));
|
xed_sort_plugin_register_type (G_TYPE_MODULE (module));
|
||||||
|
|
||||||
peas_object_module_register_extension_type (module,
|
peas_object_module_register_extension_type (module,
|
||||||
PEAS_TYPE_ACTIVATABLE,
|
PEAS_TYPE_ACTIVATABLE,
|
||||||
XED_TYPE_SORT_PLUGIN);
|
XED_TYPE_SORT_PLUGIN);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue