summaryrefslogtreecommitdiffstats
path: root/cmd-set-buffer.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2014-03-06 11:50:07 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2014-03-06 11:50:07 +0000
commit8c0edcbfa3bd78bb8adaba13aad29a06d8988de8 (patch)
treeccd88d63c207ccb28a1af76bcc62c28ab863ef75 /cmd-set-buffer.c
parentb7f6356053638188a162df0d1912a6317e9d593d (diff)
Add setb -a to append and a copy mode append command, from J Raynor with minor
changes.
Diffstat (limited to 'cmd-set-buffer.c')
-rw-r--r--cmd-set-buffer.c65
1 files changed, 39 insertions, 26 deletions
diff --git a/cmd-set-buffer.c b/cmd-set-buffer.c
index fade4fe3..30a137cd 100644
--- a/cmd-set-buffer.c
+++ b/cmd-set-buffer.c
@@ -24,15 +24,15 @@
#include "tmux.h"
/*
- * Add or set a paste buffer.
+ * Add, set, or append to a paste buffer.
*/
enum cmd_retval cmd_set_buffer_exec(struct cmd *, struct cmd_q *);
const struct cmd_entry cmd_set_buffer_entry = {
"set-buffer", "setb",
- "b:", 1, 1,
- CMD_BUFFER_USAGE " data",
+ "ab:", 1, 1,
+ "[-a] " CMD_BUFFER_USAGE " data",
0,
NULL,
cmd_set_buffer_exec
@@ -41,35 +41,48 @@ const struct cmd_entry cmd_set_buffer_entry = {
enum cmd_retval
cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
{
- struct args *args = self->args;
- u_int limit;
- char *pdata, *cause;
- size_t psize;
- int buffer;
+ struct args *args = self->args;
+ struct paste_buffer *pb;
+ u_int limit;
+ char *pdata, *cause;
+ size_t psize, newsize;
+ int buffer;
limit = options_get_number(&global_options, "buffer-limit");
- pdata = xstrdup(args->argv[0]);
- psize = strlen(pdata);
+ psize = 0;
+ pdata = NULL;
- if (!args_has(args, 'b')) {
- paste_add(&global_buffers, pdata, psize, limit);
- return (CMD_RETURN_NORMAL);
- }
+ if (args_has(args, 'b')) {
+ buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
+ if (cause != NULL) {
+ cmdq_error(cmdq, "buffer %s", cause);
+ free(cause);
+ return (CMD_RETURN_ERROR);
+ }
+ pb = paste_get_index(&global_buffers, buffer);
+ if (pb == NULL) {
+ cmdq_error(cmdq, "no buffer %d", buffer);
+ return (CMD_RETURN_ERROR);
+ }
+ if (args_has(args, 'a')) {
+ psize = pb->size;
+ pdata = xmalloc(psize);
+ memcpy(pdata, pb->data, psize);
+ }
+ } else
+ buffer = -1;
+
+ newsize = strlen(args->argv[0]);
- buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
- if (cause != NULL) {
- cmdq_error(cmdq, "buffer %s", cause);
- free(cause);
- free(pdata);
- return (CMD_RETURN_ERROR);
- }
+ pdata = xrealloc(pdata, 1, psize + newsize);
+ memcpy(pdata + psize, args->argv[0], newsize);
+ psize += newsize;
- if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) {
- cmdq_error(cmdq, "no buffer %d", buffer);
- free(pdata);
- return (CMD_RETURN_ERROR);
- }
+ if (buffer == -1)
+ paste_add(&global_buffers, pdata, psize, limit);
+ else
+ paste_replace(&global_buffers, buffer, pdata, psize);
return (CMD_RETURN_NORMAL);
}