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

    magit-diff-visit-avoid-prefer-worktree: New option
    
    I strongly recommend you don't change the value of this option.
    See the information I have added to the manual.
    
    Closes #5421.
---
 docs/magit.org     | 35 +++++++++++++++++++++++++++++++++++
 docs/magit.texi    | 35 +++++++++++++++++++++++++++++++++++
 lisp/magit-diff.el | 46 +++++++++++++++++++++++++++++++++++++---------
 3 files changed, 107 insertions(+), 9 deletions(-)

diff --git a/docs/magit.org b/docs/magit.org
index 8fe1a871b6b..dae754b0116 100644
--- a/docs/magit.org
+++ b/docs/magit.org
@@ -4097,6 +4097,41 @@ argument.
   These commands behave like the respective commands described above,
   except that they display the blob or file in another window or frame.
 
+- User Option: magit-diff-visit-prefer-worktree ::
+
+  This option controls whether ~magit-diff-visit-file~ always visits the
+  respective file in the worktree, when invoked anywhere from within a
+  hunk of staged or unstaged changes.
+
+  By default ~magit-diff-visit-file~ does not do that.  Instead it
+  behaves for staged and unstaged changes as it does for committed
+  changes, by visiting a blob from the old/left or new/right side,
+  depending on whether point is on a removed line or not.
+
+  For staged changes the old side is the blob from HEAD and the right
+  side is the blog from the index.  For unstaged changes the left side
+  is the blob from the index (if there are any changes in the index
+  for that file, else the blob from HEAD), and the right side is the
+  file in the worktree.
+
+  Being able to jump to HEAD or the index from a removed line is very
+  useful, because it allows you to, e.g., use blame to investigate why
+  some line, which you have already removed, was added in the first
+  place.
+
+  But if you want to make further changes to already staged changes,
+  you of course instead need to go to the respective file in the
+  worktree.  The command ~magit-diff-visit-worktree-file~ was created
+  for that purpose, and it is strongly recommend that you make use of
+  that command, even if you initially find it inconvenient having to
+  remember to use ~C-<return>~ instead of ~RET~ in this case.
+
+  While discouraged, you can alternatively set this option to ~t~,
+  which causes ~magit-diff-visit-file~ itself to go to the file in the
+  worktree, even when invoked from within a hunk of staged changes.
+  If you do that, you lose the ability to instantly go to lines you
+  have already removed.
+
 - User Option: magit-diff-visit-previous-blob ::
 
   This option controls whether ~magit-diff-visit-file~ visits the
diff --git a/docs/magit.texi b/docs/magit.texi
index 76938a67e79..2cf90b00487 100644
--- a/docs/magit.texi
+++ b/docs/magit.texi
@@ -4821,6 +4821,41 @@ These commands behave like the respective commands 
described above,
 except that they display the blob or file in another window or frame.
 @end deffn
 
+@defopt magit-diff-visit-prefer-worktree
+This option controls whether @code{magit-diff-visit-file} always visits the
+respective file in the worktree, when invoked anywhere from within a
+hunk of staged or unstaged changes.
+
+By default @code{magit-diff-visit-file} does not do that.  Instead it
+behaves for staged and unstaged changes as it does for committed
+changes, by visiting a blob from the old/left or new/right side,
+depending on whether point is on a removed line or not.
+
+For staged changes the old side is the blob from HEAD and the right
+side is the blog from the index.  For unstaged changes the left side
+is the blob from the index (if there are any changes in the index
+for that file, else the blob from HEAD), and the right side is the
+file in the worktree.
+
+Being able to jump to HEAD or the index from a removed line is very
+useful, because it allows you to, e.g., use blame to investigate why
+some line, which you have already removed, was added in the first
+place.
+
+But if you want to make further changes to already staged changes,
+you of course instead need to go to the respective file in the
+worktree.  The command @code{magit-diff-visit-worktree-file} was created
+for that purpose, and it is strongly recommend that you make use of
+that command, even if you initially find it inconvenient having to
+remember to use @code{C-<return>} instead of @code{RET} in this case.
+
+While discouraged, you can alternatively set this option to @code{t},
+which causes @code{magit-diff-visit-file} itself to go to the file in the
+worktree, even when invoked from within a hunk of staged changes.
+If you do that, you lose the ability to instantly go to lines you
+have already removed.
+@end defopt
+
 @defopt magit-diff-visit-previous-blob
 This option controls whether @code{magit-diff-visit-file} visits the
 previous blob when invoked with point on a removed line.
diff --git a/lisp/magit-diff.el b/lisp/magit-diff.el
index 3b2007cb152..36b785c1f8a 100644
--- a/lisp/magit-diff.el
+++ b/lisp/magit-diff.el
@@ -544,6 +544,26 @@ log's file filter is always honored."
 
 ;;;; Visit Commands
 
+(defcustom magit-diff-visit-prefer-worktree nil
+  "Whether `magit-diff-visit-file' prefers visiting the worktree file.
+
+By default `magit-diff-visit-file' does not do that.  Instead it
+behaves for staged and unstaged changes as it does for committed
+changes, by visiting a blob from the old/left or new/right side,
+depending on whether point is on a removed line or not.
+
+Setting this to nil, causes `magit-diff-visit-file' to always go to
+the file in the worktree when invoked from anywhere within a staged
+or unstaged change.
+
+It is strongly recommended that instead of changing the value of
+this option, you use the command `magit-diff-visit-worktree-file',
+which was created for that very purpose.  See the description of
+this option in the manual for an explanation."
+  :package-version '(magit . "4.4.0")
+  :group 'magit-diff
+  :type 'boolean)
+
 (defcustom magit-diff-visit-previous-blob t
   "Whether `magit-diff-visit-file' may visit the previous blob.
 
@@ -552,12 +572,11 @@ When this is t (the default) and point is on a removed 
line, then
 which still has that line, instead of going to the new/right blob,
 which removes that line.
 
-Setting this to nil, causes `magit-diff-visit-file' always goes to
+Setting this to nil, causes `magit-diff-visit-file' to always go to
 the new/right blob, even when point is on a removed line.  This is
-strongly discouraged.  Instead place the cursor on an added or
-context line, or on the heading, if you want to visit the new/right
-side.  That way you don't lose the ability to visit the old/left
-side."
+very strongly discouraged.  Instead place the cursor anywhere else
+within the hunk but on a removed line, if you want to visit the new
+side.  That way you don't lose the ability to visit the old side."
   :package-version '(magit . "2.9.0")
   :group 'magit-diff
   :type 'boolean)
@@ -1619,7 +1638,7 @@ is about, use \
 
 In the past \\`<return>' (this command) used to go to the file in the
 worktree, if point is on an added or context line of a diff showing
-staged changes.  Set `magit-diff-visit-avoid-index' to t to restore
+staged changes.  Set `magit-diff-visit-prefer-worktree' to t to restore
 that behavior, but note that doing so makes the behavior inconsistent
 and you would give up on the ability to visit the index blob.  If you
 already use \\[magit-diff-visit-worktree-file] to jump to the live \
@@ -1630,19 +1649,28 @@ changes.
 This command only works when point is inside a diff; elsewhere use
 `magit-find-file'."
   (interactive "P")
-  (magit-diff-visit-file--internal nil (and other-window t)))
+  (magit-diff-visit-file--internal
+   (and magit-diff-visit-prefer-worktree
+        (memq (magit-diff--dwim) '(staged unstaged)))
+   (and other-window t)))
 
 (defun magit-diff-visit-file-other-window ()
   "From a diff visit a version of the file at point in another window.
 Like `magit-diff-visit-file' but always display in another window."
   (interactive)
-  (magit-diff-visit-file--internal nil #'switch-to-buffer-other-window))
+  (magit-diff-visit-file--internal
+   (and magit-diff-visit-prefer-worktree
+        (memq (magit-diff--dwim) '(staged unstaged)))
+   #'switch-to-buffer-other-window))
 
 (defun magit-diff-visit-file-other-frame ()
   "From a diff visit a version of the file at point in another frame.
 Like `magit-diff-visit-file' but always display in another frame."
   (interactive)
-  (magit-diff-visit-file--internal nil #'switch-to-buffer-other-frame))
+  (magit-diff-visit-file--internal
+   (and magit-diff-visit-prefer-worktree
+        (memq (magit-diff--dwim) '(staged unstaged)))
+   #'switch-to-buffer-other-frame))
 
 ;;;;; Worktree Variants
 

Reply via email to