Files
todo.txt-vim/autoload/todo/txt.vim
Robert Ames f2354abaa0 Prefer: 0i instead of I for mechanical insertion
vim details: `I` inserts before the first character instead of `0i` which inserts at the beginning of the line.

Usually `I` inserts at the first character in the line, and usually the first character is on the far left.  However, `I` ignores leading spaces which conflicts a bit with the mechanical manipulation of the text.

While it's not great `todo.txt` practices, if you did "mark as done" while on a line like ` * ...etc...` (eg: an indented bullet), the `I...` would put the `x` away from the far left (not the first character on the line, but instead the first character before the first character).

Prefer: `0i` instead of `I` for mechanical insertion
2024-03-27 06:49:44 -07:00

132 lines
4.0 KiB
VimL
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

" File: todo.txt.vim
" Description: Todo.txt filetype detection
" Author: Leandro Freitas <freitass@gmail.com>
" License: Vim license
" Website: http://github.com/freitass/todo.txt-vim
" Version: 0.4
" Export Context Dictionary for unit testing {{{1
function! s:get_SID()
return matchstr(expand('<sfile>'), '<SNR>\d\+_')
endfunction
let s:SID = s:get_SID()
delfunction s:get_SID
function! todo#txt#__context__()
return { 'sid': s:SID, 'scope': s: }
endfunction
" Functions {{{1
function! s:remove_priority()
:s/^(\w)\s\+//ge
endfunction
function! s:get_current_date()
return strftime('%Y-%m-%d')
endfunction
function! todo#txt#prepend_date()
execute 'normal! 0i' . s:get_current_date() . ' '
endfunction
function! todo#txt#replace_date()
let current_line = getline('.')
if (current_line =~ '^\(([a-zA-Z]) \)\?\d\{2,4\}-\d\{2\}-\d\{2\} ') &&
\ exists('g:todo_existing_date') && g:todo_existing_date == 'n'
return
endif
execute 's/^\(([a-zA-Z]) \)\?\(\d\{2,4\}-\d\{2\}-\d\{2\} \)\?/\1' . s:get_current_date() . ' /'
endfunction
function! todo#txt#mark_as_done()
call s:remove_priority()
call todo#txt#prepend_date()
execute 'normal! 0ix '
endfunction
function! todo#txt#mark_all_as_done()
:g!/^x /:call todo#txt#mark_as_done()
endfunction
function! s:append_to_file(file, lines)
let l:lines = []
" Place existing tasks in done.txt at the beggining of the list.
if filereadable(a:file)
call extend(l:lines, readfile(a:file))
endif
" Append new completed tasks to the list.
call extend(l:lines, a:lines)
" Write to file.
call writefile(l:lines, a:file)
endfunction
function! todo#txt#remove_completed()
" Check if we can write to done.txt before proceeding.
let l:target_dir = expand('%:p:h')
let l:todo_file = expand('%:p')
" Check for user-defined g:todo_done_filename
if exists("g:todo_done_filename")
let l:todo_done_filename = g:todo_done_filename
elseif expand('%:t') == 'Todo.txt'
let l:todo_done_filename = 'Done.txt'
else
let l:todo_done_filename = 'done.txt'
endif
let l:done_file = substitute(substitute(l:todo_file, 'todo.txt$', l:todo_done_filename, ''), 'Todo.txt$', l:todo_done_filename, '')
if !filewritable(l:done_file) && !filewritable(l:target_dir)
echoerr "Can't write to file '" . l:todo_done_filename . "'"
return
endif
let l:completed = []
:g/^x /call add(l:completed, getline(line(".")))|d
call s:append_to_file(l:done_file, l:completed)
endfunction
function! todo#txt#sort_by_context() range
execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs@[^[:blank:]]\\+/ r"
endfunction
function! todo#txt#sort_by_project() range
execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs+[^[:blank:]]\\+/ r"
endfunction
function! todo#txt#sort_by_date() range
let l:date_regex = "\\d\\{2,4\\}-\\d\\{2\\}-\\d\\{2\\}"
execute a:firstline . "," . a:lastline . "sort /" . l:date_regex . "/ r"
execute a:firstline . "," . a:lastline . "g!/" . l:date_regex . "/m" . a:lastline
endfunction
function! todo#txt#sort_by_due_date() range
let l:date_regex = "due:\\d\\{2,4\\}-\\d\\{2\\}-\\d\\{2\\}"
execute a:firstline . "," . a:lastline . "sort /" . l:date_regex . "/ r"
execute a:firstline . "," . a:lastline . "g!/" . l:date_regex . "/m" . a:lastline
endfunction
" Increment and Decrement The Priority
:set nf=octal,hex,alpha
function! todo#txt#prioritize_increase()
normal! 0f)h
endfunction
function! todo#txt#prioritize_decrease()
normal! 0f)h
endfunction
function! todo#txt#prioritize_add(priority)
" Need to figure out how to only do this if the first visible letter in a line is not (
:call todo#txt#prioritize_add_action(a:priority)
endfunction
function! todo#txt#prioritize_add_action(priority)
execute 's/^\(([a-zA-Z]) \)\?/(' . a:priority . ') /'
endfunction
" Modeline {{{1
" vim: ts=8 sw=4 sts=4 et foldenable foldmethod=marker foldcolumn=1