Permalink
Browse files

Automatically rename notes when title changed

 * Split rename() into two phases (see cleanup()).
 * Renamed edit() to shortcut() to clarify intent.
  • Loading branch information...
1 parent bd9b792 commit 26741868c3b576afdeb7a8693ac341e9edad4ba1 @xolox committed Dec 23, 2010
Showing with 45 additions and 52 deletions.
  1. +1 −1 TODO.md
  2. +24 −28 autoload.vim
  3. +0 −3 filetype.vim
  4. +20 −20 notes.vim
View
2 TODO.md
@@ -2,4 +2,4 @@
* Add a key mapping or command to toggle the visibility of `{{{ … }}}` code markers?
* Find a good way to support notes with generates contents, e.g. *'all notes'*.
- * Automatically rename notes on disk when their titles change?!
+ * When renaming a note, also update references to the note in other notes? (make this optional of course!)
View
52 autoload.vim
@@ -21,34 +21,40 @@ function! xolox#notes#new(bang) " {{{1
endfunction
function! xolox#notes#rename() " {{{1
- " Set the filename of notes based on their title.
- if line('.') > 1 && xolox#notes#title_changed()
- let oldname = expand('%:p')
- let title = getline(1)
- let newname = xolox#notes#title_to_fname(title)
- if !xolox#path#equals(newname, oldname)
- call xolox#notes#cache_del(oldname)
- execute 'silent file' fnameescape(newname)
- " Delete empty, useless buffer created by :file?
- if oldname != ''
- execute 'silent bwipeout' fnameescape(oldname)
+ " When the current note's title is changed, automatically rename the buffer.
+ if &filetype == 'notes' && &modified && line('.') > 1
+ let oldpath = expand('%:p')
+ let newpath = xolox#notes#title_to_fname(getline(1))
+ if newpath != '' && !xolox#path#equals(oldpath, newpath)
+ if oldpath != '' && !exists('b:notes_oldfname')
+ let b:notes_oldfname = oldpath
endif
- call xolox#notes#cache_add(newname, title)
+ execute 'silent file' fnameescape(newpath)
" Redraw tab line with new filename.
let &stal = &stal
endif
- call xolox#notes#remember_title()
endif
endfunction
-function! xolox#notes#edit(bang, fname) " {{{1
- " Edit an existing note using a command such as :edit note:keyword.
+function! xolox#notes#cleanup() " {{{1
+ " Once the user has saved the note under a new name, remove the old file.
+ if exists('b:notes_oldfname')
+ if filereadable(b:notes_oldfname)
+ call delete(b:notes_oldfname)
+ endif
+ unlet b:notes_oldfname
+ endif
+endfunction
+
+function! xolox#notes#shortcut() " {{{1
+ " Edit existing notes using commands such as :edit note:keyword.
let starttime = xolox#timer#start()
let notes = {}
+ let bang = v:cmdbang ? '!' : ''
let filename = ''
- let arguments = xolox#trim(matchstr(a:fname, 'note:\zs.*'))
+ let arguments = xolox#trim(matchstr(expand('<afile>'), 'note:\zs.*'))
if arguments == ''
- call xolox#notes#new(a:bang)
+ call xolox#notes#new(bang)
return
endif
for [fname, title] in items(xolox#notes#get_fnames_and_titles())
@@ -84,7 +90,7 @@ function! xolox#notes#edit(bang, fname) " {{{1
if empty(filename)
call xolox#warning("No matching notes!")
else
- execute 'edit' . (v:cmdbang ? '!' : '') v:cmdarg fnameescape(filename)
+ execute 'edit' . bang v:cmdarg fnameescape(filename)
setlocal filetype=notes
call xolox#timer#stop('%s: Opened note in %s.', s:script, starttime)
endif
@@ -489,16 +495,6 @@ function! s:normalize_ws(s)
return xolox#trim(substitute(a:s, '\_s\+', '', 'g'))
endfunction
-function! xolox#notes#remember_title() " {{{3
- " Remember the title of the current note to detect renaming.
- let b:note_title = getline(1)
-endfunction
-
-function! xolox#notes#title_changed() " {{{3
- " Check whether the title of the current note has been modified.
- return &modified && !(exists('b:note_title') && b:note_title == getline(1))
-endfunction
-
function! xolox#notes#foldexpr() " {{{3
" Folding expression to fold atx style Markdown headings.
let lastlevel = foldlevel(v:lnum - 1)
View
3 filetype.vim
@@ -9,9 +9,6 @@ else
let b:did_ftplugin = 1
endif
-" Remember the original title of the current note.
-call xolox#notes#remember_title()
-
" Disable highlighting of matching pairs. {{{1
setlocal matchpairs=
let b:undo_ftplugin = 'set matchpairs<'
View
40 notes.vim
@@ -1,9 +1,9 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
-" Last Change: December 22, 2010
+" Last Change: December 23, 2010
" URL: http://peterodding.com/code/vim/notes/
" License: MIT
-" Version: 0.7.6
+" Version: 0.7.7
" Support for automatic update using the GLVS plug-in.
" GetLatestVimScripts: 3375 1 :AutoInstall: session.zip
@@ -49,36 +49,36 @@ if !exists('g:notes_indexscript')
endif
endif
-" Define user commands to create notes.
+" User commands to create, delete and search notes.
command! -bar -bang NewNote call xolox#notes#new(<q-bang>)
command! -bar -bang DeleteNote call xolox#notes#delete(<q-bang>)
command! -bar -bang -nargs=1 SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
command! -bar -bang RelatedNotes call xolox#notes#related(<q-bang>)
-" Install an automatic command to edit notes using filenames like "note:todo".
+" Automatic commands to enable the :edit note:… shortcut and load the notes file type.
+
+function! s:DefAutoCmd(events, directory, command)
+ " Resolve the path to the directory with notes; Vim matches filename
+ " patterns in automatic commands after resolving filenames, which means
+ " the automatic commands will also apply when symbolic links are used.
+ let directory = xolox#path#absolute(a:directory)
+ let pattern = xolox#path#merge(fnameescape(directory), '*')
+ execute 'autocmd' a:events pattern a:command
+endfunction
+
augroup PluginNotes
autocmd!
" NB: "nested" is used here so that SwapExists automatic commands apply
" to notes (which is IMHO better than always showing the E325 prompt).
- au BufReadCmd note:* nested call xolox#notes#edit(v:cmdbang ? '!' : '', expand('<afile>'))
+ au BufReadCmd note:* nested call xolox#notes#shortcut()
au SwapExists * call xolox#notes#swaphack()
+ call s:DefAutoCmd('BufWritePost', g:notes_directory, 'call xolox#notes#cleanup()')
augroup END
-" Install automatic commands that recognize the notes file type.
-
-function! s:NewAutoCmd(directory)
- " Resolve the path to the directory with notes. Because Vim matches filename
- " patterns in automatic commands after resolving the filename, this makes sure
- " the automatic command below always applies, even in case of symbolic links.
- let directory = xolox#path#absolute(a:directory)
- let pattern = xolox#path#merge(fnameescape(directory), '*')
- augroup filetypedetect
- exe 'au BufNewFile,BufRead,BufWritePost' pattern 'if &bt == "" | setl ft=notes | endif'
- augroup END
-endfunction
-
-call s:NewAutoCmd(g:notes_directory)
-call s:NewAutoCmd(g:notes_shadowdir)
+augroup filetypedetect
+ call s:DefAutoCmd('BufNewFile,BufRead,BufWritePost', g:notes_directory, 'if &bt == "" | setl ft=notes | endif')
+ call s:DefAutoCmd('BufNewFile,BufRead,BufWritePost', g:notes_shadowdir, 'if &bt == "" | setl ft=notes | endif')
+augroup END
" Make sure the plug-in is only loaded once.
let g:loaded_notes = 1

0 comments on commit 2674186

Please sign in to comment.