Tweak previous - find end of style correctly.
[tmux-openbsd.git] / screen.c
blob87b3330a7b18eeb2804aecb642c17f3bb755c866
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 /* Selected area in screen. */
29 struct screen_sel {
30 int hidden;
31 int rectangle;
32 int modekeys;
34 u_int sx;
35 u_int sy;
37 u_int ex;
38 u_int ey;
40 struct grid_cell cell;
43 /* Entry on title stack. */
44 struct screen_title_entry {
45 char *text;
47 TAILQ_ENTRY(screen_title_entry) entry;
49 TAILQ_HEAD(screen_titles, screen_title_entry);
51 static void screen_resize_y(struct screen *, u_int, int, u_int *);
52 static void screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
54 /* Free titles stack. */
55 static void
56 screen_free_titles(struct screen *s)
58 struct screen_title_entry *title_entry;
60 if (s->titles == NULL)
61 return;
63 while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
64 TAILQ_REMOVE(s->titles, title_entry, entry);
65 free(title_entry->text);
66 free(title_entry);
69 free(s->titles);
70 s->titles = NULL;
73 /* Create a new screen. */
74 void
75 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
77 s->grid = grid_create(sx, sy, hlimit);
78 s->saved_grid = NULL;
80 s->title = xstrdup("");
81 s->titles = NULL;
82 s->path = NULL;
84 s->cstyle = SCREEN_CURSOR_DEFAULT;
85 s->default_cstyle = SCREEN_CURSOR_DEFAULT;
86 s->default_mode = 0;
87 s->ccolour = -1;
88 s->default_ccolour = -1;
89 s->tabs = NULL;
90 s->sel = NULL;
92 s->write_list = NULL;
94 screen_reinit(s);
97 /* Reinitialise screen. */
98 void
99 screen_reinit(struct screen *s)
101 s->cx = 0;
102 s->cy = 0;
104 s->rupper = 0;
105 s->rlower = screen_size_y(s) - 1;
107 s->mode = MODE_CURSOR|MODE_WRAP|(s->mode & MODE_CRLF);
108 if (options_get_number(global_options, "extended-keys") == 2)
109 s->mode |= MODE_KEXTENDED;
111 if (s->saved_grid != NULL)
112 screen_alternate_off(s, NULL, 0);
113 s->saved_cx = UINT_MAX;
114 s->saved_cy = UINT_MAX;
116 screen_reset_tabs(s);
118 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
120 screen_clear_selection(s);
121 screen_free_titles(s);
124 /* Destroy a screen. */
125 void
126 screen_free(struct screen *s)
128 free(s->sel);
129 free(s->tabs);
130 free(s->path);
131 free(s->title);
133 if (s->write_list != NULL)
134 screen_write_free_list(s);
136 if (s->saved_grid != NULL)
137 grid_destroy(s->saved_grid);
138 grid_destroy(s->grid);
140 screen_free_titles(s);
143 /* Reset tabs to default, eight spaces apart. */
144 void
145 screen_reset_tabs(struct screen *s)
147 u_int i;
149 free(s->tabs);
151 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
152 fatal("bit_alloc failed");
153 for (i = 8; i < screen_size_x(s); i += 8)
154 bit_set(s->tabs, i);
157 /* Set screen cursor style and mode. */
158 void
159 screen_set_cursor_style(u_int style, enum screen_cursor_style *cstyle,
160 int *mode)
162 switch (style) {
163 case 0:
164 *cstyle = SCREEN_CURSOR_DEFAULT;
165 break;
166 case 1:
167 *cstyle = SCREEN_CURSOR_BLOCK;
168 *mode |= MODE_CURSOR_BLINKING;
169 break;
170 case 2:
171 *cstyle = SCREEN_CURSOR_BLOCK;
172 *mode &= ~MODE_CURSOR_BLINKING;
173 break;
174 case 3:
175 *cstyle = SCREEN_CURSOR_UNDERLINE;
176 *mode |= MODE_CURSOR_BLINKING;
177 break;
178 case 4:
179 *cstyle = SCREEN_CURSOR_UNDERLINE;
180 *mode &= ~MODE_CURSOR_BLINKING;
181 break;
182 case 5:
183 *cstyle = SCREEN_CURSOR_BAR;
184 *mode |= MODE_CURSOR_BLINKING;
185 break;
186 case 6:
187 *cstyle = SCREEN_CURSOR_BAR;
188 *mode &= ~MODE_CURSOR_BLINKING;
189 break;
193 /* Set screen cursor colour. */
194 void
195 screen_set_cursor_colour(struct screen *s, int colour)
197 s->ccolour = colour;
200 /* Set screen title. */
202 screen_set_title(struct screen *s, const char *title)
204 if (!utf8_isvalid(title))
205 return (0);
206 free(s->title);
207 s->title = xstrdup(title);
208 return (1);
211 /* Set screen path. */
212 void
213 screen_set_path(struct screen *s, const char *path)
215 free(s->path);
216 utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
219 /* Push the current title onto the stack. */
220 void
221 screen_push_title(struct screen *s)
223 struct screen_title_entry *title_entry;
225 if (s->titles == NULL) {
226 s->titles = xmalloc(sizeof *s->titles);
227 TAILQ_INIT(s->titles);
229 title_entry = xmalloc(sizeof *title_entry);
230 title_entry->text = xstrdup(s->title);
231 TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
235 * Pop a title from the stack and set it as the screen title. If the stack is
236 * empty, do nothing.
238 void
239 screen_pop_title(struct screen *s)
241 struct screen_title_entry *title_entry;
243 if (s->titles == NULL)
244 return;
246 title_entry = TAILQ_FIRST(s->titles);
247 if (title_entry != NULL) {
248 screen_set_title(s, title_entry->text);
250 TAILQ_REMOVE(s->titles, title_entry, entry);
251 free(title_entry->text);
252 free(title_entry);
256 /* Resize screen with options. */
257 void
258 screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
259 int eat_empty, int cursor)
261 u_int cx = s->cx, cy = s->grid->hsize + s->cy;
263 if (s->write_list != NULL)
264 screen_write_free_list(s);
266 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
267 __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
268 cx, cy);
270 if (sx < 1)
271 sx = 1;
272 if (sy < 1)
273 sy = 1;
275 if (sx != screen_size_x(s)) {
276 s->grid->sx = sx;
277 screen_reset_tabs(s);
278 } else
279 reflow = 0;
281 if (sy != screen_size_y(s))
282 screen_resize_y(s, sy, eat_empty, &cy);
284 if (reflow)
285 screen_reflow(s, sx, &cx, &cy, cursor);
287 if (cy >= s->grid->hsize) {
288 s->cx = cx;
289 s->cy = cy - s->grid->hsize;
290 } else {
291 s->cx = 0;
292 s->cy = 0;
295 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
296 s->cy, cx, cy);
298 if (s->write_list != NULL)
299 screen_write_make_list(s);
302 /* Resize screen. */
303 void
304 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
306 screen_resize_cursor(s, sx, sy, reflow, 1, 1);
309 static void
310 screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
312 struct grid *gd = s->grid;
313 u_int needed, available, oldy, i;
315 if (sy == 0)
316 fatalx("zero size");
317 oldy = screen_size_y(s);
320 * When resizing:
322 * If the height is decreasing, delete lines from the bottom until
323 * hitting the cursor, then push lines from the top into the history.
325 * When increasing, pull as many lines as possible from scrolled
326 * history (not explicitly cleared from view) to the top, then fill the
327 * remaining with blanks at the bottom.
330 /* Size decreasing. */
331 if (sy < oldy) {
332 needed = oldy - sy;
334 /* Delete as many lines as possible from the bottom. */
335 if (eat_empty) {
336 available = oldy - 1 - s->cy;
337 if (available > 0) {
338 if (available > needed)
339 available = needed;
340 grid_view_delete_lines(gd, oldy - available,
341 available, 8);
343 needed -= available;
347 * Now just increase the history size, if possible, to take
348 * over the lines which are left. If history is off, delete
349 * lines from the top.
351 available = s->cy;
352 if (gd->flags & GRID_HISTORY) {
353 gd->hscrolled += needed;
354 gd->hsize += needed;
355 } else if (needed > 0 && available > 0) {
356 if (available > needed)
357 available = needed;
358 grid_view_delete_lines(gd, 0, available, 8);
359 (*cy) -= available;
363 /* Resize line array. */
364 grid_adjust_lines(gd, gd->hsize + sy);
366 /* Size increasing. */
367 if (sy > oldy) {
368 needed = sy - oldy;
371 * Try to pull as much as possible out of scrolled history, if
372 * is is enabled.
374 available = gd->hscrolled;
375 if (gd->flags & GRID_HISTORY && available > 0) {
376 if (available > needed)
377 available = needed;
378 gd->hscrolled -= available;
379 gd->hsize -= available;
380 } else
381 available = 0;
382 needed -= available;
384 /* Then fill the rest in with blanks. */
385 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
386 grid_empty_line(gd, i, 8);
389 /* Set the new size, and reset the scroll region. */
390 gd->sy = sy;
391 s->rupper = 0;
392 s->rlower = screen_size_y(s) - 1;
395 /* Set selection. */
396 void
397 screen_set_selection(struct screen *s, u_int sx, u_int sy,
398 u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
400 if (s->sel == NULL)
401 s->sel = xcalloc(1, sizeof *s->sel);
403 memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
404 s->sel->hidden = 0;
405 s->sel->rectangle = rectangle;
406 s->sel->modekeys = modekeys;
408 s->sel->sx = sx;
409 s->sel->sy = sy;
410 s->sel->ex = ex;
411 s->sel->ey = ey;
414 /* Clear selection. */
415 void
416 screen_clear_selection(struct screen *s)
418 free(s->sel);
419 s->sel = NULL;
422 /* Hide selection. */
423 void
424 screen_hide_selection(struct screen *s)
426 if (s->sel != NULL)
427 s->sel->hidden = 1;
430 /* Check if cell in selection. */
432 screen_check_selection(struct screen *s, u_int px, u_int py)
434 struct screen_sel *sel = s->sel;
435 u_int xx;
437 if (sel == NULL || sel->hidden)
438 return (0);
440 if (sel->rectangle) {
441 if (sel->sy < sel->ey) {
442 /* start line < end line -- downward selection. */
443 if (py < sel->sy || py > sel->ey)
444 return (0);
445 } else if (sel->sy > sel->ey) {
446 /* start line > end line -- upward selection. */
447 if (py > sel->sy || py < sel->ey)
448 return (0);
449 } else {
450 /* starting line == ending line. */
451 if (py != sel->sy)
452 return (0);
456 * Need to include the selection start row, but not the cursor
457 * row, which means the selection changes depending on which
458 * one is on the left.
460 if (sel->ex < sel->sx) {
461 /* Cursor (ex) is on the left. */
462 if (px < sel->ex)
463 return (0);
465 if (px > sel->sx)
466 return (0);
467 } else {
468 /* Selection start (sx) is on the left. */
469 if (px < sel->sx)
470 return (0);
472 if (px > sel->ex)
473 return (0);
475 } else {
477 * Like emacs, keep the top-left-most character, and drop the
478 * bottom-right-most, regardless of copy direction.
480 if (sel->sy < sel->ey) {
481 /* starting line < ending line -- downward selection. */
482 if (py < sel->sy || py > sel->ey)
483 return (0);
485 if (py == sel->sy && px < sel->sx)
486 return (0);
488 if (sel->modekeys == MODEKEY_EMACS)
489 xx = (sel->ex == 0 ? 0 : sel->ex - 1);
490 else
491 xx = sel->ex;
492 if (py == sel->ey && px > xx)
493 return (0);
494 } else if (sel->sy > sel->ey) {
495 /* starting line > ending line -- upward selection. */
496 if (py > sel->sy || py < sel->ey)
497 return (0);
499 if (py == sel->ey && px < sel->ex)
500 return (0);
502 if (sel->modekeys == MODEKEY_EMACS)
503 xx = sel->sx - 1;
504 else
505 xx = sel->sx;
506 if (py == sel->sy && (sel->sx == 0 || px > xx))
507 return (0);
508 } else {
509 /* starting line == ending line. */
510 if (py != sel->sy)
511 return (0);
513 if (sel->ex < sel->sx) {
514 /* cursor (ex) is on the left */
515 if (sel->modekeys == MODEKEY_EMACS)
516 xx = sel->sx - 1;
517 else
518 xx = sel->sx;
519 if (px > xx || px < sel->ex)
520 return (0);
521 } else {
522 /* selection start (sx) is on the left */
523 if (sel->modekeys == MODEKEY_EMACS)
524 xx = (sel->ex == 0 ? 0 : sel->ex - 1);
525 else
526 xx = sel->ex;
527 if (px < sel->sx || px > xx)
528 return (0);
533 return (1);
536 /* Get selected grid cell. */
537 void
538 screen_select_cell(struct screen *s, struct grid_cell *dst,
539 const struct grid_cell *src)
541 if (s->sel == NULL || s->sel->hidden)
542 return;
544 memcpy(dst, &s->sel->cell, sizeof *dst);
546 utf8_copy(&dst->data, &src->data);
547 dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
548 dst->attr |= src->attr & GRID_ATTR_CHARSET;
549 dst->flags = src->flags;
552 /* Reflow wrapped lines. */
553 static void
554 screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
556 u_int wx, wy;
558 if (cursor) {
559 grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
560 log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
561 wy);
564 grid_reflow(s->grid, new_x);
566 if (cursor) {
567 grid_unwrap_position(s->grid, cx, cy, wx, wy);
568 log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
570 else {
571 *cx = 0;
572 *cy = s->grid->hsize;
577 * Enter alternative screen mode. A copy of the visible screen is saved and the
578 * history is not updated.
580 void
581 screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
583 u_int sx, sy;
585 if (s->saved_grid != NULL)
586 return;
587 sx = screen_size_x(s);
588 sy = screen_size_y(s);
590 s->saved_grid = grid_create(sx, sy, 0);
591 grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
592 if (cursor) {
593 s->saved_cx = s->cx;
594 s->saved_cy = s->cy;
596 memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
598 grid_view_clear(s->grid, 0, 0, sx, sy, 8);
600 s->saved_flags = s->grid->flags;
601 s->grid->flags &= ~GRID_HISTORY;
604 /* Exit alternate screen mode and restore the copied grid. */
605 void
606 screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
608 u_int sx = screen_size_x(s), sy = screen_size_y(s);
611 * If the current size is different, temporarily resize to the old size
612 * before copying back.
614 if (s->saved_grid != NULL)
615 screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 1);
618 * Restore the cursor position and cell. This happens even if not
619 * currently in the alternate screen.
621 if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
622 s->cx = s->saved_cx;
623 s->cy = s->saved_cy;
624 if (gc != NULL)
625 memcpy(gc, &s->saved_cell, sizeof *gc);
628 /* If not in the alternate screen, do nothing more. */
629 if (s->saved_grid == NULL) {
630 if (s->cx > screen_size_x(s) - 1)
631 s->cx = screen_size_x(s) - 1;
632 if (s->cy > screen_size_y(s) - 1)
633 s->cy = screen_size_y(s) - 1;
634 return;
637 /* Restore the saved grid. */
638 grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
639 s->saved_grid->sy);
642 * Turn history back on (so resize can use it) and then resize back to
643 * the current size.
645 if (s->saved_flags & GRID_HISTORY)
646 s->grid->flags |= GRID_HISTORY;
647 screen_resize(s, sx, sy, 1);
649 grid_destroy(s->saved_grid);
650 s->saved_grid = NULL;
652 if (s->cx > screen_size_x(s) - 1)
653 s->cx = screen_size_x(s) - 1;
654 if (s->cy > screen_size_y(s) - 1)
655 s->cy = screen_size_y(s) - 1;
658 /* Get mode as a string. */
659 const char *
660 screen_mode_to_string(int mode)
662 static char tmp[1024];
664 if (mode == 0)
665 return ("NONE");
666 if (mode == ALL_MODES)
667 return ("ALL");
669 *tmp = '\0';
670 if (mode & MODE_CURSOR)
671 strlcat(tmp, "CURSOR,", sizeof tmp);
672 if (mode & MODE_INSERT)
673 strlcat(tmp, "INSERT,", sizeof tmp);
674 if (mode & MODE_KCURSOR)
675 strlcat(tmp, "KCURSOR,", sizeof tmp);
676 if (mode & MODE_KKEYPAD)
677 strlcat(tmp, "KKEYPAD,", sizeof tmp);
678 if (mode & MODE_WRAP)
679 strlcat(tmp, "WRAP,", sizeof tmp);
680 if (mode & MODE_MOUSE_STANDARD)
681 strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp);
682 if (mode & MODE_MOUSE_BUTTON)
683 strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp);
684 if (mode & MODE_CURSOR_BLINKING)
685 strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp);
686 if (mode & MODE_CURSOR_VERY_VISIBLE)
687 strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp);
688 if (mode & MODE_MOUSE_UTF8)
689 strlcat(tmp, "UTF8,", sizeof tmp);
690 if (mode & MODE_MOUSE_SGR)
691 strlcat(tmp, "SGR,", sizeof tmp);
692 if (mode & MODE_BRACKETPASTE)
693 strlcat(tmp, "BRACKETPASTE,", sizeof tmp);
694 if (mode & MODE_FOCUSON)
695 strlcat(tmp, "FOCUSON,", sizeof tmp);
696 if (mode & MODE_MOUSE_ALL)
697 strlcat(tmp, "MOUSE_ALL,", sizeof tmp);
698 if (mode & MODE_ORIGIN)
699 strlcat(tmp, "ORIGIN,", sizeof tmp);
700 if (mode & MODE_CRLF)
701 strlcat(tmp, "CRLF,", sizeof tmp);
702 if (mode & MODE_KEXTENDED)
703 strlcat(tmp, "KEXTENDED,", sizeof tmp);
704 tmp[strlen(tmp) - 1] = '\0';
705 return (tmp);