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