Skip to content

Commit d7c9dd4

Browse files
committed
README, LICENSE, global vars improvements
- README.md - LICENSE - Postpone global vars initialization until scroll mode is enabled - check for invalid actions in g:scrollmode_actions - v:null for g:scrollmode_highlight_group_edge
1 parent 258222f commit d7c9dd4

File tree

8 files changed

+297
-79
lines changed

8 files changed

+297
-79
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016, 2017, 2019 Evgeny Zharov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+185-23
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,196 @@
1-
Vim ScrollMode
2-
===============
1+
Scroll Mode for Vim
2+
===================
33

4-
```vim
5-
let g:scroll_mode_mappings = {
6-
\ ":Bdelete<CR>": ["-"]
7-
\ }
4+
“Scroll mode” is a new mode for Vim that allows to scroll a text buffer with
5+
classical `h`/`j`/`k`/`l` bindings and some others, that are all designed for
6+
one-hand usage. Forget about `<C-f>`, `<C-d>`, and `G`. You don't need to keep
7+
holding modifier keys anymore — everything can be done with single-key
8+
mappings!
89

9-
nmap ξ{main}; <Plug>ScrollMode
10-
```
10+
New key mappings available in the scroll mode:
11+
12+
* `j` / `k` — scroll 5 lines down / up
13+
* `l` / `h` — scroll page down / up
14+
* `b` — scroll to the ending of the buffer (bottom)
15+
* `<Space>` — scroll to the beginning of the buffer
16+
* `;` — quit the scroll mode
17+
* `-` — quit the scroll mode and delete the buffer
18+
19+
With these mappings, you can quickly look around, walk through several screens
20+
of text, and when you are in the right place return back to normal mode for
21+
more accurate text manipulations.
1122

12-
TODO
23+
A comparison of pressed keys in normal mode and in the scroll mode:
1324

14-
`g:scrollmode_actions`
15-
`g:scrollmode_mappings`
16-
`g:scrollmode_distance`
25+
| Normal Mode | Scroll Mode |
26+
| ------------------------------- | ------------------------ |
27+
| `shift` `g` | `b` |
28+
| `ctrl` `b` | `h` |
29+
| `ctrl` `b` | `h` |
30+
| `k` `k` `k` `k` `k` `k` `k` `k` | `j` `j` |
31+
| 13 keys (including 3 modifiers) | 5 keys, all for one hand |
1732

18-
`g:scrollmode_cmdline_indicator`
19-
`g:scrollmode_airline_indicator`
20-
`g:scrollmode_statusline_highlight`
33+
Since scroll mode is in fact just a wrapper around normal mode, all keys, that
34+
are not affected continue to be available in the scroll mode: searching (`/`,
35+
`?`, `n`, `N`), window managing (`<C-w>`), etc.
2136

22-
`g:scrollmode_statusline_group`
23-
`g:scrollmode_statusline_group_edge`
37+
Installation
38+
------------
2439

25-
### `g:ScrollmodeOnQuite`
26-
Funcref that is called on quit from the scroll mode.
40+
| Plugin Manager | Command |
41+
| --- | --- |
42+
| [vim-plug](https://github.com/junegunn/vim-plug) | `Plug 'GeneZharov/vim-scrollmode'` |
43+
| [Vundle](https://github.com/VundleVim/Vundle.vim) | `Plugin 'GeneZharov/vim-scrollmode'` |
44+
| [pathogen.vim](https://github.com/tpope/vim-pathogen/) | `git clone 'git@github.com:GeneZharov/vim-scrollmode.git ~/.vim/bundle/vim-scrollmode'` |
45+
| native pack | `git clone 'git@github.com:GeneZharov/vim-scrollmode.git' ~/.vim/pack/dist/start/vim-scrollmode` |
46+
47+
Set a preferred shortcut to enter the scroll mode:
2748

2849
```vim
29-
function! s:fn() abort
30-
echo "scroll mode is over..."
31-
endfunction
50+
nmap <Leader>; <Plug>ScrollMode
51+
```
52+
53+
I prefer to use `<Space>` as a `<Leader>` key. You can achieve the same with:
3254

33-
let g:ScrollmodeOnQuit = function("s:fn")
55+
```vim
56+
map <SPACE> <Nop>
57+
let mapleader = "\<Space>"
3458
```
59+
60+
Configuration
61+
-------------
62+
63+
### Scrolling Settings
64+
65+
* **`g:scrollmode_actions`**
66+
67+
This option allows to configure default scroll mode key mappings. These
68+
key mappings are internally set with the following dictionary.
69+
70+
```vim
71+
\ {
72+
\ "up": ["k", "<Up>"],
73+
\ "down": ["j", "<Down>"],
74+
\ "pagedown": ["l"],
75+
\ "pageup": ["h"],
76+
\ "bottom": ["b"],
77+
\ "top": ["<Space>"],
78+
\ "exit": [";"],
79+
\ "bdelete": ["-"]
80+
\ }
81+
```
82+
83+
You can specify any subset of this dictionary as a value for this option in
84+
order to override default mappings.
85+
86+
For example, to add `<Esc>` key to the keys that exit the scroll mode:
87+
88+
```vim
89+
let g:scrollmode_actions = {
90+
\ "exit": [";", "<Esc>"],
91+
\ }
92+
```
93+
94+
_I didn't make `<Esc>` to quit the scroll mode as default behavior, because
95+
in the terminal Vim (but not Neovim) it can break some complex keys like
96+
`<Up>` and `<Down>` while the scroll mode is active. If you are not going
97+
to use these keys in the scroll mode or if you use a different Vim version
98+
(GVim, Neovim), then you are welcome to use the config from the example
99+
above._
100+
101+
* **`g:scrollmode_mappings`**
102+
103+
With this option, you can specify new mappings available in the scroll
104+
mode. This option has precedence over `g:scrollmode_actions`, so you can
105+
rebind a default action with any custom command.
106+
107+
For example, let's add a custom command for buffer deletion:
108+
109+
```vim
110+
let g:scroll_mode_mappings = {
111+
\ ":Bdelete<CR>": ["-", "c"]
112+
\ }
113+
```
114+
115+
When the scroll mode is activated, this code will internally result to:
116+
117+
```vim
118+
nnoremap <silent> <buffer> - :Bdelete<CR>
119+
nnoremap <silent> <buffer> c :Bdelete<CR>
120+
```
121+
122+
* **`g:scrollmode_distance`**
123+
124+
Number of lines to scroll at once by `j` / `k` (`"up"` and `"down"`
125+
actions).
126+
127+
*Default:* `5`
128+
129+
### Scrolling Indicators
130+
131+
* **`g:scrollmode_cmdline_indicator`**
132+
133+
Enables or disables the scroll mode indicator in the command line. The
134+
indicator looks similar to other indicators for built-in modes and looks
135+
like this:
136+
137+
```
138+
-- SCROLL --
139+
```
140+
141+
*Default:* `v:true`
142+
143+
* **`g:scrollmode_statusline_highlight`**
144+
145+
Enables or disables StatusLine highlighting, when the scroll mode is
146+
activated. Has no effect with plugins that replace the status line
147+
([Airline](https://github.com/vim-airline/vim-airline),
148+
[Powerline](https://github.com/powerline/powerline),
149+
[Lightline](https://github.com/itchyny/lightline.vim)).
150+
151+
_I am not fully satisfied with how this feature works, so it is disabled by
152+
default. But if you need scroll mode to be more noticeable and colorful,
153+
then you can enable this option. See the options below for color
154+
customization._
155+
156+
*Default:* `v:false`
157+
158+
### Highlight Groups
159+
160+
Options for status line color customization. Only have effect if
161+
`g:scrollmode_statusline_highlight` is enabled.
162+
163+
* **`g:scrollmode_statusline_group`**
164+
165+
Highlight group that is used for `StatusLine` when the scroll mode
166+
is active.
167+
168+
*Default:* `"DiffAdd"`
169+
170+
* **`g:scrollmode_statusline_group_edge`**
171+
172+
Highlight group that is used for `StatusLine` when the scroll mode is
173+
active and the top/bottom edge of the buffer is reached. The color helps to
174+
notice that you can't scroll in that direction anymore. Specify `v:null` if
175+
you don't need this behavior.
176+
177+
*Default:* `"DiffChange"`
178+
179+
### Hooks
180+
181+
* **`g:ScrollmodeOnQuit`**
182+
183+
Funcref that is called after you exit the scroll mode.
184+
185+
```vim
186+
function! s:fn() abort
187+
" ... your code
188+
endfunction
189+
190+
let g:ScrollmodeOnQuit = function("s:fn")
191+
```
192+
193+
License
194+
-------
195+
196+
MIT license

autoload/scrollmode/const.vim

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let g:scrollmode#const#state_init = "STATE_INIT"
2+
let g:scrollmode#const#state_top = "STATE_TOP"
3+
let g:scrollmode#const#state_middle = "STATE_MIDDLE"
4+
let g:scrollmode#const#state_bottom = "STATE_BOTTOM"
5+
6+
let g:scrollmode#const#default_actions = {
7+
\ "up": ["k", "<Up>"],
8+
\ "down": ["j", "<Down>"],
9+
\ "pagedown": ["l"],
10+
\ "pageup": ["h"],
11+
\ "bottom": ["b"],
12+
\ "top": ["<Space>"],
13+
\ "exit": [";"],
14+
\ "bdelete": ["-"]
15+
\ }
16+
" Can't map <Esc> to "exit" because it breaks mappings like <Up> and <Down>
17+
" in Vim (though in Neovim works fine).

autoload/scrollmode/enable.vim

+14-30
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,28 @@
1-
let s:STATE_INIT = "STATE_INIT"
2-
let s:STATE_TOP = "STATE_TOP"
3-
let s:STATE_MIDDLE = "STATE_MIDDLE"
4-
let s:STATE_BOTTOM = "STATE_BOTTOM"
5-
61
function s:cmd(cmd) abort
72
return (has("nvim") ? "<Cmd>" : ":<C-u>") . a:cmd
83
endfunction
94

10-
let s:default_actions = {
11-
\ "up": ["k", "<Up>"],
12-
\ "down": ["j", "<Down>"],
13-
\ "pagedown": ["l"],
14-
\ "pageup": ["h"],
15-
\ "bottom": ["b"],
16-
\ "top": ["<Space>"],
17-
\ "exit": [";"],
18-
\ "bdelete": ["-"]
19-
\ }
20-
" Can't map <Esc> to "exit" because it breaks mappings like <Up> and <Down>
21-
" in Vim (though in Neovim works fine).
22-
235
function! s:detect_state() abort
246
if line("w0") == 1
25-
return s:STATE_TOP
7+
return g:scrollmode#const#state_top
268
elseif line("w$") == line("$")
27-
return s:STATE_BOTTOM
9+
return g:scrollmode#const#state_bottom
2810
else
29-
return s:STATE_MIDDLE
11+
return g:scrollmode#const#state_middle
3012
endif
3113
endfunction
3214

3315
function! s:echo_mode(new_state) abort
34-
if !has("nvim") || w:scrollmode_state == s:STATE_INIT
16+
if !has("nvim") || w:scrollmode_state == g:scrollmode#const#state_init
3517
redraw
3618
echo "-- SCROLL --"
3719
endif
3820
endfunction
3921

4022
function! s:highlight(new_state) abort
4123
if a:new_state != w:scrollmode_state
42-
if a:new_state == s:STATE_MIDDLE
24+
if a:new_state == g:scrollmode#const#state_middle
25+
\ || g:scroll_mode_statusline_group_edge == v:null
4326
exe "highlight! link StatusLine" g:scrollmode_statusline_group
4427
else
4528
exe "highlight! link StatusLine" g:scrollmode_statusline_group_edge
@@ -90,7 +73,9 @@ function! s:affected_keys(dicts) abort
9073
endfunction
9174

9275
function! scrollmode#enable#enable() abort
93-
if !scrollmode#valid#valid_conf()
76+
call scrollmode#init#init_globals()
77+
78+
if !scrollmode#valid#valid_globals()
9479
return
9580
endif
9681

@@ -100,18 +85,17 @@ function! scrollmode#enable#enable() abort
10085
return
10186
endif
10287

103-
if scrollmode#tools#has_statusline_plugin()
104-
let g:scrollmode_hi_statusline = v:false
105-
endif
106-
10788
let filename = expand("%:p")
108-
let actions = extend(copy(s:default_actions), g:scrollmode_actions)
89+
let actions = extend(
90+
\ copy(g:scrollmode#const#default_actions),
91+
\ g:scrollmode_actions
92+
\ )
10993
let mappings = g:scrollmode_mappings
11094
let distance = g:scrollmode_distance
11195

11296
" Window variables
11397
let w:scrollmode_enabled = v:true
114-
let w:scrollmode_state = s:STATE_INIT
98+
let w:scrollmode_state = g:scrollmode#const#state_init
11599
let w:scrollmode_scrolloff = &scrolloff
116100
let w:scrollmode_cursorcolumn = &cursorcolumn
117101
let w:scrollmode_cursor_pos = getpos(".")

autoload/scrollmode/init.vim

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function! scrollmode#init#init_globals() abort
2+
if get(g:, "scrollmode_initialized", v:false)
3+
return
4+
endif
5+
let g:scrollmode_initialized = v:true
6+
7+
let g:scrollmode_actions =
8+
\ get(g:, "scrollmode_actions", {})
9+
let g:scrollmode_mappings =
10+
\ get(g:, "scrollmode_mappings", {})
11+
let g:scrollmode_distance =
12+
\ get(g:, "scrollmode_distance", 5)
13+
14+
let g:scrollmode_cmdline_indicator =
15+
\ get(g:, "scrollmode_cmdline_indicator", v:true)
16+
let g:scrollmode_statusline_highlight =
17+
\ get(g:, "scrollmode_statusline_highlight", v:false)
18+
let g:scrollmode_airline_indicator =
19+
\ scrollmode#tools#has_statusline_plugin()
20+
\ ? v:false
21+
\ : get(g:, "scrollmode_airline_indicator", v:false)
22+
23+
let g:scrollmode_statusline_group =
24+
\ get(g:, "scrollmode_statusline_group", "DiffAdd")
25+
let g:scrollmode_statusline_group_edge =
26+
\ get(g:, "scrollmode_statusline_group_edge", "DiffChange")
27+
endfunction

autoload/scrollmode/util.vim

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ function! scrollmode#util#reduce(list, iterator, initial) abort
2727
return acc
2828
endfunction
2929

30+
function! scrollmode#util#difference(xs, ys) abort
31+
let _xs = []
32+
for x in a:xs
33+
if index(a:ys, x) == -1
34+
call add(_xs, x)
35+
endif
36+
endfor
37+
return _xs
38+
endfunction
39+
3040
function scrollmode#util#to_unix_path(path) abort
3141
return substitute(a:path, "\\", "/", "g")
3242
endfunction

0 commit comments

Comments
 (0)