makecols.vim: converting lists to columns made simple
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. " makecols.vim - Makecolumns
  2. " Author: Levi Olson <http://leviolson.com/>
  3. " Version: 1.0
  4. if !exists("g:makecols_orientation") || ! g:makecols_orientation
  5. let g:makecols_orientation = "horz"
  6. endif
  7. if !exists("g:makecols_cols") || ! g:makecols_cols
  8. let g:makecols_cols = "5"
  9. endif
  10. function! s:beep()
  11. exe "norm! \<Esc>"
  12. return ""
  13. endfunction
  14. function! s:get_visual_selection()
  15. let [lnum1, col1] = getpos("'<")[1:2]
  16. let [lnum2, col2] = getpos("'>")[1:2]
  17. let lines = getline(lnum1, lnum2)
  18. " let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  19. " let lines[0] = lines[0][col1 - 1:]
  20. "
  21. " Remove the current selection
  22. execute lnum1 . "," . lnum2 . "delete"
  23. let selection = join(lines, ",")
  24. return selection
  25. endfunction
  26. function! s:convert_selection()
  27. let new_string = ""
  28. if g:makecols_orientation == "horz"
  29. let new_string = s:convert_selection_horz()
  30. else
  31. let new_string = s:convert_selection_vert()
  32. endif
  33. return new_string
  34. endfunction
  35. function! s:convert_selection_horz()
  36. " Setup some variables
  37. let c = 0
  38. let new_string = ""
  39. let selection = s:get_visual_selection()
  40. let old_selection = split(selection, ",")
  41. let @z = ""
  42. " For Loopage Goes here
  43. for i in old_selection
  44. if (c == 0)
  45. " If first selected line
  46. let new_string = join([new_string, i], "")
  47. else
  48. if (c % g:makecols_cols)
  49. " If regular column
  50. let new_string = join([new_string, i], "\t")
  51. else
  52. " If end of row
  53. let new_string = join([new_string, i], "\n")
  54. endif
  55. endif
  56. let c += 1
  57. endfor
  58. return join([new_string, ""], "\n")
  59. endfunction
  60. function! s:convert_selection_vert()
  61. " Setup some variables
  62. let c = 0
  63. let new_string = ""
  64. let selection = s:get_visual_selection()
  65. let old_selection = split(selection, ",")
  66. let @z = ""
  67. " For Loopage Goes here
  68. for i in old_selection
  69. if (c == 0)
  70. " If first selected line
  71. let new_string = join([new_string, i], "")
  72. else
  73. if (c % g:makecols_cols)
  74. " If regular column
  75. let new_string = join([new_string, i], "\t")
  76. else
  77. " If end of row
  78. let new_string = join([new_string, i], "\n")
  79. endif
  80. endif
  81. let c += 1
  82. endfor
  83. return join([new_string, ""], "\n")
  84. endfunction
  85. function! s:replace_selected_text(converted_text)
  86. let @z = a:converted_text
  87. execute "normal! \"zP"
  88. echom "Just replaced the selection."
  89. let @z = ""
  90. return ""
  91. endfunction
  92. function! s:makecols(orient, cols) range
  93. " backup the globals
  94. let default_orientation = g:makecols_orientation
  95. let default_cols = g:makecols_cols
  96. let g:makecols_orientation = a:orient
  97. if (v:count > 0)
  98. let g:makecols_cols = v:count
  99. else
  100. let g:makecols_cols = a:cols
  101. endif
  102. echom "Orientation: " . g:makecols_orientation
  103. echom "Number of Columns: " . g:makecols_cols
  104. let mode = visualmode()
  105. if (mode !=# "V")
  106. echo "You must be in linewise visual mode"
  107. return s:beep()
  108. else
  109. echo "You are in the right mode"
  110. endif
  111. let converted_text = s:convert_selection()
  112. s:replace_selected_text(converted_text)
  113. let g:makecols_orientation = default_orientation
  114. let g:makecols_cols = default_cols
  115. return ""
  116. endfunction
  117. vnoremap <silent> mc :<C-U>call <SID>makecols(g:makecols_orientation, g:makecols_cols)<CR>
  118. vnoremap <silent> mch :<C-U>call <SID>makecols("horz", g:makecols_cols)<CR>
  119. vnoremap <silent> mcv :<C-U>call <SID>makecols("vert", g:makecols_cols)<CR>
  120. " vim:set ft=vim sw=4 sts=4 et: