Added toggling todos from a visual selection by passing ranges into the Telekasten command

This commit is contained in:
Alexander Lay-Calvert
2022-09-24 13:50:52 -04:00
parent 4217ee2423
commit a0a04beaac
2 changed files with 31 additions and 21 deletions

View File

@@ -1532,8 +1532,7 @@ local function PreviewImg(opts)
preview_type = "media", preview_type = "media",
attach_mappings = function(prompt_bufnr, map) attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function() actions.select_default:replace(function() actions.close(prompt_bufnr)
actions.close(prompt_bufnr)
end) end)
map("i", "<c-y>", picker_actions.yank_img_link(opts)) map("i", "<c-y>", picker_actions.yank_img_link(opts))
map("i", "<c-i>", picker_actions.paste_img_link(opts)) map("i", "<c-i>", picker_actions.paste_img_link(opts))
@@ -2718,28 +2717,39 @@ local function ToggleTodo(opts)
-- - [x] by - -- - [x] by -
-- enter insert mode if opts.i == true -- enter insert mode if opts.i == true
opts = opts or {} opts = opts or {}
local linenr = vim.api.nvim_win_get_cursor(0)[1] -- Neovim sends wrong visual selection when using a mapping
local curline = vim.api.nvim_buf_get_lines(0, linenr - 1, linenr, false)[1] -- See https://github.com/neovim/neovim/issues/15144
local stripped = vim.trim(curline) vim.cmd("execute 'normal! \\<ESC>'")
local repline local startline = vim.api.nvim_buf_get_mark(0, "<")[1]
if local endline = vim.api.nvim_buf_get_mark(0, ">")[1]
vim.startswith(stripped, "- ") and not vim.startswith(stripped, "- [") local cursorlinenr = vim.api.nvim_win_get_cursor(0)[1]
then if startline == 0 or endline == 0 then
repline = curline:gsub("%- ", "- [ ] ", 1) startline = cursorlinenr
else endline = cursorlinenr
if vim.startswith(stripped, "- [ ]") then end
repline = curline:gsub("%- %[ %]", "- [x]", 1) for curlinenr = startline, endline do
local curline = vim.api.nvim_buf_get_lines(0, curlinenr - 1, curlinenr, false)[1]
local stripped = vim.trim(curline)
local repline
if
vim.startswith(stripped, "- ") and not vim.startswith(stripped, "- [")
then
repline = curline:gsub("%- ", "- [ ] ", 1)
else else
if vim.startswith(stripped, "- [x]") then if vim.startswith(stripped, "- [ ]") then
repline = curline:gsub("%- %[x%]", "-", 1) repline = curline:gsub("%- %[ %]", "- [x]", 1)
else else
repline = curline:gsub("(%S)", "- [ ] %1", 1) if vim.startswith(stripped, "- [x]") then
repline = curline:gsub("%- %[x%]", "-", 1)
else
repline = curline:gsub("(%S)", "- [ ] %1", 1)
end
end end
end end
end vim.api.nvim_buf_set_lines(0, curlinenr - 1, curlinenr, false, { repline })
vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, false, { repline }) if opts.i then
if opts.i then vim.api.nvim_feedkeys("A", "m", false)
vim.api.nvim_feedkeys("A", "m", false) end
end end
end end

View File

@@ -8,7 +8,7 @@ function! s:telekasten_complete(arg,line,pos)
return join(l:candidates, "\n") return join(l:candidates, "\n")
endfunction endfunction
command! -nargs=? -complete=custom,s:telekasten_complete Telekasten lua require('telekasten').panel(<f-args>) command! -nargs=? -range -complete=custom,s:telekasten_complete Telekasten lua require('telekasten').panel(<f-args>)
" overriding does not work -- so this is done by the plugin now in post_open() " overriding does not work -- so this is done by the plugin now in post_open()
" au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md setf telekasten " au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md setf telekasten