/* name should be in UTF-8, and can have a path. */ static void show_output(const gchar * std_output, const gchar * name, const gchar * force_encoding, gint filetype_new_file) { gint page; GtkNotebook *book; GeanyDocument *doc, *cur_doc; if (std_output) { cur_doc = document_get_current(); doc = document_find_by_filename(name); if (doc == NULL) { doc = document_new_file(name, filetypes[filetype_new_file], std_output); } else { sci_set_text(doc->editor->sci, std_output); book = GTK_NOTEBOOK(geany->main_widgets->notebook); page = gtk_notebook_page_num(book, GTK_WIDGET(doc->editor->sci)); gtk_notebook_set_current_page(book, page); } document_set_text_changed(doc, FALSE); document_set_encoding(doc, (force_encoding ? force_encoding : "UTF-8")); navqueue_goto_line(cur_doc, document_get_current(), 1); } else { ui_set_statusbar(FALSE, _("Could not parse the output of command")); } }
/* --------------------------------------------------------------------- * 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); }
static int Document_set_property(Document *self, PyObject *value, const gchar *prop_name) { g_return_val_if_fail(self != NULL, -1); g_return_val_if_fail(value != NULL, -1); g_return_val_if_fail(prop_name != NULL, -1); if (!self->doc) { PyErr_SetString(PyExc_RuntimeError, "Document instance not initialized properly"); return -1; } if (g_str_equal(prop_name, "encoding")) { gchar *encoding = PyString_AsString(value); if (encoding) { document_set_encoding(self->doc, encoding); return 0; } } else if (g_str_equal(prop_name, "filetype")) { Filetype *filetype = (Filetype *) value; if (filetype->ft) { document_set_filetype(self->doc, filetype->ft); return 0; } } else if (g_str_equal(prop_name, "text_changed")) { long v = PyInt_AsLong(value); if (v == -1 && PyErr_Occurred()) { PyErr_Print(); return -1; } document_set_text_changed(self->doc, (gboolean) v); return 0; } PyErr_SetString(PyExc_AttributeError, "can't set property"); return -1; }
/* --------------------------------------------------------------------- * 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 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); }