📄 isearch.c
字号:
/* isearch.c - incremental searching *//* **************************************************************** *//* *//* I-Search and Searching *//* *//* **************************************************************** *//* Copyright (C) 1987-2009 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. Readline is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Readline is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Readline. If not, see <http://www.gnu.org/licenses/>.*/#define READLINE_LIBRARY#if defined (HAVE_CONFIG_H)# include <config.h>#endif#include <sys/types.h>#include <stdio.h>#if defined (HAVE_UNISTD_H)# include <unistd.h>#endif#if defined (HAVE_STDLIB_H)# include <stdlib.h>#else# include "ansi_stdlib.h"#endif#include "rldefs.h"#include "rlmbutil.h"#include "readline.h"#include "history.h"#include "rlprivate.h"#include "xmalloc.h"/* Variables exported to other files in the readline library. */char *_rl_isearch_terminators = (char *)NULL;_rl_search_cxt *_rl_iscxt = 0;/* Variables imported from other files in the readline library. */extern HIST_ENTRY *_rl_saved_line_for_history;static int rl_search_history PARAMS((int, int));static _rl_search_cxt *_rl_isearch_init PARAMS((int));static void _rl_isearch_fini PARAMS((_rl_search_cxt *));static int _rl_isearch_cleanup PARAMS((_rl_search_cxt *, int));/* Last line found by the current incremental search, so we don't `find' identical lines many times in a row. Now part of isearch context. *//* static char *prev_line_found; *//* Last search string and its length. */static char *last_isearch_string;static int last_isearch_string_len;static char * const default_isearch_terminators = "\033\012";_rl_search_cxt *_rl_scxt_alloc (type, flags) int type, flags;{ _rl_search_cxt *cxt; cxt = (_rl_search_cxt *)xmalloc (sizeof (_rl_search_cxt)); cxt->type = type; cxt->sflags = flags; cxt->search_string = 0; cxt->search_string_size = cxt->search_string_index = 0; cxt->lines = 0; cxt->allocated_line = 0; cxt->hlen = cxt->hindex = 0; cxt->save_point = rl_point; cxt->save_mark = rl_mark; cxt->save_line = where_history (); cxt->last_found_line = cxt->save_line; cxt->prev_line_found = 0; cxt->save_undo_list = 0; cxt->keymap = _rl_keymap; cxt->okeymap = _rl_keymap; cxt->history_pos = 0; cxt->direction = 0; cxt->lastc = 0; cxt->sline = 0; cxt->sline_len = cxt->sline_index = 0; cxt->search_terminators = 0; return cxt;}void_rl_scxt_dispose (cxt, flags) _rl_search_cxt *cxt; int flags;{ FREE (cxt->search_string); FREE (cxt->allocated_line); FREE (cxt->lines); xfree (cxt);}/* Search backwards through the history looking for a string which is typed interactively. Start with the current line. */intrl_reverse_search_history (sign, key) int sign, key;{ return (rl_search_history (-sign, key));}/* Search forwards through the history looking for a string which is typed interactively. Start with the current line. */intrl_forward_search_history (sign, key) int sign, key;{ return (rl_search_history (sign, key));}/* Display the current state of the search in the echo-area. SEARCH_STRING contains the string that is being searched for, DIRECTION is zero for forward, or non-zero for reverse, WHERE is the history list number of the current line. If it is -1, then this line is the starting one. */static voidrl_display_search (search_string, reverse_p, where) char *search_string; int reverse_p, where;{ char *message; int msglen, searchlen; searchlen = (search_string && *search_string) ? strlen (search_string) : 0; message = (char *)xmalloc (searchlen + 33); msglen = 0;#if defined (NOTDEF) if (where != -1) { sprintf (message, "[%d]", where + history_base); msglen = strlen (message); }#endif /* NOTDEF */ message[msglen++] = '('; if (reverse_p) { strcpy (message + msglen, "reverse-"); msglen += 8; } strcpy (message + msglen, "i-search)`"); msglen += 10; if (search_string) { strcpy (message + msglen, search_string); msglen += searchlen; } strcpy (message + msglen, "': "); rl_message ("%s", message); xfree (message); (*rl_redisplay_function) ();}static _rl_search_cxt *_rl_isearch_init (direction) int direction;{ _rl_search_cxt *cxt; register int i; HIST_ENTRY **hlist; cxt = _rl_scxt_alloc (RL_SEARCH_ISEARCH, 0); if (direction < 0) cxt->sflags |= SF_REVERSE; cxt->search_terminators = _rl_isearch_terminators ? _rl_isearch_terminators : default_isearch_terminators; /* Create an arrary of pointers to the lines that we want to search. */ hlist = history_list (); rl_maybe_replace_line (); i = 0; if (hlist) for (i = 0; hlist[i]; i++); /* Allocate space for this many lines, +1 for the current input line, and remember those lines. */ cxt->lines = (char **)xmalloc ((1 + (cxt->hlen = i)) * sizeof (char *)); for (i = 0; i < cxt->hlen; i++) cxt->lines[i] = hlist[i]->line; if (_rl_saved_line_for_history) cxt->lines[i] = _rl_saved_line_for_history->line; else { /* Keep track of this so we can free it. */ cxt->allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer)); strcpy (cxt->allocated_line, &rl_line_buffer[0]); cxt->lines[i] = cxt->allocated_line; } cxt->hlen++; /* The line where we start the search. */ cxt->history_pos = cxt->save_line; rl_save_prompt (); /* Initialize search parameters. */ cxt->search_string = (char *)xmalloc (cxt->search_string_size = 128); cxt->search_string[cxt->search_string_index = 0] = '\0'; /* Normalize DIRECTION into 1 or -1. */ cxt->direction = (direction >= 0) ? 1 : -1; cxt->sline = rl_line_buffer; cxt->sline_len = strlen (cxt->sline); cxt->sline_index = rl_point; _rl_iscxt = cxt; /* save globally */ return cxt;}static void_rl_isearch_fini (cxt) _rl_search_cxt *cxt;{ /* First put back the original state. */ strcpy (rl_line_buffer, cxt->lines[cxt->save_line]); rl_restore_prompt (); /* Save the search string for possible later use. */ FREE (last_isearch_string); last_isearch_string = cxt->search_string; last_isearch_string_len = cxt->search_string_index; cxt->search_string = 0; if (cxt->last_found_line < cxt->save_line) rl_get_previous_history (cxt->save_line - cxt->last_found_line, 0); else rl_get_next_history (cxt->last_found_line - cxt->save_line, 0); /* If the string was not found, put point at the end of the last matching line. If last_found_line == orig_line, we didn't find any matching history lines at all, so put point back in its original position. */ if (cxt->sline_index < 0) { if (cxt->last_found_line == cxt->save_line) cxt->sline_index = cxt->save_point; else cxt->sline_index = strlen (rl_line_buffer); rl_mark = cxt->save_mark; } rl_point = cxt->sline_index; /* Don't worry about where to put the mark here; rl_get_previous_history and rl_get_next_history take care of it. */ rl_clear_message ();}int_rl_search_getchar (cxt) _rl_search_cxt *cxt;{ int c; /* Read a key and decide how to proceed. */ RL_SETSTATE(RL_STATE_MOREINPUT); c = cxt->lastc = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT);#if defined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) c = cxt->lastc = _rl_read_mbstring (cxt->lastc, cxt->mb, MB_LEN_MAX);#endif return c;}/* Process just-read character C according to isearch context CXT. Return -1 if the caller should just free the context and return, 0 if we should break out of the loop, and 1 if we should continue to read characters. */int_rl_isearch_dispatch (cxt, c) _rl_search_cxt *cxt; int c;{ int n, wstart, wlen, limit, cval; rl_command_func_t *f; f = (rl_command_func_t *)NULL; if (c < 0) { cxt->sflags |= SF_FAILED; cxt->history_pos = cxt->last_found_line; return -1; } /* If we are moving into a new keymap, modify cxt->keymap and go on. This can be a problem if c == ESC and we want to terminate the incremental search, so we check */ if (c >= 0 && cxt->keymap[c].type == ISKMAP && strchr (cxt->search_terminators, cxt->lastc) == 0) { cxt->keymap = FUNCTION_TO_KEYMAP (cxt->keymap, c); cxt->sflags |= SF_CHGKMAP; /* XXX - we should probably save this sequence, so we can do
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -