Allow attach-session -t to accept a window and pane to select them on
[tmux-openbsd.git] / cmd-capture-pane.c
blobcf9a2f49b04cf2a8b3bd9cb4b13a8be62b59fa73
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Jonathan Alvarado <radobobo@users.sourceforge.net>
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>
24 #include "tmux.h"
27 * Write the entire contents of a pane to a buffer or stdout.
30 enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmd_q *);
32 char *cmd_capture_pane_append(char *, size_t *, char *, size_t);
33 char *cmd_capture_pane_pending(struct args *, struct window_pane *,
34 size_t *);
35 char *cmd_capture_pane_history(struct args *, struct cmd_q *,
36 struct window_pane *, size_t *);
38 const struct cmd_entry cmd_capture_pane_entry = {
39 "capture-pane", "capturep",
40 "ab:CeE:JpPqS:t:", 0, 0,
41 "[-aCeJpPq] [-b buffer-index] [-E end-line] [-S start-line]"
42 CMD_TARGET_PANE_USAGE,
44 NULL,
45 cmd_capture_pane_exec
48 char *
49 cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
51 buf = xrealloc(buf, 1, *len + linelen + 1);
52 memcpy(buf + *len, line, linelen);
53 *len += linelen;
54 return (buf);
57 char *
58 cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
59 size_t *len)
61 char *buf, *line, tmp[5];
62 size_t linelen;
63 u_int i;
65 if (wp->ictx.since_ground == NULL)
66 return (xstrdup(""));
68 line = EVBUFFER_DATA(wp->ictx.since_ground);
69 linelen = EVBUFFER_LENGTH(wp->ictx.since_ground);
71 buf = xstrdup("");
72 if (args_has(args, 'C')) {
73 for (i = 0; i < linelen; i++) {
74 if (line[i] >= ' ') {
75 tmp[0] = line[i];
76 tmp[1] = '\0';
77 } else
78 xsnprintf(tmp, sizeof tmp, "\\%03o", line[i]);
79 buf = cmd_capture_pane_append(buf, len, tmp,
80 strlen(tmp));
82 } else
83 buf = cmd_capture_pane_append(buf, len, line, linelen);
84 return (buf);
87 char *
88 cmd_capture_pane_history(struct args *args, struct cmd_q *cmdq,
89 struct window_pane *wp, size_t *len)
91 struct grid *gd;
92 const struct grid_line *gl;
93 struct grid_cell *gc = NULL;
94 int n, with_codes, escape_c0, join_lines;
95 u_int i, sx, top, bottom, tmp;
96 char *cause, *buf, *line;
97 size_t linelen;
99 sx = screen_size_x(&wp->base);
100 if (args_has(args, 'a')) {
101 gd = wp->saved_grid;
102 if (gd == NULL) {
103 if (!args_has(args, 'q')) {
104 cmdq_error(cmdq, "no alternate screen");
105 return (NULL);
107 return (xstrdup(""));
109 } else
110 gd = wp->base.grid;
112 n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
113 if (cause != NULL) {
114 top = gd->hsize;
115 free(cause);
116 } else if (n < 0 && (u_int) -n > gd->hsize)
117 top = 0;
118 else
119 top = gd->hsize + n;
120 if (top > gd->hsize + gd->sy - 1)
121 top = gd->hsize + gd->sy - 1;
123 n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
124 if (cause != NULL) {
125 bottom = gd->hsize + gd->sy - 1;
126 free(cause);
127 } else if (n < 0 && (u_int) -n > gd->hsize)
128 bottom = 0;
129 else
130 bottom = gd->hsize + n;
131 if (bottom > gd->hsize + gd->sy - 1)
132 bottom = gd->hsize + gd->sy - 1;
134 if (bottom < top) {
135 tmp = bottom;
136 bottom = top;
137 top = tmp;
140 with_codes = args_has(args, 'e');
141 escape_c0 = args_has(args, 'C');
142 join_lines = args_has(args, 'J');
144 buf = NULL;
145 for (i = top; i <= bottom; i++) {
146 line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
147 escape_c0, !join_lines);
148 linelen = strlen(line);
150 buf = cmd_capture_pane_append(buf, len, line, linelen);
152 gl = grid_peek_line(gd, i);
153 if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
154 buf[(*len)++] = '\n';
156 free(line);
158 return (buf);
161 enum cmd_retval
162 cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
164 struct args *args = self->args;
165 struct client *c;
166 struct window_pane *wp;
167 char *buf, *cause;
168 int buffer;
169 u_int limit;
170 size_t len;
172 if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
173 return (CMD_RETURN_ERROR);
175 len = 0;
176 if (args_has(args, 'P'))
177 buf = cmd_capture_pane_pending(args, wp, &len);
178 else
179 buf = cmd_capture_pane_history(args, cmdq, wp, &len);
180 if (buf == NULL)
181 return (CMD_RETURN_ERROR);
183 if (args_has(args, 'p')) {
184 c = cmdq->client;
185 if (c == NULL ||
186 (c->session != NULL && !(c->flags & CLIENT_CONTROL))) {
187 cmdq_error(cmdq, "can't write to stdout");
188 return (CMD_RETURN_ERROR);
190 evbuffer_add(c->stdout_data, buf, len);
191 if (args_has(args, 'P') && len > 0)
192 evbuffer_add(c->stdout_data, "\n", 1);
193 server_push_stdout(c);
194 } else {
195 limit = options_get_number(&global_options, "buffer-limit");
196 if (!args_has(args, 'b')) {
197 paste_add(&global_buffers, buf, len, limit);
198 return (CMD_RETURN_NORMAL);
201 buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
202 if (cause != NULL) {
203 cmdq_error(cmdq, "buffer %s", cause);
204 free(buf);
205 free(cause);
206 return (CMD_RETURN_ERROR);
209 if (paste_replace(&global_buffers, buffer, buf, len) != 0) {
210 cmdq_error(cmdq, "no buffer %d", buffer);
211 free(buf);
212 return (CMD_RETURN_ERROR);
216 return (CMD_RETURN_NORMAL);