Advertisement
Guest User

Untitled

a guest
Mar 6th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 4.79 KB | None | 0 0
  1. " File:   increment.vim
  2. " Author: Srinath Avadhanula  fvw fixed
  3. " Email:  srinath@eecs.berkeley.edu
  4. "
  5. " This script provides a way to quickly create incremented lists (either
  6. " increasing or decreasing) using the visual block mode.
  7. "
  8. " Synopsis:
  9. " =========
  10. " 1. Suppose you have a column of the following form:
  11. "     array(1) = 3;
  12. "     array(1) = 3;
  13. "     array(1) = 3;
  14. "     array(1) = 3;
  15. "     array(1) = 3;
  16. " 2. Choose the column of '1's using Ctrl-V and then run the command Inc (you
  17. "    should see "'<,'>Inc" on the command line at this point)(if you do not
  18. "    have any other user commands starting with 'I', just I will suffice).
  19. "    This will tranform the text as:
  20. "     array(1) = 3;       array(1) = 3;
  21. "     array(1) = 3;       array(2) = 3;
  22. "     array(1) = 3;  -->  array(3) = 3;
  23. "     array(1) = 3;       array(4) = 3;
  24. "     array(1) = 3;       array(5) = 3;
  25. " 3. You can then choose the column of '3's (again using Ctrl-V) and run the
  26. "    command "Inc 3" to generate another incremented list. This will
  27. "    generate:
  28. "     array(1) = 3;        array(1) =  3;
  29. "     array(2) = 3;        array(2) =  6;
  30. "     array(3) = 3;  -->   array(3) =  9;
  31. "     array(4) = 3;        array(4) = 12;
  32. "     array(5) = 3;        array(5) = 15;
  33. "  
  34. "   Note: increment.vim automatically pads the numbers in the the column
  35. "   with spaces in order to get them right aligned. This is useful in most
  36. "   cases, but for cases when this might be bad, use IncN which doesnt do
  37. "   any alignment.
  38. "
  39. " Commands:
  40. " =========
  41. " 1. Inc : generates a column of increasing numbers with RIGHT  alignment.
  42. " 2. IncN: generates a column of increasing numbers with NO     alignment.
  43. " 3. IncL: generates a column of increasing numbers with LEFT   alignment
  44. "
  45. " Tip:
  46. " A mapping which goes well with this command is the following:
  47. "
  48. " vnoremap <c-a> :Inc<CR>
  49. "
  50. " With this mapping, select a column of numbers and press Ctrl-A, which will
  51. " get them in increasing order. I use <c-a> because its similar to the <c-a>
  52. " command in normal mode which increments the number under the cursor.
  53. "
  54. " Last Modification: Dec 17 2001 00:59:09
  55.  
  56.  
  57. "===========================================================================
  58. "2008-11-16 08:43:11 fvw
  59. com! -ra -nargs=* Inc :call IncrementColumn(1,<f-args>)
  60. com! -ra -nargs=* IncL :call IncrementColumn(2,<f-args>)
  61. com! -ra -nargs=* IncN :call IncrementColumn(0,<f-args>)
  62.  
  63. "===========================================================================
  64. function! StrRepeat(str, count)
  65.     let i = 1
  66.     let retStr = ""
  67.     while i <= a:count
  68.         let retStr = retStr.a:str
  69.         let i = i + 1
  70.     endwhile
  71.     return retStr
  72. endfunction
  73.  
  74. " first argument is either 0 or 1 or 2 depending on whether padding with
  75. " spaces is desired (pad = 1,2) or not. the second argument contains the
  76. " counter increment. its optional. if not specified, its assumed to be 1.
  77. function! IncrementColumn(pad, ...)
  78.     "2008-11-16 08:43:11 fvw
  79.     let setNum = 0
  80.     if a:0 == 0
  81.         let incr = 1
  82.     elseif a:0 == 1
  83.         let incr = a:1
  84.     elseif a:0 == 2
  85.         let presNum = a:1
  86.         let incr = a:2
  87.         let setNum = 1
  88.     else
  89.         return
  90.     end
  91.  
  92.     let c1 = col("'<")
  93.     let c2 = col("'>")
  94.     let c1v = virtcol("'<")
  95.     let c2v = virtcol("'>")
  96.     let clen = c2v - c1v
  97.     if c1 > c2
  98.         let temp = c1
  99.         let c1 = c2
  100.         let c2 = temp
  101.     end
  102.  
  103.     let r1 = line("'<")
  104.     let r2 = line("'>")
  105.     if r1 > r2
  106.         let temp = r1
  107.         let r1 = r2
  108.         let r2 = temp
  109.     end
  110.  
  111.     exe r1
  112.  
  113.     "exe "let presNum = ".strpart(getline('.'), c1-1, clen+1)
  114.    
  115.     if setNum == 0
  116.         let presNum = strpart(getline('.'), c1-1, clen+1)
  117.     endif
  118.  
  119.     let lastnum = presNum + incr*(r2-r1)
  120.  
  121.     " a simple way to find the number of digits in a number (including decimal
  122.     " points, - signs etc).
  123.     let maxstrlen = strlen("".lastnum)
  124.  
  125.     let r = r1
  126.     exe 'normal! '.c1v.'|'
  127.     while (r <= r2)
  128.         let cnow = col(".")
  129.         let linebef = strpart(getline('.'), 0, cnow-1)
  130.         let lineaft = strpart(getline('.'), cnow+clen, 1000)
  131.  
  132.         " find the number of padding spaces required for left/rigth alignment
  133.         if a:pad
  134.             let preslen = strlen("".presNum)
  135.             let padspace = StrRepeat(" ", maxstrlen - preslen)
  136.         else
  137.             let padspace = ""
  138.         end
  139.  
  140.         " the final line is made up of
  141.         " 1. the part of the line before the number
  142.         " 2. the padding spaces.
  143.         " 3. the present number
  144.         " 4. the part of the line after the number
  145.         " the padding spaces are either before or after the current number
  146.         " depending on whether the pad argument is 1 or 2 (respectively).
  147.         if a:pad == 1
  148.             let lineset = linebef.padspace.presNum.lineaft
  149.         elseif a:pad == 2
  150.             let lineset = linebef.presNum.padspace.lineaft
  151.         else
  152.             let lineset = linebef.presNum.lineaft
  153.         end
  154.  
  155.         call setline('.', lineset)
  156.         let presNum = presNum + incr
  157.         normal! j
  158.         let r = r + 1
  159.     endwhile
  160.     normal! `<
  161. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement