📄 isearch.c
字号:
something useful if this doesn't end up mapping to a command. */ return 1; } /* Translate the keys we do something with to opcodes. */ if (c >= 0 && cxt->keymap[c].type == ISFUNC) { f = cxt->keymap[c].function; if (f == rl_reverse_search_history) cxt->lastc = (cxt->sflags & SF_REVERSE) ? -1 : -2; else if (f == rl_forward_search_history) cxt->lastc = (cxt->sflags & SF_REVERSE) ? -2 : -1; else if (f == rl_rubout) cxt->lastc = -3; else if (c == CTRL ('G') || f == rl_abort) cxt->lastc = -4; else if (c == CTRL ('W') || f == rl_unix_word_rubout) /* XXX */ cxt->lastc = -5; else if (c == CTRL ('Y') || f == rl_yank) /* XXX */ cxt->lastc = -6; } /* If we changed the keymap earlier while translating a key sequence into a command, restore it now that we've succeeded. */ if (cxt->sflags & SF_CHGKMAP) { cxt->keymap = cxt->okeymap; cxt->sflags &= ~SF_CHGKMAP; } /* The characters in isearch_terminators (set from the user-settable variable isearch-terminators) are used to terminate the search but not subsequently execute the character as a command. The default value is "\033\012" (ESC and C-J). */ if (cxt->lastc > 0 && strchr (cxt->search_terminators, cxt->lastc)) { /* ESC still terminates the search, but if there is pending input or if input arrives within 0.1 seconds (on systems with select(2)) it is used as a prefix character with rl_execute_next. WATCH OUT FOR THIS! This is intended to allow the arrow keys to be used like ^F and ^B are used to terminate the search and execute the movement command. XXX - since _rl_input_available depends on the application- settable keyboard timeout value, this could alternatively use _rl_input_queued(100000) */ if (cxt->lastc == ESC && _rl_input_available ()) rl_execute_next (ESC); return (0); }#define ENDSRCH_CHAR(c) \ ((CTRL_CHAR (c) || META_CHAR (c) || (c) == RUBOUT) && ((c) != CTRL ('G')))#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) { if (cxt->lastc >= 0 && (cxt->mb[0] && cxt->mb[1] == '\0') && ENDSRCH_CHAR (cxt->lastc)) { /* This sets rl_pending_input to LASTC; it will be picked up the next time rl_read_key is called. */ rl_execute_next (cxt->lastc); return (0); } } else#endif if (cxt->lastc >= 0 && ENDSRCH_CHAR (cxt->lastc)) { /* This sets rl_pending_input to LASTC; it will be picked up the next time rl_read_key is called. */ rl_execute_next (cxt->lastc); return (0); } /* Now dispatch on the character. `Opcodes' affect the search string or state. Other characters are added to the string. */ switch (cxt->lastc) { /* search again */ case -1: if (cxt->search_string_index == 0) { if (last_isearch_string) { cxt->search_string_size = 64 + last_isearch_string_len; cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size); strcpy (cxt->search_string, last_isearch_string); cxt->search_string_index = last_isearch_string_len; rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1); break; } return (1); } else if (cxt->sflags & SF_REVERSE) cxt->sline_index--; else if (cxt->sline_index != cxt->sline_len) cxt->sline_index++; else rl_ding (); break; /* switch directions */ case -2: cxt->direction = -cxt->direction; if (cxt->direction < 0) cxt->sflags |= SF_REVERSE; else cxt->sflags &= ~SF_REVERSE; break; /* delete character from search string. */ case -3: /* C-H, DEL */ /* This is tricky. To do this right, we need to keep a stack of search positions for the current search, with sentinels marking the beginning and end. But this will do until we have a real isearch-undo. */ if (cxt->search_string_index == 0) rl_ding (); else cxt->search_string[--cxt->search_string_index] = '\0'; break; case -4: /* C-G, abort */ rl_replace_line (cxt->lines[cxt->save_line], 0); rl_point = cxt->save_point; rl_mark = cxt->save_mark; rl_restore_prompt(); rl_clear_message (); return -1; case -5: /* C-W */ /* skip over portion of line we already matched and yank word */ wstart = rl_point + cxt->search_string_index; if (wstart >= rl_end) { rl_ding (); break; } /* if not in a word, move to one. */ cval = _rl_char_value (rl_line_buffer, wstart); if (_rl_walphabetic (cval) == 0) { rl_ding (); break; } n = MB_NEXTCHAR (rl_line_buffer, wstart, 1, MB_FIND_NONZERO);; while (n < rl_end) { cval = _rl_char_value (rl_line_buffer, n); if (_rl_walphabetic (cval) == 0) break; n = MB_NEXTCHAR (rl_line_buffer, n, 1, MB_FIND_NONZERO);; } wlen = n - wstart + 1; if (cxt->search_string_index + wlen + 1 >= cxt->search_string_size) { cxt->search_string_size += wlen + 1; cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size); } for (; wstart < n; wstart++) cxt->search_string[cxt->search_string_index++] = rl_line_buffer[wstart]; cxt->search_string[cxt->search_string_index] = '\0'; break; case -6: /* C-Y */ /* skip over portion of line we already matched and yank rest */ wstart = rl_point + cxt->search_string_index; if (wstart >= rl_end) { rl_ding (); break; } n = rl_end - wstart + 1; if (cxt->search_string_index + n + 1 >= cxt->search_string_size) { cxt->search_string_size += n + 1; cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size); } for (n = wstart; n < rl_end; n++) cxt->search_string[cxt->search_string_index++] = rl_line_buffer[n]; cxt->search_string[cxt->search_string_index] = '\0'; break; /* Add character to search string and continue search. */ default: if (cxt->search_string_index + 2 >= cxt->search_string_size) { cxt->search_string_size += 128; cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size); }#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) { int j, l; for (j = 0, l = strlen (cxt->mb); j < l; ) cxt->search_string[cxt->search_string_index++] = cxt->mb[j++]; } else#endif cxt->search_string[cxt->search_string_index++] = c; cxt->search_string[cxt->search_string_index] = '\0'; break; } for (cxt->sflags &= ~(SF_FOUND|SF_FAILED);; ) { limit = cxt->sline_len - cxt->search_string_index + 1; /* Search the current line. */ while ((cxt->sflags & SF_REVERSE) ? (cxt->sline_index >= 0) : (cxt->sline_index < limit)) { if (STREQN (cxt->search_string, cxt->sline + cxt->sline_index, cxt->search_string_index)) { cxt->sflags |= SF_FOUND; break; } else cxt->sline_index += cxt->direction; } if (cxt->sflags & SF_FOUND) break; /* Move to the next line, but skip new copies of the line we just found and lines shorter than the string we're searching for. */ do { /* Move to the next line. */ cxt->history_pos += cxt->direction; /* At limit for direction? */ if ((cxt->sflags & SF_REVERSE) ? (cxt->history_pos < 0) : (cxt->history_pos == cxt->hlen)) { cxt->sflags |= SF_FAILED; break; } /* We will need these later. */ cxt->sline = cxt->lines[cxt->history_pos]; cxt->sline_len = strlen (cxt->sline); } while ((cxt->prev_line_found && STREQ (cxt->prev_line_found, cxt->lines[cxt->history_pos])) || (cxt->search_string_index > cxt->sline_len)); if (cxt->sflags & SF_FAILED) break; /* Now set up the line for searching... */ cxt->sline_index = (cxt->sflags & SF_REVERSE) ? cxt->sline_len - cxt->search_string_index : 0; } if (cxt->sflags & SF_FAILED) { /* We cannot find the search string. Ding the bell. */ rl_ding (); cxt->history_pos = cxt->last_found_line; return 1; } /* We have found the search string. Just display it. But don't actually move there in the history list until the user accepts the location. */ if (cxt->sflags & SF_FOUND) { cxt->prev_line_found = cxt->lines[cxt->history_pos]; rl_replace_line (cxt->lines[cxt->history_pos], 0); rl_point = cxt->sline_index; cxt->last_found_line = cxt->history_pos; rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), (cxt->history_pos == cxt->save_line) ? -1 : cxt->history_pos); } return 1;}static int_rl_isearch_cleanup (cxt, r) _rl_search_cxt *cxt; int r;{ if (r >= 0) _rl_isearch_fini (cxt); _rl_scxt_dispose (cxt, 0); _rl_iscxt = 0; RL_UNSETSTATE(RL_STATE_ISEARCH); return (r != 0);}/* Search through the history looking for an interactively typed string. This is analogous to i-search. We start the search in the current line. DIRECTION is which direction to search; >= 0 means forward, < 0 means backwards. */static intrl_search_history (direction, invoking_key) int direction, invoking_key;{ _rl_search_cxt *cxt; /* local for now, but saved globally */ int c, r; RL_SETSTATE(RL_STATE_ISEARCH); cxt = _rl_isearch_init (direction); rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1); /* If we are using the callback interface, all we do is set up here and return. The key is that we leave RL_STATE_ISEARCH set. */ if (RL_ISSTATE (RL_STATE_CALLBACK)) return (0); r = -1; for (;;) { c = _rl_search_getchar (cxt); /* We might want to handle EOF here (c == 0) */ r = _rl_isearch_dispatch (cxt, cxt->lastc); if (r <= 0) break; } /* The searching is over. The user may have found the string that she was looking for, or else she may have exited a failing search. If LINE_INDEX is -1, then that shows that the string searched for was not found. We use this to determine where to place rl_point. */ return (_rl_isearch_cleanup (cxt, r));}#if defined (READLINE_CALLBACKS)/* Called from the callback functions when we are ready to read a key. The callback functions know to call this because RL_ISSTATE(RL_STATE_ISEARCH). If _rl_isearch_dispatch finishes searching, this function is responsible for turning off RL_STATE_ISEARCH, which it does using _rl_isearch_cleanup. */int_rl_isearch_callback (cxt) _rl_search_cxt *cxt;{ int c, r; c = _rl_search_getchar (cxt); /* We might want to handle EOF here */ r = _rl_isearch_dispatch (cxt, cxt->lastc); return (r <= 0) ? _rl_isearch_cleanup (cxt, r) : 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -