implement equivalent of vi's 'b' cursor motion
This commit is contained in:
@@ -132,6 +132,22 @@ struct
|
|||||||
in
|
in
|
||||||
(newApp, drawMsg)
|
(newApp, drawMsg)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
fun prevWord (app: app_type) =
|
||||||
|
let
|
||||||
|
val {buffer, windowWidth, windowHeight, startLine, cursorIdx, ...} = app
|
||||||
|
|
||||||
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
|
val cursorIdx = Cursor.prevWord (buffer, cursorIdx)
|
||||||
|
|
||||||
|
val buffer = LineGap.goToLine (startLine, buffer)
|
||||||
|
val drawMsg = TextBuilder.build
|
||||||
|
(startLine, cursorIdx, buffer, windowWidth, windowHeight)
|
||||||
|
|
||||||
|
val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx)
|
||||||
|
in
|
||||||
|
(newApp, drawMsg)
|
||||||
|
end
|
||||||
|
|
||||||
fun handleChr (app: app_type, chr) =
|
fun handleChr (app: app_type, chr) =
|
||||||
case chr of
|
case chr of
|
||||||
@@ -142,6 +158,7 @@ struct
|
|||||||
| #"0" => moveToLineStart app
|
| #"0" => moveToLineStart app
|
||||||
| #"$" => moveToLineEnd app
|
| #"$" => moveToLineEnd app
|
||||||
| #"w" => nextWord app
|
| #"w" => nextWord app
|
||||||
|
| #"b" => prevWord app
|
||||||
| _ => (app, [])
|
| _ => (app, [])
|
||||||
|
|
||||||
fun update (app, msg) =
|
fun update (app, msg) =
|
||||||
|
|||||||
287
fcore/cursor.sml
287
fcore/cursor.sml
@@ -861,79 +861,79 @@ struct
|
|||||||
fun helpNextWordString
|
fun helpNextWordString
|
||||||
( strPos, str, absIdx
|
( strPos, str, absIdx
|
||||||
, strTl, lineTl
|
, strTl, lineTl
|
||||||
, wordType, hasPassedSpace) =
|
, prevWordType, hasPassedSpace) =
|
||||||
if strPos = String.size str then
|
if strPos = String.size str then
|
||||||
helpNextWordList
|
helpNextWordList
|
||||||
(strTl, lineTl, absIdx, wordType, hasPassedSpace)
|
(strTl, lineTl, absIdx, prevWordType, hasPassedSpace)
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val chr = String.sub (str, strPos)
|
val chr = String.sub (str, strPos)
|
||||||
in
|
in
|
||||||
if Char.isAlphaNum chr orelse chr = #"_" then
|
if Char.isAlphaNum chr orelse chr = #"_" then
|
||||||
case wordType of
|
case prevWordType of
|
||||||
ALPHA_NUM =>
|
ALPHA_NUM =>
|
||||||
(* current chr is ALPHA_NUM
|
(* current chr is ALPHA_NUM
|
||||||
* and previous chr was also ALPHA_NUM
|
* and previous chr was also ALPHA_NUM
|
||||||
* so continue iterating *)
|
* so continue iterating *)
|
||||||
helpNextWordString
|
|
||||||
( strPos + 1, str, absIdx + 1
|
|
||||||
, strTl, lineTl
|
|
||||||
, ALPHA_NUM, hasPassedSpace
|
|
||||||
)
|
|
||||||
| SPACE=>
|
|
||||||
(* we moved from SPACE to ALPHA_NUM,
|
|
||||||
* meaning we may have reached a new word. *)
|
|
||||||
if hasPassedSpace then
|
|
||||||
absIdx
|
|
||||||
else
|
|
||||||
helpNextWordString
|
helpNextWordString
|
||||||
( strPos + 1, str, absIdx + 1
|
( strPos + 1, str, absIdx + 1
|
||||||
, strTl, lineTl
|
, strTl, lineTl
|
||||||
, ALPHA_NUM, true
|
, ALPHA_NUM, hasPassedSpace
|
||||||
)
|
)
|
||||||
| NON_BLANK=>
|
| SPACE =>
|
||||||
(* moved from NON_BLANK to ALPHA_NUM,
|
(* we moved from SPACE to ALPHA_NUM,
|
||||||
* meaning we reached new word *)
|
* meaning we may have reached a new word. *)
|
||||||
absIdx
|
if hasPassedSpace then
|
||||||
else if Char.isSpace chr then
|
absIdx
|
||||||
(* nothing to do on space, except keep iterating *)
|
else
|
||||||
helpNextWordString
|
helpNextWordString
|
||||||
( strPos + 1, str, absIdx + 1
|
( strPos + 1, str, absIdx + 1
|
||||||
, strTl, lineTl
|
, strTl, lineTl
|
||||||
, SPACE, true
|
, ALPHA_NUM, true
|
||||||
)
|
)
|
||||||
else
|
| NON_BLANK=>
|
||||||
(* chr is NON_BLANK. *)
|
(* moved from NON_BLANK to ALPHA_NUM,
|
||||||
case wordType of
|
* meaning we reached new word *)
|
||||||
NON_BLANK =>
|
absIdx
|
||||||
(* moved from non-blank to non-blank
|
else if Char.isSpace chr then
|
||||||
* so keep iterating *)
|
(* nothing to do on space, except keep iterating *)
|
||||||
helpNextWordString
|
helpNextWordString
|
||||||
( strPos + 1, str, absIdx + 1
|
( strPos + 1, str, absIdx + 1
|
||||||
, strTl, lineTl
|
, strTl, lineTl
|
||||||
, NON_BLANK, hasPassedSpace
|
, SPACE, true
|
||||||
)
|
)
|
||||||
| ALPHA_NUM =>
|
else
|
||||||
(* moved from ALPHA_NUM to non-blank
|
(* chr is NON_BLANK. *)
|
||||||
* so we have reached new word *)
|
case prevWordType of
|
||||||
absIdx
|
NON_BLANK =>
|
||||||
| SPACE =>
|
(* moved from non-blank to non-blank
|
||||||
(* from space to non-blank *)
|
* so keep iterating *)
|
||||||
if hasPassedSpace then
|
|
||||||
absIdx
|
|
||||||
else
|
|
||||||
helpNextWordString
|
helpNextWordString
|
||||||
( strPos + 1, str, absIdx + 1
|
( strPos + 1, str, absIdx + 1
|
||||||
, strTl, lineTl
|
, strTl, lineTl
|
||||||
, NON_BLANK, true
|
, NON_BLANK, hasPassedSpace
|
||||||
)
|
)
|
||||||
end
|
| ALPHA_NUM =>
|
||||||
|
(* moved from ALPHA_NUM to non-blank
|
||||||
|
* so we have reached new word *)
|
||||||
|
absIdx
|
||||||
|
| SPACE =>
|
||||||
|
(* from space to non-blank *)
|
||||||
|
if hasPassedSpace then
|
||||||
|
absIdx
|
||||||
|
else
|
||||||
|
helpNextWordString
|
||||||
|
( strPos + 1, str, absIdx + 1
|
||||||
|
, strTl, lineTl
|
||||||
|
, NON_BLANK, true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
and helpNextWordList (strings, lines, absIdx, wordType, hasPassedSpace) =
|
and helpNextWordList (strings, lines, absIdx, prevWordType, hasPassedSpace) =
|
||||||
case (strings, lines) of
|
case (strings, lines) of
|
||||||
(strHd::strTl, lineHd::lineTl) =>
|
(strHd::strTl, lineHd::lineTl) =>
|
||||||
helpNextWordString
|
helpNextWordString
|
||||||
( 0, strHd, absIdx, strTl, lineTl, wordType, hasPassedSpace )
|
( 0, strHd, absIdx, strTl, lineTl, prevWordType, hasPassedSpace )
|
||||||
| (_, _) =>
|
| (_, _) =>
|
||||||
(* reached end of lineGap;
|
(* reached end of lineGap;
|
||||||
* return last valid chr position *)
|
* return last valid chr position *)
|
||||||
@@ -976,4 +976,169 @@ struct
|
|||||||
end
|
end
|
||||||
| (_, _) => cursorIdx
|
| (_, _) => cursorIdx
|
||||||
end
|
end
|
||||||
|
|
||||||
|
fun isPrevChrSpace (strPos, str, strTl) =
|
||||||
|
if strPos > 0 then
|
||||||
|
let
|
||||||
|
val prevChr = String.sub (str, strPos - 1)
|
||||||
|
in
|
||||||
|
Char.isSpace prevChr
|
||||||
|
end
|
||||||
|
else
|
||||||
|
case strTl of
|
||||||
|
hd :: _ =>
|
||||||
|
let
|
||||||
|
val prevChr = String.sub (hd, String.size hd - 1)
|
||||||
|
in
|
||||||
|
Char.isSpace prevChr
|
||||||
|
end
|
||||||
|
| [] => true
|
||||||
|
|
||||||
|
fun helpPrevWordString
|
||||||
|
(strPos, str, absIdx, strTl, lineTl, lastWordType) =
|
||||||
|
if strPos < 0 then
|
||||||
|
helpPrevWordList
|
||||||
|
(strTl, lineTl, absIdx, lastWordType)
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val chr = String.sub (str, strPos)
|
||||||
|
in
|
||||||
|
if Char.isAlphaNum chr orelse chr = #"_" then
|
||||||
|
case lastWordType of
|
||||||
|
ALPHA_NUM =>
|
||||||
|
(* check if prev chr is space;
|
||||||
|
* if it is, return current idx
|
||||||
|
* and if not, keep iterating *)
|
||||||
|
if isPrevChrSpace (strPos, str, strTl) then
|
||||||
|
absIdx
|
||||||
|
else
|
||||||
|
helpPrevWordString
|
||||||
|
( strPos - 1, str, absIdx - 1
|
||||||
|
, strTl, lineTl, ALPHA_NUM
|
||||||
|
)
|
||||||
|
| SPACE =>
|
||||||
|
(* last chr checked was space
|
||||||
|
* and this chr is ALPHA_NUM
|
||||||
|
* so keep iterating *)
|
||||||
|
helpPrevWordString
|
||||||
|
( strPos - 1, str, absIdx - 1
|
||||||
|
, strTl, lineTl, ALPHA_NUM
|
||||||
|
)
|
||||||
|
| NON_BLANK =>
|
||||||
|
(* last chr was NON_BLANK
|
||||||
|
* and this chr is ALPHA_NUM
|
||||||
|
* and this change is a word break.
|
||||||
|
* So return idx of last chr. *)
|
||||||
|
absIdx + 1
|
||||||
|
else if Char.isSpace chr then
|
||||||
|
case lastWordType of
|
||||||
|
SPACE =>
|
||||||
|
(* nothing to do on double space,
|
||||||
|
* except keep iterating *)
|
||||||
|
helpPrevWordString
|
||||||
|
( strPos - 1, str, absIdx - 1
|
||||||
|
, strTl, lineTl, SPACE
|
||||||
|
)
|
||||||
|
| _ =>
|
||||||
|
(* last chr was either ALPHA_NUM or NON_BLANK
|
||||||
|
* and current chr is space
|
||||||
|
* so we have word break.
|
||||||
|
* thus, return start of last word *)
|
||||||
|
absIdx + 1
|
||||||
|
else
|
||||||
|
(* is NON_BLANK *)
|
||||||
|
case lastWordType of
|
||||||
|
NON_BLANK =>
|
||||||
|
(* last and current wordType is same
|
||||||
|
* so keep iterating *)
|
||||||
|
if isPrevChrSpace (strPos, str, strTl) then
|
||||||
|
absIdx
|
||||||
|
else
|
||||||
|
helpPrevWordString
|
||||||
|
( strPos - 1, str, absIdx - 1
|
||||||
|
, strTl, lineTl, NON_BLANK
|
||||||
|
)
|
||||||
|
| ALPHA_NUM =>
|
||||||
|
(* word break since we last chr was ALPHA_NUM
|
||||||
|
* and this one is NON_BLANK
|
||||||
|
* so return idx of last chr *)
|
||||||
|
absIdx + 1
|
||||||
|
| SPACE =>
|
||||||
|
(* space means keep iterating *)
|
||||||
|
helpPrevWordString
|
||||||
|
( strPos - 1, str, absIdx - 1
|
||||||
|
, strTl, lineTl, NON_BLANK
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
and helpPrevWordList (strings, lines, absIdx, lastWordType) =
|
||||||
|
case (strings, lines) of
|
||||||
|
(strHd::strTl, lineHd::lineTl) =>
|
||||||
|
helpPrevWordString
|
||||||
|
( String.size strHd - 1, strHd, absIdx
|
||||||
|
, strTl, lineTl, lastWordType
|
||||||
|
)
|
||||||
|
| (_, _) =>
|
||||||
|
(* reached start of lineGap;
|
||||||
|
* return 0 which is start idx *)
|
||||||
|
0
|
||||||
|
|
||||||
|
fun startPrevWord (shd, strIdx, absIdx, stl, ltl) =
|
||||||
|
(* we want to start iterating from previous character
|
||||||
|
* and ignore the character the cursor is at
|
||||||
|
* so check previous character *)
|
||||||
|
if strIdx > 0 then
|
||||||
|
let
|
||||||
|
val prevChr = String.sub (shd, strIdx - 1)
|
||||||
|
val prevWordType = getWordType prevChr
|
||||||
|
in
|
||||||
|
helpPrevWordString
|
||||||
|
(strIdx - 1, shd, absIdx - 1, stl, ltl, prevWordType)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
case (stl, ltl) of
|
||||||
|
(stlhd::stltl, ltlhd::ltltl) =>
|
||||||
|
let
|
||||||
|
val prevIdx = String.size stlhd - 1
|
||||||
|
val prevChr = String.sub (stlhd, prevIdx)
|
||||||
|
val prevWordType = getWordType prevChr
|
||||||
|
in
|
||||||
|
helpPrevWordString
|
||||||
|
(prevIdx, stlhd, absIdx - 1, stltl, ltltl, prevWordType)
|
||||||
|
end
|
||||||
|
| (_, _) =>
|
||||||
|
(* tl is empty; just return idx 0 *)
|
||||||
|
0
|
||||||
|
|
||||||
|
(* equivalent of vi's `b` command *)
|
||||||
|
fun prevWord (lineGap: LineGap.t, cursorIdx) =
|
||||||
|
let
|
||||||
|
val {rightStrings, rightLines, leftStrings, leftLines, idx = bufferIdx, ...} = lineGap
|
||||||
|
in
|
||||||
|
case (rightStrings, rightLines) of
|
||||||
|
(shd :: stl, lhd :: ltl) =>
|
||||||
|
let
|
||||||
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||||
|
val strIdx = cursorIdx - bufferIdx
|
||||||
|
in
|
||||||
|
if strIdx < String.size shd then
|
||||||
|
(* strIdx is in this string *)
|
||||||
|
startPrevWord
|
||||||
|
(shd, strIdx, cursorIdx, leftStrings, leftLines)
|
||||||
|
else
|
||||||
|
(* strIdx is in tl *)
|
||||||
|
(case (stl, ltl) of
|
||||||
|
(stlhd :: stltl, ltlhd :: ltltl) =>
|
||||||
|
let
|
||||||
|
val strIdx = strIdx - String.size shd
|
||||||
|
val leftStrings = shd :: leftStrings
|
||||||
|
val leftLines = lhd :: leftLines
|
||||||
|
in
|
||||||
|
startPrevWord
|
||||||
|
(stlhd, strIdx, cursorIdx, leftStrings, leftLines)
|
||||||
|
end
|
||||||
|
| (_, _) => cursorIdx)
|
||||||
|
end
|
||||||
|
| (_, _) => cursorIdx
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user