From 998d8d60cb5b0b1907fde49f3a58a09b228ddd0f Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 21 Aug 2001 18:46:10 +0000 Subject: [PATCH] Whitespace massaging modifications. git-svn-id: https://samskivert.googlecode.com/svn/trunk@309 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- projects/lookuplet/ChangeLog | 6 +++++ projects/lookuplet/NEWS | 6 ++++- projects/lookuplet/src/c/querybox.c | 36 +++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/projects/lookuplet/ChangeLog b/projects/lookuplet/ChangeLog index c3515af5..f98d09f4 100644 --- a/projects/lookuplet/ChangeLog +++ b/projects/lookuplet/ChangeLog @@ -1,3 +1,9 @@ +2001-08-21 Michael Bayne + + * src/querybox.c: Massage the selection somewhat by converting + newlines to spaces and if we believe that we're looking at a URL, + by removing spaces. + 2001-08-17 Michael Bayne * src/lookuplet.c: Got support for running as an applet working diff --git a/projects/lookuplet/NEWS b/projects/lookuplet/NEWS index b2a882ac..2d6bc25a 100644 --- a/projects/lookuplet/NEWS +++ b/projects/lookuplet/NEWS @@ -1,3 +1,7 @@ +- Convert newlines to spaces when grabbing the selection. If we think that + the selection is a URL (it starts with http: etc.), then we remove + spaces as well. + * lookuplet-1.1 (2001-08-17) - Fixed support for running as an applet and made the code automatically @@ -9,4 +13,4 @@ - Initial release. -$Id: NEWS,v 1.3 2001/08/18 02:39:36 mdb Exp $ +$Id: NEWS,v 1.4 2001/08/21 18:46:10 mdb Exp $ diff --git a/projects/lookuplet/src/c/querybox.c b/projects/lookuplet/src/c/querybox.c index 5e468e01..e639b51d 100644 --- a/projects/lookuplet/src/c/querybox.c +++ b/projects/lookuplet/src/c/querybox.c @@ -1,5 +1,5 @@ /** - * $Id: querybox.c,v 1.7 2001/08/18 02:34:06 mdb Exp $ + * $Id: querybox.c,v 1.8 2001/08/21 18:46:10 mdb Exp $ * * lookuplet - a utility for quickly looking up information * Copyright (C) 2001 Michael Bayne @@ -34,15 +34,32 @@ static GtkWidget* _query; +/* used by url_p */ +#define MATCH_PREFIX(text, prefix) !strncmp(text, prefix, sizeof(prefix)-1) + +/** + * A very primitive function to try to determine if the selection is a URL + * of some sort. + */ +static int +url_p (const char* text) +{ + return (MATCH_PREFIX(text, "http://") || + MATCH_PREFIX(text, "ftp://") || + MATCH_PREFIX(text, "https://") || + MATCH_PREFIX(text, "file:/")); +} + static void selection_received (GtkWidget* widget, GtkSelectionData* selection_data, gpointer data) { int length = selection_data->length; + int is_url; /* make sure some selection was provided */ if (length > 0) { - gchar* start, *end; + gchar* start, *end, *pos; gchar* search_text = gtk_editable_get_chars(GTK_EDITABLE(widget), 0, -1); @@ -51,6 +68,21 @@ selection_received (GtkWidget* widget, GtkSelectionData* selection_data, for (end = search_text + length - 1; isspace(*end); end--); end++; *end = '\0'; + /* check to see if we're looking at a url here */ + is_url = url_p(start); + + /* do some whitespace processing */ + for (pos = start; *pos != '\0'; pos++) { + /* convert newlines to spaces */ + if ((*pos == '\n') || (*pos == '\r')) { + *pos = ' '; + } + /* if this is a URL, we want to eat whitespace */ + if (*pos == ' ' && is_url) { + memmove(pos, pos+1, strlen(pos)); + } + } + /* only set the new text if we changed anything */ if (length != strlen(start)) { length = strlen(start);