2006-08-18 22:59:53 -07:00
|
|
|
/*
|
|
|
|
* mooeditaction.c
|
|
|
|
*
|
2007-05-30 02:37:41 -07:00
|
|
|
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
2006-08-18 22:59:53 -07:00
|
|
|
*
|
2007-06-24 10:56:20 -07:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2007-09-23 09:47:28 -07:00
|
|
|
* License version 2.1 as published by the Free Software Foundation.
|
2006-08-18 22:59:53 -07:00
|
|
|
*
|
|
|
|
* See COPYING file that comes with this distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MOOEDIT_COMPILATION
|
|
|
|
#include "mooedit/mooeditaction.h"
|
2006-08-20 01:49:33 -07:00
|
|
|
#include "mooedit/mooeditaction-factory.h"
|
2006-08-18 23:40:34 -07:00
|
|
|
#include "mooedit/mooedit-private.h"
|
2007-09-02 13:42:17 -07:00
|
|
|
#include "mooedit/mooeditfiltersettings.h"
|
2007-05-30 02:37:41 -07:00
|
|
|
#include <glib/gregex.h>
|
2006-08-18 22:59:53 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
typedef enum {
|
|
|
|
FILTER_SENSITIVE,
|
|
|
|
FILTER_VISIBLE
|
|
|
|
} FilterType;
|
|
|
|
|
|
|
|
#define N_FILTERS 2
|
|
|
|
|
2006-08-18 22:59:53 -07:00
|
|
|
struct _MooEditActionPrivate {
|
|
|
|
MooEdit *doc;
|
2007-09-02 13:42:17 -07:00
|
|
|
MooEditFilter *file_filter;
|
2007-05-30 02:37:41 -07:00
|
|
|
GRegex *filters[N_FILTERS];
|
2006-08-18 22:59:53 -07:00
|
|
|
};
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
typedef struct {
|
2007-05-30 02:37:41 -07:00
|
|
|
GRegex *regex;
|
2006-08-22 21:21:14 -07:00
|
|
|
guint use_count;
|
|
|
|
} RegexRef;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GHashTable *hash;
|
|
|
|
} FilterStore;
|
|
|
|
|
|
|
|
static FilterStore filter_store;
|
2006-08-18 22:59:53 -07:00
|
|
|
|
2006-10-17 22:24:35 -07:00
|
|
|
G_DEFINE_TYPE (MooEditAction, moo_edit_action, MOO_TYPE_ACTION)
|
2006-08-18 22:59:53 -07:00
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_DOC,
|
2007-09-02 13:42:17 -07:00
|
|
|
PROP_FILE_FILTER,
|
2006-08-22 21:21:14 -07:00
|
|
|
PROP_FILTER_SENSITIVE,
|
|
|
|
PROP_FILTER_VISIBLE
|
2006-08-18 22:59:53 -07:00
|
|
|
};
|
|
|
|
|
2006-08-18 23:40:34 -07:00
|
|
|
|
2007-05-30 02:37:41 -07:00
|
|
|
static GRegex *get_filter_regex (const char *pattern);
|
|
|
|
static void unuse_filter_regex (GRegex *regex);
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
static gboolean moo_edit_action_check_visible (MooEditAction *action);
|
|
|
|
static gboolean moo_edit_action_check_sensitive (MooEditAction *action);
|
|
|
|
static void moo_edit_action_check_state (MooEditAction *action);
|
|
|
|
|
|
|
|
|
2006-08-18 22:59:53 -07:00
|
|
|
static void
|
|
|
|
moo_edit_action_finalize (GObject *object)
|
|
|
|
{
|
2006-08-22 21:21:14 -07:00
|
|
|
guint i;
|
2006-08-18 22:59:53 -07:00
|
|
|
MooEditAction *action = MOO_EDIT_ACTION (object);
|
2006-08-18 23:40:34 -07:00
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
for (i = 0; i < N_FILTERS; ++i)
|
|
|
|
if (action->priv->filters[i])
|
|
|
|
unuse_filter_regex (action->priv->filters[i]);
|
|
|
|
|
2007-09-02 13:42:17 -07:00
|
|
|
_moo_edit_filter_free (action->priv->file_filter);
|
2006-08-18 23:40:34 -07:00
|
|
|
|
2006-08-18 22:59:53 -07:00
|
|
|
G_OBJECT_CLASS(moo_edit_action_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
static const char *
|
|
|
|
moo_edit_action_get_filter (MooEditAction *action,
|
|
|
|
FilterType type)
|
|
|
|
{
|
|
|
|
g_assert (type < N_FILTERS);
|
|
|
|
return action->priv->filters[type] ?
|
2007-05-30 02:37:41 -07:00
|
|
|
g_regex_get_pattern (action->priv->filters[type]) : NULL;
|
2006-08-22 21:21:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-18 22:59:53 -07:00
|
|
|
static void
|
|
|
|
moo_edit_action_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MooEditAction *action = MOO_EDIT_ACTION (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_DOC:
|
|
|
|
g_value_set_object (value, action->priv->doc);
|
|
|
|
break;
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
case PROP_FILTER_VISIBLE:
|
|
|
|
g_value_set_string (value, moo_edit_action_get_filter (action, FILTER_VISIBLE));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_FILTER_SENSITIVE:
|
|
|
|
g_value_set_string (value, moo_edit_action_get_filter (action, FILTER_SENSITIVE));
|
|
|
|
break;
|
|
|
|
|
2006-08-18 22:59:53 -07:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2007-09-02 13:42:17 -07:00
|
|
|
moo_edit_action_set_file_filter (MooEditAction *action,
|
|
|
|
const char *string)
|
2006-08-18 22:59:53 -07:00
|
|
|
{
|
2007-09-02 13:42:17 -07:00
|
|
|
_moo_edit_filter_free (action->priv->file_filter);
|
2008-05-12 15:24:05 -07:00
|
|
|
|
|
|
|
if (string && string[0])
|
|
|
|
action->priv->file_filter = _moo_edit_filter_new (string);
|
|
|
|
else
|
|
|
|
action->priv->file_filter = NULL;
|
|
|
|
|
2007-09-02 13:42:17 -07:00
|
|
|
g_object_notify (G_OBJECT (action), "file-filter");
|
2006-08-18 22:59:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MooEdit *
|
|
|
|
moo_edit_action_get_doc (MooEditAction *action)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (MOO_IS_EDIT_ACTION (action), NULL);
|
|
|
|
return action->priv->doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
static void
|
|
|
|
moo_edit_action_set_filter (MooEditAction *action,
|
|
|
|
const char *filter,
|
|
|
|
FilterType type)
|
|
|
|
{
|
2007-05-30 02:37:41 -07:00
|
|
|
GRegex *tmp;
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
g_assert (type < N_FILTERS);
|
|
|
|
|
|
|
|
tmp = action->priv->filters[type];
|
|
|
|
action->priv->filters[type] = filter ? get_filter_regex (filter) : NULL;
|
|
|
|
unuse_filter_regex (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-18 22:59:53 -07:00
|
|
|
static void
|
|
|
|
moo_edit_action_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MooEditAction *action = MOO_EDIT_ACTION (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_DOC:
|
|
|
|
action->priv->doc = g_value_get_object (value);
|
|
|
|
g_object_notify (object, "doc");
|
|
|
|
break;
|
|
|
|
|
2007-09-02 13:42:17 -07:00
|
|
|
case PROP_FILE_FILTER:
|
|
|
|
moo_edit_action_set_file_filter (action, g_value_get_string (value));
|
2006-08-18 22:59:53 -07:00
|
|
|
break;
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
case PROP_FILTER_SENSITIVE:
|
|
|
|
moo_edit_action_set_filter (action,
|
|
|
|
g_value_get_string (value),
|
|
|
|
FILTER_SENSITIVE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_FILTER_VISIBLE:
|
|
|
|
moo_edit_action_set_filter (action,
|
|
|
|
g_value_get_string (value),
|
|
|
|
FILTER_VISIBLE);
|
|
|
|
break;
|
|
|
|
|
2006-08-18 22:59:53 -07:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_edit_action_init (MooEditAction *action)
|
|
|
|
{
|
|
|
|
action->priv = G_TYPE_INSTANCE_GET_PRIVATE (action, MOO_TYPE_EDIT_ACTION, MooEditActionPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
static const char *
|
|
|
|
get_current_line (MooEdit *doc)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
line = g_object_get_data (G_OBJECT (doc), "moo-edit-current-line");
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
{
|
|
|
|
GtkTextIter line_start, line_end;
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
|
|
|
|
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
|
|
|
|
gtk_text_buffer_get_iter_at_mark (buffer, &line_start,
|
|
|
|
gtk_text_buffer_get_insert (buffer));
|
|
|
|
line_end = line_start;
|
|
|
|
gtk_text_iter_set_line_offset (&line_start, 0);
|
|
|
|
if (!gtk_text_iter_ends_line (&line_end))
|
|
|
|
gtk_text_iter_forward_to_line_end (&line_end);
|
|
|
|
|
|
|
|
line = gtk_text_buffer_get_slice (buffer, &line_start, &line_end, TRUE);
|
|
|
|
|
|
|
|
g_object_set_data_full (G_OBJECT (doc), "moo-edit-current-line", line, g_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
moo_edit_action_check_visible_real (MooEditAction *action)
|
2006-08-18 22:59:53 -07:00
|
|
|
{
|
2006-08-22 21:21:14 -07:00
|
|
|
gboolean visible = TRUE;
|
2007-05-30 02:37:41 -07:00
|
|
|
GRegex *filter = action->priv->filters[FILTER_VISIBLE];
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
if (!action->priv->doc)
|
|
|
|
return gtk_action_get_visible (GTK_ACTION (action));
|
|
|
|
|
2007-09-02 13:42:17 -07:00
|
|
|
if (!action->priv->file_filter && !filter)
|
2006-08-22 21:21:14 -07:00
|
|
|
return gtk_action_get_visible (GTK_ACTION (action));
|
2006-08-18 22:59:53 -07:00
|
|
|
|
2007-09-02 13:42:17 -07:00
|
|
|
if (visible && action->priv->file_filter)
|
|
|
|
if (!_moo_edit_filter_match (action->priv->file_filter, action->priv->doc))
|
2006-08-22 21:21:14 -07:00
|
|
|
visible = FALSE;
|
|
|
|
|
|
|
|
if (visible && filter)
|
|
|
|
{
|
|
|
|
const char *line = get_current_line (action->priv->doc);
|
2007-05-30 02:37:41 -07:00
|
|
|
if (!g_regex_match (filter, line, 0, NULL))
|
2006-08-22 21:21:14 -07:00
|
|
|
visible = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return visible;
|
|
|
|
}
|
2006-08-18 22:59:53 -07:00
|
|
|
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
static gboolean
|
|
|
|
moo_edit_action_check_sensitive_real (MooEditAction *action)
|
|
|
|
{
|
|
|
|
const char *line;
|
2007-05-30 02:37:41 -07:00
|
|
|
GRegex *filter = action->priv->filters[FILTER_SENSITIVE];
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
if (!action->priv->doc || !filter)
|
2006-12-22 01:42:29 -08:00
|
|
|
return gtk_action_get_sensitive (GTK_ACTION (action));
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
line = get_current_line (action->priv->doc);
|
2007-05-30 02:37:41 -07:00
|
|
|
return g_regex_match (filter, line, 0, NULL);
|
2006-08-22 21:21:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_edit_action_check_state_real (MooEditAction *action)
|
|
|
|
{
|
|
|
|
gboolean visible, sensitive;
|
|
|
|
|
|
|
|
visible = moo_edit_action_check_visible (action);
|
|
|
|
sensitive = moo_edit_action_check_sensitive (action);
|
|
|
|
|
|
|
|
g_object_set (action, "sensitive", sensitive, "visible", visible, NULL);
|
2006-08-18 22:59:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_edit_action_class_init (MooEditActionClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = moo_edit_action_finalize;
|
|
|
|
gobject_class->set_property = moo_edit_action_set_property;
|
|
|
|
gobject_class->get_property = moo_edit_action_get_property;
|
|
|
|
|
|
|
|
klass->check_state = moo_edit_action_check_state_real;
|
2006-08-22 21:21:14 -07:00
|
|
|
klass->check_visible = moo_edit_action_check_visible_real;
|
|
|
|
klass->check_sensitive = moo_edit_action_check_sensitive_real;
|
2006-08-18 22:59:53 -07:00
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (MooEditActionPrivate));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_DOC,
|
|
|
|
g_param_spec_object ("doc",
|
|
|
|
"doc",
|
|
|
|
"doc",
|
|
|
|
MOO_TYPE_EDIT,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
2007-09-02 13:42:17 -07:00
|
|
|
PROP_FILE_FILTER,
|
|
|
|
g_param_spec_string ("file-filter",
|
|
|
|
"file-filter",
|
|
|
|
"file-filter",
|
2006-08-18 22:59:53 -07:00
|
|
|
NULL,
|
|
|
|
G_PARAM_WRITABLE));
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_FILTER_SENSITIVE,
|
|
|
|
g_param_spec_string ("filter-sensitive",
|
|
|
|
"filter-sensitive",
|
|
|
|
"filter-sensitive",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_FILTER_VISIBLE,
|
|
|
|
g_param_spec_string ("filter-visible",
|
|
|
|
"filter-visible",
|
|
|
|
"filter-visible",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
moo_edit_action_check_visible (MooEditAction *action)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (MOO_IS_EDIT_ACTION (action), TRUE);
|
|
|
|
|
|
|
|
if (MOO_EDIT_ACTION_GET_CLASS (action)->check_visible)
|
|
|
|
return MOO_EDIT_ACTION_GET_CLASS (action)->check_visible (action);
|
|
|
|
else
|
|
|
|
return gtk_action_get_visible (GTK_ACTION (action));
|
2006-08-18 22:59:53 -07:00
|
|
|
}
|
|
|
|
|
2006-08-22 21:21:14 -07:00
|
|
|
static gboolean
|
|
|
|
moo_edit_action_check_sensitive (MooEditAction *action)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (MOO_IS_EDIT_ACTION (action), TRUE);
|
|
|
|
|
|
|
|
if (MOO_EDIT_ACTION_GET_CLASS (action)->check_sensitive)
|
|
|
|
return MOO_EDIT_ACTION_GET_CLASS (action)->check_sensitive (action);
|
|
|
|
else
|
|
|
|
return gtk_action_get_sensitive (GTK_ACTION (action));
|
|
|
|
}
|
2006-08-18 22:59:53 -07:00
|
|
|
|
2006-08-18 23:40:34 -07:00
|
|
|
static void
|
|
|
|
moo_edit_action_check_state (MooEditAction *action)
|
2006-08-18 22:59:53 -07:00
|
|
|
{
|
2006-08-18 23:40:34 -07:00
|
|
|
g_return_if_fail (MOO_IS_EDIT_ACTION (action));
|
|
|
|
g_return_if_fail (MOO_EDIT_ACTION_GET_CLASS (action)->check_state != NULL);
|
|
|
|
MOO_EDIT_ACTION_GET_CLASS (action)->check_state (action);
|
|
|
|
}
|
2006-08-18 22:59:53 -07:00
|
|
|
|
|
|
|
|
2006-08-18 23:40:34 -07:00
|
|
|
void
|
|
|
|
_moo_edit_check_actions (MooEdit *edit)
|
|
|
|
{
|
2006-08-20 01:49:33 -07:00
|
|
|
GtkActionGroup *group = moo_edit_get_actions (edit);
|
|
|
|
GList *actions = gtk_action_group_list_actions (group);
|
2006-08-18 22:59:53 -07:00
|
|
|
|
2006-08-18 23:40:34 -07:00
|
|
|
while (actions)
|
|
|
|
{
|
|
|
|
if (MOO_IS_EDIT_ACTION (actions->data))
|
|
|
|
moo_edit_action_check_state (actions->data);
|
|
|
|
actions = g_list_delete_link (actions, actions);
|
|
|
|
}
|
2006-08-18 22:59:53 -07:00
|
|
|
}
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
regex_ref_free (RegexRef *ref)
|
|
|
|
{
|
|
|
|
if (ref)
|
|
|
|
{
|
2007-05-30 02:37:41 -07:00
|
|
|
g_regex_unref (ref->regex);
|
2006-08-22 21:21:14 -07:00
|
|
|
g_free (ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_filter_store (void)
|
|
|
|
{
|
|
|
|
if (!filter_store.hash)
|
|
|
|
filter_store.hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
|
|
|
|
(GDestroyNotify) regex_ref_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-30 02:37:41 -07:00
|
|
|
static GRegex *
|
2006-08-22 21:21:14 -07:00
|
|
|
get_filter_regex (const char *pattern)
|
|
|
|
{
|
|
|
|
RegexRef *ref;
|
|
|
|
|
|
|
|
g_return_val_if_fail (pattern != NULL, NULL);
|
|
|
|
|
|
|
|
init_filter_store ();
|
|
|
|
|
|
|
|
ref = g_hash_table_lookup (filter_store.hash, pattern);
|
|
|
|
|
|
|
|
if (!ref)
|
|
|
|
{
|
2007-05-30 02:37:41 -07:00
|
|
|
GRegex *regex;
|
2006-08-22 21:21:14 -07:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2007-05-30 02:37:41 -07:00
|
|
|
regex = g_regex_new (pattern, G_REGEX_OPTIMIZE, 0, &error);
|
2006-08-22 21:21:14 -07:00
|
|
|
|
|
|
|
if (!regex)
|
|
|
|
{
|
|
|
|
g_warning ("%s: %s", G_STRLOC, error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = g_new0 (RegexRef, 1);
|
|
|
|
ref->regex = regex;
|
|
|
|
|
|
|
|
g_hash_table_insert (filter_store.hash, g_strdup (pattern), ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
ref->use_count++;
|
|
|
|
return ref->regex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2007-05-30 02:37:41 -07:00
|
|
|
unuse_filter_regex (GRegex *regex)
|
2006-08-22 21:21:14 -07:00
|
|
|
{
|
|
|
|
RegexRef *ref;
|
|
|
|
const char *pattern;
|
|
|
|
|
|
|
|
g_return_if_fail (regex != NULL);
|
|
|
|
|
|
|
|
init_filter_store ();
|
|
|
|
|
2007-05-30 02:37:41 -07:00
|
|
|
pattern = g_regex_get_pattern (regex);
|
2006-08-22 21:21:14 -07:00
|
|
|
ref = g_hash_table_lookup (filter_store.hash, pattern);
|
|
|
|
g_return_if_fail (ref != NULL);
|
|
|
|
|
|
|
|
if (!--ref->use_count)
|
|
|
|
g_hash_table_remove (filter_store.hash, pattern);
|
|
|
|
}
|