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.

103 lines
2.5 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. 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(orient) range
  55. let g:makecols_orientation = a:orient
  56. echom "Orientation: " . g:makecols_orientation
  57. echom "Number of Columns: " . v:count ? g:makecols_cols : 5
  58. let mode = visualmode()
  59. if (mode !=# "V")
  60. echo "You must be in linewise visual mode"
  61. return s:beep()
  62. else
  63. echo "You are in the right mode"
  64. endif
  65. let selection = s:get_visual_selection()
  66. echo selection
  67. let converted_text = s:convert_selection(selection)
  68. return s:replace_selected_text()
  69. endfunction
  70. vnoremap <silent> mc :<C-U>call <SID>makecols(g:makecols_orientation)<CR>
  71. vnoremap <silent> mch :<C-U>call <SID>makecols("horz")<CR>
  72. vnoremap <silent> mcv :<C-U>call <SID>makecols("vert")<CR>
  73. if !exists("g:makecols_orientation") || ! g:makecols_orientation
  74. let g:makecols_orientation = "horz"
  75. endif
  76. if !exists("g:makecols_cols") || ! g:makecols_cols
  77. let g:makecols_cols = "5"
  78. endif
  79. " vim:set ft=vim sw=4 sts=4 et: