From: Pavel Sanda Date: Wed, 14 Jan 2009 15:34:56 +0000 (+0000) Subject: FindAdvSearch. Next patch from Tommaso. X-Git-Tag: 2.0.0~7423 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=82883a05f4280cc4509dcf44266a7b3fd95a0ed0;p=features.git FindAdvSearch. Next patch from Tommaso. http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg147338.html git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@28163 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/frontends/qt4/FindAndReplace.cpp b/src/frontends/qt4/FindAndReplace.cpp index 9f32ad9bf8..cec8a27616 100644 --- a/src/frontends/qt4/FindAndReplace.cpp +++ b/src/frontends/qt4/FindAndReplace.cpp @@ -80,30 +80,32 @@ bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event) return QWidget::eventFilter(obj, event); } +static docstring buffer_to_latex(Buffer & buffer) { + OutputParams runparams(&buffer.params().encoding()); + odocstringstream os; + runparams.nice = true; + runparams.flavor = OutputParams::LATEX; + runparams.linelen = 80; //lyxrc.plaintext_linelen; + // No side effect of file copying and image conversion + runparams.dryrun = true; + buffer.texrow().reset(); + ParagraphList::const_iterator pit = buffer.paragraphs().begin(); + ParagraphList::const_iterator const end = buffer.paragraphs().end(); + for (; pit != end; ++pit) { + TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams); + LYXERR(Debug::DEBUG, "searchString up to here: " << os.str()); + } + return os.str(); +} -void FindAndReplaceWidget::findAdv(bool casesensitive, - bool matchword, bool backwards, - bool expandmacros, bool ignoreformat) +void FindAndReplaceWidget::findAndReplace( + bool casesensitive, bool matchword, bool backwards, + bool expandmacros, bool ignoreformat, bool replace) { Buffer & buffer = find_work_area_->bufferView().buffer(); docstring searchString; if (!ignoreformat) { - OutputParams runparams(&buffer.params().encoding()); - odocstringstream os; - runparams.nice = true; - runparams.flavor = OutputParams::LATEX; - runparams.linelen = 80; //lyxrc.plaintext_linelen; - // No side effect of file copying and image conversion - runparams.dryrun = true; - buffer.texrow().reset(); -// latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams); - ParagraphList::const_iterator pit = buffer.paragraphs().begin(); - ParagraphList::const_iterator const end = buffer.paragraphs().end(); - for (; pit != end; ++pit) { - TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams); - LYXERR0("searchString up to here: " << os.str()); - } - searchString = os.str(); + searchString = buffer_to_latex(buffer); } else { ParIterator it = buffer.par_iterator_begin(); ParIterator end = buffer.par_iterator_end(); @@ -124,8 +126,15 @@ void FindAndReplaceWidget::findAdv(bool casesensitive, return; } bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos; - FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards, - expandmacros, ignoreformat, regexp); + docstring replaceString; + if (replace) { + Buffer & replace_buffer = replace_work_area_->bufferView().buffer(); + replaceString = buffer_to_latex(replace_buffer); + } else { + replaceString = from_utf8(LYX_FR_NULL_STRING); + } + FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards, + expandmacros, ignoreformat, regexp, replaceString); LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl); std::ostringstream oss; oss << opt; @@ -137,16 +146,17 @@ void FindAndReplaceWidget::findAdv(bool casesensitive, } -void FindAndReplaceWidget::find(bool backwards) +void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace) { // FIXME: create a Dialog::returnFocus() or something instead of this: view_.setCurrentWorkArea(view_.currentMainWorkArea()); // FIXME: This should be an LFUN. - findAdv(caseCB->isChecked(), - wordsCB->isChecked(), - backwards, - expandMacrosCB->isChecked(), - ignoreFormatCB->isChecked()); + findAndReplace(caseCB->isChecked(), + wordsCB->isChecked(), + backwards, + expandMacrosCB->isChecked(), + ignoreFormatCB->isChecked(), + replace); view_.currentMainWorkArea()->redraw(); find_work_area_->setFocus(); } @@ -176,17 +186,24 @@ void FindAndReplaceWidget::on_closePB_clicked() void FindAndReplaceWidget::on_findNextPB_clicked() { - find(false); + findAndReplace(false, false); } void FindAndReplaceWidget::on_findPrevPB_clicked() { - find(true); + findAndReplace(true, false); +} + + +void FindAndReplaceWidget::on_replaceNextPB_clicked() +{ + findAndReplace(false, true); } -void FindAndReplaceWidget::on_replacePB_clicked() +void FindAndReplaceWidget::on_replacePrevPB_clicked() { + findAndReplace(true, true); } diff --git a/src/frontends/qt4/FindAndReplace.h b/src/frontends/qt4/FindAndReplace.h index 87a7328678..e74e878f78 100644 --- a/src/frontends/qt4/FindAndReplace.h +++ b/src/frontends/qt4/FindAndReplace.h @@ -44,16 +44,14 @@ private: // add a string to the combo if needed void remember(std::string const & find, QComboBox & combo); - void findAdv(bool casesensitive, - bool matchword, bool backwards, - bool expandmacros, bool ignoreformat); + void findAndReplace( + bool casesensitive, bool matchword, bool backwards, + bool expandmacros, bool ignoreformat, bool replace + ); void find(docstring const & str, int len, bool casesens, bool words, bool backwards, bool expandmacros); - void find(bool backwards); + void findAndReplace(bool backwards, bool replace); - void replace(docstring const & findstr, - docstring const & replacestr, - bool casesens, bool words, bool backwards, bool expandmacros, bool all); bool eventFilter(QObject *obj, QEvent *event); void virtual showEvent(QShowEvent *ev); @@ -62,7 +60,8 @@ private: protected Q_SLOTS: void on_findNextPB_clicked(); void on_findPrevPB_clicked(); - void on_replacePB_clicked(); + void on_replaceNextPB_clicked(); + void on_replacePrevPB_clicked(); void on_replaceallPB_clicked(); void on_closePB_clicked(); void on_regexpInsertCombo_currentIndexChanged(int index); diff --git a/src/frontends/qt4/ui/FindAndReplaceUi.ui b/src/frontends/qt4/ui/FindAndReplaceUi.ui index 2b675109d9..cb201ab038 100644 --- a/src/frontends/qt4/ui/FindAndReplaceUi.ui +++ b/src/frontends/qt4/ui/FindAndReplaceUi.ui @@ -66,7 +66,7 @@ 0 0 - 227 + 221 351 @@ -90,8 +90,8 @@ 0 0 - 221 - 85 + 215 + 89 @@ -123,7 +123,7 @@ - false + true Replace Ne&xt @@ -163,8 +163,8 @@ 0 0 - 221 - 85 + 215 + 89 @@ -186,7 +186,7 @@ - false + true Replace P&rev diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index 9b9b855d34..0b6bc6509f 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -30,6 +30,8 @@ #include "ParIterator.h" #include "TexRow.h" #include "Text.h" +#include "FuncRequest.h" +#include "LyXFunc.h" #include "mathed/InsetMath.h" #include "mathed/InsetMathGrid.h" @@ -531,7 +533,7 @@ bool braces_match(string::const_iterator const & beg, **/ class MatchStringAdv { public: - MatchStringAdv(lyx::Buffer const & buf, FindAdvOptions const & opt); + MatchStringAdv(lyx::Buffer const & buf, FindAndReplaceOptions const & opt); /** Tests if text starting at the supplied position matches with the one provided to the MatchStringAdv ** constructor as opt.search, under the opt.* options settings. @@ -549,7 +551,7 @@ public: /// buffer lyx::Buffer const & buf; /// options - FindAdvOptions const & opt; + FindAndReplaceOptions const & opt; private: /** Normalize a stringified or latexified LyX paragraph. @@ -580,7 +582,7 @@ private: }; -MatchStringAdv::MatchStringAdv(lyx::Buffer const & buf, FindAdvOptions const & opt) +MatchStringAdv::MatchStringAdv(lyx::Buffer const & buf, FindAndReplaceOptions const & opt) : buf(buf), opt(opt) { par_as_string = normalize(opt.search); @@ -943,7 +945,7 @@ int findBackwardsAdv(DocIterator & cur, MatchStringAdv const & match) { } // anonym namespace -docstring stringifyFromForSearch(FindAdvOptions const & opt, +docstring stringifyFromForSearch(FindAndReplaceOptions const & opt, DocIterator const & cur, int len) { if (!opt.ignoreformat) @@ -953,17 +955,17 @@ docstring stringifyFromForSearch(FindAdvOptions const & opt, } -lyx::FindAdvOptions::FindAdvOptions(docstring const & search, bool casesensitive, +lyx::FindAndReplaceOptions::FindAndReplaceOptions(docstring const & search, bool casesensitive, bool matchword, bool forward, bool expandmacros, bool ignoreformat, - bool regexp) + bool regexp, docstring const & replace) : search(search), casesensitive(casesensitive), matchword(matchword), forward(forward), expandmacros(expandmacros), ignoreformat(ignoreformat), - regexp(regexp) + regexp(regexp), replace(replace) { } /// Perform a FindAdv operation. -bool findAdv(BufferView * bv, FindAdvOptions const & opt) +bool findAdv(BufferView * bv, FindAndReplaceOptions const & opt) { DocIterator cur = bv->cursor(); int match_len = 0; @@ -997,7 +999,9 @@ bool findAdv(BufferView * bv, FindAdvOptions const & opt) LYXERR(Debug::DEBUG, "Putting selection at " << cur << " with len: " << match_len); bv->putSelectionAt(cur, match_len, ! opt.forward); bv->message(_("Match found!")); - //bv->update(); + if (opt.replace != docstring(from_utf8(LYX_FR_NULL_STRING))) { + dispatch(FuncRequest(LFUN_SELF_INSERT, opt.replace)); + } return true; } @@ -1008,14 +1012,14 @@ void findAdv(BufferView * bv, FuncRequest const & ev) if (!bv || ev.action != LFUN_WORD_FINDADV) return; - FindAdvOptions opt; + FindAndReplaceOptions opt; istringstream iss(to_utf8(ev.argument())); iss >> opt; findAdv(bv, opt); } -ostringstream & operator<<(ostringstream & os, lyx::FindAdvOptions const & opt) +ostringstream & operator<<(ostringstream & os, lyx::FindAndReplaceOptions const & opt) { os << to_utf8(opt.search) << "\nEOSS\n" << opt.casesensitive << ' ' @@ -1023,14 +1027,15 @@ ostringstream & operator<<(ostringstream & os, lyx::FindAdvOptions const & opt) << opt.forward << ' ' << opt.expandmacros << ' ' << opt.ignoreformat << ' ' - << opt.regexp; + << opt.regexp << ' ' + << to_utf8(opt.replace) << "\nEOSS\n"; LYXERR(Debug::DEBUG, "built: " << os.str()); return os; } -istringstream & operator>>(istringstream & is, lyx::FindAdvOptions & opt) +istringstream & operator>>(istringstream & is, lyx::FindAndReplaceOptions & opt) { LYXERR(Debug::DEBUG, "parsing"); string s; @@ -1047,8 +1052,21 @@ istringstream & operator>>(istringstream & is, lyx::FindAdvOptions & opt) LYXERR(Debug::DEBUG, "searching for: '" << s << "'"); opt.search = from_utf8(s); is >> opt.casesensitive >> opt.matchword >> opt.forward >> opt.expandmacros >> opt.ignoreformat >> opt.regexp; + is.get(); // Waste space before replace string + s = ""; + getline(is, line); + while (line != "EOSS") { + if (! s.empty()) + s = s + "\n"; + s = s + line; + if (is.eof()) // Tolerate malformed request + break; + getline(is, line); + } LYXERR(Debug::DEBUG, "parsed: " << opt.casesensitive << ' ' << opt.matchword << ' ' << opt.forward << ' ' << opt.expandmacros << ' ' << opt.ignoreformat << ' ' << opt.regexp); + LYXERR(Debug::DEBUG, "replacing with: '" << s << "'"); + opt.replace = from_utf8(s); return is; } diff --git a/src/lyxfind.h b/src/lyxfind.h index f7a85ccdb9..7da80af65c 100644 --- a/src/lyxfind.h +++ b/src/lyxfind.h @@ -66,18 +66,22 @@ void replace(BufferView * bv, FuncRequest const &, bool has_deleted = false); /// find the next change in the buffer bool findNextChange(BufferView * bv); -class FindAdvOptions { +// Hopefully, nobody will ever replace with something like this +#define LYX_FR_NULL_STRING "__LYX__F&R__NULL__STRING__" + +class FindAndReplaceOptions { public: - FindAdvOptions( + FindAndReplaceOptions( docstring const & search, bool casesensitive, bool matchword, bool forward, bool expandmacros, bool ignoreformat, - bool regexp + bool regexp, + docstring const & replace ); - FindAdvOptions() { } + FindAndReplaceOptions() { } docstring search; bool casesensitive; bool matchword; @@ -85,19 +89,20 @@ public: bool expandmacros; bool ignoreformat; bool regexp; + docstring replace; }; /// Write a FindAdvOptions instance to a stringstream -std::ostringstream & operator<<(std::ostringstream & os, lyx::FindAdvOptions const & opt); +std::ostringstream & operator<<(std::ostringstream & os, lyx::FindAndReplaceOptions const & opt); /// Read a FindAdvOptions instance from a stringstream -std::istringstream & operator>>(std::istringstream & is, lyx::FindAdvOptions & opt); +std::istringstream & operator>>(std::istringstream & is, lyx::FindAndReplaceOptions & opt); /// Dispatch a LFUN_WORD_FINDADV command request void findAdv(BufferView * bv, FuncRequest const & ev); /// Perform a FindAdv operation. -bool findAdv(BufferView * bv, FindAdvOptions const & opt); +bool findAdv(BufferView * bv, FindAndReplaceOptions const & opt); /** Computes the simple-text or LaTeX export (depending on opt) of buf starting ** from cur and ending len positions after cur, if len is positive, or at the @@ -107,7 +112,7 @@ bool findAdv(BufferView * bv, FindAdvOptions const & opt); ** Ideally, this should not be needed, and the opt.search field should become a Text const &. **/ docstring stringifyFromForSearch( - FindAdvOptions const & opt, + FindAndReplaceOptions const & opt, DocIterator const & cur, int len = -1);