There is no longer a need for a paste_stack struct or for global_buffers

to be global. Move to paste.c.
This commit is contained in:
nicm 2014-04-24 09:14:43 +00:00
parent 7ab2690be8
commit bec6c807cd
14 changed files with 63 additions and 66 deletions

View File

@ -194,7 +194,7 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
} else { } else {
limit = options_get_number(&global_options, "buffer-limit"); limit = options_get_number(&global_options, "buffer-limit");
if (!args_has(args, 'b')) { if (!args_has(args, 'b')) {
paste_add(&global_buffers, buf, len, limit); paste_add(buf, len, limit);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }
@ -206,7 +206,7 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
if (paste_replace(&global_buffers, buffer, buf, len) != 0) { if (paste_replace(buffer, buf, len) != 0) {
cmdq_error(cmdq, "no buffer %d", buffer); cmdq_error(cmdq, "no buffer %d", buffer);
free(buf); free(buf);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);

View File

@ -63,7 +63,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
utf8flag = options_get_number(&wl->window->options, "utf8"); utf8flag = options_get_number(&wl->window->options, "utf8");
if (paste_get_top(&global_buffers) == NULL) if (paste_get_top() == NULL)
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
@ -75,7 +75,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
action = xstrdup("paste-buffer -b '%%'"); action = xstrdup("paste-buffer -b '%%'");
idx = 0; idx = 0;
while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) { while ((pb = paste_walk_stack(&idx)) != NULL) {
cdata = window_choose_data_create(TREE_OTHER, c, c->session); cdata = window_choose_data_create(TREE_OTHER, c, c->session);
cdata->idx = idx - 1; cdata->idx = idx - 1;

View File

@ -45,7 +45,7 @@ cmd_delete_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
int buffer; int buffer;
if (!args_has(args, 'b')) { if (!args_has(args, 'b')) {
paste_free_top(&global_buffers); paste_free_top();
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }
@ -56,7 +56,7 @@ cmd_delete_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
if (paste_free_index(&global_buffers, buffer) != 0) { if (paste_free_index(buffer) != 0) {
cmdq_error(cmdq, "no buffer %d", buffer); cmdq_error(cmdq, "no buffer %d", buffer);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }

View File

@ -52,7 +52,7 @@ cmd_list_buffers_exec(unused struct cmd *self, struct cmd_q *cmdq)
template = LIST_BUFFERS_TEMPLATE; template = LIST_BUFFERS_TEMPLATE;
idx = 0; idx = 0;
while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) { while ((pb = paste_walk_stack(&idx)) != NULL) {
ft = format_create(); ft = format_create();
format_add(ft, "line", "%u", idx - 1); format_add(ft, "line", "%u", idx - 1);
format_paste_buffer(ft, pb, 0); format_paste_buffer(ft, pb, 0);

View File

@ -119,10 +119,10 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
limit = options_get_number(&global_options, "buffer-limit"); limit = options_get_number(&global_options, "buffer-limit");
if (buffer == -1) { if (buffer == -1) {
paste_add(&global_buffers, pdata, psize, limit); paste_add(pdata, psize, limit);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }
if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) { if (paste_replace(buffer, pdata, psize) != 0) {
cmdq_error(cmdq, "no buffer %d", buffer); cmdq_error(cmdq, "no buffer %d", buffer);
free(pdata); free(pdata);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
@ -164,8 +164,8 @@ cmd_load_buffer_callback(struct client *c, int closed, void *data)
limit = options_get_number(&global_options, "buffer-limit"); limit = options_get_number(&global_options, "buffer-limit");
if (*buffer == -1) if (*buffer == -1)
paste_add(&global_buffers, pdata, psize, limit); paste_add(pdata, psize, limit);
else if (paste_replace(&global_buffers, *buffer, pdata, psize) != 0) { else if (paste_replace(*buffer, pdata, psize) != 0) {
/* No context so can't use server_client_msg_error. */ /* No context so can't use server_client_msg_error. */
evbuffer_add_printf(c->stderr_data, "no buffer %d\n", *buffer); evbuffer_add_printf(c->stderr_data, "no buffer %d\n", *buffer);
server_push_stderr(c); server_push_stderr(c);

View File

@ -69,9 +69,9 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
} }
if (buffer == -1) if (buffer == -1)
pb = paste_get_top(&global_buffers); pb = paste_get_top();
else { else {
pb = paste_get_index(&global_buffers, buffer); pb = paste_get_index(buffer);
if (pb == NULL) { if (pb == NULL) {
cmdq_error(cmdq, "no buffer %d", buffer); cmdq_error(cmdq, "no buffer %d", buffer);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
@ -93,9 +93,9 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
/* Delete the buffer if -d. */ /* Delete the buffer if -d. */
if (args_has(args, 'd')) { if (args_has(args, 'd')) {
if (buffer == -1) if (buffer == -1)
paste_free_top(&global_buffers); paste_free_top();
else else
paste_free_index(&global_buffers, buffer); paste_free_index(buffer);
} }
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);

View File

@ -66,7 +66,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
FILE *f; FILE *f;
if (!args_has(args, 'b')) { if (!args_has(args, 'b')) {
if ((pb = paste_get_top(&global_buffers)) == NULL) { if ((pb = paste_get_top()) == NULL) {
cmdq_error(cmdq, "no buffers"); cmdq_error(cmdq, "no buffers");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
@ -78,7 +78,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
pb = paste_get_index(&global_buffers, buffer); pb = paste_get_index(buffer);
if (pb == NULL) { if (pb == NULL) {
cmdq_error(cmdq, "no buffer %d", buffer); cmdq_error(cmdq, "no buffer %d", buffer);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);

View File

@ -66,13 +66,13 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
free(cause); free(cause);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
pb = paste_get_index(&global_buffers, buffer); pb = paste_get_index(buffer);
if (pb == NULL) { if (pb == NULL) {
cmdq_error(cmdq, "no buffer %d", buffer); cmdq_error(cmdq, "no buffer %d", buffer);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
} else if (args_has(args, 'a')) { } else if (args_has(args, 'a')) {
pb = paste_get_top(&global_buffers); pb = paste_get_top();
if (pb != NULL) if (pb != NULL)
buffer = 0; buffer = 0;
} }
@ -88,9 +88,9 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
psize += newsize; psize += newsize;
if (buffer == -1) if (buffer == -1)
paste_add(&global_buffers, pdata, psize, limit); paste_add(pdata, psize, limit);
else else
paste_replace(&global_buffers, buffer, pdata, psize); paste_replace(buffer, pdata, psize);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

View File

@ -240,7 +240,7 @@ input_mouse(struct window_pane *wp, struct session *s, struct mouse_event *m)
if (m->button == 1 && (m->event & MOUSE_EVENT_CLICK) && if (m->button == 1 && (m->event & MOUSE_EVENT_CLICK) &&
options_get_number(&wp->window->options, "mode-mouse") == 1) { options_get_number(&wp->window->options, "mode-mouse") == 1) {
pb = paste_get_top(&global_buffers); pb = paste_get_top();
if (pb != NULL) { if (pb != NULL) {
paste_send_pane(pb, wp, "\r", paste_send_pane(pb, wp, "\r",
wp->screen->mode & MODE_BRACKETPASTE); wp->screen->mode & MODE_BRACKETPASTE);

50
paste.c
View File

@ -30,46 +30,48 @@
* string! * string!
*/ */
ARRAY_DECL(, struct paste_buffer *) paste_buffers = ARRAY_INITIALIZER;
/* Return each item of the stack in turn. */ /* Return each item of the stack in turn. */
struct paste_buffer * struct paste_buffer *
paste_walk_stack(struct paste_stack *ps, u_int *idx) paste_walk_stack(u_int *idx)
{ {
struct paste_buffer *pb; struct paste_buffer *pb;
pb = paste_get_index(ps, *idx); pb = paste_get_index(*idx);
(*idx)++; (*idx)++;
return (pb); return (pb);
} }
/* Get the top item on the stack. */ /* Get the top item on the stack. */
struct paste_buffer * struct paste_buffer *
paste_get_top(struct paste_stack *ps) paste_get_top(void)
{ {
if (ARRAY_LENGTH(ps) == 0) if (ARRAY_LENGTH(&paste_buffers) == 0)
return (NULL); return (NULL);
return (ARRAY_FIRST(ps)); return (ARRAY_FIRST(&paste_buffers));
} }
/* Get an item by its index. */ /* Get an item by its index. */
struct paste_buffer * struct paste_buffer *
paste_get_index(struct paste_stack *ps, u_int idx) paste_get_index(u_int idx)
{ {
if (idx >= ARRAY_LENGTH(ps)) if (idx >= ARRAY_LENGTH(&paste_buffers))
return (NULL); return (NULL);
return (ARRAY_ITEM(ps, idx)); return (ARRAY_ITEM(&paste_buffers, idx));
} }
/* Free the top item on the stack. */ /* Free the top item on the stack. */
int int
paste_free_top(struct paste_stack *ps) paste_free_top(void)
{ {
struct paste_buffer *pb; struct paste_buffer *pb;
if (ARRAY_LENGTH(ps) == 0) if (ARRAY_LENGTH(&paste_buffers) == 0)
return (-1); return (-1);
pb = ARRAY_FIRST(ps); pb = ARRAY_FIRST(&paste_buffers);
ARRAY_REMOVE(ps, 0); ARRAY_REMOVE(&paste_buffers, 0);
free(pb->data); free(pb->data);
free(pb); free(pb);
@ -79,15 +81,15 @@ paste_free_top(struct paste_stack *ps)
/* Free an item by index. */ /* Free an item by index. */
int int
paste_free_index(struct paste_stack *ps, u_int idx) paste_free_index(u_int idx)
{ {
struct paste_buffer *pb; struct paste_buffer *pb;
if (idx >= ARRAY_LENGTH(ps)) if (idx >= ARRAY_LENGTH(&paste_buffers))
return (-1); return (-1);
pb = ARRAY_ITEM(ps, idx); pb = ARRAY_ITEM(&paste_buffers, idx);
ARRAY_REMOVE(ps, idx); ARRAY_REMOVE(&paste_buffers, idx);
free(pb->data); free(pb->data);
free(pb); free(pb);
@ -100,22 +102,22 @@ paste_free_index(struct paste_stack *ps, u_int idx)
* that the caller is responsible for allocating data. * that the caller is responsible for allocating data.
*/ */
void void
paste_add(struct paste_stack *ps, char *data, size_t size, u_int limit) paste_add(char *data, size_t size, u_int limit)
{ {
struct paste_buffer *pb; struct paste_buffer *pb;
if (size == 0) if (size == 0)
return; return;
while (ARRAY_LENGTH(ps) >= limit) { while (ARRAY_LENGTH(&paste_buffers) >= limit) {
pb = ARRAY_LAST(ps); pb = ARRAY_LAST(&paste_buffers);
free(pb->data); free(pb->data);
free(pb); free(pb);
ARRAY_TRUNC(ps, 1); ARRAY_TRUNC(&paste_buffers, 1);
} }
pb = xmalloc(sizeof *pb); pb = xmalloc(sizeof *pb);
ARRAY_INSERT(ps, 0, pb); ARRAY_INSERT(&paste_buffers, 0, pb);
pb->data = data; pb->data = data;
pb->size = size; pb->size = size;
@ -127,7 +129,7 @@ paste_add(struct paste_stack *ps, char *data, size_t size, u_int limit)
* allocating data. * allocating data.
*/ */
int int
paste_replace(struct paste_stack *ps, u_int idx, char *data, size_t size) paste_replace(u_int idx, char *data, size_t size)
{ {
struct paste_buffer *pb; struct paste_buffer *pb;
@ -136,10 +138,10 @@ paste_replace(struct paste_stack *ps, u_int idx, char *data, size_t size)
return (0); return (0);
} }
if (idx >= ARRAY_LENGTH(ps)) if (idx >= ARRAY_LENGTH(&paste_buffers))
return (-1); return (-1);
pb = ARRAY_ITEM(ps, idx); pb = ARRAY_ITEM(&paste_buffers, idx);
free(pb->data); free(pb->data);
pb->data = data; pb->data = data;

View File

@ -51,8 +51,6 @@ int server_shutdown;
struct event server_ev_accept; struct event server_ev_accept;
struct event server_ev_second; struct event server_ev_second;
struct paste_stack global_buffers;
int server_create_socket(void); int server_create_socket(void);
void server_loop(void); void server_loop(void);
int server_should_shutdown(void); int server_should_shutdown(void);
@ -147,7 +145,6 @@ server_start(int lockfd, char *lockfile)
RB_INIT(&sessions); RB_INIT(&sessions);
RB_INIT(&dead_sessions); RB_INIT(&dead_sessions);
TAILQ_INIT(&session_groups); TAILQ_INIT(&session_groups);
ARRAY_INIT(&global_buffers);
mode_key_init_trees(); mode_key_init_trees();
key_bindings_init(); key_bindings_init();
utf8_build(); utf8_build();

View File

@ -1157,7 +1157,7 @@ status_prompt_key(struct client *c, int key)
c->flags |= CLIENT_STATUS; c->flags |= CLIENT_STATUS;
break; break;
case MODEKEYEDIT_PASTE: case MODEKEYEDIT_PASTE:
if ((pb = paste_get_top(&global_buffers)) == NULL) if ((pb = paste_get_top()) == NULL)
break; break;
for (n = 0; n < pb->size; n++) { for (n = 0; n < pb->size; n++) {
ch = (u_char) pb->data[n]; ch = (u_char) pb->data[n];

16
tmux.h
View File

@ -1037,7 +1037,6 @@ struct paste_buffer {
char *data; char *data;
size_t size; size_t size;
}; };
ARRAY_DECL(paste_stack, struct paste_buffer *);
/* Environment variable. */ /* Environment variable. */
struct environ_entry { struct environ_entry {
@ -1709,13 +1708,13 @@ void tty_keys_free(struct tty *);
int tty_keys_next(struct tty *); int tty_keys_next(struct tty *);
/* paste.c */ /* paste.c */
struct paste_buffer *paste_walk_stack(struct paste_stack *, u_int *); struct paste_buffer *paste_walk_stack(u_int *);
struct paste_buffer *paste_get_top(struct paste_stack *); struct paste_buffer *paste_get_top(void);
struct paste_buffer *paste_get_index(struct paste_stack *, u_int); struct paste_buffer *paste_get_index(u_int);
int paste_free_top(struct paste_stack *); int paste_free_top(void);
int paste_free_index(struct paste_stack *, u_int); int paste_free_index(u_int);
void paste_add(struct paste_stack *, char *, size_t, u_int); void paste_add(char *, size_t, u_int);
int paste_replace(struct paste_stack *, u_int, char *, size_t); int paste_replace(u_int, char *, size_t);
char *paste_make_sample(struct paste_buffer *, int); char *paste_make_sample(struct paste_buffer *, int);
void paste_send_pane(struct paste_buffer *, struct window_pane *, void paste_send_pane(struct paste_buffer *, struct window_pane *,
const char *, int); const char *, int);
@ -1886,7 +1885,6 @@ const char *key_string_lookup_key(int);
/* server.c */ /* server.c */
extern struct clients clients; extern struct clients clients;
extern struct clients dead_clients; extern struct clients dead_clients;
extern struct paste_stack global_buffers;
int server_start(int, char *); int server_start(int, char *);
void server_update_socket(void); void server_update_socket(void);
void server_add_accept(int); void server_add_accept(int);

View File

@ -776,7 +776,7 @@ window_copy_key_input(struct window_pane *wp, int key)
*data->inputstr = '\0'; *data->inputstr = '\0';
break; break;
case MODEKEYEDIT_PASTE: case MODEKEYEDIT_PASTE:
if ((pb = paste_get_top(&global_buffers)) == NULL) if ((pb = paste_get_top()) == NULL)
break; break;
for (n = 0; n < pb->size; n++) { for (n = 0; n < pb->size; n++) {
ch = (u_char) pb->data[n]; ch = (u_char) pb->data[n];
@ -1465,8 +1465,8 @@ window_copy_copy_buffer(struct window_pane *wp, int idx, void *buf, size_t len)
if (idx == -1) { if (idx == -1) {
limit = options_get_number(&global_options, "buffer-limit"); limit = options_get_number(&global_options, "buffer-limit");
paste_add(&global_buffers, buf, len, limit); paste_add(buf, len, limit);
} else if (paste_replace(&global_buffers, idx, buf, len) != 0) } else if (paste_replace(idx, buf, len) != 0)
free(buf); free(buf);
} }
@ -1524,13 +1524,13 @@ window_copy_append_selection(struct window_pane *wp, int idx)
if (idx == -1) if (idx == -1)
idx = 0; idx = 0;
if (idx == 0 && paste_get_top(&global_buffers) == NULL) { if (idx == 0 && paste_get_top() == NULL) {
limit = options_get_number(&global_options, "buffer-limit"); limit = options_get_number(&global_options, "buffer-limit");
paste_add(&global_buffers, buf, len, limit); paste_add(buf, len, limit);
return; return;
} }
pb = paste_get_index(&global_buffers, idx); pb = paste_get_index(idx);
if (pb != NULL) { if (pb != NULL) {
buf = xrealloc(buf, 1, len + pb->size); buf = xrealloc(buf, 1, len + pb->size);
memmove(buf + pb->size, buf, len); memmove(buf + pb->size, buf, len);
@ -1538,7 +1538,7 @@ window_copy_append_selection(struct window_pane *wp, int idx)
len += pb->size; len += pb->size;
} }
if (paste_replace(&global_buffers, idx, buf, len) != 0) if (paste_replace(idx, buf, len) != 0)
free(buf); free(buf);
} }