Support hyperlinks with capture-pane -e and add a mouse_hyperlink
[tmux-openbsd.git] / paste.c
blob5ff3685401561d224ee7ab2e876eea939bad4188
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <vis.h>
26 #include "tmux.h"
29 * Set of paste buffers. Note that paste buffer data is not necessarily a C
30 * string!
33 struct paste_buffer {
34 char *data;
35 size_t size;
37 char *name;
38 time_t created;
39 int automatic;
40 u_int order;
42 RB_ENTRY(paste_buffer) name_entry;
43 RB_ENTRY(paste_buffer) time_entry;
46 static u_int paste_next_index;
47 static u_int paste_next_order;
48 static u_int paste_num_automatic;
49 static RB_HEAD(paste_name_tree, paste_buffer) paste_by_name;
50 static RB_HEAD(paste_time_tree, paste_buffer) paste_by_time;
52 static int paste_cmp_names(const struct paste_buffer *,
53 const struct paste_buffer *);
54 RB_GENERATE_STATIC(paste_name_tree, paste_buffer, name_entry, paste_cmp_names);
56 static int paste_cmp_times(const struct paste_buffer *,
57 const struct paste_buffer *);
58 RB_GENERATE_STATIC(paste_time_tree, paste_buffer, time_entry, paste_cmp_times);
60 static int
61 paste_cmp_names(const struct paste_buffer *a, const struct paste_buffer *b)
63 return (strcmp(a->name, b->name));
66 static int
67 paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
69 if (a->order > b->order)
70 return (-1);
71 if (a->order < b->order)
72 return (1);
73 return (0);
76 /* Get paste buffer name. */
77 const char *
78 paste_buffer_name(struct paste_buffer *pb)
80 return (pb->name);
83 /* Get paste buffer order. */
84 u_int
85 paste_buffer_order(struct paste_buffer *pb)
87 return (pb->order);
90 /* Get paste buffer created. */
91 time_t
92 paste_buffer_created(struct paste_buffer *pb)
94 return (pb->created);
97 /* Get paste buffer data. */
98 const char *
99 paste_buffer_data(struct paste_buffer *pb, size_t *size)
101 if (size != NULL)
102 *size = pb->size;
103 return (pb->data);
106 /* Walk paste buffers by time. */
107 struct paste_buffer *
108 paste_walk(struct paste_buffer *pb)
110 if (pb == NULL)
111 return (RB_MIN(paste_time_tree, &paste_by_time));
112 return (RB_NEXT(paste_time_tree, &paste_by_time, pb));
116 paste_is_empty(void)
118 return RB_ROOT(&paste_by_time) == NULL;
121 /* Get the most recent automatic buffer. */
122 struct paste_buffer *
123 paste_get_top(const char **name)
125 struct paste_buffer *pb;
127 pb = RB_MIN(paste_time_tree, &paste_by_time);
128 while (pb != NULL && !pb->automatic)
129 pb = RB_NEXT(paste_time_tree, &paste_by_time, pb);
130 if (pb == NULL)
131 return (NULL);
132 if (name != NULL)
133 *name = pb->name;
134 return (pb);
137 /* Get a paste buffer by name. */
138 struct paste_buffer *
139 paste_get_name(const char *name)
141 struct paste_buffer pbfind;
143 if (name == NULL || *name == '\0')
144 return (NULL);
146 pbfind.name = (char *)name;
147 return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind));
150 /* Free a paste buffer. */
151 void
152 paste_free(struct paste_buffer *pb)
154 RB_REMOVE(paste_name_tree, &paste_by_name, pb);
155 RB_REMOVE(paste_time_tree, &paste_by_time, pb);
156 if (pb->automatic)
157 paste_num_automatic--;
159 free(pb->data);
160 free(pb->name);
161 free(pb);
165 * Add an automatic buffer, freeing the oldest automatic item if at limit. Note
166 * that the caller is responsible for allocating data.
168 void
169 paste_add(const char *prefix, char *data, size_t size)
171 struct paste_buffer *pb, *pb1;
172 u_int limit;
174 if (prefix == NULL)
175 prefix = "buffer";
177 if (size == 0) {
178 free(data);
179 return;
182 limit = options_get_number(global_options, "buffer-limit");
183 RB_FOREACH_REVERSE_SAFE(pb, paste_time_tree, &paste_by_time, pb1) {
184 if (paste_num_automatic < limit)
185 break;
186 if (pb->automatic)
187 paste_free(pb);
190 pb = xmalloc(sizeof *pb);
192 pb->name = NULL;
193 do {
194 free(pb->name);
195 xasprintf(&pb->name, "%s%u", prefix, paste_next_index);
196 paste_next_index++;
197 } while (paste_get_name(pb->name) != NULL);
199 pb->data = data;
200 pb->size = size;
202 pb->automatic = 1;
203 paste_num_automatic++;
205 pb->created = time(NULL);
207 pb->order = paste_next_order++;
208 RB_INSERT(paste_name_tree, &paste_by_name, pb);
209 RB_INSERT(paste_time_tree, &paste_by_time, pb);
212 /* Rename a paste buffer. */
214 paste_rename(const char *oldname, const char *newname, char **cause)
216 struct paste_buffer *pb, *pb_new;
218 if (cause != NULL)
219 *cause = NULL;
221 if (oldname == NULL || *oldname == '\0') {
222 if (cause != NULL)
223 *cause = xstrdup("no buffer");
224 return (-1);
226 if (newname == NULL || *newname == '\0') {
227 if (cause != NULL)
228 *cause = xstrdup("new name is empty");
229 return (-1);
232 pb = paste_get_name(oldname);
233 if (pb == NULL) {
234 if (cause != NULL)
235 xasprintf(cause, "no buffer %s", oldname);
236 return (-1);
239 pb_new = paste_get_name(newname);
240 if (pb_new != NULL) {
241 if (cause != NULL)
242 xasprintf(cause, "buffer %s already exists", newname);
243 return (-1);
246 RB_REMOVE(paste_name_tree, &paste_by_name, pb);
248 free(pb->name);
249 pb->name = xstrdup(newname);
251 if (pb->automatic)
252 paste_num_automatic--;
253 pb->automatic = 0;
255 RB_INSERT(paste_name_tree, &paste_by_name, pb);
257 return (0);
261 * Add or replace an item in the store. Note that the caller is responsible for
262 * allocating data.
265 paste_set(char *data, size_t size, const char *name, char **cause)
267 struct paste_buffer *pb, *old;
269 if (cause != NULL)
270 *cause = NULL;
272 if (size == 0) {
273 free(data);
274 return (0);
276 if (name == NULL) {
277 paste_add(NULL, data, size);
278 return (0);
281 if (*name == '\0') {
282 if (cause != NULL)
283 *cause = xstrdup("empty buffer name");
284 return (-1);
287 pb = xmalloc(sizeof *pb);
289 pb->name = xstrdup(name);
291 pb->data = data;
292 pb->size = size;
294 pb->automatic = 0;
295 pb->order = paste_next_order++;
297 pb->created = time(NULL);
299 if ((old = paste_get_name(name)) != NULL)
300 paste_free(old);
302 RB_INSERT(paste_name_tree, &paste_by_name, pb);
303 RB_INSERT(paste_time_tree, &paste_by_time, pb);
305 return (0);
308 /* Set paste data without otherwise changing it. */
309 void
310 paste_replace(struct paste_buffer *pb, char *data, size_t size)
312 free(pb->data);
313 pb->data = data;
314 pb->size = size;
317 /* Convert start of buffer into a nice string. */
318 char *
319 paste_make_sample(struct paste_buffer *pb)
321 char *buf;
322 size_t len, used;
323 const int flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
324 const size_t width = 200;
326 len = pb->size;
327 if (len > width)
328 len = width;
329 buf = xreallocarray(NULL, len, 4 + 4);
331 used = utf8_strvis(buf, pb->data, len, flags);
332 if (pb->size > width || used > width)
333 strlcpy(buf + width, "...", 4);
334 return (buf);