]> git.lyx.org Git - features.git/commitdiff
* Singleton-ify the used SpellChecker object.
authorAbdelrazak Younes <younes@lyx.org>
Sun, 29 Mar 2009 21:18:16 +0000 (21:18 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Sun, 29 Mar 2009 21:18:16 +0000 (21:18 +0000)
* Simplify Aspell construction as the object is capable of supporting multiple languages at the same time.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@28974 a592a061-630c-0410-9148-cb99ea01b6c8

src/ASpell.cpp
src/ASpell_local.h
src/LyX.cpp
src/LyX.h
src/frontends/qt4/GuiSpellchecker.cpp

index fee21fca2e2e8c85cd13e004b759cd5e47a15e55..c99ff0498c6eef8117285c5eea6a5b318b390c0f 100644 (file)
@@ -25,10 +25,8 @@ using namespace std;
 
 namespace lyx {
 
-ASpell::ASpell(BufferParams const &, string const & lang)
-       : els(0), spell_error_object(0)
+ASpell::ASpell(): els(0), spell_error_object(0)
 {
-       addSpeller(lang);
 }
 
 
index 1f81370c606d39a13855858ddd3a01f56fd04195..9f5fed78c841c8b083f7dd250e14cd234707b22f 100644 (file)
@@ -25,32 +25,27 @@ struct AspellConfig;
 
 namespace lyx {
 
-class BufferParams;
 
-
-class ASpell : public SpellChecker {
+class ASpell : public SpellChecker
+{
 public:
-       /**
-        * Initialise the spellchecker with the given buffer params and language.
-        */
-       ASpell(BufferParams const & params, std::string const & lang);
-
-       virtual ~ASpell();
+       ASpell();
+       ~ASpell();
 
        /// check the given word and return the result
-       virtual enum Result check(WordLangTuple const &);
+       enum Result check(WordLangTuple const &);
 
        /// insert the given word into the personal dictionary
-       virtual void insert(WordLangTuple const &);
+       void insert(WordLangTuple const &);
 
        /// accept the given word temporarily
-       virtual void accept(WordLangTuple const &);
+       void accept(WordLangTuple const &);
 
        /// return the next near miss after a SUGGESTED_WORDS result
-       virtual docstring const nextMiss();
+       docstring const nextMiss();
 
        /// give an error message on messy exit
-       virtual docstring const error();
+       docstring const error();
 
 private:
        /// add a speller of the given language
index 5bd3d0f8c360a00ad20bc138cc50047549517e84..c30b1215ae94843c8166af1c77ceefb1eb6c5a5d 100644 (file)
@@ -17,7 +17,7 @@
 
 #include "LyX.h"
 
-#include "LayoutFile.h"
+#include "ASpell_local.h"
 #include "Buffer.h"
 #include "BufferList.h"
 #include "CmdDef.h"
@@ -31,6 +31,7 @@
 #include "FuncStatus.h"
 #include "KeyMap.h"
 #include "Language.h"
+#include "LayoutFile.h"
 #include "Lexer.h"
 #include "LyXAction.h"
 #include "LyXFunc.h"
@@ -127,7 +128,19 @@ struct LyX::Impl
                // The language used will be derived from the environment
                // variables.
                messages_["GUI"] = Messages();
+
+#if defined(USE_ASPELL)
+               spell_checker_ = new ASpell();
+#else
+               spell_checker_ = 0;
+#endif
+       }
+
+       ~Impl()
+       {
+               delete spell_checker_;
        }
+
        /// our function handler
        LyXFunc lyxfunc_;
        ///
@@ -169,6 +182,8 @@ struct LyX::Impl
 
        ///
        graphics::Previews preview_;
+       ///
+       SpellChecker * spell_checker_;
 };
 
 ///
@@ -1245,4 +1260,10 @@ CmdDef & theTopLevelCmdDef()
        return singleton_->pimpl_->toplevel_cmddef_;
 }
 
+
+SpellChecker * theSpellChecker()
+{
+       return singleton_->pimpl_->spell_checker_;
+}
+
 } // namespace lyx
index c27e2045cf056399596fb69bc680b9ab6ac12b85..100b593047e7fdda62972854e2100c56db9de9e6 100644 (file)
--- a/src/LyX.h
+++ b/src/LyX.h
@@ -32,6 +32,7 @@ class Movers;
 class Server;
 class ServerSocket;
 class Session;
+class SpellChecker;
 
 extern bool use_gui;
 
@@ -137,6 +138,7 @@ private:
        friend graphics::Previews & thePreviews();
        friend Session & theSession();
        friend CmdDef & theTopLevelCmdDef();
+       friend SpellChecker * theSpellChecker();
        friend void setRcGuiLanguage();
        friend void emergencyCleanup();
        friend void execBatchCommands();
@@ -158,3 +160,4 @@ void execBatchCommands();
 } // namespace lyx
 
 #endif // LYX_H
+
index e939aaad0e917aec7251c12dd261d4c1d72ac553..0b2355d6a1724210bd3575463ae194045425bac9 100644 (file)
@@ -21,6 +21,7 @@
 #include "Cursor.h"
 #include "CutAndPaste.h"
 #include "Language.h"
+#include "LyX.h"
 #include "LyXRC.h"
 #include "Paragraph.h"
 
@@ -75,7 +76,6 @@ GuiSpellchecker::GuiSpellchecker(GuiView & lv)
 
 GuiSpellchecker::~GuiSpellchecker()
 {
-       delete speller_;
 }
 
 
@@ -193,24 +193,11 @@ void GuiSpellchecker::partialUpdate(int state)
 }
 
 
-static SpellChecker * createSpeller(BufferParams const & bp)
-{
-       string lang = lyxrc.spellchecker_use_alt_lang
-                     ? lyxrc.spellchecker_alt_lang
-                     : bp.language->code();
-
-#if defined(USE_ASPELL)
-       return new ASpell(bp, lang);
-#endif
-       return 0;
-}
-
-
 bool GuiSpellchecker::initialiseParams(string const &)
 {
        LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
 
-       speller_ = createSpeller(buffer().params());
+       speller_ = theSpellChecker();
        if (!speller_)
                return false;
 
@@ -225,7 +212,6 @@ bool GuiSpellchecker::initialiseParams(string const &)
                Alert::error(_("Spellchecker error"),
                             _("The spellchecker could not be started\n")
                             + speller_->error());
-               delete speller_;
                speller_ = 0;
        }
 
@@ -236,7 +222,6 @@ bool GuiSpellchecker::initialiseParams(string const &)
 void GuiSpellchecker::clearParams()
 {
        LYXERR(Debug::GUI, "Spellchecker::clearParams");
-       delete speller_;
        speller_ = 0;
 }
 
@@ -255,7 +240,9 @@ static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
        cur.resetAnchor();
        cur.setCursor(to);
        cur.setSelection();
-       string lang_code = from.paragraph().getFontSettings(buf.params(), cur.pos()).language()->code();
+       string lang_code = lyxrc.spellchecker_use_alt_lang
+                     ? lyxrc.spellchecker_alt_lang
+                     : from.paragraph().getFontSettings(buf.params(), cur.pos()).language()->code();
        ++progress;
        return WordLangTuple(word, lang_code);
 }
@@ -304,15 +291,15 @@ void GuiSpellchecker::check()
                        partialUpdate(SPELL_PROGRESSED);
                }
 
-               // speller might be dead ...
-               if (!checkAlive())
-                       return;
-
                res = speller_->check(word_);
 
-               // ... or it might just be reporting an error
-               if (!checkAlive())
+               // ... just bail out if the spellchecker reports an error.
+               if (!speller_->error().empty()) {
+                       docstring const message =
+                               _("The spellchecker has failed.\n") + speller_->error();
+                       slotClose();
                        return;
+               }       
        }
 
        LYXERR(Debug::GUI, "Found word \"" << to_utf8(getWord()) << "\"");
@@ -334,28 +321,9 @@ void GuiSpellchecker::check()
 }
 
 
-bool GuiSpellchecker::checkAlive()
-{
-       if (speller_->error().empty())
-               return true;
-
-       docstring message;
-       if (speller_->error().empty())
-               message = _("The spellchecker has died for some reason.\n"
-                                        "Maybe it has been killed.");
-       else
-               message = _("The spellchecker has failed.\n") + speller_->error();
-
-       slotClose();
-
-       Alert::error(_("The spellchecker has failed"), message);
-       return false;
-}
-
-
 void GuiSpellchecker::showSummary()
 {
-       if (!checkAlive() || count_ == 0) {
+       if (count_ == 0) {
                slotClose();
                return;
        }