]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/FindAndReplace.cpp
If we are in a closeEvent, we don't want to close all buffers, because these may...
[lyx.git] / src / frontends / qt4 / FindAndReplace.cpp
index 9f32ad9bf8b316f7a8082a7f06e8dee8832065e8..26bbc9612a9c43bb55666a88fda5aa9c1cd8dcd2 100644 (file)
@@ -26,7 +26,6 @@
 #include "output_latex.h"
 #include "TexRow.h"
 
-#include "support/convert.h"
 #include "support/debug.h"
 #include "support/FileName.h"
 #include "support/gettext.h"
@@ -60,7 +59,7 @@ FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
 
 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
 {
-       LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
+       LYXERR(Debug::FIND, "FindAndReplace::eventFilter()");
        if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
                QKeyEvent *e = static_cast<QKeyEvent *> (event);
                if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
@@ -80,30 +79,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::FIND, "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();
@@ -114,22 +115,37 @@ void FindAndReplaceWidget::findAdv(bool casesensitive,
                runparams.linelen = 100000; //lyxrc.plaintext_linelen;
                runparams.dryrun = true;
                for (; it != end; ++it) {
-                       LYXERR(Debug::DEBUG, "Adding to search string: '" << it->asString(false) << "'");
+                       LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
                        searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
                }
        }
-//     lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
        if (to_utf8(searchString).empty()) {
                buffer.message(_("Nothing to search"));
                return;
        }
        bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
-       FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
-               expandmacros, ignoreformat, regexp);
-       LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
+       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);
+       }
+       LYXERR(Debug::FIND, "FindAndReplaceOptions: "
+              << "searchstring=" << searchString
+              << ", casesensitiv=" << casesensitive
+              << ", matchword=" << matchword
+              << ", backwards=" << backwards
+              << ", expandmacros=" << expandmacros
+              << ", ignoreformat=" << ignoreformat
+              << ", regexp=" << regexp
+              << ", replaceString" << replaceString);
+       FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
+               expandmacros, ignoreformat, regexp, replaceString);
+       LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
        std::ostringstream oss;
        oss << opt;
-       LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
+       LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
        dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
 
        //      findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
@@ -137,16 +153,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();
 }
@@ -157,7 +174,7 @@ void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
        static char const * regexps[] = {
                ".*", ".+", "[a-z]+", "[0-9]+"
        };
-       //lyxerr << "Index: " << index << std::endl;
+       LYXERR(Debug::FIND, "Index: " << index);
        if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
                find_work_area_->setFocus();
                Cursor & cur = find_work_area_->bufferView().cursor();
@@ -176,17 +193,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);
 }
 
 
@@ -195,7 +219,7 @@ void FindAndReplaceWidget::on_replaceallPB_clicked()
 }
 
 
-void FindAndReplaceWidget::showEvent(QShowEvent *ev)
+void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
 {
        replace_work_area_->setEnabled(true);
        replace_work_area_->redraw();
@@ -214,7 +238,7 @@ void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
 }
 
 
-bool FindAndReplaceWidget::initialiseParams(std::string const & params)
+bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
 {
        find_work_area_->redraw();
        replace_work_area_->setEnabled(true);