Hi,

on most systems vim forks when gui mode is entered. On OS X this is  
not implemented. As a workaround, gvim is usually launched in the  
background ('gvim &') from a separated script, so it can survive if  
its spawning shell is closed. This has the undesirable side effect  
that you can't pipe stdin to the shell script (at least I couldn't get  
it to work):

     $ cat test.sh
     #!/bin/bash
     /Applications/_Nico/Vim.app/Contents/MacOS/Vim -g $* &
     $ ls | ./test.sh

After that, vim claims that it's reading from stdin, but nothing shows  
up in vim. If I remove the '&' in the script, piping works fine, but  
even after `^Z; bg` vim dies if I close the spawning shell.  
Furthermore, in whatever way you tweak the script it's not possible to  
make vim's '-f' ("don't fork") flag work, which you need for example  
if you set gvim as the EDITOR for version control programs.

To summarize:
1. If started with '-g', vim should become independent of its spawning  
shell and still support reading from stdin
2. If started with '-f' and '-g', vim should _not_ do fork (but still  
support reading from stdin)
Oh, and if started without '-g' and the doing `:gui`, things should  
still kind of work.

As far as I see it, the only way to do all this is to add forking  
support to vim's code. This is a bit difficult, as OS X doesn't really  
support fork() in the way other unices do (see CAVEATS here: 
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/fork.2.html
 
  "All APIs, including global data symbols, in any framework or  
library should be assumed to be unsafe after a fork() unless  
explicitly documented to be safe or async-signal safe. If you need to  
use these frameworks in the child process, you must exec. In this  
situation it is reasonable to exec yourself." ), so I didn't add  
complete support.

Here's what the attached patch does: If vim is started via '-g', it  
forks and exec()s a new (!) vim instance in the child process. But if  
the gui is started via `:gui`, no forking is done. I did some basic  
testing and it seems to work, but I'm not very proficient in this kind  
of code, so I'm thankful for comments.

This should work for all vim guis on OS X, but I tested it only with  
MacVim.

If there's a simpler way to achieve the same thing, I'd be happy to  
hear about it ;-)

Bye,
Nico

ps: Bjorn: If I do `ls | .../Vim -` followed by `:gui` in vim, and  
finally cmd-q, I get a single
     2007-11-23 16:33:14.603 MacVim[25537:10b] *** -[NSMachPort  
handlePortMessage:]: dropping incoming DO message because the  
connection or ports are invalid
message. I get this message only if I start in text mode, let it read  
from stdin, and then switch over to gui mode (and close the window).  
Any ideas why this is?
pps: This deprecates the '&' at the end of most of the gvim and mvim  
scripts on OS X out there.



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Attachment: mac_fork.patch
Description: Binary data



Raspunde prin e-mail lui