Fix bug 2393 (from Dov Feldstern)

* src/lyxtext.h
	* src/text.C
	(LyXText::charsTranspose): New method for transposing characters

	* src/text.C
	(LyXText::dispatch): Call charsTranspose for LFUN_CHARS_TRANSPOSE


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_4_X@15952 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-11-17 09:34:31 +00:00
parent 0698a90dd4
commit c904b9aa0c
4 changed files with 61 additions and 1 deletions

View File

@ -244,6 +244,8 @@ public:
}; };
/// Change the case of the word at cursor position. /// Change the case of the word at cursor position.
void changeCase(LCursor & cur, TextCase action); void changeCase(LCursor & cur, TextCase action);
/// Transposes the character at the cursor with the one before it.
void charsTranspose(LCursor & cur);
/** the DTP switches for paragraphs. LyX will store the top settings /** the DTP switches for paragraphs. LyX will store the top settings
always in the first physical paragraph, the bottom settings in the always in the first physical paragraph, the bottom settings in the

View File

@ -1585,6 +1585,62 @@ void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
} }
void LyXText::charsTranspose(LCursor & cur)
{
BOOST_ASSERT(this == cur.text());
pos_type pos = cur.pos();
// If cursor is at beginning or end of paragraph, do nothing.
if (pos == cur.lastpos() || pos == 0)
return;
Paragraph & par = cur.paragraph();
// Get the positions of the characters to be transposed.
pos_type pos1 = pos - 1;
pos_type pos2 = pos;
// In change tracking mode, ignore deleted characters.
while (pos2 < cur.lastpos() && isDeletedText(par, pos2))
++pos2;
if (pos2 == cur.lastpos())
return;
while (pos1 >= 0 && isDeletedText(par, pos1))
--pos1;
if (pos1 < 0)
return;
// Don't do anything if one of the "characters" is not regular text.
if (par.isInset(pos1) || par.isInset(pos2))
return;
// Store the characters to be transposed (including font information).
char char1 = par.getChar(pos1);
LyXFont const font1 =
par.getFontSettings(cur.buffer().params(), pos1);
char char2 = par.getChar(pos2);
LyXFont const font2 =
par.getFontSettings(cur.buffer().params(), pos2);
// And finally, we are ready to perform the transposition.
// We use erase() and insertChar(), which take care of tracking the
// change.
recordUndo(cur);
par.erase(pos2);
par.erase(pos1);
par.insertChar(pos1, char2, font2);
par.insertChar(pos2, char1, font1);
// After the transposition, move cursor to after the transposition.
setCursor(cur, cur.pit(), pos2);
cur.forwardPos();
}
bool LyXText::Delete(LCursor & cur) bool LyXText::Delete(LCursor & cur)
{ {
BOOST_ASSERT(this == cur.text()); BOOST_ASSERT(this == cur.text());

View File

@ -728,7 +728,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
break; break;
case LFUN_TRANSPOSE_CHARS: case LFUN_TRANSPOSE_CHARS:
recordUndo(cur); charsTranspose(cur);
break; break;
case LFUN_PASTE: case LFUN_PASTE:

View File

@ -101,6 +101,8 @@ What's new
- Fix insertion position for cross-reference in superscript (bug 2789) - Fix insertion position for cross-reference in superscript (bug 2789)
- Add support for the chars-transpose LFUN (bug 2939)
* Build/installation: * Build/installation: