From 2e9d946dfe7ca960524b918fa04895547adac86d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C4=85sowski?= Date: Sun, 25 Oct 2020 17:29:50 +0100 Subject: [PATCH 1/6] Two tests: 1. move to existin dir, 2. move to fresh dir (fails) --- test/test-page/test-move.vim | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/test-page/test-move.vim diff --git a/test/test-page/test-move.vim b/test/test-page/test-move.vim new file mode 100644 index 00000000..6ad63f11 --- /dev/null +++ b/test/test-page/test-move.vim @@ -0,0 +1,16 @@ +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(':h') . '/wiki-tmp/subdir/GoodName.wiki', + \ expand('%:p')) + +silent edit wiki-tmp/index.wiki +silent call wiki#page#rename('nosubdir/index') +call wiki#test#assert_equal( + \ expand(':h') . '/wiki-tmp/nosubdir/index.wiki', + \ expand('%:p')) +quitall! From a3604d704df2828f8592d87e9880f5c9d9b38eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C4=85sowski?= Date: Tue, 27 Oct 2020 10:20:40 +0100 Subject: [PATCH 2/6] Add an error message for missing target directory --- autoload/wiki/page.vim | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index 58aef551..92e948f6 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -68,6 +68,14 @@ function! wiki#page#rename(newname) abort "{{{1 return endif + " The target directory must exist + let l:target_dir = fnamemodify (expand (l:newpath), ':h') + if !isdirectory (l:target_dir) + echom 'wikiError: Cannot rename to "' . l:newpath . '". + \ Target directory "' . l:target_dir . '" does not exist!' + return + endif + " Rename current file to l:newpath try echom printf('wiki: Renaming "%s" to "%s"', From 60aab34ced4c512d34d06fbafab9b0bb15e094ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C4=85sowski?= Date: Sat, 12 Dec 2020 15:47:39 +0100 Subject: [PATCH 3/6] Create the directory after asking user for permission --- autoload/wiki/page.vim | 25 +++++++++++++++---------- test/test-page/test-move.vim | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index 92e948f6..b9fdcf14 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -68,20 +68,12 @@ function! wiki#page#rename(newname) abort "{{{1 return endif - " The target directory must exist - let l:target_dir = fnamemodify (expand (l:newpath), ':h') - if !isdirectory (l:target_dir) - echom 'wikiError: Cannot rename to "' . l:newpath . '". - \ Target directory "' . l:target_dir . '" does not exist!' - return - 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"', @@ -128,11 +120,24 @@ function! wiki#page#rename_ask() abort "{{{1 if input('Rename "' . expand('%:t:r') . '" [y]es/[N]o? ') !~? '^y' return endif - + " Get new page name redraw! echo 'Enter new name (without extension):' let l:name = input('> ') + + " Check if directory if it does not exist + let l:newpath = printf('%s/%s', expand('%:p:h'), l:name) + let l:target_dir = fnamemodify(expand(l:newpath), ':h') + if !isdirectory(l:target_dir) + redraw! + echo "Directory '" . l:target_dir . "' does not exist. " + if input("Create [Y]es/[n]o ? ", "Y") !=? "y" + return + endif + call mkdir(l:target_dir, "p") + endif + call wiki#page#rename(l:name) endfunction diff --git a/test/test-page/test-move.vim b/test/test-page/test-move.vim index 6ad63f11..16b0fed7 100644 --- a/test/test-page/test-move.vim +++ b/test/test-page/test-move.vim @@ -9,7 +9,7 @@ call wiki#test#assert_equal( \ expand('%:p')) silent edit wiki-tmp/index.wiki -silent call wiki#page#rename('nosubdir/index') +call wiki#page#rename('nosubdir/index') call wiki#test#assert_equal( \ expand(':h') . '/wiki-tmp/nosubdir/index.wiki', \ expand('%:p')) From e79161b4f3d89fb89aa0bd1fdf8ea19d9aeb43e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C4=85sowski?= Date: Sat, 12 Dec 2020 17:33:51 +0100 Subject: [PATCH 4/6] Prompt on moving to a new dir, remove the non-prompt test --- autoload/wiki/page.vim | 14 +++++++------- test/test-page/test-move.vim | 5 ----- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index b9fdcf14..ea21e865 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -39,8 +39,8 @@ endfunction 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)) " Check if current file exists if !filereadable(l:oldpath) @@ -95,7 +95,7 @@ function! wiki#page#rename(newname) abort "{{{1 for l:bufname in l:bufs execute 'buffer' fnameescape(l:bufname) update - execute 'bwipeout' fnameescape(l:bufname) + execute 'bwipeout' fnameescape(l:bufname) endfor let b:wiki = l:wiki @@ -120,15 +120,15 @@ function! wiki#page#rename_ask() abort "{{{1 if input('Rename "' . expand('%:t:r') . '" [y]es/[N]o? ') !~? '^y' return endif - + " Get new page name redraw! echo 'Enter new name (without extension):' let l:name = input('> ') - " Check if directory if it does not exist - let l:newpath = printf('%s/%s', expand('%:p:h'), l:name) - let l:target_dir = fnamemodify(expand(l:newpath), ':h') + " Check if directory exists + let l:newpath = printf('%s/%s', expand('%:h'), l:name) + let l:target_dir = simplify(fnamemodify(expand(l:newpath), ':h')) if !isdirectory(l:target_dir) redraw! echo "Directory '" . l:target_dir . "' does not exist. " diff --git a/test/test-page/test-move.vim b/test/test-page/test-move.vim index 16b0fed7..990a0d5f 100644 --- a/test/test-page/test-move.vim +++ b/test/test-page/test-move.vim @@ -8,9 +8,4 @@ call wiki#test#assert_equal( \ expand(':h') . '/wiki-tmp/subdir/GoodName.wiki', \ expand('%:p')) -silent edit wiki-tmp/index.wiki -call wiki#page#rename('nosubdir/index') -call wiki#test#assert_equal( - \ expand(':h') . '/wiki-tmp/nosubdir/index.wiki', - \ expand('%:p')) quitall! From 649ce28deac0ba3fe36ddc3104ce7dd5b97a7909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C4=85sowski?= Date: Sun, 27 Dec 2020 14:19:35 +0100 Subject: [PATCH 5/6] Address the few issues outstanding (mostly minor) --- autoload/wiki/page.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index ea21e865..99f32b50 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -95,7 +95,7 @@ function! wiki#page#rename(newname) abort "{{{1 for l:bufname in l:bufs execute 'buffer' fnameescape(l:bufname) update - execute 'bwipeout' fnameescape(l:bufname) + execute 'bwipeout' fnameescape(l:bufname) endfor let b:wiki = l:wiki @@ -127,13 +127,13 @@ function! wiki#page#rename_ask() abort "{{{1 let l:name = input('> ') " Check if directory exists - let l:newpath = printf('%s/%s', expand('%:h'), l:name) - let l:target_dir = simplify(fnamemodify(expand(l:newpath), ':h')) + let l:target_dir = fnamemodify( + \ simplify(printf('%s/%s', expand('%:p:h'), l:name)), ":p:h") if !isdirectory(l:target_dir) redraw! echo "Directory '" . l:target_dir . "' does not exist. " if input("Create [Y]es/[n]o ? ", "Y") !=? "y" - return + return endif call mkdir(l:target_dir, "p") endif From 373cda0c02dd4185e40a004a09df79a350dc52e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C4=85sowski?= Date: Tue, 29 Dec 2020 09:06:51 +0100 Subject: [PATCH 6/6] Move mkdir from wiki#page#rename_ask to wiki#page#rename --- autoload/wiki/page.vim | 43 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/autoload/wiki/page.vim b/autoload/wiki/page.vim index 99f32b50..b938926a 100644 --- a/autoload/wiki/page.vim +++ b/autoload/wiki/page.vim @@ -36,12 +36,20 @@ 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 = 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) echom 'wiki Error: Cannot rename "' . l:oldpath @@ -67,6 +75,25 @@ 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 @@ -126,19 +153,7 @@ function! wiki#page#rename_ask() abort "{{{1 echo 'Enter new name (without extension):' let l:name = input('> ') - " Check if directory exists - let l:target_dir = fnamemodify( - \ simplify(printf('%s/%s', expand('%:p:h'), l:name)), ":p:h") - if !isdirectory(l:target_dir) - redraw! - echo "Directory '" . l:target_dir . "' does not exist. " - if input("Create [Y]es/[n]o ? ", "Y") !=? "y" - return - endif - call mkdir(l:target_dir, "p") - endif - - call wiki#page#rename(l:name) + call wiki#page#rename(l:name, 'ask') endfunction " }}}1