will133 / vim-dirdiff

Vim plugin to diff two directories
335 stars 59 forks source link

Suggestion: enable mappings only while inside dirdiff mode #46

Open greenfoo opened 2 years ago

greenfoo commented 2 years ago

I map <A-j> and <A-k> to :cprevious and :cnext to navigate the "quickfix" list when opened.

I would also like to use these same keys to go to the previous and next file while in dirdiff mode (by setting variables g:DirDiffNextKeyMap and g:DirDiffPrevKeyMap). Unfortunately this does not work because the original ("quickfix") mappings are lost when I do this.

I see two workarounds for this:

  1. Modify the dirdiff plugin to add a new API function that we can query to check whether dirdiff mode is currently enabled or not and then externally set the mapping for <A-j> and <A-k> to execute either :cprevious/:cnext or :DirDiffPrev/:DirDiffNext.
  2. Modify the dirdiff plugin to save and restore the previous mappings when entering/exiting dirdiff mode.

Any thoughts on this?

Thanks!

will133 commented 2 years ago

Can you try using &diff to detect if you are in a diff mode and map something differently?

greenfoo commented 2 years ago

Good idea. I tried it but there are two small problem with this approach:

  1. The window at the bottom (the one listing all files) does not have "&diff" set (so I cannot use the shortcuts from there)
  2. &diff is set everytime there is a diff, even if we are not in dirdiff

But your idea gave me another one: it would be nice to have another buffer specific variable which is only set in those buffers opened by dirdiff. Something like this:

--- dirdiff.vim.ori 2022-05-07 22:10:41.828692192 +0200
+++ dirdiff.vim 2022-05-07 22:47:02.066283682 +0200
@@ -234,6 +234,7 @@
         return
     endif
     silent exe "edit ".s:FilenameDiffWindow
+    let b:dirdiff = 1
     echo "Defining [A] and [B] ... "
     " We then do a substitution on the directory path
     " We need to do substitution of the the LONGER string first, otherwise
@@ -530,17 +531,20 @@
             if s:LastMode == 2
                 call <SID>Drop(previousFileA)
                 silent exec "edit ".s:FilenameA
+                let b:dirdiff = 1
                 diffthis
                 silent exec "bd ".bufnr(previousFileA)

                 call <SID>Drop(previousFileB)
                 silent exec "edit ".s:FilenameB
+                let b:dirdiff = 1
                 diffthis
                 silent exec "bd ".bufnr(previousFileB)
             else
                 let previousFile = (s:LastMode == "A") ? previousFileA : previousFileB
                 call <SID>Drop(previousFile)
                 silent exec "edit ".s:FilenameB
+                let b:dirdiff = 1
                 silent exec "bd ".bufnr(previousFile)
                 diffthis

@@ -550,9 +554,11 @@
         else
             "Open the diff windows
             silent exec "split ".s:FilenameB
+            let b:dirdiff = 1

             " To ensure that A is on the left and B on the right, splitright must be off
             silent exec "leftabove vert diffsplit ".s:FilenameA
+            let b:dirdiff = 1
         endif

         " Go back to the diff window

Then I can do something like this on my .vimrc:

nnoremap <expr> <A-k> b:dirdiff ? ':silent! DirDiffPrev<CR>' : ':cp!<CR>' 
nnoremap <expr> <A-j> b:dirdiff ? ':silent! DirDiffNext<CR>' : ':cn!<CR>' 

...and it works :)

Let me know if you like this approach and I will happily create a PR.