]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
7164aa25db6b20f1e0b6814f5701e54dd80470b8
[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 }
211
212
213 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
214 {
215         static char const * regexps[] = {
216                 ".*", ".+", "[a-z]+", "[0-9]+", ""
217         };
218         LYXERR(Debug::FIND, "Index: " << index);
219         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
220                 find_work_area_->setFocus();
221                 Cursor & cur = find_work_area_->bufferView().cursor();
222                 if (! cur.inRegexped())
223                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
224                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
225                 regexpInsertCombo->setCurrentIndex(0);
226         }
227 }
228
229
230 void FindAndReplaceWidget::on_closePB_clicked()
231 {
232         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
233 }
234
235
236 void FindAndReplaceWidget::on_findNextPB_clicked() {
237         findAndReplace(false, false);
238         find_work_area_->setFocus();
239 }
240
241
242 void FindAndReplaceWidget::on_findPrevPB_clicked() {
243         findAndReplace(true, false);
244         find_work_area_->setFocus();
245 }
246
247
248 void FindAndReplaceWidget::on_replaceNextPB_clicked()
249 {
250         findAndReplace(false, true);
251         replace_work_area_->setFocus();
252 }
253
254
255 void FindAndReplaceWidget::on_replacePrevPB_clicked()
256 {
257         findAndReplace(true, true);
258         replace_work_area_->setFocus();
259 }
260
261
262 void FindAndReplaceWidget::on_replaceallPB_clicked()
263 {
264         replace_work_area_->setFocus();
265 }
266
267
268 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
269 {
270         view_.setCurrentWorkArea(find_work_area_);
271         LYXERR(Debug::FIND, "Selecting entire find buffer");
272         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
273         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
274         find_work_area_->installEventFilter(this);
275         replace_work_area_->installEventFilter(this);
276 }
277
278
279 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
280 {
281         replace_work_area_->removeEventFilter(this);
282         find_work_area_->removeEventFilter(this);
283         this->QWidget::hideEvent(ev);
284 }
285
286
287 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
288 {
289         return true;
290 }
291
292
293 FindAndReplace::FindAndReplace(GuiView & parent,
294                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
295         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
296 {
297         widget_ = new FindAndReplaceWidget(parent);
298         setWidget(widget_);
299         setFocusProxy(widget_);
300 }
301
302
303 FindAndReplace::~FindAndReplace()
304 {
305         setFocusProxy(0);
306         delete widget_;
307 }
308
309
310 bool FindAndReplace::initialiseParams(std::string const & params)
311 {
312         return widget_->initialiseParams(params);
313 }
314
315
316 Dialog * createGuiSearchAdv(GuiView & lv)
317 {
318         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
319 }
320
321
322 } // namespace frontend
323 } // namespace lyx
324
325
326 #include "moc_FindAndReplace.cpp"