Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: choice of commands in pick-web-browser



Minor addition to pick-web-browser to allow you to configure the
command used to start each given browser.  You might think at this
point it would be simpler to invoke the browser directly, but the
point of this is to be part of a MIME system that can be configured
entirely with styles.

I also noted it didn't detect running variants of Mozilla because the
executable is has a -bin tacked on the end.

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.54
diff -u -r1.54 contrib.yo
--- Doc/Zsh/contrib.yo	18 Dec 2005 20:12:29 -0000	1.54
+++ Doc/Zsh/contrib.yo	7 Mar 2006 21:14:58 -0000
@@ -1346,6 +1346,8 @@
 command to an appropriate viewer.
 
 startitem()
+findex(zsh-mime-setup)
+findex(zsh-mime-handler)
 xitem(tt(zsh-mime-setup [-flv]))
 item(tt(zsh-mime-handler))(
 These two functions use the files tt(~/.mime.types) and tt(/etc/mime.types),
@@ -1510,6 +1512,7 @@
 )
 enditem()
 )
+findex(pick-web-browser)
 item(tt(pick-web-browser))(
 This function is separate from the two MIME functions described above
 and can be assigned directly to a suffix:
@@ -1518,19 +1521,19 @@
 alias -s html=pick-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 is already a browser running which can accept a remote
+It will check if an X Windows display is available, and if so if there
+is already a browser running on the display which can accept a remote
 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 builtin
+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.
 
 Two styles are available to customize the choice of browsers:
-tt(x-browsers) when running under the X Windows System, and
+tt(x-browsers) when running under the X Window System, and
 tt(tty-browsers) otherwise.  These are arrays in decreasing order
-of preference consiting of the command name under which to start the
+of preference consisting of the command name under which to start the
 browser.  They are looked up in the context tt(:mime:) (which may
 be extended in future, so appending `tt(*)' is recommended).  For
 example,
@@ -1540,6 +1543,18 @@
 specifies that tt(pick-web-browser) should first look for a runing
 instance of Opera, Konqueror or Netscape, in that order, and if it
 fails to find any should attempt to start Opera.
+
+In addition, the style tt(command), if set, is used to pick the command
+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:running:$browser:) to open a URL in a browser already
+runing on the current X display.  The escape sequence tt(%b) in the
+style's value will be replaced by the browser, while tt(%u) 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
+Konqueror, tt(firefox -new-tab %u) for Firefox and tt(%b -remote
+"openUrl(%u)") for all others.
 )
 enditem()
 
Index: Functions/MIME/pick-web-browser
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/MIME/pick-web-browser,v
retrieving revision 1.3
diff -u -r1.3 pick-web-browser
--- Functions/MIME/pick-web-browser	19 Feb 2006 21:20:43 -0000	1.3
+++ Functions/MIME/pick-web-browser	7 Mar 2006 21:14:58 -0000
@@ -22,6 +22,8 @@
 emulate -L zsh
 setopt extendedglob cbases nonomatch
 
+zmodload -i zsh/zutil
+
 local -a xbrowsers ttybrowsers
 
 # X Windows browsers which might be running and can accept
@@ -38,7 +40,7 @@
 litc="-_./"
 
 local -a windows remoteargs match mbegin mend
-local url browser
+local url browser command
 
 url=$1
 if [[ -f $url ]]; then
@@ -80,22 +82,31 @@
 
   # Is any browser we've heard of running?
   for browser in $xbrowsers; do
-    if [[ $windows[(I)(#i)$browser] -ne 0 ]]; then
-      if [[ $browser = konqueror ]]; then
-	# kfmclient is less hairy and better supported than direct
-	# use of dcop.  Run kfmclient --commands
-	# for more information.  Note that as konqueror is a fully
-	# featured file manager, this will actually do complete
-	# MIME handling, not just web pages.
-	kfmclient openURL $url ||
-	  dcop $(dcop|grep konqueror) default openBrowserWindow $url
-      elif [[ $browser = firefox ]]; then
-	  # open in new tab: should make this customizable
-	  $browser -new-tab $url
+    # Some browser executables call themselves <browser>-bin
+    if [[ $windows[(I)(#i)$browser(|[.-]bin)] -ne 0 ]]; then
+      if zstyle -s ":mime:browser:running:${browser}:" command command; then
+	# The (q)'s here and below are pure paranoia:  no browser
+	# name is going to include metacharacters, and we already
+	# converted difficult characters in the URL to hex.
+	zformat -f command $command b:${(q)browser} u:${(q)url}
+	eval $command
       else
-	# Mozilla bells and whistles are described at:
-	# http://www.mozilla.org/unix/remote.html
-	$browser -remote "openURL($url)"
+	if [[ $browser = konqueror ]]; then
+	  # kfmclient is less hairy and better supported than direct
+	  # use of dcop.  Run kfmclient --commands
+	  # for more information.  Note that as konqueror is a fully
+	  # featured file manager, this will actually do complete
+	  # MIME handling, not just web pages.
+	  kfmclient openURL $url ||
+	    dcop $(dcop|grep konqueror) default openBrowserWindow $url
+        elif [[ $browser = firefox ]]; then
+	  # open in new tab
+	  $browser -new-tab $url
+        else
+	  # Mozilla bells and whistles are described at:
+	  # http://www.mozilla.org/unix/remote.html
+	  $browser -remote "openURL($url)"
+        fi
       fi
       return
     fi
@@ -104,8 +115,13 @@
   # Start our preferred X Windows browser in the background.
   for browser in $xbrowsers; do
       if eval "[[ =$browser != \\=$browser ]]"; then
-	# The following is to make the job text more readable.
-	eval ${(q)browser} ${(q)url} "&"
+	if zstyle -s ":mime:browser:new:${browser}:" command command; then
+	  zformat -f command $command b:${(q)browser} u:${(q)url}
+	  eval $command "&"
+	else
+	  # The following is to make the job text more readable.
+	  eval ${(q)browser} ${(q)url} "&"
+	fi
 	break
       fi
   done
@@ -113,7 +129,12 @@
   # Start up dumb terminal browser.
   for browser in $ttybrowsers; do
     if eval "[[ =$browser != \\=$browser ]]"; then
-      $browser $url
+      if zstyle -s ":mime:browser:new:${browser}" command command; then
+	zformat -f command $command b:${(q)browser} u:${(q)url}
+	eval $command
+      else
+	$browser $url
+      fi
       break
     fi
   done


-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page still at http://www.pwstephenson.fsnet.co.uk/



Messages sorted by: Reverse Date, Date, Thread, Author