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::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 QWidget::eventFilter(obj, event);
82 static docstring buffer_to_latex(Buffer & buffer) {
83 OutputParams runparams(&buffer.params().encoding());
85 runparams.nice = true;
86 runparams.flavor = OutputParams::LATEX;
87 runparams.linelen = 80; //lyxrc.plaintext_linelen;
88 // No side effect of file copying and image conversion
89 runparams.dryrun = true;
90 buffer.texrow().reset();
91 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
92 ParagraphList::const_iterator const end = buffer.paragraphs().end();
93 for (; pit != end; ++pit) {
94 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
95 LYXERR(Debug::DEBUG, "searchString up to here: " << os.str());
100 void FindAndReplaceWidget::findAndReplace(
101 bool casesensitive, bool matchword, bool backwards,
102 bool expandmacros, bool ignoreformat, bool replace)
104 Buffer & buffer = find_work_area_->bufferView().buffer();
105 docstring searchString;
107 searchString = buffer_to_latex(buffer);
109 ParIterator it = buffer.par_iterator_begin();
110 ParIterator end = buffer.par_iterator_end();
111 OutputParams runparams(&buffer.params().encoding());
113 runparams.nice = true;
114 runparams.flavor = OutputParams::LATEX;
115 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
116 runparams.dryrun = true;
117 for (; it != end; ++it) {
118 LYXERR(Debug::DEBUG, "Adding to search string: '" << it->asString(false) << "'");
119 searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
122 // lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
123 if (to_utf8(searchString).empty()) {
124 buffer.message(_("Nothing to search"));
127 bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
128 docstring replaceString;
130 Buffer & replace_buffer = replace_work_area_->bufferView().buffer();
131 replaceString = buffer_to_latex(replace_buffer);
133 replaceString = from_utf8(LYX_FR_NULL_STRING);
135 FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
136 expandmacros, ignoreformat, regexp, replaceString);
137 LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
138 std::ostringstream oss;
140 LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
141 dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
143 // findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
144 // searchString, len, casesensitive, matchword, ! backwards, expandmacros);
148 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
150 // FIXME: create a Dialog::returnFocus() or something instead of this:
151 view_.setCurrentWorkArea(view_.currentMainWorkArea());
152 // FIXME: This should be an LFUN.
153 findAndReplace(caseCB->isChecked(),
154 wordsCB->isChecked(),
156 expandMacrosCB->isChecked(),
157 ignoreFormatCB->isChecked(),
159 view_.currentMainWorkArea()->redraw();
160 find_work_area_->setFocus();
164 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
166 static char const * regexps[] = {
167 ".*", ".+", "[a-z]+", "[0-9]+"
169 //lyxerr << "Index: " << index << std::endl;
170 if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
171 find_work_area_->setFocus();
172 Cursor & cur = find_work_area_->bufferView().cursor();
173 if (! cur.inRegexped())
174 dispatch(FuncRequest(LFUN_REGEXP_MODE));
175 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
176 regexpInsertCombo->setCurrentIndex(0);
181 void FindAndReplaceWidget::on_closePB_clicked()
183 dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
187 void FindAndReplaceWidget::on_findNextPB_clicked() {
188 findAndReplace(false, false);
192 void FindAndReplaceWidget::on_findPrevPB_clicked() {
193 findAndReplace(true, false);
197 void FindAndReplaceWidget::on_replaceNextPB_clicked()
199 findAndReplace(false, true);
203 void FindAndReplaceWidget::on_replacePrevPB_clicked()
205 findAndReplace(true, true);
209 void FindAndReplaceWidget::on_replaceallPB_clicked()
214 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
216 replace_work_area_->setEnabled(true);
217 replace_work_area_->redraw();
218 find_work_area_->setFocus();
219 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
220 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
221 find_work_area_->redraw();
222 find_work_area_->installEventFilter(this);
226 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
228 find_work_area_->removeEventFilter(this);
229 this->QWidget::hideEvent(ev);
233 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
235 find_work_area_->redraw();
236 replace_work_area_->setEnabled(true);
237 replace_work_area_->redraw();
238 find_work_area_->setFocus();
239 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
240 dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
245 FindAndReplace::FindAndReplace(GuiView & parent,
246 Qt::DockWidgetArea area, Qt::WindowFlags flags)
247 : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
249 widget_ = new FindAndReplaceWidget(parent);
251 setFocusProxy(widget_);
255 FindAndReplace::~FindAndReplace()
262 bool FindAndReplace::initialiseParams(std::string const & params)
264 return widget_->initialiseParams(params);
268 Dialog * createGuiSearchAdv(GuiView & lv)
270 return new FindAndReplace(lv, Qt::RightDockWidgetArea);
274 } // namespace frontend
278 #include "moc_FindAndReplace.cpp"