|
| 1 | +" Vim auto-load script |
| 2 | +" Author: Peter Odding <peter@peterodding.com> |
| 3 | +" Last Change: May 16, 2013 |
| 4 | +" URL: http://peterodding.com/code/vim/notes/ |
| 5 | + |
| 6 | +function! xolox#notes#recent#show(bang, title_filter) " {{{1 |
| 7 | + call xolox#misc#msg#info("notes.vim %s: Generating overview of recent notes ..", g:xolox#notes#version) |
| 8 | + " Show generated note listing all notes by last modified time. |
| 9 | + let starttime = xolox#misc#timer#start() |
| 10 | + let bufname = '[Recent Notes]' |
| 11 | + " Prepare a buffer to hold the list of recent notes. |
| 12 | + call xolox#misc#buffer#prepare({ |
| 13 | + \ 'name': bufname, |
| 14 | + \ 'path': xolox#misc#path#merge($HOME, bufname)}) |
| 15 | + " Filter notes by pattern (argument)? |
| 16 | + let notes = [] |
| 17 | + let title_filter = '\v' . a:title_filter |
| 18 | + for [fname, title] in items(xolox#notes#get_fnames_and_titles(0)) |
| 19 | + if title =~? title_filter |
| 20 | + call add(notes, [getftime(fname), title]) |
| 21 | + endif |
| 22 | + endfor |
| 23 | + " Start note with "You have N note(s) [matching filter]". |
| 24 | + let readme = "You have " |
| 25 | + if empty(notes) |
| 26 | + let readme .= "no notes" |
| 27 | + elseif len(notes) == 1 |
| 28 | + let readme .= "one note" |
| 29 | + else |
| 30 | + let readme .= len(notes) . " notes" |
| 31 | + endif |
| 32 | + if a:title_filter != '' |
| 33 | + let quote_format = xolox#notes#unicode_enabled() ? '‘%s’' : "`%s'" |
| 34 | + let readme .= " matching " . printf(quote_format, a:title_filter) |
| 35 | + endif |
| 36 | + " Explain the sorting of the notes. |
| 37 | + if empty(notes) |
| 38 | + let readme .= "." |
| 39 | + elseif len(notes) == 1 |
| 40 | + let readme .= ", it's listed below." |
| 41 | + else |
| 42 | + let readme .= ". They're listed below grouped by the day they were edited, starting with your most recently edited note." |
| 43 | + endif |
| 44 | + " Add the generated text to the buffer. |
| 45 | + call setline(1, ["Recent notes", "", readme]) |
| 46 | + " Reformat the text in the buffer to auto-wrap. |
| 47 | + normal Ggqq |
| 48 | + " Sort, group and format the list of (matching) notes. |
| 49 | + let last_date = '' |
| 50 | + let list_item_format = xolox#notes#unicode_enabled() ? ' • %s' : ' * %s' |
| 51 | + call sort(notes) |
| 52 | + call reverse(notes) |
| 53 | + let lines = [] |
| 54 | + for [ftime, title] in notes |
| 55 | + let date = xolox#notes#friendly_date(ftime) |
| 56 | + if date != last_date |
| 57 | + call add(lines, '') |
| 58 | + call add(lines, substitute(date, '^\w', '\u\0', '') . ':') |
| 59 | + let last_date = date |
| 60 | + endif |
| 61 | + call add(lines, printf(list_item_format, title)) |
| 62 | + endfor |
| 63 | + " Add the formatted list of notes to the buffer. |
| 64 | + call setline(line('$') + 1, lines) |
| 65 | + " Load the notes file type. |
| 66 | + call xolox#notes#set_filetype() |
| 67 | + let &l:statusline = bufname |
| 68 | + " Change the status line |
| 69 | + " Lock the buffer contents. |
| 70 | + call xolox#misc#buffer#lock() |
| 71 | + " And we're done! |
| 72 | + call xolox#misc#timer#stop("notes.vim %s: Generated %s in %s.", g:xolox#notes#version, bufname, starttime) |
| 73 | +endfunction |
| 74 | + |
| 75 | +function! xolox#notes#recent#track() " {{{1 |
| 76 | + let fname = expand('%:p') |
| 77 | + let indexfile = expand(g:notes_recentindex) |
| 78 | + call xolox#misc#msg#debug("notes.vim %s: Recording '%s' as most recent note in %s ..", g:xolox#notes#version, fname, indexfile) |
| 79 | + if writefile([fname], indexfile) == -1 |
| 80 | + call xolox#misc#msg#warn("notes.vim %s: Failed to record most recent note in %s!", g:xolox#notes#version, indexfile) |
| 81 | + endif |
| 82 | +endfunction |
| 83 | + |
| 84 | +function! xolox#notes#recent#edit(bang) " {{{1 |
| 85 | + " Edit the most recently edited (not necessarily changed) note. |
| 86 | + let indexfile = expand(g:notes_recentindex) |
| 87 | + call xolox#misc#msg#debug("notes.vim %s: Recalling most recent note from %s ..", g:xolox#notes#version, indexfile) |
| 88 | + try |
| 89 | + let fname = readfile(indexfile)[0] |
| 90 | + if empty(fname) |
| 91 | + throw "The index of recent notes is empty?!" |
| 92 | + endif |
| 93 | + catch |
| 94 | + call xolox#misc#msg#warn("notes.vim %s: Failed to recall most recent note from %s: %s", g:xolox#notes#version, indexfile, v:exception) |
| 95 | + return |
| 96 | + endtry |
| 97 | + call xolox#misc#msg#info("notes.vim %s: Editing most recent note '%s' ..", g:xolox#notes#version, fname) |
| 98 | + execute 'edit' . a:bang fnameescape(fname) |
| 99 | + call xolox#notes#set_filetype() |
| 100 | +endfunction |
0 commit comments