From 1887a20df409a40914677aba19431a11a44ce0ca Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Mon, 3 Sep 2007 16:09:53 +0000 Subject: [PATCH] Add text argument for document_new_file(), so that it's independent from filetype templates. Make File->New create a blank document, rather than using the None filetype template. Add None option for the 'New with Template' menu commands. git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@1848 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 8 +++++++ plugins/classbuilder.c | 4 ++-- src/callbacks.c | 6 +++--- src/document.c | 47 ++++++++++++++++++------------------------ src/document.h | 13 +++--------- src/keybindings.c | 2 +- src/main.c | 4 ++-- src/plugindata.h | 4 ++-- src/socket.c | 2 +- src/templates.c | 12 +++++++---- 10 files changed, 50 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index b18061702..1d1ff2a9c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,14 @@ * src/keybindings.c: Set copy lines default KB to Ctrl-Shift-C. Set cut lines default KB to Ctrl-Shift-X. + * plugins/classbuilder.c, src/templates.c, src/keybindings.c, + src/plugindata.h, src/callbacks.c, src/document.c, src/document.h, + src/main.c, src/socket.c: + Add text argument for document_new_file(), so that it's independent + from filetype templates. + Make File->New create a blank document, rather than using the None + filetype template. + Add None option for the 'New with Template' menu commands. 2007-08-31 Nick Treleaven diff --git a/plugins/classbuilder.c b/plugins/classbuilder.c index 37e48b81a..0900b5387 100644 --- a/plugins/classbuilder.c +++ b/plugins/classbuilder.c @@ -740,7 +740,7 @@ static void cc_dlg_on_create_class(CreateClassDialog *cc_dlg) if (! utils->str_equal(class_info->source, "")) { text = get_template_class_source(class_info); - idx = documents->new_file(class_info->source, NULL); + idx = documents->new_file(class_info->source, NULL, NULL); scintilla->set_text(doc_list[idx].sci, text); g_free(text); } @@ -748,7 +748,7 @@ static void cc_dlg_on_create_class(CreateClassDialog *cc_dlg) if (! utils->str_equal(class_info->header, "")) { text = get_template_class_header(class_info); - idx = documents->new_file(class_info->header, NULL); + idx = documents->new_file(class_info->header, NULL, NULL); scintilla->set_text(doc_list[idx].sci, text); g_free(text); } diff --git a/src/callbacks.c b/src/callbacks.c index 5a9e7fcfa..4b17789be 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -193,7 +193,7 @@ void on_new1_activate (GtkMenuItem *menuitem, gpointer user_data) { - document_new_file(NULL, NULL); + document_new_file(NULL, NULL, NULL); } @@ -554,7 +554,7 @@ void on_toolbutton8_clicked (GtkToolButton *toolbutton, gpointer user_data) { - document_new_file(NULL, NULL); + document_new_file(NULL, NULL, NULL); } // open file @@ -1185,7 +1185,7 @@ void on_toolbutton_new_clicked (GtkToolButton *toolbutton, gpointer user_data) { - document_new_file(NULL, NULL); + document_new_file(NULL, NULL, NULL); } diff --git a/src/document.c b/src/document.c index 91db0da58..b21477576 100644 --- a/src/document.c +++ b/src/document.c @@ -445,21 +445,23 @@ static void store_saved_encoding(gint idx) } -/* This creates a new document, by clearing the text widget and setting the - current filename to filename or NULL. If ft is NULL and filename is not NULL, then the filetype - will be guessed from the given filename. - filename is expected in UTF-8 encoding. */ -gint document_new_file(const gchar *filename, filetype *ft) +/* Create a new document. + * filename is either the UTF-8 file name, or NULL. + * If ft is NULL and filename is not NULL, then the filetype will be guessed + * from the given filename. + * text is the contents of the new file, or NULL. + * Returns: idx of new file in doc_list. */ +gint document_new_file(const gchar *filename, filetype *ft, const gchar *text) { gint idx = document_create_new_sci(filename); - gchar *template = templates_get_template_new_file(ft); g_assert(idx != -1); sci_set_undo_collection(doc_list[idx].sci, FALSE); // avoid creation of an undo action - sci_clear_all(doc_list[idx].sci); - sci_set_text(doc_list[idx].sci, template); - g_free(template); + if (text) + sci_set_text(doc_list[idx].sci, text); + else + sci_clear_all(doc_list[idx].sci); #ifdef G_OS_WIN32 sci_set_eol_mode(doc_list[idx].sci, SC_EOL_CRLF); @@ -469,6 +471,9 @@ gint document_new_file(const gchar *filename, filetype *ft) sci_set_undo_collection(doc_list[idx].sci, TRUE); sci_empty_undo_buffer(doc_list[idx].sci); + doc_list[idx].mtime = time(NULL); + doc_list[idx].changed = FALSE; + doc_list[idx].encoding = g_strdup(encodings[prefs.default_new_encoding].charset); // store the opened encoding for undo/redo store_saved_encoding(idx); @@ -482,10 +487,6 @@ gint document_new_file(const gchar *filename, filetype *ft) filetypes[GEANY_FILETYPES_ALL]->style_func_ptr(doc_list[idx].sci); ui_set_window_title(idx); build_menu_update(idx); - - doc_list[idx].mtime = time(NULL); - doc_list[idx].changed = FALSE; - document_update_tag_list(idx, FALSE); document_set_text_changed(idx); ui_document_show_hide(idx); // update the document menu @@ -2372,19 +2373,14 @@ gint document_clone(gint old_idx, const gchar *utf8_filename) { // create a new file and copy file content and properties gint len, idx; - gchar *data; - - // use old file type (or maybe NULL for auto detect would be better?) - idx = document_new_file(utf8_filename, doc_list[old_idx].file_type); - - sci_set_undo_collection(doc_list[idx].sci, FALSE); // avoid creation of an undo action - sci_empty_undo_buffer(doc_list[idx].sci); + gchar *text; len = sci_get_length(doc_list[old_idx].sci) + 1; - data = (gchar*) g_malloc(len); - sci_get_text(doc_list[old_idx].sci, len, data); - - sci_set_text(doc_list[idx].sci, data); + text = (gchar*) g_malloc(len); + sci_get_text(doc_list[old_idx].sci, len, text); + // use old file type (or maybe NULL for auto detect would be better?) + idx = document_new_file(utf8_filename, doc_list[old_idx].file_type, text); + g_free(text); // copy file properties doc_list[idx].line_wrapping = doc_list[old_idx].line_wrapping; @@ -2393,11 +2389,8 @@ gint document_clone(gint old_idx, const gchar *utf8_filename) document_set_encoding(idx, doc_list[old_idx].encoding); sci_set_lines_wrapped(doc_list[idx].sci, doc_list[idx].line_wrapping); sci_set_readonly(doc_list[idx].sci, doc_list[idx].readonly); - sci_set_undo_collection(doc_list[idx].sci, TRUE); ui_document_show_hide(idx); - - g_free(data); return idx; } diff --git a/src/document.h b/src/document.h index 8be4d4fd2..2043ae347 100644 --- a/src/document.h +++ b/src/document.h @@ -120,20 +120,13 @@ void document_apply_update_prefs(gint idx); gboolean document_remove(guint page_num); -/* This creates a new document, by clearing the text widget and setting the - current filename to filename or NULL. If ft is NULL and filename is not NULL, then the filetype - will be guessed from the given filename. - filename is expected in UTF-8 encoding. */ -gint document_new_file(const gchar *filename, filetype *ft); +/* See document.c. */ +gint document_new_file(const gchar *filename, filetype *ft, const gchar *text); gint document_clone(gint old_idx, const gchar *utf8_filename); -/* To open a new file, set idx to -1; filename should be locale encoded. - * To reload a file, set the idx for the document to be reloaded; filename should be NULL. - * Returns: idx of the opened file or -1 if an error occurred. - * Note: If opening more than one file, document_delay_colourise() should be used before - * and document_colourise_new() after opening to avoid unnecessary recolourising. */ +/* See document.c. */ gint document_open_file(gint idx, const gchar *filename, gint pos, gboolean readonly, filetype *ft, const gchar *forced_enc); diff --git a/src/keybindings.c b/src/keybindings.c index b983d5081..72a8ed82d 100644 --- a/src/keybindings.c +++ b/src/keybindings.c @@ -715,7 +715,7 @@ static void cb_func_file_action(guint key_id) switch (key_id) { case GEANY_KEYS_MENU_NEW: - document_new_file(NULL, NULL); + document_new_file(NULL, NULL, NULL); break; case GEANY_KEYS_MENU_OPEN: on_open1_activate(NULL, NULL); diff --git a/src/main.c b/src/main.c index 64454b5a4..f117ed127 100644 --- a/src/main.c +++ b/src/main.c @@ -553,7 +553,7 @@ static gboolean open_cl_files(gint argc, gchar **argv) { // create new file if it doesn't exist gint idx; - idx = document_new_file(filename, NULL); + idx = document_new_file(filename, NULL, NULL); if (DOC_IDX_VALID(idx)) ui_add_recent_file(doc_list[idx].file_name); } @@ -740,7 +740,7 @@ gint main(gint argc, gchar **argv) // open a new file if no other file was opened if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook)) == 0) - document_new_file(NULL, NULL); + document_new_file(NULL, NULL, NULL); ui_document_buttons_update(); ui_save_buttons_toggle(FALSE); diff --git a/src/plugindata.h b/src/plugindata.h index 1ba4b4fa1..b8738583e 100644 --- a/src/plugindata.h +++ b/src/plugindata.h @@ -76,7 +76,7 @@ static const gint api_version = 15; /* The ABI version should be incremented whenever existing fields in the plugin * data types below have to be changed or reordered. It should stay the same if fields * are only appended, as this doesn't affect existing fields. */ -static const gint abi_version = 6; +static const gint abi_version = 7; /* This performs runtime checks that try to ensure: * 1. Geany ABI data types are compatible with this plugin. @@ -170,7 +170,7 @@ struct filetype; typedef struct DocumentFuncs { - gint (*new_file) (const gchar *filename, struct filetype *ft); + gint (*new_file) (const gchar *filename, struct filetype *ft, const gchar *text); gint (*get_cur_idx) (); struct document* (*get_current) (); gboolean (*save_file)(gint idx, gboolean force); diff --git a/src/socket.c b/src/socket.c index 0da00ebff..993cd323f 100644 --- a/src/socket.c +++ b/src/socket.c @@ -425,7 +425,7 @@ gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpoint { // create new file if it doesn't exist gint idx; - idx = document_new_file(buf, NULL); + idx = document_new_file(buf, NULL, NULL); if (DOC_IDX_VALID(idx)) ui_add_recent_file(doc_list[idx].file_name); else diff --git a/src/templates.c b/src/templates.c index cc0efcc67..bf1981f1e 100644 --- a/src/templates.c +++ b/src/templates.c @@ -307,7 +307,11 @@ static void on_new_with_template (GtkMenuItem *menuitem, gpointer user_data) { - document_new_file(NULL, (filetype*) user_data); + filetype *ft = user_data; + gchar *template = templates_get_template_new_file(ft); + + document_new_file(NULL, ft, template); + g_free(template); } @@ -317,16 +321,16 @@ static void create_new_menu_items() GtkWidget *template_menu = lookup_widget(app->window, "menu_new_with_template1_menu"); filetype_id ft_id; - for (ft_id = 0; ft_id < GEANY_FILETYPES_ALL; ft_id++) + for (ft_id = 0; ft_id < GEANY_MAX_FILE_TYPES; ft_id++) { GtkWidget *tmp_menu, *tmp_button; filetype *ft = filetypes[ft_id]; const gchar *label = ft->title; if (ft_templates[ft_id] == NULL) - { continue; - } + if (ft_id == GEANY_FILETYPES_ALL) + label = _("None"); tmp_menu = gtk_menu_item_new_with_label(label); tmp_button = gtk_menu_item_new_with_label(label); gtk_widget_show(tmp_menu);