Skip to content

Commit 2349000

Browse files
committed
Enable syncing of filenames <-> titles (suggested by Julien Lancien)
1 parent f6ec550 commit 2349000

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ The suffix to add to generated filenames. The plug-in generates filenames for yo
4040

4141
:let g:notes_suffix = '.txt'
4242

43+
### The `g:notes_title_sync` option
44+
45+
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.
46+
47+
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.
48+
4349
### The `g:notes_shadowdir` option
4450

4551
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.

autoload/xolox/notes.vim

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
" Vim auto-load script
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: September 29, 2011
3+
" Last Change: October 18, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55

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.6'
9+
let g:xolox#notes#version = '0.12'
1010

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

55+
function! xolox#notes#check_sync_title() " {{{1
56+
if g:notes_title_sync != 'no' && xolox#notes#buffer_is_note()
57+
" Check if the note's title and filename are out of sync.
58+
let name_on_disk = xolox#misc#path#absolute(expand('%:p'))
59+
let name_from_title = xolox#notes#title_to_fname(getline(1))
60+
if !xolox#misc#path#equals(name_on_disk, name_from_title)
61+
let action = g:notes_title_sync
62+
if action == 'prompt'
63+
" Prompt the user what to do (if anything). First we perform a redraw
64+
" to make sure the note's content is visible (without this the Vim
65+
" window would be blank in my tests).
66+
redraw
67+
let message = "The note's title and filename do not correspond. What do you want to do?"
68+
let options = "Change &title\nRename &file\nDo &nothing"
69+
let choice = confirm(message, options, 3, 'Question')
70+
if choice == 1
71+
let action = 'change_title'
72+
elseif choice == 2
73+
let action = 'rename_file'
74+
else
75+
" User chose to do nothing or <Escape>'d the prompt.
76+
return
77+
endif
78+
" Intentional fall through here :-)
79+
endif
80+
if action == 'change_title'
81+
let new_title = xolox#notes#fname_to_title(name_on_disk)
82+
call setline(1, new_title)
83+
setlocal modified
84+
call xolox#misc#msg#info("notes.vim %s: Changed note title to match filename.", g:xolox#notes#version)
85+
elseif action == 'rename_file'
86+
let new_fname = xolox#notes#title_to_fname(getline(1))
87+
if rename(name_on_disk, new_fname) == 0
88+
execute 'edit' fnameescape(new_fname)
89+
setlocal filetype=notes
90+
call xolox#misc#msg#info("notes.vim %s: Renamed file to match note title.", g:xolox#notes#version)
91+
else
92+
call xolox#misc#msg#warn("notes.vim %s: Failed to rename file to match note title?!", g:xolox#notes#version)
93+
endif
94+
endif
95+
endif
96+
endif
97+
endfunction
98+
5499
function! xolox#notes#from_selection(bang) " {{{1
55100
" TODO This will always open a new buffer in the current window which I
56101
" don't consider very friendly (because the user loses his/her context),
@@ -312,7 +357,7 @@ function! xolox#notes#related(bang) " {{{1
312357
call xolox#misc#msg#warn("notes.vim %s: :RelatedNotes only works on named buffers!", g:xolox#notes#version)
313358
else
314359
let filename = xolox#misc#path#absolute(bufname)
315-
if &filetype == 'notes' && xolox#misc#path#equals(g:notes_directory, expand('%:h'))
360+
if xolox#notes#buffer_is_note()
316361
let pattern = '\<' . s:words_to_pattern(getline(1)) . '\>'
317362
let keywords = getline(1)
318363
else
@@ -399,6 +444,10 @@ function! xolox#notes#recent(bang, title_filter) " {{{1
399444
call xolox#misc#timer#stop("notes.vim %s: Created list of notes in %s.", g:xolox#notes#version, start)
400445
endfunction
401446

447+
function! xolox#notes#buffer_is_note() " {{{1
448+
return &filetype == 'notes' && xolox#misc#path#equals(expand('%:p:h'), g:notes_directory)
449+
endfunction
450+
402451
" Miscellaneous functions. {{{1
403452

404453
function! xolox#notes#friendly_date(time) " {{{2

doc/notes.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ extension in the note's title, e.g.:
9393
>
9494
:let g:notes_suffix = '.txt'
9595
96+
-------------------------------------------------------------------------------
97+
The *g:notes_title_sync* option
98+
99+
When you rename a file in your notes directory but don't change the title, the
100+
plug-in will notice this the next time you open the note in Vim. Likewise when
101+
you change the title in another text editor but don't rename the file. By
102+
default the plug-in will prompt you whether you want it to update the title of
103+
the note, rename the file on disk or dismiss the prompt without doing
104+
anything.
105+
106+
If you set this option to the string 'no' this feature will be completely
107+
disabled. If you set it to 'change_title' it will automatically change the
108+
title to match the filename. If you set it to 'rename_file' it will
109+
automatically rename the file on disk to match the title.
110+
96111
-------------------------------------------------------------------------------
97112
The *g:notes_shadowdir* option
98113

ftplugin/notes.vim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim file type plug-in
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: September 4, 2011
3+
" Last Change: October 18, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55

66
if exists('b:did_ftplugin')
@@ -100,4 +100,11 @@ let b:undo_ftplugin .= ' | execute "sunmap <buffer> <S-Tab>"'
100100
" Automatically remove empty list items on Enter. {{{1
101101
inoremap <buffer> <silent> <expr> <CR> xolox#notes#cleanup_list()
102102
103+
" }}}1
104+
105+
" This is currently the only place where a command is guaranteed to be
106+
" executed when the user edits a note. Maybe I shouldn't abuse this (it
107+
" doesn't feel right ;-) but for now it will do.
108+
call xolox#notes#check_sync_title()
109+
103110
" vim: ts=2 sw=2 et

plugin/notes.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim plug-in
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: September 4, 2011
3+
" Last Change: October 18, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55

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

47+
" Define the default action when a note's filename and title are out of sync.
48+
if !exists('g:notes_title_sync')
49+
" Valid values are "no", "change_title", "rename_file" and "prompt".
50+
let g:notes_title_sync = 'prompt'
51+
endif
52+
4753
" User commands to create, delete and search notes.
4854
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete Note call xolox#notes#edit(<q-bang>, <q-args>)
4955
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>)

0 commit comments

Comments
 (0)