From 70acfa623be189df50b42a7402085e782341fec2 Mon Sep 17 00:00:00 2001
From: Neil
Date: Tue, 20 Aug 2013 14:57:56 +1000
Subject: Added PositionRelative to optimize navigation by character.
---
doc/ScintillaDoc.html | 6 ++++++
doc/ScintillaHistory.html | 3 +++
include/Scintilla.h | 1 +
include/Scintilla.iface | 4 ++++
src/Editor.cxx | 3 +++
test/simpleTests.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 62 insertions(+)
diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html
index 57550bfe6..cca207dae 100644
--- a/doc/ScintillaDoc.html
+++ b/doc/ScintillaDoc.html
@@ -1161,6 +1161,7 @@ struct Sci_TextToFind {
onlyWordCharacters)
SCI_POSITIONBEFORE(int position)
SCI_POSITIONAFTER(int position)
+ SCI_POSITIONRELATIVE(int position, int relative)
SCI_COUNTCHARACTERS(int startPos, int endPos)
SCI_TEXTWIDTH(int styleNumber, const char *text)
SCI_TEXTHEIGHT(int line)
@@ -1466,6 +1467,11 @@ struct Sci_TextToFind {
If called with a position within a multi byte character will return the position
of the start/end of that character.
+ SCI_POSITIONRELATIVE(int position, int relative)
+ Count a number of whole characters before or after the argument position and return that position.
+ The minimum position returned is 0 and the maximum is the last position in the document.
+
+
SCI_COUNTCHARACTERS(int startPos, int endPos)
Returns the number of whole characters between two positions..
diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html
index e0ccc5966..97ff11f58 100644
--- a/doc/ScintillaHistory.html
+++ b/doc/ScintillaHistory.html
@@ -452,6 +452,9 @@
In Unicode mode C1 control characters are represented by their mnemonics.
+ Added SCI_POSITIONRELATIVE to optimize navigation by character.
+
+
Option to allow mouse selection to switch to rectangular by pressing Alt after start of gesture.
Feature #1007.
diff --git a/include/Scintilla.h b/include/Scintilla.h
index 5bfcfc266..405cc81f7 100644
--- a/include/Scintilla.h
+++ b/include/Scintilla.h
@@ -673,6 +673,7 @@ typedef sptr_t (*SciFnDirect)(sptr_t ptr, unsigned int iMessage, uptr_t wParam,
#define SCI_PARAUPEXTEND 2416
#define SCI_POSITIONBEFORE 2417
#define SCI_POSITIONAFTER 2418
+#define SCI_POSITIONRELATIVE 2670
#define SCI_COPYRANGE 2419
#define SCI_COPYTEXT 2420
#define SC_SEL_STREAM 0
diff --git a/include/Scintilla.iface b/include/Scintilla.iface
index f7880a035..7909f2084 100644
--- a/include/Scintilla.iface
+++ b/include/Scintilla.iface
@@ -1756,6 +1756,10 @@ fun position PositionBefore=2417(position pos,)
# page into account. Maximum value returned is the last position in the document.
fun position PositionAfter=2418(position pos,)
+# Given a valid document position, return a position that differs in a number
+# of characters. Returned value is always between 0 and last position in document.
+fun position PositionRelative=2670(position pos, int relative)
+
# Copy a range of text to the clipboard. Positions are clipped into the document.
fun void CopyRange=2419(position start, position end)
diff --git a/src/Editor.cxx b/src/Editor.cxx
index 7fa1326a7..afc2983ed 100644
--- a/src/Editor.cxx
+++ b/src/Editor.cxx
@@ -7605,6 +7605,9 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
case SCI_POSITIONAFTER:
return pdoc->MovePositionOutsideChar(wParam + 1, 1, true);
+ case SCI_POSITIONRELATIVE:
+ return Platform::Clamp(pdoc->GetRelativePosition(wParam, lParam), 0, pdoc->Length());
+
case SCI_LINESCROLL:
ScrollTo(topLine + lParam);
HorizontalScrollTo(xOffset + static_cast(wParam) * vs.spaceWidth);
diff --git a/test/simpleTests.py b/test/simpleTests.py
index 57607c375..30e74209d 100644
--- a/test/simpleTests.py
+++ b/test/simpleTests.py
@@ -1376,6 +1376,51 @@ class TestMultiSelection(unittest.TestCase):
self.assertEquals(self.ed.GetSelectionNCaret(0), 3)
self.assertEquals(self.ed.GetSelectionNCaretVirtualSpace(0), 0)
+class TestCharacterNavigation(unittest.TestCase):
+ def setUp(self):
+ self.xite = Xite.xiteFrame
+ self.ed = self.xite.ed
+ self.ed.ClearAll()
+ self.ed.EmptyUndoBuffer()
+ self.ed.SetCodePage(65001)
+
+ def tearDown(self):
+ self.ed.SetCodePage(0)
+
+ def testBeforeAfter(self):
+ t = "aåflﬔ-"
+ tv = t.encode("UTF-8")
+ self.ed.SetContents(tv)
+ pos = 0
+ for i in range(len(t)-1):
+ after = self.ed.PositionAfter(pos)
+ self.assert_(after > i)
+ back = self.ed.PositionBefore(after)
+ self.assertEquals(pos, back)
+ pos = after
+
+ def testRelative(self):
+ # \x61 \xc3\xa5 \xef\xac\x82 \xef\xac\x94 \x2d
+ t = "aåflﬔ-"
+ tv = t.encode("UTF-8")
+ self.ed.SetContents(tv)
+ self.assertEquals(self.ed.PositionRelative(1, 2), 6)
+ self.assertEquals(self.ed.PositionRelative(6, -2), 1)
+ pos = 0
+ previous = 0
+ for i in range(1, len(t)):
+ after = self.ed.PositionRelative(pos, i)
+ self.assert_(after > pos)
+ self.assert_(after > previous)
+ previous = after
+ pos = len(t)
+ previous = pos
+ for i in range(1, len(t)-1):
+ after = self.ed.PositionRelative(pos, -i)
+ self.assert_(after < pos)
+ self.assert_(after < previous)
+ previous = after
+
class TestCaseMapping(unittest.TestCase):
def setUp(self):
self.xite = Xite.xiteFrame
--
cgit v1.2.3