screen.c revision 1.15 1 1.5 christos /* $OpenBSD$ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.6 christos * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.1 jmmv #include <stdlib.h>
22 1.1 jmmv #include <string.h>
23 1.1 jmmv #include <unistd.h>
24 1.1 jmmv
25 1.1 jmmv #include "tmux.h"
26 1.1 jmmv
27 1.9 christos /* Selected area in screen. */
28 1.9 christos struct screen_sel {
29 1.9 christos int hidden;
30 1.9 christos int rectangle;
31 1.9 christos int modekeys;
32 1.9 christos
33 1.9 christos u_int sx;
34 1.9 christos u_int sy;
35 1.9 christos
36 1.9 christos u_int ex;
37 1.9 christos u_int ey;
38 1.9 christos
39 1.9 christos struct grid_cell cell;
40 1.9 christos };
41 1.9 christos
42 1.9 christos /* Entry on title stack. */
43 1.9 christos struct screen_title_entry {
44 1.9 christos char *text;
45 1.9 christos
46 1.9 christos TAILQ_ENTRY(screen_title_entry) entry;
47 1.9 christos };
48 1.9 christos TAILQ_HEAD(screen_titles, screen_title_entry);
49 1.9 christos
50 1.12 christos static void screen_resize_y(struct screen *, u_int, int, u_int *);
51 1.12 christos static void screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
52 1.1 jmmv
53 1.9 christos /* Free titles stack. */
54 1.9 christos static void
55 1.9 christos screen_free_titles(struct screen *s)
56 1.9 christos {
57 1.9 christos struct screen_title_entry *title_entry;
58 1.9 christos
59 1.9 christos if (s->titles == NULL)
60 1.9 christos return;
61 1.9 christos
62 1.9 christos while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
63 1.9 christos TAILQ_REMOVE(s->titles, title_entry, entry);
64 1.9 christos free(title_entry->text);
65 1.9 christos free(title_entry);
66 1.9 christos }
67 1.9 christos
68 1.9 christos free(s->titles);
69 1.9 christos s->titles = NULL;
70 1.9 christos }
71 1.9 christos
72 1.1 jmmv /* Create a new screen. */
73 1.1 jmmv void
74 1.1 jmmv screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
75 1.1 jmmv {
76 1.1 jmmv s->grid = grid_create(sx, sy, hlimit);
77 1.12 christos s->saved_grid = NULL;
78 1.12 christos
79 1.5 christos s->title = xstrdup("");
80 1.9 christos s->titles = NULL;
81 1.12 christos s->path = NULL;
82 1.1 jmmv
83 1.14 wiz s->cstyle = SCREEN_CURSOR_DEFAULT;
84 1.14 wiz s->default_cstyle = SCREEN_CURSOR_DEFAULT;
85 1.15 wiz s->mode = MODE_CURSOR;
86 1.14 wiz s->default_mode = 0;
87 1.14 wiz s->ccolour = -1;
88 1.14 wiz s->default_ccolour = -1;
89 1.1 jmmv s->tabs = NULL;
90 1.9 christos s->sel = NULL;
91 1.1 jmmv
92 1.15 wiz #ifdef ENABLE_SIXEL
93 1.15 wiz TAILQ_INIT(&s->images);
94 1.15 wiz #endif
95 1.15 wiz
96 1.12 christos s->write_list = NULL;
97 1.15 wiz s->hyperlinks = NULL;
98 1.12 christos
99 1.1 jmmv screen_reinit(s);
100 1.1 jmmv }
101 1.1 jmmv
102 1.1 jmmv /* Reinitialise screen. */
103 1.1 jmmv void
104 1.1 jmmv screen_reinit(struct screen *s)
105 1.1 jmmv {
106 1.1 jmmv s->cx = 0;
107 1.1 jmmv s->cy = 0;
108 1.1 jmmv
109 1.1 jmmv s->rupper = 0;
110 1.1 jmmv s->rlower = screen_size_y(s) - 1;
111 1.1 jmmv
112 1.14 wiz s->mode = MODE_CURSOR|MODE_WRAP|(s->mode & MODE_CRLF);
113 1.13 christos if (options_get_number(global_options, "extended-keys") == 2)
114 1.13 christos s->mode |= MODE_KEXTENDED;
115 1.1 jmmv
116 1.12 christos if (s->saved_grid != NULL)
117 1.12 christos screen_alternate_off(s, NULL, 0);
118 1.12 christos s->saved_cx = UINT_MAX;
119 1.12 christos s->saved_cy = UINT_MAX;
120 1.12 christos
121 1.1 jmmv screen_reset_tabs(s);
122 1.1 jmmv
123 1.7 christos grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
124 1.1 jmmv
125 1.1 jmmv screen_clear_selection(s);
126 1.9 christos screen_free_titles(s);
127 1.15 wiz
128 1.15 wiz #ifdef ENABLE_SIXEL
129 1.15 wiz image_free_all(s);
130 1.15 wiz #endif
131 1.15 wiz
132 1.15 wiz screen_reset_hyperlinks(s);
133 1.15 wiz }
134 1.15 wiz
135 1.15 wiz /* Reset hyperlinks of a screen. */
136 1.15 wiz void
137 1.15 wiz screen_reset_hyperlinks(struct screen *s)
138 1.15 wiz {
139 1.15 wiz if (s->hyperlinks == NULL)
140 1.15 wiz s->hyperlinks = hyperlinks_init();
141 1.15 wiz else
142 1.15 wiz hyperlinks_reset(s->hyperlinks);
143 1.1 jmmv }
144 1.1 jmmv
145 1.1 jmmv /* Destroy a screen. */
146 1.1 jmmv void
147 1.1 jmmv screen_free(struct screen *s)
148 1.1 jmmv {
149 1.9 christos free(s->sel);
150 1.4 christos free(s->tabs);
151 1.12 christos free(s->path);
152 1.4 christos free(s->title);
153 1.9 christos
154 1.12 christos if (s->write_list != NULL)
155 1.12 christos screen_write_free_list(s);
156 1.12 christos
157 1.12 christos if (s->saved_grid != NULL)
158 1.12 christos grid_destroy(s->saved_grid);
159 1.1 jmmv grid_destroy(s->grid);
160 1.9 christos
161 1.15 wiz if (s->hyperlinks != NULL)
162 1.15 wiz hyperlinks_free(s->hyperlinks);
163 1.9 christos screen_free_titles(s);
164 1.15 wiz
165 1.15 wiz #ifdef ENABLE_SIXEL
166 1.15 wiz image_free_all(s);
167 1.15 wiz #endif
168 1.1 jmmv }
169 1.1 jmmv
170 1.1 jmmv /* Reset tabs to default, eight spaces apart. */
171 1.1 jmmv void
172 1.1 jmmv screen_reset_tabs(struct screen *s)
173 1.1 jmmv {
174 1.1 jmmv u_int i;
175 1.1 jmmv
176 1.4 christos free(s->tabs);
177 1.1 jmmv
178 1.1 jmmv if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
179 1.1 jmmv fatal("bit_alloc failed");
180 1.1 jmmv for (i = 8; i < screen_size_x(s); i += 8)
181 1.1 jmmv bit_set(s->tabs, i);
182 1.1 jmmv }
183 1.1 jmmv
184 1.14 wiz /* Set screen cursor style and mode. */
185 1.3 jmmv void
186 1.14 wiz screen_set_cursor_style(u_int style, enum screen_cursor_style *cstyle,
187 1.14 wiz int *mode)
188 1.3 jmmv {
189 1.14 wiz switch (style) {
190 1.14 wiz case 0:
191 1.14 wiz *cstyle = SCREEN_CURSOR_DEFAULT;
192 1.14 wiz break;
193 1.14 wiz case 1:
194 1.14 wiz *cstyle = SCREEN_CURSOR_BLOCK;
195 1.14 wiz *mode |= MODE_CURSOR_BLINKING;
196 1.14 wiz break;
197 1.14 wiz case 2:
198 1.14 wiz *cstyle = SCREEN_CURSOR_BLOCK;
199 1.14 wiz *mode &= ~MODE_CURSOR_BLINKING;
200 1.14 wiz break;
201 1.14 wiz case 3:
202 1.14 wiz *cstyle = SCREEN_CURSOR_UNDERLINE;
203 1.14 wiz *mode |= MODE_CURSOR_BLINKING;
204 1.14 wiz break;
205 1.14 wiz case 4:
206 1.14 wiz *cstyle = SCREEN_CURSOR_UNDERLINE;
207 1.14 wiz *mode &= ~MODE_CURSOR_BLINKING;
208 1.14 wiz break;
209 1.14 wiz case 5:
210 1.14 wiz *cstyle = SCREEN_CURSOR_BAR;
211 1.14 wiz *mode |= MODE_CURSOR_BLINKING;
212 1.14 wiz break;
213 1.14 wiz case 6:
214 1.14 wiz *cstyle = SCREEN_CURSOR_BAR;
215 1.14 wiz *mode &= ~MODE_CURSOR_BLINKING;
216 1.14 wiz break;
217 1.12 christos }
218 1.3 jmmv }
219 1.3 jmmv
220 1.3 jmmv /* Set screen cursor colour. */
221 1.3 jmmv void
222 1.14 wiz screen_set_cursor_colour(struct screen *s, int colour)
223 1.3 jmmv {
224 1.14 wiz s->ccolour = colour;
225 1.3 jmmv }
226 1.3 jmmv
227 1.1 jmmv /* Set screen title. */
228 1.11 christos int
229 1.1 jmmv screen_set_title(struct screen *s, const char *title)
230 1.1 jmmv {
231 1.11 christos if (!utf8_isvalid(title))
232 1.11 christos return (0);
233 1.4 christos free(s->title);
234 1.11 christos s->title = xstrdup(title);
235 1.11 christos return (1);
236 1.11 christos }
237 1.11 christos
238 1.11 christos /* Set screen path. */
239 1.11 christos void
240 1.11 christos screen_set_path(struct screen *s, const char *path)
241 1.11 christos {
242 1.11 christos free(s->path);
243 1.11 christos utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
244 1.1 jmmv }
245 1.1 jmmv
246 1.9 christos /* Push the current title onto the stack. */
247 1.9 christos void
248 1.9 christos screen_push_title(struct screen *s)
249 1.9 christos {
250 1.9 christos struct screen_title_entry *title_entry;
251 1.9 christos
252 1.9 christos if (s->titles == NULL) {
253 1.9 christos s->titles = xmalloc(sizeof *s->titles);
254 1.9 christos TAILQ_INIT(s->titles);
255 1.9 christos }
256 1.9 christos title_entry = xmalloc(sizeof *title_entry);
257 1.9 christos title_entry->text = xstrdup(s->title);
258 1.9 christos TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
259 1.9 christos }
260 1.9 christos
261 1.9 christos /*
262 1.9 christos * Pop a title from the stack and set it as the screen title. If the stack is
263 1.9 christos * empty, do nothing.
264 1.9 christos */
265 1.9 christos void
266 1.9 christos screen_pop_title(struct screen *s)
267 1.9 christos {
268 1.9 christos struct screen_title_entry *title_entry;
269 1.9 christos
270 1.9 christos if (s->titles == NULL)
271 1.9 christos return;
272 1.9 christos
273 1.9 christos title_entry = TAILQ_FIRST(s->titles);
274 1.9 christos if (title_entry != NULL) {
275 1.9 christos screen_set_title(s, title_entry->text);
276 1.9 christos
277 1.9 christos TAILQ_REMOVE(s->titles, title_entry, entry);
278 1.9 christos free(title_entry->text);
279 1.9 christos free(title_entry);
280 1.9 christos }
281 1.9 christos }
282 1.9 christos
283 1.12 christos /* Resize screen with options. */
284 1.1 jmmv void
285 1.12 christos screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
286 1.12 christos int eat_empty, int cursor)
287 1.1 jmmv {
288 1.12 christos u_int cx = s->cx, cy = s->grid->hsize + s->cy;
289 1.12 christos
290 1.12 christos if (s->write_list != NULL)
291 1.12 christos screen_write_free_list(s);
292 1.12 christos
293 1.12 christos log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
294 1.12 christos __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
295 1.12 christos cx, cy);
296 1.12 christos
297 1.1 jmmv if (sx < 1)
298 1.1 jmmv sx = 1;
299 1.1 jmmv if (sy < 1)
300 1.1 jmmv sy = 1;
301 1.1 jmmv
302 1.1 jmmv if (sx != screen_size_x(s)) {
303 1.10 christos s->grid->sx = sx;
304 1.1 jmmv screen_reset_tabs(s);
305 1.9 christos } else
306 1.9 christos reflow = 0;
307 1.1 jmmv
308 1.1 jmmv if (sy != screen_size_y(s))
309 1.12 christos screen_resize_y(s, sy, eat_empty, &cy);
310 1.4 christos
311 1.15 wiz if (reflow) {
312 1.15 wiz #ifdef ENABLE_SIXEL
313 1.15 wiz image_free_all(s);
314 1.15 wiz #endif
315 1.12 christos screen_reflow(s, sx, &cx, &cy, cursor);
316 1.15 wiz }
317 1.12 christos
318 1.12 christos if (cy >= s->grid->hsize) {
319 1.12 christos s->cx = cx;
320 1.12 christos s->cy = cy - s->grid->hsize;
321 1.12 christos } else {
322 1.12 christos s->cx = 0;
323 1.12 christos s->cy = 0;
324 1.12 christos }
325 1.12 christos
326 1.12 christos log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
327 1.12 christos s->cy, cx, cy);
328 1.12 christos
329 1.12 christos if (s->write_list != NULL)
330 1.12 christos screen_write_make_list(s);
331 1.12 christos }
332 1.12 christos
333 1.12 christos /* Resize screen. */
334 1.12 christos void
335 1.12 christos screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
336 1.12 christos {
337 1.12 christos screen_resize_cursor(s, sx, sy, reflow, 1, 1);
338 1.1 jmmv }
339 1.1 jmmv
340 1.7 christos static void
341 1.12 christos screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
342 1.1 jmmv {
343 1.1 jmmv struct grid *gd = s->grid;
344 1.1 jmmv u_int needed, available, oldy, i;
345 1.1 jmmv
346 1.1 jmmv if (sy == 0)
347 1.1 jmmv fatalx("zero size");
348 1.1 jmmv oldy = screen_size_y(s);
349 1.1 jmmv
350 1.1 jmmv /*
351 1.1 jmmv * When resizing:
352 1.1 jmmv *
353 1.1 jmmv * If the height is decreasing, delete lines from the bottom until
354 1.1 jmmv * hitting the cursor, then push lines from the top into the history.
355 1.1 jmmv *
356 1.7 christos * When increasing, pull as many lines as possible from scrolled
357 1.7 christos * history (not explicitly cleared from view) to the top, then fill the
358 1.7 christos * remaining with blanks at the bottom.
359 1.1 jmmv */
360 1.1 jmmv
361 1.1 jmmv /* Size decreasing. */
362 1.1 jmmv if (sy < oldy) {
363 1.1 jmmv needed = oldy - sy;
364 1.1 jmmv
365 1.1 jmmv /* Delete as many lines as possible from the bottom. */
366 1.12 christos if (eat_empty) {
367 1.12 christos available = oldy - 1 - s->cy;
368 1.12 christos if (available > 0) {
369 1.12 christos if (available > needed)
370 1.12 christos available = needed;
371 1.12 christos grid_view_delete_lines(gd, oldy - available,
372 1.12 christos available, 8);
373 1.12 christos }
374 1.12 christos needed -= available;
375 1.1 jmmv }
376 1.1 jmmv
377 1.1 jmmv /*
378 1.1 jmmv * Now just increase the history size, if possible, to take
379 1.1 jmmv * over the lines which are left. If history is off, delete
380 1.1 jmmv * lines from the top.
381 1.1 jmmv */
382 1.1 jmmv available = s->cy;
383 1.7 christos if (gd->flags & GRID_HISTORY) {
384 1.7 christos gd->hscrolled += needed;
385 1.1 jmmv gd->hsize += needed;
386 1.7 christos } else if (needed > 0 && available > 0) {
387 1.1 jmmv if (available > needed)
388 1.1 jmmv available = needed;
389 1.7 christos grid_view_delete_lines(gd, 0, available, 8);
390 1.12 christos (*cy) -= available;
391 1.1 jmmv }
392 1.1 jmmv }
393 1.1 jmmv
394 1.9 christos /* Resize line array. */
395 1.9 christos grid_adjust_lines(gd, gd->hsize + sy);
396 1.1 jmmv
397 1.1 jmmv /* Size increasing. */
398 1.1 jmmv if (sy > oldy) {
399 1.1 jmmv needed = sy - oldy;
400 1.1 jmmv
401 1.1 jmmv /*
402 1.7 christos * Try to pull as much as possible out of scrolled history, if
403 1.7 christos * is is enabled.
404 1.1 jmmv */
405 1.7 christos available = gd->hscrolled;
406 1.1 jmmv if (gd->flags & GRID_HISTORY && available > 0) {
407 1.1 jmmv if (available > needed)
408 1.1 jmmv available = needed;
409 1.7 christos gd->hscrolled -= available;
410 1.1 jmmv gd->hsize -= available;
411 1.1 jmmv } else
412 1.1 jmmv available = 0;
413 1.1 jmmv needed -= available;
414 1.1 jmmv
415 1.1 jmmv /* Then fill the rest in with blanks. */
416 1.1 jmmv for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
417 1.12 christos grid_empty_line(gd, i, 8);
418 1.1 jmmv }
419 1.1 jmmv
420 1.1 jmmv /* Set the new size, and reset the scroll region. */
421 1.1 jmmv gd->sy = sy;
422 1.1 jmmv s->rupper = 0;
423 1.1 jmmv s->rlower = screen_size_y(s) - 1;
424 1.1 jmmv }
425 1.1 jmmv
426 1.1 jmmv /* Set selection. */
427 1.1 jmmv void
428 1.1 jmmv screen_set_selection(struct screen *s, u_int sx, u_int sy,
429 1.9 christos u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
430 1.1 jmmv {
431 1.9 christos if (s->sel == NULL)
432 1.9 christos s->sel = xcalloc(1, sizeof *s->sel);
433 1.1 jmmv
434 1.9 christos memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
435 1.9 christos s->sel->hidden = 0;
436 1.9 christos s->sel->rectangle = rectangle;
437 1.9 christos s->sel->modekeys = modekeys;
438 1.9 christos
439 1.9 christos s->sel->sx = sx;
440 1.9 christos s->sel->sy = sy;
441 1.9 christos s->sel->ex = ex;
442 1.9 christos s->sel->ey = ey;
443 1.1 jmmv }
444 1.1 jmmv
445 1.1 jmmv /* Clear selection. */
446 1.1 jmmv void
447 1.1 jmmv screen_clear_selection(struct screen *s)
448 1.1 jmmv {
449 1.9 christos free(s->sel);
450 1.9 christos s->sel = NULL;
451 1.1 jmmv }
452 1.1 jmmv
453 1.7 christos /* Hide selection. */
454 1.7 christos void
455 1.7 christos screen_hide_selection(struct screen *s)
456 1.7 christos {
457 1.9 christos if (s->sel != NULL)
458 1.9 christos s->sel->hidden = 1;
459 1.7 christos }
460 1.7 christos
461 1.1 jmmv /* Check if cell in selection. */
462 1.1 jmmv int
463 1.1 jmmv screen_check_selection(struct screen *s, u_int px, u_int py)
464 1.1 jmmv {
465 1.9 christos struct screen_sel *sel = s->sel;
466 1.5 christos u_int xx;
467 1.1 jmmv
468 1.9 christos if (sel == NULL || sel->hidden)
469 1.1 jmmv return (0);
470 1.1 jmmv
471 1.9 christos if (sel->rectangle) {
472 1.1 jmmv if (sel->sy < sel->ey) {
473 1.1 jmmv /* start line < end line -- downward selection. */
474 1.1 jmmv if (py < sel->sy || py > sel->ey)
475 1.1 jmmv return (0);
476 1.1 jmmv } else if (sel->sy > sel->ey) {
477 1.1 jmmv /* start line > end line -- upward selection. */
478 1.1 jmmv if (py > sel->sy || py < sel->ey)
479 1.1 jmmv return (0);
480 1.1 jmmv } else {
481 1.1 jmmv /* starting line == ending line. */
482 1.1 jmmv if (py != sel->sy)
483 1.1 jmmv return (0);
484 1.1 jmmv }
485 1.1 jmmv
486 1.1 jmmv /*
487 1.1 jmmv * Need to include the selection start row, but not the cursor
488 1.1 jmmv * row, which means the selection changes depending on which
489 1.1 jmmv * one is on the left.
490 1.1 jmmv */
491 1.1 jmmv if (sel->ex < sel->sx) {
492 1.1 jmmv /* Cursor (ex) is on the left. */
493 1.1 jmmv if (px < sel->ex)
494 1.1 jmmv return (0);
495 1.1 jmmv
496 1.1 jmmv if (px > sel->sx)
497 1.1 jmmv return (0);
498 1.1 jmmv } else {
499 1.1 jmmv /* Selection start (sx) is on the left. */
500 1.1 jmmv if (px < sel->sx)
501 1.1 jmmv return (0);
502 1.1 jmmv
503 1.1 jmmv if (px > sel->ex)
504 1.1 jmmv return (0);
505 1.1 jmmv }
506 1.1 jmmv } else {
507 1.1 jmmv /*
508 1.1 jmmv * Like emacs, keep the top-left-most character, and drop the
509 1.1 jmmv * bottom-right-most, regardless of copy direction.
510 1.1 jmmv */
511 1.1 jmmv if (sel->sy < sel->ey) {
512 1.1 jmmv /* starting line < ending line -- downward selection. */
513 1.1 jmmv if (py < sel->sy || py > sel->ey)
514 1.1 jmmv return (0);
515 1.1 jmmv
516 1.5 christos if (py == sel->sy && px < sel->sx)
517 1.5 christos return (0);
518 1.5 christos
519 1.10 christos if (sel->modekeys == MODEKEY_EMACS)
520 1.10 christos xx = (sel->ex == 0 ? 0 : sel->ex - 1);
521 1.10 christos else
522 1.10 christos xx = sel->ex;
523 1.10 christos if (py == sel->ey && px > xx)
524 1.1 jmmv return (0);
525 1.1 jmmv } else if (sel->sy > sel->ey) {
526 1.1 jmmv /* starting line > ending line -- upward selection. */
527 1.1 jmmv if (py > sel->sy || py < sel->ey)
528 1.1 jmmv return (0);
529 1.1 jmmv
530 1.5 christos if (py == sel->ey && px < sel->ex)
531 1.5 christos return (0);
532 1.5 christos
533 1.5 christos if (sel->modekeys == MODEKEY_EMACS)
534 1.5 christos xx = sel->sx - 1;
535 1.5 christos else
536 1.5 christos xx = sel->sx;
537 1.7 christos if (py == sel->sy && (sel->sx == 0 || px > xx))
538 1.1 jmmv return (0);
539 1.1 jmmv } else {
540 1.1 jmmv /* starting line == ending line. */
541 1.1 jmmv if (py != sel->sy)
542 1.1 jmmv return (0);
543 1.1 jmmv
544 1.1 jmmv if (sel->ex < sel->sx) {
545 1.1 jmmv /* cursor (ex) is on the left */
546 1.5 christos if (sel->modekeys == MODEKEY_EMACS)
547 1.5 christos xx = sel->sx - 1;
548 1.5 christos else
549 1.5 christos xx = sel->sx;
550 1.5 christos if (px > xx || px < sel->ex)
551 1.1 jmmv return (0);
552 1.1 jmmv } else {
553 1.1 jmmv /* selection start (sx) is on the left */
554 1.10 christos if (sel->modekeys == MODEKEY_EMACS)
555 1.10 christos xx = (sel->ex == 0 ? 0 : sel->ex - 1);
556 1.10 christos else
557 1.10 christos xx = sel->ex;
558 1.10 christos if (px < sel->sx || px > xx)
559 1.1 jmmv return (0);
560 1.1 jmmv }
561 1.1 jmmv }
562 1.1 jmmv }
563 1.1 jmmv
564 1.1 jmmv return (1);
565 1.1 jmmv }
566 1.4 christos
567 1.7 christos /* Get selected grid cell. */
568 1.7 christos void
569 1.7 christos screen_select_cell(struct screen *s, struct grid_cell *dst,
570 1.7 christos const struct grid_cell *src)
571 1.7 christos {
572 1.9 christos if (s->sel == NULL || s->sel->hidden)
573 1.7 christos return;
574 1.7 christos
575 1.9 christos memcpy(dst, &s->sel->cell, sizeof *dst);
576 1.7 christos
577 1.7 christos utf8_copy(&dst->data, &src->data);
578 1.7 christos dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
579 1.7 christos dst->attr |= src->attr & GRID_ATTR_CHARSET;
580 1.7 christos dst->flags = src->flags;
581 1.7 christos }
582 1.7 christos
583 1.4 christos /* Reflow wrapped lines. */
584 1.7 christos static void
585 1.12 christos screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
586 1.12 christos {
587 1.12 christos u_int wx, wy;
588 1.12 christos
589 1.12 christos if (cursor) {
590 1.12 christos grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
591 1.12 christos log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
592 1.12 christos wy);
593 1.12 christos }
594 1.12 christos
595 1.12 christos grid_reflow(s->grid, new_x);
596 1.12 christos
597 1.12 christos if (cursor) {
598 1.12 christos grid_unwrap_position(s->grid, cx, cy, wx, wy);
599 1.12 christos log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
600 1.12 christos }
601 1.12 christos else {
602 1.12 christos *cx = 0;
603 1.12 christos *cy = s->grid->hsize;
604 1.12 christos }
605 1.12 christos }
606 1.12 christos
607 1.12 christos /*
608 1.12 christos * Enter alternative screen mode. A copy of the visible screen is saved and the
609 1.12 christos * history is not updated.
610 1.12 christos */
611 1.12 christos void
612 1.12 christos screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
613 1.4 christos {
614 1.12 christos u_int sx, sy;
615 1.12 christos
616 1.12 christos if (s->saved_grid != NULL)
617 1.12 christos return;
618 1.12 christos sx = screen_size_x(s);
619 1.12 christos sy = screen_size_y(s);
620 1.10 christos
621 1.12 christos s->saved_grid = grid_create(sx, sy, 0);
622 1.12 christos grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
623 1.12 christos if (cursor) {
624 1.12 christos s->saved_cx = s->cx;
625 1.12 christos s->saved_cy = s->cy;
626 1.12 christos }
627 1.12 christos memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
628 1.12 christos
629 1.12 christos grid_view_clear(s->grid, 0, 0, sx, sy, 8);
630 1.12 christos
631 1.12 christos s->saved_flags = s->grid->flags;
632 1.12 christos s->grid->flags &= ~GRID_HISTORY;
633 1.12 christos }
634 1.10 christos
635 1.12 christos /* Exit alternate screen mode and restore the copied grid. */
636 1.12 christos void
637 1.12 christos screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
638 1.12 christos {
639 1.12 christos u_int sx = screen_size_x(s), sy = screen_size_y(s);
640 1.10 christos
641 1.12 christos /*
642 1.12 christos * If the current size is different, temporarily resize to the old size
643 1.12 christos * before copying back.
644 1.12 christos */
645 1.12 christos if (s->saved_grid != NULL)
646 1.15 wiz screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 0);
647 1.10 christos
648 1.12 christos /*
649 1.12 christos * Restore the cursor position and cell. This happens even if not
650 1.12 christos * currently in the alternate screen.
651 1.12 christos */
652 1.12 christos if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
653 1.12 christos s->cx = s->saved_cx;
654 1.12 christos s->cy = s->saved_cy;
655 1.12 christos if (gc != NULL)
656 1.12 christos memcpy(gc, &s->saved_cell, sizeof *gc);
657 1.12 christos }
658 1.10 christos
659 1.12 christos /* If not in the alternate screen, do nothing more. */
660 1.12 christos if (s->saved_grid == NULL) {
661 1.12 christos if (s->cx > screen_size_x(s) - 1)
662 1.12 christos s->cx = screen_size_x(s) - 1;
663 1.12 christos if (s->cy > screen_size_y(s) - 1)
664 1.12 christos s->cy = screen_size_y(s) - 1;
665 1.12 christos return;
666 1.10 christos }
667 1.10 christos
668 1.12 christos /* Restore the saved grid. */
669 1.12 christos grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
670 1.12 christos s->saved_grid->sy);
671 1.10 christos
672 1.12 christos /*
673 1.12 christos * Turn history back on (so resize can use it) and then resize back to
674 1.12 christos * the current size.
675 1.12 christos */
676 1.12 christos if (s->saved_flags & GRID_HISTORY)
677 1.12 christos s->grid->flags |= GRID_HISTORY;
678 1.12 christos screen_resize(s, sx, sy, 1);
679 1.12 christos
680 1.12 christos grid_destroy(s->saved_grid);
681 1.12 christos s->saved_grid = NULL;
682 1.12 christos
683 1.12 christos if (s->cx > screen_size_x(s) - 1)
684 1.12 christos s->cx = screen_size_x(s) - 1;
685 1.12 christos if (s->cy > screen_size_y(s) - 1)
686 1.12 christos s->cy = screen_size_y(s) - 1;
687 1.4 christos }
688 1.14 wiz
689 1.14 wiz /* Get mode as a string. */
690 1.14 wiz const char *
691 1.14 wiz screen_mode_to_string(int mode)
692 1.14 wiz {
693 1.14 wiz static char tmp[1024];
694 1.14 wiz
695 1.14 wiz if (mode == 0)
696 1.14 wiz return ("NONE");
697 1.14 wiz if (mode == ALL_MODES)
698 1.14 wiz return ("ALL");
699 1.14 wiz
700 1.14 wiz *tmp = '\0';
701 1.14 wiz if (mode & MODE_CURSOR)
702 1.14 wiz strlcat(tmp, "CURSOR,", sizeof tmp);
703 1.14 wiz if (mode & MODE_INSERT)
704 1.14 wiz strlcat(tmp, "INSERT,", sizeof tmp);
705 1.14 wiz if (mode & MODE_KCURSOR)
706 1.14 wiz strlcat(tmp, "KCURSOR,", sizeof tmp);
707 1.14 wiz if (mode & MODE_KKEYPAD)
708 1.14 wiz strlcat(tmp, "KKEYPAD,", sizeof tmp);
709 1.14 wiz if (mode & MODE_WRAP)
710 1.14 wiz strlcat(tmp, "WRAP,", sizeof tmp);
711 1.14 wiz if (mode & MODE_MOUSE_STANDARD)
712 1.14 wiz strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp);
713 1.14 wiz if (mode & MODE_MOUSE_BUTTON)
714 1.14 wiz strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp);
715 1.14 wiz if (mode & MODE_CURSOR_BLINKING)
716 1.14 wiz strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp);
717 1.14 wiz if (mode & MODE_CURSOR_VERY_VISIBLE)
718 1.14 wiz strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp);
719 1.14 wiz if (mode & MODE_MOUSE_UTF8)
720 1.15 wiz strlcat(tmp, "MOUSE_UTF8,", sizeof tmp);
721 1.14 wiz if (mode & MODE_MOUSE_SGR)
722 1.15 wiz strlcat(tmp, "MOUSE_SGR,", sizeof tmp);
723 1.14 wiz if (mode & MODE_BRACKETPASTE)
724 1.14 wiz strlcat(tmp, "BRACKETPASTE,", sizeof tmp);
725 1.14 wiz if (mode & MODE_FOCUSON)
726 1.14 wiz strlcat(tmp, "FOCUSON,", sizeof tmp);
727 1.14 wiz if (mode & MODE_MOUSE_ALL)
728 1.14 wiz strlcat(tmp, "MOUSE_ALL,", sizeof tmp);
729 1.14 wiz if (mode & MODE_ORIGIN)
730 1.14 wiz strlcat(tmp, "ORIGIN,", sizeof tmp);
731 1.14 wiz if (mode & MODE_CRLF)
732 1.14 wiz strlcat(tmp, "CRLF,", sizeof tmp);
733 1.14 wiz if (mode & MODE_KEXTENDED)
734 1.14 wiz strlcat(tmp, "KEXTENDED,", sizeof tmp);
735 1.14 wiz tmp[strlen(tmp) - 1] = '\0';
736 1.14 wiz return (tmp);
737 1.14 wiz }
738