📄 display.c
字号:
prompt_last_invisible = prompt_invis_chars_first_line = 0; prompt_visible_length = prompt_physical_chars = 0; if (prompt == 0 || *prompt == 0) return (0); p = strrchr (prompt, '\n'); if (!p) { /* The prompt is only one logical line, though it might wrap. */ local_prompt = expand_prompt (prompt, &prompt_visible_length, &prompt_last_invisible, &prompt_invis_chars_first_line, &prompt_physical_chars); local_prompt_prefix = (char *)0; local_prompt_len = local_prompt ? strlen (local_prompt) : 0; return (prompt_visible_length); } else { /* The prompt spans multiple lines. */ t = ++p; local_prompt = expand_prompt (p, &prompt_visible_length, &prompt_last_invisible, &prompt_invis_chars_first_line, &prompt_physical_chars); c = *t; *t = '\0'; /* The portion of the prompt string up to and including the final newline is now null-terminated. */ local_prompt_prefix = expand_prompt (prompt, &prompt_prefix_length, (int *)NULL, (int *)NULL, (int *)NULL); *t = c; local_prompt_len = local_prompt ? strlen (local_prompt) : 0; return (prompt_prefix_length); }}/* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated arrays of line break markers. MINSIZE is the minimum size of VISIBLE_LINE and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is increased. If the lines have already been allocated, this ensures that they can hold at least MINSIZE characters. */static voidinit_line_structures (minsize) int minsize;{ register int n; if (invisible_line == 0) /* initialize it */ { if (line_size < minsize) line_size = minsize; visible_line = (char *)xmalloc (line_size); invisible_line = (char *)xmalloc (line_size); } else if (line_size < minsize) /* ensure it can hold MINSIZE chars */ { line_size *= 2; if (line_size < minsize) line_size = minsize; visible_line = (char *)xrealloc (visible_line, line_size); invisible_line = (char *)xrealloc (invisible_line, line_size); } for (n = minsize; n < line_size; n++) { visible_line[n] = 0; invisible_line[n] = 1; } if (vis_lbreaks == 0) { /* should be enough. */ inv_lbsize = vis_lbsize = 256;#if defined (HANDLE_MULTIBYTE) line_state_visible->wbsize = vis_lbsize; line_state_visible->wrapped_line = (int *)xmalloc (line_state_visible->wbsize * sizeof (int)); line_state_invisible->wbsize = inv_lbsize; line_state_invisible->wrapped_line = (int *)xmalloc (line_state_invisible->wbsize * sizeof (int));#endif inv_lbreaks = (int *)xmalloc (inv_lbsize * sizeof (int)); vis_lbreaks = (int *)xmalloc (vis_lbsize * sizeof (int)); inv_lbreaks[0] = vis_lbreaks[0] = 0; } line_structures_initialized = 1;} /* Basic redisplay algorithm. */voidrl_redisplay (){ register int in, out, c, linenum, cursor_linenum; register char *line; int inv_botlin, lb_botlin, lb_linenum, o_cpos; int newlines, lpos, temp, n0, num, prompt_lines_estimate; char *prompt_this_line;#if defined (HANDLE_MULTIBYTE) wchar_t wc; size_t wc_bytes; int wc_width; mbstate_t ps; int _rl_wrapped_multicolumn = 0;#endif if (_rl_echoing_p == 0) return; /* Block keyboard interrupts because this function manipulates global data structures. */ _rl_block_sigint (); RL_SETSTATE (RL_STATE_REDISPLAYING); if (!rl_display_prompt) rl_display_prompt = ""; if (line_structures_initialized == 0) { init_line_structures (0); rl_on_new_line (); } /* Draw the line into the buffer. */ cpos_buffer_position = -1; prompt_multibyte_chars = prompt_visible_length - prompt_physical_chars; line = invisible_line; out = inv_botlin = 0; /* Mark the line as modified or not. We only do this for history lines. */ modmark = 0; if (_rl_mark_modified_lines && current_history () && rl_undo_list) { line[out++] = '*'; line[out] = '\0'; modmark = 1; } /* If someone thought that the redisplay was handled, but the currently visible line has a different modification state than the one about to become visible, then correct the caller's misconception. */ if (visible_line[0] != invisible_line[0]) rl_display_fixed = 0; /* If the prompt to be displayed is the `primary' readline prompt (the one passed to readline()), use the values we have already expanded. If not, use what's already in rl_display_prompt. WRAP_OFFSET is the number of non-visible characters in the prompt string. */ if (rl_display_prompt == rl_prompt || local_prompt) { if (local_prompt_prefix && forced_display) _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix)); if (local_prompt_len > 0) { temp = local_prompt_len + out + 2; if (temp >= line_size) { line_size = (temp + 1024) - (temp % 1024); visible_line = (char *)xrealloc (visible_line, line_size); line = invisible_line = (char *)xrealloc (invisible_line, line_size); } strncpy (line + out, local_prompt, local_prompt_len); out += local_prompt_len; } line[out] = '\0'; wrap_offset = local_prompt_len - prompt_visible_length; } else { int pmtlen; prompt_this_line = strrchr (rl_display_prompt, '\n'); if (!prompt_this_line) prompt_this_line = rl_display_prompt; else { prompt_this_line++; pmtlen = prompt_this_line - rl_display_prompt; /* temp var */ if (forced_display) { _rl_output_some_chars (rl_display_prompt, pmtlen); /* Make sure we are at column zero even after a newline, regardless of the state of terminal output processing. */ if (pmtlen < 2 || prompt_this_line[-2] != '\r') cr (); } } prompt_physical_chars = pmtlen = strlen (prompt_this_line); temp = pmtlen + out + 2; if (temp >= line_size) { line_size = (temp + 1024) - (temp % 1024); visible_line = (char *)xrealloc (visible_line, line_size); line = invisible_line = (char *)xrealloc (invisible_line, line_size); } strncpy (line + out, prompt_this_line, pmtlen); out += pmtlen; line[out] = '\0'; wrap_offset = prompt_invis_chars_first_line = 0; }#define CHECK_INV_LBREAKS() \ do { \ if (newlines >= (inv_lbsize - 2)) \ { \ inv_lbsize *= 2; \ inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \ } \ } while (0)#if defined (HANDLE_MULTIBYTE) #define CHECK_LPOS() \ do { \ lpos++; \ if (lpos >= _rl_screenwidth) \ { \ if (newlines >= (inv_lbsize - 2)) \ { \ inv_lbsize *= 2; \ inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \ } \ inv_lbreaks[++newlines] = out; \ if (newlines >= (line_state_invisible->wbsize - 1)) \ { \ line_state_invisible->wbsize *= 2; \ line_state_invisible->wrapped_line = (int *)xrealloc (line_state_invisible->wrapped_line, line_state_invisible->wbsize * sizeof(int)); \ } \ line_state_invisible->wrapped_line[newlines] = _rl_wrapped_multicolumn; \ lpos = 0; \ } \ } while (0)#else#define CHECK_LPOS() \ do { \ lpos++; \ if (lpos >= _rl_screenwidth) \ { \ if (newlines >= (inv_lbsize - 2)) \ { \ inv_lbsize *= 2; \ inv_lbreaks = (int *)xrealloc (inv_lbreaks, inv_lbsize * sizeof (int)); \ } \ inv_lbreaks[++newlines] = out; \ lpos = 0; \ } \ } while (0)#endif /* inv_lbreaks[i] is where line i starts in the buffer. */ inv_lbreaks[newlines = 0] = 0; lpos = prompt_physical_chars + modmark;#if defined (HANDLE_MULTIBYTE) memset (line_state_invisible->wrapped_line, 0, line_state_invisible->wbsize * sizeof (int)); num = 0;#endif /* prompt_invis_chars_first_line is the number of invisible characters in the first physical line of the prompt. wrap_offset - prompt_invis_chars_first_line is the number of invis chars on the second (or, more generally, last) line. */ /* This is zero-based, used to set the newlines */ prompt_lines_estimate = lpos / _rl_screenwidth; /* what if lpos is already >= _rl_screenwidth before we start drawing the contents of the command line? */ while (lpos >= _rl_screenwidth) { int z; /* fix from Darin Johnson <darin@acuson.com> for prompt string with invisible characters that is longer than the screen width. The prompt_invis_chars_first_line variable could be made into an array saying how many invisible characters there are per line, but that's probably too much work for the benefit gained. How many people have prompts that exceed two physical lines? Additional logic fix from Edward Catmur <ed@catmur.co.uk> */#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0) { n0 = num; temp = local_prompt_len; while (num < temp) { z = _rl_col_width (local_prompt, n0, num, 1); if (z > _rl_screenwidth) { num = _rl_find_prev_mbchar (local_prompt, num, MB_FIND_ANY); break; } else if (z == _rl_screenwidth) break; num++; } temp = num; } else#endif /* !HANDLE_MULTIBYTE */ temp = ((newlines + 1) * _rl_screenwidth); /* Now account for invisible characters in the current line. */ /* XXX - this assumes that the invisible characters may be split, but only between the first and the last lines. */ temp += ((local_prompt_prefix == 0) ? ((newlines == 0) ? prompt_invis_chars_first_line : ((newlines == prompt_lines_estimate) ? wrap_offset : prompt_invis_chars_first_line)) : ((newlines == 0) ? wrap_offset : 0)); inv_lbreaks[++newlines] = temp;#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0 && prompt_multibyte_chars > 0) lpos -= _rl_col_width (local_prompt, n0, num, 1); else#endif lpos -= _rl_screenwidth; } prompt_last_screen_line = newlines; /* Draw the rest of the line (after the prompt) into invisible_line, keeping track of where the cursor is (cpos_buffer_position), the number of the line containing the cursor (lb_linenum), the last line number (lb_botlin and inv_botlin). It maintains an array of line breaks for display (inv_lbreaks). This handles expanding tabs for display and displaying meta characters. */ lb_linenum = 0;#if defined (HANDLE_MULTIBYTE) in = 0; if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) { memset (&ps, 0, sizeof (mbstate_t)); /* XXX - what if wc_bytes ends up <= 0? check for MB_INVALIDCH */ wc_bytes = mbrtowc (&wc, rl_line_buffer, rl_end, &ps); } else wc_bytes = 1; while (in < rl_end)#else for (in = 0; in < rl_end; in++)#endif { c = (unsigned char)rl_line_buffer[in];#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) { if (MB_INVALIDCH (wc_bytes)) { /* Byte sequence is invalid or shortened. Assume that the first byte represents a character. */ wc_bytes = 1; /* Assume that a character occupies a single column. */ wc_width = 1; memset (&ps, 0, sizeof (mbstate_t)); } else if (MB_NULLWCH (wc_bytes)) break; /* Found '\0' */ else { temp = wcwidth (wc); wc_width = (temp >= 0) ? temp : 1; } }#endif if (out + 8 >= line_size) /* XXX - 8 for \t */ { line_size *= 2; visible_line = (char *)xrealloc (visible_line, line_size); invisible_line = (char *)xrealloc (invisible_line, line_size); line = invisible_line; } if (in == rl_point) { cpos_buffer_position = out; lb_linenum = newlines; }#if defined (HANDLE_MULTIBYTE) if (META_CHAR (c) && _rl_output_meta_chars == 0) /* XXX - clean up */#else if (META_CHAR (c))#endif { if (_rl_output_meta_chars == 0) { sprintf (line + out, "\\%o", c); if (lpos + 4 >= _rl_screenwidth) { temp = _rl_screenwidth - lpos;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -