Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 0 additions & 106 deletions lua/core/horiz.lua

This file was deleted.

149 changes: 149 additions & 0 deletions lua/move/core/horiz.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
local utils = require('move.utils')

local M = {}

-- Moves the character under the cursor to the left or right
-- Updates the cursor position.
M.horzChar = function(dir)
-- sRow and col: current cursor position
local sRow, col = unpack(vim.api.nvim_win_get_cursor(0))

local line = vim.api.nvim_get_current_line()

local target = ''
local prefix = ''
local suffix = ''
local result = {}
local line_res = ''
local selected = ''

local sChar = ''
local tChar = ''

-- Checks line limits with the direction
if dir < 0 and col < 1 then
return
end

-- Default
prefix = string.sub(line, 1, col + (dir > 0 and 0 or dir))

-- Unicode or tilde character
sChar = utils.curUnicodeOrTilde()
selected = utils.getChar()

-- Check if target is unicode or tilde
-- move cursor to get the character
if dir < 0 then
vim.cmd(':normal! h')
end
tChar = utils.curUnicodeOrTilde()

-- Put a space if the character reaches the end of the line
-- Default
target = utils.getChar()
suffix = string.sub(line, col + (dir > 0 and 3 or 2))

-- Character under cursor is unicode or tilde
if utils.isTilde(sChar) or utils.isUnicode(sChar) then
if utils.isUnicode(sChar) then
suffix = utils.suffixUnicode(tChar, sChar, line, col, dir)
else
suffix = utils.suffixTilde(tChar, sChar, line, col, dir)
end
else
-- Target character is tilde or unicode
if utils.isTilde(tChar) or utils.isUnicode(tChar) then
if utils.isUnicode(tChar) then
suffix = utils.suffixUnicode(tChar, sChar, line, col, dir)
else
suffix = utils.suffixTilde(tChar, sChar, line, col, dir)
end
end
end

if utils.isTilde(tChar) or utils.isUnicode(tChar) then
if utils.isUnicode(tChar) then
prefix = string.sub(line, 1, col + (dir < 0 and -3 or 0))
else
prefix = string.sub(line, 1, col + (dir < 0 and -2 or 0))
end
end

-- Return cursor position to original
vim.api.nvim_win_set_cursor(0, { sRow, col })

if dir > 0 then
if col == line:len() - utils.calc_eolOffset(sChar) then
target = ' '
end
end

-- Remove trailing spaces before putting into the table
line_res = prefix .. (dir > 0 and target .. selected or selected .. target) .. suffix
line_res = line_res:gsub('%s+$', '')

table.insert(result, line_res)

-- Update the line with the new one and update cursor position
vim.api.nvim_buf_set_lines(0, sRow - 1, sRow, true, result)
vim.api.nvim_win_set_cursor(0, { sRow, col + utils.cursor_offset(tChar, sChar, dir) })
end


-- Moves the visual area left or right
-- and keeps it selected
M.horzBlock = function(dir)
-- Get the boundries (cols) of the visual area
local sCol = vim.fn.col("'<")
local eCol = vim.fn.col("'>")

-- The current line of the cursor will be the last line
-- of the visual area and eRow will be the first line
local sRow, col = unpack(vim.api.nvim_win_get_cursor(0))
local eRow = vim.fn.line("'>")

-- Checks line limits with the direction
if dir < 0 and col < 1 then
vim.cmd('execute "normal! \\<C-V>' .. (eCol - sCol) .. 'l' .. (eRow - sRow) .. 'j' .. '"')
return
end

local offset_composite = 0
vim.api.nvim_win_set_cursor(0, { sRow, sCol - 1 })

local i = sCol

while i < eCol + 1 do
local comp = utils.isCharComposite()
if comp then
local char = utils.getCharNew()
local offset = utils.getSize(char)
offset_composite = offset_composite + utils.getSize(char) - 1
print('col '..i, 'offComp '..offset_composite, 'offset '..offset, 'char '..char)
i = i + offset
else
i = i + 1
end
vim.api.nvim_win_set_cursor(0, { sRow, i - 1 })
end

vim.api.nvim_win_set_cursor(0, { sRow, sCol - 1 })
vim.cmd('execute "normal! \\<C-V>' .. (eCol - sCol - offset_composite) .. 'l' .. (eRow - sRow) .. 'j' .. '"')
vim.cmd(':normal! x')

if dir > 0 then
vim.cmd(':normal! p')
else
vim.api.nvim_win_set_cursor(0, { sRow, sCol - 2 })
vim.cmd(':normal! P')
end

-- Update the visual area with the new position of the characters
vim.cmd('execute "normal! \\e\\e"')
local cmd_suffix = (eCol - sCol > 0 and (eCol - sCol - offset_composite) .. 'l' or '')
cmd_suffix = cmd_suffix .. (eRow - sRow > 0 and (eRow - sRow) .. 'j' or '')
vim.cmd('execute "normal! \\<C-V>' .. cmd_suffix .. '"')
end

return M
2 changes: 1 addition & 1 deletion lua/core/vert.lua → lua/move/core/vert.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require('utils')
local utils = require('move.utils')

local M = {}

Expand Down
4 changes: 2 additions & 2 deletions lua/move.lua → lua/move/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local move_vert = require('core.vert')
local move_hor = require('core.horiz')
local move_hor = require('move.core.horiz')
local move_vert = require('move.core.vert')

return {
MoveLine = move_vert.moveLine,
Expand Down
Loading