xref: /openbsd-src/usr.bin/tmux/grid-view.c (revision d59bb9942320b767f2a19aaa7690c8c6e30b724c)
1 /* $OpenBSD: grid-view.c,v 1.28 2017/02/16 12:43:08 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <string.h>
22 
23 #include "tmux.h"
24 
25 /*
26  * Grid view functions. These work using coordinates relative to the visible
27  * screen area.
28  */
29 
30 #define grid_view_x(gd, x) (x)
31 #define grid_view_y(gd, y) ((gd)->hsize + (y))
32 
33 /* Get cell. */
34 void
35 grid_view_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
36 {
37 	grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
38 }
39 
40 /* Set cell. */
41 void
42 grid_view_set_cell(struct grid *gd, u_int px, u_int py,
43     const struct grid_cell *gc)
44 {
45 	grid_set_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
46 }
47 
48 /* Set cells. */
49 void
50 grid_view_set_cells(struct grid *gd, u_int px, u_int py,
51     const struct grid_cell *gc, const char *s, size_t slen)
52 {
53 	grid_set_cells(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc, s,
54 	    slen);
55 }
56 
57 /* Clear into history. */
58 void
59 grid_view_clear_history(struct grid *gd, u_int bg)
60 {
61 	struct grid_line	*gl;
62 	u_int			 yy, last;
63 
64 	/* Find the last used line. */
65 	last = 0;
66 	for (yy = 0; yy < gd->sy; yy++) {
67 		gl = &gd->linedata[grid_view_y(gd, yy)];
68 		if (gl->cellused != 0)
69 			last = yy + 1;
70 	}
71 	if (last == 0) {
72 		grid_view_clear(gd, 0, 0, gd->sx, gd->sy, bg);
73 		return;
74 	}
75 
76 	/* Scroll the lines into the history. */
77 	for (yy = 0; yy < last; yy++) {
78 		grid_collect_history(gd, bg);
79 		grid_scroll_history(gd, bg);
80 	}
81 	if (last < gd->sy)
82 		grid_view_clear(gd, 0, 0, gd->sx, gd->sy - last, bg);
83 	gd->hscrolled = 0;
84 }
85 
86 /* Clear area. */
87 void
88 grid_view_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny,
89     u_int bg)
90 {
91 	px = grid_view_x(gd, px);
92 	py = grid_view_y(gd, py);
93 
94 	grid_clear(gd, px, py, nx, ny, bg);
95 }
96 
97 /* Scroll region up. */
98 void
99 grid_view_scroll_region_up(struct grid *gd, u_int rupper, u_int rlower)
100 {
101 	if (gd->flags & GRID_HISTORY) {
102 		grid_collect_history(gd, 8);
103 		if (rupper == 0 && rlower == gd->sy - 1)
104 			grid_scroll_history(gd, 8);
105 		else {
106 			rupper = grid_view_y(gd, rupper);
107 			rlower = grid_view_y(gd, rlower);
108 			grid_scroll_history_region(gd, rupper, rlower);
109 		}
110 	} else {
111 		rupper = grid_view_y(gd, rupper);
112 		rlower = grid_view_y(gd, rlower);
113 		grid_move_lines(gd, rupper, rupper + 1, rlower - rupper, 8);
114 	}
115 }
116 
117 /* Scroll region down. */
118 void
119 grid_view_scroll_region_down(struct grid *gd, u_int rupper, u_int rlower)
120 {
121 	rupper = grid_view_y(gd, rupper);
122 	rlower = grid_view_y(gd, rlower);
123 
124 	grid_move_lines(gd, rupper + 1, rupper, rlower - rupper, 8);
125 }
126 
127 /* Insert lines. */
128 void
129 grid_view_insert_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
130 {
131 	u_int	sy;
132 
133 	py = grid_view_y(gd, py);
134 
135 	sy = grid_view_y(gd, gd->sy);
136 
137 	grid_move_lines(gd, py + ny, py, sy - py - ny, bg);
138 }
139 
140 /* Insert lines in region. */
141 void
142 grid_view_insert_lines_region(struct grid *gd, u_int rlower, u_int py,
143     u_int ny, u_int bg)
144 {
145 	u_int	ny2;
146 
147 	rlower = grid_view_y(gd, rlower);
148 
149 	py = grid_view_y(gd, py);
150 
151 	ny2 = rlower + 1 - py - ny;
152 	grid_move_lines(gd, rlower + 1 - ny2, py, ny2, bg);
153 	grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
154 }
155 
156 /* Delete lines. */
157 void
158 grid_view_delete_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
159 {
160 	u_int	sy;
161 
162 	py = grid_view_y(gd, py);
163 
164 	sy = grid_view_y(gd, gd->sy);
165 
166 	grid_move_lines(gd, py, py + ny, sy - py - ny, bg);
167 	grid_clear(gd, 0, sy - ny, gd->sx, py + ny - (sy - ny), bg);
168 }
169 
170 /* Delete lines inside scroll region. */
171 void
172 grid_view_delete_lines_region(struct grid *gd, u_int rlower, u_int py,
173     u_int ny, u_int bg)
174 {
175 	u_int	ny2;
176 
177 	rlower = grid_view_y(gd, rlower);
178 
179 	py = grid_view_y(gd, py);
180 
181 	ny2 = rlower + 1 - py - ny;
182 	grid_move_lines(gd, py, py + ny, ny2, bg);
183 	grid_clear(gd, 0, py + ny2, gd->sx, ny - ny2, bg);
184 }
185 
186 /* Insert characters. */
187 void
188 grid_view_insert_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
189 {
190 	u_int	sx;
191 
192 	px = grid_view_x(gd, px);
193 	py = grid_view_y(gd, py);
194 
195 	sx = grid_view_x(gd, gd->sx);
196 
197 	if (px >= sx - 1)
198 		grid_clear(gd, px, py, 1, 1, bg);
199 	else
200 		grid_move_cells(gd, px + nx, px, py, sx - px - nx, bg);
201 }
202 
203 /* Delete characters. */
204 void
205 grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx, u_int bg)
206 {
207 	u_int	sx;
208 
209 	px = grid_view_x(gd, px);
210 	py = grid_view_y(gd, py);
211 
212 	sx = grid_view_x(gd, gd->sx);
213 
214 	grid_move_cells(gd, px, px + nx, py, sx - px - nx, bg);
215 	grid_clear(gd, sx - nx, py, px + nx - (sx - nx), 1, bg);
216 }
217 
218 /* Convert cells into a string. */
219 char *
220 grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
221 {
222 	px = grid_view_x(gd, px);
223 	py = grid_view_y(gd, py);
224 
225 	return (grid_string_cells(gd, px, py, nx, NULL, 0, 0, 0));
226 }
227