]> Sergey Matveev's repositories - codecomm.git/commitdiff
Counter state in external file
authorSergey Matveev <stargrave@stargrave.org>
Sun, 28 May 2023 14:22:04 +0000 (17:22 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 28 May 2023 14:22:04 +0000 (17:22 +0300)
autoload/codecomm.vim
doc/codecomm.txt
plugin/codecomm.vim

index 22a3fd98a812f7a911df1704e0e1074045ae045f..ab3fcb3f1a9dd4da01a342f2011cbb4c0f7804f1 100644 (file)
@@ -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
index 809911334993fce26f3acf07f08f19ea849c5375..9a7549ae9ddca45866a0b8f8da530dce1dd0ab16 100644 (file)
@@ -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*
 
index 1b6d1e1ed0eb849ddb057c2f65bf67f14167964a..4f9d4eca9f0c8e3fe57c87b8870f170c4d248331 100644 (file)
@@ -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 <line1>, <line2> call <SID>CCRangeWrapper()
 vnoremap <silent><Leader>cc :call <SID>CCRangeWrapper()<CR>