Skip to content

Commit

Permalink
FauxFauxGH-3: Add very liberal URL regex as a default option
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Aug 11, 2013
1 parent 3034bd9 commit 4a781b4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
6 changes: 6 additions & 0 deletions putty.h
Expand Up @@ -145,6 +145,12 @@ enum {
URLHACK_UNDERLINE_NEVER
};

enum {
URLHACK_REGEX_CUSTOM = 0,
URLHACK_REGEX_CLASSIC = 1,
URLHACK_REGEX_LIBERAL,
};

/*
* HACK: PuttyTray
* Tray options
Expand Down
7 changes: 7 additions & 0 deletions settings.c
Expand Up @@ -71,6 +71,13 @@ const char* urlhack_default_regex =
"|(spotify:[^ ]+:[^ ]+)"
;

const char* urlhack_liberal_regex =
"("
"([a-zA-Z]+://|[wW][wW][wW]\\.|spotify:|telnet:)"
"[^ '\")>]+"
")"
;

/*
* Convenience functions to access the backends[] array
* (which is only present in tools that manage settings).
Expand Down
24 changes: 21 additions & 3 deletions urlhack.c
@@ -1,4 +1,7 @@
#include <string.h>
#include <assert.h>

#include "putty.h"
#include "urlhack.h"
#include "misc.h"
#include "puttymem.h"
Expand Down Expand Up @@ -156,13 +159,28 @@ void urlhack_reset()
window_text_current_pos = 0;
}

void urlhack_set_regular_expression(const char* expression)
void urlhack_set_regular_expression(int mode, const char* expression)
{
const char *to_use;
switch (mode) {
case URLHACK_REGEX_CUSTOM:
to_use = expression;
break;
case URLHACK_REGEX_CLASSIC:
to_use = urlhack_default_regex;
break;
case URLHACK_REGEX_LIBERAL:
to_use = urlhack_liberal_regex;
break;
default:
assert(!"illegal default regex setting");
}

is_regexp_compiled = 0;
urlhack_disabled = 0;

set_regerror_func(rtfm);
urlhack_rx = regcomp((char*)(expression));
urlhack_rx = regcomp((char*)(to_use));

if (urlhack_rx == 0) {
urlhack_disabled = 1;
Expand All @@ -178,7 +196,7 @@ void urlhack_go_find_me_some_hyperlinks(int screen_width)
if (urlhack_disabled != 0) return;

if (is_regexp_compiled == 0) {
urlhack_set_regular_expression(urlhack_default_regex);
urlhack_set_regular_expression(URLHACK_REGEX_CLASSIC, urlhack_default_regex);
}

urlhack_link_regions_clear();
Expand Down
3 changes: 2 additions & 1 deletion windows/urlhack.h
Expand Up @@ -8,6 +8,7 @@
typedef struct { int x0, y0, x1, y1; } text_region;

extern const char* urlhack_default_regex;
extern const char* urlhack_liberal_regex;
extern int urlhack_mouse_old_x, urlhack_mouse_old_y, urlhack_current_region;

void urlhack_reset();
Expand All @@ -21,7 +22,7 @@ text_region urlhack_get_link_bounds(int x, int y);
void urlhack_add_link_region(int x0, int y0, int x1, int y1);
void urlhack_launch_url(const char* app, const wchar_t *url);
int urlhack_is_ctrl_pressed();
void urlhack_set_regular_expression(const char* expression);
void urlhack_set_regular_expression(int mode, const char* expression);
void rtfm(const char *error);

void urlhack_init();
Expand Down
17 changes: 10 additions & 7 deletions windows/wincfg.c
Expand Up @@ -9,6 +9,7 @@
#include "putty.h"
#include "dialog.h"
#include "storage.h"
#include "urlhack.h"

static void about_handler(union control *ctrl, void *dlg,
void *data, int event)
Expand Down Expand Up @@ -447,18 +448,20 @@ void win_setup_config_box(struct controlbox *b, HWND *hwndp, int has_help,

s = ctrl_getset(b, "Window/Hyperlinks", "regexp", "Regular expression");

ctrl_checkbox(s, "Use the default regular expression", 'r',
HELPCTX(no_help),
conf_checkbox_handler, I(CONF_url_defregex));
ctrl_radiobuttons(s, "URL selection:", NO_SHORTCUT, 1,
HELPCTX(no_help),
conf_radiobutton_handler,
I(CONF_url_defregex),
"Select sensible URLs", NO_SHORTCUT, I(URLHACK_REGEX_CLASSIC),
"Select nearly any URL", NO_SHORTCUT, I(URLHACK_REGEX_LIBERAL),
"Custom", NO_SHORTCUT, I(URLHACK_REGEX_CUSTOM),
NULL);

ctrl_editbox(s, "or specify your own:", NO_SHORTCUT, 100,
ctrl_editbox(s, "Customise regex:", NO_SHORTCUT, 100,
HELPCTX(no_help),
conf_editbox_handler, I(CONF_url_regex),
I(1));

ctrl_text(s, "The single white space will be cropped in front of the link, if exists.",
HELPCTX(no_help));

/*
* Windows supports a local-command proxy. This also means we
* must adjust the text on the `Telnet command' control.
Expand Down
11 changes: 5 additions & 6 deletions windows/window.c
Expand Up @@ -968,9 +968,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
SetMenuInfo(popup_menus[CTXMENU].menu, &mi);
}

if (conf_get_int(term->conf, CONF_url_defregex) == 0) {
urlhack_set_regular_expression(conf_get_str(term->conf, CONF_url_regex));
}
urlhack_set_regular_expression(conf_get_int(term->conf, CONF_url_defregex),
conf_get_str(term->conf, CONF_url_regex));

/*
* Set up the initial input locale.
Expand Down Expand Up @@ -2463,9 +2462,9 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
MakeWindowTransparent(hwnd, 255);
}

if (conf_get_int(conf, CONF_url_defregex) == 0) {
urlhack_set_regular_expression(conf_get_str(conf, CONF_url_regex));
}
urlhack_set_regular_expression(conf_get_int(term->conf, CONF_url_defregex),
conf_get_str(term->conf, CONF_url_regex));

term->url_update = TRUE;
term_update(term);

Expand Down

0 comments on commit 4a781b4

Please sign in to comment.