Ignore ligature breaks and hyphenations during simple search (fixes #1468).

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@36427 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Tommaso Cucinotta 2010-11-22 07:50:34 +00:00
parent 1afbf18168
commit 5e5122699d
3 changed files with 51 additions and 35 deletions

View File

@ -51,6 +51,7 @@
#include "insets/InsetBibitem.h"
#include "insets/InsetLabel.h"
#include "insets/InsetSpecialChar.h"
#include "support/debug.h"
#include "support/docstring_list.h"
@ -3258,36 +3259,44 @@ void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
}
bool Paragraph::find(docstring const & str, bool cs, bool mw,
pos_type pos, bool del) const
int Paragraph::find(docstring const & str, bool cs, bool mw,
pos_type start_pos, bool del) const
{
pos_type pos = start_pos;
int const strsize = str.length();
int i = 0;
pos_type const parsize = d->text_.size();
for (i = 0; pos + i < parsize; ++i) {
if (i >= strsize)
for (i = 0; i < strsize && pos < parsize; ++i, ++pos) {
// Ignore ligature break and hyphenation chars while searching
while (pos < parsize - 1 && isInset(pos)) {
const InsetSpecialChar *isc = dynamic_cast<const InsetSpecialChar*>(getInset(pos));
if (isc == 0
|| (isc->kind() != InsetSpecialChar::HYPHENATION
&& isc->kind() != InsetSpecialChar::LIGATURE_BREAK))
break;
pos++;
}
if (cs && str[i] != d->text_[pos])
break;
if (cs && str[i] != d->text_[pos + i])
if (!cs && uppercase(str[i]) != uppercase(d->text_[pos]))
break;
if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
break;
if (!del && isDeleted(pos + i))
if (!del && isDeleted(pos))
break;
}
if (i != strsize)
return false;
return 0;
// if necessary, check whether string matches word
if (mw) {
if (pos > 0 && !isWordSeparator(pos - 1))
return false;
if (pos + strsize < parsize
&& !isWordSeparator(pos + strsize))
return false;
if (start_pos > 0 && !isWordSeparator(start_pos - 1))
return 0;
if (pos < parsize
&& !isWordSeparator(pos))
return 0;
}
return true;
return pos - start_pos;
}

View File

@ -424,9 +424,10 @@ public:
pos_type & right, TextCase action);
/// find \param str string inside Paragraph.
/// \return true if the specified string is at the specified position
/// \return non-zero if the specified string is at the specified
/// position; returned value is the actual match length in positions
/// \param del specifies whether deleted strings in ct mode will be considered
bool find(
int find(
docstring const & str, ///< string to search
bool cs, ///<
bool mw, ///<

View File

@ -67,7 +67,7 @@ bool parse_bool(docstring & howto)
}
class MatchString : public binary_function<Paragraph, pos_type, bool>
class MatchString : public binary_function<Paragraph, pos_type, int>
{
public:
MatchString(docstring const & str, bool cs, bool mw)
@ -76,7 +76,7 @@ public:
// returns true if the specified string is at the specified position
// del specifies whether deleted strings in ct mode will be considered
bool operator()(Paragraph const & par, pos_type pos, bool del = true) const
int operator()(Paragraph const & par, pos_type pos, bool del = true) const
{
return par.find(str, case_sens, whole_words, pos, del);
}
@ -91,27 +91,31 @@ private:
};
bool findForward(DocIterator & cur, MatchString const & match,
int findForward(DocIterator & cur, MatchString const & match,
bool find_del = true)
{
for (; cur; cur.forwardChar())
if (cur.inTexted() &&
match(cur.paragraph(), cur.pos(), find_del))
return true;
return false;
if (cur.inTexted()) {
int len = match(cur.paragraph(), cur.pos(), find_del);
if (len > 0)
return len;
}
return 0;
}
bool findBackwards(DocIterator & cur, MatchString const & match,
int findBackwards(DocIterator & cur, MatchString const & match,
bool find_del = true)
{
while (cur) {
cur.backwardChar();
if (cur.inTexted() &&
match(cur.paragraph(), cur.pos(), find_del))
return true;
if (cur.inTexted()) {
int len = match(cur.paragraph(), cur.pos(), find_del);
if (len > 0)
return len;
}
}
return false;
return 0;
}
@ -152,13 +156,13 @@ bool findOne(BufferView * bv, docstring const & searchstr,
MatchString const match(searchstr, case_sens, whole);
bool found = forward ? findForward(cur, match, find_del) :
int match_len = forward ? findForward(cur, match, find_del) :
findBackwards(cur, match, find_del);
if (found)
bv->putSelectionAt(cur, searchstr.length(), !forward);
if (match_len > 0)
bv->putSelectionAt(cur, match_len, !forward);
return found;
return match_len > 0;
}
@ -181,12 +185,13 @@ int replaceAll(BufferView * bv,
Cursor cur(*bv);
cur.setCursor(doc_iterator_begin(&buf));
while (findForward(cur, match, false)) {
int match_len = findForward(cur, match, false);
while (match_len > 0) {
// Backup current cursor position and font.
pos_type const pos = cur.pos();
Font const font = cur.paragraph().getFontSettings(buf.params(), pos);
cur.recordUndo();
int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
int striked = ssize - cur.paragraph().eraseChars(pos, pos + match_len,
buf.params().trackChanges);
cur.paragraph().insert(pos, replacestr, font,
Change(buf.params().trackChanges ?
@ -194,6 +199,7 @@ int replaceAll(BufferView * bv,
for (int i = 0; i < rsize + striked; ++i)
cur.forwardChar();
++num;
match_len = findForward(cur, match, false);
}
bv->putSelectionAt(doc_iterator_begin(&buf), 0, false);