Calendar Picker for (Neo)Vim
I really liked the date picker in Neovim Org-mode, but after fighting with Lua for a while I couldn't come up with a way to make it work. I was able to get it to show and to pick a date, but I couldn't get it to do anything with the picked date. Ideally, i'd be able to use it insert a date into the current buffer at the current location and all would be well. I didn't accomplish that, but I did create something else that I've been wanting for a while. Mainly a method of browsing my notes by choosing a calendar day.
In the past, I used T383838calendar-vim and it conveniently let me browse notes that were stored based on their names matching a file pattern. That was when I was putting a ton of information into a single note -- ie one note per day. My new favorite means of naming files doesn't fit that so well, and I like multiple notes on any given day. Turns out that Telescope makes a good intermediate thing for picking files from, so i just needed a way to provide a date pattern to it. Also turns out that T383838calendar.vim makes it really easy to hook actions.
Among the things that can be hooked:
• sign decorations
• selecting a date
The sign decorations are a good way to mark a date that has notes.
The date selection is a good way to fire an action when the user picks a date from the calendar.
There are others, but I'm starting with these.
I've never written a plugin before and I was making use of an existing plugin that's written in VimScript. There are some odd things with VimScript. As far as I can tell, functions have to start with uppercase. And variables require prefixes.
I came up with a solution that when selecting a calendar day will launch Telescope's find file picker and filter based on the selected date.
It has some down sides:
• file name pattern is hard coded
• directory with notes is hard coded
• separator for elements of the name in the pattern are hard coded
Here's some pretty terrible VimScript embedded in a lua block that's in the middle of my lazy setup:
T282828
Tb4b4b4{ Ta5d6ff"Ta5d6ffnvim-telekasten/calendar-vimTa5d6ff"Tb4b4b4,
Te6edf3config Tff7b72= Tff7b72function Tb4b4b4(Tb4b4b4)
Te6edf3vimTb4b4b4.Td2a8ffcmdTb4b4b4([[
Te6edf3let Te6edf3gTb4b4b4:Te6edf3note_cal_sep_char Tff7b72= Ta5d6ff'Ta5d6ff-Ta5d6ff' Ta5d6ff"Ta5d6ff the hyphen separating date elements YY-MM-DD-hh-mm-ss
let g:note_cal_notes_dir = '~/messy/notes/' Ta5d6ff" Te6edf3where Te6edf3to Te6edf3look Tff7b72for Te6edf3note Te6edf3files
Tff7b72function Td2a8ffFormat_numberTb4b4b4(Te6edf3numTb4b4b4)
Tff7b72return Td2a8ffprintfTb4b4b4(Ta5d6ff"Ta5d6ff%02dTa5d6ff"Tb4b4b4, Te6edf3aTb4b4b4:Te6edf3numTb4b4b4)
Te6edf3endfunction
Tff7b72function Td2a8ffFormat_path_subTb4b4b4(Te6edf3dayTb4b4b4, Te6edf3monthTb4b4b4, Te6edf3yearTb4b4b4)
Tff7b72return Td2a8ffFormat_numberTb4b4b4(Te6edf3aTb4b4b4:Te6edf3yearTb4b4b4) Tb4b4b4. Te6edf3gTb4b4b4:Te6edf3note_cal_sep_char Tb4b4b4. Td2a8ffFormat_numberTb4b4b4(Te6edf3aTb4b4b4:Te6edf3monthTb4b4b4) Tb4b4b4. Te6edf3gTb4b4b4:Te6edf3note_cal_sep_char Tb4b4b4. Td2a8ffFormat_numberTb4b4b4(Te6edf3aTb4b4b4:Te6edf3dayTb4b4b4) Tb4b4b4. Te6edf3gTb4b4b4:Te6edf3note_cal_sep_char
Te6edf3endfunction
Tff7b72function Td2a8ffMyHookActionTb4b4b4(Te6edf3dayTb4b4b4, Te6edf3monthTb4b4b4, Te6edf3yearTb4b4b4, Te6edf3weekTb4b4b4, Te6edf3dirTb4b4b4)
Te6edf3let Te6edf3my_path Tff7b72= Td2a8ffFormat_path_subTb4b4b4(Te6edf3aTb4b4b4:Te6edf3dayTb4b4b4, Te6edf3aTb4b4b4:Te6edf3monthTb4b4b4, Te6edf3aTb4b4b4:Te6edf3yearTb4b4b4)
Te6edf3let Te6edf3my_glob Tff7b72= Td2a8ffexpandTb4b4b4(Te6edf3gTb4b4b4:Te6edf3note_cal_notes_dirTb4b4b4) Tb4b4b4. Te6edf3my_path Tb4b4b4. '*'
Te6edf3if !Td2a8ffemptyTb4b4b4(Td2a8ffglobTb4b4b4(Te6edf3my_globTb4b4b4))
Te6edf3execute Ta5d6ff'Ta5d6ff:Telescope find_files cwd=Ta5d6ff' Tb4b4b4. Td2a8ffexpandTb4b4b4(Te6edf3gTb4b4b4:Te6edf3note_cal_notes_dirTb4b4b4) Tb4b4b4. Ta5d6ff'Ta5d6ff search_file=Ta5d6ff' Tb4b4b4. Td2a8ffFormat_path_subTb4b4b4(Te6edf3aTb4b4b4:Te6edf3dayTb4b4b4, Te6edf3aTb4b4b4:Te6edf3monthTb4b4b4, Te6edf3aTb4b4b4:Te6edf3yearTb4b4b4)
Tff7b72else
Te6edf3echo Ta5d6ff"Ta5d6ffNo notes match pattern: Ta5d6ff" Tb4b4b4. Te6edf3my_glob
Te6edf3endif
Te6edf3endfunction
Te6edf3let Te6edf3calendar_action Tff7b72= Ta5d6ff'Ta5d6ffMyHookActionTa5d6ff'
Tff7b72function Td2a8ffMyHookSignTb4b4b4(Te6edf3dayTb4b4b4, Te6edf3monthTb4b4b4, Te6edf3yearTb4b4b4)
Te6edf3let Te6edf3my_path Tff7b72= Td2a8ffFormat_path_subTb4b4b4(Te6edf3aTb4b4b4:Te6edf3dayTb4b4b4, Te6edf3aTb4b4b4:Te6edf3monthTb4b4b4, Te6edf3aTb4b4b4:Te6edf3yearTb4b4b4)
Te6edf3let Te6edf3my_glob Tff7b72= Td2a8ffexpandTb4b4b4(Te6edf3gTb4b4b4:Te6edf3note_cal_notes_dirTb4b4b4) Tb4b4b4. Te6edf3my_path Tb4b4b4. '*'
Te6edf3if !Td2a8ffemptyTb4b4b4(Td2a8ffglobTb4b4b4(Te6edf3my_globTb4b4b4))
Tff7b72return T79c0ff1
Te6edf3endif
Tff7b72return T79c0ff0
Te6edf3endfunction
Te6edf3let Te6edf3calendar_sign Tff7b72= Ta5d6ff'Ta5d6ffMyHookSignTa5d6ff'
Tb4b4b4]])
Tff7b72end
Tb4b4b4},
This is in my list of plugins that are sent to lazy as the things I want to use.
It's not fancy, but it's a start at what I want and it's immeidately useful to me.
Tags: index, nvim, cli
Tags
Navigation