Subject: How can I view a URL listed in an xterm window
Date: 01/28/98

The netscape program has a nifty feature, -remote, that allows you to request an existing netscape window to execute a command. I wrote a small script, url, which just tells netscape to jump to whatever url you pass it. The script is as follows:

	#!/bin/sh
	if ! netscape -remote "openURL($1)" > /dev/null 2>&1
	then
		exec netscape $1 &
	fi
You will notice that if the -remote fails then we just start up a new copy of netscape. I then bound a key in my .twmrc to call this:
	"n"     =       m       : all           : !"xurl &"
You probably noticed that this actually calls xurl. The xurl script is:
	#!/bin/sh
	url $(xpaste)
(The sources to xpaste are included at the bottom of the NSFAQ.) Now when I select a URL on the screen I just hit and netscape automatically displays the selected URL.

A suggested enhancement is to parse the output of xpaste and look for a real URL and just pass that. This would allow you to select a page of text and just tell netscape to go to the first URL found. That is left up to the reader.

Here is the xpaste.c sources:

/*
 * Copyright (c) 1993 Paul Borman
 * All rights reserved.
 *  
 * Permission to redistribute this source code is hereby granted as long as
 * this notice is included in all copies and there is no charge for either
 * the source code or binary.
 * 
 * This code is provided AS IS.
 */

#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>

main(int ac, char **av)
{
    Display *dpy;
    Atom primary;
    Atom string;
    Atom selection;
    Window win;
    unsigned char *data;
    XEvent ev;
    unsigned long nitems;
    unsigned long leftover;
    int actual_format;
    Atom actual_type;

    dpy = XOpenDisplay(NULL);

    if (!dpy) {
	fprintf(stderr, "Could not open display: %s\n", XDisplayName(NULL));
	exit(1);
    }

    primary = XInternAtom(dpy, "PRIMARY", False);
    string  = XInternAtom(dpy, "STRING", False);
    selection = XInternAtom(dpy, "CUT_BUFFER0", False);

    if (!primary) {
	fprintf(stderr, "Failed to intern PRIMARY\n");
	exit(1);
    }
    if (!string) {
	fprintf(stderr, "Failed to intern STRING\n");
	exit(1);
    }
    if (!selection) {
	fprintf(stderr, "Failed to intern CUT_BUFFER0\n");
	exit(1);
    }

    win = DefaultRootWindow(dpy);

    if (XGetWindowProperty(dpy, win, selection, 0L, 10000000L,
                           False, string,
                           &actual_type, &actual_format, &nitems,
                           &leftover, &data) != Success) 
	exit(1);
    write(1, data, nitems * actual_format >> 3);
}