choose-tree: Reset top when toggling items
[tmux-openbsd.git] / cmd-capture-pane.c
blob779cbe08a618b7d5c867885c3af948eef8c774f0
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 NULL,
46 cmd_capture_pane_exec
49 char *
50 cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
52 buf = xrealloc(buf, 1, *len + linelen + 1);
53 memcpy(buf + *len, line, linelen);
54 *len += linelen;
55 return (buf);
58 char *
59 cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
60 size_t *len)
62 char *buf, *line, tmp[5];
63 size_t linelen;
64 u_int i;
66 if (wp->ictx.since_ground == NULL)
67 return (xstrdup(""));
69 line = EVBUFFER_DATA(wp->ictx.since_ground);
70 linelen = EVBUFFER_LENGTH(wp->ictx.since_ground);
72 buf = xstrdup("");
73 if (args_has(args, 'C')) {
74 for (i = 0; i < linelen; i++) {
75 if (line[i] >= ' ') {
76 tmp[0] = line[i];
77 tmp[1] = '\0';
78 } else
79 xsnprintf(tmp, sizeof tmp, "\\%03o", line[i]);
80 buf = cmd_capture_pane_append(buf, len, tmp,
81 strlen(tmp));
83 } else
84 buf = cmd_capture_pane_append(buf, len, line, linelen);
85 return (buf);
88 char *
89 cmd_capture_pane_history(struct args *args, struct cmd_q *cmdq,
90 struct window_pane *wp, size_t *len)
92 struct grid *gd;
93 const struct grid_line *gl;
94 struct grid_cell *gc = NULL;
95 int n, with_codes, escape_c0, join_lines;
96 u_int i, sx, top, bottom, tmp;
97 char *cause, *buf, *line;
98 size_t linelen;
100 sx = screen_size_x(&wp->base);
101 if (args_has(args, 'a')) {
102 gd = wp->saved_grid;
103 if (gd == NULL) {
104 if (!args_has(args, 'q')) {
105 cmdq_error(cmdq, "no alternate screen");
106 return (NULL);
108 return (xstrdup(""));
110 } else
111 gd = wp->base.grid;
113 n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
114 if (cause != NULL) {
115 top = gd->hsize;
116 free(cause);
117 } else if (n < 0 && (u_int) -n > gd->hsize)
118 top = 0;
119 else
120 top = gd->hsize + n;
121 if (top > gd->hsize + gd->sy - 1)
122 top = gd->hsize + gd->sy - 1;
124 n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
125 if (cause != NULL) {
126 bottom = gd->hsize + gd->sy - 1;
127 free(cause);
128 } else if (n < 0 && (u_int) -n > gd->hsize)
129 bottom = 0;
130 else
131 bottom = gd->hsize + n;
132 if (bottom > gd->hsize + gd->sy - 1)
133 bottom = gd->hsize + gd->sy - 1;
135 if (bottom < top) {
136 tmp = bottom;
137 bottom = top;
138 top = tmp;
141 with_codes = args_has(args, 'e');
142 escape_c0 = args_has(args, 'C');
143 join_lines = args_has(args, 'J');
145 buf = NULL;
146 for (i = top; i <= bottom; i++) {
147 line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
148 escape_c0, !join_lines);
149 linelen = strlen(line);
151 buf = cmd_capture_pane_append(buf, len, line, linelen);
153 gl = grid_peek_line(gd, i);
154 if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
155 buf[(*len)++] = '\n';
157 free(line);
159 return (buf);
162 enum cmd_retval
163 cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
165 struct args *args = self->args;
166 struct client *c;
167 struct window_pane *wp;
168 char *buf, *cause;
169 int buffer;
170 u_int limit;
171 size_t len;
173 if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
174 return (CMD_RETURN_ERROR);
176 len = 0;
177 if (args_has(args, 'P'))
178 buf = cmd_capture_pane_pending(args, wp, &len);
179 else
180 buf = cmd_capture_pane_history(args, cmdq, wp, &len);
181 if (buf == NULL)
182 return (CMD_RETURN_ERROR);
184 if (args_has(args, 'p')) {
185 c = cmdq->client;
186 if (c == NULL ||
187 (c->session != NULL && !(c->flags & CLIENT_CONTROL))) {
188 cmdq_error(cmdq, "can't write to stdout");
189 return (CMD_RETURN_ERROR);
191 evbuffer_add(c->stdout_data, buf, len);
192 if (args_has(args, 'P') && len > 0)
193 evbuffer_add(c->stdout_data, "\n", 1);
194 server_push_stdout(c);
195 } else {
196 limit = options_get_number(&global_options, "buffer-limit");
197 if (!args_has(args, 'b')) {
198 paste_add(&global_buffers, buf, len, limit);
199 return (CMD_RETURN_NORMAL);
202 buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
203 if (cause != NULL) {
204 cmdq_error(cmdq, "buffer %s", cause);
205 free(buf);
206 free(cause);
207 return (CMD_RETURN_ERROR);
210 if (paste_replace(&global_buffers, buffer, buf, len) != 0) {
211 cmdq_error(cmdq, "no buffer %d", buffer);
212 free(buf);
213 return (CMD_RETURN_ERROR);
217 return (CMD_RETURN_NORMAL);