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.

148 lines
3.6 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
  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. let @z = new_string
  34. return @z
  35. endfunction
  36. function! s:convert_selection_horz()
  37. " Setup some variables
  38. let c = 0
  39. let new_string = ""
  40. let selection = s:get_visual_selection()
  41. let old_selection = split(selection, ",")
  42. let @z = ""
  43. " For Loopage Goes here
  44. for i in old_selection
  45. if (c == 0)
  46. " If first selected line
  47. let new_string = join([new_string, i], "")
  48. else
  49. if (c % g:makecols_cols)
  50. " If regular column
  51. let new_string = join([new_string, i], "\t")
  52. else
  53. " If end of row
  54. let new_string = join([new_string, i], "\n")
  55. endif
  56. endif
  57. let c += 1
  58. endfor
  59. return join([new_string, ""], "\n")
  60. endfunction
  61. function! s:convert_selection_vert()
  62. " Setup some variables
  63. let c = 0
  64. let new_string = ""
  65. let selection = s:get_visual_selection()
  66. let old_selection = split(selection, ",")
  67. let @z = ""
  68. " For Loopage Goes here
  69. for i in old_selection
  70. if (c == 0)
  71. " If first selected line
  72. let new_string = join([new_string, i], "")
  73. else
  74. if (c % g:makecols_cols)
  75. " If regular column
  76. let new_string = join([new_string, i], "\t")
  77. else
  78. " If end of row
  79. let new_string = join([new_string, i], "\n")
  80. endif
  81. endif
  82. let c += 1
  83. endfor
  84. endfunction
  85. function! s:replace_selected_text()
  86. execute "normal! \"zP"
  87. echo "Just tried to replace the selection."
  88. return ""
  89. endfunction
  90. function! s:makecols(orient, cols) range
  91. let default_orientation = g:makecols_orientation
  92. let default_cols = g:makecols_cols
  93. let g:makecols_orientation = a:orient
  94. if (v:count > 0)
  95. let g:makecols_cols = v:count
  96. else
  97. let g:makecols_cols = a:cols
  98. endif
  99. echom "Orientation: " . g:makecols_orientation
  100. echom "Number of Columns: " . g:makecols_cols
  101. let mode = visualmode()
  102. if (mode !=# "V")
  103. echo "You must be in linewise visual mode"
  104. return s:beep()
  105. else
  106. echo "You are in the right mode"
  107. endif
  108. let converted_text = s:convert_selection()
  109. let g:makecols_orientation = default_orientation
  110. let g:makecols_cols = default_cols
  111. return s:replace_selected_text()
  112. endfunction
  113. vnoremap <silent> mc :<C-U>call <SID>makecols(g:makecols_orientation, g:makecols_cols)<CR>
  114. vnoremap <silent> mch :<C-U>call <SID>makecols("horz", g:makecols_cols)<CR>
  115. vnoremap <silent> mcv :<C-U>call <SID>makecols("vert", g:makecols_cols)<CR>
  116. " vim:set ft=vim sw=4 sts=4 et: