]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
"Inter-word Space"
[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 sort of 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                         return 0;
83                 }
84         }
85
86         bool found = false;
87         int replace_count = 0;
88         do {
89                 text = bv->getLyXText();
90                 // We have to do this check only because mathed insets don't
91                 // return their own LyXText but the LyXText of it's parent!
92                 if (!bv->theLockingInset() ||
93                         ((text != bv->text) &&
94                          (text->inset_owner == text->inset_owner->getLockingInset()))) {
95                         bv->update(text, BufferView::SELECT);
96                         bv->toggleSelection(false);
97                         text->replaceSelectionWithString(replacestr);
98                         text->setSelectionRange(replacestr.length());
99                         bv->update(text, BufferView::SELECT);
100                         ++replace_count;
101                 }
102                 if (!once)
103                         found = LyXFind(bv, searchstr, fw, casesens, matchwrd);
104         } while (!once && replaceall && found);
105
106         // FIXME: should be called via an LFUN
107         bv->buffer()->markDirty();
108         bv->fitCursor();
109
110         return replace_count;
111 }
112
113
114 bool LyXFind(BufferView * bv,
115              string const & searchstr, bool forward,
116              bool casesens, bool matchwrd)
117 {
118         if (!bv->available() || searchstr.empty())
119                 return false;
120
121         bv->update(bv->getLyXText(), BufferView::SELECT);
122
123         if (bv->theLockingInset()) {
124                 bool found = forward ?
125                         bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
126                         bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
127                 // We found the stuff inside the inset so we don't have to
128                 // do anything as the inset did all the update for us!
129                 if (found)
130                         return true;
131                 // We now are in the main text but if we did a forward
132                 // search we have to put the cursor behind the inset.
133                 if (forward) {
134                         bv->text->cursorRight(true);
135                 }
136         }
137         // If we arrive here we are in the main text again so we
138         // just start searching from the root LyXText at the position
139         // we are!
140         LyXText * text = bv->text;
141
142
143         if (text->selection.set())
144                 text->cursor = forward ?
145                         text->selection.end : text->selection.start;
146
147         bv->toggleSelection();
148         text->clearSelection();
149
150         SearchResult result = forward ?
151                 SearchForward(bv, text, searchstr, casesens, matchwrd) :
152                 SearchBackward(bv, text, searchstr, casesens, matchwrd);
153
154         bool found = true;
155         // If we found the cursor inside an inset we will get back
156         // SR_FOUND_NOUPDATE and we don't have to do anything as the
157         // inset did it already.
158         if (result == SR_FOUND) {
159                 bv->unlockInset(bv->theLockingInset());
160                 bv->update(text, BufferView::SELECT);
161                 text->setSelectionRange(searchstr.length());
162                 bv->toggleSelection(false);
163                 bv->update(text, BufferView::SELECT);
164         } else if (result == SR_NOT_FOUND) {
165                 bv->unlockInset(bv->theLockingInset());
166                 bv->update(text, BufferView::SELECT);
167                 found = false;
168         }
169
170         bv->fitCursor();
171
172         return found;
173 }
174
175
176 SearchResult LyXFind(BufferView * bv, LyXText * text,
177                      string const & searchstr, bool forward,
178                      bool casesens, bool matchwrd)
179 {
180         if (text->selection.set())
181                 text->cursor = forward ?
182                         text->selection.end : text->selection.start;
183
184         bv->toggleSelection();
185         text->clearSelection();
186
187         SearchResult result = forward ?
188                 SearchForward(bv, text, searchstr, casesens, matchwrd) :
189                 SearchBackward(bv, text, searchstr, casesens, matchwrd);
190
191         return result;
192 }
193
194
195 // returns true if the specified string is at the specified position
196 bool IsStringInText(Paragraph const & par, pos_type pos,
197                     string const & str, bool const & cs,
198                     bool const & mw)
199 {
200         string::size_type size = str.length();
201         pos_type i = 0;
202         pos_type parsize = par.size();
203         while (((pos + i) < parsize)
204                && (string::size_type(i) < size)
205                && (cs ? (str[i] == par.getChar(pos + i))
206                    : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
207                 ++i;
208         }
209
210         if (size == string::size_type(i)) {
211                 // if necessary, check whether string matches word
212                 if (!mw)
213                         return true;
214                 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
215                         && (pos + pos_type(size) >= parsize
216                         || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
217                         return true;
218                 }
219         }
220         return false;
221 }
222
223 // forward search:
224 // if the string can be found: return true and set the cursor to
225 // the new position, cs = casesensitive, mw = matchword
226 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
227                            bool const & cs, bool const & mw)
228 {
229         ParagraphList::iterator pit = text->cursor.par();
230         ParagraphList::iterator pend = text->ownerParagraphs().end();
231         pos_type pos = text->cursor.pos();
232         UpdatableInset * inset;
233
234         while (pit != pend && !IsStringInText(*pit, pos, str, cs, mw)) {
235                 if (pos < pit->size()
236                     && pit->isInset(pos)
237                     && (inset = (UpdatableInset *)pit->getInset(pos))
238                     && inset->isTextInset()
239                     && inset->searchForward(bv, str, cs, mw))
240                         return SR_FOUND_NOUPDATE;
241
242                 if (++pos >= pit->size()) {
243                         ++pit;
244                         pos = 0;
245                 }
246         }
247
248         if (pit != pend) {
249                 text->setCursor(pit, pos);
250                 return SR_FOUND;
251         } else
252                 return SR_NOT_FOUND;
253 }
254
255
256 // backward search:
257 // if the string can be found: return true and set the cursor to
258 // the new position, cs = casesensitive, mw = matchword
259 SearchResult SearchBackward(BufferView * bv, LyXText * text,
260                             string const & str,
261                             bool const & cs, bool const & mw)
262 {
263         ParagraphList::iterator pit = text->cursor.par();
264         ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
265         pos_type pos = text->cursor.pos();
266
267         // skip past a match at the current cursor pos
268         if (pos > 0) {
269                 --pos;
270         } else if (pit != pbegin) {
271                 --pit;
272                 pos = pit->size();
273         } else {
274                 return SR_NOT_FOUND;
275         }
276
277         while (true) {
278                 if (pos < pit->size()) {
279                         if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
280                                 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
281                                 if (inset->searchBackward(bv, str, cs, mw))
282                                         return SR_FOUND_NOUPDATE;
283                         }
284
285                         if (IsStringInText(*pit, pos, str, cs, mw)) {
286                                 text->setCursor(pit, pos);
287                                 return SR_FOUND;
288                         }
289                 }
290
291                 if (pos == 0 && pit == pbegin)
292                         break;
293
294                 if (pos > 0) {
295                         --pos;
296                 } else if (pit != pbegin) {
297                         --pit;
298                         pos = pit->size();
299                 }
300         }
301
302         return SR_NOT_FOUND;
303 }
304
305
306 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
307 {
308         ParagraphList::iterator pit = text->cursor.par();
309         ParagraphList::iterator pend = text->ownerParagraphs().end();
310         pos_type pos = text->cursor.pos();
311
312         while (pit != pend) {
313                 pos_type parsize = pit->size();
314
315                 if (pos < parsize) {
316                         if ((!parsize || pos != parsize)
317                                 && pit->lookupChange(pos) != Change::UNCHANGED)
318                                 break;
319
320                         if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
321                                 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
322                                 if (inset->nextChange(bv, length))
323                                         return SR_FOUND_NOUPDATE;
324                         }
325                 }
326
327                 ++pos;
328
329                 if (pos >= parsize) {
330                         ++pit;
331                         pos = 0;
332                 }
333         }
334
335         if (pit == pend)
336                 return SR_NOT_FOUND;
337
338         text->setCursor(pit, pos);
339         Change orig_change = pit->lookupChangeFull(pos);
340         pos_type parsize = pit->size();
341         pos_type end = pos;
342
343         for (; end != parsize; ++end) {
344                 Change change = pit->lookupChangeFull(end);
345                 if (change != orig_change) {
346                         // slight UI optimisation: for replacements, we get
347                         // text like : _old_new. Consider that as one change.
348                         if (!(orig_change.type == Change::DELETED &&
349                                 change.type == Change::INSERTED))
350                                 break;
351                 }
352         }
353         length = end - pos;
354         return SR_FOUND;
355 }
356
357
358 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
359 {
360         if (text->selection.set())
361                 text->cursor = text->selection.end;
362
363         bv->toggleSelection();
364         text->clearSelection();
365
366         return nextChange(bv, text, length);
367 }
368
369
370 bool findNextChange(BufferView * bv)
371 {
372         if (!bv->available())
373                 return false;
374
375         bv->update(bv->getLyXText(), BufferView::SELECT);
376
377         pos_type length;
378
379         if (bv->theLockingInset()) {
380                 bool found = bv->theLockingInset()->nextChange(bv, length);
381
382                 // We found the stuff inside the inset so we don't have to
383                 // do anything as the inset did all the update for us!
384                 if (found)
385                         return true;
386
387                 // We now are in the main text but if we did a forward
388                 // search we have to put the cursor behind the inset.
389                 bv->text->cursorRight(true);
390         }
391         // If we arrive here we are in the main text again so we
392         // just start searching from the root LyXText at the position
393         // we are!
394         LyXText * text = bv->text;
395
396         if (text->selection.set())
397                 text->cursor = text->selection.end;
398
399         bv->toggleSelection();
400         text->clearSelection();
401
402         SearchResult result = nextChange(bv, text, length);
403
404         bool found = true;
405
406         // If we found the cursor inside an inset we will get back
407         // SR_FOUND_NOUPDATE and we don't have to do anything as the
408         // inset did it already.
409         if (result == SR_FOUND) {
410                 bv->unlockInset(bv->theLockingInset());
411                 bv->update(text, BufferView::SELECT);
412                 text->setSelectionRange(length);
413                 bv->toggleSelection(false);
414                 bv->update(text, BufferView::SELECT);
415         } else if (result == SR_NOT_FOUND) {
416                 bv->unlockInset(bv->theLockingInset());
417                 bv->update(text, BufferView::SELECT);
418                 found = false;
419         }
420
421         bv->fitCursor();
422
423         return found;
424 }
425
426 } // end lyxfind namespace