├── demo.gif ├── README.md ├── LICENSE.txt └── plugin └── scroll_barnacle.vim /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sslivkoff/vim-scroll-barnacle/HEAD/demo.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # vim-scroll-barnacle 3 | 4 | a scrollbar for vim in the terminal 5 | 6 | ![vim-scroll-barnacle demo](demo.gif) 7 | 8 | 9 | ## Features 10 | - unicode characters for high-resolution 11 | - mouse click support 12 | - customizable colors and glyphs 13 | 14 | 15 | ## Install 16 | - vundle: `Plugin 'sslivkoff/vim-scroll-barnacle'` 17 | - vim-plug: `Plug 'sslivkoff/vim-scroll-barnacle'` 18 | 19 | 20 | ## Usage 21 | 22 | #### Functions 23 | - change scrollbar state with 24 | - `:ScrollbarToggle` 25 | - `:ScrollbarOn` 26 | - `:ScrollbarOff` 27 | - can bind to keys e.g. `nnoremap :ScrollbarToggle` 28 | 29 | 30 | #### Configuration 31 | - can put these in your vimrc, e.g. `let g:sb_default_behavior = "always"` 32 | - `g:sb_default_behavior` whether scrollbar is on by default (default = `"overflow"`) 33 | - `"always"`: scrollbar always on 34 | - `"overflow"`: scrollbar on when buffer does not fit in window 35 | - `"never"`: scrollbar off by default 36 | - `g:sb_bar_style` style to use for scrollbar (default = `"solid"`) 37 | - `"solid"` solid scrollbar using `▁▂▃▄▅▆▇█` 38 | - `"left dot"` dot scrollbar using `⠁⠂⠄⡀⠃⠆⡄⠇⡆⡇` 39 | - `"right dot"` dot scrollbar using `⠈⠐⠠⢀⠘⠰⢠⠸⢰⢸` 40 | - `"double dot"` dot scrollbar using `⠉⠒⠤⣀⠛⠶⣤⠿⣶⣿` 41 | - `g:sb_patch_mouse` whether to patch mouse for mouse click events (default = `1`) 42 | - see section below 43 | - `g:sb_patch_keys` whether to patch certain keys for scroll events (default = `1`) 44 | - `Scrollbar` highlight group 45 | - change scrollbar color using the `Scrollbar` highlight group 46 | - e.g. 47 | - `highlight Scrollbar guifg=grey` 48 | - `highlight link Scrollbar Normal` [default] 49 | - `highlight link Scrollbar Comment` 50 | 51 | 52 | ## Implementation 53 | - uses floating window, window has filetype `scrollbar` 54 | - [gifine](https://github.com/leafo/gifine) and [vim-smoothie](https://github.com/psliwka/vim-smoothie) used for creating the gif in this readme 55 | - weclome to suggestions, bug reports, and pull requests 56 | 57 | 58 | ## Shortcomings 59 | - Neovim only for now 60 | - some `nvim_*` functions in the neovim [api](https://neovim.io/doc/user/api.html) were crucial for functionality and efficiency 61 | - suggestions on how to port to vim welcome 62 | - Can only mouse click, not mouse drag 63 | - The vim mouse api is very limited 64 | - Clicking requires patching `` input 65 | - Can disable this patching with `let g:sb_patch_mouse = 0` 66 | - Vim does not emit scroll events directly 67 | - Most actions that affect the scrollbar can be detected indirectly using vim events (e.g. `CursorMove` and `VimResize`) 68 | - However, some scroll actions are not detectable with events 69 | - ScrollBarnacle patches these key combinations so that they can affect the scrollbar: `zz`, `zt`, `zb`, ``, ``, ``, `` 70 | - You can disable this patching with `let g:sb_patch_keys = 0` 71 | - Does not work with tabs 72 | - This is because there is no way to close floating windows in a tab when it is closed. Attempting to close the window results in an error. 73 | - A neovim patch is in the works, see [issue](https://github.com/neovim/neovim/issues/11440) and [WIP PR](https://github.com/neovim/neovim/pull/11938) 74 | 75 | 76 | ## Related Projects 77 | - https://github.com/lornix/vim-scrollbar 78 | - https://github.com/gcavallanti/vim-noscrollbar 79 | - https://github.com/drzel/vim-line-no-indicator 80 | 81 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /plugin/scroll_barnacle.vim: -------------------------------------------------------------------------------- 1 | 2 | " vim-scroll-barnacle 3 | 4 | " plugin to add scrollbar to terminal vim windows 5 | 6 | 7 | " add scrollbar to current window 8 | function SBAddScrollbar() 9 | 10 | call SBInitializeWindowState() 11 | 12 | if exists("w:_sb_has_scrollbar") && w:_sb_has_scrollbar 13 | return 14 | endif 15 | 16 | " check for underflow 17 | if winheight(0) >= line("$") && w:sb_window_behavior == "overflow" 18 | let w:_sb_off_because_underflow = 1 19 | return 20 | endif 21 | 22 | " create contents of scrollbar 23 | let contents = SBCreateContents() 24 | 25 | " create window 26 | let w:scrollbuf = nvim_create_buf(v:false, v:true) 27 | call nvim_buf_set_lines(w:scrollbuf, 0, -1, v:true, contents) 28 | let opts = { 29 | \ "relative": "win", 30 | \ "width": 1, 31 | \ "height": (winheight(0) - 0), 32 | \ "col": (winwidth(0) - 1), 33 | \ "row": 0, 34 | \ "focusable": g:sb_patch_mouse, 35 | \ "anchor": "NW", 36 | \ "style": "minimal"} 37 | let w:win = nvim_open_win(w:scrollbuf, 0, opts) 38 | 39 | " configure window 40 | call nvim_win_set_option(w:win, "winhl", "Normal:Scrollbar") 41 | let window = nvim_get_current_win() 42 | let g:_sb_scrollbar_windows[window] = w:win 43 | call nvim_set_current_win(w:win) 44 | 45 | " settings for scrollbar floating window 46 | set filetype=scrollbar 47 | let w:original_window = window 48 | syntax match ScrollBlockBottom /[ ▁▂▃▄▅▆▇█]r/ 49 | highlight ScrollBlockBottom gui=reverse 50 | set nowrap 51 | 52 | " change focus back to original window 53 | call nvim_set_current_win(window) 54 | let w:_sb_has_scrollbar = 1 55 | 56 | " set sidescrolloff to width of scrollbar for proper horizontal scrolling 57 | set sidescrolloff=1 58 | 59 | endfunction 60 | 61 | 62 | " create scrollbar contents 63 | function SBCreateContents() 64 | 65 | let n_window_lines = winheight(0) 66 | let window_lower_bound = line("w0") - 1 67 | let window_upper_bound = line("w$") - 1 68 | let lines_per_block = (line("$")) / (winheight(0) + 0.0) 69 | 70 | if g:sb_bar_style == "solid" 71 | let n_subblocks_per_block = 8 72 | let min_subblocks = 8 73 | let upper_block_glyphs = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] 74 | let lower_block_glyphs = ["█r", "▇r", "▆r", "▅r", "▄r", "▃r", "▂r", "▁r", " r"] 75 | let subblock_glyphs = [] 76 | elseif g:sb_bar_style == "double dot" 77 | let n_subblocks_per_block = 4 78 | let min_subblocks = 1 79 | let upper_block_glyphs = [" ", "⣀", "⣤", "⣶", "⣿"] 80 | let lower_block_glyphs = [" ", "⠉", "⠛", "⠿", "⣿"] 81 | let subblock_glyphs = [[], ["⠉", "⠒", "⠤", "⣀"], ["⠛", "⠶", "⣤"], ["⠿", "⣶"], ["⣿"]] 82 | elseif g:sb_bar_style == "left dot" 83 | let n_subblocks_per_block = 4 84 | let min_subblocks = 1 85 | let upper_block_glyphs = [" ", "⡀", "⡄", "⡆", "⡇"] 86 | let lower_block_glyphs = [" ", "⠁", "⠃", "⠇", "⡇"] 87 | let subblock_glyphs = [[], ["⠁", "⠂", "⠄", "⡀"], ["⠃", "⠆", "⡄"], ["⠇", "⡆"], ["⡇"]] 88 | elseif g:sb_bar_style == "right dot" 89 | let n_subblocks_per_block = 4 90 | let min_subblocks = 1 91 | let upper_block_glyphs = [" ", "⢀", "⢠", "⢰", "⢸"] 92 | let lower_block_glyphs = [" ", "⠈", "⠘", "⠸", "⢸"] 93 | let subblock_glyphs = [[], ["⠈", "⠐", "⠠", "⢀"], ["⠘", "⠰", "⢠"], ["⠸", "⢰"], ["⢸"]] 94 | else 95 | echoerr "unkown scrollbar style: " . string(g:sb_bar_style) 96 | endif 97 | 98 | let fill_glyph = upper_block_glyphs[-1] 99 | 100 | let n_total_blocks = winheight(0) 101 | let n_total_subblocks = n_total_blocks * n_subblocks_per_block 102 | let n_subblocks = (window_upper_bound - window_lower_bound + 1) * n_total_subblocks / (line("$") + 0.0) 103 | let n_subblocks = float2nr(round(n_subblocks)) 104 | if n_subblocks < min_subblocks 105 | let n_subblocks = min_subblocks 106 | endif 107 | 108 | let float_first_block = (line("w0") - 1) / lines_per_block 109 | let float_first_subblock = float_first_block * n_subblocks_per_block 110 | let int_first_subblock = float2nr(round(float_first_subblock)) 111 | let int_last_subblock = int_first_subblock + n_subblocks - 1 112 | let int_first_block = float2nr(floor(int_first_subblock / (n_subblocks_per_block + 0.0))) 113 | let int_last_block = float2nr(floor(int_last_subblock / (n_subblocks_per_block + 0.0))) 114 | let subblocks_in_first_block = n_subblocks_per_block - int_first_subblock % n_subblocks_per_block 115 | let subblocks_in_last_block = 1 + int_last_subblock % n_subblocks_per_block 116 | 117 | let contents = [] 118 | let b = 0 119 | while b < n_total_blocks 120 | if b < int_first_block 121 | " empty 122 | let contents = contents + [" "] 123 | elseif b == int_first_block && b == int_last_block 124 | " subblock 125 | if len(subblock_glyphs) == 0 126 | " no subblock rendering 127 | let contents = contents + [fill_glyph] 128 | else 129 | " subblock rendering 130 | let first_subblock_in_block = n_subblocks_per_block - subblocks_in_first_block 131 | let glyph = subblock_glyphs[n_subblocks][first_subblock_in_block] 132 | let contents = contents + [glyph] 133 | endif 134 | elseif b == int_first_block 135 | " top block 136 | let contents = contents + [upper_block_glyphs[subblocks_in_first_block]] 137 | elseif int_first_block < b && b < int_last_block 138 | " middle block 139 | let contents = contents + [fill_glyph] 140 | elseif b == int_last_block 141 | " bottom block 142 | let contents = contents + [lower_block_glyphs[subblocks_in_last_block]] 143 | elseif b > int_last_block 144 | " empty 145 | let contents = contents + [" "] 146 | endif 147 | 148 | let b = b + 1 149 | endwhile 150 | 151 | return contents 152 | endfunction 153 | 154 | 155 | " remove scrollbar from current window 156 | function SBRemoveScrollbar() 157 | if exists("w:_sb_has_scrollbar") && w:_sb_has_scrollbar 158 | call nvim_win_close(w:win, 1) 159 | unlet g:_sb_scrollbar_windows[string(nvim_get_current_win())] 160 | endif 161 | let w:_sb_has_scrollbar = 0 162 | endfunction 163 | 164 | 165 | " remove scrollbars that became orphaned when vim windows closed 166 | function SBRemoveOrphanScrollbars() 167 | let all_windows = nvim_list_wins() 168 | for win_pair in items(g:_sb_scrollbar_windows) 169 | if index(all_windows, str2nr(win_pair[0])) < 0 170 | call nvim_win_close(win_pair[1], 1) 171 | unlet g:_sb_scrollbar_windows[win_pair[0]] 172 | endif 173 | endfor 174 | endfunction 175 | 176 | 177 | " return whether current window has a scrollbar 178 | function SBHasScrollbar() 179 | " disable functionality if using tabs, see README.md 180 | if g:_sb_tab_disabled 181 | return 0 182 | end 183 | 184 | return exists("w:_sb_has_scrollbar") && w:_sb_has_scrollbar 185 | endfunction 186 | 187 | 188 | " redraw scrollbar contents, this is main function to update scrollbar 189 | function SBRedrawScrollbar() 190 | if &filetype == "scrollbar" 191 | return 192 | endif 193 | 194 | " check for underflow 195 | if winheight(0) >= line("$") 196 | call SBRemoveScrollbar() 197 | let w:_sb_off_because_underflow = 1 198 | 199 | " check for overflow 200 | elseif exists("w:_sb_off_because_underflow") 201 | \ && w:_sb_off_because_underflow 202 | \ && winheight(0) < line("$") 203 | call SBAddScrollbar() 204 | let w:_sb_off_because_underflow = 0 205 | 206 | " redraw existing scrollbar 207 | elseif SBHasScrollbar() 208 | let new_contents = SBCreateContents() 209 | call nvim_buf_set_lines(w:scrollbuf, 0, -1, 0, new_contents) 210 | call nvim_win_set_cursor(w:win, [1, 0]) 211 | endif 212 | endfunction 213 | 214 | 215 | " reset scrollbar, used when window has changed size 216 | function SBResetScrollbar() 217 | if SBHasScrollbar() 218 | call SBRemoveScrollbar() 219 | call SBAddScrollbar() 220 | endif 221 | endfunction 222 | 223 | 224 | " toggle scrollbar on or off 225 | function SBToggleScrollbar() 226 | if SBHasScrollbar() 227 | call SBRemoveScrollbar() 228 | elseif exists("w:_sb_off_because_underflow") 229 | \ && w:_sb_off_because_underflow 230 | let w:_sb_window_behavior = "always" 231 | call SBAddScrollbar() 232 | else 233 | call SBAddScrollbar() 234 | endif 235 | endfunction 236 | 237 | 238 | " patch keys whose scrolling actions cannot be detected by vim events 239 | function SBPatchKeys() 240 | " use `` in insert mode shortcuts to temporarily leave insert mode 241 | " add `gv` after visual mode shortcuts to restore selection 242 | 243 | nmap zz zz:call SBRedrawScrollbar() 244 | vmap zz zz:call SBRedrawScrollbar()gv 245 | nmap zt zt:call SBRedrawScrollbar() 246 | vmap zt zt:call SBRedrawScrollbar()gv 247 | nmap zb zb:call SBRedrawScrollbar() 248 | vmap zb zb:call SBRedrawScrollbar()gv 249 | nmap :call SBRedrawScrollbar() 250 | vmap :call SBRedrawScrollbar()gv 251 | nmap :call SBRedrawScrollbar() 252 | vmap :call SBRedrawScrollbar()gv 253 | 254 | nmap :call SBRedrawScrollbar() 255 | nmap :call SBRedrawScrollbar() 256 | imap :call SBRedrawScrollbar() 257 | imap :call SBRedrawScrollbar() 258 | vmap :call SBRedrawScrollbar()gv 259 | vmap :call SBRedrawScrollbar()gv 260 | endfun 261 | 262 | 263 | " move window according to where scrollbar is clicked 264 | function SBHandleMouseClick() 265 | if &filetype == "scrollbar" 266 | let scrollblock = line(".") - 1 267 | call nvim_set_current_win(w:original_window) 268 | let lines_per_block = (line("$")) / (winheight(0) + 0.0) 269 | let new_line = scrollblock * lines_per_block 270 | let new_line = float2nr(round(new_line) + 1) 271 | exec(":" . string(new_line)) 272 | call feedkeys("zt") 273 | endif 274 | endfun 275 | 276 | 277 | " patch mouse clicks to manipulate scrollbar 278 | function SBPatchMouse() 279 | nnoremap :call SBHandleMouseClick() 280 | endfun 281 | 282 | 283 | " initialize window's scrollbar configuration state 284 | function SBInitializeWindowState() 285 | if !exists("w:_sb_initialized") || !w:_sb_initialized 286 | let w:_sb_initialized = 1 287 | if g:sb_default_behavior == "always" || g:sb_default_behavior == "overflow" 288 | let w:sb_window_behavior = g:sb_default_behavior 289 | call SBAddScrollbar() 290 | endif 291 | end 292 | endfunction 293 | 294 | 295 | " 296 | " " tab functionality 297 | " 298 | 299 | let g:_sb_tab_disabled = 0 300 | 301 | 302 | " disable functionality if using tabs, see README.md 303 | function SBDisableForTabs() 304 | let g:sb_default_behavior = "never" 305 | 306 | for win_pair in items(g:_sb_scrollbar_windows) 307 | call nvim_win_close(win_pair[1], 1) 308 | endfor 309 | let g:_sb_scrollbar_windows = {} 310 | 311 | if !g:_sb_tab_disabled 312 | echon "vim-scroll-barnacle disabled for tabs (see README.md)" 313 | endif 314 | 315 | let g:_sb_tab_disabled = 1 316 | endfunction 317 | 318 | 319 | " 320 | " " initialize plugin 321 | " 322 | 323 | " neovim only 324 | if has('nvim') 325 | 326 | " set defaults 327 | if !exists("g:sb_default_behavior") 328 | let g:sb_default_behavior = "overflow" 329 | end 330 | if !exists("g:sb_bar_style") 331 | let g:sb_bar_style = "solid" 332 | end 333 | if !exists("g:sb_patch_keys") 334 | let g:sb_patch_keys = 1 335 | end 336 | if !exists("g:sb_patch_mouse") 337 | let g:sb_patch_mouse = 1 338 | end 339 | if !exists("g:_sb_scrollbar_windows") 340 | let g:_sb_scrollbar_windows = {} 341 | end 342 | 343 | " patch keys and mouse 344 | if g:sb_patch_keys 345 | call SBPatchKeys() 346 | endif 347 | if g:sb_patch_mouse 348 | call SBPatchMouse() 349 | endif 350 | 351 | " set commands 352 | command ScrollbarOn call SBAddScrollbar() 353 | command ScrollbarOff call SBRemoveScrollbar() 354 | command ScrollbarToggle call SBToggleScrollbar() 355 | 356 | " set autocmds 357 | augroup scroll_barnacle 358 | autocmd CursorMoved,CursorMovedI,TextChanged,TextChangedI,InsertLeave,FileChangedShellPost,BufEnter * call SBRedrawScrollbar() 359 | autocmd VimResized * call SBResetScrollbar() 360 | autocmd WinEnter * call SBRemoveOrphanScrollbars() 361 | autocmd WinEnter,BufWinEnter * call SBInitializeWindowState() 362 | autocmd WinLeave * call SBRedrawScrollbar() 363 | 364 | " disable functionality if using tabs, see README.md 365 | autocmd TabLeave * call SBDisableForTabs() 366 | augroup end 367 | else 368 | echo "vim-scroll-barnacle only available in neovim" 369 | endif 370 | 371 | --------------------------------------------------------------------------------