]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
Fix bug #6144: Remover RegExp from them Menu and give the user the possibility to...
[lyx.git] / src / frontends / qt4 / FindAndReplace.cpp
1 /**
2  * \file FindAndReplace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Tommaso Cucinotta
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "FindAndReplace.h"
14
15 #include "GuiApplication.h"
16 #include "qt_helpers.h"
17 #include "GuiView.h"
18 #include "GuiWorkArea.h"
19
20 #include "buffer_funcs.h"
21 #include "BufferParams.h"
22 #include "Cursor.h"
23 #include "FuncRequest.h"
24 #include "lyxfind.h"
25 #include "OutputParams.h"
26 #include "output_latex.h"
27 #include "TexRow.h"
28
29 #include "support/debug.h"
30 #include "support/FileName.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
33
34 #include <QCloseEvent>
35 #include <QLineEdit>
36
37 #include <iostream>
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43 namespace frontend {
44
45
46 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
47         :       view_(view)
48 {
49         setupUi(this);
50 #if QT_VERSION < 0x040400
51         scrollArea->setWidget(scrollAreaWidgetContents);
52 #endif
53         find_work_area_->setGuiView(view_);
54         find_work_area_->init();
55         setFocusProxy(find_work_area_);
56         replace_work_area_->setGuiView(view_);
57         replace_work_area_->init();
58         // We don't want two cursors blinking.
59         replace_work_area_->stopBlinkingCursor();
60 }
61
62
63 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
64 {
65         LYXERR(Debug::FIND, "FindAndReplace::eventFilter(): obj=" << obj
66                << ", fwa=" << find_work_area_ << ", rwa=" << replace_work_area_
67                << "fsa=" << find_scroll_area_ << ", rsa=" << replace_scroll_area_);
68         if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
69                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
70                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
71                         on_closePB_clicked();
72                         return true;
73                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
74                         if (e->modifiers() == Qt::ShiftModifier) {
75                                 on_findPrevPB_clicked();
76                                 return true;
77                         } else if (e->modifiers() == Qt::NoModifier) {
78                                 on_findNextPB_clicked();
79                                 return true;
80                         }
81                 } else if (e->key() == Qt::Key_Tab && e->modifiers() == Qt::NoModifier) {
82                         LYXERR(Debug::FIND, "Focusing replace WA");
83                         replace_work_area_->setFocus();
84                         return true;
85                 }
86         }
87         if (obj == replace_work_area_ && event->type() == QEvent::KeyPress) {
88                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
89                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
90                         on_closePB_clicked();
91                         return true;
92                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
93                         if (e->modifiers() == Qt::ShiftModifier) {
94                                 on_replacePrevPB_clicked();
95                                 return true;
96                         } else if (e->modifiers() == Qt::NoModifier) {
97                                 on_replaceNextPB_clicked();
98                                 return true;
99                         }
100                 } else if (e->key() == Qt::Key_Backtab) {
101                         LYXERR(Debug::FIND, "Focusing find WA");
102                         find_work_area_->setFocus();
103                         return true;
104                 }
105         }
106         // standard event processing
107         return QWidget::eventFilter(obj, event);
108 }
109
110 static docstring buffer_to_latex(Buffer & buffer) {
111         OutputParams runparams(&buffer.params().encoding());
112         odocstringstream os;
113         runparams.nice = true;
114         runparams.flavor = OutputParams::LATEX;
115         runparams.linelen = 80; //lyxrc.plaintext_linelen;
116         // No side effect of file copying and image conversion
117         runparams.dryrun = true;
118         buffer.texrow().reset();
119         ParagraphList::const_iterator pit = buffer.paragraphs().begin();
120         ParagraphList::const_iterator const end = buffer.paragraphs().end();
121         for (; pit != end; ++pit) {
122                 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
123                 LYXERR(Debug::FIND, "searchString up to here: " << os.str());
124         }
125         return os.str();
126 }
127
128 void FindAndReplaceWidget::findAndReplace(
129         bool casesensitive, bool matchword, bool backwards,
130         bool expandmacros, bool ignoreformat, bool replace,
131         bool keep_case)
132 {
133         Buffer & buffer = find_work_area_->bufferView().buffer();
134         docstring searchString;
135         if (!ignoreformat) {
136                 searchString = buffer_to_latex(buffer);
137         } else {
138                 ParIterator it = buffer.par_iterator_begin();
139                 ParIterator end = buffer.par_iterator_end();
140                 OutputParams runparams(&buffer.params().encoding());
141                 odocstringstream os;
142                 runparams.nice = true;
143                 runparams.flavor = OutputParams::LATEX;
144                 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
145                 runparams.dryrun = true;
146                 for (; it != end; ++it) {
147                         LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
148                         searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
149                 }
150         }
151         if (to_utf8(searchString).empty()) {
152                 buffer.message(_("Nothing to search"));
153                 return;
154         }
155         bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
156         docstring replaceString;
157         if (replace) {
158                 Buffer & repl_buffer = replace_work_area_->bufferView().buffer();
159                 ostringstream oss;
160                 repl_buffer.write(oss);
161                 replaceString = from_utf8(oss.str()); //buffer_to_latex(replace_buffer);
162         } else {
163                 replaceString = from_utf8(LYX_FR_NULL_STRING);
164         }
165         LYXERR(Debug::FIND, "FindAndReplaceOptions: "
166                << "searchstring=" << searchString
167                << ", casesensitiv=" << casesensitive
168                << ", matchword=" << matchword
169                << ", backwards=" << backwards
170                << ", expandmacros=" << expandmacros
171                << ", ignoreformat=" << ignoreformat
172                << ", regexp=" << regexp
173                << ", replaceString" << replaceString
174                << ", keep_case=" << keep_case);
175         FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
176                   expandmacros, ignoreformat, regexp, replaceString, keep_case);
177         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
178         std::ostringstream oss;
179         oss << opt;
180         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
181         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
182 }
183
184
185 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
186 {
187         if (! view_.currentMainWorkArea()) {
188                 view_.message(_("No open document(s) in which to search"));
189                 return;
190         }
191         // FIXME: create a Dialog::returnFocus() or something instead of this:
192         view_.setCurrentWorkArea(view_.currentMainWorkArea());
193         findAndReplace(caseCB->isChecked(),
194                 wordsCB->isChecked(),
195                 backwards,
196                 expandMacrosCB->isChecked(),
197                 ignoreFormatCB->isChecked(),
198                 replace,
199                 keepCaseCB->isChecked());
200         view_.currentMainWorkArea()->redraw();
201 }
202
203
204 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
205 {
206         static char const * regexps[] = {
207                 ".*", ".+", "[a-z]+", "[0-9]+", ""
208         };
209         LYXERR(Debug::FIND, "Index: " << index);
210         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
211                 find_work_area_->setFocus();
212                 Cursor & cur = find_work_area_->bufferView().cursor();
213                 if (! cur.inRegexped())
214                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
215                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
216                 regexpInsertCombo->setCurrentIndex(0);
217         }
218 }
219
220
221 void FindAndReplaceWidget::on_closePB_clicked()
222 {
223         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
224 }
225
226
227 void FindAndReplaceWidget::on_findNextPB_clicked() {
228         findAndReplace(false, false);
229         find_work_area_->setFocus();
230 }
231
232
233 void FindAndReplaceWidget::on_findPrevPB_clicked() {
234         findAndReplace(true, false);
235         find_work_area_->setFocus();
236 }
237
238
239 void FindAndReplaceWidget::on_replaceNextPB_clicked()
240 {
241         findAndReplace(false, true);
242         replace_work_area_->setFocus();
243 }
244
245
246 void FindAndReplaceWidget::on_replacePrevPB_clicked()
247 {
248         findAndReplace(true, true);
249         replace_work_area_->setFocus();
250 }
251
252
253 void FindAndReplaceWidget::on_replaceallPB_clicked()
254 {
255         replace_work_area_->setFocus();
256 }
257
258
259 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
260 {
261         replace_work_area_->redraw();
262         find_work_area_->setFocus();
263         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
264         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
265         find_work_area_->redraw();
266         find_work_area_->installEventFilter(this);
267         replace_work_area_->installEventFilter(this);
268 }
269
270
271 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
272 {
273         replace_work_area_->removeEventFilter(this);
274         find_work_area_->removeEventFilter(this);
275         this->QWidget::hideEvent(ev);
276 }
277
278
279 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
280 {
281         find_work_area_->redraw();
282         replace_work_area_->redraw();
283         find_work_area_->setFocus();
284         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
285         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
286         return true;
287 }
288
289
290 FindAndReplace::FindAndReplace(GuiView & parent,
291                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
292         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
293 {
294         widget_ = new FindAndReplaceWidget(parent);
295         setWidget(widget_);
296         setFocusProxy(widget_);
297 }
298
299
300 FindAndReplace::~FindAndReplace()
301 {
302         setFocusProxy(0);
303         delete widget_;
304 }
305
306
307 bool FindAndReplace::initialiseParams(std::string const & params)
308 {
309         return widget_->initialiseParams(params);
310 }
311
312
313 Dialog * createGuiSearchAdv(GuiView & lv)
314 {
315         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
316 }
317
318
319 } // namespace frontend
320 } // namespace lyx
321
322
323 #include "moc_FindAndReplace.cpp"