dotfiles/config/nvim/after/ftplugin/markdown.lua (view raw)
| 1 | vim.opt_local.wrap = true |
| 2 | vim.opt_local.tabstop = 2 |
| 3 | vim.opt_local.shiftwidth = 2 |
| 4 | |
| 5 | vim.b.minihipatterns_config = { highlighters = { |
| 6 | next = { pattern = "%#n[ext]*", group = "MiniTestPass" }, |
| 7 | tags = { pattern = "^tags%:", group = "Boolean" }, |
| 8 | links = { pattern = "https?://[^%s]+", group = "Tag" }, |
| 9 | } } |
| 10 | |
| 11 | -- TASKS |
| 12 | local function find_archive_heading(lines) |
| 13 | return vim |
| 14 | .iter(ipairs(lines)) |
| 15 | :find(function(_, line) return line:match "^%s*# Archive" end) |
| 16 | end |
| 17 | |
| 18 | local function can_be_completed(str) return str:match "^-%s*" ~= nil end |
| 19 | local function is_labled(str) return str:match "^%s*[%-*] .*`done:%d" ~= nil end |
| 20 | local function to_labled(str) |
| 21 | local task_prefix = str:match "^(%s*%- %[[x ]%])" |
| 22 | local prefix = task_prefix or str:match "^(%s*[%-*] )" |
| 23 | if not prefix then return nil end |
| 24 | local label = "`" .. os.date "done:%Y%m%d-%H%M" .. "`" |
| 25 | if task_prefix then |
| 26 | str = task_prefix .. " " .. label .. str:sub(#task_prefix + 1) |
| 27 | str = str:gsub("^(%s*%- )%[%s*%]", "%1[x]") -- mark task as complete |
| 28 | else |
| 29 | str = prefix .. label .. " " .. str:sub(#prefix + 1) |
| 30 | end |
| 31 | str = str:gsub("%[%[(.-)%]%]", "[%1]") -- remove link within a task |
| 32 | str = str:gsub(" %#n[ext]*", "") -- remove next tag |
| 33 | str = str:gsub("%s+$", "") -- white space in the end |
| 34 | return str |
| 35 | end |
| 36 | |
| 37 | Config.map("n", "<localleader>t", function() |
| 38 | local bufnr = vim.api.nvim_get_current_buf() |
| 39 | local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 40 | local line_idx = vim.api.nvim_win_get_cursor(0)[1] |
| 41 | local line = lines[line_idx] |
| 42 | if not can_be_completed(line) then |
| 43 | vim.notify("Can't be complated", vim.log.levels.ERROR) |
| 44 | return |
| 45 | end |
| 46 | |
| 47 | if is_labled(line) then |
| 48 | vim.notify("Already completed", vim.log.levels.ERROR) |
| 49 | return |
| 50 | end |
| 51 | |
| 52 | local archived_heading = find_archive_heading(lines) |
| 53 | if archived_heading == nil then |
| 54 | table.insert(lines, "") |
| 55 | table.insert(lines, "# Archive") |
| 56 | archived_heading = #lines |
| 57 | end |
| 58 | |
| 59 | table.remove(lines, line_idx) |
| 60 | table.insert(lines, archived_heading, to_labled(line)) |
| 61 | vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines) |
| 62 | vim.cmd.update() |
| 63 | end, true) |