1 /* display.c -- readline redisplay facility. */
3 /* Copyright (C) 1987-2005 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
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.
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.
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
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
32 #endif /* HAVE_UNISTD_H */
34 #include "posixstat.h"
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
44 /* System-specific feature definitions and include files. */
48 /* Termcap library stuff. */
51 /* Some standard library routines. */
55 #include "rlprivate.h"
58 #if !defined (strchr) && !defined (__STDC__)
59 extern char *strchr (), *strrchr ();
60 #endif /* !strchr && !__STDC__ */
62 #if defined (HACK_TERMCAP_MOTION)
63 extern char *_rl_term_forward_char;
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));
72 #if defined (HANDLE_MULTIBYTE)
73 static int _rl_col_width PARAMS((const char *, int, int));
74 static int *_rl_wrapped_line;
76 # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
79 static int *inv_lbreaks, *vis_lbreaks;
80 static int inv_lbsize, vis_lbsize;
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)))
86 /* **************************************************************** */
90 /* **************************************************************** */
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. */
95 /* (PWP) Well... Good for a simple line updater, but totally ignores
96 the problems of input lines longer than the screen width.
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. */
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.
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. */
113 /* Application-specific redisplay function. */
114 rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
116 /* Global variables declared here. */
117 /* What YOU turn on when you have handled all redisplay yourself. */
118 int rl_display_fixed = 0;
120 int _rl_suppress_redisplay = 0;
121 int _rl_want_redisplay = 0;
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;
127 /* Pseudo-global variables declared here. */
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;
137 /* Number of lines currently on screen minus 1. */
138 int _rl_vis_botlin = 0;
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;
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;
150 /* A buffer for `modeline' messages. */
151 static char msg_buf[128];
153 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
154 static int forced_display;
156 /* Default and initial buffer size. Can grow. */
157 static int line_size = 1024;
159 /* Variables to keep track of the expanded prompt string, which may
160 include invisible characters. */
162 static char *local_prompt, *local_prompt_prefix;
163 static int prompt_visible_length, prompt_prefix_length;
165 /* The number of invisible characters in the line currently being
166 displayed on the screen. */
167 static int visible_wrap_offset;
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;
173 /* The index of the last invisible character in the prompt string. */
174 static int prompt_last_invisible;
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;
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;
185 static int prompt_last_screen_line;
187 static int prompt_physical_chars;
189 /* Variables to save and restore prompt and display information. */
191 /* These are getting numerous enough that it's time to create a struct. */
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;
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. */
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'. */
217 expand_prompt (pmt, lp, lip, niflp, vlp)
219 int *lp, *lip, *niflp, *vlp;
222 int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
224 /* Short-circuit if we can. */
225 if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
227 r = savestring (pmt);
235 *vlp = lp ? *lp : strlen (r);
240 r = ret = (char *)xmalloc (l + 1);
242 invfl = 0; /* invisible chars in first line of prompt */
243 invflset = 0; /* we only want to set invfl once */
245 for (rl = ignoring = last = ninvis = physchars = 0, p = pmt; p && *p; p++)
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)
254 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
257 if (p[-1] != RL_PROMPT_START_IGNORE)
263 #if defined (HANDLE_MULTIBYTE)
264 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
267 ind = _rl_find_next_mbchar (pmt, pind, 1, MB_FIND_NONZERO);
274 physchars += _rl_col_width (pmt, pind, ind);
277 ninvis += ind - pind;
278 p--; /* compensate for later increment */
286 rl++; /* visible length byte counter */
290 ninvis++; /* invisible chars byte counter */
293 if (invflset == 0 && rl >= _rl_screenwidth)
301 if (rl < _rl_screenwidth)
316 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
317 PMT and return the rest of PMT. */
319 _rl_strip_prompt (pmt)
324 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
329 * Expand the prompt string into the various display components, if
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
339 * This function is called once per call to readline(). It may also be
340 * called arbitrarily to expand the primary prompt.
342 * The return value is the number of visible characters on the last line
343 * of the (possibly multi-line) prompt.
346 rl_expand_prompt (prompt)
352 /* Clear out any saved values. */
354 FREE (local_prompt_prefix);
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;
360 if (prompt == 0 || *prompt == 0)
363 p = strrchr (prompt, '\n');
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);
376 /* The prompt spans multiple lines. */
378 local_prompt = expand_prompt (p, &prompt_visible_length,
379 &prompt_last_invisible,
381 &prompt_physical_chars);
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,
387 &prompt_invis_chars_first_line,
390 return (prompt_prefix_length);
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. */
400 init_line_structures (minsize)
405 if (invisible_line == 0) /* initialize it */
407 if (line_size < minsize)
409 visible_line = (char *)xmalloc (line_size);
410 invisible_line = (char *)xmalloc (line_size);
412 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
415 if (line_size < minsize)
417 visible_line = (char *)xrealloc (visible_line, line_size);
418 invisible_line = (char *)xrealloc (invisible_line, line_size);
421 for (n = minsize; n < line_size; n++)
424 invisible_line[n] = 1;
427 if (vis_lbreaks == 0)
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));
436 inv_lbreaks[0] = vis_lbreaks[0] = 0;
440 /* Basic redisplay algorithm. */
444 register int in, out, c, linenum, cursor_linenum;
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)
454 int _rl_wrapped_multicolumn = 0;
457 if (!readline_echoing_p)
460 if (!rl_display_prompt)
461 rl_display_prompt = "";
463 if (invisible_line == 0 || vis_lbreaks == 0)
465 init_line_structures (0);
469 /* Draw the line into the buffer. */
472 line = invisible_line;
473 out = inv_botlin = 0;
475 /* Mark the line as modified or not. We only do this for history
478 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
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;
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)
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));
503 temp = local_len + out + 2;
504 if (temp >= line_size)
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);
510 strncpy (line + out, local_prompt, local_len);
514 wrap_offset = local_len - prompt_visible_length;
519 prompt_this_line = strrchr (rl_display_prompt, '\n');
520 if (!prompt_this_line)
521 prompt_this_line = rl_display_prompt;
525 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
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')
536 prompt_physical_chars = pmtlen = strlen (prompt_this_line);
537 temp = pmtlen + out + 2;
538 if (temp >= line_size)
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);
544 strncpy (line + out, prompt_this_line, pmtlen);
547 wrap_offset = prompt_invis_chars_first_line = 0;
550 #define CHECK_INV_LBREAKS() \
552 if (newlines >= (inv_lbsize - 2)) \
555 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
559 #if defined (HANDLE_MULTIBYTE)
560 #define CHECK_LPOS() \
563 if (lpos >= _rl_screenwidth) \
565 if (newlines >= (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)); \
571 inv_lbreaks[++newlines] = out; \
572 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
577 #define CHECK_LPOS() \
580 if (lpos >= _rl_screenwidth) \
582 if (newlines >= (inv_lbsize - 2)) \
585 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
587 inv_lbreaks[++newlines] = out; \
593 /* inv_lbreaks[i] is where line i starts in the buffer. */
594 inv_lbreaks[newlines = 0] = 0;
596 lpos = out - wrap_offset;
598 lpos = prompt_physical_chars + modmark;
601 #if defined (HANDLE_MULTIBYTE)
602 memset (_rl_wrapped_line, 0, vis_lbsize);
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. */
611 /* what if lpos is already >= _rl_screenwidth before we start drawing the
612 contents of the command line? */
613 while (lpos >= _rl_screenwidth)
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)
624 temp = local_prompt ? strlen (local_prompt) : 0;
627 if (_rl_col_width (local_prompt, n0, num) > _rl_screenwidth)
629 num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
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));
642 inv_lbreaks[++newlines] = temp;
643 #if defined (HANDLE_MULTIBYTE)
644 lpos -= _rl_col_width (local_prompt, n0, num);
646 lpos -= _rl_screenwidth;
650 prompt_last_screen_line = newlines;
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. */
658 #if defined (HANDLE_MULTIBYTE)
660 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
662 memset (&ps, 0, sizeof (mbstate_t));
663 wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
669 for (in = 0; in < rl_end; in++)
672 c = (unsigned char)rl_line_buffer[in];
674 #if defined (HANDLE_MULTIBYTE)
675 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
677 if (MB_INVALIDCH (wc_bytes))
679 /* Byte sequence is invalid or shortened. Assume that the
680 first byte represents a character. */
682 /* Assume that a character occupies a single column. */
684 memset (&ps, 0, sizeof (mbstate_t));
686 else if (MB_NULLWCH (wc_bytes))
687 break; /* Found '\0' */
691 wc_width = (temp >= 0) ? temp : 1;
696 if (out + 8 >= line_size) /* XXX - 8 for \t */
699 visible_line = (char *)xrealloc (visible_line, line_size);
700 invisible_line = (char *)xrealloc (invisible_line, line_size);
701 line = invisible_line;
707 lb_linenum = newlines;
710 #if defined (HANDLE_MULTIBYTE)
711 if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
716 if (_rl_output_meta_chars == 0)
718 sprintf (line + out, "\\%o", c);
720 if (lpos + 4 >= _rl_screenwidth)
722 temp = _rl_screenwidth - lpos;
723 CHECK_INV_LBREAKS ();
724 inv_lbreaks[++newlines] = out + temp;
738 #if defined (DISPLAY_TABS)
744 newout = (out | (int)7) + 1;
746 newout = out + 8 - lpos % 8;
749 if (lpos + temp >= _rl_screenwidth)
752 temp2 = _rl_screenwidth - lpos;
753 CHECK_INV_LBREAKS ();
754 inv_lbreaks[++newlines] = out + temp2;
767 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
769 line[out++] = '\0'; /* XXX - sentinel */
770 CHECK_INV_LBREAKS ();
771 inv_lbreaks[++newlines] = out;
774 else if (CTRL_CHAR (c) || c == RUBOUT)
778 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
783 #if defined (HANDLE_MULTIBYTE)
784 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
788 _rl_wrapped_multicolumn = 0;
790 if (_rl_screenwidth < lpos + wc_width)
791 for (i = lpos; i < _rl_screenwidth; i++)
793 /* The space will be removed in update_line() */
795 _rl_wrapped_multicolumn++;
801 lb_linenum = newlines;
803 for (i = in; i < in+wc_bytes; i++)
804 line[out++] = rl_line_buffer[i];
805 for (i = 0; i < wc_width; i++)
819 #if defined (HANDLE_MULTIBYTE)
820 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
823 wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
834 lb_linenum = newlines;
837 inv_botlin = lb_botlin = newlines;
838 CHECK_INV_LBREAKS ();
839 inv_lbreaks[newlines+1] = out;
840 cursor_linenum = lb_linenum;
842 /* C_POS == position in buffer where cursor should be placed.
843 CURSOR_LINENUM == line number where the cursor should be placed. */
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. */
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. */
854 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
856 int nleft, pos, changed_screen_line, tx;
858 if (!rl_display_fixed || forced_display)
862 /* If we have more than a screenful of material to display, then
863 only display a screenful. We should display the last screen,
865 if (out >= _rl_screenchars)
867 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
868 out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
870 out = _rl_screenchars - 1;
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). */
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])
884 /* For each line in the buffer, do the updating display. */
885 for (linenum = 0; linenum <= inv_botlin; linenum++)
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);
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;
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. */
911 inv_botlin == 0 && _rl_last_c_pos == out &&
912 (wrap_offset > visible_wrap_offset) &&
913 (_rl_last_c_pos < visible_first_line_len))
915 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
916 nleft = _rl_screenwidth - _rl_last_c_pos;
918 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
920 _rl_clear_to_eol (nleft);
923 /* Since the new first line is now visible, save its length. */
925 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
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)
933 for (; linenum <= _rl_vis_botlin; linenum++)
935 tt = VIS_CHARS (linenum);
936 _rl_move_vert (linenum);
937 _rl_move_cursor_relative (0, tt);
939 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
942 _rl_vis_botlin = inv_botlin;
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)
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;
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)
967 #if defined (__MSDOS__)
968 putc ('\r', rl_outstream);
971 tputs (_rl_term_cr, 1, _rl_output_character_function);
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;
977 _rl_last_c_pos = nleft;
980 /* Where on that line? And where does that line start
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. */
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. */
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)
997 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
998 tx = _rl_col_width (&visible_line[pos], 0, nleft) - visible_wrap_offset;
1001 if (_rl_last_c_pos > tx)
1003 _rl_backspace (_rl_last_c_pos - tx); /* XXX */
1004 _rl_last_c_pos = tx;
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]);
1018 else /* Do horizontal scrolling. */
1020 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
1021 int lmargin, ndisp, nleft, phys_c_pos, t;
1023 /* Always at top line. */
1026 /* Compute where in the buffer the displayed line should start. This
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;
1037 /* If the number of characters had already exceeded the screenwidth,
1038 last_lmargin will be > 0. */
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)
1045 lmargin = c_pos - (2 * t);
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)
1053 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
1055 else if (phys_c_pos < 1)
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)
1064 lmargin = last_lmargin;
1066 /* If the first character on the screen isn't the first character
1067 in the display line, indicate this with a special character. */
1069 line[lmargin] = '<';
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;
1079 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1082 update_line (&visible_line[last_lmargin],
1083 &invisible_line[lmargin],
1085 _rl_screenwidth + visible_wrap_offset,
1086 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
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)
1097 nleft = _rl_screenwidth - t;
1098 _rl_clear_to_eol (nleft);
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;
1104 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1105 last_lmargin = lmargin;
1108 fflush (rl_outstream);
1110 /* Swap visible and non-visible lines. */
1112 char *vtemp = visible_line;
1113 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
1115 visible_line = invisible_line;
1116 invisible_line = vtemp;
1118 vis_lbreaks = inv_lbreaks;
1119 inv_lbreaks = itemp;
1121 vis_lbsize = inv_lbsize;
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
1128 if (_rl_horizontal_scroll_mode && last_lmargin)
1129 visible_wrap_offset = 0;
1131 visible_wrap_offset = wrap_offset;
1135 /* PWP: update_line() is based on finding the middle difference of each
1136 line on the screen; vis:
1138 /old first difference
1139 /beginning of line | /old last same /old EOL
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
1144 \beginning of line | \new last same \new end of line
1145 \new first difference
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.
1150 Could be made even smarter, but this works well enough */
1152 update_line (old, new, current_line, omax, nmax, inv_botlin)
1153 register char *old, *new;
1154 int current_line, omax, nmax, inv_botlin;
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;
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;
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)
1177 #if defined (HANDLE_MULTIBYTE)
1178 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1182 int tempwidth, bytes;
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]);
1191 memset (&ps, 0, sizeof (mbstate_t));
1192 ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
1193 if (MB_INVALIDCH (ret))
1198 else if (MB_NULLWCH (ret))
1201 tempwidth = wcwidth (wc);
1207 for (count = 0; count < bytes; count++)
1208 putc (new[count], rl_outstream);
1209 _rl_last_c_pos = tempwidth;
1211 memset (&ps, 0, sizeof (mbstate_t));
1212 ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1213 if (ret != 0 && bytes != 0)
1215 if (MB_INVALIDCH (ret))
1216 memmove (old+bytes, old+1, strlen (old+1));
1218 memmove (old+bytes, old+ret, strlen (old+ret));
1219 memcpy (old, new, bytes);
1224 putc (' ', rl_outstream);
1227 if (old[0] && new[0])
1235 putc (new[0], rl_outstream);
1237 putc (' ', rl_outstream);
1238 _rl_last_c_pos = 1; /* XXX */
1240 if (old[0] && new[0])
1246 /* Find first difference. */
1247 #if defined (HANDLE_MULTIBYTE)
1248 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
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)
1260 memset (&ps_new, 0, sizeof(mbstate_t));
1261 memset (&ps_old, 0, sizeof(mbstate_t));
1263 if (omax == nmax && STREQN (new, old, omax))
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); )
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;
1285 for (ofd = old, nfd = new;
1286 (ofd - old < omax) && *ofd && (*ofd == *nfd);
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++);
1296 /* If no difference, continue to next line. */
1297 if (ofd == oe && nfd == ne)
1300 wsatend = 1; /* flag for trailing whitespace */
1302 #if defined (HANDLE_MULTIBYTE)
1303 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
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))
1309 memset (&ps_old, 0, sizeof (mbstate_t));
1310 memset (&ps_new, 0, sizeof (mbstate_t));
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);
1318 if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
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);
1330 #endif /* HANDLE_MULTIBYTE */
1331 ols = oe - 1; /* find last same */
1333 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
1340 #if defined (HANDLE_MULTIBYTE)
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)
1355 else if (*ols != *nls)
1358 if (*ols) /* don't step past the NUL */
1360 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1361 ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1367 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1368 nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
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)
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;
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.
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. */
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)
1401 #if defined (__MSDOS__)
1402 putc ('\r', rl_outstream);
1404 tputs (_rl_term_cr, 1, _rl_output_character_function);
1406 _rl_output_some_chars (local_prompt, lendiff);
1407 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1409 _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff) - wrap_offset;
1411 _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
1414 _rl_last_c_pos = lendiff;
1417 _rl_move_cursor_relative (od, old);
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);
1427 col_lendiff = lendiff;
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)
1435 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1437 lendiff += visible_wrap_offset - current_invis_chars;
1438 col_lendiff += visible_wrap_offset - current_invis_chars;
1442 lendiff += visible_wrap_offset - current_invis_chars;
1443 col_lendiff = lendiff;
1447 /* Insert (diff (len (old), len (new)) ch. */
1449 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1450 col_temp = _rl_col_width (new, nfd - new, ne - new);
1454 if (col_lendiff > 0) /* XXX - was lendiff */
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))
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))
1471 insert_some_chars (nfd, lendiff, col_lendiff);
1472 _rl_last_c_pos += col_lendiff;
1474 else if ((MB_CUR_MAX == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0)
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;
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 */
1492 /* Copy (new) chars to screen from first diff to last match. */
1494 if ((temp - lendiff) > 0)
1496 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
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);
1504 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff);
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. */
1519 else /* Delete characters from line. */
1521 /* If possible and inexpensive to use terminal deletion, then do so. */
1522 if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
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)
1532 delete_chars (-col_lendiff); /* delete (diff) characters */
1534 /* Copy (new) chars to screen from first diff to last match */
1538 _rl_output_some_chars (nfd, temp);
1539 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
1542 /* Otherwise, print over the existing material. */
1547 _rl_output_some_chars (nfd, temp);
1548 _rl_last_c_pos += col_temp; /* XXX */
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);
1554 col_lendiff = lendiff;
1558 if (_rl_term_autowrap && current_line < inv_botlin)
1559 space_to_eol (col_lendiff);
1561 _rl_clear_to_eol (col_lendiff);
1567 /* Tell the update routines that we have moved onto a new (empty) line. */
1572 visible_line[0] = '\0';
1574 _rl_last_c_pos = _rl_last_v_pos = 0;
1575 _rl_vis_botlin = last_lmargin = 0;
1577 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1578 visible_wrap_offset = 0;
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. */
1587 rl_on_new_line_with_prompt ()
1589 int prompt_size, i, l, real_screenwidth, newlines;
1590 char *prompt_last_line, *lprompt;
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);
1597 /* Make sure the line structures hold the already-displayed prompt for
1599 lprompt = local_prompt ? local_prompt : rl_prompt;
1600 strcpy (visible_line, lprompt);
1601 strcpy (invisible_line, lprompt);
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;
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 */
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);
1626 newlines = 0; i = 0;
1629 _rl_vis_botlin = newlines;
1630 vis_lbreaks[newlines++] = i;
1631 i += real_screenwidth;
1633 vis_lbreaks[newlines] = l;
1634 visible_wrap_offset = 0;
1636 rl_display_prompt = rl_prompt; /* XXX - make sure it's set */
1641 /* Actually update the display, period. */
1643 rl_forced_update_display ()
1647 register char *temp = visible_line;
1654 (*rl_redisplay_function) ();
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
1661 DATA is the contents of the screen line of interest; i.e., where
1662 the movement is being done. */
1664 _rl_move_cursor_relative (new, data)
1669 int woff; /* number of invisible chars on current line */
1670 int cpos, dpos; /* current and desired cursor positions */
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)
1682 dpos = _rl_col_width (data, 0, new);
1690 /* If we don't have to do anything, then return. */
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)
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))
1706 #if defined (__MSDOS__)
1707 putc ('\r', rl_outstream);
1709 tputs (_rl_term_cr, 1, _rl_output_character_function);
1710 #endif /* !__MSDOS__ */
1711 cpos = _rl_last_c_pos = 0;
1716 /* Move the cursor forward. We do it by printing the command
1717 to move the cursor forward if there is one, else print that
1718 portion of the output buffer again. Which is cheaper? */
1720 /* The above comment is left here for posterity. It is faster
1721 to print one character (non-control) than to print a control
1722 sequence telling the terminal to move forward one character.
1723 That kind of control is for people who don't know what the
1724 data is underneath the cursor. */
1725 #if defined (HACK_TERMCAP_MOTION)
1726 if (_rl_term_forward_char)
1728 for (i = cpos; i < dpos; i++)
1729 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1732 #endif /* HACK_TERMCAP_MOTION */
1733 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1735 tputs (_rl_term_cr, 1, _rl_output_character_function);
1736 for (i = 0; i < new; i++)
1737 putc (data[i], rl_outstream);
1740 for (i = cpos; i < new; i++)
1741 putc (data[i], rl_outstream);
1744 #if defined (HANDLE_MULTIBYTE)
1745 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1746 The byte length of the string is probably bigger than the column width
1747 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1748 display point is less than _rl_last_c_pos. */
1750 else if (cpos > dpos)
1751 _rl_backspace (cpos - dpos);
1753 _rl_last_c_pos = dpos;
1756 /* PWP: move the cursor up or down. */
1761 register int delta, i;
1763 if (_rl_last_v_pos == to || to > _rl_screenheight)
1766 if ((delta = to - _rl_last_v_pos) > 0)
1768 for (i = 0; i < delta; i++)
1769 putc ('\n', rl_outstream);
1770 #if defined (__MSDOS__)
1771 putc ('\r', rl_outstream);
1773 tputs (_rl_term_cr, 1, _rl_output_character_function);
1779 if (_rl_term_up && *_rl_term_up)
1780 for (i = 0; i < -delta; i++)
1781 tputs (_rl_term_up, 1, _rl_output_character_function);
1784 _rl_last_v_pos = to; /* Now TO is here */
1787 /* Physically print C on rl_outstream. This is for functions which know
1788 how to optimize the display. Return the number of characters output. */
1794 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1796 fprintf (rl_outstream, "M-");
1801 #if defined (DISPLAY_TABS)
1802 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1804 if (CTRL_CHAR (c) || c == RUBOUT)
1805 #endif /* !DISPLAY_TABS */
1807 fprintf (rl_outstream, "C-");
1809 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1812 putc (c, rl_outstream);
1813 fflush (rl_outstream);
1818 rl_character_len (c, pos)
1819 register int c, pos;
1823 uc = (unsigned char)c;
1826 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1830 #if defined (DISPLAY_TABS)
1831 return (((pos | 7) + 1) - pos);
1834 #endif /* !DISPLAY_TABS */
1837 if (CTRL_CHAR (c) || c == RUBOUT)
1840 return ((ISPRINT (uc)) ? 1 : 2);
1842 /* How to print things in the "echo-area". The prompt is treated as a
1844 static int msg_saved_prompt = 0;
1846 #if defined (USE_VARARGS)
1848 #if defined (PREFER_STDARG)
1849 rl_message (const char *format, ...)
1851 rl_message (va_alist)
1856 #if defined (PREFER_VARARGS)
1860 #if defined (PREFER_STDARG)
1861 va_start (args, format);
1864 format = va_arg (args, char *);
1867 #if defined (HAVE_VSNPRINTF)
1868 vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
1870 vsprintf (msg_buf, format, args);
1871 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1875 if (saved_local_prompt == 0)
1878 msg_saved_prompt = 1;
1880 rl_display_prompt = msg_buf;
1881 local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
1882 &prompt_last_invisible,
1883 &prompt_invis_chars_first_line,
1884 &prompt_physical_chars);
1885 local_prompt_prefix = (char *)NULL;
1886 (*rl_redisplay_function) ();
1890 #else /* !USE_VARARGS */
1892 rl_message (format, arg1, arg2)
1895 sprintf (msg_buf, format, arg1, arg2);
1896 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1898 rl_display_prompt = msg_buf;
1899 if (saved_local_prompt == 0)
1902 msg_saved_prompt = 1;
1904 local_prompt = expand_prompt (msg_buf, &prompt_visible_length,
1905 &prompt_last_invisible,
1906 &prompt_invis_chars_first_line,
1907 &prompt_physical_chars);
1908 local_prompt_prefix = (char *)NULL;
1909 (*rl_redisplay_function) ();
1913 #endif /* !USE_VARARGS */
1915 /* How to clear things from the "echo-area". */
1919 rl_display_prompt = rl_prompt;
1920 if (msg_saved_prompt)
1922 rl_restore_prompt ();
1923 msg_saved_prompt = 0;
1925 (*rl_redisplay_function) ();
1930 rl_reset_line_state ()
1934 rl_display_prompt = rl_prompt ? rl_prompt : "";
1942 saved_local_prompt = local_prompt;
1943 saved_local_prefix = local_prompt_prefix;
1944 saved_prefix_length = prompt_prefix_length;
1945 saved_last_invisible = prompt_last_invisible;
1946 saved_visible_length = prompt_visible_length;
1947 saved_invis_chars_first_line = prompt_invis_chars_first_line;
1948 saved_physical_chars = prompt_physical_chars;
1950 local_prompt = local_prompt_prefix = (char *)0;
1951 prompt_last_invisible = prompt_visible_length = prompt_prefix_length = 0;
1952 prompt_invis_chars_first_line = prompt_physical_chars = 0;
1956 rl_restore_prompt ()
1958 FREE (local_prompt);
1959 FREE (local_prompt_prefix);
1961 local_prompt = saved_local_prompt;
1962 local_prompt_prefix = saved_local_prefix;
1963 prompt_prefix_length = saved_prefix_length;
1964 prompt_last_invisible = saved_last_invisible;
1965 prompt_visible_length = saved_visible_length;
1966 prompt_invis_chars_first_line = saved_invis_chars_first_line;
1967 prompt_physical_chars = saved_physical_chars;
1969 /* can test saved_local_prompt to see if prompt info has been saved. */
1970 saved_local_prompt = saved_local_prefix = (char *)0;
1971 saved_last_invisible = saved_visible_length = saved_prefix_length = 0;
1972 saved_invis_chars_first_line = saved_physical_chars = 0;
1976 _rl_make_prompt_for_search (pchar)
1984 if (saved_local_prompt == 0)
1986 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1987 pmt = (char *)xmalloc (len + 2);
1989 strcpy (pmt, rl_prompt);
1995 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1996 pmt = (char *)xmalloc (len + 2);
1998 strcpy (pmt, saved_local_prompt);
2001 local_prompt = savestring (pmt);
2002 prompt_last_invisible = saved_last_invisible;
2003 prompt_visible_length = saved_visible_length + 1;
2006 prompt_physical_chars = saved_physical_chars + 1;
2011 /* Quick redisplay hack when erasing characters at the end of the line. */
2013 _rl_erase_at_end_of_line (l)
2019 for (i = 0; i < l; i++)
2020 putc (' ', rl_outstream);
2022 for (i = 0; i < l; i++)
2023 visible_line[--_rl_last_c_pos] = '\0';
2027 /* Clear to the end of the line. COUNT is the minimum
2028 number of character spaces to clear, */
2030 _rl_clear_to_eol (count)
2033 if (_rl_term_clreol)
2034 tputs (_rl_term_clreol, 1, _rl_output_character_function);
2036 space_to_eol (count);
2039 /* Clear to the end of the line using spaces. COUNT is the minimum
2040 number of character spaces to clear, */
2042 space_to_eol (count)
2047 for (i = 0; i < count; i++)
2048 putc (' ', rl_outstream);
2050 _rl_last_c_pos += count;
2056 if (_rl_term_clrpag)
2057 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
2062 /* Insert COUNT characters from STRING to the output stream at column COL. */
2064 insert_some_chars (string, count, col)
2068 #if defined (__MSDOS__) || defined (__MINGW32__)
2069 _rl_output_some_chars (string, count);
2072 if (MB_CUR_MAX == 1 || rl_byte_oriented)
2074 fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
2076 /* If IC is defined, then we do not have to "enter" insert mode. */
2081 buffer = tgoto (_rl_term_IC, 0, col);
2082 tputs (buffer, 1, _rl_output_character_function);
2083 _rl_output_some_chars (string, count);
2089 /* If we have to turn on insert-mode, then do so. */
2090 if (_rl_term_im && *_rl_term_im)
2091 tputs (_rl_term_im, 1, _rl_output_character_function);
2093 /* If there is a special command for inserting characters, then
2094 use that first to open up the space. */
2095 if (_rl_term_ic && *_rl_term_ic)
2097 for (i = col; i--; )
2098 tputs (_rl_term_ic, 1, _rl_output_character_function);
2101 /* Print the text. */
2102 _rl_output_some_chars (string, count);
2104 /* If there is a string to turn off insert mode, we had best use
2106 if (_rl_term_ei && *_rl_term_ei)
2107 tputs (_rl_term_ei, 1, _rl_output_character_function);
2109 #endif /* __MSDOS__ || __MINGW32__ */
2112 /* Delete COUNT characters from the display line. */
2114 delete_chars (count)
2117 if (count > _rl_screenwidth) /* XXX */
2120 #if !defined (__MSDOS__) && !defined (__MINGW32__)
2121 if (_rl_term_DC && *_rl_term_DC)
2124 buffer = tgoto (_rl_term_DC, count, count);
2125 tputs (buffer, count, _rl_output_character_function);
2129 if (_rl_term_dc && *_rl_term_dc)
2131 tputs (_rl_term_dc, 1, _rl_output_character_function);
2133 #endif /* !__MSDOS__ && !__MINGW32__ */
2142 /* If the cursor is the only thing on an otherwise-blank last line,
2143 compensate so we don't print an extra CRLF. */
2144 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
2145 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
2150 _rl_move_vert (_rl_vis_botlin);
2151 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
2152 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
2156 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
2157 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
2158 _rl_clear_to_eol (0);
2159 putc (last_line[_rl_screenwidth - 1], rl_outstream);
2163 fflush (rl_outstream);
2167 /* Move to the start of the current line. */
2173 #if defined (__MSDOS__)
2174 putc ('\r', rl_outstream);
2176 tputs (_rl_term_cr, 1, _rl_output_character_function);
2182 /* Redraw the last line of a multi-line prompt that may possibly contain
2183 terminal escape sequences. Called with the cursor at column 0 of the
2184 line to draw the prompt on. */
2191 oldp = rl_display_prompt;
2194 rl_display_prompt = t;
2195 local_prompt = expand_prompt (t, &prompt_visible_length,
2196 &prompt_last_invisible,
2197 &prompt_invis_chars_first_line,
2198 &prompt_physical_chars);
2199 local_prompt_prefix = (char *)NULL;
2201 rl_forced_update_display ();
2203 rl_display_prompt = oldp;
2204 rl_restore_prompt();
2207 /* Redisplay the current line after a SIGWINCH is received. */
2209 _rl_redisplay_after_sigwinch ()
2213 /* Clear the current line and put the cursor at column 0. Make sure
2214 the right thing happens if we have wrapped to a new screen line. */
2217 #if defined (__MSDOS__)
2218 putc ('\r', rl_outstream);
2220 tputs (_rl_term_cr, 1, _rl_output_character_function);
2223 #if defined (__MSDOS__)
2224 space_to_eol (_rl_screenwidth);
2225 putc ('\r', rl_outstream);
2227 if (_rl_term_clreol)
2228 tputs (_rl_term_clreol, 1, _rl_output_character_function);
2231 space_to_eol (_rl_screenwidth);
2232 tputs (_rl_term_cr, 1, _rl_output_character_function);
2235 if (_rl_last_v_pos > 0)
2241 /* Redraw only the last line of a multi-line prompt. */
2242 t = strrchr (rl_display_prompt, '\n');
2244 redraw_prompt (++t);
2246 rl_forced_update_display ();
2250 _rl_clean_up_for_exit ()
2252 if (readline_echoing_p)
2254 _rl_move_vert (_rl_vis_botlin);
2256 fflush (rl_outstream);
2257 rl_restart_output (1, 0);
2262 _rl_erase_entire_line ()
2265 _rl_clear_to_eol (0);
2267 fflush (rl_outstream);
2270 /* return the `current display line' of the cursor -- the number of lines to
2271 move up to get to the first screen line of the current readline line. */
2273 _rl_current_display_line ()
2277 /* Find out whether or not there might be invisible characters in the
2279 if (rl_display_prompt == rl_prompt)
2280 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
2282 nleft = _rl_last_c_pos - _rl_screenwidth;
2285 ret = 1 + nleft / _rl_screenwidth;
2292 #if defined (HANDLE_MULTIBYTE)
2293 /* Calculate the number of screen columns occupied by STR from START to END.
2294 In the case of multibyte characters with stateful encoding, we have to
2295 scan from the beginning of the string to take the state into account. */
2297 _rl_col_width (str, start, end)
2303 int tmp, point, width, max;
2311 while (point < start)
2313 tmp = mbrlen (str + point, max, &ps);
2314 if (MB_INVALIDCH ((size_t)tmp))
2316 /* In this case, the bytes are invalid or too short to compose a
2317 multibyte character, so we assume that the first byte represents
2318 a single character. */
2322 /* Clear the state of the byte sequence, because in this case the
2323 effect of mbstate is undefined. */
2324 memset (&ps, 0, sizeof (mbstate_t));
2326 else if (MB_NULLWCH (tmp))
2327 break; /* Found '\0' */
2335 /* If START is not a byte that starts a character, then POINT will be
2336 greater than START. In this case, assume that (POINT - START) gives
2337 a byte count that is the number of columns of difference. */
2338 width = point - start;
2342 tmp = mbrtowc (&wc, str + point, max, &ps);
2343 if (MB_INVALIDCH ((size_t)tmp))
2345 /* In this case, the bytes are invalid or too short to compose a
2346 multibyte character, so we assume that the first byte represents
2347 a single character. */
2351 /* and assume that the byte occupies a single column. */
2354 /* Clear the state of the byte sequence, because in this case the
2355 effect of mbstate is undefined. */
2356 memset (&ps, 0, sizeof (mbstate_t));
2358 else if (MB_NULLWCH (tmp))
2359 break; /* Found '\0' */
2365 width += (tmp >= 0) ? tmp : 1;
2369 width += point - end;
2373 #endif /* HANDLE_MULTIBYTE */