📄 vi_mode.c
字号:
RL_SETSTATE (RL_STATE_VIMOTION); return (0); }#endif else r = rl_vi_domove (key, &c); if (r < 0) { rl_ding (); r = -1; } _rl_mvcxt_dispose (_rl_vimvcxt); _rl_vimvcxt = 0; return r;}static intvidomove_dispatch (m) _rl_vimotion_cxt *m;{ int r; switch (m->op) { case VIM_DELETE: r = vi_delete_dispatch (m); break; case VIM_CHANGE: r = vi_change_dispatch (m); break; case VIM_YANK: r = vi_yank_dispatch (m); break; default: _rl_errmsg ("vidomove_dispatch: unknown operator %d", m->op); r = 1; break; } RL_UNSETSTATE (RL_STATE_VIMOTION); return r;}intrl_vi_rubout (count, key) int count, key;{ int opoint; if (count < 0) return (rl_vi_delete (-count, key)); if (rl_point == 0) { rl_ding (); return -1; } opoint = rl_point; if (count > 1 && MB_CUR_MAX > 1 && rl_byte_oriented == 0) rl_backward_char (count, key); else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO); else rl_point -= count; if (rl_point < 0) rl_point = 0; rl_kill_text (rl_point, opoint); return (0);}intrl_vi_delete (count, key) int count, key;{ int end; if (count < 0) return (rl_vi_rubout (-count, key)); if (rl_end == 0) { rl_ding (); return -1; } if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) end = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO); else end = rl_point + count; if (end >= rl_end) end = rl_end; rl_kill_text (rl_point, end); if (rl_point > 0 && rl_point == rl_end) rl_backward_char (1, key); return (0);}intrl_vi_back_to_indent (count, key) int count, key;{ rl_beg_of_line (1, key); while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point])) rl_point++; return (0);}intrl_vi_first_print (count, key) int count, key;{ return (rl_vi_back_to_indent (1, key));}static int _rl_cs_dir, _rl_cs_orig_dir;#if defined (READLINE_CALLBACKS)static int_rl_vi_callback_char_search (data) _rl_callback_generic_arg *data;{ int c;#if defined (HANDLE_MULTIBYTE) c = _rl_vi_last_search_mblen = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX);#else RL_SETSTATE(RL_STATE_MOREINPUT); c = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT);#endif if (c <= 0) return -1;#if !defined (HANDLE_MULTIBYTE) _rl_vi_last_search_char = c;#endif _rl_callback_func = 0; _rl_want_redisplay = 1;#if defined (HANDLE_MULTIBYTE) return (_rl_char_search_internal (data->count, _rl_cs_dir, _rl_vi_last_search_mbchar, _rl_vi_last_search_mblen));#else return (_rl_char_search_internal (data->count, _rl_cs_dir, _rl_vi_last_search_char));#endif }#endifintrl_vi_char_search (count, key) int count, key;{ int c;#if defined (HANDLE_MULTIBYTE) static char *target; static int tlen;#else static char target;#endif if (key == ';' || key == ',') { if (_rl_cs_orig_dir == 0) return -1;#if defined (HANDLE_MULTIBYTE) if (_rl_vi_last_search_mblen == 0) return -1;#else if (_rl_vi_last_search_char == 0) return -1;#endif _rl_cs_dir = (key == ';') ? _rl_cs_orig_dir : -_rl_cs_orig_dir; } else { switch (key) { case 't': _rl_cs_orig_dir = _rl_cs_dir = FTO; break; case 'T': _rl_cs_orig_dir = _rl_cs_dir = BTO; break; case 'f': _rl_cs_orig_dir = _rl_cs_dir = FFIND; break; case 'F': _rl_cs_orig_dir = _rl_cs_dir = BFIND; break; } if (vi_redoing) { /* set target and tlen below */ }#if defined (READLINE_CALLBACKS) else if (RL_ISSTATE (RL_STATE_CALLBACK)) { _rl_callback_data = _rl_callback_data_alloc (count); _rl_callback_data->i1 = _rl_cs_dir; _rl_callback_func = _rl_vi_callback_char_search; return (0); }#endif else {#if defined (HANDLE_MULTIBYTE) c = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX); if (c <= 0) return -1; _rl_vi_last_search_mblen = c;#else RL_SETSTATE(RL_STATE_MOREINPUT); c = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT); if (c < 0) return -1; _rl_vi_last_search_char = c;#endif } }#if defined (HANDLE_MULTIBYTE) target = _rl_vi_last_search_mbchar; tlen = _rl_vi_last_search_mblen;#else target = _rl_vi_last_search_char;#endif#if defined (HANDLE_MULTIBYTE) return (_rl_char_search_internal (count, _rl_cs_dir, target, tlen));#else return (_rl_char_search_internal (count, _rl_cs_dir, target));#endif}/* Match brackets */intrl_vi_match (ignore, key) int ignore, key;{ int count = 1, brack, pos, tmp, pre; pos = rl_point; if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0) { if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) { while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0) { pre = rl_point; rl_forward_char (1, key); if (pre == rl_point) break; } } else while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 && rl_point < rl_end - 1) rl_forward_char (1, key); if (brack <= 0) { rl_point = pos; rl_ding (); return -1; } } pos = rl_point; if (brack < 0) { while (count) { tmp = pos; if (MB_CUR_MAX == 1 || rl_byte_oriented) pos--; else { pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY); if (tmp == pos) pos--; } if (pos >= 0) { int b = rl_vi_bracktype (rl_line_buffer[pos]); if (b == -brack) count--; else if (b == brack) count++; } else { rl_ding (); return -1; } } } else { /* brack > 0 */ while (count) { if (MB_CUR_MAX == 1 || rl_byte_oriented) pos++; else pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY); if (pos < rl_end) { int b = rl_vi_bracktype (rl_line_buffer[pos]); if (b == -brack) count--; else if (b == brack) count++; } else { rl_ding (); return -1; } } } rl_point = pos; return (0);}intrl_vi_bracktype (c) int c;{ switch (c) { case '(': return 1; case ')': return -1; case '[': return 2; case ']': return -2; case '{': return 3; case '}': return -3; default: return 0; }}static int_rl_vi_change_char (count, c, mb) int count, c; char *mb;{ int p; if (c == '\033' || c == CTRL ('C')) return -1; rl_begin_undo_group (); while (count-- && rl_point < rl_end) { p = rl_point; rl_vi_delete (1, c); if (rl_point < p) /* Did we retreat at EOL? */ rl_point++;#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) rl_insert_text (mb); else#endif _rl_insert_char (1, c); } /* The cursor shall be left on the last character changed. */ rl_backward_char (1, c); rl_end_undo_group (); return (0);}static int_rl_vi_callback_getchar (mb, mlen) char *mb; int mlen;{ int c; RL_SETSTATE(RL_STATE_MOREINPUT); c = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT); if (c < 0) return -1;#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) c = _rl_read_mbstring (c, mb, mlen);#endif return c;}#if defined (READLINE_CALLBACKS)static int_rl_vi_callback_change_char (data) _rl_callback_generic_arg *data;{ int c; char mb[MB_LEN_MAX]; _rl_vi_last_replacement = c = _rl_vi_callback_getchar (mb, MB_LEN_MAX); if (c < 0) return -1; _rl_callback_func = 0; _rl_want_redisplay = 1; return (_rl_vi_change_char (data->count, c, mb));}#endifintrl_vi_change_char (count, key) int count, key;{ int c; char mb[MB_LEN_MAX]; if (vi_redoing) { c = _rl_vi_last_replacement; mb[0] = c; mb[1] = '\0'; }#if defined (READLINE_CALLBACKS) else if (RL_ISSTATE (RL_STATE_CALLBACK)) { _rl_callback_data = _rl_callback_data_alloc (count); _rl_callback_func = _rl_vi_callback_change_char; return (0); }#endif else _rl_vi_last_replacement = c = _rl_vi_callback_getchar (mb, MB_LEN_MAX); if (c < 0) return -1; return (_rl_vi_change_char (count, c, mb));}intrl_vi_subst (count, key) int count, key;{ /* If we are redoing, rl_vi_change_to will stuff the last motion char */ if (vi_redoing == 0) rl_stuff_char ((key == 'S') ? 'c' : 'l'); /* `S' == `cc', `s' == `cl' */ return (rl_vi_change_to (count, 'c'));}intrl_vi_overstrike (count, key) int count, key;{ if (_rl_vi_doing_insert == 0) { _rl_vi_doing_insert = 1; rl_begin_undo_group (); } if (count > 0) { _rl_overwrite_char (count, key); vi_replace_count += count; } return (0);}intrl_vi_overstrike_delete (count, key) int count, key;{ int i, s; for (i = 0; i < count; i++) { if (vi_replace_count == 0) { rl_ding (); break; } s = rl_point; if (rl_do_undo ()) vi_replace_count--; if (rl_point == s) rl_backward_char (1, key); } if (vi_replace_count == 0 && _rl_vi_doing_insert) { rl_end_undo_group (); rl_do_undo (); _rl_vi_doing_insert = 0; } return (0);}intrl_vi_replace (count, key) int count, key;{ int i; vi_replace_count = 0; if (!vi_replace_map) { vi_replace_map = rl_make_bare_keymap (); for (i = ' '; i < KEYMAP_SIZE; i++) vi_replace_map[i].function = rl_vi_overstrike; vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete; vi_replace_map[ESC].function = rl_vi_movement_mode; vi_replace_map[RETURN].function = rl_newline; vi_replace_map[NEWLINE].function = rl_newline; /* If the normal vi insertion keymap has ^H bound to erase, do the same here. Probably should remove the assignment to RUBOUT up there, but I don't think it will make a difference in real life. */ if (vi_insertion_keymap[CTRL ('H')].type == ISFUNC && vi_insertion_keymap[CTRL ('H')].function == rl_rubout) vi_replace_map[CTRL ('H')].function = rl_vi_overstrike_delete; } _rl_keymap = vi_replace_map; return (0);}#if 0/* Try to complete the word we are standing on or the word that ends with the previous character. A space matches everything. Word delimiters are space and ;. */intrl_vi_possible_completions(){ int save_pos = rl_point; if (rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';') { while (rl_point < rl_end && rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';') rl_point++; } else if (rl_line_buffer[rl_point - 1] == ';') { rl_ding (); return (0); } rl_possible_completions (); rl_point = save_pos; return (0);}#endif/* Functions to save and restore marks. */static int_rl_vi_set_mark (){ int ch; RL_SETSTATE(RL_STATE_MOREINPUT); ch = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT); if (ch < 0 || ch < 'a' || ch > 'z') /* make test against 0 explicit */ { rl_ding (); return -1; } ch -= 'a'; vi_mark_chars[ch] = rl_point; return 0;}#if defined (READLINE_CALLBACKS)static int_rl_vi_callback_set_mark (data) _rl_callback_generic_arg *data;{ _rl_callback_func = 0; _rl_want_redisplay = 1; return (_rl_vi_set_mark ());}#endifintrl_vi_set_mark (count, key) int count, key;{#if defined (READLINE_CALLBACKS) if (RL_ISSTATE (RL_STATE_CALLBACK)) { _rl_callback_data = 0; _rl_callback_func = _rl_vi_callback_set_mark; return (0); }#endif return (_rl_vi_set_mark ());}static int_rl_vi_goto_mark (){ int ch; RL_SETSTATE(RL_STATE_MOREINPUT); ch = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT); if (ch == '`') { rl_point = rl_mark; return 0; } else if (ch < 0 || ch < 'a' || ch > 'z') /* make test against 0 explicit */ { rl_ding (); return -1; } ch -= 'a'; if (vi_mark_chars[ch] == -1) { rl_ding (); return -1; } rl_point = vi_mark_chars[ch]; return 0;}#if defined (READLINE_CALLBACKS)static int_rl_vi_callback_goto_mark (data) _rl_callback_generic_arg *data;{ _rl_callback_func = 0; _rl_want_redisplay = 1; return (_rl_vi_goto_mark ());}#endifintrl_vi_goto_mark (count, key) int count, key;{#if defined (READLINE_CALLBACKS) if (RL_ISSTATE (RL_STATE_CALLBACK)) { _rl_callback_data = 0; _rl_callback_func = _rl_vi_callback_goto_mark; return (0); }#endif return (_rl_vi_goto_mark ());}#endif /* VI_MODE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -