On Sat, Dec 29, 2007 at 05:24:15PM +0100, Giorgio Lando wrote:
> The command should open in firefox the contents of the X selection
> buffer. So xsel gives me the buffer. In the interactive shell, the
> following command works when the buffer consists in an URL:
> 
> firefox `xsel`
> 
> Now I have tried the following line in config.h:
> 
>  { MODKEY,                       XK_o,           spawn,   "exec firefox 
> `xsel`" }, 

You might be able to make this work by squeezing it through bash so that 
bash expands the backticks, something like this (untested):

   { MODKEY,  XK_o, spawn,   "exec bash -c firefox `xsel`" },


> ... and as a last resort I have created  a shell script called
> firefoxterm:
> 
> #! /bin/sh
> firefoxtab `xsel`
> 
> It works from the command line, I am able to bind it with xbindkeys,
> but:
> 
> { MODKEY,                     XK_o,           spawn, "exec firefoxterm" },
> 
> does nothing. The problem is not in the keybinding itself,
> because I am able to bind that combination to other commands without
> problems.

This approach works fine for me - my keybinding is:

   { MODKEY,  XK_o,   spawn, "exec browse CLIPBOARD" },

and my script is attached - note that it is calling opera (and 
optionally iceweasel) instead of firefox - you want to change the 
openlink function.

The script needs some tidying/commenting/improving, but it basically 
does what I need, so I've not fiddled with it recently.  The intention 
is that (if called with argument 'CLIPBOARD') it should open all things 
that look like URLs on the clipboard and ignore anything non-URL.  
Otherwise it opens all URLs given on the command-line, or the current 
dir if nothing specified (I use this if I am at a shell prompt and want 
to open a browser on the current directory - I just use 'browse')


Karl.
-- 
http://mowson.org/karl
#! /bin/sh

###########################################################################
## Function to exit with error message.
## First param is return code, remaining params are lines of error message.
#
Fail() {
   ExitCode=$1
   shift
   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" >&2
   echo "$(basename "$0"): FATAL ERROR" >&2
   while [ $# -gt 0 ]; do
      echo "$1" >&2
      shift
   done
   sleep 2
   TidyUp
   exit $ExitCode
}

###########################################################################
## Function to check a list of desired tools are available.
#
Toolcheck() {
   while [ $# -gt 0 ]; do
      TOOL="$1"
      shift
      echo "Checking '$TOOL'"
      which "$TOOL" >/dev/null 2>/dev/null \
         || Fail 3 "'which' failed for '$TOOL' - can't find this command."
   done
} 

#///////////////////////////////////////////////////////////////
OpenLink () {
   SELF=$(basename $0)

   if [ "$SELF" = 'browse' -o "$SELF" = 'b' ]; then
      opera --nomail --newpage "$1" 2>/dev/null >/dev/null &
   else
      # Something is stopping iceweasel from loading the cmd-line URL
      # It might be to do with the environment, in that a simple
      # invocation of "iceweasel /etc/iceweasel/iceweaselrc" in this 
      # script doesn't work, but doing it from shell cmd-line does
      iceweasel -new-tab "$1" &
   fi
}

#///////////////////////////////////////////////////////////////
OpenAllOnClipboard () {
   STARTEDBROWSER=False
   for TARGET in $(xclip -o | tr ' !|<>,' '\n'); do
   #for TARGET in $(xclip -o); do
   #xclip -o | tr ' !|<>,' '\n' | while read -r TARGET; do
      # Choose anything that looks like it could be a url (contains
      # a '.' with at least one char on each side) or path (starts
      # with a '/' - only absolute paths are reasonable when opening
      # from clipboard - there is no sensible working dir)
      if [ \
        "${TARGET#*?.?}"  != "$TARGET" -o \
        "${TARGET#/*?/?}" != "$TARGET"    \
        ]; then
         # Don't open anything with an '@' in it, since it's probably an email 
address!
         if [ "[EMAIL PROTECTED]" = "$TARGET" ]; then
            OpenLink "$TARGET"
            STARTEDBROWSER=True
         else
            echo Probably email: "$TARGET"
         fi
      else
         echo not URL: "$TARGET"
      fi
   done
   if [ "$STARTEDBROWSER" != "True" ]; then
      echo No URL found, opening browser with nothing
      OpenLink
   fi
}
# old tests for specific things:
#        "${TARGET#http:}" = "$TARGET" -a \
#        "${TARGET#www.}"  = "$TARGET" -a \
#        "${TARGET#file:}" = "$TARGET" -a \
#        "${TARGET%.htm}"  = "$TARGET" -a \
#        "${TARGET%.html}" = "$TARGET"    \

#///////////////////////////////////////////////////////////////

Toolcheck \
   xclip  \
   opera

if [ "$#" = "0" ]; then
   OpenLink "$(pwd)"
elif [ "$1" = "CLIPBOARD" ]; then
   OpenAllOnClipboard
else
   for TARGET in "$@"; do
      OpenLink "$TARGET"
   done
fi

Reply via email to