|
1 | 1 | " Vim auto-load script
|
2 | 2 | " Author: Peter Odding <peter@peterodding.com>
|
3 |
| -" Last Change: June 8, 2011 |
| 3 | +" Last Change: June 11, 2011 |
4 | 4 | " URL: http://peterodding.com/code/vim/notes/
|
5 | 5 |
|
6 | 6 | " Note: This file is encoded in UTF-8 including a byte order mark so
|
@@ -309,6 +309,78 @@ function! xolox#notes#related(bang) " {{{1
|
309 | 309 | endif
|
310 | 310 | endfunction
|
311 | 311 |
|
| 312 | +function! xolox#notes#recent(bang, title_filter) " {{{1 |
| 313 | + let start = xolox#misc#timer#start() |
| 314 | + let bufname = '[All Notes]' |
| 315 | + " Open buffer that holds list of notes. |
| 316 | + if !bufexists(bufname) |
| 317 | + execute 'hide edit' fnameescape(bufname) |
| 318 | + setlocal buftype=nofile nospell |
| 319 | + else |
| 320 | + execute 'hide buffer' fnameescape(bufname) |
| 321 | + setlocal noreadonly modifiable |
| 322 | + silent %delete |
| 323 | + endif |
| 324 | + " Filter notes by pattern (argument)? |
| 325 | + let notes = [] |
| 326 | + let title_filter = '\v' . a:title_filter |
| 327 | + for [fname, title] in items(xolox#notes#get_fnames_and_titles()) |
| 328 | + if title =~? title_filter |
| 329 | + call add(notes, [getftime(fname), title]) |
| 330 | + endif |
| 331 | + endfor |
| 332 | + " Start note with title and short description. |
| 333 | + let readme = "You have " |
| 334 | + if empty(notes) |
| 335 | + let readme .= "no notes" |
| 336 | + elseif len(notes) == 1 |
| 337 | + let readme .= "one note" |
| 338 | + else |
| 339 | + let readme .= len(notes) . " notes" |
| 340 | + endif |
| 341 | + if a:title_filter != '' |
| 342 | + let quote_format = xolox#notes#unicode_enabled() ? '‘%s’' : "`%s'" |
| 343 | + let readme .= " matching " . printf(quote_format, a:title_filter) |
| 344 | + endif |
| 345 | + if empty(notes) |
| 346 | + let readme .= "." |
| 347 | + elseif len(notes) == 1 |
| 348 | + let readme .= ", it's listed below." |
| 349 | + else |
| 350 | + let readme .= ". They're listed below grouped by the day they were edited, starting with your most recently edited note." |
| 351 | + endif |
| 352 | + call setline(1, ["All notes", "", readme]) |
| 353 | + normal Ggqq |
| 354 | + " Sort, group and format list of (matching) notes. |
| 355 | + let last_date = '' |
| 356 | + let list_item_format = xolox#notes#unicode_enabled() ? ' • %s' : ' * %s' |
| 357 | + let date_format = '%A, %B %d:' |
| 358 | + let today = strftime(date_format, localtime()) |
| 359 | + let yesterday = strftime(date_format, localtime() - 60*60*24) |
| 360 | + call sort(notes) |
| 361 | + call reverse(notes) |
| 362 | + let lines = [] |
| 363 | + for [ftime, title] in notes |
| 364 | + let date = strftime(date_format, ftime) |
| 365 | + " Add date heading because date changed? |
| 366 | + if date != last_date |
| 367 | + call add(lines, '') |
| 368 | + if date == today |
| 369 | + call add(lines, "Today:") |
| 370 | + elseif date == yesterday |
| 371 | + call add(lines, "Yesterday:") |
| 372 | + else |
| 373 | + call add(lines, date) |
| 374 | + endif |
| 375 | + let last_date = date |
| 376 | + endif |
| 377 | + call add(lines, printf(list_item_format, title)) |
| 378 | + endfor |
| 379 | + call setline(line('$') + 1, lines) |
| 380 | + setlocal readonly nomodifiable nomodified filetype=notes |
| 381 | + call xolox#misc#timer#stop("%s: Created list of notes in %s.", s:script, start) |
| 382 | +endfunction |
| 383 | + |
312 | 384 | " Miscellaneous functions. {{{1
|
313 | 385 |
|
314 | 386 | function! s:is_empty_buffer() " {{{2
|
|
0 commit comments