]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
Remove unused UI file.
[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 "BufferList.h"
23 #include "Cursor.h"
24 #include "FuncRequest.h"
25 #include "lyxfind.h"
26 #include "OutputParams.h"
27 #include "output_latex.h"
28 #include "TexRow.h"
29 #include "alert.h"
30
31 #include "support/debug.h"
32 #include "support/FileName.h"
33 #include "support/gettext.h"
34 #include "support/lassert.h"
35
36 #include <QCloseEvent>
37 #include <QLineEdit>
38
39 #include <iostream>
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45 namespace frontend {
46
47
48 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
49         :       view_(view)
50 {
51         setupUi(this);
52 #if QT_VERSION < 0x040400
53         scrollArea->setWidget(scrollAreaWidgetContents);
54 #endif
55         find_work_area_->setGuiView(view_);
56         find_work_area_->init();
57         setFocusProxy(find_work_area_);
58         replace_work_area_->setGuiView(view_);
59         replace_work_area_->init();
60         // We don't want two cursors blinking.
61         replace_work_area_->stopBlinkingCursor();
62 }
63
64
65 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
66 {
67         if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
68                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
69                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
70                         on_closePB_clicked();
71                         return true;
72                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
73                         if (e->modifiers() == Qt::ShiftModifier) {
74                                 on_findPrevPB_clicked();
75                                 return true;
76                         } else if (e->modifiers() == Qt::NoModifier) {
77                                 on_findNextPB_clicked();
78                                 return true;
79                         }
80                 } else if (e->key() == Qt::Key_Tab && e->modifiers() == Qt::NoModifier) {
81                         LYXERR(Debug::FIND, "Focusing replace WA");
82                         replace_work_area_->setFocus();
83                         return true;
84                 }
85         }
86         if (obj == replace_work_area_ && event->type() == QEvent::KeyPress) {
87                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
88                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
89                         on_closePB_clicked();
90                         return true;
91                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
92                         if (e->modifiers() == Qt::ShiftModifier) {
93                                 on_replacePrevPB_clicked();
94                                 return true;
95                         } else if (e->modifiers() == Qt::NoModifier) {
96                                 on_replaceNextPB_clicked();
97                                 return true;
98                         }
99                 } else if (e->key() == Qt::Key_Backtab) {
100                         LYXERR(Debug::FIND, "Focusing find WA");
101                         find_work_area_->setFocus();
102                         return true;
103                 }
104         }
105         // standard event processing
106         return QWidget::eventFilter(obj, event);
107 }
108
109 static docstring buffer_to_latex(Buffer & buffer) {
110         OutputParams runparams(&buffer.params().encoding());
111         odocstringstream os;
112         runparams.nice = true;
113         runparams.flavor = OutputParams::LATEX;
114         runparams.linelen = 80; //lyxrc.plaintext_linelen;
115         // No side effect of file copying and image conversion
116         runparams.dryrun = true;
117         buffer.texrow().reset();
118         ParagraphList::const_iterator pit = buffer.paragraphs().begin();
119         ParagraphList::const_iterator const end = buffer.paragraphs().end();
120         for (; pit != end; ++pit) {
121                 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
122                 LYXERR(Debug::FIND, "searchString up to here: " << os.str());
123         }
124         return os.str();
125 }
126
127
128 /** Switch p_buf to point to next document buffer.
129  **
130  ** Return true if restarted from master-document buffer.
131  **
132  ** @note
133  ** Not using p_buf->allRelatives() here, because I'm not sure
134  ** whether or not the returned order is independent of p_buf.
135  **/
136 static bool next_document_buffer(Buffer * & p_buf) {
137         Buffer *p_master = p_buf;
138         Buffer *p_old;
139         do {
140                 p_old = p_master;
141                 p_master = const_cast<Buffer *>(p_master->masterBuffer());
142                 LYXERR(Debug::FIND, "p_old=" << p_old << ", p_master=" << p_master);
143         } while (p_master != p_old);
144         LASSERT(p_master != NULL, /**/);
145         vector<Buffer *> v_children;
146         /* Root master added as first buffer in the vector */
147         v_children.push_back(p_master);
148         p_master->getChildren(v_children, true);
149         LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
150         vector<Buffer *>::const_iterator it = find(v_children.begin(), v_children.end(), p_buf);
151         LASSERT(it != v_children.end(), /**/)
152         ++it;
153         if (it == v_children.end()) {
154                 p_buf = *v_children.begin();
155                 return true;
156         }
157         p_buf = *it;
158         return false;
159 }
160
161
162 /** Switch p_buf to point to previous document buffer.
163  **
164  ** Return true if restarted from last child buffer.
165  **
166  ** @note
167  ** Not using p_buf->allRelatives() here, because I'm not sure
168  ** whether or not the returned order is independent of p_buf.
169  **/
170 static bool prev_document_buffer(Buffer * & p_buf) {
171         Buffer *p_master = p_buf;
172         Buffer *p_old;
173         do {
174                 p_old = p_master;
175                 p_master = const_cast<Buffer *>(p_master->masterBuffer());
176                 LYXERR(Debug::FIND, "p_old=" << p_old << ", p_master=" << p_master);
177         } while (p_master != p_old);
178         LASSERT(p_master != NULL, /**/);
179         vector<Buffer *> v_children;
180         /* Root master added as first buffer in the vector */
181         v_children.push_back(p_master);
182         p_master->getChildren(v_children, true);
183         LYXERR(Debug::FIND, "v_children.size()=" << v_children.size());
184         vector<Buffer *>::const_iterator it = find(v_children.begin(), v_children.end(), p_buf);
185         LASSERT(it != v_children.end(), /**/)
186         if (it == v_children.begin()) {
187                 it = v_children.end();
188                 --it;
189                 p_buf = *it;
190                 return true;
191         }
192         --it;
193         p_buf = *it;
194         return false;
195 }
196
197
198 /** Switch buf to point to next or previous buffer in search scope.
199  **
200  ** Return true if restarted from scratch.
201  **/
202 static bool next_prev_buffer(Buffer * & buf, FindAndReplaceOptions const & opt) {
203         bool restarted = false;
204         switch (opt.scope) {
205         case FindAndReplaceOptions::S_BUFFER:
206                 restarted = true;
207                 break;
208         case FindAndReplaceOptions::S_DOCUMENT:
209                 if (opt.forward)
210                         restarted = next_document_buffer(buf);
211                 else
212                         restarted = prev_document_buffer(buf);
213                 break;
214         case FindAndReplaceOptions::S_OPEN_BUFFERS:
215                 if (opt.forward) {
216                         buf = theBufferList().next(buf);
217                         restarted = buf == *theBufferList().begin();
218                 } else {
219                         buf = theBufferList().previous(buf);
220                         restarted = buf == *(theBufferList().end() - 1);
221                 }
222                 break;
223         }
224         return restarted;
225 }
226
227
228 /** Find the finest question message to post to the user */
229 docstring question_string(FindAndReplaceOptions const & opt)
230 {
231         docstring cur_pos = opt.forward ? _("End") : _("Begin");
232         docstring new_pos = opt.forward ? _("begin") : _("end");
233         docstring scope;
234         switch (opt.scope) {
235         case FindAndReplaceOptions::S_BUFFER:
236                 scope = _("file");
237                 break;
238         case FindAndReplaceOptions::S_DOCUMENT:
239                 scope = _("master document");
240                 break;
241         case FindAndReplaceOptions::S_OPEN_BUFFERS:
242                 scope = _("open files");
243                 break;
244         }
245         docstring dir = opt.forward ? _("forward") : _("backwards");
246         return cur_pos + _(" of ") + scope
247                 + _(" reached while searching ") + dir + ".\n"
248                 + "\n"
249                 + _("Continue searching from ") + new_pos + " ?";
250 }
251
252
253 void FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt) {
254         int wrap_answer = -1;
255         ostringstream oss;
256         oss << opt;
257         FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));
258         BufferView * bv = view_.documentBufferView();
259         Buffer * buf = &bv->buffer();
260
261         Buffer * buf_orig = &bv->buffer();
262         Cursor cur_orig(bv->cursor());
263
264         do {
265                 LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
266                 dispatch(cmd);
267                 if (bv->cursor().result().dispatched()) {
268                         // Match found, selected and replaced if needed
269                         return;
270                 }
271
272                 // No match found in current buffer: select next buffer in scope, if any
273                 bool prompt = next_prev_buffer(buf, opt);
274                 if (prompt) {
275                         if (wrap_answer != -1)
276                                 break;
277                         docstring q = question_string(opt);
278                         wrap_answer = frontend::Alert::prompt(
279                                 _("Wrap search?"), q,
280                                 0, 1, _("&Yes"), _("&No"));
281                         if (wrap_answer == 1)
282                                 break;
283                 }
284                 lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
285                                           buf->absFileName()));
286                 bv = view_.documentBufferView();
287                 if (opt.forward) {
288                         bv->cursor().clear();
289                         bv->cursor().push_back(CursorSlice(buf->inset()));
290                 } else {
291                         lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
292                         bv->cursor().setCursor(doc_iterator_end(buf));
293                         bv->cursor().backwardPos();
294                         LYXERR(Debug::FIND, "findBackAdv5: cur: " << bv->cursor());
295                 }
296                 bv->clearSelection();
297         } while (wrap_answer != 1);
298         lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
299                                   buf_orig->absFileName()));
300         bv = view_.documentBufferView();
301         bv->cursor() = cur_orig;
302 }
303
304
305 void FindAndReplaceWidget::findAndReplace(
306         bool casesensitive, bool matchword, bool backwards,
307         bool expandmacros, bool ignoreformat, bool replace,
308         bool keep_case)
309 {
310         Buffer & buffer = find_work_area_->bufferView().buffer();
311         docstring searchString;
312         if (!ignoreformat) {
313                 searchString = buffer_to_latex(buffer);
314         } else {
315                 ParIterator it = buffer.par_iterator_begin();
316                 ParIterator end = buffer.par_iterator_end();
317                 OutputParams runparams(&buffer.params().encoding());
318                 odocstringstream os;
319                 runparams.nice = true;
320                 runparams.flavor = OutputParams::LATEX;
321                 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
322                 runparams.dryrun = true;
323                 for (; it != end; ++it) {
324                         LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
325                         searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
326                 }
327         }
328         if (to_utf8(searchString).empty()) {
329                 buffer.message(_("Nothing to search"));
330                 return;
331         }
332         bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
333         docstring replaceString;
334         if (replace) {
335                 Buffer & repl_buffer = replace_work_area_->bufferView().buffer();
336                 ostringstream oss;
337                 repl_buffer.write(oss);
338                 replaceString = from_utf8(oss.str()); //buffer_to_latex(replace_buffer);
339         } else {
340                 replaceString = from_utf8(LYX_FR_NULL_STRING);
341         }
342         FindAndReplaceOptions::SearchScope scope = FindAndReplaceOptions::S_BUFFER;
343         if (CurrentDocument->isChecked())
344                 scope = FindAndReplaceOptions::S_BUFFER;
345         else if (MasterDocument->isChecked())
346                 scope = FindAndReplaceOptions::S_DOCUMENT;
347         else if (OpenDocuments->isChecked())
348                 scope = FindAndReplaceOptions::S_OPEN_BUFFERS;
349         else
350                 LASSERT(false, /**/);
351         LYXERR(Debug::FIND, "FindAndReplaceOptions: "
352                << "searchstring=" << searchString
353                << ", casesensitiv=" << casesensitive
354                << ", matchword=" << matchword
355                << ", backwards=" << backwards
356                << ", expandmacros=" << expandmacros
357                << ", ignoreformat=" << ignoreformat
358                << ", regexp=" << regexp
359                << ", replaceString" << replaceString
360                << ", keep_case=" << keep_case
361                << ", scope=" << scope);
362         FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
363                 expandmacros, ignoreformat, regexp, replaceString, keep_case, scope);
364         findAndReplaceScope(opt);
365 }
366
367
368 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
369 {
370         if (! view_.currentMainWorkArea()) {
371                 view_.message(_("No open document(s) in which to search"));
372                 return;
373         }
374         // FIXME: create a Dialog::returnFocus() or something instead of this:
375         view_.setCurrentWorkArea(view_.currentMainWorkArea());
376         findAndReplace(caseCB->isChecked(),
377                 wordsCB->isChecked(),
378                 backwards,
379                 expandMacrosCB->isChecked(),
380                 ignoreFormatCB->isChecked(),
381                 replace,
382                 keepCaseCB->isChecked());
383 }
384
385
386 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
387 {
388         static char const * regexps[] = {
389                 ".*", ".+", "[a-z]+", "[0-9]+", ""
390         };
391         LYXERR(Debug::FIND, "Index: " << index);
392         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
393                 find_work_area_->setFocus();
394                 Cursor & cur = find_work_area_->bufferView().cursor();
395                 if (! cur.inRegexped())
396                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
397                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
398                 regexpInsertCombo->setCurrentIndex(0);
399         }
400 }
401
402
403 void FindAndReplaceWidget::on_closePB_clicked()
404 {
405         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
406 }
407
408
409 void FindAndReplaceWidget::on_findNextPB_clicked() {
410         findAndReplace(false, false);
411         find_work_area_->setFocus();
412 }
413
414
415 void FindAndReplaceWidget::on_findPrevPB_clicked() {
416         findAndReplace(true, false);
417         find_work_area_->setFocus();
418 }
419
420
421 void FindAndReplaceWidget::on_replaceNextPB_clicked()
422 {
423         findAndReplace(false, true);
424         replace_work_area_->setFocus();
425 }
426
427
428 void FindAndReplaceWidget::on_replacePrevPB_clicked()
429 {
430         findAndReplace(true, true);
431         replace_work_area_->setFocus();
432 }
433
434
435 void FindAndReplaceWidget::on_replaceallPB_clicked()
436 {
437         replace_work_area_->setFocus();
438 }
439
440
441 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
442 {
443         view_.setCurrentWorkArea(find_work_area_);
444         LYXERR(Debug::FIND, "Selecting entire find buffer");
445         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
446         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
447         find_work_area_->installEventFilter(this);
448         replace_work_area_->installEventFilter(this);
449 }
450
451
452 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
453 {
454         replace_work_area_->removeEventFilter(this);
455         find_work_area_->removeEventFilter(this);
456         this->QWidget::hideEvent(ev);
457 }
458
459
460 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
461 {
462         return true;
463 }
464
465
466 FindAndReplace::FindAndReplace(GuiView & parent,
467                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
468         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
469 {
470         widget_ = new FindAndReplaceWidget(parent);
471         setWidget(widget_);
472         setFocusProxy(widget_);
473 }
474
475
476 FindAndReplace::~FindAndReplace()
477 {
478         setFocusProxy(0);
479         delete widget_;
480 }
481
482
483 bool FindAndReplace::initialiseParams(std::string const & params)
484 {
485         return widget_->initialiseParams(params);
486 }
487
488
489 Dialog * createGuiSearchAdv(GuiView & lv)
490 {
491         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
492 }
493
494
495 } // namespace frontend
496 } // namespace lyx
497
498
499 #include "moc_FindAndReplace.cpp"