]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
09ff2f257a848e5fe661ec9049bb9c3a2233dce3
[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/convert.h"
30 #include "support/debug.h"
31 #include "support/FileName.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
34
35 #include <QCloseEvent>
36 #include <QLineEdit>
37
38 #include <iostream>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44 namespace frontend {
45
46 FindAndReplace::FindAndReplace(GuiView & parent)
47         : DockView(parent, "Find LyX", "Find LyX Dialog", Qt::RightDockWidgetArea)
48 {
49         setupUi(this);
50         find_work_area_->setGuiView(parent);
51         find_work_area_->init();
52         setFocusProxy(find_work_area_);
53         replace_work_area_->setGuiView(parent);
54         replace_work_area_->init();
55         // We don't want two cursors blinking.
56         replace_work_area_->stopBlinkingCursor();
57 }
58
59
60 bool FindAndReplace::eventFilter(QObject *obj, QEvent *event)
61 {
62         LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
63         if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
64                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
65                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
66                         on_closePB_clicked();
67                         return true;
68                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
69                         if (e->modifiers() == Qt::ShiftModifier) {
70                                 on_findPrevPB_clicked();
71                                 return true;
72                         } else if (e->modifiers() == Qt::NoModifier) {
73                                 on_findNextPB_clicked();
74                                 return true;
75                         }
76                 }
77         }
78         // standard event processing
79         return QObject::eventFilter(obj, event);
80 }
81
82
83 void FindAndReplace::findAdv(bool casesensitive,
84                 bool matchword, bool backwards,
85                 bool expandmacros, bool ignoreformat)
86 {
87         Buffer & buffer = find_work_area_->bufferView().buffer();
88         docstring searchString;
89         if (!ignoreformat) {
90                 OutputParams runparams(&buffer.params().encoding());
91                 odocstringstream os;
92                 runparams.nice = true;
93                 runparams.flavor = OutputParams::LATEX;
94                 runparams.linelen = 80; //lyxrc.plaintext_linelen;
95                 // No side effect of file copying and image conversion
96                 runparams.dryrun = true;
97                 buffer.texrow().reset();
98 //              latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
99                 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
100                 ParagraphList::const_iterator const end = buffer.paragraphs().end();
101                 for (; pit != end; ++pit) {
102                         TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
103                         LYXERR0("searchString up to here: " << os.str());
104                 }
105                 searchString = os.str();
106         } else {
107                 ParIterator it = buffer.par_iterator_begin();
108                 ParIterator end = buffer.par_iterator_end();
109                 OutputParams runparams(&buffer.params().encoding());
110                 odocstringstream os;
111                 runparams.nice = true;
112                 runparams.flavor = OutputParams::LATEX;
113                 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
114                 runparams.dryrun = true;
115                 for (; it != end; ++it) {
116                         LYXERR0("Adding to search string: '" << it->asString(false) << "'");
117                         searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
118                 }
119         }
120 //      lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
121         if (to_utf8(searchString).empty()) {
122                 buffer.message(_("Nothing to search"));
123                 return;
124         }
125         bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
126         FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
127                 expandmacros, ignoreformat, regexp);
128         std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
129         std::ostringstream oss;
130         oss << opt;
131         std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
132         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
133
134         //      findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
135         //                      searchString, len, casesensitive, matchword, ! backwards, expandmacros);
136 }
137
138 bool FindAndReplace::initialiseParams(std::string const &)
139 {
140         find_work_area_->redraw();
141         replace_work_area_->setEnabled(true);
142         replace_work_area_->redraw();
143         find_work_area_->setFocus();
144         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
145         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
146         return true;
147 }
148
149
150 void FindAndReplace::find(bool backwards)
151 {
152         // FIXME: create a Dialog::returnFocus() or something instead of this:
153         GuiView & gv = const_cast<GuiView &>(lyxview());
154         gv.setCurrentWorkArea(gv.currentMainWorkArea());
155         // FIXME: This should be an LFUN.
156         findAdv(caseCB->isChecked(),
157                         wordsCB->isChecked(),
158                         backwards,
159                         expandMacrosCB->isChecked(),
160                         ignoreFormatCB->isChecked());
161         gv.currentMainWorkArea()->redraw();
162         find_work_area_->setFocus();
163 }
164
165
166 void FindAndReplace::on_regexpInsertCombo_currentIndexChanged(int index)
167 {
168         static char const * regexps[] = {
169                 ".*", ".+", "[a-z]+", "[0-9]+"
170         };
171         //lyxerr << "Index: " << index << std::endl;
172         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
173                 find_work_area_->setFocus();
174                 Cursor & cur = find_work_area_->bufferView().cursor();
175                 if (! cur.inRegexped())
176                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
177                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
178                 regexpInsertCombo->setCurrentIndex(0);
179         }
180 }
181
182
183 void FindAndReplace::on_closePB_clicked()
184 {
185         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
186 }
187
188
189 void FindAndReplace::on_findNextPB_clicked() {
190         find(false);
191 }
192
193
194 void FindAndReplace::on_findPrevPB_clicked() {
195         find(true);
196 }
197
198
199 void FindAndReplace::on_replacePB_clicked()
200 {
201 }
202
203
204 void FindAndReplace::on_replaceallPB_clicked()
205 {
206 }
207
208 Dialog * createGuiSearchAdv(GuiView & lv)
209 {
210         return new FindAndReplace(lv);
211 }
212
213
214 } // namespace frontend
215 } // namespace lyx
216
217
218 #include "moc_FindAndReplace.cpp"