]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
a3c6f833ab4e31be8dfac9a3fc454c3988c0c89e
[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         FindAndReplaceOptions::SearchScope scope = FindAndReplaceOptions::S_BUFFER;
166         if (CurrentDocument->isChecked())
167                 scope = FindAndReplaceOptions::S_BUFFER;
168         else if (MasterDocument->isChecked())
169                 scope = FindAndReplaceOptions::S_DOCUMENT;
170         else if (OpenDocuments->isChecked())
171                 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
172         else
173                 LASSERT(false, /**/);
174         LYXERR(Debug::FIND, "FindAndReplaceOptions: "
175                << "searchstring=" << searchString
176                << ", casesensitiv=" << casesensitive
177                << ", matchword=" << matchword
178                << ", backwards=" << backwards
179                << ", expandmacros=" << expandmacros
180                << ", ignoreformat=" << ignoreformat
181                << ", regexp=" << regexp
182                << ", replaceString" << replaceString
183                << ", keep_case=" << keep_case
184                << ", scope=" << scope);
185         FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
186                 expandmacros, ignoreformat, regexp, replaceString, keep_case, scope);
187         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
188         std::ostringstream oss;
189         oss << opt;
190         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
191         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
192 }
193
194
195 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
196 {
197         if (! view_.currentMainWorkArea()) {
198                 view_.message(_("No open document(s) in which to search"));
199                 return;
200         }
201         // FIXME: create a Dialog::returnFocus() or something instead of this:
202         view_.setCurrentWorkArea(view_.currentMainWorkArea());
203         findAndReplace(caseCB->isChecked(),
204                 wordsCB->isChecked(),
205                 backwards,
206                 expandMacrosCB->isChecked(),
207                 ignoreFormatCB->isChecked(),
208                 replace,
209                 keepCaseCB->isChecked());
210         view_.currentMainWorkArea()->redraw();
211 }
212
213
214 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
215 {
216         static char const * regexps[] = {
217                 ".*", ".+", "[a-z]+", "[0-9]+", ""
218         };
219         LYXERR(Debug::FIND, "Index: " << index);
220         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
221                 find_work_area_->setFocus();
222                 Cursor & cur = find_work_area_->bufferView().cursor();
223                 if (! cur.inRegexped())
224                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
225                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
226                 regexpInsertCombo->setCurrentIndex(0);
227         }
228 }
229
230
231 void FindAndReplaceWidget::on_closePB_clicked()
232 {
233         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
234 }
235
236
237 void FindAndReplaceWidget::on_findNextPB_clicked() {
238         findAndReplace(false, false);
239         find_work_area_->setFocus();
240 }
241
242
243 void FindAndReplaceWidget::on_findPrevPB_clicked() {
244         findAndReplace(true, false);
245         find_work_area_->setFocus();
246 }
247
248
249 void FindAndReplaceWidget::on_replaceNextPB_clicked()
250 {
251         findAndReplace(false, true);
252         replace_work_area_->setFocus();
253 }
254
255
256 void FindAndReplaceWidget::on_replacePrevPB_clicked()
257 {
258         findAndReplace(true, true);
259         replace_work_area_->setFocus();
260 }
261
262
263 void FindAndReplaceWidget::on_replaceallPB_clicked()
264 {
265         replace_work_area_->setFocus();
266 }
267
268
269 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
270 {
271         view_.currentMainWorkArea()->redraw();
272         replace_work_area_->setFocus();
273         find_work_area_->setFocus();
274         view_.setCurrentWorkArea(find_work_area_);
275         LYXERR(Debug::FIND, "Selecting entire find buffer");
276         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
277         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
278         find_work_area_->redraw();
279         find_work_area_->installEventFilter(this);
280         replace_work_area_->installEventFilter(this);
281 }
282
283
284 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
285 {
286         replace_work_area_->removeEventFilter(this);
287         find_work_area_->removeEventFilter(this);
288         this->QWidget::hideEvent(ev);
289 }
290
291
292 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
293 {
294         return true;
295 }
296
297
298 FindAndReplace::FindAndReplace(GuiView & parent,
299                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
300         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
301 {
302         widget_ = new FindAndReplaceWidget(parent);
303         setWidget(widget_);
304         setFocusProxy(widget_);
305 }
306
307
308 FindAndReplace::~FindAndReplace()
309 {
310         setFocusProxy(0);
311         delete widget_;
312 }
313
314
315 bool FindAndReplace::initialiseParams(std::string const & params)
316 {
317         return widget_->initialiseParams(params);
318 }
319
320
321 Dialog * createGuiSearchAdv(GuiView & lv)
322 {
323         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
324 }
325
326
327 } // namespace frontend
328 } // namespace lyx
329
330
331 #include "moc_FindAndReplace.cpp"