From c0a425a319d2ea5f6c54d6bafcc0e00be7f1a1dd Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Wed, 14 Sep 2016 10:23:39 +0200 Subject: [PATCH] Move wrap check for simple search/replace to lyxfind This gets rid of some dirty dispatch tricks and fixes wrap-around on replace and in the spellchecker Fixes: #10378 --- src/BufferView.cpp | 22 ++------------ src/frontends/qt4/GuiSearch.cpp | 41 ++----------------------- src/frontends/qt4/GuiSearch.h | 3 -- src/frontends/qt4/GuiSpellchecker.cpp | 4 ++- src/lyxfind.cpp | 44 ++++++++++++++++++++++----- 5 files changed, 44 insertions(+), 70 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 0212b165d1..fa95d400c7 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -1523,14 +1523,8 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr) docstring const data = find2string(searched_string, true, false, fw); bool found = lyxfind(this, FuncRequest(LFUN_WORD_FIND, data)); - if (found) { + if (found) dr.screenUpdate(Update::Force | Update::FitCursor); - cur.dispatched(); - dispatched = true; - } else { - cur.undispatched(); - dispatched = false; - } break; } @@ -1542,14 +1536,9 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr) lyx::dispatch(FuncRequest(LFUN_DIALOG_SHOW, "findreplace")); break; } - if (lyxfind(this, req)) { + if (lyxfind(this, req)) dr.screenUpdate(Update::Force | Update::FitCursor); - cur.dispatched(); - dispatched = true; - } else { - cur.undispatched(); - dispatched = false; - } + d->search_request_cache_ = req; break; } @@ -1571,11 +1560,6 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr) if (lyxreplace(this, cmd, has_deleted)) { dr.forceBufferUpdate(); dr.screenUpdate(Update::Force | Update::FitCursor); - cur.dispatched(); - dispatched = true; - } else { - cur.undispatched(); - dispatched = false; } break; } diff --git a/src/frontends/qt4/GuiSearch.cpp b/src/frontends/qt4/GuiSearch.cpp index 7124647380..411ba2ef10 100644 --- a/src/frontends/qt4/GuiSearch.cpp +++ b/src/frontends/qt4/GuiSearch.cpp @@ -120,49 +120,12 @@ void GuiSearch::replaceallClicked() } -void GuiSearch::wrap_dispatch(const FuncRequest & func, bool forward) -{ - dispatch(func); - - BufferView * bv = const_cast(bufferview()); - - if (!bv->cursor().result().dispatched()) { - GuiView & lv = *const_cast(&lyxview()); - DocIterator cur_orig(bv->cursor()); - docstring q; - if (forward) - q = _("End of file reached while searching forward.\n" - "Continue searching from the beginning?"); - else - q = _("Beginning of file reached while searching backward.\n" - "Continue searching from the end?"); - int wrap_answer = frontend::Alert::prompt(_("Wrap search?"), - q, 0, 1, _("&Yes"), _("&No")); - if (wrap_answer == 0) { - if (forward) { - bv->cursor().clear(); - bv->cursor().push_back(CursorSlice(bv->buffer().inset())); - } else { - bv->cursor().setCursor(doc_iterator_end(&bv->buffer())); - bv->cursor().backwardPos(); - } - bv->clearSelection(); - dispatch(func); - if (bv->cursor().result().dispatched()) - return; - } - bv->cursor().setCursor(cur_orig); - lv.message(_("String not found.")); - } -} - - void GuiSearch::find(docstring const & search, bool casesensitive, bool matchword, bool forward) { docstring const data = find2string(search, casesensitive, matchword, forward); - wrap_dispatch(FuncRequest(LFUN_WORD_FIND, data), forward); + dispatch(FuncRequest(LFUN_WORD_FIND, data)); } @@ -173,7 +136,7 @@ void GuiSearch::replace(docstring const & search, docstring const & replace, docstring const data = replace2string(replace, search, casesensitive, matchword, all, forward); - wrap_dispatch(FuncRequest(LFUN_WORD_REPLACE, data), forward); + dispatch(FuncRequest(LFUN_WORD_REPLACE, data)); } diff --git a/src/frontends/qt4/GuiSearch.h b/src/frontends/qt4/GuiSearch.h index bd8231996a..b565553074 100644 --- a/src/frontends/qt4/GuiSearch.h +++ b/src/frontends/qt4/GuiSearch.h @@ -40,9 +40,6 @@ private: void dispatchParams() {} bool isBufferDependent() const { return true; } - /// Dispatches repeatedly func with wrap around question - void wrap_dispatch(const FuncRequest & func, bool forward); - /// Searches occurence of string void find(docstring const & search, bool casesensitive, bool matchword, bool forward); diff --git a/src/frontends/qt4/GuiSpellchecker.cpp b/src/frontends/qt4/GuiSpellchecker.cpp index 24aa43d683..cbdf4f4893 100644 --- a/src/frontends/qt4/GuiSpellchecker.cpp +++ b/src/frontends/qt4/GuiSpellchecker.cpp @@ -346,7 +346,7 @@ void SpellcheckerWidget::Private::setSelection( int const size = end.pos() - from.pos(); bv->putSelectionAt(from, size, false); } - bv->processUpdateFlags(Update::Force | Update::FitCursor); + bv->processUpdateFlags(Update::Force | Update::FitCursor); } void SpellcheckerWidget::Private::forward() @@ -490,6 +490,8 @@ void SpellcheckerWidget::on_replaceAllPB_clicked() LYXERR(Debug::GUI, "Replace all (" << replacement << ")"); dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring)); d->forward(); + // replace all wraps around + d->wrapAround(true); d->check(); // continue spellchecking d->canCheck(); } diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index cc8d1cf12f..92f9be155e 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -132,7 +132,8 @@ bool searchAllowed(docstring const & str) bool findOne(BufferView * bv, docstring const & searchstr, - bool case_sens, bool whole, bool forward, bool find_del = true) + bool case_sens, bool whole, bool forward, + bool find_del = true, bool check_wrap = false) { if (!searchAllowed(searchstr)) return false; @@ -149,6 +150,32 @@ bool findOne(BufferView * bv, docstring const & searchstr, if (match_len > 0) bv->putSelectionAt(cur, match_len, !forward); + else if (check_wrap) { + DocIterator cur_orig(bv->cursor()); + docstring q; + if (forward) + q = _("End of file reached while searching forward.\n" + "Continue searching from the beginning?"); + else + q = _("Beginning of file reached while searching backward.\n" + "Continue searching from the end?"); + int wrap_answer = frontend::Alert::prompt(_("Wrap search?"), + q, 0, 1, _("&Yes"), _("&No")); + if (wrap_answer == 0) { + if (forward) { + bv->cursor().clear(); + bv->cursor().push_back(CursorSlice(bv->buffer().inset())); + } else { + bv->cursor().setCursor(doc_iterator_end(&bv->buffer())); + bv->cursor().backwardPos(); + } + bv->clearSelection(); + if (findOne(bv, searchstr, case_sens, whole, forward, find_del, false)) + return true; + } + bv->cursor().setCursor(cur_orig); + return false; + } return match_len > 0; } @@ -221,11 +248,12 @@ pair replaceOne(BufferView * bv, docstring searchstr, bool whole, bool forward, bool findnext) { Cursor & cur = bv->cursor(); + bool found = false; if (!cur.selection()) { // no selection, non-empty search string: find it if (!searchstr.empty()) { - findOne(bv, searchstr, case_sens, whole, forward); - return make_pair(true, 0); + found = findOne(bv, searchstr, case_sens, whole, forward, true, findnext); + return make_pair(found, 0); } // empty search string if (!cur.inTexted()) @@ -253,8 +281,8 @@ pair replaceOne(BufferView * bv, docstring searchstr, // no selection or current selection is not search word: // just find the search word if (!have_selection || !match) { - findOne(bv, searchstr, case_sens, whole, forward); - return make_pair(true, 0); + found = findOne(bv, searchstr, case_sens, whole, forward, true, findnext); + return make_pair(found, 0); } // we're now actually ready to replace. if the buffer is @@ -269,7 +297,7 @@ pair replaceOne(BufferView * bv, docstring searchstr, cur.pos() = cur.lastpos()); } if (findnext) - findOne(bv, searchstr, case_sens, whole, forward, false); + findOne(bv, searchstr, case_sens, whole, forward, false, findnext); return make_pair(true, 1); } @@ -323,7 +351,7 @@ bool lyxfind(BufferView * bv, FuncRequest const & ev) bool matchword = parse_bool(howto); bool forward = parse_bool(howto); - return findOne(bv, search, casesensitive, matchword, forward); + return findOne(bv, search, casesensitive, matchword, forward, true, true); } @@ -380,7 +408,7 @@ bool lyxreplace(BufferView * bv, } else if (findnext) { // if we have deleted characters, we do not replace at all, but // rather search for the next occurence - if (findOne(bv, search, casesensitive, matchword, forward)) + if (findOne(bv, search, casesensitive, matchword, forward, true, findnext)) update = true; else bv->message(_("String not found."));