static void on_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data) { insert_multiline_comment(document_get_current(), editor_info.click_pos); }
static void on_open_selected_file(GtkMenuItem *menuitem, gpointer user_data) { GeanyDocument *doc = document_get_current(); gchar *sel = NULL; gchar *filename = NULL; gchar *path = NULL; g_return_if_fail(doc != NULL); sel = get_selection(); if (!sel) return; setptr(sel, utils_get_locale_from_utf8(sel)); if (g_path_is_absolute(sel)) { filename = g_strdup(sel); if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { g_free(filename); filename = NULL; } } if (!filename) { if (doc->file_name) { path = g_path_get_dirname(doc->file_name); setptr(path, utils_get_locale_from_utf8(path)); } if (!path) path = g_get_current_dir(); filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL); if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { g_free(filename); filename = NULL; } g_free(path); } if (!filename && geany_data->app->project != NULL) { gchar **pathv; gint i; path = g_strdup(""); pathv = g_strsplit_set(sel, "/\\", -1); for (i = g_strv_length(pathv) - 1; i >= 0; i--) { if (g_strcmp0(pathv[i], "..") == 0) break; setptr(path, g_build_filename(G_DIR_SEPARATOR_S, pathv[i], path, NULL)); } g_strfreev(pathv); if (g_strcmp0(path, "") != 0) { FindData data; data.subpath = path; data.found_path = NULL; g_hash_table_foreach(g_prj->file_tag_table, (GHFunc)find_name, &data); if (data.found_path) { filename = g_strdup(data.found_path); setptr(filename, utils_get_locale_from_utf8(filename)); if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { g_free(filename); filename = NULL; } } } g_free(path); } #ifdef G_OS_UNIX if (!filename) { filename = g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL); if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { g_free(filename); filename = NULL; } } if (!filename) { filename = g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL); if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { g_free(filename); filename = NULL; } } #endif if (filename) open_file(filename); g_free(filename); g_free(sel); }
static void on_swap_header_source(G_GNUC_UNUSED GtkMenuItem * menuitem, G_GNUC_UNUSED gpointer user_data) { GSList *header_patterns, *source_patterns; GeanyDocument *doc; gboolean known_type = TRUE; gboolean is_header; gchar *doc_basename; doc = document_get_current(); if (!g_prj || !geany_data->app->project || !doc || !doc->file_name) return; header_patterns = get_precompiled_patterns(g_prj->header_patterns); source_patterns = get_precompiled_patterns(g_prj->source_patterns); doc_basename = g_path_get_basename(doc->file_name); if (patterns_match(header_patterns, doc_basename)) is_header = TRUE; else if (patterns_match(source_patterns, doc_basename)) is_header = FALSE; else known_type = FALSE; if (known_type) { gboolean swapped; GSList *elem, *list = NULL; gint i; foreach_document(i) { gchar *filename; filename = document_index(i)->file_name; if (gprj_project_is_in_project(filename)) list = g_slist_prepend(list, filename); } swapped = try_swap_header_source(doc->file_name, is_header, list, header_patterns, source_patterns); g_slist_free(list); list = NULL; if (!swapped) { gchar *doc_dir; doc_dir = g_path_get_dirname(doc->file_name); setptr(doc_dir, utils_get_locale_from_utf8(doc_dir)); list = utils_get_file_list(doc_dir, NULL, NULL); for (elem = list; elem != NULL; elem = g_slist_next(elem)) { gchar *full_name; full_name = g_build_filename(doc_dir, elem->data, NULL); setptr(full_name, utils_get_utf8_from_locale(full_name)); setptr(elem->data, full_name); } swapped = try_swap_header_source(doc->file_name, is_header, list, header_patterns, source_patterns); g_slist_foreach(list, (GFunc) g_free, NULL); g_slist_free(list); g_free(doc_dir); list = NULL; } if (!swapped) { g_hash_table_foreach(g_prj->file_tag_table, (GHFunc) get_project_file_list, &list); try_swap_header_source(doc->file_name, is_header, list, header_patterns, source_patterns); g_slist_free(list); } } g_free(doc_basename); g_slist_foreach(header_patterns, (GFunc) g_pattern_spec_free, NULL); g_slist_free(header_patterns); g_slist_foreach(source_patterns, (GFunc) g_pattern_spec_free, NULL); g_slist_free(source_patterns); }
void glatex_kb_insert_enumerate_list(G_GNUC_UNUSED guint key_id) { g_return_if_fail(document_get_current() != NULL); glatex_insert_list_environment(GLATEX_LIST_ENUMERATE); }
static ScintillaObject *scintilla_get_current(void) { GeanyDocument *doc = document_get_current(); return doc ? doc->editor->sci : NULL; }
void sc_gui_kb_run_activate_cb(guint key_id) { perform_check(document_get_current()); }
void glatex_kb_insert_description_list(G_GNUC_UNUSED guint key_id) { g_return_if_fail(document_get_current() != NULL); glatex_insert_list_environment(GLATEX_LIST_DESCRIPTION); }
static void on_scope_reset_markers(G_GNUC_UNUSED const MenuItem *menu_item) { utils_remark(document_get_current()); }
void xml_format(GtkMenuItem* menuitem, gpointer gdata) { /* retrieves the current document */ GeanyDocument* doc = document_get_current(); GeanyEditor* editor; ScintillaObject* sco; int length; char* buffer; xmlDoc* parsedDocument; int result; int xOffset; GeanyFiletype* fileType; g_return_if_fail(doc != NULL); editor = doc->editor; sco = editor->sci; /* default printing options */ if (prettyPrintingOptions == NULL) { prettyPrintingOptions = createDefaultPrettyPrintingOptions(); } /* prepare the buffer that will contain the text * from the scintilla object */ length = sci_get_length(sco)+1; buffer = (char*)malloc(length*sizeof(char)); if (buffer == NULL) { exit(-1); } /* malloc error */ /* retrieves the text */ sci_get_text(sco, length, buffer); /* checks if the data is an XML format */ parsedDocument = xmlParseDoc((unsigned char*)buffer); /* this is not a valid xml => exit with an error message */ if(parsedDocument == NULL) { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to parse the content as XML.")); return; } /* free all */ xmlFreeDoc(parsedDocument); /* process pretty-printing */ result = processXMLPrettyPrinting(&buffer, &length, prettyPrintingOptions); if (result != PRETTY_PRINTING_SUCCESS) { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to process PrettyPrinting on the specified XML because some features are not supported.\n\nSee Help > Debug messages for more details...")); return; } /* updates the document */ sci_set_text(sco, buffer); /* set the line */ xOffset = scintilla_send_message(sco, SCI_GETXOFFSET, 0, 0); scintilla_send_message(sco, SCI_LINESCROLL, -xOffset, 0); /* TODO update with the right function-call for geany-0.19 */ /* sets the type */ fileType = filetypes_index(GEANY_FILETYPES_XML); document_set_filetype(doc, fileType); }
/* Callback for sending file as attachment */ static void send_as_attachment(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata) { GeanyDocument *doc; gchar *locale_filename = NULL; gchar *command = NULL; GError *error = NULL; GString *cmd_str = NULL; gchar *data; doc = document_get_current(); if (doc->file_name == NULL) { dialogs_show_save_as(); } else { document_save_file(doc, FALSE); } if (doc->file_name != NULL) { if (mailer) { locale_filename = utils_get_locale_from_utf8(doc->file_name); cmd_str = g_string_new(mailer); if ((use_address_dialog == TRUE) && (g_strrstr(mailer, "%r") != NULL)) { GKeyFile *config = NULL; gchar *config_dir = NULL; gchar *input = dialogs_show_input(_("Recipient's Address"), GTK_WINDOW(geany->main_widgets->window), _("Enter the recipient's e-mail address:"), address); if (input) { config = g_key_file_new(); g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL); g_free(address); address = input; g_key_file_set_string(config, "tools", "address", address); } else { g_string_free(cmd_str, TRUE); g_free(locale_filename); return; } config_dir = g_path_get_dirname(config_file); if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0) { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Plugin configuration directory could not be created.")); } else { /* write config to file */ data = g_key_file_to_data(config, NULL, NULL); utils_write_file(config_file, data); g_free(data); } g_key_file_free(config); g_free(config_dir); } if (! utils_string_replace_all(cmd_str, "%f", locale_filename)) ui_set_statusbar(FALSE, _("Filename placeholder not found. The executed command might have failed.")); if (use_address_dialog == TRUE && address != NULL) { if (! utils_string_replace_all(cmd_str, "%r", address)) ui_set_statusbar(FALSE, _("Recipient address placeholder not found. The executed command might have failed.")); } else { /* Removes %r if option was not activ but was included into command */ utils_string_replace_all(cmd_str, "%r", ""); } utils_string_replace_all(cmd_str, "%b", g_path_get_basename(locale_filename)); command = g_string_free(cmd_str, FALSE); g_spawn_command_line_async(command, &error); if (error != NULL) { ui_set_statusbar(FALSE, _("Could not execute mailer. Please check your configuration.")); g_error_free(error); } g_free(locale_filename); g_free(command); } else { ui_set_statusbar(FALSE, _("Please define a mail client first.")); } } else { ui_set_statusbar(FALSE, _("File has to be saved before sending.")); } }
/* handler that opens the current filetype's configuration file */ static void open_current_filetype_conf_handler (GtkWidget *widget, gpointer data) { GeanyDocument *doc; (void)widget; (void)data; doc = document_get_current (); if (DOC_VALID (doc)) { gchar *path_read; gchar *path_write; GError *err = NULL; path_write = ggd_file_type_manager_get_conf_path (doc->file_type->id, GGD_PERM_W | GGD_PERM_NOCREAT, &err); if (! path_write) { msgwin_status_add (_("Failed to find configuration file " "for file type \"%s\": %s"), doc->file_type->name, err->message); g_error_free (err); } else { gchar *text = NULL; gchar *path_write_u8; path_read = ggd_file_type_manager_get_conf_path (doc->file_type->id, GGD_PERM_R, &err); if (! path_read) { text = g_strdup (_( "# Configuration for this file type doesn't exist yet.\n" "# To create it, just write it in this file and save it. For the description\n" "# of the syntax of this file, please refer to the manual.\n" )); } else { gchar *content = NULL; gsize length; if (! g_file_get_contents (path_read, &content, &length, &err)) { gchar *display_path_read; display_path_read = g_filename_display_name (path_read); g_warning (_("Failed to load file \"%s\": %s"), display_path_read, err->message); g_free (display_path_read); g_error_free (err); } else { text = encodings_convert_to_utf8 (content, length, NULL); g_free (content); } g_free (path_read); } path_write_u8 = utils_get_utf8_from_locale (path_write); /* It's no Ruby, but it is the closest one I've found. It has: * - # comments * - multi-line double-quoted strings */ document_new_file (path_write_u8, filetypes[GEANY_FILETYPES_RUBY], text); g_free (path_write_u8); g_free (text); g_free (path_write); } } }
static gboolean goto_compiler_file_line(const gchar *fname, gint line, gboolean focus_editor) { gboolean ret = FALSE; gchar *filename; if (!fname || line <= -1) return FALSE; filename = utils_get_locale_from_utf8(fname); /* If the path doesn't exist, try the current document. * This happens when we receive build messages in the wrong order - after the * 'Leaving directory' messages */ if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { gchar *cur_dir = utils_get_current_file_dir_utf8(); gchar *name; if (cur_dir) { /* we let the user know we couldn't find the parsed filename from the message window */ SETPTR(cur_dir, utils_get_locale_from_utf8(cur_dir)); name = g_path_get_basename(filename); SETPTR(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL)); g_free(cur_dir); if (g_file_test(name, G_FILE_TEST_EXISTS)) { ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."), fname); SETPTR(filename, name); } else g_free(name); } } { gchar *utf8_filename = utils_get_utf8_from_locale(filename); GeanyDocument *doc = document_find_by_filename(utf8_filename); GeanyDocument *old_doc = document_get_current(); g_free(utf8_filename); if (doc == NULL) /* file not already open */ doc = document_open_file(filename, FALSE, NULL, NULL); if (doc != NULL) { line = adjust_line_number(doc, line, msgwindow.line_shifts_compiler); if (editor_prefs.use_indicators) editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1); ret = navqueue_goto_line(old_doc, doc, line); if (ret && focus_editor) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); ret = TRUE; } } g_free(filename); return ret; }
/* * create target page */ void tpage_init() { page = gtk_hbox_new(FALSE, 0); GtkWidget *lbox = gtk_vbox_new(FALSE, SPACING); GtkWidget *mbox = gtk_vbox_new(FALSE, SPACING); GtkWidget *rbox = gtk_vbox_new(FALSE, SPACING); GtkWidget* separator = gtk_vseparator_new(); /* right box with Load/Save buttons */ gtk_container_set_border_width(GTK_CONTAINER(rbox), SPACING); loadbtn = gtk_button_new_from_stock(GTK_STOCK_OPEN); g_signal_connect(G_OBJECT(loadbtn), "clicked", G_CALLBACK (on_load_config), (gpointer)TRUE); savebtn = gtk_button_new_from_stock(GTK_STOCK_SAVE); g_signal_connect(G_OBJECT(savebtn), "clicked", G_CALLBACK (on_save_config), NULL); clearbtn = gtk_button_new_from_stock(GTK_STOCK_CLEAR); g_signal_connect(G_OBJECT(clearbtn), "clicked", G_CALLBACK (on_clear), NULL); gtk_box_pack_start(GTK_BOX(rbox), loadbtn, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(rbox), savebtn, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(rbox), clearbtn, FALSE, TRUE, 0); GtkWidget *hombox = gtk_hbox_new(TRUE, 0); /* left box */ gtk_container_set_border_width(GTK_CONTAINER(lbox), SPACING); /* Target frame */ GtkWidget *frame = gtk_frame_new(_("Target")); GtkWidget *vbox = gtk_vbox_new(FALSE, 0); /* filename hbox */ GtkWidget *hbox = gtk_hbox_new(FALSE, SPACING); gtk_container_set_border_width(GTK_CONTAINER(hbox), SPACING); targetname = gtk_entry_new (); button_browse = gtk_button_new_with_label(_("Browse")); g_signal_connect(G_OBJECT(button_browse), "clicked", G_CALLBACK (on_target_browse_clicked), NULL); gtk_box_pack_start(GTK_BOX(hbox), targetname, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox), button_browse, FALSE, TRUE, 0); /* pack in the vertical box */ gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); /* debugger type hbox */ hbox = gtk_hbox_new(FALSE, SPACING); gtk_container_set_border_width(GTK_CONTAINER(hbox), SPACING); GtkWidget *label = gtk_label_new(_("Debugger:")); cmb_debugger = gtk_combo_box_new_text(); GList *modules = debug_get_modules(); GList *iter = modules; while (iter) { gtk_combo_box_append_text(GTK_COMBO_BOX(cmb_debugger), (gchar*)iter->data); iter = iter->next; } g_list_free(modules); gtk_combo_box_set_active(GTK_COMBO_BOX(cmb_debugger), 0); gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), cmb_debugger, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); gtk_container_add(GTK_CONTAINER(frame), vbox); gtk_box_pack_start(GTK_BOX(lbox), frame, FALSE, FALSE, 0); /* Arguments frame */ frame = gtk_frame_new(_("Arguments")); hbox = gtk_vbox_new(FALSE, SPACING); gtk_container_set_border_width(GTK_CONTAINER(hbox), SPACING); textview = gtk_text_view_new (); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_CHAR); gtk_box_pack_start(GTK_BOX(hbox), textview, TRUE, TRUE, 0); gtk_container_add(GTK_CONTAINER(frame), hbox); gtk_box_pack_start(GTK_BOX(lbox), frame, TRUE, TRUE, 0); /* Environment */ gtk_container_set_border_width(GTK_CONTAINER(mbox), SPACING); frame = gtk_frame_new(_("Environment variables")); hbox = gtk_hbox_new(FALSE, SPACING); gtk_container_set_border_width(GTK_CONTAINER(hbox), SPACING); store = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING); model = GTK_TREE_MODEL(store); envtree = gtk_tree_view_new_with_model (model); gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(envtree), TRUE); gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(envtree), GTK_TREE_VIEW_GRID_LINES_VERTICAL); g_object_set(envtree, "rules-hint", TRUE, NULL); g_signal_connect(G_OBJECT(envtree), "key-press-event", G_CALLBACK (on_envtree_keypressed), NULL); gchar *header; int char_width = get_char_width(envtree); header = _("Name"); renderer_name = gtk_cell_renderer_text_new (); g_object_set (renderer_name, "editable", TRUE, NULL); g_signal_connect (G_OBJECT (renderer_name), "edited", G_CALLBACK (on_name_changed), NULL); column_name = create_column(header, renderer_name, FALSE, get_header_string_width(header, MW_NAME, char_width), "text", NAME); gtk_tree_view_append_column (GTK_TREE_VIEW (envtree), column_name); header = _("Value"); renderer_value = gtk_cell_renderer_text_new (); column_value = create_column(header, renderer_value, TRUE, get_header_string_width(header, MW_VALUE, char_width), "text", VALUE); g_signal_connect (G_OBJECT (renderer_value), "edited", G_CALLBACK (on_value_changed), NULL); g_signal_connect (G_OBJECT (renderer_value), "editing-started", G_CALLBACK (on_value_editing_started), NULL); g_signal_connect (G_OBJECT (renderer_value), "editing-canceled", G_CALLBACK (on_value_editing_cancelled), NULL); gtk_tree_view_column_set_cell_data_func(column_value, renderer_value, on_render_value, NULL, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (envtree), column_value); /* add empty row */ add_empty_row(); /* set multiple selection */ GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(envtree)); gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE); gtk_box_pack_start(GTK_BOX(hbox), envtree, TRUE, TRUE, 0); gtk_container_add(GTK_CONTAINER(frame), hbox); gtk_box_pack_start(GTK_BOX(mbox), frame, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hombox), lbox, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hombox), mbox, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(page), hombox, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(page), separator, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(page), rbox, FALSE, TRUE, 0); /* update Load/Save config button */ tpage_on_document_activate(document_get_current()); }
static void kb_run_unix_ts_converter(G_GNUC_UNUSED guint key_id) { unixts_to_string(document_get_current()); }
static void on_menu_comments_multiline_activate(GtkMenuItem *menuitem, gpointer user_data) { insert_multiline_comment(document_get_current(), -1); }
/* if type == -1 then we will try to autodetect the type */ void glatex_insert_environment(const gchar *environment, gint type) { GeanyDocument *doc = NULL; doc = document_get_current(); /* Only do anything if it is realy needed to */ if (doc != NULL && environment != NULL) { if (sci_has_selection(doc->editor->sci)) { gchar *selection = NULL; gchar *replacement = NULL; selection = sci_get_selection_contents(doc->editor->sci); sci_start_undo_action(doc->editor->sci); if (utils_str_equal(environment, "block") == TRUE) { replacement = g_strconcat("\\begin{", environment, "}{}\n", selection, "\n\\end{", environment, "}\n", NULL); } else { replacement = g_strconcat("\\begin{", environment, "}\n", selection, "\n\\end{", environment, "}\n", NULL); } sci_replace_sel(doc->editor->sci, replacement); sci_end_undo_action(doc->editor->sci); g_free(selection); g_free(replacement); } else { gint indent, pos; GString *tmpstring = NULL; gchar *tmp = NULL; static const GeanyIndentPrefs *indention_prefs = NULL; if (type == -1) { gint i; /* First, we check whether we have a known list over here * an reset type to fit new value*/ for (i = 0; i < GLATEX_LIST_END; i++) { if (utils_str_equal(glatex_list_environments[i], environment) == TRUE) { type = GLATEX_ENVIRONMENT_TYPE_LIST; break; } } } pos = sci_get_current_position(doc->editor->sci); sci_start_undo_action(doc->editor->sci); tmpstring = g_string_new("\\begin{"); g_string_append(tmpstring, environment); if (utils_str_equal(environment, "block") == TRUE) { g_string_append(tmpstring, "}{}"); } else { g_string_append(tmpstring, "}"); } g_string_append(tmpstring, "\n"); if (type == GLATEX_ENVIRONMENT_TYPE_LIST) { g_string_append(tmpstring, "\t\\item "); } tmp = g_string_free(tmpstring, FALSE); glatex_insert_string(tmp, TRUE); g_free(tmp); indent = sci_get_line_indentation(doc->editor->sci, sci_get_line_from_position(doc->editor->sci, pos)); tmp = g_strdup_printf("\n\\end{%s}", environment); glatex_insert_string(tmp, FALSE); g_free(tmp); indention_prefs = editor_get_indent_prefs(doc->editor); if (type == GLATEX_ENVIRONMENT_TYPE_LIST) { sci_set_line_indentation(doc->editor->sci, sci_get_current_line(doc->editor->sci), indent + indention_prefs->width); } sci_set_line_indentation(doc->editor->sci, sci_get_current_line(doc->editor->sci) + 1, indent); sci_end_undo_action(doc->editor->sci); } } }
void on_find_prevsel1_activate(GtkMenuItem *menuitem, gpointer user_data) { search_find_selection(document_get_current(), TRUE); }
static void shift_right_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata){ gchar *txt; gchar *txt_i; gchar char_after; gint txt_len; gint startpos; gint endpos; gint startline; gint endline; gint line_iter; gint linepos; gint linelen; gint startcol; gint endcol; gint i; ScintillaObject *sci; /* get a pointer to the scintilla object */ sci = document_get_current()->editor->sci; if (sci_has_selection(sci)){ startpos = sci_get_selection_start(sci); endpos = sci_get_selection_end(sci); /* sanity check -- we dont care which way the block was selected */ if(startpos > endpos){ i = endpos; endpos = startpos; startpos = i; } startline = sci_get_line_from_position(sci, startpos); linepos = sci_get_position_from_line(sci, startline); endline = sci_get_line_from_position(sci, endpos); /* normal mode */ if(startline == endline){ /* get the text in question */ txt_len = endpos - startpos; txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_after = sci_get_char_at(sci, endpos); /* set up new text buf */ (void) g_sprintf(txt, "%c%s", char_after, txt_i); /* start undo */ sci_start_undo_action(sci); /* put the new text in */ sci_set_selection_end(sci, endpos + 1); sci_replace_sel(sci, txt); /* select the right bit again */ sci_set_selection_start(sci, startpos + 1); sci_set_selection_end(sci, endpos + 1); /* end undo */ sci_end_undo_action(sci); g_free(txt); g_free(txt_i); } /* rectangle mode (we hope!) */ else{ startcol = sci_get_col_from_position(sci, startpos); endcol = sci_get_col_from_position(sci, endpos); /* start undo */ sci_start_undo_action(sci); for(line_iter = startline; line_iter <= endline; line_iter++){ linepos = sci_get_position_from_line(sci, line_iter); linelen = sci_get_line_length(sci, line_iter); /* do we need to do something */ if(linelen >= startcol - 1 ){ /* if between the two columns or at the end */ /* add in a space */ if(linelen <= endcol || linelen - 1 == endcol){ txt = g_malloc(sizeof(gchar) * 2); sprintf(txt, " "); sci_insert_text(sci, linepos + startcol, txt); g_free(txt); } else{ /* move the text itself */ sci_set_selection_mode(sci, 0); sci_set_selection_start(sci, linepos + startcol); sci_set_selection_end(sci, linepos + endcol); txt_len = sci_get_selected_text_length(sci); txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_after = sci_get_char_at(sci, linepos + endcol); /* set up new text buf */ (void) g_sprintf(txt, "%c%s", char_after, txt_i); /* put the new text in */ sci_set_selection_end(sci, linepos + endcol + 1); sci_replace_sel(sci, txt); g_free(txt); g_free(txt_i); } } } /* put the selection box back */ /* here we rely upon the last result of linepos */ sci_set_selection_mode(sci, 1); sci_set_selection_start(sci, startpos + 1); sci_set_selection_end(sci, linepos + endcol + 1); /* end undo action */ sci_end_undo_action(sci); } } }
void glatex_kb_format_right(G_GNUC_UNUSED guint key_id) { g_return_if_fail(document_get_current() != NULL); glatex_insert_latex_format(NULL, GINT_TO_POINTER(LATEX_RIGHT)); }
static void shift_left_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata){ gchar *txt; gchar *txt_i; gchar char_before; gint txt_len; gint startpos; gint endpos; gint startline; gint endline; gint line_iter; gint linepos; gint linelen; gint startcol; gint endcol; gint i; gint n_spaces; gchar *spaces; ScintillaObject *sci; /* get a pointer to the scintilla object */ sci = document_get_current()->editor->sci; if (sci_has_selection(sci)){ startpos = sci_get_selection_start(sci); endpos = sci_get_selection_end(sci); /* sanity check -- we dont care which way the block was selected */ if(startpos > endpos){ i = endpos; endpos = startpos; startpos = i; } startline = sci_get_line_from_position(sci, startpos); /* Setting also start point for 1st line */ linepos = sci_get_position_from_line(sci, startline); endline = sci_get_line_from_position(sci, endpos); /* normal mode */ if(startline == endline){ /* get the text in question */ txt_len = endpos - startpos; txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_before = sci_get_char_at(sci, startpos - 1); /* set up new text buf */ (void) g_sprintf(txt, "%s%c", txt_i, char_before); /* start undo */ sci_start_undo_action(sci); /* put the new text in */ sci_set_selection_start(sci, startpos - 1); sci_replace_sel(sci, txt); /* select the right bit again */ sci_set_selection_start(sci, startpos - 1); sci_set_selection_end(sci, endpos - 1); /* end undo */ sci_end_undo_action(sci); g_free(txt); g_free(txt_i); } /* rectangle mode (we hope!) */ else{ startcol = sci_get_col_from_position(sci, startpos); endcol = sci_get_col_from_position(sci, endpos); /* return early for the trivial case */ if(startcol == 0 || startcol == endcol){ return; } /* start undo */ sci_start_undo_action(sci); for(line_iter = startline; line_iter <= endline; line_iter++){ linepos = sci_get_position_from_line(sci, line_iter); linelen = sci_get_line_length(sci, line_iter); /* do we need to do something */ if(linelen >= startcol - 1 ){ /* if between the two columns */ /* pad to the end first */ if(linelen <= endcol){ /* bung in some spaces -- sorry, I dont like tabs */ n_spaces = endcol - linelen + 1; spaces = g_malloc(sizeof(gchar) * (n_spaces + 1)); for(i = 0; i < n_spaces; i++){ spaces[i] = ' '; } spaces[i] = '\0'; sci_insert_text(sci, linepos + linelen - 1, spaces); g_free(spaces); } /* now move the text itself */ sci_set_selection_mode(sci, 0); sci_set_selection_start(sci, linepos + startcol); sci_set_selection_end(sci, linepos + endcol); txt_len = sci_get_selected_text_length(sci); txt_i = g_malloc(txt_len + 1); txt = g_malloc(txt_len + 2); sci_get_selected_text(sci, txt_i); char_before = sci_get_char_at(sci, linepos + startcol - 1); /* set up new text buf */ (void) g_sprintf(txt, "%s%c", txt_i, char_before); /* put the new text in */ sci_set_selection_start(sci, linepos + startcol - 1); sci_replace_sel(sci, txt); g_free(txt); g_free(txt_i); } } /* put the selection box back */ /* here we rely upon the last result of linepos */ sci_set_selection_mode(sci, 1); sci_set_selection_start(sci, startpos - 1); sci_set_selection_end(sci, linepos + endcol - 1); /* end undo action */ sci_end_undo_action(sci); } } }
void glatex_kb_insert_itemize_list(G_GNUC_UNUSED guint key_id) { g_return_if_fail(document_get_current() != NULL); glatex_insert_list_environment(GLATEX_LIST_ITEMIZE); }
static void create_file_save_as_dialog(const gchar *extension, ExportFunc func, gboolean show_zoom_level_checkbox) { GtkWidget *dialog, *vbox; GeanyDocument *doc; ExportInfo *exi; g_return_if_fail(extension != NULL); doc = document_get_current(); g_return_if_fail(doc != NULL); exi = g_new(ExportInfo, 1); exi->doc = doc; exi->export_func = func; exi->have_zoom_level_checkbox = FALSE; dialog = gtk_file_chooser_dialog_new(_("Export File"), GTK_WINDOW(geany->main_widgets->window), GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL); gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE); gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG); gtk_widget_set_name(dialog, "GeanyExportDialog"); gtk_dialog_add_buttons(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); /* file chooser extra widget */ vbox = gtk_vbox_new(FALSE, 0); gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox); { GtkWidget *check_line_numbers; check_line_numbers = gtk_check_button_new_with_mnemonic(_("_Insert line numbers")); gtk_widget_set_tooltip_text(check_line_numbers, _("Insert line numbers before each line in the exported document")); gtk_box_pack_start(GTK_BOX(vbox), check_line_numbers, FALSE, FALSE, 0); gtk_widget_show_all(vbox); ui_hookup_widget(dialog, check_line_numbers, "check_line_numbers"); } if (show_zoom_level_checkbox) { GtkWidget *check_zoom_level; check_zoom_level = gtk_check_button_new_with_mnemonic(_("_Use current zoom level")); gtk_widget_set_tooltip_text(check_zoom_level, _("Renders the font size of the document together with the current zoom level")); gtk_box_pack_start(GTK_BOX(vbox), check_zoom_level, FALSE, FALSE, 0); gtk_widget_show_all(vbox); ui_hookup_widget(dialog, check_zoom_level, "check_zoom_level"); exi->have_zoom_level_checkbox = TRUE; } g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL); g_signal_connect(dialog, "response", G_CALLBACK(on_file_save_dialog_response), exi); gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(geany->main_widgets->window)); /* if the current document has a filename we use it as the default. */ gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog)); if (doc->file_name != NULL) { gchar *base_name = g_path_get_basename(doc->file_name); gchar *file_name; gchar *locale_filename; gchar *locale_dirname; const gchar *suffix = ""; if (g_str_has_suffix(doc->file_name, extension)) suffix = "_export"; file_name = g_strconcat(base_name, suffix, extension, NULL); locale_filename = utils_get_locale_from_utf8(doc->file_name); locale_dirname = g_path_get_dirname(locale_filename); /* set the current name to base_name.html which probably doesn't exist yet so * gtk_file_chooser_set_filename() can't be used and we need * gtk_file_chooser_set_current_folder() additionally */ gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), file_name); g_free(locale_dirname); g_free(locale_filename); g_free(file_name); g_free(base_name); } else { const gchar *default_open_path = geany->prefs->default_open_path; gchar *fname = g_strconcat(GEANY_STRING_UNTITLED, extension, NULL); gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog)); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname); /* use default startup directory(if set) if no files are open */ if (!EMPTY(default_open_path) && g_path_is_absolute(default_open_path)) { gchar *locale_path = utils_get_locale_from_utf8(default_open_path); gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path); g_free(locale_path); } g_free(fname); } gtk_dialog_run(GTK_DIALOG(dialog)); }
void glatex_kb_structure_lvlup(G_GNUC_UNUSED guint key_id) { g_return_if_fail(document_get_current() != NULL); glatex_structure_lvlup(); }
static void do_format(GeanyDocument *doc, bool entire_doc, bool autof) { GString *formatted; ScintillaObject *sci; size_t offset = 0, length = 0, sci_len; size_t cursor_pos, old_first_line, new_first_line, line_delta; const char *sci_buf; bool changed = true; bool was_changed; if (doc == NULL) doc = document_get_current(); if (!DOC_VALID(doc)) { g_warning("Cannot format with no documents open"); return; } sci = doc->editor->sci; was_changed = doc->changed; // FIXME: instead of failing, ask user to save the document once if (!doc->real_path) { g_warning("Cannot format document that's never been saved"); return; } if (!entire_doc) { if (sci_has_selection(sci)) { // format selection offset = sci_get_selection_start(sci); length = sci_get_selection_end(sci) - offset; } else { // format current line size_t cur_line = sci_get_current_line(sci); offset = sci_get_position_from_line(sci, cur_line); length = sci_get_line_end_position(sci, cur_line) - offset; } } else { // format entire document offset = 0; length = sci_get_length(sci); } cursor_pos = sci_get_current_position(sci); sci_len = sci_get_length(sci); sci_buf = (const char *)scintilla_send_message(sci, SCI_GETCHARACTERPOINTER, 0, 0); formatted = fmt_clang_format(doc->file_name, sci_buf, sci_len, &cursor_pos, offset, length, false); // FIXME: handle better if (formatted == NULL) return; if (!autof) { changed = (formatted->len != sci_len) || (g_strcmp0(formatted->str, sci_buf) != 0); } old_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); // Replace document text and move cursor to new position scintilla_send_message(sci, SCI_BEGINUNDOACTION, 0, 0); scintilla_send_message(sci, SCI_CLEARALL, 0, 0); scintilla_send_message(sci, SCI_ADDTEXT, formatted->len, (sptr_t)formatted->str); scintilla_send_message(sci, SCI_GOTOPOS, cursor_pos, 0); new_first_line = scintilla_send_message(sci, SCI_GETFIRSTVISIBLELINE, 0, 0); line_delta = new_first_line - old_first_line; scintilla_send_message(sci, SCI_LINESCROLL, 0, -line_delta); scintilla_send_message(sci, SCI_ENDUNDOACTION, 0, 0); document_set_text_changed(doc, (was_changed || changed)); g_string_free(formatted, true); }