Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* display.c -- readline redisplay facility. */ |
2 | ||
cb41b9e7 | 3 | /* Copyright (C) 1987-2017 Free Software Foundation, Inc. |
d60d9f65 | 4 | |
cc88a640 JK |
5 | This file is part of the GNU Readline Library (Readline), a library |
6 | for reading lines of text with interactive input and history editing. | |
d60d9f65 | 7 | |
cc88a640 JK |
8 | Readline is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
d60d9f65 SS |
11 | (at your option) any later version. |
12 | ||
cc88a640 JK |
13 | Readline is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
d60d9f65 SS |
16 | GNU General Public License for more details. |
17 | ||
cc88a640 JK |
18 | You should have received a copy of the GNU General Public License |
19 | along with Readline. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
d60d9f65 SS |
22 | #define READLINE_LIBRARY |
23 | ||
24 | #if defined (HAVE_CONFIG_H) | |
25 | # include <config.h> | |
26 | #endif | |
27 | ||
28 | #include <sys/types.h> | |
29 | ||
30 | #if defined (HAVE_UNISTD_H) | |
31 | # include <unistd.h> | |
32 | #endif /* HAVE_UNISTD_H */ | |
33 | ||
34 | #include "posixstat.h" | |
35 | ||
36 | #if defined (HAVE_STDLIB_H) | |
37 | # include <stdlib.h> | |
38 | #else | |
39 | # include "ansi_stdlib.h" | |
40 | #endif /* HAVE_STDLIB_H */ | |
41 | ||
42 | #include <stdio.h> | |
43 | ||
30083a32 | 44 | #ifdef __MSDOS__ |
775e241e | 45 | # include <pc.h> |
30083a32 EZ |
46 | #endif |
47 | ||
d60d9f65 SS |
48 | /* System-specific feature definitions and include files. */ |
49 | #include "rldefs.h" | |
9255ee31 | 50 | #include "rlmbutil.h" |
d60d9f65 SS |
51 | |
52 | /* Termcap library stuff. */ | |
53 | #include "tcap.h" | |
54 | ||
55 | /* Some standard library routines. */ | |
56 | #include "readline.h" | |
57 | #include "history.h" | |
58 | ||
1b17e766 EZ |
59 | #include "rlprivate.h" |
60 | #include "xmalloc.h" | |
61 | ||
d60d9f65 SS |
62 | #if !defined (strchr) && !defined (__STDC__) |
63 | extern char *strchr (), *strrchr (); | |
64 | #endif /* !strchr && !__STDC__ */ | |
65 | ||
9255ee31 EZ |
66 | static void update_line PARAMS((char *, char *, int, int, int, int)); |
67 | static void space_to_eol PARAMS((int)); | |
68 | static void delete_chars PARAMS((int)); | |
69 | static void insert_some_chars PARAMS((char *, int, int)); | |
775e241e | 70 | static void open_some_spaces PARAMS((int)); |
9255ee31 | 71 | static void cr PARAMS((void)); |
775e241e TT |
72 | static void redraw_prompt PARAMS((char *)); |
73 | ||
74 | /* Values for FLAGS */ | |
75 | #define PMT_MULTILINE 0x01 | |
76 | ||
77 | static char *expand_prompt PARAMS((char *, int, int *, int *, int *, int *)); | |
9255ee31 | 78 | |
cc88a640 JK |
79 | /* State of visible and invisible lines. */ |
80 | struct line_state | |
81 | { | |
82 | char *line; | |
83 | int *lbreaks; | |
84 | int lbsize; | |
9255ee31 | 85 | #if defined (HANDLE_MULTIBYTE) |
cc88a640 | 86 | int wbsize; |
775e241e | 87 | int *wrapped_line; |
9255ee31 | 88 | #endif |
cc88a640 JK |
89 | }; |
90 | ||
91 | /* The line display buffers. One is the line currently displayed on | |
92 | the screen. The other is the line about to be displayed. */ | |
93 | static struct line_state line_state_array[2]; | |
94 | static struct line_state *line_state_visible = &line_state_array[0]; | |
95 | static struct line_state *line_state_invisible = &line_state_array[1]; | |
96 | static int line_structures_initialized = 0; | |
d60d9f65 | 97 | |
cc88a640 JK |
98 | /* Backwards-compatible names. */ |
99 | #define inv_lbreaks (line_state_invisible->lbreaks) | |
100 | #define inv_lbsize (line_state_invisible->lbsize) | |
101 | #define vis_lbreaks (line_state_visible->lbreaks) | |
102 | #define vis_lbsize (line_state_visible->lbsize) | |
103 | ||
104 | #define visible_line (line_state_visible->line) | |
105 | #define invisible_line (line_state_invisible->line) | |
106 | ||
107 | #if defined (HANDLE_MULTIBYTE) | |
108 | static int _rl_col_width PARAMS((const char *, int, int, int)); | |
109 | #else | |
110 | # define _rl_col_width(l, s, e, f) (((e) <= (s)) ? 0 : (e) - (s)) | |
111 | #endif | |
d60d9f65 SS |
112 | |
113 | /* Heuristic used to decide whether it is faster to move from CUR to NEW | |
cc88a640 JK |
114 | by backing up or outputting a carriage return and moving forward. CUR |
115 | and NEW are either both buffer positions or absolute screen positions. */ | |
d60d9f65 SS |
116 | #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new))) |
117 | ||
cc88a640 JK |
118 | /* _rl_last_c_pos is an absolute cursor position in multibyte locales and a |
119 | buffer index in others. This macro is used when deciding whether the | |
120 | current cursor position is in the middle of a prompt string containing | |
121 | invisible characters. XXX - might need to take `modmark' into account. */ | |
775e241e TT |
122 | /* XXX - only valid when tested against _rl_last_c_pos; buffer indices need |
123 | to use prompt_last_invisible directly. */ | |
cc88a640 JK |
124 | #define PROMPT_ENDING_INDEX \ |
125 | ((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars : prompt_last_invisible+1) | |
126 | ||
d60d9f65 SS |
127 | /* **************************************************************** */ |
128 | /* */ | |
129 | /* Display stuff */ | |
130 | /* */ | |
131 | /* **************************************************************** */ | |
132 | ||
133 | /* This is the stuff that is hard for me. I never seem to write good | |
134 | display routines in C. Let's see how I do this time. */ | |
135 | ||
136 | /* (PWP) Well... Good for a simple line updater, but totally ignores | |
137 | the problems of input lines longer than the screen width. | |
138 | ||
139 | update_line and the code that calls it makes a multiple line, | |
140 | automatically wrapping line update. Careful attention needs | |
141 | to be paid to the vertical position variables. */ | |
142 | ||
143 | /* Keep two buffers; one which reflects the current contents of the | |
144 | screen, and the other to draw what we think the new contents should | |
145 | be. Then compare the buffers, and make whatever changes to the | |
146 | screen itself that we should. Finally, make the buffer that we | |
147 | just drew into be the one which reflects the current contents of the | |
148 | screen, and place the cursor where it belongs. | |
149 | ||
150 | Commands that want to can fix the display themselves, and then let | |
151 | this function know that the display has been fixed by setting the | |
152 | RL_DISPLAY_FIXED variable. This is good for efficiency. */ | |
153 | ||
154 | /* Application-specific redisplay function. */ | |
9255ee31 | 155 | rl_voidfunc_t *rl_redisplay_function = rl_redisplay; |
d60d9f65 SS |
156 | |
157 | /* Global variables declared here. */ | |
158 | /* What YOU turn on when you have handled all redisplay yourself. */ | |
159 | int rl_display_fixed = 0; | |
160 | ||
d60d9f65 SS |
161 | /* The stuff that gets printed out before the actual text of the line. |
162 | This is usually pointing to rl_prompt. */ | |
163 | char *rl_display_prompt = (char *)NULL; | |
164 | ||
775e241e TT |
165 | /* Variables used to include the editing mode in the prompt. */ |
166 | char *_rl_emacs_mode_str; | |
167 | int _rl_emacs_modestr_len; | |
168 | ||
169 | char *_rl_vi_ins_mode_str; | |
170 | int _rl_vi_ins_modestr_len; | |
171 | ||
172 | char *_rl_vi_cmd_mode_str; | |
173 | int _rl_vi_cmd_modestr_len; | |
174 | ||
d60d9f65 | 175 | /* Pseudo-global variables declared here. */ |
5bdf8622 | 176 | |
cb41b9e7 TT |
177 | /* Hints for other parts of readline to give to the display engine. */ |
178 | int _rl_suppress_redisplay = 0; | |
179 | int _rl_want_redisplay = 0; | |
180 | ||
d60d9f65 | 181 | /* The visible cursor position. If you print some text, adjust this. */ |
5bdf8622 DJ |
182 | /* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale |
183 | supporting multibyte characters, and an absolute cursor position when | |
184 | in such a locale. This is an artifact of the donated multibyte support. | |
185 | Care must be taken when modifying its value. */ | |
d60d9f65 SS |
186 | int _rl_last_c_pos = 0; |
187 | int _rl_last_v_pos = 0; | |
188 | ||
cb41b9e7 TT |
189 | /* Number of physical lines consumed by the current line buffer currently |
190 | on screen minus 1. */ | |
191 | int _rl_vis_botlin = 0; | |
192 | ||
193 | /* This is a hint update_line gives to rl_redisplay that it has adjusted the | |
194 | value of _rl_last_c_pos *and* taken the presence of any invisible chars in | |
195 | the prompt into account. rl_redisplay notes this and does not do the | |
196 | adjustment itself. */ | |
5bdf8622 | 197 | static int cpos_adjusted; |
cb41b9e7 TT |
198 | |
199 | /* The index into the line buffer corresponding to the cursor position */ | |
cc88a640 | 200 | static int cpos_buffer_position; |
cb41b9e7 TT |
201 | |
202 | /* A flag to note when we're displaying the first line of the prompt */ | |
775e241e | 203 | static int displaying_prompt_first_line; |
cb41b9e7 | 204 | /* The number of multibyte characters in the prompt, if any */ |
cc88a640 | 205 | static int prompt_multibyte_chars; |
5bdf8622 | 206 | |
775e241e TT |
207 | static int _rl_inv_botlin = 0; |
208 | ||
d60d9f65 SS |
209 | /* Variables used only in this file. */ |
210 | /* The last left edge of text that was displayed. This is used when | |
211 | doing horizontal scrolling. It shifts in thirds of a screenwidth. */ | |
212 | static int last_lmargin; | |
213 | ||
d60d9f65 | 214 | /* A buffer for `modeline' messages. */ |
775e241e TT |
215 | static char *msg_buf = 0; |
216 | static int msg_bufsiz = 0; | |
d60d9f65 SS |
217 | |
218 | /* Non-zero forces the redisplay even if we thought it was unnecessary. */ | |
219 | static int forced_display; | |
220 | ||
221 | /* Default and initial buffer size. Can grow. */ | |
222 | static int line_size = 1024; | |
223 | ||
9255ee31 EZ |
224 | /* Variables to keep track of the expanded prompt string, which may |
225 | include invisible characters. */ | |
226 | ||
d60d9f65 | 227 | static char *local_prompt, *local_prompt_prefix; |
cc88a640 | 228 | static int local_prompt_len; |
cb41b9e7 TT |
229 | static int prompt_prefix_length; |
230 | /* Number of chars in the buffer that contribute to visible chars on the screen. | |
231 | This might be different from the number of physical chars in the presence | |
232 | of multibyte characters */ | |
233 | static int prompt_visible_length; | |
d60d9f65 SS |
234 | |
235 | /* The number of invisible characters in the line currently being | |
236 | displayed on the screen. */ | |
237 | static int visible_wrap_offset; | |
238 | ||
9255ee31 EZ |
239 | /* The number of invisible characters in the prompt string. Static so it |
240 | can be shared between rl_redisplay and update_line */ | |
d60d9f65 SS |
241 | static int wrap_offset; |
242 | ||
9255ee31 EZ |
243 | /* The index of the last invisible character in the prompt string. */ |
244 | static int prompt_last_invisible; | |
d60d9f65 SS |
245 | |
246 | /* The length (buffer offset) of the first line of the last (possibly | |
247 | multi-line) buffer displayed on the screen. */ | |
248 | static int visible_first_line_len; | |
249 | ||
9255ee31 EZ |
250 | /* Number of invisible characters on the first physical line of the prompt. |
251 | Only valid when the number of physical characters in the prompt exceeds | |
252 | (or is equal to) _rl_screenwidth. */ | |
253 | static int prompt_invis_chars_first_line; | |
254 | ||
255 | static int prompt_last_screen_line; | |
256 | ||
5bdf8622 DJ |
257 | static int prompt_physical_chars; |
258 | ||
cb41b9e7 TT |
259 | /* An array of indexes into the prompt string where we will break physical |
260 | screen lines. It's easier to compute in expand_prompt and use later in | |
261 | rl_redisplay instead of having rl_redisplay try to guess about invisible | |
262 | characters in the prompt or use heuristics about where they are. */ | |
263 | static int *local_prompt_newlines; | |
264 | ||
cc88a640 JK |
265 | /* set to a non-zero value by rl_redisplay if we are marking modified history |
266 | lines and the current line is so marked. */ | |
267 | static int modmark; | |
268 | ||
cb41b9e7 TT |
269 | static int line_totbytes; |
270 | ||
5bdf8622 DJ |
271 | /* Variables to save and restore prompt and display information. */ |
272 | ||
273 | /* These are getting numerous enough that it's time to create a struct. */ | |
274 | ||
275 | static char *saved_local_prompt; | |
276 | static char *saved_local_prefix; | |
cb41b9e7 TT |
277 | static int *saved_local_prompt_newlines; |
278 | ||
5bdf8622 DJ |
279 | static int saved_last_invisible; |
280 | static int saved_visible_length; | |
281 | static int saved_prefix_length; | |
cc88a640 | 282 | static int saved_local_length; |
5bdf8622 DJ |
283 | static int saved_invis_chars_first_line; |
284 | static int saved_physical_chars; | |
285 | ||
775e241e TT |
286 | /* Return a string indicating the editing mode, for use in the prompt. */ |
287 | ||
288 | static char * | |
cb41b9e7 | 289 | prompt_modestr (int *lenp) |
775e241e TT |
290 | { |
291 | if (rl_editing_mode == emacs_mode) | |
292 | { | |
293 | if (lenp) | |
294 | *lenp = _rl_emacs_mode_str ? _rl_emacs_modestr_len : RL_EMACS_MODESTR_DEFLEN; | |
295 | return _rl_emacs_mode_str ? _rl_emacs_mode_str : RL_EMACS_MODESTR_DEFAULT; | |
296 | } | |
297 | else if (_rl_keymap == vi_insertion_keymap) | |
298 | { | |
299 | if (lenp) | |
300 | *lenp = _rl_vi_ins_mode_str ? _rl_vi_ins_modestr_len : RL_VI_INS_MODESTR_DEFLEN; | |
301 | return _rl_vi_ins_mode_str ? _rl_vi_ins_mode_str : RL_VI_INS_MODESTR_DEFAULT; /* vi insert mode */ | |
302 | } | |
303 | else | |
304 | { | |
305 | if (lenp) | |
306 | *lenp = _rl_vi_cmd_mode_str ? _rl_vi_cmd_modestr_len : RL_VI_CMD_MODESTR_DEFLEN; | |
307 | return _rl_vi_cmd_mode_str ? _rl_vi_cmd_mode_str : RL_VI_CMD_MODESTR_DEFAULT; /* vi command mode */ | |
308 | } | |
309 | } | |
310 | ||
d60d9f65 SS |
311 | /* Expand the prompt string S and return the number of visible |
312 | characters in *LP, if LP is not null. This is currently more-or-less | |
313 | a placeholder for expansion. LIP, if non-null is a place to store the | |
9255ee31 EZ |
314 | index of the last invisible character in the returned string. NIFLP, |
315 | if non-zero, is a place to store the number of invisible characters in | |
5bdf8622 | 316 | the first prompt line. The previous are used as byte counts -- indexes |
cb41b9e7 TT |
317 | into a character buffer. *VLP gets the number of physical characters in |
318 | the expanded prompt (visible length) */ | |
d60d9f65 SS |
319 | |
320 | /* Current implementation: | |
321 | \001 (^A) start non-visible characters | |
322 | \002 (^B) end non-visible characters | |
323 | all characters except \001 and \002 (following a \001) are copied to | |
324 | the returned string; all characters except those between \001 and | |
325 | \002 are assumed to be `visible'. */ | |
326 | ||
775e241e TT |
327 | /* Possible values for FLAGS: |
328 | PMT_MULTILINE caller indicates that this is part of a multiline prompt | |
329 | */ | |
330 | ||
cb41b9e7 TT |
331 | /* This approximates the number of lines the prompt will take when displayed */ |
332 | #define APPROX_DIV(n, d) (((n) < (d)) ? 1 : ((n) / (d)) + 1) | |
333 | ||
d60d9f65 | 334 | static char * |
cb41b9e7 | 335 | expand_prompt (char *pmt, int flags, int *lp, int *lip, int *niflp, int *vlp) |
d60d9f65 | 336 | { |
775e241e | 337 | char *r, *ret, *p, *igstart, *nprompt, *ms; |
5bdf8622 | 338 | int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars; |
cb41b9e7 TT |
339 | int mlen, newlines, newlines_guess, bound; |
340 | int mb_cur_max; | |
775e241e TT |
341 | |
342 | /* We only expand the mode string for the last line of a multiline prompt | |
343 | (a prompt with embedded newlines). */ | |
344 | ms = (((pmt == rl_prompt) ^ (flags & PMT_MULTILINE)) && _rl_show_mode_in_prompt) ? prompt_modestr (&mlen) : 0; | |
345 | if (ms) | |
346 | { | |
347 | l = strlen (pmt); | |
348 | nprompt = (char *)xmalloc (l + mlen + 1); | |
349 | memcpy (nprompt, ms, mlen); | |
350 | strcpy (nprompt + mlen, pmt); | |
351 | } | |
352 | else | |
353 | nprompt = pmt; | |
d60d9f65 | 354 | |
cb41b9e7 TT |
355 | mb_cur_max = MB_CUR_MAX; |
356 | ||
357 | if (_rl_screenwidth == 0) | |
358 | _rl_get_screen_size (0, 0); /* avoid division by zero */ | |
359 | ||
360 | /* Short-circuit if we can. We can do this if we are treating the prompt as | |
361 | a sequence of bytes and there are no invisible characters in the prompt | |
362 | to deal with. Since we populate local_prompt_newlines, we have to run | |
363 | through the rest of the function if this prompt looks like it's going to | |
364 | be longer than one screen line. */ | |
365 | if ((mb_cur_max <= 1 || rl_byte_oriented) && strchr (nprompt, RL_PROMPT_START_IGNORE) == 0) | |
d60d9f65 | 366 | { |
cb41b9e7 TT |
367 | l = strlen (nprompt); |
368 | if (l < (_rl_screenwidth > 0 ? _rl_screenwidth : 80)) | |
369 | { | |
370 | r = (nprompt == pmt) ? savestring (pmt) : nprompt; | |
371 | if (lp) | |
372 | *lp = l; | |
373 | if (lip) | |
374 | *lip = 0; | |
375 | if (niflp) | |
376 | *niflp = 0; | |
377 | if (vlp) | |
378 | *vlp = l; | |
379 | ||
380 | local_prompt_newlines = (int *) xrealloc (local_prompt_newlines, sizeof (int) * 2); | |
381 | local_prompt_newlines[0] = 0; | |
382 | local_prompt_newlines[1] = -1; | |
775e241e | 383 | |
cb41b9e7 TT |
384 | return r; |
385 | } | |
d60d9f65 SS |
386 | } |
387 | ||
775e241e | 388 | l = strlen (nprompt); /* XXX */ |
9255ee31 EZ |
389 | r = ret = (char *)xmalloc (l + 1); |
390 | ||
cb41b9e7 TT |
391 | /* Guess at how many screen lines the prompt will take to size the array that |
392 | keeps track of where the line wraps happen */ | |
393 | newlines_guess = (_rl_screenwidth > 0) ? APPROX_DIV(l, _rl_screenwidth) : APPROX_DIV(l, 80); | |
394 | local_prompt_newlines = (int *) xrealloc (local_prompt_newlines, sizeof (int) * (newlines_guess + 1)); | |
395 | local_prompt_newlines[newlines = 0] = 0; | |
396 | for (rl = 1; rl <= newlines_guess; rl++) | |
397 | local_prompt_newlines[rl] = -1; | |
398 | ||
775e241e | 399 | rl = physchars = 0; /* mode string now part of nprompt */ |
cb41b9e7 TT |
400 | invfl = 0; /* invisible chars in first line of prompt */ |
401 | invflset = 0; /* we only want to set invfl once */ | |
402 | igstart = 0; /* we're not ignoring any characters yet */ | |
775e241e TT |
403 | |
404 | for (ignoring = last = ninvis = 0, p = nprompt; p && *p; p++) | |
d60d9f65 SS |
405 | { |
406 | /* This code strips the invisible character string markers | |
407 | RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */ | |
cc88a640 | 408 | if (ignoring == 0 && *p == RL_PROMPT_START_IGNORE) /* XXX - check ignoring? */ |
d60d9f65 | 409 | { |
cc88a640 JK |
410 | ignoring = 1; |
411 | igstart = p; | |
d60d9f65 SS |
412 | continue; |
413 | } | |
414 | else if (ignoring && *p == RL_PROMPT_END_IGNORE) | |
415 | { | |
416 | ignoring = 0; | |
cc88a640 | 417 | if (p != (igstart + 1)) |
5bdf8622 | 418 | last = r - ret - 1; |
d60d9f65 SS |
419 | continue; |
420 | } | |
421 | else | |
422 | { | |
5bdf8622 | 423 | #if defined (HANDLE_MULTIBYTE) |
cb41b9e7 | 424 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
5bdf8622 | 425 | { |
775e241e TT |
426 | pind = p - nprompt; |
427 | ind = _rl_find_next_mbchar (nprompt, pind, 1, MB_FIND_NONZERO); | |
5bdf8622 DJ |
428 | l = ind - pind; |
429 | while (l--) | |
430 | *r++ = *p++; | |
431 | if (!ignoring) | |
432 | { | |
cc88a640 JK |
433 | /* rl ends up being assigned to prompt_visible_length, |
434 | which is the number of characters in the buffer that | |
435 | contribute to characters on the screen, which might | |
436 | not be the same as the number of physical characters | |
437 | on the screen in the presence of multibyte characters */ | |
5bdf8622 | 438 | rl += ind - pind; |
775e241e | 439 | physchars += _rl_col_width (nprompt, pind, ind, 0); |
5bdf8622 DJ |
440 | } |
441 | else | |
442 | ninvis += ind - pind; | |
443 | p--; /* compensate for later increment */ | |
444 | } | |
9255ee31 | 445 | else |
5bdf8622 DJ |
446 | #endif |
447 | { | |
448 | *r++ = *p; | |
449 | if (!ignoring) | |
450 | { | |
451 | rl++; /* visible length byte counter */ | |
452 | physchars++; | |
453 | } | |
454 | else | |
455 | ninvis++; /* invisible chars byte counter */ | |
456 | } | |
457 | ||
cb41b9e7 | 458 | if (invflset == 0 && physchars >= _rl_screenwidth) |
5bdf8622 DJ |
459 | { |
460 | invfl = ninvis; | |
461 | invflset = 1; | |
462 | } | |
cb41b9e7 TT |
463 | |
464 | if (physchars >= (bound = (newlines + 1) * _rl_screenwidth) && local_prompt_newlines[newlines+1] == -1) | |
465 | { | |
466 | int new; | |
467 | if (physchars > bound) /* should rarely happen */ | |
468 | { | |
469 | #if defined (HANDLE_MULTIBYTE) | |
470 | *r = '\0'; /* need null-termination for strlen */ | |
471 | if (mb_cur_max > 1 && rl_byte_oriented == 0) | |
472 | new = _rl_find_prev_mbchar (ret, r - ret, MB_FIND_ANY); | |
473 | else | |
474 | #endif | |
475 | new = r - ret - (physchars - bound); /* XXX */ | |
476 | } | |
477 | else | |
478 | new = r - ret; | |
479 | local_prompt_newlines[++newlines] = new; | |
480 | } | |
d60d9f65 SS |
481 | } |
482 | } | |
483 | ||
9255ee31 EZ |
484 | if (rl < _rl_screenwidth) |
485 | invfl = ninvis; | |
486 | ||
d60d9f65 SS |
487 | *r = '\0'; |
488 | if (lp) | |
489 | *lp = rl; | |
490 | if (lip) | |
491 | *lip = last; | |
9255ee31 EZ |
492 | if (niflp) |
493 | *niflp = invfl; | |
5bdf8622 DJ |
494 | if (vlp) |
495 | *vlp = physchars; | |
775e241e TT |
496 | |
497 | if (nprompt != pmt) | |
498 | free (nprompt); | |
499 | ||
d60d9f65 SS |
500 | return ret; |
501 | } | |
502 | ||
1b17e766 EZ |
503 | /* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from |
504 | PMT and return the rest of PMT. */ | |
505 | char * | |
cb41b9e7 | 506 | _rl_strip_prompt (char *pmt) |
1b17e766 EZ |
507 | { |
508 | char *ret; | |
509 | ||
775e241e | 510 | ret = expand_prompt (pmt, 0, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL); |
1b17e766 EZ |
511 | return ret; |
512 | } | |
513 | ||
775e241e | 514 | void |
cb41b9e7 | 515 | _rl_reset_prompt (void) |
775e241e TT |
516 | { |
517 | rl_visible_prompt_length = rl_expand_prompt (rl_prompt); | |
518 | } | |
519 | ||
d60d9f65 SS |
520 | /* |
521 | * Expand the prompt string into the various display components, if | |
522 | * necessary. | |
523 | * | |
524 | * local_prompt = expanded last line of string in rl_display_prompt | |
525 | * (portion after the final newline) | |
526 | * local_prompt_prefix = portion before last newline of rl_display_prompt, | |
527 | * expanded via expand_prompt | |
9255ee31 EZ |
528 | * prompt_visible_length = number of visible characters in local_prompt |
529 | * prompt_prefix_length = number of visible characters in local_prompt_prefix | |
d60d9f65 | 530 | * |
cb41b9e7 TT |
531 | * It also tries to keep track of the number of invisible characters in the |
532 | * prompt string, and where they are. | |
533 | * | |
d60d9f65 SS |
534 | * This function is called once per call to readline(). It may also be |
535 | * called arbitrarily to expand the primary prompt. | |
536 | * | |
537 | * The return value is the number of visible characters on the last line | |
cb41b9e7 TT |
538 | * of the (possibly multi-line) prompt. In this case, multi-line means |
539 | * there are embedded newlines in the prompt string itself, not that the | |
540 | * number of physical characters exceeds the screen width and the prompt | |
541 | * wraps. | |
d60d9f65 SS |
542 | */ |
543 | int | |
cb41b9e7 | 544 | rl_expand_prompt (char *prompt) |
d60d9f65 SS |
545 | { |
546 | char *p, *t; | |
547 | int c; | |
548 | ||
549 | /* Clear out any saved values. */ | |
9255ee31 EZ |
550 | FREE (local_prompt); |
551 | FREE (local_prompt_prefix); | |
552 | ||
d60d9f65 | 553 | local_prompt = local_prompt_prefix = (char *)0; |
cc88a640 | 554 | local_prompt_len = 0; |
5bdf8622 DJ |
555 | prompt_last_invisible = prompt_invis_chars_first_line = 0; |
556 | prompt_visible_length = prompt_physical_chars = 0; | |
d60d9f65 SS |
557 | |
558 | if (prompt == 0 || *prompt == 0) | |
559 | return (0); | |
560 | ||
561 | p = strrchr (prompt, '\n'); | |
775e241e | 562 | if (p == 0) |
d60d9f65 | 563 | { |
9255ee31 | 564 | /* The prompt is only one logical line, though it might wrap. */ |
775e241e TT |
565 | local_prompt = expand_prompt (prompt, 0, &prompt_visible_length, |
566 | &prompt_last_invisible, | |
567 | &prompt_invis_chars_first_line, | |
568 | &prompt_physical_chars); | |
d60d9f65 | 569 | local_prompt_prefix = (char *)0; |
cc88a640 | 570 | local_prompt_len = local_prompt ? strlen (local_prompt) : 0; |
9255ee31 | 571 | return (prompt_visible_length); |
d60d9f65 SS |
572 | } |
573 | else | |
574 | { | |
575 | /* The prompt spans multiple lines. */ | |
576 | t = ++p; | |
775e241e TT |
577 | local_prompt = expand_prompt (p, PMT_MULTILINE, |
578 | &prompt_visible_length, | |
9255ee31 | 579 | &prompt_last_invisible, |
cc88a640 | 580 | &prompt_invis_chars_first_line, |
5bdf8622 | 581 | &prompt_physical_chars); |
d60d9f65 SS |
582 | c = *t; *t = '\0'; |
583 | /* The portion of the prompt string up to and including the | |
584 | final newline is now null-terminated. */ | |
775e241e TT |
585 | local_prompt_prefix = expand_prompt (prompt, PMT_MULTILINE, |
586 | &prompt_prefix_length, | |
9255ee31 | 587 | (int *)NULL, |
cc88a640 | 588 | (int *)NULL, |
5bdf8622 | 589 | (int *)NULL); |
d60d9f65 | 590 | *t = c; |
cc88a640 | 591 | local_prompt_len = local_prompt ? strlen (local_prompt) : 0; |
9255ee31 | 592 | return (prompt_prefix_length); |
d60d9f65 SS |
593 | } |
594 | } | |
595 | ||
1b17e766 EZ |
596 | /* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated |
597 | arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE | |
598 | and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is | |
599 | increased. If the lines have already been allocated, this ensures that | |
600 | they can hold at least MINSIZE characters. */ | |
601 | static void | |
cb41b9e7 | 602 | init_line_structures (int minsize) |
1b17e766 EZ |
603 | { |
604 | register int n; | |
32a1adcc | 605 | int original_minsize = minsize; |
1b17e766 | 606 | |
830b6706 TV |
607 | if (minsize <= _rl_screenwidth) /* XXX - for gdb */ |
608 | minsize = _rl_screenwidth + 1; | |
609 | ||
1b17e766 EZ |
610 | if (invisible_line == 0) /* initialize it */ |
611 | { | |
612 | if (line_size < minsize) | |
613 | line_size = minsize; | |
9255ee31 EZ |
614 | visible_line = (char *)xmalloc (line_size); |
615 | invisible_line = (char *)xmalloc (line_size); | |
1b17e766 EZ |
616 | } |
617 | else if (line_size < minsize) /* ensure it can hold MINSIZE chars */ | |
618 | { | |
619 | line_size *= 2; | |
620 | if (line_size < minsize) | |
621 | line_size = minsize; | |
9255ee31 EZ |
622 | visible_line = (char *)xrealloc (visible_line, line_size); |
623 | invisible_line = (char *)xrealloc (invisible_line, line_size); | |
1b17e766 EZ |
624 | } |
625 | ||
32a1adcc | 626 | for (n = original_minsize; n < line_size; n++) |
1b17e766 EZ |
627 | { |
628 | visible_line[n] = 0; | |
629 | invisible_line[n] = 1; | |
630 | } | |
631 | ||
632 | if (vis_lbreaks == 0) | |
633 | { | |
634 | /* should be enough. */ | |
635 | inv_lbsize = vis_lbsize = 256; | |
cc88a640 | 636 | |
9255ee31 | 637 | #if defined (HANDLE_MULTIBYTE) |
cc88a640 JK |
638 | line_state_visible->wbsize = vis_lbsize; |
639 | line_state_visible->wrapped_line = (int *)xmalloc (line_state_visible->wbsize * sizeof (int)); | |
640 | ||
641 | line_state_invisible->wbsize = inv_lbsize; | |
642 | line_state_invisible->wrapped_line = (int *)xmalloc (line_state_invisible->wbsize * sizeof (int)); | |
9255ee31 | 643 | #endif |
cc88a640 JK |
644 | |
645 | inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int)); | |
646 | vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int)); | |
1b17e766 EZ |
647 | inv_lbreaks[0] = vis_lbreaks[0] = 0; |
648 | } | |
cc88a640 JK |
649 | |
650 | line_structures_initialized = 1; | |
1b17e766 EZ |
651 | } |
652 | ||
cb41b9e7 | 653 | /* Basic redisplay algorithm. See comments inline. */ |
d60d9f65 | 654 | void |
cb41b9e7 | 655 | rl_redisplay (void) |
d60d9f65 SS |
656 | { |
657 | register int in, out, c, linenum, cursor_linenum; | |
658 | register char *line; | |
cc88a640 JK |
659 | int inv_botlin, lb_botlin, lb_linenum, o_cpos; |
660 | int newlines, lpos, temp, n0, num, prompt_lines_estimate; | |
d60d9f65 | 661 | char *prompt_this_line; |
775e241e | 662 | int mb_cur_max = MB_CUR_MAX; |
9255ee31 EZ |
663 | #if defined (HANDLE_MULTIBYTE) |
664 | wchar_t wc; | |
665 | size_t wc_bytes; | |
666 | int wc_width; | |
667 | mbstate_t ps; | |
668 | int _rl_wrapped_multicolumn = 0; | |
669 | #endif | |
d60d9f65 | 670 | |
cc88a640 | 671 | if (_rl_echoing_p == 0) |
d60d9f65 SS |
672 | return; |
673 | ||
cc88a640 JK |
674 | /* Block keyboard interrupts because this function manipulates global |
675 | data structures. */ | |
676 | _rl_block_sigint (); | |
677 | RL_SETSTATE (RL_STATE_REDISPLAYING); | |
87adec2e | 678 | |
d60d9f65 SS |
679 | if (!rl_display_prompt) |
680 | rl_display_prompt = ""; | |
681 | ||
cc88a640 | 682 | if (line_structures_initialized == 0) |
d60d9f65 | 683 | { |
1b17e766 | 684 | init_line_structures (0); |
d60d9f65 SS |
685 | rl_on_new_line (); |
686 | } | |
830b6706 TV |
687 | else if (line_size <= _rl_screenwidth) |
688 | init_line_structures (_rl_screenwidth + 1); | |
d60d9f65 SS |
689 | |
690 | /* Draw the line into the buffer. */ | |
cc88a640 JK |
691 | cpos_buffer_position = -1; |
692 | ||
693 | prompt_multibyte_chars = prompt_visible_length - prompt_physical_chars; | |
d60d9f65 SS |
694 | |
695 | line = invisible_line; | |
696 | out = inv_botlin = 0; | |
697 | ||
698 | /* Mark the line as modified or not. We only do this for history | |
699 | lines. */ | |
5bdf8622 | 700 | modmark = 0; |
d60d9f65 SS |
701 | if (_rl_mark_modified_lines && current_history () && rl_undo_list) |
702 | { | |
703 | line[out++] = '*'; | |
704 | line[out] = '\0'; | |
5bdf8622 | 705 | modmark = 1; |
d60d9f65 SS |
706 | } |
707 | ||
708 | /* If someone thought that the redisplay was handled, but the currently | |
709 | visible line has a different modification state than the one about | |
710 | to become visible, then correct the caller's misconception. */ | |
711 | if (visible_line[0] != invisible_line[0]) | |
712 | rl_display_fixed = 0; | |
713 | ||
714 | /* If the prompt to be displayed is the `primary' readline prompt (the | |
715 | one passed to readline()), use the values we have already expanded. | |
716 | If not, use what's already in rl_display_prompt. WRAP_OFFSET is the | |
717 | number of non-visible characters in the prompt string. */ | |
cb41b9e7 TT |
718 | /* This is where we output the characters in the prompt before the last |
719 | newline, if any. If there aren't any embedded newlines, we don't | |
720 | write anything. Copy the last line of the prompt string into the line in | |
721 | any case */ | |
d60d9f65 SS |
722 | if (rl_display_prompt == rl_prompt || local_prompt) |
723 | { | |
d60d9f65 SS |
724 | if (local_prompt_prefix && forced_display) |
725 | _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix)); | |
726 | ||
cc88a640 | 727 | if (local_prompt_len > 0) |
d60d9f65 | 728 | { |
cc88a640 | 729 | temp = local_prompt_len + out + 2; |
c862e87b JM |
730 | if (temp >= line_size) |
731 | { | |
732 | line_size = (temp + 1024) - (temp % 1024); | |
9255ee31 EZ |
733 | visible_line = (char *)xrealloc (visible_line, line_size); |
734 | line = invisible_line = (char *)xrealloc (invisible_line, line_size); | |
c862e87b | 735 | } |
cc88a640 JK |
736 | strncpy (line + out, local_prompt, local_prompt_len); |
737 | out += local_prompt_len; | |
d60d9f65 SS |
738 | } |
739 | line[out] = '\0'; | |
cc88a640 | 740 | wrap_offset = local_prompt_len - prompt_visible_length; |
d60d9f65 SS |
741 | } |
742 | else | |
743 | { | |
744 | int pmtlen; | |
745 | prompt_this_line = strrchr (rl_display_prompt, '\n'); | |
746 | if (!prompt_this_line) | |
747 | prompt_this_line = rl_display_prompt; | |
748 | else | |
749 | { | |
750 | prompt_this_line++; | |
1b17e766 | 751 | pmtlen = prompt_this_line - rl_display_prompt; /* temp var */ |
d60d9f65 SS |
752 | if (forced_display) |
753 | { | |
1b17e766 | 754 | _rl_output_some_chars (rl_display_prompt, pmtlen); |
d60d9f65 SS |
755 | /* Make sure we are at column zero even after a newline, |
756 | regardless of the state of terminal output processing. */ | |
1b17e766 | 757 | if (pmtlen < 2 || prompt_this_line[-2] != '\r') |
d60d9f65 SS |
758 | cr (); |
759 | } | |
760 | } | |
761 | ||
5bdf8622 | 762 | prompt_physical_chars = pmtlen = strlen (prompt_this_line); |
c862e87b JM |
763 | temp = pmtlen + out + 2; |
764 | if (temp >= line_size) | |
765 | { | |
766 | line_size = (temp + 1024) - (temp % 1024); | |
9255ee31 EZ |
767 | visible_line = (char *)xrealloc (visible_line, line_size); |
768 | line = invisible_line = (char *)xrealloc (invisible_line, line_size); | |
c862e87b | 769 | } |
d60d9f65 SS |
770 | strncpy (line + out, prompt_this_line, pmtlen); |
771 | out += pmtlen; | |
772 | line[out] = '\0'; | |
9255ee31 | 773 | wrap_offset = prompt_invis_chars_first_line = 0; |
d60d9f65 SS |
774 | } |
775 | ||
cb41b9e7 | 776 | #if defined (HANDLE_MULTIBYTE) |
1b17e766 EZ |
777 | #define CHECK_INV_LBREAKS() \ |
778 | do { \ | |
779 | if (newlines >= (inv_lbsize - 2)) \ | |
780 | { \ | |
781 | inv_lbsize *= 2; \ | |
782 | inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \ | |
783 | } \ | |
cb41b9e7 TT |
784 | if (newlines >= (line_state_invisible->wbsize - 2)) \ |
785 | { \ | |
786 | line_state_invisible->wbsize *= 2; \ | |
787 | line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \ | |
788 | } \ | |
1b17e766 | 789 | } while (0) |
cb41b9e7 TT |
790 | #else |
791 | #define CHECK_INV_LBREAKS() \ | |
792 | do { \ | |
793 | if (newlines >= (inv_lbsize - 2)) \ | |
794 | { \ | |
795 | inv_lbsize *= 2; \ | |
796 | inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \ | |
797 | } \ | |
798 | } while (0) | |
799 | #endif /* !HANDLE_MULTIBYTE */ | |
9255ee31 EZ |
800 | |
801 | #if defined (HANDLE_MULTIBYTE) | |
d60d9f65 SS |
802 | #define CHECK_LPOS() \ |
803 | do { \ | |
c862e87b | 804 | lpos++; \ |
9255ee31 | 805 | if (lpos >= _rl_screenwidth) \ |
c862e87b | 806 | { \ |
1b17e766 EZ |
807 | if (newlines >= (inv_lbsize - 2)) \ |
808 | { \ | |
809 | inv_lbsize *= 2; \ | |
810 | inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \ | |
811 | } \ | |
c862e87b | 812 | inv_lbreaks[++newlines] = out; \ |
cb41b9e7 | 813 | if (newlines >= (line_state_invisible->wbsize - 2)) \ |
cc88a640 JK |
814 | { \ |
815 | line_state_invisible->wbsize *= 2; \ | |
816 | line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \ | |
817 | } \ | |
818 | line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; \ | |
c862e87b JM |
819 | lpos = 0; \ |
820 | } \ | |
d60d9f65 | 821 | } while (0) |
9255ee31 EZ |
822 | #else |
823 | #define CHECK_LPOS() \ | |
824 | do { \ | |
825 | lpos++; \ | |
826 | if (lpos >= _rl_screenwidth) \ | |
827 | { \ | |
828 | if (newlines >= (inv_lbsize - 2)) \ | |
829 | { \ | |
830 | inv_lbsize *= 2; \ | |
831 | inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \ | |
832 | } \ | |
833 | inv_lbreaks[++newlines] = out; \ | |
834 | lpos = 0; \ | |
835 | } \ | |
836 | } while (0) | |
837 | #endif | |
d60d9f65 SS |
838 | |
839 | /* inv_lbreaks[i] is where line i starts in the buffer. */ | |
840 | inv_lbreaks[newlines = 0] = 0; | |
775e241e | 841 | /* lpos is a physical cursor position, so it needs to be adjusted by the |
cb41b9e7 TT |
842 | number of invisible characters in the prompt, per line. We compute |
843 | the line breaks in the prompt string in expand_prompt, taking invisible | |
844 | characters into account, and if lpos exceeds the screen width, we copy | |
845 | the data in the loop below. */ | |
5bdf8622 | 846 | lpos = prompt_physical_chars + modmark; |
5bdf8622 | 847 | |
9255ee31 | 848 | #if defined (HANDLE_MULTIBYTE) |
cc88a640 | 849 | memset (line_state_invisible->wrapped_line, 0, line_state_invisible->wbsize * sizeof (int)); |
5bdf8622 | 850 | num = 0; |
9255ee31 EZ |
851 | #endif |
852 | ||
853 | /* prompt_invis_chars_first_line is the number of invisible characters in | |
854 | the first physical line of the prompt. | |
cb41b9e7 TT |
855 | wrap_offset - prompt_invis_chars_first_line is usually the number of |
856 | invis chars on the second (or, more generally, last) line. */ | |
cc88a640 JK |
857 | |
858 | /* This is zero-based, used to set the newlines */ | |
859 | prompt_lines_estimate = lpos / _rl_screenwidth; | |
d60d9f65 | 860 | |
9255ee31 | 861 | /* what if lpos is already >= _rl_screenwidth before we start drawing the |
d60d9f65 | 862 | contents of the command line? */ |
cb41b9e7 TT |
863 | if (lpos >= _rl_screenwidth) |
864 | { | |
865 | temp = 0; | |
cc88a640 | 866 | |
cb41b9e7 TT |
867 | /* first copy the linebreaks array we computed in expand_prompt */ |
868 | while (local_prompt_newlines[newlines+1] != -1) | |
869 | { | |
870 | temp = local_prompt_newlines[newlines+1]; | |
871 | inv_lbreaks[++newlines] = temp; | |
872 | } | |
775e241e | 873 | |
cb41b9e7 | 874 | /* Now set lpos from the last newline */ |
775e241e | 875 | if (mb_cur_max > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0) |
cb41b9e7 | 876 | lpos = _rl_col_width (local_prompt, temp, local_prompt_len, 1) - (wrap_offset - prompt_invis_chars_first_line); |
cc88a640 | 877 | else |
cb41b9e7 | 878 | lpos -= (_rl_screenwidth * newlines); |
d60d9f65 SS |
879 | } |
880 | ||
9255ee31 EZ |
881 | prompt_last_screen_line = newlines; |
882 | ||
883 | /* Draw the rest of the line (after the prompt) into invisible_line, keeping | |
cb41b9e7 TT |
884 | track of where the cursor is (cpos_buffer_position), the number of the |
885 | line containing the cursor (lb_linenum), the last line number (lb_botlin | |
886 | and inv_botlin). | |
9255ee31 EZ |
887 | It maintains an array of line breaks for display (inv_lbreaks). |
888 | This handles expanding tabs for display and displaying meta characters. */ | |
d60d9f65 | 889 | lb_linenum = 0; |
9255ee31 EZ |
890 | #if defined (HANDLE_MULTIBYTE) |
891 | in = 0; | |
775e241e | 892 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
893 | { |
894 | memset (&ps, 0, sizeof (mbstate_t)); | |
cb41b9e7 TT |
895 | if (_rl_utf8locale && UTF8_SINGLEBYTE(rl_line_buffer[0])) |
896 | { | |
897 | wc = (wchar_t)rl_line_buffer[0]; | |
898 | wc_bytes = 1; | |
899 | } | |
900 | else | |
901 | wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps); | |
9255ee31 EZ |
902 | } |
903 | else | |
904 | wc_bytes = 1; | |
905 | while (in < rl_end) | |
906 | #else | |
d60d9f65 | 907 | for (in = 0; in < rl_end; in++) |
9255ee31 | 908 | #endif |
d60d9f65 SS |
909 | { |
910 | c = (unsigned char)rl_line_buffer[in]; | |
911 | ||
9255ee31 | 912 | #if defined (HANDLE_MULTIBYTE) |
775e241e | 913 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 | 914 | { |
5bdf8622 | 915 | if (MB_INVALIDCH (wc_bytes)) |
9255ee31 EZ |
916 | { |
917 | /* Byte sequence is invalid or shortened. Assume that the | |
918 | first byte represents a character. */ | |
919 | wc_bytes = 1; | |
920 | /* Assume that a character occupies a single column. */ | |
921 | wc_width = 1; | |
922 | memset (&ps, 0, sizeof (mbstate_t)); | |
923 | } | |
5bdf8622 | 924 | else if (MB_NULLWCH (wc_bytes)) |
9255ee31 EZ |
925 | break; /* Found '\0' */ |
926 | else | |
927 | { | |
775e241e | 928 | temp = WCWIDTH (wc); |
5bdf8622 | 929 | wc_width = (temp >= 0) ? temp : 1; |
9255ee31 EZ |
930 | } |
931 | } | |
932 | #endif | |
933 | ||
d60d9f65 SS |
934 | if (out + 8 >= line_size) /* XXX - 8 for \t */ |
935 | { | |
936 | line_size *= 2; | |
9255ee31 EZ |
937 | visible_line = (char *)xrealloc (visible_line, line_size); |
938 | invisible_line = (char *)xrealloc (invisible_line, line_size); | |
d60d9f65 SS |
939 | line = invisible_line; |
940 | } | |
941 | ||
942 | if (in == rl_point) | |
943 | { | |
cc88a640 | 944 | cpos_buffer_position = out; |
d60d9f65 SS |
945 | lb_linenum = newlines; |
946 | } | |
947 | ||
9255ee31 EZ |
948 | #if defined (HANDLE_MULTIBYTE) |
949 | if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */ | |
950 | #else | |
d60d9f65 | 951 | if (META_CHAR (c)) |
9255ee31 | 952 | #endif |
d60d9f65 SS |
953 | { |
954 | if (_rl_output_meta_chars == 0) | |
955 | { | |
956 | sprintf (line + out, "\\%o", c); | |
957 | ||
9255ee31 | 958 | if (lpos + 4 >= _rl_screenwidth) |
d60d9f65 | 959 | { |
9255ee31 | 960 | temp = _rl_screenwidth - lpos; |
1b17e766 | 961 | CHECK_INV_LBREAKS (); |
d60d9f65 | 962 | inv_lbreaks[++newlines] = out + temp; |
cb41b9e7 TT |
963 | #if defined (HANDLE_MULTIBYTE) |
964 | line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; | |
965 | #endif | |
d60d9f65 SS |
966 | lpos = 4 - temp; |
967 | } | |
968 | else | |
969 | lpos += 4; | |
970 | ||
971 | out += 4; | |
972 | } | |
973 | else | |
974 | { | |
975 | line[out++] = c; | |
976 | CHECK_LPOS(); | |
977 | } | |
978 | } | |
979 | #if defined (DISPLAY_TABS) | |
980 | else if (c == '\t') | |
981 | { | |
9255ee31 | 982 | register int newout; |
c862e87b JM |
983 | |
984 | #if 0 | |
d60d9f65 | 985 | newout = (out | (int)7) + 1; |
c862e87b JM |
986 | #else |
987 | newout = out + 8 - lpos % 8; | |
988 | #endif | |
d60d9f65 | 989 | temp = newout - out; |
9255ee31 | 990 | if (lpos + temp >= _rl_screenwidth) |
d60d9f65 SS |
991 | { |
992 | register int temp2; | |
9255ee31 | 993 | temp2 = _rl_screenwidth - lpos; |
1b17e766 | 994 | CHECK_INV_LBREAKS (); |
d60d9f65 | 995 | inv_lbreaks[++newlines] = out + temp2; |
cb41b9e7 TT |
996 | #if defined (HANDLE_MULTIBYTE) |
997 | line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; | |
998 | #endif | |
d60d9f65 SS |
999 | lpos = temp - temp2; |
1000 | while (out < newout) | |
1001 | line[out++] = ' '; | |
1002 | } | |
1003 | else | |
1004 | { | |
1005 | while (out < newout) | |
1006 | line[out++] = ' '; | |
1007 | lpos += temp; | |
1008 | } | |
1009 | } | |
1010 | #endif | |
9255ee31 | 1011 | else if (c == '\n' && _rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up) |
c862e87b JM |
1012 | { |
1013 | line[out++] = '\0'; /* XXX - sentinel */ | |
1b17e766 | 1014 | CHECK_INV_LBREAKS (); |
c862e87b | 1015 | inv_lbreaks[++newlines] = out; |
cb41b9e7 TT |
1016 | #if defined (HANDLE_MULTIBYTE) |
1017 | line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; | |
1018 | #endif | |
c862e87b JM |
1019 | lpos = 0; |
1020 | } | |
d60d9f65 SS |
1021 | else if (CTRL_CHAR (c) || c == RUBOUT) |
1022 | { | |
1023 | line[out++] = '^'; | |
1024 | CHECK_LPOS(); | |
1025 | line[out++] = CTRL_CHAR (c) ? UNCTRL (c) : '?'; | |
1026 | CHECK_LPOS(); | |
1027 | } | |
1028 | else | |
1029 | { | |
9255ee31 | 1030 | #if defined (HANDLE_MULTIBYTE) |
775e241e | 1031 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1032 | { |
1033 | register int i; | |
1034 | ||
1035 | _rl_wrapped_multicolumn = 0; | |
1036 | ||
1037 | if (_rl_screenwidth < lpos + wc_width) | |
1038 | for (i = lpos; i < _rl_screenwidth; i++) | |
1039 | { | |
1040 | /* The space will be removed in update_line() */ | |
1041 | line[out++] = ' '; | |
1042 | _rl_wrapped_multicolumn++; | |
1043 | CHECK_LPOS(); | |
1044 | } | |
1045 | if (in == rl_point) | |
1046 | { | |
cc88a640 | 1047 | cpos_buffer_position = out; |
9255ee31 EZ |
1048 | lb_linenum = newlines; |
1049 | } | |
1050 | for (i = in; i < in+wc_bytes; i++) | |
1051 | line[out++] = rl_line_buffer[i]; | |
1052 | for (i = 0; i < wc_width; i++) | |
1053 | CHECK_LPOS(); | |
1054 | } | |
1055 | else | |
1056 | { | |
1057 | line[out++] = c; | |
1058 | CHECK_LPOS(); | |
1059 | } | |
1060 | #else | |
d60d9f65 SS |
1061 | line[out++] = c; |
1062 | CHECK_LPOS(); | |
9255ee31 | 1063 | #endif |
d60d9f65 | 1064 | } |
9255ee31 EZ |
1065 | |
1066 | #if defined (HANDLE_MULTIBYTE) | |
775e241e | 1067 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1068 | { |
1069 | in += wc_bytes; | |
cb41b9e7 TT |
1070 | if (_rl_utf8locale && UTF8_SINGLEBYTE(rl_line_buffer[in])) |
1071 | { | |
1072 | wc = (wchar_t)rl_line_buffer[in]; | |
1073 | wc_bytes = 1; | |
1074 | memset (&ps, 0, sizeof (mbstate_t)); /* re-init state */ | |
1075 | } | |
1076 | else | |
1077 | wc_bytes = mbrtowc (&wc, rl_line_buffer + in, rl_end - in, &ps); | |
9255ee31 EZ |
1078 | } |
1079 | else | |
1080 | in++; | |
1081 | #endif | |
d60d9f65 SS |
1082 | } |
1083 | line[out] = '\0'; | |
cb41b9e7 | 1084 | line_totbytes = out; |
cc88a640 | 1085 | if (cpos_buffer_position < 0) |
d60d9f65 | 1086 | { |
cc88a640 | 1087 | cpos_buffer_position = out; |
d60d9f65 SS |
1088 | lb_linenum = newlines; |
1089 | } | |
1090 | ||
775e241e | 1091 | inv_botlin = lb_botlin = _rl_inv_botlin = newlines; |
1b17e766 | 1092 | CHECK_INV_LBREAKS (); |
d60d9f65 | 1093 | inv_lbreaks[newlines+1] = out; |
cb41b9e7 TT |
1094 | #if defined (HANDLE_MULTIBYTE) |
1095 | /* This should be 0 anyway */ | |
1096 | line_state_invisible->wrapped_line[newlines+1] = _rl_wrapped_multicolumn; | |
1097 | #endif | |
d60d9f65 SS |
1098 | cursor_linenum = lb_linenum; |
1099 | ||
cc88a640 | 1100 | /* CPOS_BUFFER_POSITION == position in buffer where cursor should be placed. |
9255ee31 | 1101 | CURSOR_LINENUM == line number where the cursor should be placed. */ |
d60d9f65 SS |
1102 | |
1103 | /* PWP: now is when things get a bit hairy. The visible and invisible | |
1104 | line buffers are really multiple lines, which would wrap every | |
1105 | (screenwidth - 1) characters. Go through each in turn, finding | |
1106 | the changed region and updating it. The line order is top to bottom. */ | |
1107 | ||
1108 | /* If we can move the cursor up and down, then use multiple lines, | |
1109 | otherwise, let long lines display in a single terminal line, and | |
1110 | horizontally scroll it. */ | |
775e241e | 1111 | displaying_prompt_first_line = 1; |
9255ee31 | 1112 | if (_rl_horizontal_scroll_mode == 0 && _rl_term_up && *_rl_term_up) |
d60d9f65 | 1113 | { |
5bdf8622 | 1114 | int nleft, pos, changed_screen_line, tx; |
d60d9f65 SS |
1115 | |
1116 | if (!rl_display_fixed || forced_display) | |
1117 | { | |
1118 | forced_display = 0; | |
1119 | ||
1120 | /* If we have more than a screenful of material to display, then | |
1121 | only display a screenful. We should display the last screen, | |
1122 | not the first. */ | |
9255ee31 EZ |
1123 | if (out >= _rl_screenchars) |
1124 | { | |
775e241e TT |
1125 | #if defined (HANDLE_MULTIBYTE) |
1126 | if (mb_cur_max > 1 && rl_byte_oriented == 0) | |
9255ee31 EZ |
1127 | out = _rl_find_prev_mbchar (line, _rl_screenchars, MB_FIND_ANY); |
1128 | else | |
775e241e | 1129 | #endif |
9255ee31 EZ |
1130 | out = _rl_screenchars - 1; |
1131 | } | |
d60d9f65 SS |
1132 | |
1133 | /* The first line is at character position 0 in the buffer. The | |
1134 | second and subsequent lines start at inv_lbreaks[N], offset by | |
1135 | OFFSET (which has already been calculated above). */ | |
1136 | ||
cc88a640 JK |
1137 | #define INVIS_FIRST() (prompt_physical_chars > _rl_screenwidth ? prompt_invis_chars_first_line : wrap_offset) |
1138 | #define WRAP_OFFSET(line, offset) ((line == 0) \ | |
1139 | ? (offset ? INVIS_FIRST() : 0) \ | |
1140 | : ((line == prompt_last_screen_line) ? wrap_offset-prompt_invis_chars_first_line : 0)) | |
d60d9f65 SS |
1141 | #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0) |
1142 | #define VIS_LLEN(l) ((l) > _rl_vis_botlin ? 0 : (vis_lbreaks[l+1] - vis_lbreaks[l])) | |
1143 | #define INV_LLEN(l) (inv_lbreaks[l+1] - inv_lbreaks[l]) | |
1144 | #define VIS_CHARS(line) (visible_line + vis_lbreaks[line]) | |
1145 | #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line) | |
1146 | #define INV_LINE(line) (invisible_line + inv_lbreaks[line]) | |
1147 | ||
cc88a640 JK |
1148 | #define OLD_CPOS_IN_PROMPT() (cpos_adjusted == 0 && \ |
1149 | _rl_last_c_pos != o_cpos && \ | |
1150 | _rl_last_c_pos > wrap_offset && \ | |
1151 | o_cpos < prompt_last_invisible) | |
1152 | ||
d60d9f65 SS |
1153 | /* For each line in the buffer, do the updating display. */ |
1154 | for (linenum = 0; linenum <= inv_botlin; linenum++) | |
1155 | { | |
cc88a640 JK |
1156 | /* This can lead us astray if we execute a program that changes |
1157 | the locale from a non-multibyte to a multibyte one. */ | |
5bdf8622 DJ |
1158 | o_cpos = _rl_last_c_pos; |
1159 | cpos_adjusted = 0; | |
d60d9f65 SS |
1160 | update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum, |
1161 | VIS_LLEN(linenum), INV_LLEN(linenum), inv_botlin); | |
1162 | ||
5bdf8622 DJ |
1163 | /* update_line potentially changes _rl_last_c_pos, but doesn't |
1164 | take invisible characters into account, since _rl_last_c_pos | |
cb41b9e7 TT |
1165 | is an absolute cursor position in a multibyte locale. We |
1166 | choose to (mostly) compensate for that here, rather than | |
cc88a640 | 1167 | change update_line itself. There are several cases in which |
5bdf8622 DJ |
1168 | update_line adjusts _rl_last_c_pos itself (so it can pass |
1169 | _rl_move_cursor_relative accurate values); it communicates | |
cc88a640 JK |
1170 | this back by setting cpos_adjusted. If we assume that |
1171 | _rl_last_c_pos is correct (an absolute cursor position) each | |
1172 | time update_line is called, then we can assume in our | |
1173 | calculations that o_cpos does not need to be adjusted by | |
1174 | wrap_offset. */ | |
775e241e | 1175 | if (linenum == 0 && (mb_cur_max > 1 && rl_byte_oriented == 0) && OLD_CPOS_IN_PROMPT()) |
cc88a640 | 1176 | _rl_last_c_pos -= prompt_invis_chars_first_line; /* XXX - was wrap_offset */ |
cb41b9e7 TT |
1177 | else if (cpos_adjusted == 0 && |
1178 | linenum == prompt_last_screen_line && | |
1179 | prompt_physical_chars > _rl_screenwidth && | |
775e241e | 1180 | (mb_cur_max > 1 && rl_byte_oriented == 0) && |
cc88a640 | 1181 | _rl_last_c_pos != o_cpos && |
cb41b9e7 TT |
1182 | _rl_last_c_pos > (prompt_last_invisible - _rl_screenwidth - prompt_invis_chars_first_line)) /* XXX - rethink this last one */ |
1183 | /* This assumes that all the invisible characters are split | |
1184 | between the first and last lines of the prompt, if the | |
1185 | prompt consumes more than two lines. It's usually right */ | |
1186 | /* XXX - not sure this is ever executed */ | |
cc88a640 | 1187 | _rl_last_c_pos -= (wrap_offset-prompt_invis_chars_first_line); |
775e241e | 1188 | |
d60d9f65 SS |
1189 | /* If this is the line with the prompt, we might need to |
1190 | compensate for invisible characters in the new line. Do | |
1191 | this only if there is not more than one new line (which | |
1192 | implies that we completely overwrite the old visible line) | |
1193 | and the new line is shorter than the old. Make sure we are | |
1194 | at the end of the new line before clearing. */ | |
1195 | if (linenum == 0 && | |
1196 | inv_botlin == 0 && _rl_last_c_pos == out && | |
1197 | (wrap_offset > visible_wrap_offset) && | |
1198 | (_rl_last_c_pos < visible_first_line_len)) | |
1199 | { | |
775e241e | 1200 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
5bdf8622 DJ |
1201 | nleft = _rl_screenwidth - _rl_last_c_pos; |
1202 | else | |
1203 | nleft = _rl_screenwidth + wrap_offset - _rl_last_c_pos; | |
d60d9f65 SS |
1204 | if (nleft) |
1205 | _rl_clear_to_eol (nleft); | |
1206 | } | |
cc88a640 JK |
1207 | #if 0 |
1208 | /* This segment is intended to handle the case where the prompt | |
1209 | has invisible characters on the second line and the new line | |
1210 | to be displayed needs to clear the rest of the old characters | |
1211 | out (e.g., when printing the i-search prompt). In general, | |
1212 | the case of the new line being shorter than the old. | |
1213 | Incomplete */ | |
1214 | else if (linenum == prompt_last_screen_line && | |
1215 | prompt_physical_chars > _rl_screenwidth && | |
1216 | wrap_offset != prompt_invis_chars_first_line && | |
1217 | _rl_last_c_pos == out && | |
1218 | #endif | |
1219 | ||
d60d9f65 SS |
1220 | |
1221 | /* Since the new first line is now visible, save its length. */ | |
1222 | if (linenum == 0) | |
1223 | visible_first_line_len = (inv_botlin > 0) ? inv_lbreaks[1] : out - wrap_offset; | |
1224 | } | |
1225 | ||
1226 | /* We may have deleted some lines. If so, clear the left over | |
1227 | blank ones at the bottom out. */ | |
1228 | if (_rl_vis_botlin > inv_botlin) | |
1229 | { | |
1230 | char *tt; | |
1231 | for (; linenum <= _rl_vis_botlin; linenum++) | |
1232 | { | |
1233 | tt = VIS_CHARS (linenum); | |
1234 | _rl_move_vert (linenum); | |
1235 | _rl_move_cursor_relative (0, tt); | |
1236 | _rl_clear_to_eol | |
9255ee31 | 1237 | ((linenum == _rl_vis_botlin) ? strlen (tt) : _rl_screenwidth); |
d60d9f65 SS |
1238 | } |
1239 | } | |
1240 | _rl_vis_botlin = inv_botlin; | |
1241 | ||
1242 | /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a | |
1243 | different screen line during this redisplay. */ | |
1244 | changed_screen_line = _rl_last_v_pos != cursor_linenum; | |
1245 | if (changed_screen_line) | |
1246 | { | |
1247 | _rl_move_vert (cursor_linenum); | |
9255ee31 | 1248 | /* If we moved up to the line with the prompt using _rl_term_up, |
c862e87b JM |
1249 | the physical cursor position on the screen stays the same, |
1250 | but the buffer position needs to be adjusted to account | |
1251 | for invisible characters. */ | |
775e241e | 1252 | if ((mb_cur_max == 1 || rl_byte_oriented) && cursor_linenum == 0 && wrap_offset) |
c862e87b | 1253 | _rl_last_c_pos += wrap_offset; |
d60d9f65 SS |
1254 | } |
1255 | ||
cb41b9e7 TT |
1256 | /* Now we move the cursor to where it needs to be. First, make |
1257 | sure we are on the correct line (cursor_linenum). */ | |
1258 | ||
d60d9f65 SS |
1259 | /* We have to reprint the prompt if it contains invisible |
1260 | characters, since it's not generally OK to just reprint | |
1261 | the characters from the current cursor position. But we | |
1262 | only need to reprint it if the cursor is before the last | |
1263 | invisible character in the prompt string. */ | |
9255ee31 | 1264 | nleft = prompt_visible_length + wrap_offset; |
d60d9f65 | 1265 | if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 && |
cc88a640 JK |
1266 | #if 0 |
1267 | _rl_last_c_pos <= PROMPT_ENDING_INDEX && local_prompt) | |
1268 | #else | |
1269 | _rl_last_c_pos < PROMPT_ENDING_INDEX && local_prompt) | |
1270 | #endif | |
d60d9f65 | 1271 | { |
771578d1 SS |
1272 | #if defined (__MSDOS__) |
1273 | putc ('\r', rl_outstream); | |
1274 | #else | |
9255ee31 EZ |
1275 | if (_rl_term_cr) |
1276 | tputs (_rl_term_cr, 1, _rl_output_character_function); | |
771578d1 | 1277 | #endif |
cc88a640 JK |
1278 | if (modmark) |
1279 | _rl_output_some_chars ("*", 1); | |
1280 | ||
d60d9f65 | 1281 | _rl_output_some_chars (local_prompt, nleft); |
775e241e | 1282 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
cc88a640 | 1283 | _rl_last_c_pos = _rl_col_width (local_prompt, 0, nleft, 1) - wrap_offset + modmark; |
9255ee31 | 1284 | else |
cc88a640 | 1285 | _rl_last_c_pos = nleft + modmark; |
d60d9f65 SS |
1286 | } |
1287 | ||
1288 | /* Where on that line? And where does that line start | |
1289 | in the buffer? */ | |
1290 | pos = inv_lbreaks[cursor_linenum]; | |
1291 | /* nleft == number of characters in the line buffer between the | |
cc88a640 JK |
1292 | start of the line and the desired cursor position. */ |
1293 | nleft = cpos_buffer_position - pos; | |
d60d9f65 | 1294 | |
5bdf8622 DJ |
1295 | /* NLEFT is now a number of characters in a buffer. When in a |
1296 | multibyte locale, however, _rl_last_c_pos is an absolute cursor | |
1297 | position that doesn't take invisible characters in the prompt | |
1298 | into account. We use a fudge factor to compensate. */ | |
1299 | ||
cb41b9e7 TT |
1300 | /* Since _rl_backspace() doesn't know about invisible characters in |
1301 | the prompt, and there's no good way to tell it, we compensate for | |
1302 | those characters here and call _rl_backspace() directly if | |
1303 | necessary */ | |
d60d9f65 SS |
1304 | if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos) |
1305 | { | |
cc88a640 | 1306 | /* TX == new physical cursor position in multibyte locale. */ |
775e241e | 1307 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
cc88a640 | 1308 | tx = _rl_col_width (&visible_line[pos], 0, nleft, 1) - visible_wrap_offset; |
9255ee31 | 1309 | else |
5bdf8622 | 1310 | tx = nleft; |
cc88a640 | 1311 | if (tx >= 0 && _rl_last_c_pos > tx) |
5bdf8622 DJ |
1312 | { |
1313 | _rl_backspace (_rl_last_c_pos - tx); /* XXX */ | |
1314 | _rl_last_c_pos = tx; | |
1315 | } | |
d60d9f65 SS |
1316 | } |
1317 | ||
5bdf8622 DJ |
1318 | /* We need to note that in a multibyte locale we are dealing with |
1319 | _rl_last_c_pos as an absolute cursor position, but moving to a | |
1320 | point specified by a buffer position (NLEFT) that doesn't take | |
1321 | invisible characters into account. */ | |
775e241e | 1322 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1323 | _rl_move_cursor_relative (nleft, &invisible_line[pos]); |
1324 | else if (nleft != _rl_last_c_pos) | |
d60d9f65 SS |
1325 | _rl_move_cursor_relative (nleft, &invisible_line[pos]); |
1326 | } | |
1327 | } | |
cb41b9e7 | 1328 | else /* Do horizontal scrolling. Much simpler */ |
d60d9f65 SS |
1329 | { |
1330 | #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0) | |
1331 | int lmargin, ndisp, nleft, phys_c_pos, t; | |
1332 | ||
1333 | /* Always at top line. */ | |
1334 | _rl_last_v_pos = 0; | |
1335 | ||
1336 | /* Compute where in the buffer the displayed line should start. This | |
1337 | will be LMARGIN. */ | |
1338 | ||
1339 | /* The number of characters that will be displayed before the cursor. */ | |
cc88a640 | 1340 | ndisp = cpos_buffer_position - wrap_offset; |
9255ee31 | 1341 | nleft = prompt_visible_length + wrap_offset; |
d60d9f65 | 1342 | /* Where the new cursor position will be on the screen. This can be |
c862e87b | 1343 | longer than SCREENWIDTH; if it is, lmargin will be adjusted. */ |
cc88a640 | 1344 | phys_c_pos = cpos_buffer_position - (last_lmargin ? last_lmargin : wrap_offset); |
9255ee31 | 1345 | t = _rl_screenwidth / 3; |
d60d9f65 SS |
1346 | |
1347 | /* If the number of characters had already exceeded the screenwidth, | |
c862e87b | 1348 | last_lmargin will be > 0. */ |
d60d9f65 SS |
1349 | |
1350 | /* If the number of characters to be displayed is more than the screen | |
c862e87b JM |
1351 | width, compute the starting offset so that the cursor is about |
1352 | two-thirds of the way across the screen. */ | |
9255ee31 | 1353 | if (phys_c_pos > _rl_screenwidth - 2) |
d60d9f65 | 1354 | { |
cc88a640 | 1355 | lmargin = cpos_buffer_position - (2 * t); |
d60d9f65 SS |
1356 | if (lmargin < 0) |
1357 | lmargin = 0; | |
1358 | /* If the left margin would be in the middle of a prompt with | |
1359 | invisible characters, don't display the prompt at all. */ | |
1360 | if (wrap_offset && lmargin > 0 && lmargin < nleft) | |
1361 | lmargin = nleft; | |
1362 | } | |
9255ee31 | 1363 | else if (ndisp < _rl_screenwidth - 2) /* XXX - was -1 */ |
c862e87b | 1364 | lmargin = 0; |
d60d9f65 SS |
1365 | else if (phys_c_pos < 1) |
1366 | { | |
1367 | /* If we are moving back towards the beginning of the line and | |
1368 | the last margin is no longer correct, compute a new one. */ | |
cc88a640 | 1369 | lmargin = ((cpos_buffer_position - 1) / t) * t; /* XXX */ |
d60d9f65 SS |
1370 | if (wrap_offset && lmargin > 0 && lmargin < nleft) |
1371 | lmargin = nleft; | |
1372 | } | |
1373 | else | |
c862e87b | 1374 | lmargin = last_lmargin; |
d60d9f65 | 1375 | |
775e241e TT |
1376 | displaying_prompt_first_line = lmargin < nleft; |
1377 | ||
d60d9f65 SS |
1378 | /* If the first character on the screen isn't the first character |
1379 | in the display line, indicate this with a special character. */ | |
1380 | if (lmargin > 0) | |
1381 | line[lmargin] = '<'; | |
1382 | ||
1383 | /* If SCREENWIDTH characters starting at LMARGIN do not encompass | |
c862e87b JM |
1384 | the whole line, indicate that with a special character at the |
1385 | right edge of the screen. If LMARGIN is 0, we need to take the | |
1386 | wrap offset into account. */ | |
9255ee31 | 1387 | t = lmargin + M_OFFSET (lmargin, wrap_offset) + _rl_screenwidth; |
cb41b9e7 | 1388 | if (t > 0 && t < out) |
c862e87b | 1389 | line[t - 1] = '>'; |
d60d9f65 | 1390 | |
cc88a640 | 1391 | if (rl_display_fixed == 0 || forced_display || lmargin != last_lmargin) |
d60d9f65 SS |
1392 | { |
1393 | forced_display = 0; | |
cc88a640 JK |
1394 | o_cpos = _rl_last_c_pos; |
1395 | cpos_adjusted = 0; | |
d60d9f65 SS |
1396 | update_line (&visible_line[last_lmargin], |
1397 | &invisible_line[lmargin], | |
1398 | 0, | |
9255ee31 EZ |
1399 | _rl_screenwidth + visible_wrap_offset, |
1400 | _rl_screenwidth + (lmargin ? 0 : wrap_offset), | |
d60d9f65 SS |
1401 | 0); |
1402 | ||
775e241e TT |
1403 | if ((mb_cur_max > 1 && rl_byte_oriented == 0) && |
1404 | displaying_prompt_first_line && OLD_CPOS_IN_PROMPT()) | |
cc88a640 JK |
1405 | _rl_last_c_pos -= prompt_invis_chars_first_line; /* XXX - was wrap_offset */ |
1406 | ||
d60d9f65 SS |
1407 | /* If the visible new line is shorter than the old, but the number |
1408 | of invisible characters is greater, and we are at the end of | |
1409 | the new line, we need to clear to eol. */ | |
1410 | t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset); | |
1411 | if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) && | |
775e241e | 1412 | (_rl_last_c_pos == out) && displaying_prompt_first_line && |
d60d9f65 SS |
1413 | t < visible_first_line_len) |
1414 | { | |
9255ee31 | 1415 | nleft = _rl_screenwidth - t; |
d60d9f65 SS |
1416 | _rl_clear_to_eol (nleft); |
1417 | } | |
1418 | visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset); | |
9255ee31 EZ |
1419 | if (visible_first_line_len > _rl_screenwidth) |
1420 | visible_first_line_len = _rl_screenwidth; | |
d60d9f65 | 1421 | |
cc88a640 | 1422 | _rl_move_cursor_relative (cpos_buffer_position - lmargin, &invisible_line[lmargin]); |
d60d9f65 SS |
1423 | last_lmargin = lmargin; |
1424 | } | |
1425 | } | |
1426 | fflush (rl_outstream); | |
1427 | ||
1428 | /* Swap visible and non-visible lines. */ | |
1429 | { | |
cc88a640 | 1430 | struct line_state *vtemp = line_state_visible; |
1b17e766 | 1431 | |
cc88a640 JK |
1432 | line_state_visible = line_state_invisible; |
1433 | line_state_invisible = vtemp; | |
1b17e766 | 1434 | |
d60d9f65 SS |
1435 | rl_display_fixed = 0; |
1436 | /* If we are displaying on a single line, and last_lmargin is > 0, we | |
1437 | are not displaying any invisible characters, so set visible_wrap_offset | |
1438 | to 0. */ | |
1439 | if (_rl_horizontal_scroll_mode && last_lmargin) | |
1440 | visible_wrap_offset = 0; | |
1441 | else | |
1442 | visible_wrap_offset = wrap_offset; | |
1443 | } | |
87adec2e | 1444 | |
cc88a640 | 1445 | RL_UNSETSTATE (RL_STATE_REDISPLAYING); |
87adec2e | 1446 | _rl_release_sigint (); |
d60d9f65 SS |
1447 | } |
1448 | ||
1449 | /* PWP: update_line() is based on finding the middle difference of each | |
1450 | line on the screen; vis: | |
1451 | ||
1452 | /old first difference | |
1453 | /beginning of line | /old last same /old EOL | |
1454 | v v v v | |
1455 | old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as | |
1456 | new: eddie> Oh, my little buggy says to me, as lurgid as | |
1457 | ^ ^ ^ ^ | |
1458 | \beginning of line | \new last same \new end of line | |
1459 | \new first difference | |
1460 | ||
1461 | All are character pointers for the sake of speed. Special cases for | |
c862e87b | 1462 | no differences, as well as for end of line additions must be handled. |
d60d9f65 SS |
1463 | |
1464 | Could be made even smarter, but this works well enough */ | |
1465 | static void | |
cb41b9e7 | 1466 | update_line (char *old, char *new, int current_line, int omax, int nmax, int inv_botlin) |
d60d9f65 SS |
1467 | { |
1468 | register char *ofd, *ols, *oe, *nfd, *nls, *ne; | |
cc88a640 | 1469 | int temp, lendiff, wsatend, od, nd, twidth, o_cpos; |
d60d9f65 | 1470 | int current_invis_chars; |
9255ee31 | 1471 | int col_lendiff, col_temp; |
775e241e TT |
1472 | int bytes_to_insert; |
1473 | int mb_cur_max = MB_CUR_MAX; | |
9255ee31 EZ |
1474 | #if defined (HANDLE_MULTIBYTE) |
1475 | mbstate_t ps_new, ps_old; | |
cc88a640 | 1476 | int new_offset, old_offset; |
9255ee31 | 1477 | #endif |
d60d9f65 SS |
1478 | |
1479 | /* If we're at the right edge of a terminal that supports xn, we're | |
1480 | ready to wrap around, so do so. This fixes problems with knowing | |
1481 | the exact cursor position and cut-and-paste with certain terminal | |
1482 | emulators. In this calculation, TEMP is the physical screen | |
1483 | position of the cursor. */ | |
775e241e | 1484 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
5bdf8622 DJ |
1485 | temp = _rl_last_c_pos; |
1486 | else | |
cc88a640 | 1487 | temp = _rl_last_c_pos - WRAP_OFFSET (_rl_last_v_pos, visible_wrap_offset); |
9255ee31 EZ |
1488 | if (temp == _rl_screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode |
1489 | && _rl_last_v_pos == current_line - 1) | |
d60d9f65 | 1490 | { |
cb41b9e7 TT |
1491 | /* We're going to wrap around by writing the first character of NEW to |
1492 | the screen and dealing with changes to what's visible by modifying | |
1493 | OLD to match it. Complicated by the presence of multi-width | |
1494 | characters at the end of the line or beginning of the new one. */ | |
1495 | /* old is always somewhere in visible_line; new is always somewhere in | |
1496 | invisible_line. These should always be null-terminated. */ | |
9255ee31 | 1497 | #if defined (HANDLE_MULTIBYTE) |
775e241e | 1498 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1499 | { |
1500 | wchar_t wc; | |
1501 | mbstate_t ps; | |
cb41b9e7 TT |
1502 | int oldwidth, newwidth; |
1503 | int oldbytes, newbytes; | |
9255ee31 EZ |
1504 | size_t ret; |
1505 | ||
1506 | /* This fixes only double-column characters, but if the wrapped | |
775e241e | 1507 | character consumes more than three columns, spaces will be |
9255ee31 | 1508 | inserted in the string buffer. */ |
cb41b9e7 TT |
1509 | /* XXX remember that we are working on the invisible line right now; |
1510 | we don't swap visible and invisible until just before rl_redisplay | |
1511 | returns */ | |
1512 | /* This will remove the extra placeholder space we added with | |
1513 | _rl_wrapped_multicolumn */ | |
1514 | if (current_line < line_state_invisible->wbsize && line_state_invisible->wrapped_line[current_line] > 0) | |
1515 | _rl_clear_to_eol (line_state_invisible->wrapped_line[current_line]); | |
1516 | ||
1517 | /* 1. how many screen positions does first char in old consume? */ | |
1518 | memset (&ps, 0, sizeof (mbstate_t)); | |
1519 | ret = mbrtowc (&wc, old, mb_cur_max, &ps); | |
1520 | oldbytes = ret; | |
1521 | if (MB_INVALIDCH (ret)) | |
1522 | { | |
1523 | oldwidth = 1; | |
1524 | oldbytes = 1; | |
1525 | } | |
1526 | else if (MB_NULLWCH (ret)) | |
1527 | oldwidth = 0; | |
1528 | else | |
1529 | oldwidth = WCWIDTH (wc); | |
1530 | if (oldwidth < 0) | |
1531 | oldwidth = 1; | |
9255ee31 | 1532 | |
cb41b9e7 | 1533 | /* 2. how many screen positions does the first char in new consume? */ |
9255ee31 | 1534 | memset (&ps, 0, sizeof (mbstate_t)); |
775e241e | 1535 | ret = mbrtowc (&wc, new, mb_cur_max, &ps); |
cb41b9e7 | 1536 | newbytes = ret; |
5bdf8622 | 1537 | if (MB_INVALIDCH (ret)) |
9255ee31 | 1538 | { |
cb41b9e7 TT |
1539 | newwidth = 1; |
1540 | newbytes = 1; | |
9255ee31 | 1541 | } |
5bdf8622 | 1542 | else if (MB_NULLWCH (ret)) |
cb41b9e7 | 1543 | newwidth = 0; |
9255ee31 | 1544 | else |
cb41b9e7 TT |
1545 | newwidth = WCWIDTH (wc); |
1546 | if (newwidth < 0) | |
1547 | newwidth = 1; | |
1548 | ||
1549 | /* 3. if the new width is less than the old width, we need to keep | |
1550 | going in new until we have consumed at least that many screen | |
1551 | positions, and figure out how many bytes that will take */ | |
1552 | while (newbytes < nmax && newwidth < oldwidth) | |
1553 | { | |
1554 | int t; | |
9255ee31 | 1555 | |
cb41b9e7 TT |
1556 | ret = mbrtowc (&wc, new+newbytes, mb_cur_max, &ps); |
1557 | if (MB_INVALIDCH (ret)) | |
1558 | { | |
1559 | newwidth += 1; | |
1560 | newbytes += 1; | |
1561 | } | |
1562 | else if (MB_NULLWCH (ret)) | |
1563 | break; | |
1564 | else | |
1565 | { | |
1566 | t = WCWIDTH (wc); | |
1567 | newwidth += (t >= 0) ? t : 1; | |
1568 | newbytes += ret; | |
1569 | } | |
1570 | } | |
1571 | /* 4. If the new width is more than the old width, keep going in old | |
1572 | until we have consumed exactly that many screen positions, and | |
1573 | figure out how many bytes that will take. This is an optimization */ | |
1574 | while (oldbytes < omax && oldwidth < newwidth) | |
9255ee31 | 1575 | { |
cb41b9e7 TT |
1576 | int t; |
1577 | ||
1578 | ret = mbrtowc (&wc, old+oldbytes, mb_cur_max, &ps); | |
1579 | if (MB_INVALIDCH (ret)) | |
1580 | { | |
1581 | oldwidth += 1; | |
1582 | oldbytes += 1; | |
1583 | } | |
1584 | else if (MB_NULLWCH (ret)) | |
1585 | break; | |
1586 | else | |
1587 | { | |
1588 | t = WCWIDTH (wc); | |
1589 | oldwidth += (t >= 0) ? t : 1; | |
1590 | oldbytes += ret; | |
1591 | } | |
1592 | } | |
1593 | /* 5. write the first newbytes of new, which takes newwidth. This is | |
1594 | where the screen wrapping takes place, and we are now writing | |
1595 | characters onto the new line. We need to fix up old so it | |
1596 | accurately reflects what is on the screen after the | |
1597 | _rl_output_some_chars below. */ | |
1598 | if (newwidth > 0) | |
1599 | { | |
1600 | int count, i, j; | |
1601 | char *optr; | |
1602 | ||
1603 | _rl_output_some_chars (new, newbytes); | |
1604 | _rl_last_c_pos = newwidth; | |
9255ee31 | 1605 | _rl_last_v_pos++; |
cb41b9e7 TT |
1606 | |
1607 | /* 5a. If the number of screen positions doesn't match, punt | |
1608 | and do a dumb update. */ | |
1609 | if (newwidth != oldwidth) | |
9255ee31 | 1610 | { |
cb41b9e7 TT |
1611 | ne = new + nmax; |
1612 | nd = newbytes; | |
1613 | nfd = new + nd; | |
1614 | goto dumb_update; | |
1615 | } | |
1616 | if (oldbytes != 0 && newbytes != 0) | |
1617 | { | |
1618 | /* We have written as many bytes from new as we need to | |
1619 | consume the first character of old. Fix up `old' so it | |
1620 | reflects the new screen contents. We use +1 in the | |
1621 | memmove call to copy the trailing NUL. */ | |
1622 | memmove (old+newbytes, old+oldbytes, strlen (old+oldbytes) + 1); | |
1623 | memcpy (old, new, newbytes); | |
1624 | j = newbytes - oldbytes; | |
1625 | ||
1626 | omax += j; | |
cc88a640 | 1627 | /* Fix up indices if we copy data from one line to another */ |
775e241e | 1628 | for (i = current_line+1; i <= inv_botlin+1; i++) |
cb41b9e7 | 1629 | vis_lbreaks[i] += j; |
9255ee31 EZ |
1630 | } |
1631 | } | |
1632 | else | |
1633 | { | |
1634 | putc (' ', rl_outstream); | |
1635 | _rl_last_c_pos = 1; | |
1636 | _rl_last_v_pos++; | |
1637 | if (old[0] && new[0]) | |
1638 | old[0] = new[0]; | |
1639 | } | |
1640 | } | |
d60d9f65 | 1641 | else |
9255ee31 EZ |
1642 | #endif |
1643 | { | |
1644 | if (new[0]) | |
1645 | putc (new[0], rl_outstream); | |
1646 | else | |
1647 | putc (' ', rl_outstream); | |
5bdf8622 | 1648 | _rl_last_c_pos = 1; |
9255ee31 EZ |
1649 | _rl_last_v_pos++; |
1650 | if (old[0] && new[0]) | |
1651 | old[0] = new[0]; | |
1652 | } | |
d60d9f65 | 1653 | } |
9255ee31 | 1654 | |
d60d9f65 | 1655 | /* Find first difference. */ |
9255ee31 | 1656 | #if defined (HANDLE_MULTIBYTE) |
775e241e | 1657 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 | 1658 | { |
5bdf8622 DJ |
1659 | /* See if the old line is a subset of the new line, so that the |
1660 | only change is adding characters. */ | |
1661 | temp = (omax < nmax) ? omax : nmax; | |
cc88a640 | 1662 | if (memcmp (old, new, temp) == 0) /* adding at the end */ |
9255ee31 | 1663 | { |
775e241e | 1664 | new_offset = old_offset = temp; |
5bdf8622 DJ |
1665 | ofd = old + temp; |
1666 | nfd = new + temp; | |
1667 | } | |
1668 | else | |
1669 | { | |
1670 | memset (&ps_new, 0, sizeof(mbstate_t)); | |
1671 | memset (&ps_old, 0, sizeof(mbstate_t)); | |
1672 | ||
cb41b9e7 | 1673 | /* Are the old and new lines the same? */ |
5bdf8622 DJ |
1674 | if (omax == nmax && STREQN (new, old, omax)) |
1675 | { | |
775e241e TT |
1676 | old_offset = omax; |
1677 | new_offset = nmax; | |
5bdf8622 DJ |
1678 | ofd = old + omax; |
1679 | nfd = new + nmax; | |
1680 | } | |
1681 | else | |
1682 | { | |
cb41b9e7 TT |
1683 | /* Go through the line from the beginning and find the first |
1684 | difference. */ | |
5bdf8622 DJ |
1685 | new_offset = old_offset = 0; |
1686 | for (ofd = old, nfd = new; | |
1687 | (ofd - old < omax) && *ofd && | |
1688 | _rl_compare_chars(old, old_offset, &ps_old, new, new_offset, &ps_new); ) | |
1689 | { | |
1690 | old_offset = _rl_find_next_mbchar (old, old_offset, 1, MB_FIND_ANY); | |
1691 | new_offset = _rl_find_next_mbchar (new, new_offset, 1, MB_FIND_ANY); | |
775e241e | 1692 | |
5bdf8622 DJ |
1693 | ofd = old + old_offset; |
1694 | nfd = new + new_offset; | |
1695 | } | |
1696 | } | |
9255ee31 EZ |
1697 | } |
1698 | } | |
1699 | else | |
1700 | #endif | |
d60d9f65 SS |
1701 | for (ofd = old, nfd = new; |
1702 | (ofd - old < omax) && *ofd && (*ofd == *nfd); | |
1703 | ofd++, nfd++) | |
1704 | ; | |
1705 | ||
1706 | /* Move to the end of the screen line. ND and OD are used to keep track | |
1707 | of the distance between ne and new and oe and old, respectively, to | |
1708 | move a subtraction out of each loop. */ | |
1709 | for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++); | |
1710 | for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++); | |
1711 | ||
1712 | /* If no difference, continue to next line. */ | |
1713 | if (ofd == oe && nfd == ne) | |
1714 | return; | |
1715 | ||
775e241e TT |
1716 | #if defined (HANDLE_MULTIBYTE) |
1717 | if (mb_cur_max > 1 && rl_byte_oriented == 0 && _rl_utf8locale) | |
1718 | { | |
1719 | wchar_t wc; | |
1720 | mbstate_t ps = { 0 }; | |
1721 | int t; | |
1722 | ||
1723 | /* If the first character in the difference is a zero-width character, | |
1724 | assume it's a combining character and back one up so the two base | |
1725 | characters no longer compare equivalently. */ | |
1726 | t = mbrtowc (&wc, ofd, mb_cur_max, &ps); | |
1727 | if (t > 0 && UNICODE_COMBINING_CHAR (wc) && WCWIDTH (wc) == 0) | |
1728 | { | |
1729 | old_offset = _rl_find_prev_mbchar (old, ofd - old, MB_FIND_ANY); | |
1730 | new_offset = _rl_find_prev_mbchar (new, nfd - new, MB_FIND_ANY); | |
1731 | ofd = old + old_offset; /* equal by definition */ | |
1732 | nfd = new + new_offset; | |
1733 | } | |
1734 | } | |
1735 | #endif | |
1736 | ||
d60d9f65 | 1737 | wsatend = 1; /* flag for trailing whitespace */ |
9255ee31 EZ |
1738 | |
1739 | #if defined (HANDLE_MULTIBYTE) | |
cb41b9e7 TT |
1740 | /* Find the last character that is the same between the two lines. This |
1741 | bounds the region that needs to change. */ | |
775e241e | 1742 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1743 | { |
1744 | ols = old + _rl_find_prev_mbchar (old, oe - old, MB_FIND_ANY); | |
1745 | nls = new + _rl_find_prev_mbchar (new, ne - new, MB_FIND_ANY); | |
775e241e | 1746 | |
9255ee31 EZ |
1747 | while ((ols > ofd) && (nls > nfd)) |
1748 | { | |
1749 | memset (&ps_old, 0, sizeof (mbstate_t)); | |
1750 | memset (&ps_new, 0, sizeof (mbstate_t)); | |
1751 | ||
5bdf8622 DJ |
1752 | #if 0 |
1753 | /* On advice from jir@yamato.ibm.com */ | |
9255ee31 EZ |
1754 | _rl_adjust_point (old, ols - old, &ps_old); |
1755 | _rl_adjust_point (new, nls - new, &ps_new); | |
5bdf8622 | 1756 | #endif |
9255ee31 EZ |
1757 | |
1758 | if (_rl_compare_chars (old, ols - old, &ps_old, new, nls - new, &ps_new) == 0) | |
1759 | break; | |
1760 | ||
1761 | if (*ols == ' ') | |
1762 | wsatend = 0; | |
1763 | ||
1764 | ols = old + _rl_find_prev_mbchar (old, ols - old, MB_FIND_ANY); | |
1765 | nls = new + _rl_find_prev_mbchar (new, nls - new, MB_FIND_ANY); | |
1766 | } | |
1767 | } | |
1768 | else | |
1769 | { | |
1770 | #endif /* HANDLE_MULTIBYTE */ | |
d60d9f65 SS |
1771 | ols = oe - 1; /* find last same */ |
1772 | nls = ne - 1; | |
1773 | while ((ols > ofd) && (nls > nfd) && (*ols == *nls)) | |
1774 | { | |
1775 | if (*ols != ' ') | |
1776 | wsatend = 0; | |
1777 | ols--; | |
1778 | nls--; | |
1779 | } | |
9255ee31 EZ |
1780 | #if defined (HANDLE_MULTIBYTE) |
1781 | } | |
1782 | #endif | |
d60d9f65 SS |
1783 | |
1784 | if (wsatend) | |
1785 | { | |
1786 | ols = oe; | |
1787 | nls = ne; | |
1788 | } | |
9255ee31 EZ |
1789 | #if defined (HANDLE_MULTIBYTE) |
1790 | /* This may not work for stateful encoding, but who cares? To handle | |
1791 | stateful encoding properly, we have to scan each string from the | |
1792 | beginning and compare. */ | |
1793 | else if (_rl_compare_chars (ols, 0, NULL, nls, 0, NULL) == 0) | |
1794 | #else | |
d60d9f65 | 1795 | else if (*ols != *nls) |
9255ee31 | 1796 | #endif |
d60d9f65 SS |
1797 | { |
1798 | if (*ols) /* don't step past the NUL */ | |
9255ee31 | 1799 | { |
775e241e | 1800 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1801 | ols = old + _rl_find_next_mbchar (old, ols - old, 1, MB_FIND_ANY); |
1802 | else | |
1803 | ols++; | |
1804 | } | |
d60d9f65 | 1805 | if (*nls) |
9255ee31 | 1806 | { |
775e241e | 1807 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1808 | nls = new + _rl_find_next_mbchar (new, nls - new, 1, MB_FIND_ANY); |
1809 | else | |
1810 | nls++; | |
1811 | } | |
d60d9f65 SS |
1812 | } |
1813 | ||
1814 | /* count of invisible characters in the current invisible line. */ | |
1815 | current_invis_chars = W_OFFSET (current_line, wrap_offset); | |
1816 | if (_rl_last_v_pos != current_line) | |
1817 | { | |
1818 | _rl_move_vert (current_line); | |
775e241e TT |
1819 | /* We have moved up to a new screen line. This line may or may not have |
1820 | invisible characters on it, but we do our best to recalculate | |
1821 | visible_wrap_offset based on what we know. */ | |
1822 | if (current_line == 0) | |
1823 | visible_wrap_offset = prompt_invis_chars_first_line; /* XXX */ | |
1824 | if ((mb_cur_max == 1 || rl_byte_oriented) && current_line == 0 && visible_wrap_offset) | |
d60d9f65 SS |
1825 | _rl_last_c_pos += visible_wrap_offset; |
1826 | } | |
1827 | ||
1828 | /* If this is the first line and there are invisible characters in the | |
1829 | prompt string, and the prompt string has not changed, and the current | |
1830 | cursor position is before the last invisible character in the prompt, | |
1831 | and the index of the character to move to is past the end of the prompt | |
1832 | string, then redraw the entire prompt string. We can only do this | |
1833 | reliably if the terminal supports a `cr' capability. | |
1834 | ||
775e241e TT |
1835 | This can also happen if the prompt string has changed, and the first |
1836 | difference in the line is in the middle of the prompt string, after a | |
1837 | sequence of invisible characters (worst case) and before the end of | |
1838 | the prompt. In this case, we have to redraw the entire prompt string | |
1839 | so that the entire sequence of invisible characters is drawn. We need | |
1840 | to handle the worst case, when the difference is after (or in the middle | |
1841 | of) a sequence of invisible characters that changes the text color and | |
1842 | before the sequence that restores the text color to normal. Then we have | |
1843 | to make sure that the lines still differ -- if they don't, we can | |
1844 | return immediately. | |
1845 | ||
d60d9f65 SS |
1846 | This is not an efficiency hack -- there is a problem with redrawing |
1847 | portions of the prompt string if they contain terminal escape | |
1848 | sequences (like drawing the `unbold' sequence without a corresponding | |
1849 | `bold') that manifests itself on certain terminals. */ | |
1850 | ||
cc88a640 | 1851 | lendiff = local_prompt_len; |
775e241e TT |
1852 | if (lendiff > nmax) |
1853 | lendiff = nmax; | |
d60d9f65 | 1854 | od = ofd - old; /* index of first difference in visible line */ |
775e241e | 1855 | nd = nfd - new; /* nd, od are buffer indexes */ |
d60d9f65 | 1856 | if (current_line == 0 && !_rl_horizontal_scroll_mode && |
9255ee31 | 1857 | _rl_term_cr && lendiff > prompt_visible_length && _rl_last_c_pos > 0 && |
775e241e TT |
1858 | (((od > 0 || nd > 0) && (od <= prompt_last_invisible || nd <= prompt_last_invisible)) || |
1859 | ((od >= lendiff) && _rl_last_c_pos < PROMPT_ENDING_INDEX))) | |
d60d9f65 | 1860 | { |
771578d1 SS |
1861 | #if defined (__MSDOS__) |
1862 | putc ('\r', rl_outstream); | |
1863 | #else | |
9255ee31 | 1864 | tputs (_rl_term_cr, 1, _rl_output_character_function); |
1b17e766 | 1865 | #endif |
cc88a640 JK |
1866 | if (modmark) |
1867 | _rl_output_some_chars ("*", 1); | |
d60d9f65 | 1868 | _rl_output_some_chars (local_prompt, lendiff); |
775e241e | 1869 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
5bdf8622 DJ |
1870 | { |
1871 | /* We take wrap_offset into account here so we can pass correct | |
1872 | information to _rl_move_cursor_relative. */ | |
cc88a640 | 1873 | _rl_last_c_pos = _rl_col_width (local_prompt, 0, lendiff, 1) - wrap_offset + modmark; |
5bdf8622 DJ |
1874 | cpos_adjusted = 1; |
1875 | } | |
9255ee31 | 1876 | else |
cc88a640 | 1877 | _rl_last_c_pos = lendiff + modmark; |
775e241e TT |
1878 | |
1879 | /* Now if we have printed the prompt string because the first difference | |
1880 | was within the prompt, see if we need to recompute where the lines | |
1881 | differ. Check whether where we are now is past the last place where | |
1882 | the old and new lines are the same and short-circuit now if we are. */ | |
1883 | if ((od <= prompt_last_invisible || nd <= prompt_last_invisible) && | |
1884 | omax == nmax && | |
1885 | lendiff > (ols-old) && lendiff > (nls-new)) | |
1886 | return; | |
1887 | ||
1888 | /* XXX - we need to fix up our calculations if we are now past the | |
1889 | old ofd/nfd and the prompt length (or line length) has changed. | |
1890 | We punt on the problem and do a dumb update. We'd like to be able | |
1891 | to just output the prompt from the beginning of the line up to the | |
1892 | first difference, but you don't know the number of invisible | |
1893 | characters in that case. | |
1894 | This needs a lot of work to be efficient. */ | |
1895 | if ((od <= prompt_last_invisible || nd <= prompt_last_invisible)) | |
1896 | { | |
1897 | nfd = new + lendiff; /* number of characters we output above */ | |
1898 | nd = lendiff; | |
1899 | ||
1900 | /* Do a dumb update and return */ | |
cb41b9e7 | 1901 | dumb_update: |
775e241e TT |
1902 | temp = ne - nfd; |
1903 | if (temp > 0) | |
1904 | { | |
1905 | _rl_output_some_chars (nfd, temp); | |
1906 | if (mb_cur_max > 1 && rl_byte_oriented == 0) | |
cb41b9e7 TT |
1907 | { |
1908 | _rl_last_c_pos += _rl_col_width (new, nd, ne - new, 1); | |
1909 | /* Need to adjust here based on wrap_offset. Guess that if | |
1910 | this is the line containing the last line of the prompt | |
1911 | we need to adjust by | |
1912 | wrap_offset-prompt_invis_chars_first_line | |
1913 | on the assumption that this is the number of invisible | |
1914 | characters in the last line of the prompt. */ | |
1915 | if (wrap_offset > prompt_invis_chars_first_line && | |
1916 | current_line == prompt_last_screen_line && | |
1917 | prompt_physical_chars > _rl_screenwidth && | |
1918 | _rl_horizontal_scroll_mode == 0) | |
1919 | { | |
1920 | _rl_last_c_pos -= wrap_offset - prompt_invis_chars_first_line; | |
1921 | cpos_adjusted = 1; | |
1922 | } | |
1923 | } | |
775e241e TT |
1924 | else |
1925 | _rl_last_c_pos += temp; | |
1926 | } | |
1927 | if (nmax < omax) | |
1928 | goto clear_rest_of_line; /* XXX */ | |
1929 | else | |
1930 | return; | |
1931 | } | |
d60d9f65 SS |
1932 | } |
1933 | ||
cc88a640 JK |
1934 | o_cpos = _rl_last_c_pos; |
1935 | ||
1936 | /* When this function returns, _rl_last_c_pos is correct, and an absolute | |
775e241e | 1937 | cursor position in multibyte mode, but a buffer index when not in a |
cc88a640 | 1938 | multibyte locale. */ |
d60d9f65 | 1939 | _rl_move_cursor_relative (od, old); |
775e241e | 1940 | |
cc88a640 JK |
1941 | #if defined (HANDLE_MULTIBYTE) |
1942 | /* We need to indicate that the cursor position is correct in the presence of | |
1943 | invisible characters in the prompt string. Let's see if setting this when | |
1944 | we make sure we're at the end of the drawn prompt string works. */ | |
775e241e | 1945 | if (current_line == 0 && mb_cur_max > 1 && rl_byte_oriented == 0 && |
cc88a640 JK |
1946 | (_rl_last_c_pos > 0 || o_cpos > 0) && |
1947 | _rl_last_c_pos == prompt_physical_chars) | |
1948 | cpos_adjusted = 1; | |
cc88a640 | 1949 | #endif |
d60d9f65 | 1950 | |
9255ee31 | 1951 | /* if (len (new) > len (old)) |
775e241e TT |
1952 | lendiff == difference in buffer (bytes) |
1953 | col_lendiff == difference on screen (columns) | |
9255ee31 | 1954 | When not using multibyte characters, these are equal */ |
d60d9f65 | 1955 | lendiff = (nls - nfd) - (ols - ofd); |
775e241e | 1956 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
cc88a640 | 1957 | col_lendiff = _rl_col_width (new, nfd - new, nls - new, 1) - _rl_col_width (old, ofd - old, ols - old, 1); |
9255ee31 EZ |
1958 | else |
1959 | col_lendiff = lendiff; | |
d60d9f65 SS |
1960 | |
1961 | /* If we are changing the number of invisible characters in a line, and | |
1962 | the spot of first difference is before the end of the invisible chars, | |
1963 | lendiff needs to be adjusted. */ | |
775e241e | 1964 | if (current_line == 0 && /* !_rl_horizontal_scroll_mode && */ |
d60d9f65 | 1965 | current_invis_chars != visible_wrap_offset) |
9255ee31 | 1966 | { |
775e241e | 1967 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 EZ |
1968 | { |
1969 | lendiff += visible_wrap_offset - current_invis_chars; | |
1970 | col_lendiff += visible_wrap_offset - current_invis_chars; | |
1971 | } | |
1972 | else | |
1973 | { | |
1974 | lendiff += visible_wrap_offset - current_invis_chars; | |
1975 | col_lendiff = lendiff; | |
1976 | } | |
1977 | } | |
d60d9f65 | 1978 | |
775e241e TT |
1979 | /* We use temp as a count of the number of bytes from the first difference |
1980 | to the end of the new line. col_temp is the corresponding number of | |
1981 | screen columns. A `dumb' update moves to the spot of first difference | |
1982 | and writes TEMP bytes. */ | |
d60d9f65 SS |
1983 | /* Insert (diff (len (old), len (new)) ch. */ |
1984 | temp = ne - nfd; | |
775e241e | 1985 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
cc88a640 | 1986 | col_temp = _rl_col_width (new, nfd - new, ne - new, 1); |
9255ee31 EZ |
1987 | else |
1988 | col_temp = temp; | |
1989 | ||
775e241e TT |
1990 | /* how many bytes from the new line buffer to write to the display */ |
1991 | bytes_to_insert = nls - nfd; | |
1992 | ||
1993 | /* col_lendiff > 0 if we are adding characters to the line */ | |
9255ee31 | 1994 | if (col_lendiff > 0) /* XXX - was lendiff */ |
d60d9f65 SS |
1995 | { |
1996 | /* Non-zero if we're increasing the number of lines. */ | |
1997 | int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin; | |
cb41b9e7 | 1998 | |
cc88a640 JK |
1999 | /* If col_lendiff is > 0, implying that the new string takes up more |
2000 | screen real estate than the old, but lendiff is < 0, meaning that it | |
2001 | takes fewer bytes, we need to just output the characters starting | |
2002 | from the first difference. These will overwrite what is on the | |
2003 | display, so there's no reason to do a smart update. This can really | |
2004 | only happen in a multibyte environment. */ | |
2005 | if (lendiff < 0) | |
2006 | { | |
2007 | _rl_output_some_chars (nfd, temp); | |
775e241e | 2008 | _rl_last_c_pos += col_temp; /* XXX - was _rl_col_width (nfd, 0, temp, 1); */ |
cc88a640 JK |
2009 | /* If nfd begins before any invisible characters in the prompt, |
2010 | adjust _rl_last_c_pos to account for wrap_offset and set | |
2011 | cpos_adjusted to let the caller know. */ | |
775e241e | 2012 | if (current_line == 0 && displaying_prompt_first_line && wrap_offset && ((nfd - new) <= prompt_last_invisible)) |
cc88a640 | 2013 | { |
cb41b9e7 | 2014 | _rl_last_c_pos -= wrap_offset; /* XXX - prompt_invis_chars_first_line? */ |
cc88a640 JK |
2015 | cpos_adjusted = 1; |
2016 | } | |
2017 | return; | |
2018 | } | |
d60d9f65 SS |
2019 | /* Sometimes it is cheaper to print the characters rather than |
2020 | use the terminal's capabilities. If we're growing the number | |
2021 | of lines, make sure we actually cause the new line to wrap | |
2022 | around on auto-wrapping terminals. */ | |
cc88a640 | 2023 | else if (_rl_terminal_can_insert && ((2 * col_temp) >= col_lendiff || _rl_term_IC) && (!_rl_term_autowrap || !gl)) |
d60d9f65 | 2024 | { |
9255ee31 | 2025 | /* If lendiff > prompt_visible_length and _rl_last_c_pos == 0 and |
d60d9f65 | 2026 | _rl_horizontal_scroll_mode == 1, inserting the characters with |
9255ee31 | 2027 | _rl_term_IC or _rl_term_ic will screw up the screen because of the |
d60d9f65 | 2028 | invisible characters. We need to just draw them. */ |
cc88a640 JK |
2029 | /* The same thing happens if we're trying to draw before the last |
2030 | invisible character in the prompt string or we're increasing the | |
2031 | number of invisible characters in the line and we're not drawing | |
2032 | the entire prompt string. */ | |
2033 | if (*ols && ((_rl_horizontal_scroll_mode && | |
2034 | _rl_last_c_pos == 0 && | |
2035 | lendiff > prompt_visible_length && | |
2036 | current_invis_chars > 0) == 0) && | |
775e241e | 2037 | (((mb_cur_max > 1 && rl_byte_oriented == 0) && |
cc88a640 JK |
2038 | current_line == 0 && wrap_offset && |
2039 | ((nfd - new) <= prompt_last_invisible) && | |
2040 | (col_lendiff < prompt_visible_length)) == 0) && | |
2041 | (visible_wrap_offset >= current_invis_chars)) | |
d60d9f65 | 2042 | { |
775e241e TT |
2043 | open_some_spaces (col_lendiff); |
2044 | _rl_output_some_chars (nfd, bytes_to_insert); | |
2045 | if (mb_cur_max > 1 && rl_byte_oriented == 0) | |
2046 | _rl_last_c_pos += _rl_col_width (nfd, 0, bytes_to_insert, 1); | |
2047 | else | |
2048 | _rl_last_c_pos += bytes_to_insert; | |
cc88a640 | 2049 | } |
775e241e | 2050 | else if ((mb_cur_max == 1 || rl_byte_oriented != 0) && *ols == 0 && lendiff > 0) |
d60d9f65 SS |
2051 | { |
2052 | /* At the end of a line the characters do not have to | |
2053 | be "inserted". They can just be placed on the screen. */ | |
775e241e TT |
2054 | _rl_output_some_chars (nfd, temp); |
2055 | _rl_last_c_pos += col_temp; | |
2056 | return; | |
d60d9f65 | 2057 | } |
775e241e | 2058 | else /* just write from first difference to end of new line */ |
d60d9f65 | 2059 | { |
d60d9f65 | 2060 | _rl_output_some_chars (nfd, temp); |
9255ee31 | 2061 | _rl_last_c_pos += col_temp; |
cc88a640 JK |
2062 | /* If nfd begins before the last invisible character in the |
2063 | prompt, adjust _rl_last_c_pos to account for wrap_offset | |
2064 | and set cpos_adjusted to let the caller know. */ | |
775e241e | 2065 | if ((mb_cur_max > 1 && rl_byte_oriented == 0) && current_line == 0 && displaying_prompt_first_line && wrap_offset && ((nfd - new) <= prompt_last_invisible)) |
cc88a640 | 2066 | { |
cb41b9e7 | 2067 | _rl_last_c_pos -= wrap_offset; /* XXX - prompt_invis_chars_first_line? */ |
cc88a640 JK |
2068 | cpos_adjusted = 1; |
2069 | } | |
d60d9f65 SS |
2070 | return; |
2071 | } | |
775e241e TT |
2072 | |
2073 | if (bytes_to_insert > lendiff) | |
d60d9f65 | 2074 | { |
cc88a640 JK |
2075 | /* If nfd begins before the last invisible character in the |
2076 | prompt, adjust _rl_last_c_pos to account for wrap_offset | |
2077 | and set cpos_adjusted to let the caller know. */ | |
775e241e | 2078 | if ((mb_cur_max > 1 && rl_byte_oriented == 0) && current_line == 0 && displaying_prompt_first_line && wrap_offset && ((nfd - new) <= prompt_last_invisible)) |
cc88a640 | 2079 | { |
cb41b9e7 | 2080 | _rl_last_c_pos -= wrap_offset; /* XXX - prompt_invis_chars_first_line? */ |
cc88a640 JK |
2081 | cpos_adjusted = 1; |
2082 | } | |
d60d9f65 SS |
2083 | } |
2084 | } | |
2085 | else | |
2086 | { | |
2087 | /* cannot insert chars, write to EOL */ | |
2088 | _rl_output_some_chars (nfd, temp); | |
9255ee31 | 2089 | _rl_last_c_pos += col_temp; |
5bdf8622 DJ |
2090 | /* If we're in a multibyte locale and were before the last invisible |
2091 | char in the current line (which implies we just output some invisible | |
2092 | characters) we need to adjust _rl_last_c_pos, since it represents | |
2093 | a physical character position. */ | |
775e241e TT |
2094 | /* The current_line*rl_screenwidth+prompt_invis_chars_first_line is a |
2095 | crude attempt to compute how far into the new line buffer we are. | |
2096 | It doesn't work well in the face of multibyte characters and needs | |
2097 | to be rethought. XXX */ | |
2098 | if ((mb_cur_max > 1 && rl_byte_oriented == 0) && | |
cc88a640 | 2099 | current_line == prompt_last_screen_line && wrap_offset && |
775e241e | 2100 | displaying_prompt_first_line && |
cc88a640 | 2101 | wrap_offset != prompt_invis_chars_first_line && |
775e241e | 2102 | ((nfd-new) < (prompt_last_invisible-(current_line*_rl_screenwidth+prompt_invis_chars_first_line)))) |
cc88a640 JK |
2103 | { |
2104 | _rl_last_c_pos -= wrap_offset - prompt_invis_chars_first_line; | |
2105 | cpos_adjusted = 1; | |
2106 | } | |
d60d9f65 SS |
2107 | } |
2108 | } | |
2109 | else /* Delete characters from line. */ | |
2110 | { | |
2111 | /* If possible and inexpensive to use terminal deletion, then do so. */ | |
9255ee31 | 2112 | if (_rl_term_dc && (2 * col_temp) >= -col_lendiff) |
d60d9f65 SS |
2113 | { |
2114 | /* If all we're doing is erasing the invisible characters in the | |
2115 | prompt string, don't bother. It screws up the assumptions | |
2116 | about what's on the screen. */ | |
2117 | if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 && | |
775e241e | 2118 | displaying_prompt_first_line && |
d60d9f65 | 2119 | -lendiff == visible_wrap_offset) |
9255ee31 | 2120 | col_lendiff = 0; |
d60d9f65 | 2121 | |
775e241e TT |
2122 | /* If we have moved lmargin and we're shrinking the line, we've |
2123 | already moved the cursor to the first character of the new line, | |
2124 | so deleting -col_lendiff characters will mess up the cursor | |
2125 | position calculation */ | |
2126 | if (_rl_horizontal_scroll_mode && displaying_prompt_first_line == 0 && | |
2127 | col_lendiff && _rl_last_c_pos < -col_lendiff) | |
2128 | col_lendiff = 0; | |
2129 | ||
9255ee31 EZ |
2130 | if (col_lendiff) |
2131 | delete_chars (-col_lendiff); /* delete (diff) characters */ | |
d60d9f65 | 2132 | |
775e241e TT |
2133 | /* Copy (new) chars to screen from first diff to last match, |
2134 | overwriting what is there. */ | |
2135 | if (bytes_to_insert > 0) | |
d60d9f65 | 2136 | { |
cc88a640 JK |
2137 | /* If nfd begins at the prompt, or before the invisible |
2138 | characters in the prompt, we need to adjust _rl_last_c_pos | |
2139 | in a multibyte locale to account for the wrap offset and | |
2140 | set cpos_adjusted accordingly. */ | |
775e241e TT |
2141 | _rl_output_some_chars (nfd, bytes_to_insert); |
2142 | if (mb_cur_max > 1 && rl_byte_oriented == 0) | |
cc88a640 | 2143 | { |
775e241e TT |
2144 | _rl_last_c_pos += _rl_col_width (nfd, 0, bytes_to_insert, 1); |
2145 | if (current_line == 0 && wrap_offset && | |
2146 | displaying_prompt_first_line && | |
cb41b9e7 | 2147 | _rl_last_c_pos >= wrap_offset && /* XXX was > */ |
775e241e | 2148 | ((nfd - new) <= prompt_last_invisible)) |
cc88a640 | 2149 | { |
cb41b9e7 | 2150 | _rl_last_c_pos -= wrap_offset; /* XXX - prompt_invis_chars_first_line? */ |
cc88a640 JK |
2151 | cpos_adjusted = 1; |
2152 | } | |
cb41b9e7 TT |
2153 | |
2154 | #if 1 | |
2155 | #ifdef HANDLE_MULTIBYTE | |
2156 | /* If we write a non-space into the last screen column, | |
2157 | remove the note that we added a space to compensate for | |
2158 | a multibyte double-width character that didn't fit, since | |
2159 | it's only valid for what was previously there. */ | |
2160 | /* XXX - watch this */ | |
2161 | if (_rl_last_c_pos == _rl_screenwidth && | |
2162 | line_state_invisible->wrapped_line[current_line+1] && | |
2163 | nfd[bytes_to_insert-1] != ' ') | |
2164 | line_state_invisible->wrapped_line[current_line+1] = 0; | |
2165 | #endif | |
2166 | #endif | |
cc88a640 JK |
2167 | } |
2168 | else | |
775e241e TT |
2169 | _rl_last_c_pos += bytes_to_insert; |
2170 | ||
2171 | /* XXX - we only want to do this if we are at the end of the line | |
2172 | so we move there with _rl_move_cursor_relative */ | |
2173 | if (_rl_horizontal_scroll_mode && ((oe-old) > (ne-new))) | |
2174 | { | |
2175 | _rl_move_cursor_relative (ne-new, new); | |
2176 | goto clear_rest_of_line; | |
2177 | } | |
d60d9f65 SS |
2178 | } |
2179 | } | |
2180 | /* Otherwise, print over the existing material. */ | |
2181 | else | |
2182 | { | |
2183 | if (temp > 0) | |
2184 | { | |
cc88a640 JK |
2185 | /* If nfd begins at the prompt, or before the invisible |
2186 | characters in the prompt, we need to adjust _rl_last_c_pos | |
2187 | in a multibyte locale to account for the wrap offset and | |
2188 | set cpos_adjusted accordingly. */ | |
d60d9f65 | 2189 | _rl_output_some_chars (nfd, temp); |
5bdf8622 | 2190 | _rl_last_c_pos += col_temp; /* XXX */ |
775e241e | 2191 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
cc88a640 | 2192 | { |
775e241e TT |
2193 | if (current_line == 0 && wrap_offset && |
2194 | displaying_prompt_first_line && | |
2195 | _rl_last_c_pos > wrap_offset && | |
2196 | ((nfd - new) <= prompt_last_invisible)) | |
cc88a640 | 2197 | { |
cb41b9e7 | 2198 | _rl_last_c_pos -= wrap_offset; /* XXX - prompt_invis_chars_first_line? */ |
cc88a640 JK |
2199 | cpos_adjusted = 1; |
2200 | } | |
2201 | } | |
d60d9f65 | 2202 | } |
775e241e | 2203 | clear_rest_of_line: |
d60d9f65 | 2204 | lendiff = (oe - old) - (ne - new); |
775e241e | 2205 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
cc88a640 | 2206 | col_lendiff = _rl_col_width (old, 0, oe - old, 1) - _rl_col_width (new, 0, ne - new, 1); |
9255ee31 EZ |
2207 | else |
2208 | col_lendiff = lendiff; | |
2209 | ||
cc88a640 JK |
2210 | /* If we've already printed over the entire width of the screen, |
2211 | including the old material, then col_lendiff doesn't matter and | |
2212 | space_to_eol will insert too many spaces. XXX - maybe we should | |
2213 | adjust col_lendiff based on the difference between _rl_last_c_pos | |
2214 | and _rl_screenwidth */ | |
775e241e | 2215 | if (col_lendiff && ((mb_cur_max == 1 || rl_byte_oriented) || (_rl_last_c_pos < _rl_screenwidth))) |
c862e87b JM |
2216 | { |
2217 | if (_rl_term_autowrap && current_line < inv_botlin) | |
9255ee31 | 2218 | space_to_eol (col_lendiff); |
c862e87b | 2219 | else |
9255ee31 | 2220 | _rl_clear_to_eol (col_lendiff); |
c862e87b | 2221 | } |
d60d9f65 SS |
2222 | } |
2223 | } | |
2224 | } | |
2225 | ||
2226 | /* Tell the update routines that we have moved onto a new (empty) line. */ | |
2227 | int | |
cb41b9e7 | 2228 | rl_on_new_line (void) |
d60d9f65 SS |
2229 | { |
2230 | if (visible_line) | |
2231 | visible_line[0] = '\0'; | |
2232 | ||
2233 | _rl_last_c_pos = _rl_last_v_pos = 0; | |
2234 | _rl_vis_botlin = last_lmargin = 0; | |
2235 | if (vis_lbreaks) | |
2236 | vis_lbreaks[0] = vis_lbreaks[1] = 0; | |
2237 | visible_wrap_offset = 0; | |
2238 | return 0; | |
2239 | } | |
2240 | ||
775e241e TT |
2241 | /* Clear all screen lines occupied by the current readline line buffer |
2242 | (visible line) */ | |
2243 | int | |
cb41b9e7 | 2244 | rl_clear_visible_line (void) |
775e241e TT |
2245 | { |
2246 | int curr_line; | |
2247 | ||
2248 | /* Make sure we move to column 0 so we clear the entire line */ | |
2249 | #if defined (__MSDOS__) | |
2250 | putc ('\r', rl_outstream); | |
2251 | #else | |
2252 | tputs (_rl_term_cr, 1, _rl_output_character_function); | |
2253 | #endif | |
2254 | _rl_last_c_pos = 0; | |
2255 | ||
2256 | /* Move to the last screen line of the current visible line */ | |
2257 | _rl_move_vert (_rl_vis_botlin); | |
2258 | ||
2259 | /* And erase screen lines going up to line 0 (first visible line) */ | |
2260 | for (curr_line = _rl_last_v_pos; curr_line >= 0; curr_line--) | |
2261 | { | |
2262 | _rl_move_vert (curr_line); | |
2263 | _rl_clear_to_eol (0); | |
2264 | } | |
2265 | ||
2266 | return 0; | |
2267 | } | |
2268 | ||
1b17e766 EZ |
2269 | /* Tell the update routines that we have moved onto a new line with the |
2270 | prompt already displayed. Code originally from the version of readline | |
5bdf8622 | 2271 | distributed with CLISP. rl_expand_prompt must have already been called |
cb41b9e7 TT |
2272 | (explicitly or implicitly). This still doesn't work exactly right; it |
2273 | should use expand_prompt() */ | |
1b17e766 | 2274 | int |
cb41b9e7 | 2275 | rl_on_new_line_with_prompt (void) |
1b17e766 EZ |
2276 | { |
2277 | int prompt_size, i, l, real_screenwidth, newlines; | |
5bdf8622 | 2278 | char *prompt_last_line, *lprompt; |
1b17e766 EZ |
2279 | |
2280 | /* Initialize visible_line and invisible_line to ensure that they can hold | |
2281 | the already-displayed prompt. */ | |
2282 | prompt_size = strlen (rl_prompt) + 1; | |
2283 | init_line_structures (prompt_size); | |
2284 | ||
2285 | /* Make sure the line structures hold the already-displayed prompt for | |
2286 | redisplay. */ | |
5bdf8622 DJ |
2287 | lprompt = local_prompt ? local_prompt : rl_prompt; |
2288 | strcpy (visible_line, lprompt); | |
2289 | strcpy (invisible_line, lprompt); | |
1b17e766 EZ |
2290 | |
2291 | /* If the prompt contains newlines, take the last tail. */ | |
2292 | prompt_last_line = strrchr (rl_prompt, '\n'); | |
2293 | if (!prompt_last_line) | |
2294 | prompt_last_line = rl_prompt; | |
2295 | ||
2296 | l = strlen (prompt_last_line); | |
9255ee31 | 2297 | if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
cc88a640 | 2298 | _rl_last_c_pos = _rl_col_width (prompt_last_line, 0, l, 1); /* XXX */ |
9255ee31 EZ |
2299 | else |
2300 | _rl_last_c_pos = l; | |
1b17e766 EZ |
2301 | |
2302 | /* Dissect prompt_last_line into screen lines. Note that here we have | |
2303 | to use the real screenwidth. Readline's notion of screenwidth might be | |
2304 | one less, see terminal.c. */ | |
9255ee31 | 2305 | real_screenwidth = _rl_screenwidth + (_rl_term_autowrap ? 0 : 1); |
1b17e766 EZ |
2306 | _rl_last_v_pos = l / real_screenwidth; |
2307 | /* If the prompt length is a multiple of real_screenwidth, we don't know | |
2308 | whether the cursor is at the end of the last line, or already at the | |
2309 | beginning of the next line. Output a newline just to be safe. */ | |
2310 | if (l > 0 && (l % real_screenwidth) == 0) | |
2311 | _rl_output_some_chars ("\n", 1); | |
2312 | last_lmargin = 0; | |
2313 | ||
2314 | newlines = 0; i = 0; | |
2315 | while (i <= l) | |
2316 | { | |
2317 | _rl_vis_botlin = newlines; | |
2318 | vis_lbreaks[newlines++] = i; | |
2319 | i += real_screenwidth; | |
2320 | } | |
2321 | vis_lbreaks[newlines] = l; | |
2322 | visible_wrap_offset = 0; | |
2323 | ||
5bdf8622 DJ |
2324 | rl_display_prompt = rl_prompt; /* XXX - make sure it's set */ |
2325 | ||
1b17e766 EZ |
2326 | return 0; |
2327 | } | |
2328 | ||
d60d9f65 SS |
2329 | /* Actually update the display, period. */ |
2330 | int | |
cb41b9e7 | 2331 | rl_forced_update_display (void) |
d60d9f65 | 2332 | { |
cc88a640 JK |
2333 | register char *temp; |
2334 | ||
d60d9f65 SS |
2335 | if (visible_line) |
2336 | { | |
cc88a640 | 2337 | temp = visible_line; |
d60d9f65 | 2338 | while (*temp) |
c862e87b | 2339 | *temp++ = '\0'; |
d60d9f65 SS |
2340 | } |
2341 | rl_on_new_line (); | |
2342 | forced_display++; | |
2343 | (*rl_redisplay_function) (); | |
2344 | return 0; | |
2345 | } | |
2346 | ||
775e241e TT |
2347 | /* Redraw only the last line of a multi-line prompt. */ |
2348 | void | |
cb41b9e7 | 2349 | rl_redraw_prompt_last_line (void) |
775e241e TT |
2350 | { |
2351 | char *t; | |
2352 | ||
2353 | t = strrchr (rl_display_prompt, '\n'); | |
2354 | if (t) | |
2355 | redraw_prompt (++t); | |
2356 | else | |
2357 | rl_forced_update_display (); | |
2358 | } | |
2359 | ||
d60d9f65 | 2360 | /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices. |
5bdf8622 DJ |
2361 | (Well, when we don't have multibyte characters, _rl_last_c_pos is a |
2362 | buffer index.) | |
d60d9f65 | 2363 | DATA is the contents of the screen line of interest; i.e., where |
775e241e TT |
2364 | the movement is being done. |
2365 | DATA is always the visible line or the invisible line */ | |
d60d9f65 | 2366 | void |
cb41b9e7 | 2367 | _rl_move_cursor_relative (int new, const char *data) |
d60d9f65 SS |
2368 | { |
2369 | register int i; | |
5bdf8622 DJ |
2370 | int woff; /* number of invisible chars on current line */ |
2371 | int cpos, dpos; /* current and desired cursor positions */ | |
cc88a640 | 2372 | int adjust; |
775e241e TT |
2373 | int in_invisline; |
2374 | int mb_cur_max = MB_CUR_MAX; | |
d60d9f65 | 2375 | |
cc88a640 | 2376 | woff = WRAP_OFFSET (_rl_last_v_pos, wrap_offset); |
5bdf8622 | 2377 | cpos = _rl_last_c_pos; |
cc88a640 JK |
2378 | |
2379 | if (cpos == 0 && cpos == new) | |
2380 | return; | |
2381 | ||
9255ee31 EZ |
2382 | #if defined (HANDLE_MULTIBYTE) |
2383 | /* If we have multibyte characters, NEW is indexed by the buffer point in | |
2384 | a multibyte string, but _rl_last_c_pos is the display position. In | |
288381c0 | 2385 | this case, NEW's display position is not obvious and must be |
5bdf8622 DJ |
2386 | calculated. We need to account for invisible characters in this line, |
2387 | as long as we are past them and they are counted by _rl_col_width. */ | |
775e241e | 2388 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
288381c0 | 2389 | { |
cc88a640 JK |
2390 | adjust = 1; |
2391 | /* Try to short-circuit common cases and eliminate a bunch of multibyte | |
2392 | character function calls. */ | |
2393 | /* 1. prompt string */ | |
2394 | if (new == local_prompt_len && memcmp (data, local_prompt, new) == 0) | |
2395 | { | |
2396 | dpos = prompt_physical_chars; | |
2397 | cpos_adjusted = 1; | |
2398 | adjust = 0; | |
2399 | } | |
2400 | /* 2. prompt_string + line contents */ | |
2401 | else if (new > local_prompt_len && local_prompt && memcmp (data, local_prompt, local_prompt_len) == 0) | |
2402 | { | |
2403 | dpos = prompt_physical_chars + _rl_col_width (data, local_prompt_len, new, 1); | |
2404 | cpos_adjusted = 1; | |
2405 | adjust = 0; | |
2406 | } | |
2407 | else | |
2408 | dpos = _rl_col_width (data, 0, new, 1); | |
2409 | ||
775e241e TT |
2410 | if (displaying_prompt_first_line == 0) |
2411 | adjust = 0; | |
2412 | ||
2413 | /* yet another special case: printing the last line of a prompt with | |
2414 | multibyte characters and invisible characters whose printable length | |
2415 | exceeds the screen width with the last invisible character | |
2416 | (prompt_last_invisible) in the last line. IN_INVISLINE is the | |
2417 | offset of DATA in invisible_line */ | |
2418 | in_invisline = 0; | |
2419 | if (data > invisible_line && data < invisible_line+inv_lbreaks[_rl_inv_botlin+1]) | |
2420 | in_invisline = data - invisible_line; | |
2421 | ||
cc88a640 JK |
2422 | /* Use NEW when comparing against the last invisible character in the |
2423 | prompt string, since they're both buffer indices and DPOS is a | |
2424 | desired display position. */ | |
775e241e TT |
2425 | /* NEW is relative to the current displayed line, while |
2426 | PROMPT_LAST_INVISIBLE is relative to the entire (wrapped) line. | |
2427 | Need a way to reconcile these two variables by turning NEW into a | |
2428 | buffer position relative to the start of the line */ | |
cc88a640 | 2429 | if (adjust && ((new > prompt_last_invisible) || /* XXX - don't use woff here */ |
775e241e TT |
2430 | (new+in_invisline > prompt_last_invisible) || /* invisible line */ |
2431 | (prompt_physical_chars >= _rl_screenwidth && /* visible line */ | |
cc88a640 JK |
2432 | _rl_last_v_pos == prompt_last_screen_line && |
2433 | wrap_offset >= woff && dpos >= woff && | |
775e241e | 2434 | new > (prompt_last_invisible-(vis_lbreaks[_rl_last_v_pos])-wrap_offset)))) |
cc88a640 JK |
2435 | /* XXX last comparison might need to be >= */ |
2436 | { | |
2437 | dpos -= woff; | |
2438 | /* Since this will be assigned to _rl_last_c_pos at the end (more | |
2439 | precisely, _rl_last_c_pos == dpos when this function returns), | |
2440 | let the caller know. */ | |
2441 | cpos_adjusted = 1; | |
2442 | } | |
288381c0 | 2443 | } |
5bdf8622 | 2444 | else |
9255ee31 | 2445 | #endif |
5bdf8622 DJ |
2446 | dpos = new; |
2447 | ||
2448 | /* If we don't have to do anything, then return. */ | |
2449 | if (cpos == dpos) | |
2450 | return; | |
d60d9f65 SS |
2451 | |
2452 | /* It may be faster to output a CR, and then move forwards instead | |
2453 | of moving backwards. */ | |
2454 | /* i == current physical cursor position. */ | |
5bdf8622 | 2455 | #if defined (HANDLE_MULTIBYTE) |
775e241e | 2456 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
5bdf8622 DJ |
2457 | i = _rl_last_c_pos; |
2458 | else | |
2459 | #endif | |
2460 | i = _rl_last_c_pos - woff; | |
cc88a640 | 2461 | if (dpos == 0 || CR_FASTER (dpos, _rl_last_c_pos) || |
9255ee31 | 2462 | (_rl_term_autowrap && i == _rl_screenwidth)) |
d60d9f65 SS |
2463 | { |
2464 | #if defined (__MSDOS__) | |
2465 | putc ('\r', rl_outstream); | |
2466 | #else | |
9255ee31 | 2467 | tputs (_rl_term_cr, 1, _rl_output_character_function); |
d60d9f65 | 2468 | #endif /* !__MSDOS__ */ |
5bdf8622 | 2469 | cpos = _rl_last_c_pos = 0; |
d60d9f65 SS |
2470 | } |
2471 | ||
5bdf8622 | 2472 | if (cpos < dpos) |
d60d9f65 SS |
2473 | { |
2474 | /* Move the cursor forward. We do it by printing the command | |
2475 | to move the cursor forward if there is one, else print that | |
2476 | portion of the output buffer again. Which is cheaper? */ | |
2477 | ||
2478 | /* The above comment is left here for posterity. It is faster | |
2479 | to print one character (non-control) than to print a control | |
2480 | sequence telling the terminal to move forward one character. | |
2481 | That kind of control is for people who don't know what the | |
2482 | data is underneath the cursor. */ | |
cc88a640 JK |
2483 | |
2484 | /* However, we need a handle on where the current display position is | |
2485 | in the buffer for the immediately preceding comment to be true. | |
2486 | In multibyte locales, we don't currently have that info available. | |
2487 | Without it, we don't know where the data we have to display begins | |
2488 | in the buffer and we have to go back to the beginning of the screen | |
2489 | line. In this case, we can use the terminal sequence to move forward | |
2490 | if it's available. */ | |
775e241e | 2491 | if (mb_cur_max > 1 && rl_byte_oriented == 0) |
9255ee31 | 2492 | { |
cc88a640 JK |
2493 | if (_rl_term_forward_char) |
2494 | { | |
2495 | for (i = cpos; i < dpos; i++) | |
2496 | tputs (_rl_term_forward_char, 1, _rl_output_character_function); | |
2497 | } | |
2498 | else | |
2499 | { | |
2500 | tputs (_rl_term_cr, 1, _rl_output_character_function); | |
2501 | for (i = 0; i < new; i++) | |
2502 | putc (data[i], rl_outstream); | |
2503 | } | |
9255ee31 | 2504 | } |
d60d9f65 | 2505 | else |
5bdf8622 | 2506 | for (i = cpos; i < new; i++) |
d60d9f65 | 2507 | putc (data[i], rl_outstream); |
d60d9f65 | 2508 | } |
5bdf8622 | 2509 | |
9255ee31 EZ |
2510 | #if defined (HANDLE_MULTIBYTE) |
2511 | /* NEW points to the buffer point, but _rl_last_c_pos is the display point. | |
2512 | The byte length of the string is probably bigger than the column width | |
2513 | of the string, which means that if NEW == _rl_last_c_pos, then NEW's | |
2514 | display point is less than _rl_last_c_pos. */ | |
9255ee31 | 2515 | #endif |
5bdf8622 DJ |
2516 | else if (cpos > dpos) |
2517 | _rl_backspace (cpos - dpos); | |
9255ee31 | 2518 | |
5bdf8622 | 2519 | _rl_last_c_pos = dpos; |
d60d9f65 SS |
2520 | } |
2521 | ||
2522 | /* PWP: move the cursor up or down. */ | |
2523 | void | |
cb41b9e7 | 2524 | _rl_move_vert (int to) |
d60d9f65 SS |
2525 | { |
2526 | register int delta, i; | |
2527 | ||
9255ee31 | 2528 | if (_rl_last_v_pos == to || to > _rl_screenheight) |
d60d9f65 SS |
2529 | return; |
2530 | ||
d60d9f65 SS |
2531 | if ((delta = to - _rl_last_v_pos) > 0) |
2532 | { | |
2533 | for (i = 0; i < delta; i++) | |
2534 | putc ('\n', rl_outstream); | |
1b17e766 EZ |
2535 | #if defined (__MSDOS__) |
2536 | putc ('\r', rl_outstream); | |
2537 | #else | |
9255ee31 | 2538 | tputs (_rl_term_cr, 1, _rl_output_character_function); |
1b17e766 | 2539 | #endif |
d60d9f65 SS |
2540 | _rl_last_c_pos = 0; |
2541 | } | |
2542 | else | |
2543 | { /* delta < 0 */ | |
775e241e | 2544 | #ifdef __DJGPP__ |
30083a32 EZ |
2545 | int row, col; |
2546 | ||
775e241e | 2547 | fflush (rl_outstream); |
30083a32 | 2548 | ScreenGetCursor (&row, &col); |
b0f0a30e | 2549 | ScreenSetCursor (row + delta, col); |
775e241e TT |
2550 | i = -delta; |
2551 | #else | |
9255ee31 | 2552 | if (_rl_term_up && *_rl_term_up) |
d60d9f65 | 2553 | for (i = 0; i < -delta; i++) |
9255ee31 | 2554 | tputs (_rl_term_up, 1, _rl_output_character_function); |
775e241e | 2555 | #endif /* !__DJGPP__ */ |
d60d9f65 | 2556 | } |
1b17e766 | 2557 | |
d60d9f65 SS |
2558 | _rl_last_v_pos = to; /* Now TO is here */ |
2559 | } | |
2560 | ||
2561 | /* Physically print C on rl_outstream. This is for functions which know | |
2562 | how to optimize the display. Return the number of characters output. */ | |
2563 | int | |
cb41b9e7 | 2564 | rl_show_char (int c) |
d60d9f65 SS |
2565 | { |
2566 | int n = 1; | |
2567 | if (META_CHAR (c) && (_rl_output_meta_chars == 0)) | |
2568 | { | |
2569 | fprintf (rl_outstream, "M-"); | |
2570 | n += 2; | |
2571 | c = UNMETA (c); | |
2572 | } | |
2573 | ||
2574 | #if defined (DISPLAY_TABS) | |
2575 | if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT) | |
2576 | #else | |
2577 | if (CTRL_CHAR (c) || c == RUBOUT) | |
2578 | #endif /* !DISPLAY_TABS */ | |
2579 | { | |
2580 | fprintf (rl_outstream, "C-"); | |
2581 | n += 2; | |
2582 | c = CTRL_CHAR (c) ? UNCTRL (c) : '?'; | |
2583 | } | |
2584 | ||
2585 | putc (c, rl_outstream); | |
2586 | fflush (rl_outstream); | |
2587 | return n; | |
2588 | } | |
2589 | ||
2590 | int | |
cb41b9e7 | 2591 | rl_character_len (int c, int pos) |
d60d9f65 SS |
2592 | { |
2593 | unsigned char uc; | |
2594 | ||
2595 | uc = (unsigned char)c; | |
2596 | ||
2597 | if (META_CHAR (uc)) | |
2598 | return ((_rl_output_meta_chars == 0) ? 4 : 1); | |
2599 | ||
2600 | if (uc == '\t') | |
2601 | { | |
2602 | #if defined (DISPLAY_TABS) | |
2603 | return (((pos | 7) + 1) - pos); | |
2604 | #else | |
2605 | return (2); | |
2606 | #endif /* !DISPLAY_TABS */ | |
2607 | } | |
2608 | ||
2609 | if (CTRL_CHAR (c) || c == RUBOUT) | |
2610 | return (2); | |
2611 | ||
9255ee31 | 2612 | return ((ISPRINT (uc)) ? 1 : 2); |
d60d9f65 | 2613 | } |
d60d9f65 SS |
2614 | /* How to print things in the "echo-area". The prompt is treated as a |
2615 | mini-modeline. */ | |
5bdf8622 | 2616 | static int msg_saved_prompt = 0; |
d60d9f65 SS |
2617 | |
2618 | #if defined (USE_VARARGS) | |
2619 | int | |
2620 | #if defined (PREFER_STDARG) | |
2621 | rl_message (const char *format, ...) | |
2622 | #else | |
2623 | rl_message (va_alist) | |
2624 | va_dcl | |
2625 | #endif | |
2626 | { | |
2627 | va_list args; | |
2628 | #if defined (PREFER_VARARGS) | |
2629 | char *format; | |
2630 | #endif | |
775e241e TT |
2631 | #if defined (HAVE_VSNPRINTF) |
2632 | int bneed; | |
2633 | #endif | |
d60d9f65 SS |
2634 | |
2635 | #if defined (PREFER_STDARG) | |
2636 | va_start (args, format); | |
2637 | #else | |
2638 | va_start (args); | |
2639 | format = va_arg (args, char *); | |
2640 | #endif | |
2641 | ||
775e241e TT |
2642 | if (msg_buf == 0) |
2643 | msg_buf = xmalloc (msg_bufsiz = 128); | |
2644 | ||
9255ee31 | 2645 | #if defined (HAVE_VSNPRINTF) |
775e241e TT |
2646 | bneed = vsnprintf (msg_buf, msg_bufsiz, format, args); |
2647 | if (bneed >= msg_bufsiz - 1) | |
2648 | { | |
2649 | msg_bufsiz = bneed + 1; | |
2650 | msg_buf = xrealloc (msg_buf, msg_bufsiz); | |
2651 | va_end (args); | |
2652 | ||
2653 | #if defined (PREFER_STDARG) | |
2654 | va_start (args, format); | |
2655 | #else | |
2656 | va_start (args); | |
2657 | format = va_arg (args, char *); | |
2658 | #endif | |
2659 | vsnprintf (msg_buf, msg_bufsiz - 1, format, args); | |
2660 | } | |
9255ee31 | 2661 | #else |
d60d9f65 | 2662 | vsprintf (msg_buf, format, args); |
775e241e | 2663 | msg_buf[msg_bufsiz - 1] = '\0'; /* overflow? */ |
9255ee31 | 2664 | #endif |
d60d9f65 SS |
2665 | va_end (args); |
2666 | ||
5bdf8622 DJ |
2667 | if (saved_local_prompt == 0) |
2668 | { | |
2669 | rl_save_prompt (); | |
2670 | msg_saved_prompt = 1; | |
2671 | } | |
775e241e TT |
2672 | else if (local_prompt != saved_local_prompt) |
2673 | { | |
2674 | FREE (local_prompt); | |
2675 | FREE (local_prompt_prefix); | |
2676 | local_prompt = (char *)NULL; | |
2677 | } | |
d60d9f65 | 2678 | rl_display_prompt = msg_buf; |
775e241e TT |
2679 | local_prompt = expand_prompt (msg_buf, 0, &prompt_visible_length, |
2680 | &prompt_last_invisible, | |
2681 | &prompt_invis_chars_first_line, | |
2682 | &prompt_physical_chars); | |
5bdf8622 | 2683 | local_prompt_prefix = (char *)NULL; |
cc88a640 | 2684 | local_prompt_len = local_prompt ? strlen (local_prompt) : 0; |
d60d9f65 | 2685 | (*rl_redisplay_function) (); |
5bdf8622 | 2686 | |
d60d9f65 SS |
2687 | return 0; |
2688 | } | |
2689 | #else /* !USE_VARARGS */ | |
2690 | int | |
2691 | rl_message (format, arg1, arg2) | |
2692 | char *format; | |
2693 | { | |
775e241e TT |
2694 | if (msg_buf == 0) |
2695 | msg_buf = xmalloc (msg_bufsiz = 128); | |
2696 | ||
d60d9f65 | 2697 | sprintf (msg_buf, format, arg1, arg2); |
775e241e | 2698 | msg_buf[msg_bufsiz - 1] = '\0'; /* overflow? */ |
5bdf8622 | 2699 | |
d60d9f65 | 2700 | rl_display_prompt = msg_buf; |
5bdf8622 DJ |
2701 | if (saved_local_prompt == 0) |
2702 | { | |
2703 | rl_save_prompt (); | |
2704 | msg_saved_prompt = 1; | |
2705 | } | |
775e241e TT |
2706 | else if (local_prompt != saved_local_prompt) |
2707 | { | |
2708 | FREE (local_prompt); | |
2709 | FREE (local_prompt_prefix); | |
2710 | local_prompt = (char *)NULL; | |
2711 | } | |
2712 | local_prompt = expand_prompt (msg_buf, 0, &prompt_visible_length, | |
2713 | &prompt_last_invisible, | |
2714 | &prompt_invis_chars_first_line, | |
2715 | &prompt_physical_chars); | |
5bdf8622 | 2716 | local_prompt_prefix = (char *)NULL; |
cc88a640 | 2717 | local_prompt_len = local_prompt ? strlen (local_prompt) : 0; |
d60d9f65 | 2718 | (*rl_redisplay_function) (); |
5bdf8622 | 2719 | |
d60d9f65 SS |
2720 | return 0; |
2721 | } | |
2722 | #endif /* !USE_VARARGS */ | |
2723 | ||
2724 | /* How to clear things from the "echo-area". */ | |
2725 | int | |
cb41b9e7 | 2726 | rl_clear_message (void) |
d60d9f65 SS |
2727 | { |
2728 | rl_display_prompt = rl_prompt; | |
5bdf8622 DJ |
2729 | if (msg_saved_prompt) |
2730 | { | |
2731 | rl_restore_prompt (); | |
2732 | msg_saved_prompt = 0; | |
2733 | } | |
d60d9f65 SS |
2734 | (*rl_redisplay_function) (); |
2735 | return 0; | |
2736 | } | |
2737 | ||
2738 | int | |
cb41b9e7 | 2739 | rl_reset_line_state (void) |
d60d9f65 SS |
2740 | { |
2741 | rl_on_new_line (); | |
2742 | ||
2743 | rl_display_prompt = rl_prompt ? rl_prompt : ""; | |
2744 | forced_display = 1; | |
2745 | return 0; | |
2746 | } | |
2747 | ||
cb41b9e7 TT |
2748 | /* Save all of the variables associated with the prompt and its display. Most |
2749 | of the complexity is dealing with the invisible characters in the prompt | |
2750 | string and where they are. There are enough of these that I should consider | |
2751 | a struct. */ | |
d60d9f65 | 2752 | void |
cb41b9e7 | 2753 | rl_save_prompt (void) |
d60d9f65 SS |
2754 | { |
2755 | saved_local_prompt = local_prompt; | |
2756 | saved_local_prefix = local_prompt_prefix; | |
5bdf8622 | 2757 | saved_prefix_length = prompt_prefix_length; |
cc88a640 | 2758 | saved_local_length = local_prompt_len; |
9255ee31 EZ |
2759 | saved_last_invisible = prompt_last_invisible; |
2760 | saved_visible_length = prompt_visible_length; | |
5bdf8622 DJ |
2761 | saved_invis_chars_first_line = prompt_invis_chars_first_line; |
2762 | saved_physical_chars = prompt_physical_chars; | |
cb41b9e7 | 2763 | saved_local_prompt_newlines = local_prompt_newlines; |
d60d9f65 SS |
2764 | |
2765 | local_prompt = local_prompt_prefix = (char *)0; | |
cc88a640 | 2766 | local_prompt_len = 0; |
cb41b9e7 TT |
2767 | local_prompt_newlines = (int *)0; |
2768 | ||
5bdf8622 DJ |
2769 | prompt_last_invisible = prompt_visible_length = prompt_prefix_length = 0; |
2770 | prompt_invis_chars_first_line = prompt_physical_chars = 0; | |
d60d9f65 SS |
2771 | } |
2772 | ||
2773 | void | |
cb41b9e7 | 2774 | rl_restore_prompt (void) |
d60d9f65 | 2775 | { |
9255ee31 EZ |
2776 | FREE (local_prompt); |
2777 | FREE (local_prompt_prefix); | |
cb41b9e7 | 2778 | FREE (local_prompt_newlines); |
d60d9f65 SS |
2779 | |
2780 | local_prompt = saved_local_prompt; | |
2781 | local_prompt_prefix = saved_local_prefix; | |
cc88a640 | 2782 | local_prompt_len = saved_local_length; |
cb41b9e7 TT |
2783 | local_prompt_newlines = saved_local_prompt_newlines; |
2784 | ||
5bdf8622 | 2785 | prompt_prefix_length = saved_prefix_length; |
9255ee31 EZ |
2786 | prompt_last_invisible = saved_last_invisible; |
2787 | prompt_visible_length = saved_visible_length; | |
5bdf8622 DJ |
2788 | prompt_invis_chars_first_line = saved_invis_chars_first_line; |
2789 | prompt_physical_chars = saved_physical_chars; | |
2790 | ||
2791 | /* can test saved_local_prompt to see if prompt info has been saved. */ | |
2792 | saved_local_prompt = saved_local_prefix = (char *)0; | |
cc88a640 | 2793 | saved_local_length = 0; |
5bdf8622 DJ |
2794 | saved_last_invisible = saved_visible_length = saved_prefix_length = 0; |
2795 | saved_invis_chars_first_line = saved_physical_chars = 0; | |
cb41b9e7 | 2796 | saved_local_prompt_newlines = 0; |
d60d9f65 SS |
2797 | } |
2798 | ||
2799 | char * | |
cb41b9e7 | 2800 | _rl_make_prompt_for_search (int pchar) |
d60d9f65 SS |
2801 | { |
2802 | int len; | |
5bdf8622 | 2803 | char *pmt, *p; |
d60d9f65 | 2804 | |
c862e87b | 2805 | rl_save_prompt (); |
d60d9f65 | 2806 | |
5bdf8622 DJ |
2807 | /* We've saved the prompt, and can do anything with the various prompt |
2808 | strings we need before they're restored. We want the unexpanded | |
2809 | portion of the prompt string after any final newline. */ | |
2810 | p = rl_prompt ? strrchr (rl_prompt, '\n') : 0; | |
2811 | if (p == 0) | |
d60d9f65 SS |
2812 | { |
2813 | len = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0; | |
9255ee31 | 2814 | pmt = (char *)xmalloc (len + 2); |
d60d9f65 | 2815 | if (len) |
c862e87b | 2816 | strcpy (pmt, rl_prompt); |
d60d9f65 SS |
2817 | pmt[len] = pchar; |
2818 | pmt[len+1] = '\0'; | |
2819 | } | |
2820 | else | |
2821 | { | |
5bdf8622 DJ |
2822 | p++; |
2823 | len = strlen (p); | |
9255ee31 | 2824 | pmt = (char *)xmalloc (len + 2); |
d60d9f65 | 2825 | if (len) |
5bdf8622 | 2826 | strcpy (pmt, p); |
d60d9f65 SS |
2827 | pmt[len] = pchar; |
2828 | pmt[len+1] = '\0'; | |
5bdf8622 DJ |
2829 | } |
2830 | ||
2831 | /* will be overwritten by expand_prompt, called from rl_message */ | |
2832 | prompt_physical_chars = saved_physical_chars + 1; | |
d60d9f65 SS |
2833 | return pmt; |
2834 | } | |
2835 | ||
2836 | /* Quick redisplay hack when erasing characters at the end of the line. */ | |
2837 | void | |
cb41b9e7 | 2838 | _rl_erase_at_end_of_line (int l) |
d60d9f65 SS |
2839 | { |
2840 | register int i; | |
2841 | ||
2842 | _rl_backspace (l); | |
2843 | for (i = 0; i < l; i++) | |
2844 | putc (' ', rl_outstream); | |
2845 | _rl_backspace (l); | |
2846 | for (i = 0; i < l; i++) | |
2847 | visible_line[--_rl_last_c_pos] = '\0'; | |
2848 | rl_display_fixed++; | |
2849 | } | |
2850 | ||
2851 | /* Clear to the end of the line. COUNT is the minimum | |
cb41b9e7 TT |
2852 | number of character spaces to clear, but we use a terminal escape |
2853 | sequence if available. */ | |
d60d9f65 | 2854 | void |
cb41b9e7 | 2855 | _rl_clear_to_eol (int count) |
d60d9f65 | 2856 | { |
30083a32 | 2857 | #ifndef __MSDOS__ |
9255ee31 EZ |
2858 | if (_rl_term_clreol) |
2859 | tputs (_rl_term_clreol, 1, _rl_output_character_function); | |
30083a32 EZ |
2860 | else |
2861 | #endif | |
775e241e TT |
2862 | if (count) |
2863 | space_to_eol (count); | |
d60d9f65 SS |
2864 | } |
2865 | ||
2866 | /* Clear to the end of the line using spaces. COUNT is the minimum | |
2867 | number of character spaces to clear, */ | |
2868 | static void | |
cb41b9e7 | 2869 | space_to_eol (int count) |
d60d9f65 SS |
2870 | { |
2871 | register int i; | |
2872 | ||
2873 | for (i = 0; i < count; i++) | |
cb41b9e7 | 2874 | putc (' ', rl_outstream); |
d60d9f65 SS |
2875 | |
2876 | _rl_last_c_pos += count; | |
2877 | } | |
2878 | ||
2879 | void | |
cb41b9e7 | 2880 | _rl_clear_screen (void) |
d60d9f65 | 2881 | { |
cb41b9e7 TT |
2882 | #if defined (__DJGPP__) |
2883 | ScreenClear (); | |
2884 | ScreenSetCursor (0, 0); | |
2885 | #else | |
9255ee31 EZ |
2886 | if (_rl_term_clrpag) |
2887 | tputs (_rl_term_clrpag, 1, _rl_output_character_function); | |
d60d9f65 | 2888 | else |
9255ee31 | 2889 | rl_crlf (); |
775e241e | 2890 | #endif /* __DJGPP__ */ |
d60d9f65 SS |
2891 | } |
2892 | ||
9255ee31 | 2893 | /* Insert COUNT characters from STRING to the output stream at column COL. */ |
d60d9f65 | 2894 | static void |
cb41b9e7 | 2895 | insert_some_chars (char *string, int count, int col) |
d60d9f65 | 2896 | { |
775e241e | 2897 | open_some_spaces (col); |
30083a32 | 2898 | _rl_output_some_chars (string, count); |
775e241e TT |
2899 | } |
2900 | ||
2901 | /* Insert COL spaces, keeping the cursor at the same position. We follow the | |
2902 | ncurses documentation and use either im/ei with explicit spaces, or IC/ic | |
2903 | by itself. We assume there will either be ei or we don't need to use it. */ | |
2904 | static void | |
cb41b9e7 | 2905 | open_some_spaces (int col) |
775e241e TT |
2906 | { |
2907 | #if !defined (__MSDOS__) && (!defined (__MINGW32__) || defined (NCURSES_VERSION)) | |
2908 | char *buffer; | |
2909 | register int i; | |
9255ee31 | 2910 | |
d60d9f65 | 2911 | /* If IC is defined, then we do not have to "enter" insert mode. */ |
9255ee31 | 2912 | if (_rl_term_IC) |
d60d9f65 | 2913 | { |
9255ee31 | 2914 | buffer = tgoto (_rl_term_IC, 0, col); |
d60d9f65 | 2915 | tputs (buffer, 1, _rl_output_character_function); |
d60d9f65 | 2916 | } |
775e241e | 2917 | else if (_rl_term_im && *_rl_term_im) |
4a11f206 | 2918 | { |
775e241e TT |
2919 | tputs (_rl_term_im, 1, _rl_output_character_function); |
2920 | /* just output the desired number of spaces */ | |
2921 | for (i = col; i--; ) | |
2922 | _rl_output_character_function (' '); | |
2923 | /* If there is a string to turn off insert mode, use it now. */ | |
5836a818 PP |
2924 | if (_rl_term_ei && *_rl_term_ei) |
2925 | tputs (_rl_term_ei, 1, _rl_output_character_function); | |
775e241e TT |
2926 | /* and move back the right number of spaces */ |
2927 | _rl_backspace (col); | |
4a11f206 | 2928 | } |
775e241e TT |
2929 | else if (_rl_term_ic && *_rl_term_ic) |
2930 | { | |
2931 | /* If there is a special command for inserting characters, then | |
2932 | use that first to open up the space. */ | |
2933 | for (i = col; i--; ) | |
2934 | tputs (_rl_term_ic, 1, _rl_output_character_function); | |
2935 | } | |
2936 | #endif /* !__MSDOS__ && (!__MINGW32__ || NCURSES_VERSION)*/ | |
d60d9f65 SS |
2937 | } |
2938 | ||
2939 | /* Delete COUNT characters from the display line. */ | |
2940 | static void | |
cb41b9e7 | 2941 | delete_chars (int count) |
d60d9f65 | 2942 | { |
9255ee31 | 2943 | if (count > _rl_screenwidth) /* XXX */ |
d60d9f65 SS |
2944 | return; |
2945 | ||
775e241e | 2946 | #if !defined (__MSDOS__) && (!defined (__MINGW32__) || defined (NCURSES_VERSION)) |
9255ee31 | 2947 | if (_rl_term_DC && *_rl_term_DC) |
d60d9f65 SS |
2948 | { |
2949 | char *buffer; | |
9255ee31 | 2950 | buffer = tgoto (_rl_term_DC, count, count); |
d60d9f65 SS |
2951 | tputs (buffer, count, _rl_output_character_function); |
2952 | } | |
2953 | else | |
2954 | { | |
9255ee31 | 2955 | if (_rl_term_dc && *_rl_term_dc) |
d60d9f65 | 2956 | while (count--) |
9255ee31 | 2957 | tputs (_rl_term_dc, 1, _rl_output_character_function); |
d60d9f65 | 2958 | } |
775e241e | 2959 | #endif /* !__MSDOS__ && (!__MINGW32__ || NCURSES_VERSION)*/ |
d60d9f65 SS |
2960 | } |
2961 | ||
2962 | void | |
cb41b9e7 | 2963 | _rl_update_final (void) |
d60d9f65 | 2964 | { |
cb41b9e7 | 2965 | int full_lines, woff, botline_length; |
d60d9f65 SS |
2966 | |
2967 | full_lines = 0; | |
2968 | /* If the cursor is the only thing on an otherwise-blank last line, | |
2969 | compensate so we don't print an extra CRLF. */ | |
2970 | if (_rl_vis_botlin && _rl_last_c_pos == 0 && | |
2971 | visible_line[vis_lbreaks[_rl_vis_botlin]] == 0) | |
2972 | { | |
2973 | _rl_vis_botlin--; | |
2974 | full_lines = 1; | |
2975 | } | |
2976 | _rl_move_vert (_rl_vis_botlin); | |
cb41b9e7 TT |
2977 | woff = W_OFFSET(_rl_vis_botlin, wrap_offset); |
2978 | botline_length = VIS_LLEN(_rl_vis_botlin) - woff; | |
d60d9f65 | 2979 | /* If we've wrapped lines, remove the final xterm line-wrap flag. */ |
cb41b9e7 | 2980 | if (full_lines && _rl_term_autowrap && botline_length == _rl_screenwidth) |
d60d9f65 SS |
2981 | { |
2982 | char *last_line; | |
9255ee31 | 2983 | |
cb41b9e7 TT |
2984 | /* LAST_LINE includes invisible characters, so if you want to get the |
2985 | last character of the first line, you have to take WOFF into account. | |
2986 | This needs to be done for both calls to _rl_move_cursor_relative, | |
2987 | which takes a buffer position as the first argument, and any direct | |
2988 | subscripts of LAST_LINE. */ | |
2989 | last_line = &visible_line[vis_lbreaks[_rl_vis_botlin]]; /* = VIS_CHARS(_rl_vis_botlin); */ | |
cc88a640 | 2990 | cpos_buffer_position = -1; /* don't know where we are in buffer */ |
cb41b9e7 | 2991 | _rl_move_cursor_relative (_rl_screenwidth - 1 + woff, last_line); /* XXX */ |
d60d9f65 | 2992 | _rl_clear_to_eol (0); |
cb41b9e7 | 2993 | putc (last_line[_rl_screenwidth - 1 + woff], rl_outstream); |
d60d9f65 SS |
2994 | } |
2995 | _rl_vis_botlin = 0; | |
cb41b9e7 TT |
2996 | if (botline_length > 0 || _rl_last_c_pos > 0) |
2997 | rl_crlf (); | |
d60d9f65 SS |
2998 | fflush (rl_outstream); |
2999 | rl_display_fixed++; | |
3000 | } | |
3001 | ||
3002 | /* Move to the start of the current line. */ | |
3003 | static void | |
cb41b9e7 | 3004 | cr (void) |
d60d9f65 | 3005 | { |
9255ee31 | 3006 | if (_rl_term_cr) |
d60d9f65 | 3007 | { |
771578d1 SS |
3008 | #if defined (__MSDOS__) |
3009 | putc ('\r', rl_outstream); | |
3010 | #else | |
9255ee31 | 3011 | tputs (_rl_term_cr, 1, _rl_output_character_function); |
1b17e766 | 3012 | #endif |
d60d9f65 SS |
3013 | _rl_last_c_pos = 0; |
3014 | } | |
3015 | } | |
3016 | ||
1b17e766 EZ |
3017 | /* Redraw the last line of a multi-line prompt that may possibly contain |
3018 | terminal escape sequences. Called with the cursor at column 0 of the | |
3019 | line to draw the prompt on. */ | |
3020 | static void | |
cb41b9e7 | 3021 | redraw_prompt (char *t) |
1b17e766 | 3022 | { |
5bdf8622 | 3023 | char *oldp; |
1b17e766 | 3024 | |
1b17e766 | 3025 | oldp = rl_display_prompt; |
5bdf8622 | 3026 | rl_save_prompt (); |
1b17e766 EZ |
3027 | |
3028 | rl_display_prompt = t; | |
775e241e TT |
3029 | local_prompt = expand_prompt (t, PMT_MULTILINE, |
3030 | &prompt_visible_length, | |
9255ee31 | 3031 | &prompt_last_invisible, |
5bdf8622 DJ |
3032 | &prompt_invis_chars_first_line, |
3033 | &prompt_physical_chars); | |
1b17e766 | 3034 | local_prompt_prefix = (char *)NULL; |
cc88a640 | 3035 | local_prompt_len = local_prompt ? strlen (local_prompt) : 0; |
5bdf8622 | 3036 | |
1b17e766 EZ |
3037 | rl_forced_update_display (); |
3038 | ||
3039 | rl_display_prompt = oldp; | |
5bdf8622 | 3040 | rl_restore_prompt(); |
1b17e766 EZ |
3041 | } |
3042 | ||
d60d9f65 SS |
3043 | /* Redisplay the current line after a SIGWINCH is received. */ |
3044 | void | |
cb41b9e7 | 3045 | _rl_redisplay_after_sigwinch (void) |
d60d9f65 | 3046 | { |
1b17e766 | 3047 | char *t; |
d60d9f65 | 3048 | |
cc88a640 JK |
3049 | /* Clear the last line (assuming that the screen size change will result in |
3050 | either more or fewer characters on that line only) and put the cursor at | |
3051 | column 0. Make sure the right thing happens if we have wrapped to a new | |
3052 | screen line. */ | |
9255ee31 | 3053 | if (_rl_term_cr) |
d60d9f65 | 3054 | { |
cc88a640 JK |
3055 | _rl_move_vert (_rl_vis_botlin); |
3056 | ||
771578d1 SS |
3057 | #if defined (__MSDOS__) |
3058 | putc ('\r', rl_outstream); | |
3059 | #else | |
9255ee31 | 3060 | tputs (_rl_term_cr, 1, _rl_output_character_function); |
1b17e766 | 3061 | #endif |
d60d9f65 | 3062 | _rl_last_c_pos = 0; |
771578d1 | 3063 | #if defined (__MSDOS__) |
9255ee31 | 3064 | space_to_eol (_rl_screenwidth); |
771578d1 SS |
3065 | putc ('\r', rl_outstream); |
3066 | #else | |
9255ee31 EZ |
3067 | if (_rl_term_clreol) |
3068 | tputs (_rl_term_clreol, 1, _rl_output_character_function); | |
d60d9f65 SS |
3069 | else |
3070 | { | |
9255ee31 EZ |
3071 | space_to_eol (_rl_screenwidth); |
3072 | tputs (_rl_term_cr, 1, _rl_output_character_function); | |
d60d9f65 | 3073 | } |
771578d1 | 3074 | #endif |
d60d9f65 SS |
3075 | if (_rl_last_v_pos > 0) |
3076 | _rl_move_vert (0); | |
3077 | } | |
3078 | else | |
9255ee31 | 3079 | rl_crlf (); |
d60d9f65 SS |
3080 | |
3081 | /* Redraw only the last line of a multi-line prompt. */ | |
3082 | t = strrchr (rl_display_prompt, '\n'); | |
3083 | if (t) | |
1b17e766 | 3084 | redraw_prompt (++t); |
d60d9f65 SS |
3085 | else |
3086 | rl_forced_update_display (); | |
3087 | } | |
3088 | ||
3089 | void | |
cb41b9e7 | 3090 | _rl_clean_up_for_exit (void) |
d60d9f65 | 3091 | { |
cc88a640 | 3092 | if (_rl_echoing_p) |
d60d9f65 | 3093 | { |
775e241e TT |
3094 | if (_rl_vis_botlin > 0) /* minor optimization plus bug fix */ |
3095 | _rl_move_vert (_rl_vis_botlin); | |
d60d9f65 SS |
3096 | _rl_vis_botlin = 0; |
3097 | fflush (rl_outstream); | |
c862e87b | 3098 | rl_restart_output (1, 0); |
d60d9f65 SS |
3099 | } |
3100 | } | |
c862e87b JM |
3101 | |
3102 | void | |
cb41b9e7 | 3103 | _rl_erase_entire_line (void) |
c862e87b JM |
3104 | { |
3105 | cr (); | |
3106 | _rl_clear_to_eol (0); | |
3107 | cr (); | |
3108 | fflush (rl_outstream); | |
3109 | } | |
1b17e766 | 3110 | |
775e241e | 3111 | void |
cb41b9e7 | 3112 | _rl_ttyflush (void) |
775e241e TT |
3113 | { |
3114 | fflush (rl_outstream); | |
3115 | } | |
3116 | ||
1b17e766 EZ |
3117 | /* return the `current display line' of the cursor -- the number of lines to |
3118 | move up to get to the first screen line of the current readline line. */ | |
3119 | int | |
cb41b9e7 | 3120 | _rl_current_display_line (void) |
1b17e766 EZ |
3121 | { |
3122 | int ret, nleft; | |
3123 | ||
3124 | /* Find out whether or not there might be invisible characters in the | |
3125 | editing buffer. */ | |
3126 | if (rl_display_prompt == rl_prompt) | |
9255ee31 | 3127 | nleft = _rl_last_c_pos - _rl_screenwidth - rl_visible_prompt_length; |
1b17e766 | 3128 | else |
9255ee31 | 3129 | nleft = _rl_last_c_pos - _rl_screenwidth; |
1b17e766 EZ |
3130 | |
3131 | if (nleft > 0) | |
9255ee31 | 3132 | ret = 1 + nleft / _rl_screenwidth; |
1b17e766 EZ |
3133 | else |
3134 | ret = 0; | |
3135 | ||
3136 | return ret; | |
3137 | } | |
9255ee31 EZ |
3138 | |
3139 | #if defined (HANDLE_MULTIBYTE) | |
3140 | /* Calculate the number of screen columns occupied by STR from START to END. | |
3141 | In the case of multibyte characters with stateful encoding, we have to | |
3142 | scan from the beginning of the string to take the state into account. */ | |
3143 | static int | |
cb41b9e7 | 3144 | _rl_col_width (const char *str, int start, int end, int flags) |
9255ee31 EZ |
3145 | { |
3146 | wchar_t wc; | |
cc88a640 | 3147 | mbstate_t ps; |
9255ee31 EZ |
3148 | int tmp, point, width, max; |
3149 | ||
3150 | if (end <= start) | |
3151 | return 0; | |
cc88a640 | 3152 | if (MB_CUR_MAX == 1 || rl_byte_oriented) |
775e241e | 3153 | /* this can happen in some cases where it's inconvenient to check */ |
cc88a640 | 3154 | return (end - start); |
cc88a640 JK |
3155 | |
3156 | memset (&ps, 0, sizeof (mbstate_t)); | |
9255ee31 EZ |
3157 | |
3158 | point = 0; | |
3159 | max = end; | |
3160 | ||
cc88a640 JK |
3161 | /* Try to short-circuit common cases. The adjustment to remove wrap_offset |
3162 | is done by the caller. */ | |
3163 | /* 1. prompt string */ | |
3164 | if (flags && start == 0 && end == local_prompt_len && memcmp (str, local_prompt, local_prompt_len) == 0) | |
3165 | return (prompt_physical_chars + wrap_offset); | |
3166 | /* 2. prompt string + line contents */ | |
3167 | else if (flags && start == 0 && local_prompt_len > 0 && end > local_prompt_len && local_prompt && memcmp (str, local_prompt, local_prompt_len) == 0) | |
3168 | { | |
3169 | tmp = prompt_physical_chars + wrap_offset; | |
3170 | /* XXX - try to call ourselves recursively with non-prompt portion */ | |
3171 | tmp += _rl_col_width (str, local_prompt_len, end, flags); | |
3172 | return (tmp); | |
3173 | } | |
3174 | ||
9255ee31 EZ |
3175 | while (point < start) |
3176 | { | |
cb41b9e7 TT |
3177 | if (_rl_utf8locale && UTF8_SINGLEBYTE(str[point])) |
3178 | { | |
3179 | memset (&ps, 0, sizeof (mbstate_t)); | |
3180 | tmp = 1; | |
3181 | } | |
3182 | else | |
3183 | tmp = mbrlen (str + point, max, &ps); | |
5bdf8622 | 3184 | if (MB_INVALIDCH ((size_t)tmp)) |
9255ee31 EZ |
3185 | { |
3186 | /* In this case, the bytes are invalid or too short to compose a | |
3187 | multibyte character, so we assume that the first byte represents | |
3188 | a single character. */ | |
3189 | point++; | |
3190 | max--; | |
3191 | ||
3192 | /* Clear the state of the byte sequence, because in this case the | |
3193 | effect of mbstate is undefined. */ | |
3194 | memset (&ps, 0, sizeof (mbstate_t)); | |
3195 | } | |
5bdf8622 DJ |
3196 | else if (MB_NULLWCH (tmp)) |
3197 | break; /* Found '\0' */ | |
9255ee31 EZ |
3198 | else |
3199 | { | |
3200 | point += tmp; | |
3201 | max -= tmp; | |
3202 | } | |
3203 | } | |
3204 | ||
3205 | /* If START is not a byte that starts a character, then POINT will be | |
3206 | greater than START. In this case, assume that (POINT - START) gives | |
3207 | a byte count that is the number of columns of difference. */ | |
3208 | width = point - start; | |
3209 | ||
3210 | while (point < end) | |
3211 | { | |
cb41b9e7 TT |
3212 | if (_rl_utf8locale && UTF8_SINGLEBYTE(str[point])) |
3213 | { | |
3214 | tmp = 1; | |
3215 | wc = (wchar_t) str[point]; | |
3216 | } | |
3217 | else | |
3218 | tmp = mbrtowc (&wc, str + point, max, &ps); | |
5bdf8622 | 3219 | if (MB_INVALIDCH ((size_t)tmp)) |
9255ee31 EZ |
3220 | { |
3221 | /* In this case, the bytes are invalid or too short to compose a | |
3222 | multibyte character, so we assume that the first byte represents | |
3223 | a single character. */ | |
3224 | point++; | |
3225 | max--; | |
3226 | ||
3227 | /* and assume that the byte occupies a single column. */ | |
3228 | width++; | |
3229 | ||
3230 | /* Clear the state of the byte sequence, because in this case the | |
3231 | effect of mbstate is undefined. */ | |
3232 | memset (&ps, 0, sizeof (mbstate_t)); | |
3233 | } | |
5bdf8622 DJ |
3234 | else if (MB_NULLWCH (tmp)) |
3235 | break; /* Found '\0' */ | |
9255ee31 EZ |
3236 | else |
3237 | { | |
3238 | point += tmp; | |
3239 | max -= tmp; | |
775e241e | 3240 | tmp = WCWIDTH(wc); |
9255ee31 EZ |
3241 | width += (tmp >= 0) ? tmp : 1; |
3242 | } | |
3243 | } | |
3244 | ||
3245 | width += point - end; | |
3246 | ||
3247 | return width; | |
3248 | } | |
3249 | #endif /* HANDLE_MULTIBYTE */ |