This commit was manufactured by cvs2svn to create tag 'TMUX_1_2'.
[tmux.git] / screen.c
blob122b24e8cc71ab95275d2fee8c9cd2a50f43a359
1 /* $Id: screen.c,v 1.99 2010-02-08 18:13:17 tcunha Exp $ */
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>
24 #include "tmux.h"
26 void screen_resize_x(struct screen *, u_int);
27 void screen_resize_y(struct screen *, u_int);
29 /* Create a new screen. */
30 void
31 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
33 s->grid = grid_create(sx, sy, hlimit);
35 s->title = xstrdup("");
37 s->tabs = NULL;
39 screen_reinit(s);
42 /* Reinitialise screen. */
43 void
44 screen_reinit(struct screen *s)
46 s->cx = 0;
47 s->cy = 0;
49 s->rupper = 0;
50 s->rlower = screen_size_y(s) - 1;
52 s->mode = MODE_CURSOR;
54 screen_reset_tabs(s);
56 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
58 screen_clear_selection(s);
61 /* Destroy a screen. */
62 void
63 screen_free(struct screen *s)
65 if (s->tabs != NULL)
66 xfree(s->tabs);
67 xfree(s->title);
68 grid_destroy(s->grid);
71 /* Reset tabs to default, eight spaces apart. */
72 void
73 screen_reset_tabs(struct screen *s)
75 u_int i;
77 if (s->tabs != NULL)
78 xfree(s->tabs);
80 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
81 fatal("bit_alloc failed");
82 for (i = 8; i < screen_size_x(s); i += 8)
83 bit_set(s->tabs, i);
86 /* Set screen title. */
87 void
88 screen_set_title(struct screen *s, const char *title)
90 char tmp[BUFSIZ];
92 strnvis(tmp, title, sizeof tmp, VIS_OCTAL|VIS_TAB|VIS_NL);
94 xfree(s->title);
95 s->title = xstrdup(tmp);
98 /* Resize screen. */
99 void
100 screen_resize(struct screen *s, u_int sx, u_int sy)
102 if (sx < 1)
103 sx = 1;
104 if (sy < 1)
105 sy = 1;
107 if (sx != screen_size_x(s)) {
108 screen_resize_x(s, sx);
111 * It is unclear what should happen to tabs on resize. xterm
112 * seems to try and maintain them, rxvt resets them. Resetting
113 * is simpler and more reliable so let's do that.
115 screen_reset_tabs(s);
118 if (sy != screen_size_y(s))
119 screen_resize_y(s, sy);
122 void
123 screen_resize_x(struct screen *s, u_int sx)
125 struct grid *gd = s->grid;
127 if (sx == 0)
128 fatalx("zero size");
131 * Treat resizing horizontally simply: just ensure the cursor is
132 * on-screen and change the size. Don't bother to truncate any lines -
133 * then the data should be accessible if the size is then incrased.
135 * The only potential wrinkle is if UTF-8 double-width characters are
136 * left in the last column, but UTF-8 terminals should deal with this
137 * sanely.
139 if (s->cx >= sx)
140 s->cx = sx - 1;
141 gd->sx = sx;
144 void
145 screen_resize_y(struct screen *s, u_int sy)
147 struct grid *gd = s->grid;
148 u_int needed, available, oldy, i;
150 if (sy == 0)
151 fatalx("zero size");
152 oldy = screen_size_y(s);
155 * When resizing:
157 * If the height is decreasing, delete lines from the bottom until
158 * hitting the cursor, then push lines from the top into the history.
160 * When increasing, pull as many lines as possible from the history to
161 * the top, then fill the remaining with blanks at the bottom.
164 /* Size decreasing. */
165 if (sy < oldy) {
166 needed = oldy - sy;
168 /* Delete as many lines as possible from the bottom. */
169 available = oldy - 1 - s->cy;
170 if (available > 0) {
171 if (available > needed)
172 available = needed;
173 grid_view_delete_lines(gd, oldy - available, available);
175 needed -= available;
178 * Now just increase the history size, if possible, to take
179 * over the lines which are left. If history is off, delete
180 * lines from the top.
182 * XXX Should apply history limit?
184 available = s->cy;
185 if (gd->flags & GRID_HISTORY)
186 gd->hsize += needed;
187 else if (needed > 0 && available > 0) {
188 if (available > needed)
189 available = needed;
190 grid_view_delete_lines(gd, 0, available);
192 s->cy -= needed;
195 /* Resize line arrays. */
196 gd->linedata = xrealloc(
197 gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
199 /* Size increasing. */
200 if (sy > oldy) {
201 needed = sy - oldy;
204 * Try to pull as much as possible out of the history, if is
205 * is enabled.
207 available = gd->hsize;
208 if (gd->flags & GRID_HISTORY && available > 0) {
209 if (available > needed)
210 available = needed;
211 gd->hsize -= available;
212 s->cy += available;
213 } else
214 available = 0;
215 needed -= available;
217 /* Then fill the rest in with blanks. */
218 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
219 memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
222 /* Set the new size, and reset the scroll region. */
223 gd->sy = sy;
224 s->rupper = 0;
225 s->rlower = screen_size_y(s) - 1;
228 /* Set selection. */
229 void
230 screen_set_selection(struct screen *s, u_int sx, u_int sy,
231 u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
233 struct screen_sel *sel = &s->sel;
235 memcpy(&sel->cell, gc, sizeof sel->cell);
236 sel->flag = 1;
237 sel->rectflag = rectflag;
239 sel->sx = sx; sel->sy = sy;
240 sel->ex = ex; sel->ey = ey;
243 /* Clear selection. */
244 void
245 screen_clear_selection(struct screen *s)
247 struct screen_sel *sel = &s->sel;
249 sel->flag = 0;
252 /* Check if cell in selection. */
254 screen_check_selection(struct screen *s, u_int px, u_int py)
256 struct screen_sel *sel = &s->sel;
258 if (!sel->flag)
259 return (0);
261 if (sel->rectflag) {
262 if (sel->sy < sel->ey) {
263 /* start line < end line -- downward selection. */
264 if (py < sel->sy || py > sel->ey)
265 return (0);
266 } else if (sel->sy > sel->ey) {
267 /* start line > end line -- upward selection. */
268 if (py > sel->sy || py < sel->ey)
269 return (0);
270 } else {
271 /* starting line == ending line. */
272 if (py != sel->sy)
273 return (0);
277 * Need to include the selection start row, but not the cursor
278 * row, which means the selection changes depending on which
279 * one is on the left.
281 if (sel->ex < sel->sx) {
282 /* Cursor (ex) is on the left. */
283 if (px <= sel->ex)
284 return (0);
286 if (px > sel->sx)
287 return (0);
288 } else {
289 /* Selection start (sx) is on the left. */
290 if (px < sel->sx)
291 return (0);
293 if (px >= sel->ex)
294 return (0);
296 } else {
298 * Like emacs, keep the top-left-most character, and drop the
299 * bottom-right-most, regardless of copy direction.
301 if (sel->sy < sel->ey) {
302 /* starting line < ending line -- downward selection. */
303 if (py < sel->sy || py > sel->ey)
304 return (0);
306 if ((py == sel->sy && px < sel->sx)
307 || (py == sel->ey && px > sel->ex))
308 return (0);
309 } else if (sel->sy > sel->ey) {
310 /* starting line > ending line -- upward selection. */
311 if (py > sel->sy || py < sel->ey)
312 return (0);
314 if ((py == sel->sy && px >= sel->sx)
315 || (py == sel->ey && px < sel->ex))
316 return (0);
317 } else {
318 /* starting line == ending line. */
319 if (py != sel->sy)
320 return (0);
322 if (sel->ex < sel->sx) {
323 /* cursor (ex) is on the left */
324 if (px > sel->sx || px < sel->ex)
325 return (0);
326 } else {
327 /* selection start (sx) is on the left */
328 if (px < sel->sx || px > sel->ex)
329 return (0);
334 return (1);