refresh.c revision 1.57 1 1.57 ryo /* $NetBSD: refresh.c,v 1.57 2020/03/30 06:54:37 ryo Exp $ */
2 1.2 lukem
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1992, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Christos Zoulas of Cornell University.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.26 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.18 christos #include "config.h"
36 1.1 cgd #if !defined(lint) && !defined(SCCSID)
37 1.2 lukem #if 0
38 1.1 cgd static char sccsid[] = "@(#)refresh.c 8.1 (Berkeley) 6/4/93";
39 1.2 lukem #else
40 1.57 ryo __RCSID("$NetBSD: refresh.c,v 1.57 2020/03/30 06:54:37 ryo Exp $");
41 1.2 lukem #endif
42 1.1 cgd #endif /* not lint && not SCCSID */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * refresh.c: Lower level screen refreshing functions
46 1.1 cgd */
47 1.1 cgd #include <stdio.h>
48 1.57 ryo #include <stdlib.h>
49 1.44 christos #include <string.h>
50 1.1 cgd #include <unistd.h>
51 1.1 cgd
52 1.1 cgd #include "el.h"
53 1.1 cgd
54 1.49 christos static void re_nextline(EditLine *);
55 1.49 christos static void re_addc(EditLine *, wint_t);
56 1.49 christos static void re_update_line(EditLine *, wchar_t *, wchar_t *, int);
57 1.49 christos static void re_insert (EditLine *, wchar_t *, int, int, wchar_t *, int);
58 1.49 christos static void re_delete(EditLine *, wchar_t *, int, int, int);
59 1.49 christos static void re_fastputc(EditLine *, wint_t);
60 1.49 christos static void re_clear_eol(EditLine *, int, int, int);
61 1.49 christos static void re__strncopy(wchar_t *, wchar_t *, size_t);
62 1.49 christos static void re__copy_and_pad(wchar_t *, const wchar_t *, size_t);
63 1.1 cgd
64 1.1 cgd #ifdef DEBUG_REFRESH
65 1.49 christos static void re_printstr(EditLine *, const char *, wchar_t *, wchar_t *);
66 1.15 lukem #define __F el->el_errfile
67 1.44 christos #define ELRE_ASSERT(a, b, c) do \
68 1.20 christos if (/*CONSTCOND*/ a) { \
69 1.1 cgd (void) fprintf b; \
70 1.1 cgd c; \
71 1.1 cgd } \
72 1.20 christos while (/*CONSTCOND*/0)
73 1.17 lukem #define ELRE_DEBUG(a, b) ELRE_ASSERT(a,b,;)
74 1.15 lukem
75 1.1 cgd /* re_printstr():
76 1.1 cgd * Print a string on the debugging pty
77 1.1 cgd */
78 1.49 christos static void
79 1.48 christos re_printstr(EditLine *el, const char *str, wchar_t *f, wchar_t *t)
80 1.1 cgd {
81 1.15 lukem
82 1.17 lukem ELRE_DEBUG(1, (__F, "%s:\"", str));
83 1.15 lukem while (f < t)
84 1.17 lukem ELRE_DEBUG(1, (__F, "%c", *f++ & 0177));
85 1.17 lukem ELRE_DEBUG(1, (__F, "\"\r\n"));
86 1.8 simonb }
87 1.1 cgd #else
88 1.17 lukem #define ELRE_ASSERT(a, b, c)
89 1.17 lukem #define ELRE_DEBUG(a, b)
90 1.1 cgd #endif
91 1.1 cgd
92 1.31 christos /* re_nextline():
93 1.31 christos * Move to the next line or scroll
94 1.31 christos */
95 1.49 christos static void
96 1.31 christos re_nextline(EditLine *el)
97 1.31 christos {
98 1.31 christos el->el_refresh.r_cursor.h = 0; /* reset it. */
99 1.31 christos
100 1.31 christos /*
101 1.31 christos * If we would overflow (input is longer than terminal size),
102 1.31 christos * emulate scroll by dropping first line and shuffling the rest.
103 1.31 christos * We do this via pointer shuffling - it's safe in this case
104 1.31 christos * and we avoid memcpy().
105 1.31 christos */
106 1.36 christos if (el->el_refresh.r_cursor.v + 1 >= el->el_terminal.t_size.v) {
107 1.36 christos int i, lins = el->el_terminal.t_size.v;
108 1.48 christos wchar_t *firstline = el->el_vdisplay[0];
109 1.31 christos
110 1.31 christos for(i = 1; i < lins; i++)
111 1.31 christos el->el_vdisplay[i - 1] = el->el_vdisplay[i];
112 1.31 christos
113 1.44 christos firstline[0] = '\0'; /* empty the string */
114 1.31 christos el->el_vdisplay[i - 1] = firstline;
115 1.31 christos } else
116 1.31 christos el->el_refresh.r_cursor.v++;
117 1.31 christos
118 1.36 christos ELRE_ASSERT(el->el_refresh.r_cursor.v >= el->el_terminal.t_size.v,
119 1.31 christos (__F, "\r\nre_putc: overflow! r_cursor.v == %d > %d\r\n",
120 1.36 christos el->el_refresh.r_cursor.v, el->el_terminal.t_size.v),
121 1.31 christos abort());
122 1.31 christos }
123 1.1 cgd
124 1.1 cgd /* re_addc():
125 1.1 cgd * Draw c, expanding tabs, control chars etc.
126 1.1 cgd */
127 1.49 christos static void
128 1.39 christos re_addc(EditLine *el, wint_t c)
129 1.1 cgd {
130 1.48 christos switch (ct_chr_class(c)) {
131 1.35 christos case CHTYPE_TAB: /* expand the tab */
132 1.35 christos for (;;) {
133 1.35 christos re_putc(el, ' ', 1);
134 1.35 christos if ((el->el_refresh.r_cursor.h & 07) == 0)
135 1.35 christos break; /* go until tab stop */
136 1.35 christos }
137 1.35 christos break;
138 1.35 christos case CHTYPE_NL: {
139 1.15 lukem int oldv = el->el_refresh.r_cursor.v;
140 1.16 jdolecek re_putc(el, '\0', 0); /* assure end of line */
141 1.31 christos if (oldv == el->el_refresh.r_cursor.v) /* XXX */
142 1.31 christos re_nextline(el);
143 1.35 christos break;
144 1.35 christos }
145 1.35 christos case CHTYPE_PRINT:
146 1.35 christos re_putc(el, c, 1);
147 1.35 christos break;
148 1.35 christos default: {
149 1.48 christos wchar_t visbuf[VISUAL_WIDTH_MAX];
150 1.35 christos ssize_t i, n =
151 1.48 christos ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
152 1.35 christos for (i = 0; n-- > 0; ++i)
153 1.35 christos re_putc(el, visbuf[i], 1);
154 1.35 christos break;
155 1.15 lukem }
156 1.15 lukem }
157 1.15 lukem }
158 1.1 cgd
159 1.53 christos /* re_putliteral():
160 1.52 christos * Place the literal string given
161 1.52 christos */
162 1.52 christos libedit_private void
163 1.52 christos re_putliteral(EditLine *el, const wchar_t *begin, const wchar_t *end)
164 1.52 christos {
165 1.52 christos coord_t *cur = &el->el_refresh.r_cursor;
166 1.52 christos wint_t c;
167 1.52 christos int sizeh = el->el_terminal.t_size.h;
168 1.54 kre int i, w;
169 1.52 christos
170 1.54 kre c = literal_add(el, begin, end, &w);
171 1.54 kre if (c == 0 || w <= 0)
172 1.52 christos return;
173 1.52 christos el->el_vdisplay[cur->v][cur->h] = c;
174 1.54 kre
175 1.54 kre i = w;
176 1.54 kre if (i > sizeh - cur->h) /* avoid overflow */
177 1.54 kre i = sizeh - cur->h;
178 1.54 kre while (--i > 0)
179 1.54 kre el->el_vdisplay[cur->v][cur->h + i] = MB_FILL_CHAR;
180 1.54 kre
181 1.54 kre cur->h += w;
182 1.52 christos if (cur->h >= sizeh) {
183 1.52 christos /* assure end of line */
184 1.52 christos el->el_vdisplay[cur->v][sizeh] = '\0';
185 1.52 christos re_nextline(el);
186 1.52 christos }
187 1.52 christos }
188 1.1 cgd
189 1.1 cgd /* re_putc():
190 1.1 cgd * Draw the character given
191 1.1 cgd */
192 1.51 christos libedit_private void
193 1.39 christos re_putc(EditLine *el, wint_t c, int shift)
194 1.1 cgd {
195 1.52 christos coord_t *cur = &el->el_refresh.r_cursor;
196 1.46 christos int i, w = wcwidth(c);
197 1.52 christos int sizeh = el->el_terminal.t_size.h;
198 1.52 christos
199 1.39 christos ELRE_DEBUG(1, (__F, "printing %5x '%lc'\r\n", c, c));
200 1.46 christos if (w == -1)
201 1.46 christos w = 0;
202 1.35 christos
203 1.52 christos while (shift && (cur->h + w > sizeh))
204 1.35 christos re_putc(el, ' ', 1);
205 1.1 cgd
206 1.52 christos el->el_vdisplay[cur->v][cur->h] = c;
207 1.35 christos /* assumes !shift is only used for single-column chars */
208 1.35 christos i = w;
209 1.35 christos while (--i > 0)
210 1.52 christos el->el_vdisplay[cur->v][cur->h + i] = MB_FILL_CHAR;
211 1.15 lukem
212 1.16 jdolecek if (!shift)
213 1.16 jdolecek return;
214 1.16 jdolecek
215 1.52 christos cur->h += w; /* advance to next place */
216 1.52 christos if (cur->h >= sizeh) {
217 1.15 lukem /* assure end of line */
218 1.52 christos el->el_vdisplay[cur->v][sizeh] = '\0';
219 1.31 christos re_nextline(el);
220 1.31 christos }
221 1.15 lukem }
222 1.15 lukem
223 1.1 cgd
224 1.1 cgd /* re_refresh():
225 1.1 cgd * draws the new virtual screen image from the current input
226 1.44 christos * line, then goes line-by-line changing the real image to the new
227 1.1 cgd * virtual image. The routine to re-draw a line can be replaced
228 1.1 cgd * easily in hopes of a smarter one being placed there.
229 1.1 cgd */
230 1.51 christos libedit_private void
231 1.15 lukem re_refresh(EditLine *el)
232 1.1 cgd {
233 1.15 lukem int i, rhdiff;
234 1.48 christos wchar_t *cp, *st;
235 1.15 lukem coord_t cur;
236 1.16 jdolecek #ifdef notyet
237 1.16 jdolecek size_t termsz;
238 1.16 jdolecek #endif
239 1.15 lukem
240 1.46 christos ELRE_DEBUG(1, (__F, "el->el_line.buffer = :%ls:\r\n",
241 1.17 lukem el->el_line.buffer));
242 1.15 lukem
243 1.52 christos literal_clear(el);
244 1.15 lukem /* reset the Drawing cursor */
245 1.15 lukem el->el_refresh.r_cursor.h = 0;
246 1.15 lukem el->el_refresh.r_cursor.v = 0;
247 1.15 lukem
248 1.52 christos terminal_move_to_char(el, 0);
249 1.52 christos
250 1.15 lukem /* temporarily draw rprompt to calculate its size */
251 1.15 lukem prompt_print(el, EL_RPROMPT);
252 1.15 lukem
253 1.15 lukem /* reset the Drawing cursor */
254 1.15 lukem el->el_refresh.r_cursor.h = 0;
255 1.15 lukem el->el_refresh.r_cursor.v = 0;
256 1.15 lukem
257 1.22 christos if (el->el_line.cursor >= el->el_line.lastchar) {
258 1.22 christos if (el->el_map.current == el->el_map.alt
259 1.22 christos && el->el_line.lastchar != el->el_line.buffer)
260 1.22 christos el->el_line.cursor = el->el_line.lastchar - 1;
261 1.22 christos else
262 1.22 christos el->el_line.cursor = el->el_line.lastchar;
263 1.22 christos }
264 1.22 christos
265 1.15 lukem cur.h = -1; /* set flag in case I'm not set */
266 1.15 lukem cur.v = 0;
267 1.15 lukem
268 1.15 lukem prompt_print(el, EL_PROMPT);
269 1.15 lukem
270 1.15 lukem /* draw the current input buffer */
271 1.16 jdolecek #if notyet
272 1.36 christos termsz = el->el_terminal.t_size.h * el->el_terminal.t_size.v;
273 1.16 jdolecek if (el->el_line.lastchar - el->el_line.buffer > termsz) {
274 1.16 jdolecek /*
275 1.16 jdolecek * If line is longer than terminal, process only part
276 1.16 jdolecek * of line which would influence display.
277 1.16 jdolecek */
278 1.16 jdolecek size_t rem = (el->el_line.lastchar-el->el_line.buffer)%termsz;
279 1.16 jdolecek
280 1.16 jdolecek st = el->el_line.lastchar - rem
281 1.36 christos - (termsz - (((rem / el->el_terminal.t_size.v) - 1)
282 1.36 christos * el->el_terminal.t_size.v));
283 1.16 jdolecek } else
284 1.16 jdolecek #endif
285 1.16 jdolecek st = el->el_line.buffer;
286 1.16 jdolecek
287 1.16 jdolecek for (cp = st; cp < el->el_line.lastchar; cp++) {
288 1.15 lukem if (cp == el->el_line.cursor) {
289 1.46 christos int w = wcwidth(*cp);
290 1.16 jdolecek /* save for later */
291 1.15 lukem cur.h = el->el_refresh.r_cursor.h;
292 1.15 lukem cur.v = el->el_refresh.r_cursor.v;
293 1.35 christos /* handle being at a linebroken doublewidth char */
294 1.35 christos if (w > 1 && el->el_refresh.r_cursor.h + w >
295 1.36 christos el->el_terminal.t_size.h) {
296 1.35 christos cur.h = 0;
297 1.35 christos cur.v++;
298 1.35 christos }
299 1.15 lukem }
300 1.35 christos re_addc(el, *cp);
301 1.15 lukem }
302 1.15 lukem
303 1.15 lukem if (cur.h == -1) { /* if I haven't been set yet, I'm at the end */
304 1.15 lukem cur.h = el->el_refresh.r_cursor.h;
305 1.15 lukem cur.v = el->el_refresh.r_cursor.v;
306 1.15 lukem }
307 1.36 christos rhdiff = el->el_terminal.t_size.h - el->el_refresh.r_cursor.h -
308 1.15 lukem el->el_rprompt.p_pos.h;
309 1.15 lukem if (el->el_rprompt.p_pos.h && !el->el_rprompt.p_pos.v &&
310 1.15 lukem !el->el_refresh.r_cursor.v && rhdiff > 1) {
311 1.15 lukem /*
312 1.15 lukem * have a right-hand side prompt that will fit
313 1.15 lukem * on the end of the first line with at least
314 1.15 lukem * one character gap to the input buffer.
315 1.15 lukem */
316 1.15 lukem while (--rhdiff > 0) /* pad out with spaces */
317 1.16 jdolecek re_putc(el, ' ', 1);
318 1.15 lukem prompt_print(el, EL_RPROMPT);
319 1.15 lukem } else {
320 1.15 lukem el->el_rprompt.p_pos.h = 0; /* flag "not using rprompt" */
321 1.15 lukem el->el_rprompt.p_pos.v = 0;
322 1.15 lukem }
323 1.15 lukem
324 1.16 jdolecek re_putc(el, '\0', 0); /* make line ended with NUL, no cursor shift */
325 1.16 jdolecek
326 1.15 lukem el->el_refresh.r_newcv = el->el_refresh.r_cursor.v;
327 1.15 lukem
328 1.15 lukem ELRE_DEBUG(1, (__F,
329 1.15 lukem "term.h=%d vcur.h=%d vcur.v=%d vdisplay[0]=\r\n:%80.80s:\r\n",
330 1.36 christos el->el_terminal.t_size.h, el->el_refresh.r_cursor.h,
331 1.45 christos el->el_refresh.r_cursor.v, ct_encode_string(el->el_vdisplay[0],
332 1.45 christos &el->el_scratch)));
333 1.15 lukem
334 1.17 lukem ELRE_DEBUG(1, (__F, "updating %d lines.\r\n", el->el_refresh.r_newcv));
335 1.15 lukem for (i = 0; i <= el->el_refresh.r_newcv; i++) {
336 1.15 lukem /* NOTE THAT re_update_line MAY CHANGE el_display[i] */
337 1.15 lukem re_update_line(el, el->el_display[i], el->el_vdisplay[i], i);
338 1.15 lukem
339 1.15 lukem /*
340 1.15 lukem * Copy the new line to be the current one, and pad out with
341 1.15 lukem * spaces to the full width of the terminal so that if we try
342 1.15 lukem * moving the cursor by writing the character that is at the
343 1.15 lukem * end of the screen line, it won't be a NUL or some old
344 1.15 lukem * leftover stuff.
345 1.15 lukem */
346 1.15 lukem re__copy_and_pad(el->el_display[i], el->el_vdisplay[i],
347 1.36 christos (size_t) el->el_terminal.t_size.h);
348 1.15 lukem }
349 1.15 lukem ELRE_DEBUG(1, (__F,
350 1.15 lukem "\r\nel->el_refresh.r_cursor.v=%d,el->el_refresh.r_oldcv=%d i=%d\r\n",
351 1.17 lukem el->el_refresh.r_cursor.v, el->el_refresh.r_oldcv, i));
352 1.15 lukem
353 1.15 lukem if (el->el_refresh.r_oldcv > el->el_refresh.r_newcv)
354 1.15 lukem for (; i <= el->el_refresh.r_oldcv; i++) {
355 1.36 christos terminal_move_to_line(el, i);
356 1.36 christos terminal_move_to_char(el, 0);
357 1.47 christos /* This wcslen should be safe even with MB_FILL_CHARs */
358 1.47 christos terminal_clear_EOL(el, (int) wcslen(el->el_display[i]));
359 1.1 cgd #ifdef DEBUG_REFRESH
360 1.47 christos terminal_overwrite(el, L"C\b", 2);
361 1.1 cgd #endif /* DEBUG_REFRESH */
362 1.16 jdolecek el->el_display[i][0] = '\0';
363 1.15 lukem }
364 1.8 simonb
365 1.15 lukem el->el_refresh.r_oldcv = el->el_refresh.r_newcv; /* set for next time */
366 1.15 lukem ELRE_DEBUG(1, (__F,
367 1.15 lukem "\r\ncursor.h = %d, cursor.v = %d, cur.h = %d, cur.v = %d\r\n",
368 1.15 lukem el->el_refresh.r_cursor.h, el->el_refresh.r_cursor.v,
369 1.17 lukem cur.h, cur.v));
370 1.36 christos terminal_move_to_line(el, cur.v); /* go to where the cursor is */
371 1.36 christos terminal_move_to_char(el, cur.h);
372 1.15 lukem }
373 1.1 cgd
374 1.1 cgd
375 1.1 cgd /* re_goto_bottom():
376 1.8 simonb * used to go to last used screen line
377 1.1 cgd */
378 1.51 christos libedit_private void
379 1.15 lukem re_goto_bottom(EditLine *el)
380 1.1 cgd {
381 1.15 lukem
382 1.36 christos terminal_move_to_line(el, el->el_refresh.r_oldcv);
383 1.36 christos terminal__putc(el, '\n');
384 1.15 lukem re_clear_display(el);
385 1.36 christos terminal__flush(el);
386 1.15 lukem }
387 1.1 cgd
388 1.1 cgd
389 1.1 cgd /* re_insert():
390 1.1 cgd * insert num characters of s into d (in front of the character)
391 1.8 simonb * at dat, maximum length of d is dlen
392 1.1 cgd */
393 1.49 christos static void
394 1.1 cgd /*ARGSUSED*/
395 1.25 christos re_insert(EditLine *el __attribute__((__unused__)),
396 1.48 christos wchar_t *d, int dat, int dlen, wchar_t *s, int num)
397 1.1 cgd {
398 1.48 christos wchar_t *a, *b;
399 1.1 cgd
400 1.15 lukem if (num <= 0)
401 1.15 lukem return;
402 1.15 lukem if (num > dlen - dat)
403 1.15 lukem num = dlen - dat;
404 1.1 cgd
405 1.15 lukem ELRE_DEBUG(1,
406 1.15 lukem (__F, "re_insert() starting: %d at %d max %d, d == \"%s\"\n",
407 1.45 christos num, dat, dlen, ct_encode_string(d, &el->el_scratch)));
408 1.45 christos ELRE_DEBUG(1, (__F, "s == \"%s\"\n", ct_encode_string(s,
409 1.45 christos &el->el_scratch)));
410 1.1 cgd
411 1.15 lukem /* open up the space for num chars */
412 1.15 lukem if (num > 0) {
413 1.15 lukem b = d + dlen - 1;
414 1.15 lukem a = b - num;
415 1.15 lukem while (a >= &d[dat])
416 1.15 lukem *b-- = *a--;
417 1.15 lukem d[dlen] = '\0'; /* just in case */
418 1.15 lukem }
419 1.35 christos
420 1.15 lukem ELRE_DEBUG(1, (__F,
421 1.1 cgd "re_insert() after insert: %d at %d max %d, d == \"%s\"\n",
422 1.45 christos num, dat, dlen, ct_encode_string(d, &el->el_scratch)));
423 1.45 christos ELRE_DEBUG(1, (__F, "s == \"%s\"\n", ct_encode_string(s,
424 1.45 christos &el->el_scratch)));
425 1.1 cgd
426 1.15 lukem /* copy the characters */
427 1.15 lukem for (a = d + dat; (a < d + dlen) && (num > 0); num--)
428 1.15 lukem *a++ = *s++;
429 1.15 lukem
430 1.35 christos #ifdef notyet
431 1.35 christos /* ct_encode_string() uses a static buffer, so we can't conveniently
432 1.35 christos * encode both d & s here */
433 1.15 lukem ELRE_DEBUG(1,
434 1.15 lukem (__F, "re_insert() after copy: %d at %d max %d, %s == \"%s\"\n",
435 1.17 lukem num, dat, dlen, d, s));
436 1.27 christos ELRE_DEBUG(1, (__F, "s == \"%s\"\n", s));
437 1.35 christos #endif
438 1.15 lukem }
439 1.1 cgd
440 1.1 cgd
441 1.1 cgd /* re_delete():
442 1.8 simonb * delete num characters d at dat, maximum length of d is dlen
443 1.1 cgd */
444 1.49 christos static void
445 1.1 cgd /*ARGSUSED*/
446 1.25 christos re_delete(EditLine *el __attribute__((__unused__)),
447 1.48 christos wchar_t *d, int dat, int dlen, int num)
448 1.1 cgd {
449 1.48 christos wchar_t *a, *b;
450 1.1 cgd
451 1.15 lukem if (num <= 0)
452 1.15 lukem return;
453 1.15 lukem if (dat + num >= dlen) {
454 1.15 lukem d[dat] = '\0';
455 1.15 lukem return;
456 1.15 lukem }
457 1.15 lukem ELRE_DEBUG(1,
458 1.15 lukem (__F, "re_delete() starting: %d at %d max %d, d == \"%s\"\n",
459 1.45 christos num, dat, dlen, ct_encode_string(d, &el->el_scratch)));
460 1.1 cgd
461 1.15 lukem /* open up the space for num chars */
462 1.15 lukem if (num > 0) {
463 1.15 lukem b = d + dat;
464 1.15 lukem a = b + num;
465 1.15 lukem while (a < &d[dlen])
466 1.15 lukem *b++ = *a++;
467 1.15 lukem d[dlen] = '\0'; /* just in case */
468 1.15 lukem }
469 1.15 lukem ELRE_DEBUG(1,
470 1.15 lukem (__F, "re_delete() after delete: %d at %d max %d, d == \"%s\"\n",
471 1.45 christos num, dat, dlen, ct_encode_string(d, &el->el_scratch)));
472 1.15 lukem }
473 1.1 cgd
474 1.1 cgd
475 1.1 cgd /* re__strncopy():
476 1.1 cgd * Like strncpy without padding.
477 1.1 cgd */
478 1.49 christos static void
479 1.48 christos re__strncopy(wchar_t *a, wchar_t *b, size_t n)
480 1.1 cgd {
481 1.15 lukem
482 1.15 lukem while (n-- && *b)
483 1.15 lukem *a++ = *b++;
484 1.15 lukem }
485 1.1 cgd
486 1.27 christos /* re_clear_eol():
487 1.27 christos * Find the number of characters we need to clear till the end of line
488 1.27 christos * in order to make sure that we have cleared the previous contents of
489 1.27 christos * the line. fx and sx is the number of characters inserted or deleted
490 1.35 christos * in the first or second diff, diff is the difference between the
491 1.44 christos * number of characters between the new and old line.
492 1.27 christos */
493 1.49 christos static void
494 1.27 christos re_clear_eol(EditLine *el, int fx, int sx, int diff)
495 1.27 christos {
496 1.27 christos
497 1.27 christos ELRE_DEBUG(1, (__F, "re_clear_eol sx %d, fx %d, diff %d\n",
498 1.27 christos sx, fx, diff));
499 1.27 christos
500 1.27 christos if (fx < 0)
501 1.27 christos fx = -fx;
502 1.27 christos if (sx < 0)
503 1.27 christos sx = -sx;
504 1.27 christos if (fx > diff)
505 1.27 christos diff = fx;
506 1.27 christos if (sx > diff)
507 1.27 christos diff = sx;
508 1.27 christos
509 1.27 christos ELRE_DEBUG(1, (__F, "re_clear_eol %d\n", diff));
510 1.36 christos terminal_clear_EOL(el, diff);
511 1.27 christos }
512 1.1 cgd
513 1.15 lukem /*****************************************************************
514 1.1 cgd re_update_line() is based on finding the middle difference of each line
515 1.1 cgd on the screen; vis:
516 1.1 cgd
517 1.1 cgd /old first difference
518 1.1 cgd /beginning of line | /old last same /old EOL
519 1.1 cgd v v v v
520 1.1 cgd old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
521 1.1 cgd new: eddie> Oh, my little buggy says to me, as lurgid as
522 1.1 cgd ^ ^ ^ ^
523 1.1 cgd \beginning of line | \new last same \new end of line
524 1.1 cgd \new first difference
525 1.1 cgd
526 1.1 cgd all are character pointers for the sake of speed. Special cases for
527 1.1 cgd no differences, as well as for end of line additions must be handled.
528 1.1 cgd **************************************************************** */
529 1.1 cgd
530 1.1 cgd /* Minimum at which doing an insert it "worth it". This should be about
531 1.1 cgd * half the "cost" of going into insert mode, inserting a character, and
532 1.1 cgd * going back out. This should really be calculated from the termcap
533 1.1 cgd * data... For the moment, a good number for ANSI terminals.
534 1.1 cgd */
535 1.15 lukem #define MIN_END_KEEP 4
536 1.1 cgd
537 1.49 christos static void
538 1.48 christos re_update_line(EditLine *el, wchar_t *old, wchar_t *new, int i)
539 1.1 cgd {
540 1.48 christos wchar_t *o, *n, *p, c;
541 1.48 christos wchar_t *ofd, *ols, *oe, *nfd, *nls, *ne;
542 1.48 christos wchar_t *osb, *ose, *nsb, *nse;
543 1.15 lukem int fx, sx;
544 1.30 christos size_t len;
545 1.15 lukem
546 1.15 lukem /*
547 1.15 lukem * find first diff
548 1.15 lukem */
549 1.15 lukem for (o = old, n = new; *o && (*o == *n); o++, n++)
550 1.15 lukem continue;
551 1.15 lukem ofd = o;
552 1.15 lukem nfd = n;
553 1.15 lukem
554 1.15 lukem /*
555 1.15 lukem * Find the end of both old and new
556 1.15 lukem */
557 1.15 lukem while (*o)
558 1.15 lukem o++;
559 1.15 lukem /*
560 1.15 lukem * Remove any trailing blanks off of the end, being careful not to
561 1.15 lukem * back up past the beginning.
562 1.15 lukem */
563 1.15 lukem while (ofd < o) {
564 1.15 lukem if (o[-1] != ' ')
565 1.15 lukem break;
566 1.15 lukem o--;
567 1.15 lukem }
568 1.15 lukem oe = o;
569 1.15 lukem *oe = '\0';
570 1.15 lukem
571 1.15 lukem while (*n)
572 1.15 lukem n++;
573 1.15 lukem
574 1.15 lukem /* remove blanks from end of new */
575 1.15 lukem while (nfd < n) {
576 1.15 lukem if (n[-1] != ' ')
577 1.15 lukem break;
578 1.15 lukem n--;
579 1.15 lukem }
580 1.15 lukem ne = n;
581 1.15 lukem *ne = '\0';
582 1.15 lukem
583 1.15 lukem /*
584 1.15 lukem * if no diff, continue to next line of redraw
585 1.15 lukem */
586 1.15 lukem if (*ofd == '\0' && *nfd == '\0') {
587 1.17 lukem ELRE_DEBUG(1, (__F, "no difference.\r\n"));
588 1.15 lukem return;
589 1.15 lukem }
590 1.15 lukem /*
591 1.15 lukem * find last same pointer
592 1.15 lukem */
593 1.15 lukem while ((o > ofd) && (n > nfd) && (*--o == *--n))
594 1.15 lukem continue;
595 1.15 lukem ols = ++o;
596 1.15 lukem nls = ++n;
597 1.15 lukem
598 1.15 lukem /*
599 1.50 christos * find same beginning and same end
600 1.15 lukem */
601 1.1 cgd osb = ols;
602 1.15 lukem nsb = nls;
603 1.1 cgd ose = ols;
604 1.1 cgd nse = nls;
605 1.1 cgd
606 1.15 lukem /*
607 1.15 lukem * case 1: insert: scan from nfd to nls looking for *ofd
608 1.15 lukem */
609 1.15 lukem if (*ofd) {
610 1.15 lukem for (c = *ofd, n = nfd; n < nls; n++) {
611 1.15 lukem if (c == *n) {
612 1.15 lukem for (o = ofd, p = n;
613 1.15 lukem p < nls && o < ols && *o == *p;
614 1.15 lukem o++, p++)
615 1.15 lukem continue;
616 1.15 lukem /*
617 1.15 lukem * if the new match is longer and it's worth
618 1.15 lukem * keeping, then we take it
619 1.15 lukem */
620 1.15 lukem if (((nse - nsb) < (p - n)) &&
621 1.15 lukem (2 * (p - n) > n - nfd)) {
622 1.15 lukem nsb = n;
623 1.15 lukem nse = p;
624 1.15 lukem osb = ofd;
625 1.15 lukem ose = o;
626 1.15 lukem }
627 1.15 lukem }
628 1.15 lukem }
629 1.15 lukem }
630 1.15 lukem /*
631 1.15 lukem * case 2: delete: scan from ofd to ols looking for *nfd
632 1.15 lukem */
633 1.15 lukem if (*nfd) {
634 1.15 lukem for (c = *nfd, o = ofd; o < ols; o++) {
635 1.15 lukem if (c == *o) {
636 1.15 lukem for (n = nfd, p = o;
637 1.15 lukem p < ols && n < nls && *p == *n;
638 1.15 lukem p++, n++)
639 1.15 lukem continue;
640 1.15 lukem /*
641 1.15 lukem * if the new match is longer and it's worth
642 1.15 lukem * keeping, then we take it
643 1.15 lukem */
644 1.15 lukem if (((ose - osb) < (p - o)) &&
645 1.15 lukem (2 * (p - o) > o - ofd)) {
646 1.15 lukem nsb = nfd;
647 1.15 lukem nse = n;
648 1.15 lukem osb = o;
649 1.15 lukem ose = p;
650 1.15 lukem }
651 1.15 lukem }
652 1.15 lukem }
653 1.15 lukem }
654 1.15 lukem /*
655 1.15 lukem * Pragmatics I: If old trailing whitespace or not enough characters to
656 1.15 lukem * save to be worth it, then don't save the last same info.
657 1.15 lukem */
658 1.15 lukem if ((oe - ols) < MIN_END_KEEP) {
659 1.15 lukem ols = oe;
660 1.15 lukem nls = ne;
661 1.15 lukem }
662 1.15 lukem /*
663 1.15 lukem * Pragmatics II: if the terminal isn't smart enough, make the data
664 1.15 lukem * dumber so the smart update doesn't try anything fancy
665 1.15 lukem */
666 1.15 lukem
667 1.15 lukem /*
668 1.15 lukem * fx is the number of characters we need to insert/delete: in the
669 1.15 lukem * beginning to bring the two same begins together
670 1.15 lukem */
671 1.29 christos fx = (int)((nsb - nfd) - (osb - ofd));
672 1.15 lukem /*
673 1.15 lukem * sx is the number of characters we need to insert/delete: in the
674 1.15 lukem * end to bring the two same last parts together
675 1.15 lukem */
676 1.29 christos sx = (int)((nls - nse) - (ols - ose));
677 1.15 lukem
678 1.15 lukem if (!EL_CAN_INSERT) {
679 1.15 lukem if (fx > 0) {
680 1.15 lukem osb = ols;
681 1.15 lukem ose = ols;
682 1.15 lukem nsb = nls;
683 1.15 lukem nse = nls;
684 1.15 lukem }
685 1.15 lukem if (sx > 0) {
686 1.15 lukem ols = oe;
687 1.15 lukem nls = ne;
688 1.15 lukem }
689 1.15 lukem if ((ols - ofd) < (nls - nfd)) {
690 1.15 lukem ols = oe;
691 1.15 lukem nls = ne;
692 1.15 lukem }
693 1.15 lukem }
694 1.15 lukem if (!EL_CAN_DELETE) {
695 1.15 lukem if (fx < 0) {
696 1.15 lukem osb = ols;
697 1.15 lukem ose = ols;
698 1.15 lukem nsb = nls;
699 1.15 lukem nse = nls;
700 1.15 lukem }
701 1.15 lukem if (sx < 0) {
702 1.15 lukem ols = oe;
703 1.15 lukem nls = ne;
704 1.15 lukem }
705 1.15 lukem if ((ols - ofd) > (nls - nfd)) {
706 1.15 lukem ols = oe;
707 1.15 lukem nls = ne;
708 1.15 lukem }
709 1.15 lukem }
710 1.15 lukem /*
711 1.15 lukem * Pragmatics III: make sure the middle shifted pointers are correct if
712 1.15 lukem * they don't point to anything (we may have moved ols or nls).
713 1.15 lukem */
714 1.15 lukem /* if the change isn't worth it, don't bother */
715 1.15 lukem /* was: if (osb == ose) */
716 1.15 lukem if ((ose - osb) < MIN_END_KEEP) {
717 1.15 lukem osb = ols;
718 1.15 lukem ose = ols;
719 1.15 lukem nsb = nls;
720 1.15 lukem nse = nls;
721 1.15 lukem }
722 1.15 lukem /*
723 1.15 lukem * Now that we are done with pragmatics we recompute fx, sx
724 1.15 lukem */
725 1.29 christos fx = (int)((nsb - nfd) - (osb - ofd));
726 1.29 christos sx = (int)((nls - nse) - (ols - ose));
727 1.15 lukem
728 1.27 christos ELRE_DEBUG(1, (__F, "fx %d, sx %d\n", fx, sx));
729 1.45 christos ELRE_DEBUG(1, (__F, "ofd %td, osb %td, ose %td, ols %td, oe %td\n",
730 1.17 lukem ofd - old, osb - old, ose - old, ols - old, oe - old));
731 1.45 christos ELRE_DEBUG(1, (__F, "nfd %td, nsb %td, nse %td, nls %td, ne %td\n",
732 1.17 lukem nfd - new, nsb - new, nse - new, nls - new, ne - new));
733 1.15 lukem ELRE_DEBUG(1, (__F,
734 1.17 lukem "xxx-xxx:\"00000000001111111111222222222233333333334\"\r\n"));
735 1.15 lukem ELRE_DEBUG(1, (__F,
736 1.17 lukem "xxx-xxx:\"01234567890123456789012345678901234567890\"\r\n"));
737 1.1 cgd #ifdef DEBUG_REFRESH
738 1.15 lukem re_printstr(el, "old- oe", old, oe);
739 1.15 lukem re_printstr(el, "new- ne", new, ne);
740 1.15 lukem re_printstr(el, "old-ofd", old, ofd);
741 1.15 lukem re_printstr(el, "new-nfd", new, nfd);
742 1.15 lukem re_printstr(el, "ofd-osb", ofd, osb);
743 1.15 lukem re_printstr(el, "nfd-nsb", nfd, nsb);
744 1.15 lukem re_printstr(el, "osb-ose", osb, ose);
745 1.15 lukem re_printstr(el, "nsb-nse", nsb, nse);
746 1.15 lukem re_printstr(el, "ose-ols", ose, ols);
747 1.15 lukem re_printstr(el, "nse-nls", nse, nls);
748 1.15 lukem re_printstr(el, "ols- oe", ols, oe);
749 1.15 lukem re_printstr(el, "nls- ne", nls, ne);
750 1.1 cgd #endif /* DEBUG_REFRESH */
751 1.1 cgd
752 1.15 lukem /*
753 1.15 lukem * el_cursor.v to this line i MUST be in this routine so that if we
754 1.15 lukem * don't have to change the line, we don't move to it. el_cursor.h to
755 1.15 lukem * first diff char
756 1.15 lukem */
757 1.36 christos terminal_move_to_line(el, i);
758 1.15 lukem
759 1.15 lukem /*
760 1.15 lukem * at this point we have something like this:
761 1.15 lukem *
762 1.15 lukem * /old /ofd /osb /ose /ols /oe
763 1.15 lukem * v.....................v v..................v v........v
764 1.15 lukem * eddie> Oh, my fredded gruntle-buggy is to me, as foo var lurgid as
765 1.15 lukem * eddie> Oh, my fredded quiux buggy is to me, as gruntle-lurgid as
766 1.15 lukem * ^.....................^ ^..................^ ^........^
767 1.15 lukem * \new \nfd \nsb \nse \nls \ne
768 1.15 lukem *
769 1.15 lukem * fx is the difference in length between the chars between nfd and
770 1.15 lukem * nsb, and the chars between ofd and osb, and is thus the number of
771 1.15 lukem * characters to delete if < 0 (new is shorter than old, as above),
772 1.15 lukem * or insert (new is longer than short).
773 1.15 lukem *
774 1.15 lukem * sx is the same for the second differences.
775 1.15 lukem */
776 1.15 lukem
777 1.15 lukem /*
778 1.15 lukem * if we have a net insert on the first difference, AND inserting the
779 1.15 lukem * net amount ((nsb-nfd) - (osb-ofd)) won't push the last useful
780 1.15 lukem * character (which is ne if nls != ne, otherwise is nse) off the edge
781 1.36 christos * of the screen (el->el_terminal.t_size.h) else we do the deletes first
782 1.15 lukem * so that we keep everything we need to.
783 1.15 lukem */
784 1.15 lukem
785 1.15 lukem /*
786 1.15 lukem * if the last same is the same like the end, there is no last same
787 1.15 lukem * part, otherwise we want to keep the last same part set p to the
788 1.15 lukem * last useful old character
789 1.15 lukem */
790 1.15 lukem p = (ols != oe) ? oe : ose;
791 1.15 lukem
792 1.15 lukem /*
793 1.15 lukem * if (There is a diffence in the beginning) && (we need to insert
794 1.15 lukem * characters) && (the number of characters to insert is less than
795 1.15 lukem * the term width)
796 1.15 lukem * We need to do an insert!
797 1.15 lukem * else if (we need to delete characters)
798 1.15 lukem * We need to delete characters!
799 1.15 lukem * else
800 1.15 lukem * No insert or delete
801 1.15 lukem */
802 1.15 lukem if ((nsb != nfd) && fx > 0 &&
803 1.36 christos ((p - old) + fx <= el->el_terminal.t_size.h)) {
804 1.15 lukem ELRE_DEBUG(1,
805 1.45 christos (__F, "first diff insert at %td...\r\n", nfd - new));
806 1.1 cgd /*
807 1.15 lukem * Move to the first char to insert, where the first diff is.
808 1.15 lukem */
809 1.36 christos terminal_move_to_char(el, (int)(nfd - new));
810 1.15 lukem /*
811 1.15 lukem * Check if we have stuff to keep at end
812 1.15 lukem */
813 1.15 lukem if (nsb != ne) {
814 1.17 lukem ELRE_DEBUG(1, (__F, "with stuff to keep at end\r\n"));
815 1.15 lukem /*
816 1.15 lukem * insert fx chars of new starting at nfd
817 1.15 lukem */
818 1.15 lukem if (fx > 0) {
819 1.15 lukem ELRE_DEBUG(!EL_CAN_INSERT, (__F,
820 1.17 lukem "ERROR: cannot insert in early first diff\n"));
821 1.36 christos terminal_insertwrite(el, nfd, fx);
822 1.29 christos re_insert(el, old, (int)(ofd - old),
823 1.36 christos el->el_terminal.t_size.h, nfd, fx);
824 1.15 lukem }
825 1.15 lukem /*
826 1.15 lukem * write (nsb-nfd) - fx chars of new starting at
827 1.15 lukem * (nfd + fx)
828 1.15 lukem */
829 1.30 christos len = (size_t) ((nsb - nfd) - fx);
830 1.36 christos terminal_overwrite(el, (nfd + fx), len);
831 1.30 christos re__strncopy(ofd + fx, nfd + fx, len);
832 1.15 lukem } else {
833 1.17 lukem ELRE_DEBUG(1, (__F, "without anything to save\r\n"));
834 1.30 christos len = (size_t)(nsb - nfd);
835 1.36 christos terminal_overwrite(el, nfd, len);
836 1.30 christos re__strncopy(ofd, nfd, len);
837 1.15 lukem /*
838 1.15 lukem * Done
839 1.15 lukem */
840 1.15 lukem return;
841 1.15 lukem }
842 1.15 lukem } else if (fx < 0) {
843 1.15 lukem ELRE_DEBUG(1,
844 1.45 christos (__F, "first diff delete at %td...\r\n", ofd - old));
845 1.15 lukem /*
846 1.15 lukem * move to the first char to delete where the first diff is
847 1.15 lukem */
848 1.36 christos terminal_move_to_char(el, (int)(ofd - old));
849 1.15 lukem /*
850 1.15 lukem * Check if we have stuff to save
851 1.15 lukem */
852 1.15 lukem if (osb != oe) {
853 1.17 lukem ELRE_DEBUG(1, (__F, "with stuff to save at end\r\n"));
854 1.15 lukem /*
855 1.15 lukem * fx is less than zero *always* here but we check
856 1.15 lukem * for code symmetry
857 1.15 lukem */
858 1.15 lukem if (fx < 0) {
859 1.15 lukem ELRE_DEBUG(!EL_CAN_DELETE, (__F,
860 1.17 lukem "ERROR: cannot delete in first diff\n"));
861 1.36 christos terminal_deletechars(el, -fx);
862 1.29 christos re_delete(el, old, (int)(ofd - old),
863 1.36 christos el->el_terminal.t_size.h, -fx);
864 1.15 lukem }
865 1.15 lukem /*
866 1.15 lukem * write (nsb-nfd) chars of new starting at nfd
867 1.15 lukem */
868 1.30 christos len = (size_t) (nsb - nfd);
869 1.36 christos terminal_overwrite(el, nfd, len);
870 1.30 christos re__strncopy(ofd, nfd, len);
871 1.15 lukem
872 1.15 lukem } else {
873 1.15 lukem ELRE_DEBUG(1, (__F,
874 1.17 lukem "but with nothing left to save\r\n"));
875 1.15 lukem /*
876 1.15 lukem * write (nsb-nfd) chars of new starting at nfd
877 1.15 lukem */
878 1.36 christos terminal_overwrite(el, nfd, (size_t)(nsb - nfd));
879 1.29 christos re_clear_eol(el, fx, sx,
880 1.29 christos (int)((oe - old) - (ne - new)));
881 1.15 lukem /*
882 1.15 lukem * Done
883 1.15 lukem */
884 1.15 lukem return;
885 1.15 lukem }
886 1.15 lukem } else
887 1.15 lukem fx = 0;
888 1.15 lukem
889 1.36 christos if (sx < 0 && (ose - old) + fx < el->el_terminal.t_size.h) {
890 1.15 lukem ELRE_DEBUG(1, (__F,
891 1.45 christos "second diff delete at %td...\r\n", (ose - old) + fx));
892 1.15 lukem /*
893 1.15 lukem * Check if we have stuff to delete
894 1.15 lukem */
895 1.15 lukem /*
896 1.15 lukem * fx is the number of characters inserted (+) or deleted (-)
897 1.1 cgd */
898 1.15 lukem
899 1.36 christos terminal_move_to_char(el, (int)((ose - old) + fx));
900 1.15 lukem /*
901 1.15 lukem * Check if we have stuff to save
902 1.15 lukem */
903 1.15 lukem if (ols != oe) {
904 1.17 lukem ELRE_DEBUG(1, (__F, "with stuff to save at end\r\n"));
905 1.15 lukem /*
906 1.15 lukem * Again a duplicate test.
907 1.15 lukem */
908 1.15 lukem if (sx < 0) {
909 1.15 lukem ELRE_DEBUG(!EL_CAN_DELETE, (__F,
910 1.17 lukem "ERROR: cannot delete in second diff\n"));
911 1.36 christos terminal_deletechars(el, -sx);
912 1.15 lukem }
913 1.15 lukem /*
914 1.15 lukem * write (nls-nse) chars of new starting at nse
915 1.15 lukem */
916 1.36 christos terminal_overwrite(el, nse, (size_t)(nls - nse));
917 1.15 lukem } else {
918 1.15 lukem ELRE_DEBUG(1, (__F,
919 1.17 lukem "but with nothing left to save\r\n"));
920 1.36 christos terminal_overwrite(el, nse, (size_t)(nls - nse));
921 1.29 christos re_clear_eol(el, fx, sx,
922 1.29 christos (int)((oe - old) - (ne - new)));
923 1.15 lukem }
924 1.15 lukem }
925 1.15 lukem /*
926 1.15 lukem * if we have a first insert AND WE HAVEN'T ALREADY DONE IT...
927 1.15 lukem */
928 1.15 lukem if ((nsb != nfd) && (osb - ofd) <= (nsb - nfd) && (fx == 0)) {
929 1.45 christos ELRE_DEBUG(1, (__F, "late first diff insert at %td...\r\n",
930 1.17 lukem nfd - new));
931 1.15 lukem
932 1.36 christos terminal_move_to_char(el, (int)(nfd - new));
933 1.15 lukem /*
934 1.15 lukem * Check if we have stuff to keep at the end
935 1.15 lukem */
936 1.15 lukem if (nsb != ne) {
937 1.17 lukem ELRE_DEBUG(1, (__F, "with stuff to keep at end\r\n"));
938 1.15 lukem /*
939 1.15 lukem * We have to recalculate fx here because we set it
940 1.15 lukem * to zero above as a flag saying that we hadn't done
941 1.15 lukem * an early first insert.
942 1.15 lukem */
943 1.29 christos fx = (int)((nsb - nfd) - (osb - ofd));
944 1.15 lukem if (fx > 0) {
945 1.15 lukem /*
946 1.15 lukem * insert fx chars of new starting at nfd
947 1.15 lukem */
948 1.15 lukem ELRE_DEBUG(!EL_CAN_INSERT, (__F,
949 1.17 lukem "ERROR: cannot insert in late first diff\n"));
950 1.36 christos terminal_insertwrite(el, nfd, fx);
951 1.29 christos re_insert(el, old, (int)(ofd - old),
952 1.36 christos el->el_terminal.t_size.h, nfd, fx);
953 1.15 lukem }
954 1.15 lukem /*
955 1.15 lukem * write (nsb-nfd) - fx chars of new starting at
956 1.15 lukem * (nfd + fx)
957 1.15 lukem */
958 1.30 christos len = (size_t) ((nsb - nfd) - fx);
959 1.36 christos terminal_overwrite(el, (nfd + fx), len);
960 1.30 christos re__strncopy(ofd + fx, nfd + fx, len);
961 1.15 lukem } else {
962 1.17 lukem ELRE_DEBUG(1, (__F, "without anything to save\r\n"));
963 1.30 christos len = (size_t) (nsb - nfd);
964 1.36 christos terminal_overwrite(el, nfd, len);
965 1.30 christos re__strncopy(ofd, nfd, len);
966 1.15 lukem }
967 1.15 lukem }
968 1.15 lukem /*
969 1.15 lukem * line is now NEW up to nse
970 1.15 lukem */
971 1.15 lukem if (sx >= 0) {
972 1.15 lukem ELRE_DEBUG(1, (__F,
973 1.29 christos "second diff insert at %d...\r\n", (int)(nse - new)));
974 1.36 christos terminal_move_to_char(el, (int)(nse - new));
975 1.15 lukem if (ols != oe) {
976 1.17 lukem ELRE_DEBUG(1, (__F, "with stuff to keep at end\r\n"));
977 1.15 lukem if (sx > 0) {
978 1.15 lukem /* insert sx chars of new starting at nse */
979 1.15 lukem ELRE_DEBUG(!EL_CAN_INSERT, (__F,
980 1.17 lukem "ERROR: cannot insert in second diff\n"));
981 1.36 christos terminal_insertwrite(el, nse, sx);
982 1.15 lukem }
983 1.15 lukem /*
984 1.15 lukem * write (nls-nse) - sx chars of new starting at
985 1.15 lukem * (nse + sx)
986 1.15 lukem */
987 1.36 christos terminal_overwrite(el, (nse + sx),
988 1.30 christos (size_t)((nls - nse) - sx));
989 1.15 lukem } else {
990 1.17 lukem ELRE_DEBUG(1, (__F, "without anything to save\r\n"));
991 1.36 christos terminal_overwrite(el, nse, (size_t)(nls - nse));
992 1.15 lukem
993 1.15 lukem /*
994 1.15 lukem * No need to do a clear-to-end here because we were
995 1.15 lukem * doing a second insert, so we will have over
996 1.15 lukem * written all of the old string.
997 1.15 lukem */
998 1.15 lukem }
999 1.15 lukem }
1000 1.17 lukem ELRE_DEBUG(1, (__F, "done.\r\n"));
1001 1.15 lukem }
1002 1.1 cgd
1003 1.1 cgd
1004 1.1 cgd /* re__copy_and_pad():
1005 1.1 cgd * Copy string and pad with spaces
1006 1.1 cgd */
1007 1.49 christos static void
1008 1.48 christos re__copy_and_pad(wchar_t *dst, const wchar_t *src, size_t width)
1009 1.1 cgd {
1010 1.21 thorpej size_t i;
1011 1.1 cgd
1012 1.15 lukem for (i = 0; i < width; i++) {
1013 1.15 lukem if (*src == '\0')
1014 1.15 lukem break;
1015 1.15 lukem *dst++ = *src++;
1016 1.15 lukem }
1017 1.15 lukem
1018 1.16 jdolecek for (; i < width; i++)
1019 1.15 lukem *dst++ = ' ';
1020 1.16 jdolecek
1021 1.15 lukem *dst = '\0';
1022 1.15 lukem }
1023 1.1 cgd
1024 1.1 cgd
1025 1.1 cgd /* re_refresh_cursor():
1026 1.1 cgd * Move to the new cursor position
1027 1.1 cgd */
1028 1.51 christos libedit_private void
1029 1.15 lukem re_refresh_cursor(EditLine *el)
1030 1.1 cgd {
1031 1.48 christos wchar_t *cp;
1032 1.35 christos int h, v, th, w;
1033 1.15 lukem
1034 1.22 christos if (el->el_line.cursor >= el->el_line.lastchar) {
1035 1.22 christos if (el->el_map.current == el->el_map.alt
1036 1.22 christos && el->el_line.lastchar != el->el_line.buffer)
1037 1.22 christos el->el_line.cursor = el->el_line.lastchar - 1;
1038 1.22 christos else
1039 1.22 christos el->el_line.cursor = el->el_line.lastchar;
1040 1.22 christos }
1041 1.22 christos
1042 1.15 lukem /* first we must find where the cursor is... */
1043 1.15 lukem h = el->el_prompt.p_pos.h;
1044 1.15 lukem v = el->el_prompt.p_pos.v;
1045 1.36 christos th = el->el_terminal.t_size.h; /* optimize for speed */
1046 1.15 lukem
1047 1.15 lukem /* do input buffer to el->el_line.cursor */
1048 1.15 lukem for (cp = el->el_line.buffer; cp < el->el_line.cursor; cp++) {
1049 1.35 christos switch (ct_chr_class(*cp)) {
1050 1.35 christos case CHTYPE_NL: /* handle newline in data part too */
1051 1.15 lukem h = 0;
1052 1.15 lukem v++;
1053 1.32 christos break;
1054 1.35 christos case CHTYPE_TAB: /* if a tab, to next tab stop */
1055 1.32 christos while (++h & 07)
1056 1.32 christos continue;
1057 1.32 christos break;
1058 1.32 christos default:
1059 1.46 christos w = wcwidth(*cp);
1060 1.35 christos if (w > 1 && h + w > th) { /* won't fit on line */
1061 1.35 christos h = 0;
1062 1.35 christos v++;
1063 1.35 christos }
1064 1.35 christos h += ct_visual_width(*cp);
1065 1.32 christos break;
1066 1.35 christos }
1067 1.1 cgd
1068 1.15 lukem if (h >= th) { /* check, extra long tabs picked up here also */
1069 1.32 christos h -= th;
1070 1.15 lukem v++;
1071 1.15 lukem }
1072 1.15 lukem }
1073 1.35 christos /* if we have a next character, and it's a doublewidth one, we need to
1074 1.35 christos * check whether we need to linebreak for it to fit */
1075 1.46 christos if (cp < el->el_line.lastchar && (w = wcwidth(*cp)) > 1)
1076 1.35 christos if (h + w > th) {
1077 1.35 christos h = 0;
1078 1.35 christos v++;
1079 1.35 christos }
1080 1.15 lukem
1081 1.15 lukem /* now go there */
1082 1.36 christos terminal_move_to_line(el, v);
1083 1.36 christos terminal_move_to_char(el, h);
1084 1.36 christos terminal__flush(el);
1085 1.15 lukem }
1086 1.1 cgd
1087 1.1 cgd
1088 1.1 cgd /* re_fastputc():
1089 1.1 cgd * Add a character fast.
1090 1.1 cgd */
1091 1.49 christos static void
1092 1.39 christos re_fastputc(EditLine *el, wint_t c)
1093 1.1 cgd {
1094 1.55 christos wchar_t *lastline;
1095 1.55 christos int w;
1096 1.55 christos
1097 1.55 christos w = wcwidth(c);
1098 1.36 christos while (w > 1 && el->el_cursor.h + w > el->el_terminal.t_size.h)
1099 1.35 christos re_fastputc(el, ' ');
1100 1.15 lukem
1101 1.36 christos terminal__putc(el, c);
1102 1.48 christos el->el_display[el->el_cursor.v][el->el_cursor.h++] = c;
1103 1.35 christos while (--w > 0)
1104 1.35 christos el->el_display[el->el_cursor.v][el->el_cursor.h++]
1105 1.35 christos = MB_FILL_CHAR;
1106 1.35 christos
1107 1.36 christos if (el->el_cursor.h >= el->el_terminal.t_size.h) {
1108 1.15 lukem /* if we must overflow */
1109 1.15 lukem el->el_cursor.h = 0;
1110 1.16 jdolecek
1111 1.16 jdolecek /*
1112 1.16 jdolecek * If we would overflow (input is longer than terminal size),
1113 1.16 jdolecek * emulate scroll by dropping first line and shuffling the rest.
1114 1.16 jdolecek * We do this via pointer shuffling - it's safe in this case
1115 1.16 jdolecek * and we avoid memcpy().
1116 1.16 jdolecek */
1117 1.36 christos if (el->el_cursor.v + 1 >= el->el_terminal.t_size.v) {
1118 1.36 christos int i, lins = el->el_terminal.t_size.v;
1119 1.44 christos
1120 1.55 christos lastline = el->el_display[0];
1121 1.31 christos for(i = 1; i < lins; i++)
1122 1.31 christos el->el_display[i - 1] = el->el_display[i];
1123 1.16 jdolecek
1124 1.55 christos el->el_display[i - 1] = lastline;
1125 1.16 jdolecek } else {
1126 1.16 jdolecek el->el_cursor.v++;
1127 1.55 christos lastline = el->el_display[++el->el_refresh.r_oldcv];
1128 1.16 jdolecek }
1129 1.55 christos re__copy_and_pad(lastline, L"", (size_t)el->el_terminal.t_size.h);
1130 1.55 christos
1131 1.15 lukem if (EL_HAS_AUTO_MARGINS) {
1132 1.15 lukem if (EL_HAS_MAGIC_MARGINS) {
1133 1.36 christos terminal__putc(el, ' ');
1134 1.36 christos terminal__putc(el, '\b');
1135 1.15 lukem }
1136 1.15 lukem } else {
1137 1.36 christos terminal__putc(el, '\r');
1138 1.36 christos terminal__putc(el, '\n');
1139 1.15 lukem }
1140 1.12 christos }
1141 1.15 lukem }
1142 1.1 cgd
1143 1.1 cgd
1144 1.1 cgd /* re_fastaddc():
1145 1.1 cgd * we added just one char, handle it fast.
1146 1.8 simonb * Assumes that screen cursor == real cursor
1147 1.1 cgd */
1148 1.51 christos libedit_private void
1149 1.15 lukem re_fastaddc(EditLine *el)
1150 1.1 cgd {
1151 1.48 christos wchar_t c;
1152 1.15 lukem int rhdiff;
1153 1.1 cgd
1154 1.15 lukem c = el->el_line.cursor[-1];
1155 1.1 cgd
1156 1.15 lukem if (c == '\t' || el->el_line.cursor != el->el_line.lastchar) {
1157 1.15 lukem re_refresh(el); /* too hard to handle */
1158 1.15 lukem return;
1159 1.15 lukem }
1160 1.36 christos rhdiff = el->el_terminal.t_size.h - el->el_cursor.h -
1161 1.15 lukem el->el_rprompt.p_pos.h;
1162 1.15 lukem if (el->el_rprompt.p_pos.h && rhdiff < 3) {
1163 1.15 lukem re_refresh(el); /* clear out rprompt if less than 1 char gap */
1164 1.15 lukem return;
1165 1.15 lukem } /* else (only do at end of line, no TAB) */
1166 1.35 christos switch (ct_chr_class(c)) {
1167 1.35 christos case CHTYPE_TAB: /* already handled, should never happen here */
1168 1.35 christos break;
1169 1.35 christos case CHTYPE_NL:
1170 1.35 christos case CHTYPE_PRINT:
1171 1.15 lukem re_fastputc(el, c);
1172 1.35 christos break;
1173 1.35 christos case CHTYPE_ASCIICTL:
1174 1.35 christos case CHTYPE_NONPRINT: {
1175 1.48 christos wchar_t visbuf[VISUAL_WIDTH_MAX];
1176 1.35 christos ssize_t i, n =
1177 1.48 christos ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
1178 1.35 christos for (i = 0; n-- > 0; ++i)
1179 1.35 christos re_fastputc(el, visbuf[i]);
1180 1.35 christos break;
1181 1.35 christos }
1182 1.15 lukem }
1183 1.36 christos terminal__flush(el);
1184 1.15 lukem }
1185 1.1 cgd
1186 1.1 cgd
1187 1.1 cgd /* re_clear_display():
1188 1.8 simonb * clear the screen buffers so that new new prompt starts fresh.
1189 1.1 cgd */
1190 1.51 christos libedit_private void
1191 1.15 lukem re_clear_display(EditLine *el)
1192 1.1 cgd {
1193 1.15 lukem int i;
1194 1.1 cgd
1195 1.15 lukem el->el_cursor.v = 0;
1196 1.15 lukem el->el_cursor.h = 0;
1197 1.36 christos for (i = 0; i < el->el_terminal.t_size.v; i++)
1198 1.15 lukem el->el_display[i][0] = '\0';
1199 1.15 lukem el->el_refresh.r_oldcv = 0;
1200 1.15 lukem }
1201 1.1 cgd
1202 1.1 cgd
1203 1.1 cgd /* re_clear_lines():
1204 1.8 simonb * Make sure all lines are *really* blank
1205 1.1 cgd */
1206 1.51 christos libedit_private void
1207 1.15 lukem re_clear_lines(EditLine *el)
1208 1.1 cgd {
1209 1.15 lukem
1210 1.15 lukem if (EL_CAN_CEOL) {
1211 1.15 lukem int i;
1212 1.33 christos for (i = el->el_refresh.r_oldcv; i >= 0; i--) {
1213 1.15 lukem /* for each line on the screen */
1214 1.36 christos terminal_move_to_line(el, i);
1215 1.36 christos terminal_move_to_char(el, 0);
1216 1.36 christos terminal_clear_EOL(el, el->el_terminal.t_size.h);
1217 1.15 lukem }
1218 1.15 lukem } else {
1219 1.36 christos terminal_move_to_line(el, el->el_refresh.r_oldcv);
1220 1.15 lukem /* go to last line */
1221 1.36 christos terminal__putc(el, '\r'); /* go to BOL */
1222 1.36 christos terminal__putc(el, '\n'); /* go to new line */
1223 1.15 lukem }
1224 1.15 lukem }
1225