Skip to content

Commit

Permalink
add commentary plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Nov 24, 2023
1 parent c9b188c commit 0d1ac59
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ctrlpvim/ctrlp.vim
mattn/emmet-vim
scrooloose/nerdtree
NLKNguyen/papercolor-theme
tpope/vim-commentary
2 changes: 2 additions & 0 deletions plugins/vim-commentary-master/.github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: tpope
custom: ["https://www.paypal.me/vimpope"]
1 change: 1 addition & 0 deletions plugins/vim-commentary-master/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/doc/tags
1 change: 1 addition & 0 deletions plugins/vim-commentary-master/CONTRIBUTING.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See the [contribution guidelines for pathogen.vim](https://github.com/tpope/vim-pathogen/blob/master/CONTRIBUTING.markdown).
49 changes: 49 additions & 0 deletions plugins/vim-commentary-master/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# commentary.vim

Comment stuff out. Use `gcc` to comment out a line (takes a count),
`gc` to comment out the target of a motion (for example, `gcap` to
comment out a paragraph), `gc` in visual mode to comment out the selection,
and `gc` in operator pending mode to target a comment. You can also use
it as a command, either with a range like `:7,17Commentary`, or as part of a
`:global` invocation like with `:g/TODO/Commentary`. That's it.

I wrote this because 5 years after Vim added support for mapping an
operator, I still couldn't find a commenting plugin that leveraged that
feature (I overlooked
[tcomment.vim](https://github.com/tomtom/tcomment_vim)). Striving for
minimalism, it weighs in at under 100 lines of code.

Oh, and it uncomments, too. The above maps actually toggle, and `gcgc`
uncomments a set of adjacent commented lines.

## Installation

Install using your favorite package manager, or use Vim's built-in package
support:

mkdir -p ~/.vim/pack/tpope/start
cd ~/.vim/pack/tpope/start
git clone https://tpope.io/vim/commentary.git
vim -u NONE -c "helptags commentary/doc" -c q

## FAQ

> My favorite file type isn't supported!
Relax! You just have to adjust `'commentstring'`:

autocmd FileType apache setlocal commentstring=#\ %s

## Self-Promotion

Like commentary.vim? Follow the repository on
[GitHub](https://github.com/tpope/vim-commentary) and vote for it on
[vim.org](http://www.vim.org/scripts/script.php?script_id=3695). And if
you're feeling especially charitable, follow [tpope](http://tpo.pe/) on
[Twitter](http://twitter.com/tpope) and
[GitHub](https://github.com/tpope).

## License

Copyright (c) Tim Pope. Distributed under the same terms as Vim itself.
See `:help license`.
35 changes: 35 additions & 0 deletions plugins/vim-commentary-master/doc/commentary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
*commentary.txt* Comment stuff out

Author: Tim Pope <http://tpo.pe/>
License: Same terms as Vim itself (see |license|)

Comment stuff out. Then uncomment it later. Relies on 'commentstring' to be
correctly set, or uses b:commentary_format if it is set.

Assign b:commentary_startofline to insert comment characters at column 1
regardless of indentation.

*gc*
gc{motion} Comment or uncomment lines that {motion} moves over.

*gcc*
gcc Comment or uncomment [count] lines.

*v_gc*
{Visual}gc Comment or uncomment the highlighted lines.

*o_gc*
gc Text object for a comment (operator pending mode
only.)

*gcgc* *gcu*
gcgc Uncomment the current and adjacent commented lines.
gcu

*:Commentary*
:[range]Commentary Comment or uncomment [range] lines

The |User| CommentaryPost autocommand fires after a successful operation and
can be used for advanced customization.

vim:tw=78:et:ft=help:norl:
121 changes: 121 additions & 0 deletions plugins/vim-commentary-master/plugin/commentary.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
" commentary.vim - Comment stuff out
" Maintainer: Tim Pope <http://tpo.pe/>
" Version: 1.3
" GetLatestVimScripts: 3695 1 :AutoInstall: commentary.vim

if exists("g:loaded_commentary") || v:version < 703
finish
endif
let g:loaded_commentary = 1

function! s:surroundings() abort
return split(get(b:, 'commentary_format', substitute(substitute(substitute(
\ &commentstring, '^$', '%s', ''), '\S\zs%s',' %s', '') ,'%s\ze\S', '%s ', '')), '%s', 1)
endfunction

function! s:strip_white_space(l,r,line) abort
let [l, r] = [a:l, a:r]
if l[-1:] ==# ' ' && stridx(a:line,l) == -1 && stridx(a:line,l[0:-2]) == 0
let l = l[:-2]
endif
if r[0] ==# ' ' && (' ' . a:line)[-strlen(r)-1:] != r && a:line[-strlen(r):] == r[1:]
let r = r[1:]
endif
return [l, r]
endfunction

function! s:go(...) abort
if !a:0
let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$')
return 'g@'
elseif a:0 > 1
let [lnum1, lnum2] = [a:1, a:2]
else
let [lnum1, lnum2] = [line("'["), line("']")]
endif

let [l, r] = s:surroundings()
let uncomment = 2
let force_uncomment = a:0 > 2 && a:3
for lnum in range(lnum1,lnum2)
let line = matchstr(getline(lnum),'\S.*\s\@<!')
let [l, r] = s:strip_white_space(l,r,line)
if len(line) && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
let uncomment = 0
endif
endfor

if get(b:, 'commentary_startofline')
let indent = '^'
else
let indent = '^\s*'
endif

let lines = []
for lnum in range(lnum1,lnum2)
let line = getline(lnum)
if strlen(r) > 2 && l.r !~# '\\'
let line = substitute(line,
\'\M' . substitute(l, '\ze\S\s*$', '\\zs\\d\\*\\ze', '') . '\|' . substitute(r, '\S\zs', '\\zs\\d\\*\\ze', ''),
\'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
endif
if force_uncomment
if line =~ '^\s*' . l
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
endif
elseif uncomment
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
else
let line = substitute(line,'^\%('.matchstr(getline(lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
endif
call add(lines, line)
endfor
call setline(lnum1, lines)
let modelines = &modelines
try
set modelines=0
silent doautocmd User CommentaryPost
finally
let &modelines = modelines
endtry
return ''
endfunction

function! s:textobject(inner) abort
let [l, r] = s:surroundings()
let lnums = [line('.')+1, line('.')-2]
for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']]
while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
let lnums[index] += dir
let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!')
let [l, r] = s:strip_white_space(l,r,line)
endwhile
endfor
while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0]))
let lnums[0] += 1
endwhile
while a:inner && empty(getline(lnums[1]))
let lnums[1] -= 1
endwhile
if lnums[0] <= lnums[1]
execute 'normal! 'lnums[0].'GV'.lnums[1].'G'
endif
endfunction

command! -range -bar -bang Commentary call s:go(<line1>,<line2>,<bang>0)
xnoremap <expr> <Plug>Commentary <SID>go()
nnoremap <expr> <Plug>Commentary <SID>go()
nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
xmap gc <Plug>Commentary
nmap gc <Plug>Commentary
omap gc <Plug>Commentary
nmap gcc <Plug>CommentaryLine
nmap gcu <Plug>Commentary<Plug>Commentary
endif

" vim:set et sw=2:

0 comments on commit 0d1ac59

Please sign in to comment.