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.

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