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>
28 /* Selected area in screen. */
40 struct grid_cell cell
;
43 /* Entry on title stack. */
44 struct screen_title_entry
{
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. */
56 screen_free_titles(struct screen
*s
)
58 struct screen_title_entry
*title_entry
;
60 if (s
->titles
== NULL
)
63 while ((title_entry
= TAILQ_FIRST(s
->titles
)) != NULL
) {
64 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
65 free(title_entry
->text
);
73 /* Create a new screen. */
75 screen_init(struct screen
*s
, u_int sx
, u_int sy
, u_int hlimit
)
77 s
->grid
= grid_create(sx
, sy
, hlimit
);
80 s
->title
= xstrdup("");
84 s
->cstyle
= SCREEN_CURSOR_DEFAULT
;
85 s
->default_cstyle
= SCREEN_CURSOR_DEFAULT
;
88 s
->default_ccolour
= -1;
98 /* Reinitialise screen. */
100 screen_reinit(struct screen
*s
)
106 s
->rlower
= screen_size_y(s
) - 1;
108 s
->mode
= MODE_CURSOR
|MODE_WRAP
|(s
->mode
& MODE_CRLF
);
109 if (options_get_number(global_options
, "extended-keys") == 2)
110 s
->mode
|= MODE_KEXTENDED
;
112 if (s
->saved_grid
!= NULL
)
113 screen_alternate_off(s
, NULL
, 0);
114 s
->saved_cx
= UINT_MAX
;
115 s
->saved_cy
= UINT_MAX
;
117 screen_reset_tabs(s
);
119 grid_clear_lines(s
->grid
, s
->grid
->hsize
, s
->grid
->sy
, 8);
121 screen_clear_selection(s
);
122 screen_free_titles(s
);
123 screen_reset_hyperlinks(s
);
126 /* Reset hyperlinks of a screen. */
128 screen_reset_hyperlinks(struct screen
*s
)
130 if (s
->hyperlinks
== NULL
)
131 s
->hyperlinks
= hyperlinks_init();
133 hyperlinks_reset(s
->hyperlinks
);
136 /* Destroy a screen. */
138 screen_free(struct screen
*s
)
145 if (s
->write_list
!= NULL
)
146 screen_write_free_list(s
);
148 if (s
->saved_grid
!= NULL
)
149 grid_destroy(s
->saved_grid
);
150 grid_destroy(s
->grid
);
152 if (s
->hyperlinks
!= NULL
)
153 hyperlinks_free(s
->hyperlinks
);
154 screen_free_titles(s
);
157 /* Reset tabs to default, eight spaces apart. */
159 screen_reset_tabs(struct screen
*s
)
165 if ((s
->tabs
= bit_alloc(screen_size_x(s
))) == NULL
)
166 fatal("bit_alloc failed");
167 for (i
= 8; i
< screen_size_x(s
); i
+= 8)
171 /* Set screen cursor style and mode. */
173 screen_set_cursor_style(u_int style
, enum screen_cursor_style
*cstyle
,
178 *cstyle
= SCREEN_CURSOR_DEFAULT
;
181 *cstyle
= SCREEN_CURSOR_BLOCK
;
182 *mode
|= MODE_CURSOR_BLINKING
;
185 *cstyle
= SCREEN_CURSOR_BLOCK
;
186 *mode
&= ~MODE_CURSOR_BLINKING
;
189 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
190 *mode
|= MODE_CURSOR_BLINKING
;
193 *cstyle
= SCREEN_CURSOR_UNDERLINE
;
194 *mode
&= ~MODE_CURSOR_BLINKING
;
197 *cstyle
= SCREEN_CURSOR_BAR
;
198 *mode
|= MODE_CURSOR_BLINKING
;
201 *cstyle
= SCREEN_CURSOR_BAR
;
202 *mode
&= ~MODE_CURSOR_BLINKING
;
207 /* Set screen cursor colour. */
209 screen_set_cursor_colour(struct screen
*s
, int colour
)
214 /* Set screen title. */
216 screen_set_title(struct screen
*s
, const char *title
)
218 if (!utf8_isvalid(title
))
221 s
->title
= xstrdup(title
);
225 /* Set screen path. */
227 screen_set_path(struct screen
*s
, const char *path
)
230 utf8_stravis(&s
->path
, path
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
233 /* Push the current title onto the stack. */
235 screen_push_title(struct screen
*s
)
237 struct screen_title_entry
*title_entry
;
239 if (s
->titles
== NULL
) {
240 s
->titles
= xmalloc(sizeof *s
->titles
);
241 TAILQ_INIT(s
->titles
);
243 title_entry
= xmalloc(sizeof *title_entry
);
244 title_entry
->text
= xstrdup(s
->title
);
245 TAILQ_INSERT_HEAD(s
->titles
, title_entry
, entry
);
249 * Pop a title from the stack and set it as the screen title. If the stack is
253 screen_pop_title(struct screen
*s
)
255 struct screen_title_entry
*title_entry
;
257 if (s
->titles
== NULL
)
260 title_entry
= TAILQ_FIRST(s
->titles
);
261 if (title_entry
!= NULL
) {
262 screen_set_title(s
, title_entry
->text
);
264 TAILQ_REMOVE(s
->titles
, title_entry
, entry
);
265 free(title_entry
->text
);
270 /* Resize screen with options. */
272 screen_resize_cursor(struct screen
*s
, u_int sx
, u_int sy
, int reflow
,
273 int eat_empty
, int cursor
)
275 u_int cx
= s
->cx
, cy
= s
->grid
->hsize
+ s
->cy
;
277 if (s
->write_list
!= NULL
)
278 screen_write_free_list(s
);
280 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
281 __func__
, sx
, sy
, screen_size_x(s
), screen_size_y(s
), s
->cx
, s
->cy
,
289 if (sx
!= screen_size_x(s
)) {
291 screen_reset_tabs(s
);
295 if (sy
!= screen_size_y(s
))
296 screen_resize_y(s
, sy
, eat_empty
, &cy
);
299 screen_reflow(s
, sx
, &cx
, &cy
, cursor
);
301 if (cy
>= s
->grid
->hsize
) {
303 s
->cy
= cy
- s
->grid
->hsize
;
309 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__
, s
->cx
,
312 if (s
->write_list
!= NULL
)
313 screen_write_make_list(s
);
318 screen_resize(struct screen
*s
, u_int sx
, u_int sy
, int reflow
)
320 screen_resize_cursor(s
, sx
, sy
, reflow
, 1, 1);
324 screen_resize_y(struct screen
*s
, u_int sy
, int eat_empty
, u_int
*cy
)
326 struct grid
*gd
= s
->grid
;
327 u_int needed
, available
, oldy
, i
;
331 oldy
= screen_size_y(s
);
336 * If the height is decreasing, delete lines from the bottom until
337 * hitting the cursor, then push lines from the top into the history.
339 * When increasing, pull as many lines as possible from scrolled
340 * history (not explicitly cleared from view) to the top, then fill the
341 * remaining with blanks at the bottom.
344 /* Size decreasing. */
348 /* Delete as many lines as possible from the bottom. */
350 available
= oldy
- 1 - s
->cy
;
352 if (available
> needed
)
354 grid_view_delete_lines(gd
, oldy
- available
,
361 * Now just increase the history size, if possible, to take
362 * over the lines which are left. If history is off, delete
363 * lines from the top.
366 if (gd
->flags
& GRID_HISTORY
) {
367 gd
->hscrolled
+= needed
;
369 } else if (needed
> 0 && available
> 0) {
370 if (available
> needed
)
372 grid_view_delete_lines(gd
, 0, available
, 8);
377 /* Resize line array. */
378 grid_adjust_lines(gd
, gd
->hsize
+ sy
);
380 /* Size increasing. */
385 * Try to pull as much as possible out of scrolled history, if
388 available
= gd
->hscrolled
;
389 if (gd
->flags
& GRID_HISTORY
&& available
> 0) {
390 if (available
> needed
)
392 gd
->hscrolled
-= available
;
393 gd
->hsize
-= available
;
398 /* Then fill the rest in with blanks. */
399 for (i
= gd
->hsize
+ sy
- needed
; i
< gd
->hsize
+ sy
; i
++)
400 grid_empty_line(gd
, i
, 8);
403 /* Set the new size, and reset the scroll region. */
406 s
->rlower
= screen_size_y(s
) - 1;
411 screen_set_selection(struct screen
*s
, u_int sx
, u_int sy
,
412 u_int ex
, u_int ey
, u_int rectangle
, int modekeys
, struct grid_cell
*gc
)
415 s
->sel
= xcalloc(1, sizeof *s
->sel
);
417 memcpy(&s
->sel
->cell
, gc
, sizeof s
->sel
->cell
);
419 s
->sel
->rectangle
= rectangle
;
420 s
->sel
->modekeys
= modekeys
;
428 /* Clear selection. */
430 screen_clear_selection(struct screen
*s
)
436 /* Hide selection. */
438 screen_hide_selection(struct screen
*s
)
444 /* Check if cell in selection. */
446 screen_check_selection(struct screen
*s
, u_int px
, u_int py
)
448 struct screen_sel
*sel
= s
->sel
;
451 if (sel
== NULL
|| sel
->hidden
)
454 if (sel
->rectangle
) {
455 if (sel
->sy
< sel
->ey
) {
456 /* start line < end line -- downward selection. */
457 if (py
< sel
->sy
|| py
> sel
->ey
)
459 } else if (sel
->sy
> sel
->ey
) {
460 /* start line > end line -- upward selection. */
461 if (py
> sel
->sy
|| py
< sel
->ey
)
464 /* starting line == ending line. */
470 * Need to include the selection start row, but not the cursor
471 * row, which means the selection changes depending on which
472 * one is on the left.
474 if (sel
->ex
< sel
->sx
) {
475 /* Cursor (ex) is on the left. */
482 /* Selection start (sx) is on the left. */
491 * Like emacs, keep the top-left-most character, and drop the
492 * bottom-right-most, regardless of copy direction.
494 if (sel
->sy
< sel
->ey
) {
495 /* starting line < ending line -- downward selection. */
496 if (py
< sel
->sy
|| py
> sel
->ey
)
499 if (py
== sel
->sy
&& px
< sel
->sx
)
502 if (sel
->modekeys
== MODEKEY_EMACS
)
503 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
506 if (py
== sel
->ey
&& px
> xx
)
508 } else if (sel
->sy
> sel
->ey
) {
509 /* starting line > ending line -- upward selection. */
510 if (py
> sel
->sy
|| py
< sel
->ey
)
513 if (py
== sel
->ey
&& px
< sel
->ex
)
516 if (sel
->modekeys
== MODEKEY_EMACS
)
520 if (py
== sel
->sy
&& (sel
->sx
== 0 || px
> xx
))
523 /* starting line == ending line. */
527 if (sel
->ex
< sel
->sx
) {
528 /* cursor (ex) is on the left */
529 if (sel
->modekeys
== MODEKEY_EMACS
)
533 if (px
> xx
|| px
< sel
->ex
)
536 /* selection start (sx) is on the left */
537 if (sel
->modekeys
== MODEKEY_EMACS
)
538 xx
= (sel
->ex
== 0 ? 0 : sel
->ex
- 1);
541 if (px
< sel
->sx
|| px
> xx
)
550 /* Get selected grid cell. */
552 screen_select_cell(struct screen
*s
, struct grid_cell
*dst
,
553 const struct grid_cell
*src
)
555 if (s
->sel
== NULL
|| s
->sel
->hidden
)
558 memcpy(dst
, &s
->sel
->cell
, sizeof *dst
);
560 utf8_copy(&dst
->data
, &src
->data
);
561 dst
->attr
= dst
->attr
& ~GRID_ATTR_CHARSET
;
562 dst
->attr
|= src
->attr
& GRID_ATTR_CHARSET
;
563 dst
->flags
= src
->flags
;
566 /* Reflow wrapped lines. */
568 screen_reflow(struct screen
*s
, u_int new_x
, u_int
*cx
, u_int
*cy
, int cursor
)
573 grid_wrap_position(s
->grid
, *cx
, *cy
, &wx
, &wy
);
574 log_debug("%s: cursor %u,%u is %u,%u", __func__
, *cx
, *cy
, wx
,
578 grid_reflow(s
->grid
, new_x
);
581 grid_unwrap_position(s
->grid
, cx
, cy
, wx
, wy
);
582 log_debug("%s: new cursor is %u,%u", __func__
, *cx
, *cy
);
586 *cy
= s
->grid
->hsize
;
591 * Enter alternative screen mode. A copy of the visible screen is saved and the
592 * history is not updated.
595 screen_alternate_on(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
599 if (s
->saved_grid
!= NULL
)
601 sx
= screen_size_x(s
);
602 sy
= screen_size_y(s
);
604 s
->saved_grid
= grid_create(sx
, sy
, 0);
605 grid_duplicate_lines(s
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
610 memcpy(&s
->saved_cell
, gc
, sizeof s
->saved_cell
);
612 grid_view_clear(s
->grid
, 0, 0, sx
, sy
, 8);
614 s
->saved_flags
= s
->grid
->flags
;
615 s
->grid
->flags
&= ~GRID_HISTORY
;
618 /* Exit alternate screen mode and restore the copied grid. */
620 screen_alternate_off(struct screen
*s
, struct grid_cell
*gc
, int cursor
)
622 u_int sx
= screen_size_x(s
), sy
= screen_size_y(s
);
625 * If the current size is different, temporarily resize to the old size
626 * before copying back.
628 if (s
->saved_grid
!= NULL
)
629 screen_resize(s
, s
->saved_grid
->sx
, s
->saved_grid
->sy
, 0);
632 * Restore the cursor position and cell. This happens even if not
633 * currently in the alternate screen.
635 if (cursor
&& s
->saved_cx
!= UINT_MAX
&& s
->saved_cy
!= UINT_MAX
) {
639 memcpy(gc
, &s
->saved_cell
, sizeof *gc
);
642 /* If not in the alternate screen, do nothing more. */
643 if (s
->saved_grid
== NULL
) {
644 if (s
->cx
> screen_size_x(s
) - 1)
645 s
->cx
= screen_size_x(s
) - 1;
646 if (s
->cy
> screen_size_y(s
) - 1)
647 s
->cy
= screen_size_y(s
) - 1;
651 /* Restore the saved grid. */
652 grid_duplicate_lines(s
->grid
, screen_hsize(s
), s
->saved_grid
, 0,
656 * Turn history back on (so resize can use it) and then resize back to
659 if (s
->saved_flags
& GRID_HISTORY
)
660 s
->grid
->flags
|= GRID_HISTORY
;
661 screen_resize(s
, sx
, sy
, 1);
663 grid_destroy(s
->saved_grid
);
664 s
->saved_grid
= NULL
;
666 if (s
->cx
> screen_size_x(s
) - 1)
667 s
->cx
= screen_size_x(s
) - 1;
668 if (s
->cy
> screen_size_y(s
) - 1)
669 s
->cy
= screen_size_y(s
) - 1;
672 /* Get mode as a string. */
674 screen_mode_to_string(int mode
)
676 static char tmp
[1024];
680 if (mode
== ALL_MODES
)
684 if (mode
& MODE_CURSOR
)
685 strlcat(tmp
, "CURSOR,", sizeof tmp
);
686 if (mode
& MODE_INSERT
)
687 strlcat(tmp
, "INSERT,", sizeof tmp
);
688 if (mode
& MODE_KCURSOR
)
689 strlcat(tmp
, "KCURSOR,", sizeof tmp
);
690 if (mode
& MODE_KKEYPAD
)
691 strlcat(tmp
, "KKEYPAD,", sizeof tmp
);
692 if (mode
& MODE_WRAP
)
693 strlcat(tmp
, "WRAP,", sizeof tmp
);
694 if (mode
& MODE_MOUSE_STANDARD
)
695 strlcat(tmp
, "MOUSE_STANDARD,", sizeof tmp
);
696 if (mode
& MODE_MOUSE_BUTTON
)
697 strlcat(tmp
, "MOUSE_BUTTON,", sizeof tmp
);
698 if (mode
& MODE_CURSOR_BLINKING
)
699 strlcat(tmp
, "CURSOR_BLINKING,", sizeof tmp
);
700 if (mode
& MODE_CURSOR_VERY_VISIBLE
)
701 strlcat(tmp
, "CURSOR_VERY_VISIBLE,", sizeof tmp
);
702 if (mode
& MODE_MOUSE_UTF8
)
703 strlcat(tmp
, "UTF8,", sizeof tmp
);
704 if (mode
& MODE_MOUSE_SGR
)
705 strlcat(tmp
, "SGR,", sizeof tmp
);
706 if (mode
& MODE_BRACKETPASTE
)
707 strlcat(tmp
, "BRACKETPASTE,", sizeof tmp
);
708 if (mode
& MODE_FOCUSON
)
709 strlcat(tmp
, "FOCUSON,", sizeof tmp
);
710 if (mode
& MODE_MOUSE_ALL
)
711 strlcat(tmp
, "MOUSE_ALL,", sizeof tmp
);
712 if (mode
& MODE_ORIGIN
)
713 strlcat(tmp
, "ORIGIN,", sizeof tmp
);
714 if (mode
& MODE_CRLF
)
715 strlcat(tmp
, "CRLF,", sizeof tmp
);
716 if (mode
& MODE_KEXTENDED
)
717 strlcat(tmp
, "KEXTENDED,", sizeof tmp
);
718 tmp
[strlen(tmp
) - 1] = '\0';