Last active
February 8, 2023 10:56
-
-
Save toomasv/0085fd7c61e53dc9cedb45ae98f777e9 to your computer and use it in GitHub Desktop.
Example of getting and setting caret in area
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Needs: 'View | |
License: "BSD-3" | |
] | |
;Mainly @hiiamboris | |
;Inserts closing `"])}` for `"[({` automatically | |
#system [ | |
#import [ | |
"user32.dll" stdcall [ | |
SendMessage: "SendMessageW" [ | |
hWnd [handle!] | |
msg [integer!] | |
wParam [integer!] | |
lParam [integer!] | |
return: [handle!] | |
] | |
] | |
] | |
get-char: func [ | |
p [byte-ptr!] | |
unit [integer!] | |
return: [integer!] | |
/local | |
p4 [int-ptr!] | |
][ | |
switch unit [ | |
Latin1 [as-integer p/value] | |
UCS-2 [(as-integer p/2) << 8 + p/1] | |
UCS-4 [p4: as int-ptr! p p4/value] | |
] | |
] | |
sniff-chars: func [ | |
p [byte-ptr!] | |
tail [byte-ptr!] | |
unit [integer!] | |
quote [int-ptr!] | |
nl [int-ptr!] | |
/local | |
cp [integer!] | |
p4 [int-ptr!] | |
][ | |
while [p < tail][ | |
cp: switch unit [ | |
Latin1 [as-integer p/value] | |
UCS-2 [(as-integer p/2) << 8 + p/1] | |
UCS-4 [p4: as int-ptr! p p4/value] | |
] | |
switch cp [ | |
#"^"" [quote/value: quote/value + 1] ;" | |
#"^/" [nl/value: nl/value + 1] | |
default [0] | |
] | |
p: p + unit | |
] | |
] | |
adjust-selection: func [ | |
str [red-string!] | |
bgn [int-ptr!] | |
end [int-ptr!] | |
inc [integer!] ;-- +1 to increase, -1 to decrease | |
/local | |
quote [integer!] | |
nl [integer!] | |
unit [integer!] | |
unit-b [integer!] | |
cp [integer!] | |
size [integer!] | |
s [series!] | |
head [byte-ptr!] | |
tail [byte-ptr!] | |
p [byte-ptr!] | |
p-bgn [byte-ptr!] | |
p-end [byte-ptr!] | |
][ | |
assert bgn/value <= end/value | |
if TYPE_OF(str) <> TYPE_STRING [exit] | |
s: GET_BUFFER(str) | |
unit: GET_UNIT(s) | |
unit-b: log-b unit | |
head: (as byte-ptr! s/offset) + (str/head << unit-b) | |
tail: as byte-ptr! s/tail | |
either inc > 0 [ | |
p-bgn: head + (bgn/1 << unit-b) | |
p-end: head + (end/1 << unit-b) | |
quote: 0 nl: 0 | |
sniff-chars head p-bgn unit :quote :nl | |
bgn/1: bgn/1 + nl | |
sniff-chars p-bgn p-end unit :quote :nl | |
end/1: end/1 + nl | |
][ | |
p: head | |
while [p < tail] [ | |
cp: get-char p unit | |
if cp = as-integer #"^/" [ | |
size: (as-integer p - head) >> unit-b | |
case [ | |
size >= end/1 [break] | |
size >= bgn/1 [end/1: end/1 - 1] | |
true [bgn/1: bgn/1 - 1 end/1: end/1 - 1] | |
] | |
] | |
p: p + unit | |
] | |
] | |
] | |
] | |
get-caret1: routine [handle [handle!] text [string!] return: [integer!] /local a b] [ | |
a: 0 b: 0 | |
SendMessage as handle! handle/value B0h as-integer :a as-integer :b | |
adjust-selection text :a :b -1 | |
b | |
] | |
set-caret1: routine [handle [handle!] text [string!] where [integer!] /local junk] [ | |
junk: where | |
adjust-selection text :where :junk 1 | |
SendMessage as handle! handle/value B1h where where | |
] | |
get-caret: func [face [object!]][get-caret1 face/state/1 face/text] | |
set-caret: func [face [object!] pos [integer!]][set-caret1 face/state/1 face/text :pos] | |
view [ | |
area no-border wrap focus "" | |
on-key [ | |
case [ | |
switch event/key [ | |
#"[" [ | |
insert at face/text pos: 1 + get-caret face #"]" | |
set-caret face pos - 1 | |
] | |
#"{" [ | |
insert at face/text pos: 1 + get-caret face #"}" | |
set-caret face pos - 1 | |
] | |
#"^"" [ | |
insert at face/text pos: 1 + get-caret face #"^"" ;" | |
set-caret face pos - 1 | |
] | |
#"(" [ | |
insert at face/text pos: 1 + get-caret face #")" | |
set-caret face pos - 1 | |
] | |
] | |
] | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment