📄 search.c
字号:
/* $NetBSD: search.c,v 1.12 2002/03/18 16:00:58 christos Exp $ *//*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Christos Zoulas of Cornell University. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include "config.h"#if !defined(lint) && !defined(SCCSID)#if 0static char sccsid[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";#else__RCSID("$NetBSD: search.c,v 1.12 2002/03/18 16:00:58 christos Exp $");#endif#endif /* not lint && not SCCSID *//* * search.c: History and character search functions */#include <stdlib.h>#if defined(REGEX)#include <regex.h>#elif defined(REGEXP)#include <regexp.h>#endif#include "el.h"/* * Adjust cursor in vi mode to include the character under it */#define EL_CURSOR(el) \ ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \ ((el)->el_map.current == (el)->el_map.alt)))/* search_init(): * Initialize the search stuff */protected intsearch_init(EditLine *el){ el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ); if (el->el_search.patbuf == NULL) return (-1); el->el_search.patlen = 0; el->el_search.patdir = -1; el->el_search.chacha = '\0'; el->el_search.chadir = -1; return (0);}/* search_end(): * Initialize the search stuff */protected voidsearch_end(EditLine *el){ el_free((ptr_t) el->el_search.patbuf); el->el_search.patbuf = NULL;}#ifdef REGEXP/* regerror(): * Handle regular expression errors */public void/*ARGSUSED*/regerror(const char *msg){}#endif/* el_match(): * Return if string matches pattern */protected intel_match(const char *str, const char *pat){#if defined (REGEX) regex_t re; int rv;#elif defined (REGEXP) regexp *rp; int rv;#else extern char *re_comp(const char *); extern int re_exec(const char *);#endif if (strstr(str, pat) != NULL) return (1);#if defined(REGEX) if (regcomp(&re, pat, 0) == 0) { rv = regexec(&re, str, 0, NULL, 0) == 0; regfree(&re); } else { rv = 0; } return (rv);#elif defined(REGEXP) if ((re = regcomp(pat)) != NULL) { rv = regexec(re, str); free((ptr_t) re); } else { rv = 0; } return (rv);#else if (re_comp(pat) != NULL) return (0); else return (re_exec(str) == 1);#endif}/* c_hmatch(): * return True if the pattern matches the prefix */protected intc_hmatch(EditLine *el, const char *str){#ifdef SDEBUG (void) fprintf(el->el_errfile, "match `%s' with `%s'\n", el->el_search.patbuf, str);#endif /* SDEBUG */ return (el_match(str, el->el_search.patbuf));}/* c_setpat(): * Set the history seatch pattern */protected voidc_setpat(EditLine *el){ if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY && el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) { el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer; if (el->el_search.patlen >= EL_BUFSIZ) el->el_search.patlen = EL_BUFSIZ - 1; if (el->el_search.patlen != 0) { (void) strncpy(el->el_search.patbuf, el->el_line.buffer, el->el_search.patlen); el->el_search.patbuf[el->el_search.patlen] = '\0'; } else el->el_search.patlen = strlen(el->el_search.patbuf); }#ifdef SDEBUG (void) fprintf(el->el_errfile, "\neventno = %d\n", el->el_history.eventno); (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen); (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n", el->el_search.patbuf); (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n", EL_CURSOR(el) - el->el_line.buffer, el->el_line.lastchar - el->el_line.buffer);#endif}/* ce_inc_search(): * Emacs incremental search */protected el_action_tce_inc_search(EditLine *el, int dir){ static const char STRfwd[] = {'f', 'w', 'd', '\0'}, STRbck[] = {'b', 'c', 'k', '\0'}; static char pchar = ':';/* ':' = normal, '?' = failed */ static char endcmd[2] = {'\0', '\0'}; char ch, *ocursor = el->el_line.cursor, oldpchar = pchar; const char *cp; el_action_t ret = CC_NORM; int ohisteventno = el->el_history.eventno; int oldpatlen = el->el_search.patlen; int newdir = dir; int done, redo; if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 + el->el_search.patlen >= el->el_line.limit) return (CC_ERROR); for (;;) { if (el->el_search.patlen == 0) { /* first round */ pchar = ':';#ifdef ANCHOR el->el_search.patbuf[el->el_search.patlen++] = '.'; el->el_search.patbuf[el->el_search.patlen++] = '*';#endif } done = redo = 0; *el->el_line.lastchar++ = '\n'; for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd; *cp; *el->el_line.lastchar++ = *cp++) continue; *el->el_line.lastchar++ = pchar; for (cp = &el->el_search.patbuf[1]; cp < &el->el_search.patbuf[el->el_search.patlen]; *el->el_line.lastchar++ = *cp++) continue; *el->el_line.lastchar = '\0'; re_refresh(el); if (el_getc(el, &ch) != 1) return (ed_end_of_file(el, 0)); switch (el->el_map.current[(unsigned char) ch]) { case ED_INSERT: case ED_DIGIT: if (el->el_search.patlen > EL_BUFSIZ - 3) term_beep(el); else { el->el_search.patbuf[el->el_search.patlen++] = ch; *el->el_line.lastchar++ = ch; *el->el_line.lastchar = '\0'; re_refresh(el); } break; case EM_INC_SEARCH_NEXT: newdir = ED_SEARCH_NEXT_HISTORY; redo++; break; case EM_INC_SEARCH_PREV: newdir = ED_SEARCH_PREV_HISTORY; redo++; break; case ED_DELETE_PREV_CHAR: if (el->el_search.patlen > 1) done++; else term_beep(el); break; default: switch (ch) { case 0007: /* ^G: Abort */ ret = CC_ERROR; done++; break; case 0027: /* ^W: Append word */ /* No can do if globbing characters in pattern */ for (cp = &el->el_search.patbuf[1];; cp++) if (cp >= &el->el_search.patbuf[el->el_search.patlen]) { el->el_line.cursor += el->el_search.patlen - 1; cp = c__next_word(el->el_line.cursor, el->el_line.lastchar, 1, ce__isword); while (el->el_line.cursor < cp && *el->el_line.cursor != '\n') { if (el->el_search.patlen > EL_BUFSIZ - 3) { term_beep(el); break; } el->el_search.patbuf[el->el_search.patlen++] = *el->el_line.cursor; *el->el_line.lastchar++ = *el->el_line.cursor++; } el->el_line.cursor = ocursor; *el->el_line.lastchar = '\0'; re_refresh(el); break; } else if (isglob(*cp)) { term_beep(el); break; } break; default: /* Terminate and execute cmd */ endcmd[0] = ch; el_push(el, endcmd); /* FALLTHROUGH */ case 0033: /* ESC: Terminate */ ret = CC_REFRESH; done++; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -