[9] | 1 | /* ELLE - Copyright 1982, 1984, 1987 by Ken Harrenstien, SRI International
|
---|
| 2 | * This software is quasi-public; it may be used freely with
|
---|
| 3 | * like software, but may NOT be sold or made part of licensed
|
---|
| 4 | * products without permission of the author.
|
---|
| 5 | */
|
---|
| 6 | /*
|
---|
| 7 | * EESRCH Searching functions
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "elle.h"
|
---|
| 11 | #if !(V6)
|
---|
| 12 | #include <signal.h>
|
---|
| 13 | #else
|
---|
| 14 | #include "eesigs.h" /* Use this on V6 system */
|
---|
| 15 | #endif /*V6*/
|
---|
| 16 |
|
---|
| 17 | /*
|
---|
| 18 | * Buffer String Search routines
|
---|
| 19 | *
|
---|
| 20 | * If no search string is provided, a string that was previously
|
---|
| 21 | * used in the last search is once again used.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | /* EFUN: "String Search" */
|
---|
| 25 | f_srch()
|
---|
| 26 | { return (lin_search (0));
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | /* EFUN: "Reverse String Search" */
|
---|
| 30 | f_rsrch()
|
---|
| 31 | { return (lin_search (1));
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /* LIN_SEARCH - Main routine for non-incremental String Search. Asks for
|
---|
| 35 | * a search string and looks for it.
|
---|
| 36 | */
|
---|
| 37 | lin_search (backwards)
|
---|
| 38 | int backwards;
|
---|
| 39 | { register char *mem; /* item to be searched for */
|
---|
| 40 | register int res;
|
---|
| 41 | int srchint(), (*sav_srchalarm)();
|
---|
| 42 | char *srch_ask();
|
---|
| 43 | chroff savdot;
|
---|
| 44 |
|
---|
| 45 | savdot = cur_dot; /* Save original loc */
|
---|
| 46 |
|
---|
| 47 | #if ICONOGRAPHICS
|
---|
| 48 | if((mem = srch_ask(backwards ? "Reverse Search%s%s%s"
|
---|
| 49 | : "Search%s%s%s"))==0)
|
---|
| 50 | return;
|
---|
| 51 | #else
|
---|
| 52 | if((mem = srch_ask(backwards ? "Reverse Search: " : "Search: "))==0)
|
---|
| 53 | return;
|
---|
| 54 | #endif /*-ICONOGRAPHICS*/
|
---|
| 55 | sav_srchalarm = signal(SIGALRM,/*&*/srchint); /* Handle timeout */
|
---|
| 56 | alarm(1); /* One sec from now */
|
---|
| 57 |
|
---|
| 58 | res = e_search(mem,srch_len,backwards); /* Search for str! */
|
---|
| 59 |
|
---|
| 60 | alarm(0); /* Turn off alarm */
|
---|
| 61 | signal(SIGALRM,sav_srchalarm); /* Restore old handler */
|
---|
| 62 |
|
---|
| 63 | if(res) /* Search won? */
|
---|
| 64 | { ed_setcur();
|
---|
| 65 | return;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /* Search failed */
|
---|
| 69 | e_gosetcur(savdot);
|
---|
| 70 | ding("Search Failed");
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | srchint()
|
---|
| 74 | { yelltoo(" ...");
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | char *
|
---|
| 78 | srch_ask(prompt)
|
---|
| 79 | char *prompt;
|
---|
| 80 | { register char *ans, *old;
|
---|
| 81 |
|
---|
| 82 | #if ICONOGRAPHICS
|
---|
| 83 | if (srch_str)
|
---|
| 84 | ans = ask(prompt, " (", srch_str, "): ");
|
---|
| 85 | else ans = ask (prompt, ": ", "", "");
|
---|
| 86 | if (ans == 0) return (0);
|
---|
| 87 | #else
|
---|
| 88 | if((ans = ask(prompt)) == 0)
|
---|
| 89 | return(0); /* user punted ... */
|
---|
| 90 | #endif /*-ICONOGRAPHICS*/
|
---|
| 91 | old = srch_str;
|
---|
| 92 | if (*ans == '\0')
|
---|
| 93 | { chkfree(ans);
|
---|
| 94 | if ((ans = old) == 0) /* no string specified */
|
---|
| 95 | { dingtoo("Nothing to search for");
|
---|
| 96 | return(0);
|
---|
| 97 | }
|
---|
| 98 | #if !(ICONOGRAPHICS)
|
---|
| 99 | saylntoo(old, srch_len); /* Show what old string is */
|
---|
| 100 | #endif /*-ICONOGRAPHICS*/
|
---|
| 101 | }
|
---|
| 102 | else
|
---|
| 103 | { if (old)
|
---|
| 104 | chkfree(old); /* free up old srch string */
|
---|
| 105 | srch_str = ans;
|
---|
| 106 | srch_len = ask_len;
|
---|
| 107 | }
|
---|
| 108 | return(ans);
|
---|
| 109 | }
|
---|
| 110 | |
---|
| 111 |
|
---|
| 112 | #if 0
|
---|
| 113 | Incremental Search stuff.
|
---|
| 114 | Description of EMACS behavior:
|
---|
| 115 | ^Q quotes next char.
|
---|
| 116 | DEL cancels last char. If this cancelled a match, point is moved
|
---|
| 117 | to previous match.
|
---|
| 118 | If not all of input can be found, it is not discarded. Can rub out,
|
---|
| 119 | discard unmatched stuff with ^G, exit, etc.
|
---|
| 120 | ^S repeats search forward; ^R repeats backward.
|
---|
| 121 | If empty string, either
|
---|
| 122 | changes direction (if not same)
|
---|
| 123 | or brings back previous string
|
---|
| 124 | ESC exits. If empty string, changes to non-incremental string search.
|
---|
| 125 | ^G of a winning search aborts, exits, and moves point back to origin.
|
---|
| 126 | ^G of a failing search discards the input that wasn''t found.
|
---|
| 127 | Other C- or M- chars exit and are executed.
|
---|
| 128 | ELLE also interprets ^H (BS) as DEL, because some keyboards make it hard to
|
---|
| 129 | type DEL and there is no way the user can
|
---|
| 130 | re-bind the incremental-search commands.
|
---|
| 131 | #endif /*COMMENT*/
|
---|
| 132 |
|
---|
| 133 | #if FX_ISRCH
|
---|
| 134 | /* EFUN: "Incremental Search" */
|
---|
| 135 | f_isrch() { i_search(0); }
|
---|
| 136 | #endif /*FX_ISRCH*/
|
---|
| 137 |
|
---|
| 138 | #if FX_RISRCH
|
---|
| 139 | /* EFUN: "Reverse Search" */
|
---|
| 140 | f_risrch() { i_search(1); }
|
---|
| 141 | #endif /*FX_RISRCH*/
|
---|
| 142 |
|
---|
| 143 | #if FX_ISRCH || FX_RISRCH
|
---|
| 144 |
|
---|
| 145 | i_search(back)
|
---|
| 146 | int back; /* Current mode: 0 if forward, 1 if backward */
|
---|
| 147 | { register int c;
|
---|
| 148 | register int inpcnt; /* # chars in current input srch str */
|
---|
| 149 | int inpgood; /* Length of last winning string */
|
---|
| 150 | char inpstr[ISRCHLIM]; /* Holds current input search string */
|
---|
| 151 | chroff inpdot[ISRCHLIM]; /* Holds winning addrs for each */
|
---|
| 152 | struct window *savwin;
|
---|
| 153 | int winning; /* 1 = currently winning, 0 = currently failing */
|
---|
| 154 | int pref, shown;
|
---|
| 155 | int f_insself(), (*(cmd_fun()))();
|
---|
| 156 |
|
---|
| 157 | winning = 1;
|
---|
| 158 | inpcnt = 0;
|
---|
| 159 | inpgood = 0;
|
---|
| 160 | inpdot[0] = cur_dot;
|
---|
| 161 | savwin = cur_win;
|
---|
| 162 |
|
---|
| 163 | /* Set up prompt and read all TTY input thus far */
|
---|
| 164 | shown = 0;
|
---|
| 165 | sloop: c = cmd_wait(); /* See if any command input waiting */
|
---|
| 166 | if(shown || !c)
|
---|
| 167 | { e_setcur(); /* Assume we moved around, so set cur_dot */
|
---|
| 168 | chg_win(ask_win);
|
---|
| 169 | ed_reset(); /* Flush contents & invoke redisplay */
|
---|
| 170 | ed_sins(back ? "R-search: " : "I-search: ");
|
---|
| 171 | ed_nsins(inpstr, inpcnt);
|
---|
| 172 | if(!winning) ed_sins("\t(FAILING)");
|
---|
| 173 | upd_wind((struct window *)0); /* Force ask_win update */
|
---|
| 174 | if(c)
|
---|
| 175 | { upd_curs(cur_dot);
|
---|
| 176 | tbufls();
|
---|
| 177 | }
|
---|
| 178 | chg_win(savwin);
|
---|
| 179 | shown = 1; /* Say search prompt has been shown */
|
---|
| 180 | }
|
---|
| 181 | if(!c) /* If no user input waiting, show buffer */
|
---|
| 182 | { redp(RD_MOVE); /* Cursor moved in window */
|
---|
| 183 | redisplay();
|
---|
| 184 | }
|
---|
| 185 | c = cmd_read(); /* Get input char */
|
---|
| 186 | switch(c)
|
---|
| 187 | { case DEL: /* Cancel last char */
|
---|
| 188 | case BS: /* Hard to type DEL on some kbds */
|
---|
| 189 | if(inpcnt <= 0) goto sloop;
|
---|
| 190 | if(--inpcnt > inpgood) goto sloop;
|
---|
| 191 | winning = 1;
|
---|
| 192 | if(inpcnt == inpgood) goto sloop;
|
---|
| 193 | inpgood--;
|
---|
| 194 | ed_go(inpdot[inpcnt]);
|
---|
| 195 | goto sloop;
|
---|
| 196 |
|
---|
| 197 | case CTRL('Q'):
|
---|
| 198 | c = cmd_read(); /* Quote next char */
|
---|
| 199 | break;
|
---|
| 200 | case CTRL('S'):
|
---|
| 201 | pref = 0;
|
---|
| 202 | goto ctlsr;
|
---|
| 203 | case CTRL('R'):
|
---|
| 204 | pref = 1;
|
---|
| 205 | goto ctlsr;
|
---|
| 206 |
|
---|
| 207 | case CTRL('G'):
|
---|
| 208 | if(winning)
|
---|
| 209 | { ed_go(inpdot[0]);
|
---|
| 210 | goto sdone;
|
---|
| 211 | }
|
---|
| 212 | inpcnt = inpgood;
|
---|
| 213 | winning = 1;
|
---|
| 214 | goto sloop;
|
---|
| 215 | case ESC:
|
---|
| 216 | case CR:
|
---|
| 217 | if(inpcnt)
|
---|
| 218 | goto sdone;
|
---|
| 219 | lin_search(back);
|
---|
| 220 | return;
|
---|
| 221 | default:
|
---|
| 222 | if(f_insself != cmd_fun(c))
|
---|
| 223 | { unrchf = c;
|
---|
| 224 | goto sdone;
|
---|
| 225 | }
|
---|
| 226 | case TAB: /* Strange self-inserting char */
|
---|
| 227 | break;
|
---|
| 228 | }
|
---|
| 229 | if(inpcnt >= ISRCHLIM-1)
|
---|
| 230 | { ding("I-search str too long");
|
---|
| 231 | sleep(1);
|
---|
| 232 | goto sdone;
|
---|
| 233 | }
|
---|
| 234 | inpstr[inpcnt++] = c;
|
---|
| 235 | if(!winning) goto sloop;
|
---|
| 236 |
|
---|
| 237 | /* Now search for string. (Arm alarm interrupt?) */
|
---|
| 238 | /* cur_dot has current location cursor is at; we want to back off
|
---|
| 239 | * from this so a repeated search will find the same location if
|
---|
| 240 | * appropriate. */
|
---|
| 241 | e_igoff(back ? inpcnt : -(inpcnt-1));
|
---|
| 242 | dosrch:
|
---|
| 243 | winning = e_search(inpstr,inpcnt,back);
|
---|
| 244 | if (winning)
|
---|
| 245 | { inpgood = inpcnt; /* Remember last win length */
|
---|
| 246 | inpdot[inpcnt] = e_dot(); /* and location */
|
---|
| 247 | }
|
---|
| 248 | else e_gocur(); /* Back to start position */
|
---|
| 249 | goto sloop;
|
---|
| 250 |
|
---|
| 251 | ctlsr: if (pref != back)
|
---|
| 252 | { back = pref;
|
---|
| 253 | if(inpcnt <= 0) goto sloop;
|
---|
| 254 | }
|
---|
| 255 | if(inpcnt <= 0)
|
---|
| 256 | { if(!srch_str || (inpcnt = srch_len) <= 0)
|
---|
| 257 | goto sloop;
|
---|
| 258 | bcopy((SBMA)srch_str, (SBMA)inpstr, srch_len);
|
---|
| 259 | inpcnt = srch_len;
|
---|
| 260 | unrchf = c; /* Repeat cmd after display */
|
---|
| 261 | shown = 1; /* Force search-string display */
|
---|
| 262 | goto sloop;
|
---|
| 263 | }
|
---|
| 264 | goto dosrch;
|
---|
| 265 |
|
---|
| 266 | sdone:
|
---|
| 267 | if(srch_str) chkfree(srch_str);
|
---|
| 268 | srch_str = memalloc((SBMO)(inpcnt+1));
|
---|
| 269 | bcopy((SBMA)inpstr,(SBMA)srch_str,inpcnt); /* Copy into srch_str */
|
---|
| 270 | srch_len = inpcnt;
|
---|
| 271 | e_setcur();
|
---|
| 272 | chg_win(ask_win);
|
---|
| 273 | ed_reset();
|
---|
| 274 | chg_win(savwin);
|
---|
| 275 | redp(RD_CHKALL);
|
---|
| 276 | }
|
---|
| 277 | #endif /*FX_ISRCH || FX_RISRCH*/
|
---|