| 1 |
/* display.c -- readline redisplay facility. */ |
| 2 |
|
| 3 |
/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of the GNU Readline Library, a library for |
| 6 |
reading lines of text with interactive input and history editing. |
| 7 |
|
| 8 |
The GNU Readline Library is free software; you can redistribute it |
| 9 |
and/or modify it under the terms of the GNU General Public License |
| 10 |
as published by the Free Software Foundation; either version 1, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
The GNU Readline Library is distributed in the hope that it will be |
| 14 |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 15 |
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
The GNU General Public License is often shipped with GNU software, and |
| 19 |
is generally kept in a file called COPYING or LICENSE. If you do not |
| 20 |
have a copy of the license, write to the Free Software Foundation, |
| 21 |
675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 22 |
|
| 23 |
|
| 24 |
/* |
| 25 |
.VERSION |
| 26 |
051014 last modif |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
#define READLINE_LIBRARY |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <sys/types.h> |
| 34 |
|
| 35 |
#if defined (HAVE_UNISTD_H) |
| 36 |
# include <unistd.h> |
| 37 |
#endif /* HAVE_UNISTD_H */ |
| 38 |
|
| 39 |
#if defined (HAVE_STDLIB_H) |
| 40 |
# include <stdlib.h> |
| 41 |
#else |
| 42 |
# include "ansi_stdlib.h" |
| 43 |
#endif /* HAVE_STDLIB_H */ |
| 44 |
|
| 45 |
#include "posixstat.h" |
| 46 |
|
| 47 |
/* System-specific feature definitions and include files. */ |
| 48 |
#include "rldefs.h" |
| 49 |
|
| 50 |
/* Some standard library routines. */ |
| 51 |
#include "readline.h" |
| 52 |
#include "history.h" |
| 53 |
|
| 54 |
#if !defined (strchr) && !defined (__STDC__) |
| 55 |
extern char *strchr (), *strrchr (); |
| 56 |
#endif /* !strchr && !__STDC__ */ |
| 57 |
|
| 58 |
/* Global and pseudo-global variables and functions |
| 59 |
imported from readline.c. */ |
| 60 |
extern char *rl_prompt; |
| 61 |
extern int readline_echoing_p; |
| 62 |
extern char *term_clreol, *term_im, *term_ic, *term_ei, *term_DC; |
| 63 |
/* Termcap variables. */ |
| 64 |
extern char *term_up, *term_dc, *term_cr, *term_IC; |
| 65 |
extern int screenheight, screenwidth, screenchars; |
| 66 |
extern int terminal_can_insert; |
| 67 |
|
| 68 |
extern void _rl_output_some_chars (); |
| 69 |
extern int _rl_output_character_function (); |
| 70 |
|
| 71 |
extern int _rl_output_meta_chars; |
| 72 |
extern int _rl_horizontal_scroll_mode; |
| 73 |
extern int _rl_mark_modified_lines; |
| 74 |
extern int _rl_prefer_visible_bell; |
| 75 |
|
| 76 |
/* Pseudo-global functions (local to the readline library) exported |
| 77 |
by this file. */ |
| 78 |
void _rl_move_cursor_relative (), _rl_output_some_chars (); |
| 79 |
void _rl_move_vert (); |
| 80 |
|
| 81 |
static void update_line (), clear_to_eol (); |
| 82 |
static void delete_chars (), insert_some_chars (); |
| 83 |
|
| 84 |
extern char *xmalloc (), *xrealloc (); |
| 85 |
|
| 86 |
/* Heuristic used to decide whether it is faster to move from CUR to NEW |
| 87 |
by backing up or outputting a carriage return and moving forward. */ |
| 88 |
#define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new))) |
| 89 |
|
| 90 |
/* **************************************************************** */ |
| 91 |
/* */ |
| 92 |
/* Display stuff */ |
| 93 |
/* */ |
| 94 |
/* **************************************************************** */ |
| 95 |
|
| 96 |
/* This is the stuff that is hard for me. I never seem to write good |
| 97 |
display routines in C. Let's see how I do this time. */ |
| 98 |
|
| 99 |
/* (PWP) Well... Good for a simple line updater, but totally ignores |
| 100 |
the problems of input lines longer than the screen width. |
| 101 |
|
| 102 |
update_line and the code that calls it makes a multiple line, |
| 103 |
automatically wrapping line update. Carefull attention needs |
| 104 |
to be paid to the vertical position variables. |
| 105 |
|
| 106 |
handling of terminals with autowrap on (incl. DEC braindamage) |
| 107 |
could be improved a bit. Right now I just cheat and decrement |
| 108 |
screenwidth by one. */ |
| 109 |
|
| 110 |
/* Keep two buffers; one which reflects the current contents of the |
| 111 |
screen, and the other to draw what we think the new contents should |
| 112 |
be. Then compare the buffers, and make whatever changes to the |
| 113 |
screen itself that we should. Finally, make the buffer that we |
| 114 |
just drew into be the one which reflects the current contents of the |
| 115 |
screen, and place the cursor where it belongs. |
| 116 |
|
| 117 |
Commands that want to can fix the display themselves, and then let |
| 118 |
this function know that the display has been fixed by setting the |
| 119 |
RL_DISPLAY_FIXED variable. This is good for efficiency. */ |
| 120 |
|
| 121 |
/* Global variables declared here. */ |
| 122 |
/* What YOU turn on when you have handled all redisplay yourself. */ |
| 123 |
int rl_display_fixed = 0; |
| 124 |
|
| 125 |
/* The stuff that gets printed out before the actual text of the line. |
| 126 |
This is usually pointing to rl_prompt. */ |
| 127 |
char *rl_display_prompt = (char *)NULL; |
| 128 |
|
| 129 |
/* Pseudo-global variables declared here. */ |
| 130 |
/* The visible cursor position. If you print some text, adjust this. */ |
| 131 |
int _rl_last_c_pos = 0; |
| 132 |
int _rl_last_v_pos = 0; |
| 133 |
|
| 134 |
/* Number of lines currently on screen minus 1. */ |
| 135 |
int _rl_vis_botlin = 0; |
| 136 |
|
| 137 |
/* Variables used only in this file. */ |
| 138 |
/* The last left edge of text that was displayed. This is used when |
| 139 |
doing horizontal scrolling. It shifts in thirds of a screenwidth. */ |
| 140 |
static int last_lmargin = 0; |
| 141 |
|
| 142 |
/* The line display buffers. One is the line currently displayed on |
| 143 |
the screen. The other is the line about to be displayed. */ |
| 144 |
static char *visible_line = (char *)NULL; |
| 145 |
static char *invisible_line = (char *)NULL; |
| 146 |
|
| 147 |
/* A buffer for `modeline' messages. */ |
| 148 |
static char msg_buf[128]; |
| 149 |
|
| 150 |
/* Non-zero forces the redisplay even if we thought it was unnecessary. */ |
| 151 |
static int forced_display = 0; |
| 152 |
|
| 153 |
/* Default and initial buffer size. Can grow. */ |
| 154 |
static int line_size = 1024; |
| 155 |
|
| 156 |
static char *last_prompt_string = (char *)NULL; |
| 157 |
static char *local_prompt, *local_prompt_prefix; |
| 158 |
static int visible_length, prefix_length; |
| 159 |
|
| 160 |
/* The number of invisible characters in the line currently being |
| 161 |
displayed on the screen. */ |
| 162 |
static int visible_wrap_offset = 0; |
| 163 |
|
| 164 |
/* The length (buffer offset) of the first line of the last (possibly |
| 165 |
multi-line) buffer displayed on the screen. */ |
| 166 |
static int visible_first_line_len = 0; |
| 167 |
|
| 168 |
/* Expand the prompt string S and return the number of visible |
| 169 |
characters in *LP, if LP is not null. This is currently more-or-less |
| 170 |
a placeholder for expansion. */ |
| 171 |
|
| 172 |
/* Current implementation: |
| 173 |
\001 (^A) start non-visible characters |
| 174 |
\002 (^B) end non-visible characters |
| 175 |
all characters except \001 and \002 (following a \001) are copied to |
| 176 |
the returned string; all characters except those between \001 and |
| 177 |
\002 are assumed to be `visible'. */ |
| 178 |
|
| 179 |
static char * |
| 180 |
expand_prompt (pmt, lp) |
| 181 |
char *pmt; |
| 182 |
int *lp; |
| 183 |
{ |
| 184 |
char *r, *ret, *p; |
| 185 |
int l, rl, ignoring; |
| 186 |
|
| 187 |
/* Short-circuit if we can. */ |
| 188 |
if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0) |
| 189 |
{ |
| 190 |
r = savestring (pmt); |
| 191 |
if (lp) |
| 192 |
*lp = strlen (r); |
| 193 |
return r; |
| 194 |
} |
| 195 |
|
| 196 |
l = strlen (pmt); |
| 197 |
r = ret = xmalloc (l + 1); |
| 198 |
|
| 199 |
for (rl = ignoring = 0, p = pmt; p && *p; p++) |
| 200 |
{ |
| 201 |
/* This code strips the invisible character string markers |
| 202 |
RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */ |
| 203 |
if (*p == RL_PROMPT_START_IGNORE) |
| 204 |
{ |
| 205 |
ignoring++; |
| 206 |
continue; |
| 207 |
} |
| 208 |
else if (ignoring && *p == RL_PROMPT_END_IGNORE) |
| 209 |
{ |
| 210 |
ignoring = 0; |
| 211 |
continue; |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
*r++ = *p; |
| 216 |
if (!ignoring) |
| 217 |
rl++; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
*r = '\0'; |
| 222 |
if (lp) |
| 223 |
*lp = rl; |
| 224 |
return ret; |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* Expand the prompt string into the various display components, if |
| 229 |
* necessary. |
| 230 |
* |
| 231 |
* local_prompt = expanded last line of string in rl_display_prompt |
| 232 |
* (portion after the final newline) |
| 233 |
* local_prompt_prefix = portion before last newline of rl_display_prompt, |
| 234 |
* expanded via expand_prompt |
| 235 |
* visible_length = number of visible characters in local_prompt |
| 236 |
* prefix_length = number of visible characters in local_prompt_prefix |
| 237 |
* |
| 238 |
* This function is called once per call to readline(). It may also be |
| 239 |
* called arbitrarily to expand the primary prompt. |
| 240 |
* |
| 241 |
* The return value is the number of visible characters on the last line |
| 242 |
* of the (possibly multi-line) prompt. |
| 243 |
*/ |
| 244 |
int |
| 245 |
rl_expand_prompt (prompt) |
| 246 |
char *prompt; |
| 247 |
{ |
| 248 |
char *p, *t; |
| 249 |
int c; |
| 250 |
|
| 251 |
/* Clear out any saved values. */ |
| 252 |
if (local_prompt) |
| 253 |
free (local_prompt); |
| 254 |
if (local_prompt_prefix) |
| 255 |
free (local_prompt_prefix); |
| 256 |
local_prompt = local_prompt_prefix = (char *)0; |
| 257 |
|
| 258 |
p = strrchr (prompt, '\n'); |
| 259 |
if (!p) |
| 260 |
{ |
| 261 |
/* The prompt is only one line. */ |
| 262 |
local_prompt = expand_prompt (prompt, &visible_length); |
| 263 |
local_prompt_prefix = (char *)0; |
| 264 |
return (visible_length); |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
/* The prompt spans multiple lines. */ |
| 269 |
t = ++p; |
| 270 |
local_prompt = expand_prompt (p, &visible_length); |
| 271 |
c = *t; *t = '\0'; |
| 272 |
/* The portion of the prompt string up to and including the |
| 273 |
final newline is now null-terminated. */ |
| 274 |
local_prompt_prefix = expand_prompt (prompt, &prefix_length); |
| 275 |
*t = c; |
| 276 |
return (prefix_length); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/* Basic redisplay algorithm. */ |
| 281 |
void |
| 282 |
rl_redisplay () |
| 283 |
{ |
| 284 |
register int in, out, c, linenum; |
| 285 |
register char *line = invisible_line; |
| 286 |
int c_pos = 0, inv_botlin = 0, wrap_offset, wrap_column; |
| 287 |
char *prompt_this_line; |
| 288 |
|
| 289 |
if (!readline_echoing_p) |
| 290 |
return; |
| 291 |
|
| 292 |
if (!rl_display_prompt) |
| 293 |
rl_display_prompt = ""; |
| 294 |
|
| 295 |
if (!invisible_line) |
| 296 |
{ |
| 297 |
visible_line = xmalloc (line_size); |
| 298 |
invisible_line = xmalloc (line_size); |
| 299 |
line = invisible_line; |
| 300 |
for (in = 0; in < line_size; in++) |
| 301 |
{ |
| 302 |
visible_line[in] = 0; |
| 303 |
invisible_line[in] = 1; |
| 304 |
} |
| 305 |
rl_on_new_line (); |
| 306 |
} |
| 307 |
|
| 308 |
/* Draw the line into the buffer. */ |
| 309 |
c_pos = -1; |
| 310 |
|
| 311 |
/* Mark the line as modified or not. We only do this for history |
| 312 |
lines. */ |
| 313 |
out = 0; |
| 314 |
if (_rl_mark_modified_lines && current_history () && rl_undo_list) |
| 315 |
{ |
| 316 |
line[out++] = '*'; |
| 317 |
line[out] = '\0'; |
| 318 |
} |
| 319 |
|
| 320 |
/* If someone thought that the redisplay was handled, but the currently |
| 321 |
visible line has a different modification state than the one about |
| 322 |
to become visible, then correct the caller's misconception. */ |
| 323 |
if (visible_line[0] != invisible_line[0]) |
| 324 |
rl_display_fixed = 0; |
| 325 |
|
| 326 |
/* If the prompt to be displayed is the `primary' readline prompt (the |
| 327 |
one passed to readline()), use the values we have already expanded. |
| 328 |
If not, use what's already in rl_display_prompt. WRAP_OFFSET is the |
| 329 |
number of non-visible characters in the prompt string. */ |
| 330 |
if (rl_display_prompt == rl_prompt) |
| 331 |
{ |
| 332 |
int local_len = strlen (local_prompt); |
| 333 |
if (local_prompt_prefix && forced_display) |
| 334 |
_rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix)); |
| 335 |
|
| 336 |
if (local_prompt) |
| 337 |
strncpy (line + out, local_prompt, local_len); |
| 338 |
out += local_len; |
| 339 |
line[out] = '\0'; |
| 340 |
wrap_offset = local_len - visible_length; |
| 341 |
} |
| 342 |
else |
| 343 |
{ |
| 344 |
int pmtlen; |
| 345 |
prompt_this_line = strrchr (rl_display_prompt, '\n'); |
| 346 |
if (!prompt_this_line) |
| 347 |
prompt_this_line = rl_display_prompt; |
| 348 |
else |
| 349 |
{ |
| 350 |
prompt_this_line++; |
| 351 |
if (forced_display) |
| 352 |
_rl_output_some_chars (rl_display_prompt, prompt_this_line - rl_display_prompt); |
| 353 |
} |
| 354 |
|
| 355 |
pmtlen = strlen (prompt_this_line); |
| 356 |
strncpy (line + out, prompt_this_line, pmtlen); |
| 357 |
out += pmtlen; |
| 358 |
line[out] = '\0'; |
| 359 |
wrap_offset = 0; |
| 360 |
} |
| 361 |
|
| 362 |
for (in = 0; in < rl_end; in++) |
| 363 |
{ |
| 364 |
c = (unsigned char)rl_line_buffer[in]; |
| 365 |
|
| 366 |
if (out + 8 >= line_size) /* XXX - 8 for \t */ |
| 367 |
{ |
| 368 |
line_size *= 2; |
| 369 |
visible_line = xrealloc (visible_line, line_size); |
| 370 |
invisible_line = xrealloc (invisible_line, line_size); |
| 371 |
line = invisible_line; |
| 372 |
} |
| 373 |
|
| 374 |
if (in == rl_point) |
| 375 |
c_pos = out; |
| 376 |
|
| 377 |
if (META_CHAR (c)) |
| 378 |
{ |
| 379 |
if (_rl_output_meta_chars == 0) |
| 380 |
{ |
| 381 |
sprintf (line + out, "\\%o", c); |
| 382 |
out += 4; |
| 383 |
} |
| 384 |
else |
| 385 |
line[out++] = c; |
| 386 |
} |
| 387 |
#if defined (DISPLAY_TABS) |
| 388 |
else if (c == '\t') |
| 389 |
{ |
| 390 |
register int newout = (out | (int)7) + 1; |
| 391 |
while (out < newout) |
| 392 |
line[out++] = ' '; |
| 393 |
} |
| 394 |
#endif |
| 395 |
else if (c < ' ') |
| 396 |
{ |
| 397 |
line[out++] = '^'; |
| 398 |
line[out++] = UNCTRL (c); /* XXX was c ^ 0x40 */ |
| 399 |
} |
| 400 |
else if (c == 127) |
| 401 |
{ |
| 402 |
line[out++] = '^'; |
| 403 |
line[out++] = '?'; |
| 404 |
} |
| 405 |
else |
| 406 |
line[out++] = c; |
| 407 |
} |
| 408 |
line[out] = '\0'; |
| 409 |
if (c_pos < 0) |
| 410 |
c_pos = out; |
| 411 |
|
| 412 |
/* C_POS == position in buffer where cursor should be placed. */ |
| 413 |
|
| 414 |
/* PWP: now is when things get a bit hairy. The visible and invisible |
| 415 |
line buffers are really multiple lines, which would wrap every |
| 416 |
(screenwidth - 1) characters. Go through each in turn, finding |
| 417 |
the changed region and updating it. The line order is top to bottom. */ |
| 418 |
|
| 419 |
/* If we can move the cursor up and down, then use multiple lines, |
| 420 |
otherwise, let long lines display in a single terminal line, and |
| 421 |
horizontally scroll it. */ |
| 422 |
|
| 423 |
if (!_rl_horizontal_scroll_mode && term_up && *term_up) |
| 424 |
{ |
| 425 |
int total_screen_chars = screenchars; |
| 426 |
int nleft, cursor_linenum, pos; |
| 427 |
|
| 428 |
if (!rl_display_fixed || forced_display) |
| 429 |
{ |
| 430 |
forced_display = 0; |
| 431 |
|
| 432 |
/* If we have more than a screenful of material to display, then |
| 433 |
only display a screenful. We should display the last screen, |
| 434 |
not the first. I'll fix this in a minute. */ |
| 435 |
if (out >= total_screen_chars) |
| 436 |
out = total_screen_chars - 1; |
| 437 |
|
| 438 |
/* Number of screen lines to display. The first line wraps at |
| 439 |
(screenwidth + wrap_offset) chars, the rest of the lines have |
| 440 |
screenwidth chars. */ |
| 441 |
nleft = out - screenwidth - wrap_offset; |
| 442 |
if (nleft > 0) |
| 443 |
inv_botlin = 1 + nleft / screenwidth; |
| 444 |
else |
| 445 |
inv_botlin = 0; |
| 446 |
|
| 447 |
/* The first line is at character position 0 in the buffer. The |
| 448 |
second and subsequent lines start at N * screenwidth, offset by |
| 449 |
OFFSET. OFFSET is wrap_offset for the invisible line and |
| 450 |
visible_wrap_offset for the line currently displayed. */ |
| 451 |
|
| 452 |
#define W_OFFSET(line, offset) ((line) == 0 ? offset : 0) |
| 453 |
#define L_OFFSET(n, offset) ((n) > 0 ? ((n) * screenwidth) + (offset) : 0) |
| 454 |
#define VIS_CHARS(line) &visible_line[L_OFFSET((line), visible_wrap_offset)] |
| 455 |
#define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line) |
| 456 |
#define INV_LINE(line) &invisible_line[L_OFFSET((line), wrap_offset)] |
| 457 |
|
| 458 |
/* For each line in the buffer, do the updating display. */ |
| 459 |
for (linenum = 0; linenum <= inv_botlin; linenum++) |
| 460 |
{ |
| 461 |
update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum, |
| 462 |
screenwidth + W_OFFSET(linenum, visible_wrap_offset), |
| 463 |
screenwidth + W_OFFSET(linenum, wrap_offset)); |
| 464 |
|
| 465 |
/* If this is the line with the prompt, we might need to |
| 466 |
compensate for invisible characters in the new line. Do |
| 467 |
this only if there is not more than one new line (which |
| 468 |
implies that we completely overwrite the old visible line) |
| 469 |
and the new line is shorter than the old. */ |
| 470 |
if (linenum == 0 && |
| 471 |
inv_botlin == 0 && |
| 472 |
(wrap_offset > visible_wrap_offset) && |
| 473 |
(_rl_last_c_pos < visible_first_line_len)) |
| 474 |
{ |
| 475 |
nleft = screenwidth + wrap_offset - _rl_last_c_pos; |
| 476 |
clear_to_eol (nleft); |
| 477 |
} |
| 478 |
|
| 479 |
/* Since the new first line is now visible, save its length. */ |
| 480 |
if (linenum == 0) |
| 481 |
visible_first_line_len = _rl_last_c_pos; |
| 482 |
} |
| 483 |
|
| 484 |
/* We may have deleted some lines. If so, clear the left over |
| 485 |
blank ones at the bottom out. */ |
| 486 |
if (_rl_vis_botlin > inv_botlin) |
| 487 |
{ |
| 488 |
char *tt; |
| 489 |
for (; linenum <= _rl_vis_botlin; linenum++) |
| 490 |
{ |
| 491 |
tt = VIS_CHARS (linenum); |
| 492 |
_rl_move_vert (linenum); |
| 493 |
_rl_move_cursor_relative (0, tt); |
| 494 |
clear_to_eol |
| 495 |
((linenum == _rl_vis_botlin) ? strlen (tt) : screenwidth); |
| 496 |
} |
| 497 |
} |
| 498 |
_rl_vis_botlin = inv_botlin; |
| 499 |
|
| 500 |
/* Move the cursor where it should be. */ |
| 501 |
/* Which line? */ |
| 502 |
nleft = c_pos - screenwidth - wrap_offset; |
| 503 |
if (nleft >= 0) |
| 504 |
cursor_linenum = 1 + nleft / screenwidth; |
| 505 |
else |
| 506 |
cursor_linenum = 0; |
| 507 |
_rl_move_vert (cursor_linenum); |
| 508 |
|
| 509 |
/* We have to reprint the prompt if it contains invisible |
| 510 |
characters, since it's not generally OK to just reprint |
| 511 |
the characters from the current cursor position. */ |
| 512 |
nleft = visible_length + wrap_offset; |
| 513 |
if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 && |
| 514 |
_rl_last_c_pos < nleft && local_prompt) |
| 515 |
{ |
| 516 |
if (term_cr) |
| 517 |
tputs (term_cr, 1, _rl_output_character_function); |
| 518 |
_rl_output_some_chars (local_prompt, nleft); |
| 519 |
_rl_last_c_pos = nleft; |
| 520 |
} |
| 521 |
|
| 522 |
/* Where on that line? And where does that line start |
| 523 |
in the buffer? */ |
| 524 |
pos = L_OFFSET(cursor_linenum, wrap_offset); |
| 525 |
nleft = c_pos - pos; |
| 526 |
|
| 527 |
/* Since backspace() doesn't know about invisible characters in the |
| 528 |
prompt, and there's no good way to tell it, we compensate for |
| 529 |
those characters here and call backspace() directly. */ |
| 530 |
if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos) |
| 531 |
{ |
| 532 |
/* XXX - I'm not sure yet why the -1 is necessary - XXX */ |
| 533 |
if (nleft < _rl_last_c_pos - 1) |
| 534 |
nleft -= wrap_offset; |
| 535 |
backspace (_rl_last_c_pos - nleft); |
| 536 |
_rl_last_c_pos = nleft; |
| 537 |
} |
| 538 |
|
| 539 |
if (nleft != _rl_last_c_pos) |
| 540 |
_rl_move_cursor_relative (nleft, &invisible_line[pos]); |
| 541 |
} |
| 542 |
} |
| 543 |
else /* Do horizontal scrolling. */ |
| 544 |
{ |
| 545 |
int lmargin; |
| 546 |
|
| 547 |
/* Always at top line. */ |
| 548 |
_rl_last_v_pos = 0; |
| 549 |
|
| 550 |
/* If the display position of the cursor would be off the edge |
| 551 |
of the screen, start the display of this line at an offset that |
| 552 |
leaves the cursor on the screen. */ |
| 553 |
if (c_pos - last_lmargin > screenwidth - 2) |
| 554 |
lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3); |
| 555 |
else if (c_pos - last_lmargin < 1) |
| 556 |
lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3); |
| 557 |
else |
| 558 |
lmargin = last_lmargin; |
| 559 |
|
| 560 |
/* If the first character on the screen isn't the first character |
| 561 |
in the display line, indicate this with a special character. */ |
| 562 |
if (lmargin > 0) |
| 563 |
line[lmargin] = '<'; |
| 564 |
|
| 565 |
if (lmargin + screenwidth < out) |
| 566 |
line[lmargin + screenwidth - 1] = '>'; |
| 567 |
|
| 568 |
if (!rl_display_fixed || forced_display || lmargin != last_lmargin) |
| 569 |
{ |
| 570 |
forced_display = 0; |
| 571 |
update_line (&visible_line[last_lmargin], &invisible_line[lmargin], |
| 572 |
0, screenwidth, screenwidth); |
| 573 |
|
| 574 |
_rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]); |
| 575 |
last_lmargin = lmargin; |
| 576 |
} |
| 577 |
} |
| 578 |
fflush (rl_outstream); |
| 579 |
|
| 580 |
/* Swap visible and non-visible lines. */ |
| 581 |
{ |
| 582 |
char *temp = visible_line; |
| 583 |
visible_line = invisible_line; |
| 584 |
invisible_line = temp; |
| 585 |
rl_display_fixed = 0; |
| 586 |
visible_wrap_offset = wrap_offset; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/* PWP: update_line() is based on finding the middle difference of each |
| 591 |
line on the screen; vis: |
| 592 |
|
| 593 |
/old first difference |
| 594 |
/beginning of line | /old last same /old EOL |
| 595 |
v v v v |
| 596 |
old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as |
| 597 |
new: eddie> Oh, my little buggy says to me, as lurgid as |
| 598 |
^ ^ ^ ^ |
| 599 |
\beginning of line | \new last same \new end of line |
| 600 |
\new first difference |
| 601 |
|
| 602 |
All are character pointers for the sake of speed. Special cases for |
| 603 |
no differences, as well as for end of line additions must be handeled. |
| 604 |
|
| 605 |
Could be made even smarter, but this works well enough */ |
| 606 |
static void |
| 607 |
update_line (old, new, current_line, omax, nmax) |
| 608 |
register char *old, *new; |
| 609 |
int current_line, omax, nmax; |
| 610 |
{ |
| 611 |
register char *ofd, *ols, *oe, *nfd, *nls, *ne; |
| 612 |
int temp, lendiff, wsatend, od, nd; |
| 613 |
|
| 614 |
/* Find first difference. */ |
| 615 |
for (ofd = old, nfd = new; |
| 616 |
(ofd - old < omax) && *ofd && (*ofd == *nfd); |
| 617 |
ofd++, nfd++) |
| 618 |
; |
| 619 |
|
| 620 |
/* Move to the end of the screen line. ND and OD are used to keep track |
| 621 |
of the distance between ne and new and oe and old, respectively, to |
| 622 |
move a subtraction out of each loop. */ |
| 623 |
for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++); |
| 624 |
for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++); |
| 625 |
|
| 626 |
/* If no difference, continue to next line. */ |
| 627 |
if (ofd == oe && nfd == ne) |
| 628 |
return; |
| 629 |
|
| 630 |
wsatend = 1; /* flag for trailing whitespace */ |
| 631 |
ols = oe - 1; /* find last same */ |
| 632 |
nls = ne - 1; |
| 633 |
while ((ols > ofd) && (nls > nfd) && (*ols == *nls)) |
| 634 |
{ |
| 635 |
if (*ols != ' ') |
| 636 |
wsatend = 0; |
| 637 |
ols--; |
| 638 |
nls--; |
| 639 |
} |
| 640 |
|
| 641 |
if (wsatend) |
| 642 |
{ |
| 643 |
ols = oe; |
| 644 |
nls = ne; |
| 645 |
} |
| 646 |
else if (*ols != *nls) |
| 647 |
{ |
| 648 |
if (*ols) /* don't step past the NUL */ |
| 649 |
ols++; |
| 650 |
if (*nls) |
| 651 |
nls++; |
| 652 |
} |
| 653 |
|
| 654 |
_rl_move_vert (current_line); |
| 655 |
|
| 656 |
/* If this is the first line and there are invisible characters in the |
| 657 |
prompt string, and the prompt string has not changed, then redraw |
| 658 |
the entire prompt string. We can only do this reliably if the |
| 659 |
terminal supports a `cr' capability. |
| 660 |
|
| 661 |
This is more than just an efficiency hack -- there is a problem with |
| 662 |
redrawing portions of the prompt string if they contain terminal |
| 663 |
escape sequences (like drawing the `unbold' sequence without a |
| 664 |
corresponding `bold') that manifests itself on certain terminals. */ |
| 665 |
|
| 666 |
lendiff = strlen (local_prompt); |
| 667 |
if (current_line == 0 && lendiff > visible_length && |
| 668 |
_rl_last_c_pos > 0 && (ofd - old) >= lendiff && term_cr) |
| 669 |
{ |
| 670 |
tputs (term_cr, 1, _rl_output_character_function); |
| 671 |
_rl_output_some_chars (local_prompt, lendiff); |
| 672 |
_rl_last_c_pos = lendiff; |
| 673 |
} |
| 674 |
|
| 675 |
_rl_move_cursor_relative (ofd - old, old); |
| 676 |
|
| 677 |
/* if (len (new) > len (old)) */ |
| 678 |
lendiff = (nls - nfd) - (ols - ofd); |
| 679 |
|
| 680 |
/* Insert (diff (len (old), len (new)) ch. */ |
| 681 |
temp = ne - nfd; |
| 682 |
if (lendiff > 0) |
| 683 |
{ |
| 684 |
if (terminal_can_insert) |
| 685 |
{ |
| 686 |
/* Sometimes it is cheaper to print the characters rather than |
| 687 |
use the terminal's capabilities. */ |
| 688 |
if ((2 * temp) < lendiff && !term_IC) |
| 689 |
{ |
| 690 |
_rl_output_some_chars (nfd, temp); |
| 691 |
_rl_last_c_pos += temp; |
| 692 |
} |
| 693 |
else |
| 694 |
{ |
| 695 |
if (*ols) |
| 696 |
{ |
| 697 |
insert_some_chars (nfd, lendiff); |
| 698 |
_rl_last_c_pos += lendiff; |
| 699 |
} |
| 700 |
else |
| 701 |
{ |
| 702 |
/* At the end of a line the characters do not have to |
| 703 |
be "inserted". They can just be placed on the screen. */ |
| 704 |
_rl_output_some_chars (nfd, lendiff); |
| 705 |
_rl_last_c_pos += lendiff; |
| 706 |
} |
| 707 |
temp = nls - nfd; |
| 708 |
/* Copy (new) chars to screen from first diff to last match. */ |
| 709 |
if ((temp - lendiff) > 0) |
| 710 |
{ |
| 711 |
_rl_output_some_chars (&nfd[lendiff], temp - lendiff); |
| 712 |
_rl_last_c_pos += temp - lendiff; |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
else |
| 717 |
{ /* cannot insert chars, write to EOL */ |
| 718 |
_rl_output_some_chars (nfd, temp); |
| 719 |
_rl_last_c_pos += temp; |
| 720 |
} |
| 721 |
} |
| 722 |
else /* Delete characters from line. */ |
| 723 |
{ |
| 724 |
/* If possible and inexpensive to use terminal deletion, then do so. */ |
| 725 |
if (term_dc && (2 * temp) >= -lendiff) |
| 726 |
{ |
| 727 |
if (lendiff) |
| 728 |
delete_chars (-lendiff); /* delete (diff) characters */ |
| 729 |
|
| 730 |
/* Copy (new) chars to screen from first diff to last match */ |
| 731 |
temp = nls - nfd; |
| 732 |
if (temp > 0) |
| 733 |
{ |
| 734 |
_rl_output_some_chars (nfd, temp); |
| 735 |
_rl_last_c_pos += temp; |
| 736 |
} |
| 737 |
} |
| 738 |
/* Otherwise, print over the existing material. */ |
| 739 |
else |
| 740 |
{ |
| 741 |
if (temp > 0) |
| 742 |
{ |
| 743 |
_rl_output_some_chars (nfd, temp); |
| 744 |
_rl_last_c_pos += temp; |
| 745 |
} |
| 746 |
clear_to_eol ((oe - old) - (ne - new)); |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
/* Tell the update routines that we have moved onto a new (empty) line. */ |
| 752 |
rl_on_new_line () |
| 753 |
{ |
| 754 |
if (visible_line) |
| 755 |
visible_line[0] = '\0'; |
| 756 |
|
| 757 |
_rl_last_c_pos = _rl_last_v_pos = 0; |
| 758 |
_rl_vis_botlin = last_lmargin = 0; |
| 759 |
return 0; |
| 760 |
} |
| 761 |
|
| 762 |
/* Actually update the display, period. */ |
| 763 |
rl_forced_update_display () |
| 764 |
{ |
| 765 |
if (visible_line) |
| 766 |
{ |
| 767 |
register char *temp = visible_line; |
| 768 |
|
| 769 |
while (*temp) *temp++ = '\0'; |
| 770 |
} |
| 771 |
rl_on_new_line (); |
| 772 |
forced_display++; |
| 773 |
rl_redisplay (); |
| 774 |
return 0; |
| 775 |
} |
| 776 |
|
| 777 |
/* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices. |
| 778 |
DATA is the contents of the screen line of interest; i.e., where |
| 779 |
the movement is being done. */ |
| 780 |
void |
| 781 |
_rl_move_cursor_relative (new, data) |
| 782 |
int new; |
| 783 |
char *data; |
| 784 |
{ |
| 785 |
register int i; |
| 786 |
|
| 787 |
/* It may be faster to output a CR, and then move forwards instead |
| 788 |
of moving backwards. */ |
| 789 |
if (CR_FASTER (new, _rl_last_c_pos)) |
| 790 |
{ |
| 791 |
tputs (term_cr, 1, _rl_output_character_function); |
| 792 |
_rl_last_c_pos = 0; |
| 793 |
} |
| 794 |
|
| 795 |
if (_rl_last_c_pos == new) return; |
| 796 |
|
| 797 |
if (_rl_last_c_pos < new) |
| 798 |
{ |
| 799 |
/* Move the cursor forward. We do it by printing the command |
| 800 |
to move the cursor forward if there is one, else print that |
| 801 |
portion of the output buffer again. Which is cheaper? */ |
| 802 |
|
| 803 |
/* The above comment is left here for posterity. It is faster |
| 804 |
to print one character (non-control) than to print a control |
| 805 |
sequence telling the terminal to move forward one character. |
| 806 |
That kind of control is for people who don't know what the |
| 807 |
data is underneath the cursor. */ |
| 808 |
#if defined (HACK_TERMCAP_MOTION) |
| 809 |
extern char *term_forward_char; |
| 810 |
|
| 811 |
if (term_forward_char) |
| 812 |
for (i = _rl_last_c_pos; i < new; i++) |
| 813 |
tputs (term_forward_char, 1, _rl_output_character_function); |
| 814 |
else |
| 815 |
for (i = _rl_last_c_pos; i < new; i++) |
| 816 |
putc (data[i], rl_outstream); |
| 817 |
#else |
| 818 |
for (i = _rl_last_c_pos; i < new; i++) |
| 819 |
putc (data[i], rl_outstream); |
| 820 |
#endif /* HACK_TERMCAP_MOTION */ |
| 821 |
} |
| 822 |
else |
| 823 |
backspace (_rl_last_c_pos - new); |
| 824 |
_rl_last_c_pos = new; |
| 825 |
} |
| 826 |
|
| 827 |
/* PWP: move the cursor up or down. */ |
| 828 |
void |
| 829 |
_rl_move_vert (to) |
| 830 |
int to; |
| 831 |
{ |
| 832 |
register int delta, i; |
| 833 |
|
| 834 |
if (_rl_last_v_pos == to || to > screenheight) |
| 835 |
return; |
| 836 |
|
| 837 |
#if defined (__GO32__) |
| 838 |
{ |
| 839 |
int row, col; |
| 840 |
|
| 841 |
ScreenGetCursor (&row, &col); |
| 842 |
ScreenSetCursor ((row + to - _rl_last_v_pos), col); |
| 843 |
} |
| 844 |
#else /* !__GO32__ */ |
| 845 |
|
| 846 |
if ((delta = to - _rl_last_v_pos) > 0) |
| 847 |
{ |
| 848 |
for (i = 0; i < delta; i++) |
| 849 |
putc ('\n', rl_outstream); |
| 850 |
tputs (term_cr, 1, _rl_output_character_function); |
| 851 |
_rl_last_c_pos = 0; |
| 852 |
} |
| 853 |
else |
| 854 |
{ /* delta < 0 */ |
| 855 |
if (term_up && *term_up) |
| 856 |
for (i = 0; i < -delta; i++) |
| 857 |
tputs (term_up, 1, _rl_output_character_function); |
| 858 |
} |
| 859 |
#endif /* !__GO32__ */ |
| 860 |
_rl_last_v_pos = to; /* Now TO is here */ |
| 861 |
} |
| 862 |
|
| 863 |
/* Physically print C on rl_outstream. This is for functions which know |
| 864 |
how to optimize the display. Return the number of characters output. */ |
| 865 |
rl_show_char (c) |
| 866 |
int c; |
| 867 |
{ |
| 868 |
int n = 1; |
| 869 |
if (META_CHAR (c) && (_rl_output_meta_chars == 0)) |
| 870 |
{ |
| 871 |
fprintf (rl_outstream, "M-"); |
| 872 |
n += 2; |
| 873 |
c = UNMETA (c); |
| 874 |
} |
| 875 |
|
| 876 |
#if defined (DISPLAY_TABS) |
| 877 |
if (c < 32 && c != '\t') |
| 878 |
#else |
| 879 |
if (c < 32) |
| 880 |
#endif /* !DISPLAY_TABS */ |
| 881 |
{ |
| 882 |
fprintf (rl_outstream, "C-"); |
| 883 |
n += 2; |
| 884 |
c += 64; |
| 885 |
} |
| 886 |
|
| 887 |
putc (c, rl_outstream); |
| 888 |
fflush (rl_outstream); |
| 889 |
return n; |
| 890 |
} |
| 891 |
|
| 892 |
int |
| 893 |
rl_character_len (c, pos) |
| 894 |
register int c, pos; |
| 895 |
{ |
| 896 |
/* was: unsigned char uc; => complaining in if (META_CHAR(uc))... */ |
| 897 |
int uc; |
| 898 |
|
| 899 |
uc = (unsigned char)c; |
| 900 |
|
| 901 |
if (META_CHAR (uc)) |
| 902 |
return ((_rl_output_meta_chars == 0) ? 4 : 1); |
| 903 |
|
| 904 |
if (uc == '\t') |
| 905 |
{ |
| 906 |
#if defined (DISPLAY_TABS) |
| 907 |
return (((pos | 7) + 1) - pos); |
| 908 |
#else |
| 909 |
return (2); |
| 910 |
#endif /* !DISPLAY_TABS */ |
| 911 |
} |
| 912 |
|
| 913 |
return ((isprint (uc)) ? 1 : 2); |
| 914 |
} |
| 915 |
|
| 916 |
/* How to print things in the "echo-area". The prompt is treated as a |
| 917 |
mini-modeline. */ |
| 918 |
|
| 919 |
/* needs editing for new usage of <stdarg.h> ... |
| 920 |
#if defined (HAVE_VARARGS_H) |
| 921 |
rl_message (va_alist) |
| 922 |
va_dcl |
| 923 |
{ |
| 924 |
char *format; |
| 925 |
va_list args; |
| 926 |
|
| 927 |
va_start (args); |
| 928 |
format = va_arg (args, char *); |
| 929 |
vsprintf (msg_buf, format, args); |
| 930 |
va_end (args); |
| 931 |
|
| 932 |
rl_display_prompt = msg_buf; |
| 933 |
rl_redisplay (); |
| 934 |
return 0; |
| 935 |
} |
| 936 |
#else /. !HAVE_VARARGS_H ./ |
| 937 |
#endif /. !HAVE_VARARGS_H ./ |
| 938 |
*/ |
| 939 |
|
| 940 |
|
| 941 |
rl_message (format, arg1, arg2) |
| 942 |
char *format; |
| 943 |
{ |
| 944 |
sprintf (msg_buf, format, arg1, arg2); |
| 945 |
rl_display_prompt = msg_buf; |
| 946 |
rl_redisplay (); |
| 947 |
return 0; |
| 948 |
} |
| 949 |
|
| 950 |
/* How to clear things from the "echo-area". */ |
| 951 |
rl_clear_message () |
| 952 |
{ |
| 953 |
rl_display_prompt = rl_prompt; |
| 954 |
rl_redisplay (); |
| 955 |
return 0; |
| 956 |
} |
| 957 |
|
| 958 |
rl_reset_line_state () |
| 959 |
{ |
| 960 |
rl_on_new_line (); |
| 961 |
|
| 962 |
rl_display_prompt = rl_prompt ? rl_prompt : ""; |
| 963 |
forced_display = 1; |
| 964 |
return 0; |
| 965 |
} |
| 966 |
|
| 967 |
/* Quick redisplay hack when erasing characters at the end of the line. */ |
| 968 |
void |
| 969 |
_rl_erase_at_end_of_line (l) |
| 970 |
int l; |
| 971 |
{ |
| 972 |
register int i; |
| 973 |
|
| 974 |
backspace (l); |
| 975 |
for (i = 0; i < l; i++) |
| 976 |
putc (' ', rl_outstream); |
| 977 |
backspace (l); |
| 978 |
for (i = 0; i < l; i++) |
| 979 |
visible_line[--_rl_last_c_pos] = '\0'; |
| 980 |
rl_display_fixed++; |
| 981 |
} |
| 982 |
|
| 983 |
/* Clear to the end of the line. COUNT is the minimum |
| 984 |
number of character spaces to clear, */ |
| 985 |
static void |
| 986 |
clear_to_eol (count) |
| 987 |
int count; |
| 988 |
{ |
| 989 |
#if !defined (__GO32__) |
| 990 |
if (term_clreol) |
| 991 |
{ |
| 992 |
tputs (term_clreol, 1, _rl_output_character_function); |
| 993 |
} |
| 994 |
else |
| 995 |
#endif /* !__GO32__ */ |
| 996 |
{ |
| 997 |
register int i; |
| 998 |
|
| 999 |
/* Do one more character space. */ |
| 1000 |
count++; |
| 1001 |
|
| 1002 |
for (i = 0; i < count; i++) |
| 1003 |
putc (' ', rl_outstream); |
| 1004 |
|
| 1005 |
backspace (count); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* Insert COUNT characters from STRING to the output stream. */ |
| 1010 |
static void |
| 1011 |
insert_some_chars (string, count) |
| 1012 |
char *string; |
| 1013 |
int count; |
| 1014 |
{ |
| 1015 |
#if defined (__GO32__) |
| 1016 |
int row, col, width; |
| 1017 |
char *row_start; |
| 1018 |
|
| 1019 |
ScreenGetCursor (&row, &col); |
| 1020 |
width = ScreenCols (); |
| 1021 |
row_start = ScreenPrimary + (row * width); |
| 1022 |
|
| 1023 |
memcpy (row_start + col + count, row_start + col, width - col - count); |
| 1024 |
|
| 1025 |
/* Place the text on the screen. */ |
| 1026 |
_rl_output_some_chars (string, count); |
| 1027 |
#else /* !_GO32 */ |
| 1028 |
|
| 1029 |
/* If IC is defined, then we do not have to "enter" insert mode. */ |
| 1030 |
if (term_IC) |
| 1031 |
{ |
| 1032 |
char *tgoto (), *buffer; |
| 1033 |
buffer = tgoto (term_IC, 0, count); |
| 1034 |
tputs (buffer, 1, _rl_output_character_function); |
| 1035 |
_rl_output_some_chars (string, count); |
| 1036 |
} |
| 1037 |
else |
| 1038 |
{ |
| 1039 |
register int i; |
| 1040 |
|
| 1041 |
/* If we have to turn on insert-mode, then do so. */ |
| 1042 |
if (term_im && *term_im) |
| 1043 |
tputs (term_im, 1, _rl_output_character_function); |
| 1044 |
|
| 1045 |
/* If there is a special command for inserting characters, then |
| 1046 |
use that first to open up the space. */ |
| 1047 |
if (term_ic && *term_ic) |
| 1048 |
{ |
| 1049 |
for (i = count; i--; ) |
| 1050 |
tputs (term_ic, 1, _rl_output_character_function); |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* Print the text. */ |
| 1054 |
_rl_output_some_chars (string, count); |
| 1055 |
|
| 1056 |
/* If there is a string to turn off insert mode, we had best use |
| 1057 |
it now. */ |
| 1058 |
if (term_ei && *term_ei) |
| 1059 |
tputs (term_ei, 1, _rl_output_character_function); |
| 1060 |
} |
| 1061 |
#endif /* !__GO32__ */ |
| 1062 |
} |
| 1063 |
|
| 1064 |
/* Delete COUNT characters from the display line. */ |
| 1065 |
static void |
| 1066 |
delete_chars (count) |
| 1067 |
int count; |
| 1068 |
{ |
| 1069 |
#if defined (__GO32__) |
| 1070 |
int row, col, width; |
| 1071 |
char *row_start; |
| 1072 |
|
| 1073 |
ScreenGetCursor (&row, &col); |
| 1074 |
width = ScreenCols (); |
| 1075 |
row_start = ScreenPrimary + (row * width); |
| 1076 |
|
| 1077 |
memcpy (row_start + col, row_start + col + count, width - col - count); |
| 1078 |
memset (row_start + width - count, 0, count * 2); |
| 1079 |
#else /* !_GO32 */ |
| 1080 |
|
| 1081 |
if (count > screenwidth) |
| 1082 |
return; |
| 1083 |
|
| 1084 |
if (term_DC && *term_DC) |
| 1085 |
{ |
| 1086 |
char *tgoto (), *buffer; |
| 1087 |
buffer = tgoto (term_DC, count, count); |
| 1088 |
tputs (buffer, count, _rl_output_character_function); |
| 1089 |
} |
| 1090 |
else |
| 1091 |
{ |
| 1092 |
if (term_dc && *term_dc) |
| 1093 |
while (count--) |
| 1094 |
tputs (term_dc, 1, _rl_output_character_function); |
| 1095 |
} |
| 1096 |
#endif /* !__GO32__ */ |
| 1097 |
} |