1 /* $FreeBSD$ */
2 /* display.c -- readline redisplay facility. */
3
4 /* Copyright (C) 1987-2006 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
13
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
24
25 #if defined (HAVE_CONFIG_H)
26 # include <config.h>
27 #endif
28
29 #include <sys/types.h>
30
31 #if defined (HAVE_UNISTD_H)
32 # include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
34
35 #include "posixstat.h"
36
37 #if defined (HAVE_STDLIB_H)
38 # include <stdlib.h>
39 #else
40 # include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
42
43 #include <stdio.h>
44
45 /* System-specific feature definitions and include files. */
46 #include "rldefs.h"
47 #include "rlmbutil.h"
48
49 /* Termcap library stuff. */
50 #include "tcap.h"
51
52 /* Some standard library routines. */
53 #include "readline.h"
54 #include "history.h"
55
56 #include "rlprivate.h"
57 #include "xmalloc.h"
58
59 #if !defined (strchr) && !defined (__STDC__)
60 extern char *strchr (), *strrchr ();
61 #endif /* !strchr && !__STDC__ */
62
63 static void update_line PARAMS((char *, char *, int, int, int, int));
64 static void space_to_eol PARAMS((int));
65 static void delete_chars PARAMS((int));
66 static void insert_some_chars PARAMS((char *, int, int));
67 static void cr PARAMS((void));
68
69 #if defined (HANDLE_MULTIBYTE)
70 static int _rl_col_width PARAMS((const char *, int, int));
71 static int *_rl_wrapped_line;
72 #else
73 # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
74 #endif
75
76 static int *inv_lbreaks, *vis_lbreaks;
77 static int inv_lbsize, vis_lbsize;
78
79 /* Heuristic used to decide whether it is faster to move from CUR to NEW
80 by backing up or outputting a carriage return and moving forward. CUR
81 and NEW are either both buffer positions or absolute screen positions. */
82 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
83
84 /* _rl_last_c_pos is an absolute cursor position in multibyte locales and a
85 buffer index in others. This macro is used when deciding whether the
86 current cursor position is in the middle of a prompt string containing
87 invisible characters. */
88 #define PROMPT_ENDING_INDEX \
89 ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars : prompt_last_invisible+1)
90
91
92 /* **************************************************************** */
93 /* */
94 /* Display stuff */
95 /* */
96 /* **************************************************************** */
97
98 /* This is the stuff that is hard for me. I never seem to write good
99 display routines in C. Let's see how I do this time. */
100
101 /* (PWP) Well... Good for a simple line updater, but totally ignores
102 the problems of input lines longer than the screen width.
103
104 update_line and the code that calls it makes a multiple line,
105 automatically wrapping line update. Careful attention needs
106 to be paid to the vertical position variables. */
107
108 /* Keep two buffers; one which reflects the current contents of the
109 screen, and the other to draw what we think the new contents should
110 be. Then compare the buffers, and make whatever changes to the
111 screen itself that we should. Finally, make the buffer that we
112 just drew into be the one which reflects the current contents of the
113 screen, and place the cursor where it belongs.
114
115 Commands that want to can fix the display themselves, and then let
116 this function know that the display has been fixed by setting the
117 RL_DISPLAY_FIXED variable. This is good for efficiency. */
118
119 /* Application-specific redisplay function. */
120 rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
121
122 /* Global variables declared here. */
123 /* What YOU turn on when you have handled all redisplay yourself. */
124 int rl_display_fixed = 0;
125
126 int _rl_suppress_redisplay = 0;
127 int _rl_want_redisplay = 0;
128
129 /* The stuff that gets printed out before the actual text of the line.
130 This is usually pointing to rl_prompt. */
131 char *rl_display_prompt = (char *)NULL;
132
133 /* Pseudo-global variables declared here. */
134
135 /* The visible cursor position. If you print some text, adjust this. */
136 /* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
137 supporting multibyte characters, and an absolute cursor position when
138 in such a locale. This is an artifact of the donated multibyte support.
139 Care must be taken when modifying its value. */
140 int _rl_last_c_pos = 0;
141 int _rl_last_v_pos = 0;
142
143 static int cpos_adjusted;
144 static int cpos_buffer_position;
145
146 /* Number of lines currently on screen minus 1. */
147 int _rl_vis_botlin = 0;
148
149 /* Variables used only in this file. */
150 /* The last left edge of text that was displayed. This is used when
151 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
152 static int last_lmargin;
153
154 /* The line display buffers. One is the line currently displayed on
155 the screen. The other is the line about to be displayed. */
156 static char *visible_line = (char *)NULL;
157 static char *invisible_line = (char *)NULL;
158
159 /* A buffer for `modeline' messages. */
160 static char msg_buf[128];
161
162 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
163 static int forced_display;
164
165 /* Default and initial buffer size. Can grow. */
166 static int line_size = 1024;
167
168 /* Variables to keep track of the expanded prompt string, which may
169 include invisible characters. */
170
171 static char *local_prompt, *local_prompt_prefix;
172 static int local_prompt_len;
173 static int prompt_visible_length, prompt_prefix_length;
174
175 /* The number of invisible characters in the line currently being
176 displayed on the screen. */
177 static int visible_wrap_offset;
178
179 /* The number of invisible characters in the prompt string. Static so it
180 can be shared between rl_redisplay and update_line */
181 static int wrap_offset;
182
183 /* The index of the last invisible character in the prompt string. */
184 static int prompt_last_invisible;
185
186 /* The length (buffer offset) of the first line of the last (possibly
187 multi-line) buffer displayed on the screen. */
188 static int visible_first_line_len;
189
190 /* Number of invisible characters on the first physical line of the prompt.
191 Only valid when the number of physical characters in the prompt exceeds
192 (or is equal to) _rl_screenwidth. */
193 static int prompt_invis_chars_first_line;
194
195 static int prompt_last_screen_line;
196
197 static int prompt_physical_chars;
198
199 /* Variables to save and restore prompt and display information. */
200
201 /* These are getting numerous enough that it's time to create a struct. */
202
203 static char *saved_local_prompt;
204 static char *saved_local_prefix;
205 static int saved_last_invisible;
206 static int saved_visible_length;
207 static int saved_prefix_length;
208 static int saved_local_length;
209 static int saved_invis_chars_first_line;
210 static int saved_physical_chars;
211
212 /* Expand the prompt string S and return the number of visible
213 characters in *LP, if LP is not null. This is currently more-or-less
214 a placeholder for expansion. LIP, if non-null is a place to store the
215 index of the last invisible character in the returned string. NIFLP,
216 if non-zero, is a place to store the number of invisible characters in
217 the first prompt line. The previous are used as byte counts -- indexes
218 into a character buffer. */
219
220 /* Current implementation:
221 \001 (^A) start non-visible characters
222 \002 (^B) end non-visible characters
223 all characters except \001 and \002 (following a \001) are copied to
224 the returned string; all characters except those between \001 and
225 \002 are assumed to be `visible'. */
226
227 static char *
expand_prompt(pmt,lp,lip,niflp,vlp)228 expand_prompt (pmt, lp, lip, niflp, vlp)
229 char *pmt;
230 int *lp, *lip, *niflp, *vlp;
231 {
232 char *r, *ret, *p, *igstart;
233 int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
234
235 /* Short-circuit if we can. */
236 if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
237 {
238 r = savestring (pmt);
239 if (lp)
240 *lp = strlen (r);
241 if (lip)
242 *lip = 0;
243 if (niflp)
244 *niflp = 0;
245 if (vlp)
246 *vlp = lp ? *lp : strlen (r);
247 return r;
248 }
249
250 l = strlen (pmt);
251 r = ret = (char *)xmalloc (l + 1);
252
253 invfl = 0; /* invisible chars in first line of prompt */
254 invflset = 0; /* we only want to set invfl once */
255
256 igstart = 0;
257 for (rl = ignoring = last = ninvis = physchars = 0, p = pmt; p && *p; p++)
258 {
259 /* This code strips the invisible character string markers
260 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
261 if (ignoring == 0 && *p == RL_PROMPT_START_IGNORE) /* XXX - check ignoring? */
262 {
263 ignoring = 1;
264 igstart = p;
265 continue;
266 }
267 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
268 {
269 ignoring = 0;
270 if (p != (igstart + 1))
271 last = r - ret - 1;
272 continue;
273 }
274 else
275 {
276 #if defined (HANDLE_MULTIBYTE)
277 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
278 {
279 pind = p - pmt;
280 ind = _rl_find_next_mbchar (pmt, pind, 1, MB_FIND_NONZERO);
281 l = ind - pind;
282 while (l--)
283 *r++ = *p++;
284 if (!ignoring)
285 {
286 rl += ind - pind;
287 physchars += _rl_col_width (pmt, pind, ind);
288 }
289 else
290 ninvis += ind - pind;
291 p--; /* compensate for later increment */
292 }
293 else
294 #endif
295 {
296 *r++ = *p;
297 if (!ignoring)
298 {
299 rl++; /* visible length byte counter */
300 physchars++;
301 }
302 else
303 ninvis++; /* invisible chars byte counter */
304 }
305
306 if (invflset == 0 && rl >= _rl_screenwidth)
307 {
308 invfl = ninvis;
309 invflset = 1;
310 }
311 }
312 }
313
314 if (rl < _rl_screenwidth)
315 invfl = ninvis;
316
317 *r = '\0';
318 if (lp)
319 *lp = rl;
320 if (lip)
321 *lip = last;
322 if (niflp)
323 *niflp = invfl;
324 if (vlp)
325 *vlp = physchars;
326 return ret;
327 }
328
329 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
330 PMT and return the rest of PMT. */
331 char *
_rl_strip_prompt(pmt)332 _rl_strip_prompt (pmt)
333 char *pmt;
334 {
335 char *ret;
336
337 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
338 return ret;
339 }
340
341 /*
342 * Expand the prompt string into the various display components, if
343 * necessary.
344 *
345 * local_prompt = expanded last line of string in rl_display_prompt
346 * (portion after the final newline)
347 * local_prompt_prefix = portion before last newline of rl_display_prompt,
348 * expanded via expand_prompt
349 * prompt_visible_length = number of visible characters in local_prompt
350 * prompt_prefix_length = number of visible characters in local_prompt_prefix
351 *
352 * This function is called once per call to readline(). It may also be
353 * called arbitrarily to expand the primary prompt.
354 *
355 * The return value is the number of visible characters on the last line
356 * of the (possibly multi-line) prompt.
357 */
358 int
rl_expand_prompt(prompt)359 rl_expand_prompt (prompt)
360 char *prompt;
361 {
362 char *p, *t;
363 int c;
364
365 /* Clear out any saved values. */
366 FREE (local_prompt);
367 FREE (local_prompt_prefix);
368
369 local_prompt = local_prompt_prefix = (char *)0;
370 local_prompt_len = 0;
371 prompt_last_invisible = prompt_invis_chars_first_line = 0;
372 prompt_visible_length = prompt_physical_chars = 0;
373
374 if (prompt == 0 || *prompt == 0)
375 return (0);
376
377 p = strrchr (prompt, '\n');
378 if (!p)
379 {
380 /* The prompt is only one logical line, though it might wrap. */
381 local_prompt = expand_prompt (prompt, &prompt_visible_length,
382 &prompt_last_invisible,
383 &prompt_invis_chars_first_line,
384 &prompt_physical_chars);
385 local_prompt_prefix = (char *)0;
386 local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
387 return (prompt_visible_length);
388 }
389 else
390 {
391 /* The prompt spans multiple lines. */
392 t = ++p;
393 local_prompt = expand_prompt (p, &prompt_visible_length,
394 &prompt_last_invisible,
395 &prompt_invis_chars_first_line,
396 &prompt_physical_chars);
397 c = *t; *t = '\0';
398 /* The portion of the prompt string up to and including the
399 final newline is now null-terminated. */
400 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
401 (int *)NULL,
402 (int *)NULL,
403 (int *)NULL);
404 *t = c;
405 local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
406 return (prompt_prefix_length);
407 }
408 }
409
410 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
411 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
412 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
413 increased. If the lines have already been allocated, this ensures that
414 they can hold at least MINSIZE characters. */
415 static void
init_line_structures(minsize)416 init_line_structures (minsize)
417 int minsize;
418 {
419 register int n;
420
421 if (invisible_line == 0) /* initialize it */
422 {
423 if (line_size < minsize)
424 line_size = minsize;
425 visible_line = (char *)xmalloc (line_size);
426 invisible_line = (char *)xmalloc (line_size);
427 }
428 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
429 {
430 line_size *= 2;
431 if (line_size < minsize)
432 line_size = minsize;
433 visible_line = (char *)xrealloc (visible_line, line_size);
434 invisible_line = (char *)xrealloc (invisible_line, line_size);
435 }
436
437 for (n = minsize; n < line_size; n++)
438 {
439 visible_line[n] = 0;
440 invisible_line[n] = 1;
441 }
442
443 if (vis_lbreaks == 0)
444 {
445 /* should be enough. */
446 inv_lbsize = vis_lbsize = 256;
447 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
448 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
449 #if defined (HANDLE_MULTIBYTE)
450 _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
451 #endif
452 inv_lbreaks[0] = vis_lbreaks[0] = 0;
453 }
454 }
455
456 /* Basic redisplay algorithm. */
457 void
rl_redisplay()458 rl_redisplay ()
459 {
460 register int in, out, c, linenum, cursor_linenum;
461 register char *line;
462 int inv_botlin, lb_botlin, lb_linenum, o_cpos;
463 int newlines, lpos, temp, modmark, n0, num;
464 char *prompt_this_line;
465 #if defined (HANDLE_MULTIBYTE)
466 wchar_t wc;
467 size_t wc_bytes;
468 int wc_width;
469 mbstate_t ps;
470 int _rl_wrapped_multicolumn = 0;
471 #endif
472
473 if (!readline_echoing_p)
474 return;
475
476 if (!rl_display_prompt)
477 rl_display_prompt = "";
478
479 if (invisible_line == 0 || vis_lbreaks == 0)
480 {
481 init_line_structures (0);
482 rl_on_new_line ();
483 }
484
485 /* Draw the line into the buffer. */
486 cpos_buffer_position = -1;
487
488 line = invisible_line;
489 out = inv_botlin = 0;
490
491 /* Mark the line as modified or not. We only do this for history
492 lines. */
493 modmark = 0;
494 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
495 {
496 line[out++] = '*';
497 line[out] = '\0';
498 modmark = 1;
499 }
500
501 /* If someone thought that the redisplay was handled, but the currently
502 visible line has a different modification state than the one about
503 to become visible, then correct the caller's misconception. */
504 if (visible_line[0] != invisible_line[0])
505 rl_display_fixed = 0;
506
507 /* If the prompt to be displayed is the `primary' readline prompt (the
508 one passed to readline()), use the values we have already expanded.
509 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
510 number of non-visible characters in the prompt string. */
511 if (rl_display_prompt == rl_prompt || local_prompt)
512 {
513 if (local_prompt_prefix && forced_display)
514 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
515
516 if (local_prompt_len > 0)
517 {
518 temp = local_prompt_len + out + 2;
519 if (temp >= line_size)
520 {
521 line_size = (temp + 1024) - (temp % 1024);
522 visible_line = (char *)xrealloc (visible_line, line_size);
523 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
524 }
525 strncpy (line + out, local_prompt, local_prompt_len);
526 out += local_prompt_len;
527 }
528 line[out] = '\0';
529 wrap_offset = local_prompt_len - prompt_visible_length;
530 }
531 else
532 {
533 int pmtlen;
534 prompt_this_line = strrchr (rl_display_prompt, '\n');
535 if (!prompt_this_line)
536 prompt_this_line = rl_display_prompt;
537 else
538 {
539 prompt_this_line++;
540 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
541 if (forced_display)
542 {
543 _rl_output_some_chars (rl_display_prompt, pmtlen);
544 /* Make sure we are at column zero even after a newline,
545 regardless of the state of terminal output processing. */
546 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
547 cr ();
548 }
549 }
550
551 prompt_physical_chars = pmtlen = strlen (prompt_this_line);
552 temp = pmtlen + out + 2;
553 if (temp >= line_size)
554 {
555 line_size = (temp + 1024) - (temp % 1024);
556 visible_line = (char *)xrealloc (visible_line, line_size);
557 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
558 }
559 strncpy (line + out, prompt_this_line, pmtlen);
560 out += pmtlen;
561 line[out] = '\0';
562 wrap_offset = prompt_invis_chars_first_line = 0;
563 }
564
565 #if defined (HANDLE_MULTIBYTE)
566 #define CHECK_INV_LBREAKS() \
567 do { \
568 if (newlines >= (inv_lbsize - 2)) \
569 { \
570 inv_lbsize *= 2; \
571 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
572 _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
573 } \
574 } while (0)
575 #else
576 #define CHECK_INV_LBREAKS() \
577 do { \
578 if (newlines >= (inv_lbsize - 2)) \
579 { \
580 inv_lbsize *= 2; \
581 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
582 } \
583 } while (0)
584 #endif /* HANDLE_MULTIBYTE */
585
586 #if defined (HANDLE_MULTIBYTE)
587 #define CHECK_LPOS() \
588 do { \
589 lpos++; \
590 if (lpos >= _rl_screenwidth) \
591 { \
592 if (newlines >= (inv_lbsize - 2)) \
593 { \
594 inv_lbsize *= 2; \
595 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
596 _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
597 } \
598 inv_lbreaks[++newlines] = out; \
599 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
600 lpos = 0; \
601 } \
602 } while (0)
603 #else
604 #define CHECK_LPOS() \
605 do { \
606 lpos++; \
607 if (lpos >= _rl_screenwidth) \
608 { \
609 if (newlines >= (inv_lbsize - 2)) \
610 { \
611 inv_lbsize *= 2; \
612 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
613 } \
614 inv_lbreaks[++newlines] = out; \
615 lpos = 0; \
616 } \
617 } while (0)
618 #endif
619
620 /* inv_lbreaks[i] is where line i starts in the buffer. */
621 inv_lbreaks[newlines = 0] = 0;
622 #if 0
623 lpos = out - wrap_offset;
624 #else
625 lpos = prompt_physical_chars + modmark;
626 #endif
627
628 #if defined (HANDLE_MULTIBYTE)
629 memset (_rl_wrapped_line, 0, vis_lbsize);
630 num = 0;
631 #endif
632
633 /* prompt_invis_chars_first_line is the number of invisible characters in
634 the first physical line of the prompt.
635 wrap_offset - prompt_invis_chars_first_line is the number of invis
636 chars on the second line. */
637
638 /* what if lpos is already >= _rl_screenwidth before we start drawing the
639 contents of the command line? */
640 while (lpos >= _rl_screenwidth)
641 {
642 int z;
643 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
644 invisible characters that is longer than the screen width. The
645 prompt_invis_chars_first_line variable could be made into an array
646 saying how many invisible characters there are per line, but that's
647 probably too much work for the benefit gained. How many people have
648 prompts that exceed two physical lines?
649 Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
650 #if defined (HANDLE_MULTIBYTE)
651 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
652 {
653 n0 = num;
654 temp = local_prompt_len;
655 while (num < temp)
656 {
657 z = _rl_col_width (local_prompt, n0, num);
658 if (z > _rl_screenwidth)
659 {
660 num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
661 break;
662 }
663 else if (z == _rl_screenwidth)
664 break;
665 num++;
666 }
667 temp = num;
668 }
669 else
670 #endif /* !HANDLE_MULTIBYTE */
671 temp = ((newlines + 1) * _rl_screenwidth);
672
673 /* Now account for invisible characters in the current line. */
674 temp += ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line
675 : ((newlines == 1) ? wrap_offset : 0))
676 : ((newlines == 0) ? wrap_offset :0));
677
678 inv_lbreaks[++newlines] = temp;
679 #if defined (HANDLE_MULTIBYTE)
680 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
681 lpos -= _rl_col_width (local_prompt, n0, num);
682 else
683 #endif
684 lpos -= _rl_screenwidth;
685 }
686
687 prompt_last_screen_line = newlines;
688
689 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
690 track of where the cursor is (cpos_buffer_position), the number of the line containing
691 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
692 It maintains an array of line breaks for display (inv_lbreaks).
693 This handles expanding tabs for display and displaying meta characters. */
694 lb_linenum = 0;
695 #if defined (HANDLE_MULTIBYTE)
696 in = 0;
697 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
698 {
699 memset (&ps, 0, sizeof (mbstate_t));
700 wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
701 }
702 else
703 wc_bytes = 1;
704 while (in < rl_end)
705 #else
706 for (in = 0; in < rl_end; in++)
707 #endif
708 {
709 c = (unsigned char)rl_line_buffer[in];
710
711 #if defined (HANDLE_MULTIBYTE)
712 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
713 {
714 if (MB_INVALIDCH (wc_bytes))
715 {
716 /* Byte sequence is invalid or shortened. Assume that the
717 first byte represents a character. */
718 wc_bytes = 1;
719 /* Assume that a character occupies a single column. */
720 wc_width = 1;
721 memset (&ps, 0, sizeof (mbstate_t));
722 }
723 else if (MB_NULLWCH (wc_bytes))
724 break; /* Found '\0' */
725 else
726 {
727 temp = wcwidth (wc);
728 wc_width = (temp >= 0) ? temp : 1;
729 }
730 }
731 #endif
732
733 if (out + 8 >= line_size) /* XXX - 8 for \t */
734 {
735 line_size *= 2;
736 visible_line = (char *)xrealloc (visible_line, line_size);
737 invisible_line = (char *)xrealloc (invisible_line, line_size);
738 line = invisible_line;
739 }
740
741 if (in == rl_point)
742 {
743 cpos_buffer_position = out;
744 lb_linenum = newlines;
745 }
746
747 #if defined (HANDLE_MULTIBYTE)
748 if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
749 #else
750 if (META_CHAR (c))
751 #endif
752 {
753 if (_rl_output_meta_chars == 0)
754 {
755 sprintf (line + out, "\\%o", c);
756
757 if (lpos + 4 >= _rl_screenwidth)
758 {
759 temp = _rl_screenwidth - lpos;
760 CHECK_INV_LBREAKS ();
761 inv_lbreaks[++newlines] = out + temp;
762 lpos = 4 - temp;
763 }
764 else
765 lpos += 4;
766
767 out += 4;
768 }
769 else
770 {
771 line[out++] = c;
772 CHECK_LPOS();
773 }
774 }
775 #if defined (DISPLAY_TABS)
776 else if (c == '\t')
777 {
778 register int newout;
779
780 #if 0
781 newout = (out | (int)7) + 1;
782 #else
783 newout = out + 8 - lpos % 8;
784 #endif
785 temp = newout - out;
786 if (lpos + temp >= _rl_screenwidth)
787 {
788 register int temp2;
789 temp2 = _rl_screenwidth - lpos;
790 CHECK_INV_LBREAKS ();
791 inv_lbreaks[++newlines] = out + temp2;
792 lpos = temp - temp2;
793 while (out < newout)
794 line[out++] = ' ';
795 }
796 else
797 {
798 while (out < newout)
799 line[out++] = ' ';
800 lpos += temp;
801 }
802 }
803 #endif
804 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
805 {
806 line[out++] = '\0'; /* XXX - sentinel */
807 CHECK_INV_LBREAKS ();
808 inv_lbreaks[++newlines] = out;
809 lpos = 0;
810 }
811 else if (CTRL_CHAR (c) || c == RUBOUT)
812 {
813 line[out++] = '^';
814 CHECK_LPOS();
815 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
816 CHECK_LPOS();
817 }
818 else
819 {
820 #if defined (HANDLE_MULTIBYTE)
821 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
822 {
823 register int i;
824
825 _rl_wrapped_multicolumn = 0;
826
827 if (_rl_screenwidth < lpos + wc_width)
828 for (i = lpos; i < _rl_screenwidth; i++)
829 {
830 /* The space will be removed in update_line() */
831 line[out++] = ' ';
832 _rl_wrapped_multicolumn++;
833 CHECK_LPOS();
834 }
835 if (in == rl_point)
836 {
837 cpos_buffer_position = out;
838 lb_linenum = newlines;
839 }
840 for (i = in; i < in+wc_bytes; i++)
841 line[out++] = rl_line_buffer[i];
842 for (i = 0; i < wc_width; i++)
843 CHECK_LPOS();
844 }
845 else
846 {
847 line[out++] = c;
848 CHECK_LPOS();
849 }
850 #else
851 line[out++] = c;
852 CHECK_LPOS();
853 #endif
854 }
855
856 #if defined (HANDLE_MULTIBYTE)
857 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
858 {
859 in += wc_bytes;
860 wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
861 }
862 else
863 in++;
864 #endif
865
866 }
867 line[out] = '\0';
868 if (cpos_buffer_position < 0)
869 {
870 cpos_buffer_position = out;
871 lb_linenum = newlines;
872 }
873
874 inv_botlin = lb_botlin = newlines;
875 CHECK_INV_LBREAKS ();
876 inv_lbreaks[newlines+1] = out;
877 cursor_linenum = lb_linenum;
878
879 /* CPOS_BUFFER_POSITION == position in buffer where cursor should be placed.
880 CURSOR_LINENUM == line number where the cursor should be placed. */
881
882 /* PWP: now is when things get a bit hairy. The visible and invisible
883 line buffers are really multiple lines, which would wrap every
884 (screenwidth - 1) characters. Go through each in turn, finding
885 the changed region and updating it. The line order is top to bottom. */
886
887 /* If we can move the cursor up and down, then use multiple lines,
888 otherwise, let long lines display in a single terminal line, and
889 horizontally scroll it. */
890
891 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
892 {
893 int nleft, pos, changed_screen_line, tx;
894
895 if (!rl_display_fixed || forced_display)
896 {
897 forced_display = 0;
898
899 /* If we have more than a screenful of material to display, then
900 only display a screenful. We should display the last screen,
901 not the first. */
902 if (out >= _rl_screenchars)
903 {
904 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
905 out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
906 else
907 out = _rl_screenchars - 1;
908 }
909
910 /* The first line is at character position 0 in the buffer. The
911 second and subsequent lines start at inv_lbreaks[N], offset by
912 OFFSET (which has already been calculated above). */
913
914 #define INVIS_FIRST() (prompt_physical_chars > _rl_screenwidth ? prompt_invis_chars_first_line : wrap_offset)
915 #define WRAP_OFFSET(line, offset) ((line == 0) \
916 ? (offset ? INVIS_FIRST() : 0) \
917 : ((line == prompt_last_screen_line) ? wrap_offset-prompt_invis_chars_first_line : 0))
918 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
919 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
920 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
921 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
922 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
923 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
924
925 /* For each line in the buffer, do the updating display. */
926 for (linenum = 0; linenum <= inv_botlin; linenum++)
927 {
928 /* This can lead us astray if we execute a program that changes
929 the locale from a non-multibyte to a multibyte one. */
930 o_cpos = _rl_last_c_pos;
931 cpos_adjusted = 0;
932 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
933 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
934
935 /* update_line potentially changes _rl_last_c_pos, but doesn't
936 take invisible characters into account, since _rl_last_c_pos
937 is an absolute cursor position in a multibyte locale. See
938 if compensating here is the right thing, or if we have to
939 change update_line itself. There is one case in which
940 update_line adjusts _rl_last_c_pos itself (so it can pass
941 _rl_move_cursor_relative accurate values); it communicates
942 this back by setting cpos_adjusted. If we assume that
943 _rl_last_c_pos is correct (an absolute cursor position) each
944 time update_line is called, then we can assume in our
945 calculations that o_cpos does not need to be adjusted by
946 wrap_offset. */
947 if (linenum == 0 && (MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
948 cpos_adjusted == 0 &&
949 _rl_last_c_pos != o_cpos &&
950 _rl_last_c_pos > wrap_offset &&
951 o_cpos < prompt_last_invisible)
952 _rl_last_c_pos -= prompt_invis_chars_first_line; /* XXX - was wrap_offset */
953 else if (linenum == prompt_last_screen_line && prompt_physical_chars > _rl_screenwidth &&
954 (MB_CUR_MAX > 1 && rl_byte_oriented == 0) &&
955 cpos_adjusted == 0 &&
956 _rl_last_c_pos != o_cpos &&
957 _rl_last_c_pos > (prompt_last_invisible - _rl_screenwidth - prompt_invis_chars_first_line))
958 _rl_last_c_pos -= (wrap_offset-prompt_invis_chars_first_line);
959
960 /* If this is the line with the prompt, we might need to
961 compensate for invisible characters in the new line. Do
962 this only if there is not more than one new line (which
963 implies that we completely overwrite the old visible line)
964 and the new line is shorter than the old. Make sure we are
965 at the end of the new line before clearing. */
966 if (linenum == 0 &&
967 inv_botlin == 0 && _rl_last_c_pos == out &&
968 (wrap_offset > visible_wrap_offset) &&
969 (_rl_last_c_pos < visible_first_line_len))
970 {
971 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
972 nleft = _rl_screenwidth - _rl_last_c_pos;
973 else
974 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
975 if (nleft)
976 _rl_clear_to_eol (nleft);
977 }
978
979 /* Since the new first line is now visible, save its length. */
980 if (linenum == 0)
981 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
982 }
983
984 /* We may have deleted some lines. If so, clear the left over
985 blank ones at the bottom out. */
986 if (_rl_vis_botlin > inv_botlin)
987 {
988 char *tt;
989 for (; linenum <= _rl_vis_botlin; linenum++)
990 {
991 tt = VIS_CHARS (linenum);
992 _rl_move_vert (linenum);
993 _rl_move_cursor_relative (0, tt);
994 _rl_clear_to_eol
995 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
996 }
997 }
998 _rl_vis_botlin = inv_botlin;
999
1000 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
1001 different screen line during this redisplay. */
1002 changed_screen_line = _rl_last_v_pos != cursor_linenum;
1003 if (changed_screen_line)
1004 {
1005 _rl_move_vert (cursor_linenum);
1006 /* If we moved up to the line with the prompt using _rl_term_up,
1007 the physical cursor position on the screen stays the same,
1008 but the buffer position needs to be adjusted to account
1009 for invisible characters. */
1010 if ((MB_CUR_MAX == 1 || rl_byte_oriented) && cursor_linenum == 0 && wrap_offset)
1011 _rl_last_c_pos += wrap_offset;
1012 }
1013
1014 /* We have to reprint the prompt if it contains invisible
1015 characters, since it's not generally OK to just reprint
1016 the characters from the current cursor position. But we
1017 only need to reprint it if the cursor is before the last
1018 invisible character in the prompt string. */
1019 nleft = prompt_visible_length + wrap_offset;
1020 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
1021 #if 0
1022 _rl_last_c_pos <= PROMPT_ENDING_INDEX && local_prompt)
1023 #else
1024 _rl_last_c_pos < PROMPT_ENDING_INDEX && local_prompt)
1025 #endif
1026 {
1027 #if defined (__MSDOS__)
1028 putc ('\r', rl_outstream);
1029 #else
1030 if (_rl_term_cr)
1031 tputs (_rl_term_cr, 1, _rl_output_character_function);
1032 #endif
1033 _rl_output_some_chars (local_prompt, nleft);
1034 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1035 _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft) - wrap_offset;
1036 else
1037 _rl_last_c_pos = nleft;
1038 }
1039
1040 /* Where on that line? And where does that line start
1041 in the buffer? */
1042 pos = inv_lbreaks[cursor_linenum];
1043 /* nleft == number of characters in the line buffer between the
1044 start of the line and the desired cursor position. */
1045 nleft = cpos_buffer_position - pos;
1046
1047 /* NLEFT is now a number of characters in a buffer. When in a
1048 multibyte locale, however, _rl_last_c_pos is an absolute cursor
1049 position that doesn't take invisible characters in the prompt
1050 into account. We use a fudge factor to compensate. */
1051
1052 /* Since _rl_backspace() doesn't know about invisible characters in the
1053 prompt, and there's no good way to tell it, we compensate for
1054 those characters here and call _rl_backspace() directly. */
1055 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
1056 {
1057 /* TX == new physical cursor position in multibyte locale. */
1058 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1059 tx = _rl_col_width (&visible_line[pos], 0, nleft) - visible_wrap_offset;
1060 else
1061 tx = nleft;
1062 if (tx >= 0 && _rl_last_c_pos > tx)
1063 {
1064 _rl_backspace (_rl_last_c_pos - tx); /* XXX */
1065 _rl_last_c_pos = tx;
1066 }
1067 }
1068
1069 /* We need to note that in a multibyte locale we are dealing with
1070 _rl_last_c_pos as an absolute cursor position, but moving to a
1071 point specified by a buffer position (NLEFT) that doesn't take
1072 invisible characters into account. */
1073 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1074 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
1075 else if (nleft != _rl_last_c_pos)
1076 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
1077 }
1078 }
1079 else /* Do horizontal scrolling. */
1080 {
1081 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
1082 int lmargin, ndisp, nleft, phys_c_pos, t;
1083
1084 /* Always at top line. */
1085 _rl_last_v_pos = 0;
1086
1087 /* Compute where in the buffer the displayed line should start. This
1088 will be LMARGIN. */
1089
1090 /* The number of characters that will be displayed before the cursor. */
1091 ndisp = cpos_buffer_position - wrap_offset;
1092 nleft = prompt_visible_length + wrap_offset;
1093 /* Where the new cursor position will be on the screen. This can be
1094 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
1095 phys_c_pos = cpos_buffer_position - (last_lmargin ? last_lmargin : wrap_offset);
1096 t = _rl_screenwidth / 3;
1097
1098 /* If the number of characters had already exceeded the screenwidth,
1099 last_lmargin will be > 0. */
1100
1101 /* If the number of characters to be displayed is more than the screen
1102 width, compute the starting offset so that the cursor is about
1103 two-thirds of the way across the screen. */
1104 if (phys_c_pos > _rl_screenwidth - 2)
1105 {
1106 lmargin = cpos_buffer_position - (2 * t);
1107 if (lmargin < 0)
1108 lmargin = 0;
1109 /* If the left margin would be in the middle of a prompt with
1110 invisible characters, don't display the prompt at all. */
1111 if (wrap_offset && lmargin > 0 && lmargin < nleft)
1112 lmargin = nleft;
1113 }
1114 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
1115 lmargin = 0;
1116 else if (phys_c_pos < 1)
1117 {
1118 /* If we are moving back towards the beginning of the line and
1119 the last margin is no longer correct, compute a new one. */
1120 lmargin = ((cpos_buffer_position - 1) / t) * t; /* XXX */
1121 if (wrap_offset && lmargin > 0 && lmargin < nleft)
1122 lmargin = nleft;
1123 }
1124 else
1125 lmargin = last_lmargin;
1126
1127 /* If the first character on the screen isn't the first character
1128 in the display line, indicate this with a special character. */
1129 if (lmargin > 0)
1130 line[lmargin] = '<';
1131
1132 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
1133 the whole line, indicate that with a special character at the
1134 right edge of the screen. If LMARGIN is 0, we need to take the
1135 wrap offset into account. */
1136 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
1137 if (t < out)
1138 line[t - 1] = '>';
1139
1140 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1141 {
1142 forced_display = 0;
1143 update_line (&visible_line[last_lmargin],
1144 &invisible_line[lmargin],
1145 0,
1146 _rl_screenwidth + visible_wrap_offset,
1147 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
1148 0);
1149
1150 /* If the visible new line is shorter than the old, but the number
1151 of invisible characters is greater, and we are at the end of
1152 the new line, we need to clear to eol. */
1153 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
1154 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
1155 (_rl_last_c_pos == out) &&
1156 t < visible_first_line_len)
1157 {
1158 nleft = _rl_screenwidth - t;
1159 _rl_clear_to_eol (nleft);
1160 }
1161 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
1162 if (visible_first_line_len > _rl_screenwidth)
1163 visible_first_line_len = _rl_screenwidth;
1164
1165 _rl_move_cursor_relative (cpos_buffer_position - lmargin, &invisible_line[lmargin]);
1166 last_lmargin = lmargin;
1167 }
1168 }
1169 fflush (rl_outstream);
1170
1171 /* Swap visible and non-visible lines. */
1172 {
1173 char *vtemp = visible_line;
1174 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
1175
1176 visible_line = invisible_line;
1177 invisible_line = vtemp;
1178
1179 vis_lbreaks = inv_lbreaks;
1180 inv_lbreaks = itemp;
1181
1182 vis_lbsize = inv_lbsize;
1183 inv_lbsize = ntemp;
1184
1185 rl_display_fixed = 0;
1186 /* If we are displaying on a single line, and last_lmargin is > 0, we
1187 are not displaying any invisible characters, so set visible_wrap_offset
1188 to 0. */
1189 if (_rl_horizontal_scroll_mode && last_lmargin)
1190 visible_wrap_offset = 0;
1191 else
1192 visible_wrap_offset = wrap_offset;
1193 }
1194 }
1195
1196 /* PWP: update_line() is based on finding the middle difference of each
1197 line on the screen; vis:
1198
1199 /old first difference
1200 /beginning of line | /old last same /old EOL
1201 v v v v
1202 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1203 new: eddie> Oh, my little buggy says to me, as lurgid as
1204 ^ ^ ^ ^
1205 \beginning of line | \new last same \new end of line
1206 \new first difference
1207
1208 All are character pointers for the sake of speed. Special cases for
1209 no differences, as well as for end of line additions must be handled.
1210
1211 Could be made even smarter, but this works well enough */
1212 static void
update_line(old,new,current_line,omax,nmax,inv_botlin)1213 update_line (old, new, current_line, omax, nmax, inv_botlin)
1214 register char *old, *new;
1215 int current_line, omax, nmax, inv_botlin;
1216 {
1217 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1218 int temp, lendiff, wsatend, od, nd, twidth, o_cpos;
1219 int current_invis_chars;
1220 int col_lendiff, col_temp;
1221 #if defined (HANDLE_MULTIBYTE)
1222 mbstate_t ps_new, ps_old;
1223 int new_offset, old_offset;
1224 #endif
1225
1226 /* If we're at the right edge of a terminal that supports xn, we're
1227 ready to wrap around, so do so. This fixes problems with knowing
1228 the exact cursor position and cut-and-paste with certain terminal
1229 emulators. In this calculation, TEMP is the physical screen
1230 position of the cursor. */
1231 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1232 temp = _rl_last_c_pos;
1233 else
1234 temp = _rl_last_c_pos - WRAP_OFFSET (_rl_last_v_pos, visible_wrap_offset);
1235 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
1236 && _rl_last_v_pos == current_line - 1)
1237 {
1238 #if defined (HANDLE_MULTIBYTE)
1239 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1240 {
1241 wchar_t wc;
1242 mbstate_t ps;
1243 int tempwidth, bytes;
1244 size_t ret;
1245
1246 /* This fixes only double-column characters, but if the wrapped
1247 character comsumes more than three columns, spaces will be
1248 inserted in the string buffer. */
1249 if (_rl_wrapped_line[current_line] > 0)
1250 _rl_clear_to_eol (_rl_wrapped_line[current_line]);
1251
1252 memset (&ps, 0, sizeof (mbstate_t));
1253 ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
1254 if (MB_INVALIDCH (ret))
1255 {
1256 tempwidth = 1;
1257 ret = 1;
1258 }
1259 else if (MB_NULLWCH (ret))
1260 tempwidth = 0;
1261 else
1262 tempwidth = wcwidth (wc);
1263
1264 if (tempwidth > 0)
1265 {
1266 int count;
1267 bytes = ret;
1268 for (count = 0; count < bytes; count++)
1269 putc (new[count], rl_outstream);
1270 _rl_last_c_pos = tempwidth;
1271 _rl_last_v_pos++;
1272 memset (&ps, 0, sizeof (mbstate_t));
1273 ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1274 if (ret != 0 && bytes != 0)
1275 {
1276 if (MB_INVALIDCH (ret))
1277 memmove (old+bytes, old+1, strlen (old+1));
1278 else
1279 memmove (old+bytes, old+ret, strlen (old+ret));
1280 memcpy (old, new, bytes);
1281 }
1282 }
1283 else
1284 {
1285 putc (' ', rl_outstream);
1286 _rl_last_c_pos = 1;
1287 _rl_last_v_pos++;
1288 if (old[0] && new[0])
1289 old[0] = new[0];
1290 }
1291 }
1292 else
1293 #endif
1294 {
1295 if (new[0])
1296 putc (new[0], rl_outstream);
1297 else
1298 putc (' ', rl_outstream);
1299 _rl_last_c_pos = 1;
1300 _rl_last_v_pos++;
1301 if (old[0] && new[0])
1302 old[0] = new[0];
1303 }
1304 }
1305
1306
1307 /* Find first difference. */
1308 #if defined (HANDLE_MULTIBYTE)
1309 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1310 {
1311 /* See if the old line is a subset of the new line, so that the
1312 only change is adding characters. */
1313 temp = (omax < nmax) ? omax : nmax;
1314 if (memcmp (old, new, temp) == 0)
1315 {
1316 ofd = old + temp;
1317 nfd = new + temp;
1318 }
1319 else
1320 {
1321 memset (&ps_new, 0, sizeof(mbstate_t));
1322 memset (&ps_old, 0, sizeof(mbstate_t));
1323
1324 if (omax == nmax && STREQN (new, old, omax))
1325 {
1326 ofd = old + omax;
1327 nfd = new + nmax;
1328 }
1329 else
1330 {
1331 new_offset = old_offset = 0;
1332 for (ofd = old, nfd = new;
1333 (ofd - old < omax) && *ofd &&
1334 _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
1335 {
1336 old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
1337 new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
1338 ofd = old + old_offset;
1339 nfd = new + new_offset;
1340 }
1341 }
1342 }
1343 }
1344 else
1345 #endif
1346 for (ofd = old, nfd = new;
1347 (ofd - old < omax) && *ofd && (*ofd == *nfd);
1348 ofd++, nfd++)
1349 ;
1350
1351 /* Move to the end of the screen line. ND and OD are used to keep track
1352 of the distance between ne and new and oe and old, respectively, to
1353 move a subtraction out of each loop. */
1354 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
1355 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
1356
1357 /* If no difference, continue to next line. */
1358 if (ofd == oe && nfd == ne)
1359 return;
1360
1361 wsatend = 1; /* flag for trailing whitespace */
1362
1363 #if defined (HANDLE_MULTIBYTE)
1364 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1365 {
1366 ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
1367 nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
1368 while ((ols > ofd) && (nls > nfd))
1369 {
1370 memset (&ps_old, 0, sizeof (mbstate_t));
1371 memset (&ps_new, 0, sizeof (mbstate_t));
1372
1373 #if 0
1374 /* On advice from jir@yamato.ibm.com */
1375 _rl_adjust_point (old, ols - old, &ps_old);
1376 _rl_adjust_point (new, nls - new, &ps_new);
1377 #endif
1378
1379 if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
1380 break;
1381
1382 if (*ols == ' ')
1383 wsatend = 0;
1384
1385 ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
1386 nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
1387 }
1388 }
1389 else
1390 {
1391 #endif /* HANDLE_MULTIBYTE */
1392 ols = oe - 1; /* find last same */
1393 nls = ne - 1;
1394 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
1395 {
1396 if (*ols != ' ')
1397 wsatend = 0;
1398 ols--;
1399 nls--;
1400 }
1401 #if defined (HANDLE_MULTIBYTE)
1402 }
1403 #endif
1404
1405 if (wsatend)
1406 {
1407 ols = oe;
1408 nls = ne;
1409 }
1410 #if defined (HANDLE_MULTIBYTE)
1411 /* This may not work for stateful encoding, but who cares? To handle
1412 stateful encoding properly, we have to scan each string from the
1413 beginning and compare. */
1414 else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
1415 #else
1416 else if (*ols != *nls)
1417 #endif
1418 {
1419 if (*ols) /* don't step past the NUL */
1420 {
1421 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1422 ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1423 else
1424 ols++;
1425 }
1426 if (*nls)
1427 {
1428 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1429 nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
1430 else
1431 nls++;
1432 }
1433 }
1434
1435 /* count of invisible characters in the current invisible line. */
1436 current_invis_chars = W_OFFSET (current_line, wrap_offset);
1437 if (_rl_last_v_pos != current_line)
1438 {
1439 _rl_move_vert (current_line);
1440 if ((MB_CUR_MAX == 1 || rl_byte_oriented) && current_line == 0 && visible_wrap_offset)
1441 _rl_last_c_pos += visible_wrap_offset;
1442 }
1443
1444 /* If this is the first line and there are invisible characters in the
1445 prompt string, and the prompt string has not changed, and the current
1446 cursor position is before the last invisible character in the prompt,
1447 and the index of the character to move to is past the end of the prompt
1448 string, then redraw the entire prompt string. We can only do this
1449 reliably if the terminal supports a `cr' capability.
1450
1451 This is not an efficiency hack -- there is a problem with redrawing
1452 portions of the prompt string if they contain terminal escape
1453 sequences (like drawing the `unbold' sequence without a corresponding
1454 `bold') that manifests itself on certain terminals. */
1455
1456 lendiff = local_prompt_len;
1457 od = ofd - old; /* index of first difference in visible line */
1458 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1459 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
1460 od >= lendiff && _rl_last_c_pos < PROMPT_ENDING_INDEX)
1461 {
1462 #if defined (__MSDOS__)
1463 putc ('\r', rl_outstream);
1464 #else
1465 tputs (_rl_term_cr, 1, _rl_output_character_function);
1466 #endif
1467 _rl_output_some_chars (local_prompt, lendiff);
1468 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1469 {
1470 /* We take wrap_offset into account here so we can pass correct
1471 information to _rl_move_cursor_relative. */
1472 _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff) - wrap_offset;
1473 cpos_adjusted = 1;
1474 }
1475 else
1476 _rl_last_c_pos = lendiff;
1477 }
1478
1479 o_cpos = _rl_last_c_pos;
1480
1481 /* When this function returns, _rl_last_c_pos is correct, and an absolute
1482 cursor postion in multibyte mode, but a buffer index when not in a
1483 multibyte locale. */
1484 _rl_move_cursor_relative (od, old);
1485 #if 1
1486 #if defined (HANDLE_MULTIBYTE)
1487 /* We need to indicate that the cursor position is correct in the presence of
1488 invisible characters in the prompt string. Let's see if setting this when
1489 we make sure we're at the end of the drawn prompt string works. */
1490 if (current_line == 0 && MB_CUR_MAX > 1 && rl_byte_oriented == 0 &&
1491 (_rl_last_c_pos > 0 || o_cpos > 0) &&
1492 _rl_last_c_pos == prompt_physical_chars)
1493 cpos_adjusted = 1;
1494 #endif
1495 #endif
1496
1497 /* if (len (new) > len (old))
1498 lendiff == difference in buffer
1499 col_lendiff == difference on screen
1500 When not using multibyte characters, these are equal */
1501 lendiff = (nls - nfd) - (ols - ofd);
1502 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1503 col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
1504 else
1505 col_lendiff = lendiff;
1506
1507 /* If we are changing the number of invisible characters in a line, and
1508 the spot of first difference is before the end of the invisible chars,
1509 lendiff needs to be adjusted. */
1510 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1511 current_invis_chars != visible_wrap_offset)
1512 {
1513 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1514 {
1515 lendiff += visible_wrap_offset - current_invis_chars;
1516 col_lendiff += visible_wrap_offset - current_invis_chars;
1517 }
1518 else
1519 {
1520 lendiff += visible_wrap_offset - current_invis_chars;
1521 col_lendiff = lendiff;
1522 }
1523 }
1524
1525 /* Insert (diff (len (old), len (new)) ch. */
1526 temp = ne - nfd;
1527 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1528 col_temp = _rl_col_width (new, nfd - new, ne - new);
1529 else
1530 col_temp = temp;
1531
1532 if (col_lendiff > 0) /* XXX - was lendiff */
1533 {
1534 /* Non-zero if we're increasing the number of lines. */
1535 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
1536 /* If col_lendiff is > 0, implying that the new string takes up more
1537 screen real estate than the old, but lendiff is < 0, meaning that it
1538 takes fewer bytes, we need to just output the characters starting
1539 from the first difference. These will overwrite what is on the
1540 display, so there's no reason to do a smart update. This can really
1541 only happen in a multibyte environment. */
1542 if (lendiff < 0)
1543 {
1544 _rl_output_some_chars (nfd, temp);
1545 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);
1546 /* If nfd begins before any invisible characters in the prompt,
1547 adjust _rl_last_c_pos to account for wrap_offset and set
1548 cpos_adjusted to let the caller know. */
1549 if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
1550 {
1551 _rl_last_c_pos -= wrap_offset;
1552 cpos_adjusted = 1;
1553 }
1554 return;
1555 }
1556 /* Sometimes it is cheaper to print the characters rather than
1557 use the terminal's capabilities. If we're growing the number
1558 of lines, make sure we actually cause the new line to wrap
1559 around on auto-wrapping terminals. */
1560 else if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
1561 {
1562 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1563 _rl_horizontal_scroll_mode == 1, inserting the characters with
1564 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1565 invisible characters. We need to just draw them. */
1566 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
1567 lendiff <= prompt_visible_length || !current_invis_chars))
1568 {
1569 insert_some_chars (nfd, lendiff, col_lendiff);
1570 _rl_last_c_pos += col_lendiff;
1571 }
1572 else if ((MB_CUR_MAX == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0)
1573 {
1574 /* At the end of a line the characters do not have to
1575 be "inserted". They can just be placed on the screen. */
1576 /* However, this screws up the rest of this block, which
1577 assumes you've done the insert because you can. */
1578 _rl_output_some_chars (nfd, lendiff);
1579 _rl_last_c_pos += col_lendiff;
1580 }
1581 else
1582 {
1583 _rl_output_some_chars (nfd, temp);
1584 _rl_last_c_pos += col_temp;
1585 /* If nfd begins before any invisible characters in the prompt,
1586 adjust _rl_last_c_pos to account for wrap_offset and set
1587 cpos_adjusted to let the caller know. */
1588 if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
1589 {
1590 _rl_last_c_pos -= wrap_offset;
1591 cpos_adjusted = 1;
1592 }
1593 return;
1594 }
1595 /* Copy (new) chars to screen from first diff to last match. */
1596 temp = nls - nfd;
1597 if ((temp - lendiff) > 0)
1598 {
1599 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1600 /* XXX -- this bears closer inspection. Fixes a redisplay bug
1601 reported against bash-3.0-alpha by Andreas Schwab involving
1602 multibyte characters and prompt strings with invisible
1603 characters, but was previously disabled. */
1604 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1605 twidth = _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
1606 else
1607 twidth = temp - lendiff;
1608 _rl_last_c_pos += twidth;
1609 }
1610 }
1611 else
1612 {
1613 /* cannot insert chars, write to EOL */
1614 _rl_output_some_chars (nfd, temp);
1615 _rl_last_c_pos += col_temp;
1616 /* If we're in a multibyte locale and were before the last invisible
1617 char in the current line (which implies we just output some invisible
1618 characters) we need to adjust _rl_last_c_pos, since it represents
1619 a physical character position. */
1620 }
1621 }
1622 else /* Delete characters from line. */
1623 {
1624 /* If possible and inexpensive to use terminal deletion, then do so. */
1625 if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
1626 {
1627 /* If all we're doing is erasing the invisible characters in the
1628 prompt string, don't bother. It screws up the assumptions
1629 about what's on the screen. */
1630 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1631 -lendiff == visible_wrap_offset)
1632 col_lendiff = 0;
1633
1634 if (col_lendiff)
1635 delete_chars (-col_lendiff); /* delete (diff) characters */
1636
1637 /* Copy (new) chars to screen from first diff to last match */
1638 temp = nls - nfd;
1639 if (temp > 0)
1640 {
1641 /* If nfd begins at the prompt, or before the invisible
1642 characters in the prompt, we need to adjust _rl_last_c_pos
1643 in a multibyte locale to account for the wrap offset and
1644 set cpos_adjusted accordingly. */
1645 _rl_output_some_chars (nfd, temp);
1646 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1647 {
1648 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);
1649 if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
1650 {
1651 _rl_last_c_pos -= wrap_offset;
1652 cpos_adjusted = 1;
1653 }
1654 }
1655 else
1656 _rl_last_c_pos += temp;
1657 }
1658 }
1659 /* Otherwise, print over the existing material. */
1660 else
1661 {
1662 if (temp > 0)
1663 {
1664 /* If nfd begins at the prompt, or before the invisible
1665 characters in the prompt, we need to adjust _rl_last_c_pos
1666 in a multibyte locale to account for the wrap offset and
1667 set cpos_adjusted accordingly. */
1668 _rl_output_some_chars (nfd, temp);
1669 _rl_last_c_pos += col_temp; /* XXX */
1670 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1671 {
1672 if (current_line == 0 && wrap_offset && ((nfd - new) <= prompt_last_invisible))
1673 {
1674 _rl_last_c_pos -= wrap_offset;
1675 cpos_adjusted = 1;
1676 }
1677 }
1678 }
1679 lendiff = (oe - old) - (ne - new);
1680 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1681 col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
1682 else
1683 col_lendiff = lendiff;
1684
1685 if (col_lendiff)
1686 {
1687 if (_rl_term_autowrap && current_line < inv_botlin)
1688 space_to_eol (col_lendiff);
1689 else
1690 _rl_clear_to_eol (col_lendiff);
1691 }
1692 }
1693 }
1694 }
1695
1696 /* Tell the update routines that we have moved onto a new (empty) line. */
1697 int
rl_on_new_line()1698 rl_on_new_line ()
1699 {
1700 if (visible_line)
1701 visible_line[0] = '\0';
1702
1703 _rl_last_c_pos = _rl_last_v_pos = 0;
1704 _rl_vis_botlin = last_lmargin = 0;
1705 if (vis_lbreaks)
1706 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1707 visible_wrap_offset = 0;
1708 return 0;
1709 }
1710
1711 /* Tell the update routines that we have moved onto a new line with the
1712 prompt already displayed. Code originally from the version of readline
1713 distributed with CLISP. rl_expand_prompt must have already been called
1714 (explicitly or implicitly). This still doesn't work exactly right. */
1715 int
rl_on_new_line_with_prompt()1716 rl_on_new_line_with_prompt ()
1717 {
1718 int prompt_size, i, l, real_screenwidth, newlines;
1719 char *prompt_last_line, *lprompt;
1720
1721 /* Initialize visible_line and invisible_line to ensure that they can hold
1722 the already-displayed prompt. */
1723 prompt_size = strlen (rl_prompt) + 1;
1724 init_line_structures (prompt_size);
1725
1726 /* Make sure the line structures hold the already-displayed prompt for
1727 redisplay. */
1728 lprompt = local_prompt ? local_prompt : rl_prompt;
1729 strcpy (visible_line, lprompt);
1730 strcpy (invisible_line, lprompt);
1731
1732 /* If the prompt contains newlines, take the last tail. */
1733 prompt_last_line = strrchr (rl_prompt, '\n');
1734 if (!prompt_last_line)
1735 prompt_last_line = rl_prompt;
1736
1737 l = strlen (prompt_last_line);
1738 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1739 _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l); /* XXX */
1740 else
1741 _rl_last_c_pos = l;
1742
1743 /* Dissect prompt_last_line into screen lines. Note that here we have
1744 to use the real screenwidth. Readline's notion of screenwidth might be
1745 one less, see terminal.c. */
1746 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
1747 _rl_last_v_pos = l / real_screenwidth;
1748 /* If the prompt length is a multiple of real_screenwidth, we don't know
1749 whether the cursor is at the end of the last line, or already at the
1750 beginning of the next line. Output a newline just to be safe. */
1751 if (l > 0 && (l % real_screenwidth) == 0)
1752 _rl_output_some_chars ("\n", 1);
1753 last_lmargin = 0;
1754
1755 newlines = 0; i = 0;
1756 while (i <= l)
1757 {
1758 _rl_vis_botlin = newlines;
1759 vis_lbreaks[newlines++] = i;
1760 i += real_screenwidth;
1761 }
1762 vis_lbreaks[newlines] = l;
1763 visible_wrap_offset = 0;
1764
1765 rl_display_prompt = rl_prompt; /* XXX - make sure it's set */
1766
1767 return 0;
1768 }
1769
1770 /* Actually update the display, period. */
1771 int
rl_forced_update_display()1772 rl_forced_update_display ()
1773 {
1774 register char *temp;
1775
1776 if (visible_line)
1777 {
1778 temp = visible_line;
1779 while (*temp)
1780 *temp++ = '\0';
1781 }
1782 rl_on_new_line ();
1783 forced_display++;
1784 (*rl_redisplay_function) ();
1785 return 0;
1786 }
1787
1788 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1789 (Well, when we don't have multibyte characters, _rl_last_c_pos is a
1790 buffer index.)
1791 DATA is the contents of the screen line of interest; i.e., where
1792 the movement is being done. */
1793 void
_rl_move_cursor_relative(new,data)1794 _rl_move_cursor_relative (new, data)
1795 int new;
1796 const char *data;
1797 {
1798 register int i;
1799 int woff; /* number of invisible chars on current line */
1800 int cpos, dpos; /* current and desired cursor positions */
1801
1802 woff = WRAP_OFFSET (_rl_last_v_pos, wrap_offset);
1803 cpos = _rl_last_c_pos;
1804 #if defined (HANDLE_MULTIBYTE)
1805 /* If we have multibyte characters, NEW is indexed by the buffer point in
1806 a multibyte string, but _rl_last_c_pos is the display position. In
1807 this case, NEW's display position is not obvious and must be
1808 calculated. We need to account for invisible characters in this line,
1809 as long as we are past them and they are counted by _rl_col_width. */
1810 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1811 {
1812 dpos = _rl_col_width (data, 0, new);
1813 /* Use NEW when comparing against the last invisible character in the
1814 prompt string, since they're both buffer indices and DPOS is a
1815 desired display position. */
1816 if ((new > prompt_last_invisible) || /* XXX - don't use woff here */
1817 (prompt_physical_chars > _rl_screenwidth &&
1818 _rl_last_v_pos == prompt_last_screen_line &&
1819 wrap_offset != woff &&
1820 new > (prompt_last_invisible-_rl_screenwidth-wrap_offset)))
1821 {
1822 dpos -= woff;
1823 /* Since this will be assigned to _rl_last_c_pos at the end (more
1824 precisely, _rl_last_c_pos == dpos when this function returns),
1825 let the caller know. */
1826 cpos_adjusted = 1;
1827 }
1828 }
1829 else
1830 #endif
1831 dpos = new;
1832
1833 /* If we don't have to do anything, then return. */
1834 if (cpos == dpos)
1835 return;
1836
1837 /* It may be faster to output a CR, and then move forwards instead
1838 of moving backwards. */
1839 /* i == current physical cursor position. */
1840 #if defined (HANDLE_MULTIBYTE)
1841 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1842 i = _rl_last_c_pos;
1843 else
1844 #endif
1845 i = _rl_last_c_pos - woff;
1846 if (dpos == 0 || CR_FASTER (dpos, _rl_last_c_pos) ||
1847 (_rl_term_autowrap && i == _rl_screenwidth))
1848 {
1849 #if defined (__MSDOS__)
1850 putc ('\r', rl_outstream);
1851 #else
1852 tputs (_rl_term_cr, 1, _rl_output_character_function);
1853 #endif /* !__MSDOS__ */
1854 cpos = _rl_last_c_pos = 0;
1855 }
1856
1857 if (cpos < dpos)
1858 {
1859 /* Move the cursor forward. We do it by printing the command
1860 to move the cursor forward if there is one, else print that
1861 portion of the output buffer again. Which is cheaper? */
1862
1863 /* The above comment is left here for posterity. It is faster
1864 to print one character (non-control) than to print a control
1865 sequence telling the terminal to move forward one character.
1866 That kind of control is for people who don't know what the
1867 data is underneath the cursor. */
1868
1869 /* However, we need a handle on where the current display position is
1870 in the buffer for the immediately preceding comment to be true.
1871 In multibyte locales, we don't currently have that info available.
1872 Without it, we don't know where the data we have to display begins
1873 in the buffer and we have to go back to the beginning of the screen
1874 line. In this case, we can use the terminal sequence to move forward
1875 if it's available. */
1876 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1877 {
1878 if (_rl_term_forward_char)
1879 {
1880 for (i = cpos; i < dpos; i++)
1881 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1882 }
1883 else
1884 {
1885 tputs (_rl_term_cr, 1, _rl_output_character_function);
1886 for (i = 0; i < new; i++)
1887 putc (data[i], rl_outstream);
1888 }
1889 }
1890 else
1891 for (i = cpos; i < new; i++)
1892 putc (data[i], rl_outstream);
1893 }
1894
1895 #if defined (HANDLE_MULTIBYTE)
1896 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1897 The byte length of the string is probably bigger than the column width
1898 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1899 display point is less than _rl_last_c_pos. */
1900 #endif
1901 else if (cpos > dpos)
1902 _rl_backspace (cpos - dpos);
1903
1904 _rl_last_c_pos = dpos;
1905 }
1906
1907 /* PWP: move the cursor up or down. */
1908 void
_rl_move_vert(to)1909 _rl_move_vert (to)
1910 int to;
1911 {
1912 register int delta, i;
1913
1914 if (_rl_last_v_pos == to || to > _rl_screenheight)
1915 return;
1916
1917 if ((delta = to - _rl_last_v_pos) > 0)
1918 {
1919 for (i = 0; i < delta; i++)
1920 putc ('\n', rl_outstream);
1921 #if defined (__MSDOS__)
1922 putc ('\r', rl_outstream);
1923 #else
1924 tputs (_rl_term_cr, 1, _rl_output_character_function);
1925 #endif
1926 _rl_last_c_pos = 0;
1927 }
1928 else
1929 { /* delta < 0 */
1930 if (_rl_term_up && *_rl_term_up)
1931 for (i = 0; i < -delta; i++)
1932 tputs (_rl_term_up, 1, _rl_output_character_function);
1933 }
1934
1935 _rl_last_v_pos = to; /* Now TO is here */
1936 }
1937
1938 /* Physically print C on rl_outstream. This is for functions which know
1939 how to optimize the display. Return the number of characters output. */
1940 int
rl_show_char(c)1941 rl_show_char (c)
1942 int c;
1943 {
1944 int n = 1;
1945 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1946 {
1947 fprintf (rl_outstream, "M-");
1948 n += 2;
1949 c = UNMETA (c);
1950 }
1951
1952 #if defined (DISPLAY_TABS)
1953 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1954 #else
1955 if (CTRL_CHAR (c) || c == RUBOUT)
1956 #endif /* !DISPLAY_TABS */
1957 {
1958 fprintf (rl_outstream, "C-");
1959 n += 2;
1960 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1961 }
1962
1963 putc (c, rl_outstream);
1964 fflush (rl_outstream);
1965 return n;
1966 }
1967
1968 int
rl_character_len(c,pos)1969 rl_character_len (c, pos)
1970 register int c, pos;
1971 {
1972 unsigned char uc;
1973
1974 uc = (unsigned char)c;
1975
1976 if (META_CHAR (uc))
1977 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1978
1979 if (uc == '\t')
1980 {
1981 #if defined (DISPLAY_TABS)
1982 return (((pos | 7) + 1) - pos);
1983 #else
1984 return (2);
1985 #endif /* !DISPLAY_TABS */
1986 }
1987
1988 if (CTRL_CHAR (c) || c == RUBOUT)
1989 return (2);
1990
1991 return ((ISPRINT (uc)) ? 1 : 2);
1992 }
1993 /* How to print things in the "echo-area". The prompt is treated as a
1994 mini-modeline. */
1995 static int msg_saved_prompt = 0;
1996
1997 #if defined (USE_VARARGS)
1998 int
1999 #if defined (PREFER_STDARG)
rl_message(const char * format,...)2000 rl_message (const char *format, ...)
2001 #else
2002 rl_message (va_alist)
2003 va_dcl
2004 #endif
2005 {
2006 va_list args;
2007 #if defined (PREFER_VARARGS)
2008 char *format;
2009 #endif
2010
2011 #if defined (PREFER_STDARG)
2012 va_start (args, format);
2013 #else
2014 va_start (args);
2015 format = va_arg (args, char *);
2016 #endif
2017
2018 #if defined (HAVE_VSNPRINTF)
2019 vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
2020 #else
2021 vsprintf (msg_buf, format, args);
2022 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
2023 #endif
2024 va_end (args);
2025
2026 if (saved_local_prompt == 0)
2027 {
2028 rl_save_prompt ();
2029 msg_saved_prompt = 1;
2030 }
2031 rl_display_prompt = msg_buf;
2032 local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
2033 &prompt_last_invisible,
2034 &prompt_invis_chars_first_line,
2035 &prompt_physical_chars);
2036 local_prompt_prefix = (char *)NULL;
2037 local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
2038 (*rl_redisplay_function) ();
2039
2040 return 0;
2041 }
2042 #else /* !USE_VARARGS */
2043 int
rl_message(format,arg1,arg2)2044 rl_message (format, arg1, arg2)
2045 char *format;
2046 {
2047 sprintf (msg_buf, format, arg1, arg2);
2048 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
2049
2050 rl_display_prompt = msg_buf;
2051 if (saved_local_prompt == 0)
2052 {
2053 rl_save_prompt ();
2054 msg_saved_prompt = 1;
2055 }
2056 local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
2057 &prompt_last_invisible,
2058 &prompt_invis_chars_first_line,
2059 &prompt_physical_chars);
2060 local_prompt_prefix = (char *)NULL;
2061 local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
2062 (*rl_redisplay_function) ();
2063
2064 return 0;
2065 }
2066 #endif /* !USE_VARARGS */
2067
2068 /* How to clear things from the "echo-area". */
2069 int
rl_clear_message()2070 rl_clear_message ()
2071 {
2072 rl_display_prompt = rl_prompt;
2073 if (msg_saved_prompt)
2074 {
2075 rl_restore_prompt ();
2076 msg_saved_prompt = 0;
2077 }
2078 (*rl_redisplay_function) ();
2079 return 0;
2080 }
2081
2082 int
rl_reset_line_state()2083 rl_reset_line_state ()
2084 {
2085 rl_on_new_line ();
2086
2087 rl_display_prompt = rl_prompt ? rl_prompt : "";
2088 forced_display = 1;
2089 return 0;
2090 }
2091
2092 void
rl_save_prompt()2093 rl_save_prompt ()
2094 {
2095 saved_local_prompt = local_prompt;
2096 saved_local_prefix = local_prompt_prefix;
2097 saved_prefix_length = prompt_prefix_length;
2098 saved_local_length = local_prompt_len;
2099 saved_last_invisible = prompt_last_invisible;
2100 saved_visible_length = prompt_visible_length;
2101 saved_invis_chars_first_line = prompt_invis_chars_first_line;
2102 saved_physical_chars = prompt_physical_chars;
2103
2104 local_prompt = local_prompt_prefix = (char *)0;
2105 local_prompt_len = 0;
2106 prompt_last_invisible = prompt_visible_length = prompt_prefix_length = 0;
2107 prompt_invis_chars_first_line = prompt_physical_chars = 0;
2108 }
2109
2110 void
rl_restore_prompt()2111 rl_restore_prompt ()
2112 {
2113 FREE (local_prompt);
2114 FREE (local_prompt_prefix);
2115
2116 local_prompt = saved_local_prompt;
2117 local_prompt_prefix = saved_local_prefix;
2118 local_prompt_len = saved_local_length;
2119 prompt_prefix_length = saved_prefix_length;
2120 prompt_last_invisible = saved_last_invisible;
2121 prompt_visible_length = saved_visible_length;
2122 prompt_invis_chars_first_line = saved_invis_chars_first_line;
2123 prompt_physical_chars = saved_physical_chars;
2124
2125 /* can test saved_local_prompt to see if prompt info has been saved. */
2126 saved_local_prompt = saved_local_prefix = (char *)0;
2127 saved_local_length = 0;
2128 saved_last_invisible = saved_visible_length = saved_prefix_length = 0;
2129 saved_invis_chars_first_line = saved_physical_chars = 0;
2130 }
2131
2132 char *
_rl_make_prompt_for_search(pchar)2133 _rl_make_prompt_for_search (pchar)
2134 int pchar;
2135 {
2136 int len;
2137 char *pmt, *p;
2138
2139 rl_save_prompt ();
2140
2141 /* We've saved the prompt, and can do anything with the various prompt
2142 strings we need before they're restored. We want the unexpanded
2143 portion of the prompt string after any final newline. */
2144 p = rl_prompt ? strrchr (rl_prompt, '\n') : 0;
2145 if (p == 0)
2146 {
2147 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
2148 pmt = (char *)xmalloc (len + 2);
2149 if (len)
2150 strcpy (pmt, rl_prompt);
2151 pmt[len] = pchar;
2152 pmt[len+1] = '\0';
2153 }
2154 else
2155 {
2156 p++;
2157 len = strlen (p);
2158 pmt = (char *)xmalloc (len + 2);
2159 if (len)
2160 strcpy (pmt, p);
2161 pmt[len] = pchar;
2162 pmt[len+1] = '\0';
2163 }
2164
2165 /* will be overwritten by expand_prompt, called from rl_message */
2166 prompt_physical_chars = saved_physical_chars + 1;
2167 return pmt;
2168 }
2169
2170 /* Quick redisplay hack when erasing characters at the end of the line. */
2171 void
_rl_erase_at_end_of_line(l)2172 _rl_erase_at_end_of_line (l)
2173 int l;
2174 {
2175 register int i;
2176
2177 _rl_backspace (l);
2178 for (i = 0; i < l; i++)
2179 putc (' ', rl_outstream);
2180 _rl_backspace (l);
2181 for (i = 0; i < l; i++)
2182 visible_line[--_rl_last_c_pos] = '\0';
2183 rl_display_fixed++;
2184 }
2185
2186 /* Clear to the end of the line. COUNT is the minimum
2187 number of character spaces to clear, */
2188 void
_rl_clear_to_eol(count)2189 _rl_clear_to_eol (count)
2190 int count;
2191 {
2192 if (_rl_term_clreol)
2193 tputs (_rl_term_clreol, 1, _rl_output_character_function);
2194 else if (count)
2195 space_to_eol (count);
2196 }
2197
2198 /* Clear to the end of the line using spaces. COUNT is the minimum
2199 number of character spaces to clear, */
2200 static void
space_to_eol(count)2201 space_to_eol (count)
2202 int count;
2203 {
2204 register int i;
2205
2206 for (i = 0; i < count; i++)
2207 putc (' ', rl_outstream);
2208
2209 _rl_last_c_pos += count;
2210 }
2211
2212 void
_rl_clear_screen()2213 _rl_clear_screen ()
2214 {
2215 if (_rl_term_clrpag)
2216 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
2217 else
2218 rl_crlf ();
2219 }
2220
2221 /* Insert COUNT characters from STRING to the output stream at column COL. */
2222 static void
insert_some_chars(string,count,col)2223 insert_some_chars (string, count, col)
2224 char *string;
2225 int count, col;
2226 {
2227 #if defined (__MSDOS__) || defined (__MINGW32__)
2228 _rl_output_some_chars (string, count);
2229 #else
2230 /* DEBUGGING */
2231 if (MB_CUR_MAX == 1 || rl_byte_oriented)
2232 if (count != col)
2233 fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
2234
2235 /* If IC is defined, then we do not have to "enter" insert mode. */
2236 if (_rl_term_IC)
2237 {
2238 char *buffer;
2239
2240 buffer = tgoto (_rl_term_IC, 0, col);
2241 tputs (buffer, 1, _rl_output_character_function);
2242 _rl_output_some_chars (string, count);
2243 }
2244 else
2245 {
2246 register int i;
2247
2248 /* If we have to turn on insert-mode, then do so. */
2249 if (_rl_term_im && *_rl_term_im)
2250 tputs (_rl_term_im, 1, _rl_output_character_function);
2251
2252 /* If there is a special command for inserting characters, then
2253 use that first to open up the space. */
2254 if (_rl_term_ic && *_rl_term_ic)
2255 {
2256 for (i = col; i--; )
2257 tputs (_rl_term_ic, 1, _rl_output_character_function);
2258 }
2259
2260 /* Print the text. */
2261 _rl_output_some_chars (string, count);
2262
2263 /* If there is a string to turn off insert mode, we had best use
2264 it now. */
2265 if (_rl_term_ei && *_rl_term_ei)
2266 tputs (_rl_term_ei, 1, _rl_output_character_function);
2267 }
2268 #endif /* __MSDOS__ || __MINGW32__ */
2269 }
2270
2271 /* Delete COUNT characters from the display line. */
2272 static void
delete_chars(count)2273 delete_chars (count)
2274 int count;
2275 {
2276 if (count > _rl_screenwidth) /* XXX */
2277 return;
2278
2279 #if !defined (__MSDOS__) && !defined (__MINGW32__)
2280 if (_rl_term_DC && *_rl_term_DC)
2281 {
2282 char *buffer;
2283 buffer = tgoto (_rl_term_DC, count, count);
2284 tputs (buffer, count, _rl_output_character_function);
2285 }
2286 else
2287 {
2288 if (_rl_term_dc && *_rl_term_dc)
2289 while (count--)
2290 tputs (_rl_term_dc, 1, _rl_output_character_function);
2291 }
2292 #endif /* !__MSDOS__ && !__MINGW32__ */
2293 }
2294
2295 void
_rl_update_final()2296 _rl_update_final ()
2297 {
2298 int full_lines;
2299
2300 full_lines = 0;
2301 /* If the cursor is the only thing on an otherwise-blank last line,
2302 compensate so we don't print an extra CRLF. */
2303 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
2304 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
2305 {
2306 _rl_vis_botlin--;
2307 full_lines = 1;
2308 }
2309 _rl_move_vert (_rl_vis_botlin);
2310 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
2311 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
2312 {
2313 char *last_line;
2314
2315 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
2316 cpos_buffer_position = -1; /* don't know where we are in buffer */
2317 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line); /* XXX */
2318 _rl_clear_to_eol (0);
2319 putc (last_line[_rl_screenwidth - 1], rl_outstream);
2320 }
2321 _rl_vis_botlin = 0;
2322 rl_crlf ();
2323 fflush (rl_outstream);
2324 rl_display_fixed++;
2325 }
2326
2327 /* Move to the start of the current line. */
2328 static void
cr()2329 cr ()
2330 {
2331 if (_rl_term_cr)
2332 {
2333 #if defined (__MSDOS__)
2334 putc ('\r', rl_outstream);
2335 #else
2336 tputs (_rl_term_cr, 1, _rl_output_character_function);
2337 #endif
2338 _rl_last_c_pos = 0;
2339 }
2340 }
2341
2342 /* Redraw the last line of a multi-line prompt that may possibly contain
2343 terminal escape sequences. Called with the cursor at column 0 of the
2344 line to draw the prompt on. */
2345 static void
redraw_prompt(t)2346 redraw_prompt (t)
2347 char *t;
2348 {
2349 char *oldp;
2350
2351 oldp = rl_display_prompt;
2352 rl_save_prompt ();
2353
2354 rl_display_prompt = t;
2355 local_prompt = expand_prompt (t, &prompt_visible_length,
2356 &prompt_last_invisible,
2357 &prompt_invis_chars_first_line,
2358 &prompt_physical_chars);
2359 local_prompt_prefix = (char *)NULL;
2360 local_prompt_len = local_prompt ? strlen (local_prompt) : 0;
2361
2362 rl_forced_update_display ();
2363
2364 rl_display_prompt = oldp;
2365 rl_restore_prompt();
2366 }
2367
2368 /* Redisplay the current line after a SIGWINCH is received. */
2369 void
_rl_redisplay_after_sigwinch()2370 _rl_redisplay_after_sigwinch ()
2371 {
2372 char *t;
2373
2374 /* Clear the current line and put the cursor at column 0. Make sure
2375 the right thing happens if we have wrapped to a new screen line. */
2376 if (_rl_term_cr)
2377 {
2378 #if defined (__MSDOS__)
2379 putc ('\r', rl_outstream);
2380 #else
2381 tputs (_rl_term_cr, 1, _rl_output_character_function);
2382 #endif
2383 _rl_last_c_pos = 0;
2384 #if defined (__MSDOS__)
2385 space_to_eol (_rl_screenwidth);
2386 putc ('\r', rl_outstream);
2387 #else
2388 if (_rl_term_clreol)
2389 tputs (_rl_term_clreol, 1, _rl_output_character_function);
2390 else
2391 {
2392 space_to_eol (_rl_screenwidth);
2393 tputs (_rl_term_cr, 1, _rl_output_character_function);
2394 }
2395 #endif
2396 if (_rl_last_v_pos > 0)
2397 _rl_move_vert (0);
2398 }
2399 else
2400 rl_crlf ();
2401
2402 /* Redraw only the last line of a multi-line prompt. */
2403 t = strrchr (rl_display_prompt, '\n');
2404 if (t)
2405 redraw_prompt (++t);
2406 else
2407 rl_forced_update_display ();
2408 }
2409
2410 void
_rl_clean_up_for_exit()2411 _rl_clean_up_for_exit ()
2412 {
2413 if (readline_echoing_p)
2414 {
2415 _rl_move_vert (_rl_vis_botlin);
2416 _rl_vis_botlin = 0;
2417 fflush (rl_outstream);
2418 rl_restart_output (1, 0);
2419 }
2420 }
2421
2422 void
_rl_erase_entire_line()2423 _rl_erase_entire_line ()
2424 {
2425 cr ();
2426 _rl_clear_to_eol (0);
2427 cr ();
2428 fflush (rl_outstream);
2429 }
2430
2431 /* return the `current display line' of the cursor -- the number of lines to
2432 move up to get to the first screen line of the current readline line. */
2433 int
_rl_current_display_line()2434 _rl_current_display_line ()
2435 {
2436 int ret, nleft;
2437
2438 /* Find out whether or not there might be invisible characters in the
2439 editing buffer. */
2440 if (rl_display_prompt == rl_prompt)
2441 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
2442 else
2443 nleft = _rl_last_c_pos - _rl_screenwidth;
2444
2445 if (nleft > 0)
2446 ret = 1 + nleft / _rl_screenwidth;
2447 else
2448 ret = 0;
2449
2450 return ret;
2451 }
2452
2453 #if defined (HANDLE_MULTIBYTE)
2454 /* Calculate the number of screen columns occupied by STR from START to END.
2455 In the case of multibyte characters with stateful encoding, we have to
2456 scan from the beginning of the string to take the state into account. */
2457 static int
_rl_col_width(str,start,end)2458 _rl_col_width (str, start, end)
2459 const char *str;
2460 int start, end;
2461 {
2462 wchar_t wc;
2463 mbstate_t ps;
2464 int tmp, point, width, max;
2465
2466 if (end <= start)
2467 return 0;
2468 if (MB_CUR_MAX == 1 || rl_byte_oriented)
2469 return (end - start);
2470
2471 memset (&ps, 0, sizeof (mbstate_t));
2472
2473 point = 0;
2474 max = end;
2475
2476 while (point < start)
2477 {
2478 tmp = mbrlen (str + point, max, &ps);
2479 if (MB_INVALIDCH ((size_t)tmp))
2480 {
2481 /* In this case, the bytes are invalid or too short to compose a
2482 multibyte character, so we assume that the first byte represents
2483 a single character. */
2484 point++;
2485 max--;
2486
2487 /* Clear the state of the byte sequence, because in this case the
2488 effect of mbstate is undefined. */
2489 memset (&ps, 0, sizeof (mbstate_t));
2490 }
2491 else if (MB_NULLWCH (tmp))
2492 break; /* Found '\0' */
2493 else
2494 {
2495 point += tmp;
2496 max -= tmp;
2497 }
2498 }
2499
2500 /* If START is not a byte that starts a character, then POINT will be
2501 greater than START. In this case, assume that (POINT - START) gives
2502 a byte count that is the number of columns of difference. */
2503 width = point - start;
2504
2505 while (point < end)
2506 {
2507 tmp = mbrtowc (&wc, str + point, max, &ps);
2508 if (MB_INVALIDCH ((size_t)tmp))
2509 {
2510 /* In this case, the bytes are invalid or too short to compose a
2511 multibyte character, so we assume that the first byte represents
2512 a single character. */
2513 point++;
2514 max--;
2515
2516 /* and assume that the byte occupies a single column. */
2517 width++;
2518
2519 /* Clear the state of the byte sequence, because in this case the
2520 effect of mbstate is undefined. */
2521 memset (&ps, 0, sizeof (mbstate_t));
2522 }
2523 else if (MB_NULLWCH (tmp))
2524 break; /* Found '\0' */
2525 else
2526 {
2527 point += tmp;
2528 max -= tmp;
2529 tmp = wcwidth(wc);
2530 width += (tmp >= 0) ? tmp : 1;
2531 }
2532 }
2533
2534 width += point - end;
2535
2536 return width;
2537 }
2538 #endif /* HANDLE_MULTIBYTE */
2539