-- UnivArgument is in Yi.Keymap.Emacs.Utils but we can't import it due
-- to cyclic imports.
-- | emacs' @delete-horizontal-space@ with the optional argument.
deleteHorizontalSpaceB :: Maybe Int -> BufferM ()
deleteHorizontalSpaceB u = do
  c <- curCol
  reg <- regionOfB Line
  text <- readRegionB reg
  let (r, jb) = deleteSpaces c text
  modifyRegionB (const r) reg
  -- Jump backwards to where the now-deleted spaces have started so
  -- it's consistent and feels natural instead of leaving us somewhere
  -- in the text.
  moveToColB $ c - jb
  where
    deleteSpaces :: Int -> R.YiString -> (R.YiString, Int)
    deleteSpaces c l =
      let (f, b) = R.splitAt c l
          f' = R.dropWhileEnd isSpace f
          cleaned = f' <> case u of
            Nothing -> R.dropWhile isSpace b
            Just _ -> b
      -- We only want to jump back the number of spaces before the
      -- point, not the total number of characters we're removing.
      in (cleaned, R.length f - R.length f')

----------------------------------------
-- Transform operations

-- | capitalise the word under the cursor
deleteHorizontalSpaceB :: Maybe Int -> BufferM ()
deleteHorizontalSpaceB u = do
  c <- curCol
  reg <- regionOfB Line
  text <- readRegionB reg
  let (r, jb) = deleteSpaces c text
  modifyRegionB (const r) reg
  -- Jump backwards to where the now-deleted spaces have started so
  -- it's consistent and feels natural instead of leaving us somewhere
  -- in the text.
  moveToColB $ c - jb
  where
    deleteSpaces :: Int -> R.YiString -> (R.YiString, Int)
    deleteSpaces c l =
      let (f, b) = R.splitAt c l
          f' = R.dropWhileEnd isSpace f
          cleaned = f' <> case u of
            Nothing -> R.dropWhile isSpace b
            Just _ -> b
      -- We only want to jump back the number of spaces before the
      -- point, not the total number of characters we're removing.
      in (cleaned, R.length f - R.length f')

----------------------------------------
-- Transform operations

-- | capitalise the word under the cursor
deleteHorizontalSpaceB u = do
  c <- curCol
  reg <- regionOfB Line
  text <- readRegionB reg
  let (r, jb) = deleteSpaces c text
  modifyRegionB (const r) reg
  -- Jump backwards to where the now-deleted spaces have started so
  -- it's consistent and feels natural instead of leaving us somewhere
  -- in the text.
  moveToColB $ c - jb
  where
    deleteSpaces :: Int -> R.YiString -> (R.YiString, Int)
    deleteSpaces c l =
      let (f, b) = R.splitAt c l
          f' = R.dropWhileEnd isSpace f
          cleaned = f' <> case u of
            Nothing -> R.dropWhile isSpace b
            Just _ -> b
      -- We only want to jump back the number of spaces before the
      -- point, not the total number of characters we're removing.
      in (cleaned, R.length f - R.length f')

----------------------------------------
-- Transform operations

-- | capitalise the word under the cursor