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;
47 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
51 find_work_area_->setGuiView(view_);
52 find_work_area_->init();
53 setFocusProxy(find_work_area_);
54 replace_work_area_->setGuiView(view_);
55 replace_work_area_->init();
56 // We don't want two cursors blinking.
57 replace_work_area_->stopBlinkingCursor();
61 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
63 LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
64 if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
65 QKeyEvent *e = static_cast<QKeyEvent *> (event);
66 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
69 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
70 if (e->modifiers() == Qt::ShiftModifier) {
71 on_findPrevPB_clicked();
73 } else if (e->modifiers() == Qt::NoModifier) {
74 on_findNextPB_clicked();
79 // standard event processing
80 return QWidget::eventFilter(obj, event);
84 void FindAndReplaceWidget::findAdv(bool casesensitive,
85 bool matchword, bool backwards,
86 bool expandmacros, bool ignoreformat)
88 Buffer & buffer = find_work_area_->bufferView().buffer();
89 docstring searchString;
91 OutputParams runparams(&buffer.params().encoding());
93 runparams.nice = true;
94 runparams.flavor = OutputParams::LATEX;
95 runparams.linelen = 80; //lyxrc.plaintext_linelen;
96 // No side effect of file copying and image conversion
97 runparams.dryrun = true;
98 buffer.texrow().reset();
99 // latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
100 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
101 ParagraphList::const_iterator const end = buffer.paragraphs().end();
102 for (; pit != end; ++pit) {
103 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
104 LYXERR0("searchString up to here: " << os.str());
106 searchString = os.str();
108 ParIterator it = buffer.par_iterator_begin();
109 ParIterator end = buffer.par_iterator_end();
110 OutputParams runparams(&buffer.params().encoding());
112 runparams.nice = true;
113 runparams.flavor = OutputParams::LATEX;
114 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
115 runparams.dryrun = true;
116 for (; it != end; ++it) {
117 LYXERR(Debug::DEBUG, "Adding to search string: '" << it->asString(false) << "'");
118 searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
121 // lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
122 if (to_utf8(searchString).empty()) {
123 buffer.message(_("Nothing to search"));
126 bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
127 FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
128 expandmacros, ignoreformat, regexp);
129 LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
130 std::ostringstream oss;
132 LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
133 dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
135 // findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
136 // searchString, len, casesensitive, matchword, ! backwards, expandmacros);
140 void FindAndReplaceWidget::find(bool backwards)
142 // FIXME: create a Dialog::returnFocus() or something instead of this:
143 view_.setCurrentWorkArea(view_.currentMainWorkArea());
144 // FIXME: This should be an LFUN.
145 findAdv(caseCB->isChecked(),
146 wordsCB->isChecked(),
148 expandMacrosCB->isChecked(),
149 ignoreFormatCB->isChecked());
150 view_.currentMainWorkArea()->redraw();
151 find_work_area_->setFocus();
155 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
157 static char const * regexps[] = {
158 ".*", ".+", "[a-z]+", "[0-9]+"
160 //lyxerr << "Index: " << index << std::endl;
161 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
162 find_work_area_->setFocus();
163 Cursor & cur = find_work_area_->bufferView().cursor();
164 if (! cur.inRegexped())
165 dispatch(FuncRequest(LFUN_REGEXP_MODE));
166 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
167 regexpInsertCombo->setCurrentIndex(0);
172 void FindAndReplaceWidget::on_closePB_clicked()
174 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
178 void FindAndReplaceWidget::on_findNextPB_clicked() {
183 void FindAndReplaceWidget::on_findPrevPB_clicked() {
188 void FindAndReplaceWidget::on_replacePB_clicked()
193 void FindAndReplaceWidget::on_replaceallPB_clicked()
198 void FindAndReplaceWidget::showEvent(QShowEvent *ev)
200 replace_work_area_->setEnabled(true);
201 replace_work_area_->redraw();
202 find_work_area_->setFocus();
203 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
204 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
205 find_work_area_->redraw();
206 find_work_area_->installEventFilter(this);
210 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
212 find_work_area_->removeEventFilter(this);
213 this->QWidget::hideEvent(ev);
217 bool FindAndReplaceWidget::initialiseParams(std::string const & params)
219 find_work_area_->redraw();
220 replace_work_area_->setEnabled(true);
221 replace_work_area_->redraw();
222 find_work_area_->setFocus();
223 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
224 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
229 FindAndReplace::FindAndReplace(GuiView & parent,
230 Qt::DockWidgetArea area, Qt::WindowFlags flags)
231 : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
233 widget_ = new FindAndReplaceWidget(parent);
235 setFocusProxy(widget_);
239 FindAndReplace::~FindAndReplace()
246 bool FindAndReplace::initialiseParams(std::string const & params)
248 return widget_->initialiseParams(params);
252 Dialog * createGuiSearchAdv(GuiView & lv)
254 return new FindAndReplace(lv, Qt::RightDockWidgetArea);
258 } // namespace frontend
262 #include "moc_FindAndReplace.cpp"