]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/FileBuffer.c
ShellPkg: Make the USB mouse behavior in 'edit' consistent with 'hexedit'.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / Edit / FileBuffer.c
index 5843c7807d570817dcb5845ded56ec3fcd58f15a..fb0c76e2e9db75a680ece56706793907530625d7 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implements filebuffer interface functions.\r
 \r
-  Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved. <BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -53,7 +53,7 @@ EFI_EDITOR_FILE_BUFFER  FileBufferConst = {
 //\r
 // the whole edit area needs to be refreshed\r
 //\r
-STATIC BOOLEAN          FileBufferNeedRefresh;\r
+BOOLEAN          FileBufferNeedRefresh;        \r
 \r
 //\r
 // only the current line in edit area needs to be refresh\r
@@ -247,15 +247,80 @@ MoveLine (
   // if > 0, the advance\r
   //\r
   if (Count <= 0) {\r
-    AbsCount  = -Count;\r
+    AbsCount  = (UINTN)ABS(Count);\r
     Line      = InternalEditorMiscLineRetreat (AbsCount,MainEditor.FileBuffer->CurrentLine,MainEditor.FileBuffer->ListHead);\r
   } else {\r
-    Line = InternalEditorMiscLineAdvance (Count,MainEditor.FileBuffer->CurrentLine,MainEditor.FileBuffer->ListHead);\r
+    Line = InternalEditorMiscLineAdvance ((UINTN)Count,MainEditor.FileBuffer->CurrentLine,MainEditor.FileBuffer->ListHead);\r
   }\r
 \r
   return Line;\r
 }\r
 \r
+/**\r
+  Decide if a point is in the already selected area.\r
+\r
+  @param[in] MouseRow     The row of the point to test.\r
+  @param[in] MouseCol     The col of the point to test.\r
+\r
+  @retval TRUE      The point is in the selected area.\r
+  @retval FALSE     The point is not in the selected area.\r
+**/\r
+BOOLEAN\r
+FileBufferIsInSelectedArea (\r
+  IN UINTN MouseRow,\r
+  IN UINTN MouseCol\r
+  )\r
+{\r
+  UINTN FRow;\r
+  UINTN RowStart;\r
+  UINTN RowEnd;\r
+  UINTN ColStart;\r
+  UINTN ColEnd;\r
+  UINTN MouseColStart;\r
+  UINTN MouseColEnd;\r
+\r
+  //\r
+  // judge mouse position whether is in selected area\r
+  //\r
+  //\r
+  // not select\r
+  //\r
+  if (MainEditor.SelectStart == 0 || MainEditor.SelectEnd == 0) {\r
+    return FALSE;\r
+  }\r
+  //\r
+  // calculate the select area\r
+  //\r
+  RowStart  = (MainEditor.SelectStart - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+  RowEnd    = (MainEditor.SelectEnd - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+\r
+  ColStart  = (MainEditor.SelectStart - 1) % SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+  ColEnd    = (MainEditor.SelectEnd - 1) % SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+\r
+  FRow      = FileBuffer.LowVisibleRange.Row + MouseRow - 2;\r
+  if (FRow < RowStart || FRow > RowEnd) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (FRow > RowStart) {\r
+    ColStart = 1;\r
+  }\r
+\r
+  if (FRow < RowEnd) {\r
+    ColEnd = SHELL_EDIT_MAX_LINE_SIZE;\r
+  }\r
+\r
+  MouseColStart = ColStart;\r
+\r
+  MouseColEnd = ColEnd;\r
+\r
+  if (MouseCol < MouseColStart || MouseCol > MouseColEnd) {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
 /**\r
   Function to update the 'screen' to display the mouse position.\r
 \r
@@ -300,8 +365,9 @@ FileBufferRestoreMousePosition (
       // backup the old screen attributes\r
       //\r
       Orig                  = MainEditor.ColorAttributes;\r
-      New.Colors.Foreground = Orig.Colors.Background;\r
-      New.Colors.Background = Orig.Colors.Foreground;\r
+      New.Data              = 0;\r
+      New.Colors.Foreground = Orig.Colors.Background & 0xF;\r
+      New.Colors.Background = Orig.Colors.Foreground & 0x7;\r
 \r
       //\r
       // clear the old mouse position\r
@@ -310,6 +376,29 @@ FileBufferRestoreMousePosition (
 \r
       FColumn       = FileBuffer.LowVisibleRange.Column + FileBufferBackupVar.MousePosition.Column - 1;\r
 \r
+      if (FRow <= FileBuffer.NumLines) {\r
+        CurrentLine = FileBuffer.CurrentLine;\r
+        Line        = MoveLine (FRow - FileBuffer.FilePosition.Row);\r
+        FileBuffer.CurrentLine = CurrentLine;\r
+      }\r
+\r
+      //\r
+      // if in selected area,\r
+      // so do not need to refresh mouse\r
+      //\r
+      if (!FileBufferIsInSelectedArea (\r
+            FileBufferBackupVar.MousePosition.Row,\r
+            FileBufferBackupVar.MousePosition.Column\r
+            ) ||\r
+          (Line == NULL || FColumn > Line->Size)\r
+      ) {\r
+        gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);\r
+      } else {\r
+        gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);\r
+      }\r
+\r
+      Line = NULL;\r
+\r
       HasCharacter  = TRUE;\r
       if (FRow > FileBuffer.NumLines) {\r
         HasCharacter = FALSE;\r
@@ -317,7 +406,7 @@ FileBufferRestoreMousePosition (
         CurrentLine = FileBuffer.CurrentLine;\r
         Line        = MoveLine (FRow - FileBuffer.FilePosition.Row);\r
 \r
-        if (FColumn > Line->Size) {\r
+        if (Line == NULL || FColumn > Line->Size) {\r
           HasCharacter = FALSE;\r
         }\r
 \r
@@ -342,8 +431,15 @@ FileBufferRestoreMousePosition (
       //\r
       // set the new mouse position\r
       //\r
-      gST->ConOut->SetAttribute (gST->ConOut, New.Data);\r
-\r
+      if (!FileBufferIsInSelectedArea (\r
+            FileBuffer.MousePosition.Row,\r
+            FileBuffer.MousePosition.Column\r
+            )\r
+      ) {\r
+        gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);\r
+      } else {\r
+        gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);\r
+      }\r
       //\r
       // clear the old mouse position\r
       //\r
@@ -357,7 +453,7 @@ FileBufferRestoreMousePosition (
         CurrentLine = FileBuffer.CurrentLine;\r
         Line        = MoveLine (FRow - FileBuffer.FilePosition.Row);\r
 \r
-        if (FColumn > Line->Size) {\r
+        if (Line == NULL || FColumn > Line->Size) {\r
           HasCharacter = FALSE;\r
         }\r
 \r
@@ -471,24 +567,68 @@ FileBufferCleanup (
 \r
 }\r
 \r
+\r
 /**\r
-  Print a line specified by Line on a row specified by Row of the screen.\r
+  Print Line on Row\r
 \r
-  @param[in] Line               The line to print.\r
-  @param[in] Row                The row on the screen to print onto (begin from 1).\r
+  @param[in] Line     The lline to print.\r
+  @param[in] Row      The row on screen ( begin from 1 ).\r
+  @param[in] FRow     The FRow.\r
+  @param[in] Orig     The original color.\r
+  @param[in] New      The color to print with.\r
 \r
-  @retval EFI_SUCCESS           The printing was successful.\r
+  @retval EFI_SUCCESS The operation was successful.\r
 **/\r
 EFI_STATUS\r
 FileBufferPrintLine (\r
-  IN CONST EFI_EDITOR_LINE  *Line,\r
-  IN CONST UINTN            Row\r
+  IN CONST EFI_EDITOR_LINE        *Line,\r
+  IN CONST  UINTN                 Row,\r
+  IN CONST  UINTN                 FRow,\r
+  IN EFI_EDITOR_COLOR_UNION       Orig,\r
+  IN EFI_EDITOR_COLOR_UNION       New\r
   )\r
 {\r
 \r
   CHAR16  *Buffer;\r
   UINTN   Limit;\r
-  CHAR16  PrintLine[200];\r
+  CHAR16  *PrintLine;\r
+  CHAR16  *PrintLine2;\r
+  CHAR16  *TempString;\r
+  UINTN   BufLen;\r
+  BOOLEAN Selected;\r
+  UINTN   RowStart;\r
+  UINTN   RowEnd;\r
+  UINTN   ColStart;\r
+  UINTN   ColEnd;\r
+\r
+  ColStart    = 0;\r
+  ColEnd      = 0;\r
+  Selected    = FALSE;\r
+  TempString  = NULL;\r
+\r
+  //\r
+  // print the selected area in opposite color\r
+  //\r
+  if (MainEditor.SelectStart != 0 && MainEditor.SelectEnd != 0) {\r
+    RowStart  = (MainEditor.SelectStart - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+    RowEnd    = (MainEditor.SelectEnd - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+\r
+    ColStart  = (MainEditor.SelectStart - 1) % SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+    ColEnd    = (MainEditor.SelectEnd - 1) % SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+\r
+    if (FRow >= RowStart && FRow <= RowEnd) {\r
+      Selected = TRUE;\r
+    }\r
+\r
+    if (FRow > RowStart) {\r
+      ColStart = 1;\r
+    }\r
+\r
+    if (FRow < RowEnd) {\r
+      ColEnd = Line->Size + 1;\r
+    }\r
+\r
+  }\r
 \r
   //\r
   // print start from correct character\r
@@ -500,19 +640,80 @@ FileBufferPrintLine (
     Limit = 0;\r
   }\r
 \r
-  StrnCpy (PrintLine, Buffer, Limit > MainEditor.ScreenSize.Column ? MainEditor.ScreenSize.Column : Limit);\r
+  BufLen = (MainEditor.ScreenSize.Column + 1) * sizeof (CHAR16);\r
+  PrintLine = AllocatePool (BufLen);\r
+  ASSERT (PrintLine != NULL);\r
+\r
+  StrnCpyS (PrintLine, BufLen/sizeof(CHAR16), Buffer, MIN(Limit, MainEditor.ScreenSize.Column));\r
   for (; Limit < MainEditor.ScreenSize.Column; Limit++) {\r
     PrintLine[Limit] = L' ';\r
   }\r
 \r
   PrintLine[MainEditor.ScreenSize.Column] = CHAR_NULL;\r
 \r
-  ShellPrintEx (\r
-    0,\r
-    (INT32)Row - 1,\r
-    L"%s",\r
-    PrintLine\r
-    );\r
+  PrintLine2 = AllocatePool (BufLen * 2);\r
+  ASSERT (PrintLine2 != NULL);\r
+\r
+  ShellCopySearchAndReplace(PrintLine, PrintLine2, BufLen * 2, L"%", L"^%", FALSE, FALSE);\r
+\r
+  if (!Selected) {\r
+    ShellPrintEx (\r
+      0,\r
+      (INT32)Row - 1,\r
+      L"%s",\r
+      PrintLine2\r
+      );\r
+  } else {\r
+    //\r
+    // If the current line is selected.\r
+    //\r
+    if (ColStart != 1) {\r
+      gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);\r
+      TempString = AllocateCopyPool ((ColStart - 1) * sizeof(CHAR16), PrintLine2);\r
+      ASSERT (TempString != NULL);\r
+      ShellPrintEx (\r
+        0,\r
+        (INT32)Row - 1,\r
+        L"%s",\r
+        TempString\r
+      );\r
+      FreePool (TempString);\r
+    }\r
+\r
+    gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);\r
+    TempString = AllocateCopyPool (\r
+                  (ColEnd - ColStart + 1)  * sizeof(CHAR16),\r
+                  PrintLine2 + ColStart - 1\r
+                  );\r
+    ASSERT (TempString != NULL);\r
+    ShellPrintEx (\r
+      (INT32)ColStart - 1,\r
+      (INT32)Row - 1,\r
+      L"%s",\r
+      TempString\r
+      );\r
+    FreePool (TempString);\r
+\r
+    if (ColEnd != SHELL_EDIT_MAX_LINE_SIZE) {\r
+      gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);\r
+      TempString = AllocateCopyPool (\r
+                    (SHELL_EDIT_MAX_LINE_SIZE - ColEnd + 1)  * sizeof(CHAR16),\r
+                    PrintLine2 + ColEnd - 1\r
+                    );\r
+      ASSERT (TempString != NULL);\r
+      ShellPrintEx (\r
+        (INT32)ColEnd - 1,\r
+        (INT32)Row - 1,\r
+        L"%s",\r
+        TempString\r
+      );\r
+      FreePool (TempString);\r
+    }\r
+  }\r
+\r
+  FreePool (PrintLine);\r
+  FreePool (PrintLine2);\r
+  gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -553,6 +754,18 @@ FileBufferRefresh (
   LIST_ENTRY  *Link;\r
   EFI_EDITOR_LINE *Line;\r
   UINTN           Row;\r
+  EFI_EDITOR_COLOR_UNION Orig;\r
+  EFI_EDITOR_COLOR_UNION New;\r
+\r
+  UINTN                   StartRow;\r
+  UINTN                   EndRow;\r
+  UINTN                   FStartRow;\r
+  UINTN                   Tmp;\r
+\r
+  Orig                  = MainEditor.ColorAttributes;\r
+  New.Data              = 0;\r
+  New.Colors.Foreground = Orig.Colors.Background;\r
+  New.Colors.Background = Orig.Colors.Foreground;\r
 \r
   //\r
   // if it's the first time after editor launch, so should refresh\r
@@ -564,13 +777,10 @@ FileBufferRefresh (
     //\r
     if (!FileBufferNeedRefresh &&\r
         !FileBufferOnlyLineNeedRefresh &&\r
-        FileBufferBackupVar.LowVisibleRange.Row == FileBuffer.LowVisibleRange.Row &&\r
-        FileBufferBackupVar.LowVisibleRange.Column == FileBuffer.LowVisibleRange.Column\r
+        FileBufferBackupVar.LowVisibleRange.Row == FileBuffer.LowVisibleRange.Row\r
         ) {\r
-\r
       FileBufferRestoreMousePosition ();\r
       FileBufferRestorePosition ();\r
-\r
       return EFI_SUCCESS;\r
     }\r
   }\r
@@ -581,19 +791,60 @@ FileBufferRefresh (
   // only need to refresh current line\r
   //\r
   if (FileBufferOnlyLineNeedRefresh &&\r
-      FileBufferBackupVar.LowVisibleRange.Row == FileBuffer.LowVisibleRange.Row &&\r
-      FileBufferBackupVar.LowVisibleRange.Column == FileBuffer.LowVisibleRange.Column\r
+      FileBufferBackupVar.LowVisibleRange.Row == FileBuffer.LowVisibleRange.Row\r
       ) {\r
 \r
     EditorClearLine (FileBuffer.DisplayPosition.Row, MainEditor.ScreenSize.Column, MainEditor.ScreenSize.Row);\r
     FileBufferPrintLine (\r
       FileBuffer.CurrentLine,\r
-      FileBuffer.DisplayPosition.Row\r
+      FileBuffer.DisplayPosition.Row,\r
+      FileBuffer.FilePosition.Row,\r
+      Orig,\r
+      New\r
       );\r
   } else {\r
     //\r
     // the whole edit area need refresh\r
     //\r
+    if (EditorMouseAction && MainEditor.SelectStart != 0 && MainEditor.SelectEnd != 0) {\r
+      if (MainEditor.SelectStart != MainEditorBackupVar.SelectStart) {\r
+        if (MainEditor.SelectStart >= MainEditorBackupVar.SelectStart && MainEditorBackupVar.SelectStart != 0) {\r
+          StartRow = (MainEditorBackupVar.SelectStart - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+        } else {\r
+          StartRow = (MainEditor.SelectStart - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+        }\r
+      } else {\r
+        StartRow = (MainEditor.SelectStart - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+      }\r
+\r
+      if (MainEditor.SelectEnd <= MainEditorBackupVar.SelectEnd) {\r
+        EndRow = (MainEditorBackupVar.SelectEnd - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+      } else {\r
+        EndRow = (MainEditor.SelectEnd - 1) / SHELL_EDIT_MAX_LINE_SIZE + 1;\r
+      }\r
+\r
+      //\r
+      // swap\r
+      //\r
+      if (StartRow > EndRow) {\r
+        Tmp       = StartRow;\r
+        StartRow  = EndRow;\r
+        EndRow    = Tmp;\r
+      }\r
+\r
+      FStartRow = StartRow;\r
+\r
+      StartRow  = 2 + StartRow - FileBuffer.LowVisibleRange.Row;\r
+      EndRow    = 2 + EndRow - FileBuffer.LowVisibleRange.Row;\r
+\r
+    } else {\r
+      //\r
+      // not mouse selection actions\r
+      //\r
+      FStartRow = FileBuffer.LowVisibleRange.Row;\r
+      StartRow  = 2;\r
+      EndRow    = (MainEditor.ScreenSize.Row - 1);\r
+    }\r
 \r
     //\r
     // no line\r
@@ -623,15 +874,20 @@ FileBufferRefresh (
       //\r
       // print line at row\r
       //\r
-      FileBufferPrintLine (Line, Row);\r
+      FileBufferPrintLine (Line,\r
+        Row,\r
+        FileBuffer.LowVisibleRange.Row + Row - 2,\r
+        Orig,\r
+        New\r
+        );\r
 \r
       Link = Link->ForwardLink;\r
       Row++;\r
-    } while (Link != FileBuffer.ListHead && Row <= (MainEditor.ScreenSize.Row - 4));\r
+    } while (Link != FileBuffer.ListHead && Row <= EndRow);\r
     //\r
     // while not file end and not screen full\r
     //\r
-    while (Row <= (MainEditor.ScreenSize.Row - 4)) {\r
+    while (Row <= EndRow) {\r
       EditorClearLine (Row, MainEditor.ScreenSize.Column, MainEditor.ScreenSize.Row);\r
       Row++;\r
     }\r
@@ -1367,7 +1623,7 @@ GetNewLine (
   Change a Unicode string to an ASCII string.\r
 \r
   @param[in] UStr     The Unicode string.\r
-  @param[in] Lenght   The maximum size of AStr.\r
+  @param[in] Length   The maximum size of AStr.\r
   @param[out] AStr    ASCII string to pass out.\r
 \r
   @return The actuall length.\r
@@ -1375,9 +1631,9 @@ GetNewLine (
 UINTN\r
 EFIAPI\r
 UnicodeToAscii (\r
-  IN CONST CHAR16  *UStr,\r
-  IN CONST UINTN   Length,\r
-  OUT CHAR8   *AStr\r
+  IN CONST CHAR16   *UStr,\r
+  IN CONST UINTN    Length,\r
+  OUT CHAR8         *AStr\r
   )\r
 {\r
   UINTN Index;\r
@@ -1446,7 +1702,7 @@ FileBufferSave (
   //\r
   // if is the old file\r
   //\r
-  if (StrCmp (FileName, FileBuffer.FileName) == 0) {\r
+  if (FileBuffer.FileName != NULL && StrCmp (FileName, FileBuffer.FileName) == 0) {\r
     //\r
     // file has not been modified\r
     //\r
@@ -1621,7 +1877,7 @@ FileBufferSave (
   //\r
   // now everything is ready , you can set the new file name to filebuffer\r
   //\r
-  if (StrCmp (FileName, FileBuffer.FileName) != 0) {\r
+  if (FileName != NULL && FileBuffer.FileName != NULL && StrCmp (FileName, FileBuffer.FileName) != 0) {\r
     //\r
     // not the same\r
     //\r
@@ -1685,8 +1941,8 @@ FileBufferScrollLeft (
 /**\r
   Delete a char in line\r
 \r
-  @param[in,out] Line   The line to delete in.\r
-  @param[in] Pos        Position to delete the char at ( start from 0 ).\r
+  @param[in, out] Line   The line to delete in.\r
+  @param[in] Pos         Position to delete the char at ( start from 0 ).\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1710,8 +1966,8 @@ LineDeleteAt (
 /**\r
   Concatenate Src into Dest.\r
 \r
-  @param[in,out] Dest   Destination string\r
-  @param[in] Src        Src String.\r
+  @param[in, out] Dest   Destination string\r
+  @param[in] Src         Src String.\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -2300,8 +2556,8 @@ FileBufferPageDown (
   //\r
   // has next page\r
   //\r
-  if (FileBuffer.NumLines >= FRow + (MainEditor.ScreenSize.Row - 5)) {\r
-    Gap = (MainEditor.ScreenSize.Row - 5);\r
+  if (FileBuffer.NumLines >= FRow + (MainEditor.ScreenSize.Row - 2)) {\r
+    Gap = (MainEditor.ScreenSize.Row - 2);\r
   } else {\r
     //\r
     // MOVE CURSOR TO LAST LINE\r
@@ -2316,7 +2572,7 @@ FileBufferPageDown (
   //\r
   // if that line, is not that long, so move to the end of that line\r
   //\r
-  if (FCol > Line->Size) {\r
+  if (Line != NULL && FCol > Line->Size) {\r
     FCol = Line->Size + 1;\r
   }\r
 \r
@@ -2352,8 +2608,8 @@ FileBufferPageUp (
   //\r
   // has previous page\r
   //\r
-  if (FRow > (MainEditor.ScreenSize.Row - 5)) {\r
-    Gap = (MainEditor.ScreenSize.Row - 5);\r
+  if (FRow > (MainEditor.ScreenSize.Row - 2)) {\r
+    Gap = (MainEditor.ScreenSize.Row - 2);\r
   } else {\r
     //\r
     // the first line of file will displayed on the first line of screen\r
@@ -2372,7 +2628,7 @@ FileBufferPageUp (
   //\r
   // if that line is not that long, so move to the end of that line\r
   //\r
-  if (FCol > Line->Size) {\r
+  if (Line != NULL && FCol > Line->Size) {\r
     FCol = Line->Size + 1;\r
   }\r
 \r
@@ -2575,7 +2831,7 @@ UnderCurrentScreen (
   //\r
   // if is to the under of the screen\r
   //\r
-  if (FileRow > FileBuffer.LowVisibleRange.Row + (MainEditor.ScreenSize.Row - 5) - 1) {\r
+  if (FileRow > FileBuffer.LowVisibleRange.Row + (MainEditor.ScreenSize.Row - 2) - 1) {\r
     return TRUE;\r
   }\r
 \r
@@ -2650,10 +2906,10 @@ MoveCurrentLine (
   UINTN           AbsCount;\r
 \r
   if (Count <= 0) {\r
-    AbsCount  = -Count;\r
+    AbsCount  = (UINTN)ABS(Count);\r
     Line      = InternalEditorMiscLineRetreat (AbsCount,MainEditor.FileBuffer->CurrentLine,MainEditor.FileBuffer->ListHead);\r
   } else {\r
-    Line = InternalEditorMiscLineAdvance (Count,MainEditor.FileBuffer->CurrentLine,MainEditor.FileBuffer->ListHead);\r
+    Line = InternalEditorMiscLineAdvance ((UINTN)Count,MainEditor.FileBuffer->CurrentLine,MainEditor.FileBuffer->ListHead);\r
   }\r
 \r
   if (Line == NULL) {\r
@@ -2720,7 +2976,7 @@ FileBufferMovePosition (
       //\r
       FileBuffer.FilePosition.Row = NewFilePosRow;\r
       if (RowGap < 0) {\r
-        Abs = -RowGap;\r
+        Abs = (UINTN)ABS(RowGap);\r
         FileBuffer.DisplayPosition.Row -= Abs;\r
       } else {\r
         FileBuffer.DisplayPosition.Row += RowGap;\r
@@ -2760,7 +3016,7 @@ FileBufferMovePosition (
       //\r
       FileBuffer.FilePosition.Column = NewFilePosCol;\r
       if (ColGap < 0) {\r
-        Abs = -ColGap;\r
+        Abs = (UINTN)(-ColGap);\r
         FileBuffer.DisplayPosition.Column -= Abs;\r
       } else {\r
         FileBuffer.DisplayPosition.Column += ColGap;\r
@@ -2956,7 +3212,8 @@ FileBufferSearch (
   BOOLEAN         Found;\r
 \r
   Column = 0;\r
-\r
+  Position = 0;\r
+  \r
   //\r
   // search if in current line\r
   //\r
@@ -2969,20 +3226,20 @@ FileBufferSearch (
     Current = FileBuffer.CurrentLine->Buffer + FileBuffer.CurrentLine->Size;\r
   }\r
 \r
+  Found = FALSE;\r
+\r
   CharPos  =  StrStr (Current, Str);\r
   if (CharPos != NULL) {\r
-    Position = CharPos - Current;\r
-  } else {\r
-    Position = 0;\r
-  }\r
+    Position = CharPos - Current + 1;\r
+    Found   = TRUE;\r
+  } \r
 \r
   //\r
   // found\r
   //\r
-  if (Position != 0) {\r
+  if (Found) {\r
     Column  = (Position - 1) + FileBuffer.FilePosition.Column + Offset;\r
     Row     = FileBuffer.FilePosition.Row;\r
-    Found   = TRUE;\r
   } else {\r
     //\r
     // not found so find through next lines\r
@@ -2995,9 +3252,11 @@ FileBufferSearch (
 //      Position  = StrStr (Line->Buffer, Str);\r
       CharPos  =  StrStr (Line->Buffer, Str);\r
       if (CharPos != NULL) {\r
-        Position = CharPos - Line->Buffer;\r
-      }\r
-      if (Position != 0) {\r
+        Position = CharPos - Line->Buffer + 1;\r
+        Found   = TRUE;\r
+      } \r
+      \r
+      if (Found) {\r
         //\r
         // found\r
         //\r
@@ -3096,14 +3355,14 @@ FileBufferReplace (
     //\r
     // set replace into it\r
     //\r
-    Buffer = FileBuffer.CurrentLine->Buffer + FileBuffer.FilePosition.Column;\r
+    Buffer = FileBuffer.CurrentLine->Buffer + FileBuffer.FilePosition.Column - 1;\r
     for (Index = 0; Index < ReplaceLen; Index++) {\r
       Buffer[Index] = Replace[Index];\r
     }\r
   }\r
 \r
   if (ReplaceLen < SearchLen) {\r
-    Buffer = FileBuffer.CurrentLine->Buffer + FileBuffer.FilePosition.Column;\r
+    Buffer = FileBuffer.CurrentLine->Buffer + FileBuffer.FilePosition.Column - 1;\r
 \r
     for (Index = 0; Index < ReplaceLen; Index++) {\r
       Buffer[Index] = Replace[Index];\r
@@ -3122,7 +3381,7 @@ FileBufferReplace (
   }\r
 \r
   if (ReplaceLen == SearchLen) {\r
-    Buffer = FileBuffer.CurrentLine->Buffer + FileBuffer.FilePosition.Column;\r
+    Buffer = FileBuffer.CurrentLine->Buffer + FileBuffer.FilePosition.Column - 1;\r
     for (Index = 0; Index < ReplaceLen; Index++) {\r
       Buffer[Index] = Replace[Index];\r
     }\r
@@ -3134,7 +3393,7 @@ FileBufferReplace (
 \r
   FileBuffer.FileModified       = TRUE;\r
 \r
-  MainTitleBarRefresh (MainEditor.FileBuffer->FileName, MainEditor.FileBuffer->FileType, MainEditor.FileBuffer->ReadOnly, MainEditor.FileBuffer->FileModified, MainEditor.ScreenSize.Column, MainEditor.ScreenSize.Row);\r
+  MainTitleBarRefresh (MainEditor.FileBuffer->FileName, MainEditor.FileBuffer->FileType, MainEditor.FileBuffer->ReadOnly, MainEditor.FileBuffer->FileModified, MainEditor.ScreenSize.Column, MainEditor.ScreenSize.Row, 0, 0);\r
   FileBufferRestorePosition ();\r
   FileBufferRefresh ();\r
 \r
@@ -3207,12 +3466,12 @@ FileBufferAdjustMousePosition (
   // check whether new mouse row position is beyond screen\r
   // if not, adjust it\r
   //\r
-  if (CoordinateY >= 2 && CoordinateY <= (MainEditor.ScreenSize.Row - 4)) {\r
+  if (CoordinateY >= 2 && CoordinateY <= (MainEditor.ScreenSize.Row - 1)) {\r
     FileBuffer.MousePosition.Row = CoordinateY;\r
   } else if (CoordinateY < 2) {\r
     FileBuffer.MousePosition.Row = 2;\r
-  } else if (CoordinateY > (MainEditor.ScreenSize.Row - 4)) {\r
-    FileBuffer.MousePosition.Row = (MainEditor.ScreenSize.Row - 4);\r
+  } else if (CoordinateY > (MainEditor.ScreenSize.Row - 1)) {\r
+    FileBuffer.MousePosition.Row = (MainEditor.ScreenSize.Row - 1);\r
   }\r
 \r
 }\r
@@ -3351,3 +3610,39 @@ FileBufferSetModified (
   FileBuffer.FileModified = TRUE;\r
 }\r
 \r
+\r
+/**\r
+  Get the size of the open buffer.\r
+\r
+  @retval The size in bytes.\r
+**/\r
+UINTN\r
+FileBufferGetTotalSize (\r
+  VOID\r
+  )\r
+{\r
+  UINTN             Size;\r
+\r
+  EFI_EDITOR_LINE   *Line;\r
+\r
+  //\r
+  // calculate the total size of whole line list's buffer\r
+  //\r
+  if (FileBuffer.Lines == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  Line = CR (\r
+          FileBuffer.ListHead->BackLink,\r
+          EFI_EDITOR_LINE,\r
+          Link,\r
+          LINE_LIST_SIGNATURE\r
+          );\r
+  //\r
+  // one line at most 0x50\r
+  //\r
+  Size = SHELL_EDIT_MAX_LINE_SIZE * (FileBuffer.NumLines - 1) + Line->Size;\r
+\r
+  return Size;\r
+}\r
+\r