Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions autoload/wiki/page.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ function! wiki#page#delete() abort "{{{1
endfunction

"}}}1
function! wiki#page#rename(newname) abort "{{{1
function! wiki#page#rename(newname, ...) abort "{{{1
redraw!
let l:oldpath = expand('%:p')
let l:newpath = printf('%s/%s.%s',
\ expand('%:p:h'), a:newname, b:wiki.extension)
let l:newpath = simplify(printf('%s/%s.%s',
\ expand('%:p:h'), a:newname, b:wiki.extension))

let l:dir_mode = get (a:, 1, 'abort')
if index (['abort', 'ask', 'create'], l:dir_mode) ==? -1
echom "wiki Error: The second argument to wiki#page#rename must be "
\ . "either 'abort', 'ask', or 'create', but '"
\ . l:dir_mode . "' received."
return
end

" Check if current file exists
if !filereadable(l:oldpath)
Expand All @@ -67,13 +75,32 @@ function! wiki#page#rename(newname) abort "{{{1
\ . '". File with that name exist!'
return
endif

" Check if directory exists
let l:target_dir = fnamemodify(l:newpath, ":p:h")
if !isdirectory(l:target_dir)
if l:dir_mode ==? 'abort'
echo "Directory '" . l:target_dir . "' does not exist. Aborting."
return
elseif l:dir_mode ==? 'ask'
redraw!
echo "Directory '" . l:target_dir . "' does not exist. "
if input("Create [Y]es/[n]o ? ", "Y") !=? "y"
return
endif
echo '\n'
end
" At this point dir_mode is 'create' or the user said 'yes'
echo "Creating directory '" . l:target_dir . "'."
call mkdir(l:target_dir, "p")
endif

" Rename current file to l:newpath
try
echom printf('wiki: Renaming "%s" to "%s"',
\ expand('%:t') , fnamemodify(l:newpath, ':t'))
if rename(l:oldpath, l:newpath) != 0
throw 'Cannot rename!'
throw 'wiki.vim: Cannot rename file!'
end
catch
echom printf('wiki Error: Cannot rename "%s" to "%s"',
Expand Down Expand Up @@ -125,7 +152,8 @@ function! wiki#page#rename_ask() abort "{{{1
redraw!
echo 'Enter new name (without extension):'
let l:name = input('> ')
call wiki#page#rename(l:name)

call wiki#page#rename(l:name, 'ask')
endfunction

" }}}1
Expand Down
11 changes: 11 additions & 0 deletions test/test-page/test-move.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source ../init.vim
runtime plugin/wiki.vim

silent edit wiki-tmp/BadName.wiki

silent call wiki#page#rename('subdir/GoodName')
call wiki#test#assert_equal(
\ expand('<sfile>:h') . '/wiki-tmp/subdir/GoodName.wiki',
\ expand('%:p'))

quitall!