[Xfce4-commits] [apps/mousepad] 02/02: Guess file language on save if user has not set it

noreply at xfce.org noreply at xfce.org
Mon Jul 14 21:08:07 CEST 2014


This is an automated email from the git hooks/post-receive script.

mbrush pushed a commit to branch master
in repository apps/mousepad.

commit 87ae5a08165461c01317719a3cea4c73a5f07dce
Author: Matthew Brush <mbrush at codebrainz.ca>
Date:   Mon Jul 14 11:59:12 2014 -0700

    Guess file language on save if user has not set it
    
    Track whether a UI action was invoked explicitly by the user to set
    the language so we know when not to try and guess. This allows
    re-saving a file with the filetype explicitly set without having
    the guessing mechanism clobber the choice the user made.
---
 mousepad/mousepad-action-group.c |   11 +++++++++++
 mousepad/mousepad-file.c         |   34 +++++++++++++++++++++++++++-------
 mousepad/mousepad-file.h         |    3 +++
 mousepad/mousepad-window.c       |   13 +++++++++++++
 4 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/mousepad/mousepad-action-group.c b/mousepad/mousepad-action-group.c
index 210f21a..2edef9b 100644
--- a/mousepad/mousepad-action-group.c
+++ b/mousepad/mousepad-action-group.c
@@ -120,6 +120,14 @@ mousepad_action_group_class_init (MousepadActionGroupClass *klass)
                          "The currently active style scheme action",
                          MOUSEPAD_TYPE_STYLE_SCHEME_ACTION,
                          G_PARAM_READWRITE));
+
+  g_signal_new ("user-set-language",
+                G_TYPE_FROM_CLASS (g_object_class),
+                G_SIGNAL_RUN_LAST,
+                0, NULL, NULL,
+                g_cclosure_marshal_VOID__OBJECT,
+                G_TYPE_NONE, 0,
+                GTK_TYPE_SOURCE_LANGUAGE);
 }
 
 
@@ -455,6 +463,9 @@ mousepad_action_group_language_action_activate (MousepadActionGroup    *self,
 
       language = mousepad_language_action_get_language (action);
       mousepad_action_group_set_active_language (self, language);
+
+      /* notify interested parties that the user explicitly set the filetype */
+      g_signal_emit_by_name (self, "user-set-language", language);
     }
 }
 
diff --git a/mousepad/mousepad-file.c b/mousepad/mousepad-file.c
index 0091219..48130fe 100644
--- a/mousepad/mousepad-file.c
+++ b/mousepad/mousepad-file.c
@@ -82,6 +82,9 @@ struct _MousepadFile
 
   /* whether we write the bom at the start of the file */
   guint               write_bom : 1;
+
+  /* whether the filetype has been set by user or we should guess it */
+  gboolean            user_set_language;
 };
 
 
@@ -142,16 +145,17 @@ static void
 mousepad_file_init (MousepadFile *file)
 {
   /* initialize */
-  file->filename    = NULL;
-  file->encoding    = MOUSEPAD_ENCODING_UTF_8;
+  file->filename          = NULL;
+  file->encoding          = MOUSEPAD_ENCODING_UTF_8;
 #ifdef G_OS_WIN32
-  file->line_ending = MOUSEPAD_EOL_DOS;
+  file->line_ending       = MOUSEPAD_EOL_DOS;
 #else
-  file->line_ending = MOUSEPAD_EOL_UNIX;
+  file->line_ending       = MOUSEPAD_EOL_UNIX;
 #endif
-  file->readonly    = TRUE;
-  file->mtime       = 0;
-  file->write_bom   = FALSE;
+  file->readonly          = TRUE;
+  file->mtime             = 0;
+  file->write_bom         = FALSE;
+  file->user_set_language = FALSE;
 }
 
 
@@ -836,6 +840,11 @@ mousepad_file_save (MousepadFile  *file,
           /* we saved succesfully */
           mousepad_file_set_readonly (file, FALSE);
 
+          /* if the user hasn't set the filetype, try and re-guess it now
+           * that we have a new filename to go by */
+          if (! file->user_set_language)
+            mousepad_file_set_language (file, mousepad_file_guess_language (file));
+
           /* everything went file */
           succeed = TRUE;
 
@@ -921,3 +930,14 @@ mousepad_file_get_externally_modified (MousepadFile  *file,
 
   return TRUE;
 }
+
+
+
+void
+mousepad_file_set_user_set_language (MousepadFile *file,
+                                     gboolean      set_by_user)
+{
+  g_return_if_fail (MOUSEPAD_IS_FILE (file));
+
+  file->user_set_language = set_by_user;
+}
diff --git a/mousepad/mousepad-file.h b/mousepad/mousepad-file.h
index e9a8be3..1b1ed94 100644
--- a/mousepad/mousepad-file.h
+++ b/mousepad/mousepad-file.h
@@ -92,6 +92,9 @@ gboolean            mousepad_file_reload                   (MousepadFile
 
 gboolean            mousepad_file_get_externally_modified  (MousepadFile        *file,
                                                             GError             **error);
+
+void                mousepad_file_set_user_set_language    (MousepadFile        *file,
+                                                            gboolean             set_by_user);
 G_END_DECLS
 
 #endif /* !__MOUSEPAD_FILE_H__ */
diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c
index 6c04742..c493f2d 100644
--- a/mousepad/mousepad-window.c
+++ b/mousepad/mousepad-window.c
@@ -1000,6 +1000,18 @@ mousepad_window_create_statusbar (MousepadWindow *window)
 
 
 static void
+mousepad_window_user_set_language (MousepadWindow      *window,
+                                   GtkSourceLanguage   *language,
+                                   MousepadActionGroup *group)
+{
+  /* mark the file as having its language chosen explicitly by the user
+   * so we don't clobber their choice by guessing ourselves */
+  mousepad_file_set_user_set_language (window->active->file, TRUE);
+}
+
+
+
+static void
 mousepad_window_init (MousepadWindow *window)
 {
   GtkAccelGroup *accel_group;
@@ -1032,6 +1044,7 @@ mousepad_window_init (MousepadWindow *window)
   gtk_action_group_add_actions (window->action_group, action_entries, G_N_ELEMENTS (action_entries), GTK_WIDGET (window));
   gtk_action_group_add_toggle_actions (window->action_group, toggle_action_entries, G_N_ELEMENTS (toggle_action_entries), GTK_WIDGET (window));
   gtk_action_group_add_radio_actions (window->action_group, radio_action_entries, G_N_ELEMENTS (radio_action_entries), -1, G_CALLBACK (mousepad_window_action_line_ending), GTK_WIDGET (window));
+  g_signal_connect_object (window->action_group, "user-set-language", G_CALLBACK (mousepad_window_user_set_language), window, G_CONNECT_SWAPPED);
 
   /* create the ui manager and connect proxy signals for the statusbar */
   window->ui_manager = gtk_ui_manager_new ();

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list