static gboolean match_keyword (MacroPlugin * plugin, GtkTreeIter * iter, const gchar *keyword) { gchar *name; gint offset = 0; gtk_tree_model_get(macro_db_get_model(plugin->macro_db), iter, MACRO_NAME, &name, -1); if ( name && strcmp(keyword, name) == 0) { gchar* text = macro_db_get_macro(plugin, plugin->macro_db, iter, &offset); if (plugin->current_editor != NULL && text != NULL) { gint i; IAnjutaIterable *pos = ianjuta_editor_get_position (IANJUTA_EDITOR(plugin->current_editor), NULL); ianjuta_editor_insert (IANJUTA_EDITOR (plugin->current_editor), pos, text, -1, NULL); for (i = 0; i < offset; i++) ianjuta_iterable_next (pos, NULL); ianjuta_editor_goto_position (IANJUTA_EDITOR(plugin->current_editor), pos, NULL); g_free(text); g_object_unref (pos); } return TRUE; } return FALSE; }
static gint set_line_indentation (IndentCPlugin *plugin, IAnjutaEditor *editor, gint line_num, gint indentation, gint parenthesis_indentation) { IAnjutaIterable *line_begin, *line_end, *indent_position; IAnjutaIterable *current_pos; gint carat_offset, nchars = 0; gchar *old_indent_string = NULL, *indent_string = NULL; /* DEBUG_PRINT ("In %s()", __FUNCTION__); */ line_begin = ianjuta_editor_get_line_begin_position (editor, line_num, NULL); line_end = ianjuta_editor_get_line_end_position (editor, line_num, NULL); /* DEBUG_PRINT ("line begin = %d, line end = %d, current_pos = %d", line_begin, line_end, current_pos); */ indent_position = ianjuta_iterable_clone (line_begin, NULL); if (ianjuta_iterable_compare (line_end, line_begin, NULL) > 0) { gchar *idx; gchar *line_string = ianjuta_editor_get_text (editor, line_begin, line_end, NULL); //DEBUG_PRINT ("line_string = '%s'", line_string); if (line_string) { idx = line_string; /* Find first non-white space */ while (*idx != '\0' && isspace (*idx)) { idx = g_utf8_find_next_char (idx, NULL); ianjuta_iterable_next (indent_position, NULL); } g_free (line_string); } } /* Indent iter defined at this point, Identify how much is current * position is beyound this point. We need to restore it later after * indentation */ current_pos = ianjuta_editor_get_position (editor, NULL); carat_offset = ianjuta_iterable_diff (indent_position, current_pos, NULL); //DEBUG_PRINT ("carat offset is = %d", carat_offset); /* Set new indentation */ if ((indentation + parenthesis_indentation) > 0) { indent_string = get_line_indentation_string (plugin, editor, indentation, parenthesis_indentation); nchars = indent_string ? g_utf8_strlen (indent_string, -1) : 0; /* Only indent if there is something to indent with */ if (indent_string) { /* Get existing indentation */ if (ianjuta_iterable_compare (indent_position, line_begin, NULL) > 0) { old_indent_string = ianjuta_editor_get_text (editor, line_begin, indent_position, NULL); //DEBUG_PRINT ("old_indent_string = '%s'", old_indent_string); } /* Only indent if there was no indentation before or old * indentation string was different from the new indent string */ if (old_indent_string == NULL || strcmp (old_indent_string, indent_string) != 0) { /* Remove the old indentation string, if there is any */ if (old_indent_string) ianjuta_editor_erase (editor, line_begin, indent_position, NULL); /* Insert the new indentation string */ ianjuta_editor_insert (editor, line_begin, indent_string, -1, NULL); } } } /* If indentation == 0, we really didn't enter the previous code block, * but we may need to clear existing indentation. */ if ((indentation + parenthesis_indentation) == 0) { /* Get existing indentation */ if (ianjuta_iterable_compare (indent_position, line_begin, NULL) > 0) { old_indent_string = ianjuta_editor_get_text (editor, line_begin, indent_position, NULL); } if (old_indent_string) ianjuta_editor_erase (editor, line_begin, indent_position, NULL); } /* Restore current position */ if (carat_offset >= 0) { /* If the cursor was not before the first non-space character in * the line, restore it's position after indentation. */ gint i; IAnjutaIterable *pos = ianjuta_editor_get_line_begin_position (editor, line_num, NULL); for (i = 0; i < nchars + carat_offset; i++) ianjuta_iterable_next (pos, NULL); ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL); ianjuta_editor_goto_position (editor, pos, NULL); ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL); g_object_unref (pos); } else /* cursor_offset < 0 */ { /* If the cursor was somewhere in the old indentation spaces, * home the cursor to first non-space character in the line (or * end of line if there is no non-space characters in the line. */ gint i; IAnjutaIterable *pos = ianjuta_editor_get_line_begin_position (editor, line_num, NULL); for (i = 0; i < nchars; i++) ianjuta_iterable_next (pos, NULL); ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL); ianjuta_editor_goto_position (editor, pos, NULL); ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL); g_object_unref (pos); } g_object_unref (current_pos); g_object_unref (indent_position); g_object_unref (line_begin); g_object_unref (line_end); g_free (old_indent_string); g_free (indent_string); return nchars; }
void cpp_java_indentation_char_added (IAnjutaEditor *editor, IAnjutaIterable *insert_pos, gchar ch, IndentCPlugin *plugin) { IAnjutaEditorAttribute attrib; IAnjutaIterable *iter; gboolean should_auto_indent = FALSE; iter = ianjuta_iterable_clone (insert_pos, NULL); /* If autoindent is enabled*/ if (plugin->smart_indentation) { /* DEBUG_PRINT ("Char added at position %d: '%c'", insert_pos, ch); */ if (iter_is_newline (iter, ch)) { skip_iter_to_newline_head (iter, ch); /* All newline entries means enable indenting */ should_auto_indent = TRUE; } else if (ch == '{' || ch == '}' || ch == '#') { /* Indent only when it's the first non-white space char in the line */ /* Don't bother if we are inside string */ attrib = ianjuta_editor_cell_get_attribute (IANJUTA_EDITOR_CELL (iter), NULL); if (attrib != IANJUTA_EDITOR_STRING) { /* Iterate backwards till the begining of the line and disable * indenting if any non-white space char is encountered */ /* Begin by assuming it should be indented */ should_auto_indent = TRUE; while (ianjuta_iterable_previous (iter, NULL)) { ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter), 0, NULL); //DEBUG_PRINT ("Looking at char '%c'", ch); /* Break on begining of line (== end of previous line) */ if (iter_is_newline (iter, ch)) { skip_iter_to_newline_head (iter, ch); break; } /* If a non-white space char is encountered, disabled indenting */ if (!isspace (ch)) { should_auto_indent = FALSE; break; } } } } if (should_auto_indent) { gint insert_line; gint line_indent; gint parenthesis_indentation; ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL); insert_line = ianjuta_editor_get_lineno (editor, NULL); line_indent = get_line_auto_indentation (plugin, editor, insert_line, &parenthesis_indentation); set_line_indentation (plugin, editor, insert_line, line_indent, parenthesis_indentation); ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL); } } if (g_settings_get_boolean (plugin->settings, PREF_BRACE_AUTOCOMPLETION)) { if (ch == '[' || ch == '(') { gchar *prev_char, *next_char; IAnjutaIterable *previous, *next, *next_end; previous = ianjuta_iterable_clone (iter, NULL); ianjuta_iterable_previous (previous, NULL); prev_char = ianjuta_editor_get_text (editor, previous, iter, NULL); next = ianjuta_iterable_clone (iter, NULL); ianjuta_iterable_next (next, NULL); next_end = ianjuta_iterable_clone (next, NULL); ianjuta_iterable_next (next_end, NULL); next_char = ianjuta_editor_get_text (editor, next, next_end, NULL); /* If the previous char is a ' we don't have to autocomplete, also we only autocomplete if the next character is white-space a closing bracket, "," or ";" */ if (*prev_char != '\'' && (g_ascii_isspace(*next_char) || is_closing_bracket (*next_char) || *next_char == ',' || *next_char == ';'|| *next_char == '\0')) { ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (editor), NULL); ianjuta_iterable_next (iter, NULL); switch (ch) { case '[': insert_editor_blocked (editor, iter, "]", plugin); break; case '(': insert_editor_blocked (editor, iter, ")", plugin); break; default: break; } ianjuta_editor_goto_position (editor, iter, NULL); ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (editor), NULL); } g_object_unref (previous); } else if (ch == '"' || ch == '\'') { gchar *prev_char; IAnjutaIterable *previous; previous = ianjuta_iterable_clone (iter, NULL); ianjuta_iterable_previous (previous, NULL); prev_char = ianjuta_editor_get_text (editor, previous, iter, NULL); /* First iter*/ ianjuta_iterable_next (iter, NULL); /* * If the character is " we have to decide if we need insert * another " or we have to skip the character */ if (ch == '"' || ch == '\'') { /* * Now we have to detect if we want to manage " as a char */ if (*prev_char != '\'' && *prev_char != '\\') { gchar *c; if (ch == '"') c = g_strdup ("\""); else c = g_strdup ("'"); ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT (editor), NULL); insert_editor_blocked (editor, iter, c, plugin); ianjuta_editor_goto_position (editor, iter, NULL); ianjuta_document_end_undo_action (IANJUTA_DOCUMENT (editor), NULL); g_free (c); } g_object_unref (previous); g_object_unref (iter); return; } g_object_unref (previous); } } g_object_unref (iter); }