]> Sergey Matveev's repositories - codecomm.git/blob - plugin/codecomm.vim
2f108853e649de15f9d3a904caa915ada88c3ba0
[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 tmpdir = getenv("TMPDIR")
10     if tmpdir == v:null | let tmpdir = "/tmp" | endif
11     let g:codecomm_file = tmpdir . "/" . "codecomm.txt"
12 endif
13
14 if !exists("g:codecomm_count") | let g:codecomm_count = 0 | endif
15
16 function! s:CodeComm() range
17     let SHA1_LENGTH = 40
18     if bufwinnr("CodeCommCommenting") != -1
19         echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None
20         return
21     endif
22     if bufexists("CodeCommCommenting") != 0
23         bdelete! CodeCommCommenting
24     endif
25     let g:codecomm_count += 1
26     " Determine file's path inside repository
27     let path = expand("%:p")
28     let path = substitute(path, fugitive#extract_git_dir(".")[:-5], "", "")
29     let path = substitute(path, "^.*\.git//", "", "")
30     " Header generation
31     let header = []
32     if match(path, "/") ==# SHA1_LENGTH
33         let header = add(header, path[:8])
34         let header = add(header, "|")
35         let header = add(header, path[SHA1_LENGTH+1:])
36     else
37         let header = add(header, path)
38     endif
39     let ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))]
40     " Collect enumerated selected code block's lines
41     for bufline_n in range(a:firstline, a:lastline)
42         let fmted = printf("%4d", bufline_n)
43         let line = getline(bufline_n)
44         if len(line) > 0 | let fmted = fmted . " " . line | endif
45         let ready = add(ready, fmted)
46     endfor
47     let ready = add(ready, "---------------------------------- >8 ----------------------------------")
48     " Place commented signs
49     sign define commented text=C texthl=Search
50     for linen in range(a:firstline, a:lastline)
51         let cmd = ":sign place " . linen . " line=" . linen
52         let cmd .= " name=commented buffer=" . bufnr("%")
53         execute cmd
54     endfor
55     " Spawn a new small code commenting window nonbinded to file
56     new CodeCommCommenting
57     setlocal noreadonly noswapfile buftype=acwrite
58     call append("^", ready)
59     " Separate codecomm_file consolidating function, called when buffer is saved
60     autocmd! BufWriteCmd CodeCommCommenting
61     function! s:AppendCC()
62         " Collect already written comments from file if it exists
63         let ccprev = []
64         if filereadable(g:codecomm_file)
65             let ccprev = readfile(g:codecomm_file)
66         endif
67         " Save all those consolidated data to file
68         let ready = ccprev + getline(0, "$") + [""]
69         call writefile(ready, g:codecomm_file)
70         setlocal nomodified
71         echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None
72     endfunction
73     autocmd BufWriteCmd CodeCommCommenting call s:AppendCC()
74     " Simple syntax highlighting for that window
75     syntax region CCBlock start=/^-\{5}#/ end=/^-\+ >8 -\+/
76     highlight link CCBlock Statement
77     normal zR
78     startinsert
79 endfunction
80
81 function! s:CodeCommClear()
82     call writefile([], g:codecomm_file)
83     let g:codecomm_count = 0
84     echohl WarningMsg | echomsg "Comments are wiped" | echohl None
85 endfunction
86
87 command! CodeCommClear call <SID>CodeCommClear()
88 command! -range CodeComm <line1>, <line2> call <SID>CodeComm()
89 vnoremap <silent><Leader>cc :call <SID>CodeComm()<CR>