3 /* display.c -- readline redisplay facility. */
5 /* Copyright (C) 1987-2004 Free Software Foundation, Inc.
7 This file is part of the GNU Readline Library, a library for
8 reading lines of text with interactive input and history editing.
10 The GNU Readline Library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2, or
13 (at your option) any later version.
15 The GNU Readline Library is distributed in the hope that it will be
16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 The GNU General Public License is often shipped with GNU software, and
21 is generally kept in a file called COPYING or LICENSE. If you do not
22 have a copy of the license, write to the Free Software Foundation,
23 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #define READLINE_LIBRARY
26 #if defined (HAVE_CONFIG_H)
30 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 #include "posixstat.h"
38 #if defined (HAVE_STDLIB_H)
41 # include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
46 /* System-specific feature definitions and include files. */
50 /* Termcap library stuff. */
53 /* Some standard library routines. */
57 #include "rlprivate.h"
60 #if !defined (strchr) && !defined (__STDC__)
61 extern char *strchr (), *strrchr ();
62 #endif /* !strchr && !__STDC__ */
64 #if defined (HACK_TERMCAP_MOTION)
65 extern char *_rl_term_forward_char;
68 static void update_line PARAMS((char *, char *, int, int, int, int));
69 static void space_to_eol PARAMS((int));
70 static void delete_chars PARAMS((int));
71 static void insert_some_chars PARAMS((char *, int, int));
72 static void cr PARAMS((void));
74 #if defined (HANDLE_MULTIBYTE)
75 static int _rl_col_width PARAMS((const char *, int, int));
76 static int *_rl_wrapped_line;
78 # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
81 static int *inv_lbreaks, *vis_lbreaks;
82 static int inv_lbsize, vis_lbsize;
84 /* Heuristic used to decide whether it is faster to move from CUR to NEW
85 by backing up or outputting a carriage return and moving forward. */
86 #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
88 /* **************************************************************** */
92 /* **************************************************************** */
94 /* This is the stuff that is hard for me. I never seem to write good
95 display routines in C. Let's see how I do this time. */
97 /* (PWP) Well... Good for a simple line updater, but totally ignores
98 the problems of input lines longer than the screen width.
100 update_line and the code that calls it makes a multiple line,
101 automatically wrapping line update. Careful attention needs
102 to be paid to the vertical position variables. */
104 /* Keep two buffers; one which reflects the current contents of the
105 screen, and the other to draw what we think the new contents should
106 be. Then compare the buffers, and make whatever changes to the
107 screen itself that we should. Finally, make the buffer that we
108 just drew into be the one which reflects the current contents of the
109 screen, and place the cursor where it belongs.
111 Commands that want to can fix the display themselves, and then let
112 this function know that the display has been fixed by setting the
113 RL_DISPLAY_FIXED variable. This is good for efficiency. */
115 /* Application-specific redisplay function. */
116 rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
118 /* Global variables declared here. */
119 /* What YOU turn on when you have handled all redisplay yourself. */
120 int rl_display_fixed = 0;
122 int _rl_suppress_redisplay = 0;
124 /* The stuff that gets printed out before the actual text of the line.
125 This is usually pointing to rl_prompt. */
126 char *rl_display_prompt = (char *)NULL;
128 /* Pseudo-global variables declared here. */
129 /* The visible cursor position. If you print some text, adjust this. */
130 int _rl_last_c_pos = 0;
131 int _rl_last_v_pos = 0;
133 /* Number of lines currently on screen minus 1. */
134 int _rl_vis_botlin = 0;
136 /* Variables used only in this file. */
137 /* The last left edge of text that was displayed. This is used when
138 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
139 static int last_lmargin;
141 /* The line display buffers. One is the line currently displayed on
142 the screen. The other is the line about to be displayed. */
143 static char *visible_line = (char *)NULL;
144 static char *invisible_line = (char *)NULL;
146 /* A buffer for `modeline' messages. */
147 static char msg_buf[128];
149 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
150 static int forced_display;
152 /* Default and initial buffer size. Can grow. */
153 static int line_size = 1024;
155 /* Variables to keep track of the expanded prompt string, which may
156 include invisible characters. */
158 static char *local_prompt, *local_prompt_prefix;
159 static int prompt_visible_length, prompt_prefix_length;
161 /* The number of invisible characters in the line currently being
162 displayed on the screen. */
163 static int visible_wrap_offset;
165 /* The number of invisible characters in the prompt string. Static so it
166 can be shared between rl_redisplay and update_line */
167 static int wrap_offset;
169 /* The index of the last invisible character in the prompt string. */
170 static int prompt_last_invisible;
172 /* The length (buffer offset) of the first line of the last (possibly
173 multi-line) buffer displayed on the screen. */
174 static int visible_first_line_len;
176 /* Number of invisible characters on the first physical line of the prompt.
177 Only valid when the number of physical characters in the prompt exceeds
178 (or is equal to) _rl_screenwidth. */
179 static int prompt_invis_chars_first_line;
181 static int prompt_last_screen_line;
183 static int prompt_physical_chars;
185 /* Expand the prompt string S and return the number of visible
186 characters in *LP, if LP is not null. This is currently more-or-less
187 a placeholder for expansion. LIP, if non-null is a place to store the
188 index of the last invisible character in the returned string. NIFLP,
189 if non-zero, is a place to store the number of invisible characters in
190 the first prompt line. The previous are used as byte counts -- indexes
191 into a character buffer. */
193 /* Current implementation:
194 \001 (^A) start non-visible characters
195 \002 (^B) end non-visible characters
196 all characters except \001 and \002 (following a \001) are copied to
197 the returned string; all characters except those between \001 and
198 \002 are assumed to be `visible'. */
201 expand_prompt (pmt, lp, lip, niflp, vlp)
203 int *lp, *lip, *niflp, *vlp;
206 int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
208 /* Short-circuit if we can. */
209 if ((MB_CUR_MAX <= 1 || rl_byte_oriented) && strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
211 r = savestring (pmt);
219 *vlp = lp ? *lp : strlen (r);
224 r = ret = (char *)xmalloc (l + 1);
226 invfl = 0; /* invisible chars in first line of prompt */
227 invflset = 0; /* we only want to set invfl once */
229 for (rl = ignoring = last = ninvis = physchars = 0, p = pmt; p && *p; p++)
231 /* This code strips the invisible character string markers
232 RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
233 if (*p == RL_PROMPT_START_IGNORE)
238 else if (ignoring && *p == RL_PROMPT_END_IGNORE)
246 #if defined (HANDLE_MULTIBYTE)
247 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
250 ind = _rl_find_next_mbchar (pmt, pind, 1, MB_FIND_NONZERO);
257 physchars += _rl_col_width (pmt, pind, ind);
260 ninvis += ind - pind;
261 p--; /* compensate for later increment */
269 rl++; /* visible length byte counter */
273 ninvis++; /* invisible chars byte counter */
276 if (invflset == 0 && rl >= _rl_screenwidth)
284 if (rl < _rl_screenwidth)
299 /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
300 PMT and return the rest of PMT. */
302 _rl_strip_prompt (pmt)
307 ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
312 * Expand the prompt string into the various display components, if
315 * local_prompt = expanded last line of string in rl_display_prompt
316 * (portion after the final newline)
317 * local_prompt_prefix = portion before last newline of rl_display_prompt,
318 * expanded via expand_prompt
319 * prompt_visible_length = number of visible characters in local_prompt
320 * prompt_prefix_length = number of visible characters in local_prompt_prefix
322 * This function is called once per call to readline(). It may also be
323 * called arbitrarily to expand the primary prompt.
325 * The return value is the number of visible characters on the last line
326 * of the (possibly multi-line) prompt.
329 rl_expand_prompt (prompt)
335 /* Clear out any saved values. */
337 FREE (local_prompt_prefix);
339 local_prompt = local_prompt_prefix = (char *)0;
340 prompt_last_invisible = prompt_visible_length = 0;
342 if (prompt == 0 || *prompt == 0)
345 p = strrchr (prompt, '\n');
348 /* The prompt is only one logical line, though it might wrap. */
349 local_prompt = expand_prompt (prompt, &prompt_visible_length,
350 &prompt_last_invisible,
351 &prompt_invis_chars_first_line,
352 &prompt_physical_chars);
353 local_prompt_prefix = (char *)0;
354 return (prompt_visible_length);
358 /* The prompt spans multiple lines. */
360 local_prompt = expand_prompt (p, &prompt_visible_length,
361 &prompt_last_invisible,
363 &prompt_physical_chars);
365 /* The portion of the prompt string up to and including the
366 final newline is now null-terminated. */
367 local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
369 &prompt_invis_chars_first_line,
372 return (prompt_prefix_length);
376 /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
377 arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
378 and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
379 increased. If the lines have already been allocated, this ensures that
380 they can hold at least MINSIZE characters. */
382 init_line_structures (minsize)
387 if (invisible_line == 0) /* initialize it */
389 if (line_size < minsize)
391 visible_line = (char *)xmalloc (line_size);
392 invisible_line = (char *)xmalloc (line_size);
394 else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
397 if (line_size < minsize)
399 visible_line = (char *)xrealloc (visible_line, line_size);
400 invisible_line = (char *)xrealloc (invisible_line, line_size);
403 for (n = minsize; n < line_size; n++)
406 invisible_line[n] = 1;
409 if (vis_lbreaks == 0)
411 /* should be enough. */
412 inv_lbsize = vis_lbsize = 256;
413 inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
414 vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
415 #if defined (HANDLE_MULTIBYTE)
416 _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
418 inv_lbreaks[0] = vis_lbreaks[0] = 0;
422 /* Basic redisplay algorithm. */
426 register int in, out, c, linenum, cursor_linenum;
428 int c_pos, inv_botlin, lb_botlin, lb_linenum;
429 int newlines, lpos, temp, modmark, n0, num;
430 char *prompt_this_line;
431 #if defined (HANDLE_MULTIBYTE)
436 int _rl_wrapped_multicolumn = 0;
439 if (!readline_echoing_p)
442 if (!rl_display_prompt)
443 rl_display_prompt = "";
445 if (invisible_line == 0)
447 init_line_structures (0);
451 /* Draw the line into the buffer. */
454 line = invisible_line;
455 out = inv_botlin = 0;
457 /* Mark the line as modified or not. We only do this for history
460 if (_rl_mark_modified_lines && current_history () && rl_undo_list)
467 /* If someone thought that the redisplay was handled, but the currently
468 visible line has a different modification state than the one about
469 to become visible, then correct the caller's misconception. */
470 if (visible_line[0] != invisible_line[0])
471 rl_display_fixed = 0;
473 /* If the prompt to be displayed is the `primary' readline prompt (the
474 one passed to readline()), use the values we have already expanded.
475 If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
476 number of non-visible characters in the prompt string. */
477 if (rl_display_prompt == rl_prompt || local_prompt)
479 int local_len = local_prompt ? strlen (local_prompt) : 0;
480 if (local_prompt_prefix && forced_display)
481 _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
485 temp = local_len + out + 2;
486 if (temp >= line_size)
488 line_size = (temp + 1024) - (temp % 1024);
489 visible_line = (char *)xrealloc (visible_line, line_size);
490 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
492 strncpy (line + out, local_prompt, local_len);
496 wrap_offset = local_len - prompt_visible_length;
501 prompt_this_line = strrchr (rl_display_prompt, '\n');
502 if (!prompt_this_line)
503 prompt_this_line = rl_display_prompt;
507 pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
510 _rl_output_some_chars (rl_display_prompt, pmtlen);
511 /* Make sure we are at column zero even after a newline,
512 regardless of the state of terminal output processing. */
513 if (pmtlen < 2 || prompt_this_line[-2] != '\r')
518 prompt_physical_chars = pmtlen = strlen (prompt_this_line);
519 temp = pmtlen + out + 2;
520 if (temp >= line_size)
522 line_size = (temp + 1024) - (temp % 1024);
523 visible_line = (char *)xrealloc (visible_line, line_size);
524 line = invisible_line = (char *)xrealloc (invisible_line, line_size);
526 strncpy (line + out, prompt_this_line, pmtlen);
529 wrap_offset = prompt_invis_chars_first_line = 0;
532 #define CHECK_INV_LBREAKS() \
534 if (newlines >= (inv_lbsize - 2)) \
537 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
541 #if defined (HANDLE_MULTIBYTE)
542 #define CHECK_LPOS() \
545 if (lpos >= _rl_screenwidth) \
547 if (newlines >= (inv_lbsize - 2)) \
550 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
551 _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
553 inv_lbreaks[++newlines] = out; \
554 _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
559 #define CHECK_LPOS() \
562 if (lpos >= _rl_screenwidth) \
564 if (newlines >= (inv_lbsize - 2)) \
567 inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
569 inv_lbreaks[++newlines] = out; \
575 /* inv_lbreaks[i] is where line i starts in the buffer. */
576 inv_lbreaks[newlines = 0] = 0;
578 lpos = out - wrap_offset;
580 lpos = prompt_physical_chars + modmark;
583 #if defined (HANDLE_MULTIBYTE)
584 memset (_rl_wrapped_line, 0, vis_lbsize);
588 /* prompt_invis_chars_first_line is the number of invisible characters in
589 the first physical line of the prompt.
590 wrap_offset - prompt_invis_chars_first_line is the number of invis
591 chars on the second line. */
593 /* what if lpos is already >= _rl_screenwidth before we start drawing the
594 contents of the command line? */
595 while (lpos >= _rl_screenwidth)
597 /* fix from Darin Johnson <darin@acuson.com> for prompt string with
598 invisible characters that is longer than the screen width. The
599 prompt_invis_chars_first_line variable could be made into an array
600 saying how many invisible characters there are per line, but that's
601 probably too much work for the benefit gained. How many people have
602 prompts that exceed two physical lines?
603 Additional logic fix from Edward Catmur <ed@catmur.co.uk> */
604 #if defined (HANDLE_MULTIBYTE)
606 temp = local_prompt ? strlen (local_prompt) : 0;
609 if (_rl_col_width (local_prompt, n0, num) > _rl_screenwidth)
611 num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY);
618 temp = ((newlines + 1) * _rl_screenwidth) +
619 #endif /* !HANDLE_MULTIBYTE */
620 ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line
621 : ((newlines == 1) ? wrap_offset : 0))
622 : ((newlines == 0) ? wrap_offset :0));
624 inv_lbreaks[++newlines] = temp;
625 #if defined (HANDLE_MULTIBYTE)
626 lpos -= _rl_col_width (local_prompt, n0, num);
628 lpos -= _rl_screenwidth;
632 prompt_last_screen_line = newlines;
634 /* Draw the rest of the line (after the prompt) into invisible_line, keeping
635 track of where the cursor is (c_pos), the number of the line containing
636 the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
637 It maintains an array of line breaks for display (inv_lbreaks).
638 This handles expanding tabs for display and displaying meta characters. */
640 #if defined (HANDLE_MULTIBYTE)
642 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
644 memset (&ps, 0, sizeof (mbstate_t));
645 wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
651 for (in = 0; in < rl_end; in++)
654 c = (unsigned char)rl_line_buffer[in];
656 #if defined (HANDLE_MULTIBYTE)
657 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
659 if (MB_INVALIDCH (wc_bytes))
661 /* Byte sequence is invalid or shortened. Assume that the
662 first byte represents a character. */
664 /* Assume that a character occupies a single column. */
666 memset (&ps, 0, sizeof (mbstate_t));
668 else if (MB_NULLWCH (wc_bytes))
669 break; /* Found '\0' */
673 wc_width = (temp >= 0) ? temp : 1;
678 if (out + 8 >= line_size) /* XXX - 8 for \t */
681 visible_line = (char *)xrealloc (visible_line, line_size);
682 invisible_line = (char *)xrealloc (invisible_line, line_size);
683 line = invisible_line;
689 lb_linenum = newlines;
692 #if defined (HANDLE_MULTIBYTE)
693 if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
698 if (_rl_output_meta_chars == 0)
700 sprintf (line + out, "\\%o", c);
702 if (lpos + 4 >= _rl_screenwidth)
704 temp = _rl_screenwidth - lpos;
705 CHECK_INV_LBREAKS ();
706 inv_lbreaks[++newlines] = out + temp;
720 #if defined (DISPLAY_TABS)
726 newout = (out | (int)7) + 1;
728 newout = out + 8 - lpos % 8;
731 if (lpos + temp >= _rl_screenwidth)
734 temp2 = _rl_screenwidth - lpos;
735 CHECK_INV_LBREAKS ();
736 inv_lbreaks[++newlines] = out + temp2;
749 else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
751 line[out++] = '\0'; /* XXX - sentinel */
752 CHECK_INV_LBREAKS ();
753 inv_lbreaks[++newlines] = out;
756 else if (CTRL_CHAR (c) || c == RUBOUT)
760 line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
765 #if defined (HANDLE_MULTIBYTE)
766 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
770 _rl_wrapped_multicolumn = 0;
772 if (_rl_screenwidth < lpos + wc_width)
773 for (i = lpos; i < _rl_screenwidth; i++)
775 /* The space will be removed in update_line() */
777 _rl_wrapped_multicolumn++;
783 lb_linenum = newlines;
785 for (i = in; i < in+wc_bytes; i++)
786 line[out++] = rl_line_buffer[i];
787 for (i = 0; i < wc_width; i++)
801 #if defined (HANDLE_MULTIBYTE)
802 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
805 wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
816 lb_linenum = newlines;
819 inv_botlin = lb_botlin = newlines;
820 CHECK_INV_LBREAKS ();
821 inv_lbreaks[newlines+1] = out;
822 cursor_linenum = lb_linenum;
824 /* C_POS == position in buffer where cursor should be placed.
825 CURSOR_LINENUM == line number where the cursor should be placed. */
827 /* PWP: now is when things get a bit hairy. The visible and invisible
828 line buffers are really multiple lines, which would wrap every
829 (screenwidth - 1) characters. Go through each in turn, finding
830 the changed region and updating it. The line order is top to bottom. */
832 /* If we can move the cursor up and down, then use multiple lines,
833 otherwise, let long lines display in a single terminal line, and
834 horizontally scroll it. */
836 if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
838 int nleft, pos, changed_screen_line;
840 if (!rl_display_fixed || forced_display)
844 /* If we have more than a screenful of material to display, then
845 only display a screenful. We should display the last screen,
847 if (out >= _rl_screenchars)
849 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
850 out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
852 out = _rl_screenchars - 1;
855 /* The first line is at character position 0 in the buffer. The
856 second and subsequent lines start at inv_lbreaks[N], offset by
857 OFFSET (which has already been calculated above). */
859 #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
860 #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
861 #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
862 #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
863 #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
864 #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
866 /* For each line in the buffer, do the updating display. */
867 for (linenum = 0; linenum <= inv_botlin; linenum++)
869 update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
870 VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
872 /* If this is the line with the prompt, we might need to
873 compensate for invisible characters in the new line. Do
874 this only if there is not more than one new line (which
875 implies that we completely overwrite the old visible line)
876 and the new line is shorter than the old. Make sure we are
877 at the end of the new line before clearing. */
879 inv_botlin == 0 && _rl_last_c_pos == out &&
880 (wrap_offset > visible_wrap_offset) &&
881 (_rl_last_c_pos < visible_first_line_len))
883 nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
885 _rl_clear_to_eol (nleft);
888 /* Since the new first line is now visible, save its length. */
890 visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
893 /* We may have deleted some lines. If so, clear the left over
894 blank ones at the bottom out. */
895 if (_rl_vis_botlin > inv_botlin)
898 for (; linenum <= _rl_vis_botlin; linenum++)
900 tt = VIS_CHARS (linenum);
901 _rl_move_vert (linenum);
902 _rl_move_cursor_relative (0, tt);
904 ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
907 _rl_vis_botlin = inv_botlin;
909 /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
910 different screen line during this redisplay. */
911 changed_screen_line = _rl_last_v_pos != cursor_linenum;
912 if (changed_screen_line)
914 _rl_move_vert (cursor_linenum);
915 /* If we moved up to the line with the prompt using _rl_term_up,
916 the physical cursor position on the screen stays the same,
917 but the buffer position needs to be adjusted to account
918 for invisible characters. */
919 if (cursor_linenum == 0 && wrap_offset)
920 _rl_last_c_pos += wrap_offset;
923 /* We have to reprint the prompt if it contains invisible
924 characters, since it's not generally OK to just reprint
925 the characters from the current cursor position. But we
926 only need to reprint it if the cursor is before the last
927 invisible character in the prompt string. */
928 nleft = prompt_visible_length + wrap_offset;
929 if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
930 _rl_last_c_pos <= prompt_last_invisible && local_prompt)
932 #if defined (__MSDOS__)
933 putc ('\r', rl_outstream);
936 tputs (_rl_term_cr, 1, _rl_output_character_function);
938 _rl_output_some_chars (local_prompt, nleft);
939 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
940 _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft);
942 _rl_last_c_pos = nleft;
945 /* Where on that line? And where does that line start
947 pos = inv_lbreaks[cursor_linenum];
948 /* nleft == number of characters in the line buffer between the
949 start of the line and the cursor position. */
952 /* Since _rl_backspace() doesn't know about invisible characters in the
953 prompt, and there's no good way to tell it, we compensate for
954 those characters here and call _rl_backspace() directly. */
955 if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
957 _rl_backspace (_rl_last_c_pos - nleft);
958 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
959 _rl_last_c_pos = _rl_col_width (&visible_line[pos], 0, nleft);
961 _rl_last_c_pos = nleft;
964 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
965 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
966 else if (nleft != _rl_last_c_pos)
967 _rl_move_cursor_relative (nleft, &invisible_line[pos]);
970 else /* Do horizontal scrolling. */
972 #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
973 int lmargin, ndisp, nleft, phys_c_pos, t;
975 /* Always at top line. */
978 /* Compute where in the buffer the displayed line should start. This
981 /* The number of characters that will be displayed before the cursor. */
982 ndisp = c_pos - wrap_offset;
983 nleft = prompt_visible_length + wrap_offset;
984 /* Where the new cursor position will be on the screen. This can be
985 longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
986 phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
987 t = _rl_screenwidth / 3;
989 /* If the number of characters had already exceeded the screenwidth,
990 last_lmargin will be > 0. */
992 /* If the number of characters to be displayed is more than the screen
993 width, compute the starting offset so that the cursor is about
994 two-thirds of the way across the screen. */
995 if (phys_c_pos > _rl_screenwidth - 2)
997 lmargin = c_pos - (2 * t);
1000 /* If the left margin would be in the middle of a prompt with
1001 invisible characters, don't display the prompt at all. */
1002 if (wrap_offset && lmargin > 0 && lmargin < nleft)
1005 else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
1007 else if (phys_c_pos < 1)
1009 /* If we are moving back towards the beginning of the line and
1010 the last margin is no longer correct, compute a new one. */
1011 lmargin = ((c_pos - 1) / t) * t; /* XXX */
1012 if (wrap_offset && lmargin > 0 && lmargin < nleft)
1016 lmargin = last_lmargin;
1018 /* If the first character on the screen isn't the first character
1019 in the display line, indicate this with a special character. */
1021 line[lmargin] = '<';
1023 /* If SCREENWIDTH characters starting at LMARGIN do not encompass
1024 the whole line, indicate that with a special character at the
1025 right edge of the screen. If LMARGIN is 0, we need to take the
1026 wrap offset into account. */
1027 t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
1031 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1034 update_line (&visible_line[last_lmargin],
1035 &invisible_line[lmargin],
1037 _rl_screenwidth + visible_wrap_offset,
1038 _rl_screenwidth + (lmargin ? 0 : wrap_offset),
1041 /* If the visible new line is shorter than the old, but the number
1042 of invisible characters is greater, and we are at the end of
1043 the new line, we need to clear to eol. */
1044 t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
1045 if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
1046 (_rl_last_c_pos == out) &&
1047 t < visible_first_line_len)
1049 nleft = _rl_screenwidth - t;
1050 _rl_clear_to_eol (nleft);
1052 visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
1053 if (visible_first_line_len > _rl_screenwidth)
1054 visible_first_line_len = _rl_screenwidth;
1056 _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1057 last_lmargin = lmargin;
1060 fflush (rl_outstream);
1062 /* Swap visible and non-visible lines. */
1064 char *vtemp = visible_line;
1065 int *itemp = vis_lbreaks, ntemp = vis_lbsize;
1067 visible_line = invisible_line;
1068 invisible_line = vtemp;
1070 vis_lbreaks = inv_lbreaks;
1071 inv_lbreaks = itemp;
1073 vis_lbsize = inv_lbsize;
1076 rl_display_fixed = 0;
1077 /* If we are displaying on a single line, and last_lmargin is > 0, we
1078 are not displaying any invisible characters, so set visible_wrap_offset
1080 if (_rl_horizontal_scroll_mode && last_lmargin)
1081 visible_wrap_offset = 0;
1083 visible_wrap_offset = wrap_offset;
1087 /* PWP: update_line() is based on finding the middle difference of each
1088 line on the screen; vis:
1090 /old first difference
1091 /beginning of line | /old last same /old EOL
1093 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1094 new: eddie> Oh, my little buggy says to me, as lurgid as
1096 \beginning of line | \new last same \new end of line
1097 \new first difference
1099 All are character pointers for the sake of speed. Special cases for
1100 no differences, as well as for end of line additions must be handled.
1102 Could be made even smarter, but this works well enough */
1104 update_line (old, new, current_line, omax, nmax, inv_botlin)
1105 register char *old, *new;
1106 int current_line, omax, nmax, inv_botlin;
1108 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1109 int temp, lendiff, wsatend, od, nd;
1110 int current_invis_chars;
1111 int col_lendiff, col_temp;
1112 #if defined (HANDLE_MULTIBYTE)
1113 mbstate_t ps_new, ps_old;
1114 int new_offset, old_offset, tmp;
1117 /* If we're at the right edge of a terminal that supports xn, we're
1118 ready to wrap around, so do so. This fixes problems with knowing
1119 the exact cursor position and cut-and-paste with certain terminal
1120 emulators. In this calculation, TEMP is the physical screen
1121 position of the cursor. */
1122 temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1123 if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
1124 && _rl_last_v_pos == current_line - 1)
1126 #if defined (HANDLE_MULTIBYTE)
1127 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1131 int tempwidth, bytes;
1134 /* This fixes only double-column characters, but if the wrapped
1135 character comsumes more than three columns, spaces will be
1136 inserted in the string buffer. */
1137 if (_rl_wrapped_line[current_line] > 0)
1138 _rl_clear_to_eol (_rl_wrapped_line[current_line]);
1140 memset (&ps, 0, sizeof (mbstate_t));
1141 ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
1142 if (MB_INVALIDCH (ret))
1147 else if (MB_NULLWCH (ret))
1150 tempwidth = wcwidth (wc);
1156 for (count = 0; count < bytes; count++)
1157 putc (new[count], rl_outstream);
1158 _rl_last_c_pos = tempwidth;
1160 memset (&ps, 0, sizeof (mbstate_t));
1161 ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1162 if (ret != 0 && bytes != 0)
1164 if (MB_INVALIDCH (ret))
1165 memmove (old+bytes, old+1, strlen (old+1));
1167 memmove (old+bytes, old+ret, strlen (old+ret));
1168 memcpy (old, new, bytes);
1173 putc (' ', rl_outstream);
1176 if (old[0] && new[0])
1184 putc (new[0], rl_outstream);
1186 putc (' ', rl_outstream);
1187 _rl_last_c_pos = 1; /* XXX */
1189 if (old[0] && new[0])
1195 /* Find first difference. */
1196 #if defined (HANDLE_MULTIBYTE)
1197 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1199 /* See if the old line is a subset of the new line, so that the
1200 only change is adding characters. */
1201 temp = (omax < nmax) ? omax : nmax;
1202 if (memcmp (old, new, temp) == 0)
1209 memset (&ps_new, 0, sizeof(mbstate_t));
1210 memset (&ps_old, 0, sizeof(mbstate_t));
1212 if (omax == nmax && STREQN (new, old, omax))
1219 new_offset = old_offset = 0;
1220 for (ofd = old, nfd = new;
1221 (ofd - old < omax) && *ofd &&
1222 _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
1224 old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
1225 new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
1226 ofd = old + old_offset;
1227 nfd = new + new_offset;
1234 for (ofd = old, nfd = new;
1235 (ofd - old < omax) && *ofd && (*ofd == *nfd);
1239 /* Move to the end of the screen line. ND and OD are used to keep track
1240 of the distance between ne and new and oe and old, respectively, to
1241 move a subtraction out of each loop. */
1242 for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
1243 for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
1245 /* If no difference, continue to next line. */
1246 if (ofd == oe && nfd == ne)
1249 wsatend = 1; /* flag for trailing whitespace */
1251 #if defined (HANDLE_MULTIBYTE)
1252 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1254 ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
1255 nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
1256 while ((ols > ofd) && (nls > nfd))
1258 memset (&ps_old, 0, sizeof (mbstate_t));
1259 memset (&ps_new, 0, sizeof (mbstate_t));
1262 /* On advice from jir@yamato.ibm.com */
1263 _rl_adjust_point (old, ols - old, &ps_old);
1264 _rl_adjust_point (new, nls - new, &ps_new);
1267 if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
1273 ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
1274 nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
1279 #endif /* HANDLE_MULTIBYTE */
1280 ols = oe - 1; /* find last same */
1282 while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
1289 #if defined (HANDLE_MULTIBYTE)
1298 #if defined (HANDLE_MULTIBYTE)
1299 /* This may not work for stateful encoding, but who cares? To handle
1300 stateful encoding properly, we have to scan each string from the
1301 beginning and compare. */
1302 else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
1304 else if (*ols != *nls)
1307 if (*ols) /* don't step past the NUL */
1309 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1310 ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1316 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1317 nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
1323 /* count of invisible characters in the current invisible line. */
1324 current_invis_chars = W_OFFSET (current_line, wrap_offset);
1325 if (_rl_last_v_pos != current_line)
1327 _rl_move_vert (current_line);
1328 if (current_line == 0 && visible_wrap_offset)
1329 _rl_last_c_pos += visible_wrap_offset;
1332 /* If this is the first line and there are invisible characters in the
1333 prompt string, and the prompt string has not changed, and the current
1334 cursor position is before the last invisible character in the prompt,
1335 and the index of the character to move to is past the end of the prompt
1336 string, then redraw the entire prompt string. We can only do this
1337 reliably if the terminal supports a `cr' capability.
1339 This is not an efficiency hack -- there is a problem with redrawing
1340 portions of the prompt string if they contain terminal escape
1341 sequences (like drawing the `unbold' sequence without a corresponding
1342 `bold') that manifests itself on certain terminals. */
1344 lendiff = local_prompt ? strlen (local_prompt) : 0;
1345 od = ofd - old; /* index of first difference in visible line */
1346 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1347 _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
1348 od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
1350 #if defined (__MSDOS__)
1351 putc ('\r', rl_outstream);
1353 tputs (_rl_term_cr, 1, _rl_output_character_function);
1355 _rl_output_some_chars (local_prompt, lendiff);
1356 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1357 _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
1359 _rl_last_c_pos = lendiff;
1362 _rl_move_cursor_relative (od, old);
1364 /* if (len (new) > len (old))
1365 lendiff == difference in buffer
1366 col_lendiff == difference on screen
1367 When not using multibyte characters, these are equal */
1368 lendiff = (nls - nfd) - (ols - ofd);
1369 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1370 col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
1372 col_lendiff = lendiff;
1374 /* If we are changing the number of invisible characters in a line, and
1375 the spot of first difference is before the end of the invisible chars,
1376 lendiff needs to be adjusted. */
1377 if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1378 current_invis_chars != visible_wrap_offset)
1380 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1382 lendiff += visible_wrap_offset - current_invis_chars;
1383 col_lendiff += visible_wrap_offset - current_invis_chars;
1387 lendiff += visible_wrap_offset - current_invis_chars;
1388 col_lendiff = lendiff;
1392 /* Insert (diff (len (old), len (new)) ch. */
1394 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1395 col_temp = _rl_col_width (new, nfd - new, ne - new);
1399 if (col_lendiff > 0) /* XXX - was lendiff */
1401 /* Non-zero if we're increasing the number of lines. */
1402 int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
1403 /* Sometimes it is cheaper to print the characters rather than
1404 use the terminal's capabilities. If we're growing the number
1405 of lines, make sure we actually cause the new line to wrap
1406 around on auto-wrapping terminals. */
1407 if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
1409 /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
1410 _rl_horizontal_scroll_mode == 1, inserting the characters with
1411 _rl_term_IC or _rl_term_ic will screw up the screen because of the
1412 invisible characters. We need to just draw them. */
1413 if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
1414 lendiff <= prompt_visible_length || !current_invis_chars))
1416 insert_some_chars (nfd, lendiff, col_lendiff);
1417 _rl_last_c_pos += col_lendiff;
1419 else if (*ols == 0 && lendiff > 0)
1421 /* At the end of a line the characters do not have to
1422 be "inserted". They can just be placed on the screen. */
1423 /* However, this screws up the rest of this block, which
1424 assumes you've done the insert because you can. */
1425 _rl_output_some_chars (nfd, lendiff);
1426 _rl_last_c_pos += col_lendiff;
1430 /* We have horizontal scrolling and we are not inserting at
1431 the end. We have invisible characters in this line. This
1432 is a dumb update. */
1433 _rl_output_some_chars (nfd, temp);
1434 _rl_last_c_pos += col_temp;
1437 /* Copy (new) chars to screen from first diff to last match. */
1439 if ((temp - lendiff) > 0)
1441 _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1443 /* XXX -- this bears closer inspection. Fixes a redisplay bug
1444 reported against bash-3.0-alpha by Andreas Schwab involving
1445 multibyte characters and prompt strings with invisible
1446 characters, but was previously disabled. */
1447 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
1449 _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff);
1455 /* cannot insert chars, write to EOL */
1456 _rl_output_some_chars (nfd, temp);
1457 _rl_last_c_pos += col_temp;
1460 else /* Delete characters from line. */
1462 /* If possible and inexpensive to use terminal deletion, then do so. */
1463 if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
1465 /* If all we're doing is erasing the invisible characters in the
1466 prompt string, don't bother. It screws up the assumptions
1467 about what's on the screen. */
1468 if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
1469 -lendiff == visible_wrap_offset)
1473 delete_chars (-col_lendiff); /* delete (diff) characters */
1475 /* Copy (new) chars to screen from first diff to last match */
1479 _rl_output_some_chars (nfd, temp);
1480 _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
1483 /* Otherwise, print over the existing material. */
1488 _rl_output_some_chars (nfd, temp);
1489 _rl_last_c_pos += col_temp;
1491 lendiff = (oe - old) - (ne - new);
1492 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1493 col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
1495 col_lendiff = lendiff;
1499 if (_rl_term_autowrap && current_line < inv_botlin)
1500 space_to_eol (col_lendiff);
1502 _rl_clear_to_eol (col_lendiff);
1508 /* Tell the update routines that we have moved onto a new (empty) line. */
1513 visible_line[0] = '\0';
1515 _rl_last_c_pos = _rl_last_v_pos = 0;
1516 _rl_vis_botlin = last_lmargin = 0;
1518 vis_lbreaks[0] = vis_lbreaks[1] = 0;
1519 visible_wrap_offset = 0;
1523 /* Tell the update routines that we have moved onto a new line with the
1524 prompt already displayed. Code originally from the version of readline
1525 distributed with CLISP. rl_expand_prompt must have already been called
1526 (explicitly or implicitly). This still doesn't work exactly right. */
1528 rl_on_new_line_with_prompt ()
1530 int prompt_size, i, l, real_screenwidth, newlines;
1531 char *prompt_last_line, *lprompt;
1533 /* Initialize visible_line and invisible_line to ensure that they can hold
1534 the already-displayed prompt. */
1535 prompt_size = strlen (rl_prompt) + 1;
1536 init_line_structures (prompt_size);
1538 /* Make sure the line structures hold the already-displayed prompt for
1540 lprompt = local_prompt ? local_prompt : rl_prompt;
1541 strcpy (visible_line, lprompt);
1542 strcpy (invisible_line, lprompt);
1544 /* If the prompt contains newlines, take the last tail. */
1545 prompt_last_line = strrchr (rl_prompt, '\n');
1546 if (!prompt_last_line)
1547 prompt_last_line = rl_prompt;
1549 l = strlen (prompt_last_line);
1550 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1551 _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);
1555 /* Dissect prompt_last_line into screen lines. Note that here we have
1556 to use the real screenwidth. Readline's notion of screenwidth might be
1557 one less, see terminal.c. */
1558 real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
1559 _rl_last_v_pos = l / real_screenwidth;
1560 /* If the prompt length is a multiple of real_screenwidth, we don't know
1561 whether the cursor is at the end of the last line, or already at the
1562 beginning of the next line. Output a newline just to be safe. */
1563 if (l > 0 && (l % real_screenwidth) == 0)
1564 _rl_output_some_chars ("\n", 1);
1567 newlines = 0; i = 0;
1570 _rl_vis_botlin = newlines;
1571 vis_lbreaks[newlines++] = i;
1572 i += real_screenwidth;
1574 vis_lbreaks[newlines] = l;
1575 visible_wrap_offset = 0;
1577 rl_display_prompt = rl_prompt; /* XXX - make sure it's set */
1582 /* Actually update the display, period. */
1584 rl_forced_update_display ()
1588 register char *temp = visible_line;
1595 (*rl_redisplay_function) ();
1599 /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
1600 DATA is the contents of the screen line of interest; i.e., where
1601 the movement is being done. */
1603 _rl_move_cursor_relative (new, data)
1609 /* If we don't have to do anything, then return. */
1610 #if defined (HANDLE_MULTIBYTE)
1611 /* If we have multibyte characters, NEW is indexed by the buffer point in
1612 a multibyte string, but _rl_last_c_pos is the display position. In
1613 this case, NEW's display position is not obvious and must be
1615 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1617 if (_rl_last_c_pos == new)
1620 else if (_rl_last_c_pos == _rl_col_width (data, 0, new))
1623 if (_rl_last_c_pos == new) return;
1626 /* It may be faster to output a CR, and then move forwards instead
1627 of moving backwards. */
1628 /* i == current physical cursor position. */
1629 i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1630 if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1631 (_rl_term_autowrap && i == _rl_screenwidth))
1633 #if defined (__MSDOS__)
1634 putc ('\r', rl_outstream);
1636 tputs (_rl_term_cr, 1, _rl_output_character_function);
1637 #endif /* !__MSDOS__ */
1641 if (_rl_last_c_pos < new)
1643 /* Move the cursor forward. We do it by printing the command
1644 to move the cursor forward if there is one, else print that
1645 portion of the output buffer again. Which is cheaper? */
1647 /* The above comment is left here for posterity. It is faster
1648 to print one character (non-control) than to print a control
1649 sequence telling the terminal to move forward one character.
1650 That kind of control is for people who don't know what the
1651 data is underneath the cursor. */
1652 #if defined (HACK_TERMCAP_MOTION)
1653 if (_rl_term_forward_char)
1655 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1658 width = _rl_col_width (data, _rl_last_c_pos, new);
1659 for (i = 0; i < width; i++)
1660 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1664 for (i = _rl_last_c_pos; i < new; i++)
1665 tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1668 else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1670 tputs (_rl_term_cr, 1, _rl_output_character_function);
1671 for (i = 0; i < new; i++)
1672 putc (data[i], rl_outstream);
1675 for (i = _rl_last_c_pos; i < new; i++)
1676 putc (data[i], rl_outstream);
1678 #else /* !HACK_TERMCAP_MOTION */
1680 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1682 tputs (_rl_term_cr, 1, _rl_output_character_function);
1683 for (i = 0; i < new; i++)
1684 putc (data[i], rl_outstream);
1687 for (i = _rl_last_c_pos; i < new; i++)
1688 putc (data[i], rl_outstream);
1690 #endif /* !HACK_TERMCAP_MOTION */
1693 #if defined (HANDLE_MULTIBYTE)
1694 /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1695 The byte length of the string is probably bigger than the column width
1696 of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1697 display point is less than _rl_last_c_pos. */
1698 else if (_rl_last_c_pos >= new)
1700 else if (_rl_last_c_pos > new)
1703 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1704 _rl_backspace (_rl_last_c_pos - _rl_col_width (data, 0, new));
1706 _rl_backspace (_rl_last_c_pos - new);
1709 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1710 _rl_last_c_pos = _rl_col_width (data, 0, new);
1712 _rl_last_c_pos = new;
1715 /* PWP: move the cursor up or down. */
1720 register int delta, i;
1722 if (_rl_last_v_pos == to || to > _rl_screenheight)
1725 if ((delta = to - _rl_last_v_pos) > 0)
1727 for (i = 0; i < delta; i++)
1728 putc ('\n', rl_outstream);
1729 #if defined (__MSDOS__)
1730 putc ('\r', rl_outstream);
1732 tputs (_rl_term_cr, 1, _rl_output_character_function);
1738 if (_rl_term_up && *_rl_term_up)
1739 for (i = 0; i < -delta; i++)
1740 tputs (_rl_term_up, 1, _rl_output_character_function);
1743 _rl_last_v_pos = to; /* Now TO is here */
1746 /* Physically print C on rl_outstream. This is for functions which know
1747 how to optimize the display. Return the number of characters output. */
1753 if (META_CHAR (c) && (_rl_output_meta_chars == 0))
1755 fprintf (rl_outstream, "M-");
1760 #if defined (DISPLAY_TABS)
1761 if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
1763 if (CTRL_CHAR (c) || c == RUBOUT)
1764 #endif /* !DISPLAY_TABS */
1766 fprintf (rl_outstream, "C-");
1768 c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
1771 putc (c, rl_outstream);
1772 fflush (rl_outstream);
1777 rl_character_len (c, pos)
1778 register int c, pos;
1782 uc = (unsigned char)c;
1785 return ((_rl_output_meta_chars == 0) ? 4 : 1);
1789 #if defined (DISPLAY_TABS)
1790 return (((pos | 7) + 1) - pos);
1793 #endif /* !DISPLAY_TABS */
1796 if (CTRL_CHAR (c) || c == RUBOUT)
1799 return ((ISPRINT (uc)) ? 1 : 2);
1802 /* How to print things in the "echo-area". The prompt is treated as a
1805 #if defined (USE_VARARGS)
1807 #if defined (PREFER_STDARG)
1808 rl_message (const char *format, ...)
1810 rl_message (va_alist)
1815 #if defined (PREFER_VARARGS)
1819 #if defined (PREFER_STDARG)
1820 va_start (args, format);
1823 format = va_arg (args, char *);
1826 #if defined (HAVE_VSNPRINTF)
1827 vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
1829 vsprintf (msg_buf, format, args);
1830 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1834 rl_display_prompt = msg_buf;
1835 (*rl_redisplay_function) ();
1838 #else /* !USE_VARARGS */
1840 rl_message (format, arg1, arg2)
1843 sprintf (msg_buf, format, arg1, arg2);
1844 msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1845 rl_display_prompt = msg_buf;
1846 (*rl_redisplay_function) ();
1849 #endif /* !USE_VARARGS */
1851 /* How to clear things from the "echo-area". */
1855 rl_display_prompt = rl_prompt;
1856 (*rl_redisplay_function) ();
1861 rl_reset_line_state ()
1865 rl_display_prompt = rl_prompt ? rl_prompt : "";
1870 /* These are getting numerous enough that it's time to create a struct. */
1872 static char *saved_local_prompt;
1873 static char *saved_local_prefix;
1874 static int saved_last_invisible;
1875 static int saved_visible_length;
1876 static int saved_invis_chars_first_line;
1877 static int saved_physical_chars;
1882 saved_local_prompt = local_prompt;
1883 saved_local_prefix = local_prompt_prefix;
1884 saved_last_invisible = prompt_last_invisible;
1885 saved_visible_length = prompt_visible_length;
1886 saved_invis_chars_first_line = prompt_invis_chars_first_line;
1887 saved_physical_chars = prompt_physical_chars;
1889 local_prompt = local_prompt_prefix = (char *)0;
1890 prompt_last_invisible = prompt_visible_length = 0;
1891 prompt_invis_chars_first_line = prompt_physical_chars = 0;
1895 rl_restore_prompt ()
1897 FREE (local_prompt);
1898 FREE (local_prompt_prefix);
1900 local_prompt = saved_local_prompt;
1901 local_prompt_prefix = saved_local_prefix;
1902 prompt_last_invisible = saved_last_invisible;
1903 prompt_visible_length = saved_visible_length;
1904 prompt_invis_chars_first_line = saved_invis_chars_first_line;
1905 prompt_physical_chars = saved_physical_chars;
1909 _rl_make_prompt_for_search (pchar)
1917 if (saved_local_prompt == 0)
1919 len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1920 pmt = (char *)xmalloc (len + 2);
1922 strcpy (pmt, rl_prompt);
1928 len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1929 pmt = (char *)xmalloc (len + 2);
1931 strcpy (pmt, saved_local_prompt);
1934 local_prompt = savestring (pmt);
1935 prompt_last_invisible = saved_last_invisible;
1936 prompt_visible_length = saved_visible_length + 1;
1942 /* Quick redisplay hack when erasing characters at the end of the line. */
1944 _rl_erase_at_end_of_line (l)
1950 for (i = 0; i < l; i++)
1951 putc (' ', rl_outstream);
1953 for (i = 0; i < l; i++)
1954 visible_line[--_rl_last_c_pos] = '\0';
1958 /* Clear to the end of the line. COUNT is the minimum
1959 number of character spaces to clear, */
1961 _rl_clear_to_eol (count)
1964 if (_rl_term_clreol)
1965 tputs (_rl_term_clreol, 1, _rl_output_character_function);
1967 space_to_eol (count);
1970 /* Clear to the end of the line using spaces. COUNT is the minimum
1971 number of character spaces to clear, */
1973 space_to_eol (count)
1978 for (i = 0; i < count; i++)
1979 putc (' ', rl_outstream);
1981 _rl_last_c_pos += count;
1987 if (_rl_term_clrpag)
1988 tputs (_rl_term_clrpag, 1, _rl_output_character_function);
1993 /* Insert COUNT characters from STRING to the output stream at column COL. */
1995 insert_some_chars (string, count, col)
2000 if (MB_CUR_MAX == 1 || rl_byte_oriented)
2002 fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
2004 /* If IC is defined, then we do not have to "enter" insert mode. */
2009 buffer = tgoto (_rl_term_IC, 0, col);
2010 tputs (buffer, 1, _rl_output_character_function);
2011 _rl_output_some_chars (string, count);
2017 /* If we have to turn on insert-mode, then do so. */
2018 if (_rl_term_im && *_rl_term_im)
2019 tputs (_rl_term_im, 1, _rl_output_character_function);
2021 /* If there is a special command for inserting characters, then
2022 use that first to open up the space. */
2023 if (_rl_term_ic && *_rl_term_ic)
2025 for (i = col; i--; )
2026 tputs (_rl_term_ic, 1, _rl_output_character_function);
2029 /* Print the text. */
2030 _rl_output_some_chars (string, count);
2032 /* If there is a string to turn off insert mode, we had best use
2034 if (_rl_term_ei && *_rl_term_ei)
2035 tputs (_rl_term_ei, 1, _rl_output_character_function);
2039 /* Delete COUNT characters from the display line. */
2041 delete_chars (count)
2044 if (count > _rl_screenwidth) /* XXX */
2047 if (_rl_term_DC && *_rl_term_DC)
2050 buffer = tgoto (_rl_term_DC, count, count);
2051 tputs (buffer, count, _rl_output_character_function);
2055 if (_rl_term_dc && *_rl_term_dc)
2057 tputs (_rl_term_dc, 1, _rl_output_character_function);
2067 /* If the cursor is the only thing on an otherwise-blank last line,
2068 compensate so we don't print an extra CRLF. */
2069 if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
2070 visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
2075 _rl_move_vert (_rl_vis_botlin);
2076 /* If we've wrapped lines, remove the final xterm line-wrap flag. */
2077 if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
2081 last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
2082 _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
2083 _rl_clear_to_eol (0);
2084 putc (last_line[_rl_screenwidth - 1], rl_outstream);
2088 fflush (rl_outstream);
2092 /* Move to the start of the current line. */
2098 #if defined (__MSDOS__)
2099 putc ('\r', rl_outstream);
2101 tputs (_rl_term_cr, 1, _rl_output_character_function);
2107 /* Redraw the last line of a multi-line prompt that may possibly contain
2108 terminal escape sequences. Called with the cursor at column 0 of the
2109 line to draw the prompt on. */
2114 char *oldp, *oldl, *oldlprefix;
2115 int oldlen, oldlast, oldplen, oldninvis, oldphyschars;
2117 /* Geez, I should make this a struct. */
2118 oldp = rl_display_prompt;
2119 oldl = local_prompt;
2120 oldlprefix = local_prompt_prefix;
2121 oldlen = prompt_visible_length;
2122 oldplen = prompt_prefix_length;
2123 oldlast = prompt_last_invisible;
2124 oldninvis = prompt_invis_chars_first_line;
2125 oldphyschars = prompt_physical_chars;
2127 rl_display_prompt = t;
2128 local_prompt = expand_prompt (t, &prompt_visible_length,
2129 &prompt_last_invisible,
2130 &prompt_invis_chars_first_line,
2131 &prompt_physical_chars);
2132 local_prompt_prefix = (char *)NULL;
2133 rl_forced_update_display ();
2135 rl_display_prompt = oldp;
2136 local_prompt = oldl;
2137 local_prompt_prefix = oldlprefix;
2138 prompt_visible_length = oldlen;
2139 prompt_prefix_length = oldplen;
2140 prompt_last_invisible = oldlast;
2141 prompt_invis_chars_first_line = oldninvis;
2142 prompt_physical_chars = oldphyschars;
2145 /* Redisplay the current line after a SIGWINCH is received. */
2147 _rl_redisplay_after_sigwinch ()
2151 /* Clear the current line and put the cursor at column 0. Make sure
2152 the right thing happens if we have wrapped to a new screen line. */
2155 #if defined (__MSDOS__)
2156 putc ('\r', rl_outstream);
2158 tputs (_rl_term_cr, 1, _rl_output_character_function);
2161 #if defined (__MSDOS__)
2162 space_to_eol (_rl_screenwidth);
2163 putc ('\r', rl_outstream);
2165 if (_rl_term_clreol)
2166 tputs (_rl_term_clreol, 1, _rl_output_character_function);
2169 space_to_eol (_rl_screenwidth);
2170 tputs (_rl_term_cr, 1, _rl_output_character_function);
2173 if (_rl_last_v_pos > 0)
2179 /* Redraw only the last line of a multi-line prompt. */
2180 t = strrchr (rl_display_prompt, '\n');
2182 redraw_prompt (++t);
2184 rl_forced_update_display ();
2188 _rl_clean_up_for_exit ()
2190 if (readline_echoing_p)
2192 _rl_move_vert (_rl_vis_botlin);
2194 fflush (rl_outstream);
2195 rl_restart_output (1, 0);
2200 _rl_erase_entire_line ()
2203 _rl_clear_to_eol (0);
2205 fflush (rl_outstream);
2208 /* return the `current display line' of the cursor -- the number of lines to
2209 move up to get to the first screen line of the current readline line. */
2211 _rl_current_display_line ()
2215 /* Find out whether or not there might be invisible characters in the
2217 if (rl_display_prompt == rl_prompt)
2218 nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
2220 nleft = _rl_last_c_pos - _rl_screenwidth;
2223 ret = 1 + nleft / _rl_screenwidth;
2230 #if defined (HANDLE_MULTIBYTE)
2231 /* Calculate the number of screen columns occupied by STR from START to END.
2232 In the case of multibyte characters with stateful encoding, we have to
2233 scan from the beginning of the string to take the state into account. */
2235 _rl_col_width (str, start, end)
2241 int tmp, point, width, max;
2249 while (point < start)
2251 tmp = mbrlen (str + point, max, &ps);
2252 if (MB_INVALIDCH ((size_t)tmp))
2254 /* In this case, the bytes are invalid or too short to compose a
2255 multibyte character, so we assume that the first byte represents
2256 a single character. */
2260 /* Clear the state of the byte sequence, because in this case the
2261 effect of mbstate is undefined. */
2262 memset (&ps, 0, sizeof (mbstate_t));
2264 else if (MB_NULLWCH (tmp))
2265 break; /* Found '\0' */
2273 /* If START is not a byte that starts a character, then POINT will be
2274 greater than START. In this case, assume that (POINT - START) gives
2275 a byte count that is the number of columns of difference. */
2276 width = point - start;
2280 tmp = mbrtowc (&wc, str + point, max, &ps);
2281 if (MB_INVALIDCH ((size_t)tmp))
2283 /* In this case, the bytes are invalid or too short to compose a
2284 multibyte character, so we assume that the first byte represents
2285 a single character. */
2289 /* and assume that the byte occupies a single column. */
2292 /* Clear the state of the byte sequence, because in this case the
2293 effect of mbstate is undefined. */
2294 memset (&ps, 0, sizeof (mbstate_t));
2296 else if (MB_NULLWCH (tmp))
2297 break; /* Found '\0' */
2303 width += (tmp >= 0) ? tmp : 1;
2307 width += point - end;
2311 #endif /* HANDLE_MULTIBYTE */