]> Sergey Matveev's repositories - codecomm.git/blob - plugin/codecomm.vim
e7d1a762ed2bd6daf2edf40fddb6d3e1f5435a06
[codecomm.git] / plugin / codecomm.vim
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
4
5 if exists("g:loaded_codecomm") | finish | endif
6 let g:loaded_codecomm = 1
7
8 if !exists("g:codecomm_file")
9     let g:codecomm_file = "/tmp/codecomm.txt"
10 endif
11
12 if !exists("g:codecomm_count") | let g:codecomm_count = 0 | endif
13
14 function! s:CodeComm() range
15     let SHA1_LENGTH = 40
16     if bufwinnr("CodeCommCommenting") != -1
17         echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None
18         return
19     endif
20     if bufexists("CodeCommCommenting") != 0
21         bdelete! CodeCommCommenting
22     endif
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//", "", "")
28     " Header generation
29     let header = []
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:])
34     else
35         let header = add(header, path)
36     endif
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)
44     endfor
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("%")
51         execute cmd
52     endfor
53     " Spawn a new small code commenting window nonbinded to file
54     new CodeCommCommenting
55     setlocal noswapfile
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
62         let ccprev = []
63         if filereadable(g:codecomm_file)
64             let ccprev = readfile(g:codecomm_file)
65         endif
66         " Save all those consolidated data to file
67         let ready = ccprev + getline(0, "$") + [""]
68         call writefile(ready, g:codecomm_file)
69         setlocal nomodified
70         echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
71     endfunction
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
76     normal zR
77     startinsert
78 endfunction
79
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
84 endfunction
85
86 command! CodeCommClear call <SID>CodeCommClear()
87 command! -range CodeComm <line1>, <line2> call <SID>CodeComm()
88 vnoremap <silent><Leader>cc :call <SID>CodeComm()<CR>