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.

102 lines
2.4 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
  1. " makecols.vim - Makecolumns
  2. " Author: Levi Olson <http://leviolson.com/>
  3. " Version: 1.0
  4. function! s:beep()
  5. exe "norm! \<Esc>"
  6. return ""
  7. endfunction
  8. function! s:get_visual_selection()
  9. let [lnum1, col1] = getpos("'<")[1:2]
  10. let [lnum2, col2] = getpos("'>")[1:2]
  11. let lines = getline(lnum1, lnum2)
  12. " let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  13. " let lines[0] = lines[0][col1 - 1:]
  14. execute lnum1 . "," . lnum2 . "delete"
  15. let selection = join(lines, ",")
  16. return selection
  17. endfunction
  18. function! s:convert_selection(selection)
  19. " Remove the current selection
  20. " Setup some variables
  21. let c = 0
  22. let selection = a:selection
  23. let old_selection = split(selection, ",")
  24. let no_of_cols = 6
  25. let new_string = ""
  26. let @z = ""
  27. " Let's print out some info
  28. echom "Old Selection: "
  29. echom join(old_selection, ", ")
  30. " For Loopage Goes here
  31. for i in old_selection
  32. if (c == 0)
  33. " If first selected line
  34. let new_string = join([new_string, i], "")
  35. else
  36. if (c % no_of_cols)
  37. " If regular column
  38. let new_string = join([new_string, i], "\t")
  39. else
  40. " If end of row
  41. let new_string = join([new_string, i], "\n")
  42. endif
  43. endif
  44. let c += 1
  45. endfor
  46. let @z = join([new_string, ""], "\n")
  47. return @z
  48. endfunction
  49. function! s:replace_selected_text()
  50. execute "normal! \"zP"
  51. echo "Just tried to replace the selection."
  52. return ""
  53. endfunction
  54. function! s:makecols(options) range
  55. let options = a:options
  56. echom "Orientation: " . g:orientation
  57. let cols = options[1]
  58. echom "Number of Columns: " . g:cols
  59. let mode = visualmode()
  60. if (mode !=# "V")
  61. echo "You must be in linewise visual mode"
  62. return s:beep()
  63. else
  64. echo "You are in the right mode"
  65. endif
  66. let selection = s:get_visual_selection()
  67. echo selection
  68. let converted_text = s:convert_selection(selection)
  69. return s:replace_selected_text()
  70. endfunction
  71. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  72. if exists(g:makecols_orientation) || ! g:makecols_orientation)
  73. let g:makecols_orientation = "horz"
  74. endif
  75. if exists(g:makecols_cols) || ! g:makecols_cols)
  76. let g:makecols_cols = "5"
  77. endif
  78. " vim:set ft=vim sw=4 sts=4 et: