]> Sergey Matveev's repositories - codecomm.git/blob - autoload/codecomm.vim
Counter state in external file
[codecomm.git] / autoload / codecomm.vim
1 vim9script
2
3 const SHA1Len = 40
4 const SHA256Len = 64
5 const Separator = "---------------------------------- >8 ----------------------------------"
6
7 export def Do(firstline: number, lastline: number, gitDir: string)
8     if bufwinnr("CodeCommCommenting") != -1
9         echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None
10         return
11     endif
12     if bufexists("CodeCommCommenting") != 0
13         bdelete! CodeCommCommenting
14     endif
15
16     g:codecomm_ctr_ = -1
17     if filereadable(g:codecomm_ctr) | g:codecomm_ctr_ = str2nr(readfile(g:codecomm_ctr)[0]) | endif
18     g:codecomm_ctr_ += 1
19
20     # Determine file's path inside repository
21     var path = expand("%:p")
22     path = substitute(path, gitDir[: -5], "", "")
23     path = substitute(path, "^.*\.git//", "", "")
24
25     # Header generation
26     var header: list<string>
27     if match(path, "/") ==# SHA1Len
28         header = add(header, path[: 8])
29         header = add(header, "|")
30         header = add(header, path[SHA1Len + 1 :])
31     elseif match(path, "/") ==# SHA256Len
32         header = add(header, path[: 8])
33         header = add(header, "|")
34         header = add(header, path[SHA256Len + 1 :])
35     else
36         header = add(header, path)
37     endif
38     var ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_ctr_, join(header, " "))]
39
40     # Collect enumerated selected code block's lines
41     var fmted: string
42     var line: string
43     for n in range(firstline, lastline)
44         fmted = printf("%4d", n)
45         line = getline(n)
46         if len(line) > 0 | fmted = fmted .. " " .. line | endif
47         ready = add(ready, fmted)
48     endfor
49     ready = add(ready, Separator)
50
51     # Place commented signs
52     sign define commented text=C texthl=Search
53     var cmd: string
54     for n in range(firstline, lastline)
55         cmd = ":sign place " .. n .. " line=" .. n
56         cmd ..= " name=commented buffer=" .. bufnr("%")
57         execute cmd
58     endfor
59
60     # Spawn a new small code commenting window nonbinded to file
61     new CodeCommCommenting
62     setlocal noreadonly noswapfile buftype=acwrite filetype=codecomm
63     append(0, ready)
64
65     autocmd! BufWriteCmd CodeCommCommenting {
66         var ccprev = [" vim: filetype=codecomm", ""]
67         if filereadable(g:codecomm_txt) | ccprev = readfile(g:codecomm_txt) | endif
68         var ready = ccprev + getline(0, "$") + [""]
69         writefile(ready, g:codecomm_txt)
70         writefile([string(g:codecomm_ctr_)], g:codecomm_ctr)
71         setlocal nomodified
72         echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
73     }
74     normal zR
75     startinsert
76 enddef
77
78 export def Clear()
79     delete(g:codecomm_txt)
80     delete(g:codecomm_ctr)
81     execute "sign unplace * buffer=" .. bufnr("%")
82     echohl WarningMsg | echomsg "Comments are wiped" | echohl None
83 enddef