diff --git a/src/CutAndPaste.C b/src/CutAndPaste.C index 566d63016f..8dc3e7be8a 100644 --- a/src/CutAndPaste.C +++ b/src/CutAndPaste.C @@ -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; diff --git a/src/CutAndPaste.h b/src/CutAndPaste.h index 2b3a1c36be..10e5a80c3d 100644 --- a/src/CutAndPaste.h +++ b/src/CutAndPaste.h @@ -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); /// diff --git a/src/frontends/controllers/ControlSpellchecker.C b/src/frontends/controllers/ControlSpellchecker.C index 092c2e3323..2aa74e0173 100644 --- a/src/frontends/controllers/ControlSpellchecker.C +++ b/src/frontends/controllers/ControlSpellchecker.C @@ -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 diff --git a/src/lyxfind.C b/src/lyxfind.C index c54c36b1ec..14feb353e5 100644 --- a/src/lyxfind.C +++ b/src/lyxfind.C @@ -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 // "