" Code comments preparation helper " Maintainer: Sergey Matveev " License: GNU General Public License version 3 of the License or later if exists("g:loaded_codecomm") | finish | endif let g:loaded_codecomm = 1 if !exists("g:codecomm_file") let g:codecomm_file = "/tmp/codecomm.txt" endif if !exists("g:codecomm_count") | let g:codecomm_count = 0 | endif function! s:CodeComm() range let SHA1_LENGTH = 40 if bufwinnr("CodeCommCommenting") != -1 echohl ErrorMsg | echomsg "Close already existing code commenting window first" | echohl None return endif if bufexists("CodeCommCommenting") != 0 bdelete! CodeCommCommenting endif let g:codecomm_count += 1 " Determine file's path inside repository let path = expand("%:p") let path = substitute(path, fugitive#extract_git_dir(".")[:-5], "", "") let path = substitute(path, "^.*\.git//", "", "") " Header generation let header = [] if match(path, "/") ==# SHA1_LENGTH let header = add(header, path[:8]) let header = add(header, "|") let header = add(header, path[SHA1_LENGTH+1:]) else let header = add(header, path) endif let ready = [printf("-----#%2d [ %54S ]-----", g:codecomm_count, join(header, " "))] " Collect enumerated selected code block's lines for bufline_n in range(a:firstline, a:lastline) let fmted = printf("%4d", bufline_n) let line = getline(bufline_n) if len(line) > 0 | let fmted = fmted . " " . line | endif let ready = add(ready, fmted) endfor let ready = add(ready, "---------------------------------- >8 ----------------------------------") " Spawn a new small code commenting window nonbinded to file new CodeCommCommenting setlocal noswapfile setlocal buftype=acwrite call append("^", ready) " Separate codecomm_file consolidating function, called when buffer is saved autocmd! BufWriteCmd CodeCommCommenting function! s:AppendCC() " Collect already written comments from file if it exists let ccprev = [] if filereadable(g:codecomm_file) let ccprev = readfile(g:codecomm_file) endif " Save all those consolidated data to file let ready = ccprev + getline(0, "$") + [""] call writefile(ready, g:codecomm_file) setlocal nomodified echohl MoreMsg | echomsg "Commented:" len(ready) "lines" | echohl None endfunction autocmd BufWriteCmd CodeCommCommenting call s:AppendCC() " Simple syntax highlighting for that window syntax region CCBlock start=/^-\{5}#/ end=/^-\+ >8 -\+/ highlight link CCBlock Statement normal zR startinsert endfunction function! s:CodeCommClear() call writefile([], g:codecomm_file) let g:codecomm_count = 0 echohl WarningMsg | echomsg "Comments are wiped" | echohl None endfunction command! CodeCommClear call CodeCommClear() command! -range CodeComm , call CodeComm() vnoremap cc :call CodeComm()