2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file lyxfind.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author John Levon
|
|
|
|
|
* \author J<EFBFBD>rgen Vigna
|
2003-11-04 12:01:15 +00:00
|
|
|
|
* \author Alfredo Braunstein
|
2003-08-23 00:17:00 +00:00
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
2001-03-06 10:20:33 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "lyxfind.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
2001-03-06 10:20:33 +00:00
|
|
|
|
#include "buffer.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
#include "BufferView.h"
|
2003-11-04 12:01:15 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "iterators.h"
|
2001-04-17 15:15:59 +00:00
|
|
|
|
#include "gettext.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
#include "lyxtext.h"
|
2003-09-16 12:12:33 +00:00
|
|
|
|
#include "paragraph.h"
|
2003-11-04 12:01:15 +00:00
|
|
|
|
#include "PosIterator.h"
|
|
|
|
|
#include "undo.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
|
|
|
|
#include "frontends/Alert.h"
|
|
|
|
|
|
2002-07-26 09:50:16 +00:00
|
|
|
|
#include "insets/insettext.h"
|
2001-03-06 10:20:33 +00:00
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
#include "support/textutils.h"
|
|
|
|
|
|
|
|
|
|
using lyx::support::lowercase;
|
|
|
|
|
using lyx::support::uppercase;
|
2003-11-04 12:01:15 +00:00
|
|
|
|
using bv_funcs::put_selection_at;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
|
|
|
|
|
2003-07-27 12:02:58 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace find {
|
2001-07-20 14:18:48 +00:00
|
|
|
|
|
2003-10-09 10:52:12 +00:00
|
|
|
|
|
2003-07-27 12:02:58 +00:00
|
|
|
|
namespace {
|
2001-07-20 14:18:48 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
class MatchString
|
2003-07-27 12:02:58 +00:00
|
|
|
|
{
|
2003-11-04 12:01:15 +00:00
|
|
|
|
public:
|
|
|
|
|
MatchString(string const & str, bool cs, bool mw)
|
|
|
|
|
: str(str), cs(cs), mw(mw) {};
|
|
|
|
|
// returns true if the specified string is at the specified position
|
|
|
|
|
bool operator()(Paragraph const & par, pos_type pos) const
|
|
|
|
|
{
|
|
|
|
|
string::size_type size = str.length();
|
|
|
|
|
pos_type i = 0;
|
|
|
|
|
pos_type parsize = par.size();
|
|
|
|
|
while ((pos + i < parsize)
|
|
|
|
|
&& (string::size_type(i) < size)
|
|
|
|
|
&& (cs ? (str[i] == par.getChar(pos + i))
|
|
|
|
|
: (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
|
|
|
|
|
++i;
|
2003-07-27 12:02:58 +00:00
|
|
|
|
}
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (size == string::size_type(i)) {
|
|
|
|
|
// if necessary, check whether string matches word
|
|
|
|
|
if (!mw)
|
|
|
|
|
return true;
|
|
|
|
|
if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
|
|
|
|
|
&& (pos + pos_type(size) >= parsize
|
|
|
|
|
|| !IsLetterCharOrDigit(par.getChar(pos + size)))) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2003-07-27 12:02:58 +00:00
|
|
|
|
}
|
2003-11-04 12:01:15 +00:00
|
|
|
|
return false;
|
2003-07-27 12:02:58 +00:00
|
|
|
|
}
|
2003-11-04 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
string str;
|
|
|
|
|
bool cs;
|
|
|
|
|
bool mw;
|
|
|
|
|
};
|
2003-07-27 12:02:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
} //namespace anon
|
2003-07-27 12:02:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
namespace {
|
2003-07-27 12:02:58 +00:00
|
|
|
|
|
2001-07-20 14:18:48 +00:00
|
|
|
|
|
2003-07-27 12:02:58 +00:00
|
|
|
|
|
2003-10-09 10:52:12 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bool findForward(PosIterator & cur, PosIterator const & end,
|
|
|
|
|
MatchString & match)
|
2001-03-06 10:20:33 +00:00
|
|
|
|
{
|
2003-11-04 12:01:15 +00:00
|
|
|
|
for (; cur != end && !match(*cur.pit(), cur.pos()); ++cur)
|
|
|
|
|
;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
return cur != end;
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-08-13 10:09:50 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bool findBackwards(PosIterator & cur, PosIterator const & beg,
|
|
|
|
|
MatchString & match)
|
|
|
|
|
{
|
|
|
|
|
if (beg == cur)
|
|
|
|
|
return false;
|
2001-07-20 14:18:48 +00:00
|
|
|
|
do {
|
2003-11-04 12:01:15 +00:00
|
|
|
|
--cur;
|
|
|
|
|
if (match(*cur.pit(), cur.pos()))
|
|
|
|
|
break;
|
|
|
|
|
} while (cur != beg);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
return match(*cur.pit(), cur.pos());
|
|
|
|
|
}
|
2003-03-19 14:45:22 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
bool findChange(PosIterator & cur, PosIterator const & end)
|
|
|
|
|
{
|
|
|
|
|
for (; cur != end; ++cur) {
|
|
|
|
|
if ((!cur.pit()->size() || !cur.at_end())
|
|
|
|
|
&& cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur != end;
|
2001-03-06 10:20:33 +00:00
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2001-03-06 10:20:33 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bool searchAllowed(BufferView * bv, string const & str)
|
2001-03-06 10:20:33 +00:00
|
|
|
|
{
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (str.empty()) {
|
|
|
|
|
Alert::error(_("Search error"), _("Search string is empty"));
|
2001-07-20 14:18:48 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (!bv->available())
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
|
2001-03-06 10:20:33 +00:00
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
} // namespace anon
|
2001-03-06 10:20:33 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
bool find(BufferView * bv, string const & searchstr,
|
|
|
|
|
bool cs, bool mw, bool fw)
|
2002-06-18 15:44:30 +00:00
|
|
|
|
{
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (!searchAllowed(bv, searchstr))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
PosIterator cur = PosIterator(*bv);
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
MatchString match(searchstr, cs, mw);
|
|
|
|
|
|
|
|
|
|
bool found;
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (fw) {
|
|
|
|
|
PosIterator const end = bv->buffer()->pos_iterator_end();
|
|
|
|
|
found = findForward(cur, end, match);
|
|
|
|
|
} else {
|
|
|
|
|
PosIterator const beg = bv->buffer()->pos_iterator_begin();
|
|
|
|
|
found = findBackwards(cur, beg, match);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
|
put_selection_at(bv, cur, searchstr.length(), !fw);
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
return found;
|
2002-06-18 15:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
2001-03-06 10:20:33 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
} //namespace anon
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
int replaceAll(BufferView * bv,
|
|
|
|
|
string const & searchstr, string const & replacestr,
|
|
|
|
|
bool cs, bool mw)
|
2003-02-08 19:18:01 +00:00
|
|
|
|
{
|
2003-11-04 12:01:15 +00:00
|
|
|
|
Buffer & buf = *bv->buffer();
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (!searchAllowed(bv, searchstr) || buf.isReadonly())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
recordUndo(Undo::ATOMIC, bv->text, 0,
|
|
|
|
|
buf.paragraphs().size() - 1);
|
|
|
|
|
|
|
|
|
|
PosIterator cur = buf.pos_iterator_begin();
|
|
|
|
|
PosIterator const end = buf.pos_iterator_end();
|
|
|
|
|
MatchString match(searchstr, cs, mw);
|
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
|
|
int const rsize = replacestr.size();
|
|
|
|
|
int const ssize = searchstr.size();
|
|
|
|
|
while (findForward(cur, end, match)) {
|
|
|
|
|
pos_type pos = cur.pos();
|
|
|
|
|
LyXFont const font
|
|
|
|
|
= cur.pit()->getFontSettings(buf.params(), pos);
|
|
|
|
|
int striked = ssize - cur.pit()->erase(pos, pos + ssize);
|
|
|
|
|
cur.pit()->insert(pos, replacestr, font);
|
|
|
|
|
advance(cur, rsize + striked);
|
|
|
|
|
++num;
|
|
|
|
|
}
|
|
|
|
|
PosIterator beg = buf.pos_iterator_begin();
|
|
|
|
|
bv->text->init(bv);
|
|
|
|
|
put_selection_at(bv, beg, 0, false);
|
2003-11-06 10:10:43 +00:00
|
|
|
|
if (num)
|
|
|
|
|
buf.markDirty();
|
2003-11-04 12:01:15 +00:00
|
|
|
|
return num;
|
|
|
|
|
}
|
2003-04-16 00:39:24 +00:00
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
int replace(BufferView * bv,
|
|
|
|
|
string const & searchstr, string const & replacestr,
|
|
|
|
|
bool cs, bool mw, bool fw)
|
|
|
|
|
{
|
|
|
|
|
if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
LyXText * text = bv->getLyXText();
|
|
|
|
|
// if nothing selected or selection does not equal search
|
|
|
|
|
// string search and select next occurance and return
|
|
|
|
|
string const str1 = searchstr;
|
|
|
|
|
string const str2 = text->selectionAsString(*bv->buffer(),
|
|
|
|
|
false);
|
|
|
|
|
if ((cs && str1 != str2)
|
|
|
|
|
|| lowercase(str1) != lowercase(str2)) {
|
|
|
|
|
find(bv, searchstr, cs, mw, fw);
|
|
|
|
|
return 0;
|
2003-02-08 19:18:01 +00:00
|
|
|
|
}
|
2003-11-04 12:01:15 +00:00
|
|
|
|
}
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
LyXText * text = bv->getLyXText();
|
|
|
|
|
// We have to do this check only because mathed insets don't
|
|
|
|
|
// return their own LyXText but the LyXText of it's parent!
|
|
|
|
|
if (!bv->theLockingInset() ||
|
|
|
|
|
((text != bv->text) &&
|
|
|
|
|
(text->inset_owner == text->inset_owner->getLockingInset()))) {
|
|
|
|
|
text->replaceSelectionWithString(replacestr);
|
|
|
|
|
text->setSelectionRange(replacestr.length());
|
|
|
|
|
text->cursor = fw ? text->selection.end
|
|
|
|
|
: text->selection.start;
|
2003-02-08 19:18:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
// FIXME: should be called via an LFUN
|
|
|
|
|
bv->buffer()->markDirty();
|
|
|
|
|
|
|
|
|
|
find(bv, searchstr, cs, mw, fw);
|
|
|
|
|
bv->update();
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool findNextChange(BufferView * bv)
|
|
|
|
|
{
|
|
|
|
|
if (!bv->available())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
PosIterator cur = PosIterator(*bv);
|
|
|
|
|
PosIterator const endit = bv->buffer()->pos_iterator_end();
|
2003-04-16 00:39:24 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (!findChange(cur, endit))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ParagraphList::iterator pit = cur.pit();
|
|
|
|
|
pos_type pos = cur.pos();
|
|
|
|
|
|
2003-04-16 00:39:24 +00:00
|
|
|
|
Change orig_change = pit->lookupChangeFull(pos);
|
|
|
|
|
pos_type parsize = pit->size();
|
|
|
|
|
pos_type end = pos;
|
|
|
|
|
|
|
|
|
|
for (; end != parsize; ++end) {
|
|
|
|
|
Change change = pit->lookupChangeFull(end);
|
|
|
|
|
if (change != orig_change) {
|
|
|
|
|
// slight UI optimisation: for replacements, we get
|
|
|
|
|
// text like : _old_new. Consider that as one change.
|
|
|
|
|
if (!(orig_change.type == Change::DELETED &&
|
|
|
|
|
change.type == Change::INSERTED))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-02-08 19:18:01 +00:00
|
|
|
|
}
|
2003-11-04 12:01:15 +00:00
|
|
|
|
pos_type length = end - pos;
|
|
|
|
|
bv->text->init(bv);
|
|
|
|
|
put_selection_at(bv, cur, length, true);
|
|
|
|
|
return true;
|
2003-02-08 19:18:01 +00:00
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2003-07-27 12:02:58 +00:00
|
|
|
|
} // find namespace
|
|
|
|
|
} // lyx namespace
|