2 * \file FindAndReplace.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Tommaso Cucinotta
8 * Full author contact details are available in file CREDITS.
13 #include "FindAndReplace.h"
15 #include "GuiApplication.h"
16 #include "qt_helpers.h"
18 #include "GuiWorkArea.h"
20 #include "buffer_funcs.h"
21 #include "BufferParams.h"
23 #include "FuncRequest.h"
25 #include "OutputParams.h"
26 #include "output_latex.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/FileName.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
35 #include <QCloseEvent>
41 using namespace lyx::support;
46 FindAndReplace::FindAndReplace(GuiView & parent)
47 : DockView(parent, "Find LyX", "Find LyX Dialog", Qt::RightDockWidgetArea)
50 find_work_area_->setGuiView(parent);
51 find_work_area_->init();
52 setFocusProxy(find_work_area_);
53 replace_work_area_->setGuiView(parent);
54 replace_work_area_->init();
55 // We don't want two cursors blinking.
56 replace_work_area_->stopBlinkingCursor();
60 bool FindAndReplace::eventFilter(QObject *obj, QEvent *event)
62 LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
63 if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
64 QKeyEvent *e = static_cast<QKeyEvent *> (event);
65 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
68 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
69 if (e->modifiers() == Qt::ShiftModifier) {
70 on_findPrevPB_clicked();
72 } else if (e->modifiers() == Qt::NoModifier) {
73 on_findNextPB_clicked();
78 // standard event processing
79 return QObject::eventFilter(obj, event);
83 void FindAndReplace::findAdv(bool casesensitive,
84 bool matchword, bool backwards,
85 bool expandmacros, bool ignoreformat)
87 Buffer & buffer = find_work_area_->bufferView().buffer();
88 docstring searchString;
90 OutputParams runparams(&buffer.params().encoding());
92 runparams.nice = true;
93 runparams.flavor = OutputParams::LATEX;
94 runparams.linelen = 80; //lyxrc.plaintext_linelen;
95 // No side effect of file copying and image conversion
96 runparams.dryrun = true;
97 buffer.texrow().reset();
98 // latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
99 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
100 ParagraphList::const_iterator const end = buffer.paragraphs().end();
101 for (; pit != end; ++pit) {
102 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
103 LYXERR0("searchString up to here: " << os.str());
105 searchString = os.str();
107 ParIterator it = buffer.par_iterator_begin();
108 ParIterator end = buffer.par_iterator_end();
109 for (; it != end; ++it) {
110 LYXERR0("Adding to search string: '" << it->asString(false) << "'");
111 searchString += it->asString(AS_STR_INSETS);
114 // lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
115 if (to_utf8(searchString).empty()) {
116 buffer.message(_("Nothing to search"));
119 bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
120 FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
121 expandmacros, ignoreformat, regexp);
122 std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
123 std::ostringstream oss;
125 std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
126 dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
128 // findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
129 // searchString, len, casesensitive, matchword, ! backwards, expandmacros);
132 bool FindAndReplace::initialiseParams(std::string const &)
134 find_work_area_->redraw();
135 replace_work_area_->setEnabled(true);
136 replace_work_area_->redraw();
137 find_work_area_->setFocus();
138 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
139 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
144 void FindAndReplace::find(bool backwards)
146 // FIXME: create a Dialog::returnFocus() or something instead of this:
147 GuiView & gv = const_cast<GuiView &>(lyxview());
148 gv.setCurrentWorkArea(gv.currentMainWorkArea());
149 // FIXME: This should be an LFUN.
150 findAdv(caseCB->isChecked(),
151 wordsCB->isChecked(),
153 expandMacrosCB->isChecked(),
154 ignoreFormatCB->isChecked());
155 gv.currentMainWorkArea()->redraw();
156 find_work_area_->setFocus();
160 void FindAndReplace::on_regexpInsertCombo_currentIndexChanged(int index)
162 static char const * regexps[] = {
163 ".*", ".+", "[a-z]+", "[0-9]+"
165 //lyxerr << "Index: " << index << std::endl;
166 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
167 find_work_area_->setFocus();
168 Cursor & cur = find_work_area_->bufferView().cursor();
169 if (! cur.inRegexped())
170 dispatch(FuncRequest(LFUN_REGEXP_MODE));
171 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
172 regexpInsertCombo->setCurrentIndex(0);
177 void FindAndReplace::on_closePB_clicked()
179 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
183 void FindAndReplace::on_findNextPB_clicked() {
188 void FindAndReplace::on_findPrevPB_clicked() {
193 void FindAndReplace::on_replacePB_clicked()
198 void FindAndReplace::on_replaceallPB_clicked()
202 Dialog * createGuiSearchAdv(GuiView & lv)
204 return new FindAndReplace(lv);
208 } // namespace frontend
212 #include "moc_FindAndReplace.cpp"