/
edit.lua
143 lines (132 loc) · 3.84 KB
/
edit.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
-- Edit utilities
-- https://github.com/rafi/vim-config
---@class rafi.util.edit
local M = {}
-- Get visually selected lines.
-- Source: https://github.com/ibhagwan/fzf-lua/blob/main/lua/fzf-lua/utils.lua
---@return string
function M.get_visual_selection()
-- this will exit visual mode
-- use 'gv' to reselect the text
local _, csrow, cscol, cerow, cecol
local mode = vim.fn.mode()
local is_visual = vim.tbl_contains({ 'v', 'V', '\22' }, mode)
if is_visual then
-- if we are in visual mode use the live position
_, csrow, cscol, _ = unpack(vim.fn.getpos('.') or { 0, 0, 0, 0 })
_, cerow, cecol, _ = unpack(vim.fn.getpos('v') or { 0, 0, 0, 0 })
if mode == 'V' then
-- visual line doesn't provide columns
cscol, cecol = 0, 999
end
-- exit visual mode
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes('<Esc>', true, false, true),
'n',
true
)
else
-- otherwise, use the last known visual position
_, csrow, cscol, _ = unpack(vim.fn.getpos("'<") or { 0, 0, 0, 0 })
_, cerow, cecol, _ = unpack(vim.fn.getpos("'>") or { 0, 0, 0, 0 })
end
-- swap vars if needed
if cerow < csrow then
csrow, cerow = cerow, csrow
end
if cecol < cscol then
cscol, cecol = cecol, cscol
end
local lines = vim.fn.getline(csrow, cerow)
-- local n = cerow-csrow+1
local n = #lines
if n <= 0 or type(lines) ~= 'table' then
return ''
end
lines[n] = string.sub(lines[n], 1, cecol)
lines[1] = string.sub(lines[1], cscol)
return table.concat(lines, '\n')
end
-- Append modeline at end of file.
function M.append_modeline()
local modeline = string.format(
'vim: set ts=%d sw=%d tw=%d %set :',
vim.bo.tabstop,
vim.bo.shiftwidth,
vim.bo.textwidth,
vim.bo.expandtab and '' or 'no'
)
modeline = string.gsub(vim.bo.commentstring, '%%s', modeline)
vim.api.nvim_buf_set_lines(0, -1, -1, false, { modeline })
end
-- Go to newer/older buffer through jumplist.
---@param direction 1 | -1
function M.jump_buffer(direction)
local jumplist, curjump = unpack(vim.fn.getjumplist() or { 0, 0 })
if #jumplist == 0 then
return
end
local cur_buf = vim.api.nvim_get_current_buf()
local jumpcmd = direction > 0 and '<C-i>' or '<C-o>'
local searchrange = {}
curjump = curjump + 1
if direction > 0 then
searchrange = vim.fn.range(curjump + 1, #jumplist)
else
searchrange = vim.fn.range(curjump - 1, 1, -1)
end
for _, i in ipairs(searchrange) do
local nr = jumplist[i]['bufnr']
if nr ~= cur_buf and vim.fn.bufname(nr):find('^%w+://') == nil then
local n = tostring(math.abs(i - curjump))
vim.notify('Executing ' .. jumpcmd .. ' ' .. n .. ' times')
jumpcmd = vim.api.nvim_replace_termcodes(jumpcmd, true, true, true)
vim.cmd.normal({ n .. jumpcmd, bang = true })
break
end
end
end
-- Jump to next/previous whitespace error.
---@param direction 1 | -1
function M.whitespace_jump(direction)
local opts = 'wz'
if direction < 1 then
opts = opts .. 'b'
end
-- Whitespace pattern: Trailing whitespace or mixed tabs/spaces.
local pat = '\\s\\+$\\| \\+\\ze\\t'
vim.fn.search(pat, opts)
end
-- Toggle list window
---@param name "quickfix" | "loclist"
M.toggle_list = function(name)
local win_bufs = M.get_tabpage_win_bufs(0)
for win, buf in pairs(win_bufs) do
if vim.bo[buf].filetype == 'qf' and vim.fn.win_gettype(win) == name then
vim.api.nvim_win_close(win, false)
return
end
end
if name == 'loclist' then
vim.cmd([[ botright lopen ]])
else
vim.cmd([[ botright copen ]])
end
end
-- Return a table with all window buffers from a tabpage.
---@private
---@param tabpage integer
---@return table
M.get_tabpage_win_bufs = function(tabpage)
local bufs = {}
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
if win ~= nil and vim.api.nvim_win_is_valid(win) then
local buf = vim.api.nvim_win_get_buf(win)
if buf ~= nil and vim.api.nvim_buf_is_valid(buf) then
bufs[win] = buf
end
end
end
return bufs
end
return M