Hm, there is not too much completely independent stuff in there after all...
Andre' -- Those who desire to give up Freedom in order to gain Security, will not have, nor do they deserve, either one. (T. Jefferson or B. Franklin or both...)
Index: text.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text.C,v retrieving revision 1.370 diff -u -p -r1.370 text.C --- text.C 27 Jun 2003 08:38:37 -0000 1.370 +++ text.C 27 Jun 2003 15:44:56 -0000 @@ -30,6 +30,7 @@ #include "language.h" #include "ParagraphParameters.h" #include "undo_funcs.h" +#include "text_funcs.h" #include "WordLangTuple.h" #include "paragraph_funcs.h" #include "rowpainter.h" @@ -47,7 +48,10 @@ using std::max; using std::min; using std::endl; using std::pair; + using lyx::pos_type; +using lyx::word_location; + using namespace bv_funcs; /// top, right, bottom pixel margin @@ -1974,27 +1978,8 @@ void LyXText::prepareToPrint(RowList::it void LyXText::cursorRightOneWord() { - // treat floats, HFills and Insets as words - ParagraphList::iterator pit = cursor.par(); - pos_type pos = cursor.pos(); - - // CHECK See comment on top of text.C - - if (pos == pit->size() - && boost::next(pit) != ownerParagraphs().end()) { - ++pit; - pos = 0; - } else { - // Skip through initial nonword stuff. - while (pos < pit->size() && !pit->isWord(pos)) { - ++pos; - } - // Advance through word. - while (pos < pit->size() && pit->isWord(pos)) { - ++pos; - } - } - setCursor(pit, pos); + ::cursorRightOneWord(cursor, ownerParagraphs()); + setCursor(cursor.par(), cursor.pos()); } @@ -2003,102 +1988,16 @@ void LyXText::cursorRightOneWord() void LyXText::cursorLeftOneWord() { LyXCursor tmpcursor = cursor; - cursorLeftOneWord(tmpcursor); + ::cursorLeftOneWord(tmpcursor, ownerParagraphs()); setCursor(tmpcursor.par(), tmpcursor.pos()); } -void LyXText::cursorLeftOneWord(LyXCursor & cur) -{ - // treat HFills, floats and Insets as words - - ParagraphList::iterator pit = cursor.par(); - pos_type pos = cursor.pos(); - - while (pos && - (pit->isSeparator(pos - 1) || - pit->isKomma(pos - 1) || - pit->isNewline(pos - 1)) && - !(pit->isHfill(pos - 1) || - pit->isInset(pos - 1))) - --pos; - - if (pos && - (pit->isInset(pos - 1) || - pit->isHfill(pos - 1))) { - --pos; - } else if (!pos) { - if (pit != ownerParagraphs().begin()) { - --pit; - pos = pit->size(); - } - } else { // Here, cur != 0 - while (pos > 0 && - pit->isWord(pos - 1)) - --pos; - } - - cur.par(pit); - cur.pos(pos); -} - - -// Select current word. This depends on behaviour of -// CursorLeftOneWord(), so it is patched as well. -void LyXText::getWord(LyXCursor & from, LyXCursor & to, - word_location const loc) -{ - // first put the cursor where we wana start to select the word - from = cursor; - switch (loc) { - case WHOLE_WORD_STRICT: - if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size() - || cursor.par()->isSeparator(cursor.pos()) - || cursor.par()->isKomma(cursor.pos()) - || cursor.par()->isNewline(cursor.pos()) - || cursor.par()->isSeparator(cursor.pos() - 1) - || cursor.par()->isKomma(cursor.pos() - 1) - || cursor.par()->isNewline(cursor.pos() - 1)) { - to = from; - return; - } - // no break here, we go to the next - - case WHOLE_WORD: - // Move cursor to the beginning, when not already there. - if (from.pos() && !from.par()->isSeparator(from.pos() - 1) - && !(from.par()->isKomma(from.pos() - 1) - || from.par()->isNewline(from.pos() - 1))) - cursorLeftOneWord(from); - break; - case PREVIOUS_WORD: - // always move the cursor to the beginning of previous word - cursorLeftOneWord(from); - break; - case NEXT_WORD: - lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n"; - break; - case PARTIAL_WORD: - break; - } - to = from; - while (to.pos() < to.par()->size() - && !to.par()->isSeparator(to.pos()) - && !to.par()->isKomma(to.pos()) - && !to.par()->isNewline(to.pos()) - && !to.par()->isHfill(to.pos()) - && !to.par()->isInset(to.pos())) - { - to.pos(to.pos() + 1); - } -} - - void LyXText::selectWord(word_location loc) { - LyXCursor from; + LyXCursor from = cursor; LyXCursor to; - getWord(from, to, loc); + ::getWord(from, to, loc, ownerParagraphs()); if (cursor != from) setCursor(from.par(), from.pos()); if (to == from) @@ -2356,7 +2255,8 @@ void LyXText::changeCase(LyXText::TextCa from = selection.start; to = selection.end; } else { - getWord(from, to, PARTIAL_WORD); + from = cursor; + ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs()); setCursor(to.par(), to.pos() + 1); } Index: text2.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text2.C,v retrieving revision 1.376 diff -u -p -r1.376 text2.C --- text2.C 27 Jun 2003 13:13:03 -0000 1.376 +++ text2.C 27 Jun 2003 15:44:56 -0000 @@ -832,7 +832,7 @@ void LyXText::toggleFree(LyXFont const & LyXCursor resetCursor = cursor; bool implicitSelection = (font.language() == ignore_language && font.number() == LyXFont::IGNORE) - ? selectWordWhenUnderCursor(WHOLE_WORD_STRICT) : false; + ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false; // Set font setFont(font, toggleall); @@ -856,7 +856,8 @@ string LyXText::getStringToIndex() // If there is a change in the language the implicit word selection // is disabled. LyXCursor const reset_cursor = cursor; - bool const implicitSelection = selectWordWhenUnderCursor(PREVIOUS_WORD); + bool const implicitSelection = + selectWordWhenUnderCursor(lyx::PREVIOUS_WORD); string idxstring; if (!selection.set()) Index: text3.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text3.C,v retrieving revision 1.84 diff -u -p -r1.84 text3.C --- text3.C 27 Jun 2003 09:44:25 -0000 1.84 +++ text3.C 27 Jun 2003 15:44:56 -0000 @@ -588,9 +588,9 @@ Inset::RESULT LyXText::dispatch(FuncRequ case LFUN_WORDSEL: { update(); - LyXCursor cur1; + LyXCursor cur1 = cursor; LyXCursor cur2; - getWord(cur1, cur2, WHOLE_WORD); + ::getWord(cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs()); setCursor(cur1.par(), cur1.pos()); bv->beforeChange(this); setCursor(cur2.par(), cur2.pos()); @@ -1016,7 +1016,9 @@ Inset::RESULT LyXText::dispatch(FuncRequ case LFUN_TRANSPOSE_CHARS: update(); - transposeChars(*this, cursor); + setUndo(bv, Undo::FINISH, cursor.par()); + if (transposeChars(cursor)) + checkParagraph(cursor.par(), cursor.pos()); if (inset_owner) bv->updateInset(inset_owner); update(); @@ -1266,10 +1268,10 @@ Inset::RESULT LyXText::dispatch(FuncRequ if (cmd.button() == mouse_button::button1) { if (!isInInset()) { bv->screen().toggleSelection(this, bv); - selectWord(LyXText::WHOLE_WORD_STRICT); + selectWord(lyx::WHOLE_WORD_STRICT); bv->screen().toggleSelection(this, bv, false); } else { - selectWord(LyXText::WHOLE_WORD_STRICT); + selectWord(lyx::WHOLE_WORD_STRICT); } update(); bv->haveSelection(selection.set()); Index: text_funcs.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text_funcs.C,v retrieving revision 1.5 diff -u -p -r1.5 text_funcs.C --- text_funcs.C 23 May 2003 10:33:39 -0000 1.5 +++ text_funcs.C 27 Jun 2003 15:44:56 -0000 @@ -14,31 +14,31 @@ #include <config.h> -#include "lyxtext.h" -#include "paragraph.h" +#include "debug.h" #include "lyxcursor.h" -#include "undo_funcs.h" +#include "ParagraphList.h" + +#include "support/types.h" #include <boost/next_prior.hpp> using lyx::pos_type; +using lyx::word_location; + -void transposeChars(LyXText & text, LyXCursor const & cursor) +bool transposeChars(LyXCursor const & cursor) { ParagraphList::iterator tmppit = cursor.par(); - - setUndo(text.bv(), Undo::FINISH, tmppit); - pos_type tmppos = cursor.pos(); // First decide if it is possible to transpose at all if (tmppos == 0 || tmppos == tmppit->size()) - return; + return false; if (isDeletedText(*tmppit, tmppos - 1) || isDeletedText(*tmppit, tmppos)) - return; + return false; unsigned char c1 = tmppit->getChar(tmppos); unsigned char c2 = tmppit->getChar(tmppos - 1); @@ -46,13 +46,122 @@ void transposeChars(LyXText & text, LyXC // We should have an implementation that handles insets // as well, but that will have to come later. (Lgb) if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET) - return; + return false; bool const erased = tmppit->erase(tmppos - 1, tmppos + 1); - pos_type const ipos(erased ? tmppos - 1 : tmppos + 1); + size_t const ipos = erased ? tmppos - 1 : tmppos + 1; tmppit->insertChar(ipos, c1); tmppit->insertChar(ipos + 1, c2); + return true; +} + + +void cursorLeftOneWord(LyXCursor & cursor, ParagraphList const & pars) +{ + // treat HFills, floats and Insets as words + + ParagraphList::iterator pit = cursor.par(); + size_t pos = cursor.pos(); + + while (pos && + (pit->isSeparator(pos - 1) || + pit->isKomma(pos - 1) || + pit->isNewline(pos - 1)) && + !(pit->isHfill(pos - 1) || + pit->isInset(pos - 1))) + --pos; + + if (pos && + (pit->isInset(pos - 1) || + pit->isHfill(pos - 1))) { + --pos; + } else if (!pos) { + if (pit != pars.begin()) { + --pit; + pos = pit->size(); + } + } else { // Here, cur != 0 + while (pos > 0 && pit->isWord(pos - 1)) + --pos; + } + + cursor.par(pit); + cursor.pos(pos); +} - text.checkParagraph(tmppit, tmppos); + +void cursorRightOneWord(LyXCursor & cursor, ParagraphList const & pars) +{ + // treat floats, HFills and Insets as words + ParagraphList::iterator pit = cursor.par(); + pos_type pos = cursor.pos(); + + // CHECK See comment on top of text.C + + if (pos == pit->size() && boost::next(pit) != pars.end()) { + ++pit; + pos = 0; + } else { + // Skip through initial nonword stuff. + while (pos < pit->size() && !pit->isWord(pos)) { + ++pos; + } + // Advance through word. + while (pos < pit->size() && pit->isWord(pos)) { + ++pos; + } + } + + cursor.par(pit); + cursor.pos(pos); +} + + +// Select current word. This depends on behaviour of +// CursorLeftOneWord(), so it is patched as well. +void getWord(LyXCursor & from, LyXCursor & to, word_location const loc, + ParagraphList const & pars) +{ + switch (loc) { + case lyx::WHOLE_WORD_STRICT: + if (from.pos() == 0 || from.pos() == from.par()->size() + || from.par()->isSeparator(from.pos()) + || from.par()->isKomma(from.pos()) + || from.par()->isNewline(from.pos()) + || from.par()->isSeparator(from.pos() - 1) + || from.par()->isKomma(from.pos() - 1) + || from.par()->isNewline(from.pos() - 1)) { + to = from; + return; + } + // no break here, we go to the next + + case lyx::WHOLE_WORD: + // Move cursor to the beginning, when not already there. + if (from.pos() && !from.par()->isSeparator(from.pos() - 1) + && !(from.par()->isKomma(from.pos() - 1) + || from.par()->isNewline(from.pos() - 1))) + cursorLeftOneWord(from, pars); + break; + case lyx::PREVIOUS_WORD: + // always move the cursor to the beginning of previous word + cursorLeftOneWord(from, pars); + break; + case lyx::NEXT_WORD: + lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n"; + break; + case lyx::PARTIAL_WORD: + break; + } + to = from; + while (to.pos() < to.par()->size() + && !to.par()->isSeparator(to.pos()) + && !to.par()->isKomma(to.pos()) + && !to.par()->isNewline(to.pos()) + && !to.par()->isHfill(to.pos()) + && !to.par()->isInset(to.pos())) + { + to.pos(to.pos() + 1); + } } Index: text_funcs.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text_funcs.h,v retrieving revision 1.1 diff -u -p -r1.1 text_funcs.h --- text_funcs.h 2 Apr 2003 00:51:46 -0000 1.1 +++ text_funcs.h 27 Jun 2003 15:44:56 -0000 @@ -17,9 +17,25 @@ #include <config.h> -class LyXText; +#include "support/types.h" + class LyXCursor; +class ParagraphList; + + +// do no use LyXText or BufferView here + + +/// +bool transposeChars(LyXCursor const & cursor); +/// +void cursorLeftOneWord(LyXCursor &, ParagraphList const &); +/// +void cursorRightOneWord(LyXCursor &, ParagraphList const &); -void transposeChars(LyXText & text, LyXCursor const & cursor); +// Select current word. This depends on behaviour of +// CursorLeftOneWord(), so it is patched as well. +void getWord(LyXCursor & from, LyXCursor & to, lyx::word_location const loc, + ParagraphList const & pars); #endif // TEXT_FUNCS_H Index: textcursor.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/textcursor.h,v retrieving revision 1.2 diff -u -p -r1.2 textcursor.h --- textcursor.h 27 Jun 2003 13:13:04 -0000 1.2 +++ textcursor.h 27 Jun 2003 15:44:56 -0000 @@ -16,6 +16,9 @@ #ifndef TEXTCURSOR_H #define TEXTCURSOR_H +// Do not even think of forward declaring LyXText/BufferView etc here! +// If you need Paragraph proper, go to text_func.h + /** The cursor. Later this variable has to be removed. There should be now internal cursor in a text (and thus not in a buffer). By keeping this it is Index: support/types.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/types.h,v retrieving revision 1.8 diff -u -p -r1.8 types.h --- support/types.h 18 Jun 2003 09:56:10 -0000 1.8 +++ support/types.h 27 Jun 2003 15:44:56 -0000 @@ -70,6 +70,21 @@ namespace lyx #endif + /// + enum word_location { + // the word around the cursor, only if the cursor is + //not at a boundary + WHOLE_WORD_STRICT, + // the word around the cursor + WHOLE_WORD, + /// the word begining from the cursor position + PARTIAL_WORD, + /// the word around the cursor or before the cursor + PREVIOUS_WORD, + /// the next word (not yet used) + NEXT_WORD + }; + } #endif // LYX_TYPES_H Index: lyxtext.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxtext.h,v retrieving revision 1.181 diff -u -p -r1.181 lyxtext.h --- lyxtext.h 27 Jun 2003 13:13:03 -0000 1.181 +++ lyxtext.h 27 Jun 2003 15:44:56 -0000 @@ -54,21 +54,6 @@ public: REFRESH_AREA = 2 }; - /// - enum word_location { - // the word around the cursor, only if the cursor is - //not at a boundary - WHOLE_WORD_STRICT, - // the word around the cursor - WHOLE_WORD, - /// the word begining from the cursor position - PARTIAL_WORD, - /// the word around the cursor or before the cursor - PREVIOUS_WORD, - /// the next word (not yet used) - NEXT_WORD - }; - /// Constructor LyXText(BufferView *); /// sets inset as owner @@ -280,9 +265,9 @@ public: /// select the word we need depending on word_location void getWord(LyXCursor & from, LyXCursor & to, - word_location const); + lyx::word_location const); /// just selects the word the cursor is in - void selectWord(word_location const); + void selectWord(lyx::word_location const); /// returns the inset at cursor (if it exists), 0 otherwise Inset * getInset() const; @@ -363,7 +348,7 @@ public: /// void backspace(); /// - bool selectWordWhenUnderCursor(word_location); + bool selectWordWhenUnderCursor(lyx::word_location); /// enum TextCase { /// @@ -452,8 +437,6 @@ public: private: /// mutable RowList rowlist_; - /// - void cursorLeftOneWord(LyXCursor &); /// float getCursorX(RowList::iterator rit, lyx::pos_type pos,