Skip to content

Commit

Permalink
Enable syncing of filenames <-> titles (suggested by Julien Lancien)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Oct 18, 2011
1 parent f6ec550 commit 2349000
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -40,6 +40,12 @@ The suffix to add to generated filenames. The plug-in generates filenames for yo

:let g:notes_suffix = '.txt'

### The `g:notes_title_sync` option

When you rename a file in your notes directory but don't change the title, the plug-in will notice this the next time you open the note in Vim. Likewise when you change the title in another text editor but don't rename the file. By default the plug-in will prompt you whether you want it to update the title of the note, rename the file on disk or dismiss the prompt without doing anything.

If you set this option to the string `'no'` this feature will be completely disabled. If you set it to `'change_title'` it will automatically change the title to match the filename. If you set it to `'rename_file'` it will automatically rename the file on disk to match the title.

### The `g:notes_shadowdir` option

The notes plug-in comes with some default notes containing documentation about the plug-in. This option defines the path of the directory containing these notes.
Expand Down
55 changes: 52 additions & 3 deletions autoload/xolox/notes.vim
@@ -1,12 +1,12 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: September 29, 2011
" Last Change: October 18, 2011
" URL: http://peterodding.com/code/vim/notes/

" Note: This file is encoded in UTF-8 including a byte order mark so
" that Vim loads the script using the right encoding transparently.

let g:xolox#notes#version = '0.11.6'
let g:xolox#notes#version = '0.12'

function! xolox#notes#shortcut() " {{{1
" The "note:" pseudo protocol is just a shortcut for the :Note command.
Expand All @@ -32,6 +32,7 @@ function! xolox#notes#edit(bang, title) abort " {{{1
else
let title = 'New note'
endif
" At this point we're dealing with a new note.
let fname = xolox#notes#title_to_fname(title)
execute 'edit' . a:bang fnameescape(fname)
setlocal filetype=notes
Expand All @@ -51,6 +52,50 @@ function! xolox#notes#edit(bang, title) abort " {{{1
call xolox#misc#timer#stop('notes.vim %s: Started new note in %s.', g:xolox#notes#version, starttime)
endfunction

function! xolox#notes#check_sync_title() " {{{1
if g:notes_title_sync != 'no' && xolox#notes#buffer_is_note()
" Check if the note's title and filename are out of sync.
let name_on_disk = xolox#misc#path#absolute(expand('%:p'))
let name_from_title = xolox#notes#title_to_fname(getline(1))
if !xolox#misc#path#equals(name_on_disk, name_from_title)
let action = g:notes_title_sync
if action == 'prompt'
" Prompt the user what to do (if anything). First we perform a redraw
" to make sure the note's content is visible (without this the Vim
" window would be blank in my tests).
redraw
let message = "The note's title and filename do not correspond. What do you want to do?"
let options = "Change &title\nRename &file\nDo &nothing"
let choice = confirm(message, options, 3, 'Question')
if choice == 1
let action = 'change_title'
elseif choice == 2
let action = 'rename_file'
else
" User chose to do nothing or <Escape>'d the prompt.
return
endif
" Intentional fall through here :-)
endif
if action == 'change_title'
let new_title = xolox#notes#fname_to_title(name_on_disk)
call setline(1, new_title)
setlocal modified
call xolox#misc#msg#info("notes.vim %s: Changed note title to match filename.", g:xolox#notes#version)
elseif action == 'rename_file'
let new_fname = xolox#notes#title_to_fname(getline(1))
if rename(name_on_disk, new_fname) == 0
execute 'edit' fnameescape(new_fname)
setlocal filetype=notes
call xolox#misc#msg#info("notes.vim %s: Renamed file to match note title.", g:xolox#notes#version)
else
call xolox#misc#msg#warn("notes.vim %s: Failed to rename file to match note title?!", g:xolox#notes#version)
endif
endif
endif
endif
endfunction

function! xolox#notes#from_selection(bang) " {{{1
" TODO This will always open a new buffer in the current window which I
" don't consider very friendly (because the user loses his/her context),
Expand Down Expand Up @@ -312,7 +357,7 @@ function! xolox#notes#related(bang) " {{{1
call xolox#misc#msg#warn("notes.vim %s: :RelatedNotes only works on named buffers!", g:xolox#notes#version)
else
let filename = xolox#misc#path#absolute(bufname)
if &filetype == 'notes' && xolox#misc#path#equals(g:notes_directory, expand('%:h'))
if xolox#notes#buffer_is_note()
let pattern = '\<' . s:words_to_pattern(getline(1)) . '\>'
let keywords = getline(1)
else
Expand Down Expand Up @@ -399,6 +444,10 @@ function! xolox#notes#recent(bang, title_filter) " {{{1
call xolox#misc#timer#stop("notes.vim %s: Created list of notes in %s.", g:xolox#notes#version, start)
endfunction

function! xolox#notes#buffer_is_note() " {{{1
return &filetype == 'notes' && xolox#misc#path#equals(expand('%:p:h'), g:notes_directory)
endfunction

" Miscellaneous functions. {{{1

function! xolox#notes#friendly_date(time) " {{{2
Expand Down
15 changes: 15 additions & 0 deletions doc/notes.txt
Expand Up @@ -93,6 +93,21 @@ extension in the note's title, e.g.:
>
:let g:notes_suffix = '.txt'
-------------------------------------------------------------------------------
The *g:notes_title_sync* option

When you rename a file in your notes directory but don't change the title, the
plug-in will notice this the next time you open the note in Vim. Likewise when
you change the title in another text editor but don't rename the file. By
default the plug-in will prompt you whether you want it to update the title of
the note, rename the file on disk or dismiss the prompt without doing
anything.

If you set this option to the string 'no' this feature will be completely
disabled. If you set it to 'change_title' it will automatically change the
title to match the filename. If you set it to 'rename_file' it will
automatically rename the file on disk to match the title.

-------------------------------------------------------------------------------
The *g:notes_shadowdir* option

Expand Down
9 changes: 8 additions & 1 deletion ftplugin/notes.vim
@@ -1,6 +1,6 @@
" Vim file type plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: September 4, 2011
" Last Change: October 18, 2011
" URL: http://peterodding.com/code/vim/notes/

if exists('b:did_ftplugin')
Expand Down Expand Up @@ -100,4 +100,11 @@ let b:undo_ftplugin .= ' | execute "sunmap <buffer> <S-Tab>"'
" Automatically remove empty list items on Enter. {{{1
inoremap <buffer> <silent> <expr> <CR> xolox#notes#cleanup_list()
" }}}1

" This is currently the only place where a command is guaranteed to be
" executed when the user edits a note. Maybe I shouldn't abuse this (it
" doesn't feel right ;-) but for now it will do.
call xolox#notes#check_sync_title()

" vim: ts=2 sw=2 et
8 changes: 7 additions & 1 deletion plugin/notes.vim
@@ -1,6 +1,6 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: September 4, 2011
" Last Change: October 18, 2011
" URL: http://peterodding.com/code/vim/notes/

" Support for automatic update using the GLVS plug-in.
Expand Down Expand Up @@ -44,6 +44,12 @@ if !exists('g:notes_tagsindex')
let g:notes_tagsindex = s:plugindir . '/tags.txt'
endif

" Define the default action when a note's filename and title are out of sync.
if !exists('g:notes_title_sync')
" Valid values are "no", "change_title", "rename_file" and "prompt".
let g:notes_title_sync = 'prompt'
endif

" User commands to create, delete and search notes.
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete Note call xolox#notes#edit(<q-bang>, <q-args>)
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>)
Expand Down

0 comments on commit 2349000

Please sign in to comment.