static gboolean gb_vim_command_syntax (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) { GtkSourceView *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); if (g_str_equal (options, "enable") || g_str_equal (options, "on")) g_object_set (source_view, "highlight-syntax", TRUE, NULL); else if (g_str_equal (options, "off")) g_object_set (source_view, "highlight-syntax", FALSE, NULL); else { g_set_error (error, GB_VIM_ERROR, GB_VIM_ERROR_UNKNOWN_OPTION, _("Invalid :syntax subcommand: %s"), options); return FALSE; } return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_syntax (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_PAGE (active_widget)) { IdeBuffer *buffer = ide_editor_page_get_buffer (IDE_EDITOR_PAGE (active_widget)); if (g_str_equal (options, "enable") || g_str_equal (options, "on")) gtk_source_buffer_set_highlight_syntax (GTK_SOURCE_BUFFER (buffer), TRUE); else if (g_str_equal (options, "off")) gtk_source_buffer_set_highlight_syntax (GTK_SOURCE_BUFFER (buffer), FALSE); else { g_set_error (error, GB_VIM_ERROR, GB_VIM_ERROR_UNKNOWN_OPTION, _("Invalid :syntax subcommand: %s"), options); return FALSE; } return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_jump_to_line (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) { GtkSourceView *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); GtkTextBuffer *buffer; gboolean extend_selection; gint line; if (!int32_parse (&line, options, 0, G_MAXINT32, "line number", error)) return FALSE; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view)); extend_selection = gtk_text_buffer_get_has_selection (buffer); ide_source_view_set_count (IDE_SOURCE_VIEW (source_view), line); if (line == 0) { GtkTextIter iter; /* * Zero is not a valid line number, and IdeSourceView treats * that as a move to the end of the buffer. Instead, we want * line 1 to be like vi/vim. */ gtk_text_buffer_get_start_iter (buffer, &iter); gtk_text_buffer_select_range (buffer, &iter, &iter); gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (source_view), gtk_text_buffer_get_insert (buffer), 0.0, FALSE, 0.0, 0.0); } else { g_signal_emit_by_name (source_view, "movement", IDE_SOURCE_VIEW_MOVEMENT_NTH_LINE, extend_selection, TRUE, TRUE); } ide_source_view_set_count (IDE_SOURCE_VIEW (source_view), 0); g_signal_emit_by_name (source_view, "save-insert-mark"); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_wq (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) return (gb_vim_command_write (active_widget, command, options, error) && gb_vim_command_quit (active_widget, command, options, error)); else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_nohl (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_PAGE (active_widget)) { IdeEditorSearch *search = ide_editor_page_get_search (IDE_EDITOR_PAGE (active_widget)); ide_editor_search_set_visible (search, FALSE); return TRUE; } return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_cprevious (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) { GtkSourceView *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); g_signal_emit_by_name (source_view, "move-error", GTK_DIR_UP); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_write (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) { GtkSourceView *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); ide_widget_action (GTK_WIDGET (source_view), "view", "save", NULL); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_cprevious (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_PAGE (active_widget)) { IdeSourceView *source_view = ide_editor_page_get_view (IDE_EDITOR_PAGE (active_widget)); g_signal_emit_by_name (source_view, "move-error", GTK_DIR_UP); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_write (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_PAGE (active_widget)) { IdeSourceView *source_view = ide_editor_page_get_view (IDE_EDITOR_PAGE (active_widget)); dzl_gtk_widget_action (GTK_WIDGET (source_view), "editor-page", "save", NULL); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_sort (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) { GtkSourceView *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); g_signal_emit_by_name (source_view, "sort", FALSE, FALSE); g_signal_emit_by_name (source_view, "clear-selection"); g_signal_emit_by_name (source_view, "set-mode", NULL, IDE_SOURCE_VIEW_MODE_TYPE_PERMANENT); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_nohl (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) { GtkSourceSearchContext *context = NULL; GtkSourceView *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); g_object_get (source_view, "search-context", &context, NULL); g_object_set (context, "highlight", FALSE, NULL); g_clear_object (&context); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_colorscheme (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { g_assert (GTK_IS_WIDGET (active_widget)); if (IDE_IS_EDITOR_VIEW (active_widget)) { GtkSourceStyleSchemeManager *manager; GtkSourceStyleScheme *style_scheme; GtkTextBuffer *buffer; g_autofree gchar *trimmed = NULL; GtkSourceView *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); trimmed = g_strstrip (g_strdup (options)); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view)); manager = gtk_source_style_scheme_manager_get_default (); style_scheme = gtk_source_style_scheme_manager_get_scheme (manager, trimmed); if (style_scheme == NULL) { g_set_error (error, GB_VIM_ERROR, GB_VIM_ERROR_UNKNOWN_OPTION, _("Cannot find colorscheme '%s'"), options); return FALSE; } g_object_set (buffer, "style-scheme", style_scheme, NULL); return TRUE; } else return gb_vim_set_source_view_error (error); }
static gboolean gb_vim_command_set (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { GtkSourceView *source_view; gboolean ret = FALSE; gchar **parts; gsize i; g_assert (GTK_IS_WIDGET (active_widget)); g_assert (command); g_assert (options); if (IDE_IS_EDITOR_VIEW (active_widget)) source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); else return gb_vim_set_source_view_error (error); parts = g_strsplit (options, " ", 0); if (parts [0] == NULL) { ret = TRUE; goto cleanup; } for (i = 0; parts [i]; i++) { const GbVimSet *set; const gchar *value = ""; gchar *key = parts [i]; gchar *tmp; for (tmp = key; *tmp; tmp = g_utf8_next_char (tmp)) { if (g_utf8_get_char (tmp) == '=') { *tmp = '\0'; value = ++tmp; break; } } set = lookup_set (key); if (set == NULL) { g_set_error (error, GB_VIM_ERROR, GB_VIM_ERROR_UNKNOWN_OPTION, _("Unknown option: %s"), key); goto cleanup; } if (!set->func (source_view, key, value, error)) goto cleanup; } ret = TRUE; cleanup: g_strfreev (parts); return ret; }
static gboolean gb_vim_command_search (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { GtkSourceView *source_view; GtkTextBuffer *buffer; const gchar *search_begin = NULL; const gchar *search_end = NULL; const gchar *replace_begin = NULL; const gchar *replace_end = NULL; gchar *search_text = NULL; gchar *replace_text = NULL; gunichar separator; gboolean confirm_replace = FALSE; g_assert (GTK_IS_WIDGET (active_widget)); g_assert (g_str_has_prefix (command, "%s") || g_str_has_prefix (command, "s")); if (IDE_IS_EDITOR_VIEW (active_widget)) source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view); else return gb_vim_set_source_view_error (error); if (*command == '%') command++; command++; separator = g_utf8_get_char (command); if (!separator) goto invalid_request; search_begin = command = g_utf8_next_char (command); for (; *command; command = g_utf8_next_char (command)) { if (*command == '\\') { command = g_utf8_next_char (command); if (!*command) goto invalid_request; continue; } if (g_utf8_get_char (command) == separator) { search_end = command; break; } } if (!search_end) goto invalid_request; replace_begin = command = g_utf8_next_char (command); for (; *command; command = g_utf8_next_char (command)) { if (*command == '\\') { command = g_utf8_next_char (command); if (!*command) goto invalid_request; continue; } if (g_utf8_get_char (command) == separator) { replace_end = command; break; } } if (!replace_end) goto invalid_request; command = g_utf8_next_char (command); if (*command) { for (; *command; command++) { switch (*command) { case 'c': confirm_replace = TRUE; break; case 'g': break; /* what other options are supported? */ default: break; } } } search_text = g_strndup (search_begin, search_end - search_begin); replace_text = g_strndup (replace_begin, replace_end - replace_begin); if (confirm_replace) { GVariant *variant; GVariantBuilder builder; g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY); g_variant_builder_add (&builder, "s", search_text); g_variant_builder_add (&builder, "s", replace_text); variant = g_variant_builder_end (&builder); ide_widget_action (GTK_WIDGET (IDE_EDITOR_VIEW (active_widget)->frame1), "frame", "replace-confirm", variant); return TRUE; } buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view)); if (gtk_text_buffer_get_has_selection (buffer)) { GtkTextIter begin; GtkTextIter end; gtk_text_buffer_get_selection_bounds (buffer, &begin, &end); gtk_text_iter_order (&begin, &end); gb_vim_do_search_and_replace (buffer, &begin, &end, search_text, replace_text, FALSE); } else gb_vim_do_search_and_replace (buffer, NULL, NULL, search_text, replace_text, TRUE); g_free (search_text); g_free (replace_text); return TRUE; invalid_request: g_set_error (error, GB_VIM_ERROR, GB_VIM_ERROR_UNKNOWN_OPTION, _("Invalid search and replace request")); return FALSE; }
static gboolean gb_vim_command_substitute (GtkWidget *active_widget, const gchar *command, const gchar *options, GError **error) { IdeSourceView *source_view; GtkTextBuffer *buffer; const gchar *search_begin = NULL; const gchar *search_end = NULL; const gchar *replace_begin = NULL; const gchar *replace_end = NULL; g_autofree gchar *search_text = NULL; g_autofree gchar *replace_text = NULL; GtkTextIter *substitute_begin = NULL; GtkTextIter *substitute_end = NULL; gunichar separator; gboolean replace_in_every_line = FALSE; gboolean replace_every_occurence_in_line = FALSE; gboolean replace_ask_for_confirmation = FALSE; GtkTextIter selection_begin, selection_end; g_assert (GTK_IS_WIDGET (active_widget)); g_assert (g_str_has_prefix (command, "%s") || g_str_has_prefix (command, "s")); if (IDE_IS_EDITOR_PAGE (active_widget)) source_view = ide_editor_page_get_view (IDE_EDITOR_PAGE (active_widget)); else return gb_vim_set_source_view_error (error); if (command[0] == '%') { replace_in_every_line = TRUE; command++; } command++; separator = g_utf8_get_char (command); if (!separator) goto invalid_request; search_begin = command = g_utf8_next_char (command); for (; *command; command = g_utf8_next_char (command)) { if (*command == '\\') { command = g_utf8_next_char (command); if (!*command) goto invalid_request; continue; } if (g_utf8_get_char (command) == separator) { search_end = command; break; } } if (search_end == NULL) { search_text = g_strdup (search_begin); replace_text = g_strdup (""); } else { search_text = g_strndup (search_begin, search_end - search_begin); replace_begin = command = g_utf8_next_char (command); for (; *command; command = g_utf8_next_char (command)) { if (*command == '\\') { command = g_utf8_next_char (command); if (!*command) goto invalid_request; continue; } if (g_utf8_get_char (command) == separator) { replace_end = command; break; } } if (replace_end == NULL) replace_text = g_strdup (replace_begin); else { replace_text = g_strndup (replace_begin, replace_end - replace_begin); command = g_utf8_next_char (command); } if (*command) { for (; *command; command++) { switch (*command) { case 'c': replace_ask_for_confirmation = TRUE; break; case 'g': replace_every_occurence_in_line = TRUE; break; /* what other options are supported? */ default: break; } } } } if (replace_ask_for_confirmation) { GVariant *variant; GVariantBuilder builder; g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY); g_variant_builder_add (&builder, "s", search_text); g_variant_builder_add (&builder, "s", replace_text); variant = g_variant_builder_end (&builder); dzl_gtk_widget_action (active_widget, "editor-page", "replace-confirm", variant); return TRUE; } buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view)); if (gtk_text_buffer_get_has_selection (buffer)) { gtk_text_buffer_get_selection_bounds (buffer, &selection_begin, &selection_end); substitute_begin = &selection_begin; substitute_end = &selection_end; } gtk_text_buffer_begin_user_action (buffer); gb_vim_do_substitute (buffer, substitute_begin, substitute_end, search_text, replace_text, replace_every_occurence_in_line, replace_in_every_line); gtk_text_buffer_end_user_action (buffer); return TRUE; invalid_request: g_set_error (error, GB_VIM_ERROR, GB_VIM_ERROR_UNKNOWN_OPTION, _("Invalid search and replace request")); return FALSE; }