diff --git a/autoload/codepainter.vim b/autoload/codepainter.vim index 92ed606..e0ff5fa 100644 --- a/autoload/codepainter.vim +++ b/autoload/codepainter.vim @@ -21,6 +21,7 @@ let g:marks = {} let g:vim_index = 0 let g:navigation_index = 0 let g:has_nvim = has('nvim') +let g:codepainter_file_keep_original_extension = 1 "dictionary holding the markings folowing this structure "marks = { @@ -216,11 +217,31 @@ endfunc func! codepainter#SaveMarks(...) abort let l:path = a:0 == 0 ? expand("%") : substitute(a:1, "\"", "","g") - let l:aux = expand("%:e") + let l:aux = fnamemodify(expand(l:path), ':e') if len(l:aux) == 0 + " If no extension add .json let l:path = l:path . ".json" else - let l:path = substitute(l:path, expand("%:e"), "json", "") + if l:aux ==# 'json' + " Path already ends with .json + " Potential overwrite if script with marks is also a JSON + " Opt. A: echo error and return + if a:0 == 0 + echom "Current script is JSON" + echom "Pass an arg to PainterSaveMarks to store the codepainters" + return + endif + " Opt. B: replace all next '.json' with '_codepainter.json' + " (not coded) + else + if g:codepainter_file_keep_original_extension + " Add .json to path keeping original extension + let l:path = expand('%:p') . '.json' + else + " Replace extension with .json + let l:path = expand('%:p:r') . '.json' + endif + endif endif let jsonString = json_encode(g:marks) execute ("redir! >" . l:path) @@ -232,11 +253,18 @@ endfunc func! codepainter#LoadMarks(...) abort let l:path = a:0 == 0 ? expand("%") : substitute(a:1, "\"", "","g") if a:0 == 0 - let l:aux = expand("%:e") + let l:aux = fnamemodify(expand(l:path), ':e') if len(l:aux) == 0 + " If no extension add .json let l:path = l:path . ".json" else - let l:path = substitute(l:path, expand("%:e"), "json", "") + if g:codepainter_file_keep_original_extension + " Add .json to path keeping original extension + let l:path = expand('%:p') . '.json' + else + " Replace extension with .json + let l:path = expand('%:p:r') . '.json' + endif endif endif let l:file = readfile(l:path) diff --git a/plugin/codepainter.vim b/plugin/codepainter.vim index a57cd53..7ca7bd0 100644 --- a/plugin/codepainter.vim +++ b/plugin/codepainter.vim @@ -15,5 +15,5 @@ command! -nargs=1 PainterPickColor silent! call codepainter#ChangeColor command! -nargs=1 PainterPickColorByName silent! call codepainter#ChangeColorByName() command! -nargs=0 PainterEraseAll silent! call codepainter#EraseAll() command! -nargs=? PainterEraseLine silent! call codepainter#EraseLine() -command! -nargs=? PainterSaveMarks silent! call codepainter#SaveMarks() +command! -nargs=? PainterSaveMarks call codepainter#SaveMarks() command! -nargs=? PainterLoadMarks silent! call codepainter#LoadMarks()