Advertisement
Guest User

Untitled

a guest
Jul 1st, 2012
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. " endtagcomment.vim
  2. " こういうHTMLがあったときに
  3. " <div id="hoge" class="fuga">
  4. " ...
  5. " </div>
  6. "
  7. " 実行するとこうなる
  8. " <div id="hoge" class="fuga">
  9. " ...
  10. " <!-- /div#hoge.fuga --></div>
  11. "
  12. " ----------
  13. " update:2010-12-08 @kosei27
  14. "
  15. " ,ti でidのみを出力
  16. " ,tc でclassのみを出力(",tt" ",t" も同様)
  17. " ,ta でidとclassを出力
  18. " ,t<Space> でコメント内のテキストの前後の空白をトグル
  19. " <!--/hoge--> <-> <!-- /hoge -->
  20.  
  21. function! Endtagcomment(type)
  22. let reg_save = @@
  23.  
  24. try
  25. silent normal vaty
  26. catch
  27. execute "normal \<Esc>"
  28. echohl ErrorMsg
  29. echo 'no match html tags'
  30. echohl None
  31. return
  32. endtry
  33.  
  34. let html = @@
  35.  
  36. let start_tag = matchstr(html, '\v(\<.{-}\>)')
  37. let tag_name = matchstr(start_tag, '\v([a-zA-Z]+)')
  38.  
  39. let comment_type = a:type
  40. let comment_space = g:endtagcomment_space
  41. let firstclass_symbol = g:endtagcomment_firstclass_symbol
  42.  
  43. let id = ''
  44. let id_match = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')
  45. if exists('id_match[1]')
  46. let id = '#' . id_match[1]
  47. endif
  48.  
  49. let class = ''
  50. let class_match = matchlist(start_tag, '\vclass\=["'']([^"'']+)["'']')
  51. if exists('class_match[1]')
  52. "let class = '.' . join(split(class_match[1], '\v\s+'), '.')
  53. let class = join(split(class_match[1], '\v\s+'), '.')
  54. endif
  55.  
  56. execute "normal `>va<\<Esc>`<"
  57.  
  58. if l:comment_type == 'id'
  59. let g:endtagcommentFormat = '<!--%comment_space/%id%comment_space-->'
  60. elseif l:comment_type == 'class'
  61. let g:endtagcommentFormat = '<!--%comment_space/%firstclass_symbol%class%comment_space-->'
  62. elseif l:comment_type == 'id_class'
  63. let g:endtagcommentFormat = '<!--%comment_space/%id.%class%comment_space-->'
  64. elseif l:comment_type == 'tag_id_class'
  65. let g:endtagcommentFormat = '<!--%comment_space/%tag_name%id.%class%comment_space-->'
  66. endif
  67.  
  68. let comment = g:endtagcommentFormat
  69. let comment = substitute(comment, '%comment_space', comment_space, 'g')
  70. let comment = substitute(comment, '%firstclass_symbol', firstclass_symbol, 'g')
  71. let comment = substitute(comment, '%tag_name', tag_name, 'g')
  72. let comment = substitute(comment, '%id', id, 'g')
  73. let comment = substitute(comment, '%class', class, 'g')
  74.  
  75. let @@ = comment
  76.  
  77. normal ""P
  78.  
  79. let @@ = reg_save
  80. endfunction
  81.  
  82. "let g:endtagcommentFormat = '<!-- /%tag_name%id%class -->'
  83.  
  84. "" keymap
  85. nnoremap ,ti :<C-u>call Endtagcomment('id')<CR>
  86. nnoremap ,tc :<C-u>call Endtagcomment('class')<CR>
  87. nnoremap ,ta :<C-u>call Endtagcomment('id_class')<CR>
  88. nnoremap ,tg :<C-u>call Endtagcomment('tag_id_class')<CR>
  89.  
  90. nmap ,tt :<C-u>normal ,tc<CR>
  91.  
  92.  
  93. " コメント内のテキストの前後の空白
  94. let g:endtagcomment_space = ''
  95. "" 有無をトグル
  96. function! Endtagcomment_space_toggle()
  97. let comment_space = g:endtagcomment_space
  98. if l:comment_space == ''
  99. let g:endtagcomment_space = ' '
  100. else
  101. let g:endtagcomment_space = ''
  102. endif
  103. endfunction
  104. """ keymap
  105. nnoremap <silent> ,t<Space> :<C-u>call Endtagcomment_space_toggle()<CR>
  106.  
  107.  
  108. " コメント内のテキストの最初のclass名の「.」
  109. let g:endtagcomment_firstclass_symbol = ''
  110. "" 有無をトグル(id+classの場合は固定で「.」がつきます)
  111. function! Endtagcomment_firstclass_symbol_toggle()
  112. let firstclass_symbol = g:endtagcomment_firstclass_symbol
  113. if l:firstclass_symbol == ''
  114. let g:endtagcomment_firstclass_symbol = '.'
  115. else
  116. let g:endtagcomment_firstclass_symbol = ''
  117. endif
  118. endfunction
  119. "" keymap
  120. nnoremap <silent> ,t. :<C-u>call Endtagcomment_firstclass_symbol_toggle()<CR>
Advertisement
RAW Paste Data Copied
Advertisement