diff --git a/doc/cgmnlm.scd b/doc/cgmnlm.scd
index 0e50aa7bfe8eed0814a18456ef101cd30180fb14..bbc057517679bf20d097d3c13cce66ff867d44e1 100644
--- a/doc/cgmnlm.scd
+++ b/doc/cgmnlm.scd
@@ -31,3 +31,6 @@ Sets the maximum width, in columns, of Gemtext pages.
*-A*
Enables alternate text instead of preformatted text by default.
+
+*-T*
+ Open files downloaded with *t* command in default viewer (requires *xdg-open*)
diff --git a/src/gmnlm.c b/src/gmnlm.c
index 647899d41fd3c61b7a1bb16363b2eb4360f35128..5671394f01788abfa991400576fbc6fd2f4725be 100644
--- a/src/gmnlm.c
+++ b/src/gmnlm.c
@@ -45,7 +45,7 @@ struct history *prev, *next;
};
struct browser {
- bool pagination, unicode, alttext;
+ bool pagination, unicode, alttext, autoopen;
int max_width;
struct gemini_options opts;
struct gemini_tofu tofu;
@@ -763,11 +763,7 @@ }
}
if (url[0]) {
fprintf(browser->tty, "=> %s\n", url);
- if (in[0] == 'e') {
- char target[2048] = {0};
- snprintf(target, sizeof(target), "xdg-open %s", url);
- if ( !system(target) ) fprintf(browser->tty, "Link send to xdg-open\n");
- }
+ char tempfile[] = "/tmp/cgmnlm_XXXXXX";
if (in[0] == 't') {
struct gemini_response resp;
set_url(browser, url, &browser->history);
@@ -776,12 +772,16 @@ if (res != GEMINI_OK) {
fprintf(stderr, "Error: %s\n",
gemini_strerr(res, &resp));
} else {
- char tempfile[] = "/tmp/cgmnlm_XXXXXX";
close(mkstemp(tempfile));
download_resp(browser->tty, resp, tempfile, url);
}
gemini_response_finish(&resp);
set_url(browser, url, NULL);
+ }
+ if (in[0] == 'e' || browser->autoopen) {
+ char target[1024];
+ snprintf(target, sizeof(target), "xdg-open %s >/dev/null 2>&1", in[0] == 't' ? tempfile : url);
+ if ( !system(target) ) fprintf(browser->tty, "Link send to xdg-open\n");
}
fprintf(browser->tty, "\n");
}
@@ -1134,7 +1134,6 @@
static bool
display_plaintext(struct browser *browser, struct gemini_response *resp)
{
- // TODO: Strip ANSI escape sequences
struct winsize ws;
int row = 0, col = 0;
ioctl(fileno(browser->tty), TIOCGWINSZ, &ws);
@@ -1274,6 +1273,7 @@ {
struct browser browser = {
.pagination = true,
.alttext = false,
+ .autoopen = false,
.tofu_mode = TOFU_ASK,
.unicode = true,
.url = curl_url(),
@@ -1282,7 +1282,7 @@ .meta = NULL,
};
int c;
- while ((c = getopt(argc, argv, "hj:PAUW:")) != -1) {
+ while ((c = getopt(argc, argv, "hj:PTAUW:")) != -1) {
switch (c) {
case 'h':
usage(argv[0]);
@@ -1298,6 +1298,9 @@ } else {
usage(argv[0]);
return 1;
}
+ break;
+ case 'T':
+ browser.autoopen = true;
break;
case 'A':
browser.alttext = true;