void open_file(gchar *utf8_name) { gchar *name; GeanyDocument *doc; name = utils_get_locale_from_utf8(utf8_name); doc = document_find_by_filename(utf8_name); if (!doc) { document_open_file(name, FALSE, NULL, NULL); } else { GtkNotebook *notebook; gint page_num; notebook = GTK_NOTEBOOK(geany->main_widgets->notebook); page_num = gtk_notebook_page_num(notebook, GTK_WIDGET(doc->editor->sci)); gtk_notebook_set_current_page(notebook, page_num); } g_free(name); }
static void on_config_file_clicked(G_GNUC_UNUSED GtkWidget *widget, gpointer user_data) { const gchar *file_name = user_data; GeanyFiletype *ft = NULL; if (strstr(file_name, G_DIR_SEPARATOR_S "filetypes.")) ft = filetypes_index(GEANY_FILETYPES_CONF); if (g_file_test(file_name, G_FILE_TEST_EXISTS)) document_open_file(file_name, FALSE, ft, NULL); else { gchar *utf8_filename = utils_get_utf8_from_locale(file_name); gchar *base_name = g_path_get_basename(file_name); gchar *global_file = g_build_filename(geany->app->datadir, base_name, NULL); gchar *global_content = NULL; /* if the requested file doesn't exist in the user's config dir, try loading the file * from the global data directory and use its contents for the newly created file */ if (g_file_test(global_file, G_FILE_TEST_EXISTS)) g_file_get_contents(global_file, &global_content, NULL, NULL); document_new_file(utf8_filename, ft, global_content); g_free(utf8_filename); g_free(base_name); g_free(global_file); g_free(global_content); } }
static void goto_file_line_cb(const gchar * filename, const gchar * line, const gchar * reason) { gint pos; gint page; GeanyDocument *doc; gint line_num = gdbio_atoi((gchar *) line) - 1; if (reason) { msgwin_compiler_add(COLOR_BLUE, "%s", reason); } doc = document_open_file(filename, FALSE, NULL, NULL); if (!(doc && doc->is_valid)) { return; } page = gtk_notebook_page_num(NOTEBOOK, GTK_WIDGET(doc->editor->sci)); gtk_notebook_set_current_page(NOTEBOOK, page); pos = sci_get_position_from_line(doc->editor->sci, line_num); sci_ensure_line_is_visible(doc->editor->sci, line_num); while (gtk_events_pending()) { gtk_main_iteration(); } sci_set_current_position(doc->editor->sci, pos, TRUE); gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); gtk_window_present(GTK_WINDOW(geany->main_widgets->window)); }
static PyObject* Document_open_file(PyObject *self, PyObject *args, PyObject *kwargs) { gint read_only = 0; gchar *filename = NULL, *forced_encoding = NULL; GeanyDocument *doc; GeanyFiletype *ft = NULL; Filetype *filetype = NULL; PyObject *py_ft = NULL; static gchar *kwlist[] = { "filename", "read_only", "filetype", "forced_enc", NULL }; if (PyArg_ParseTupleAndKeywords(args, kwargs, "s|iOz", kwlist, &filename, &read_only, &py_ft, &forced_encoding)) { if (py_ft != NULL && py_ft != Py_None) { filetype = (Filetype *) py_ft; if (filetype->ft != NULL) ft = filetype->ft; } doc = document_open_file(filename, read_only, ft, forced_encoding); if (DOC_VALID(doc)) return (PyObject *) Document_create_new_from_geany_document(doc); } Py_RETURN_NONE; }
/* * opens position in a editor */ void editor_open_position(const gchar *filename, int line) { GeanyDocument* doc = NULL; gboolean already_open = (doc = document_get_current()) && !strcmp(DOC_FILENAME(doc), filename); if (!already_open) doc = document_open_file(filename, FALSE, NULL, NULL); if (doc) { /* temporarily set debug caret policy */ scintilla_send_message(doc->editor->sci, SCI_SETYCARETPOLICY, CARET_SLOP | CARET_JUMPS | CARET_EVEN, 3); sci_goto_line(doc->editor->sci, line - 1, TRUE); /* revert to default edit caret policy */ scintilla_send_message(doc->editor->sci, SCI_SETYCARETPOLICY, CARET_EVEN, 0); scintilla_send_message(doc->editor->sci, SCI_SETFOCUS, TRUE, 0); } else { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Can't find a source file \"%s\""), filename); } }
void plugin_help(void) { // FIXME: if FMT_README_FILE isn't defined or not found on disk, open the // web page version in the browser instead. document_open_file(FMT_README_FILE, true, filetypes[GEANY_FILETYPES_MARKDOWN], NULL); }
/* --------------------------------------------------------------------- * Callback when the menu item is clicked. * --------------------------------------------------------------------- */ static void menu_item_activate(guint key_id) { GtkWidget* dialog; GtkWidget* dialog_new = NULL; GtkWidget* dialog_entry; GtkTreeModel* completion_list; GeanyDocument* current_doc = document_get_current(); gchar *chosen_path; const gchar *chosen_file; gint response; log_func(); if(current_doc == NULL || current_doc->file_name == NULL || current_doc->file_name[0] == '\0') return; /* Build current directory listing */ directory_ref = g_path_get_dirname(current_doc->file_name); completion_list = build_file_list(directory_ref, ""); /* Create the user dialog and get response */ dialog_entry = create_dialog(&dialog, completion_list); response = gtk_dialog_run(GTK_DIALOG(dialog)); /* Filename */ chosen_file = gtk_entry_get_text(GTK_ENTRY(dialog_entry)); /* Path + Filename */ chosen_path = g_build_filename(directory_ref, chosen_file, NULL); if ( response == GTK_RESPONSE_ACCEPT ) { log_debug("Trying to open: %s", chosen_path); if ( ! g_file_test(chosen_path, G_FILE_TEST_EXISTS) ) { log_debug("File not found."); dialog_new = gtk_message_dialog_new(GTK_WINDOW(geany_data->main_widgets->window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, _("%s not found, create it?"), chosen_file); gtk_window_set_title(GTK_WINDOW(dialog_new), "Geany"); if(gtk_dialog_run(GTK_DIALOG(dialog_new)) == GTK_RESPONSE_OK) { document_new_file(chosen_path, current_doc->file_type, NULL); document_set_text_changed(document_get_current(), TRUE); } gtk_widget_destroy(dialog_new); } else document_open_file(chosen_path, FALSE, NULL, NULL); } /* Freeing memory */ gtk_widget_destroy(dialog); g_free(directory_ref); g_object_unref (completion_list); }
gboolean msgwin_goto_messages_file_line(gboolean focus_editor) { GtkTreeIter iter; GtkTreeModel *model; GtkTreeSelection *selection; gboolean ret = FALSE; selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg)); if (gtk_tree_selection_get_selected(selection, &model, &iter)) { gint line; guint id; gchar *string; GeanyDocument *doc; GeanyDocument *old_doc = document_get_current(); gtk_tree_model_get(model, &iter, MSG_COL_LINE, &line, MSG_COL_DOC_ID, &id, MSG_COL_STRING, &string, -1); if (line >= 0 && id > 0) { /* check doc is still open */ doc = document_find_by_id(id); if (!doc) { ui_set_statusbar(FALSE, _("The document has been closed.")); utils_beep(); } else { line = adjust_line_number(doc, line, msgwindow.line_shifts_msg); ret = navqueue_goto_line(old_doc, doc, line); if (ret && focus_editor) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); } } else if (line < 0 && string != NULL) { gchar *filename; /* try with a file:line parsing */ msgwin_parse_generic_line(string, &filename, &line); if (filename != NULL) { /* use document_open_file to find an already open file, or open it in place */ doc = document_open_file(filename, FALSE, NULL, NULL); if (doc != NULL) { line = adjust_line_number(doc, line, msgwindow.line_shifts_msg); ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line); if (ret && focus_editor) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); } } g_free(filename); } g_free(string); } return ret; }
static gboolean goto_compiler_file_line(const gchar *filename, gint line, gboolean focus_editor) { if (!filename || line <= -1) return FALSE; /* 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."), filename); 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) { gboolean ret; if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */ 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)); return ret; } } return FALSE; }
gboolean geany_dbus_application_open_document (GeanyDBusApplication* self, const char* filename) { gboolean result = FALSE; gboolean _tmp0_ = FALSE; g_return_val_if_fail (self != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE); if (document_open_file (filename, FALSE, NULL, NULL) != NULL) { _tmp0_ = TRUE; } else { _tmp0_ = FALSE; } result = _tmp0_; return result; }
static void on_open_config_file(GtkMenuItem *item, gpointer user_data) { char *fn; GeanyDocument *doc = document_get_current(); if (!DOC_VALID(doc) || !doc->file_name) return; fn = fmt_lookup_clang_format_dot_file(doc->file_name); if (fn) { document_open_file(fn, false, filetypes[GEANY_FILETYPES_YAML], NULL); g_free(fn); } }
static void selected_row(GtkTreeSelection *selected, gpointer data) { GtkTreeIter iter; GtkTreeModel *model; if(gtk_tree_selection_get_selected(selected, &model, &iter)) { gchar *line, *file; gtk_tree_model_get(model, &iter, 1, &line, 2, &file, -1); file = g_build_path(G_DIR_SEPARATOR_S, base_directory, file, NULL); GeanyDocument *doc = document_open_file(file, FALSE, NULL, NULL); editor_goto_line(doc->editor, atoi(line) - 1, 0); g_free(line); g_free(file); } }
gboolean msgwin_goto_messages_file_line(gboolean focus_editor) { GtkTreeIter iter; GtkTreeModel *model; GtkTreeSelection *selection; gboolean ret = FALSE; selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg)); if (gtk_tree_selection_get_selected(selection, &model, &iter)) { gint line; gchar *string; GeanyDocument *doc; GeanyDocument *old_doc = document_get_current(); gtk_tree_model_get(model, &iter, 0, &line, 1, &doc, 3, &string, -1); /* doc may have been closed, so check doc->index: */ if (line >= 0 && DOC_VALID(doc)) { ret = navqueue_goto_line(old_doc, doc, line); if (ret && focus_editor) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); } else if (line < 0 && string != NULL) { gchar *filename; /* try with a file:line parsing */ msgwin_parse_generic_line(string, &filename, &line); if (filename != NULL) { /* use document_open_file to find an already open file, or open it in place */ doc = document_open_file(filename, FALSE, NULL, NULL); if (doc != NULL) { ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line); if (ret && focus_editor) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); } } g_free(filename); } g_free(string); } return ret; }
void open_file(gchar *utf8_name) { gchar *name; GeanyDocument *doc; name = utils_get_locale_from_utf8(utf8_name); doc = document_find_by_filename(utf8_name); if (!doc) doc = document_open_file(name, FALSE, NULL, NULL); else { gtk_notebook_set_current_page(GTK_NOTEBOOK(geany->main_widgets->notebook), document_get_notebook_page(doc)); } if (doc) gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); g_free(name); }
/* --------------------------------------------------------------------- * Callback when the menu item is clicked. * --------------------------------------------------------------------- */ static void menu_item_activate(guint key_id) { GeanyDocument* current_doc = document_get_current(); GeanyDocument* new_doc = NULL; guint nb_documents = geany->documents_array->len; gchar* extension = NULL; /* e.g. : "hpp" */ GSList* p_extensions_to_test = NULL; /* e.g. : ["cpp", "cxx", ...] */ GSList* filenames_to_test = NULL; /* e.g. : ["f.cpp", "f.cxx", ...] */ GSList* iter_lang = NULL; GSList* iter_ext = NULL; GSList* iter_filename = NULL; gint i=0; gchar* dirname = NULL; gchar* basename = NULL; gchar* basename_no_extension = NULL; gchar* p_str = NULL; /* Local variables, used as temporary buffers */ gchar* p_str2 = NULL; log_func(); log_debug("current_doc->file_name == %s", current_doc->file_name); log_debug("geany->documents_array->len == %d", geany->documents_array->len); if(current_doc != NULL && current_doc->file_name != NULL && current_doc->file_name[0] != '\0') { /* Get the basename, e.g. : "/home/me/file.cpp" -> "file.cpp" */ basename = g_path_get_basename(current_doc->file_name); if(g_utf8_strlen(basename, -1) < 2) goto free_mem; log_debug("basename == %s", basename); /* Get the extension , e.g. : "cpp" */ extension = get_extension(basename); if(extension == NULL || g_utf8_strlen(extension, -1) == 0) goto free_mem; log_debug("extension == %s", extension); /* Get the basename without any extension */ basename_no_extension = copy_and_remove_extension(basename); if(basename_no_extension == NULL || g_utf8_strlen(basename_no_extension, -1) == 0) goto free_mem; /* Identify the language and whether the file is a header or an implementation. */ /* For each recognized language : */ for(iter_lang = languages ; iter_lang != NULL ; iter_lang = iter_lang->next) { Language* lang = (Language*)(iter_lang->data); /* Test the headers : */ if(g_slist_find_custom(lang->head_extensions, extension, (GCompareFunc)(&compare_strings)) != NULL) { p_extensions_to_test = lang->impl_extensions; break; } /* Test the implementations : */ else if(g_slist_find_custom(lang->impl_extensions, extension, (GCompareFunc)(&compare_strings)) != NULL) { p_extensions_to_test = lang->head_extensions; break; } } if(p_extensions_to_test == NULL) goto free_mem; #ifdef CODE_NAVIGATION_DEBUG log_debug("extension known !"); log_debug("p_extensions_to_test : "); g_slist_foreach(p_extensions_to_test, (GFunc)(&log_debug), NULL); #endif /* Build a list of filenames to test : */ filenames_to_test = NULL; for(iter_ext = p_extensions_to_test ; iter_ext != NULL ; iter_ext = iter_ext->next) { p_str = g_strdup_printf("%s.%s", basename_no_extension, (const gchar*)(iter_ext->data)); filenames_to_test = g_slist_prepend(filenames_to_test, p_str); } filenames_to_test = g_slist_reverse(filenames_to_test); #ifdef CODE_NAVIGATION_DEBUG log_debug("filenames to test :"); g_slist_foreach(filenames_to_test, (GFunc)(&log_debug), NULL); #endif /* First : look for a corresponding file in the opened files. * If found, open it. */ for(i=0 ; i < nb_documents ; i++) { new_doc = document_index(i); for(iter_filename = filenames_to_test ; iter_filename != NULL ; iter_filename = iter_filename->next) { p_str = g_path_get_basename(new_doc->file_name); log_debug("comparing \"%s\" and \"%s\"", (const gchar*)(iter_filename->data), p_str); if(utils_str_equal((const gchar*)(iter_filename->data), p_str)) { log_debug("FOUND !"); g_free(p_str); p_str = g_locale_from_utf8(new_doc->file_name, -1, NULL, NULL, NULL); document_open_file(p_str, FALSE, NULL, NULL); g_free(p_str); goto free_mem; } g_free(p_str); } } /* Second : if not found, look for a corresponding file in the same directory. * If found, open it. */ /* -> compute dirname */ dirname = g_path_get_dirname(current_doc->real_path); if(dirname == NULL) goto free_mem; log_debug("dirname == \"%s\"", dirname); /* -> try all the extensions we should test */ for(iter_ext = p_extensions_to_test ; iter_ext != NULL ; iter_ext = iter_ext->next) { p_str = g_strdup_printf( "%s" G_DIR_SEPARATOR_S "%s.%s", dirname, basename_no_extension, (const gchar*)(iter_ext->data)); p_str2 = g_locale_from_utf8(p_str, -1, NULL, NULL, NULL); g_free(p_str); log_debug("trying to open the file \"%s\"\n", p_str2); /* Try without read-only and in read-only mode */ if( document_open_file(p_str2, FALSE, NULL, NULL) != NULL || document_open_file(p_str2, TRUE, NULL, NULL) != NULL) { g_free(p_str2); goto free_mem; } g_free(p_str2); } /* Third : if not found, ask the user if he wants to create it or not. */ { p_str = g_strdup_printf("%s.%s", basename_no_extension, (const gchar*)(p_extensions_to_test->data)); GtkWidget* dialog = gtk_message_dialog_new( GTK_WINDOW(geany_data->main_widgets->window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, _("%s not found, create it?"), p_str); gtk_window_set_title(GTK_WINDOW(dialog), "Geany"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) { p_str2 = g_strdup_printf( "%s" G_DIR_SEPARATOR_S "%s", dirname, p_str); document_new_file(p_str2, current_doc->file_type, NULL); document_set_text_changed(document_get_current(), TRUE); g_free(p_str2); } log_debug("DESTROY"); gtk_widget_destroy(dialog); g_free(p_str); } /* Free the memory */ free_mem: g_slist_foreach(filenames_to_test, (GFunc)(&g_free), NULL); g_free(dirname); g_free(basename_no_extension); g_free(extension); g_free(basename); } }
static void find_tags(const gchar *name, gboolean declaration, gboolean case_sensitive, MatchType match_type) { tagFile *tf; GeanyProject *prj; gchar *tag_filename = NULL; tagEntry entry; tagFileInfo info; prj = geany_data->app->project; if (!prj) return; msgwin_clear_tab(MSG_MESSAGE); msgwin_set_messages_dir(prj->base_path); tag_filename = get_tags_filename(); tf = tagsOpen(tag_filename, &info); if (tf) { if (find_first(tf, &entry, name, match_type)) { GPatternSpec *name_pat; gchar *name_case; gchar *path = NULL; gint num = 0; if (case_sensitive) name_case = g_strdup(name); else name_case = g_utf8_strdown(name, -1); SETPTR(name_case, g_strconcat("*", name_case, "*", NULL)); name_pat = g_pattern_spec_new(name_case); if (!filter_tag(&entry, name_pat, declaration, case_sensitive)) { path = g_build_filename(prj->base_path, entry.file, NULL); show_entry(&entry); num++; } while (find_next(tf, &entry, match_type)) { if (!filter_tag(&entry, name_pat, declaration, case_sensitive)) { if (!path) path = g_build_filename(prj->base_path, entry.file, NULL); show_entry(&entry); num++; } } if (num == 1) { GeanyDocument *doc = document_open_file(path, FALSE, NULL, NULL); if (doc != NULL) { navqueue_goto_line(document_get_current(), doc, entry.address.lineNumber); gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); } } g_pattern_spec_free(name_pat); g_free(name_case); g_free(path); } tagsClose(tf); } msgwin_switch_tab(MSG_MESSAGE, TRUE); g_free(tag_filename); }