]> git.lyx.org Git - lyx.git/blob - src/text_funcs.C
tab funcs changed
[lyx.git] / src / text_funcs.C
1 /**
2  * \file text_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  *
11  * This file contains some utility functions for actually mutating
12  * the text contents of a document 
13  */
14
15 #include <config.h>
16
17 #include "lyxtext.h"
18 #include "paragraph.h"
19 #include "lyxcursor.h"
20 #include "undo_funcs.h"
21
22 using lyx::pos_type;
23
24 void transposeChars(LyXText & text, LyXCursor const & cursor)
25 {
26         Paragraph * tmppar = cursor.par();
27
28         setUndo(text.bv(), Undo::FINISH, tmppar, tmppar->next());
29
30         pos_type tmppos = cursor.pos();
31
32         // First decide if it is possible to transpose at all
33
34         if (tmppos == 0 || tmppos == tmppar->size())
35                 return;
36
37         if (isDeletedText(*tmppar, tmppos - 1)
38                 || isDeletedText(*tmppar, tmppos))
39                 return;
40
41         unsigned char c1 = tmppar->getChar(tmppos);
42         unsigned char c2 = tmppar->getChar(tmppos - 1);
43
44         // We should have an implementation that handles insets
45         // as well, but that will have to come later. (Lgb)
46         if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
47                 return;
48
49         bool const erased = tmppar->erase(tmppos - 1, tmppos + 1);
50         pos_type const ipos(erased ? tmppos - 1 : tmppos + 1);
51
52         tmppar->insertChar(ipos, c1);
53         tmppar->insertChar(ipos + 1, c2);
54
55         text.checkParagraph(tmppar, tmppos);
56 }