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