From d808aedb19896ea043a2be6c657e541811a95af8 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 19 Jan 2025 13:34:15 +0900 Subject: [PATCH 1/2] Allow overwriting existing file and enable file name completion --- plugin/mdip.vim | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/plugin/mdip.vim b/plugin/mdip.vim index 5f61c08..f6276b0 100644 --- a/plugin/mdip.vim +++ b/plugin/mdip.vim @@ -170,13 +170,26 @@ function! s:RandomName() return l:new_random endfunction +function! g:MarkdownPasteImageNames(A, L, P) + let files = glob(s:workdir . '/*.png', '', 1) + let names = map(files, 'fnamemodify(v:val, ":t:r")') + return filter(names, 'v:val =~ "^" . a:A') +endfunction + function! s:InputName() call inputsave() - let name = input('Image name: ') + let name = input('Image name: ', '', 'customlist,MarkdownPasteImageNames') call inputrestore() return name endfunction +function! s:Confirm(message) + call inputsave() + let response = input(a:message) + call inputrestore() + return response =~ '^y' +endfunction + function! g:MarkdownPasteImage(relpath) execute "normal! i![" . g:mdip_tmpname[0:0] let ipos = getcurpos() @@ -208,21 +221,25 @@ function! mdip#MarkdownClipboardImage() " add check whether file with the name exists while 1 - let workdir = s:SafeMakeDir() + let s:workdir = s:SafeMakeDir() " change temp-file-name and image-name let g:mdip_tmpname = s:InputName() if empty(g:mdip_tmpname) let g:mdip_tmpname = g:mdip_imgname . '_' . s:RandomName() endif - let testpath = workdir . '/' . g:mdip_tmpname . '.png' + let testpath = s:workdir . '/' . g:mdip_tmpname + if testpath !~ '\.png$' + let testpath = testpath . '.png' + endif if filereadable(testpath) == 0 break - else - echo "\nThis file name already exists" + elseif s:Confirm("This file name already exists. Overwrite? [y/n] ") + break + endif endif endwhile - let tmpfile = s:SaveFileTMP(workdir, g:mdip_tmpname) + let tmpfile = s:SaveFileTMP(s:workdir, g:mdip_tmpname) if tmpfile == 1 return else From 79410037eef4944ebbeb7701b6ba8e7a0ca7669b Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 20 Jan 2025 11:38:55 +0900 Subject: [PATCH 2/2] Fix error --- plugin/mdip.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin/mdip.vim b/plugin/mdip.vim index f6276b0..f00004d 100644 --- a/plugin/mdip.vim +++ b/plugin/mdip.vim @@ -236,7 +236,6 @@ function! mdip#MarkdownClipboardImage() elseif s:Confirm("This file name already exists. Overwrite? [y/n] ") break endif - endif endwhile let tmpfile = s:SaveFileTMP(s:workdir, g:mdip_tmpname)