]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
Correct Andr�'s name
[lyx.git] / src / lyxfind.C
1 /**
2  * \file lyxfind.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Jürgen Vigna
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "lyxfind.h"
16
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "gettext.h"
20 #include "lyxtext.h"
21
22 #include "frontends/Alert.h"
23
24 #include "insets/insettext.h"
25
26 #include "support/textutils.h"
27
28 using lyx::support::lowercase;
29 using lyx::support::uppercase;
30
31
32 namespace lyx {
33 namespace find {
34
35 namespace {
36
37 // returns true if the specified string is at the specified position
38 bool isStringInText(Paragraph const & par, pos_type pos,
39                     string const & str, bool const & cs,
40                     bool const & mw)
41 {
42         string::size_type size = str.length();
43         pos_type i = 0;
44         pos_type parsize = par.size();
45         while (((pos + i) < parsize)
46                && (string::size_type(i) < size)
47                && (cs ? (str[i] == par.getChar(pos + i))
48                    : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
49                 ++i;
50         }
51
52         if (size == string::size_type(i)) {
53                 // if necessary, check whether string matches word
54                 if (!mw)
55                         return true;
56                 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
57                         && (pos + pos_type(size) >= parsize
58                         || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
59                         return true;
60                 }
61         }
62         return false;
63 }
64
65 // forward search:
66 // if the string can be found: return true and set the cursor to
67 // the new position, cs = casesensitive, mw = matchword
68 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
69                            bool const & cs, bool const & mw)
70 {
71         ParagraphList::iterator pit = text->cursor.par();
72         ParagraphList::iterator pend = text->ownerParagraphs().end();
73         pos_type pos = text->cursor.pos();
74         UpdatableInset * inset;
75
76         while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
77                 if (pos < pit->size()
78                     && pit->isInset(pos)
79                     && (inset = (UpdatableInset *)pit->getInset(pos))
80                     && inset->isTextInset()
81                     && inset->searchForward(bv, str, cs, mw))
82                         return SR_FOUND_NOUPDATE;
83
84                 if (++pos >= pit->size()) {
85                         ++pit;
86                         pos = 0;
87                 }
88         }
89
90         if (pit != pend) {
91                 text->setCursor(pit, pos);
92                 return SR_FOUND;
93         } else
94                 return SR_NOT_FOUND;
95 }
96
97
98 // backward search:
99 // if the string can be found: return true and set the cursor to
100 // the new position, cs = casesensitive, mw = matchword
101 SearchResult searchBackward(BufferView * bv, LyXText * text,
102                             string const & str,
103                             bool const & cs, bool const & mw)
104 {
105         ParagraphList::iterator pit = text->cursor.par();
106         ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
107         pos_type pos = text->cursor.pos();
108
109         // skip past a match at the current cursor pos
110         if (pos > 0) {
111                 --pos;
112         } else if (pit != pbegin) {
113                 --pit;
114                 pos = pit->size();
115         } else {
116                 return SR_NOT_FOUND;
117         }
118
119         while (true) {
120                 if (pos < pit->size()) {
121                         if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
122                                 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
123                                 if (inset->searchBackward(bv, str, cs, mw))
124                                         return SR_FOUND_NOUPDATE;
125                         }
126
127                         if (isStringInText(*pit, pos, str, cs, mw)) {
128                                 text->setCursor(pit, pos);
129                                 return SR_FOUND;
130                         }
131                 }
132
133                 if (pos == 0 && pit == pbegin)
134                         break;
135
136                 if (pos > 0) {
137                         --pos;
138                 } else if (pit != pbegin) {
139                         --pit;
140                         pos = pit->size();
141                 }
142         }
143
144         return SR_NOT_FOUND;
145 }
146
147 } // anon namespace
148
149
150 int replace(BufferView * bv,
151                string const & searchstr, string const & replacestr,
152                bool forward, bool casesens, bool matchwrd, bool replaceall,
153                bool once)
154 {
155         if (!bv->available() || bv->buffer()->isReadonly())
156                 return 0;
157
158         // CutSelection cannot cut a single space, so we have to stop
159         // in order to avoid endless loop :-(
160         if (searchstr.length() == 0
161                 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
162 #ifdef WITH_WARNINGS
163 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
164 #endif
165                 Alert::error(_("Cannot replace"),
166                         _("You cannot replace a single space or "
167                           "an empty character."));
168                 return 0;
169         }
170
171         // now we can start searching for the first
172         // start at top if replaceall
173         LyXText * text = bv->getLyXText();
174         bool fw = forward;
175         if (replaceall) {
176                 text->clearSelection();
177                 bv->unlockInset(bv->theLockingInset());
178                 text = bv->text;
179                 text->cursorTop();
180                 // override search direction because we search top to bottom
181                 fw = true;
182         }
183
184         // if nothing selected or selection does not equal search string
185         // search and select next occurance and return if no replaceall
186         string str1;
187         string str2;
188         if (casesens) {
189                 str1 = searchstr;
190                 str2 = text->selectionAsString(*bv->buffer(), false);
191         } else {
192                 str1 = lowercase(searchstr);
193                 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
194         }
195         if (str1 != str2) {
196                 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
197                         !replaceall) {
198                         return 0;
199                 }
200         }
201
202         bool found = false;
203         int replace_count = 0;
204         do {
205                 text = bv->getLyXText();
206                 // We have to do this check only because mathed insets don't
207                 // return their own LyXText but the LyXText of it's parent!
208                 if (!bv->theLockingInset() ||
209                         ((text != bv->text) &&
210                          (text->inset_owner == text->inset_owner->getLockingInset()))) {
211                         text->replaceSelectionWithString(replacestr);
212                         text->setSelectionRange(replacestr.length());
213                         ++replace_count;
214                 }
215                 if (!once)
216                         found = find(bv, searchstr, fw, casesens, matchwrd);
217         } while (!once && replaceall && found);
218
219         // FIXME: should be called via an LFUN
220         bv->buffer()->markDirty();
221         bv->update();
222
223         return replace_count;
224 }
225
226
227 bool find(BufferView * bv,
228              string const & searchstr, bool forward,
229              bool casesens, bool matchwrd)
230 {
231         if (!bv->available() || searchstr.empty())
232                 return false;
233
234         if (bv->theLockingInset()) {
235                 bool found = forward ?
236                         bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
237                         bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
238                 // We found the stuff inside the inset so we don't have to
239                 // do anything as the inset did all the update for us!
240                 if (found)
241                         return true;
242                 // We now are in the main text but if we did a forward
243                 // search we have to put the cursor behind the inset.
244                 if (forward) {
245                         bv->text->cursorRight(true);
246                 }
247         }
248         // If we arrive here we are in the main text again so we
249         // just start searching from the root LyXText at the position
250         // we are!
251         LyXText * text = bv->text;
252
253
254         if (text->selection.set())
255                 text->cursor = forward ?
256                         text->selection.end : text->selection.start;
257
258         text->clearSelection();
259
260         SearchResult result = forward ?
261                 searchForward(bv, text, searchstr, casesens, matchwrd) :
262                 searchBackward(bv, text, searchstr, casesens, matchwrd);
263
264         bool found = true;
265         // If we found the cursor inside an inset we will get back
266         // SR_FOUND_NOUPDATE and we don't have to do anything as the
267         // inset did it already.
268         if (result == SR_FOUND) {
269                 bv->unlockInset(bv->theLockingInset());
270                 text->setSelectionRange(searchstr.length());
271         } else if (result == SR_NOT_FOUND) {
272                 bv->unlockInset(bv->theLockingInset());
273                 found = false;
274         }
275         bv->update();
276
277         return found;
278 }
279
280
281 SearchResult find(BufferView * bv, LyXText * text,
282                      string const & searchstr, bool forward,
283                      bool casesens, bool matchwrd)
284 {
285         if (text->selection.set())
286                 text->cursor = forward ?
287                         text->selection.end : text->selection.start;
288
289         text->clearSelection();
290
291         SearchResult result = forward ?
292                 searchForward(bv, text, searchstr, casesens, matchwrd) :
293                 searchBackward(bv, text, searchstr, casesens, matchwrd);
294
295         return result;
296 }
297
298
299
300
301 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
302 {
303         ParagraphList::iterator pit = text->cursor.par();
304         ParagraphList::iterator pend = text->ownerParagraphs().end();
305         pos_type pos = text->cursor.pos();
306
307         while (pit != pend) {
308                 pos_type parsize = pit->size();
309
310                 if (pos < parsize) {
311                         if ((!parsize || pos != parsize)
312                                 && pit->lookupChange(pos) != Change::UNCHANGED)
313                                 break;
314
315                         if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
316                                 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
317                                 if (inset->nextChange(bv, length))
318                                         return SR_FOUND_NOUPDATE;
319                         }
320                 }
321
322                 ++pos;
323
324                 if (pos >= parsize) {
325                         ++pit;
326                         pos = 0;
327                 }
328         }
329
330         if (pit == pend)
331                 return SR_NOT_FOUND;
332
333         text->setCursor(pit, pos);
334         Change orig_change = pit->lookupChangeFull(pos);
335         pos_type parsize = pit->size();
336         pos_type end = pos;
337
338         for (; end != parsize; ++end) {
339                 Change change = pit->lookupChangeFull(end);
340                 if (change != orig_change) {
341                         // slight UI optimisation: for replacements, we get
342                         // text like : _old_new. Consider that as one change.
343                         if (!(orig_change.type == Change::DELETED &&
344                                 change.type == Change::INSERTED))
345                                 break;
346                 }
347         }
348         length = end - pos;
349         return SR_FOUND;
350 }
351
352
353 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
354 {
355         if (text->selection.set())
356                 text->cursor = text->selection.end;
357
358         text->clearSelection();
359
360         return nextChange(bv, text, length);
361 }
362
363
364 bool findNextChange(BufferView * bv)
365 {
366         if (!bv->available())
367                 return false;
368
369         pos_type length;
370
371         if (bv->theLockingInset()) {
372                 bool found = bv->theLockingInset()->nextChange(bv, length);
373
374                 // We found the stuff inside the inset so we don't have to
375                 // do anything as the inset did all the update for us!
376                 if (found)
377                         return true;
378
379                 // We now are in the main text but if we did a forward
380                 // search we have to put the cursor behind the inset.
381                 bv->text->cursorRight(true);
382         }
383         // If we arrive here we are in the main text again so we
384         // just start searching from the root LyXText at the position
385         // we are!
386         LyXText * text = bv->text;
387
388         if (text->selection.set())
389                 text->cursor = text->selection.end;
390
391         text->clearSelection();
392
393         SearchResult result = nextChange(bv, text, length);
394
395         bool found = true;
396
397         // If we found the cursor inside an inset we will get back
398         // SR_FOUND_NOUPDATE and we don't have to do anything as the
399         // inset did it already.
400         if (result == SR_FOUND) {
401                 bv->unlockInset(bv->theLockingInset());
402                 text->setSelectionRange(length);
403         } else if (result == SR_NOT_FOUND) {
404                 bv->unlockInset(bv->theLockingInset());
405                 found = false;
406         }
407
408         bv->update();
409
410         return found;
411 }
412
413 } // find namespace
414 } // lyx namespace