2013-05-20 01:16:08 -04:00
|
|
|
#include "isearch.h"
|
|
|
|
|
2010-11-14 21:10:03 -05:00
|
|
|
/* isearch.c
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
|
|
|
* The functions in this file implement commands that perform incremental
|
|
|
|
* searches in the forward and backward directions. This "ISearch" command
|
2010-01-29 19:17:02 -05:00
|
|
|
* is intended to emulate the same command from the original EMACS
|
2005-05-31 11:50:56 -04:00
|
|
|
* implementation (ITS). Contains references to routines internal to
|
|
|
|
* SEARCH.C.
|
|
|
|
*
|
|
|
|
* REVISION HISTORY:
|
|
|
|
*
|
|
|
|
* D. R. Banks 9-May-86
|
|
|
|
* - added ITS EMACSlike ISearch
|
|
|
|
*
|
|
|
|
* John M. Gamble 5-Oct-86
|
|
|
|
* - Made iterative search use search.c's scanner() routine.
|
|
|
|
* This allowed the elimination of bakscan().
|
|
|
|
* - Put isearch constants into estruct.h
|
|
|
|
* - Eliminated the passing of 'status' to scanmore() and
|
|
|
|
* checknext(), since there were no circumstances where
|
|
|
|
* it ever equalled FALSE.
|
|
|
|
*
|
2010-11-14 21:10:03 -05:00
|
|
|
* Modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2010-11-14 21:10:03 -05:00
|
|
|
#include <stdio.h>
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <string.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
|
2013-05-28 03:10:16 -04:00
|
|
|
#include "basic.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "buffer.h"
|
2013-05-28 02:57:03 -04:00
|
|
|
#include "display.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "estruct.h"
|
2013-10-09 21:55:03 -04:00
|
|
|
#include "exec.h"
|
2013-05-28 02:46:44 -04:00
|
|
|
#include "input.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "line.h"
|
2013-05-28 00:22:31 -04:00
|
|
|
#include "search.h"
|
2013-09-25 09:45:05 -04:00
|
|
|
#include "terminal.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "window.h"
|
2013-05-30 02:36:11 -04:00
|
|
|
|
2013-09-23 08:55:59 -04:00
|
|
|
/*
|
|
|
|
* Incremental search defines.
|
|
|
|
*/
|
|
|
|
#if ISRCH
|
|
|
|
|
|
|
|
#define CMDBUFLEN 256 /* Length of our command buffer */
|
|
|
|
|
|
|
|
#define IS_ABORT 0x07 /* Abort the isearch */
|
|
|
|
#define IS_BACKSP 0x08 /* Delete previous char */
|
|
|
|
#define IS_TAB 0x09 /* Tab character (allowed search char) */
|
|
|
|
#define IS_NEWLINE 0x0D /* New line from keyboard (Carriage return) */
|
|
|
|
#define IS_QUOTE 0x11 /* Quote next character */
|
|
|
|
#define IS_REVERSE 0x12 /* Search backward */
|
|
|
|
#define IS_FORWARD 0x13 /* Search forward */
|
|
|
|
#define IS_VMSQUOTE 0x16 /* VMS quote character */
|
|
|
|
#define IS_VMSFORW 0x18 /* Search forward for VMS */
|
|
|
|
#define IS_QUIT 0x1B /* Exit the search */
|
|
|
|
#define IS_RUBOUT 0x7F /* Delete previous character */
|
|
|
|
|
|
|
|
/* IS_QUIT is no longer used, the variable metac is used instead */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-05-30 02:36:11 -04:00
|
|
|
static int isearch( int f, int n) ;
|
|
|
|
static int checknext( char chr, char *patrn, int dir) ;
|
|
|
|
static int scanmore( char *patrn, int dir) ;
|
|
|
|
static int match_pat( char *patrn) ;
|
|
|
|
static int promptpattern( char *prompt) ;
|
|
|
|
static int get_char( void) ;
|
|
|
|
static int uneat( void) ;
|
|
|
|
static void reeat( int c) ;
|
|
|
|
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
#if ISRCH
|
|
|
|
|
2005-10-01 03:22:55 -04:00
|
|
|
static int echo_char(int c, int col);
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* A couple of "own" variables for re-eat */
|
|
|
|
|
2010-02-27 05:38:24 -05:00
|
|
|
static int (*saved_get_char) (void); /* Get character routine */
|
2010-01-29 19:17:02 -05:00
|
|
|
static int eaten_char = -1; /* Re-eaten char */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* A couple more "own" variables for the command string */
|
|
|
|
|
2010-01-29 19:17:02 -05:00
|
|
|
static int cmd_buff[CMDBUFLEN]; /* Save the command args here */
|
|
|
|
static int cmd_offset; /* Current offset into command buff */
|
|
|
|
static int cmd_reexecute = -1; /* > 0 if re-executing command */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Subroutine to do incremental reverse search. It actually uses the
|
|
|
|
* same code as the normal incremental search, as both can go both ways.
|
|
|
|
*/
|
2005-10-01 01:52:45 -04:00
|
|
|
int risearch(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* Current line on entry */
|
2005-09-30 18:26:09 -04:00
|
|
|
int curoff; /* Current offset on entry */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* remember the initial . on entry: */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
curline = curwp->w_dotp; /* Save the current line pointer */
|
|
|
|
curoff = curwp->w_doto; /* Save the current offset */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* Make sure the search doesn't match where we already are: */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
backchar(TRUE, 1); /* Back up a character */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!(isearch(f, -n))) { /* Call ISearch backwards *//* If error in search: */
|
|
|
|
curwp->w_dotp = curline; /* Reset the line pointer */
|
|
|
|
curwp->w_doto = curoff; /* and the offset to original value */
|
|
|
|
curwp->w_flag |= WFMOVE; /* Say we've moved */
|
|
|
|
update(FALSE); /* And force an update */
|
|
|
|
mlwrite("(search failed)"); /* Say we died */
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
matchlen = strlen(pat);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
|
|
|
mlerase(); /* If happy, just erase the cmd line */
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
|
|
|
matchlen = strlen(pat);
|
|
|
|
#endif
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* Again, but for the forward direction
|
|
|
|
*/
|
|
|
|
int fisearch(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* Current line on entry */
|
2005-09-30 18:26:09 -04:00
|
|
|
int curoff; /* Current offset on entry */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* remember the initial . on entry: */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
curline = curwp->w_dotp; /* Save the current line pointer */
|
|
|
|
curoff = curwp->w_doto; /* Save the current offset */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* do the search */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!(isearch(f, n))) { /* Call ISearch forwards *//* If error in search: */
|
|
|
|
curwp->w_dotp = curline; /* Reset the line pointer */
|
|
|
|
curwp->w_doto = curoff; /* and the offset to original value */
|
|
|
|
curwp->w_flag |= WFMOVE; /* Say we've moved */
|
|
|
|
update(FALSE); /* And force an update */
|
|
|
|
mlwrite("(search failed)"); /* Say we died */
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
matchlen = strlen(pat);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
|
|
|
mlerase(); /* If happy, just erase the cmd line */
|
2005-05-31 11:50:56 -04:00
|
|
|
#if PKCODE
|
|
|
|
matchlen = strlen(pat);
|
|
|
|
#endif
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Subroutine to do an incremental search. In general, this works similarly
|
|
|
|
* to the older micro-emacs search function, except that the search happens
|
|
|
|
* as each character is typed, with the screen and cursor updated with each
|
|
|
|
* new search character.
|
|
|
|
*
|
|
|
|
* While searching forward, each successive character will leave the cursor
|
|
|
|
* at the end of the entire matched string. Typing a Control-S or Control-X
|
|
|
|
* will cause the next occurrence of the string to be searched for (where the
|
|
|
|
* next occurrence does NOT overlap the current occurrence). A Control-R will
|
|
|
|
* change to a backwards search, META will terminate the search and Control-G
|
|
|
|
* will abort the search. Rubout will back up to the previous match of the
|
|
|
|
* string, or if the starting point is reached first, it will delete the
|
|
|
|
* last character from the search string.
|
|
|
|
*
|
|
|
|
* While searching backward, each successive character will leave the cursor
|
|
|
|
* at the beginning of the matched string. Typing a Control-R will search
|
|
|
|
* backward for the next occurrence of the string. Control-S or Control-X
|
|
|
|
* will revert the search to the forward direction. In general, the reverse
|
|
|
|
* incremental search is just like the forward incremental search inverted.
|
|
|
|
*
|
|
|
|
* In all cases, if the search fails, the user will be feeped, and the search
|
|
|
|
* will stall until the pattern string is edited back into something that
|
|
|
|
* exists (or until the search is aborted).
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2013-05-30 02:36:11 -04:00
|
|
|
static int isearch(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
int status; /* Search status */
|
|
|
|
int col; /* prompt column */
|
2015-02-28 07:29:00 -05:00
|
|
|
unsigned cpos ; /* character number in search string */
|
2010-02-27 05:38:22 -05:00
|
|
|
int c; /* current input character */
|
|
|
|
int expc; /* function expanded input char */
|
2014-06-02 03:16:18 -04:00
|
|
|
spat_t pat_save ; /* Saved copy of the old pattern str */
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* Current line on entry */
|
2005-09-30 18:26:09 -04:00
|
|
|
int curoff; /* Current offset on entry */
|
|
|
|
int init_direction; /* The initial search direction */
|
|
|
|
|
|
|
|
/* Initialize starting conditions */
|
|
|
|
|
|
|
|
cmd_reexecute = -1; /* We're not re-executing (yet?) */
|
|
|
|
cmd_offset = 0; /* Start at the beginning of the buff */
|
|
|
|
cmd_buff[0] = '\0'; /* Init the command buffer */
|
2014-06-02 03:16:18 -04:00
|
|
|
strncpy( pat_save, pat, sizeof pat_save) ; /* Save the old pattern string */
|
2005-09-30 18:26:09 -04:00
|
|
|
curline = curwp->w_dotp; /* Save the current line pointer */
|
|
|
|
curoff = curwp->w_doto; /* Save the current offset */
|
|
|
|
init_direction = n; /* Save the initial search direction */
|
|
|
|
|
|
|
|
/* This is a good place to start a re-execution: */
|
|
|
|
|
|
|
|
start_over:
|
|
|
|
|
|
|
|
/* ask the user for the text of a pattern */
|
|
|
|
col = promptpattern("ISearch: "); /* Prompt, remember the col */
|
|
|
|
|
|
|
|
cpos = 0; /* Start afresh */
|
|
|
|
status = TRUE; /* Assume everything's cool */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the first character in the pattern. If we get an initial Control-S
|
|
|
|
or Control-R, re-use the old search string and find the first occurrence
|
|
|
|
*/
|
|
|
|
|
|
|
|
c = ectoc(expc = get_char()); /* Get the first character */
|
|
|
|
if ((c == IS_FORWARD) || (c == IS_REVERSE) || (c == IS_VMSFORW)) { /* Reuse old search string? */
|
|
|
|
for (cpos = 0; pat[cpos] != 0; cpos++) /* Yup, find the length */
|
2005-10-01 03:22:55 -04:00
|
|
|
col = echo_char(pat[cpos], col); /* and re-echo the string */
|
2005-09-30 18:26:09 -04:00
|
|
|
if (c == IS_REVERSE) { /* forward search? */
|
|
|
|
n = -1; /* No, search in reverse */
|
|
|
|
backchar(TRUE, 1); /* Be defensive about EOB */
|
|
|
|
} else
|
|
|
|
n = 1; /* Yes, search forward */
|
|
|
|
status = scanmore(pat, n); /* Do the search */
|
|
|
|
c = ectoc(expc = get_char()); /* Get another character */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
/* Top of the per character loop */
|
|
|
|
|
|
|
|
for (;;) { /* ISearch per character loop */
|
|
|
|
/* Check for special characters first: */
|
|
|
|
/* Most cases here change the search */
|
|
|
|
|
|
|
|
if (expc == metac) /* Want to quit searching? */
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE; /* Quit searching now */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
switch (c) { /* dispatch on the input char */
|
|
|
|
case IS_ABORT: /* If abort search request */
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE; /* Quit searching again */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
case IS_REVERSE: /* If backward search */
|
|
|
|
case IS_FORWARD: /* If forward search */
|
|
|
|
case IS_VMSFORW: /* of either flavor */
|
|
|
|
if (c == IS_REVERSE) /* If reverse search */
|
|
|
|
n = -1; /* Set the reverse direction */
|
|
|
|
else /* Otherwise, */
|
|
|
|
n = 1; /* go forward */
|
|
|
|
status = scanmore(pat, n); /* Start the search again */
|
|
|
|
c = ectoc(expc = get_char()); /* Get the next char */
|
|
|
|
continue; /* Go continue with the search */
|
|
|
|
|
|
|
|
case IS_NEWLINE: /* Carriage return */
|
|
|
|
c = '\n'; /* Make it a new line */
|
|
|
|
break; /* Make sure we use it */
|
|
|
|
|
|
|
|
case IS_QUOTE: /* Quote character */
|
|
|
|
case IS_VMSQUOTE: /* of either variety */
|
|
|
|
c = ectoc(expc = get_char()); /* Get the next char */
|
|
|
|
|
|
|
|
case IS_TAB: /* Generically allowed */
|
|
|
|
case '\n': /* controlled characters */
|
|
|
|
break; /* Make sure we use it */
|
|
|
|
|
|
|
|
case IS_BACKSP: /* If a backspace: */
|
|
|
|
case IS_RUBOUT: /* or if a Rubout: */
|
|
|
|
if (cmd_offset <= 1) /* Anything to delete? */
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE; /* No, just exit */
|
2005-09-30 18:26:09 -04:00
|
|
|
--cmd_offset; /* Back up over the Rubout */
|
|
|
|
cmd_buff[--cmd_offset] = '\0'; /* Yes, delete last char */
|
|
|
|
curwp->w_dotp = curline; /* Reset the line pointer */
|
|
|
|
curwp->w_doto = curoff; /* and the offset */
|
|
|
|
n = init_direction; /* Reset the search direction */
|
2014-06-02 03:16:18 -04:00
|
|
|
strncpy( pat, pat_save, sizeof pat) ; /* Restore the old search str */
|
2005-09-30 18:26:09 -04:00
|
|
|
cmd_reexecute = 0; /* Start the whole mess over */
|
|
|
|
goto start_over; /* Let it take care of itself */
|
|
|
|
|
|
|
|
/* Presumably a quasi-normal character comes here */
|
|
|
|
|
|
|
|
default: /* All other chars */
|
|
|
|
if (c < ' ') { /* Is it printable? *//* Nope. */
|
|
|
|
reeat(c); /* Re-eat the char */
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE; /* And return the last status */
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
} /* Switch */
|
|
|
|
|
|
|
|
/* I guess we got something to search for, so search for it */
|
|
|
|
|
|
|
|
pat[cpos++] = c; /* put the char in the buffer */
|
2014-06-02 03:16:18 -04:00
|
|
|
if (cpos >= sizeof pat) { /* too many chars in string? */
|
|
|
|
/* Yup. Complain about it */
|
2005-09-30 18:26:09 -04:00
|
|
|
mlwrite("? Search string too long");
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE; /* Return an error */
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
pat[cpos] = 0; /* null terminate the buffer */
|
2005-10-01 03:22:55 -04:00
|
|
|
col = echo_char(c, col); /* Echo the character */
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!status) { /* If we lost last time */
|
2015-01-21 01:48:03 -05:00
|
|
|
TTbeep() ; /* Feep again */
|
2005-09-30 18:26:09 -04:00
|
|
|
TTflush(); /* see that the feep feeps */
|
|
|
|
} else /* Otherwise, we must have won */ if (!(status = checknext(c, pat, n))) /* See if match */
|
|
|
|
status = scanmore(pat, n); /* or find the next match */
|
|
|
|
c = ectoc(expc = get_char()); /* Get the next char */
|
|
|
|
} /* for {;;} */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trivial routine to insure that the next character in the search string is
|
|
|
|
* still true to whatever we're pointing to in the buffer. This routine will
|
|
|
|
* not attempt to move the "point" if the match fails, although it will
|
|
|
|
* implicitly move the "point" if we're forward searching, and find a match,
|
|
|
|
* since that's the way forward isearch works.
|
|
|
|
*
|
|
|
|
* If the compare fails, we return FALSE and assume the caller will call
|
|
|
|
* scanmore or something.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* char chr; Next char to look for
|
|
|
|
* char *patrn; The entire search string (incl chr)
|
|
|
|
* int dir; Search direction
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2013-05-30 02:36:11 -04:00
|
|
|
static int checknext(char chr, char *patrn, int dir) /* Check next character in search string */
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* current line during scan */
|
2010-02-27 05:38:22 -05:00
|
|
|
int curoff; /* position within current line */
|
|
|
|
int buffchar; /* character at current position */
|
2005-09-30 18:26:09 -04:00
|
|
|
int status; /* how well things go */
|
|
|
|
|
|
|
|
|
|
|
|
/* setup the local scan pointer to current "." */
|
|
|
|
|
2005-10-01 03:56:15 -04:00
|
|
|
curline = curwp->w_dotp; /* Get the current line structure */
|
|
|
|
curoff = curwp->w_doto; /* Get the offset within that line */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-10-01 03:56:15 -04:00
|
|
|
if (dir > 0) { /* If searching forward */
|
|
|
|
if (curoff == llength(curline)) { /* If at end of line */
|
|
|
|
curline = lforw(curline); /* Skip to the next line */
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curline == curbp->b_linep)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE; /* Abort if at end of buffer */
|
2005-10-01 03:56:15 -04:00
|
|
|
curoff = 0; /* Start at the beginning of the line */
|
|
|
|
buffchar = '\n'; /* And say the next char is NL */
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
|
|
|
buffchar = lgetc(curline, curoff++); /* Get the next char */
|
2005-10-01 03:56:15 -04:00
|
|
|
if ((status = eq(buffchar, chr)) != 0) { /* Is it what we're looking for? */
|
|
|
|
curwp->w_dotp = curline; /* Yes, set the buffer's point */
|
|
|
|
curwp->w_doto = curoff; /* to the matched character */
|
|
|
|
curwp->w_flag |= WFMOVE; /* Say that we've moved */
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return status; /* And return the status */
|
2005-10-01 03:56:15 -04:00
|
|
|
} else /* Else, if reverse search: */
|
2010-08-29 06:03:55 -04:00
|
|
|
return match_pat(patrn); /* See if we're in the right place */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This hack will search for the next occurrence of <pat> in the buffer, either
|
|
|
|
* forward or backward. It is called with the status of the prior search
|
|
|
|
* attempt, so that it knows not to bother if it didn't work last time. If
|
|
|
|
* we can't find any more matches, "point" is left where it was before. If
|
|
|
|
* we do find a match, "point" will be at the end of the matched string for
|
|
|
|
* forward searches and at the beginning of the matched string for reverse
|
|
|
|
* searches.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* char *patrn; string to scan for
|
|
|
|
* int dir; direction to search
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2013-05-30 02:36:11 -04:00
|
|
|
static int scanmore(char *patrn, int dir) /* search forward or back for a pattern */
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
int sts; /* search status */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (dir < 0) { /* reverse search? */
|
|
|
|
rvstrcpy(tap, patrn); /* Put reversed string in tap */
|
2005-05-31 11:50:56 -04:00
|
|
|
sts = scanner(tap, REVERSE, PTBEG);
|
2005-09-30 18:26:09 -04:00
|
|
|
} else
|
2005-05-31 11:50:56 -04:00
|
|
|
sts = scanner(patrn, FORWARD, PTEND); /* Nope. Go forward */
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!sts) {
|
2015-01-21 01:48:03 -05:00
|
|
|
TTbeep() ; /* Feep if search fails */
|
2005-09-30 18:26:09 -04:00
|
|
|
TTflush(); /* see that the feep feeps */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return sts; /* else, don't even try */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following is a worker subroutine used by the reverse search. It
|
|
|
|
* compares the pattern string with the characters at "." for equality. If
|
|
|
|
* any characters mismatch, it will return FALSE.
|
|
|
|
*
|
|
|
|
* This isn't used for forward searches, because forward searches leave "."
|
|
|
|
* at the end of the search string (instead of in front), so all that needs to
|
|
|
|
* be done is match the last char input.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
|
|
|
* char *patrn; String to match to buffer
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2013-05-30 02:36:11 -04:00
|
|
|
static int match_pat(char *patrn) /* See if the pattern string matches string at "." */
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2015-02-28 07:29:00 -05:00
|
|
|
unsigned i ; /* Generic loop index/offset */
|
2010-02-27 05:38:22 -05:00
|
|
|
int buffchar; /* character at current position */
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *curline; /* current line during scan */
|
2010-02-27 05:38:22 -05:00
|
|
|
int curoff; /* position within current line */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
/* setup the local scan pointer to current "." */
|
|
|
|
|
|
|
|
curline = curwp->w_dotp; /* Get the current line structure */
|
|
|
|
curoff = curwp->w_doto; /* Get the offset within that line */
|
|
|
|
|
|
|
|
/* top of per character compare loop: */
|
|
|
|
|
|
|
|
for (i = 0; i < strlen(patrn); i++) { /* Loop for all characters in patrn */
|
|
|
|
if (curoff == llength(curline)) { /* If at end of line */
|
|
|
|
curline = lforw(curline); /* Skip to the next line */
|
|
|
|
curoff = 0; /* Start at the beginning of the line */
|
|
|
|
if (curline == curbp->b_linep)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE; /* Abort if at end of buffer */
|
2005-09-30 18:26:09 -04:00
|
|
|
buffchar = '\n'; /* And say the next char is NL */
|
|
|
|
} else
|
|
|
|
buffchar = lgetc(curline, curoff++); /* Get the next char */
|
|
|
|
if (!eq(buffchar, patrn[i])) /* Is it what we're looking for? */
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE; /* Nope, just punt it then */
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE; /* Everything matched? Let's celebrate */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* Routine to prompt for I-Search string.
|
|
|
|
*/
|
2013-05-30 02:36:11 -04:00
|
|
|
static int promptpattern(char *prompt)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
char tpat[NPAT + 20];
|
|
|
|
|
|
|
|
strcpy(tpat, prompt); /* copy prompt to output string */
|
|
|
|
strcat(tpat, " ("); /* build new prompt string */
|
|
|
|
expandp(pat, &tpat[strlen(tpat)], NPAT / 2); /* add old pattern */
|
|
|
|
strcat(tpat, ")<Meta>: ");
|
|
|
|
|
|
|
|
/* check to see if we are executing a command line */
|
|
|
|
if (!clexec) {
|
|
|
|
mlwrite(tpat);
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return strlen(tpat);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* routine to echo i-search characters
|
|
|
|
*
|
|
|
|
* int c; character to be echoed
|
|
|
|
* int col; column to be echoed in
|
|
|
|
*/
|
2005-10-01 03:22:55 -04:00
|
|
|
static int echo_char(int c, int col)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
movecursor(term.t_nrow, col); /* Position the cursor */
|
|
|
|
if ((c < ' ') || (c == 0x7F)) { /* Control character? */
|
|
|
|
switch (c) { /* Yes, dispatch special cases */
|
|
|
|
case '\n': /* Newline */
|
|
|
|
TTputc('<');
|
|
|
|
TTputc('N');
|
|
|
|
TTputc('L');
|
|
|
|
TTputc('>');
|
|
|
|
col += 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\t': /* Tab */
|
|
|
|
TTputc('<');
|
|
|
|
TTputc('T');
|
|
|
|
TTputc('A');
|
|
|
|
TTputc('B');
|
|
|
|
TTputc('>');
|
|
|
|
col += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x7F: /* Rubout: */
|
|
|
|
TTputc('^'); /* Output a funny looking */
|
|
|
|
TTputc('?'); /* indication of Rubout */
|
|
|
|
col++; /* Count the extra char */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Vanilla control char */
|
|
|
|
TTputc('^'); /* Yes, output prefix */
|
|
|
|
TTputc(c + 0x40); /* Make it "^X" */
|
|
|
|
col++; /* Count this char */
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
TTputc(c); /* Otherwise, output raw char */
|
|
|
|
TTflush(); /* Flush the output */
|
2010-08-29 06:03:55 -04:00
|
|
|
return ++col; /* return the new column no */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routine to get the next character from the input stream. If we're reading
|
|
|
|
* from the real terminal, force a screen update before we get the char.
|
|
|
|
* Otherwise, we must be re-executing the command string, so just return the
|
|
|
|
* next character.
|
|
|
|
*/
|
2013-05-30 02:36:11 -04:00
|
|
|
static int get_char(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
int c; /* A place to get a character */
|
|
|
|
|
|
|
|
/* See if we're re-executing: */
|
|
|
|
|
|
|
|
if (cmd_reexecute >= 0) /* Is there an offset? */
|
|
|
|
if ((c = cmd_buff[cmd_reexecute++]) != 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return c; /* Yes, return any character */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
/* We're not re-executing (or aren't any more). Try for a real char */
|
|
|
|
|
|
|
|
cmd_reexecute = -1; /* Say we're in real mode again */
|
|
|
|
update(FALSE); /* Pretty up the screen */
|
|
|
|
if (cmd_offset >= CMDBUFLEN - 1) { /* If we're getting too big ... */
|
|
|
|
mlwrite("? command too long"); /* Complain loudly and bitterly */
|
2010-08-29 06:03:55 -04:00
|
|
|
return metac; /* And force a quit */
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
c = get1key(); /* Get the next character */
|
|
|
|
cmd_buff[cmd_offset++] = c; /* Save the char for next time */
|
|
|
|
cmd_buff[cmd_offset] = '\0'; /* And terminate the buffer */
|
2010-08-29 06:03:55 -04:00
|
|
|
return c; /* Return the character */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hacky routine to re-eat a character. This will save the character to be
|
|
|
|
* re-eaten by redirecting the input call to a routine here. Hack, etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Come here on the next term.t_getchar call: */
|
|
|
|
|
2013-05-30 02:36:11 -04:00
|
|
|
static int uneat(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
int c;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
term.t_getchar = saved_get_char; /* restore the routine address */
|
|
|
|
c = eaten_char; /* Get the re-eaten char */
|
|
|
|
eaten_char = -1; /* Clear the old char */
|
2010-08-29 06:03:55 -04:00
|
|
|
return c; /* and return the last char */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2013-05-30 02:36:11 -04:00
|
|
|
static void reeat(int c)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
if (eaten_char != -1) /* If we've already been here */
|
|
|
|
return /*(NULL) */ ; /* Don't do it again */
|
|
|
|
eaten_char = c; /* Else, save the char for later */
|
|
|
|
saved_get_char = term.t_getchar; /* Save the char get routine */
|
|
|
|
term.t_getchar = uneat; /* Replace it with ours */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
#else
|
2005-10-01 01:52:45 -04:00
|
|
|
int isearch(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|