diff --git a/fcore/app-type.sml b/fcore/app-type.sml index b6cc065..290ec54 100644 --- a/fcore/app-type.sml +++ b/fcore/app-type.sml @@ -2,7 +2,8 @@ structure AppType = struct datatype mode = NORMAL_MODE of string - | NORMAL_SEARCH_MODE of {searchString: string, tempSearchList: int vector} + | NORMAL_SEARCH_MODE of {searchString: string, tempSearchList: int vector, + searchCursorIdx: int} type app_type = { mode: mode diff --git a/fcore/normal-mode/normal-mode.sml b/fcore/normal-mode/normal-mode.sml index 9e44dc5..052bd5b 100644 --- a/fcore/normal-mode/normal-mode.sml +++ b/fcore/normal-mode/normal-mode.sml @@ -6,14 +6,8 @@ struct open InputMsg fun switchToNormalSearchMode (app: app_type) = - let - val mode = - NORMAL_SEARCH_MODE - {searchString = "", tempSearchList = Vector.fromList []} - in - NormalSearchFinish.onSearchChanged - (app, "", Vector.fromList [], #buffer app) - end + NormalSearchFinish.onSearchChanged + (app, "", Vector.fromList [], 0, #buffer app) fun getNumLength (pos, str) = if pos = String.size str then diff --git a/fcore/normal-mode/normal-search-finish.sml b/fcore/normal-mode/normal-search-finish.sml index 792655a..40b6d8a 100644 --- a/fcore/normal-mode/normal-search-finish.sml +++ b/fcore/normal-mode/normal-search-finish.sml @@ -2,14 +2,17 @@ structure NormalSearchFinish = struct open AppType - fun onSearchChanged (app: app_type, searchString, tempSearchList, buffer) = + fun onSearchChanged + (app: app_type, searchString, tempSearchList, searchCursorIdx, buffer) = let open DrawMsg val {buffer, cursorIdx, startLine, windowWidth, windowHeight, ...} = app - val mode = - NORMAL_SEARCH_MODE - {searchString = searchString, tempSearchList = tempSearchList} + val mode = NORMAL_SEARCH_MODE + { searchString = searchString + , tempSearchList = tempSearchList + , searchCursorIdx = searchCursorIdx + } val floatWindowWidth = Real32.fromInt windowWidth val floatWindowHeight = Real32.fromInt windowHeight diff --git a/fcore/normal-mode/normal-search-mode.sml b/fcore/normal-mode/normal-search-mode.sml index abf3e6e..dbdf7f6 100644 --- a/fcore/normal-mode/normal-search-mode.sml +++ b/fcore/normal-mode/normal-search-mode.sml @@ -4,19 +4,30 @@ struct open InputMsg open MailboxType - fun addChr (app: app_type, searchString, chr) = + fun addChr (app: app_type, searchString, searchCurosrIdx, chr) = let val {cursorIdx, buffer, ...} = app val c = String.implode [chr] - val searchString = searchString ^ c + val searchString = + if searchCurosrIdx = String.size searchString then + searchString ^ c + else + let + val sub1 = Substring.extract (searchString, 0, SOME searchCurosrIdx) + val sub2 = Substring.full c + val sub3 = Substring.extract (searchString, searchCurosrIdx, NONE) + in + Substring.concat [sub1, sub2, sub3] + end + val searchCurosrIdx = searchCurosrIdx + 1 val buffer = LineGap.goToIdx (cursorIdx - 1111, buffer) val tempSearchList = SearchList.buildRange (buffer, searchString, cursorIdx + 1111) in NormalSearchFinish.onSearchChanged - (app, searchString, tempSearchList, buffer) + (app, searchString, tempSearchList, searchCurosrIdx, buffer) end (* return to normal mode, keeping the same searchString and searchList @@ -67,9 +78,9 @@ struct fun backspace (app: app_type, searchString, tempSearchList) = raise Fail "normal-search-mode: KEY_BACKSPACE unimplemented" - fun update (app, {searchString, tempSearchList}, msg, time) = + fun update (app, {searchString, tempSearchList, searchCursorIdx}, msg, time) = case msg of - CHAR_EVENT chr => addChr (app, searchString, chr) + CHAR_EVENT chr => addChr (app, searchString, searchCursorIdx, chr) | KEY_BACKSPACE => backspace (app, searchString, tempSearchList) | KEY_ESC => exitToNormalMode app | KEY_ENTER => saveSearch (app, searchString, tempSearchList)