comment.vim (5628B)
1 vim9script 2 3 # Maintainer: Maxim Kim <habamax@gmail.com> 4 # Last Update: 2026-06-27 5 # 6 # Toggle comments 7 # Usage: 8 # Add following mappings to vimrc: 9 # import autoload 'dist/comment.vim' 10 # nnoremap <silent> <expr> gc comment.Toggle() 11 # xnoremap <silent> <expr> gc comment.Toggle() 12 # nnoremap <silent> <expr> gcc comment.Toggle() .. '_' 13 # nnoremap <silent> <expr> gC comment.Toggle() .. '$' 14 export def Toggle(): string 15 &opfunc = (_) => { 16 if empty(&cms) || !&ma | return | endif 17 var cms = substitute(substitute(&cms, '\S\zs%s\s*', ' %s', ''), '%s\ze\S', '%s ', '') 18 var [lnum1, lnum2] = [line("'["), line("']")] 19 var cms_l = split(escape(cms, '*.\^$['), '\s*%s\s*') 20 21 var first_col = indent(lnum1) 22 var start_col = getpos("'[")[2] - 1 23 if len(cms_l) == 1 && lnum1 == lnum2 && first_col < start_col 24 var line_start = getline(lnum1)[0 : start_col - 1] 25 var line_end = getline(lnum1)[start_col : -1] 26 line_end = line_end =~ $'\c^\s*{cms_l[0]}' ? 27 \ substitute(line_end, $'\c^\s*\zs{cms_l[0]}\s\ze\s*', line_end =~ '^\s' ? ' ' : '', '') : 28 \ printf(substitute(cms, '%s\@!', '%%', ''), line_end) 29 setline(lnum1, line_start .. line_end) 30 return 31 endif 32 33 if len(cms_l) == 0 | return | endif 34 if len(cms_l) == 1 | call add(cms_l, '') | endif 35 var comment = false 36 var indent_spaces = false 37 var indent_tabs = false 38 var indent_min = indent(lnum1) 39 var indent_start = matchstr(getline(lnum1), '^\s*') 40 for lnum in range(lnum1, lnum2) 41 if getline(lnum) =~ '^\s*$' | continue | endif 42 var indent_str = matchstr(getline(lnum), '^\s*') 43 if indent_min > indent(lnum) 44 indent_min = indent(lnum) 45 indent_start = indent_str 46 endif 47 indent_spaces = indent_spaces || (stridx(indent_str, ' ') != -1) 48 indent_tabs = indent_tabs || (stridx(indent_str, "\t") != -1) 49 if getline(lnum) !~ $'\c^\s*{cms_l[0]}.*{cms_l[1]}$' 50 comment = true 51 endif 52 endfor 53 var mixed_indent = indent_spaces && indent_tabs 54 var lines = [] 55 var line = '' 56 for lnum in range(lnum1, lnum2) 57 if getline(lnum) =~ '^\s*$' 58 line = getline(lnum) 59 elseif comment 60 if exists("g:comment_first_col") || exists("b:comment_first_col") 61 line = printf(substitute(cms, '%s\@!', '%%', 'g'), getline(lnum)) 62 else 63 # consider different whitespace indenting 64 var indent_current = mixed_indent ? matchstr(getline(lnum), '^\s*') : indent_start 65 line = printf(indent_current .. substitute(cms, '%s\@!', '%%', 'g'), 66 strpart(getline(lnum), strlen(indent_current))) 67 endif 68 else 69 line = substitute(getline(lnum), $'\c^\s*\zs{cms_l[0]} \?\| \?{cms_l[1]}$', '', 'g') 70 endif 71 add(lines, line) 72 endfor 73 noautocmd keepjumps setline(lnum1, lines) 74 } 75 return 'g@' 76 enddef 77 78 79 # Comment text object 80 # Usage: 81 # import autoload 'dist/comment.vim' 82 # onoremap <silent>ic <scriptcmd>comment.ObjComment(v:true)<CR> 83 # onoremap <silent>ac <scriptcmd>comment.ObjComment(v:false)<CR> 84 # xnoremap <silent>ic <esc><scriptcmd>comment.ObjComment(v:true)<CR> 85 # xnoremap <silent>ac <esc><scriptcmd>comment.ObjComment(v:false)<CR> 86 export def ObjComment(inner: bool) 87 def IsComment(): bool 88 var stx = map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')->join() 89 return stx =~? 'Comment' 90 enddef 91 92 # requires syntax support 93 if !exists("g:syntax_on") 94 return 95 endif 96 97 var pos_init = getcurpos() 98 99 # If not in comment, search next one, 100 if !IsComment() 101 if search('\v\k+', 'W', line(".") + 100, 100, () => !IsComment()) <= 0 102 return 103 endif 104 endif 105 106 # Search for the beginning of the comment block 107 if IsComment() 108 if search('\v%(\S+)|%(^\s*$)', 'bW', 0, 200, IsComment) > 0 109 search('\v%(\S)|%(^\s*$)', 'W', 0, 200, () => !IsComment()) 110 else 111 cursor(1, 1) 112 search('\v\S+', 'cW', 0, 200) 113 endif 114 endif 115 116 var pos_start = getcurpos() 117 118 if !inner 119 var col = pos_start[2] 120 var prefix = getline(pos_start[1])[ : col - 2] 121 while col > 0 && prefix[col - 2] =~ '\s' 122 col -= 1 123 endwhile 124 pos_start[2] = col 125 endif 126 127 # Search for the comment end. 128 if pos_init[1] > pos_start[1] 129 cursor(pos_init[1], pos_init[2]) 130 endif 131 if search('\v%(\S+)|%(^\s*$)', 'W', 0, 200, IsComment) > 0 132 search('\S', 'beW', 0, 200, () => !IsComment()) 133 else 134 if search('\%$', 'W', 0, 200) > 0 135 search('\ze\S', 'beW', line('.'), 200, () => !IsComment()) 136 endif 137 endif 138 139 var pos_end = getcurpos() 140 141 if !inner 142 var spaces = matchstr(getline(pos_end[1]), '\%>.c\s*') 143 pos_end[2] += spaces->len() 144 if getline(pos_end[1])[pos_end[2] : ] =~ '^\s*$' 145 && (pos_start[2] <= 1 || getline(pos_start[1])[ : pos_start[2]] =~ '^\s*$') 146 if search('\v\s*\_$(\s*\n)+', 'eW', 0, 200) > 0 147 pos_end = getcurpos() 148 endif 149 endif 150 endif 151 152 if (pos_end[2] == (getline(pos_end[1])->len() ?? 1)) && pos_start[2] <= 1 153 cursor(pos_end[1], 1) 154 normal! V 155 cursor(pos_start[1], 1) 156 else 157 cursor(pos_end[1], pos_end[2]) 158 normal! v 159 if &selection == 'exclusive' 160 normal! lo 161 endif 162 cursor(pos_start[1], pos_start[2]) 163 endif 164 enddef