nerdcommenter.vim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. if exists('loaded_nerd_comments')
  2. finish
  3. endif
  4. if v:version < 700
  5. echoerr "NERDCommenter: this plugin requires vim >= 7. DOWNLOAD IT! You'll thank me later!"
  6. finish
  7. endif
  8. let loaded_nerd_comments = 1
  9. " Function: s:InitVariable() function
  10. " This function is used to initialise a given variable to a given value. The
  11. " variable is only initialised if it does not exist prior
  12. "
  13. " Args:
  14. " -var: the name of the var to be initialised
  15. " -value: the value to initialise var to
  16. "
  17. " Returns:
  18. " 0
  19. function s:InitVariable(var, value)
  20. if !exists(a:var)
  21. execute 'let ' . a:var . ' = ' . string(a:value)
  22. endif
  23. endfunction
  24. " Section: variable initialization
  25. call s:InitVariable('g:NERDAllowAnyVisualDelims', 1)
  26. call s:InitVariable('g:NERDBlockComIgnoreEmpty', 0)
  27. call s:InitVariable('g:NERDCommentWholeLinesInVMode', 0)
  28. call s:InitVariable('g:NERDCommentEmptyLines', 0)
  29. call s:InitVariable('g:NERDCompactSexyComs', 0)
  30. call s:InitVariable('g:NERDCreateDefaultMappings', 1)
  31. call s:InitVariable('g:NERDDefaultNesting', 1)
  32. call s:InitVariable('g:NERDMenuMode', 3)
  33. call s:InitVariable('g:NERDLPlace', '[>')
  34. call s:InitVariable('g:NERDUsePlaceHolders', 1)
  35. call s:InitVariable('g:NERDRemoveAltComs', 1)
  36. call s:InitVariable('g:NERDRemoveExtraSpaces', 0)
  37. call s:InitVariable('g:NERDRPlace', '<]')
  38. call s:InitVariable('g:NERDSpaceDelims', 0)
  39. call s:InitVariable('g:NERDDefaultAlign', 'none')
  40. call s:InitVariable('g:NERDTrimTrailingWhitespace', 0)
  41. call s:InitVariable('g:NERDToggleCheckAllLines', 0)
  42. call s:InitVariable('g:NERDDisableTabsInBlockComm', 0)
  43. call s:InitVariable('g:NERDSuppressWarnings', 0)
  44. " Section: Comment mapping and menu item setup
  45. " ===========================================================================
  46. " Create menu items for the specified modes. If a:combo is not empty, then
  47. " also define mappings and show a:combo in the menu items.
  48. function! s:CreateMaps(modes, target, desc, combo)
  49. " Build up a map command like
  50. " 'noremap <silent> <Plug>NERDCommenterComment :call nerdcommenter#Comment("n", "Comment")'
  51. let plug = '<Plug>NERDCommenter' . a:target
  52. let plug_start = 'noremap <silent> ' . plug . ' :call nerdcommenter#Comment("'
  53. let plug_end = '", "' . a:target . '")<CR>'
  54. " Build up a menu command like
  55. " 'menu <silent> comment.Comment<Tab>\\cc <Plug>NERDCommenterComment'
  56. let menuRoot = get(['', 'comment', '&comment', '&Plugin.&comment', '&Plugin.Nerd\ &Commenter'],
  57. \ g:NERDMenuMode, '')
  58. let menu_command = 'menu <silent> ' . menuRoot . '.' . escape(a:desc, ' ')
  59. if strlen(a:combo)
  60. let leader = exists('g:mapleader') ? g:mapleader : '\'
  61. let menu_command .= '<Tab>' . escape(leader, '\') . a:combo
  62. endif
  63. let menu_command .= ' ' . (strlen(a:combo) ? plug : a:target)
  64. " Execute the commands built above for each requested mode.
  65. for mode in (a:modes ==# '') ? [''] : split(a:modes, '\zs')
  66. if strlen(a:combo)
  67. execute mode . plug_start . mode . plug_end
  68. if g:NERDCreateDefaultMappings && !hasmapto(plug, mode)
  69. execute mode . 'map <leader>' . a:combo . ' ' . plug
  70. endif
  71. endif
  72. " Check if the user wants the menu to be displayed.
  73. if g:NERDMenuMode !=# 0
  74. execute mode . menu_command
  75. endif
  76. endfor
  77. endfunction
  78. call s:CreateMaps('nx', 'Comment', 'Comment', 'cc')
  79. call s:CreateMaps('nx', 'Toggle', 'Toggle', 'c<Space>')
  80. call s:CreateMaps('nx', 'Minimal', 'Minimal', 'cm')
  81. call s:CreateMaps('nx', 'Nested', 'Nested', 'cn')
  82. call s:CreateMaps('n', 'ToEOL', 'To EOL', 'c$')
  83. call s:CreateMaps('nx', 'Invert', 'Invert', 'ci')
  84. call s:CreateMaps('nx', 'Sexy', 'Sexy', 'cs')
  85. call s:CreateMaps('nx', 'Yank', 'Yank then comment', 'cy')
  86. call s:CreateMaps('n', 'Append', 'Append', 'cA')
  87. call s:CreateMaps('', ':', '-Sep-', '')
  88. call s:CreateMaps('nx', 'AlignLeft', 'Left aligned', 'cl')
  89. call s:CreateMaps('nx', 'AlignBoth', 'Left and right aligned', 'cb')
  90. call s:CreateMaps('', ':', '-Sep2-', '')
  91. call s:CreateMaps('nx', 'Uncomment', 'Uncomment', 'cu')
  92. call s:CreateMaps('n', 'AltDelims', 'Switch Delimiters', 'ca')
  93. call s:CreateMaps('i', 'Insert', 'Insert Comment Here', '')
  94. call s:CreateMaps('', ':', '-Sep3-', '')
  95. call s:CreateMaps('', ':help NERDCommenterContents<CR>', 'Help', '')
  96. " Shim functions so old code gets passed through to the autoload functions
  97. function! NERDComment(mode, type) range
  98. if !g:NERDSuppressWarnings
  99. echom 'Function NERDComment() has been deprecated, please use nerdcommenter#Comment() instead'
  100. endif
  101. if a:firstline != a:lastline
  102. echoerr "Sorry! We can't pass a range through this deprecation shim, please update your code."
  103. return v:false
  104. endif
  105. return nerdcommenter#Comment(a:mode, a:type)
  106. endfunction
  107. function! NERDCommentIsLineCommented(lineNo)
  108. if !g:NERDSuppressWarnings
  109. echom 'Function NERDCommentIsLineCommented() has been deprecated, please use nerdcommenter#IsLineCommented() instead'
  110. endif
  111. return nerdcommenter#IsLineCommented(a:lineNo)
  112. endfunction
  113. function! NERDCommentIsCharCommented(line, col)
  114. if !g:NERDSuppressWarnings
  115. echom 'Function NERDCommentIsCharCommented() has been deprecated, please use nerdcommenter#IsCharCommented() instead'
  116. endif
  117. return nerdcommenter#IsCharCommented(a:line, a:col)
  118. endfunction
  119. inoremap <silent> <Plug>NERDCommenterInsert <C-\><C-O>:call nerdcommenter#Comment('i', "Insert")<CR>
  120. " switch to/from alternative delimiters (does not use wrapper function)
  121. nnoremap <Plug>NERDCommenterAltDelims :call nerdcommenter#SwitchToAlternativeDelimiters(1)<CR>