]> Sergey Matveev's repositories - codecomm.git/blob - plugin/codecomm.vim
a2fde108a428c48bdefb5d7be0f371f1834a167a
[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 noreadonly noswapfile buftype=acwrite
56     call append("^", ready)
57     " Separate codecomm_file consolidating function, called when buffer is saved
58     autocmd! BufWriteCmd CodeCommCommenting
59     function! s:AppendCC()
60         " Collect already written comments from file if it exists
61         let ccprev = []
62         if filereadable(g:codecomm_file)
63             let ccprev = readfile(g:codecomm_file)
64         endif
65         " Save all those consolidated data to file
66         let ready = ccprev + getline(0, "$") + [""]
67         call writefile(ready, g:codecomm_file)
68         setlocal nomodified
69         echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
70     endfunction
71     autocmd BufWriteCmd CodeCommCommenting call s:AppendCC()
72     " Simple syntax highlighting for that window
73     syntax region CCBlock start=/^-\{5}#/ end=/^-\+ >8 -\+/
74     highlight link CCBlock Statement
75     normal zR
76     startinsert
77 endfunction
78
79 function! s:CodeCommClear()
80     call writefile([], g:codecomm_file)
81     let g:codecomm_count = 0
82     echohl WarningMsg | echomsg "Comments are wiped" | echohl None
83 endfunction
84
85 command! CodeCommClear call <SID>CodeCommClear()
86 command! -range CodeComm <line1>, <line2> call <SID>CodeComm()
87 vnoremap <silent><Leader>cc :call <SID>CodeComm()<CR>