2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, 2013 The FreeBSD Foundation
7 * This software was developed by Ed Schouten under sponsorship from the
10 * Portions of this software were developed by Oleksandr Rybalko
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/reboot.h>
44 #include <sys/systm.h>
46 #include <dev/vt/vt.h>
48 static MALLOC_DEFINE(M_VTBUF, "vtbuf", "vt buffer");
50 #define VTBUF_LOCK(vb) mtx_lock_spin(&(vb)->vb_lock)
51 #define VTBUF_UNLOCK(vb) mtx_unlock_spin(&(vb)->vb_lock)
53 #define POS_INDEX(c, r) (((r) << 12) + (c))
54 #define POS_COPY(d, s) do { \
55 (d).tp_col = (s).tp_col; \
56 (d).tp_row = (s).tp_row; \
59 #ifndef SC_NO_CUTPASTE
60 static int vtbuf_htw(const struct vt_buf *vb, int row);
61 static int vtbuf_wth(const struct vt_buf *vb, int row);
62 static int vtbuf_in_this_range(int begin, int test, int end, int sz);
67 * line5 <--- curroffset (terminal output to that line)
69 * line1 <--- roffset (history display from that point)
74 vthistory_seek(struct vt_buf *vb, int offset, int whence)
76 int diff, top, bottom, roffset;
78 /* No scrolling if not enabled. */
79 if ((vb->vb_flags & VBF_SCROLL) == 0) {
80 if (vb->vb_roffset != vb->vb_curroffset) {
81 vb->vb_roffset = vb->vb_curroffset;
84 return (0); /* No changes */
87 /* "top" may be a negative integer. */
88 bottom = vb->vb_curroffset;
89 top = (vb->vb_flags & VBF_HISTORY_FULL) ?
90 bottom + vb->vb_scr_size.tp_row - vb->vb_history_size :
93 roffset = 0; /* Make gcc happy. */
98 roffset = top + offset;
102 * Operate on copy of offset value, since it temporary
103 * can be bigger than amount of rows in buffer.
105 roffset = vb->vb_roffset;
106 if (roffset >= bottom + vb->vb_scr_size.tp_row)
107 roffset -= vb->vb_history_size;
110 roffset = MAX(roffset, top);
111 roffset = MIN(roffset, bottom);
114 roffset = vb->vb_history_size + roffset;
118 /* Go to current offset. */
119 roffset = vb->vb_curroffset;
123 diff = vb->vb_roffset != roffset;
124 vb->vb_roffset = roffset;
130 vthistory_addlines(struct vt_buf *vb, int offset)
132 #ifndef SC_NO_CUTPASTE
136 vb->vb_curroffset += offset;
137 if (vb->vb_curroffset + vb->vb_scr_size.tp_row >= vb->vb_history_size) {
138 vb->vb_flags |= VBF_HISTORY_FULL;
139 vb->vb_curroffset %= vb->vb_history_size;
141 if ((vb->vb_flags & VBF_SCROLL) == 0) {
142 vb->vb_roffset = vb->vb_curroffset;
145 #ifndef SC_NO_CUTPASTE
146 sz = vb->vb_history_size;
147 cur = vb->vb_roffset + vb->vb_scr_size.tp_row + sz - 1;
148 if (vtbuf_in_this_range(cur, vb->vb_mark_start.tp_row, cur + offset, sz) ||
149 vtbuf_in_this_range(cur, vb->vb_mark_end.tp_row, cur + offset, sz)) {
150 /* clear screen selection */
151 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row;
152 vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
158 vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
161 *offset = vb->vb_roffset;
164 #ifndef SC_NO_CUTPASTE /* Only mouse support use it now. */
165 /* Translate history row to current view row number. */
167 vtbuf_htw(const struct vt_buf *vb, int row)
172 * History offset roffset winrow
173 * 205 200 ((205 - 200 + 1000) % 1000) = 5
174 * 90 990 ((90 - 990 + 1000) % 1000) = 100
176 return ((row - vb->vb_roffset + vb->vb_history_size) %
177 vb->vb_history_size);
180 /* Translate current view row number to history row. */
182 vtbuf_wth(const struct vt_buf *vb, int row)
185 return ((vb->vb_roffset + row) % vb->vb_history_size);
189 * Test if an index in a circular buffer is within a range.
191 * begin - start index
194 * sz - size of circular buffer when it turns over
197 vtbuf_in_this_range(int begin, int test, int end, int sz)
203 /* check for inversion */
205 return (test >= begin || test < end);
207 return (test >= begin && test < end);
212 vtbuf_iscursor(const struct vt_buf *vb, int row, int col)
214 #ifndef SC_NO_CUTPASTE
215 int sc, sr, sz, ec, er, tmp;
218 if ((vb->vb_flags & (VBF_CURSOR|VBF_SCROLL)) == VBF_CURSOR &&
219 (vb->vb_cursor.tp_row == row) && (vb->vb_cursor.tp_col == col))
222 #ifndef SC_NO_CUTPASTE
223 /* Mark cut/paste region. */
224 if (vb->vb_mark_start.tp_col == vb->vb_mark_end.tp_col &&
225 vb->vb_mark_start.tp_row == vb->vb_mark_end.tp_row)
228 sc = vb->vb_mark_start.tp_col;
229 sr = vb->vb_mark_start.tp_row;
230 ec = vb->vb_mark_end.tp_col;
231 er = vb->vb_mark_end.tp_row;
234 * Information about if the selection was made bottom-top or
235 * top-bottom is lost due to modulo arithmetics and needs to
238 sz = vb->vb_history_size;
239 tmp = (sz + er - sr) % sz;
240 row = vtbuf_wth(vb, row);
242 /* Swap start and end if start > end */
243 if ((2 * tmp) > sz || (tmp == 0 && sc > ec)) {
244 tmp = sc; sc = ec; ec = tmp;
245 tmp = sr; sr = er; er = tmp;
248 if (vtbuf_in_this_range(POS_INDEX(sc, sr), POS_INDEX(col, row),
249 POS_INDEX(ec, er), POS_INDEX(0, sz)))
257 vtbuf_lock(struct vt_buf *vb)
264 vtbuf_unlock(struct vt_buf *vb)
271 vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
274 if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
275 vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
276 if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
277 vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
278 if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
279 vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
280 if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
281 vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
285 vtbuf_dirty_cell(struct vt_buf *vb, const term_pos_t *p)
290 area.tr_end.tp_row = p->tp_row + 1;
291 area.tr_end.tp_col = p->tp_col + 1;
292 vtbuf_dirty(vb, &area);
296 vtbuf_make_undirty(struct vt_buf *vb)
299 vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
300 vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
304 vtbuf_undirty(struct vt_buf *vb, term_rect_t *r)
307 *r = vb->vb_dirtyrect;
308 vtbuf_make_undirty(vb);
312 vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
314 const term_pos_t *p1 = &r->tr_begin;
316 unsigned int rows, cols;
319 KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
320 ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
321 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
322 KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
323 ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
324 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
326 KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
327 ("vtbuf_copy end.tp_row %d must be less than screen width %d",
328 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
329 KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
330 ("vtbuf_copy end.tp_col %d must be less than screen height %d",
331 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
333 KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
334 ("vtbuf_copy tp_row %d must be less than screen width %d",
335 p2->tp_row, vb->vb_scr_size.tp_row));
336 KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
337 ("vtbuf_copy tp_col %d must be less than screen height %d",
338 p2->tp_col, vb->vb_scr_size.tp_col));
340 rows = r->tr_end.tp_row - r->tr_begin.tp_row;
341 rdiff = r->tr_begin.tp_row - p2->tp_row;
342 cols = r->tr_end.tp_col - r->tr_begin.tp_col;
343 if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
344 r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
345 (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
346 rdiff > 0) { /* Only forward direction. Do not eat history. */
347 vthistory_addlines(vb, rdiff);
348 } else if (p2->tp_row < p1->tp_row) {
349 /* Handle overlapping copies of line segments. */
351 for (pr = 0; pr < rows; pr++)
353 &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
354 &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
355 cols * sizeof(term_char_t));
357 /* Move data down. */
358 for (pr = rows - 1; pr >= 0; pr--)
360 &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
361 &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
362 cols * sizeof(term_char_t));
366 area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
367 area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
368 vtbuf_dirty(vb, &area);
372 vtbuf_do_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
377 for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
378 row = vb->vb_rows[(vb->vb_curroffset + pr) %
379 VTBUF_MAX_HEIGHT(vb)];
380 for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
387 vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
390 KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
391 ("vtbuf_fill begin.tp_row %d must be < screen height %d",
392 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
393 KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
394 ("vtbuf_fill begin.tp_col %d must be < screen width %d",
395 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
397 KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
398 ("vtbuf_fill end.tp_row %d must be <= screen height %d",
399 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
400 KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
401 ("vtbuf_fill end.tp_col %d must be <= screen width %d",
402 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
404 vtbuf_do_fill(vb, r, c);
409 vtbuf_init_rows(struct vt_buf *vb)
413 vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
415 for (r = 0; r < vb->vb_history_size; r++)
416 vb->vb_rows[r] = &vb->vb_buffer[r * vb->vb_scr_size.tp_col];
420 vtbuf_init_early(struct vt_buf *vb)
423 const teken_attr_t *a;
426 vb->vb_flags |= VBF_CURSOR;
428 vb->vb_curroffset = 0;
429 vb->vb_mark_start.tp_row = 0;
430 vb->vb_mark_start.tp_col = 0;
431 vb->vb_mark_end.tp_row = 0;
432 vb->vb_mark_end.tp_col = 0;
435 rect.tr_begin.tp_row = rect.tr_begin.tp_col = 0;
436 rect.tr_end.tp_col = vb->vb_scr_size.tp_col;
437 rect.tr_end.tp_row = vb->vb_history_size;
439 a = teken_get_curattr(&vb->vb_terminal->tm_emulator);
440 c = TCOLOR_FG((term_char_t)a->ta_fgcolor) |
441 TCOLOR_BG((term_char_t)a->ta_bgcolor);
442 vtbuf_do_fill(vb, &rect, VTBUF_SPACE_CHAR(c));
443 vtbuf_make_undirty(vb);
444 if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
445 mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
446 vb->vb_flags |= VBF_MTX_INIT;
451 vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
455 vb->vb_scr_size = *p;
456 vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
458 if ((vb->vb_flags & VBF_STATIC) == 0) {
459 sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
460 vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
462 sz = vb->vb_history_size * sizeof(term_char_t *);
463 vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
466 vtbuf_init_early(vb);
470 vtbuf_sethistory_size(struct vt_buf *vb, unsigned int size)
475 p.tp_row = vb->vb_scr_size.tp_row;
476 p.tp_col = vb->vb_scr_size.tp_col;
477 vtbuf_grow(vb, &p, size);
481 vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, unsigned int history_size)
483 term_char_t *old, *new, **rows, **oldrows, **copyrows, *row, *oldrow;
484 unsigned int w, h, c, r, old_history_size;
485 size_t bufsize, rowssize;
487 const teken_attr_t *a;
490 a = teken_get_curattr(&vb->vb_terminal->tm_emulator);
491 ch = TCOLOR_FG(a->ta_fgcolor) | TCOLOR_BG(a->ta_bgcolor);
493 history_size = MAX(history_size, p->tp_row);
495 /* Allocate new buffer. */
496 bufsize = history_size * p->tp_col * sizeof(term_char_t);
497 new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
498 rowssize = history_size * sizeof(term_pos_t *);
499 rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
503 old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
504 oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
505 copyrows = vb->vb_rows;
507 w = vb->vb_scr_size.tp_col;
508 h = vb->vb_scr_size.tp_row;
509 old_history_size = vb->vb_history_size;
510 history_full = vb->vb_flags & VBF_HISTORY_FULL ||
511 vb->vb_curroffset + h >= history_size;
513 vb->vb_history_size = history_size;
516 vb->vb_flags &= ~VBF_STATIC;
517 vb->vb_scr_size = *p;
521 * Copy rows to the new buffer. The first row in the history
522 * is back to index 0, ie. the new buffer doesn't cycle.
524 if (history_size > old_history_size) {
525 for (r = 0; r < old_history_size; r ++) {
528 /* Compute the corresponding row in the old buffer. */
531 * The buffer is full, the "top" row is
532 * the one just after the viewable area
533 * (curroffset + viewable height) in the
534 * cycling buffer. The corresponding row
535 * is computed from this top row.
538 (vb->vb_curroffset + h + r) %
542 * The buffer is not full, therefore,
543 * we didn't cycle already. The
544 * corresponding rows are the same in
547 oldrow = copyrows[r];
550 MIN(p->tp_col, w) * sizeof(term_char_t));
553 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
554 * extended lines of kernel text using the wrong
557 for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
558 row[c] = VTBUF_SPACE_CHAR(ch);
562 /* Fill remaining rows. */
563 for (r = old_history_size; r < history_size; r++) {
565 for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
566 row[c] = VTBUF_SPACE_CHAR(ch);
570 vb->vb_flags &= ~VBF_HISTORY_FULL;
573 * If the screen is already filled (there are non-visible lines
574 * above the current viewable area), adjust curroffset to the
577 * If the old buffer was full, set curroffset to the
578 * <h>th most recent line of history in the new, non-cycled
579 * buffer. Otherwise, it didn't cycle, so the old curroffset
580 * is the same in the new buffer.
583 vb->vb_curroffset = old_history_size - h;
586 * (old_history_size - history_size) lines of history are
589 for (r = 0; r < history_size; r ++) {
593 * Compute the corresponding row in the old buffer.
595 * See the equivalent if{} block above for an
600 (vb->vb_curroffset + h + r +
601 (old_history_size - history_size)) %
604 oldrow = copyrows[r];
607 MIN(p->tp_col, w) * sizeof(term_char_t));
610 * XXX VTBUF_SPACE_CHAR(TERMINAL_NORM_ATTR) will
611 * extended lines of kernel text using the wrong
614 for (c = MIN(p->tp_col, w); c < p->tp_col; c++) {
615 row[c] = VTBUF_SPACE_CHAR(ch);
620 vb->vb_curroffset = history_size - h;
621 vb->vb_flags |= VBF_HISTORY_FULL;
625 vb->vb_roffset = vb->vb_curroffset;
627 /* Adjust cursor position. */
628 if (vb->vb_cursor.tp_col > p->tp_col - 1)
630 * Move cursor to the last column, in case its previous
631 * position is outside of the new screen area.
633 vb->vb_cursor.tp_col = p->tp_col - 1;
635 if (vb->vb_curroffset > 0 || vb->vb_cursor.tp_row > p->tp_row - 1)
636 /* Move cursor to the last line on the screen. */
637 vb->vb_cursor.tp_row = p->tp_row - 1;
641 /* Deallocate old buffer. */
643 free(oldrows, M_VTBUF);
647 vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
651 KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
652 ("vtbuf_putchar tp_row %d must be less than screen width %d",
653 p->tp_row, vb->vb_scr_size.tp_row));
654 KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
655 ("vtbuf_putchar tp_col %d must be less than screen height %d",
656 p->tp_col, vb->vb_scr_size.tp_col));
658 row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
659 VTBUF_MAX_HEIGHT(vb)];
660 if (row[p->tp_col] != c) {
662 vtbuf_dirty_cell(vb, p);
667 vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
669 if (vb->vb_flags & VBF_CURSOR) {
670 vtbuf_dirty_cell(vb, &vb->vb_cursor);
672 vtbuf_dirty_cell(vb, &vb->vb_cursor);
678 #ifndef SC_NO_CUTPASTE
680 vtbuf_flush_mark(struct vt_buf *vb)
685 /* Notify renderer to update marked region. */
686 if ((vb->vb_mark_start.tp_col != vb->vb_mark_end.tp_col) ||
687 (vb->vb_mark_start.tp_row != vb->vb_mark_end.tp_row)) {
689 s = vtbuf_htw(vb, vb->vb_mark_start.tp_row);
690 e = vtbuf_htw(vb, vb->vb_mark_end.tp_row);
692 area.tr_begin.tp_col = 0;
693 area.tr_begin.tp_row = MIN(s, e);
695 area.tr_end.tp_col = vb->vb_scr_size.tp_col;
696 area.tr_end.tp_row = MAX(s, e) + 1;
699 vtbuf_dirty(vb, &area);
705 vtbuf_get_marked_len(struct vt_buf *vb)
710 /* Swap according to window coordinates. */
711 if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
712 vb->vb_mark_start.tp_col) >
713 POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
714 vb->vb_mark_end.tp_col)) {
715 POS_COPY(e, vb->vb_mark_start);
716 POS_COPY(s, vb->vb_mark_end);
718 POS_COPY(s, vb->vb_mark_start);
719 POS_COPY(e, vb->vb_mark_end);
722 si = s.tp_row * vb->vb_scr_size.tp_col + s.tp_col;
723 ei = e.tp_row * vb->vb_scr_size.tp_col + e.tp_col;
725 /* Number symbols and number of rows to inject \n */
726 sz = ei - si + ((e.tp_row - s.tp_row) * 2);
728 return (sz * sizeof(term_char_t));
732 vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz)
737 /* Swap according to window coordinates. */
738 if (POS_INDEX(vtbuf_htw(vb, vb->vb_mark_start.tp_row),
739 vb->vb_mark_start.tp_col) >
740 POS_INDEX(vtbuf_htw(vb, vb->vb_mark_end.tp_row),
741 vb->vb_mark_end.tp_col)) {
742 POS_COPY(e, vb->vb_mark_start);
743 POS_COPY(s, vb->vb_mark_end);
745 POS_COPY(s, vb->vb_mark_start);
746 POS_COPY(e, vb->vb_mark_end);
750 for (r = s.tp_row; r <= e.tp_row; r ++) {
751 cs = (r == s.tp_row)?s.tp_col:0;
752 ce = (r == e.tp_row)?e.tp_col:vb->vb_scr_size.tp_col;
753 for (c = cs; c < ce; c ++) {
754 buf[i++] = vb->vb_rows[r][c];
756 /* Add new line for all rows, but not for last one. */
765 vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row)
771 case VTB_MARK_END: /* B1 UP */
772 if (vb->vb_mark_last != VTB_MARK_MOVE)
776 case VTB_MARK_EXTEND:
777 vtbuf_flush_mark(vb); /* Clean old mark. */
778 vb->vb_mark_end.tp_col = col;
779 vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
782 vtbuf_flush_mark(vb); /* Clean old mark. */
783 vb->vb_mark_start.tp_col = col;
784 vb->vb_mark_start.tp_row = vtbuf_wth(vb, row);
785 /* Start again, so clear end point. */
786 vb->vb_mark_end.tp_col = col;
787 vb->vb_mark_end.tp_row = vtbuf_wth(vb, row);
790 vtbuf_flush_mark(vb); /* Clean old mark. */
791 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
793 r = vb->vb_rows[vb->vb_mark_start.tp_row];
794 for (i = col; i >= 0; i --) {
795 if (TCHAR_CHARACTER(r[i]) == ' ') {
796 vb->vb_mark_start.tp_col = i + 1;
800 for (i = col; i < vb->vb_scr_size.tp_col; i ++) {
801 if (TCHAR_CHARACTER(r[i]) == ' ') {
802 vb->vb_mark_end.tp_col = i;
806 if (vb->vb_mark_start.tp_col > vb->vb_mark_end.tp_col)
807 vb->vb_mark_start.tp_col = vb->vb_mark_end.tp_col;
810 vtbuf_flush_mark(vb); /* Clean old mark. */
811 vb->vb_mark_start.tp_col = 0;
812 vb->vb_mark_end.tp_col = vb->vb_scr_size.tp_col;
813 vb->vb_mark_start.tp_row = vb->vb_mark_end.tp_row =
817 vb->vb_mark_last = type;
824 vb->vb_mark_last = type;
825 /* Draw new marked region. */
826 vtbuf_flush_mark(vb);
832 vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
836 oflags = vb->vb_flags;
838 vb->vb_flags |= VBF_CURSOR;
840 vb->vb_flags &= ~VBF_CURSOR;
841 nflags = vb->vb_flags;
843 if (oflags != nflags)
844 vtbuf_dirty_cell(vb, &vb->vb_cursor);
848 vtbuf_scroll_mode(struct vt_buf *vb, int yes)
853 oflags = vb->vb_flags;
855 vb->vb_flags |= VBF_SCROLL;
857 vb->vb_flags &= ~VBF_SCROLL;
858 nflags = vb->vb_flags;
860 if (oflags != nflags)
861 vtbuf_dirty_cell(vb, &vb->vb_cursor);