From: Sergey Matveev Date: Sun, 28 May 2023 14:22:04 +0000 (+0300) Subject: Counter state in external file X-Git-Url: http://www.git.stargrave.org/?p=codecomm.git;a=commitdiff_plain;h=ca495ac6c5d03265caaa8e2b2357cd704ed80d5c Counter state in external file --- diff --git a/autoload/codecomm.vim b/autoload/codecomm.vim index 22a3fd9..ab3fcb3 100644 --- a/autoload/codecomm.vim +++ b/autoload/codecomm.vim @@ -12,7 +12,10 @@ export def Do(firstline: number, lastline: number, gitDir: string) if bufexists("CodeCommCommenting") != 0 bdelete! CodeCommCommenting endif - g:codecomm_count += 1 + + g:codecomm_ctr_ = -1 + if filereadable(g:codecomm_ctr) | g:codecomm_ctr_ = str2nr(readfile(g:codecomm_ctr)[0]) | endif + g:codecomm_ctr_ += 1 # Determine file's path inside repository var path = expand("%:p") @@ -32,7 +35,7 @@ export def Do(firstline: number, lastline: number, gitDir: string) else header = add(header, path) endif - var ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))] + var ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_ctr_, join(header, " "))] # Collect enumerated selected code block's lines var fmted: string @@ -59,13 +62,12 @@ export def Do(firstline: number, lastline: number, gitDir: string) setlocal noreadonly noswapfile buftype=acwrite filetype=codecomm append(0, ready) - # Separate codecomm_file consolidating function, called when buffer is saved - # autocmd! BufWriteCmd CodeCommCommenting autocmd! BufWriteCmd CodeCommCommenting { var ccprev = [" vim: filetype=codecomm", ""] - if filereadable(g:codecomm_file) | ccprev = readfile(g:codecomm_file) | endif + if filereadable(g:codecomm_txt) | ccprev = readfile(g:codecomm_txt) | endif var ready = ccprev + getline(0, "$") + [""] - writefile(ready, g:codecomm_file) + writefile(ready, g:codecomm_txt) + writefile([string(g:codecomm_ctr_)], g:codecomm_ctr) setlocal nomodified echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None } @@ -74,8 +76,8 @@ export def Do(firstline: number, lastline: number, gitDir: string) enddef export def Clear() - writefile([], g:codecomm_file) - g:codecomm_count = 0 + delete(g:codecomm_txt) + delete(g:codecomm_ctr) execute "sign unplace * buffer=" .. bufnr("%") echohl WarningMsg | echomsg "Comments are wiped" | echohl None enddef diff --git a/doc/codecomm.txt b/doc/codecomm.txt index 8099113..9a7549a 100644 --- a/doc/codecomm.txt +++ b/doc/codecomm.txt @@ -12,7 +12,8 @@ The only option for the Vim plugin is the placement of temporary file with aggregated comments. By default it is /tmp/codecomm.txt. You can override /tmp with $TMPDIR environment variable and the whole file path with: > - let g:codecomm_file = "/another/path.txt" + let g:codecomm_txt = "/another/path.txt" + let g:codecomm_ctr = "/another/path.ctr" PLUGIN USAGE *codecomm-usage* diff --git a/plugin/codecomm.vim b/plugin/codecomm.vim index 1b6d1e1..4f9d4ec 100644 --- a/plugin/codecomm.vim +++ b/plugin/codecomm.vim @@ -6,18 +6,17 @@ vim9script if exists("*codecomm#Do") | finish | endif -if !exists("g:codecomm_file") - g:codecomm_file = ((getenv("TMPDIR") == null) ? "/tmp" : getenv("TMPDIR")) .. - "/" .. "codecomm.txt" +if !exists("g:codecomm_txt") + var tmp = (getenv("TMPDIR") == null) ? "/tmp" : getenv("TMPDIR") + g:codecomm_txt = tmp .. "/" .. "codecomm.txt" + g:codecomm_ctr = tmp .. "/" .. "codecomm.ctr" endif -if !exists("g:codecomm_count") | g:codecomm_count = 0 | endif - function CCRangeWrapper() range call codecomm#Do(a:firstline, a:lastline, FugitiveExtractGitDir(getcwd())) endfunction command! CodeCommClear codecomm#Clear() -command! CodeCommEdit :execute "edit " .. g:codecomm_file +command! CodeCommEdit :execute "edit " .. g:codecomm_txt command! -range CodeComm , call CCRangeWrapper() vnoremap cc :call CCRangeWrapper()