[PATCH] Added strip trailing whitespace on save
Shuhao
shuhao at shuhaowu.com
Fri Apr 19 09:17:54 CEST 2013
This feature is found in numerous other editors and should be here as
well. Very simple change, will be my first.
Also, another feature to implement is not to strip the current line's
trailing as you probably don't want that.
---
mousepad/mousepad-document.c | 22 +++++++++++++++++++++-
mousepad/mousepad-document.h | 5 +++++
mousepad/mousepad-preferences.c | 11 +++++++++--
mousepad/mousepad-window-ui.xml | 1 +
mousepad/mousepad-window.c | 30 ++++++++++++++++++++++++++++++
5 files changed, 66 insertions(+), 3 deletions(-)
diff --git a/mousepad/mousepad-document.c b/mousepad/mousepad-document.c
index d248dc9..3bec596 100644
--- a/mousepad/mousepad-document.c
+++ b/mousepad/mousepad-document.c
@@ -99,6 +99,7 @@ struct _MousepadDocumentPrivate
/* settings */
guint word_wrap : 1;
+ guint strip_trailing_on_save : 1;
};
@@ -176,7 +177,7 @@ static void
mousepad_document_init (MousepadDocument *document)
{
GtkTargetList *target_list;
- gboolean word_wrap, auto_indent, line_numbers, insert_spaces;
+ gboolean word_wrap, auto_indent, line_numbers, insert_spaces, strip_trailing_on_save;
gchar *font_name, *color_scheme;
gint tab_size;
GtkSourceStyleScheme *scheme = NULL;
@@ -229,6 +230,7 @@ mousepad_document_init (MousepadDocument *document)
"view-tab-size", &tab_size,
"view-insert-spaces", &insert_spaces,
"view-color-scheme", &color_scheme,
+ "view-strip-trailing-on-save", &strip_trailing_on_save,
NULL);
/* release the preferences */
@@ -237,6 +239,7 @@ mousepad_document_init (MousepadDocument *document)
/* set all the settings */
mousepad_document_set_word_wrap (document, word_wrap);
mousepad_document_set_font (document, font_name);
+ mousepad_document_set_strip_trailing_on_save (document, strip_trailing_on_save);
mousepad_view_set_line_numbers (document->textview, line_numbers);
mousepad_view_set_auto_indent (document->textview, auto_indent);
mousepad_view_set_tab_size (document->textview, tab_size);
@@ -497,6 +500,15 @@ mousepad_document_set_word_wrap (MousepadDocument *document,
word_wrap ? GTK_WRAP_WORD : GTK_WRAP_NONE);
}
+void
+mousepad_document_set_strip_trailing_on_save (MousepadDocument *document,
+ gboolean strip_trailing_on_save)
+{
+ mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+
+ document->priv->strip_trailing_on_save = strip_trailing_on_save;
+}
+
void
@@ -648,3 +660,11 @@ mousepad_document_get_word_wrap (MousepadDocument *document)
return document->priv->word_wrap;
}
+
+gboolean
+mousepad_document_get_strip_trailing_on_save (MousepadDocument *document)
+{
+ mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
+
+ return document->priv->strip_trailing_on_save;
+}
\ No newline at end of file
diff --git a/mousepad/mousepad-document.h b/mousepad/mousepad-document.h
index f2176c1..7556375 100644
--- a/mousepad/mousepad-document.h
+++ b/mousepad/mousepad-document.h
@@ -69,6 +69,9 @@ void mousepad_document_set_overwrite (MousepadDocument *document,
void mousepad_document_set_word_wrap (MousepadDocument *document,
gboolean word_wrap);
+void mousepad_document_set_strip_trailing_on_save (MousepadDocument *document,
+ gboolean strip_trailing_on_save);
+
void mousepad_document_focus_textview (MousepadDocument *document);
void mousepad_document_send_signals (MousepadDocument *document);
@@ -81,6 +84,8 @@ const gchar *mousepad_document_get_filename (MousepadDocument *document);
gboolean mousepad_document_get_word_wrap (MousepadDocument *document);
+gboolean mousepad_document_get_strip_trailing_on_save (MousepadDocument *document);
+
G_END_DECLS
#endif /* !__MOUSEPAD_DOCUMENT_H__ */
diff --git a/mousepad/mousepad-preferences.c b/mousepad/mousepad-preferences.c
index 90a84d6..a052cf3 100644
--- a/mousepad/mousepad-preferences.c
+++ b/mousepad/mousepad-preferences.c
@@ -62,6 +62,7 @@ enum
PROP_VIEW_TABS_AS_SPACES,
PROP_VIEW_WORD_WRAP,
PROP_VIEW_COLOR_SCHEME,
+ PROP_VIEW_STRIP_TRAILING_ON_SAVE,
/* window preferences */
PROP_WINDOW_HEIGHT,
@@ -223,6 +224,14 @@ mousepad_preferences_class_init (MousepadPreferencesClass *klass)
"none",
MOUSEPAD_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class,
+ PROP_VIEW_STRIP_TRAILING_ON_SAVE,
+ g_param_spec_boolean ("view-strip-trailing-on-save",
+ "ViewStripTrailingOnSave",
+ NULL,
+ FALSE,
+ MOUSEPAD_PARAM_READWRITE));
+
/**
* Window Preferences
@@ -613,8 +622,6 @@ mousepad_preferences_store_idle (gpointer user_data)
return FALSE;
}
-
-
static void
mousepad_preferences_store_idle_destroy (gpointer user_data)
{
diff --git a/mousepad/mousepad-window-ui.xml b/mousepad/mousepad-window-ui.xml
index f960bc6..a8ce051 100644
--- a/mousepad/mousepad-window-ui.xml
+++ b/mousepad/mousepad-window-ui.xml
@@ -113,6 +113,7 @@
<menuitem action="insert-spaces" />
</menu>
<menuitem action="word-wrap" />
+ <menuitem action="strip-trailing-on-save" />
<separator />
<menuitem action="write-bom" />
<separator />
diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c
index b789d71..920e2c2 100644
--- a/mousepad/mousepad-window.c
+++ b/mousepad/mousepad-window.c
@@ -331,6 +331,8 @@ static void mousepad_window_action_word_wrap (GtkToggle
MousepadWindow *window);
static void mousepad_window_action_write_bom (GtkToggleAction *action,
MousepadWindow *window);
+static void mousepad_window_action_strip_on_save (GtkToggleAction *action,
+ MousepadWindow *window);
static void mousepad_window_action_language (GtkToggleAction *action,
MousepadWindow *window);
static void mousepad_window_action_insert_spaces (GtkToggleAction *action,
@@ -476,6 +478,7 @@ static const GtkToggleActionEntry toggle_action_entries[] =
{ "auto-indent", NULL, N_("_Auto Indent"), NULL, N_("Auto indent a new line"), G_CALLBACK (mousepad_window_action_auto_indent), FALSE, },
{ "insert-spaces", NULL, N_("Insert _Spaces"), NULL, N_("Insert spaces when the tab button is pressed"), G_CALLBACK (mousepad_window_action_insert_spaces), FALSE, },
{ "word-wrap", NULL, N_("_Word Wrap"), NULL, N_("Toggle breaking lines in between words"), G_CALLBACK (mousepad_window_action_word_wrap), FALSE, },
+ { "strip-trailing-on-save", NULL, N_("Strip _Trailing Spaces on Save"), NULL, N_("Strip all trailing white spaces upon save."), G_CALLBACK (mousepad_window_action_strip_on_save), FALSE, },
{ "write-bom", NULL, N_("Write Unicode _BOM"), NULL, N_("Store the byte-order mark in the file"), G_CALLBACK (mousepad_window_action_write_bom), FALSE, }
};
@@ -2263,6 +2266,10 @@ mousepad_window_update_actions (MousepadWindow *window)
action = gtk_action_group_get_action (window->action_group, "word-wrap");
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), active);
+ active = mousepad_document_get_strip_trailing_on_save (document);
+ action = gtk_action_group_get_action (window->action_group, "strip-trailing-on-save");
+ gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), active);
+
active = mousepad_view_get_line_numbers (document->textview);
action = gtk_action_group_get_action (window->action_group, "line-numbers");
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), active);
@@ -3715,6 +3722,9 @@ mousepad_window_action_save (GtkAction *action,
mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (window->active), FALSE);
+ if (mousepad_document_get_strip_trailing_on_save(document) == TRUE)
+ mousepad_view_strip_trailing_spaces(document->textview);
+
if (mousepad_file_get_filename (document->file) == NULL)
{
/* file has no filename yet, open the save as dialog */
@@ -3863,6 +3873,9 @@ mousepad_window_action_save_all (GtkAction *action,
if (!gtk_text_buffer_get_modified (document->buffer))
continue;
+ if (mousepad_document_get_strip_trailing_on_save(document) == TRUE)
+ mousepad_view_strip_trailing_spaces(document->textview);
+
/* we try to quickly save files, without bothering the user */
if (mousepad_file_get_filename (document->file) != NULL
&& mousepad_file_get_read_only (document->file) == FALSE
@@ -4907,7 +4920,24 @@ mousepad_window_action_word_wrap (GtkToggleAction *action,
}
}
+static void
+mousepad_window_action_strip_on_save (GtkToggleAction *action,
+ MousepadWindow *window)
+{
+ gboolean active;
+ mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+ mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+
+ /* leave when menu updates are locked */
+ if (lock_menu_updates == 0)
+ {
+ active = gtk_toggle_action_get_active (action);
+ g_object_set (G_OBJECT (window->preferences), "view-strip-trailing-on-save", active, NULL);
+
+ mousepad_document_set_strip_trailing_on_save (window->active, active);
+ }
+}
static void
mousepad_window_action_write_bom (GtkToggleAction *action,
--
1.7.9.5
More information about the Xfce4-dev
mailing list