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
This commit is contained in:
Nick Treleaven 2007-09-03 16:09:53 +00:00
parent cb333602fd
commit 1887a20df4
10 changed files with 50 additions and 52 deletions

View File

@ -3,6 +3,14 @@
* src/keybindings.c: * src/keybindings.c:
Set copy lines default KB to Ctrl-Shift-C. Set copy lines default KB to Ctrl-Shift-C.
Set cut lines default KB to Ctrl-Shift-X. 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 <nick(dot)treleaven(at)btinternet(dot)com> 2007-08-31 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>

View File

@ -740,7 +740,7 @@ static void cc_dlg_on_create_class(CreateClassDialog *cc_dlg)
if (! utils->str_equal(class_info->source, "")) if (! utils->str_equal(class_info->source, ""))
{ {
text = get_template_class_source(class_info); 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); scintilla->set_text(doc_list[idx].sci, text);
g_free(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, "")) if (! utils->str_equal(class_info->header, ""))
{ {
text = get_template_class_header(class_info); 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); scintilla->set_text(doc_list[idx].sci, text);
g_free(text); g_free(text);
} }

View File

@ -193,7 +193,7 @@ void
on_new1_activate (GtkMenuItem *menuitem, on_new1_activate (GtkMenuItem *menuitem,
gpointer user_data) gpointer user_data)
{ {
document_new_file(NULL, NULL); document_new_file(NULL, NULL, NULL);
} }
@ -554,7 +554,7 @@ void
on_toolbutton8_clicked (GtkToolButton *toolbutton, on_toolbutton8_clicked (GtkToolButton *toolbutton,
gpointer user_data) gpointer user_data)
{ {
document_new_file(NULL, NULL); document_new_file(NULL, NULL, NULL);
} }
// open file // open file
@ -1185,7 +1185,7 @@ void
on_toolbutton_new_clicked (GtkToolButton *toolbutton, on_toolbutton_new_clicked (GtkToolButton *toolbutton,
gpointer user_data) gpointer user_data)
{ {
document_new_file(NULL, NULL); document_new_file(NULL, NULL, NULL);
} }

View File

@ -445,21 +445,23 @@ static void store_saved_encoding(gint idx)
} }
/* This creates a new document, by clearing the text widget and setting the /* Create a new document.
current filename to filename or NULL. If ft is NULL and filename is not NULL, then the filetype * filename is either the UTF-8 file name, or NULL.
will be guessed from the given filename. * If ft is NULL and filename is not NULL, then the filetype will be guessed
filename is expected in UTF-8 encoding. */ * from the given filename.
gint document_new_file(const gchar *filename, filetype *ft) * 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); gint idx = document_create_new_sci(filename);
gchar *template = templates_get_template_new_file(ft);
g_assert(idx != -1); g_assert(idx != -1);
sci_set_undo_collection(doc_list[idx].sci, FALSE); // avoid creation of an undo action sci_set_undo_collection(doc_list[idx].sci, FALSE); // avoid creation of an undo action
sci_clear_all(doc_list[idx].sci); if (text)
sci_set_text(doc_list[idx].sci, template); sci_set_text(doc_list[idx].sci, text);
g_free(template); else
sci_clear_all(doc_list[idx].sci);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
sci_set_eol_mode(doc_list[idx].sci, SC_EOL_CRLF); 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_set_undo_collection(doc_list[idx].sci, TRUE);
sci_empty_undo_buffer(doc_list[idx].sci); 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); doc_list[idx].encoding = g_strdup(encodings[prefs.default_new_encoding].charset);
// store the opened encoding for undo/redo // store the opened encoding for undo/redo
store_saved_encoding(idx); 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); filetypes[GEANY_FILETYPES_ALL]->style_func_ptr(doc_list[idx].sci);
ui_set_window_title(idx); ui_set_window_title(idx);
build_menu_update(idx); build_menu_update(idx);
doc_list[idx].mtime = time(NULL);
doc_list[idx].changed = FALSE;
document_update_tag_list(idx, FALSE); document_update_tag_list(idx, FALSE);
document_set_text_changed(idx); document_set_text_changed(idx);
ui_document_show_hide(idx); // update the document menu 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 // create a new file and copy file content and properties
gint len, idx; gint len, idx;
gchar *data; gchar *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);
sci_set_undo_collection(doc_list[idx].sci, FALSE); // avoid creation of an undo action
sci_empty_undo_buffer(doc_list[idx].sci);
len = sci_get_length(doc_list[old_idx].sci) + 1; len = sci_get_length(doc_list[old_idx].sci) + 1;
data = (gchar*) g_malloc(len); text = (gchar*) g_malloc(len);
sci_get_text(doc_list[old_idx].sci, len, data); sci_get_text(doc_list[old_idx].sci, len, text);
// use old file type (or maybe NULL for auto detect would be better?)
sci_set_text(doc_list[idx].sci, data); idx = document_new_file(utf8_filename, doc_list[old_idx].file_type, text);
g_free(text);
// copy file properties // copy file properties
doc_list[idx].line_wrapping = doc_list[old_idx].line_wrapping; 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); document_set_encoding(idx, doc_list[old_idx].encoding);
sci_set_lines_wrapped(doc_list[idx].sci, doc_list[idx].line_wrapping); 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_readonly(doc_list[idx].sci, doc_list[idx].readonly);
sci_set_undo_collection(doc_list[idx].sci, TRUE);
ui_document_show_hide(idx); ui_document_show_hide(idx);
g_free(data);
return idx; return idx;
} }

View File

@ -120,20 +120,13 @@ void document_apply_update_prefs(gint idx);
gboolean document_remove(guint page_num); gboolean document_remove(guint page_num);
/* This creates a new document, by clearing the text widget and setting the /* See document.c. */
current filename to filename or NULL. If ft is NULL and filename is not NULL, then the filetype gint document_new_file(const gchar *filename, filetype *ft, const gchar *text);
will be guessed from the given filename.
filename is expected in UTF-8 encoding. */
gint document_new_file(const gchar *filename, filetype *ft);
gint document_clone(gint old_idx, const gchar *utf8_filename); gint document_clone(gint old_idx, const gchar *utf8_filename);
/* To open a new file, set idx to -1; filename should be locale encoded. /* See document.c. */
* 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. */
gint document_open_file(gint idx, const gchar *filename, gint pos, gboolean readonly, gint document_open_file(gint idx, const gchar *filename, gint pos, gboolean readonly,
filetype *ft, const gchar *forced_enc); filetype *ft, const gchar *forced_enc);

View File

@ -715,7 +715,7 @@ static void cb_func_file_action(guint key_id)
switch (key_id) switch (key_id)
{ {
case GEANY_KEYS_MENU_NEW: case GEANY_KEYS_MENU_NEW:
document_new_file(NULL, NULL); document_new_file(NULL, NULL, NULL);
break; break;
case GEANY_KEYS_MENU_OPEN: case GEANY_KEYS_MENU_OPEN:
on_open1_activate(NULL, NULL); on_open1_activate(NULL, NULL);

View File

@ -553,7 +553,7 @@ static gboolean open_cl_files(gint argc, gchar **argv)
{ // create new file if it doesn't exist { // create new file if it doesn't exist
gint idx; gint idx;
idx = document_new_file(filename, NULL); idx = document_new_file(filename, NULL, NULL);
if (DOC_IDX_VALID(idx)) if (DOC_IDX_VALID(idx))
ui_add_recent_file(doc_list[idx].file_name); 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 // open a new file if no other file was opened
if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook)) == 0) 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_document_buttons_update();
ui_save_buttons_toggle(FALSE); ui_save_buttons_toggle(FALSE);

View File

@ -76,7 +76,7 @@ static const gint api_version = 15;
/* The ABI version should be incremented whenever existing fields in the plugin /* 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 * 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. */ * 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: /* This performs runtime checks that try to ensure:
* 1. Geany ABI data types are compatible with this plugin. * 1. Geany ABI data types are compatible with this plugin.
@ -170,7 +170,7 @@ struct filetype;
typedef struct DocumentFuncs 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) (); gint (*get_cur_idx) ();
struct document* (*get_current) (); struct document* (*get_current) ();
gboolean (*save_file)(gint idx, gboolean force); gboolean (*save_file)(gint idx, gboolean force);

View File

@ -425,7 +425,7 @@ gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpoint
{ // create new file if it doesn't exist { // create new file if it doesn't exist
gint idx; gint idx;
idx = document_new_file(buf, NULL); idx = document_new_file(buf, NULL, NULL);
if (DOC_IDX_VALID(idx)) if (DOC_IDX_VALID(idx))
ui_add_recent_file(doc_list[idx].file_name); ui_add_recent_file(doc_list[idx].file_name);
else else

View File

@ -307,7 +307,11 @@ static void
on_new_with_template (GtkMenuItem *menuitem, on_new_with_template (GtkMenuItem *menuitem,
gpointer user_data) 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"); GtkWidget *template_menu = lookup_widget(app->window, "menu_new_with_template1_menu");
filetype_id ft_id; 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; GtkWidget *tmp_menu, *tmp_button;
filetype *ft = filetypes[ft_id]; filetype *ft = filetypes[ft_id];
const gchar *label = ft->title; const gchar *label = ft->title;
if (ft_templates[ft_id] == NULL) if (ft_templates[ft_id] == NULL)
{
continue; continue;
} if (ft_id == GEANY_FILETYPES_ALL)
label = _("None");
tmp_menu = gtk_menu_item_new_with_label(label); tmp_menu = gtk_menu_item_new_with_label(label);
tmp_button = gtk_menu_item_new_with_label(label); tmp_button = gtk_menu_item_new_with_label(label);
gtk_widget_show(tmp_menu); gtk_widget_show(tmp_menu);