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