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"
|
2004-01-14 17:21:39 +00:00
|
|
|
|
#include "cursor_slice.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"
|
2004-01-07 17:00:03 +00:00
|
|
|
|
#include "funcrequest.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"
|
2004-01-07 17:00:03 +00:00
|
|
|
|
#include "frontends/LyXView.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
|
|
|
|
#include "support/textutils.h"
|
2004-01-07 17:00:03 +00:00
|
|
|
|
#include "support/tostr.h"
|
|
|
|
|
|
|
|
|
|
#include "support/std_sstream.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
|
|
|
|
using lyx::support::lowercase;
|
|
|
|
|
using lyx::support::uppercase;
|
2004-01-07 17:00:03 +00:00
|
|
|
|
using lyx::support::split;
|
|
|
|
|
|
|
|
|
|
using std::ostringstream;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
|
|
|
|
|
2003-11-10 09:06:48 +00:00
|
|
|
|
|
2004-01-07 17:00:03 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
bool parse_bool(string & howto)
|
|
|
|
|
{
|
|
|
|
|
if (howto.empty())
|
|
|
|
|
return false;
|
|
|
|
|
string var;
|
|
|
|
|
howto = split(howto, var, ' ');
|
|
|
|
|
return (var == "1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool find(BufferView * bv,
|
|
|
|
|
string const & searchstr, bool cs, bool mw, bool fw);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int replace(BufferView * bv,
|
|
|
|
|
string const & searchstr, string const & replacestr,
|
|
|
|
|
bool cs, bool mw, bool fw);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int replaceAll(BufferView * bv,
|
|
|
|
|
string const & searchstr, string const & replacestr,
|
|
|
|
|
bool cs, bool mw);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool findChange(PosIterator & cur, PosIterator const & end);
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
2003-07-27 12:02:58 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace find {
|
2001-07-20 14:18:48 +00:00
|
|
|
|
|
2004-01-07 17:00:03 +00:00
|
|
|
|
string const find2string(string const & search,
|
|
|
|
|
bool casesensitive, bool matchword, bool forward)
|
|
|
|
|
{
|
|
|
|
|
ostringstream ss;
|
|
|
|
|
ss << search << '\n'
|
|
|
|
|
<< int(casesensitive) << ' '
|
|
|
|
|
<< int(matchword) << ' '
|
|
|
|
|
<< int(forward);
|
|
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const replace2string(string const & search, string const & replace,
|
|
|
|
|
bool casesensitive, bool matchword,
|
|
|
|
|
bool all, bool forward)
|
|
|
|
|
{
|
|
|
|
|
ostringstream ss;
|
|
|
|
|
ss << search << '\n'
|
|
|
|
|
<< replace << '\n'
|
|
|
|
|
<< int(casesensitive) << ' '
|
|
|
|
|
<< int(matchword) << ' '
|
|
|
|
|
<< int(all) << ' '
|
|
|
|
|
<< int(forward);
|
|
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void find(FuncRequest const & ev)
|
|
|
|
|
{
|
|
|
|
|
if (!ev.view() || ev.action != LFUN_WORD_FIND)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// data is of the form
|
|
|
|
|
// "<search>
|
|
|
|
|
// <casesensitive> <matchword> <forward>"
|
|
|
|
|
string search;
|
|
|
|
|
string howto = split(ev.argument, search, '\n');
|
|
|
|
|
|
|
|
|
|
bool casesensitive = parse_bool(howto);
|
|
|
|
|
bool matchword = parse_bool(howto);
|
|
|
|
|
bool forward = parse_bool(howto);
|
|
|
|
|
|
|
|
|
|
BufferView * bv = ev.view();
|
|
|
|
|
bool const found = ::find(bv, search,
|
|
|
|
|
forward, casesensitive, matchword);
|
|
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
|
bv->owner()->message(_("String not found!"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void replace(FuncRequest const & ev)
|
|
|
|
|
{
|
|
|
|
|
if (!ev.view() || ev.action != LFUN_WORD_REPLACE)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// data is of the form
|
|
|
|
|
// "<search>
|
|
|
|
|
// <replace>
|
|
|
|
|
// <casesensitive> <matchword> <all> <forward>"
|
|
|
|
|
string search;
|
|
|
|
|
string replace;
|
|
|
|
|
string howto = split(ev.argument, search, '\n');
|
|
|
|
|
howto = split(howto, replace, '\n');
|
|
|
|
|
|
|
|
|
|
bool casesensitive = parse_bool(howto);
|
|
|
|
|
bool matchword = parse_bool(howto);
|
|
|
|
|
bool all = parse_bool(howto);
|
|
|
|
|
bool forward = parse_bool(howto);
|
|
|
|
|
|
|
|
|
|
BufferView * bv = ev.view();
|
|
|
|
|
LyXView * lv = bv->owner();
|
|
|
|
|
|
|
|
|
|
int const replace_count = all ?
|
|
|
|
|
::replaceAll(bv, search, replace,
|
|
|
|
|
casesensitive, matchword) :
|
|
|
|
|
::replace(bv, search, replace,
|
|
|
|
|
casesensitive, matchword, forward);
|
|
|
|
|
|
|
|
|
|
if (replace_count == 0) {
|
|
|
|
|
lv->message(_("String not found!"));
|
|
|
|
|
} else {
|
|
|
|
|
if (replace_count == 1) {
|
|
|
|
|
lv->message(_("String has been replaced."));
|
|
|
|
|
} else {
|
|
|
|
|
string str = tostr(replace_count);
|
|
|
|
|
str += _(" strings have been replaced.");
|
|
|
|
|
lv->message(str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool findNextChange(BufferView * bv)
|
|
|
|
|
{
|
|
|
|
|
if (!bv->available())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
PosIterator cur = PosIterator(*bv);
|
|
|
|
|
PosIterator const endit = bv->buffer()->pos_iterator_end();
|
|
|
|
|
|
|
|
|
|
if (!findChange(cur, endit))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ParagraphList::iterator pit = cur.pit();
|
|
|
|
|
pos_type pos = cur.pos();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pos_type length = end - pos;
|
2004-01-14 17:21:39 +00:00
|
|
|
|
bv->putSelectionAt(cur, length, true);
|
2004-01-07 17:00:03 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // find namespace
|
|
|
|
|
} // lyx namespace
|
|
|
|
|
|
|
|
|
|
|
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)
|
2004-01-07 14:07:46 +00:00
|
|
|
|
: str(str), cs(cs), mw(mw)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
// returns true if the specified string is at the specified position
|
2004-01-07 17:00:03 +00:00
|
|
|
|
bool operator()(Paragraph const & par, lyx::pos_type pos) const
|
|
|
|
|
{
|
|
|
|
|
string::size_type const size = str.length();
|
|
|
|
|
lyx::pos_type i = 0;
|
|
|
|
|
lyx::pos_type const parsize = par.size();
|
2003-11-04 12:01:15 +00:00
|
|
|
|
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
|
|
|
|
}
|
2004-01-07 14:07:46 +00:00
|
|
|
|
|
|
|
|
|
if (size != string::size_type(i))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// if necessary, check whether string matches word
|
|
|
|
|
if (mw) {
|
|
|
|
|
if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
|
|
|
|
|
return false;
|
2004-01-07 17:00:03 +00:00
|
|
|
|
if (pos + lyx::pos_type(size) < parsize
|
2004-01-07 14:07:46 +00:00
|
|
|
|
&& IsLetterCharOrDigit(par.getChar(pos + size)));
|
|
|
|
|
return false;
|
2003-07-27 12:02:58 +00:00
|
|
|
|
}
|
2004-01-07 14:07:46 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
2003-07-27 12:02:58 +00:00
|
|
|
|
}
|
2003-11-04 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2004-01-07 14:07:46 +00:00
|
|
|
|
// search string
|
2003-11-04 12:01:15 +00:00
|
|
|
|
string str;
|
2004-01-07 14:07:46 +00:00
|
|
|
|
// case sensitive
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bool cs;
|
2004-01-07 14:07:46 +00:00
|
|
|
|
// match whole words only
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bool mw;
|
|
|
|
|
};
|
2003-07-27 12:02:58 +00:00
|
|
|
|
|
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bool findForward(PosIterator & cur, PosIterator const & end,
|
2003-11-17 09:02:10 +00:00
|
|
|
|
MatchString const & match)
|
2001-03-06 10:20:33 +00:00
|
|
|
|
{
|
2003-11-17 09:02:10 +00:00
|
|
|
|
for (; cur != end; ++cur) {
|
|
|
|
|
if (match(*cur.pit(), cur.pos()))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2003-11-04 12:01:15 +00:00
|
|
|
|
}
|
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,
|
2003-11-17 09:02:10 +00:00
|
|
|
|
MatchString const & match)
|
2003-11-04 12:01:15 +00:00
|
|
|
|
{
|
2003-11-17 09:02:10 +00:00
|
|
|
|
while (beg != cur) {
|
2003-11-04 12:01:15 +00:00
|
|
|
|
--cur;
|
|
|
|
|
if (match(*cur.pit(), cur.pos()))
|
2003-11-17 09:02:10 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2003-11-04 12:01:15 +00:00
|
|
|
|
}
|
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)
|
2003-11-17 09:02:10 +00:00
|
|
|
|
return true;
|
2003-11-04 12:01:15 +00:00
|
|
|
|
}
|
2003-11-17 09:02:10 +00:00
|
|
|
|
return false;
|
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-10 09:06:48 +00:00
|
|
|
|
return bv->available();
|
2001-03-06 10:20:33 +00:00
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
2003-11-10 09:06:48 +00:00
|
|
|
|
|
2004-01-07 14:07:46 +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-17 09:02:10 +00:00
|
|
|
|
MatchString const match(searchstr, cs, mw);
|
|
|
|
|
|
|
|
|
|
PosIterator const end = bv->buffer()->pos_iterator_end();
|
|
|
|
|
PosIterator const beg = bv->buffer()->pos_iterator_begin();
|
|
|
|
|
|
|
|
|
|
bool found = fw ? findForward(cur, end, match)
|
|
|
|
|
: findBackwards(cur, beg, match);
|
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
if (found)
|
2004-01-14 17:21:39 +00:00
|
|
|
|
bv->putSelectionAt(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-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;
|
|
|
|
|
|
2003-11-28 08:55:12 +00:00
|
|
|
|
recordUndo(Undo::ATOMIC, bv->text(), 0, buf.paragraphs().size() - 1);
|
2003-11-04 12:01:15 +00:00
|
|
|
|
|
|
|
|
|
PosIterator cur = buf.pos_iterator_begin();
|
|
|
|
|
PosIterator const end = buf.pos_iterator_end();
|
2003-11-17 09:02:10 +00:00
|
|
|
|
MatchString const match(searchstr, cs, mw);
|
2003-11-04 12:01:15 +00:00
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
|
|
int const rsize = replacestr.size();
|
|
|
|
|
int const ssize = searchstr.size();
|
2003-11-17 09:02:10 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
while (findForward(cur, end, match)) {
|
2004-01-07 17:00:03 +00:00
|
|
|
|
lyx::pos_type pos = cur.pos();
|
2003-11-04 12:01:15 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2003-11-17 09:02:10 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
PosIterator beg = buf.pos_iterator_begin();
|
2003-11-28 08:55:12 +00:00
|
|
|
|
bv->text()->init(bv);
|
2004-01-14 17:21:39 +00:00
|
|
|
|
bv->putSelectionAt(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-17 09:02:10 +00:00
|
|
|
|
bool stringSelected(BufferView * bv,
|
|
|
|
|
string const & searchstr,
|
|
|
|
|
bool cs, bool mw, bool fw)
|
|
|
|
|
{
|
|
|
|
|
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);
|
2003-11-28 08:55:12 +00:00
|
|
|
|
if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
|
2003-11-17 09:02:10 +00:00
|
|
|
|
find(bv, searchstr, cs, mw, fw);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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;
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
2003-11-17 09:02:10 +00:00
|
|
|
|
if (!stringSelected(bv, searchstr, cs, mw, fw))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
LyXText * text = bv->getLyXText();
|
2003-11-17 09:02:10 +00:00
|
|
|
|
|
|
|
|
|
text->replaceSelectionWithString(replacestr);
|
|
|
|
|
text->setSelectionRange(replacestr.length());
|
2004-01-14 15:09:01 +00:00
|
|
|
|
bv->cursor() = fw ? bv->selEnd() : bv->selStart();
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bv->buffer()->markDirty();
|
|
|
|
|
find(bv, searchstr, cs, mw, fw);
|
|
|
|
|
bv->update();
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-01-07 17:00:03 +00:00
|
|
|
|
} //namespace anon
|