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