From 2196ed6fc9dce1ebc4d4e631a77859967b848f72 Mon Sep 17 00:00:00 2001
From: nyamatongwe
Date: Mon, 12 Jul 2010 02:07:14 +0000
Subject: Feature #3027559 Extend "sticky caret" feature from Jason Oster.
Additional mode SC_CARETSTICKY_WHITESPACE implemented.
---
doc/ScintillaDoc.html | 23 ++++++++++++++++++-----
include/Scintilla.h | 3 +++
include/Scintilla.iface | 9 +++++++--
src/Editor.cxx | 30 ++++++++++++++++++++----------
src/Editor.h | 2 +-
5 files changed, 49 insertions(+), 18 deletions(-)
diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html
index cd4939a55..41c3a5d43 100644
--- a/doc/ScintillaDoc.html
+++ b/doc/ScintillaDoc.html
@@ -2526,7 +2526,7 @@ struct Sci_TextToFind {
SCI_SETCONTROLCHARSYMBOL(int
symbol)
SCI_GETCONTROLCHARSYMBOL
- SCI_SETCARETSTICKY(bool useCaretStickyBehaviour)
+ SCI_SETCARETSTICKY(int useCaretStickyBehaviour)
SCI_GETCARETSTICKY
SCI_TOGGLECARETSTICKY
@@ -2631,12 +2631,25 @@ struct Sci_TextToFind {
You can read back the current symbol with the SCI_GETCONTROLCHARSYMBOL message.
The default symbol value is 0.
- SCI_SETCARETSTICKY(bool useCaretStickyBehaviour)
+
SCI_SETCARETSTICKY(int useCaretStickyBehaviour)
SCI_GETCARETSTICKY
SCI_TOGGLECARETSTICKY
- These messages set, get or toggle the caretSticky flag which controls when the last position
- of the caret on the line is saved. When set to true, the position is not saved when you type
- a character, a tab, paste the clipboard content or press backspace.
+ These messages set, get or toggle the caretSticky setting which controls when the last position
+ of the caret on the line is saved.
+
+ When set to SC_CARETSTICKY_OFF (0), the sticky flag is off; all text changes
+ (and all caret position changes) will remember the
+ caret's new horizontal position when moving to different lines. This is the default.
+
+ When set to SC_CARETSTICKY_ON (1), the sticky flag is on, and the only thing which will cause the editor to remember the
+ horizontal caret position is moving the caret with mouse or keyboard (left/right arrow keys, home/end keys, etc).
+
+ When set to SC_CARETSTICKY_WHITESPACE (2), the caret acts like mode 0 (sticky off) except under one
+ special case; when space or tab characters are inserted. (Including pasting only space/tabs -- undo, redo,
+ etc. do not exhibit this behavior..).
+
+ SCI_TOGGLECARETSTICKY switches from SC_CARETSTICKY_ON and SC_CARETSTICKY_WHITESPACE
+ to SC_CARETSTICKY_OFF and from SC_CARETSTICKY_OFF to SC_CARETSTICKY_ON.
Margins
diff --git a/include/Scintilla.h b/include/Scintilla.h
index b91c78c2a..efb536742 100644
--- a/include/Scintilla.h
+++ b/include/Scintilla.h
@@ -669,6 +669,9 @@ typedef sptr_t (*SciFnDirect)(sptr_t ptr, unsigned int iMessage, uptr_t wParam,
#define SCI_FINDCOLUMN 2456
#define SCI_GETCARETSTICKY 2457
#define SCI_SETCARETSTICKY 2458
+#define SC_CARETSTICKY_OFF 0
+#define SC_CARETSTICKY_ON 1
+#define SC_CARETSTICKY_WHITESPACE 2
#define SCI_TOGGLECARETSTICKY 2459
#define SCI_SETPASTECONVERTENDINGS 2467
#define SCI_GETPASTECONVERTENDINGS 2468
diff --git a/include/Scintilla.iface b/include/Scintilla.iface
index d7c850840..f40530fda 100644
--- a/include/Scintilla.iface
+++ b/include/Scintilla.iface
@@ -1780,10 +1780,15 @@ fun int EncodedFromUTF8=2449(string utf8, stringresult encoded)
fun int FindColumn=2456(int line, int column)
# Can the caret preferred x position only be changed by explicit movement commands?
-get bool GetCaretSticky=2457(,)
+get int GetCaretSticky=2457(,)
# Stop the caret preferred x position changing when the user types.
-set void SetCaretSticky=2458(bool useCaretStickyBehaviour,)
+set void SetCaretSticky=2458(int useCaretStickyBehaviour,)
+
+enu CaretSticky=SC_CARETSTICKY_
+val SC_CARETSTICKY_OFF=0
+val SC_CARETSTICKY_ON=1
+val SC_CARETSTICKY_WHITESPACE=2
# Switch between sticky and non-sticky: meant to be bound to a key.
fun void ToggleCaretSticky=2459(,)
diff --git a/src/Editor.cxx b/src/Editor.cxx
index ecac75118..6119944c3 100644
--- a/src/Editor.cxx
+++ b/src/Editor.cxx
@@ -94,6 +94,15 @@ static inline bool IsControlCharacter(int ch) {
return ch >= 0 && ch < ' ';
}
+static inline bool IsAllSpacesOrTabs(char *s, unsigned int len) {
+ for (unsigned int i = 0; i < len; i++) {
+ // This is safe because IsSpaceOrTab() will return false for null terminators
+ if (!IsSpaceOrTab(s[i]))
+ return false;
+ }
+ return true;
+}
+
Editor::Editor() {
ctrlID = 0;
@@ -148,7 +157,7 @@ Editor::Editor() {
lineWidthMaxSeen = 0;
verticalScrollBarVisible = true;
endAtLastLine = true;
- caretSticky = false;
+ caretSticky = SC_CARETSTICKY_OFF;
multipleSelection = false;
additionalSelectionTyping = false;
multiPasteMode = SC_MULTIPASTE_ONCE;
@@ -3813,7 +3822,8 @@ void Editor::AddCharUTF(char *s, unsigned int len, bool treatAsDBCS) {
EnsureCaretVisible();
// Avoid blinking during rapid typing:
ShowCaretAtCurrentPosition();
- if (!caretSticky) {
+ if ((caretSticky == SC_CARETSTICKY_OFF) ||
+ ((caretSticky == SC_CARETSTICKY_WHITESPACE) && !IsAllSpacesOrTabs(s, len))) {
SetLastXChosen();
}
@@ -5072,21 +5082,21 @@ int Editor::KeyCommand(unsigned int iMessage) {
break;
case SCI_DELETEBACK:
DelCharBack(true);
- if (!caretSticky) {
+ if ((caretSticky == SC_CARETSTICKY_OFF) || (caretSticky == SC_CARETSTICKY_WHITESPACE)) {
SetLastXChosen();
}
EnsureCaretVisible();
break;
case SCI_DELETEBACKNOTLINE:
DelCharBack(false);
- if (!caretSticky) {
+ if ((caretSticky == SC_CARETSTICKY_OFF) || (caretSticky == SC_CARETSTICKY_WHITESPACE)) {
SetLastXChosen();
}
EnsureCaretVisible();
break;
case SCI_TAB:
Indent(true);
- if (!caretSticky) {
+ if (caretSticky == SC_CARETSTICKY_OFF) {
SetLastXChosen();
}
EnsureCaretVisible();
@@ -5094,7 +5104,7 @@ int Editor::KeyCommand(unsigned int iMessage) {
break;
case SCI_BACKTAB:
Indent(false);
- if (!caretSticky) {
+ if ((caretSticky == SC_CARETSTICKY_OFF) || (caretSticky == SC_CARETSTICKY_WHITESPACE)) {
SetLastXChosen();
}
EnsureCaretVisible();
@@ -6720,7 +6730,7 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
case SCI_PASTE:
Paste();
- if (!caretSticky) {
+ if ((caretSticky == SC_CARETSTICKY_OFF) || (caretSticky == SC_CARETSTICKY_WHITESPACE)) {
SetLastXChosen();
}
EnsureCaretVisible();
@@ -7475,9 +7485,9 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
return endAtLastLine;
case SCI_SETCARETSTICKY:
- PLATFORM_ASSERT((wParam == 0) || (wParam == 1));
- if (caretSticky != (wParam != 0)) {
- caretSticky = wParam != 0;
+ PLATFORM_ASSERT((wParam >= SC_CARETSTICKY_OFF) && (wParam <= SC_CARETSTICKY_WHITESPACE));
+ if ((wParam >= SC_CARETSTICKY_OFF) && (wParam <= SC_CARETSTICKY_WHITESPACE)) {
+ caretSticky = wParam;
}
break;
diff --git a/src/Editor.h b/src/Editor.h
index 54411fad5..e7aa2e6ce 100644
--- a/src/Editor.h
+++ b/src/Editor.h
@@ -159,7 +159,7 @@ protected: // ScintillaBase subclass needs access to much of Editor
int lineWidthMaxSeen;
bool verticalScrollBarVisible;
bool endAtLastLine;
- bool caretSticky;
+ int caretSticky;
bool multipleSelection;
bool additionalSelectionTyping;
int multiPasteMode;
--
cgit v1.2.3