branch: elpa/magit
commit c4def6d99ad74b987472e25f06d7851c97b25818
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>

    magit-{setup,refresh}-buffer: Add {INITIAL,SELECT}-SECTION arguments
    
    Add new arguments INITIAL-SECTION and SELECT-SECTION to
    `magit-setup-buffer', `magit-setup-buffer-internal' and
    `magit-refresh-buffer'.  The former replaces the hook
    `magit--initial-section-hook'.
    
    Use INITIAL-SECTION to setup status buffer.  SELECT-SECTION
    isn't used yet.
---
 lisp/magit-mode.el   | 50 ++++++++++++++++++++++++++++++++------------------
 lisp/magit-status.el |  4 +---
 2 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/lisp/magit-mode.el b/lisp/magit-mode.el
index 8cd998ef4e1..4abc458c79b 100644
--- a/lisp/magit-mode.el
+++ b/lisp/magit-mode.el
@@ -626,16 +626,27 @@ The buffer's major-mode should derive from 
`magit-section-mode'."
 
 ;;; Setup Buffer
 
-(defmacro magit-setup-buffer (mode &optional locked &rest bindings)
-  (declare (indent 2))
-  `(magit-setup-buffer-internal
-    ,mode ,locked
-    ,(cons 'list (mapcar (pcase-lambda (`(,var ,form))
-                           `(list ',var ,form))
-                         bindings))))
-
-(cl-defun magit-setup-buffer-internal ( mode locked bindings
-                                        &key buffer directory)
+(defmacro magit-setup-buffer (mode &optional locked &rest args)
+  "\n\n(fn MODE &optional LOCKED &key BUFFER DIRECTORY \
+INITIAL-SECTION SELECT-SECTION &rest BINDINGS)"
+  (declare (indent 2)
+           (debug (form [&optional locked]
+                        [&rest keywordp form]
+                        [&rest (symbolp form)])))
+  (let (kwargs)
+    (while (keywordp (car args))
+      (push (pop args) kwargs)
+      (push (pop args) kwargs))
+    `(magit-setup-buffer-internal
+      ,mode ,locked
+      ,(cons 'list (mapcar (pcase-lambda (`(,var ,form))
+                             `(list ',var ,form))
+                           args))
+      ,@(nreverse kwargs))))
+
+(cl-defun magit-setup-buffer-internal
+    ( mode locked bindings
+      &key buffer directory initial-section select-section)
   (let* ((value   (and locked
                        (with-temp-buffer
                          (pcase-dolist (`(,var ,val) bindings)
@@ -662,7 +673,9 @@ The buffer's major-mode should derive from 
`magit-section-mode'."
     (magit-display-buffer buffer)
     (with-current-buffer buffer
       (run-hooks 'magit-setup-buffer-hook)
-      (magit-refresh-buffer created)
+      (magit-refresh-buffer created
+                            :initial-section initial-section
+                            :select-section select-section)
       (when created
         (run-hooks 'magit-post-create-buffer-hook)))
     buffer))
@@ -1065,10 +1078,10 @@ Run hooks `magit-pre-refresh-hook' and 
`magit-post-refresh-hook'."
 
 (defvar-local magit--refresh-start-time nil)
 
-(defvar magit--initial-section-hook nil)
-
-(defun magit-refresh-buffer (&optional created)
-  "Refresh the current Magit buffer."
+(cl-defun magit-refresh-buffer ( &optional created
+                                 &key initial-section select-section)
+  "Refresh the current Magit buffer.
+The arguments are for internal use."
   (interactive)
   (when-let ((refresh (magit--refresh-buffer-function)))
     (let ((magit--refreshing-buffer-p t)
@@ -1080,8 +1093,8 @@ Run hooks `magit-pre-refresh-hook' and 
`magit-post-refresh-hook'."
       (cond
        (created
         (funcall refresh)
-        (run-hooks 'magit--initial-section-hook)
-        (setq-local magit--initial-section-hook nil))
+        (cond (initial-section (funcall initial-section))
+              (select-section (funcall select-section))))
        (t
         (deactivate-mark)
         (setq magit-section-pre-command-section nil)
@@ -1091,7 +1104,8 @@ Run hooks `magit-pre-refresh-hook' and 
`magit-post-refresh-hook'."
         (setq magit-section-focused-sections nil)
         (let ((positions (magit--refresh-buffer-get-positions)))
           (funcall refresh)
-          (magit--refresh-buffer-set-positions positions))))
+          (cond (select-section (funcall select-section))
+                ((magit--refresh-buffer-set-positions positions))))))
       (let ((magit-section-cache-visibility nil))
         (magit-section-show magit-root-section))
       (run-hooks 'magit-refresh-buffer-hook)
diff --git a/lisp/magit-status.el b/lisp/magit-status.el
index 75fd4bd6795..adfe20f1584 100644
--- a/lisp/magit-status.el
+++ b/lisp/magit-status.el
@@ -433,9 +433,6 @@ Type \\[magit-commit] to create a commit.
   :interactive nil
   :group 'magit-status
   (magit-hack-dir-local-variables)
-  (when magit-status-initial-section
-    (add-hook 'magit--initial-section-hook
-              #'magit-status-goto-initial-section nil t))
   (setq magit--imenu-group-types '(not branch commit)))
 
 (put 'magit-status-mode 'magit-diff-default-arguments
@@ -457,6 +454,7 @@ Type \\[magit-commit] to create a commit.
          (line (and file (save-restriction (widen) (line-number-at-pos))))
          (col  (and file (save-restriction (widen) (current-column))))
          (buf  (magit-setup-buffer #'magit-status-mode nil
+                 :initial-section #'magit-status-goto-initial-section
                  (magit-buffer-diff-args  (nth 0 d))
                  (magit-buffer-diff-files (nth 1 d))
                  (magit-buffer-log-args   (nth 0 l))

Reply via email to