From c904b9aa0ce518e4d60cecda69e831f6967b76bd Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Fri, 17 Nov 2006 09:34:31 +0000 Subject: [PATCH] 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 --- src/lyxtext.h | 2 ++ src/text.C | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/text3.C | 2 +- status.14x | 2 ++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/lyxtext.h b/src/lyxtext.h index 3375d98361..dacb98cde1 100644 --- a/src/lyxtext.h +++ b/src/lyxtext.h @@ -244,6 +244,8 @@ public: }; /// Change the case of the word at cursor position. 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 always in the first physical paragraph, the bottom settings in the diff --git a/src/text.C b/src/text.C index d01a816a12..c493ebf543 100644 --- a/src/text.C +++ b/src/text.C @@ -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) { BOOST_ASSERT(this == cur.text()); diff --git a/src/text3.C b/src/text3.C index 6bbcc87baf..79619b8f73 100644 --- a/src/text3.C +++ b/src/text3.C @@ -728,7 +728,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd) break; case LFUN_TRANSPOSE_CHARS: - recordUndo(cur); + charsTranspose(cur); break; case LFUN_PASTE: diff --git a/status.14x b/status.14x index 91834401aa..5302ad866b 100644 --- a/status.14x +++ b/status.14x @@ -101,6 +101,8 @@ What's new - Fix insertion position for cross-reference in superscript (bug 2789) +- Add support for the chars-transpose LFUN (bug 2939) + * Build/installation: