added preview scrolling for terminal buffers

main
bhagwan 3 years ago
parent 6c349939b8
commit 8feda7fb9c

@ -450,6 +450,7 @@ M.globals.file_icon_colors = {
["zsh"] = "cyan",
["bash"] = "cyan",
["bat"] = "cyan",
["term"] = "green",
["py"] = "green",
["md"] = "yellow",
["go"] = "magenta",

@ -5,7 +5,6 @@ local raw_action = require("fzf.actions").raw_action
local api = vim.api
local fn = vim.fn
local cmd = vim.cmd
local Previewer = {}
@ -157,8 +156,6 @@ local function set_cursor_hl(self, entry)
if lnum and lnum > 0 and col and col > 1 then
fn.matchaddpos(self.hl_cursor, {{lnum, math.max(1, col)}}, 11)
end
cmd(('noa call nvim_set_current_win(%d)'):format(self.win.preview_winid))
end
function Previewer:do_syntax(entry)
@ -244,7 +241,7 @@ function Previewer:preview_buf_post(entry)
-- set preview win options or load the file
-- if not already loaded from buffer
utils.win_execute(preview_winid, function()
vim.api.nvim_win_call(preview_winid, function()
set_cursor_hl(self, entry)
end)
@ -350,30 +347,42 @@ function Previewer.scroll(direction)
if not _self then return end
local self = _self
local preview_winid = self.win.preview_winid
if preview_winid < 0 or not direction then
return
end
if utils.is_term_buffer(self.preview_bufnr) then
local input = direction > 0 and [[]] or [[]]
local count = math.abs(direction)
if preview_winid < 0 or not direction then return end
if direction == 0 then
vim.api.nvim_win_call(preview_winid, function()
-- TODO: why does this cause an error?
-- 'Vim(normal):Can't re-enter normal mode from terminal mode'
-- vim.cmd([[normal! ]] .. count .. input)
print([[normal! ]] .. count .. input)
-- for some reason 'nvim_win_set_cursor'
-- only moves forward, so set to (1,0) first
api.nvim_win_set_cursor(0, {1, 0})
api.nvim_win_set_cursor(0, self.orig_pos)
utils.zz()
end)
return
end
utils.win_execute(preview_winid, function()
if direction == 0 then
api.nvim_win_set_cursor(preview_winid, self.orig_pos)
else
if utils.is_term_buffer(self.preview_bufnr) then
-- can't use ":norm!" with terminal buffers due to:
-- 'Vim(normal):Can't re-enter normal mode from terminal mode'
-- TODO: this is hacky and disgusting figure
-- out why it's failing or at the very least
-- hide the typed command from the user
local input = direction > 0 and "<C-d>" or "<C-u>"
utils.feed_keys_termcodes(('<C-\\><C-n>:noa lua vim.api.nvim_win_call(' ..
'%d, function() vim.cmd("norm! <C-v>%s") end)<CR>i'):
format(tonumber(preview_winid), input))
--[[ local input = ('%c'):format(utils._if(direction>0, 0x04, 0x15))
vim.cmd(('noa lua vim.api.nvim_win_call(' ..
'%d, function() vim.cmd("norm! %s") end)'):
format(tonumber(preview_winid), input)) ]]
else
-- ^D = 0x04, ^U = 0x15
fn.execute(('norm! %c'):format(direction > 0 and 0x04 or 0x15))
-- local input = direction > 0 and [[]] or [[]]
-- local input = direction > 0 and [[]] or [[]]
-- ^D = 0x04, ^U = 0x15 ('g8' on char to display)
local input = ('%c'):format(utils._if(direction>0, 0x04, 0x15))
vim.api.nvim_win_call(preview_winid, function()
vim.cmd([[norm! ]] .. input)
utils.zz()
end)
end
utils.zz()
cmd(('noa call nvim_set_current_win(%d)'):format(self.win.fzf_winid))
end)
end
if self.scrollbar then
self.win:update_scrollbar()
end

@ -111,7 +111,12 @@ M.buffers = function(opts)
local buficon = ''
if opts.file_icons then
local extension = path.extension(buf.info.name)
buficon = core.get_devicon(buf.info.name, extension)
if utils.is_term_bufname(buf.info.name) then
-- get shell-like icon for terminal buffers
buficon = core.get_devicon(buf.info.name, "sh")
else
buficon = core.get_devicon(buf.info.name, extension)
end
if opts.color_icons then
buficon = utils.ansi_codes[config.globals.file_icon_colors[extension] or "dark_grey"](buficon)
end

@ -243,12 +243,16 @@ function M.delayed_cb(cb, fn)
end, 20)
end
function M.is_term_buffer(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr or 0)
function M.is_term_bufname(bufname)
if bufname and bufname:match("term://") then return true end
return false
end
function M.is_term_buffer(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr or 0)
return M.is_term_bufname(bufname)
end
function M.zz()
-- skip for terminal buffers
if M.is_term_buffer() then return end

Loading…
Cancel
Save