views:

107

answers:

1

Because you (lovely) people are always so curious about posters' original intents, here's mine:

If I'm on a Mac and have a GUI (as opposed to, say, being on an ssh session), I want to set my $EDITOR to mate_wait. (And go with vim otherwise.)

And, you have an answer for that. I do too. It even works. Here. Sometimes.

So I want you to fiercely scrutinize it:

Skip intro

I can tell that I'm on a Mac by checking:

[ `uname` = 'Darwin' ]

And I think I can sort of tell that I have a GUI by checking:

[ "$TERM_PROGRAM" = 'Apple_Terminal' ]
  # or
[ "$DISPLAY" ]

Now, it's theoretically possible that I have an Aqua-less OpenDarwin setup running X11. It's also possible that I'm running fully lickable Mac GUI, yet using another terminal application.

And then there's the mind-bending possibility that I'm running xterm within Apple's X11 running on top of the OS X GUI. In which case I'd still want mate_wait as $EDITOR.

For OCD's sake, I'd like my checks to be as precise as possible.

So, please, un-reckless-fy my code.

+3  A: 

I'll take a stab at this, mind you it's probably incomplete.
Condition 1: Am I local

[ -z "$SSH_CLIENT" ]

Condition 2: Am I remote

[ -n "$SSH_CLIENT" ]

Condition 3: Do I have the lickable mac GUI

[ -n "`ps -fe | grep '[W]indowServer'`" ]

So putting these together:

if [ -z "$SSH_CLIENT" -a -n "`ps -fe | grep '[W]indowServer'`" ]; then
     EDITOR="matew"
else
     EDITOR=vim
fi

You will need a script called 'matew' that simply does:

#!/bin/sh
exec mate -w "$*"

as EDITOR is only expected to be a direct command, and won't work if it's 'mate -w' (at least on snow leopard).

Petesh
mate -w actually works (or so I remember), but you can symlink mate as mate_wait and when called as such it'll assume -w
kch
can you assume that you're local just because you're not SSH-ed? (I guess telnet and all that)
kch
as for grepping for the WindowServer, well, I'm trying to find a problem with it, but I guess if you know you're local, then you're good.
kch
Unfortunately, WindowServer's there whether or not you're in its session, so I don't think this is a useful test.
Gordon Davisson
That's true, but what are the odds that you're local, and running WindowServer and you're not logged on? Unless you're on a serial connection, I can't see how this would affect interactive logons
Petesh
How about `if [ -z "$SSH_CLIENT" ] then` ... That tests whether there's an instance of loginwindow with the same effective UID as you, which should only be the case if there's a GUI login session for your user, which excludes cases like where you've su'd to another user.
Gordon Davisson