From 05b959b2af1696f5f35676028fa85b999b4572bd Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Fri, 26 Nov 2021 14:01:07 +0100 Subject: [PATCH] toggle todo lines!!! first try! --- README.md | 4 +++- doc/telekasten.txt | 10 ++++++++-- lua/telekasten.lua | 30 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e98c006..a8aa745 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,7 @@ The plugin defines the following functions. - `yank_notelink()` : yank a link to the current note, ready to paste - `show_calendar()` : opens up the calendar in a properly-sized vertical split at the very right - `paste_img_and_link()` : pastes an image from the clipboard into a file under `image_subdir` and inserts a link to it at the current cursor position +- `toggle_todo()` : turn a line into a `- [ ] ` line, or toggle between `- [ ]`, `- [x]`, and `- `. - `setup(opts)`: used for configuring paths, file extension, etc. To use one of the functions above, just run them with the `:lua ...` command. @@ -317,12 +318,13 @@ nnoremap zN :lua require('telekasten').new_templated_note() nnoremap zy :lua require('telekasten').yank_notelink() nnoremap zc :lua require('telekasten').show_calendar() nnoremap zi :lua require('telekasten').paste_img_and_link() +nnoremap zt :lua require('telekasten').toggle_todo() " we could define [[ in **insert mode** to call insert link " inoremap [[ :lua require('telekasten').insert_link() " alternatively: leader [ inoremap [ :lua require('telekasten').insert_link() - +inoremap zt :lua require('telekasten').toggle_todo() " ----- the following are for syntax-coloring [[links]] and ==highlighted text== " ----- (see the section about coloring in README.md) diff --git a/doc/telekasten.txt b/doc/telekasten.txt index b4cc88c..aa3b5cb 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -404,6 +404,11 @@ telekasten.show_calendar()~ See also:~ - |telekasten.calendar| + *telekasten.toggle_todo()* +telekasten.toggle_todo()~ + Turns a line into a `- [ ] ` todo line, or toggle between `- [ ]`, `- [x]`, + and `-` . + -------------------------------------------------------------------------------- Section 3.1: Templates *telekasten.templates* @@ -428,7 +433,7 @@ The specific templates and settings used are: See also |telekasten.template_files| below for more on the specific templates. -The following table shows you what action creates what kind of non-existing +The folloet ing table shows you what action creates what kind of non-existing note: +----------------------------------+--------------------------------+ @@ -520,12 +525,13 @@ However, here are some suggestions: nnoremap zy :lua require('telekasten').yank_notelink() nnoremap zc :lua require('telekasten').show_calendar() nnoremap zi :lua require('telekasten').paste_img_and_link() + nnoremap zt :lua require('telekasten').toggle_todo() " we could define [[ in **insert mode** to call insert link " inoremap [[ :lua require('telekasten').insert_link() " alternatively: leader [ inoremap [ :lua require('telekasten').insert_link() - + inoremap zt :lua require('telekasten').toggle_todo() " the following are for syntax-coloring [[links]] and ==highlighted text== " (see the section about coloring in README.md) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index c28c5c1..0899162 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -608,6 +608,35 @@ local SetupCalendar = function(opts) vim.cmd(cmd) end +local ToggleTodo = function() + -- replace + -- by - + -- - by - [ ] + -- - [ ] by - [x] + -- - [x] by - + local linenr = vim.api.nvim_win_get_cursor(0)[1] + local curline = vim.api.nvim_buf_get_lines(0, linenr - 1, linenr, false)[1] + -- if curline.strip().startswith('- ') and not curline.strip.startswith('- [')... + -- print('cur: ' .. curline) + 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) + else + if vim.startswith(stripped, "- [x]") then + repline = curline:gsub("- %[x%]", "-", 1) + else + repline = curline:gsub("(%S)", "- [ ] %1", 1) + end + end + end + vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, false, repline) + -- print('rep: ' .. repl) +end + -- Setup(cfg) -- -- Overrides config with elements from cfg. See top of file for defaults. @@ -664,5 +693,6 @@ local M = { CalendarSignDay = CalendarSignDay, CalendarAction = CalendarAction, paste_img_and_link = imgFromClipboard, + toggle_todo = ToggleTodo, } return M