whiteinge / diffconflicts

A better Vimdiff Git mergetool
BSD 3-Clause "New" or "Revised" License
396 stars 20 forks source link

Using diff-specific configuration in .vimrc #27

Open danielcarr opened 1 year ago

danielcarr commented 1 year ago

Thanks for this 😊

I'd like to set up some specific configuration options (eg changing the colour scheme, remapping :diffget) when resolving git conflicts with diffconflicts.

For regular vimdiff, I have if &diff. I only half know what I'm doing in vim, so I tried changing that to if &diff || exists("g:loaded_diffconflicts"), but it didn't work.

Regarding remapping, the vimdiff configuration I have (which I must have copy-pasted from somewhere) has

nnoremap <buffer> <leader>< :diffget RE<cr>]c

(etc), but when I run that while using this plugin, I get an error that there's no such buffer (if I call :diffget without a buffer specified, that works, but it seems like that would be less robust). I can't work out from the plugin code what the buffer should be called (it looks like REMOTE, but that didn't work either).

Can you advise me on how to set this up? Ideally I'd like to have one configuration block for both vimdiff and diffconflicts, if that's possible (but if it isn't, I'd still like to set up diffconflicts automatically when running git mergetool).

whiteinge commented 1 year ago

Interesting use case. Your conditional looks right but my guess is that is getting run before diffconflicts is able to do its thing. You can get it to run after diffconflicts via an autocommand but for your use you'd want an additional check so the configuration is only set once:

fu! SetDiffSettings()
    if exists("b:didSetDiffSettings")
        return
    else
        let b:didSetDiffSettings = 1
    endif

    echom "Hello!" 
endfu

au! DiffUpdated * if &diff && exists("g:loaded_diffconflicts") | call SetDiffSettings() | endif

You could call that same function in your existing if &diff conditional too so you've just got the one place.


As for the mapping: The RE in the map above is assuming there's a buffer with REMOTE in the name, but in diffconflicts the buffer is called RCONFL. However that still won't work because I'm marking that buffer as not listed.

My reasoning was that's just a temporary buffer that will be thrown away so why clutter the buffer list. But I didn't realize that would prevent :diffget RCONFL until you pointed it out. diffconflicts will only ever create the two windows so using diffget without an argument should be bulletproof, but I'd be happy to change that to a normal, listed buffer if you feel strongly about it.

diffconflicts creates a new RCONFL file because it can optionally display the REMOTE version of the file. If you wanted to hard-code that buffer name in your mapping then you'd need to account for that difference and have separate mappings for vimdiff and diffconflicts, or make that buffer name conditional somehow.

Given your comment in #26 it's worth noting that hideResolved does use REMOTE for the other file name since it overwrites the contents of that file instead of creating a new one.