Highlight URLs with control and follow with click

This commit is contained in:
Jishnu Sen
2024-04-07 22:54:46 -07:00
committed by Łukasz Pankowski
parent 6dd2af8162
commit 91f0a5a008
4 changed files with 172 additions and 51 deletions

View File

@@ -511,3 +511,14 @@ static char ascii_printable[] =
" !\"#$%&'()*+,-./0123456789:;<=>?"
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
"`abcdefghijklmnopqrstuvwxyz{|}~";
/*
* Open urls starting with urlprefixes, contatining urlchars
* by passing as ARG1 to urlhandler.
*/
char* urlhandler = "xdg-open";
char urlchars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-._~:/?#@!$&'*+,;=%";
char* urlprefixes[] = {"http://", "https://", NULL};

89
st.c
View File

@@ -652,6 +652,95 @@ getsel(void)
return str;
}
char *
strstrany(char* s, char** strs) {
char *match;
for (int i = 0; strs[i]; i++) {
if ((match = strstr(s, strs[i]))) {
return match;
}
}
return NULL;
}
void
highlighturls(void)
{
char *match;
char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
for (int i = term.top; i < term.bot; i++) {
int url_start = -1;
for (int j = 0; j < term.col; j++) {
if (term.line[i][j].u < 127) {
linestr[j] = term.line[i][j].u;
}
linestr[term.col] = '\0';
}
while ((match = strstrany(linestr + url_start + 1, urlprefixes))) {
url_start = match - linestr;
for (int c = url_start; c < term.col && strchr(urlchars, linestr[c]); c++) {
term.line[i][c].mode |= ATTR_URL;
tsetdirt(i, c);
}
}
}
free(linestr);
}
void
unhighlighturls(void)
{
for (int i = term.top; i < term.bot; i++) {
for (int j = 0; j < term.col; j++) {
Glyph* g = &term.line[i][j];
if (g->mode & ATTR_URL) {
g->mode &= ~ATTR_URL;
tsetdirt(i, j);
}
}
}
return;
}
void
followurl(int x, int y) {
char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
char *match;
for (int i = 0; i < term.col; i++) {
if (term.line[x][i].u < 127) {
linestr[i] = term.line[x][i].u;
}
linestr[term.col] = '\0';
}
int url_start = -1;
while ((match = strstrany(linestr + url_start + 1, urlprefixes))) {
url_start = match - linestr;
int url_end = url_start;
for (int c = url_start; c < term.col && strchr(urlchars, linestr[c]); c++) {
url_end++;
}
if (url_start <= y && y < url_end) {
linestr[url_end] = '\0';
break;
}
}
if (url_start == -1) {
free(linestr);
return;
}
pid_t chpid;
if ((chpid = fork()) == 0) {
if (fork() == 0)
execlp(urlhandler, urlhandler, linestr + url_start, NULL);
exit(1);
}
if (chpid > 0)
waitpid(chpid, NULL, 0);
free(linestr);
unhighlighturls();
}
void
selclear(void)
{

33
st.h
View File

@@ -11,10 +11,10 @@
#define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
#define DEFAULT(a, b) (a) = (a) ? (a) : (b)
#define LIMIT(x, a, b) (x) = (x)<(a) ? (a) : (x)>(b) ? (b) : (x)
#define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
(a).bg != (b).bg)
#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
(t1.tv_nsec-t2.tv_nsec)/1E6)
#define ATTRCMP(a, b) \
((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
#define TIMEDIFF(t1, t2) \
((t1.tv_sec - t2.tv_sec) * 1000 + (t1.tv_nsec - t2.tv_nsec) / 1E6)
#define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
#define TRUECOLOR(r, g, b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
@@ -35,23 +35,14 @@ enum glyph_attribute {
ATTR_WIDE = 1 << 9,
ATTR_WDUMMY = 1 << 10,
ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
ATTR_URL = 1 << 14,
};
enum selection_mode {
SEL_IDLE = 0,
SEL_EMPTY = 1,
SEL_READY = 2
};
enum selection_mode { SEL_IDLE = 0, SEL_EMPTY = 1, SEL_READY = 2 };
enum selection_type {
SEL_REGULAR = 1,
SEL_RECTANGULAR = 2
};
enum selection_type { SEL_REGULAR = 1, SEL_RECTANGULAR = 2 };
enum selection_snap {
SNAP_WORD = 1,
SNAP_LINE = 2
};
enum selection_snap { SNAP_WORD = 1, SNAP_LINE = 2 };
typedef unsigned char uchar;
typedef unsigned int uint;
@@ -109,6 +100,10 @@ void selextend(int, int, int, int);
int selected(int, int);
char *getsel(void);
void highlighturls(void);
void unhighlighturls(void);
void followurl(int, int);
size_t utf8encode(Rune, char *);
void *xmalloc(size_t);
@@ -129,6 +124,10 @@ extern unsigned int tabspaces;
extern unsigned int defaultfg;
extern unsigned int defaultbg;
extern unsigned int defaultcs;
extern char *urlhandler;
extern char urlchars[];
extern char *urlprefixes[];
extern int nurlprefixes;
char **externalcopycmd();
char **externalpastecmd();

24
x.c
View File

@@ -204,6 +204,7 @@ static void usage(void);
static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
[KeyRelease] = kpress,
[ClientMessage] = cmessage,
[ConfigureNotify] = resize,
[VisibilityNotify] = visibility,
@@ -465,6 +466,15 @@ mouseaction(XEvent *e, uint release)
/* ignore Button<N>mask for Button<N> - it's set on release */
uint state = e->xbutton.state & ~buttonmask(e->xbutton.button);
if (release == 0 &&
e->xbutton.button == Button1 &&
(match(ControlMask, state) ||
match(ControlMask, state & ~forcemousemod))) {
followurl(evrow(e), evcol(e));
return 1;
}
for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
if (ms->release == release &&
ms->button == e->xbutton.button &&
@@ -1508,7 +1518,7 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i
XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
/* Render underline and strikethrough. */
if (base.mode & ATTR_UNDERLINE) {
if (base.mode & ATTR_UNDERLINE || base.mode & ATTR_URL) {
XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent * chscale + 1,
width, 1);
}
@@ -1872,6 +1882,18 @@ kpress(XEvent *ev)
} else {
len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
}
/* 0. highlight URLs when control held */
if (ksym == XK_Control_L) {
highlighturls();
} else if (ev->type == KeyRelease && e->keycode == XKeysymToKeycode(e->display, XK_Control_L)) {
unhighlighturls();
}
/* KeyRelease not relevant to shortcuts */
if (ev->type == KeyRelease)
return;
/* 1. shortcuts */
for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
if (ksym == bp->keysym && match(bp->mod, e->state)) {