11acd27e7Smillert /* display.c -- readline redisplay facility. */
21acd27e7Smillert
31acd27e7Smillert /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
41acd27e7Smillert
51acd27e7Smillert This file is part of the GNU Readline Library, a library for
61acd27e7Smillert reading lines of text with interactive input and history editing.
71acd27e7Smillert
81acd27e7Smillert The GNU Readline Library is free software; you can redistribute it
91acd27e7Smillert and/or modify it under the terms of the GNU General Public License
101acd27e7Smillert as published by the Free Software Foundation; either version 2, or
111acd27e7Smillert (at your option) any later version.
121acd27e7Smillert
131acd27e7Smillert The GNU Readline Library is distributed in the hope that it will be
141acd27e7Smillert useful, but WITHOUT ANY WARRANTY; without even the implied warranty
151acd27e7Smillert of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
161acd27e7Smillert GNU General Public License for more details.
171acd27e7Smillert
181acd27e7Smillert The GNU General Public License is often shipped with GNU software, and
191acd27e7Smillert is generally kept in a file called COPYING or LICENSE. If you do not
201acd27e7Smillert have a copy of the license, write to the Free Software Foundation,
211acd27e7Smillert 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
221acd27e7Smillert #define READLINE_LIBRARY
231acd27e7Smillert
241acd27e7Smillert #if defined (HAVE_CONFIG_H)
251acd27e7Smillert # include <config.h>
261acd27e7Smillert #endif
271acd27e7Smillert
281acd27e7Smillert #include <sys/types.h>
291acd27e7Smillert
301acd27e7Smillert #if defined (HAVE_UNISTD_H)
311acd27e7Smillert # include <unistd.h>
321acd27e7Smillert #endif /* HAVE_UNISTD_H */
331acd27e7Smillert
341acd27e7Smillert #include "posixstat.h"
351acd27e7Smillert
361acd27e7Smillert #if defined (HAVE_STDLIB_H)
371acd27e7Smillert # include <stdlib.h>
381acd27e7Smillert #else
391acd27e7Smillert # include "ansi_stdlib.h"
401acd27e7Smillert #endif /* HAVE_STDLIB_H */
411acd27e7Smillert
421acd27e7Smillert #include <stdio.h>
431acd27e7Smillert
441acd27e7Smillert /* System-specific feature definitions and include files. */
451acd27e7Smillert #include "rldefs.h"
46*af70c2dfSkettenis #include "rlmbutil.h"
471acd27e7Smillert
481acd27e7Smillert /* Termcap library stuff. */
491acd27e7Smillert #include "tcap.h"
501acd27e7Smillert
511acd27e7Smillert /* Some standard library routines. */
521acd27e7Smillert #include "readline.h"
531acd27e7Smillert #include "history.h"
541acd27e7Smillert
551acd27e7Smillert #include "rlprivate.h"
561acd27e7Smillert #include "xmalloc.h"
571acd27e7Smillert
581acd27e7Smillert #if !defined (strchr) && !defined (__STDC__)
591acd27e7Smillert extern char *strchr (), *strrchr ();
601acd27e7Smillert #endif /* !strchr && !__STDC__ */
611acd27e7Smillert
621acd27e7Smillert #if defined (HACK_TERMCAP_MOTION)
63*af70c2dfSkettenis extern char *_rl_term_forward_char;
641acd27e7Smillert #endif
651acd27e7Smillert
66*af70c2dfSkettenis static void update_line PARAMS((char *, char *, int, int, int, int));
67*af70c2dfSkettenis static void space_to_eol PARAMS((int));
68*af70c2dfSkettenis static void delete_chars PARAMS((int));
69*af70c2dfSkettenis static void insert_some_chars PARAMS((char *, int, int));
70*af70c2dfSkettenis static void cr PARAMS((void));
71*af70c2dfSkettenis
72*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
73*af70c2dfSkettenis static int _rl_col_width PARAMS((const char *, int, int));
74*af70c2dfSkettenis static int *_rl_wrapped_line;
75*af70c2dfSkettenis #else
76*af70c2dfSkettenis # define _rl_col_width(l, s, e) (((e) <= (s)) ? 0 : (e) - (s))
77*af70c2dfSkettenis #endif
781acd27e7Smillert
791acd27e7Smillert static int *inv_lbreaks, *vis_lbreaks;
801acd27e7Smillert static int inv_lbsize, vis_lbsize;
811acd27e7Smillert
821acd27e7Smillert /* Heuristic used to decide whether it is faster to move from CUR to NEW
831acd27e7Smillert by backing up or outputting a carriage return and moving forward. */
841acd27e7Smillert #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
851acd27e7Smillert
861acd27e7Smillert /* **************************************************************** */
871acd27e7Smillert /* */
881acd27e7Smillert /* Display stuff */
891acd27e7Smillert /* */
901acd27e7Smillert /* **************************************************************** */
911acd27e7Smillert
921acd27e7Smillert /* This is the stuff that is hard for me. I never seem to write good
931acd27e7Smillert display routines in C. Let's see how I do this time. */
941acd27e7Smillert
951acd27e7Smillert /* (PWP) Well... Good for a simple line updater, but totally ignores
961acd27e7Smillert the problems of input lines longer than the screen width.
971acd27e7Smillert
981acd27e7Smillert update_line and the code that calls it makes a multiple line,
991acd27e7Smillert automatically wrapping line update. Careful attention needs
1001acd27e7Smillert to be paid to the vertical position variables. */
1011acd27e7Smillert
1021acd27e7Smillert /* Keep two buffers; one which reflects the current contents of the
1031acd27e7Smillert screen, and the other to draw what we think the new contents should
1041acd27e7Smillert be. Then compare the buffers, and make whatever changes to the
1051acd27e7Smillert screen itself that we should. Finally, make the buffer that we
1061acd27e7Smillert just drew into be the one which reflects the current contents of the
1071acd27e7Smillert screen, and place the cursor where it belongs.
1081acd27e7Smillert
1091acd27e7Smillert Commands that want to can fix the display themselves, and then let
1101acd27e7Smillert this function know that the display has been fixed by setting the
1111acd27e7Smillert RL_DISPLAY_FIXED variable. This is good for efficiency. */
1121acd27e7Smillert
1131acd27e7Smillert /* Application-specific redisplay function. */
114*af70c2dfSkettenis rl_voidfunc_t *rl_redisplay_function = rl_redisplay;
1151acd27e7Smillert
1161acd27e7Smillert /* Global variables declared here. */
1171acd27e7Smillert /* What YOU turn on when you have handled all redisplay yourself. */
1181acd27e7Smillert int rl_display_fixed = 0;
1191acd27e7Smillert
1201acd27e7Smillert int _rl_suppress_redisplay = 0;
1211acd27e7Smillert
1221acd27e7Smillert /* The stuff that gets printed out before the actual text of the line.
1231acd27e7Smillert This is usually pointing to rl_prompt. */
1241acd27e7Smillert char *rl_display_prompt = (char *)NULL;
1251acd27e7Smillert
1261acd27e7Smillert /* Pseudo-global variables declared here. */
1271acd27e7Smillert /* The visible cursor position. If you print some text, adjust this. */
1281acd27e7Smillert int _rl_last_c_pos = 0;
1291acd27e7Smillert int _rl_last_v_pos = 0;
1301acd27e7Smillert
1311acd27e7Smillert /* Number of lines currently on screen minus 1. */
1321acd27e7Smillert int _rl_vis_botlin = 0;
1331acd27e7Smillert
1341acd27e7Smillert /* Variables used only in this file. */
1351acd27e7Smillert /* The last left edge of text that was displayed. This is used when
1361acd27e7Smillert doing horizontal scrolling. It shifts in thirds of a screenwidth. */
1371acd27e7Smillert static int last_lmargin;
1381acd27e7Smillert
1391acd27e7Smillert /* The line display buffers. One is the line currently displayed on
1401acd27e7Smillert the screen. The other is the line about to be displayed. */
1411acd27e7Smillert static char *visible_line = (char *)NULL;
1421acd27e7Smillert static char *invisible_line = (char *)NULL;
1431acd27e7Smillert
1441acd27e7Smillert /* A buffer for `modeline' messages. */
1451acd27e7Smillert static char msg_buf[128];
1461acd27e7Smillert
1471acd27e7Smillert /* Non-zero forces the redisplay even if we thought it was unnecessary. */
1481acd27e7Smillert static int forced_display;
1491acd27e7Smillert
1501acd27e7Smillert /* Default and initial buffer size. Can grow. */
1511acd27e7Smillert static int line_size = 1024;
1521acd27e7Smillert
153*af70c2dfSkettenis /* Variables to keep track of the expanded prompt string, which may
154*af70c2dfSkettenis include invisible characters. */
155*af70c2dfSkettenis
1561acd27e7Smillert static char *local_prompt, *local_prompt_prefix;
157*af70c2dfSkettenis static int prompt_visible_length, prompt_prefix_length;
1581acd27e7Smillert
1591acd27e7Smillert /* The number of invisible characters in the line currently being
1601acd27e7Smillert displayed on the screen. */
1611acd27e7Smillert static int visible_wrap_offset;
1621acd27e7Smillert
163*af70c2dfSkettenis /* The number of invisible characters in the prompt string. Static so it
164*af70c2dfSkettenis can be shared between rl_redisplay and update_line */
1651acd27e7Smillert static int wrap_offset;
1661acd27e7Smillert
167*af70c2dfSkettenis /* The index of the last invisible character in the prompt string. */
168*af70c2dfSkettenis static int prompt_last_invisible;
1691acd27e7Smillert
1701acd27e7Smillert /* The length (buffer offset) of the first line of the last (possibly
1711acd27e7Smillert multi-line) buffer displayed on the screen. */
1721acd27e7Smillert static int visible_first_line_len;
1731acd27e7Smillert
174*af70c2dfSkettenis /* Number of invisible characters on the first physical line of the prompt.
175*af70c2dfSkettenis Only valid when the number of physical characters in the prompt exceeds
176*af70c2dfSkettenis (or is equal to) _rl_screenwidth. */
177*af70c2dfSkettenis static int prompt_invis_chars_first_line;
178*af70c2dfSkettenis
179*af70c2dfSkettenis static int prompt_last_screen_line;
180*af70c2dfSkettenis
1811acd27e7Smillert /* Expand the prompt string S and return the number of visible
1821acd27e7Smillert characters in *LP, if LP is not null. This is currently more-or-less
1831acd27e7Smillert a placeholder for expansion. LIP, if non-null is a place to store the
184*af70c2dfSkettenis index of the last invisible character in the returned string. NIFLP,
185*af70c2dfSkettenis if non-zero, is a place to store the number of invisible characters in
186*af70c2dfSkettenis the first prompt line. */
1871acd27e7Smillert
1881acd27e7Smillert /* Current implementation:
1891acd27e7Smillert \001 (^A) start non-visible characters
1901acd27e7Smillert \002 (^B) end non-visible characters
1911acd27e7Smillert all characters except \001 and \002 (following a \001) are copied to
1921acd27e7Smillert the returned string; all characters except those between \001 and
1931acd27e7Smillert \002 are assumed to be `visible'. */
1941acd27e7Smillert
1951acd27e7Smillert static char *
expand_prompt(pmt,lp,lip,niflp)196*af70c2dfSkettenis expand_prompt (pmt, lp, lip, niflp)
1971acd27e7Smillert char *pmt;
198*af70c2dfSkettenis int *lp, *lip, *niflp;
1991acd27e7Smillert {
2001acd27e7Smillert char *r, *ret, *p;
201*af70c2dfSkettenis int l, rl, last, ignoring, ninvis, invfl;
2021acd27e7Smillert
2031acd27e7Smillert /* Short-circuit if we can. */
2041acd27e7Smillert if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
2051acd27e7Smillert {
2061acd27e7Smillert r = savestring (pmt);
2071acd27e7Smillert if (lp)
2081acd27e7Smillert *lp = strlen (r);
2091acd27e7Smillert return r;
2101acd27e7Smillert }
2111acd27e7Smillert
2121acd27e7Smillert l = strlen (pmt);
213*af70c2dfSkettenis r = ret = (char *)xmalloc (l + 1);
2141acd27e7Smillert
215*af70c2dfSkettenis invfl = 0; /* invisible chars in first line of prompt */
216*af70c2dfSkettenis
217*af70c2dfSkettenis for (rl = ignoring = last = ninvis = 0, p = pmt; p && *p; p++)
2181acd27e7Smillert {
2191acd27e7Smillert /* This code strips the invisible character string markers
2201acd27e7Smillert RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
2211acd27e7Smillert if (*p == RL_PROMPT_START_IGNORE)
2221acd27e7Smillert {
2231acd27e7Smillert ignoring++;
2241acd27e7Smillert continue;
2251acd27e7Smillert }
2261acd27e7Smillert else if (ignoring && *p == RL_PROMPT_END_IGNORE)
2271acd27e7Smillert {
2281acd27e7Smillert ignoring = 0;
2291acd27e7Smillert last = r - ret - 1;
2301acd27e7Smillert continue;
2311acd27e7Smillert }
2321acd27e7Smillert else
2331acd27e7Smillert {
2341acd27e7Smillert *r++ = *p;
2351acd27e7Smillert if (!ignoring)
2361acd27e7Smillert rl++;
237*af70c2dfSkettenis else
238*af70c2dfSkettenis ninvis++;
239*af70c2dfSkettenis if (rl == _rl_screenwidth)
240*af70c2dfSkettenis invfl = ninvis;
2411acd27e7Smillert }
2421acd27e7Smillert }
2431acd27e7Smillert
244*af70c2dfSkettenis if (rl < _rl_screenwidth)
245*af70c2dfSkettenis invfl = ninvis;
246*af70c2dfSkettenis
2471acd27e7Smillert *r = '\0';
2481acd27e7Smillert if (lp)
2491acd27e7Smillert *lp = rl;
2501acd27e7Smillert if (lip)
2511acd27e7Smillert *lip = last;
252*af70c2dfSkettenis if (niflp)
253*af70c2dfSkettenis *niflp = invfl;
2541acd27e7Smillert return ret;
2551acd27e7Smillert }
2561acd27e7Smillert
2571acd27e7Smillert /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
2581acd27e7Smillert PMT and return the rest of PMT. */
2591acd27e7Smillert char *
_rl_strip_prompt(pmt)2601acd27e7Smillert _rl_strip_prompt (pmt)
2611acd27e7Smillert char *pmt;
2621acd27e7Smillert {
2631acd27e7Smillert char *ret;
2641acd27e7Smillert
265*af70c2dfSkettenis ret = expand_prompt (pmt, (int *)NULL, (int *)NULL, (int *)NULL);
2661acd27e7Smillert return ret;
2671acd27e7Smillert }
2681acd27e7Smillert
2691acd27e7Smillert /*
2701acd27e7Smillert * Expand the prompt string into the various display components, if
2711acd27e7Smillert * necessary.
2721acd27e7Smillert *
2731acd27e7Smillert * local_prompt = expanded last line of string in rl_display_prompt
2741acd27e7Smillert * (portion after the final newline)
2751acd27e7Smillert * local_prompt_prefix = portion before last newline of rl_display_prompt,
2761acd27e7Smillert * expanded via expand_prompt
277*af70c2dfSkettenis * prompt_visible_length = number of visible characters in local_prompt
278*af70c2dfSkettenis * prompt_prefix_length = number of visible characters in local_prompt_prefix
2791acd27e7Smillert *
2801acd27e7Smillert * This function is called once per call to readline(). It may also be
2811acd27e7Smillert * called arbitrarily to expand the primary prompt.
2821acd27e7Smillert *
2831acd27e7Smillert * The return value is the number of visible characters on the last line
2841acd27e7Smillert * of the (possibly multi-line) prompt.
2851acd27e7Smillert */
2861acd27e7Smillert int
rl_expand_prompt(prompt)2871acd27e7Smillert rl_expand_prompt (prompt)
2881acd27e7Smillert char *prompt;
2891acd27e7Smillert {
2901acd27e7Smillert char *p, *t;
2911acd27e7Smillert int c;
2921acd27e7Smillert
2931acd27e7Smillert /* Clear out any saved values. */
294*af70c2dfSkettenis FREE (local_prompt);
295*af70c2dfSkettenis FREE (local_prompt_prefix);
296*af70c2dfSkettenis
2971acd27e7Smillert local_prompt = local_prompt_prefix = (char *)0;
298*af70c2dfSkettenis prompt_last_invisible = prompt_visible_length = 0;
2991acd27e7Smillert
3001acd27e7Smillert if (prompt == 0 || *prompt == 0)
3011acd27e7Smillert return (0);
3021acd27e7Smillert
3031acd27e7Smillert p = strrchr (prompt, '\n');
3041acd27e7Smillert if (!p)
3051acd27e7Smillert {
306*af70c2dfSkettenis /* The prompt is only one logical line, though it might wrap. */
307*af70c2dfSkettenis local_prompt = expand_prompt (prompt, &prompt_visible_length,
308*af70c2dfSkettenis &prompt_last_invisible,
309*af70c2dfSkettenis &prompt_invis_chars_first_line);
3101acd27e7Smillert local_prompt_prefix = (char *)0;
311*af70c2dfSkettenis return (prompt_visible_length);
3121acd27e7Smillert }
3131acd27e7Smillert else
3141acd27e7Smillert {
3151acd27e7Smillert /* The prompt spans multiple lines. */
3161acd27e7Smillert t = ++p;
317*af70c2dfSkettenis local_prompt = expand_prompt (p, &prompt_visible_length,
318*af70c2dfSkettenis &prompt_last_invisible,
319*af70c2dfSkettenis &prompt_invis_chars_first_line);
3201acd27e7Smillert c = *t; *t = '\0';
3211acd27e7Smillert /* The portion of the prompt string up to and including the
3221acd27e7Smillert final newline is now null-terminated. */
323*af70c2dfSkettenis local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length,
324*af70c2dfSkettenis (int *)NULL,
325*af70c2dfSkettenis &prompt_invis_chars_first_line);
3261acd27e7Smillert *t = c;
327*af70c2dfSkettenis return (prompt_prefix_length);
3281acd27e7Smillert }
3291acd27e7Smillert }
3301acd27e7Smillert
3311acd27e7Smillert /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated
3321acd27e7Smillert arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE
3331acd27e7Smillert and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
3341acd27e7Smillert increased. If the lines have already been allocated, this ensures that
3351acd27e7Smillert they can hold at least MINSIZE characters. */
3361acd27e7Smillert static void
init_line_structures(minsize)3371acd27e7Smillert init_line_structures (minsize)
3381acd27e7Smillert int minsize;
3391acd27e7Smillert {
3401acd27e7Smillert register int n;
3411acd27e7Smillert
3421acd27e7Smillert if (invisible_line == 0) /* initialize it */
3431acd27e7Smillert {
3441acd27e7Smillert if (line_size < minsize)
3451acd27e7Smillert line_size = minsize;
346*af70c2dfSkettenis visible_line = (char *)xmalloc (line_size);
347*af70c2dfSkettenis invisible_line = (char *)xmalloc (line_size);
3481acd27e7Smillert }
3491acd27e7Smillert else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
3501acd27e7Smillert {
3511acd27e7Smillert line_size *= 2;
3521acd27e7Smillert if (line_size < minsize)
3531acd27e7Smillert line_size = minsize;
354*af70c2dfSkettenis visible_line = (char *)xrealloc (visible_line, line_size);
355*af70c2dfSkettenis invisible_line = (char *)xrealloc (invisible_line, line_size);
3561acd27e7Smillert }
3571acd27e7Smillert
3581acd27e7Smillert for (n = minsize; n < line_size; n++)
3591acd27e7Smillert {
3601acd27e7Smillert visible_line[n] = 0;
3611acd27e7Smillert invisible_line[n] = 1;
3621acd27e7Smillert }
3631acd27e7Smillert
3641acd27e7Smillert if (vis_lbreaks == 0)
3651acd27e7Smillert {
3661acd27e7Smillert /* should be enough. */
3671acd27e7Smillert inv_lbsize = vis_lbsize = 256;
3681acd27e7Smillert inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int));
3691acd27e7Smillert vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int));
370*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
371*af70c2dfSkettenis _rl_wrapped_line = (int *)xmalloc (vis_lbsize * sizeof (int));
372*af70c2dfSkettenis #endif
3731acd27e7Smillert inv_lbreaks[0] = vis_lbreaks[0] = 0;
3741acd27e7Smillert }
3751acd27e7Smillert }
3761acd27e7Smillert
3771acd27e7Smillert /* Basic redisplay algorithm. */
3781acd27e7Smillert void
rl_redisplay()3791acd27e7Smillert rl_redisplay ()
3801acd27e7Smillert {
3811acd27e7Smillert register int in, out, c, linenum, cursor_linenum;
3821acd27e7Smillert register char *line;
3831acd27e7Smillert int c_pos, inv_botlin, lb_botlin, lb_linenum;
3841acd27e7Smillert int newlines, lpos, temp;
3851acd27e7Smillert char *prompt_this_line;
386*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
387*af70c2dfSkettenis wchar_t wc;
388*af70c2dfSkettenis size_t wc_bytes;
389*af70c2dfSkettenis int wc_width;
390*af70c2dfSkettenis mbstate_t ps;
391*af70c2dfSkettenis int _rl_wrapped_multicolumn = 0;
392*af70c2dfSkettenis #endif
3931acd27e7Smillert
3941acd27e7Smillert if (!readline_echoing_p)
3951acd27e7Smillert return;
3961acd27e7Smillert
3971acd27e7Smillert if (!rl_display_prompt)
3981acd27e7Smillert rl_display_prompt = "";
3991acd27e7Smillert
4001acd27e7Smillert if (invisible_line == 0)
4011acd27e7Smillert {
4021acd27e7Smillert init_line_structures (0);
4031acd27e7Smillert rl_on_new_line ();
4041acd27e7Smillert }
4051acd27e7Smillert
4061acd27e7Smillert /* Draw the line into the buffer. */
4071acd27e7Smillert c_pos = -1;
4081acd27e7Smillert
4091acd27e7Smillert line = invisible_line;
4101acd27e7Smillert out = inv_botlin = 0;
4111acd27e7Smillert
4121acd27e7Smillert /* Mark the line as modified or not. We only do this for history
4131acd27e7Smillert lines. */
4141acd27e7Smillert if (_rl_mark_modified_lines && current_history () && rl_undo_list)
4151acd27e7Smillert {
4161acd27e7Smillert line[out++] = '*';
4171acd27e7Smillert line[out] = '\0';
4181acd27e7Smillert }
4191acd27e7Smillert
4201acd27e7Smillert /* If someone thought that the redisplay was handled, but the currently
4211acd27e7Smillert visible line has a different modification state than the one about
4221acd27e7Smillert to become visible, then correct the caller's misconception. */
4231acd27e7Smillert if (visible_line[0] != invisible_line[0])
4241acd27e7Smillert rl_display_fixed = 0;
4251acd27e7Smillert
4261acd27e7Smillert /* If the prompt to be displayed is the `primary' readline prompt (the
4271acd27e7Smillert one passed to readline()), use the values we have already expanded.
4281acd27e7Smillert If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
4291acd27e7Smillert number of non-visible characters in the prompt string. */
4301acd27e7Smillert if (rl_display_prompt == rl_prompt || local_prompt)
4311acd27e7Smillert {
4321acd27e7Smillert int local_len = local_prompt ? strlen (local_prompt) : 0;
4331acd27e7Smillert if (local_prompt_prefix && forced_display)
4341acd27e7Smillert _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
4351acd27e7Smillert
4361acd27e7Smillert if (local_len > 0)
4371acd27e7Smillert {
4381acd27e7Smillert temp = local_len + out + 2;
4391acd27e7Smillert if (temp >= line_size)
4401acd27e7Smillert {
4411acd27e7Smillert line_size = (temp + 1024) - (temp % 1024);
442*af70c2dfSkettenis visible_line = (char *)xrealloc (visible_line, line_size);
443*af70c2dfSkettenis line = invisible_line = (char *)xrealloc (invisible_line, line_size);
4441acd27e7Smillert }
4451acd27e7Smillert strncpy (line + out, local_prompt, local_len);
4461acd27e7Smillert out += local_len;
4471acd27e7Smillert }
4481acd27e7Smillert line[out] = '\0';
449*af70c2dfSkettenis wrap_offset = local_len - prompt_visible_length;
4501acd27e7Smillert }
4511acd27e7Smillert else
4521acd27e7Smillert {
4531acd27e7Smillert int pmtlen;
4541acd27e7Smillert prompt_this_line = strrchr (rl_display_prompt, '\n');
4551acd27e7Smillert if (!prompt_this_line)
4561acd27e7Smillert prompt_this_line = rl_display_prompt;
4571acd27e7Smillert else
4581acd27e7Smillert {
4591acd27e7Smillert prompt_this_line++;
4601acd27e7Smillert pmtlen = prompt_this_line - rl_display_prompt; /* temp var */
4611acd27e7Smillert if (forced_display)
4621acd27e7Smillert {
4631acd27e7Smillert _rl_output_some_chars (rl_display_prompt, pmtlen);
4641acd27e7Smillert /* Make sure we are at column zero even after a newline,
4651acd27e7Smillert regardless of the state of terminal output processing. */
4661acd27e7Smillert if (pmtlen < 2 || prompt_this_line[-2] != '\r')
4671acd27e7Smillert cr ();
4681acd27e7Smillert }
4691acd27e7Smillert }
4701acd27e7Smillert
4711acd27e7Smillert pmtlen = strlen (prompt_this_line);
4721acd27e7Smillert temp = pmtlen + out + 2;
4731acd27e7Smillert if (temp >= line_size)
4741acd27e7Smillert {
4751acd27e7Smillert line_size = (temp + 1024) - (temp % 1024);
476*af70c2dfSkettenis visible_line = (char *)xrealloc (visible_line, line_size);
477*af70c2dfSkettenis line = invisible_line = (char *)xrealloc (invisible_line, line_size);
4781acd27e7Smillert }
4791acd27e7Smillert strncpy (line + out, prompt_this_line, pmtlen);
4801acd27e7Smillert out += pmtlen;
4811acd27e7Smillert line[out] = '\0';
482*af70c2dfSkettenis wrap_offset = prompt_invis_chars_first_line = 0;
4831acd27e7Smillert }
4841acd27e7Smillert
4851acd27e7Smillert #define CHECK_INV_LBREAKS() \
4861acd27e7Smillert do { \
4871acd27e7Smillert if (newlines >= (inv_lbsize - 2)) \
4881acd27e7Smillert { \
4891acd27e7Smillert inv_lbsize *= 2; \
4901acd27e7Smillert inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
4911acd27e7Smillert } \
4921acd27e7Smillert } while (0)
4931acd27e7Smillert
494*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
4951acd27e7Smillert #define CHECK_LPOS() \
4961acd27e7Smillert do { \
4971acd27e7Smillert lpos++; \
498*af70c2dfSkettenis if (lpos >= _rl_screenwidth) \
499*af70c2dfSkettenis { \
500*af70c2dfSkettenis if (newlines >= (inv_lbsize - 2)) \
501*af70c2dfSkettenis { \
502*af70c2dfSkettenis inv_lbsize *= 2; \
503*af70c2dfSkettenis inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
504*af70c2dfSkettenis _rl_wrapped_line = (int *)xrealloc (_rl_wrapped_line, inv_lbsize * sizeof (int)); \
505*af70c2dfSkettenis } \
506*af70c2dfSkettenis inv_lbreaks[++newlines] = out; \
507*af70c2dfSkettenis _rl_wrapped_line[newlines] = _rl_wrapped_multicolumn; \
508*af70c2dfSkettenis lpos = 0; \
509*af70c2dfSkettenis } \
510*af70c2dfSkettenis } while (0)
511*af70c2dfSkettenis #else
512*af70c2dfSkettenis #define CHECK_LPOS() \
513*af70c2dfSkettenis do { \
514*af70c2dfSkettenis lpos++; \
515*af70c2dfSkettenis if (lpos >= _rl_screenwidth) \
5161acd27e7Smillert { \
5171acd27e7Smillert if (newlines >= (inv_lbsize - 2)) \
5181acd27e7Smillert { \
5191acd27e7Smillert inv_lbsize *= 2; \
5201acd27e7Smillert inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \
5211acd27e7Smillert } \
5221acd27e7Smillert inv_lbreaks[++newlines] = out; \
5231acd27e7Smillert lpos = 0; \
5241acd27e7Smillert } \
5251acd27e7Smillert } while (0)
526*af70c2dfSkettenis #endif
5271acd27e7Smillert
5281acd27e7Smillert /* inv_lbreaks[i] is where line i starts in the buffer. */
5291acd27e7Smillert inv_lbreaks[newlines = 0] = 0;
5301acd27e7Smillert lpos = out - wrap_offset;
531*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
532*af70c2dfSkettenis memset (_rl_wrapped_line, 0, vis_lbsize);
533*af70c2dfSkettenis #endif
5341acd27e7Smillert
535*af70c2dfSkettenis /* prompt_invis_chars_first_line is the number of invisible characters in
536*af70c2dfSkettenis the first physical line of the prompt.
537*af70c2dfSkettenis wrap_offset - prompt_invis_chars_first_line is the number of invis
538*af70c2dfSkettenis chars on the second line. */
539*af70c2dfSkettenis
540*af70c2dfSkettenis /* what if lpos is already >= _rl_screenwidth before we start drawing the
5411acd27e7Smillert contents of the command line? */
542*af70c2dfSkettenis while (lpos >= _rl_screenwidth)
5431acd27e7Smillert {
544*af70c2dfSkettenis /* fix from Darin Johnson <darin@acuson.com> for prompt string with
545*af70c2dfSkettenis invisible characters that is longer than the screen width. The
546*af70c2dfSkettenis prompt_invis_chars_first_line variable could be made into an array
547*af70c2dfSkettenis saying how many invisible characters there are per line, but that's
548*af70c2dfSkettenis probably too much work for the benefit gained. How many people have
549*af70c2dfSkettenis prompts that exceed two physical lines? */
550*af70c2dfSkettenis temp = ((newlines + 1) * _rl_screenwidth) +
551*af70c2dfSkettenis #if 0
552*af70c2dfSkettenis ((newlines == 0) ? prompt_invis_chars_first_line : 0) +
553*af70c2dfSkettenis #else
554*af70c2dfSkettenis ((newlines == 0 && local_prompt_prefix == 0) ? prompt_invis_chars_first_line : 0) +
555*af70c2dfSkettenis #endif
556*af70c2dfSkettenis ((newlines == 1) ? wrap_offset : 0);
5571acd27e7Smillert
5581acd27e7Smillert inv_lbreaks[++newlines] = temp;
559*af70c2dfSkettenis lpos -= _rl_screenwidth;
5601acd27e7Smillert }
5611acd27e7Smillert
562*af70c2dfSkettenis prompt_last_screen_line = newlines;
563*af70c2dfSkettenis
564*af70c2dfSkettenis /* Draw the rest of the line (after the prompt) into invisible_line, keeping
565*af70c2dfSkettenis track of where the cursor is (c_pos), the number of the line containing
566*af70c2dfSkettenis the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin).
567*af70c2dfSkettenis It maintains an array of line breaks for display (inv_lbreaks).
568*af70c2dfSkettenis This handles expanding tabs for display and displaying meta characters. */
5691acd27e7Smillert lb_linenum = 0;
570*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
571*af70c2dfSkettenis in = 0;
572*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
573*af70c2dfSkettenis {
574*af70c2dfSkettenis memset (&ps, 0, sizeof (mbstate_t));
575*af70c2dfSkettenis wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps);
576*af70c2dfSkettenis }
577*af70c2dfSkettenis else
578*af70c2dfSkettenis wc_bytes = 1;
579*af70c2dfSkettenis while (in < rl_end)
580*af70c2dfSkettenis #else
5811acd27e7Smillert for (in = 0; in < rl_end; in++)
582*af70c2dfSkettenis #endif
5831acd27e7Smillert {
5841acd27e7Smillert c = (unsigned char)rl_line_buffer[in];
5851acd27e7Smillert
586*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
587*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
588*af70c2dfSkettenis {
589*af70c2dfSkettenis if (wc_bytes == (size_t)-1 || wc_bytes == (size_t)-2)
590*af70c2dfSkettenis {
591*af70c2dfSkettenis /* Byte sequence is invalid or shortened. Assume that the
592*af70c2dfSkettenis first byte represents a character. */
593*af70c2dfSkettenis wc_bytes = 1;
594*af70c2dfSkettenis /* Assume that a character occupies a single column. */
595*af70c2dfSkettenis wc_width = 1;
596*af70c2dfSkettenis memset (&ps, 0, sizeof (mbstate_t));
597*af70c2dfSkettenis }
598*af70c2dfSkettenis else if (wc_bytes == (size_t)0)
599*af70c2dfSkettenis break; /* Found '\0' */
600*af70c2dfSkettenis else
601*af70c2dfSkettenis {
602*af70c2dfSkettenis temp = wcwidth (wc);
603*af70c2dfSkettenis wc_width = (temp < 0) ? 1 : temp;
604*af70c2dfSkettenis }
605*af70c2dfSkettenis }
606*af70c2dfSkettenis #endif
607*af70c2dfSkettenis
6081acd27e7Smillert if (out + 8 >= line_size) /* XXX - 8 for \t */
6091acd27e7Smillert {
6101acd27e7Smillert line_size *= 2;
611*af70c2dfSkettenis visible_line = (char *)xrealloc (visible_line, line_size);
612*af70c2dfSkettenis invisible_line = (char *)xrealloc (invisible_line, line_size);
6131acd27e7Smillert line = invisible_line;
6141acd27e7Smillert }
6151acd27e7Smillert
6161acd27e7Smillert if (in == rl_point)
6171acd27e7Smillert {
6181acd27e7Smillert c_pos = out;
6191acd27e7Smillert lb_linenum = newlines;
6201acd27e7Smillert }
6211acd27e7Smillert
622*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
623*af70c2dfSkettenis if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */
624*af70c2dfSkettenis #else
6251acd27e7Smillert if (META_CHAR (c))
626*af70c2dfSkettenis #endif
6271acd27e7Smillert {
6281acd27e7Smillert if (_rl_output_meta_chars == 0)
6291acd27e7Smillert {
63063bef317Sbeck snprintf (line + out, line_size - out, "\\%o", c);
6311acd27e7Smillert
632*af70c2dfSkettenis if (lpos + 4 >= _rl_screenwidth)
6331acd27e7Smillert {
634*af70c2dfSkettenis temp = _rl_screenwidth - lpos;
6351acd27e7Smillert CHECK_INV_LBREAKS ();
6361acd27e7Smillert inv_lbreaks[++newlines] = out + temp;
6371acd27e7Smillert lpos = 4 - temp;
6381acd27e7Smillert }
6391acd27e7Smillert else
6401acd27e7Smillert lpos += 4;
6411acd27e7Smillert
6421acd27e7Smillert out += 4;
6431acd27e7Smillert }
6441acd27e7Smillert else
6451acd27e7Smillert {
6461acd27e7Smillert line[out++] = c;
6471acd27e7Smillert CHECK_LPOS();
6481acd27e7Smillert }
6491acd27e7Smillert }
6501acd27e7Smillert #if defined (DISPLAY_TABS)
6511acd27e7Smillert else if (c == '\t')
6521acd27e7Smillert {
653*af70c2dfSkettenis register int newout;
6541acd27e7Smillert
6551acd27e7Smillert #if 0
6561acd27e7Smillert newout = (out | (int)7) + 1;
6571acd27e7Smillert #else
6581acd27e7Smillert newout = out + 8 - lpos % 8;
6591acd27e7Smillert #endif
6601acd27e7Smillert temp = newout - out;
661*af70c2dfSkettenis if (lpos + temp >= _rl_screenwidth)
6621acd27e7Smillert {
6631acd27e7Smillert register int temp2;
664*af70c2dfSkettenis temp2 = _rl_screenwidth - lpos;
6651acd27e7Smillert CHECK_INV_LBREAKS ();
6661acd27e7Smillert inv_lbreaks[++newlines] = out + temp2;
6671acd27e7Smillert lpos = temp - temp2;
6681acd27e7Smillert while (out < newout)
6691acd27e7Smillert line[out++] = ' ';
6701acd27e7Smillert }
6711acd27e7Smillert else
6721acd27e7Smillert {
6731acd27e7Smillert while (out < newout)
6741acd27e7Smillert line[out++] = ' ';
6751acd27e7Smillert lpos += temp;
6761acd27e7Smillert }
6771acd27e7Smillert }
6781acd27e7Smillert #endif
679*af70c2dfSkettenis else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
6801acd27e7Smillert {
6811acd27e7Smillert line[out++] = '\0'; /* XXX - sentinel */
6821acd27e7Smillert CHECK_INV_LBREAKS ();
6831acd27e7Smillert inv_lbreaks[++newlines] = out;
6841acd27e7Smillert lpos = 0;
6851acd27e7Smillert }
6861acd27e7Smillert else if (CTRL_CHAR (c) || c == RUBOUT)
6871acd27e7Smillert {
6881acd27e7Smillert line[out++] = '^';
6891acd27e7Smillert CHECK_LPOS();
6901acd27e7Smillert line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
6911acd27e7Smillert CHECK_LPOS();
6921acd27e7Smillert }
6931acd27e7Smillert else
6941acd27e7Smillert {
695*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
696*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
697*af70c2dfSkettenis {
698*af70c2dfSkettenis register int i;
699*af70c2dfSkettenis
700*af70c2dfSkettenis _rl_wrapped_multicolumn = 0;
701*af70c2dfSkettenis
702*af70c2dfSkettenis if (_rl_screenwidth < lpos + wc_width)
703*af70c2dfSkettenis for (i = lpos; i < _rl_screenwidth; i++)
704*af70c2dfSkettenis {
705*af70c2dfSkettenis /* The space will be removed in update_line() */
706*af70c2dfSkettenis line[out++] = ' ';
707*af70c2dfSkettenis _rl_wrapped_multicolumn++;
708*af70c2dfSkettenis CHECK_LPOS();
709*af70c2dfSkettenis }
710*af70c2dfSkettenis if (in == rl_point)
711*af70c2dfSkettenis {
712*af70c2dfSkettenis c_pos = out;
713*af70c2dfSkettenis lb_linenum = newlines;
714*af70c2dfSkettenis }
715*af70c2dfSkettenis for (i = in; i < in+wc_bytes; i++)
716*af70c2dfSkettenis line[out++] = rl_line_buffer[i];
717*af70c2dfSkettenis for (i = 0; i < wc_width; i++)
718*af70c2dfSkettenis CHECK_LPOS();
719*af70c2dfSkettenis }
720*af70c2dfSkettenis else
721*af70c2dfSkettenis {
7221acd27e7Smillert line[out++] = c;
7231acd27e7Smillert CHECK_LPOS();
7241acd27e7Smillert }
725*af70c2dfSkettenis #else
726*af70c2dfSkettenis line[out++] = c;
727*af70c2dfSkettenis CHECK_LPOS();
728*af70c2dfSkettenis #endif
729*af70c2dfSkettenis }
730*af70c2dfSkettenis
731*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
732*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
733*af70c2dfSkettenis {
734*af70c2dfSkettenis in += wc_bytes;
735*af70c2dfSkettenis wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps);
736*af70c2dfSkettenis }
737*af70c2dfSkettenis else
738*af70c2dfSkettenis in++;
739*af70c2dfSkettenis #endif
740*af70c2dfSkettenis
7411acd27e7Smillert }
7421acd27e7Smillert line[out] = '\0';
7431acd27e7Smillert if (c_pos < 0)
7441acd27e7Smillert {
7451acd27e7Smillert c_pos = out;
7461acd27e7Smillert lb_linenum = newlines;
7471acd27e7Smillert }
7481acd27e7Smillert
7491acd27e7Smillert inv_botlin = lb_botlin = newlines;
7501acd27e7Smillert CHECK_INV_LBREAKS ();
7511acd27e7Smillert inv_lbreaks[newlines+1] = out;
7521acd27e7Smillert cursor_linenum = lb_linenum;
7531acd27e7Smillert
754*af70c2dfSkettenis /* C_POS == position in buffer where cursor should be placed.
755*af70c2dfSkettenis CURSOR_LINENUM == line number where the cursor should be placed. */
7561acd27e7Smillert
7571acd27e7Smillert /* PWP: now is when things get a bit hairy. The visible and invisible
7581acd27e7Smillert line buffers are really multiple lines, which would wrap every
7591acd27e7Smillert (screenwidth - 1) characters. Go through each in turn, finding
7601acd27e7Smillert the changed region and updating it. The line order is top to bottom. */
7611acd27e7Smillert
7621acd27e7Smillert /* If we can move the cursor up and down, then use multiple lines,
7631acd27e7Smillert otherwise, let long lines display in a single terminal line, and
7641acd27e7Smillert horizontally scroll it. */
7651acd27e7Smillert
766*af70c2dfSkettenis if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up)
7671acd27e7Smillert {
7681acd27e7Smillert int nleft, pos, changed_screen_line;
7691acd27e7Smillert
7701acd27e7Smillert if (!rl_display_fixed || forced_display)
7711acd27e7Smillert {
7721acd27e7Smillert forced_display = 0;
7731acd27e7Smillert
7741acd27e7Smillert /* If we have more than a screenful of material to display, then
7751acd27e7Smillert only display a screenful. We should display the last screen,
7761acd27e7Smillert not the first. */
777*af70c2dfSkettenis if (out >= _rl_screenchars)
778*af70c2dfSkettenis {
779*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
780*af70c2dfSkettenis out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY);
781*af70c2dfSkettenis else
782*af70c2dfSkettenis out = _rl_screenchars - 1;
783*af70c2dfSkettenis }
7841acd27e7Smillert
7851acd27e7Smillert /* The first line is at character position 0 in the buffer. The
7861acd27e7Smillert second and subsequent lines start at inv_lbreaks[N], offset by
7871acd27e7Smillert OFFSET (which has already been calculated above). */
7881acd27e7Smillert
7891acd27e7Smillert #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
7901acd27e7Smillert #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l]))
7911acd27e7Smillert #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l])
7921acd27e7Smillert #define VIS_CHARS(line) (visible_line + vis_lbreaks[line])
7931acd27e7Smillert #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
7941acd27e7Smillert #define INV_LINE(line) (invisible_line + inv_lbreaks[line])
7951acd27e7Smillert
7961acd27e7Smillert /* For each line in the buffer, do the updating display. */
7971acd27e7Smillert for (linenum = 0; linenum <= inv_botlin; linenum++)
7981acd27e7Smillert {
7991acd27e7Smillert update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
8001acd27e7Smillert VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin);
8011acd27e7Smillert
8021acd27e7Smillert /* If this is the line with the prompt, we might need to
8031acd27e7Smillert compensate for invisible characters in the new line. Do
8041acd27e7Smillert this only if there is not more than one new line (which
8051acd27e7Smillert implies that we completely overwrite the old visible line)
8061acd27e7Smillert and the new line is shorter than the old. Make sure we are
8071acd27e7Smillert at the end of the new line before clearing. */
8081acd27e7Smillert if (linenum == 0 &&
8091acd27e7Smillert inv_botlin == 0 && _rl_last_c_pos == out &&
8101acd27e7Smillert (wrap_offset > visible_wrap_offset) &&
8111acd27e7Smillert (_rl_last_c_pos < visible_first_line_len))
8121acd27e7Smillert {
813*af70c2dfSkettenis nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos;
8141acd27e7Smillert if (nleft)
8151acd27e7Smillert _rl_clear_to_eol (nleft);
8161acd27e7Smillert }
8171acd27e7Smillert
8181acd27e7Smillert /* Since the new first line is now visible, save its length. */
8191acd27e7Smillert if (linenum == 0)
8201acd27e7Smillert visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset;
8211acd27e7Smillert }
8221acd27e7Smillert
8231acd27e7Smillert /* We may have deleted some lines. If so, clear the left over
8241acd27e7Smillert blank ones at the bottom out. */
8251acd27e7Smillert if (_rl_vis_botlin > inv_botlin)
8261acd27e7Smillert {
8271acd27e7Smillert char *tt;
8281acd27e7Smillert for (; linenum <= _rl_vis_botlin; linenum++)
8291acd27e7Smillert {
8301acd27e7Smillert tt = VIS_CHARS (linenum);
8311acd27e7Smillert _rl_move_vert (linenum);
8321acd27e7Smillert _rl_move_cursor_relative (0, tt);
8331acd27e7Smillert _rl_clear_to_eol
834*af70c2dfSkettenis ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth);
8351acd27e7Smillert }
8361acd27e7Smillert }
8371acd27e7Smillert _rl_vis_botlin = inv_botlin;
8381acd27e7Smillert
8391acd27e7Smillert /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
8401acd27e7Smillert different screen line during this redisplay. */
8411acd27e7Smillert changed_screen_line = _rl_last_v_pos != cursor_linenum;
8421acd27e7Smillert if (changed_screen_line)
8431acd27e7Smillert {
8441acd27e7Smillert _rl_move_vert (cursor_linenum);
845*af70c2dfSkettenis /* If we moved up to the line with the prompt using _rl_term_up,
8461acd27e7Smillert the physical cursor position on the screen stays the same,
8471acd27e7Smillert but the buffer position needs to be adjusted to account
8481acd27e7Smillert for invisible characters. */
8491acd27e7Smillert if (cursor_linenum == 0 && wrap_offset)
8501acd27e7Smillert _rl_last_c_pos += wrap_offset;
8511acd27e7Smillert }
8521acd27e7Smillert
8531acd27e7Smillert /* We have to reprint the prompt if it contains invisible
8541acd27e7Smillert characters, since it's not generally OK to just reprint
8551acd27e7Smillert the characters from the current cursor position. But we
8561acd27e7Smillert only need to reprint it if the cursor is before the last
8571acd27e7Smillert invisible character in the prompt string. */
858*af70c2dfSkettenis nleft = prompt_visible_length + wrap_offset;
8591acd27e7Smillert if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
860*af70c2dfSkettenis _rl_last_c_pos <= prompt_last_invisible && local_prompt)
8611acd27e7Smillert {
8621acd27e7Smillert #if defined (__MSDOS__)
8631acd27e7Smillert putc ('\r', rl_outstream);
8641acd27e7Smillert #else
865*af70c2dfSkettenis if (_rl_term_cr)
866*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
8671acd27e7Smillert #endif
8681acd27e7Smillert _rl_output_some_chars (local_prompt, nleft);
869*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
870*af70c2dfSkettenis _rl_last_c_pos = _rl_col_width(local_prompt, 0, nleft);
871*af70c2dfSkettenis else
8721acd27e7Smillert _rl_last_c_pos = nleft;
8731acd27e7Smillert }
8741acd27e7Smillert
8751acd27e7Smillert /* Where on that line? And where does that line start
8761acd27e7Smillert in the buffer? */
8771acd27e7Smillert pos = inv_lbreaks[cursor_linenum];
8781acd27e7Smillert /* nleft == number of characters in the line buffer between the
8791acd27e7Smillert start of the line and the cursor position. */
8801acd27e7Smillert nleft = c_pos - pos;
8811acd27e7Smillert
8821acd27e7Smillert /* Since _rl_backspace() doesn't know about invisible characters in the
8831acd27e7Smillert prompt, and there's no good way to tell it, we compensate for
8841acd27e7Smillert those characters here and call _rl_backspace() directly. */
8851acd27e7Smillert if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
8861acd27e7Smillert {
8871acd27e7Smillert _rl_backspace (_rl_last_c_pos - nleft);
888*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
889*af70c2dfSkettenis _rl_last_c_pos = _rl_col_width (&visible_line[pos], 0, nleft);
890*af70c2dfSkettenis else
8911acd27e7Smillert _rl_last_c_pos = nleft;
8921acd27e7Smillert }
8931acd27e7Smillert
894*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
895*af70c2dfSkettenis _rl_move_cursor_relative (nleft, &invisible_line[pos]);
896*af70c2dfSkettenis else if (nleft != _rl_last_c_pos)
8971acd27e7Smillert _rl_move_cursor_relative (nleft, &invisible_line[pos]);
8981acd27e7Smillert }
8991acd27e7Smillert }
9001acd27e7Smillert else /* Do horizontal scrolling. */
9011acd27e7Smillert {
9021acd27e7Smillert #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
9031acd27e7Smillert int lmargin, ndisp, nleft, phys_c_pos, t;
9041acd27e7Smillert
9051acd27e7Smillert /* Always at top line. */
9061acd27e7Smillert _rl_last_v_pos = 0;
9071acd27e7Smillert
9081acd27e7Smillert /* Compute where in the buffer the displayed line should start. This
9091acd27e7Smillert will be LMARGIN. */
9101acd27e7Smillert
9111acd27e7Smillert /* The number of characters that will be displayed before the cursor. */
9121acd27e7Smillert ndisp = c_pos - wrap_offset;
913*af70c2dfSkettenis nleft = prompt_visible_length + wrap_offset;
9141acd27e7Smillert /* Where the new cursor position will be on the screen. This can be
9151acd27e7Smillert longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
9161acd27e7Smillert phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
917*af70c2dfSkettenis t = _rl_screenwidth / 3;
9181acd27e7Smillert
9191acd27e7Smillert /* If the number of characters had already exceeded the screenwidth,
9201acd27e7Smillert last_lmargin will be > 0. */
9211acd27e7Smillert
9221acd27e7Smillert /* If the number of characters to be displayed is more than the screen
9231acd27e7Smillert width, compute the starting offset so that the cursor is about
9241acd27e7Smillert two-thirds of the way across the screen. */
925*af70c2dfSkettenis if (phys_c_pos > _rl_screenwidth - 2)
9261acd27e7Smillert {
9271acd27e7Smillert lmargin = c_pos - (2 * t);
9281acd27e7Smillert if (lmargin < 0)
9291acd27e7Smillert lmargin = 0;
9301acd27e7Smillert /* If the left margin would be in the middle of a prompt with
9311acd27e7Smillert invisible characters, don't display the prompt at all. */
9321acd27e7Smillert if (wrap_offset && lmargin > 0 && lmargin < nleft)
9331acd27e7Smillert lmargin = nleft;
9341acd27e7Smillert }
935*af70c2dfSkettenis else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */
9361acd27e7Smillert lmargin = 0;
9371acd27e7Smillert else if (phys_c_pos < 1)
9381acd27e7Smillert {
9391acd27e7Smillert /* If we are moving back towards the beginning of the line and
9401acd27e7Smillert the last margin is no longer correct, compute a new one. */
9411acd27e7Smillert lmargin = ((c_pos - 1) / t) * t; /* XXX */
9421acd27e7Smillert if (wrap_offset && lmargin > 0 && lmargin < nleft)
9431acd27e7Smillert lmargin = nleft;
9441acd27e7Smillert }
9451acd27e7Smillert else
9461acd27e7Smillert lmargin = last_lmargin;
9471acd27e7Smillert
9481acd27e7Smillert /* If the first character on the screen isn't the first character
9491acd27e7Smillert in the display line, indicate this with a special character. */
9501acd27e7Smillert if (lmargin > 0)
9511acd27e7Smillert line[lmargin] = '<';
9521acd27e7Smillert
9531acd27e7Smillert /* If SCREENWIDTH characters starting at LMARGIN do not encompass
9541acd27e7Smillert the whole line, indicate that with a special character at the
9551acd27e7Smillert right edge of the screen. If LMARGIN is 0, we need to take the
9561acd27e7Smillert wrap offset into account. */
957*af70c2dfSkettenis t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth;
9581acd27e7Smillert if (t < out)
9591acd27e7Smillert line[t - 1] = '>';
9601acd27e7Smillert
9611acd27e7Smillert if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
9621acd27e7Smillert {
9631acd27e7Smillert forced_display = 0;
9641acd27e7Smillert update_line (&visible_line[last_lmargin],
9651acd27e7Smillert &invisible_line[lmargin],
9661acd27e7Smillert 0,
967*af70c2dfSkettenis _rl_screenwidth + visible_wrap_offset,
968*af70c2dfSkettenis _rl_screenwidth + (lmargin ? 0 : wrap_offset),
9691acd27e7Smillert 0);
9701acd27e7Smillert
9711acd27e7Smillert /* If the visible new line is shorter than the old, but the number
9721acd27e7Smillert of invisible characters is greater, and we are at the end of
9731acd27e7Smillert the new line, we need to clear to eol. */
9741acd27e7Smillert t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
9751acd27e7Smillert if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
9761acd27e7Smillert (_rl_last_c_pos == out) &&
9771acd27e7Smillert t < visible_first_line_len)
9781acd27e7Smillert {
979*af70c2dfSkettenis nleft = _rl_screenwidth - t;
9801acd27e7Smillert _rl_clear_to_eol (nleft);
9811acd27e7Smillert }
9821acd27e7Smillert visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
983*af70c2dfSkettenis if (visible_first_line_len > _rl_screenwidth)
984*af70c2dfSkettenis visible_first_line_len = _rl_screenwidth;
9851acd27e7Smillert
9861acd27e7Smillert _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
9871acd27e7Smillert last_lmargin = lmargin;
9881acd27e7Smillert }
9891acd27e7Smillert }
9901acd27e7Smillert fflush (rl_outstream);
9911acd27e7Smillert
9921acd27e7Smillert /* Swap visible and non-visible lines. */
9931acd27e7Smillert {
994*af70c2dfSkettenis char *vtemp = visible_line;
9951acd27e7Smillert int *itemp = vis_lbreaks, ntemp = vis_lbsize;
9961acd27e7Smillert
9971acd27e7Smillert visible_line = invisible_line;
998*af70c2dfSkettenis invisible_line = vtemp;
9991acd27e7Smillert
10001acd27e7Smillert vis_lbreaks = inv_lbreaks;
10011acd27e7Smillert inv_lbreaks = itemp;
10021acd27e7Smillert
10031acd27e7Smillert vis_lbsize = inv_lbsize;
10041acd27e7Smillert inv_lbsize = ntemp;
10051acd27e7Smillert
10061acd27e7Smillert rl_display_fixed = 0;
10071acd27e7Smillert /* If we are displaying on a single line, and last_lmargin is > 0, we
10081acd27e7Smillert are not displaying any invisible characters, so set visible_wrap_offset
10091acd27e7Smillert to 0. */
10101acd27e7Smillert if (_rl_horizontal_scroll_mode && last_lmargin)
10111acd27e7Smillert visible_wrap_offset = 0;
10121acd27e7Smillert else
10131acd27e7Smillert visible_wrap_offset = wrap_offset;
10141acd27e7Smillert }
10151acd27e7Smillert }
10161acd27e7Smillert
10171acd27e7Smillert /* PWP: update_line() is based on finding the middle difference of each
10181acd27e7Smillert line on the screen; vis:
10191acd27e7Smillert
10201acd27e7Smillert /old first difference
10211acd27e7Smillert /beginning of line | /old last same /old EOL
10221acd27e7Smillert v v v v
10231acd27e7Smillert old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
10241acd27e7Smillert new: eddie> Oh, my little buggy says to me, as lurgid as
10251acd27e7Smillert ^ ^ ^ ^
10261acd27e7Smillert \beginning of line | \new last same \new end of line
10271acd27e7Smillert \new first difference
10281acd27e7Smillert
10291acd27e7Smillert All are character pointers for the sake of speed. Special cases for
10301acd27e7Smillert no differences, as well as for end of line additions must be handled.
10311acd27e7Smillert
10321acd27e7Smillert Could be made even smarter, but this works well enough */
10331acd27e7Smillert static void
update_line(old,new,current_line,omax,nmax,inv_botlin)10341acd27e7Smillert update_line (old, new, current_line, omax, nmax, inv_botlin)
10351acd27e7Smillert register char *old, *new;
10361acd27e7Smillert int current_line, omax, nmax, inv_botlin;
10371acd27e7Smillert {
10381acd27e7Smillert register char *ofd, *ols, *oe, *nfd, *nls, *ne;
10391acd27e7Smillert int temp, lendiff, wsatend, od, nd;
10401acd27e7Smillert int current_invis_chars;
1041*af70c2dfSkettenis int col_lendiff, col_temp;
1042*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1043*af70c2dfSkettenis mbstate_t ps_new, ps_old;
1044*af70c2dfSkettenis int new_offset, old_offset, tmp;
1045*af70c2dfSkettenis #endif
10461acd27e7Smillert
10471acd27e7Smillert /* If we're at the right edge of a terminal that supports xn, we're
10481acd27e7Smillert ready to wrap around, so do so. This fixes problems with knowing
10491acd27e7Smillert the exact cursor position and cut-and-paste with certain terminal
10501acd27e7Smillert emulators. In this calculation, TEMP is the physical screen
10511acd27e7Smillert position of the cursor. */
10521acd27e7Smillert temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
1053*af70c2dfSkettenis if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode
10541acd27e7Smillert && _rl_last_v_pos == current_line - 1)
10551acd27e7Smillert {
1056*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1057*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1058*af70c2dfSkettenis {
1059*af70c2dfSkettenis wchar_t wc;
1060*af70c2dfSkettenis mbstate_t ps;
1061*af70c2dfSkettenis int tempwidth, bytes;
1062*af70c2dfSkettenis size_t ret;
1063*af70c2dfSkettenis
1064*af70c2dfSkettenis /* This fixes only double-column characters, but if the wrapped
1065*af70c2dfSkettenis character comsumes more than three columns, spaces will be
1066*af70c2dfSkettenis inserted in the string buffer. */
1067*af70c2dfSkettenis if (_rl_wrapped_line[current_line] > 0)
1068*af70c2dfSkettenis _rl_clear_to_eol (_rl_wrapped_line[current_line]);
1069*af70c2dfSkettenis
1070*af70c2dfSkettenis memset (&ps, 0, sizeof (mbstate_t));
1071*af70c2dfSkettenis ret = mbrtowc (&wc, new, MB_CUR_MAX, &ps);
1072*af70c2dfSkettenis if (ret == (size_t)-1 || ret == (size_t)-2)
1073*af70c2dfSkettenis {
1074*af70c2dfSkettenis tempwidth = 1;
1075*af70c2dfSkettenis ret = 1;
1076*af70c2dfSkettenis }
1077*af70c2dfSkettenis else if (ret == 0)
1078*af70c2dfSkettenis tempwidth = 0;
1079*af70c2dfSkettenis else
1080*af70c2dfSkettenis tempwidth = wcwidth (wc);
1081*af70c2dfSkettenis
1082*af70c2dfSkettenis if (tempwidth > 0)
1083*af70c2dfSkettenis {
1084*af70c2dfSkettenis int count;
1085*af70c2dfSkettenis bytes = ret;
1086*af70c2dfSkettenis for (count = 0; count < bytes; count++)
1087*af70c2dfSkettenis putc (new[count], rl_outstream);
1088*af70c2dfSkettenis _rl_last_c_pos = tempwidth;
1089*af70c2dfSkettenis _rl_last_v_pos++;
1090*af70c2dfSkettenis memset (&ps, 0, sizeof (mbstate_t));
1091*af70c2dfSkettenis ret = mbrtowc (&wc, old, MB_CUR_MAX, &ps);
1092*af70c2dfSkettenis if (ret != 0 && bytes != 0)
1093*af70c2dfSkettenis {
1094*af70c2dfSkettenis if (ret == (size_t)-1 || ret == (size_t)-2)
1095*af70c2dfSkettenis memmove (old+bytes, old+1, strlen (old+1));
1096*af70c2dfSkettenis else
1097*af70c2dfSkettenis memmove (old+bytes, old+ret, strlen (old+ret));
1098*af70c2dfSkettenis memcpy (old, new, bytes);
1099*af70c2dfSkettenis }
1100*af70c2dfSkettenis }
1101*af70c2dfSkettenis else
1102*af70c2dfSkettenis {
1103*af70c2dfSkettenis putc (' ', rl_outstream);
1104*af70c2dfSkettenis _rl_last_c_pos = 1;
1105*af70c2dfSkettenis _rl_last_v_pos++;
1106*af70c2dfSkettenis if (old[0] && new[0])
1107*af70c2dfSkettenis old[0] = new[0];
1108*af70c2dfSkettenis }
1109*af70c2dfSkettenis }
1110*af70c2dfSkettenis else
1111*af70c2dfSkettenis #endif
1112*af70c2dfSkettenis {
11131acd27e7Smillert if (new[0])
11141acd27e7Smillert putc (new[0], rl_outstream);
11151acd27e7Smillert else
11161acd27e7Smillert putc (' ', rl_outstream);
11171acd27e7Smillert _rl_last_c_pos = 1; /* XXX */
11181acd27e7Smillert _rl_last_v_pos++;
11191acd27e7Smillert if (old[0] && new[0])
11201acd27e7Smillert old[0] = new[0];
11211acd27e7Smillert }
1122*af70c2dfSkettenis }
1123*af70c2dfSkettenis
11241acd27e7Smillert
11251acd27e7Smillert /* Find first difference. */
1126*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1127*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1128*af70c2dfSkettenis {
1129*af70c2dfSkettenis memset (&ps_new, 0, sizeof(mbstate_t));
1130*af70c2dfSkettenis memset (&ps_old, 0, sizeof(mbstate_t));
1131*af70c2dfSkettenis
1132*af70c2dfSkettenis new_offset = old_offset = 0;
1133*af70c2dfSkettenis for (ofd = old, nfd = new;
1134*af70c2dfSkettenis (ofd - old < omax) && *ofd &&
1135*af70c2dfSkettenis _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); )
1136*af70c2dfSkettenis {
1137*af70c2dfSkettenis old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY);
1138*af70c2dfSkettenis new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY);
1139*af70c2dfSkettenis ofd = old + old_offset;
1140*af70c2dfSkettenis nfd = new + new_offset;
1141*af70c2dfSkettenis }
1142*af70c2dfSkettenis }
1143*af70c2dfSkettenis else
1144*af70c2dfSkettenis #endif
11451acd27e7Smillert for (ofd = old, nfd = new;
11461acd27e7Smillert (ofd - old < omax) && *ofd && (*ofd == *nfd);
11471acd27e7Smillert ofd++, nfd++)
11481acd27e7Smillert ;
11491acd27e7Smillert
11501acd27e7Smillert /* Move to the end of the screen line. ND and OD are used to keep track
11511acd27e7Smillert of the distance between ne and new and oe and old, respectively, to
11521acd27e7Smillert move a subtraction out of each loop. */
11531acd27e7Smillert for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
11541acd27e7Smillert for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
11551acd27e7Smillert
11561acd27e7Smillert /* If no difference, continue to next line. */
11571acd27e7Smillert if (ofd == oe && nfd == ne)
11581acd27e7Smillert return;
11591acd27e7Smillert
11601acd27e7Smillert wsatend = 1; /* flag for trailing whitespace */
1161*af70c2dfSkettenis
1162*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1163*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1164*af70c2dfSkettenis {
1165*af70c2dfSkettenis ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY);
1166*af70c2dfSkettenis nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY);
1167*af70c2dfSkettenis while ((ols > ofd) && (nls > nfd))
1168*af70c2dfSkettenis {
1169*af70c2dfSkettenis memset (&ps_old, 0, sizeof (mbstate_t));
1170*af70c2dfSkettenis memset (&ps_new, 0, sizeof (mbstate_t));
1171*af70c2dfSkettenis
1172*af70c2dfSkettenis _rl_adjust_point (old, ols - old, &ps_old);
1173*af70c2dfSkettenis _rl_adjust_point (new, nls - new, &ps_new);
1174*af70c2dfSkettenis
1175*af70c2dfSkettenis if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0)
1176*af70c2dfSkettenis break;
1177*af70c2dfSkettenis
1178*af70c2dfSkettenis if (*ols == ' ')
1179*af70c2dfSkettenis wsatend = 0;
1180*af70c2dfSkettenis
1181*af70c2dfSkettenis ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY);
1182*af70c2dfSkettenis nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY);
1183*af70c2dfSkettenis }
1184*af70c2dfSkettenis }
1185*af70c2dfSkettenis else
1186*af70c2dfSkettenis {
1187*af70c2dfSkettenis #endif /* HANDLE_MULTIBYTE */
11881acd27e7Smillert ols = oe - 1; /* find last same */
11891acd27e7Smillert nls = ne - 1;
11901acd27e7Smillert while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
11911acd27e7Smillert {
11921acd27e7Smillert if (*ols != ' ')
11931acd27e7Smillert wsatend = 0;
11941acd27e7Smillert ols--;
11951acd27e7Smillert nls--;
11961acd27e7Smillert }
1197*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1198*af70c2dfSkettenis }
1199*af70c2dfSkettenis #endif
12001acd27e7Smillert
12011acd27e7Smillert if (wsatend)
12021acd27e7Smillert {
12031acd27e7Smillert ols = oe;
12041acd27e7Smillert nls = ne;
12051acd27e7Smillert }
1206*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1207*af70c2dfSkettenis /* This may not work for stateful encoding, but who cares? To handle
1208*af70c2dfSkettenis stateful encoding properly, we have to scan each string from the
1209*af70c2dfSkettenis beginning and compare. */
1210*af70c2dfSkettenis else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0)
1211*af70c2dfSkettenis #else
12121acd27e7Smillert else if (*ols != *nls)
1213*af70c2dfSkettenis #endif
12141acd27e7Smillert {
12151acd27e7Smillert if (*ols) /* don't step past the NUL */
1216*af70c2dfSkettenis {
1217*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1218*af70c2dfSkettenis ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY);
1219*af70c2dfSkettenis else
12201acd27e7Smillert ols++;
1221*af70c2dfSkettenis }
12221acd27e7Smillert if (*nls)
1223*af70c2dfSkettenis {
1224*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1225*af70c2dfSkettenis nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY);
1226*af70c2dfSkettenis else
12271acd27e7Smillert nls++;
12281acd27e7Smillert }
1229*af70c2dfSkettenis }
12301acd27e7Smillert
12311acd27e7Smillert /* count of invisible characters in the current invisible line. */
12321acd27e7Smillert current_invis_chars = W_OFFSET (current_line, wrap_offset);
12331acd27e7Smillert if (_rl_last_v_pos != current_line)
12341acd27e7Smillert {
12351acd27e7Smillert _rl_move_vert (current_line);
12361acd27e7Smillert if (current_line == 0 && visible_wrap_offset)
12371acd27e7Smillert _rl_last_c_pos += visible_wrap_offset;
12381acd27e7Smillert }
12391acd27e7Smillert
12401acd27e7Smillert /* If this is the first line and there are invisible characters in the
12411acd27e7Smillert prompt string, and the prompt string has not changed, and the current
12421acd27e7Smillert cursor position is before the last invisible character in the prompt,
12431acd27e7Smillert and the index of the character to move to is past the end of the prompt
12441acd27e7Smillert string, then redraw the entire prompt string. We can only do this
12451acd27e7Smillert reliably if the terminal supports a `cr' capability.
12461acd27e7Smillert
12471acd27e7Smillert This is not an efficiency hack -- there is a problem with redrawing
12481acd27e7Smillert portions of the prompt string if they contain terminal escape
12491acd27e7Smillert sequences (like drawing the `unbold' sequence without a corresponding
12501acd27e7Smillert `bold') that manifests itself on certain terminals. */
12511acd27e7Smillert
12521acd27e7Smillert lendiff = local_prompt ? strlen (local_prompt) : 0;
12531acd27e7Smillert od = ofd - old; /* index of first difference in visible line */
12541acd27e7Smillert if (current_line == 0 && !_rl_horizontal_scroll_mode &&
1255*af70c2dfSkettenis _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 &&
1256*af70c2dfSkettenis od >= lendiff && _rl_last_c_pos <= prompt_last_invisible)
12571acd27e7Smillert {
12581acd27e7Smillert #if defined (__MSDOS__)
12591acd27e7Smillert putc ('\r', rl_outstream);
12601acd27e7Smillert #else
1261*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
12621acd27e7Smillert #endif
12631acd27e7Smillert _rl_output_some_chars (local_prompt, lendiff);
1264*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1265*af70c2dfSkettenis _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff);
1266*af70c2dfSkettenis else
12671acd27e7Smillert _rl_last_c_pos = lendiff;
12681acd27e7Smillert }
12691acd27e7Smillert
12701acd27e7Smillert _rl_move_cursor_relative (od, old);
12711acd27e7Smillert
1272*af70c2dfSkettenis /* if (len (new) > len (old))
1273*af70c2dfSkettenis lendiff == difference in buffer
1274*af70c2dfSkettenis col_lendiff == difference on screen
1275*af70c2dfSkettenis When not using multibyte characters, these are equal */
12761acd27e7Smillert lendiff = (nls - nfd) - (ols - ofd);
1277*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1278*af70c2dfSkettenis col_lendiff = _rl_col_width (new, nfd - new, nls - new) - _rl_col_width (old, ofd - old, ols - old);
1279*af70c2dfSkettenis else
1280*af70c2dfSkettenis col_lendiff = lendiff;
12811acd27e7Smillert
12821acd27e7Smillert /* If we are changing the number of invisible characters in a line, and
12831acd27e7Smillert the spot of first difference is before the end of the invisible chars,
12841acd27e7Smillert lendiff needs to be adjusted. */
12851acd27e7Smillert if (current_line == 0 && !_rl_horizontal_scroll_mode &&
12861acd27e7Smillert current_invis_chars != visible_wrap_offset)
1287*af70c2dfSkettenis {
1288*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1289*af70c2dfSkettenis {
12901acd27e7Smillert lendiff += visible_wrap_offset - current_invis_chars;
1291*af70c2dfSkettenis col_lendiff += visible_wrap_offset - current_invis_chars;
1292*af70c2dfSkettenis }
1293*af70c2dfSkettenis else
1294*af70c2dfSkettenis {
1295*af70c2dfSkettenis lendiff += visible_wrap_offset - current_invis_chars;
1296*af70c2dfSkettenis col_lendiff = lendiff;
1297*af70c2dfSkettenis }
1298*af70c2dfSkettenis }
12991acd27e7Smillert
13001acd27e7Smillert /* Insert (diff (len (old), len (new)) ch. */
13011acd27e7Smillert temp = ne - nfd;
1302*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1303*af70c2dfSkettenis col_temp = _rl_col_width (new, nfd - new, ne - new);
1304*af70c2dfSkettenis else
1305*af70c2dfSkettenis col_temp = temp;
1306*af70c2dfSkettenis
1307*af70c2dfSkettenis if (col_lendiff > 0) /* XXX - was lendiff */
13081acd27e7Smillert {
13091acd27e7Smillert /* Non-zero if we're increasing the number of lines. */
13101acd27e7Smillert int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
13111acd27e7Smillert /* Sometimes it is cheaper to print the characters rather than
13121acd27e7Smillert use the terminal's capabilities. If we're growing the number
13131acd27e7Smillert of lines, make sure we actually cause the new line to wrap
13141acd27e7Smillert around on auto-wrapping terminals. */
1315*af70c2dfSkettenis if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl))
13161acd27e7Smillert {
1317*af70c2dfSkettenis /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and
13181acd27e7Smillert _rl_horizontal_scroll_mode == 1, inserting the characters with
1319*af70c2dfSkettenis _rl_term_IC or _rl_term_ic will screw up the screen because of the
13201acd27e7Smillert invisible characters. We need to just draw them. */
13211acd27e7Smillert if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
1322*af70c2dfSkettenis lendiff <= prompt_visible_length || !current_invis_chars))
13231acd27e7Smillert {
1324*af70c2dfSkettenis insert_some_chars (nfd, lendiff, col_lendiff);
1325*af70c2dfSkettenis _rl_last_c_pos += col_lendiff;
13261acd27e7Smillert }
13271acd27e7Smillert else if (*ols == 0)
13281acd27e7Smillert {
13291acd27e7Smillert /* At the end of a line the characters do not have to
13301acd27e7Smillert be "inserted". They can just be placed on the screen. */
13311acd27e7Smillert /* However, this screws up the rest of this block, which
13321acd27e7Smillert assumes you've done the insert because you can. */
13331acd27e7Smillert _rl_output_some_chars (nfd, lendiff);
1334*af70c2dfSkettenis _rl_last_c_pos += col_lendiff;
13351acd27e7Smillert }
13361acd27e7Smillert else
13371acd27e7Smillert {
13381acd27e7Smillert /* We have horizontal scrolling and we are not inserting at
13391acd27e7Smillert the end. We have invisible characters in this line. This
13401acd27e7Smillert is a dumb update. */
13411acd27e7Smillert _rl_output_some_chars (nfd, temp);
1342*af70c2dfSkettenis _rl_last_c_pos += col_temp;
13431acd27e7Smillert return;
13441acd27e7Smillert }
13451acd27e7Smillert /* Copy (new) chars to screen from first diff to last match. */
13461acd27e7Smillert temp = nls - nfd;
13471acd27e7Smillert if ((temp - lendiff) > 0)
13481acd27e7Smillert {
13491acd27e7Smillert _rl_output_some_chars (nfd + lendiff, temp - lendiff);
1350*af70c2dfSkettenis #if 0
1351*af70c2dfSkettenis _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-col_lendiff);
1352*af70c2dfSkettenis #else
1353*af70c2dfSkettenis _rl_last_c_pos += _rl_col_width (nfd+lendiff, 0, temp-lendiff);
1354*af70c2dfSkettenis #endif
13551acd27e7Smillert }
13561acd27e7Smillert }
13571acd27e7Smillert else
13581acd27e7Smillert {
13591acd27e7Smillert /* cannot insert chars, write to EOL */
13601acd27e7Smillert _rl_output_some_chars (nfd, temp);
1361*af70c2dfSkettenis _rl_last_c_pos += col_temp;
13621acd27e7Smillert }
13631acd27e7Smillert }
13641acd27e7Smillert else /* Delete characters from line. */
13651acd27e7Smillert {
13661acd27e7Smillert /* If possible and inexpensive to use terminal deletion, then do so. */
1367*af70c2dfSkettenis if (_rl_term_dc && (2 * col_temp) >= -col_lendiff)
13681acd27e7Smillert {
13691acd27e7Smillert /* If all we're doing is erasing the invisible characters in the
13701acd27e7Smillert prompt string, don't bother. It screws up the assumptions
13711acd27e7Smillert about what's on the screen. */
13721acd27e7Smillert if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
13731acd27e7Smillert -lendiff == visible_wrap_offset)
1374*af70c2dfSkettenis col_lendiff = 0;
13751acd27e7Smillert
1376*af70c2dfSkettenis if (col_lendiff)
1377*af70c2dfSkettenis delete_chars (-col_lendiff); /* delete (diff) characters */
13781acd27e7Smillert
13791acd27e7Smillert /* Copy (new) chars to screen from first diff to last match */
13801acd27e7Smillert temp = nls - nfd;
13811acd27e7Smillert if (temp > 0)
13821acd27e7Smillert {
13831acd27e7Smillert _rl_output_some_chars (nfd, temp);
1384*af70c2dfSkettenis _rl_last_c_pos += _rl_col_width (nfd, 0, temp);;
13851acd27e7Smillert }
13861acd27e7Smillert }
13871acd27e7Smillert /* Otherwise, print over the existing material. */
13881acd27e7Smillert else
13891acd27e7Smillert {
13901acd27e7Smillert if (temp > 0)
13911acd27e7Smillert {
13921acd27e7Smillert _rl_output_some_chars (nfd, temp);
1393*af70c2dfSkettenis _rl_last_c_pos += col_temp;
13941acd27e7Smillert }
13951acd27e7Smillert lendiff = (oe - old) - (ne - new);
1396*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1397*af70c2dfSkettenis col_lendiff = _rl_col_width (old, 0, oe - old) - _rl_col_width (new, 0, ne - new);
1398*af70c2dfSkettenis else
1399*af70c2dfSkettenis col_lendiff = lendiff;
1400*af70c2dfSkettenis
1401*af70c2dfSkettenis if (col_lendiff)
14021acd27e7Smillert {
14031acd27e7Smillert if (_rl_term_autowrap && current_line < inv_botlin)
1404*af70c2dfSkettenis space_to_eol (col_lendiff);
14051acd27e7Smillert else
1406*af70c2dfSkettenis _rl_clear_to_eol (col_lendiff);
14071acd27e7Smillert }
14081acd27e7Smillert }
14091acd27e7Smillert }
14101acd27e7Smillert }
14111acd27e7Smillert
14121acd27e7Smillert /* Tell the update routines that we have moved onto a new (empty) line. */
14131acd27e7Smillert int
rl_on_new_line()14141acd27e7Smillert rl_on_new_line ()
14151acd27e7Smillert {
14161acd27e7Smillert if (visible_line)
14171acd27e7Smillert visible_line[0] = '\0';
14181acd27e7Smillert
14191acd27e7Smillert _rl_last_c_pos = _rl_last_v_pos = 0;
14201acd27e7Smillert _rl_vis_botlin = last_lmargin = 0;
14211acd27e7Smillert if (vis_lbreaks)
14221acd27e7Smillert vis_lbreaks[0] = vis_lbreaks[1] = 0;
14231acd27e7Smillert visible_wrap_offset = 0;
14241acd27e7Smillert return 0;
14251acd27e7Smillert }
14261acd27e7Smillert
14271acd27e7Smillert /* Tell the update routines that we have moved onto a new line with the
14281acd27e7Smillert prompt already displayed. Code originally from the version of readline
14291acd27e7Smillert distributed with CLISP. */
14301acd27e7Smillert int
rl_on_new_line_with_prompt()14311acd27e7Smillert rl_on_new_line_with_prompt ()
14321acd27e7Smillert {
14331acd27e7Smillert int prompt_size, i, l, real_screenwidth, newlines;
14341acd27e7Smillert char *prompt_last_line;
14351acd27e7Smillert
14361acd27e7Smillert /* Initialize visible_line and invisible_line to ensure that they can hold
14371acd27e7Smillert the already-displayed prompt. */
14381acd27e7Smillert prompt_size = strlen (rl_prompt) + 1;
14391acd27e7Smillert init_line_structures (prompt_size);
14401acd27e7Smillert
14411acd27e7Smillert /* Make sure the line structures hold the already-displayed prompt for
14421acd27e7Smillert redisplay. */
144363bef317Sbeck strlcpy (visible_line, rl_prompt, line_size);
144463bef317Sbeck strlcpy (invisible_line, rl_prompt, line_size);
14451acd27e7Smillert
14461acd27e7Smillert /* If the prompt contains newlines, take the last tail. */
14471acd27e7Smillert prompt_last_line = strrchr (rl_prompt, '\n');
14481acd27e7Smillert if (!prompt_last_line)
14491acd27e7Smillert prompt_last_line = rl_prompt;
14501acd27e7Smillert
14511acd27e7Smillert l = strlen (prompt_last_line);
1452*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1453*af70c2dfSkettenis _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l);
1454*af70c2dfSkettenis else
14551acd27e7Smillert _rl_last_c_pos = l;
14561acd27e7Smillert
14571acd27e7Smillert /* Dissect prompt_last_line into screen lines. Note that here we have
14581acd27e7Smillert to use the real screenwidth. Readline's notion of screenwidth might be
14591acd27e7Smillert one less, see terminal.c. */
1460*af70c2dfSkettenis real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1);
14611acd27e7Smillert _rl_last_v_pos = l / real_screenwidth;
14621acd27e7Smillert /* If the prompt length is a multiple of real_screenwidth, we don't know
14631acd27e7Smillert whether the cursor is at the end of the last line, or already at the
14641acd27e7Smillert beginning of the next line. Output a newline just to be safe. */
14651acd27e7Smillert if (l > 0 && (l % real_screenwidth) == 0)
14661acd27e7Smillert _rl_output_some_chars ("\n", 1);
14671acd27e7Smillert last_lmargin = 0;
14681acd27e7Smillert
14691acd27e7Smillert newlines = 0; i = 0;
14701acd27e7Smillert while (i <= l)
14711acd27e7Smillert {
14721acd27e7Smillert _rl_vis_botlin = newlines;
14731acd27e7Smillert vis_lbreaks[newlines++] = i;
14741acd27e7Smillert i += real_screenwidth;
14751acd27e7Smillert }
14761acd27e7Smillert vis_lbreaks[newlines] = l;
14771acd27e7Smillert visible_wrap_offset = 0;
14781acd27e7Smillert
14791acd27e7Smillert return 0;
14801acd27e7Smillert }
14811acd27e7Smillert
14821acd27e7Smillert /* Actually update the display, period. */
14831acd27e7Smillert int
rl_forced_update_display()14841acd27e7Smillert rl_forced_update_display ()
14851acd27e7Smillert {
14861acd27e7Smillert if (visible_line)
14871acd27e7Smillert {
14881acd27e7Smillert register char *temp = visible_line;
14891acd27e7Smillert
14901acd27e7Smillert while (*temp)
14911acd27e7Smillert *temp++ = '\0';
14921acd27e7Smillert }
14931acd27e7Smillert rl_on_new_line ();
14941acd27e7Smillert forced_display++;
14951acd27e7Smillert (*rl_redisplay_function) ();
14961acd27e7Smillert return 0;
14971acd27e7Smillert }
14981acd27e7Smillert
14991acd27e7Smillert /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
15001acd27e7Smillert DATA is the contents of the screen line of interest; i.e., where
15011acd27e7Smillert the movement is being done. */
15021acd27e7Smillert void
_rl_move_cursor_relative(new,data)15031acd27e7Smillert _rl_move_cursor_relative (new, data)
15041acd27e7Smillert int new;
1505*af70c2dfSkettenis const char *data;
15061acd27e7Smillert {
15071acd27e7Smillert register int i;
15081acd27e7Smillert
15091acd27e7Smillert /* If we don't have to do anything, then return. */
1510*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1511*af70c2dfSkettenis /* If we have multibyte characters, NEW is indexed by the buffer point in
1512*af70c2dfSkettenis a multibyte string, but _rl_last_c_pos is the display position. In
1513*af70c2dfSkettenis this case, NEW's display position is not obvious and must be
1514*af70c2dfSkettenis calculated. */
1515*af70c2dfSkettenis if (MB_CUR_MAX == 1 || rl_byte_oriented)
1516*af70c2dfSkettenis {
1517*af70c2dfSkettenis if (_rl_last_c_pos == new)
1518*af70c2dfSkettenis return;
1519*af70c2dfSkettenis }
1520*af70c2dfSkettenis else if (_rl_last_c_pos == _rl_col_width (data, 0, new))
1521*af70c2dfSkettenis return;
1522*af70c2dfSkettenis #else
15231acd27e7Smillert if (_rl_last_c_pos == new) return;
1524*af70c2dfSkettenis #endif
15251acd27e7Smillert
15261acd27e7Smillert /* It may be faster to output a CR, and then move forwards instead
15271acd27e7Smillert of moving backwards. */
15281acd27e7Smillert /* i == current physical cursor position. */
15291acd27e7Smillert i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
15301acd27e7Smillert if (new == 0 || CR_FASTER (new, _rl_last_c_pos) ||
1531*af70c2dfSkettenis (_rl_term_autowrap && i == _rl_screenwidth))
15321acd27e7Smillert {
15331acd27e7Smillert #if defined (__MSDOS__)
15341acd27e7Smillert putc ('\r', rl_outstream);
15351acd27e7Smillert #else
1536*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
15371acd27e7Smillert #endif /* !__MSDOS__ */
15381acd27e7Smillert _rl_last_c_pos = 0;
15391acd27e7Smillert }
15401acd27e7Smillert
15411acd27e7Smillert if (_rl_last_c_pos < new)
15421acd27e7Smillert {
15431acd27e7Smillert /* Move the cursor forward. We do it by printing the command
15441acd27e7Smillert to move the cursor forward if there is one, else print that
15451acd27e7Smillert portion of the output buffer again. Which is cheaper? */
15461acd27e7Smillert
15471acd27e7Smillert /* The above comment is left here for posterity. It is faster
15481acd27e7Smillert to print one character (non-control) than to print a control
15491acd27e7Smillert sequence telling the terminal to move forward one character.
15501acd27e7Smillert That kind of control is for people who don't know what the
15511acd27e7Smillert data is underneath the cursor. */
15521acd27e7Smillert #if defined (HACK_TERMCAP_MOTION)
1553*af70c2dfSkettenis if (_rl_term_forward_char)
1554*af70c2dfSkettenis {
1555*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1556*af70c2dfSkettenis {
1557*af70c2dfSkettenis int width;
1558*af70c2dfSkettenis width = _rl_col_width (data, _rl_last_c_pos, new);
1559*af70c2dfSkettenis for (i = 0; i < width; i++)
1560*af70c2dfSkettenis tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1561*af70c2dfSkettenis }
1562*af70c2dfSkettenis else
1563*af70c2dfSkettenis {
15641acd27e7Smillert for (i = _rl_last_c_pos; i < new; i++)
1565*af70c2dfSkettenis tputs (_rl_term_forward_char, 1, _rl_output_character_function);
1566*af70c2dfSkettenis }
1567*af70c2dfSkettenis }
1568*af70c2dfSkettenis else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1569*af70c2dfSkettenis {
1570*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
1571*af70c2dfSkettenis for (i = 0; i < new; i++)
1572*af70c2dfSkettenis putc (data[i], rl_outstream);
1573*af70c2dfSkettenis }
15741acd27e7Smillert else
15751acd27e7Smillert for (i = _rl_last_c_pos; i < new; i++)
15761acd27e7Smillert putc (data[i], rl_outstream);
1577*af70c2dfSkettenis
1578*af70c2dfSkettenis #else /* !HACK_TERMCAP_MOTION */
1579*af70c2dfSkettenis
1580*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1581*af70c2dfSkettenis {
1582*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
1583*af70c2dfSkettenis for (i = 0; i < new; i++)
1584*af70c2dfSkettenis putc (data[i], rl_outstream);
1585*af70c2dfSkettenis }
1586*af70c2dfSkettenis else
15871acd27e7Smillert for (i = _rl_last_c_pos; i < new; i++)
15881acd27e7Smillert putc (data[i], rl_outstream);
1589*af70c2dfSkettenis
1590*af70c2dfSkettenis #endif /* !HACK_TERMCAP_MOTION */
1591*af70c2dfSkettenis
15921acd27e7Smillert }
1593*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
1594*af70c2dfSkettenis /* NEW points to the buffer point, but _rl_last_c_pos is the display point.
1595*af70c2dfSkettenis The byte length of the string is probably bigger than the column width
1596*af70c2dfSkettenis of the string, which means that if NEW == _rl_last_c_pos, then NEW's
1597*af70c2dfSkettenis display point is less than _rl_last_c_pos. */
1598*af70c2dfSkettenis else if (_rl_last_c_pos >= new)
1599*af70c2dfSkettenis #else
16001acd27e7Smillert else if (_rl_last_c_pos > new)
1601*af70c2dfSkettenis #endif
1602*af70c2dfSkettenis {
1603*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1604*af70c2dfSkettenis _rl_backspace (_rl_last_c_pos - _rl_col_width (data, 0, new));
1605*af70c2dfSkettenis else
16061acd27e7Smillert _rl_backspace (_rl_last_c_pos - new);
1607*af70c2dfSkettenis }
1608*af70c2dfSkettenis
1609*af70c2dfSkettenis if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1610*af70c2dfSkettenis _rl_last_c_pos = _rl_col_width (data, 0, new);
1611*af70c2dfSkettenis else
16121acd27e7Smillert _rl_last_c_pos = new;
16131acd27e7Smillert }
16141acd27e7Smillert
16151acd27e7Smillert /* PWP: move the cursor up or down. */
16161acd27e7Smillert void
_rl_move_vert(to)16171acd27e7Smillert _rl_move_vert (to)
16181acd27e7Smillert int to;
16191acd27e7Smillert {
16201acd27e7Smillert register int delta, i;
16211acd27e7Smillert
1622*af70c2dfSkettenis if (_rl_last_v_pos == to || to > _rl_screenheight)
16231acd27e7Smillert return;
16241acd27e7Smillert
16251acd27e7Smillert if ((delta = to - _rl_last_v_pos) > 0)
16261acd27e7Smillert {
16271acd27e7Smillert for (i = 0; i < delta; i++)
16281acd27e7Smillert putc ('\n', rl_outstream);
16291acd27e7Smillert #if defined (__MSDOS__)
16301acd27e7Smillert putc ('\r', rl_outstream);
16311acd27e7Smillert #else
1632*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
16331acd27e7Smillert #endif
16341acd27e7Smillert _rl_last_c_pos = 0;
16351acd27e7Smillert }
16361acd27e7Smillert else
16371acd27e7Smillert { /* delta < 0 */
1638*af70c2dfSkettenis if (_rl_term_up && *_rl_term_up)
16391acd27e7Smillert for (i = 0; i < -delta; i++)
1640*af70c2dfSkettenis tputs (_rl_term_up, 1, _rl_output_character_function);
16411acd27e7Smillert }
16421acd27e7Smillert
16431acd27e7Smillert _rl_last_v_pos = to; /* Now TO is here */
16441acd27e7Smillert }
16451acd27e7Smillert
16461acd27e7Smillert /* Physically print C on rl_outstream. This is for functions which know
16471acd27e7Smillert how to optimize the display. Return the number of characters output. */
16481acd27e7Smillert int
rl_show_char(c)16491acd27e7Smillert rl_show_char (c)
16501acd27e7Smillert int c;
16511acd27e7Smillert {
16521acd27e7Smillert int n = 1;
16531acd27e7Smillert if (META_CHAR (c) && (_rl_output_meta_chars == 0))
16541acd27e7Smillert {
16551acd27e7Smillert fprintf (rl_outstream, "M-");
16561acd27e7Smillert n += 2;
16571acd27e7Smillert c = UNMETA (c);
16581acd27e7Smillert }
16591acd27e7Smillert
16601acd27e7Smillert #if defined (DISPLAY_TABS)
16611acd27e7Smillert if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)
16621acd27e7Smillert #else
16631acd27e7Smillert if (CTRL_CHAR (c) || c == RUBOUT)
16641acd27e7Smillert #endif /* !DISPLAY_TABS */
16651acd27e7Smillert {
16661acd27e7Smillert fprintf (rl_outstream, "C-");
16671acd27e7Smillert n += 2;
16681acd27e7Smillert c = CTRL_CHAR (c) ? UNCTRL (c) : '?';
16691acd27e7Smillert }
16701acd27e7Smillert
16711acd27e7Smillert putc (c, rl_outstream);
16721acd27e7Smillert fflush (rl_outstream);
16731acd27e7Smillert return n;
16741acd27e7Smillert }
16751acd27e7Smillert
16761acd27e7Smillert int
rl_character_len(c,pos)16771acd27e7Smillert rl_character_len (c, pos)
16781acd27e7Smillert register int c, pos;
16791acd27e7Smillert {
16801acd27e7Smillert unsigned char uc;
16811acd27e7Smillert
16821acd27e7Smillert uc = (unsigned char)c;
16831acd27e7Smillert
16841acd27e7Smillert if (META_CHAR (uc))
16851acd27e7Smillert return ((_rl_output_meta_chars == 0) ? 4 : 1);
16861acd27e7Smillert
16871acd27e7Smillert if (uc == '\t')
16881acd27e7Smillert {
16891acd27e7Smillert #if defined (DISPLAY_TABS)
16901acd27e7Smillert return (((pos | 7) + 1) - pos);
16911acd27e7Smillert #else
16921acd27e7Smillert return (2);
16931acd27e7Smillert #endif /* !DISPLAY_TABS */
16941acd27e7Smillert }
16951acd27e7Smillert
16961acd27e7Smillert if (CTRL_CHAR (c) || c == RUBOUT)
16971acd27e7Smillert return (2);
16981acd27e7Smillert
1699*af70c2dfSkettenis return ((ISPRINT (uc)) ? 1 : 2);
17001acd27e7Smillert }
17011acd27e7Smillert
17021acd27e7Smillert /* How to print things in the "echo-area". The prompt is treated as a
17031acd27e7Smillert mini-modeline. */
17041acd27e7Smillert
17051acd27e7Smillert #if defined (USE_VARARGS)
17061acd27e7Smillert int
17071acd27e7Smillert #if defined (PREFER_STDARG)
rl_message(const char * format,...)17081acd27e7Smillert rl_message (const char *format, ...)
17091acd27e7Smillert #else
17101acd27e7Smillert rl_message (va_alist)
17111acd27e7Smillert va_dcl
17121acd27e7Smillert #endif
17131acd27e7Smillert {
17141acd27e7Smillert va_list args;
17151acd27e7Smillert #if defined (PREFER_VARARGS)
17161acd27e7Smillert char *format;
17171acd27e7Smillert #endif
17181acd27e7Smillert
17191acd27e7Smillert #if defined (PREFER_STDARG)
17201acd27e7Smillert va_start (args, format);
17211acd27e7Smillert #else
17221acd27e7Smillert va_start (args);
17231acd27e7Smillert format = va_arg (args, char *);
17241acd27e7Smillert #endif
17251acd27e7Smillert
1726*af70c2dfSkettenis #if defined (HAVE_VSNPRINTF)
1727*af70c2dfSkettenis vsnprintf (msg_buf, sizeof (msg_buf) - 1, format, args);
1728*af70c2dfSkettenis #else
1729*af70c2dfSkettenis vsprintf (msg_buf, format, args);
1730*af70c2dfSkettenis msg_buf[sizeof(msg_buf) - 1] = '\0'; /* overflow? */
1731*af70c2dfSkettenis #endif
17321acd27e7Smillert va_end (args);
17331acd27e7Smillert
17341acd27e7Smillert rl_display_prompt = msg_buf;
17351acd27e7Smillert (*rl_redisplay_function) ();
17361acd27e7Smillert return 0;
17371acd27e7Smillert }
17381acd27e7Smillert #else /* !USE_VARARGS */
17391acd27e7Smillert int
rl_message(format,arg1,arg2)17401acd27e7Smillert rl_message (format, arg1, arg2)
17411acd27e7Smillert char *format;
17421acd27e7Smillert {
174363bef317Sbeck snprintf (msg_buf, sizeof(msg_buf), format, arg1, arg2);
17441acd27e7Smillert rl_display_prompt = msg_buf;
17451acd27e7Smillert (*rl_redisplay_function) ();
17461acd27e7Smillert return 0;
17471acd27e7Smillert }
17481acd27e7Smillert #endif /* !USE_VARARGS */
17491acd27e7Smillert
17501acd27e7Smillert /* How to clear things from the "echo-area". */
17511acd27e7Smillert int
rl_clear_message()17521acd27e7Smillert rl_clear_message ()
17531acd27e7Smillert {
17541acd27e7Smillert rl_display_prompt = rl_prompt;
17551acd27e7Smillert (*rl_redisplay_function) ();
17561acd27e7Smillert return 0;
17571acd27e7Smillert }
17581acd27e7Smillert
17591acd27e7Smillert int
rl_reset_line_state()17601acd27e7Smillert rl_reset_line_state ()
17611acd27e7Smillert {
17621acd27e7Smillert rl_on_new_line ();
17631acd27e7Smillert
17641acd27e7Smillert rl_display_prompt = rl_prompt ? rl_prompt : "";
17651acd27e7Smillert forced_display = 1;
17661acd27e7Smillert return 0;
17671acd27e7Smillert }
17681acd27e7Smillert
17691acd27e7Smillert static char *saved_local_prompt;
17701acd27e7Smillert static char *saved_local_prefix;
17711acd27e7Smillert static int saved_last_invisible;
17721acd27e7Smillert static int saved_visible_length;
17731acd27e7Smillert
17741acd27e7Smillert void
rl_save_prompt()17751acd27e7Smillert rl_save_prompt ()
17761acd27e7Smillert {
17771acd27e7Smillert saved_local_prompt = local_prompt;
17781acd27e7Smillert saved_local_prefix = local_prompt_prefix;
1779*af70c2dfSkettenis saved_last_invisible = prompt_last_invisible;
1780*af70c2dfSkettenis saved_visible_length = prompt_visible_length;
17811acd27e7Smillert
17821acd27e7Smillert local_prompt = local_prompt_prefix = (char *)0;
1783*af70c2dfSkettenis prompt_last_invisible = prompt_visible_length = 0;
17841acd27e7Smillert }
17851acd27e7Smillert
17861acd27e7Smillert void
rl_restore_prompt()17871acd27e7Smillert rl_restore_prompt ()
17881acd27e7Smillert {
1789*af70c2dfSkettenis FREE (local_prompt);
1790*af70c2dfSkettenis FREE (local_prompt_prefix);
17911acd27e7Smillert
17921acd27e7Smillert local_prompt = saved_local_prompt;
17931acd27e7Smillert local_prompt_prefix = saved_local_prefix;
1794*af70c2dfSkettenis prompt_last_invisible = saved_last_invisible;
1795*af70c2dfSkettenis prompt_visible_length = saved_visible_length;
17961acd27e7Smillert }
17971acd27e7Smillert
17981acd27e7Smillert char *
_rl_make_prompt_for_search(pchar)17991acd27e7Smillert _rl_make_prompt_for_search (pchar)
18001acd27e7Smillert int pchar;
18011acd27e7Smillert {
18021acd27e7Smillert int len;
18031acd27e7Smillert char *pmt;
18041acd27e7Smillert
18051acd27e7Smillert rl_save_prompt ();
18061acd27e7Smillert
18071acd27e7Smillert if (saved_local_prompt == 0)
18081acd27e7Smillert {
18091acd27e7Smillert len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
1810*af70c2dfSkettenis pmt = (char *)xmalloc (len + 2);
18111acd27e7Smillert if (len)
181263bef317Sbeck strlcpy (pmt, rl_prompt, len + 2);
18131acd27e7Smillert pmt[len] = pchar;
18141acd27e7Smillert pmt[len+1] = '\0';
18151acd27e7Smillert }
18161acd27e7Smillert else
18171acd27e7Smillert {
18181acd27e7Smillert len = *saved_local_prompt ? strlen (saved_local_prompt) : 0;
1819*af70c2dfSkettenis pmt = (char *)xmalloc (len + 2);
18201acd27e7Smillert if (len)
182163bef317Sbeck strlcpy (pmt, saved_local_prompt, len + 2);
18221acd27e7Smillert pmt[len] = pchar;
18231acd27e7Smillert pmt[len+1] = '\0';
18241acd27e7Smillert local_prompt = savestring (pmt);
1825*af70c2dfSkettenis prompt_last_invisible = saved_last_invisible;
1826*af70c2dfSkettenis prompt_visible_length = saved_visible_length + 1;
18271acd27e7Smillert }
18281acd27e7Smillert return pmt;
18291acd27e7Smillert }
18301acd27e7Smillert
18311acd27e7Smillert /* Quick redisplay hack when erasing characters at the end of the line. */
18321acd27e7Smillert void
_rl_erase_at_end_of_line(l)18331acd27e7Smillert _rl_erase_at_end_of_line (l)
18341acd27e7Smillert int l;
18351acd27e7Smillert {
18361acd27e7Smillert register int i;
18371acd27e7Smillert
18381acd27e7Smillert _rl_backspace (l);
18391acd27e7Smillert for (i = 0; i < l; i++)
18401acd27e7Smillert putc (' ', rl_outstream);
18411acd27e7Smillert _rl_backspace (l);
18421acd27e7Smillert for (i = 0; i < l; i++)
18431acd27e7Smillert visible_line[--_rl_last_c_pos] = '\0';
18441acd27e7Smillert rl_display_fixed++;
18451acd27e7Smillert }
18461acd27e7Smillert
18471acd27e7Smillert /* Clear to the end of the line. COUNT is the minimum
18481acd27e7Smillert number of character spaces to clear, */
18491acd27e7Smillert void
_rl_clear_to_eol(count)18501acd27e7Smillert _rl_clear_to_eol (count)
18511acd27e7Smillert int count;
18521acd27e7Smillert {
1853*af70c2dfSkettenis if (_rl_term_clreol)
1854*af70c2dfSkettenis tputs (_rl_term_clreol, 1, _rl_output_character_function);
18551acd27e7Smillert else if (count)
18561acd27e7Smillert space_to_eol (count);
18571acd27e7Smillert }
18581acd27e7Smillert
18591acd27e7Smillert /* Clear to the end of the line using spaces. COUNT is the minimum
18601acd27e7Smillert number of character spaces to clear, */
18611acd27e7Smillert static void
space_to_eol(count)18621acd27e7Smillert space_to_eol (count)
18631acd27e7Smillert int count;
18641acd27e7Smillert {
18651acd27e7Smillert register int i;
18661acd27e7Smillert
18671acd27e7Smillert for (i = 0; i < count; i++)
18681acd27e7Smillert putc (' ', rl_outstream);
18691acd27e7Smillert
18701acd27e7Smillert _rl_last_c_pos += count;
18711acd27e7Smillert }
18721acd27e7Smillert
18731acd27e7Smillert void
_rl_clear_screen()18741acd27e7Smillert _rl_clear_screen ()
18751acd27e7Smillert {
1876*af70c2dfSkettenis if (_rl_term_clrpag)
1877*af70c2dfSkettenis tputs (_rl_term_clrpag, 1, _rl_output_character_function);
18781acd27e7Smillert else
1879*af70c2dfSkettenis rl_crlf ();
18801acd27e7Smillert }
18811acd27e7Smillert
1882*af70c2dfSkettenis /* Insert COUNT characters from STRING to the output stream at column COL. */
18831acd27e7Smillert static void
insert_some_chars(string,count,col)1884*af70c2dfSkettenis insert_some_chars (string, count, col)
18851acd27e7Smillert char *string;
1886*af70c2dfSkettenis int count, col;
18871acd27e7Smillert {
1888*af70c2dfSkettenis /* DEBUGGING */
1889*af70c2dfSkettenis if (MB_CUR_MAX == 1 || rl_byte_oriented)
1890*af70c2dfSkettenis if (count != col)
1891*af70c2dfSkettenis fprintf(stderr, "readline: debug: insert_some_chars: count (%d) != col (%d)\n", count, col);
1892*af70c2dfSkettenis
18931acd27e7Smillert /* If IC is defined, then we do not have to "enter" insert mode. */
1894*af70c2dfSkettenis if (_rl_term_IC)
18951acd27e7Smillert {
18961acd27e7Smillert char *buffer;
1897*af70c2dfSkettenis
1898*af70c2dfSkettenis buffer = tgoto (_rl_term_IC, 0, col);
18991acd27e7Smillert tputs (buffer, 1, _rl_output_character_function);
19001acd27e7Smillert _rl_output_some_chars (string, count);
19011acd27e7Smillert }
19021acd27e7Smillert else
19031acd27e7Smillert {
19041acd27e7Smillert register int i;
19051acd27e7Smillert
19061acd27e7Smillert /* If we have to turn on insert-mode, then do so. */
1907*af70c2dfSkettenis if (_rl_term_im && *_rl_term_im)
1908*af70c2dfSkettenis tputs (_rl_term_im, 1, _rl_output_character_function);
19091acd27e7Smillert
19101acd27e7Smillert /* If there is a special command for inserting characters, then
19111acd27e7Smillert use that first to open up the space. */
1912*af70c2dfSkettenis if (_rl_term_ic && *_rl_term_ic)
19131acd27e7Smillert {
1914*af70c2dfSkettenis for (i = col; i--; )
1915*af70c2dfSkettenis tputs (_rl_term_ic, 1, _rl_output_character_function);
19161acd27e7Smillert }
19171acd27e7Smillert
19181acd27e7Smillert /* Print the text. */
19191acd27e7Smillert _rl_output_some_chars (string, count);
19201acd27e7Smillert
19211acd27e7Smillert /* If there is a string to turn off insert mode, we had best use
19221acd27e7Smillert it now. */
1923*af70c2dfSkettenis if (_rl_term_ei && *_rl_term_ei)
1924*af70c2dfSkettenis tputs (_rl_term_ei, 1, _rl_output_character_function);
19251acd27e7Smillert }
19261acd27e7Smillert }
19271acd27e7Smillert
19281acd27e7Smillert /* Delete COUNT characters from the display line. */
19291acd27e7Smillert static void
delete_chars(count)19301acd27e7Smillert delete_chars (count)
19311acd27e7Smillert int count;
19321acd27e7Smillert {
1933*af70c2dfSkettenis if (count > _rl_screenwidth) /* XXX */
19341acd27e7Smillert return;
19351acd27e7Smillert
1936*af70c2dfSkettenis if (_rl_term_DC && *_rl_term_DC)
19371acd27e7Smillert {
19381acd27e7Smillert char *buffer;
1939*af70c2dfSkettenis buffer = tgoto (_rl_term_DC, count, count);
19401acd27e7Smillert tputs (buffer, count, _rl_output_character_function);
19411acd27e7Smillert }
19421acd27e7Smillert else
19431acd27e7Smillert {
1944*af70c2dfSkettenis if (_rl_term_dc && *_rl_term_dc)
19451acd27e7Smillert while (count--)
1946*af70c2dfSkettenis tputs (_rl_term_dc, 1, _rl_output_character_function);
19471acd27e7Smillert }
19481acd27e7Smillert }
19491acd27e7Smillert
19501acd27e7Smillert void
_rl_update_final()19511acd27e7Smillert _rl_update_final ()
19521acd27e7Smillert {
19531acd27e7Smillert int full_lines;
19541acd27e7Smillert
19551acd27e7Smillert full_lines = 0;
19561acd27e7Smillert /* If the cursor is the only thing on an otherwise-blank last line,
19571acd27e7Smillert compensate so we don't print an extra CRLF. */
19581acd27e7Smillert if (_rl_vis_botlin && _rl_last_c_pos == 0 &&
19591acd27e7Smillert visible_line[vis_lbreaks[_rl_vis_botlin]] == 0)
19601acd27e7Smillert {
19611acd27e7Smillert _rl_vis_botlin--;
19621acd27e7Smillert full_lines = 1;
19631acd27e7Smillert }
19641acd27e7Smillert _rl_move_vert (_rl_vis_botlin);
19651acd27e7Smillert /* If we've wrapped lines, remove the final xterm line-wrap flag. */
1966*af70c2dfSkettenis if (full_lines && _rl_term_autowrap && (VIS_LLEN(_rl_vis_botlin) == _rl_screenwidth))
19671acd27e7Smillert {
19681acd27e7Smillert char *last_line;
1969*af70c2dfSkettenis
19701acd27e7Smillert last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]];
1971*af70c2dfSkettenis _rl_move_cursor_relative (_rl_screenwidth - 1, last_line);
19721acd27e7Smillert _rl_clear_to_eol (0);
1973*af70c2dfSkettenis putc (last_line[_rl_screenwidth - 1], rl_outstream);
19741acd27e7Smillert }
19751acd27e7Smillert _rl_vis_botlin = 0;
1976*af70c2dfSkettenis rl_crlf ();
19771acd27e7Smillert fflush (rl_outstream);
19781acd27e7Smillert rl_display_fixed++;
19791acd27e7Smillert }
19801acd27e7Smillert
19811acd27e7Smillert /* Move to the start of the current line. */
19821acd27e7Smillert static void
cr()19831acd27e7Smillert cr ()
19841acd27e7Smillert {
1985*af70c2dfSkettenis if (_rl_term_cr)
19861acd27e7Smillert {
19871acd27e7Smillert #if defined (__MSDOS__)
19881acd27e7Smillert putc ('\r', rl_outstream);
19891acd27e7Smillert #else
1990*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
19911acd27e7Smillert #endif
19921acd27e7Smillert _rl_last_c_pos = 0;
19931acd27e7Smillert }
19941acd27e7Smillert }
19951acd27e7Smillert
19961acd27e7Smillert /* Redraw the last line of a multi-line prompt that may possibly contain
19971acd27e7Smillert terminal escape sequences. Called with the cursor at column 0 of the
19981acd27e7Smillert line to draw the prompt on. */
19991acd27e7Smillert static void
redraw_prompt(t)20001acd27e7Smillert redraw_prompt (t)
20011acd27e7Smillert char *t;
20021acd27e7Smillert {
20031acd27e7Smillert char *oldp, *oldl, *oldlprefix;
2004*af70c2dfSkettenis int oldlen, oldlast, oldplen, oldninvis;
20051acd27e7Smillert
20061acd27e7Smillert /* Geez, I should make this a struct. */
20071acd27e7Smillert oldp = rl_display_prompt;
20081acd27e7Smillert oldl = local_prompt;
20091acd27e7Smillert oldlprefix = local_prompt_prefix;
2010*af70c2dfSkettenis oldlen = prompt_visible_length;
2011*af70c2dfSkettenis oldplen = prompt_prefix_length;
2012*af70c2dfSkettenis oldlast = prompt_last_invisible;
2013*af70c2dfSkettenis oldninvis = prompt_invis_chars_first_line;
20141acd27e7Smillert
20151acd27e7Smillert rl_display_prompt = t;
2016*af70c2dfSkettenis local_prompt = expand_prompt (t, &prompt_visible_length,
2017*af70c2dfSkettenis &prompt_last_invisible,
2018*af70c2dfSkettenis &prompt_invis_chars_first_line);
20191acd27e7Smillert local_prompt_prefix = (char *)NULL;
20201acd27e7Smillert rl_forced_update_display ();
20211acd27e7Smillert
20221acd27e7Smillert rl_display_prompt = oldp;
20231acd27e7Smillert local_prompt = oldl;
20241acd27e7Smillert local_prompt_prefix = oldlprefix;
2025*af70c2dfSkettenis prompt_visible_length = oldlen;
2026*af70c2dfSkettenis prompt_prefix_length = oldplen;
2027*af70c2dfSkettenis prompt_last_invisible = oldlast;
2028*af70c2dfSkettenis prompt_invis_chars_first_line = oldninvis;
20291acd27e7Smillert }
20301acd27e7Smillert
20311acd27e7Smillert /* Redisplay the current line after a SIGWINCH is received. */
20321acd27e7Smillert void
_rl_redisplay_after_sigwinch()20331acd27e7Smillert _rl_redisplay_after_sigwinch ()
20341acd27e7Smillert {
20351acd27e7Smillert char *t;
20361acd27e7Smillert
20371acd27e7Smillert /* Clear the current line and put the cursor at column 0. Make sure
20381acd27e7Smillert the right thing happens if we have wrapped to a new screen line. */
2039*af70c2dfSkettenis if (_rl_term_cr)
20401acd27e7Smillert {
20411acd27e7Smillert #if defined (__MSDOS__)
20421acd27e7Smillert putc ('\r', rl_outstream);
20431acd27e7Smillert #else
2044*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
20451acd27e7Smillert #endif
20461acd27e7Smillert _rl_last_c_pos = 0;
20471acd27e7Smillert #if defined (__MSDOS__)
2048*af70c2dfSkettenis space_to_eol (_rl_screenwidth);
20491acd27e7Smillert putc ('\r', rl_outstream);
20501acd27e7Smillert #else
2051*af70c2dfSkettenis if (_rl_term_clreol)
2052*af70c2dfSkettenis tputs (_rl_term_clreol, 1, _rl_output_character_function);
20531acd27e7Smillert else
20541acd27e7Smillert {
2055*af70c2dfSkettenis space_to_eol (_rl_screenwidth);
2056*af70c2dfSkettenis tputs (_rl_term_cr, 1, _rl_output_character_function);
20571acd27e7Smillert }
20581acd27e7Smillert #endif
20591acd27e7Smillert if (_rl_last_v_pos > 0)
20601acd27e7Smillert _rl_move_vert (0);
20611acd27e7Smillert }
20621acd27e7Smillert else
2063*af70c2dfSkettenis rl_crlf ();
20641acd27e7Smillert
20651acd27e7Smillert /* Redraw only the last line of a multi-line prompt. */
20661acd27e7Smillert t = strrchr (rl_display_prompt, '\n');
20671acd27e7Smillert if (t)
20681acd27e7Smillert redraw_prompt (++t);
20691acd27e7Smillert else
20701acd27e7Smillert rl_forced_update_display ();
20711acd27e7Smillert }
20721acd27e7Smillert
20731acd27e7Smillert void
_rl_clean_up_for_exit()20741acd27e7Smillert _rl_clean_up_for_exit ()
20751acd27e7Smillert {
20761acd27e7Smillert if (readline_echoing_p)
20771acd27e7Smillert {
20781acd27e7Smillert _rl_move_vert (_rl_vis_botlin);
20791acd27e7Smillert _rl_vis_botlin = 0;
20801acd27e7Smillert fflush (rl_outstream);
20811acd27e7Smillert rl_restart_output (1, 0);
20821acd27e7Smillert }
20831acd27e7Smillert }
20841acd27e7Smillert
20851acd27e7Smillert void
_rl_erase_entire_line()20861acd27e7Smillert _rl_erase_entire_line ()
20871acd27e7Smillert {
20881acd27e7Smillert cr ();
20891acd27e7Smillert _rl_clear_to_eol (0);
20901acd27e7Smillert cr ();
20911acd27e7Smillert fflush (rl_outstream);
20921acd27e7Smillert }
20931acd27e7Smillert
20941acd27e7Smillert /* return the `current display line' of the cursor -- the number of lines to
20951acd27e7Smillert move up to get to the first screen line of the current readline line. */
20961acd27e7Smillert int
_rl_current_display_line()20971acd27e7Smillert _rl_current_display_line ()
20981acd27e7Smillert {
20991acd27e7Smillert int ret, nleft;
21001acd27e7Smillert
21011acd27e7Smillert /* Find out whether or not there might be invisible characters in the
21021acd27e7Smillert editing buffer. */
21031acd27e7Smillert if (rl_display_prompt == rl_prompt)
2104*af70c2dfSkettenis nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length;
21051acd27e7Smillert else
2106*af70c2dfSkettenis nleft = _rl_last_c_pos - _rl_screenwidth;
21071acd27e7Smillert
21081acd27e7Smillert if (nleft > 0)
2109*af70c2dfSkettenis ret = 1 + nleft / _rl_screenwidth;
21101acd27e7Smillert else
21111acd27e7Smillert ret = 0;
21121acd27e7Smillert
21131acd27e7Smillert return ret;
21141acd27e7Smillert }
2115*af70c2dfSkettenis
2116*af70c2dfSkettenis #if defined (HANDLE_MULTIBYTE)
2117*af70c2dfSkettenis /* Calculate the number of screen columns occupied by STR from START to END.
2118*af70c2dfSkettenis In the case of multibyte characters with stateful encoding, we have to
2119*af70c2dfSkettenis scan from the beginning of the string to take the state into account. */
2120*af70c2dfSkettenis static int
_rl_col_width(str,start,end)2121*af70c2dfSkettenis _rl_col_width (str, start, end)
2122*af70c2dfSkettenis const char *str;
2123*af70c2dfSkettenis int start, end;
2124*af70c2dfSkettenis {
2125*af70c2dfSkettenis wchar_t wc;
2126*af70c2dfSkettenis mbstate_t ps = {0};
2127*af70c2dfSkettenis int tmp, point, width, max;
2128*af70c2dfSkettenis
2129*af70c2dfSkettenis if (end <= start)
2130*af70c2dfSkettenis return 0;
2131*af70c2dfSkettenis
2132*af70c2dfSkettenis point = 0;
2133*af70c2dfSkettenis max = end;
2134*af70c2dfSkettenis
2135*af70c2dfSkettenis while (point < start)
2136*af70c2dfSkettenis {
2137*af70c2dfSkettenis tmp = mbrlen (str + point, max, &ps);
2138*af70c2dfSkettenis if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
2139*af70c2dfSkettenis {
2140*af70c2dfSkettenis /* In this case, the bytes are invalid or too short to compose a
2141*af70c2dfSkettenis multibyte character, so we assume that the first byte represents
2142*af70c2dfSkettenis a single character. */
2143*af70c2dfSkettenis point++;
2144*af70c2dfSkettenis max--;
2145*af70c2dfSkettenis
2146*af70c2dfSkettenis /* Clear the state of the byte sequence, because in this case the
2147*af70c2dfSkettenis effect of mbstate is undefined. */
2148*af70c2dfSkettenis memset (&ps, 0, sizeof (mbstate_t));
2149*af70c2dfSkettenis }
2150*af70c2dfSkettenis else if (tmp == 0)
2151*af70c2dfSkettenis break; /* Found '\0' */
2152*af70c2dfSkettenis else
2153*af70c2dfSkettenis {
2154*af70c2dfSkettenis point += tmp;
2155*af70c2dfSkettenis max -= tmp;
2156*af70c2dfSkettenis }
2157*af70c2dfSkettenis }
2158*af70c2dfSkettenis
2159*af70c2dfSkettenis /* If START is not a byte that starts a character, then POINT will be
2160*af70c2dfSkettenis greater than START. In this case, assume that (POINT - START) gives
2161*af70c2dfSkettenis a byte count that is the number of columns of difference. */
2162*af70c2dfSkettenis width = point - start;
2163*af70c2dfSkettenis
2164*af70c2dfSkettenis while (point < end)
2165*af70c2dfSkettenis {
2166*af70c2dfSkettenis tmp = mbrtowc (&wc, str + point, max, &ps);
2167*af70c2dfSkettenis if ((size_t)tmp == (size_t)-1 || (size_t)tmp == (size_t)-2)
2168*af70c2dfSkettenis {
2169*af70c2dfSkettenis /* In this case, the bytes are invalid or too short to compose a
2170*af70c2dfSkettenis multibyte character, so we assume that the first byte represents
2171*af70c2dfSkettenis a single character. */
2172*af70c2dfSkettenis point++;
2173*af70c2dfSkettenis max--;
2174*af70c2dfSkettenis
2175*af70c2dfSkettenis /* and assume that the byte occupies a single column. */
2176*af70c2dfSkettenis width++;
2177*af70c2dfSkettenis
2178*af70c2dfSkettenis /* Clear the state of the byte sequence, because in this case the
2179*af70c2dfSkettenis effect of mbstate is undefined. */
2180*af70c2dfSkettenis memset (&ps, 0, sizeof (mbstate_t));
2181*af70c2dfSkettenis }
2182*af70c2dfSkettenis else if (tmp == 0)
2183*af70c2dfSkettenis break; /* Found '\0' */
2184*af70c2dfSkettenis else
2185*af70c2dfSkettenis {
2186*af70c2dfSkettenis point += tmp;
2187*af70c2dfSkettenis max -= tmp;
2188*af70c2dfSkettenis tmp = wcwidth(wc);
2189*af70c2dfSkettenis width += (tmp >= 0) ? tmp : 1;
2190*af70c2dfSkettenis }
2191*af70c2dfSkettenis }
2192*af70c2dfSkettenis
2193*af70c2dfSkettenis width += point - end;
2194*af70c2dfSkettenis
2195*af70c2dfSkettenis return width;
2196*af70c2dfSkettenis }
2197*af70c2dfSkettenis #endif /* HANDLE_MULTIBYTE */
2198