This commit is contained in:
2025-08-29 23:21:21 +02:00
parent 20a49c3f58
commit e46f58fd20
14 changed files with 219 additions and 152 deletions

View File

@@ -1,5 +1,6 @@
-- Neo-tree helper module
--
local M = {}
-- Function to get confirmation input (compatible with different neo-tree versions)
@@ -30,12 +31,12 @@ function M.smart_open()
vim.cmd('Neotree toggle reveal=false position=float dir=' .. vim.fn.expand '~')
-- Si c'est un fichier réel, révéler le fichier dans l'arborescence
elseif is_real_file and bufname ~= '' then
-- vim.cmd('Neotree toggle position=left reveal_force_cwd=true dir=' .. vim.fn.expand '%:p:h')
vim.cmd 'Neotree position=left reveal_force_cwd=true'
vim.cmd('Neotree toggle position=left reveal_force_cwd=true dir=' .. vim.fn.expand '%:p:h')
-- vim.cmd 'Neotree toggle position=left reveal_force_cwd=true'
-- Sinon, ouvrir dans le répertoire de travail courant
else
-- vim.cmd('Neotree toggle position=left reveal_force_cwd=true dir=' .. vim.fn.getcwd())
vim.cmd 'Neotree toggle position=left reveal_force_cwd=true'
vim.cmd('Neotree toggle position=left reveal=true dir=' .. vim.fn.getcwd())
-- vim.cmd 'Neotree toggle position=left reveal_force_cwd=true'
end
end

View File

@@ -2,140 +2,171 @@
-- from: https://github.com/jmbuhr/nvim-config
--
---@module 'utils.term'
local M = {
---Map of buffers to a terminals
---@type table<integer, Terminal>
buf_term_map = {},
}
-- ---@module 'utils.term'
-- local M = {
-- ---Map of buffers to a terminals
-- ---@type table<integer, Terminal>
-- buf_term_map = {},
-- }
--
-- ---A terminal has a buffer number, a type and a channel id
-- ---@class Terminal
-- ---@field bufnr integer
-- ---@field type string?
-- ---@field channel_id integer
--
-- ---Get the channel id of a terminal buffer
-- ---@param bufnr integer
-- ---@return integer|nil
-- local function get_channel_id(bufnr)
-- return vim.api.nvim_get_option_value('channel', { buf = bufnr })
-- end
--
-- ---Get the type of the terminal buffer
-- ---like ipython, R, bash, etc.
-- ---based on the buffer name
-- ---@param buf integer
-- ---@return string|nil
-- local function get_term_type(buf)
-- local name = vim.api.nvim_buf_get_name(buf)
-- local term_type = name:match 'term://.*/%d+:(.*)'
-- if not term_type then
-- return nil
-- end
-- -- drop flags like --no-confirm-exit
-- term_type = term_type:match '([^%s]+)'
-- -- drop the path to the executable
-- term_type = term_type:match '([^/]+)$'
-- return term_type
-- end
--
-- ---Find nvim terminal buffers
-- local function list_terminals()
-- local terms = {}
-- for _, buf in ipairs(vim.api.nvim_list_bufs()) do
-- if vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_get_option_value('buftype', { buf = buf }) == 'terminal' then
-- local chan_id = get_channel_id(buf)
-- if not chan_id then
-- goto continue
-- end
-- ---@type Terminal
-- local term = {
-- bufnr = buf,
-- type = get_term_type(buf),
-- channel_id = chan_id,
-- }
-- table.insert(terms, term)
-- end
-- ::continue::
-- end
-- return terms
-- end
--
-- ---For ipython sending
-- local cpaste_start = '%cpaste -q\n'
-- local cpaste_end = '--\n'
-- local cpaste_pause = 10
--
-- ---Send lines to a terminal
-- ---@param lines string[]
-- ---@param term Terminal
-- ---@return nil
-- local send_lines = function(lines, term)
-- local chan_id = term.channel_id
-- local text = table.concat(lines, '\n') .. '\n'
-- if term.type == 'ipython' then
-- vim.fn.chansend(chan_id, cpaste_start)
-- vim.uv.sleep(cpaste_pause)
-- end
-- vim.fn.chansend(chan_id, text)
-- if term.type == 'ipython' then
-- vim.fn.chansend(chan_id, cpaste_end)
-- end
-- end
--
-- local s = [[
-- print("hello world!")
-- def hello():
-- print("hello")
-- hello()
-- ]]
--
-- local ls = { s }
--
-- ---Connect current buffer to a terminal
-- local connect = function()
-- local bufnr = vim.api.nvim_get_current_buf()
-- local terms = list_terminals()
-- vim.ui.select(terms, {
-- prompt = 'Select terminal',
-- format_item = function(item)
-- return item.type .. ' ' .. item.bufnr
-- end,
-- }, function(choice)
-- if choice == nil then
-- return
-- end
-- M.buf_term_map[bufnr] = choice
-- end)
-- end
--
-- M.send = function(lines)
-- local bufnr = vim.api.nvim_get_current_buf()
-- local term = M.buf_term_map[bufnr]
-- if not term then
-- connect()
-- return
-- end
-- send_lines(lines, term)
-- -- scroll to the bottom
-- local term_win = vim.fn.bufwinid(term.bufnr)
-- local n = vim.api.nvim_buf_line_count(term.bufnr)
-- vim.api.nvim_win_set_cursor(term_win, { n, 0 })
-- end
--
-- M.send_visual = function()
-- -- get the selected text
-- vim.cmd.normal { '"zy', bang = true }
-- local selection = vim.fn.getreg 'z'
-- M.send { selection }
-- end
--
-- vim.keymap.set('v', '<CR>', function()
-- M.send_visual()
-- end, { noremap = true, silent = true })
--
-- -- connect
-- vim.keymap.set('n', '<leader>ct', function()
-- connect()
-- end, { noremap = true, silent = true })
---A terminal has a buffer number, a type and a channel id
---@class Terminal
---@field bufnr integer
---@field type string?
---@field channel_id integer
local M = {}
---Get the channel id of a terminal buffer
---@param bufnr integer
---@return integer|nil
local function get_channel_id(bufnr)
return vim.api.nvim_get_option_value('channel', { buf = bufnr })
local function new_terminal(lang)
vim.cmd('vsplit term://' .. lang)
end
---Get the type of the terminal buffer
---like ipython, R, bash, etc.
---based on the buffer name
---@param buf integer
---@return string|nil
local function get_term_type(buf)
local name = vim.api.nvim_buf_get_name(buf)
local term_type = name:match 'term://.*/%d+:(.*)'
if not term_type then
return nil
end
-- drop flags like --no-confirm-exit
term_type = term_type:match '([^%s]+)'
-- drop the path to the executable
term_type = term_type:match '([^/]+)$'
return term_type
function M.new_terminal_python()
new_terminal 'python'
end
---Find nvim terminal buffers
local function list_terminals()
local terms = {}
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_get_option_value('buftype', { buf = buf }) == 'terminal' then
local chan_id = get_channel_id(buf)
if not chan_id then
goto continue
end
---@type Terminal
local term = {
bufnr = buf,
type = get_term_type(buf),
channel_id = chan_id,
}
table.insert(terms, term)
end
::continue::
end
return terms
function M.new_terminal_r()
new_terminal 'R --no-save'
end
---For ipython sending
local cpaste_start = '%cpaste -q\n'
local cpaste_end = '--\n'
local cpaste_pause = 10
---Send lines to a terminal
---@param lines string[]
---@param term Terminal
---@return nil
local send_lines = function(lines, term)
local chan_id = term.channel_id
local text = table.concat(lines, '\n') .. '\n'
if term.type == 'ipython' then
vim.fn.chansend(chan_id, cpaste_start)
vim.uv.sleep(cpaste_pause)
end
vim.fn.chansend(chan_id, text)
if term.type == 'ipython' then
vim.fn.chansend(chan_id, cpaste_end)
end
function M.new_terminal_ipython()
new_terminal 'ipython --no-confirm-exit --no-autoindent'
end
local s = [[
print("hello world!")
def hello():
print("hello")
hello()
]]
local ls = { s }
---Connect current buffer to a terminal
local connect = function()
local bufnr = vim.api.nvim_get_current_buf()
local terms = list_terminals()
vim.ui.select(terms, {
prompt = 'Select terminal',
format_item = function(item)
return item.type .. ' ' .. item.bufnr
end,
}, function(choice)
if choice == nil then
return
end
M.buf_term_map[bufnr] = choice
end)
function M.new_terminal_julia()
new_terminal 'julia'
end
M.send = function(lines)
local bufnr = vim.api.nvim_get_current_buf()
local term = M.buf_term_map[bufnr]
if not term then
connect()
return
end
send_lines(lines, term)
-- scroll to the bottom
local term_win = vim.fn.bufwinid(term.bufnr)
local n = vim.api.nvim_buf_line_count(term.bufnr)
vim.api.nvim_win_set_cursor(term_win, { n, 0 })
function M.new_terminal_shell()
new_terminal '$SHELL'
end
M.send_visual = function()
-- get the selected text
vim.cmd.normal { '"zy', bang = true }
local selection = vim.fn.getreg 'z'
M.send { selection }
end
return M
vim.keymap.set('v', '<CR>', function()
M.send_visual()
end, { noremap = true, silent = true })
-- connect
vim.keymap.set('n', '<leader>ct', function()
connect()
end, { noremap = true, silent = true })
-- The line beneath this is called `modeline`. See `:help modeline`
-- vim: ts=2 sts=2 sw=2 et