📄 display.c
字号:
int current_line, omax, nmax, inv_botlin;{ register char *ofd, *ols, *oe, *nfd, *nls, *ne; int temp, lendiff, wsatend, od, nd; int current_invis_chars; /* If we're at the right edge of a terminal that supports xn, we're ready to wrap around, so do so. This fixes problems with knowing the exact cursor position and cut-and-paste with certain terminal emulators. In this calculation, TEMP is the physical screen position of the cursor. */ temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset); if (temp == screenwidth && _rl_term_autowrap && !_rl_horizontal_scroll_mode && _rl_last_v_pos == current_line - 1) { if (new[0]) putc (new[0], rl_outstream); else putc (' ', rl_outstream); _rl_last_c_pos = 1; /* XXX */ _rl_last_v_pos++; if (old[0] && new[0]) old[0] = new[0]; } /* Find first difference. */ for (ofd = old, nfd = new; (ofd - old < omax) && *ofd && (*ofd == *nfd); ofd++, nfd++) ; /* Move to the end of the screen line. ND and OD are used to keep track of the distance between ne and new and oe and old, respectively, to move a subtraction out of each loop. */ for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++); for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++); /* If no difference, continue to next line. */ if (ofd == oe && nfd == ne) return; wsatend = 1; /* flag for trailing whitespace */ ols = oe - 1; /* find last same */ nls = ne - 1; while ((ols > ofd) && (nls > nfd) && (*ols == *nls)) { if (*ols != ' ') wsatend = 0; ols--; nls--; } if (wsatend) { ols = oe; nls = ne; } else if (*ols != *nls) { if (*ols) /* don't step past the NUL */ ols++; if (*nls) nls++; } /* count of invisible characters in the current invisible line. */ current_invis_chars = W_OFFSET (current_line, wrap_offset); if (_rl_last_v_pos != current_line) { _rl_move_vert (current_line); if (current_line == 0 && visible_wrap_offset) _rl_last_c_pos += visible_wrap_offset; } /* If this is the first line and there are invisible characters in the prompt string, and the prompt string has not changed, and the current cursor position is before the last invisible character in the prompt, and the index of the character to move to is past the end of the prompt string, then redraw the entire prompt string. We can only do this reliably if the terminal supports a `cr' capability. This is not an efficiency hack -- there is a problem with redrawing portions of the prompt string if they contain terminal escape sequences (like drawing the `unbold' sequence without a corresponding `bold') that manifests itself on certain terminals. */ lendiff = local_prompt ? strlen (local_prompt) : 0; od = ofd - old; /* index of first difference in visible line */ if (current_line == 0 && !_rl_horizontal_scroll_mode && term_cr && lendiff > visible_length && _rl_last_c_pos > 0 && od > lendiff && _rl_last_c_pos < last_invisible) {#if defined (__MSDOS__) putc ('\r', rl_outstream);#else tputs (term_cr, 1, _rl_output_character_function);#endif _rl_output_some_chars (local_prompt, lendiff); _rl_last_c_pos = lendiff; } _rl_move_cursor_relative (od, old); /* if (len (new) > len (old)) */ lendiff = (nls - nfd) - (ols - ofd); /* If we are changing the number of invisible characters in a line, and the spot of first difference is before the end of the invisible chars, lendiff needs to be adjusted. */ if (current_line == 0 && !_rl_horizontal_scroll_mode && current_invis_chars != visible_wrap_offset) lendiff += visible_wrap_offset - current_invis_chars; /* Insert (diff (len (old), len (new)) ch. */ temp = ne - nfd; if (lendiff > 0) { /* Non-zero if we're increasing the number of lines. */ int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin; /* Sometimes it is cheaper to print the characters rather than use the terminal's capabilities. If we're growing the number of lines, make sure we actually cause the new line to wrap around on auto-wrapping terminals. */ if (terminal_can_insert && ((2 * temp) >= lendiff || term_IC) && (!_rl_term_autowrap || !gl)) { /* If lendiff > visible_length and _rl_last_c_pos == 0 and _rl_horizontal_scroll_mode == 1, inserting the characters with term_IC or term_ic will screw up the screen because of the invisible characters. We need to just draw them. */ if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 || lendiff <= visible_length || !current_invis_chars)) { insert_some_chars (nfd, lendiff); _rl_last_c_pos += lendiff; } else if (*ols == 0) { /* At the end of a line the characters do not have to be "inserted". They can just be placed on the screen. */ /* However, this screws up the rest of this block, which assumes you've done the insert because you can. */ _rl_output_some_chars (nfd, lendiff); _rl_last_c_pos += lendiff; } else { /* We have horizontal scrolling and we are not inserting at the end. We have invisible characters in this line. This is a dumb update. */ _rl_output_some_chars (nfd, temp); _rl_last_c_pos += temp; return; } /* Copy (new) chars to screen from first diff to last match. */ temp = nls - nfd; if ((temp - lendiff) > 0) { _rl_output_some_chars (nfd + lendiff, temp - lendiff); _rl_last_c_pos += temp - lendiff; } } else { /* cannot insert chars, write to EOL */ _rl_output_some_chars (nfd, temp); _rl_last_c_pos += temp; } } else /* Delete characters from line. */ { /* If possible and inexpensive to use terminal deletion, then do so. */ if (term_dc && (2 * temp) >= -lendiff) { /* If all we're doing is erasing the invisible characters in the prompt string, don't bother. It screws up the assumptions about what's on the screen. */ if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 && -lendiff == visible_wrap_offset) lendiff = 0; if (lendiff) delete_chars (-lendiff); /* delete (diff) characters */ /* Copy (new) chars to screen from first diff to last match */ temp = nls - nfd; if (temp > 0) { _rl_output_some_chars (nfd, temp); _rl_last_c_pos += temp; } } /* Otherwise, print over the existing material. */ else { if (temp > 0) { _rl_output_some_chars (nfd, temp); _rl_last_c_pos += temp; } lendiff = (oe - old) - (ne - new); if (lendiff) { if (_rl_term_autowrap && current_line < inv_botlin) space_to_eol (lendiff); else _rl_clear_to_eol (lendiff); } } }}/* Tell the update routines that we have moved onto a new (empty) line. */intrl_on_new_line (){ if (visible_line) visible_line[0] = '\0'; _rl_last_c_pos = _rl_last_v_pos = 0; _rl_vis_botlin = last_lmargin = 0; if (vis_lbreaks) vis_lbreaks[0] = vis_lbreaks[1] = 0; visible_wrap_offset = 0; return 0;}/* Tell the update routines that we have moved onto a new line with the prompt already displayed. Code originally from the version of readline distributed with CLISP. */intrl_on_new_line_with_prompt (){ int prompt_size, i, l, real_screenwidth, newlines; char *prompt_last_line; /* Initialize visible_line and invisible_line to ensure that they can hold the already-displayed prompt. */ prompt_size = strlen (rl_prompt) + 1; init_line_structures (prompt_size); /* Make sure the line structures hold the already-displayed prompt for redisplay. */ strcpy (visible_line, rl_prompt); strcpy (invisible_line, rl_prompt); /* If the prompt contains newlines, take the last tail. */ prompt_last_line = strrchr (rl_prompt, '\n'); if (!prompt_last_line) prompt_last_line = rl_prompt; l = strlen (prompt_last_line); _rl_last_c_pos = l; /* Dissect prompt_last_line into screen lines. Note that here we have to use the real screenwidth. Readline's notion of screenwidth might be one less, see terminal.c. */ real_screenwidth = screenwidth + (_rl_term_autowrap ? 0 : 1); _rl_last_v_pos = l / real_screenwidth; /* If the prompt length is a multiple of real_screenwidth, we don't know whether the cursor is at the end of the last line, or already at the beginning of the next line. Output a newline just to be safe. */ if (l > 0 && (l % real_screenwidth) == 0) _rl_output_some_chars ("\n", 1); last_lmargin = 0; newlines = 0; i = 0; while (i <= l) { _rl_vis_botlin = newlines; vis_lbreaks[newlines++] = i; i += real_screenwidth; } vis_lbreaks[newlines] = l; visible_wrap_offset = 0; return 0;}/* Actually update the display, period. */intrl_forced_update_display (){ if (visible_line) { register char *temp = visible_line; while (*temp) *temp++ = '\0'; } rl_on_new_line (); forced_display++; (*rl_redisplay_function) (); return 0;}/* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices. DATA is the contents of the screen line of interest; i.e., where the movement is being done. */void_rl_move_cursor_relative (new, data) int new; char *data;{ register int i; /* If we don't have to do anything, then return. */ if (_rl_last_c_pos == new) return; /* It may be faster to output a CR, and then move forwards instead of moving backwards. */ /* i == current physical cursor position. */ i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset); if (new == 0 || CR_FASTER (new, _rl_last_c_pos) || (_rl_term_autowrap && i == screenwidth)) {#if defined (__MSDOS__) putc ('\r', rl_outstream);#else tputs (term_cr, 1, _rl_output_character_function);#endif /* !__MSDOS__ */ _rl_last_c_pos = 0; } if (_rl_last_c_pos < new) { /* Move the cursor forward. We do it by printing the command to move the cursor forward if there is one, else print that portion of the output buffer again. Which is cheaper? */ /* The above comment is left here for posterity. It is faster to print one character (non-control) than to print a control sequence telling the terminal to move forward one character. That kind of control is for people who don't know what the data is underneath the cursor. */#if defined (HACK_TERMCAP_MOTION) if (term_forward_char) for (i = _rl_last_c_pos; i < new; i++) tputs (term_forward_char, 1, _rl_output_character_function); else for (i = _rl_last_c_pos; i < new; i++) putc (data[i], rl_outstream);#else for (i = _rl_last_c_pos; i < new; i++) putc (data[i], rl_outstream);#endif /* HACK_TERMCAP_MOTION */ } else if (_rl_last_c_pos > new) _rl_backspace (_rl_last_c_pos - new); _rl_last_c_pos = new;}/* PWP: move the cursor up or down. */void_rl_move_vert (to) int to;{ register int delta, i; if (_rl_last_v_pos == to || to > screenheight) return; if ((delta = to - _rl_last_v_pos) > 0) { for (i = 0; i < delta; i++) putc ('\n', rl_outstream);#if defined (__MSDOS__) putc ('\r', rl_outstream);#else tputs (term_cr, 1, _rl_output_character_function);#endif _rl_last_c_pos = 0; } else { /* delta < 0 */#ifdef __MSDOS__ int row, col; i = fflush (rl_outstream); /* make sure the cursor pos is current! */ ScreenGetCursor (&row, &col); ScreenSetCursor ((row + to - _rl_last_v_pos), col); delta = i;#else /* !__MSDOS__ */ if (term_up && *term_up) for (i = 0; i < -delta; i++) tputs (term_up, 1, _rl_output_character_function);#endif /* !__MSDOS__ */ } _rl_last_v_pos = to; /* Now TO is here */}/* Physically print C on rl_outstream. This is for functions which know how to optimize the display. Return the number of characters output. */intrl_show_char (c) int c;{ int n = 1; if (META_CHAR (c) && (_rl_output_meta_chars == 0)) { fprintf (rl_outstream, "M-"); n += 2; c = UNMETA (c); }#if defined (DISPLAY_TABS) if ((CTRL_CHAR (c) && c != '\t') || c == RUBOUT)#else if (CTRL_CHAR (c) || c == RUBOUT)#endif /* !DISPLAY_TABS */ { fprintf (rl_outstream, "C-"); n += 2; c = CTRL_CHAR (c) ? UNCTRL (c) : '?'; } putc (c, rl_outstream); fflush (rl_outstream); return n;}intrl_character_len (c, pos) register int c, pos;{ unsigned char uc; uc = (unsigned char)c;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -