Emacs - Magit 在切换 window 时为不同的项目重用相同的 window

Emacs - Magit reuse same window for different project when switching window

我是 magit 的新用户,我目前在一个单独的框架中使用它,只有一个 window (magit-status)。我希望显示最近访问的缓冲区项目的 git 状态。想象一下,在另一个框架中,我有两个 windows 和两个不同项目的两个文件缓冲区。我希望 magit window 根据当前选择的 window.

向我显示项目的 git 状态
        frame 1                  frame 2
***********************     ****************  
* window 1 * window 2 *     *    window3   *
*          *          *     *              *
*   file   *   file   *     * magit-status *
* of proj1 * of proj2 *     *              *
*          *          *     *              *
***********************     ****************

如果选择的 window 是 window 1 更清楚,我想 window 3 显示项目 1 的状态,然后如果我更改为 window 2 我希望 window 3 显示项目 2 的状态。

您实际上可以通过设置 magit-display-buffer-function 变量来自定义 Magit 显示 magit-status 的方式。

如果我正确理解了你的问题,你可能正在寻找 magit-display-buffer-same-window-except-diff-v1 设置为该变量。

您可以手动设置此变量,或按 C-h v magit-dispaly-buffer-function RET 并使用 Easy Customization.

仅供参考,您还可以为 Magit 实现自己的方式来显示缓冲区:

(setq magit-display-buffer-function
      (lambda (buffer)          
       ;; Go on
      ))

由于这里的回答,这是我正在使用的最终解决方案:

https://emacs.stackexchange.com/questions/36718/magit-reuse-same-window-for-different-project-when-switching-window

(defun magit-status-autorefresh (callee)
  (interactive)
  (let ((project-previous (magit-toplevel)))
    (call-interactively callee)
    (let ((project (magit-toplevel)))
      (when (and project
                 (not (equal project-previous project)))
        (let ((status-win
               (cl-some (lambda (b)
                          (and (with-current-buffer b
                                 (derived-mode-p 'magit-status-mode))
                               (get-buffer-window b 'visible)))
                        (buffer-list)))
              (magit-display-buffer-noselect t)
              (magit-display-buffer-function
               (lambda (buffer)
                 (display-buffer buffer '(display-buffer-same-window)))))
          (when status-win
            (with-selected-frame (window-frame status-win)
              (with-selected-window status-win
                (magit-status-internal project))))))))
  )

(defun my/other-window ()
  (interactive)
  (magit-status-autorefresh 'other-window)
  )

(defun my/other-counsel-projectile-find-file ()
  (interactive)
  (magit-status-autorefresh 'counsel-projectile-find-file)
  )

(defun my/other-ace-window ()
  (interactive)
  (magit-status-autorefresh 'ace-window)
  )