]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Fix bug #2213 (part 1): GuiChanges lacks "Previous Change" button.
[lyx.git] / src / Buffer.cpp
index 888f82072882f8237aada52558df3516180e92a9..90ce575534cfa13f7ac00c5ad3c89f2c74014ea9 100644 (file)
@@ -50,6 +50,7 @@
 #include "ParagraphParameters.h"
 #include "ParIterator.h"
 #include "PDFOptions.h"
+#include "SpellChecker.h"
 #include "sgml.h"
 #include "TexRow.h"
 #include "TexStream.h"
@@ -59,6 +60,7 @@
 #include "Undo.h"
 #include "VCBackend.h"
 #include "version.h"
+#include "WordLangTuple.h"
 #include "WordList.h"
 
 #include "insets/InsetBibitem.h"
@@ -79,6 +81,7 @@
 #include "support/lassert.h"
 #include "support/convert.h"
 #include "support/debug.h"
+#include "support/docstring_list.h"
 #include "support/ExceptionMessage.h"
 #include "support/FileName.h"
 #include "support/FileNameList.h"
@@ -930,7 +933,7 @@ bool Buffer::writeFile(FileName const & fname) const
                return false;
        }
 
-       removeAutosaveFile(d->filename.absFilename());
+       removeAutosaveFile();
 
        saveCheckSum(d->filename);
        message(str + _(" done."));
@@ -2423,6 +2426,22 @@ int AutoSaveBuffer::generateChild()
 } // namespace anon
 
 
+FileName Buffer::getAutosaveFilename() const
+{
+       string const fpath = isUnnamed() ? lyxrc.document_path : filePath();
+       string const fname = "#" + d->filename.onlyFileName() + "#";
+       return makeAbsPath(fname, fpath);
+}
+
+
+void Buffer::removeAutosaveFile() const
+{
+       FileName const f = getAutosaveFilename();
+       if (f.exists())
+               f.removeFile();
+}
+
+
 // Perfect target for a thread...
 void Buffer::autoSave() const
 {
@@ -2434,14 +2453,7 @@ void Buffer::autoSave() const
 
        // emit message signal.
        message(_("Autosaving current document..."));
-
-       // create autosave filename
-       string fname = filePath();
-       fname += '#';
-       fname += d->filename.onlyFileName();
-       fname += '#';
-
-       AutoSaveBuffer autosave(*this, FileName(fname));
+       AutoSaveBuffer autosave(*this, getAutosaveFilename());
        autosave.start();
 
        markBakClean();
@@ -3079,8 +3091,12 @@ bool Buffer::nextWord(DocIterator & from, DocIterator & to,
        bool inword = false;
        bool ignoreword = false;
        string lang_code;
+       // Go backward a bit if needed in order to return the word currently
+       // pointed by 'from'.
+       while (from && from.pos() && isLetter(from))
+               from.backwardPos();
+       // OK, we start from here.
        to = from;
-
        while (to.depth()) {
                if (isLetter(to)) {
                        if (!inword) {
@@ -3106,8 +3122,41 @@ bool Buffer::nextWord(DocIterator & from, DocIterator & to,
                }
                to.forwardPos();
        }
-
+       from = to;
+       word.clear();
        return false;
 }
 
+
+int Buffer::spellCheck(DocIterator & from, DocIterator & to,
+       WordLangTuple & word_lang, docstring_list & suggestions) const
+{
+       int progress = 0;
+       SpellChecker::Result res = SpellChecker::OK;
+       SpellChecker * speller = theSpellChecker();
+       suggestions.clear();
+       docstring word;
+       while (nextWord(from, to, word)) {
+               ++progress;
+               string lang_code = lyxrc.spellchecker_use_alt_lang
+                     ? lyxrc.spellchecker_alt_lang
+                     : from.paragraph().getFontSettings(params(), from.pos()).language()->code();
+               WordLangTuple wl(word, lang_code);
+               res = speller->check(wl);
+               // ... just bail out if the spellchecker reports an error.
+               if (!speller->error().empty()) {
+                       throw ExceptionMessage(WarningException,
+                               _("The spellchecker has failed."), speller->error());
+               }
+               if (res != SpellChecker::OK && res != SpellChecker::IGNORED_WORD) {
+                       word_lang = wl;
+                       break;
+               }
+               from = to;
+       }
+       while (!(word = speller->nextMiss()).empty())
+               suggestions.push_back(word);
+       return progress;
+}
+
 } // namespace lyx