Ejemplo n.º 1
0
/* Search string in editor, return TRUE and matching part as start and
 * end editor cell */
static gboolean
editor_search (IAnjutaEditor *editor,
               const gchar *search_text, 
               gboolean case_sensitive,
               gboolean search_forward,
               gboolean regex_mode,
               IAnjutaEditorCell* search_start,
               IAnjutaEditorCell* search_end,
               IAnjutaEditorCell** result_start,
               IAnjutaEditorCell** result_end)
{
	gboolean found;

	if (regex_mode)
	{
		gint start_pos;
		gint end_pos;
		gchar *text_to_search;

		text_to_search = ianjuta_editor_get_text (editor,
		                                          IANJUTA_ITERABLE (search_start),
		                                          IANJUTA_ITERABLE (search_end), NULL);

		found = search_regex_in_text (search_text, text_to_search, search_forward, &start_pos, &end_pos);

		start_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (search_start), NULL);
		end_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (search_start), NULL);

		if (found && start_pos >= 0)
		{
			*result_start = IANJUTA_EDITOR_CELL (ianjuta_editor_get_start_position (editor,
			                                                                        NULL));
			*result_end = IANJUTA_EDITOR_CELL (ianjuta_editor_get_start_position (editor,
			                                                                      NULL));

			if (!ianjuta_iterable_set_position(IANJUTA_ITERABLE(*result_start), start_pos, NULL) ||
				!ianjuta_iterable_set_position(IANJUTA_ITERABLE(*result_end), end_pos, NULL))
			{
				g_object_unref(*result_start);
				g_object_unref(*result_end);
				found = FALSE;
			}
		}

		g_free(text_to_search);
	}
	else
	{
		if (search_forward)
		{
			found = ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
			                                       search_text, case_sensitive,
			                                       search_start, search_end,
			                                       result_start,
			                                       result_end, NULL);
		}
		else
		{
			found = ianjuta_editor_search_backward (IANJUTA_EDITOR_SEARCH (editor),
			                                        search_text, case_sensitive,
			                                        search_end, search_start,
			                                        result_start,
			                                        result_end, NULL);
		}
	}

	return found;
}
Ejemplo n.º 2
0
gboolean
search_box_incremental_search (SearchBox* search_box,
                               gboolean search_forward,
                               gboolean search_next,
                               gboolean wrap)
{
	IAnjutaIterable* real_start;
	IAnjutaEditorCell* search_start;
	IAnjutaEditorCell* search_end;
	IAnjutaEditorCell* result_start;
	IAnjutaEditorCell* result_end;
	IAnjutaEditorSelection* selection;

	const gchar* search_text = gtk_entry_get_text (GTK_ENTRY (search_box->priv->search_entry));

	gboolean found = FALSE;

	if (!search_box->priv->current_editor || !search_text || !strlen (search_text))
		return FALSE;

	selection = IANJUTA_EDITOR_SELECTION (search_box->priv->current_editor);

	if (ianjuta_editor_selection_has_selection (selection, NULL))
	{
		search_start =
			IANJUTA_EDITOR_CELL (ianjuta_editor_selection_get_start (selection, NULL));
	}
	else
	{
		search_start =
			IANJUTA_EDITOR_CELL (ianjuta_editor_get_position (search_box->priv->current_editor,
			                                                  NULL));
	}

	real_start =
		ianjuta_iterable_clone (IANJUTA_ITERABLE (search_start), NULL);

	/* If forward, set search start and end to current position of
	 * cursor and editor end, respectively, or if backward to editor
	 * start and current position of cursor, respectively. Current
	 * position of cursor is selection start if have selection. */
	if (search_forward)
	{
		search_end = IANJUTA_EDITOR_CELL (ianjuta_editor_get_position (search_box->priv->current_editor,
	                                                                   NULL));
		ianjuta_iterable_last (IANJUTA_ITERABLE (search_end), NULL);
	}
	else
	{
		search_end = search_start;
		search_start = IANJUTA_EDITOR_CELL (ianjuta_editor_get_position (search_box->priv->current_editor,
	                                                                     NULL));
		ianjuta_iterable_first (IANJUTA_ITERABLE (search_start), NULL);
	}

	/* When there's a selection, if forward, set search start and end
     * to match end and editor end, respectively, or if backward to
     * editor start and match start, respectively. If forward and
     * selection starts with a match, look for next match.
	 */
	if (ianjuta_editor_selection_has_selection (selection,
	                                            NULL) && search_next)
	{
		gchar* selected_text =
			ianjuta_editor_selection_get (selection, NULL);

		gint start_pos, end_pos;
		gboolean selected_have_search_text = FALSE;

		selected_have_search_text = search_box->priv->regex_mode ?
			search_regex_in_text (search_text, selected_text, TRUE, &start_pos, &end_pos) :
			search_str_in_text (search_text, selected_text, search_box->priv->case_sensitive, &start_pos, &end_pos);

		if (selected_have_search_text)
		{
			IAnjutaIterable* selection_start =
				ianjuta_editor_selection_get_start (selection, NULL);

			if (search_forward && start_pos == 0)
			{
				end_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (selection_start), NULL);
				ianjuta_iterable_set_position (IANJUTA_ITERABLE(search_start), end_pos, NULL);
				ianjuta_iterable_last (IANJUTA_ITERABLE (search_end), NULL);
			}
			else if (!search_forward)
			{
				start_pos += ianjuta_iterable_get_position(IANJUTA_ITERABLE (selection_start), NULL);
				ianjuta_iterable_set_position (IANJUTA_ITERABLE(search_end), start_pos, NULL);
				ianjuta_iterable_first (IANJUTA_ITERABLE (search_start), NULL);
			}
			g_object_unref (selection_start);
		}

		g_free (selected_text);
	}

	/* Try searching in current position */
	found = editor_search (search_box->priv->current_editor,
	                       search_text,
	                       search_box->priv->case_sensitive,
	                       search_forward,
	                       search_box->priv->regex_mode,
	                       search_start,
	                       search_end,
	                       &result_start,
	                       &result_end);

	if (found)
	{
		anjuta_status_pop (ANJUTA_STATUS (search_box->priv->status));
	}
	else if (wrap)
	{
		/* Try to wrap if not found */
		ianjuta_iterable_first (IANJUTA_ITERABLE (search_start), NULL);
		ianjuta_iterable_last (IANJUTA_ITERABLE (search_end), NULL);

		/* Try to search again */
		found = editor_search (search_box->priv->current_editor,
		                       search_text,
		                       search_box->priv->case_sensitive,
		                       search_forward,
		                       search_box->priv->regex_mode,
		                       search_start,
		                       search_end,
		                       &result_start,
		                       &result_end);

		/* Check if successful */
		if (found)
		{
			if (ianjuta_iterable_compare (IANJUTA_ITERABLE (result_start),
			                              real_start, NULL) != 0)
			{
				anjuta_status_pop (search_box->priv->status);
				if (search_forward)
				{
					anjuta_status_push (search_box->priv->status,
					                    _("Search for \"%s\" reached the end and was continued at the top."), search_text);
				}
				else
				{
					anjuta_status_push (search_box->priv->status,
					                    _("Search for \"%s\" reached top and was continued at the bottom."), search_text);
				}
			}
			else if (ianjuta_editor_selection_has_selection (selection, NULL))
			{
				found = FALSE;
				anjuta_status_pop (search_box->priv->status);
				if (search_forward)
				{
					anjuta_status_push (search_box->priv->status,
						                _("Search for \"%s\" reached the end and was continued at the top but no new match was found."), search_text);
				}
				else
				{
					anjuta_status_push (search_box->priv->status,
						                _("Search for \"%s\" reached top and was continued at the bottom but no new match was found."), search_text);
				}
			}
			else
			{
				found = FALSE;
			}
		}
	}

	if (found)
	{
		ianjuta_editor_selection_set (selection,
		                              IANJUTA_ITERABLE (result_start),
		                              IANJUTA_ITERABLE (result_end), TRUE, NULL);
		g_object_unref (result_start);
		g_object_unref (result_end);
	}

	search_box_set_entry_color (search_box, found);	
	g_object_unref (real_start);
	g_object_unref (search_start);
	g_object_unref (search_end);

	return found;
}
Ejemplo n.º 3
0
static void
on_glade_drop (IAnjutaEditor* editor,
               IAnjutaIterable* iterator,
               const gchar* signal_data,
               PythonPlugin* lang_plugin)
{
	GSignalQuery query;
	GType type;
	guint id;
	
	const gchar* widget;
	const gchar* signal;
	const gchar* handler;
	const gchar* user_data;
	gboolean swapped;
	GList* names = NULL;
	GString* str = g_string_new (NULL);
	int i;
	IAnjutaIterable* start, * end;
	
	GStrv data = g_strsplit(signal_data, ":", 5);
	
	widget = data[0];
	signal = data[1];
	handler = data[2];
	user_data = data[3];
	swapped = g_str_equal (data[4], "1");
	
	type = g_type_from_name (widget);
	id = g_signal_lookup (signal, type);

	g_signal_query (id, &query);

	g_string_append_printf (str, "\ndef %s (self", handler);
	for (i = 0; i < query.n_params; i++)
	{
		const gchar* type_name = g_type_name (query.param_types[i]);
		const gchar* param_name = language_support_get_signal_parameter (type_name,
		                                                                 &names);

		g_string_append_printf (str, ", %s", param_name);
	}
	g_string_append (str, "):\n");

	ianjuta_editor_insert (editor, iterator,
	                       str->str, -1, NULL);

	/* Indent code correctly */
	start = iterator;
	end = ianjuta_iterable_clone (iterator, NULL);
	ianjuta_iterable_set_position (end, 
	                               ianjuta_iterable_get_position (iterator, NULL)
	                           		+ g_utf8_strlen (str->str, -1),
	                               NULL);
	ianjuta_indenter_indent (IANJUTA_INDENTER (lang_plugin),
	                         start, end, NULL);
	g_object_unref (end);

	g_string_free (str, TRUE);
	anjuta_util_glist_strings_free (names);
	
	g_strfreev (data);
}