From 5400a5d4269874fe4f1c35dfdd3c039ea17dfd62 Mon Sep 17 00:00:00 2001 From: Maxim Kim Date: Fri, 26 Apr 2024 19:53:13 +0200 Subject: runtime(comment): include a simple comment toggling plugin fixes #14626 closes: #14634 Signed-off-by: Maxim Kim Signed-off-by: Christian Brabandt --- runtime/pack/dist/opt/comment/autoload/comment.vim | 58 +++++++++++++++++++++ runtime/pack/dist/opt/comment/doc/comment.txt | 59 ++++++++++++++++++++++ runtime/pack/dist/opt/comment/plugin/comment.vim | 9 ++++ 3 files changed, 126 insertions(+) create mode 100644 runtime/pack/dist/opt/comment/autoload/comment.vim create mode 100644 runtime/pack/dist/opt/comment/doc/comment.txt create mode 100644 runtime/pack/dist/opt/comment/plugin/comment.vim (limited to 'runtime/pack') diff --git a/runtime/pack/dist/opt/comment/autoload/comment.vim b/runtime/pack/dist/opt/comment/autoload/comment.vim new file mode 100644 index 0000000000..9dda266ffa --- /dev/null +++ b/runtime/pack/dist/opt/comment/autoload/comment.vim @@ -0,0 +1,58 @@ +vim9script + +# Maintainer: Maxim Kim +# Last Update: 2024-04-26 +# +# Toggle comments +# Usage: +# Add following mappings to vimrc: +# import autoload 'dist/comment.vim' +# nnoremap gc comment.Toggle() +# xnoremap gc comment.Toggle() +# nnoremap gcc comment.Toggle() .. '_' +export def Toggle(...args: list): string + if len(args) == 0 + &opfunc = matchstr(expand(''), '[^. ]*\ze[') + return 'g@' + endif + if empty(&cms) || !&ma | return '' | endif + var cms = substitute(substitute(&cms, '\S\zs%s\s*', ' %s', ''), '%s\ze\S', '%s ', '') + var [lnum1, lnum2] = [line("'["), line("']")] + var cms_l = split(escape(cms, '*.'), '\s*%s\s*') + if len(cms_l) == 0 | return '' | endif + if len(cms_l) == 1 | call add(cms_l, '') | endif + var comment = 0 + var indent_min = indent(lnum1) + var indent_start = matchstr(getline(lnum1), '^\s*') + for lnum in range(lnum1, lnum2) + if getline(lnum) =~ '^\s*$' | continue | endif + if indent_min > indent(lnum) + indent_min = indent(lnum) + indent_start = matchstr(getline(lnum), '^\s*') + endif + if getline(lnum) !~ $'^\s*{cms_l[0]}.*{cms_l[1]}$' + comment = 1 + endif + endfor + var lines = [] + var line = '' + for lnum in range(lnum1, lnum2) + if getline(lnum) =~ '^\s*$' + line = getline(lnum) + elseif comment + if exists("g:comment_first_col") || exists("b:comment_first_col") + # handle % with substitute + line = printf(substitute(cms, '%s\@!', '%%', 'g'), getline(lnum)) + else + # handle % with substitute + line = printf(indent_start .. substitute(cms, '%s\@!', '%%', 'g'), + strpart(getline(lnum), strlen(indent_start))) + endif + else + line = substitute(getline(lnum), $'^\s*\zs{cms_l[0]} \?\| \?{cms_l[1]}$', '', 'g') + endif + add(lines, line) + endfor + noautocmd keepjumps setline(lnum1, lines) + return '' +enddef diff --git a/runtime/pack/dist/opt/comment/doc/comment.txt b/runtime/pack/dist/opt/comment/doc/comment.txt new file mode 100644 index 0000000000..e2bf7557fc --- /dev/null +++ b/runtime/pack/dist/opt/comment/doc/comment.txt @@ -0,0 +1,59 @@ +*comment.txt* For Vim version 9.1. Last change: 2024 Apr 26 + + + VIM REFERENCE MANUAL + +Commenting and un-commenting text. + +============================================================================== + +The comment.vim package, allows to toggle comments for a single line, a range +of lines or a selected text object. It defines the following mappings: + + *gcc* +gcc to comment/uncomment current line + *o_gc* +gc{motion} to toggle comments for the selected motion + *gcip* +gcip to comment/uncomment current paragraph + *gcG* +gcG to comment/uncomment from current line till the end of a buffer + *v_gc* +{Visual}gc to comment/uncomment the highlighted lines. + +The plugin uses the buffer-local 'commentstring' option value to add or remove +comment markers to the selected lines. Whether it will comment or un-comment +depends on the first line of the range of lines to act upon. When it matches +a comment marker, the line will be un-commented, if it doesn't, the line will +be commented out. Blank and empty lines are not touched. + +If the mapping does not seem to work, chances are high, that this particular +filetype is either not detected by Vim or the filetype plugin does not set the +'commentstring' option. + +You can simply configure this using the following autocommand (e.g. for legacy +Vim script): > + + autocmd Filetype vim :setlocal commentstring="\ %s + +This example sets the " as start of a comment for legacy Vim Script. For Vim9 +script, you would instead use the "#" char: > + + autocmd Filetype vim :setlocal commentstring=#\ %s + +============================================================================== +Options: + +*g:comment_first_col* +*b:comment_first_col* + By default comment chars are added in front of the line, i.e. if the line + was indented, commented line would stay indented as well. + + However some filetypes require a comment char on the first column, use this option + to change default behaviour. + + Use g:comment_first_col to change it globally or b:comment_first_col to + target specific filetype(s). + +============================================================================== +vim:tw=78:ts=8:fo=tcq2:ft=help: diff --git a/runtime/pack/dist/opt/comment/plugin/comment.vim b/runtime/pack/dist/opt/comment/plugin/comment.vim new file mode 100644 index 0000000000..94ac0670bf --- /dev/null +++ b/runtime/pack/dist/opt/comment/plugin/comment.vim @@ -0,0 +1,9 @@ +vim9script + +# Maintainer: Maxim Kim +# Last Update: 2024-04-26 + +import autoload 'comment.vim' +nnoremap gc comment.Toggle() +xnoremap gc comment.Toggle() +nnoremap gcc comment.Toggle() .. '_' -- cgit v1.2.3