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);
83 static docstring buffer_to_latex(Buffer & buffer) {
84 OutputParams runparams(&buffer.params().encoding());
86 runparams.nice = true;
87 runparams.flavor = OutputParams::LATEX;
88 runparams.linelen = 80; //lyxrc.plaintext_linelen;
89 // No side effect of file copying and image conversion
90 runparams.dryrun = true;
91 buffer.texrow().reset();
92 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
93 ParagraphList::const_iterator const end = buffer.paragraphs().end();
94 for (; pit != end; ++pit) {
95 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
96 LYXERR(Debug::DEBUG, "searchString up to here: " << os.str());
101 void FindAndReplaceWidget::findAndReplace(
102 bool casesensitive, bool matchword, bool backwards,
103 bool expandmacros, bool ignoreformat, bool replace)
105 Buffer & buffer = find_work_area_->bufferView().buffer();
106 docstring searchString;
108 searchString = buffer_to_latex(buffer);
110 ParIterator it = buffer.par_iterator_begin();
111 ParIterator end = buffer.par_iterator_end();
112 OutputParams runparams(&buffer.params().encoding());
114 runparams.nice = true;
115 runparams.flavor = OutputParams::LATEX;
116 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
117 runparams.dryrun = true;
118 for (; it != end; ++it) {
119 LYXERR(Debug::DEBUG, "Adding to search string: '" << it->asString(false) << "'");
120 searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
123 // lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
124 if (to_utf8(searchString).empty()) {
125 buffer.message(_("Nothing to search"));
128 bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
129 docstring replaceString;
131 Buffer & replace_buffer = replace_work_area_->bufferView().buffer();
132 replaceString = buffer_to_latex(replace_buffer);
134 replaceString = from_utf8(LYX_FR_NULL_STRING);
136 FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
137 expandmacros, ignoreformat, regexp, replaceString);
138 LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
139 std::ostringstream oss;
141 LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
142 dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
144 // findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
145 // searchString, len, casesensitive, matchword, ! backwards, expandmacros);
149 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
151 // FIXME: create a Dialog::returnFocus() or something instead of this:
152 view_.setCurrentWorkArea(view_.currentMainWorkArea());
153 // FIXME: This should be an LFUN.
154 findAndReplace(caseCB->isChecked(),
155 wordsCB->isChecked(),
157 expandMacrosCB->isChecked(),
158 ignoreFormatCB->isChecked(),
160 view_.currentMainWorkArea()->redraw();
161 find_work_area_->setFocus();
165 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
167 static char const * regexps[] = {
168 ".*", ".+", "[a-z]+", "[0-9]+"
170 //lyxerr << "Index: " << index << std::endl;
171 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
172 find_work_area_->setFocus();
173 Cursor & cur = find_work_area_->bufferView().cursor();
174 if (! cur.inRegexped())
175 dispatch(FuncRequest(LFUN_REGEXP_MODE));
176 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
177 regexpInsertCombo->setCurrentIndex(0);
182 void FindAndReplaceWidget::on_closePB_clicked()
184 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
188 void FindAndReplaceWidget::on_findNextPB_clicked() {
189 findAndReplace(false, false);
193 void FindAndReplaceWidget::on_findPrevPB_clicked() {
194 findAndReplace(true, false);
198 void FindAndReplaceWidget::on_replaceNextPB_clicked()
200 findAndReplace(false, true);
204 void FindAndReplaceWidget::on_replacePrevPB_clicked()
206 findAndReplace(true, true);
210 void FindAndReplaceWidget::on_replaceallPB_clicked()
215 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
217 replace_work_area_->setEnabled(true);
218 replace_work_area_->redraw();
219 find_work_area_->setFocus();
220 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
221 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
222 find_work_area_->redraw();
223 find_work_area_->installEventFilter(this);
227 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
229 find_work_area_->removeEventFilter(this);
230 this->QWidget::hideEvent(ev);
234 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
236 find_work_area_->redraw();
237 replace_work_area_->setEnabled(true);
238 replace_work_area_->redraw();
239 find_work_area_->setFocus();
240 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
241 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
246 FindAndReplace::FindAndReplace(GuiView & parent,
247 Qt::DockWidgetArea area, Qt::WindowFlags flags)
248 : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
250 widget_ = new FindAndReplaceWidget(parent);
252 setFocusProxy(widget_);
256 FindAndReplace::~FindAndReplace()
263 bool FindAndReplace::initialiseParams(std::string const & params)
265 return widget_->initialiseParams(params);
269 Dialog * createGuiSearchAdv(GuiView & lv)
271 return new FindAndReplace(lv, Qt::RightDockWidgetArea);
275 } // namespace frontend
279 #include "moc_FindAndReplace.cpp"