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>
28 void screen_resize_x(struct screen
*, u_int
);
29 void screen_resize_y(struct screen
*, u_int
);
31 /* Create a new screen. */
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
);
42 s
->title
= xstrdup("");
49 /* Reinitialise screen. */
51 screen_reinit(struct screen
*s
)
57 s
->rlower
= screen_size_y(s
) - 1;
59 s
->mode
= MODE_CURSOR
| MODE_WRAP
;
63 grid_clear_lines(s
->grid
, s
->grid
->hsize
, s
->grid
->sy
);
65 screen_clear_selection(s
);
68 /* Destroy a screen. */
70 screen_free(struct screen
*s
)
75 grid_destroy(s
->grid
);
78 /* Reset tabs to default, eight spaces apart. */
80 screen_reset_tabs(struct screen
*s
)
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)
93 /* Set screen title. */
95 screen_set_title(struct screen
*s
, const char *title
)
99 strnvis(tmp
, title
, sizeof tmp
, VIS_OCTAL
|VIS_TAB
|VIS_NL
);
102 s
->title
= xstrdup(tmp
);
107 screen_resize(struct screen
*s
, u_int sx
, u_int sy
)
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
);
130 screen_resize_x(struct screen
*s
, u_int sx
)
132 struct grid
*gd
= s
->grid
;
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
152 screen_resize_y(struct screen
*s
, u_int sy
)
154 struct grid
*gd
= s
->grid
;
155 u_int needed
, available
, oldy
, i
;
159 oldy
= screen_size_y(s
);
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. */
175 /* Delete as many lines as possible from the bottom. */
176 available
= oldy
- 1 - s
->cy
;
178 if (available
> needed
)
180 grid_view_delete_lines(gd
, oldy
- available
, 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?
192 if (gd
->flags
& GRID_HISTORY
)
194 else if (needed
> 0 && available
> 0) {
195 if (available
> needed
)
197 grid_view_delete_lines(gd
, 0, available
);
202 /* Resize line arrays. */
203 gd
->linedata
= xrealloc(
204 gd
->linedata
, gd
->hsize
+ sy
, sizeof *gd
->linedata
);
206 /* Size increasing. */
211 * Try to pull as much as possible out of the history, if is
214 available
= gd
->hsize
;
215 if (gd
->flags
& GRID_HISTORY
&& available
> 0) {
216 if (available
> needed
)
218 gd
->hsize
-= 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. */
232 s
->rlower
= screen_size_y(s
) - 1;
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
);
244 sel
->rectflag
= rectflag
;
246 sel
->sx
= sx
; sel
->sy
= sy
;
247 sel
->ex
= ex
; sel
->ey
= ey
;
250 /* Clear selection. */
252 screen_clear_selection(struct screen
*s
)
254 struct screen_sel
*sel
= &s
->sel
;
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
;
269 if (sel
->sy
< sel
->ey
) {
270 /* start line < end line -- downward selection. */
271 if (py
< sel
->sy
|| py
> sel
->ey
)
273 } else if (sel
->sy
> sel
->ey
) {
274 /* start line > end line -- upward selection. */
275 if (py
> sel
->sy
|| py
< sel
->ey
)
278 /* starting line == ending line. */
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. */
296 /* Selection start (sx) is on the left. */
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
)
313 if ((py
== sel
->sy
&& px
< sel
->sx
)
314 || (py
== sel
->ey
&& px
> sel
->ex
))
316 } else if (sel
->sy
> sel
->ey
) {
317 /* starting line > ending line -- upward selection. */
318 if (py
> sel
->sy
|| py
< sel
->ey
)
321 if ((py
== sel
->sy
&& px
>= sel
->sx
)
322 || (py
== sel
->ey
&& px
< sel
->ex
))
325 /* starting line == ending line. */
329 if (sel
->ex
< sel
->sx
) {
330 /* cursor (ex) is on the left */
331 if (px
> sel
->sx
|| px
< sel
->ex
)
334 /* selection start (sx) is on the left */
335 if (px
< sel
->sx
|| px
> sel
->ex
)