Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

lyxfind Namespace Reference


Enumerations

enum  SearchResult { SR_NOT_FOUND = 0, SR_FOUND, SR_FOUND_NOUPDATE }

Functions

bool IsStringInText (Paragraph *par, pos_type pos, string const &str, bool const &=true, bool const &=false)
 returns true if the specified string is at the specified position. More...

SearchResult SearchForward (BufferView *, LyXText *text, string const &str, bool const &=true, bool const &=false)
 if the string is found: return true and set the cursor to the new position. More...

SearchResult SearchBackward (BufferView *, LyXText *text, string const &str, bool const &=true, bool const &=false)
int LyXReplace (BufferView *bv, string const &searchstr, string const &replacestr, bool forward, bool casesens, bool matchwrd, bool replaceall, bool once)
bool LyXFind (BufferView *bv, string const &searchstr, bool forward, bool casesens, bool matchwrd)
 This function is called as a general interface to find some text from the actual cursor position in whatever direction we want to go. More...

SearchResult LyXFind (BufferView *bv, LyXText *text, string const &searchstr, bool forward, bool casesens, bool matchwrd)
 This function does search from the cursor position inside the passed LyXText parameter and regards this LyXText as the root LyXText. More...

bool IsStringInText (Paragraph const &par, pos_type pos, string const &str, bool const &cs, bool const &mw)
SearchResult nextChange (BufferView *bv, LyXText *text, pos_type &length)
SearchResult findNextChange (BufferView *bv, LyXText *text, pos_type &length)
bool findNextChange (BufferView *bv)
 find the next change in the buffer. More...


Enumeration Type Documentation

enum lyxfind::SearchResult
 

Enumeration values:
SR_NOT_FOUND 
SR_FOUND 
SR_FOUND_NOUPDATE 

Definition at line 13 of file lyxfind.h.

00013                   {
00014         //
00015         SR_NOT_FOUND = 0,
00016         //
00017         SR_FOUND,
00018         //
00019         SR_FOUND_NOUPDATE
00020 };


Function Documentation

bool lyxfind::findNextChange BufferView   bv
 

find the next change in the buffer.

Definition at line 372 of file lyxfind.C.

00373 {
00374         if (!bv->available())
00375                 return false;
00376 
00377         bv->update(bv->getLyXText(), BufferView::SELECT);
00378 
00379         pos_type length;
00380 
00381         if (bv->theLockingInset()) {
00382                 bool found = bv->theLockingInset()->nextChange(bv, length);
00383 
00384                 // We found the stuff inside the inset so we don't have to
00385                 // do anything as the inset did all the update for us!
00386                 if (found)
00387                         return true;
00388 
00389                 // We now are in the main text but if we did a forward
00390                 // search we have to put the cursor behind the inset.
00391                 bv->text->cursorRight(true);
00392         }
00393         // If we arrive here we are in the main text again so we
00394         // just start searching from the root LyXText at the position
00395         // we are!
00396         LyXText * text = bv->text;
00397 
00398         if (text->selection.set())
00399                 text->cursor = text->selection.end;
00400 
00401         bv->toggleSelection();
00402         text->clearSelection();
00403 
00404         SearchResult result = nextChange(bv, text, length);
00405 
00406         bool found = true;
00407 
00408         // If we found the cursor inside an inset we will get back
00409         // SR_FOUND_NOUPDATE and we don't have to do anything as the
00410         // inset did it already.
00411         if (result == SR_FOUND) {
00412                 bv->unlockInset(bv->theLockingInset());
00413                 bv->update(text, BufferView::SELECT);
00414                 text->setSelectionRange(length);
00415                 bv->toggleSelection(false);
00416                 bv->update(text, BufferView::SELECT);
00417         } else if (result == SR_NOT_FOUND) {
00418                 bv->unlockInset(bv->theLockingInset());
00419                 bv->update(text, BufferView::SELECT);
00420                 found = false;
00421         }
00422 
00423         bv->fitCursor();
00424 
00425         return found;
00426 }

SearchResult lyxfind::findNextChange BufferView   bv,
LyXText   text,
lyx::pos_type   length
 

Definition at line 360 of file lyxfind.C.

Referenced by ControlChanges::accept, BufferView::Pimpl::dispatch, ControlChanges::find, InsetText::nextChange, ControlChanges::reject, and BufferView::Pimpl::trackChanges.

00361 {
00362         if (text->selection.set())
00363                 text->cursor = text->selection.end;
00364 
00365         bv->toggleSelection();
00366         text->clearSelection();
00367 
00368         return nextChange(bv, text, length);
00369 }

bool IsStringInText Paragraph const &    par,
pos_type    pos,
string const &    str,
bool const &    cs,
bool const &    mw
 

Definition at line 198 of file lyxfind.C.

00201 {
00202         string::size_type size = str.length();
00203         pos_type i = 0;
00204         pos_type parsize = par.size();
00205         while (((pos + i) < parsize)
00206                && (string::size_type(i) < size)
00207                && (cs ? (str[i] == par.getChar(pos + i))
00208                    : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
00209                 ++i;
00210         }
00211 
00212         if (size == string::size_type(i)) {
00213                 // if necessary, check whether string matches word
00214                 if (!mw)
00215                         return true;
00216                 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
00217                         && (pos + pos_type(size) >= parsize
00218                         || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
00219                         return true;
00220                 }
00221         }
00222         return false;
00223 }

bool IsStringInText Paragraph   par,
pos_type    pos,
string const &    str,
bool const &    = true,
bool const &    = false
 

returns true if the specified string is at the specified position.

SearchResult lyxfind::LyXFind BufferView  ,
LyXText   text,
string const &    searchstr,
bool    forward,
bool    casesens = true,
bool    matchwrd = false
 

This function does search from the cursor position inside the passed LyXText parameter and regards this LyXText as the root LyXText.

It will NOT update any screen stuff. It will however set the cursor to the new position inside LyXText, before returning to the calling function.

Definition at line 178 of file lyxfind.C.

00181 {
00182         if (text->selection.set())
00183                 text->cursor = forward ?
00184                         text->selection.end : text->selection.start;
00185 
00186         bv->toggleSelection();
00187         text->clearSelection();
00188 
00189         SearchResult result = forward ?
00190                 SearchForward(bv, text, searchstr, casesens, matchwrd) :
00191                 SearchBackward(bv, text, searchstr, casesens, matchwrd);
00192 
00193         return result;
00194 }

bool lyxfind::LyXFind BufferView  ,
string const &    searchstr,
bool    forward,
bool    casesens = true,
bool    matchwrd = false
 

This function is called as a general interface to find some text from the actual cursor position in whatever direction we want to go.

This does also update the screen.

Definition at line 116 of file lyxfind.C.

Referenced by LyXFunc::dispatch, ControlSearch::find, InsetText::searchBackward, and InsetText::searchForward.

00119 {
00120         if (!bv->available() || searchstr.empty())
00121                 return false;
00122 
00123         bv->update(bv->getLyXText(), BufferView::SELECT);
00124 
00125         if (bv->theLockingInset()) {
00126                 bool found = forward ?
00127                         bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
00128                         bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
00129                 // We found the stuff inside the inset so we don't have to
00130                 // do anything as the inset did all the update for us!
00131                 if (found)
00132                         return true;
00133                 // We now are in the main text but if we did a forward
00134                 // search we have to put the cursor behind the inset.
00135                 if (forward) {
00136                         bv->text->cursorRight(true);
00137                 }
00138         }
00139         // If we arrive here we are in the main text again so we
00140         // just start searching from the root LyXText at the position
00141         // we are!
00142         LyXText * text = bv->text;
00143 
00144 
00145         if (text->selection.set())
00146                 text->cursor = forward ?
00147                         text->selection.end : text->selection.start;
00148 
00149         bv->toggleSelection();
00150         text->clearSelection();
00151 
00152         SearchResult result = forward ?
00153                 SearchForward(bv, text, searchstr, casesens, matchwrd) :
00154                 SearchBackward(bv, text, searchstr, casesens, matchwrd);
00155 
00156         bool found = true;
00157         // If we found the cursor inside an inset we will get back
00158         // SR_FOUND_NOUPDATE and we don't have to do anything as the
00159         // inset did it already.
00160         if (result == SR_FOUND) {
00161                 bv->unlockInset(bv->theLockingInset());
00162                 bv->update(text, BufferView::SELECT);
00163                 text->setSelectionRange(searchstr.length());
00164                 bv->toggleSelection(false);
00165                 bv->update(text, BufferView::SELECT);
00166         } else if (result == SR_NOT_FOUND) {
00167                 bv->unlockInset(bv->theLockingInset());
00168                 bv->update(text, BufferView::SELECT);
00169                 found = false;
00170         }
00171 
00172         bv->fitCursor();
00173 
00174         return found;
00175 }

int lyxfind::LyXReplace BufferView   bv,
string const &    searchstr,
string const &    replacestr,
bool    forward,
bool    casesens = true,
bool    matchwrd = false,
bool    replaceall = false,
bool    once = false
 

Definition at line 36 of file lyxfind.C.

Referenced by ControlThesaurus::replace, and ControlSearch::replace.

00040 {
00041         if (!bv->available() || bv->buffer()->isReadonly())
00042                 return 0;
00043 
00044         // CutSelection cannot cut a single space, so we have to stop
00045         // in order to avoid endless loop :-(
00046         if (searchstr.length() == 0
00047                 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
00048 #ifdef WITH_WARNINGS
00049 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
00050 #endif
00051                 Alert::error(_("Cannot replace"),
00052                         _("You cannot replace a single space or "
00053                           "an empty character."));
00054                 return 0;
00055         }
00056 
00057         // now we can start searching for the first
00058         // start at top if replaceall
00059         LyXText * text = bv->getLyXText();
00060         bool fw = forward;
00061         if (replaceall) {
00062                 text->clearSelection();
00063                 bv->unlockInset(bv->theLockingInset());
00064                 text = bv->text;
00065                 text->cursorTop();
00066                 // override search direction because we search top to bottom
00067                 fw = true;
00068         }
00069 
00070         // if nothing selected or selection does not equal search string
00071         // search and select next occurance and return if no replaceall
00072         string str1;
00073         string str2;
00074         if (casesens) {
00075                 str1 = searchstr;
00076                 str2 = text->selectionAsString(bv->buffer(), false);
00077         } else {
00078                 str1 = lowercase(searchstr);
00079                 str2 = lowercase(text->selectionAsString(bv->buffer(), false));
00080         }
00081         if (str1 != str2) {
00082                 if (!LyXFind(bv, searchstr, fw, casesens, matchwrd) ||
00083                         !replaceall) {
00084                         return 0;
00085                 }
00086         }
00087 
00088         bool found = false;
00089         int replace_count = 0;
00090         do {
00091                 text = bv->getLyXText();
00092                 // We have to do this check only because mathed insets don't
00093                 // return their own LyXText but the LyXText of it's parent!
00094                 if (!bv->theLockingInset() ||
00095                         ((text != bv->text) &&
00096                          (text->inset_owner == text->inset_owner->getLockingInset()))) {
00097                         bv->update(text, BufferView::SELECT);
00098                         bv->toggleSelection(false);
00099                         text->replaceSelectionWithString(replacestr);
00100                         text->setSelectionRange(replacestr.length());
00101                         bv->update(text, BufferView::SELECT);
00102                         ++replace_count;
00103                 }
00104                 if (!once)
00105                         found = LyXFind(bv, searchstr, fw, casesens, matchwrd);
00106         } while (!once && replaceall && found);
00107 
00108         // FIXME: should be called via an LFUN
00109         bv->buffer()->markDirty();
00110         bv->fitCursor();
00111 
00112         return replace_count;
00113 }

SearchResult lyxfind::nextChange BufferView   bv,
LyXText   text,
lyx::pos_type   length
 

Definition at line 308 of file lyxfind.C.

00309 {
00310         ParagraphList::iterator pit = text->cursor.par();
00311         ParagraphList::iterator pend = text->ownerParagraphs().end();
00312         pos_type pos = text->cursor.pos();
00313 
00314         while (pit != pend) {
00315                 pos_type parsize = pit->size();
00316 
00317                 if (pos < parsize) {
00318                         if ((!parsize || pos != parsize)
00319                                 && pit->lookupChange(pos) != Change::UNCHANGED)
00320                                 break;
00321 
00322                         if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
00323                                 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
00324                                 if (inset->nextChange(bv, length))
00325                                         return SR_FOUND_NOUPDATE;
00326                         }
00327                 }
00328 
00329                 ++pos;
00330 
00331                 if (pos >= parsize) {
00332                         ++pit;
00333                         pos = 0;
00334                 }
00335         }
00336 
00337         if (pit == pend)
00338                 return SR_NOT_FOUND;
00339 
00340         text->setCursor(pit, pos);
00341         Change orig_change = pit->lookupChangeFull(pos);
00342         pos_type parsize = pit->size();
00343         pos_type end = pos;
00344 
00345         for (; end != parsize; ++end) {
00346                 Change change = pit->lookupChangeFull(end);
00347                 if (change != orig_change) {
00348                         // slight UI optimisation: for replacements, we get
00349                         // text like : _old_new. Consider that as one change.
00350                         if (!(orig_change.type == Change::DELETED &&
00351                                 change.type == Change::INSERTED))
00352                                 break;
00353                 }
00354         }
00355         length = end - pos;
00356         return SR_FOUND;
00357 }

SearchResult lyxfind::SearchBackward BufferView  ,
LyXText   text,
string const &    str,
bool const &    = true,
bool const &    = false
 

Definition at line 261 of file lyxfind.C.

00264 {
00265         ParagraphList::iterator pit = text->cursor.par();
00266         ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
00267         pos_type pos = text->cursor.pos();
00268 
00269         // skip past a match at the current cursor pos
00270         if (pos > 0) {
00271                 --pos;
00272         } else if (pit != pbegin) {
00273                 --pit;
00274                 pos = pit->size();
00275         } else {
00276                 return SR_NOT_FOUND;
00277         }
00278 
00279         while (true) {
00280                 if (pos < pit->size()) {
00281                         if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
00282                                 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
00283                                 if (inset->searchBackward(bv, str, cs, mw))
00284                                         return SR_FOUND_NOUPDATE;
00285                         }
00286 
00287                         if (IsStringInText(*pit, pos, str, cs, mw)) {
00288                                 text->setCursor(pit, pos);
00289                                 return SR_FOUND;
00290                         }
00291                 }
00292 
00293                 if (pos == 0 && pit == pbegin)
00294                         break;
00295 
00296                 if (pos > 0) {
00297                         --pos;
00298                 } else if (pit != pbegin) {
00299                         --pit;
00300                         pos = pit->size();
00301                 }
00302         }
00303 
00304         return SR_NOT_FOUND;
00305 }

SearchResult lyxfind::SearchForward BufferView  ,
LyXText   text,
string const &    str,
bool const &    = true,
bool const &    = false
 

if the string is found: return true and set the cursor to the new position.

Definition at line 228 of file lyxfind.C.

00230 {
00231         ParagraphList::iterator pit = text->cursor.par();
00232         ParagraphList::iterator pend = text->ownerParagraphs().end();
00233         pos_type pos = text->cursor.pos();
00234         UpdatableInset * inset;
00235 
00236         while (pit != pend && !IsStringInText(*pit, pos, str, cs, mw)) {
00237                 if (pos < pit->size()
00238                     && pit->isInset(pos)
00239                     && (inset = (UpdatableInset *)pit->getInset(pos))
00240                     && inset->isTextInset()
00241                     && inset->searchForward(bv, str, cs, mw))
00242                         return SR_FOUND_NOUPDATE;
00243 
00244                 if (++pos >= pit->size()) {
00245                         ++pit;
00246                         pos = 0;
00247                 }
00248         }
00249 
00250         if (pit != pend) {
00251                 text->setCursor(pit, pos);
00252                 return SR_FOUND;
00253         } else
00254                 return SR_NOT_FOUND;
00255 }


Generated on Fri Jul 18 01:19:12 2003 for lyx by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002