Re: How can a script know if we're running without X ?

2007-03-14 Thread hermitte
A.J.Mechelynck [EMAIL PROTECTED] wrote:
 The ps solution is difficult to use, because there may be several instances
 of Vim running in parallel (whose prognames might or might not be different)
 and I want to make sure to access the current instance: so I would have to
 know the process ID of the current Vim and I don't know how to do that.
 Using system('ls -l /proc/self') wouldn't work, because that would return
 the PID of the ls process called by a subshell called by Vim.

libcallnr(/usr/lib/libc.so, getpid, )

should do the trick on most *nix flavours.
From here, it should be possible to play with /proc related utilities.

 Maybe I'found something... or have I?

 (first try omitted, it didn't work)
 (second try is better)
 (third try:)

  (try to) detect whether we have clipboard and X
 if has('clipboard')
   let x = @+
   let @+ = '--' . x
   redir @
   silent reg
   redir END
   let @+ = x
   unlet x
   let clipboard_present = (@ =~ '^+ ')
 else
   let clipboard_present = 0
 endif
 let X_available = has('x11')  clipboard_present

 The above would fail in a Vim compiled with X support but without clipboard
 support. I think that that risk is negligible. Do you (or does anyone) see
 other cases where the above algorithm would fail? It relies on the fact that
 when the clipboard is not available (for whatever reason: not compiled-in,
 -X, or terminal with no X access) the :reg command never lists the +
 register, not even if we just yanked a nonempty value into it.

Just in case it may help, this code returns 0 in every situation (gvim, vim, vim
-X) in my config (Solaris custom build)
However it seems tied to the fact the ^ prevents '+' from being matched --
while (@ =~ \n.'+ ') is non null. With this new regex, I always end up with
1.

 % --
VIM - Vi IMproved 7.0 (2006 May 7, compiled Nov 23 2006 16:11:47)
Included patches: 1-168
Compiled by luc
Normal version with X11-Motif GUI.  Features included (+) or not (-):
-arabic +autocmd +balloon_eval +browse +builtin_terms +byte_offset +cindent
+clientserver +clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
+cryptv -cscope +cursorshape +dialog_con_gui +diff +digraphs -dnd -ebcdic
-emacs_tags +eval +ex_extra +extra_search -farsi +file_in_path +find_in_path
+folding -footer +fork() -gettext -hangul_input -iconv +insert_expand +jumplist
-keymap -langmap +libcall +linebreak +lispindent +listcmds +localmap +menu
+mksession +modify_fname +mouse +mouseshape -mouse_dec -mouse_gpm -mouse_jsbterm
-mouse_netterm +mouse_xterm -multi_byte +multi_lang -mzscheme +netbeans_intg
-osfiletype +path_extra +perl +postscript +printer -profile -python +quickfix
+reltime -rightleft -ruby +scrollbind +signs +smartindent -sniff +statusline
-sun_workshop +syntax +tag_binary +tag_old_static -tag_any_white -tcl +terminfo
+termresponse +textobjects +title +toolbar +user_commands +vertsplit
+virtualedit +visual +visualextra +viminfo +vreplace +wildignore +wildmenu
+windows +writebackup +X11 +xfontset +xim +xsmp_interact +xterm_clipboard
-xterm_save
[...]
Compilation: cc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_MOTIF 
-I/usr/dt/include   -g-I/usr/perl5/5.00503/sun4-solaris/CORE
Linking: cc -L/usr/dt/lib -R /usr/dt/lib   -o vim  -lXmu -lXext -lXm -lSM -lICE
-lXt -lX11 -ltermlib -R /usr/perl5/5.00503/sun4-solaris/CORE 
/usr/perl5/5.00503/sun4-sol
aris/auto/DynaLoader/DynaLoader.a -L/usr/perl5/5.00503/sun4-solaris/CORE -lperl
-lsocket -lnsl -ldl -lm
 % --

HTH,

--
Luc Hermitte


Re: How can a script know if we're running without X ?

2007-03-14 Thread A.J.Mechelynck

[EMAIL PROTECTED] wrote:

A.J.Mechelynck [EMAIL PROTECTED] wrote:

The ps solution is difficult to use, because there may be several instances
of Vim running in parallel (whose prognames might or might not be different)
and I want to make sure to access the current instance: so I would have to
know the process ID of the current Vim and I don't know how to do that.
Using system('ls -l /proc/self') wouldn't work, because that would return
the PID of the ls process called by a subshell called by Vim.


libcallnr(/usr/lib/libc.so, getpid, )

should do the trick on most *nix flavours.

From here, it should be possible to play with /proc related utilities.



Maybe I'found something... or have I?

(first try omitted, it didn't work)
(second try is better)
(third try:)

 (try to) detect whether we have clipboard and X
if has('clipboard')
let x = @+
let @+ = '--' . x
redir @
silent reg
redir END
let @+ = x
unlet x
let clipboard_present = (@ =~ '^+ ')
else
let clipboard_present = 0
endif
let X_available = has('x11')  clipboard_present

The above would fail in a Vim compiled with X support but without clipboard
support. I think that that risk is negligible. Do you (or does anyone) see
other cases where the above algorithm would fail? It relies on the fact that
when the clipboard is not available (for whatever reason: not compiled-in,
-X, or terminal with no X access) the :reg command never lists the +
register, not even if we just yanked a nonempty value into it.


Just in case it may help, this code returns 0 in every situation (gvim, vim, vim
-X) in my config (Solaris custom build)
However it seems tied to the fact the ^ prevents '+' from being matched --
while (@ =~ \n.'+ ') is non null. With this new regex, I always end up with
1.


Well, I dodn't test well enough. Here's a new snippet:

 (try to) detect whether we have clipboard and X
if has('clipboard')
function TestForX()
let x = @+
let @+ = '--' . x
redir @
silent reg
redir END
let @+ = x
  unlet x
return (@ =~ '\n+ ')
endfunction
else
function TestForX()
return 0
endfunction
endif
augroup vimrclocal
au VimEnter *
\   let clipboard_present = TestForX()
\ | let X_available = has('x11')  clipboard_present
augroup END

 and further down in the vimrc

 Define these mappings only if we have access to a clipboard
augroup vimrclocal
au VimEnter *
\ if clipboard_present
\ | map F4  :$put +CR
\ | map S-F4:0put +CR
\ | imapF4  C-O:$put +CR
\ | imapS-F4C-O:0put +CR
\ | endif
augroup END


I didn't succeed to define the variables when sourcing the vimrc, apparently 
in gvim it only works after the GUI has started. Previously I had tested it 
only in various versions of console Vim with and without -X.




Best regards,
Tony.
--
Who's on first?


Re: How can a script know if we're running without X ?

2007-03-14 Thread Dimitar
* A.J.Mechelynck [EMAIL PROTECTED] [070314 07:50]:
 How can a Vim script know if we're running without an X connection?

What about if has('gui') ?

 
 Of course, some cases are obvious, such as
 
   if has('unix')  !has('x11')
 
 meaning we're on Unix with no X11 support compiled-in.
 
 But what about an X-enabled Vim running in console mode, either with the -X 
 command-line switch, or in a terminal 
 with no access to an X server?
 
 For instance, I might want to map the following
 
   :map S-F5 ccC-R+
 
 in my vimrc, to replace the current line with the clipboard. However that 
 mapping should not be enabled if we have 
 no access to the clipboard. So I wrap it in
 
   if has(clipboard)  (term != linux)
   :map S-F5 ccC-R+
   :imap S-F5 C-OccC-R+
   endif
 
 which takes care of two cases:
 - running with no clipboard support compiled-in
 - running in the (non-X) linux console (aka /dev/tty)
 It doesn't take care, however, of the case when an X-enabled Vim was started 
 as vim -X in an xterm. Is there a 
 way to check for that in vimscript?
 
 
 Best regards,
 Tony.
 -- 
 The government [is] extremely fond of amassing great quantities of
 statistics.  These are raised to the _nth degree, the cube roots are
 extracted, and the results are arranged into elaborate and impressive
 displays.  What must be kept ever in mind, however, is that in every
 case, the figures are first put down by a village watchman, and he puts
 down anything he damn well pleases.
   -- Sir Josiah Stamp


Re: How can a script know if we're running without X ?

2007-03-14 Thread A.J.Mechelynck

Dimitar wrote:

* A.J.Mechelynck [EMAIL PROTECTED] [070314 07:50]:

How can a Vim script know if we're running without an X connection?


What about if has('gui') ?


No. This wouldn't distinguish a version -gui +x11 +clipboard +clientserver 
running in a non-x console (with no clipboard and no clientserver facilities) 
from the same version running in an xterm (where those facilities are available).





Of course, some cases are obvious, such as

if has('unix')  !has('x11')

meaning we're on Unix with no X11 support compiled-in.

But what about an X-enabled Vim running in console mode, either with the -X command-line switch, or in a terminal 
with no access to an X server?


For instance, I might want to map the following

:map S-F5 ccC-R+

in my vimrc, to replace the current line with the clipboard. However that mapping should not be enabled if we have 
no access to the clipboard. So I wrap it in


if has(clipboard)  (term != linux)
:map S-F5 ccC-R+
:imap S-F5 C-OccC-R+
endif

which takes care of two cases:
- running with no clipboard support compiled-in
- running in the (non-X) linux console (aka /dev/tty)
It doesn't take care, however, of the case when an X-enabled Vim was started as vim -X in an xterm. Is there a 
way to check for that in vimscript?



Best regards,
Tony.


Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
111. You and your friends get together regularly on IRC, even though
 all of you live in the same city.



How can a script know if we're running without X ?

2007-03-13 Thread A.J.Mechelynck

How can a Vim script know if we're running without an X connection?

Of course, some cases are obvious, such as

if has('unix')  !has('x11')

meaning we're on Unix with no X11 support compiled-in.

But what about an X-enabled Vim running in console mode, either with the -X 
command-line switch, or in a terminal with no access to an X server?


For instance, I might want to map the following

:map S-F5 ccC-R+

in my vimrc, to replace the current line with the clipboard. However that 
mapping should not be enabled if we have no access to the clipboard. So I wrap 
it in


if has(clipboard)  (term != linux)
:map S-F5 ccC-R+
:imap S-F5 C-OccC-R+
endif

which takes care of two cases:
- running with no clipboard support compiled-in
- running in the (non-X) linux console (aka /dev/tty)
It doesn't take care, however, of the case when an X-enabled Vim was started 
as vim -X in an xterm. Is there a way to check for that in vimscript?



Best regards,
Tony.
--
The government [is] extremely fond of amassing great quantities of
statistics.  These are raised to the _nth degree, the cube roots are
extracted, and the results are arranged into elaborate and impressive
displays.  What must be kept ever in mind, however, is that in every
case, the figures are first put down by a village watchman, and he puts
down anything he damn well pleases.
-- Sir Josiah Stamp


Re: How can a script know if we're running without X ?

2007-03-13 Thread Cameron Simpson
On 13Mar2007 23:29, A.J.Mechelynck [EMAIL PROTECTED] wrote:
| How can a Vim script know if we're running without an X connection?
| Of course, some cases are obvious, such as
|   if has('unix')  !has('x11')
| meaning we're on Unix with no X11 support compiled-in.
| 
| But what about an X-enabled Vim running in console mode, either with the -X 
| command-line switch, or in a terminal with no access to an X server?

Examine the $DISPLAY environment variable. If non-empty, you have a X11
display.
-- 
Cameron Simpson [EMAIL PROTECTED] DoD#743
http://www.cskk.ezoshosting.com/cs/

...when my mood gets too hot and I find myself wandering beyond control I pull
out my motor-bike and hurl it at top speed through these unfit roads for hour
after hour. My nerves are jaded and gone near dead, so that nothing less than
hours of voluntary danger will prick them into life... - T.E. Lawrence 1923


Re: How can a script know if we're running without X ?

2007-03-13 Thread A.J.Mechelynck

Cameron Simpson wrote:

On 13Mar2007 23:29, A.J.Mechelynck [EMAIL PROTECTED] wrote:
| How can a Vim script know if we're running without an X connection?
| Of course, some cases are obvious, such as
|   if has('unix')  !has('x11')
| meaning we're on Unix with no X11 support compiled-in.
| 
| But what about an X-enabled Vim running in console mode, either with the -X 
| command-line switch, or in a terminal with no access to an X server?


Examine the $DISPLAY environment variable. If non-empty, you have a X11
display.


$DISPLAY means Vim was started from a shell running within X11. It doesn't 
mean Vim is aware of that X11 server. In particular, if Vim was started as 
vim -X in an xterm, $DISPLAY will be nonempty but attempts to read or write 
the clipboard or to use the +clientserver feature (both of which rely on X 
functions other than reading the keyboard or wtiting to the display) wouldn't 
work, even if they don't give an error (for instance, in that case reading @+ 
or writing to it gives no error, but the keyboard is neither read nor written).


For more clarification, please read the _whole_ of my previous post.


Best regards,
Tony.


Re: How can a script know if we're running without X ?

2007-03-13 Thread Luc Hermitte
Hello,

* On Tue, Mar 13, 2007 at 11:29:28PM +0100, A.J.Mechelynck [EMAIL PROTECTED] 
wrote:
 How can a Vim script know if we're running without an X connection?
 
 Of course, some cases are obvious, such as
 
   if has('unix')  !has('x11')
 
 meaning we're on Unix with no X11 support compiled-in.
 
 But what about an X-enabled Vim running in console mode, either with
 the -X command-line switch, or in a terminal with no access to an X
 server?
 [...]
 It doesn't take care, however, of the case when an X-enabled Vim was 
 started as vim -X in an xterm. Is there a way to check for that in 
 vimscript?

What about checking whether .gvimrc is sourced or not?
Testing also for has('x11') could be a way to be sure vim is compiled
for x11, and running with an X connection.

Reading the documentation from |.gvimrc|, it seems there are a few other
way to test for the fact you are running with graphics support.

Unless you want to also support vim running in console, and with X-term
support [1], this should do it. Otherwise, what about testing the
options vim was launched with (thanks to a system('ps
-relevant-options')) ?

HTH,


[1] I don't know whether it is relevant or not.
-- 
Luc Hermitte
http://hermitte.free.fr/vim/


Re: How can a script know if we're running without X ?

2007-03-13 Thread A.J.Mechelynck

Luc Hermitte wrote:

Hello,

* On Tue, Mar 13, 2007 at 11:29:28PM +0100, A.J.Mechelynck [EMAIL PROTECTED] 
wrote:

How can a Vim script know if we're running without an X connection?

Of course, some cases are obvious, such as

if has('unix')  !has('x11')

meaning we're on Unix with no X11 support compiled-in.

But what about an X-enabled Vim running in console mode, either with
the -X command-line switch, or in a terminal with no access to an X
server?
[...]
It doesn't take care, however, of the case when an X-enabled Vim was 
started as vim -X in an xterm. Is there a way to check for that in 
vimscript?


What about checking whether .gvimrc is sourced or not?
Testing also for has('x11') could be a way to be sure vim is compiled
for x11, and running with an X connection.

Reading the documentation from |.gvimrc|, it seems there are a few other
way to test for the fact you are running with graphics support.

Unless you want to also support vim running in console, and with X-term
support [1], this should do it. Otherwise, what about testing the
options vim was launched with (thanks to a system('ps
-relevant-options')) ?

HTH,


[1] I don't know whether it is relevant or not.


I'm on Linux, and I definitely want to support Vim running in console. Console 
Vim may access the clipboard and clientserver features but only if the 
following conditions are all fulfilled:

- compiled with +x11
- compiled with either +clipboard or +clientserver
- running in an X11 terminal emulator (xterm, konsole, gnome-terminal, etc.), 
not in a true (non-X) console

- the -X switch was not used on the command-line.

The ps solution is difficult to use, because there may be several instances of 
Vim running in parallel (whose prognames might or might not be different) and 
I want to make sure to access the current instance: so I would have to know 
the process ID of the current Vim and I don't know how to do that. Using 
system('ls -l /proc/self') wouldn't work, because that would return the PID of 
the ls process called by a subshell called by Vim.


system('ps -fC ' . v:progname)

will narrow the problem somewhat; but if the result is

UIDPID  PPID  C STIME TTY  TIME CMD
root 24547  4008  0 01:42 pts/500:00:00 vim
root 24919  3971  0 02:40 pts/200:00:00 vim -X
root 24929 25532  1 02:43 tty2 00:00:00 vim

then what? Are we 24547 (without the -X switch), 24919 (with the switch) or 
24929 (without the switch, but in /dev/tty2 which cannot access X)?




Maybe I'vound something... or have I?

(first try omitted, it didn't work)
(second try is better)
(third try:)

 (try to) detect whether we have clipboard and X
if has('clipboard')
let x = @+
let @+ = '--' . x
redir @
silent reg
redir END
let @+ = x
unlet x
let clipboard_present = (@ =~ '^+ ')
else
let clipboard_present = 0
endif
let X_available = has('x11')  clipboard_present

The above would fail in a Vim compiled with X support but without clipboard 
support. I think that that risk is negligible. Do you (or does anyone) see 
other cases where the above algorithm would fail? It relies on the fact that 
when the clipboard is not available (for whatever reason: not compiled-in, -X, 
or terminal with no X access) the :reg command never lists the + register, 
not even if we just yanked a nonempty value into it.


The above clobbers the unnamed register but anyway, I never expect it to be 
conserved by long-timed operations such as restarting Vim or sourcing a Vim 
script. I expect the following results from the above code snippet:


X_available == 0
clipboard_present == 1
non-X with clipboard (e.g. Windows version)

X_available == 1
clipboard_present == 1
Vim is connected to X server (gvim, or Console Vim with X)

X_available == 1
clipboard_present == 0
never

X_available == 0
clipboard_present == 0
no clipboard (for whatever reason)


Best regards,
Tony.
--
Pittsburgh Driver's Test

(8) Pedestrians are

(a) irrelevant.
(b) communists.
(c) a nuisance.
(d) difficult to clean off the front grille.

The correct answer is (a).  Pedestrians are not in cars, so they are
totally irrelevant to driving; you should ignore them completely.