Skip to content

Commit 2f1229f

Browse files
committed
:DeleteNote {title} support by github.com/bioe007 (pull request #9)
2 parents 3a5e573 + 57b7168 commit 2f1229f

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ When you execute this command it will start a new note with the selected text as
8282

8383
### The `:DeleteNote` command
8484

85-
The `:DeleteNote` command deletes the current note, destroys the buffer and removes the note from the internal cache of filenames and note titles. This fails when changes have been made to the current buffer, unless you use `:DeleteNote!` which discards any changes.
85+
The `:DeleteNote` command deletes a note file, destroys the buffer and removes the note from the internal cache of filenames and note titles. If you pass a note name as an argument to `:DeleteNote` it will delete the given note, otherwise it will delete the current note. This fails when changes have been made to the buffer, unless you use `:DeleteNote!` which discards any changes.
8686

8787
### The `:SearchNotes` command
8888

autoload/xolox/notes.vim

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
" Note: This file is encoded in UTF-8 including a byte order mark so
77
" that Vim loads the script using the right encoding transparently.
88

9-
let g:xolox#notes#version = '0.11.4'
9+
let g:xolox#notes#version = '0.11.5'
1010

1111
function! xolox#notes#shortcut() " {{{1
1212
" The "note:" pseudo protocol is just a shortcut for the :Note command.
@@ -225,15 +225,25 @@ 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')
231-
if filereadable(filename) && delete(filename)
232-
call xolox#misc#msg#warn("notes.vim %s: Failed to delete %s!", g:xolox#notes#version, filename)
233-
return
228+
function! xolox#notes#delete(bang, title) " {{{1
229+
" Delete the note {title} and close the associated buffer & window.
230+
" If no {title} is given the current note is deleted.
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+
endif
236+
if !xolox#notes#exists(title)
237+
call xolox#misc#msg#warn("notes.vim %s: Failed to delete %s! (not a note)", g:xolox#notes#version, expand('%:p'))
238+
else
239+
let filename = xolox#notes#title_to_fname(title)
240+
if filereadable(filename) && delete(filename)
241+
call xolox#misc#msg#warn("notes.vim %s: Failed to delete %s!", g:xolox#notes#version, filename)
242+
else
243+
call xolox#notes#cache_del(filename)
244+
execute 'bdelete' . a:bang . ' ' . bufnr(filename)
245+
endif
234246
endif
235-
call xolox#notes#cache_del(filename)
236-
execute 'bdelete' . a:bang
237247
endfunction
238248

239249
function! xolox#notes#search(bang, input) " {{{1
@@ -549,6 +559,11 @@ function! xolox#notes#get_titles(include_shadow_notes) " {{{3
549559
return titles
550560
endfunction
551561

562+
function! xolox#notes#exists(title) " {{{3
563+
" Return true if the note {title} exists.
564+
return index(xolox#notes#get_titles(0), a:title, 0, xolox#misc#os#is_win()) >= 0
565+
endfunction
566+
552567
function! xolox#notes#get_fnames_and_titles(include_shadow_notes) " {{{3
553568
" Get dictionary of filename => title pairs of all existing notes.
554569
if !s:have_cached_items

doc/notes.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ as the title of the note.
163163
-------------------------------------------------------------------------------
164164
The *:DeleteNote* command
165165

166-
The |:DeleteNote| command deletes the current note, destroys the buffer and
167-
removes the note from the internal cache of filenames and note titles. This
168-
fails when changes have been made to the current buffer, unless you use
169-
':DeleteNote!' which discards any changes.
166+
The |:DeleteNote| command deletes a note file, destroys the buffer and removes
167+
the note from the internal cache of filenames and note titles. If you pass a
168+
note name as an argument to |:DeleteNote| it will delete the given note,
169+
otherwise it will delete the current note. This fails when changes have been
170+
made to the buffer, unless you use ':DeleteNote!' which discards any changes.
170171

171172
-------------------------------------------------------------------------------
172173
The *:SearchNotes* command

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)