summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs226
1 files changed, 183 insertions, 43 deletions
diff --git a/src/main.rs b/src/main.rs
index b94b274..59d3baa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,6 +3,7 @@ use crate::types::*;
 use smol::prelude::*;
 
 use smol::fs::File;
+use smol::io::Cursor;
 use smol::lock::RwLock;
 use std::path::PathBuf;
 use std::process::ExitCode;
@@ -40,10 +41,16 @@ struct Window {
   /* The neutral column is the one that vertical movement will attempt to
    * land in, if it exists in the target row.
    */
-  cursor_neutral_column: RwLock<usize>,
+  neutral_column: RwLock<usize>,
   scroll_top: RwLock<usize>,
 }
 
+enum MovementColumnBehavior {
+  CurrentFromNeutral,
+  NeutralFromCurrent,
+  FirstNonBlank,
+}
+
 
 fn main() -> ExitCode {
   smol::block_on(async {
@@ -133,19 +140,30 @@ impl Ivy {
     Ok(())
   }
 
-  async fn draw(&mut self) -> Result<()> {
-    let mut terminal = self.terminal.write().await;
-    let buffer = self.buffer.read().await;
+  async fn draw_all(&mut self) -> Result<()> {
+    let height = *self.terminal.read().await.height.read().await;
 
-    let height = *terminal.height.read().await;
+    self.draw_range(0 .. height - 1).await?;
+    self.draw_status_line().await?;
+    self.fix_cursor_position().await?;
+
+    self.terminal.write().await.stdout.flush().await?;
 
+    Ok(())
+  }
+
+  async fn draw_range(&mut self, range: Range<usize>) -> Result<()> {
+    let mut terminal = self.terminal.write().await;
     let window = self.window.read().await;
+    let buffer = self.buffer.read().await;
     let scroll_top = *window.scroll_top.read().await;
 
-    let mut screen_y = 0;
-    for i in 0 .. height - 1 {
+    terminal.set_cursor_position(0, range.start).await?;
+
+    let mut screen_y = range.start;
+    for i in range.clone() {
       if let Some(span) = buffer.line_span(i + scroll_top).await {
-        if i > 0 {
+        if i > range.start {
           terminal.stdout.write_all("\n".as_bytes()).await?;
         }
 
@@ -157,24 +175,35 @@ impl Ivy {
       }
     }
 
-    for _ in screen_y .. height - 1 {
+    for _ in screen_y .. range.end {
       terminal.stdout.write_all("\n~".as_bytes()).await?;
     }
 
-    terminal.do_cursor_position(0, height).await?;
+    Ok(())
+  }
+
+  async fn draw_status_line(&mut self) -> Result<()> {
+    let mut terminal = self.terminal.write().await;
+    let height = *terminal.height.read().await;
+
+    terminal.set_cursor_position(0, height).await?;
     terminal.stdout.write_all("        status goes here".as_bytes()).await?;
 
+    Ok(())
+  }
+
+  async fn fix_cursor_position(&mut self) -> Result<()> {
+    let mut terminal = self.terminal.write().await;
     let window = self.window.read().await;
-    terminal.do_cursor_position(*window.cursor_column.read().await,
+    terminal.set_cursor_position(*window.cursor_column.read().await,
                                 *window.cursor_row.read().await
                                 - *window.scroll_top.read().await).await?;
-    terminal.stdout.flush().await?;
 
     Ok(())
   }
 
   async fn interact(&mut self) -> Result<()> {
-    self.draw().await?;
+    self.draw_all().await?;
 
     loop {
       let c = self.terminal.write().await.read_char().await?;
@@ -192,10 +221,10 @@ impl Ivy {
                * the movement actually moves. Tentatively, it doesn't look
                * like POSIX has an opinion on this, but it matches Vim.
                */
-              *window.cursor_neutral_column.write().await = *column;
+              Ok(MovementColumnBehavior::NeutralFromCurrent)
+            } else {
+              Ok(MovementColumnBehavior::CurrentFromNeutral)
             }
-
-            Ok(())
           }).await?;
         },
 
@@ -209,7 +238,7 @@ impl Ivy {
               *row += 1;
             };
 
-            Ok(())
+            Ok(MovementColumnBehavior::CurrentFromNeutral)
           }).await?;
         },
 
@@ -222,7 +251,7 @@ impl Ivy {
               *row -= 1;
             }
 
-            Ok(())
+            Ok(MovementColumnBehavior::CurrentFromNeutral)
           }).await?;
         },
 
@@ -243,11 +272,13 @@ impl Ivy {
                  * the movement actually moves. Tentatively, it doesn't look
                  * like POSIX has an opinion on this, but it matches Vim.
                  */
-                *window.cursor_neutral_column.write().await = *column;
+                Ok(MovementColumnBehavior::NeutralFromCurrent)
+              } else {
+                Ok(MovementColumnBehavior::CurrentFromNeutral)
               }
+            } else {
+              Ok(MovementColumnBehavior::CurrentFromNeutral)
             }
-
-            Ok(())
           }).await?;
         },
 
@@ -261,9 +292,7 @@ impl Ivy {
             /* For this one, the neutral column changes regardless of
              * whether the column does.
              */
-            *window.cursor_neutral_column.write().await = *column;
-
-            Ok(())
+            Ok(MovementColumnBehavior::NeutralFromCurrent)
           }).await?;
         },
 
@@ -282,14 +311,43 @@ impl Ivy {
               } else {
                 *column = 0;
               }
+            }
 
-              /* For this one, the neutral column changes regardless of
-               * whether the column does.
-               */
-              *window.cursor_neutral_column.write().await = *column;
+            /* For this one, the neutral column changes regardless of
+             * whether the column does.
+             */
+            Ok(MovementColumnBehavior::NeutralFromCurrent)
+          }).await?;
+        },
+
+        'H' => {
+          self.handle_movement(async |ivy: &mut Ivy| {
+            let window = ivy.window.write().await;
+
+            let mut row = window.cursor_row.write().await;
+            *row = *window.scroll_top.read().await;
+
+            Ok(MovementColumnBehavior::FirstNonBlank)
+          }).await?;
+        },
+
+        'L' => {
+          self.handle_movement(async |ivy: &mut Ivy| {
+            let window = ivy.window.write().await;
+            let terminal = ivy.terminal.read().await;
+            let buffer = ivy.buffer.read().await;
+
+            let scroll_top = *window.scroll_top.read().await;
+            let height = *terminal.height.read().await;
+            let total_lines = buffer.lines.read().await.len();
+
+            let mut row = window.cursor_row.write().await;
+            *row = scroll_top + height - 2;
+            if *row > total_lines - 1 {
+              *row = total_lines - 1;
             }
 
-            Ok(())
+            Ok(MovementColumnBehavior::FirstNonBlank)
           }).await?;
         },
 
@@ -303,9 +361,13 @@ impl Ivy {
   /* A movement is an arbitrarily complicated action that will not change the
    * contents of the buffer, but may change other things, including the cursor
    * position.
+   *
+   * The return value of the action indicates whether to update the neutral
+   * column.
    */
   async fn handle_movement(&mut self,
-                           movement: impl AsyncFn(&mut Ivy) -> Result<()>)
+                           movement: impl AsyncFn(&mut Ivy)
+                                         -> Result<MovementColumnBehavior>)
       -> Result<()>
   {
     let (old_row, old_column) = {
@@ -315,15 +377,28 @@ impl Ivy {
        *window.cursor_column.read().await)
     };
 
-    movement(self).await?;
-
-    /* We clamp the column to the line width unconditionally, regardless of
-     * what kind of movement we did. This is always correct, because no
-     * intended behavior ever places the cursor beyond the end of the line.
-     * It does require taking a couple of locks that we might always need, but
-     * there's no strong reason to avoid that.
-     */
-    self.clamp_cursor_column().await?;
+    let column_behavior = movement(self).await?;
+
+    match column_behavior {
+      MovementColumnBehavior::NeutralFromCurrent => {
+        /* We clamp the destination column to the line width and use that as
+         * the neutral column.
+         */
+        self.update_neutral_column().await?;
+      },
+      MovementColumnBehavior::CurrentFromNeutral => {
+        /* We clamp the neutral column to the line width and use that as the
+         * destination column.
+         */
+        self.clamp_cursor_column().await?;
+      },
+      MovementColumnBehavior::FirstNonBlank => {
+        /* We compute the first non-blank column on the line and use that as
+         * both the destination and neutral columns.
+         */
+        self.first_non_blank_column().await?;
+      },
+    }
 
     self.scroll_to_cursor().await?;
 
@@ -336,7 +411,7 @@ impl Ivy {
         let mut terminal = self.terminal.write().await;
         let scroll_top = *window.scroll_top.read().await;
 
-        terminal.do_cursor_position(column, row - scroll_top).await?;
+        terminal.set_cursor_position(column, row - scroll_top).await?;
         terminal.stdout.flush().await?;
       }
     }
@@ -344,6 +419,28 @@ impl Ivy {
     Ok(())
   }
 
+  async fn update_neutral_column(&mut self) -> Result<()> {
+    let buffer = self.buffer.write().await;
+    let window = self.window.write().await;
+    let mut column = window.cursor_column.write().await;
+    let mut neutral_column = window.neutral_column.write().await;
+    let row = window.cursor_row.read().await;
+
+    if let Some(span) = buffer.line_span(*row).await {
+      let width = span.end - span.start;
+
+      if width == 0 {
+        *column = 0;
+      } else if *column > width - 1 {
+        *column = width - 1;
+      }
+    }
+
+    *neutral_column = *column;
+
+    Ok(())
+  }
+
   async fn clamp_cursor_column(&mut self) -> Result<()> {
     let window = self.window.write().await;
     let row = window.cursor_row.read().await;
@@ -352,7 +449,7 @@ impl Ivy {
 
     if let Some(span) = buffer.line_span(*row).await {
       let width = span.end - span.start;
-      let neutral_column = *window.cursor_neutral_column.read().await;
+      let neutral_column = *window.neutral_column.read().await;
 
       if neutral_column < width {
         *column = neutral_column;
@@ -366,6 +463,42 @@ impl Ivy {
     Ok(())
   }
 
+  async fn first_non_blank_column(&mut self) -> Result<()> {
+    let row = *self.window.read().await.cursor_row.read().await;
+    let buffer = self.buffer.write().await;
+
+    if let Some(row_span) = buffer.line_span(row).await {
+      let mut offset = 0;
+      loop {
+        let sub_span = row_span.start + offset .. row_span.end;
+        let mut contents = buffer.contents.write().await;
+        let mut cursor = Cursor::new(&mut contents[sub_span]);
+
+        if let Ok(decode) = encoding::read_utf8_char(&mut cursor).await {
+          offset += decode.skipped_bytes;
+
+          if decode.c.is_whitespace() {
+            offset += decode.found_bytes;
+          } else {
+            break;
+          }
+        } else {
+          break;
+        }
+      }
+
+      let window = self.window.write().await;
+      *window.cursor_column.write().await = offset;
+      *window.neutral_column.write().await = offset;
+    } else {
+      let window = self.window.write().await;
+      *window.cursor_column.write().await = 0;
+      *window.neutral_column.write().await = 0;
+    }
+
+    Ok(())
+  }
+
   async fn scroll_to_cursor(&mut self) -> Result<()> {
     let old_scroll_top = *self.window.read().await.scroll_top.read().await;
 
@@ -391,9 +524,16 @@ impl Ivy {
       let difference = new_scroll_top as isize - old_scroll_top as isize;
       if (difference.abs() as usize) < height {
         self.terminal.write().await.scroll(difference).await?;
+
+        if difference > 0 {
+          self.draw_range((height as isize - difference) as usize
+                          .. height).await?;
+        } else {
+          self.draw_range(0 .. difference.abs() as usize).await?;
+        }
       } else {
         self.terminal.write().await.clear().await?;
-        self.draw().await?;
+        self.draw_all().await?;
       }
     }
 
@@ -489,7 +629,7 @@ impl Window {
     Window {
       cursor_row: RwLock::new(0),
       cursor_column: RwLock::new(0),
-      cursor_neutral_column: RwLock::new(0),
+      neutral_column: RwLock::new(0),
       scroll_top: RwLock::new(0),
     }
   }