-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen.c
More file actions
165 lines (144 loc) · 4.6 KB
/
Copy pathopen.c
File metadata and controls
165 lines (144 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* open plugin: hand a path or URL off to the system's default app.
*
* :open [target] xdg-open <target>; no arg = path/URL under cursor
* :open_file open current buffer's file
* :open_dir open directory containing current buffer's file
* gx (normal) open path/URL under cursor
*
* Uses xdg-open on Linux, `open` on macOS. */
#include "hed.h"
#include "buf/buf_helpers.h"
#include "lib/strbuf.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
static void strbuf_cat(StrBuf *s, const char *cstr) {
strbuf_append(s, cstr, strlen(cstr));
}
#if defined(__APPLE__)
#define OPEN_CMD "open"
#else
#define OPEN_CMD "xdg-open"
#endif
static int is_url_char(unsigned char c) {
if (isalnum(c)) return 1;
switch (c) {
case '/': case '.': case '_': case '-': case '~':
case '+': case ':': case '?': case '=': case '&':
case '#': case '%': case '@': case ',': case ';':
case '!': case '$': case '\'': case '(': case ')':
case '*':
return 1;
default:
return 0;
}
}
/* Extract a URL- or path-like token around the cursor. Falls back to
* buf_get_path_under_cursor's notion of a path if no URL chars cluster. */
static int extract_target_under_cursor(StrBuf *out) {
BUF(buf);
WIN(win);
if (!BOUNDS_CHECK(win->cursor.y, buf->num_rows)) return 0;
Row *row = &buf->rows[win->cursor.y];
if (row->chars.len == 0) return 0;
const char *s = row->chars.data;
int len = (int)row->chars.len;
int cx = win->cursor.x;
if (cx >= len) cx = len - 1;
if (cx < 0) return 0;
if (!is_url_char((unsigned char)s[cx])) {
int left = cx - 1;
while (left >= 0 && !is_url_char((unsigned char)s[left])) left--;
if (left < 0) return 0;
cx = left;
}
int start = cx, end = cx + 1;
while (start > 0 && is_url_char((unsigned char)s[start - 1])) start--;
while (end < len && is_url_char((unsigned char)s[end])) end++;
/* Trim common trailing punctuation that's usually sentence-glue,
* not part of the URL. */
while (end > start) {
char c = s[end - 1];
if (c == '.' || c == ',' || c == ';' || c == ':' ||
c == ')' || c == '!' || c == '?' || c == '\'')
end--;
else
break;
}
if (end <= start) return 0;
strbuf_free(out);
*out = strbuf_from(s + start, (size_t)(end - start));
return out->data && out->len > 0;
}
void open_path(const char *target) {
if (!target || !*target) {
ed_set_status_message("open: nothing to open");
return;
}
StrBuf cmd = strbuf_new();
strbuf_cat(&cmd, OPEN_CMD " ");
strbuf_append_shell_quoted(&cmd, target);
strbuf_cat(&cmd, " >/dev/null 2>&1 &");
/* Background — fire and forget; don't block the editor on viewer
* startup. system() is fine here, no raw-mode dance needed. */
int rc = system(cmd.data);
(void)rc;
ed_set_status_message("open: %s", target);
strbuf_free(&cmd);
}
/* --- commands --- */
static void cmd_open(const char *args) {
if (args && *args) {
while (*args == ' ') args++;
open_path(args);
return;
}
StrBuf target = strbuf_new();
if (!extract_target_under_cursor(&target)) {
ed_set_status_message("open: no path/URL under cursor");
strbuf_free(&target);
return;
}
open_path(target.data);
strbuf_free(&target);
}
static void cmd_open_file(const char *args) {
(void)args;
BUF(buf);
if (!buf->filename || !*buf->filename) {
ed_set_status_message("open: buffer has no file");
return;
}
open_path(buf->filename);
}
static void cmd_open_dir(const char *args) {
(void)args;
BUF(buf);
const char *fn = (buf->filename && *buf->filename) ? buf->filename : ".";
const char *slash = strrchr(fn, '/');
if (!slash) {
open_path(".");
return;
}
StrBuf dir = strbuf_from(fn, (size_t)(slash - fn));
open_path(dir.data ? dir.data : ".");
strbuf_free(&dir);
}
/* --- keybind --- */
static void kb_open_under_cursor(void) {
cmd_open(NULL);
}
/* --- lifecycle --- */
static int open_init(void) {
cmd("open", cmd_open, "open path/URL (arg or under cursor) via " OPEN_CMD);
cmd("open_file", cmd_open_file, "open current buffer's file via " OPEN_CMD);
cmd("open_dir", cmd_open_dir, "open directory of current buffer via " OPEN_CMD);
mapn("gx", kb_open_under_cursor, "open path/URL under cursor");
return 0;
}
const Plugin plugin_open = {
.name = "open",
.desc = "open paths/URLs with the system's default application",
.init = open_init,
.deinit = NULL,
};