Browse files

Sanity check automatic renaming of notes

I got a report by Lukas Muehlethaler that he lost several notes while
using my notes.vim plug-in. The problem seems to be related to notes
with Unicode filenames. I've fortunately never had this happen to me and
I can't seem to reproduce his problem but if this really is a bug in my
plug-in it's a serious bug: The plug-in should never lose a single note!

I've now added a sanity check that makes sure the new note exists on
disk before the old note is deleted and if this fails a confirmation
dialog will pop up warning the user of the problem and asking them to
report the problem to me.
  • Loading branch information...
xolox committed Jun 8, 2011
1 parent 6143437 commit a7a3d26489920b0f3710ae7ddc1b24736798f705
Showing with 13 additions and 1 deletion.
  1. +12 −0 autoload/xolox/notes.vim
  2. +1 −1 plugin/notes.vim
@@ -184,10 +184,22 @@ function! xolox#notes#save() abort " {{{1
let newpath = xolox#notes#title_to_fname(title)
if newpath == ''
echoerr "Invalid note title"
+ return
let bang = v:cmdbang ? '!' : ''
execute 'saveas' bang fnameescape(newpath)
+ " XXX If {oldpath} and {newpath} end up pointing to the same file on disk
+ " yet xolox#misc#path#equals() doesn't catch this, we might end up
+ " deleting the user's one and only note! One way to circumvent this
+ " potential problem is to first delete the old note and then save the new
+ " note. The problem with this approach is that :saveas might fail in which
+ " case we've already deleted the old note...
if !xolox#misc#path#equals(oldpath, newpath)
+ if !filereadable(newpath)
+ let message = "The notes plug-in tried to rename your note but failed to create %s so won't delete %s or you could lose your note! This should never happen... If you don't mind me borrowing some of your time, please contact me at and include the old and new filename so that I can try to reproduce the issue. Thanks!"
+ call confirm(printf(message, string(newpath), string(oldpath)))
+ return
+ endif
call delete(oldpath)
call xolox#notes#cache_del(oldpath)
@@ -3,7 +3,7 @@
" Last Change: June 8, 2011
" URL:
" License: MIT
-" Version: 0.8.10
+" Version: 0.8.11
" Support for automatic update using the GLVS plug-in.
" GetLatestVimScripts: 3375 1 :AutoInstall:

0 comments on commit a7a3d26

Please sign in to comment.