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/debug.h"
30 #include "support/FileName.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
34 #include <QCloseEvent>
40 using namespace lyx::support;
46 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
50 find_work_area_->setGuiView(view_);
51 find_work_area_->init();
52 setFocusProxy(find_work_area_);
53 replace_work_area_->setGuiView(view_);
54 replace_work_area_->init();
55 // We don't want two cursors blinking.
56 replace_work_area_->stopBlinkingCursor();
60 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
62 LYXERR(Debug::FIND, "FindAndReplace::eventFilter(): obj=" << obj
63 << ", fwa=" << find_work_area_ << ", rwa=" << replace_work_area_
64 << "fsa=" << find_scroll_area_ << ", rsa=" << replace_scroll_area_);
65 if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
66 QKeyEvent *e = static_cast<QKeyEvent *> (event);
67 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
70 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
71 if (e->modifiers() == Qt::ShiftModifier) {
72 on_findPrevPB_clicked();
74 } else if (e->modifiers() == Qt::NoModifier) {
75 on_findNextPB_clicked();
78 } else if (e->key() == Qt::Key_Tab && e->modifiers() == Qt::NoModifier) {
79 LYXERR(Debug::FIND, "Focusing replace WA");
80 replace_work_area_->setFocus();
84 if (obj == replace_work_area_ && event->type() == QEvent::KeyPress) {
85 QKeyEvent *e = static_cast<QKeyEvent *> (event);
86 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
89 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
90 if (e->modifiers() == Qt::ShiftModifier) {
91 on_replacePrevPB_clicked();
93 } else if (e->modifiers() == Qt::NoModifier) {
94 on_replaceNextPB_clicked();
97 } else if (e->key() == Qt::Key_Backtab) {
98 LYXERR(Debug::FIND, "Focusing find WA");
99 find_work_area_->setFocus();
103 // standard event processing
104 return QWidget::eventFilter(obj, event);
107 static docstring buffer_to_latex(Buffer & buffer) {
108 OutputParams runparams(&buffer.params().encoding());
110 runparams.nice = true;
111 runparams.flavor = OutputParams::LATEX;
112 runparams.linelen = 80; //lyxrc.plaintext_linelen;
113 // No side effect of file copying and image conversion
114 runparams.dryrun = true;
115 buffer.texrow().reset();
116 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
117 ParagraphList::const_iterator const end = buffer.paragraphs().end();
118 for (; pit != end; ++pit) {
119 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
120 LYXERR(Debug::FIND, "searchString up to here: " << os.str());
125 void FindAndReplaceWidget::findAndReplace(
126 bool casesensitive, bool matchword, bool backwards,
127 bool expandmacros, bool ignoreformat, bool replace,
130 Buffer & buffer = find_work_area_->bufferView().buffer();
131 docstring searchString;
133 searchString = buffer_to_latex(buffer);
135 ParIterator it = buffer.par_iterator_begin();
136 ParIterator end = buffer.par_iterator_end();
137 OutputParams runparams(&buffer.params().encoding());
139 runparams.nice = true;
140 runparams.flavor = OutputParams::LATEX;
141 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
142 runparams.dryrun = true;
143 for (; it != end; ++it) {
144 LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
145 searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
148 if (to_utf8(searchString).empty()) {
149 buffer.message(_("Nothing to search"));
152 bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
153 docstring replaceString;
155 Buffer & repl_buffer = replace_work_area_->bufferView().buffer();
157 repl_buffer.write(oss);
158 replaceString = from_utf8(oss.str()); //buffer_to_latex(replace_buffer);
160 replaceString = from_utf8(LYX_FR_NULL_STRING);
162 LYXERR(Debug::FIND, "FindAndReplaceOptions: "
163 << "searchstring=" << searchString
164 << ", casesensitiv=" << casesensitive
165 << ", matchword=" << matchword
166 << ", backwards=" << backwards
167 << ", expandmacros=" << expandmacros
168 << ", ignoreformat=" << ignoreformat
169 << ", regexp=" << regexp
170 << ", replaceString" << replaceString
171 << ", keep_case=" << keep_case);
172 FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
173 expandmacros, ignoreformat, regexp, replaceString, keep_case);
174 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
175 std::ostringstream oss;
177 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
178 dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
182 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
184 if (! view_.currentMainWorkArea()) {
185 view_.message(_("No open document(s) in which to search"));
188 // FIXME: create a Dialog::returnFocus() or something instead of this:
189 view_.setCurrentWorkArea(view_.currentMainWorkArea());
190 findAndReplace(caseCB->isChecked(),
191 wordsCB->isChecked(),
193 expandMacrosCB->isChecked(),
194 ignoreFormatCB->isChecked(),
196 keepCaseCB->isChecked());
197 view_.currentMainWorkArea()->redraw();
201 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
203 static char const * regexps[] = {
204 ".*", ".+", "[a-z]+", "[0-9]+"
206 LYXERR(Debug::FIND, "Index: " << index);
207 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
208 find_work_area_->setFocus();
209 Cursor & cur = find_work_area_->bufferView().cursor();
210 if (! cur.inRegexped())
211 dispatch(FuncRequest(LFUN_REGEXP_MODE));
212 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
213 regexpInsertCombo->setCurrentIndex(0);
218 void FindAndReplaceWidget::on_closePB_clicked()
220 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
224 void FindAndReplaceWidget::on_findNextPB_clicked() {
225 findAndReplace(false, false);
226 find_work_area_->setFocus();
230 void FindAndReplaceWidget::on_findPrevPB_clicked() {
231 findAndReplace(true, false);
232 find_work_area_->setFocus();
236 void FindAndReplaceWidget::on_replaceNextPB_clicked()
238 findAndReplace(false, true);
239 replace_work_area_->setFocus();
243 void FindAndReplaceWidget::on_replacePrevPB_clicked()
245 findAndReplace(true, true);
246 replace_work_area_->setFocus();
250 void FindAndReplaceWidget::on_replaceallPB_clicked()
252 replace_work_area_->setFocus();
256 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
258 replace_work_area_->redraw();
259 find_work_area_->setFocus();
260 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
261 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
262 find_work_area_->redraw();
263 find_work_area_->installEventFilter(this);
264 replace_work_area_->installEventFilter(this);
268 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
270 replace_work_area_->removeEventFilter(this);
271 find_work_area_->removeEventFilter(this);
272 this->QWidget::hideEvent(ev);
276 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
278 find_work_area_->redraw();
279 replace_work_area_->redraw();
280 find_work_area_->setFocus();
281 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
282 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
287 FindAndReplace::FindAndReplace(GuiView & parent,
288 Qt::DockWidgetArea area, Qt::WindowFlags flags)
289 : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
291 widget_ = new FindAndReplaceWidget(parent);
293 setFocusProxy(widget_);
297 FindAndReplace::~FindAndReplace()
304 bool FindAndReplace::initialiseParams(std::string const & params)
306 return widget_->initialiseParams(params);
310 Dialog * createGuiSearchAdv(GuiView & lv)
312 return new FindAndReplace(lv, Qt::RightDockWidgetArea);
316 } // namespace frontend
320 #include "moc_FindAndReplace.cpp"