]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
64d38f60c33bd349359dfdf62b4ff468ddc09e38
[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         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();
57 }
58
59
60 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
61 {
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) {
68                         on_closePB_clicked();
69                         return true;
70                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
71                         if (e->modifiers() == Qt::ShiftModifier) {
72                                 on_findPrevPB_clicked();
73                                 return true;
74                         } else if (e->modifiers() == Qt::NoModifier) {
75                                 on_findNextPB_clicked();
76                                 return true;
77                         }
78                 } else if (e->key() == Qt::Key_Tab && e->modifiers() == Qt::NoModifier) {
79                         LYXERR(Debug::FIND, "Focusing replace WA");
80                         replace_work_area_->setFocus();
81                         return true;
82                 }
83         }
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) {
87                         on_closePB_clicked();
88                         return true;
89                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
90                         if (e->modifiers() == Qt::ShiftModifier) {
91                                 on_replacePrevPB_clicked();
92                                 return true;
93                         } else if (e->modifiers() == Qt::NoModifier) {
94                                 on_replaceNextPB_clicked();
95                                 return true;
96                         }
97                 } else if (e->key() == Qt::Key_Backtab) {
98                         LYXERR(Debug::FIND, "Focusing find WA");
99                         find_work_area_->setFocus();
100                         return true;
101                 }
102         }
103         // standard event processing
104         return QWidget::eventFilter(obj, event);
105 }
106
107 static docstring buffer_to_latex(Buffer & buffer) {
108         OutputParams runparams(&buffer.params().encoding());
109         odocstringstream os;
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());
121         }
122         return os.str();
123 }
124
125 void FindAndReplaceWidget::findAndReplace(
126         bool casesensitive, bool matchword, bool backwards,
127         bool expandmacros, bool ignoreformat, bool replace,
128         bool keep_case)
129 {
130         Buffer & buffer = find_work_area_->bufferView().buffer();
131         docstring searchString;
132         if (!ignoreformat) {
133                 searchString = buffer_to_latex(buffer);
134         } else {
135                 ParIterator it = buffer.par_iterator_begin();
136                 ParIterator end = buffer.par_iterator_end();
137                 OutputParams runparams(&buffer.params().encoding());
138                 odocstringstream os;
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);
146                 }
147         }
148         if (to_utf8(searchString).empty()) {
149                 buffer.message(_("Nothing to search"));
150                 return;
151         }
152         bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
153         docstring replaceString;
154         if (replace) {
155                 Buffer & repl_buffer = replace_work_area_->bufferView().buffer();
156                 ostringstream oss;
157                 repl_buffer.write(oss);
158                 replaceString = from_utf8(oss.str()); //buffer_to_latex(replace_buffer);
159         } else {
160                 replaceString = from_utf8(LYX_FR_NULL_STRING);
161         }
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;
176         oss << opt;
177         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
178         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
179 }
180
181
182 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
183 {
184         if (! view_.currentMainWorkArea()) {
185                 view_.message(_("No open document(s) in which to search"));
186                 return;
187         }
188         // FIXME: create a Dialog::returnFocus() or something instead of this:
189         view_.setCurrentWorkArea(view_.currentMainWorkArea());
190         findAndReplace(caseCB->isChecked(),
191                 wordsCB->isChecked(),
192                 backwards,
193                 expandMacrosCB->isChecked(),
194                 ignoreFormatCB->isChecked(),
195                 replace,
196                 keepCaseCB->isChecked());
197         view_.currentMainWorkArea()->redraw();
198 }
199
200
201 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
202 {
203         static char const * regexps[] = {
204                 ".*", ".+", "[a-z]+", "[0-9]+"
205         };
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);
214         }
215 }
216
217
218 void FindAndReplaceWidget::on_closePB_clicked()
219 {
220         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
221 }
222
223
224 void FindAndReplaceWidget::on_findNextPB_clicked() {
225         findAndReplace(false, false);
226         find_work_area_->setFocus();
227 }
228
229
230 void FindAndReplaceWidget::on_findPrevPB_clicked() {
231         findAndReplace(true, false);
232         find_work_area_->setFocus();
233 }
234
235
236 void FindAndReplaceWidget::on_replaceNextPB_clicked()
237 {
238         findAndReplace(false, true);
239         replace_work_area_->setFocus();
240 }
241
242
243 void FindAndReplaceWidget::on_replacePrevPB_clicked()
244 {
245         findAndReplace(true, true);
246         replace_work_area_->setFocus();
247 }
248
249
250 void FindAndReplaceWidget::on_replaceallPB_clicked()
251 {
252         replace_work_area_->setFocus();
253 }
254
255
256 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
257 {
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);
265 }
266
267
268 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
269 {
270         replace_work_area_->removeEventFilter(this);
271         find_work_area_->removeEventFilter(this);
272         this->QWidget::hideEvent(ev);
273 }
274
275
276 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
277 {
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));
283         return true;
284 }
285
286
287 FindAndReplace::FindAndReplace(GuiView & parent,
288                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
289         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
290 {
291         widget_ = new FindAndReplaceWidget(parent);
292         setWidget(widget_);
293         setFocusProxy(widget_);
294 }
295
296
297 FindAndReplace::~FindAndReplace()
298 {
299         setFocusProxy(0);
300         delete widget_;
301 }
302
303
304 bool FindAndReplace::initialiseParams(std::string const & params)
305 {
306         return widget_->initialiseParams(params);
307 }
308
309
310 Dialog * createGuiSearchAdv(GuiView & lv)
311 {
312         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
313 }
314
315
316 } // namespace frontend
317 } // namespace lyx
318
319
320 #include "moc_FindAndReplace.cpp"