0

I have below function to compare perforce versions.
The function runs fine, only I must quit the version compare software to keep typing words or switch to anther window. Basically gvim stops responding after compare software starts.
Tried adding '\&' to the command end, but it's not helping.

function! P4_Diff (...)
   if ! exists('a:1')
      let P4_Diff_Log = system('p4 diff '.expand("%").' \&')
      echo P4_Diff_Log
   else
      if a:1 =~# '\d\+$'
         let P4_Diff_Log = system('p4 diff '.expand("%").'\#'.a:1.' \&')
         echo P4_Diff_Log
      else
         echo "Usage: P4_Diff (VersionNumber)
      endif
   endif
endfunction   
  • 1
    :help job_start() or even :help :terminal There are also some plugins built around "jobs"-interface to simplify async execution. – Matt Aug 1 at 10:49
  • 1
    Why are you escaping the &? It doesn't mean anything to Vim. It should work if you fix that. (Though you're probably not going to get diff output in your variable doing this.) – B Layer Aug 2 at 2:27
0

If you're spawning an external command that will open a separate window and run on its own (for example, a graphical diff tool), and if you don't need to read what the tool is writing on its standard output (which is the usual case, if you're running an external GUI command), you can simplify this to use :! instead of system(), which will make it easier to run the command in background.

This should do it:

function! P4_Diff (...)
  if ! exists('a:1')
    silent execute '!p4 diff '.shellescape(expand("%")).' &'
    redraw!
  elseif a:1 =~# '\v^\d+$'
    silent execute '!p4 diff '.shellescape(expand("%").'#'.a:1).' &')
    redraw!
  else
    echo "Usage: P4_Diff (VersionNumber)"
  endif
endfunction
  • 1
    Dude didn't say anything about a GUI command. Presumably you knew perforce diff was GUI app...? – B Layer Aug 2 at 18:12
  • @BLayer There was a subtle hint in "I must quit the version compare software to keep typing words or switch to anther window." I've used Perforce before and I recall its diff being weird... I think I had to set some environment variables so it would just give me a unified diff on the terminal and nothing else... Spawning a GUI diff tool is definitely not out of what I'd expect from Perforce :-) – filbranden Aug 2 at 18:29
  • 1
    I see. In my limited (phew) time using Perforce I never touched anything on the command line so I assumed this was a text-based diff. Oh well. – B Layer Aug 2 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.