]> git.lyx.org Git - lyx.git/blob - src/text_funcs.C
Alfredo's second patch
[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 #include <boost/next_prior.hpp>
23
24 using lyx::pos_type;
25
26 void transposeChars(LyXText & text, LyXCursor const & cursor)
27 {
28         ParagraphList::iterator tmppit = cursor.par();
29
30         setUndo(text.bv(), Undo::FINISH, tmppit, boost::next(tmppit));
31
32         pos_type tmppos = cursor.pos();
33
34         // First decide if it is possible to transpose at all
35
36         if (tmppos == 0 || tmppos == tmppit->size())
37                 return;
38
39         if (isDeletedText(*tmppit, tmppos - 1)
40                 || isDeletedText(*tmppit, tmppos))
41                 return;
42
43         unsigned char c1 = tmppit->getChar(tmppos);
44         unsigned char c2 = tmppit->getChar(tmppos - 1);
45
46         // We should have an implementation that handles insets
47         // as well, but that will have to come later. (Lgb)
48         if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
49                 return;
50
51         bool const erased = tmppit->erase(tmppos - 1, tmppos + 1);
52         pos_type const ipos(erased ? tmppos - 1 : tmppos + 1);
53
54         tmppit->insertChar(ipos, c1);
55         tmppit->insertChar(ipos + 1, c2);
56
57         text.checkParagraph(tmppit, tmppos);
58 }