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