Merge delete-buffer into cmd-set-buffer.c and change the paste buffer

API so it has one paste_free() rather than free_top and free_name
(everywhere that uses it already has the right pointer).
This commit is contained in:
nicm 2015-09-11 14:41:50 +00:00
parent cfabe30bec
commit a3de5dbab1
6 changed files with 36 additions and 122 deletions

View File

@ -17,7 +17,6 @@ SRCS= alerts.c \
cmd-command-prompt.c \ cmd-command-prompt.c \
cmd-confirm-before.c \ cmd-confirm-before.c \
cmd-copy-mode.c \ cmd-copy-mode.c \
cmd-delete-buffer.c \
cmd-detach-client.c \ cmd-detach-client.c \
cmd-display-message.c \ cmd-display-message.c \
cmd-display-panes.c \ cmd-display-panes.c \

View File

@ -1,57 +0,0 @@
/* $OpenBSD$ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stdlib.h>
#include "tmux.h"
/*
* Delete a paste buffer.
*/
enum cmd_retval cmd_delete_buffer_exec(struct cmd *, struct cmd_q *);
const struct cmd_entry cmd_delete_buffer_entry = {
"delete-buffer", "deleteb",
"b:", 0, 0,
CMD_BUFFER_USAGE,
0,
cmd_delete_buffer_exec
};
enum cmd_retval
cmd_delete_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
const char *bufname;
if (!args_has(args, 'b')) {
paste_free_top();
return (CMD_RETURN_NORMAL);
}
bufname = args_get(args, 'b');
if (paste_free_name(bufname) != 0) {
cmdq_error(cmdq, "no buffer %s", bufname);
return (CMD_RETURN_ERROR);
}
return (CMD_RETURN_NORMAL);
}

View File

@ -102,12 +102,8 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
bufferevent_write(wp->event, "\033[201~", 6); bufferevent_write(wp->event, "\033[201~", 6);
} }
if (args_has(args, 'd')) { if (pb != NULL && args_has(args, 'd'))
if (bufname == NULL) paste_free(pb);
paste_free_top();
else
paste_free_name(bufname);
}
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

View File

@ -24,7 +24,7 @@
#include "tmux.h" #include "tmux.h"
/* /*
* Add, set, or append to a paste buffer. * Add, set, append to or delete a paste buffer.
*/ */
enum cmd_retval cmd_set_buffer_exec(struct cmd *, struct cmd_q *); enum cmd_retval cmd_set_buffer_exec(struct cmd *, struct cmd_q *);
@ -37,6 +37,14 @@ const struct cmd_entry cmd_set_buffer_entry = {
cmd_set_buffer_exec cmd_set_buffer_exec
}; };
const struct cmd_entry cmd_delete_buffer_entry = {
"delete-buffer", "deleteb",
"b:", 0, 0,
CMD_BUFFER_USAGE,
0,
cmd_set_buffer_exec
};
enum cmd_retval enum cmd_retval
cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq) cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
{ {
@ -46,31 +54,31 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
const char *bufname, *olddata; const char *bufname, *olddata;
size_t bufsize, newsize; size_t bufsize, newsize;
bufname = NULL; bufname = args_get(args, 'b');
if (bufname == NULL)
pb = paste_get_top(&bufname);
else
pb = paste_get_name(bufname);
if (args_has(args, 'n')) { if (self->entry == &cmd_delete_buffer_entry) {
if (args->argc > 0) { if (pb == NULL) {
cmdq_error(cmdq, "don't provide data with n flag"); cmdq_error(cmdq, "no buffer");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
paste_free(pb);
return (CMD_RETURN_NORMAL);
}
if (args_has(args, 'b')) if (args_has(args, 'n')) {
bufname = args_get(args, 'b'); if (pb == NULL) {
cmdq_error(cmdq, "no buffer");
if (bufname == NULL) { return (CMD_RETURN_ERROR);
pb = paste_get_top(&bufname);
if (pb == NULL) {
cmdq_error(cmdq, "no buffer");
return (CMD_RETURN_ERROR);
}
} }
if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) { if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) {
cmdq_error(cmdq, "%s", cause); cmdq_error(cmdq, "%s", cause);
free(cause); free(cause);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }
@ -78,19 +86,11 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cmdq_error(cmdq, "no data specified"); cmdq_error(cmdq, "no data specified");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
pb = NULL;
bufsize = 0;
bufdata = NULL;
if ((newsize = strlen(args->argv[0])) == 0) if ((newsize = strlen(args->argv[0])) == 0)
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
if (args_has(args, 'b')) { bufsize = 0;
bufname = args_get(args, 'b'); bufdata = NULL;
pb = paste_get_name(bufname);
} else if (args_has(args, 'a'))
pb = paste_get_top(&bufname);
if (args_has(args, 'a') && pb != NULL) { if (args_has(args, 'a') && pb != NULL) {
olddata = paste_buffer_data(pb, &bufsize); olddata = paste_buffer_data(pb, &bufsize);

37
paste.c
View File

@ -111,18 +111,6 @@ paste_get_top(const char **name)
return (pb); return (pb);
} }
/* Free the most recent buffer. */
int
paste_free_top(void)
{
struct paste_buffer *pb;
pb = paste_get_top(NULL);
if (pb == NULL)
return (-1);
return (paste_free_name(pb->name));
}
/* Get a paste buffer by name. */ /* Get a paste buffer by name. */
struct paste_buffer * struct paste_buffer *
paste_get_name(const char *name) paste_get_name(const char *name)
@ -136,20 +124,10 @@ paste_get_name(const char *name)
return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind)); return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind));
} }
/* Free a paste buffer by name. */ /* Free a paste buffer. */
int void
paste_free_name(const char *name) paste_free(struct paste_buffer *pb)
{ {
struct paste_buffer *pb, pbfind;
if (name == NULL || *name == '\0')
return (-1);
pbfind.name = (char *)name;
pb = RB_FIND(paste_name_tree, &paste_by_name, &pbfind);
if (pb == NULL)
return (-1);
RB_REMOVE(paste_name_tree, &paste_by_name, pb); RB_REMOVE(paste_name_tree, &paste_by_name, pb);
RB_REMOVE(paste_time_tree, &paste_by_time, pb); RB_REMOVE(paste_time_tree, &paste_by_time, pb);
if (pb->automatic) if (pb->automatic)
@ -158,7 +136,6 @@ paste_free_name(const char *name)
free(pb->data); free(pb->data);
free(pb->name); free(pb->name);
free(pb); free(pb);
return (0);
} }
/* /*
@ -179,7 +156,7 @@ paste_add(char *data, size_t size)
if (paste_num_automatic < limit) if (paste_num_automatic < limit)
break; break;
if (pb->automatic) if (pb->automatic)
paste_free_name(pb->name); paste_free(pb);
} }
pb = xmalloc(sizeof *pb); pb = xmalloc(sizeof *pb);
@ -257,7 +234,7 @@ paste_rename(const char *oldname, const char *newname, char **cause)
int int
paste_set(char *data, size_t size, const char *name, char **cause) paste_set(char *data, size_t size, const char *name, char **cause)
{ {
struct paste_buffer *pb; struct paste_buffer *pb, *old;
if (cause != NULL) if (cause != NULL)
*cause = NULL; *cause = NULL;
@ -288,8 +265,8 @@ paste_set(char *data, size_t size, const char *name, char **cause)
pb->automatic = 0; pb->automatic = 0;
pb->order = paste_next_order++; pb->order = paste_next_order++;
if (paste_get_name(name) != NULL) if ((old = paste_get_name(name)) != NULL)
paste_free_name(name); paste_free(old);
RB_INSERT(paste_name_tree, &paste_by_name, pb); RB_INSERT(paste_name_tree, &paste_by_name, pb);
RB_INSERT(paste_time_tree, &paste_by_time, pb); RB_INSERT(paste_time_tree, &paste_by_time, pb);

3
tmux.h
View File

@ -1435,8 +1435,7 @@ const char *paste_buffer_data(struct paste_buffer *, size_t *);
struct paste_buffer *paste_walk(struct paste_buffer *); struct paste_buffer *paste_walk(struct paste_buffer *);
struct paste_buffer *paste_get_top(const char **); struct paste_buffer *paste_get_top(const char **);
struct paste_buffer *paste_get_name(const char *); struct paste_buffer *paste_get_name(const char *);
int paste_free_top(void); void paste_free(struct paste_buffer *);
int paste_free_name(const char *);
void paste_add(char *, size_t); void paste_add(char *, size_t);
int paste_rename(const char *, const char *, char **); int paste_rename(const char *, const char *, char **);
int paste_set(char *, size_t, const char *, char **); int paste_set(char *, size_t, const char *, char **);