1 " Code comments preparation helper
2 " Maintainer: Sergey Matveev <stargrave@stargrave.org>
3 " License: GNU General Public License version 3 of the License or later
5 if exists("g:loaded_codecomm") | finish | endif
6 let g:loaded_codecomm = 1
8 if !exists("g:codecomm_file")
9 let g:codecomm_file = "/tmp/codecomm.txt"
12 if !exists("g:codecomm_count") | let g:codecomm_count = 0 | endif
14 function! s:CodeComm() range
16 if bufwinnr("CodeCommCommenting") != -1
17 echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None
20 if bufexists("CodeCommCommenting") != 0
21 bdelete! CodeCommCommenting
23 let g:codecomm_count += 1
24 " Determine file's path inside repository
25 let path = expand("%:p")
26 let path = substitute(path, fugitive#extract_git_dir(".")[:-5], "", "")
27 let path = substitute(path, "^.*\.git//", "", "")
30 if match(path, "/") ==# SHA1_LENGTH
31 let header = add(header, path[:8])
32 let header = add(header, "|")
33 let header = add(header, path[SHA1_LENGTH+1:])
35 let header = add(header, path)
37 let ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))]
38 " Collect enumerated selected code block's lines
39 for bufline_n in range(a:firstline, a:lastline)
40 let fmted = printf("%4d", bufline_n)
41 let line = getline(bufline_n)
42 if len(line) > 0 | let fmted = fmted . " " . line | endif
43 let ready = add(ready, fmted)
45 let ready = add(ready, "---------------------------------- >8 ----------------------------------")
46 " Place commented signs
47 sign define commented text=C texthl=Search
48 for linen in range(a:firstline, a:lastline)
49 let cmd = ":sign place " . linen . " line=" . linen
50 let cmd .= " name=commented buffer=" . bufnr("%")
53 " Spawn a new small code commenting window nonbinded to file
54 new CodeCommCommenting
56 setlocal buftype=acwrite
57 call append("^", ready)
58 " Separate codecomm_file consolidating function, called when buffer is saved
59 autocmd! BufWriteCmd CodeCommCommenting
60 function! s:AppendCC()
61 " Collect already written comments from file if it exists
63 if filereadable(g:codecomm_file)
64 let ccprev = readfile(g:codecomm_file)
66 " Save all those consolidated data to file
67 let ready = ccprev + getline(0, "$") + [""]
68 call writefile(ready, g:codecomm_file)
70 echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
72 autocmd BufWriteCmd CodeCommCommenting call s:AppendCC()
73 " Simple syntax highlighting for that window
74 syntax region CCBlock start=/^-\{5}#/ end=/^-\+ >8 -\+/
75 highlight link CCBlock Statement
80 function! s:CodeCommClear()
81 call writefile([], g:codecomm_file)
82 let g:codecomm_count = 0
83 echohl WarningMsg | echomsg "Comments are wiped" | echohl None
86 command! CodeCommClear call <SID>CodeCommClear()
87 command! -range CodeComm <line1>, <line2> call <SID>CodeComm()
88 vnoremap <silent><Leader>cc :call <SID>CodeComm()<CR>