]> git.lyx.org Git - features.git/commitdiff
Fix bug 2068 and do some cleanup
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 16 Aug 2006 21:12:20 +0000 (21:12 +0000)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 16 Aug 2006 21:12:20 +0000 (21:12 +0000)
* src/CutAndPaste.C (setSelectionRange, replaceWord): remove.
(replaceSelectionWithString): select the new string after
replacement; add a bool parameter indicating in which sense the
selection is made.

* src/lyxfind.C (replace): adapt to above changes.
(find): comment out debug message.

* src/frontends/controllers/ControlSpellchecker.C (isLetter):
rename parameter.
(nextWord): take a LCursor as parameter; set the selection over
the word that has been found.
(check): adapt to changes above (the length of the word is not
necessarily the length of the selection -- fixes bug 2068).
(replace): use cap::replaceSelectionWithString

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

src/CutAndPaste.C
src/CutAndPaste.h
src/frontends/controllers/ControlSpellchecker.C
src/lyxfind.C

index 566d63016fb676e4f726ca00baf14a47ed9fb26c..8dc3e7be8a20fb2b9d4bd257f67ebd4aeed4d2d8 100644 (file)
@@ -651,33 +651,19 @@ void pasteSelection(LCursor & cur, ErrorList & errorList, size_t sel_index)
 }
 
 
-void setSelectionRange(LCursor & cur, pos_type length)
-{
-       LyXText * text = cur.text();
-       BOOST_ASSERT(text);
-       if (!length)
-               return;
-       cur.resetAnchor();
-       while (length--)
-               text->cursorRight(cur);
-       cur.setSelection();
-}
-
-
 // simple replacing. The font of the first selected character is used
-void replaceSelectionWithString(LCursor & cur, string const & str)
+void replaceSelectionWithString(LCursor & cur, string const & str, bool backwards)
 {
-       LyXText * text = cur.text();
-       BOOST_ASSERT(text);
        recordUndo(cur);
+       DocIterator selbeg = cur.selectionBegin();
 
        // Get font setting before we cut
-       pos_type pos = cur.selEnd().pos();
-       Paragraph & par = text->getPar(cur.selEnd().pit());
        LyXFont const font =
-               par.getFontSettings(cur.buffer().params(), cur.selBegin().pos());
+               selbeg.paragraph().getFontSettings(cur.buffer().params(), selbeg.pos());
 
        // Insert the new string
+       pos_type pos = cur.selEnd().pos();
+       Paragraph & par = cur.selEnd().paragraph();
        string::const_iterator cit = str.begin();
        string::const_iterator end = str.end();
        for (; cit != end; ++cit, ++pos)
@@ -685,6 +671,13 @@ void replaceSelectionWithString(LCursor & cur, string const & str)
 
        // Cut the selection
        cutSelection(cur, true, false);
+
+       // select the replacement
+       if (backwards) {
+               selbeg.pos() += str.length();
+               cur.setSelection(selbeg, -str.length());
+       } else
+               cur.setSelection(selbeg, str.length());
 }
 
 
@@ -695,21 +688,6 @@ void replaceSelection(LCursor & cur)
 }
 
 
-// only used by the spellchecker
-void replaceWord(LCursor & cur, string const & replacestring)
-{
-       LyXText * text = cur.text();
-       BOOST_ASSERT(text);
-
-       replaceSelectionWithString(cur, replacestring);
-       setSelectionRange(cur, replacestring.length());
-
-       // Go back so that replacement string is also spellchecked
-       for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
-               text->cursorLeft(cur);
-}
-
-
 void eraseSelection(LCursor & cur)
 {
        //lyxerr << "LCursor::eraseSelection begin: " << cur << endl;
index 2b3a1c36bece216e0e5958ad97112af2f7f457fd..10e5a80c3def8dda551fb008033d594b6a248f5c 100644 (file)
@@ -41,13 +41,12 @@ std::string getSelection(Buffer const & buffer, size_t sel_index);
 ///
 void cutSelection(LCursor & cur, bool doclear, bool realcut);
 
-/**
- * Sets the selection from the current cursor position to length
- * characters to the right. No safety checks.
+/* Replace using the font of the first selected character and select
+ * the new string. When \c backwards == false, set anchor before
+ * cursor; otherwise set cursor before anchor.
  */
-void setSelectionRange(LCursor & cur, lyx::pos_type length);
-/// simply replace using the font of the first selected character
-void replaceSelectionWithString(LCursor & cur, std::string const & str);
+void replaceSelectionWithString(LCursor & cur, std::string const & str, 
+                               bool backwards);
 /// replace selection helper
 void replaceSelection(LCursor & cur);
 
@@ -71,9 +70,6 @@ void switchBetweenClasses(lyx::textclass_type c1,
                           lyx::textclass_type c2,
                           InsetText & in, ErrorList &);
 
-// only used by the spellchecker
-void replaceWord(LCursor & cur, std::string const & replacestring);
-
 ///
 std::string grabSelection(LCursor const & cur);
 ///
index 092c2e33238d181d9681d359fba7595661a50d4e..2aa74e0173bfd7fb8ab45c4e272962b1a42cbd3f 100644 (file)
@@ -127,24 +127,25 @@ void ControlSpellchecker::clearParams()
 
 namespace {
 
-bool isLetter(DocIterator const & cur)
+bool isLetter(DocIterator const & dit)
 {
-       return cur.inTexted()
-               && cur.inset().allowSpellCheck()
-               && cur.pos() != cur.lastpos()
-               && (cur.paragraph().isLetter(cur.pos())
+       return dit.inTexted()
+               && dit.inset().allowSpellCheck()
+               && dit.pos() != dit.lastpos()
+               && (dit.paragraph().isLetter(dit.pos())
                    // We want to pass the ' and escape chars to ispell
                    || contains(lyxrc.isp_esc_chars + '\'',
-                               cur.paragraph().getChar(cur.pos())))
-               && !isDeletedText(cur.paragraph(), cur.pos());
+                               dit.paragraph().getChar(dit.pos())))
+               && !isDeletedText(dit.paragraph(), dit.pos());
 }
 
 
-WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
-       BufferParams & bp)
+WordLangTuple nextWord(LCursor & cur, ptrdiff_t & progress)
 {
+       BufferParams const & bp = cur.bv().buffer()->params();
        bool inword = false;
        bool ignoreword = false;
+       cur.resetAnchor();
        string word, lang_code;
 
        while (cur.depth()) {
@@ -152,6 +153,7 @@ WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
                        if (!inword) {
                                inword = true;
                                ignoreword = false;
+                               cur.resetAnchor();
                                word.clear();
                                lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
                        }
@@ -166,9 +168,10 @@ WordLangTuple nextWord(DocIterator & cur, ptrdiff_t & progress,
                        }
                } else { // !isLetter(cur)
                        if (inword)
-                               if (!word.empty() && !ignoreword)
+                               if (!word.empty() && !ignoreword) {
+                                       cur.setSelection();
                                        return WordLangTuple(word, lang_code);
-                               else
+                               else
                                        inword = false;
                }
 
@@ -189,7 +192,7 @@ void ControlSpellchecker::check()
 
        SpellBase::Result res = SpellBase::OK;
 
-       DocIterator cur = kernel().bufferview()->cursor();
+       LCursor cur = kernel().bufferview()->cursor();
        while (cur && cur.pos() && isLetter(cur)) {
                cur.backwardPos();
        }
@@ -202,11 +205,10 @@ void ControlSpellchecker::check()
        for (total = start; it; it.forwardPos())
                ++total;
 
-       BufferParams & bufferparams = kernel().buffer().params();
        exitEarly_ = false;
 
        while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
-               word_ = nextWord(cur, start, bufferparams);
+               word_ = nextWord(cur, start);
 
                // end of document
                if (getWord().empty()) {
@@ -240,7 +242,7 @@ void ControlSpellchecker::check()
 
        lyxerr[Debug::GUI] << "Found word \"" << getWord() << "\"" << endl;
 
-       int const size = getWord().size();
+       int const size = cur.selEnd().pos() - cur.selBegin().pos();
        cur.pos() -= size;
        kernel().bufferview()->putSelectionAt(cur, size, false);
        // if we used a lfun like in find/replace, dispatch would do
@@ -298,7 +300,7 @@ void ControlSpellchecker::replace(string const & replacement)
        lyxerr[Debug::GUI] << "ControlSpellchecker::replace("
                           << replacement << ")" << std::endl;
        BufferView & bufferview = *kernel().bufferview();
-       cap::replaceWord(bufferview.cursor(), replacement);
+       cap::replaceSelectionWithString(bufferview.cursor(), replacement, true);
        kernel().buffer().markDirty();
        bufferview.update();
        // fix up the count
index c54c36b1ec8f2e0a0c16f9b3609806174a68f329..14feb353e5518d7aac4cee337c8795c7df6ae242 100644 (file)
@@ -226,9 +226,7 @@ int replace(BufferView * bv, string const & searchstr,
                return 0;
 
        LCursor & cur = bv->cursor();
-       lyx::cap::replaceSelectionWithString(cur, replacestr);
-       lyx::cap::setSelectionRange(cur, replacestr.length());
-       cur.top() = fw ? cur.selEnd() : cur.selBegin();
+       lyx::cap::replaceSelectionWithString(cur, replacestr, fw);
        bv->buffer()->markDirty();
        find(bv, searchstr, cs, mw, fw);
        bv->update();
@@ -274,7 +272,7 @@ void find(BufferView * bv, FuncRequest const & ev)
        if (!bv || ev.action != LFUN_WORD_FIND)
                return;
 
-       lyxerr << "find called, cmd: " << ev << std::endl;
+       //lyxerr << "find called, cmd: " << ev << std::endl;
 
        // data is of the form
        // "<search>