2021-11-24

Calendar.vim extension for CSVTODO

On Windows OS I enjoy working with a mix of terminal and graphical applications, and I also try to have them interact where possible. An example, as described below, is my desktop application CSVTODO, and the possibility to display the same TODO list in VIM.

It requires CSVKIT (see this blog post), and two more VIM plugins. Calendar.vim, an exceptionally well-written plugin that, as the name implies, produces an interactive calendar inside a vim special buffer and scratch.vim. A more detailed description of scratch.vim can be found here. Both have been installed with VIM's plug-in manager.

Calendar.vim has a set of hooks, one is called calendar_action, and it allows running custom code when the user press the enter key on a particular date. In the .vimrc, I have included the following function:

    """ csvtodo vim calendar integration 
    """ [1]
    let g:todofile = '~/Documents/todo.csv' 
    function CheckCsvTodo(day,month,year,week,dir)
        let l:actionedaday = a:year . "-" . a:month . "-" . a:day
        "" [2]
        let l:due = system("csvgrep.exe -c 8 -m " . l:actionedaday . " " . g:todofile)
        let l:tododuelist = split(l:due,'\r\n')
        if len(l:tododuelist) > 1
            let g:scratch_horizontal = 1   
            """ [3]
            call scratch#selection(1)
            let t_index = 0
            call append(0, l:actionedaday)
            for todoitem in l:tododuelist 
                if t_index > 0
                    let l:todo = split( todoitem ,",")
                    let l:tododesc = "Project: " . l:todo[3] . "   " . l:todo[4] . " due date."  
                    call append(t_index,tododesc)
                endif
                let t_index = t_index+1         
            endfor
            let g:scratch_horizontal = 0   
        endif 

    endfunction
    "" [4]
    let calendar_action = 'CheckCsvTodo'
    

Specifically:

[1] Set the filename and the path of the csv file.

[2] Use csvgrep to filter out a todo with the indicated due date.

[3] Open an empty scratch buffer.

[4] Hook the function to the press enter on date event.

The short video below demonstrates how it works.

Postscript

  • Sun Feb 20 12:49:25 GMT 2022

The Calendar-vim default date format has changed, the CheckCsvTodo function should be replaced with:

    function CheckCsvTodo(day,month,year,week,dir)
        let @" = ''
        if len(a:month) == 1
            let l:month_c = "0" . a:month
        else
            let l:month_c = a:month
        endif

        if len(a:day) == 1
            let l:day_c = "0" . a:day
        else
            let l:day_c = a:day
        endif
        let l:actionedaday = a:year . "-" . l:month_c. "-" . l:day_c
        let l:due = system("csvgrep.exe -c 8 -m " . l:actionedaday . " " . g:todofile)
        let l:tododuelist = split(l:due,'\r\n')
        if len(l:tododuelist) > 1
            let g:scratch_horizontal = 1   
            call scratch#selection(1)
            let t_index = 0
            call append(0, l:actionedaday)
            for todoitem in l:tododuelist 
                if t_index > 0
                    let l:todo = split( todoitem ,",")
                    let l:tododesc = "Project: " . l:todo[3] . "  " . l:todo[4] . " DUE TODAY"  
                    call append(t_index,tododesc)
                endif
                let t_index = t_index+1         
            endfor
            let g:scratch_horizontal = 0 
        else
            echom "Nothing to do:" . l:actionedaday
        endif