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