Add Project overrides for 'Saving files' checkbox options

This commit is contained in:
Nick Treleaven 2012-01-19 18:09:29 +00:00
parent 75ff98a2b6
commit 21cd7bb213
5 changed files with 921 additions and 732 deletions

File diff suppressed because it is too large Load Diff

View File

@ -71,6 +71,7 @@
#include "win32.h"
#include "search.h"
#include "filetypesprivate.h"
#include "project.h"
#include "SciLexer.h"
@ -1687,6 +1688,7 @@ gboolean document_save_file(GeanyDocument *doc, gboolean force)
gchar *data;
gsize len;
gchar *locale_filename;
const GeanyFilePrefs *fp;
g_return_val_if_fail(doc != NULL, FALSE);
@ -1701,17 +1703,18 @@ gboolean document_save_file(GeanyDocument *doc, gboolean force)
if (! force && ! ui_prefs.allow_always_save && (! doc->changed || doc->readonly))
return FALSE;
fp = project_get_file_prefs();
/* replaces tabs by spaces but only if the current file is not a Makefile */
if (file_prefs.replace_tabs && doc->file_type->id != GEANY_FILETYPES_MAKE)
if (fp->replace_tabs && doc->file_type->id != GEANY_FILETYPES_MAKE)
editor_replace_tabs(doc->editor);
/* strip trailing spaces */
if (file_prefs.strip_trailing_spaces)
if (fp->strip_trailing_spaces)
editor_strip_trailing_spaces(doc->editor);
/* ensure the file has a newline at the end */
if (file_prefs.final_new_line)
if (fp->final_new_line)
editor_ensure_final_newline(doc->editor);
/* ensure newlines are consistent */
if (file_prefs.ensure_convert_new_lines)
if (fp->ensure_convert_new_lines)
sci_convert_eols(doc->editor->sci, sci_get_eol_mode(doc->editor->sci));
/* notify plugins which may wish to modify the document before it's saved */

View File

@ -54,14 +54,13 @@ ProjectPrefs project_prefs = { NULL, FALSE, FALSE };
static GeanyProjectPrivate priv;
static GeanyIndentPrefs indentation;
static StashGroup *indent_group = NULL;
static GSList *stash_groups = NULL;
static struct
{
gchar *project_file_path; /* in UTF-8 */
} local_prefs = { NULL };
static gboolean entries_modified;
/* simple struct to keep references to the elements of the properties dialog */
@ -87,6 +86,7 @@ static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements
static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e);
static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line);
static void apply_editor_prefs(void);
static void init_stash_prefs(void);
#define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
@ -344,6 +344,8 @@ static void remove_foreach_project_filetype(gpointer data, gpointer user_data)
/* open_default will make function reload default session files on close */
void project_close(gboolean open_default)
{
GSList *node;
g_return_if_fail(app->project != NULL);
ui_set_statusbar(TRUE, _("Project \"%s\" closed."), app->project->name);
@ -371,6 +373,12 @@ void project_close(gboolean open_default)
g_free(app->project);
app->project = NULL;
foreach_slist(node, stash_groups)
stash_group_free(node->data);
g_slist_free(stash_groups);
stash_groups = NULL;
apply_editor_prefs(); /* ensure that global settings are restored */
if (project_prefs.project_session)
@ -424,15 +432,9 @@ on_project_properties_base_path_button_clicked(GtkWidget *button,
static void insert_build_page(PropertyDialogElements *e)
{
GtkWidget *build_table, *label, *editor_tab;
GtkWidget *build_table, *label;
GeanyDocument *doc = document_get_current();
GeanyFiletype *ft = NULL;
gint page_num;
/* lookup the "Editor" tab page so the "Build" tab can be inserted
* right after it. */
editor_tab = ui_lookup_widget(e->dialog, "vbox_project_dialog_editor");
page_num = gtk_notebook_page_num(GTK_NOTEBOOK(e->notebook), editor_tab);
if (doc != NULL)
ft = doc->file_type;
@ -440,8 +442,8 @@ static void insert_build_page(PropertyDialogElements *e)
build_table = build_commands_table(doc, GEANY_BCS_PROJ, &(e->build_properties), ft);
gtk_container_set_border_width(GTK_CONTAINER(build_table), 6);
label = gtk_label_new(_("Build"));
e->build_page_num = gtk_notebook_insert_page(GTK_NOTEBOOK(e->notebook),
build_table, label, ++page_num);
e->build_page_num = gtk_notebook_append_page(GTK_NOTEBOOK(e->notebook),
build_table, label);
}
@ -493,6 +495,7 @@ static void show_project_properties(gboolean show_build)
GtkWidget *widget = NULL;
GtkWidget *radio_long_line_custom;
static PropertyDialogElements e;
GSList *node;
g_return_if_fail(app->project != NULL);
@ -503,7 +506,8 @@ static void show_project_properties(gboolean show_build)
insert_build_page(&e);
stash_group_display(indent_group, e.dialog);
foreach_slist(node, stash_groups)
stash_group_display(node->data, e.dialog);
/* fill the elements with the appropriate data */
gtk_entry_set_text(GTK_ENTRY(e.name), p->name);
@ -607,10 +611,11 @@ static GeanyProject *create_project(void)
GeanyProject *project = g_new0(GeanyProject, 1);
memset(&priv, 0, sizeof priv);
indentation = *editor_get_indent_prefs(NULL);
priv.indentation = &indentation;
project->priv = &priv;
init_stash_prefs();
project->file_patterns = NULL;
project->long_line_behaviour = 1 /* use global settings */;
@ -730,6 +735,7 @@ static gboolean update_config(const PropertyDialogElements *e, gboolean new_proj
GtkWidget *widget;
gchar *tmp;
GString *str;
GSList *node;
/* get and set the project description */
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
@ -737,7 +743,8 @@ static gboolean update_config(const PropertyDialogElements *e, gboolean new_proj
gtk_text_buffer_get_end_iter(buffer, &end);
setptr(p->description, g_strdup(gtk_text_buffer_get_text(buffer, &start, &end, FALSE)));
stash_group_update(indent_group, e->dialog);
foreach_slist(node, stash_groups)
stash_group_update(node->data, e->dialog);
/* read the project build menu */
oldvalue = ft ? ft->projfilecmds : NULL;
@ -954,6 +961,7 @@ static gboolean load_config(const gchar *filename)
{
GKeyFile *config;
GeanyProject *p;
GSList *node;
/* there should not be an open project */
g_return_val_if_fail(app->project == NULL && filename != NULL, FALSE);
@ -967,7 +975,8 @@ static gboolean load_config(const gchar *filename)
p = create_project();
stash_group_load_from_key_file(indent_group, config);
foreach_slist(node, stash_groups)
stash_group_load_from_key_file(node->data, config);
p->name = utils_get_setting_string(config, "project", "name", GEANY_STRING_UNTITLED);
p->description = utils_get_setting_string(config, "project", "description", "");
@ -1021,6 +1030,7 @@ static gboolean write_config(gboolean emit_signal)
gchar *filename;
gchar *data;
gboolean ret = FALSE;
GSList *node;
g_return_val_if_fail(app->project != NULL, FALSE);
@ -1031,7 +1041,8 @@ static gboolean write_config(gboolean emit_signal)
filename = utils_get_locale_from_utf8(p->file_name);
g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL);
stash_group_save_to_key_file(indent_group, config);
foreach_slist(node, stash_groups)
stash_group_save_to_key_file(node->data, config);
g_key_file_set_string(config, "project", "name", p->name);
g_key_file_set_string(config, "project", "base_path", p->base_path);
@ -1159,14 +1170,22 @@ void project_apply_prefs(void)
}
void project_init(void)
static void add_stash_group(StashGroup *group)
{
stash_groups = g_slist_prepend(stash_groups, group);
}
static void init_stash_prefs(void)
{
StashGroup *group;
GKeyFile *kf;
group = stash_group_new("indentation");
/* defaults are copied from editor indent prefs */
/* copy global defaults */
indentation = *editor_get_indent_prefs(NULL);
stash_group_set_use_defaults(group, FALSE);
indent_group = group;
add_stash_group(group);
stash_group_add_spin_button_integer(group, &indentation.width,
"indent_width", 4, "spin_indent_width_project");
@ -1185,10 +1204,48 @@ void project_init(void)
"detect_indent_width", FALSE, "check_detect_indent_width_project");
stash_group_add_combo_box(group, (gint*)(gpointer)&indentation.auto_indent_mode,
"indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode_project");
group = stash_group_new("file_prefs");
stash_group_add_toggle_button(group, &priv.final_new_line,
"final_new_line", file_prefs.final_new_line, "check_new_line1");
stash_group_add_toggle_button(group, &priv.ensure_convert_new_lines,
"ensure_convert_new_lines", file_prefs.ensure_convert_new_lines, "check_ensure_convert_new_lines1");
stash_group_add_toggle_button(group, &priv.strip_trailing_spaces,
"strip_trailing_spaces", file_prefs.strip_trailing_spaces, "check_trailing_spaces1");
stash_group_add_toggle_button(group, &priv.replace_tabs,
"replace_tabs", file_prefs.replace_tabs, "check_replace_tabs1");
add_stash_group(group);
/* apply defaults */
kf = g_key_file_new();
stash_group_load_from_key_file(group, kf);
g_key_file_free(kf);
}
#define COPY_PREF(dest, prefname)\
(dest.prefname = priv.prefname)
const GeanyFilePrefs *project_get_file_prefs(void)
{
static GeanyFilePrefs fp;
if (!app->project)
return &file_prefs;
fp = file_prefs;
COPY_PREF(fp, final_new_line);
COPY_PREF(fp, ensure_convert_new_lines);
COPY_PREF(fp, strip_trailing_spaces);
COPY_PREF(fp, replace_tabs);
return &fp;
}
void project_init(void)
{
}
void project_finalize(void)
{
stash_group_free(indent_group);
}

View File

@ -84,6 +84,8 @@ gboolean project_load_file_with_session(const gchar *locale_file_name);
gchar *project_get_base_path(void);
const struct GeanyFilePrefs *project_get_file_prefs(void);
void project_save_prefs(GKeyFile *config);
void project_load_prefs(GKeyFile *config);

View File

@ -28,6 +28,10 @@
typedef struct GeanyProjectPrivate
{
struct GeanyIndentPrefs *indentation;
gboolean final_new_line;
gboolean strip_trailing_spaces;
gboolean replace_tabs;
gboolean ensure_convert_new_lines;
}
GeanyProjectPrivate;