Skip to content

Commit 57b7168

Browse files
author
Perry Hargrave
committed
DeleteNote accepts a note title as an argument
This way you can delete notes without having to first open them. Also allows completion of note titles. Also prevent deleting and clearing the current (file, buffer) pair if its not a `vim-note` xolox#notes#exists : returns true if a given note exists
1 parent 60526a3 commit 57b7168

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

autoload/xolox/notes.vim

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,28 @@ function! xolox#notes#save() abort " {{{1
225225
endif
226226
endfunction
227227

228-
function! xolox#notes#delete(bang) " {{{1
229-
" Delete the current note, close the associated buffer & window.
230-
let filename = expand('%:p')
228+
function! xolox#notes#delete(bang, title) " {{{1
229+
" Delete the note `title` and closes the associated buffer & window.
230+
" If no `title` is given then use the current note.
231+
let title = xolox#misc#str#trim(a:title)
232+
if title == ''
233+
" Try the current buffer.
234+
let title = xolox#notes#fname_to_title(expand('%:p'))
235+
end
236+
237+
if xolox#notes#exists(title) == 0
238+
call xolox#misc#msg#warn("notes.vim %s: Failed to delete %s! (not a note)",
239+
\ g:xolox#notes#version, expand('%:p'))
240+
return
241+
end
242+
243+
let filename = xolox#notes#title_to_fname(title)
231244
if filereadable(filename) && delete(filename)
232245
call xolox#misc#msg#warn("notes.vim %s: Failed to delete %s!", g:xolox#notes#version, filename)
233246
return
234247
endif
235248
call xolox#notes#cache_del(filename)
236-
execute 'bdelete' . a:bang
249+
execute 'bdelete' . a:bang . bufnr(filename)
237250
endfunction
238251

239252
function! xolox#notes#search(bang, input) " {{{1
@@ -549,6 +562,17 @@ function! xolox#notes#get_titles(include_shadow_notes) " {{{3
549562
return titles
550563
endfunction
551564

565+
function! xolox#notes#exists(title) " {{{3
566+
" Return true if a note `title` exists
567+
let titles = xolox#notes#get_titles(0)
568+
for note in titles
569+
if note == a:title
570+
return 1
571+
end
572+
endfor
573+
return 0
574+
endfunction
575+
552576
function! xolox#notes#get_fnames_and_titles(include_shadow_notes) " {{{3
553577
" Get dictionary of filename => title pairs of all existing notes.
554578
if !s:have_cached_items

plugin/notes.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ endif
4747
" User commands to create, delete and search notes.
4848
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete Note call xolox#notes#edit(<q-bang>, <q-args>)
4949
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>)
50-
command! -bar -bang DeleteNote call xolox#notes#delete(<q-bang>)
50+
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete DeleteNote call xolox#notes#delete(<q-bang>, <q-args>)
5151
command! -bang -nargs=? SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
5252
command! -bar -bang RelatedNotes call xolox#notes#related(<q-bang>)
5353
command! -bar -bang -nargs=? RecentNotes call xolox#notes#recent(<q-bang>, <q-args>)

0 commit comments

Comments
 (0)