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.

115 lines
2.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
  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. execute lnum1 . "," . lnum2 . "delete"
  21. let selection = join(lines, ",")
  22. return selection
  23. endfunction
  24. function! s:convert_selection(selection)
  25. " Remove the current selection
  26. " Setup some variables
  27. let c = 0
  28. let selection = a:selection
  29. let old_selection = split(selection, ",")
  30. let new_string = ""
  31. let @z = ""
  32. " Let's print out some info
  33. echom "Old Selection: "
  34. echom join(old_selection, ", ")
  35. " For Loopage Goes here
  36. for i in old_selection
  37. if (c == 0)
  38. " If first selected line
  39. let new_string = join([new_string, i], "")
  40. else
  41. if (c % g:makecols_cols)
  42. " If regular column
  43. let new_string = join([new_string, i], "\t")
  44. else
  45. " If end of row
  46. let new_string = join([new_string, i], "\n")
  47. endif
  48. endif
  49. let c += 1
  50. endfor
  51. let @z = join([new_string, ""], "\n")
  52. return @z
  53. endfunction
  54. function! s:replace_selected_text()
  55. execute "normal! \"zP"
  56. echo "Just tried to replace the selection."
  57. return ""
  58. endfunction
  59. function! s:makecols(orient, cols) range
  60. let default_orientation = g:makecols_orientation
  61. let default_cols = g:makecols_cols
  62. let g:makecols_orientation = a:orient
  63. if (v:count > 0)
  64. let g:makecols_cols = v:count
  65. else
  66. let g:makecols_cols = a:cols
  67. endif
  68. echom "Orientation: " . g:makecols_orientation
  69. echom "Number of Columns: " . g:makecols_cols
  70. let mode = visualmode()
  71. if (mode !=# "V")
  72. echo "You must be in linewise visual mode"
  73. return s:beep()
  74. else
  75. echo "You are in the right mode"
  76. endif
  77. let selection = s:get_visual_selection()
  78. echo selection
  79. let converted_text = s:convert_selection(selection)
  80. let g:makecols_orientation = default_orientation
  81. let g:makecols_cols = default_cols
  82. return s:replace_selected_text()
  83. endfunction
  84. vnoremap <silent> mc :<C-U>call <SID>makecols(g:makecols_orientation, g:makecols_cols)<CR>
  85. vnoremap <silent> mch :<C-U>call <SID>makecols("horz", g:makecols_cols)<CR>
  86. vnoremap <silent> mcv :<C-U>call <SID>makecols("vert", g:makecols_cols)<CR>
  87. " vim:set ft=vim sw=4 sts=4 et: