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-20 14:25:24 +00:00
|
|
|
|
#include "cursor.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
|
#include "CutAndPaste.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
#include "BufferView.h"
|
2003-11-04 12:01:15 +00:00
|
|
|
|
#include "debug.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"
|
2004-03-28 22:00:22 +00:00
|
|
|
|
#include "pariterator.h"
|
2003-11-04 12:01:15 +00:00
|
|
|
|
#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
|
|
|
|
|
2005-01-06 16:39:35 +00:00
|
|
|
|
#include "support/convert.h"
|
2004-01-07 17:00:03 +00:00
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <sstream>
|
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;
|
|
|
|
|
|
2004-11-24 21:53:46 +00:00
|
|
|
|
using lyx::pit_type;
|
2004-03-25 09:16:36 +00:00
|
|
|
|
using lyx::pos_type;
|
|
|
|
|
|
2004-01-28 16:21:29 +00:00
|
|
|
|
using std::advance;
|
2004-01-07 17:00:03 +00:00
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-31 15:30:24 +00:00
|
|
|
|
class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
|
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();
|
2004-02-11 14:45:44 +00:00
|
|
|
|
for (i = 0; pos + i < parsize; ++i) {
|
|
|
|
|
if (string::size_type(i) >= size)
|
|
|
|
|
break;
|
2004-04-01 08:58:45 +00:00
|
|
|
|
if (cs && str[i] != par.getChar(pos + i))
|
2004-02-11 14:45:44 +00:00
|
|
|
|
break;
|
|
|
|
|
if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
|
|
|
|
|
break;
|
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) {
|
2004-11-18 14:58:54 +00:00
|
|
|
|
if (pos > 0 && par.isLetter(pos - 1))
|
2004-01-07 14:07:46 +00:00
|
|
|
|
return false;
|
2004-01-07 17:00:03 +00:00
|
|
|
|
if (pos + lyx::pos_type(size) < parsize
|
2005-01-20 14:38:45 +00:00
|
|
|
|
&& par.isLetter(pos + size))
|
2004-01-07 14:07:46 +00:00
|
|
|
|
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
|
|
|
|
}
|
2004-01-26 17:00:09 +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
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
bool findForward(DocIterator & cur, MatchString const & match)
|
2001-03-06 10:20:33 +00:00
|
|
|
|
{
|
2004-04-01 08:58:45 +00:00
|
|
|
|
for (; cur; cur.forwardChar())
|
|
|
|
|
if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
|
2003-11-17 09:02:10 +00:00
|
|
|
|
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
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
bool findBackwards(DocIterator & cur, MatchString const & match)
|
2003-11-04 12:01:15 +00:00
|
|
|
|
{
|
2004-09-17 16:28:47 +00:00
|
|
|
|
while (cur) {
|
|
|
|
|
cur.backwardChar();
|
2004-04-01 08:58:45 +00:00
|
|
|
|
if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
|
2003-11-17 09:02:10 +00:00
|
|
|
|
return true;
|
2004-09-17 16:28:47 +00:00
|
|
|
|
}
|
2003-11-17 09:02:10 +00:00
|
|
|
|
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
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
bool findChange(DocIterator & cur)
|
2003-11-04 12:01:15 +00:00
|
|
|
|
{
|
2006-03-11 13:31:41 +00:00
|
|
|
|
for (; cur; cur.forwardPos())
|
2006-05-08 20:30:52 +00:00
|
|
|
|
if (cur.inTexted() && cur.paragraph().lookupChange(cur.pos()).type
|
2004-04-01 08:58:45 +00:00
|
|
|
|
!= Change::UNCHANGED)
|
2003-11-17 09:02:10 +00:00
|
|
|
|
return true;
|
|
|
|
|
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;
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator cur = bv->cursor();
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
2003-11-17 09:02:10 +00:00
|
|
|
|
MatchString const match(searchstr, cs, mw);
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
|
2003-11-17 09:02:10 +00:00
|
|
|
|
|
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;
|
2004-01-26 17:00:09 +00:00
|
|
|
|
|
2005-07-14 22:09:22 +00:00
|
|
|
|
recordUndoFullDocument(bv);
|
2004-01-26 17:00:09 +00:00
|
|
|
|
|
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
|
|
|
|
|
2004-04-01 08:58:45 +00:00
|
|
|
|
DocIterator cur = doc_iterator_begin(buf.inset());
|
2004-03-25 09:16:36 +00:00
|
|
|
|
while (findForward(cur, 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
|
2004-03-25 09:16:36 +00:00
|
|
|
|
= cur.paragraph().getFontSettings(buf.params(), pos);
|
|
|
|
|
int striked = ssize - cur.paragraph().erase(pos, pos + ssize);
|
|
|
|
|
cur.paragraph().insert(pos, replacestr, font);
|
|
|
|
|
for (int i = 0; i < rsize + striked; ++i)
|
|
|
|
|
cur.forwardChar();
|
2003-11-04 12:01:15 +00:00
|
|
|
|
++num;
|
|
|
|
|
}
|
2003-11-17 09:02:10 +00:00
|
|
|
|
|
2003-11-28 08:55:12 +00:00
|
|
|
|
bv->text()->init(bv);
|
2004-04-01 08:58:45 +00:00
|
|
|
|
bv->putSelectionAt(doc_iterator_begin(buf.inset()), 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
|
|
|
|
|
2004-02-11 14:45:44 +00:00
|
|
|
|
bool stringSelected(BufferView * bv, string const & searchstr,
|
2003-11-17 09:02:10 +00:00
|
|
|
|
bool cs, bool mw, bool fw)
|
|
|
|
|
{
|
|
|
|
|
// if nothing selected or selection does not equal search
|
|
|
|
|
// string search and select next occurance and return
|
|
|
|
|
string const & str1 = searchstr;
|
2004-02-11 14:45:44 +00:00
|
|
|
|
string const str2 = bv->cursor().selectionAsString(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-11 14:45:44 +00:00
|
|
|
|
int replace(BufferView * bv, string const & searchstr,
|
2004-10-05 10:11:42 +00:00
|
|
|
|
string const & replacestr, bool cs, bool mw, bool fw)
|
2003-11-04 12:01:15 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
LCursor & cur = bv->cursor();
|
|
|
|
|
lyx::cap::replaceSelectionWithString(cur, replacestr);
|
|
|
|
|
lyx::cap::setSelectionRange(cur, replacestr.length());
|
|
|
|
|
cur.top() = fw ? cur.selEnd() : cur.selBegin();
|
2003-11-04 12:01:15 +00:00
|
|
|
|
bv->buffer()->markDirty();
|
|
|
|
|
find(bv, searchstr, cs, mw, fw);
|
|
|
|
|
bv->update();
|
2004-01-26 17:00:09 +00:00
|
|
|
|
|
2003-11-04 12:01:15 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace find {
|
|
|
|
|
|
|
|
|
|
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(BufferView * bv, FuncRequest const & ev)
|
|
|
|
|
{
|
|
|
|
|
if (!bv || ev.action != LFUN_WORD_FIND)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
lyxerr << "find called, cmd: " << ev << std::endl;
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
bool const found = ::find(bv, search,
|
|
|
|
|
casesensitive, matchword, forward);
|
|
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
|
bv->owner()->message(_("String not found!"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void replace(BufferView * bv, FuncRequest const & ev)
|
|
|
|
|
{
|
|
|
|
|
if (!bv || 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);
|
|
|
|
|
|
|
|
|
|
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 {
|
2005-01-06 15:40:49 +00:00
|
|
|
|
string str = convert<string>(replace_count);
|
2004-03-25 09:16:36 +00:00
|
|
|
|
str += _(" strings have been replaced.");
|
|
|
|
|
lv->message(str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool findNextChange(BufferView * bv)
|
|
|
|
|
{
|
|
|
|
|
if (!bv->available())
|
|
|
|
|
return false;
|
|
|
|
|
|
2005-09-07 10:00:19 +00:00
|
|
|
|
DocIterator cur = bv->cursor();
|
2004-03-25 09:16:36 +00:00
|
|
|
|
|
|
|
|
|
if (!findChange(cur))
|
|
|
|
|
return false;
|
|
|
|
|
|
2006-03-11 13:31:41 +00:00
|
|
|
|
bv->cursor().setCursor(cur);
|
|
|
|
|
bv->cursor().resetAnchor();
|
2006-04-05 23:56:29 +00:00
|
|
|
|
|
2006-05-05 23:11:19 +00:00
|
|
|
|
Change orig_change = cur.paragraph().lookupChange(cur.pos());
|
2004-03-25 09:16:36 +00:00
|
|
|
|
|
2006-03-11 13:31:41 +00:00
|
|
|
|
DocIterator et = doc_iterator_end(cur.inset());
|
|
|
|
|
for (; cur != et; cur.forwardPosNoDescend()) {
|
2006-05-05 23:11:19 +00:00
|
|
|
|
Change change = cur.paragraph().lookupChange(cur.pos());
|
2004-03-25 09:16:36 +00:00
|
|
|
|
if (change != orig_change) {
|
2006-03-11 13:31:41 +00:00
|
|
|
|
break;
|
2004-03-25 09:16:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-03-11 13:31:41 +00:00
|
|
|
|
// Now put cursor to end of selection:
|
|
|
|
|
bv->cursor().setCursor(cur);
|
|
|
|
|
bv->cursor().setSelection();
|
2004-11-22 14:56:14 +00:00
|
|
|
|
// if we used a lfun like in find/replace, dispatch would do
|
|
|
|
|
// that for us
|
|
|
|
|
bv->update();
|
2004-09-17 16:28:47 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // find namespace
|
|
|
|
|
} // lyx namespace
|