draw cursor at newline linebreaks (only \n though and not \r\n)
This commit is contained in:
@@ -1,6 +1,29 @@
|
|||||||
structure Cursor =
|
structure Cursor =
|
||||||
struct
|
struct
|
||||||
(* Prerequisite: lineGap is moved to requested idx first *)
|
(* todo: viL does not handle \r\n pair right now
|
||||||
|
* but this is not a priority so it's okay *)
|
||||||
|
local
|
||||||
|
fun helpVilLf (hd, strIdx, bufferIdx, cursorIdx, tl) =
|
||||||
|
(* if we are in \n,
|
||||||
|
* move cursor to character after newline
|
||||||
|
* if possible *)
|
||||||
|
if strIdx < String.size hd - 3 then
|
||||||
|
(* we know it is safe to move cursorIdx by 2
|
||||||
|
* if strIdx + 2 is before or at the end of the string. *)
|
||||||
|
cursorIdx + 2
|
||||||
|
else
|
||||||
|
(* we have to check the tl to see if it is not empty
|
||||||
|
* so we know it is safe to move. *)
|
||||||
|
case tl of
|
||||||
|
_ :: _ =>
|
||||||
|
(* not empty, so we can increment by 2 *)
|
||||||
|
cursorIdx + 2
|
||||||
|
| [] =>
|
||||||
|
(* empty, so return end of string which is \n *)
|
||||||
|
bufferIdx + String.size hd - 1
|
||||||
|
in
|
||||||
|
(* Prerequisite: lineGap is moved to requested idx first
|
||||||
|
* todo: check if we are in a \r\n pair, but this is not a priority *)
|
||||||
fun viL (lineGap: LineGap.t, cursorIdx) =
|
fun viL (lineGap: LineGap.t, cursorIdx) =
|
||||||
let
|
let
|
||||||
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
||||||
@@ -8,38 +31,21 @@ struct
|
|||||||
case rightStrings of
|
case rightStrings of
|
||||||
hd :: tl =>
|
hd :: tl =>
|
||||||
let
|
let
|
||||||
(* idx relative to this string *)
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||||
val strIdx = cursorIdx - bufferIdx
|
val strIdx = cursorIdx - bufferIdx
|
||||||
in
|
in
|
||||||
if strIdx < String.size hd - 2 then
|
if strIdx < String.size hd - 2 then
|
||||||
(* increment if there is a character after where cursor is
|
(case String.sub (hd, strIdx + 1) of
|
||||||
* but first, check if we are in a \r\n pair;
|
#"\n" => helpVilLf (hd, strIdx, bufferIdx, cursorIdx, tl)
|
||||||
* if we are, then increment cursor by 2 *)
|
| _ => cursorIdx + 1)
|
||||||
if
|
|
||||||
String.size hd > strIdx + 2
|
|
||||||
andalso String.sub (hd, strIdx + 1) = #"\r"
|
|
||||||
andalso String.sub (hd, strIdx + 2) = #"\n"
|
|
||||||
then
|
|
||||||
(* increment cursor by 2 if we are inside \r\n pair *)
|
|
||||||
cursorIdx + 2
|
|
||||||
else
|
|
||||||
(* inccrement cursor by 1 if we are not inside \r\n pair *)
|
|
||||||
cursorIdx + 1
|
|
||||||
else
|
else
|
||||||
(case tl of
|
(case tl of
|
||||||
_ :: _ =>
|
tlhd :: tltl =>
|
||||||
(* if there is another string after current head, we can increment cursorIdx
|
(* if there is another string after current head, we can increment cursorIdx
|
||||||
* however, first we need to check if next string is \r\n pair. *)
|
* however, first we need to check if next char is \n. *)
|
||||||
if
|
(case String.sub (tlhd, 0) of
|
||||||
String.size hd > strIdx + 2
|
#"\n" => helpVilLf (tlhd, 0, bufferIdx, cursorIdx, tltl)
|
||||||
andalso String.sub (hd, strIdx + 1) = #"\r"
|
| _ => cursorIdx + 1)
|
||||||
andalso String.sub (hd, strIdx + 2) = #"\n"
|
|
||||||
then
|
|
||||||
(* increment cursor by 2 if we are inside \r\n pair *)
|
|
||||||
cursorIdx + 2
|
|
||||||
else
|
|
||||||
(* inccrement cursor by 1 if we are not inside \r\n pair *)
|
|
||||||
cursorIdx + 1
|
|
||||||
| _ =>
|
| _ =>
|
||||||
(* if there is no string after current head, return original cursorIdx *)
|
(* if there is no string after current head, return original cursorIdx *)
|
||||||
cursorIdx)
|
cursorIdx)
|
||||||
@@ -49,3 +55,4 @@ struct
|
|||||||
cursorIdx
|
cursorIdx
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|||||||
@@ -316,11 +316,25 @@ struct
|
|||||||
)
|
)
|
||||||
| #"\n" =>
|
| #"\n" =>
|
||||||
if posY + ySpace < windowHeight then
|
if posY + ySpace < windowHeight then
|
||||||
|
if absIdx <> cursorPos then
|
||||||
|
(* not in cursor position, so iterate like normal *)
|
||||||
buildTextStringWithinCursor
|
buildTextStringWithinCursor
|
||||||
( pos + 1, str, acc, startX, posY + ySpace, startX
|
( pos + 1, str, acc, startX, posY + ySpace, startX
|
||||||
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
||||||
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb
|
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb
|
||||||
)
|
)
|
||||||
|
else
|
||||||
|
(* in cursor position, so build cursorAcc
|
||||||
|
* and call AfterCursor function *)
|
||||||
|
let
|
||||||
|
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b)
|
||||||
|
in
|
||||||
|
buildTextStringAfterCursor
|
||||||
|
( pos + 1, str, acc, startX, posY + ySpace, startX
|
||||||
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
||||||
|
, r, g, b, tl, cursorAcc
|
||||||
|
)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
accToDrawMsg (acc, cursorAcc)
|
accToDrawMsg (acc, cursorAcc)
|
||||||
| #"\r" =>
|
| #"\r" =>
|
||||||
|
|||||||
2
temp.txt
2
temp.txt
@@ -1,6 +1,8 @@
|
|||||||
signature TEXT_BUILDER =
|
signature TEXT_BUILDER =
|
||||||
|
|
||||||
sig
|
sig
|
||||||
(* Prerequisite: LineGap is moved to requested line first. *)
|
(* Prerequisite: LineGap is moved to requested line first. *)
|
||||||
val build: int * int * LineGap.t * int * int
|
val build: int * int * LineGap.t * int * int
|
||||||
-> MailboxType.t list
|
-> MailboxType.t list
|
||||||
enda
|
enda
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user