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