improved cursor motion for up/down/home/end; text editor now remembers cursor positions (instead of indices) when applying scripts.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@237 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2007-03-24 23:42:33 +00:00
parent 135941fb43
commit 114c03b6e1
5 changed files with 66 additions and 31 deletions
+36 -4
View File
@@ -182,14 +182,46 @@ size_t index_to_cursor(const String& str, size_t index, Movement dir) {
if (is_substr(str, i, _("<atom")) || is_substr(str, i, _("<sep"))) {
// skip tag contents, tag counts as a single 'character'
size_t before = i;
size_t after = match_close_tag_end(str, i);
size_t close = match_close_tag(str, i);
size_t after = skip_tag(str, close);
if (index > before && index < after) {
// index is inside an atom, determine on which side we want the cursor
if (dir == MOVE_RIGHT) {
// Index is inside an atom, determine on which side we want the cursor
// This is the only place where MOVE_LEFT/RIGHT and MOVE_*_OPT differ
// for the OPT version we must check if we are actually past any real characters
// but, if the atom is empty, it still counts as a single character!
if (dir == MOVE_LEFT) {
return cursor;
} else if (dir == MOVE_RIGHT) {
return cursor + 1;
} else if (dir == MOVE_MID) {
// take the closest side
return cursor + ((int)(after - index) < (int)(index - before));
} else if (dir == MOVE_LEFT_OPT) {
// is there any non-tag after index?
bool empty = true;
while (i < close) {
c = str.GetChar(i);
if (c == _('<')) {
i = skip_tag(str, i);
} else if (i >= index) {
return cursor; // this is a non-tag character after index
} else {
empty = false;
++i;
}
}
return empty ? cursor : cursor + 1; // still didn't pass any
} else if (dir == MOVE_RIGHT_OPT) {
// is index actually past any non-tag?
while (i < close) {
if (i >= index) {
return cursor; // we didn't pass any non-tag stuff
}
c = str.GetChar(i);
if (c != _('<')) break;
i = skip_tag(str, i);
}
return cursor + 1; // yes it is
}
}
i = after;
@@ -260,7 +292,7 @@ size_t cursor_to_index(const String& str, size_t cursor, Movement dir) {
}
}
// This allows formating to be enabled without a selection
return dir == MOVE_RIGHT ? end - 1 : start;
return dir <= 0 /*MOVE_LEFT*/ ? start : end - 1;
}
// ----------------------------------------------------------------------------- : Untagged position
+5 -3
View File
@@ -90,9 +90,11 @@ String anti_tag(const String& tag);
/// Directions of cursor movement
enum Movement
{ MOVE_LEFT = -1 ///< Always move the cursor to the left
, MOVE_MID = 0 ///< Move in whichever direction the distance to move is shorter (TODO: define shorter)
, MOVE_RIGHT = 1 ///< Always move the cursor to the right
{ MOVE_LEFT = -2 ///< Always move the cursor to the left
, MOVE_LEFT_OPT = -1 ///< Move the cursor to the left, but a position inside a tag is the same as that before
, MOVE_MID = 0 ///< Move in whichever direction the distance to move is shorter (TODO: define shorter)
, MOVE_RIGHT_OPT = 1 ///< Move the cursor to the left, but a position inside a tag is the same as that after
, MOVE_RIGHT = 2 ///< Always move the cursor to the right
};
/// Find the cursor position corresponding to the given character index.