From 6a66b2f8dc84643cf0fa50506ff7b74558ef0b37 Mon Sep 17 00:00:00 2001
From: Jerome Reybert <jreybert@gmail.com>
Date: Thu, 21 Feb 2019 13:59:55 +0100
Subject: [PATCH] Set scrolloff in magit buffer

By default, scrolloff is set to 3. Can be overwritten with
g:magit_scrolloff.
Until recent vim patch 8.1.0864, scrolloff is a global only option. Must
play with autocommand BufEnter/BufExit to mimic a local scrolloff
behavior.

ref #172
---
 doc/vimagit.txt  |  8 ++++++++
 plugin/magit.vim | 13 +++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/doc/vimagit.txt b/doc/vimagit.txt
index dd3c0c7..e0ef71e 100644
--- a/doc/vimagit.txt
+++ b/doc/vimagit.txt
@@ -475,6 +475,14 @@ This option automatically opens the fold of the hunk cursor has jump to.
 Default value is 1.
 let g:magit_auto_foldopen=[01]
 
+                                                     *vimagit-g:magit_scrolloff*
+Set *'scrolloff'* value in magit buffer.
+Minimal number of screen lines to keep above and below the cursor.
+Default value is 3.
+Set to -1 if you do not want to set scrolloff, 0 to disable scrolloff in magit
+buffer.
+let g:magit_scrolloff=3
+
                                               *vimagit-g:magit_default_sections*
 With this variable, the user is able to choose which sections are displayed in
 magit buffer, and in which order.
diff --git a/plugin/magit.vim b/plugin/magit.vim
index 8b6a9a4..1455382 100644
--- a/plugin/magit.vim
+++ b/plugin/magit.vim
@@ -37,6 +37,8 @@ let g:magit_refresh_gitgutter      = get(g:, 'magit_refresh_gitgutter',
 
 let g:magit_commit_title_limit     = get(g:, 'magit_commit_title_limit',        50)
 
+let g:magit_scrolloff              = get(g:, 'magit_scrolloff',                 3)
+
 let g:magit_warning_max_lines      = get(g:, 'magit_warning_max_lines',         10000)
 
 let g:magit_git_cmd                = get(g:, 'magit_git_cmd'          ,         "git")
@@ -808,6 +810,17 @@ function! magit#show_magit(display, ...)
 	setlocal nomodeline
 	let &l:foldlevel = b:magit_default_fold_level
 	setlocal filetype=magit
+	if ( g:magit_scrolloff != -1 )
+		if ( has("patch-8.1.0864") )
+			let &l:scrolloff = g:magit_scrolloff
+		else
+			let s:default_scrolloff=&scrolloff
+			" ugly hack, scrolloff is a global only option before patch 8.1.0864
+			execute "autocmd BufLeave " . buffer_name . " :set scrolloff=" . s:default_scrolloff
+			execute "autocmd BufEnter " . buffer_name . " :set scrolloff=" . g:magit_scrolloff
+			let &scrolloff = g:magit_scrolloff
+		endif
+	endif
 
 	augroup vimagit_buffer
 	autocmd!
-- 
GitLab