branch: externals/diff-hl
commit 9c4bc17830a7954ef7ab4897730fb56659fbac70
Author: Dmitry Gutov <dmi...@gutov.dev>
Commit: Dmitry Gutov <dmi...@gutov.dev>

    Ensure the margin is shown when we use it, and allow to hide it after
    
    First part is for smoother behavior in #212.
    
    The second - new option that's nil by default - is for #220.
---
 diff-hl-margin.el |  8 ++++++++
 diff-hl.el        | 20 +++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/diff-hl-margin.el b/diff-hl-margin.el
index 65ef337f75..3f142a2c24 100644
--- a/diff-hl-margin.el
+++ b/diff-hl-margin.el
@@ -147,7 +147,15 @@ You probably shouldn't use this function directly."
                         ,(propertize char 'face
                                      (intern (format "diff-hl-margin-%s" 
type)))))))))
 
+(defun diff-hl-margin-ensure-visible ()
+  (let ((width-var (intern (format "%s-margin-width" diff-hl-side))))
+    (when (zerop (symbol-value width-var))
+      (set width-var 1)
+      (dolist (win (get-buffer-window-list))
+        (set-window-buffer win (current-buffer))))))
+
 (defun diff-hl-highlight-on-margin (ovl type _shape)
+  (diff-hl-margin-ensure-visible)
   (let ((spec (cdr (assoc (cons type diff-hl-side)
                           (diff-hl-margin-spec-cache)))))
     (overlay-put ovl 'before-string spec)))
diff --git a/diff-hl.el b/diff-hl.el
index 39dd7201f3..9c460063b9 100644
--- a/diff-hl.el
+++ b/diff-hl.el
@@ -123,6 +123,13 @@ This requires the corresponding margin width to be >0 
already."
   :group 'diff-hl
   :type 'boolean)
 
+(defcustom diff-hl-autohide-margin nil
+  "Non-nil to reset margin width to 0 when no indicators shown.
+
+When you use it, it's recommended to verify first that other enabled
+features don't use margin for their indicators."
+  :type 'boolean)
+
 (defcustom diff-hl-highlight-function 'diff-hl-highlight-on-fringe
   "Function to highlight the current line. Its arguments are
   overlay, change type and position within a hunk."
@@ -461,6 +468,8 @@ It can be a relative expression as well, such as \"HEAD^\" 
with Git, or
   (let ((changes (diff-hl-changes))
         (current-line 1))
     (diff-hl-remove-overlays)
+    (when (not changes)
+      (diff-hl--autohide-margin))
     (save-excursion
       (save-restriction
         (widen)
@@ -491,6 +500,14 @@ It can be a relative expression as well, such as \"HEAD^\" 
with Git, or
                 (overlay-put h 'insert-in-front-hooks hook)
                 (overlay-put h 'insert-behind-hooks hook)))))))))
 
+(defun diff-hl--autohide-margin ()
+  (let ((width-var (intern (format "%s-margin-width" diff-hl-side))))
+    (when (and diff-hl-autohide-margin
+               (> (symbol-value width-var) 0))
+      (set width-var 0)
+      (dolist (win (get-buffer-window-list))
+        (set-window-buffer win (current-buffer))))))
+
 (defvar-local diff-hl--modified-tick nil)
 
 (put 'diff-hl--modified-tick 'permanent-local t)
@@ -995,7 +1012,8 @@ The value of this variable is a mode line template as in
     (remove-hook 'magit-revert-buffer-hook 'diff-hl-update t)
     (remove-hook 'magit-not-reverted-hook 'diff-hl-update t)
     (remove-hook 'text-scale-mode-hook 'diff-hl-maybe-redefine-bitmaps t)
-    (diff-hl-remove-overlays)))
+    (diff-hl-remove-overlays)
+    (diff-hl--autohide-margin)))
 
 (defun diff-hl-after-checkin ()
   (let ((fileset (vc-deduce-fileset t)))

Reply via email to