Skip to content

Commit ff8e46c

Browse files
committed
Initial
0 parents  commit ff8e46c

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vim-scroll-mode
2+
===============
3+
4+
TODO

Diff for: autoload/scrollmode/disable.vim

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function! scrollmode#disable#disable()
2+
let mappings = values(w:scroll_mode_actions) + values(w:scroll_mode_mappings)
3+
for lhs in uniq(sort(scrollmode#util#unnest(mappings)))
4+
exe "nunmap <buffer> <script> " . lhs
5+
endfor
6+
7+
echohl None
8+
echo ""
9+
10+
" Options
11+
exe "set scrolloff=" . w:scroll_mode_scrolloff
12+
if !w:scroll_mode_cul | setlocal nocul | endif
13+
if w:scroll_mode_cuc | setlocal cuc | endif
14+
15+
au! scroll_mode WinLeave,BufWinLeave,InsertEnter
16+
17+
unlet w:scroll_mode_enabled
18+
unlet w:scroll_mode_actions
19+
unlet w:scroll_mode_mappings
20+
unlet w:scroll_mode_scrolloff
21+
unlet w:scroll_mode_cul
22+
unlet w:scroll_mode_cuc
23+
endfunction

Diff for: autoload/scrollmode/enable.vim

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
let s:default_step = 5
2+
3+
let s:default_actions = {
4+
\ "up": ["k", "<Up>"],
5+
\ "down": ["j", "<Down>"],
6+
\ "pagedown": ["l"],
7+
\ "pageup": ["h"],
8+
\ "bottom": ["b"],
9+
\ "top": ["<Space>"],
10+
\ "exit": [";", "<Esc>"],
11+
\ "bdelete": ["-"]
12+
\ }
13+
14+
function! s:echo_mode()
15+
echo '-- SCROLL --'
16+
endfunction
17+
18+
function! <SID>on_motion()
19+
if (exists("w:scroll_mode_enabled"))
20+
call s:echo_mode()
21+
else
22+
echo ""
23+
endif
24+
endfunction
25+
26+
function! s:map(keys, rhs)
27+
for lhs in a:keys
28+
exe printf("nnoremap <buffer> %s %s:call <SID>on_motion()<CR>", lhs, a:rhs)
29+
endfor
30+
endfunction
31+
32+
function! s:valid_map(map)
33+
return
34+
\ type(a:map) == v:t_dict &&
35+
\ scrollmode#util#all(
36+
\ values(a:map),
37+
\ {_, x -> type(x) == v:t_list}
38+
\ ) &&
39+
\ scrollmode#util#all(
40+
\ scrollmode#util#unnest(values(a:map)),
41+
\ {_, x -> type(x) == v:t_string}
42+
\ )
43+
endfunction
44+
45+
function! s:valid_conf()
46+
if (
47+
\ exists("g:scroll_mode_actions") &&
48+
\ !s:valid_map(g:scroll_mode_actions)
49+
\ )
50+
echoerr "g:scroll_mode_actions has wrong type"
51+
return 0
52+
endif
53+
if (
54+
\ exists("g:scroll_mode_mappings") &&
55+
\ !s:valid_map(g:scroll_mode_mappings)
56+
\ )
57+
echoerr "g:scroll_mode_mappings has wrong type"
58+
return 0
59+
endif
60+
if (
61+
\ exists("g:scroll_mode_step") &&
62+
\ type(g:scroll_mode_step) != v:t_number
63+
\ )
64+
echoerr "g:scroll_mode_step must be a number"
65+
return 0
66+
endif
67+
return 1
68+
endfunction
69+
70+
function! scrollmode#enable#enable()
71+
if (!s:valid_conf())
72+
return
73+
endif
74+
75+
if (line('$') == 1)
76+
" No scrolling for new buffers because WinLeave is not triggered for them
77+
echo "ScrollMode: Nothing to scroll"
78+
return
79+
endif
80+
81+
let filename = expand("%:p")
82+
let step = exists("g:scroll_mode_step") ? g:scroll_mode_step : s:default_step
83+
84+
" Window variables
85+
let w:scroll_mode_enabled = 1
86+
let w:scroll_mode_scrolloff = &scrolloff
87+
let w:scroll_mode_cul = &cul
88+
let w:scroll_mode_cuc = &cuc
89+
let w:scroll_mode_actions = extend(
90+
\ copy(s:default_actions),
91+
\ exists("g:scroll_mode_actions") ? g:scroll_mode_actions : {}
92+
\ )
93+
let w:scroll_mode_mappings = exists("g:scroll_mode_mappings")
94+
\ ? g:scroll_mode_mappings
95+
\ : []
96+
97+
normal! M
98+
99+
echohl ModeMsg
100+
call s:echo_mode()
101+
102+
" Options
103+
set scrolloff=999
104+
setlocal cul
105+
setlocal nocuc
106+
107+
" Mappings
108+
call s:map(w:scroll_mode_actions.down, step . "jM")
109+
call s:map(w:scroll_mode_actions.up, step . "kM")
110+
call s:map(w:scroll_mode_actions.pagedown, "<C-f>M")
111+
call s:map(w:scroll_mode_actions.pageup, "<C-b>M")
112+
call s:map(w:scroll_mode_actions.bottom, "GM")
113+
call s:map(w:scroll_mode_actions.top, "ggM")
114+
call s:map(w:scroll_mode_actions.exit,
115+
\ ":call scrollmode#disable#disable()<CR>")
116+
call s:map(w:scroll_mode_actions.bdelete,
117+
\ ":call scrollmode#disable#disable()<CR>:bd<CR>")
118+
119+
for mapping in items(w:scroll_mode_mappings)
120+
call s:map(mapping[1], mapping[0])
121+
endfor
122+
123+
augroup scroll_mode
124+
au InsertEnter * call scrollmode#disable#disable()
125+
exe printf(
126+
\ "au WinLeave,BufWinLeave %s call scrollmode#disable#disable()",
127+
\ escape(filename, "^$.~*[ ")
128+
\ )
129+
augroup END
130+
endfunction

Diff for: autoload/scrollmode/toggle.vim

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function! scrollmode#toggle#toggle()
2+
if (exists("w:scroll_mode_enabled"))
3+
call scrollmode#disable#disable()
4+
else
5+
call scrollmode#enable#enable()
6+
endif
7+
endfunction

Diff for: autoload/scrollmode/util.vim

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function! scrollmode#util#all(list, test)
2+
let i = 0
3+
while (i < len(a:list))
4+
if (!a:test(i, a:list[i]))
5+
return 0
6+
endif
7+
let i += 1
8+
endwhile
9+
return 1
10+
endfunction
11+
12+
function! scrollmode#util#unnest(list)
13+
let result = []
14+
for sublist in a:list
15+
call extend(result, sublist)
16+
endfor
17+
return result
18+
endfunction

Diff for: plugin/scrollmode.vim

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if (exists("g:loaded_scroll_mode") && g:loaded_scroll_mode)
2+
finish
3+
endif
4+
let g:loaded_scroll_mode = 1
5+
6+
command ScrollMode call scrollmode#toggle#toggle()

0 commit comments

Comments
 (0)