[latexila/wip/gspell] spell: store spell settings in file metadata
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [latexila/wip/gspell] spell: store spell settings in file metadata
- Date: Sat, 26 Sep 2015 13:22:44 +0000 (UTC)
commit 65c5acd461e735f77e5c1a6f993b8e7864c63ace
Author: Sébastien Wilmet <swilmet gnome org>
Date: Fri Sep 25 17:23:48 2015 +0200
spell: store spell settings in file metadata
src/document.vala | 57 +++++++++++++++++
src/document_view.vala | 149 +++++++++++++++++++++++++++++++++-----------
src/main_window_tools.vala | 32 +++++++--
3 files changed, 195 insertions(+), 43 deletions(-)
---
diff --git a/src/document.vala b/src/document.vala
index 23e8062..cb6941c 100644
--- a/src/document.vala
+++ b/src/document.vala
@@ -38,6 +38,7 @@ public class Document : Gtk.SourceBuffer
private string? encoding = null;
private bool new_file = true;
private DocumentStructure _structure = null;
+ private FileInfo _metadata_info = new FileInfo ();
public signal void cursor_moved ();
@@ -92,6 +93,18 @@ public class Document : Gtk.SourceBuffer
public void load (File location)
{
+ // First load metadata so when the notify::location signal is emitted,
+ // get_metadata() works.
+ try
+ {
+ _metadata_info = location.query_info ("metadata::*", FileQueryInfoFlags.NONE);
+ }
+ catch (Error e)
+ {
+ warning ("Get document metadata failed: %s", e.message);
+ _metadata_info = new FileInfo ();
+ }
+
this.location = location;
try
@@ -189,6 +202,8 @@ public class Document : Gtk.SourceBuffer
RecentManager.get_default ().add_item (location.get_uri ());
backup_made = true;
+
+ save_metadata ();
}
catch (Error e)
{
@@ -599,4 +614,46 @@ public class Document : Gtk.SourceBuffer
return true;
}
+
+ private void save_metadata ()
+ {
+ return_if_fail (_metadata_info != null);
+
+ if (this.location == null)
+ return;
+
+ try
+ {
+ this.location.set_attributes_from_info (_metadata_info,
+ FileQueryInfoFlags.NONE);
+ }
+ catch (Error error)
+ {
+ warning ("Set document metadata failed: %s", error.message);
+ }
+ }
+
+ public void set_metadata (string key, string? val)
+ {
+ return_if_fail (_metadata_info != null);
+
+ if (val != null)
+ _metadata_info.set_attribute_string (key, val);
+ else
+ // Unset the key
+ _metadata_info.set_attribute (key, FileAttributeType.INVALID, null);
+
+ save_metadata ();
+ }
+
+ public string? get_metadata (string key)
+ {
+ return_val_if_fail (_metadata_info != null, null);
+
+ if (_metadata_info.has_attribute (key) &&
+ _metadata_info.get_attribute_type (key) == FileAttributeType.STRING)
+ return _metadata_info.get_attribute_string (key);
+
+ return null;
+ }
}
diff --git a/src/document_view.vala b/src/document_view.vala
index cb08d3c..4e53a46 100644
--- a/src/document_view.vala
+++ b/src/document_view.vala
@@ -23,6 +23,13 @@ public class DocumentView : Gtk.SourceView
{
public const double SCROLL_MARGIN = 0.02;
+ private static const string METADATA_ATTRIBUTE_SPELL_LANGUAGE =
+ "metadata::latexila-spell-language";
+ private static const string METADATA_ATTRIBUTE_INLINE_SPELL =
+ "metadata::latexila-inline-spell";
+ private static const string INLINE_SPELL_ACTIVATED_STR = "1";
+ private static const string INLINE_SPELL_DEACTIVATED_STR = "0";
+
private GLib.Settings _editor_settings;
private Pango.FontDescription _font_desc;
private Gspell.Checker? _spell_checker = null;
@@ -96,15 +103,7 @@ public class DocumentView : Gtk.SourceView
}
// spell checking
- unowned Gspell.Language? lang = null;
- string lang_key = _editor_settings.get_string ("spell-checking-language");
- if (lang_key[0] != '\0')
- lang = Gspell.Language.from_key (lang_key);
-
- _spell_checker = new Gspell.Checker (lang);
-
- if (_editor_settings.get_boolean ("highlight-misspelled-words"))
- activate_inline_spell_checker ();
+ init_spell_checking ();
// forward search
button_release_event.connect (on_button_release_event);
@@ -195,6 +194,79 @@ public class DocumentView : Gtk.SourceView
return "\t";
}
+ private bool on_button_release_event (Gdk.EventButton event)
+ {
+ // Forward search on Ctrl + left click
+ if (event.button == 1 &&
+ Gdk.ModifierType.CONTROL_MASK in event.state)
+ {
+ Latexila.Synctex synctex = Latexila.Synctex.get_instance ();
+ Document doc = this.buffer as Document;
+ synctex.forward_search (this.buffer, doc.location, doc.get_main_file (),
+ event.time);
+ }
+
+ // propagate the event further
+ return false;
+ }
+
+ private void hide_completion_calltip_when_needed ()
+ {
+ buffer.notify["cursor-position"].connect (() =>
+ {
+ CompletionProvider provider = CompletionProvider.get_default ();
+ provider.hide_calltip_window ();
+ });
+ }
+
+ /* Spell checking */
+
+ private void init_spell_checking ()
+ {
+ _spell_checker = new Gspell.Checker (get_spell_language ());
+ setup_inline_spell_checker ();
+
+ Document doc = get_buffer () as Document;
+
+ doc.notify["location"].connect (() =>
+ {
+ _spell_checker.set_language (get_spell_language ());
+ setup_inline_spell_checker ();
+ });
+ }
+
+ private unowned Gspell.Language? get_spell_language ()
+ {
+ Document doc = get_buffer () as Document;
+
+ string? lang_key = doc.get_metadata (METADATA_ATTRIBUTE_SPELL_LANGUAGE);
+ if (lang_key == null)
+ lang_key = _editor_settings.get_string ("spell-checking-language");
+
+ if (lang_key[0] == '\0')
+ return null;
+
+ return Gspell.Language.from_key (lang_key);
+ }
+
+ private void setup_inline_spell_checker ()
+ {
+ Document doc = get_buffer () as Document;
+
+ bool activate;
+
+ string? metadata = doc.get_metadata (METADATA_ATTRIBUTE_INLINE_SPELL);
+ if (metadata != null)
+ activate = metadata == INLINE_SPELL_ACTIVATED_STR;
+ else
+ activate = _editor_settings.get_boolean ("highlight-misspelled-words");
+
+ if (activate)
+ activate_inline_spell_checker ();
+ else
+ deactivate_inline_spell_checker ();
+ }
+
public void launch_spell_checker_dialog ()
{
return_if_fail (_spell_checker != null);
@@ -209,7 +281,7 @@ public class DocumentView : Gtk.SourceView
dialog.destroy ();
}
- public void set_spell_language ()
+ public void launch_spell_language_chooser_dialog ()
{
return_if_fail (_spell_checker != null);
@@ -219,11 +291,41 @@ public class DocumentView : Gtk.SourceView
dialog.run ();
- _spell_checker.set_language (dialog.get_language ());
+ unowned Gspell.Language? lang = dialog.get_language ();
+ _spell_checker.set_language (lang);
dialog.destroy ();
}
+ public void set_spell_language_metadata ()
+ {
+ return_if_fail (_spell_checker != null);
+
+ Document doc = get_buffer () as Document;
+
+ unowned Gspell.Language? lang = _spell_checker.get_language ();
+ if (lang != null)
+ doc.set_metadata (METADATA_ATTRIBUTE_SPELL_LANGUAGE, lang.to_key ());
+ else
+ doc.set_metadata (METADATA_ATTRIBUTE_SPELL_LANGUAGE, null);
+ }
+
+ public void set_inline_spell_metadata ()
+ {
+ Document doc = get_buffer () as Document;
+
+ if (this.highlight_misspelled_words)
+ {
+ doc.set_metadata (METADATA_ATTRIBUTE_INLINE_SPELL,
+ INLINE_SPELL_ACTIVATED_STR);
+ }
+ else
+ {
+ doc.set_metadata (METADATA_ATTRIBUTE_INLINE_SPELL,
+ INLINE_SPELL_DEACTIVATED_STR);
+ }
+ }
+
private void activate_inline_spell_checker ()
{
return_if_fail (_spell_checker != null);
@@ -281,29 +383,4 @@ public class DocumentView : Gtk.SourceView
notify_property ("highlight-misspelled-words");
}
}
-
- private bool on_button_release_event (Gdk.EventButton event)
- {
- // Forward search on Ctrl + left click
- if (event.button == 1 &&
- Gdk.ModifierType.CONTROL_MASK in event.state)
- {
- Latexila.Synctex synctex = Latexila.Synctex.get_instance ();
- Document doc = this.buffer as Document;
- synctex.forward_search (this.buffer, doc.location, doc.get_main_file (),
- event.time);
- }
-
- // propagate the event further
- return false;
- }
-
- private void hide_completion_calltip_when_needed ()
- {
- buffer.notify["cursor-position"].connect (() =>
- {
- CompletionProvider provider = CompletionProvider.get_default ();
- provider.hide_calltip_window ();
- });
- }
}
diff --git a/src/main_window_tools.vala b/src/main_window_tools.vala
index 07ba90d..6732ffd 100644
--- a/src/main_window_tools.vala
+++ b/src/main_window_tools.vala
@@ -95,26 +95,44 @@ public class MainWindowTools
public void on_spell_checker_dialog (Gtk.Action action)
{
- return_if_fail (_main_window.active_view != null);
+ DocumentView? view = _main_window.active_view;
+ return_if_fail (view != null);
- _main_window.active_view.launch_spell_checker_dialog ();
+ view.launch_spell_checker_dialog ();
+
+ // If the spell checker is used, save the language since it's probably
+ // correct. If it isn't correct, the language will be changed and the
+ // metadata will also be saved.
+ view.set_spell_language_metadata ();
}
public void on_set_language (Gtk.Action action)
{
- return_if_fail (_main_window.active_view != null);
+ DocumentView? view = _main_window.active_view;
+ return_if_fail (view != null);
- _main_window.active_view.set_spell_language ();
+ view.launch_spell_language_chooser_dialog ();
+ view.set_spell_language_metadata ();
}
public void on_inline_spell_checker (Gtk.Action action)
{
- return_if_fail (_main_window.active_view != null);
+ DocumentView? view = _main_window.active_view;
+ return_if_fail (view != null);
bool activate = (action as ToggleAction).active;
- _main_window.active_view.highlight_misspelled_words = activate;
+ // Save metadata only if property changes, because this function is
+ // also called when update_inline_spell_checker_action_state() is
+ // called.
+ if (view.highlight_misspelled_words != activate)
+ {
+ view.highlight_misspelled_words = activate;
- update_inline_spell_checker_action_state ();
+ update_inline_spell_checker_action_state ();
+
+ view.set_inline_spell_metadata ();
+ view.set_spell_language_metadata ();
+ }
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]