1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-07 11:41:16 +02:00

23759: a few improvements to pick-web-browser

This commit is contained in:
Peter Stephenson 2007-08-14 17:21:05 +00:00
parent 83cbc075ac
commit 91d714752c
3 changed files with 150 additions and 86 deletions

View file

@ -1,5 +1,8 @@
2007-08-14 Peter Stephenson <pws@csr.com> 2007-08-14 Peter Stephenson <pws@csr.com>
* 23759: Doc/Zsh/contrib.yo, Functions/MIME/pick-web-browser:
a few improvements to pick-web-browser.
* 23757: Src/lex.c: suppression correction of command * 23757: Src/lex.c: suppression correction of command
words immediately followed by () or something that looks words immediately followed by () or something that looks
like it's going to be one. like it's going to be one.

View file

@ -1697,40 +1697,76 @@ example(autoload -U pick-web-browser
alias -s html=pick-web-browser) alias -s html=pick-web-browser)
It is provided as an intelligent front end to dispatch a web browser. It is provided as an intelligent front end to dispatch a web browser.
It will check if an X Windows display is available, and if so if there It may be run as either a function or a shell script. The status
is already a browser running on the display which can accept a remote 255 is returned if no browser could be started.
connection. In that case, the file will be displayed in that browser;
you should check explicitly if it has appeared in the running browser's
window. Otherwise, it will start a new browser according to a built-in
set of preferences.
Alternatively, tt(pick-web-browser) can be run as a zsh script. Various styles are available to customize the choice of browsers:
Two styles are available to customize the choice of browsers: startitem()
tt(x-browsers) when running under the X Window System, and item(tt(browser-style))(
tt(tty-browsers) otherwise. These are arrays in decreasing order The value of the style is an array giving preferences in decreasing order
of preference consisting of the command name under which to start the for the type of browser to use. The values of elements may be
startitem()
item(tt(running))(
Use a GUI browser that is already running when an X Window display is
available. The browsers listed in the tt(x-browsers) style are tried
in order until one is found; if it is, the file will be displayed in
that browser, so the user may need to check whether it has appeared.
If no running browser is found, one is not started. Browsers other than
Firefox, Opera and Konqueror are assumed to understand the Mozilla
syntax for opening a URL remotely.
)
item(tt(x))(
Start a new GUI browser when an X Window display is available. Search for
the availability of one of the browsers listed in the tt(x-browsers) style
and start the first one that is found. No check is made for an already
running browser.
)
item(tt(tty))(
Start a terminal-based browser. Search for the availability of one
of the browsers listed in the tt(tty-browsers) style and start the
first one that is found.
)
enditem()
If the style is not set the default tt(running x tty) is used.
)
item(tt(x-browsers))(
An array in decreasing order
of preference of browsers to use when running under the X Window System.
The array consists of the command name under which to start the
browser. They are looked up in the context tt(:mime:) (which may browser. They are looked up in the context tt(:mime:) (which may
be extended in future, so appending `tt(*)' is recommended). For be extended in future, so appending `tt(*)' is recommended). For
example, example,
example(zstyle ':mime:*' x-browsers opera konqueror netscape) example(zstyle ':mime:*' x-browsers opera konqueror firefox)
specifies that tt(pick-web-browser) should first look for a runing specifies that tt(pick-web-browser) should first look for a runing
instance of Opera, Konqueror or Netscape, in that order, and if it instance of Opera, Konqueror or Firefox, in that order, and if it
fails to find any should attempt to start Opera. fails to find any should attempt to start Opera. The default is
tt(firefox mozilla netscape opera konqueror).
In addition, the style tt(command), if set, is used to pick the command )
item(tt(tty-browsers))(
An array similar to tt(x-browsers), except that it gives browsers to
use use when no X Window display is available. The default is
tt(elinks links lynx).
)
item(tt(command))(
If it is set this style is used to pick the command
used to open a page for a browser. The context is used to open a page for a browser. The context is
tt(:mime:browser:new:$browser:) to start a new browser or tt(:mime:browser:new:$browser:) to start a new browser or
tt(:mime:browser:running:$browser:) to open a URL in a browser already tt(:mime:browser:running:$browser:) to open a URL in a browser already
runing on the current X display. The escape sequence tt(%b) in the runing on the current X display, where tt($browser) is the value matched
style's value will be replaced by the browser, while tt(%u) will be in the tt(x-browsers) or tt(tty-browsers) style. The escape sequence
replaced by the URL. If the style is not set, the default for all new tt(%b) in the style's value will be replaced by the browser, while tt(%u)
instances is equivalent to tt(%b %u) and the defaults for using running will be replaced by the URL. If the style is not set, the default for all
new instances is equivalent to tt(%b %u) and the defaults for using running
browsers are equivalent to the values tt(kfmclient openURL %u) for browsers are equivalent to the values tt(kfmclient openURL %u) for
Konqueror, tt(firefox -new-tab %u) for Firefox and tt(%b -remote Konqueror, tt(firefox -new-tab %u) for Firefox, tt(opera -newpage %u)
"openUrl+LPAR()%u+RPAR()") for all others. for Opera, and tt(%b -remote "openUrl+LPAR()%u+RPAR()") for all others.
)
enditem()
) )
enditem() enditem()

View file

@ -20,24 +20,23 @@
# browser instead. # browser instead.
emulate -L zsh emulate -L zsh
setopt extendedglob cbases nonomatch setopt extendedglob cbases nonomatch warncreateglobal
zmodload -i zsh/zutil zmodload -i zsh/zutil
local -a xbrowsers ttybrowsers local -a xbrowsers ttybrowsers
# X Windows browsers which might be running and can accept # X Windows browsers which might be running and can accept
# a remote URL. You can change the order of preference. # a remote URL.
# If none is already running, starts the first in the array.
zstyle -a :mime: x-browsers xbrowsers || zstyle -a :mime: x-browsers xbrowsers ||
xbrowsers=(firefox mozilla netscape opera konqueror) xbrowsers=(firefox mozilla netscape opera konqueror)
# Preferred command line browser. Used if there is on $DISPLAY set. # Preferred command line browsers.
zstyle -a :mime: tty-browsers ttybrowsers || zstyle -a :mime: tty-browsers ttybrowsers ||
ttybrowsers=(links lynx) ttybrowsers=(elinks links lynx)
# Characters in addition to alphanumerics which can appear literally # Characters in addition to alphanumerics which can appear literally
# in a URL. `-' should be the first if it appears, so append others # in a URL. `-' should be the first if it appears, so append others
# to the end. # to the end.
litc="-_./" local litc="-_./"
local -a windows remoteargs match mbegin mend local -a windows remoteargs match mbegin mend
local url browser command local url browser command
@ -63,8 +62,14 @@ if [[ -f $url ]]; then
fi fi
fi fi
local bstyle
local -a bstyles
zstyle -a :mime: browser-styles bstyles || bstyles=(running x tty)
if [[ -n $DISPLAY ]]; then for bstyle in $bstyles; do
case $bstyle in
(running)
[[ -z $DISPLAY ]] && continue
# X Windows running # X Windows running
# Get the name of all windows running; use the internal name, not # Get the name of all windows running; use the internal name, not
@ -73,8 +78,8 @@ if [[ -n $DISPLAY ]]; then
# The nasty but portable version. # The nasty but portable version.
# The nice but non-portable version uses Perl, even though perl # The nice but non-portable version uses Perl, even though perl
# is more portable. # is more portable.
# windows=(${(f)"$(xwininfo -root -all | # windows=(${(f)"$(xwininfo -root -all |
# sed -ne 's/.*".*": ("\(.*\)" ".*").*/\1/p' |sort | uniq)"}) # sed -ne 's/.*".*": ("\(.*\)" ".*").*/\1/p' |sort | uniq)"})
windows=(${(f)"$(xwininfo -root -all | windows=(${(f)"$(xwininfo -root -all |
perl -ne '/.*"(.*)": \("(.*)" "(.*)"\).*/ and $w{$2} = 1; perl -ne '/.*"(.*)": \("(.*)" "(.*)"\).*/ and $w{$2} = 1;
@ -91,7 +96,8 @@ if [[ -n $DISPLAY ]]; then
zformat -f command $command b:${(q)browser} u:${(q)url} zformat -f command $command b:${(q)browser} u:${(q)url}
eval $command eval $command
else else
if [[ $browser = konqueror ]]; then case $browser in
(konqueror)
# kfmclient is less hairy and better supported than direct # kfmclient is less hairy and better supported than direct
# use of dcop. Run kfmclient --commands # use of dcop. Run kfmclient --commands
# for more information. Note that as konqueror is a fully # for more information. Note that as konqueror is a fully
@ -99,19 +105,31 @@ if [[ -n $DISPLAY ]]; then
# MIME handling, not just web pages. # MIME handling, not just web pages.
kfmclient openURL $url || kfmclient openURL $url ||
dcop $(dcop|grep konqueror) default openBrowserWindow $url dcop $(dcop|grep konqueror) default openBrowserWindow $url
elif [[ $browser = firefox ]]; then ;;
(firefox)
# open in new tab # open in new tab
$browser -new-tab $url $browser -new-tab $url
else ;;
(opera)
$browser -newpage $url
;;
(*)
# Mozilla bells and whistles are described at: # Mozilla bells and whistles are described at:
# http://www.mozilla.org/unix/remote.html # http://www.mozilla.org/unix/remote.html
$browser -remote "openURL($url)" $browser -remote "openURL($url)"
fi ;;
esac
fi fi
return return
fi fi
done done
;;
(x)
[[ -z $DISPLAY ]] && continue
# Start our preferred X Windows browser in the background. # Start our preferred X Windows browser in the background.
for browser in $xbrowsers; do for browser in $xbrowsers; do
if eval "[[ =$browser != \\=$browser ]]"; then if eval "[[ =$browser != \\=$browser ]]"; then
@ -122,10 +140,12 @@ if [[ -n $DISPLAY ]]; then
# The following is to make the job text more readable. # The following is to make the job text more readable.
eval ${(q)browser} ${(q)url} "&" eval ${(q)browser} ${(q)url} "&"
fi fi
break return
fi fi
done done
else ;;
(tty)
# Start up dumb terminal browser. # Start up dumb terminal browser.
for browser in $ttybrowsers; do for browser in $ttybrowsers; do
if eval "[[ =$browser != \\=$browser ]]"; then if eval "[[ =$browser != \\=$browser ]]"; then
@ -135,7 +155,12 @@ else
else else
$browser $url $browser $url
fi fi
break return
fi fi
done done
fi ;;
esac
done
# No eligible browser.
return 255