]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/FindAndReplace.cpp
Add GUI for search-ignore
[lyx.git] / src / frontends / qt / FindAndReplace.cpp
index bd3edcf3c9f1cdd7d6011956b73a131d1201436f..d72fa9265a4d1c7032a785acbbb8877df91bba24 100644 (file)
@@ -14,8 +14,6 @@
 
 #include "GuiApplication.h"
 #include "GuiView.h"
-#include "GuiWorkArea.h"
-#include "qt_helpers.h"
 
 #include "Buffer.h"
 #include "BufferList.h"
 #include "Cursor.h"
 #include "FuncRequest.h"
 #include "Language.h"
-#include "Lexer.h"
 #include "LyX.h"
 #include "lyxfind.h"
 #include "Text.h"
-#include "TextClass.h"
 
 #include "frontends/alert.h"
 
@@ -106,26 +102,22 @@ bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
 
        case Qt::Key_Enter:
        case Qt::Key_Return: {
-               // with shift we (temporarily) change search/replace direction
-               bool const searchback = searchbackCB->isChecked();
-               if (e->modifiers() == Qt::ShiftModifier && !searchback)
-                       searchbackCB->setChecked(true);
-
-               if (obj == find_work_area_)
-                       on_findNextPB_clicked();
+               bool const searchback = (e->modifiers() == Qt::ShiftModifier);
+               bool const replace = (obj == replace_work_area_);
+               findAndReplace(searchback, replace);
+               if (replace)
+                       replace_work_area_->setFocus();
                else
-                       on_replacePB_clicked();
-               // back to how it was
-               searchbackCB->setChecked(searchback);
+                       find_work_area_->setFocus();
                return true;
        }
 
        case Qt::Key_Tab:
                if (e->modifiers() == Qt::NoModifier) {
                        if (obj == find_work_area_){
-                               LYXERR(Debug::FIND, "Focusing replace WA");
+                               LYXERR(Debug::FINDVERBOSE, "Focusing replace WA");
                                replace_work_area_->setFocus();
-                               LYXERR(Debug::FIND, "Selecting entire replace buffer");
+                               LYXERR(Debug::FINDVERBOSE, "Selecting entire replace buffer");
                                dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
                                dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
                                return true;
@@ -135,9 +127,9 @@ bool FindAndReplaceWidget::eventFilter(QObject * obj, QEvent * event)
 
        case Qt::Key_Backtab:
                if (obj == replace_work_area_) {
-                       LYXERR(Debug::FIND, "Focusing find WA");
+                       LYXERR(Debug::FINDVERBOSE, "Focusing find WA");
                        find_work_area_->setFocus();
-                       LYXERR(Debug::FIND, "Selecting entire find buffer");
+                       LYXERR(Debug::FINDVERBOSE, "Selecting entire find buffer");
                        dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
                        dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
                        return true;
@@ -180,7 +172,7 @@ static vector<string> const & allManualsFiles()
 static bool nextDocumentBuffer(Buffer * & buf)
 {
        ListOfBuffers const children = buf->allRelatives();
-       LYXERR(Debug::FIND, "children.size()=" << children.size());
+       LYXERR(Debug::FINDVERBOSE, "children.size()=" << children.size());
        ListOfBuffers::const_iterator it =
                find(children.begin(), children.end(), buf);
        LASSERT(it != children.end(), return false);
@@ -201,7 +193,7 @@ static bool nextDocumentBuffer(Buffer * & buf)
 static bool prevDocumentBuffer(Buffer * & buf)
 {
        ListOfBuffers const children = buf->allRelatives();
-       LYXERR(Debug::FIND, "children.size()=" << children.size());
+       LYXERR(Debug::FINDVERBOSE, "children.size()=" << children.size());
        ListOfBuffers::const_iterator it =
                find(children.begin(), children.end(), buf);
        LASSERT(it != children.end(), return false)
@@ -352,9 +344,9 @@ bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool
        UndoGroupHelper helper(buf);
 
        do {
-               LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
+               LYXERR(Debug::FINDVERBOSE, "Dispatching LFUN_WORD_FINDADV");
                dispatch(cmd);
-               LYXERR(Debug::FIND, "dispatched");
+               LYXERR(Debug::FINDVERBOSE, "dispatched");
                if (bv->cursor().result().dispatched()) {
                        // New match found and selected (old selection replaced if needed)
                        if (replace_all)
@@ -406,7 +398,7 @@ bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool
                        //lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
                        bv->cursor().setCursor(doc_iterator_end(buf));
                        bv->cursor().backwardPos();
-                       LYXERR(Debug::FIND, "findBackAdv5: cur: "
+                       LYXERR(Debug::FINDVERBOSE, "findBackAdv5: cur: "
                                << bv->cursor());
                }
                bv->clearSelection();
@@ -429,7 +421,7 @@ bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool
 /// Return true if a match was found
 bool FindAndReplaceWidget::findAndReplace(
        bool casesensitive, bool matchword, bool backwards,
-       bool expandmacros, bool ignoreformat, bool replace,
+       bool expandmacros, bool adhereformat, bool replace,
        bool keep_case, bool replace_all)
 {
        Buffer & find_buf = find_work_area_->bufferView().buffer();
@@ -462,25 +454,65 @@ bool FindAndReplaceWidget::findAndReplace(
                        ? FindAndReplaceOptions::R_ONLY_MATHS
                        : FindAndReplaceOptions::R_EVERYTHING;
 
-       LYXERR(Debug::FIND, "FindAndReplaceOptions: "
+       LYXERR(Debug::FINDVERBOSE, "FindAndReplaceOptions: "
               << "find_buf_name=" << find_buf_name
               << ", casesensitiv=" << casesensitive
               << ", matchword=" << matchword
               << ", backwards=" << backwards
               << ", expandmacros=" << expandmacros
-              << ", ignoreformat=" << ignoreformat
+              << ", adhereformat=" << adhereformat
               << ", repl_buf_name" << repl_buf_name
               << ", keep_case=" << keep_case
               << ", scope=" << scope
               << ", restr=" << restr);
 
        FindAndReplaceOptions opt(find_buf_name, casesensitive, matchword,
-                                 !backwards, expandmacros, ignoreformat,
+                                 !backwards, expandmacros, !adhereformat,
                                  repl_buf_name, keep_case, scope, restr);
+       
+       if (adhereformat) {
+               // Formats to adhere
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("language",
+                                         !adhereLanguageCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("color",
+                                         !adhereColorCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("family",
+                                         !adhereFFamilyCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("series",
+                                         !adhereFSeriesCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("shape",
+                                         !adhereFShapeCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("size",
+                                         !adhereFSizeCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("markup",
+                                         !adhereMarkupCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("underline",
+                                         !adhereUnderlineCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("strike",
+                                         !adhereStrikeCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("deleted",
+                                         !adhereDeletedCB->isChecked())));
+               lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("sectioning",
+                                         !adhereSectioningCB->isChecked())));
+       }
+       lyx::dispatch(FuncRequest(LFUN_SEARCH_IGNORE, checkState("non-output-content",
+                                 ignoreNonOutputCB->isChecked())));
+       
        return findAndReplaceScope(opt, replace_all);
 }
 
 
+docstring const FindAndReplaceWidget::checkState(string const s, bool const b)
+{
+       docstring res = from_ascii(s) + from_ascii(" ");
+       if (b)
+               res += from_ascii("true");
+       else
+               res += from_ascii("false");
+       return res;
+}
+
+
 bool FindAndReplaceWidget::findAndReplace(bool backwards, bool replace, bool replace_all)
 {
        if (! view_.currentMainWorkArea()) {
@@ -500,7 +532,7 @@ bool FindAndReplaceWidget::findAndReplace(bool backwards, bool replace, bool rep
                wordsCB->isChecked(),
                backwards,
                expandMacrosCB->isChecked(),
-               ignoreFormatCB->isChecked(),
+               adhereFormatGB->isChecked(),
                replace,
                keepCaseCB->isChecked(),
                replace_all);
@@ -534,6 +566,40 @@ void FindAndReplaceWidget::on_replaceallPB_clicked()
 }
 
 
+void FindAndReplaceWidget::on_searchbackCB_clicked()
+{
+       updateButtons();
+}
+
+
+void FindAndReplaceWidget::setFormatIgnores(bool const b)
+{
+       adhereLanguageCB->setChecked(b);
+       adhereColorCB->setChecked(b);
+       adhereFFamilyCB->setChecked(b);
+       adhereFSeriesCB->setChecked(b);
+       adhereFShapeCB->setChecked(b);
+       adhereFSizeCB->setChecked(b);
+       adhereMarkupCB->setChecked(b);
+       adhereUnderlineCB->setChecked(b);
+       adhereStrikeCB->setChecked(b);
+       adhereDeletedCB->setChecked(b);
+       adhereSectioningCB->setChecked(b);
+}
+
+
+void FindAndReplaceWidget::on_selectAllPB_clicked()
+{
+       setFormatIgnores(true);
+}
+
+
+void FindAndReplaceWidget::on_deselectAllPB_clicked()
+{
+       setFormatIgnores(false);
+}
+
+
 // Copy selected elements from bv's BufferParams to the dest_bv's
 static void copy_params(BufferView const & bv, BufferView & dest_bv) {
        Buffer const & doc_buf = bv.buffer();
@@ -558,7 +624,7 @@ void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
        replace_work_area_->installEventFilter(this);
 
        view_.setCurrentWorkArea(find_work_area_);
-       LYXERR(Debug::FIND, "Selecting entire find buffer");
+       LYXERR(Debug::FINDVERBOSE, "Selecting entire find buffer");
        dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
        dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
 }
@@ -581,17 +647,23 @@ bool FindAndReplaceWidget::initialiseParams(std::string const & /*params*/)
 void FindAndReplace::updateView()
 {
        widget_->updateGUI();
+       widget_->updateButtons();
 }
 
 
 FindAndReplace::FindAndReplace(GuiView & parent,
-               Qt::DockWidgetArea area, Qt::WindowFlags flags)
-       : DockView(parent, "findreplaceadv", qt_("Advanced Find and Replace"),
+                              Qt::DockWidgetArea area,
+                              Qt::WindowFlags flags)
+    : DockView(parent, "findreplaceadv", qt_("Advanced Find and Replace"),
                   area, flags)
 {
        widget_ = new FindAndReplaceWidget(parent);
        setWidget(widget_);
        setFocusProxy(widget_);
+#ifdef Q_OS_MAC
+       // On Mac show and floating
+       setFloating(true);
+#endif
 
        connect(this, SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
                widget_, SLOT(dockLocationChanged(Qt::DockWidgetArea)));
@@ -633,14 +705,19 @@ void FindAndReplaceWidget::updateGUI()
 }
 
 
-Dialog * createGuiSearchAdv(GuiView & lv)
+void FindAndReplaceWidget::updateButtons()
 {
-       FindAndReplace * gui = new FindAndReplace(lv, Qt::RightDockWidgetArea);
-#ifdef Q_OS_MAC
-       // On Mac show and floating
-       gui->setFloating(true);
-#endif
-       return gui;
+       if (searchbackCB->isChecked()) {
+               findNextPB->setText(qt_("&< Find"));
+               findNextPB->setToolTip(qt_("Find previous occurrence (Shift+Enter, forwards: Enter)"));
+               replacePB->setText(qt_("< Rep&lace"));
+               replacePB->setToolTip(qt_("Replace and find previous occurrence (Shift+Enter, forwards: Enter)"));
+       } else {
+               findNextPB->setText(qt_("Find &>"));
+               findNextPB->setToolTip(qt_("Find next occurrence (Enter, backwards: Shift+Enter)"));
+               replacePB->setText(qt_("Rep&lace >"));
+               replacePB->setToolTip(qt_("Replace and find next occurrence (Enter, backwards: Shift+Enter)"));
+       }
 }