From b5b9158a61c186371988361ce4dc2eb74415ed52 Mon Sep 17 00:00:00 2001 From: juanMarinero Date: Wed, 7 May 2025 20:31:54 +0200 Subject: [PATCH 1/3] fix(SaveMarks, LoadMarks): get extension of l:path If no arg passed then l:aux is still expand("%:e") otherwise extension depends on arg passed --- autoload/codepainter.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/codepainter.vim b/autoload/codepainter.vim index 92ed606..cc8e907 100644 --- a/autoload/codepainter.vim +++ b/autoload/codepainter.vim @@ -216,7 +216,7 @@ 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 let l:path = l:path . ".json" else @@ -232,7 +232,7 @@ 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 let l:path = l:path . ".json" else From daa941a2923377960ce060fc31008f8558a13d09 Mon Sep 17 00:00:00 2001 From: juanMarinero Date: Wed, 7 May 2025 20:34:26 +0200 Subject: [PATCH 2/3] style(SaveMarks and LoadMarks): add comment --- autoload/codepainter.vim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autoload/codepainter.vim b/autoload/codepainter.vim index cc8e907..bf2fcaf 100644 --- a/autoload/codepainter.vim +++ b/autoload/codepainter.vim @@ -218,6 +218,7 @@ func! codepainter#SaveMarks(...) abort let l:path = a:0 == 0 ? expand("%") : substitute(a:1, "\"", "","g") 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", "") @@ -234,6 +235,7 @@ func! codepainter#LoadMarks(...) abort if a:0 == 0 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", "") From 1cec922cbd739dbb61b9d9fea657b383d24481d6 Mon Sep 17 00:00:00 2001 From: juanMarinero Date: Wed, 7 May 2025 20:36:21 +0200 Subject: [PATCH 3/3] fix(Save-/LoadMarks): add opt keep original file-ext g:codepainter_file_keep_original_extension to keep (1) or not (0) original file extension in JSON file to store markers. Also: - added error-msg if file to store markers is current JSON - 'silent' removed from PainterSaveMarks to see those error-msgs --- autoload/codepainter.vim | 30 ++++++++++++++++++++++++++++-- plugin/codepainter.vim | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/autoload/codepainter.vim b/autoload/codepainter.vim index bf2fcaf..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 = { @@ -221,7 +222,26 @@ func! codepainter#SaveMarks(...) abort " 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) @@ -238,7 +258,13 @@ func! codepainter#LoadMarks(...) abort " 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()