Skip to content
Open
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
36 changes: 32 additions & 4 deletions autoload/codepainter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plugin/codepainter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ command! -nargs=1 PainterPickColor silent! call codepainter#ChangeColor
command! -nargs=1 PainterPickColorByName silent! call codepainter#ChangeColorByName(<f-args>)
command! -nargs=0 PainterEraseAll silent! call codepainter#EraseAll()
command! -nargs=? PainterEraseLine silent! call codepainter#EraseLine(<f-args>)
command! -nargs=? PainterSaveMarks silent! call codepainter#SaveMarks(<f-args>)
command! -nargs=? PainterSaveMarks call codepainter#SaveMarks(<f-args>)
command! -nargs=? PainterLoadMarks silent! call codepainter#LoadMarks(<f-args>)