static void on_toggle_comment (GtkAction *action, gpointer data) { gint line; gboolean has_selection; CppJavaPlugin *lang_plugin; IAnjutaEditor *editor; lang_plugin = ANJUTA_PLUGIN_CPP_JAVA (data); editor = IANJUTA_EDITOR (lang_plugin->current_editor); ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL); has_selection = ianjuta_editor_selection_has_selection (IANJUTA_EDITOR_SELECTION (editor), NULL); if (has_selection) { IAnjutaIterable *sel_start, *sel_end; sel_start = ianjuta_editor_selection_get_start (IANJUTA_EDITOR_SELECTION (editor), NULL); sel_end = ianjuta_editor_selection_get_end (IANJUTA_EDITOR_SELECTION (editor), NULL); toggle_comment_multiline (editor, sel_start, sel_end); g_object_unref (sel_start); g_object_unref (sel_end); } else { line = ianjuta_editor_get_lineno (IANJUTA_EDITOR(editor), NULL); toggle_comment_singleline (lang_plugin, editor, line); } ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL); }
void cpp_auto_indentation (IAnjutaEditor *editor, IndentCPlugin *lang_plugin, IAnjutaIterable *start, IAnjutaIterable *end) { gint line_start, line_end; gint insert_line; gint line_indent; gboolean has_selection; has_selection = ianjuta_editor_selection_has_selection (IANJUTA_EDITOR_SELECTION (editor), NULL); if (start == NULL || end == NULL) { if (has_selection) { IAnjutaIterable *sel_start, *sel_end; sel_start = ianjuta_editor_selection_get_start (IANJUTA_EDITOR_SELECTION (editor), NULL); sel_end = ianjuta_editor_selection_get_end (IANJUTA_EDITOR_SELECTION (editor), NULL); line_start = ianjuta_editor_get_line_from_position (editor, sel_start, NULL); line_end = ianjuta_editor_get_line_from_position (editor, sel_end, NULL); g_object_unref (sel_start); g_object_unref (sel_end); } else { line_start = ianjuta_editor_get_lineno (IANJUTA_EDITOR(editor), NULL); line_end = line_start; } } else { line_start = ianjuta_editor_get_line_from_position (editor, start, NULL); line_end = ianjuta_editor_get_line_from_position (editor, end, NULL); } ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL); for (insert_line = line_start; insert_line <= line_end; insert_line++) { gint parenthesis_indentation = 0; line_indent = get_line_auto_indentation (lang_plugin, editor, insert_line, &parenthesis_indentation); /* DEBUG_PRINT ("Line indent for line %d = %d", insert_line, line_indent); */ set_line_indentation (lang_plugin, editor, insert_line, line_indent, parenthesis_indentation); } ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL); }
static void on_replace_all_activated (GtkWidget* widget, SearchBox* search_box) { IAnjutaIterable* cursor; if (!search_box->priv->current_editor) return; /* Cache current position and search from begin */ cursor = ianjuta_editor_get_position (IANJUTA_EDITOR (search_box->priv->current_editor), NULL); ianjuta_editor_goto_start (IANJUTA_EDITOR (search_box->priv->current_editor), NULL); /* Replace all instances of search_entry with replace_entry text */ ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (search_box->priv->current_editor), NULL); while (search_box_incremental_search (search_box, TRUE, TRUE, FALSE)) { search_box_replace (search_box, widget, FALSE); } ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (search_box->priv->current_editor), NULL); /* Back to cached position */ ianjuta_editor_selection_set (IANJUTA_EDITOR_SELECTION (search_box->priv->current_editor), cursor, cursor, TRUE, NULL); g_object_unref (cursor); }
void search_box_fill_search_focus (SearchBox* search_box, gboolean on_replace) { IAnjutaEditor* te = search_box->priv->current_editor; if (IANJUTA_IS_EDITOR (te) && !search_box->priv->regex_mode) { gchar *buffer; buffer = ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te), NULL); if (buffer != NULL) { g_strstrip (buffer); if (*buffer != 0) { gtk_entry_set_text (GTK_ENTRY (search_box->priv->search_entry), buffer); gtk_editable_select_region (GTK_EDITABLE (search_box->priv->search_entry), 0, -1); } g_free (buffer); } } /* Toggle replace level (replace entry, replace buttons) of search box */ search_box_set_replace (search_box, on_replace); gtk_widget_grab_focus (search_box->priv->search_entry); }
static void on_search_box_entry_changed (GtkWidget * widget, SearchBox * search_box) { if (!search_box->priv->regex_mode) { GtkEntryBuffer* buffer = gtk_entry_get_buffer (GTK_ENTRY(widget)); if (gtk_entry_buffer_get_length (buffer)) search_box_incremental_search (search_box, TRUE, FALSE, TRUE); else { /* clear selection */ IAnjutaIterable* cursor = ianjuta_editor_get_position (IANJUTA_EDITOR (search_box->priv->current_editor), NULL); ianjuta_editor_selection_set (IANJUTA_EDITOR_SELECTION (search_box->priv->current_editor), cursor, cursor, FALSE, NULL); } } if (search_box->priv->highlight_all) search_box_highlight_all (search_box); }
static gboolean search_box_replace (SearchBox * search_box, GtkWidget * widget, gboolean undo /* treat as undo action */) { IAnjutaEditorSelection* selection; gchar * selection_text; gboolean replace_successful = FALSE; const gchar* replace_text = gtk_entry_get_text (GTK_ENTRY (search_box->priv->replace_entry)); const gchar* search_text = gtk_entry_get_text (GTK_ENTRY (search_box->priv->search_entry)); selection = IANJUTA_EDITOR_SELECTION (search_box->priv->current_editor); selection_text = ianjuta_editor_selection_get (selection, NULL); if (ianjuta_editor_selection_has_selection (selection, NULL)) { if (search_box->priv->regex_mode) { GRegex * regex; gchar * replacement_text; gint start_pos, end_pos; GError * err = NULL; gboolean result = search_regex_in_text (search_text, selection_text, TRUE, &start_pos, &end_pos); if (result) { regex = g_regex_new (search_text, 0, 0, NULL); replacement_text = g_regex_replace(regex, selection_text, strlen(selection_text), 0, replace_text, 0, &err); if (err) { g_message ("%s",err->message); g_error_free (err); g_regex_unref(regex); } else { if (undo) ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (selection), NULL); ianjuta_editor_selection_replace (selection, replacement_text, strlen(replacement_text), NULL); if (undo) ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (selection), NULL); replace_successful = TRUE; } if (regex) g_regex_unref(regex); if (replacement_text) g_free(replacement_text); } } else if ((search_box->priv->case_sensitive && g_str_equal (selection_text, search_text)) || (!search_box->priv->case_sensitive && strcasecmp (selection_text, search_text) == 0)) { if (undo) ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (selection), NULL); ianjuta_editor_selection_replace (selection, replace_text, strlen(replace_text), NULL); if (undo) ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (selection), NULL); replace_successful = TRUE; } g_free(selection_text); } return replace_successful; }
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; }