When resizing the copy mode screen, don't allow it to end up with the
[tmux-openbsd.git] / screen.c
blob333ff969f8058cd1e57293da54c22356ed894ae6
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@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>
23 #include <unistd.h>
24 #include <vis.h>
26 #include "tmux.h"
28 void screen_resize_x(struct screen *, u_int);
29 void screen_resize_y(struct screen *, u_int);
31 /* Create a new screen. */
32 void
33 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
35 char hn[MAXHOSTNAMELEN];
37 s->grid = grid_create(sx, sy, hlimit);
39 if (gethostname(hn, MAXHOSTNAMELEN) == 0)
40 s->title = xstrdup(hn);
41 else
42 s->title = xstrdup("");
44 s->tabs = NULL;
46 screen_reinit(s);
49 /* Reinitialise screen. */
50 void
51 screen_reinit(struct screen *s)
53 s->cx = 0;
54 s->cy = 0;
56 s->rupper = 0;
57 s->rlower = screen_size_y(s) - 1;
59 s->mode = MODE_CURSOR | MODE_WRAP;
61 screen_reset_tabs(s);
63 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
65 screen_clear_selection(s);
68 /* Destroy a screen. */
69 void
70 screen_free(struct screen *s)
72 if (s->tabs != NULL)
73 xfree(s->tabs);
74 xfree(s->title);
75 grid_destroy(s->grid);
78 /* Reset tabs to default, eight spaces apart. */
79 void
80 screen_reset_tabs(struct screen *s)
82 u_int i;
84 if (s->tabs != NULL)
85 xfree(s->tabs);
87 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
88 fatal("bit_alloc failed");
89 for (i = 8; i < screen_size_x(s); i += 8)
90 bit_set(s->tabs, i);
93 /* Set screen title. */
94 void
95 screen_set_title(struct screen *s, const char *title)
97 char tmp[BUFSIZ];
99 strnvis(tmp, title, sizeof tmp, VIS_OCTAL|VIS_TAB|VIS_NL);
101 xfree(s->title);
102 s->title = xstrdup(tmp);
105 /* Resize screen. */
106 void
107 screen_resize(struct screen *s, u_int sx, u_int sy)
109 if (sx < 1)
110 sx = 1;
111 if (sy < 1)
112 sy = 1;
114 if (sx != screen_size_x(s)) {
115 screen_resize_x(s, sx);
118 * It is unclear what should happen to tabs on resize. xterm
119 * seems to try and maintain them, rxvt resets them. Resetting
120 * is simpler and more reliable so let's do that.
122 screen_reset_tabs(s);
125 if (sy != screen_size_y(s))
126 screen_resize_y(s, sy);
129 void
130 screen_resize_x(struct screen *s, u_int sx)
132 struct grid *gd = s->grid;
134 if (sx == 0)
135 fatalx("zero size");
138 * Treat resizing horizontally simply: just ensure the cursor is
139 * on-screen and change the size. Don't bother to truncate any lines -
140 * then the data should be accessible if the size is then incrased.
142 * The only potential wrinkle is if UTF-8 double-width characters are
143 * left in the last column, but UTF-8 terminals should deal with this
144 * sanely.
146 if (s->cx >= sx)
147 s->cx = sx - 1;
148 gd->sx = sx;
151 void
152 screen_resize_y(struct screen *s, u_int sy)
154 struct grid *gd = s->grid;
155 u_int needed, available, oldy, i;
157 if (sy == 0)
158 fatalx("zero size");
159 oldy = screen_size_y(s);
162 * When resizing:
164 * If the height is decreasing, delete lines from the bottom until
165 * hitting the cursor, then push lines from the top into the history.
167 * When increasing, pull as many lines as possible from the history to
168 * the top, then fill the remaining with blanks at the bottom.
171 /* Size decreasing. */
172 if (sy < oldy) {
173 needed = oldy - sy;
175 /* Delete as many lines as possible from the bottom. */
176 available = oldy - 1 - s->cy;
177 if (available > 0) {
178 if (available > needed)
179 available = needed;
180 grid_view_delete_lines(gd, oldy - available, available);
182 needed -= available;
185 * Now just increase the history size, if possible, to take
186 * over the lines which are left. If history is off, delete
187 * lines from the top.
189 * XXX Should apply history limit?
191 available = s->cy;
192 if (gd->flags & GRID_HISTORY)
193 gd->hsize += needed;
194 else if (needed > 0 && available > 0) {
195 if (available > needed)
196 available = needed;
197 grid_view_delete_lines(gd, 0, available);
199 s->cy -= needed;
202 /* Resize line arrays. */
203 gd->linedata = xrealloc(
204 gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
206 /* Size increasing. */
207 if (sy > oldy) {
208 needed = sy - oldy;
211 * Try to pull as much as possible out of the history, if is
212 * is enabled.
214 available = gd->hsize;
215 if (gd->flags & GRID_HISTORY && available > 0) {
216 if (available > needed)
217 available = needed;
218 gd->hsize -= available;
219 s->cy += available;
220 } else
221 available = 0;
222 needed -= available;
224 /* Then fill the rest in with blanks. */
225 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
226 memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
229 /* Set the new size, and reset the scroll region. */
230 gd->sy = sy;
231 s->rupper = 0;
232 s->rlower = screen_size_y(s) - 1;
235 /* Set selection. */
236 void
237 screen_set_selection(struct screen *s, u_int sx, u_int sy,
238 u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
240 struct screen_sel *sel = &s->sel;
242 memcpy(&sel->cell, gc, sizeof sel->cell);
243 sel->flag = 1;
244 sel->rectflag = rectflag;
246 sel->sx = sx; sel->sy = sy;
247 sel->ex = ex; sel->ey = ey;
250 /* Clear selection. */
251 void
252 screen_clear_selection(struct screen *s)
254 struct screen_sel *sel = &s->sel;
256 sel->flag = 0;
259 /* Check if cell in selection. */
261 screen_check_selection(struct screen *s, u_int px, u_int py)
263 struct screen_sel *sel = &s->sel;
265 if (!sel->flag)
266 return (0);
268 if (sel->rectflag) {
269 if (sel->sy < sel->ey) {
270 /* start line < end line -- downward selection. */
271 if (py < sel->sy || py > sel->ey)
272 return (0);
273 } else if (sel->sy > sel->ey) {
274 /* start line > end line -- upward selection. */
275 if (py > sel->sy || py < sel->ey)
276 return (0);
277 } else {
278 /* starting line == ending line. */
279 if (py != sel->sy)
280 return (0);
284 * Need to include the selection start row, but not the cursor
285 * row, which means the selection changes depending on which
286 * one is on the left.
288 if (sel->ex < sel->sx) {
289 /* Cursor (ex) is on the left. */
290 if (px <= sel->ex)
291 return (0);
293 if (px > sel->sx)
294 return (0);
295 } else {
296 /* Selection start (sx) is on the left. */
297 if (px < sel->sx)
298 return (0);
300 if (px >= sel->ex)
301 return (0);
303 } else {
305 * Like emacs, keep the top-left-most character, and drop the
306 * bottom-right-most, regardless of copy direction.
308 if (sel->sy < sel->ey) {
309 /* starting line < ending line -- downward selection. */
310 if (py < sel->sy || py > sel->ey)
311 return (0);
313 if ((py == sel->sy && px < sel->sx)
314 || (py == sel->ey && px > sel->ex))
315 return (0);
316 } else if (sel->sy > sel->ey) {
317 /* starting line > ending line -- upward selection. */
318 if (py > sel->sy || py < sel->ey)
319 return (0);
321 if ((py == sel->sy && px >= sel->sx)
322 || (py == sel->ey && px < sel->ex))
323 return (0);
324 } else {
325 /* starting line == ending line. */
326 if (py != sel->sy)
327 return (0);
329 if (sel->ex < sel->sx) {
330 /* cursor (ex) is on the left */
331 if (px > sel->sx || px < sel->ex)
332 return (0);
333 } else {
334 /* selection start (sx) is on the left */
335 if (px < sel->sx || px > sel->ex)
336 return (0);
341 return (1);