Skip to content

Instantly share code, notes, and snippets.

@kosei27
Forked from hokaccha/endtagcomment.vim
Created December 9, 2010 07:35
Show Gist options
  • Select an option

  • Save kosei27/734448 to your computer and use it in GitHub Desktop.

Select an option

Save kosei27/734448 to your computer and use it in GitHub Desktop.

Revisions

  1. kosei27 revised this gist Dec 16, 2010. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -85,9 +85,8 @@ endfunction
    nnoremap ,ti :<C-u>call Endtagcomment('id')<CR>
    nnoremap ,tc :<C-u>call Endtagcomment('class')<CR>
    nnoremap ,ta :<C-u>call Endtagcomment('id_class')<CR>
    "nnoremap ,tg :<C-u>call Endtagcomment('tag_id_class')<CR>
    nnoremap ,tg :<C-u>call Endtagcomment('tag_id_class')<CR>
    nmap ,t :<C-u>normal ,tc<CR>
    nmap ,tt :<C-u>normal ,tc<CR>

  2. kosei27 revised this gist Dec 9, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    " endtagcomment.vim
    " こういうHTMLがあったときに
    " <div id="hoge" class="fuga">
    " ...
  3. kosei27 revised this gist Dec 9, 2010. 1 changed file with 69 additions and 4 deletions.
    73 changes: 69 additions & 4 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,17 @@
    " <div id="hoge" class="fuga">
    " ...
    " <!-- /div#hoge.fuga --></div>
    "
    " ----------
    " update:2010-12-08 @kosei27
    "
    " ,ti でidのみを出力
    " ,tc でclassのみを出力(",tt" ",t" も同様)
    " ,ta でidとclassを出力
    " ,t<Space> でコメント内のテキストの前後の空白をトグル
    " <!--/hoge--> <-> <!-- /hoge -->

    function! Endtagcomment()
    function! Endtagcomment(type)
    let reg_save = @@

    try
    @@ -26,6 +35,10 @@ function! Endtagcomment()
    let start_tag = matchstr(html, '\v(\<.{-}\>)')
    let tag_name = matchstr(start_tag, '\v([a-zA-Z]+)')

    let comment_type = a:type
    let comment_space = g:endtagcomment_space
    let firstclass_symbol = g:endtagcomment_firstclass_symbol

    let id = ''
    let id_match = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')
    if exists('id_match[1]')
    @@ -35,21 +48,73 @@ function! Endtagcomment()
    let class = ''
    let class_match = matchlist(start_tag, '\vclass\=["'']([^"'']+)["'']')
    if exists('class_match[1]')
    let class = '.' . join(split(class_match[1], '\v\s+'), '.')
    "let class = '.' . join(split(class_match[1], '\v\s+'), '.')
    let class = join(split(class_match[1], '\v\s+'), '.')
    endif

    execute "normal `>va<\<Esc>`<"

    if l:comment_type == 'id'
    let g:endtagcommentFormat = '<!--%comment_space/%id%comment_space-->'
    elseif l:comment_type == 'class'
    let g:endtagcommentFormat = '<!--%comment_space/%firstclass_symbol%class%comment_space-->'
    elseif l:comment_type == 'id_class'
    let g:endtagcommentFormat = '<!--%comment_space/%id.%class%comment_space-->'
    elseif l:comment_type == 'tag_id_class'
    let g:endtagcommentFormat = '<!--%comment_space/%tag_name%id.%class%comment_space-->'
    endif

    let comment = g:endtagcommentFormat
    let comment = substitute(comment, '%comment_space', comment_space, 'g')
    let comment = substitute(comment, '%firstclass_symbol', firstclass_symbol, 'g')
    let comment = substitute(comment, '%tag_name', tag_name, 'g')
    let comment = substitute(comment, '%id', id, 'g')
    let comment = substitute(comment, '%class', class, 'g')

    let @@ = comment

    normal ""P

    let @@ = reg_save
    endfunction

    let g:endtagcommentFormat = '<!-- /%tag_name%id%class -->'
    nnoremap ,t :<C-u>call Endtagcomment()<CR>
    "let g:endtagcommentFormat = '<!-- /%tag_name%id%class -->'

    "" keymap
    nnoremap ,ti :<C-u>call Endtagcomment('id')<CR>
    nnoremap ,tc :<C-u>call Endtagcomment('class')<CR>
    nnoremap ,ta :<C-u>call Endtagcomment('id_class')<CR>
    "nnoremap ,tg :<C-u>call Endtagcomment('tag_id_class')<CR>

    nmap ,t :<C-u>normal ,tc<CR>
    nmap ,tt :<C-u>normal ,tc<CR>

    " コメント内のテキストの前後の空白
    let g:endtagcomment_space = ''
    "" 有無をトグル
    function! Endtagcomment_space_toggle()
    let comment_space = g:endtagcomment_space
    if l:comment_space == ''
    let g:endtagcomment_space = ' '
    else
    let g:endtagcomment_space = ''
    endif
    endfunction
    """ keymap
    nnoremap <silent> ,t<Space> :<C-u>call Endtagcomment_space_toggle()<CR>

    " コメント内のテキストの最初のclass名の「.」
    let g:endtagcomment_firstclass_symbol = ''
    "" 有無をトグル(id+classの場合は固定で「.」がつきます)
    function! Endtagcomment_firstclass_symbol_toggle()
    let firstclass_symbol = g:endtagcomment_firstclass_symbol
    if l:firstclass_symbol == ''
    let g:endtagcomment_firstclass_symbol = '.'
    else
    let g:endtagcomment_firstclass_symbol = ''
    endif
    endfunction
    "" keymap
    nnoremap <silent> ,t. :<C-u>call Endtagcomment_firstclass_symbol_toggle()<CR>
  4. @hokaccha hokaccha revised this gist May 25, 2010. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    " こういうHTMLがあったときに
    " <div id="hoge" class="fuga">
    " ...
    " </div>
    "
    " 実行するとこうなる
    " <div id="hoge" class="fuga">
    " ...
    " <!-- /div#hoge.fuga --></div>

    function! Endtagcomment()
    let reg_save = @@

  5. @hokaccha hokaccha revised this gist May 25, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ function! Endtagcomment()
    let class = '.' . join(split(class_match[1], '\v\s+'), '.')
    endif

    execute "normal `]va<\<Esc>`<"
    execute "normal `>va<\<Esc>`<"

    let comment = g:endtagcommentFormat
    let comment = substitute(comment, '%tag_name', tag_name, 'g')
  6. @hokaccha hokaccha revised this gist May 25, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -41,5 +41,5 @@ function! Endtagcomment()
    let @@ = reg_save
    endfunction

    let g:endtagcommentFormat = '<!-- %tag_name%id%class -->'
    let g:endtagcommentFormat = '<!-- /%tag_name%id%class -->'
    nnoremap ,t :<C-u>call Endtagcomment()<CR>
  7. @hokaccha hokaccha revised this gist May 25, 2010. 1 changed file with 2 additions and 10 deletions.
    12 changes: 2 additions & 10 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,13 @@
    " こういうHTMLがあったときに
    " <div id="hoge" class="fuga">
    " ...
    " </div>
    "
    " 実行するとこうなる
    " <div id="hoge" class="fuga">
    " ...
    " <!-- /div#hoge.fuga --></div>

    function! Endtagcomment()
    let reg_save = @@

    try
    silent normal vaty
    catch
    execute "normal \<Esc>"
    echohl ErrorMsg
    echo 'no match html tags'
    echohl None
    return
    endtry

  8. @hokaccha hokaccha revised this gist May 25, 2010. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -27,20 +27,27 @@ function! Endtagcomment()
    let id = ''
    let id_match = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')
    if exists('id_match[1]')
    let id = '#' . id_m[1]
    let id = '#' . id_match[1]
    endif

    let class = ''
    let class_match = matchlist(start_tag, '\vclass\=["'']([^"'']+)["'']')
    if exists('class_match[1]')
    let class = '.' . join(split(class_m[1], '\v\s+'), '.')
    let class = '.' . join(split(class_match[1], '\v\s+'), '.')
    endif

    execute "normal `]va<\<Esc>`<"

    let @@ = printf('<!-- /%s%s%s -->', tag_name, id, class)
    let comment = g:endtagcommentFormat
    let comment = substitute(comment, '%tag_name', tag_name, 'g')
    let comment = substitute(comment, '%id', id, 'g')
    let comment = substitute(comment, '%class', class, 'g')
    let @@ = comment

    normal ""P

    let @@ = reg_save
    endfunction

    let g:endtagcommentFormat = '<!-- %tag_name%id%class -->'
    nnoremap ,t :<C-u>call Endtagcomment()<CR>
  9. @hokaccha hokaccha revised this gist May 25, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    " <div id="hoge" class="fuga">
    " ...
    " <!-- /div#hoge.fuga --></div>

    function! Endtagcomment()
    let reg_save = @@

  10. @hokaccha hokaccha revised this gist May 25, 2010. 1 changed file with 30 additions and 14 deletions.
    44 changes: 30 additions & 14 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,45 @@
    function! g:endtagcomment()
    let tmp = @@
    let tmp_pos = getpos('.')
    silent normal vaty
    " こういうHTMLがあったときに
    " <div id="hoge" class="fuga">
    " ...
    " </div>
    "
    " 実行するとこうなる
    " <div id="hoge" class="fuga">
    " ...
    " <!-- /div#hoge.fuga --></div>
    function! Endtagcomment()
    let reg_save = @@

    try
    silent normal vaty
    catch
    execute "normal \<Esc>"
    echo 'no match html tags'
    return
    endtry

    let html = @@

    let start_tag = matchstr(html, '\v(\<.{-}\>)')
    let tag_name = matchstr(start_tag, '\v([a-zA-Z]+)')
    let id_m = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')

    let id = ''
    if exists('id_m[1]')
    let id_match = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')
    if exists('id_match[1]')
    let id = '#' . id_m[1]
    endif
    let class_m = matchlist(start_tag, '\vclass\=["'']([^"'']+)["'']')

    let class = ''
    if exists('class_m[1]')
    let class_match = matchlist(start_tag, '\vclass\=["'']([^"'']+)["'']')
    if exists('class_match[1]')
    let class = '.' . join(split(class_m[1], '\v\s+'), '.')
    endif

    call setpos('.', tmp_pos)
    execute "normal vat\<ESC>"
    normal va<y
    execute "normal `]va<\<Esc>`<"

    let @@ = printf('<!-- /%s%s%s -->', tag_name, id, class)
    normal ""P

    let @@ = tmp
    let @@ = reg_save
    endfunction

    nnoremap ,t :<C-u>call g:endtagcomment()<CR>
    nnoremap ,t :<C-u>call Endtagcomment()<CR>
  11. @hokaccha hokaccha revised this gist May 24, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,8 @@ function! g:endtagcomment()
    endif

    call setpos('.', tmp_pos)
    execute "normal vat\<ESC>"
    normal va<y
    let @@ = printf('<!-- /%s%s%s -->', tag_name, id, class)
    normal ""P

  12. @hokaccha hokaccha revised this gist May 24, 2010. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    function! g:endtagcomment()
    let tmp = @@
    let tmp_pos = getpos('.')
    silent normal vat"yy
    silent normal vaty
    let html = @@

    let start_tag = matchstr(html, '\v(\<.{-}\>)')
    let tag_name = matchstr(start_tag, '\v([a-zA-Z]+)')
    let id_m = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')
    @@ -18,8 +18,8 @@ function! g:endtagcomment()
    endif

    call setpos('.', tmp_pos)
    let @y = printf('<!-- /%s%s%s -->', tag_name, id, class)
    normal "yP
    let @@ = printf('<!-- /%s%s%s -->', tag_name, id, class)
    normal ""P

    let @@ = tmp
    endfunction
  13. @hokaccha hokaccha revised this gist May 24, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,8 @@ function! g:endtagcomment()
    endif

    call setpos('.', tmp_pos)
    let @@ = printf('<!-- /%s%s%s -->', tag_name, id, class)
    normal P
    let @y = printf('<!-- /%s%s%s -->', tag_name, id, class)
    normal "yP

    let @@ = tmp
    endfunction
  14. @hokaccha hokaccha revised this gist May 24, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ function! g:endtagcomment()
    endif

    call setpos('.', tmp_pos)
    let @@ = printf('<!-- %s%s%s -->', tag_name, id, class)
    let @@ = printf('<!-- /%s%s%s -->', tag_name, id, class)
    normal P

    let @@ = tmp
  15. @hokaccha hokaccha created this gist May 24, 2010.
    27 changes: 27 additions & 0 deletions endtagcomment.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    function! g:endtagcomment()
    let tmp = @@
    let tmp_pos = getpos('.')
    silent normal vat"yy
    let html = @@

    let start_tag = matchstr(html, '\v(\<.{-}\>)')
    let tag_name = matchstr(start_tag, '\v([a-zA-Z]+)')
    let id_m = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')
    let id = ''
    if exists('id_m[1]')
    let id = '#' . id_m[1]
    endif
    let class_m = matchlist(start_tag, '\vclass\=["'']([^"'']+)["'']')
    let class = ''
    if exists('class_m[1]')
    let class = '.' . join(split(class_m[1], '\v\s+'), '.')
    endif

    call setpos('.', tmp_pos)
    let @@ = printf('<!-- %s%s%s -->', tag_name, id, class)
    normal P

    let @@ = tmp
    endfunction

    nnoremap ,t :<C-u>call g:endtagcomment()<CR>