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.

169 lines
4.3 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
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. " Remove the current selection
  21. execute lnum1 . "," . lnum2 . "delete"
  22. let selection = join(lines, ",")
  23. return selection
  24. endfunction
  25. function! s:convert_selection()
  26. let new_string = ""
  27. if g:makecols_orientation == "horz"
  28. let new_string = s:convert_selection_horz()
  29. else
  30. let new_string = s:convert_selection_vert()
  31. endif
  32. return new_string
  33. endfunction
  34. function! s:convert_selection_horz()
  35. " Setup some variables
  36. let c = 0
  37. let new_string = ""
  38. let selection = s:get_visual_selection()
  39. let old_selection = split(selection, ",")
  40. let @z = ""
  41. " For Loopage Goes here
  42. for i in old_selection
  43. if (c == 0)
  44. " If first selected line
  45. let new_string = join([new_string, i], "")
  46. else
  47. if (c % g:makecols_cols)
  48. " If regular column
  49. let new_string = join([new_string, i], "\t")
  50. else
  51. " If end of row
  52. let new_string = join([new_string, i], "\n")
  53. endif
  54. endif
  55. let c += 1
  56. endfor
  57. return join([new_string, ""], "\n")
  58. endfunction
  59. function! s:convert_selection_vert()
  60. " Setup some variables
  61. let cols = g:makecols_cols
  62. let selection = s:get_visual_selection()
  63. let old_selection = split(selection, ",")
  64. let lines = len(old_selection) * 1.0
  65. let rows = (lines / g:makecols_cols) * 1.0
  66. let rows = float2nr(ceil(rows))
  67. let @z = ""
  68. " 12 item example below
  69. " 0 4 8
  70. " 1 5 9
  71. " 2 6 10
  72. " 3 7 11
  73. let new_string = ""
  74. let row = 1
  75. let pos = 0
  76. while row < rows
  77. let col = 1
  78. echom "ROW: " . row . " COL: " . col . " POS: " . pos
  79. if (row == 0)
  80. let content = get(old_selection, 0, "blank")
  81. let new_string = join([new_string, content], "")
  82. endif
  83. let pos = row
  84. while col < cols
  85. let col += 1
  86. let pos = pos + rows
  87. echom "ROW: " . row . " COL: " . col . " POS: " . pos
  88. let content = get(old_selection, pos, "blank")
  89. if (col == 0)
  90. let new_string = join([new_string, content], "")
  91. else
  92. let new_string = join([new_string, content], "\t")
  93. endif
  94. let pos += 1
  95. endwhile
  96. let new_string = join([new_string,""], "\n")
  97. let row += 1
  98. endwhile
  99. return join([new_string, ""], "\n")
  100. endfunction
  101. function! s:replace_selected_text(converted_text)
  102. let @z = a:converted_text
  103. execute "normal! \"zP"
  104. " echom "Just replaced the selection."
  105. let @z = ""
  106. return ""
  107. endfunction
  108. function! s:makecols(orient, cols) range
  109. " backup the globals
  110. let default_orientation = g:makecols_orientation
  111. let default_cols = g:makecols_cols
  112. let g:makecols_orientation = a:orient
  113. if (v:count > 0)
  114. let g:makecols_cols = v:count
  115. else
  116. let g:makecols_cols = a:cols
  117. endif
  118. echom "Orient: " . g:makecols_orientation . ", No Cols: " . g:makecols_cols
  119. let mode = visualmode()
  120. if (mode !=# "V")
  121. echo "You must be in linewise visual mode"
  122. return s:beep()
  123. endif
  124. let converted_text = s:convert_selection()
  125. let g:makecols_orientation = default_orientation
  126. let g:makecols_cols = default_cols
  127. return s:replace_selected_text(converted_text)
  128. endfunction
  129. vnoremap <silent> mc :<C-U>call <SID>makecols(g:makecols_orientation, g:makecols_cols)<CR>
  130. vnoremap <silent> mch :<C-U>call <SID>makecols("horz", g:makecols_cols)<CR>
  131. vnoremap <silent> mcv :<C-U>call <SID>makecols("vert", g:makecols_cols)<CR>
  132. " vim:set ft=vim sw=4 sts=4 et: