5 const Separator = "---------------------------------- >8 ----------------------------------"
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
12 if bufexists("CodeCommCommenting") != 0
13 bdelete! CodeCommCommenting
17 if filereadable(g:codecomm_ctr) | g:codecomm_ctr_ = str2nr(readfile(g:codecomm_ctr)[0]) | endif
20 # Determine file's path inside repository
21 var path = expand("%:p")
22 path = substitute(path, gitDir[: -5], "", "")
23 path = substitute(path, "^.*\.git//", "", "")
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 :])
36 header = add(header, path)
38 var ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_ctr_, join(header, " "))]
40 # Collect enumerated selected code block's lines
43 for n in range(firstline, lastline)
44 fmted = printf("%4d", n)
46 if len(line) > 0 | fmted = fmted .. " " .. line | endif
47 ready = add(ready, fmted)
49 ready = add(ready, Separator)
51 # Place commented signs
52 sign define commented text=C texthl=Search
54 for n in range(firstline, lastline)
55 cmd = ":sign place " .. n .. " line=" .. n
56 cmd ..= " name=commented buffer=" .. bufnr("%")
60 # Spawn a new small code commenting window nonbinded to file
61 new CodeCommCommenting
62 setlocal noreadonly noswapfile buftype=acwrite filetype=codecomm
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)
72 echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
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