|
| 1 | +function! InsertRandomText(low_range, high_range) " {{{ |
| 2 | + " Insert random ascii visible table text at cursor position. |
| 3 | + " Return the number of characters inserted. |
| 4 | + |
| 5 | +python3 << EOF |
| 6 | +import random, string, vim |
| 7 | +# Text has to large from a larger sample in order to avoid errors. |
| 8 | +text = random.sample( |
| 9 | + (10 * string.ascii_lowercase + string.digits + ' '), |
| 10 | + random.randint(int(vim.eval('a:low_range')), int(vim.eval('a:high_range')))) |
| 11 | +vim.current.buffer.vars['random_text'] = ''.join(text) |
| 12 | +EOF |
| 13 | +let l:textwidth = &tw |
| 14 | +set tw=0 |
| 15 | +execute "normal! i" . b:random_text |
| 16 | +let &tw = l:textwidth |
| 17 | + |
| 18 | +return len(b:random_text) |
| 19 | + |
| 20 | +endfunction " }}} |
| 21 | + |
| 22 | +function! DeleteChars(nchars) " {{{ |
| 23 | + " Delete n chars at cursor position. |
| 24 | + " It is the inverse of InsertRandomText(). |
| 25 | + |
| 26 | + let l:textwidth = &tw |
| 27 | + set tw=0 |
| 28 | + execute "normal! " . (repeat('h', a:nchars - 1)) |
| 29 | + execute "normal! " . repeat('x', a:nchars) |
| 30 | + let &tw = l:textwidth |
| 31 | + |
| 32 | +endfunction " }}} |
| 33 | + |
| 34 | +function! JumpToRandomPosition() " {{{ |
| 35 | +" Jump cursor to a random position in current buffer. |
| 36 | + |
| 37 | +python3 << EOF |
| 38 | +import random, vim |
| 39 | +cw = vim.current.window |
| 40 | +cb = vim.current.buffer |
| 41 | +rand_line = random.randint(1, len(cb) - 1) |
| 42 | +rand_line_len = len(cb[rand_line]) |
| 43 | +rand_col = random.randint(0, rand_line_len) if rand_line_len > 0 else 0 |
| 44 | +cw.cursor = (rand_line, rand_col) |
| 45 | +EOF |
| 46 | +endfunction " }}} |
| 47 | + |
| 48 | +function! DeleteRandomLines(low_range, high_range) " {{{ |
| 49 | +" Delete random lines between low_range and high_range. |
| 50 | +" Return the number of lines deleted. |
| 51 | + |
| 52 | +python3 << EOF |
| 53 | +import random, vim |
| 54 | +del_lines = random.randint( |
| 55 | + int(vim.eval('a:low_range')), int(vim.eval('a:high_range'))) |
| 56 | +vim.current.buffer.vars['del_lines'] = del_lines |
| 57 | +EOF |
| 58 | + |
| 59 | +execute "normal! " . b:del_lines . "dd" |
| 60 | + |
| 61 | +return b:del_lines |
| 62 | + |
| 63 | +endfunction "}}} |
| 64 | + |
| 65 | +function! InsertTextAtRandomPositions(ntimes) " {{{ |
| 66 | +" Insert text at random positions. May either insert in insert mode or in |
| 67 | +" normal mode. |
| 68 | + |
| 69 | + let l:total_lines = line('$') |
| 70 | + for i in range(a:ntimes) |
| 71 | + |
| 72 | +python3 << EOF |
| 73 | +import random, vim |
| 74 | +del_method = random.randint(0, 1) |
| 75 | +vim.current.buffer.vars['del_method'] = del_method |
| 76 | +EOF |
| 77 | + |
| 78 | + call JumpToRandomPosition() |
| 79 | + " b:del_method is set to either change the buffer via insert mode or |
| 80 | + " via normal mode. |
| 81 | + if b:del_method |
| 82 | + " This uses insert mode. |
| 83 | + let l:inserted_chars = InsertRandomText(3, 100) |
| 84 | + call DeleteChars(l:inserted_chars) |
| 85 | + else |
| 86 | + " This uses normal mode. |
| 87 | + let l:current_line = getpos('.')[1] |
| 88 | + let l:deleted_lines = DeleteRandomLines(1, 5) |
| 89 | + if l:current_line + l:deleted_lines <= l:total_lines |
| 90 | + execute "normal! P" |
| 91 | + else |
| 92 | + execute "normal! p" |
| 93 | + endif |
| 94 | + endif |
| 95 | + |
| 96 | + endfor |
| 97 | + |
| 98 | +endfunction " }}} |
0 commit comments