From a0a04beaac8aea4fab1b1eff545194db0ad72a61 Mon Sep 17 00:00:00 2001 From: Alexander Lay-Calvert Date: Sat, 24 Sep 2022 13:50:52 -0400 Subject: [PATCH 1/3] Added toggling todos from a visual selection by passing ranges into the Telekasten command --- lua/telekasten.lua | 50 ++++++++++++++++++++++++++----------------- plugin/telekasten.vim | 2 +- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 7899894..5e7678c 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1532,8 +1532,7 @@ local function PreviewImg(opts) preview_type = "media", attach_mappings = function(prompt_bufnr, map) - actions.select_default:replace(function() - actions.close(prompt_bufnr) + actions.select_default:replace(function() actions.close(prompt_bufnr) end) map("i", "", picker_actions.yank_img_link(opts)) map("i", "", picker_actions.paste_img_link(opts)) @@ -2718,28 +2717,39 @@ local function ToggleTodo(opts) -- - [x] by - -- enter insert mode if opts.i == true opts = opts or {} - local linenr = vim.api.nvim_win_get_cursor(0)[1] - local curline = vim.api.nvim_buf_get_lines(0, linenr - 1, linenr, false)[1] - local stripped = vim.trim(curline) - local repline - if - vim.startswith(stripped, "- ") and not vim.startswith(stripped, "- [") - then - repline = curline:gsub("%- ", "- [ ] ", 1) - else - if vim.startswith(stripped, "- [ ]") then - repline = curline:gsub("%- %[ %]", "- [x]", 1) + -- Neovim sends wrong visual selection when using a mapping + -- See https://github.com/neovim/neovim/issues/15144 + vim.cmd("execute 'normal! \\'") + local startline = vim.api.nvim_buf_get_mark(0, "<")[1] + local endline = vim.api.nvim_buf_get_mark(0, ">")[1] + local cursorlinenr = vim.api.nvim_win_get_cursor(0)[1] + if startline == 0 or endline == 0 then + startline = cursorlinenr + endline = cursorlinenr + end + 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 - if vim.startswith(stripped, "- [x]") then - repline = curline:gsub("%- %[x%]", "-", 1) + if vim.startswith(stripped, "- [ ]") then + repline = curline:gsub("%- %[ %]", "- [x]", 1) 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 - vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, false, { repline }) - if opts.i then - vim.api.nvim_feedkeys("A", "m", false) + vim.api.nvim_buf_set_lines(0, curlinenr - 1, curlinenr, false, { repline }) + if opts.i then + vim.api.nvim_feedkeys("A", "m", false) + end end end diff --git a/plugin/telekasten.vim b/plugin/telekasten.vim index fefba0b..24011eb 100644 --- a/plugin/telekasten.vim +++ b/plugin/telekasten.vim @@ -8,7 +8,7 @@ function! s:telekasten_complete(arg,line,pos) return join(l:candidates, "\n") endfunction -command! -nargs=? -complete=custom,s:telekasten_complete Telekasten lua require('telekasten').panel() +command! -nargs=? -range -complete=custom,s:telekasten_complete Telekasten lua require('telekasten').panel() " 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 From 5b460a07a0b2a72d3aa14cf1f414eb8cb24b7ae1 Mon Sep 17 00:00:00 2001 From: Alexander Lay-Calvert Date: Sat, 24 Sep 2022 22:12:58 -0400 Subject: [PATCH 2/3] Fixed bug with wrong visual ranges --- lua/telekasten.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 5e7678c..be00feb 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -2717,13 +2717,14 @@ local function ToggleTodo(opts) -- - [x] by - -- enter insert mode if opts.i == true opts = opts or {} - -- Neovim sends wrong visual selection when using a mapping - -- See https://github.com/neovim/neovim/issues/15144 - vim.cmd("execute 'normal! \\'") local startline = vim.api.nvim_buf_get_mark(0, "<")[1] local endline = vim.api.nvim_buf_get_mark(0, ">")[1] local cursorlinenr = vim.api.nvim_win_get_cursor(0)[1] - if startline == 0 or endline == 0 then + -- to avoid the visual range marks not being reset when calling + -- command from normal mode + vim.api.nvim_buf_set_mark(0, "<", 0, 0, {}) + vim.api.nvim_buf_set_mark(0, ">", 0, 0, {}) + if startline <= 0 or endline <= 0 then startline = cursorlinenr endline = cursorlinenr end From 4340762d481402d3c2e44393ea7a84288b3f25be Mon Sep 17 00:00:00 2001 From: Alexander Lay-Calvert Date: Tue, 27 Sep 2022 16:03:55 -0400 Subject: [PATCH 3/3] Updated readme with visual toggle todo info --- README.md | 4 ++++ doc/telekasten.txt | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 70ac135..51a64b4 100644 --- a/README.md +++ b/README.md @@ -661,6 +661,10 @@ The plugin defines the following functions: - this function accepts a parameter `{i}`. If `true`, it will enter input mode by pressing the 'A' key. This is useful when being used in a simple `inoremap` key mapping like shown in [Bind it](#3-bind-it). - example: `toggle_todo({ i=true })` + - this function can also be used in `visual` mode to toggle the status of multiple lines. + - if using a keymapping to `toggle_todo` in visual mode, make sure to use `:Telekasten toggle_todo` + instead of `Telekasten toggle_todo` to avoid neovim sending the wrong visual selection to + telekasten. - `show_backlinks()` : opens a telescope search for notes that `[[link]]` back to the current note. - `find_friends()` : opens a telescope search for notes that also `[[link]]` to the link under the cursor. - `insert_img_link()` : opens a telescope search for all media (PDFs, images, videos (MP4, webm)) and places a markdown diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 43038d6..701b0e1 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -649,6 +649,11 @@ telekasten.toggle_todo({opts})~ Turns a line into a `- [ ] ` todo line, or toggle between `- [ ]`, `- [x]`, and `-` . + This can also be used in visual mode to toggle the status of multiple lines. + If using a keymapping to `toggle_todo` in visual mode, make sure the `rhs` is + `:Telekasten` `toggle_todo` instead of `Telekasten` `toggle_todo` + to avoid neovim sending the wrong visual selection. + Valid keys for {opts} i:~