isearch.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /* isearch.c - incremental searching */
  2. /* **************************************************************** */
  3. /* */
  4. /* I-Search and Searching */
  5. /* */
  6. /* **************************************************************** */
  7. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  8. This file is part of the GNU Readline Library (Readline), a library
  9. for reading lines of text with interactive input and history editing.
  10. Readline is free software: you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation, either version 3 of the License, or
  13. (at your option) any later version.
  14. Readline is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define READLINE_LIBRARY
  22. #if defined (HAVE_CONFIG_H)
  23. # include <config.h>
  24. #endif
  25. #include <sys/types.h>
  26. #include <stdio.h>
  27. #if defined (HAVE_UNISTD_H)
  28. # include <unistd.h>
  29. #endif
  30. #if defined (HAVE_STDLIB_H)
  31. # include <stdlib.h>
  32. #else
  33. # include "ansi_stdlib.h"
  34. #endif
  35. #include "rldefs.h"
  36. #include "rlmbutil.h"
  37. #include "readline.h"
  38. #include "history.h"
  39. #include "rlprivate.h"
  40. #include "xmalloc.h"
  41. /* Variables exported to other files in the readline library. */
  42. char *_rl_isearch_terminators = (char *)NULL;
  43. _rl_search_cxt *_rl_iscxt = 0;
  44. /* Variables imported from other files in the readline library. */
  45. extern HIST_ENTRY *_rl_saved_line_for_history;
  46. static int rl_search_history PARAMS((int, int));
  47. static _rl_search_cxt *_rl_isearch_init PARAMS((int));
  48. static void _rl_isearch_fini PARAMS((_rl_search_cxt *));
  49. static int _rl_isearch_cleanup PARAMS((_rl_search_cxt *, int));
  50. /* Last line found by the current incremental search, so we don't `find'
  51. identical lines many times in a row. Now part of isearch context. */
  52. /* static char *prev_line_found; */
  53. /* Last search string and its length. */
  54. static char *last_isearch_string;
  55. static int last_isearch_string_len;
  56. static char * const default_isearch_terminators = "\033\012";
  57. _rl_search_cxt *
  58. _rl_scxt_alloc (type, flags)
  59. int type, flags;
  60. {
  61. _rl_search_cxt *cxt;
  62. cxt = (_rl_search_cxt *)xmalloc (sizeof (_rl_search_cxt));
  63. cxt->type = type;
  64. cxt->sflags = flags;
  65. cxt->search_string = 0;
  66. cxt->search_string_size = cxt->search_string_index = 0;
  67. cxt->lines = 0;
  68. cxt->allocated_line = 0;
  69. cxt->hlen = cxt->hindex = 0;
  70. cxt->save_point = rl_point;
  71. cxt->save_mark = rl_mark;
  72. cxt->save_line = where_history ();
  73. cxt->last_found_line = cxt->save_line;
  74. cxt->prev_line_found = 0;
  75. cxt->save_undo_list = 0;
  76. cxt->keymap = _rl_keymap;
  77. cxt->okeymap = _rl_keymap;
  78. cxt->history_pos = 0;
  79. cxt->direction = 0;
  80. cxt->lastc = 0;
  81. cxt->sline = 0;
  82. cxt->sline_len = cxt->sline_index = 0;
  83. cxt->search_terminators = 0;
  84. return cxt;
  85. }
  86. void
  87. _rl_scxt_dispose (cxt, flags)
  88. _rl_search_cxt *cxt;
  89. int flags;
  90. {
  91. FREE (cxt->search_string);
  92. FREE (cxt->allocated_line);
  93. FREE (cxt->lines);
  94. xfree (cxt);
  95. }
  96. /* Search backwards through the history looking for a string which is typed
  97. interactively. Start with the current line. */
  98. int
  99. rl_reverse_search_history (sign, key)
  100. int sign, key;
  101. {
  102. return (rl_search_history (-sign, key));
  103. }
  104. /* Search forwards through the history looking for a string which is typed
  105. interactively. Start with the current line. */
  106. int
  107. rl_forward_search_history (sign, key)
  108. int sign, key;
  109. {
  110. return (rl_search_history (sign, key));
  111. }
  112. /* Display the current state of the search in the echo-area.
  113. SEARCH_STRING contains the string that is being searched for,
  114. DIRECTION is zero for forward, or non-zero for reverse,
  115. WHERE is the history list number of the current line. If it is
  116. -1, then this line is the starting one. */
  117. static void
  118. rl_display_search (search_string, reverse_p, where)
  119. char *search_string;
  120. int reverse_p, where;
  121. {
  122. char *message;
  123. int msglen, searchlen;
  124. searchlen = (search_string && *search_string) ? strlen (search_string) : 0;
  125. message = (char *)xmalloc (searchlen + 33);
  126. msglen = 0;
  127. #if defined (NOTDEF)
  128. if (where != -1)
  129. {
  130. sprintf (message, "[%d]", where + history_base);
  131. msglen = strlen (message);
  132. }
  133. #endif /* NOTDEF */
  134. message[msglen++] = '(';
  135. if (reverse_p)
  136. {
  137. strcpy (message + msglen, "reverse-");
  138. msglen += 8;
  139. }
  140. strcpy (message + msglen, "i-search)`");
  141. msglen += 10;
  142. if (search_string)
  143. {
  144. strcpy (message + msglen, search_string);
  145. msglen += searchlen;
  146. }
  147. strcpy (message + msglen, "': ");
  148. rl_message ("%s", message);
  149. xfree (message);
  150. (*rl_redisplay_function) ();
  151. }
  152. static _rl_search_cxt *
  153. _rl_isearch_init (direction)
  154. int direction;
  155. {
  156. _rl_search_cxt *cxt;
  157. register int i;
  158. HIST_ENTRY **hlist;
  159. cxt = _rl_scxt_alloc (RL_SEARCH_ISEARCH, 0);
  160. if (direction < 0)
  161. cxt->sflags |= SF_REVERSE;
  162. cxt->search_terminators = _rl_isearch_terminators ? _rl_isearch_terminators
  163. : default_isearch_terminators;
  164. /* Create an arrary of pointers to the lines that we want to search. */
  165. hlist = history_list ();
  166. rl_maybe_replace_line ();
  167. i = 0;
  168. if (hlist)
  169. for (i = 0; hlist[i]; i++);
  170. /* Allocate space for this many lines, +1 for the current input line,
  171. and remember those lines. */
  172. cxt->lines = (char **)xmalloc ((1 + (cxt->hlen = i)) * sizeof (char *));
  173. for (i = 0; i < cxt->hlen; i++)
  174. cxt->lines[i] = hlist[i]->line;
  175. if (_rl_saved_line_for_history)
  176. cxt->lines[i] = _rl_saved_line_for_history->line;
  177. else
  178. {
  179. /* Keep track of this so we can free it. */
  180. cxt->allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer));
  181. strcpy (cxt->allocated_line, &rl_line_buffer[0]);
  182. cxt->lines[i] = cxt->allocated_line;
  183. }
  184. cxt->hlen++;
  185. /* The line where we start the search. */
  186. cxt->history_pos = cxt->save_line;
  187. rl_save_prompt ();
  188. /* Initialize search parameters. */
  189. cxt->search_string = (char *)xmalloc (cxt->search_string_size = 128);
  190. cxt->search_string[cxt->search_string_index = 0] = '\0';
  191. /* Normalize DIRECTION into 1 or -1. */
  192. cxt->direction = (direction >= 0) ? 1 : -1;
  193. cxt->sline = rl_line_buffer;
  194. cxt->sline_len = strlen (cxt->sline);
  195. cxt->sline_index = rl_point;
  196. _rl_iscxt = cxt; /* save globally */
  197. return cxt;
  198. }
  199. static void
  200. _rl_isearch_fini (cxt)
  201. _rl_search_cxt *cxt;
  202. {
  203. /* First put back the original state. */
  204. strcpy (rl_line_buffer, cxt->lines[cxt->save_line]);
  205. rl_restore_prompt ();
  206. /* Save the search string for possible later use. */
  207. FREE (last_isearch_string);
  208. last_isearch_string = cxt->search_string;
  209. last_isearch_string_len = cxt->search_string_index;
  210. cxt->search_string = 0;
  211. if (cxt->last_found_line < cxt->save_line)
  212. rl_get_previous_history (cxt->save_line - cxt->last_found_line, 0);
  213. else
  214. rl_get_next_history (cxt->last_found_line - cxt->save_line, 0);
  215. /* If the string was not found, put point at the end of the last matching
  216. line. If last_found_line == orig_line, we didn't find any matching
  217. history lines at all, so put point back in its original position. */
  218. if (cxt->sline_index < 0)
  219. {
  220. if (cxt->last_found_line == cxt->save_line)
  221. cxt->sline_index = cxt->save_point;
  222. else
  223. cxt->sline_index = strlen (rl_line_buffer);
  224. rl_mark = cxt->save_mark;
  225. }
  226. rl_point = cxt->sline_index;
  227. /* Don't worry about where to put the mark here; rl_get_previous_history
  228. and rl_get_next_history take care of it. */
  229. rl_clear_message ();
  230. }
  231. int
  232. _rl_search_getchar (cxt)
  233. _rl_search_cxt *cxt;
  234. {
  235. int c;
  236. /* Read a key and decide how to proceed. */
  237. RL_SETSTATE(RL_STATE_MOREINPUT);
  238. c = cxt->lastc = rl_read_key ();
  239. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  240. #if defined (HANDLE_MULTIBYTE)
  241. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  242. c = cxt->lastc = _rl_read_mbstring (cxt->lastc, cxt->mb, MB_LEN_MAX);
  243. #endif
  244. return c;
  245. }
  246. /* Process just-read character C according to isearch context CXT. Return
  247. -1 if the caller should just free the context and return, 0 if we should
  248. break out of the loop, and 1 if we should continue to read characters. */
  249. int
  250. _rl_isearch_dispatch (cxt, c)
  251. _rl_search_cxt *cxt;
  252. int c;
  253. {
  254. int n, wstart, wlen, limit, cval;
  255. rl_command_func_t *f;
  256. f = (rl_command_func_t *)NULL;
  257. if (c < 0)
  258. {
  259. cxt->sflags |= SF_FAILED;
  260. cxt->history_pos = cxt->last_found_line;
  261. return -1;
  262. }
  263. /* If we are moving into a new keymap, modify cxt->keymap and go on.
  264. This can be a problem if c == ESC and we want to terminate the
  265. incremental search, so we check */
  266. if (c >= 0 && cxt->keymap[c].type == ISKMAP && strchr (cxt->search_terminators, cxt->lastc) == 0)
  267. {
  268. cxt->keymap = FUNCTION_TO_KEYMAP (cxt->keymap, c);
  269. cxt->sflags |= SF_CHGKMAP;
  270. /* XXX - we should probably save this sequence, so we can do
  271. something useful if this doesn't end up mapping to a command. */
  272. return 1;
  273. }
  274. /* Translate the keys we do something with to opcodes. */
  275. if (c >= 0 && cxt->keymap[c].type == ISFUNC)
  276. {
  277. f = cxt->keymap[c].function;
  278. if (f == rl_reverse_search_history)
  279. cxt->lastc = (cxt->sflags & SF_REVERSE) ? -1 : -2;
  280. else if (f == rl_forward_search_history)
  281. cxt->lastc = (cxt->sflags & SF_REVERSE) ? -2 : -1;
  282. else if (f == rl_rubout)
  283. cxt->lastc = -3;
  284. else if (c == CTRL ('G') || f == rl_abort)
  285. cxt->lastc = -4;
  286. else if (c == CTRL ('W') || f == rl_unix_word_rubout) /* XXX */
  287. cxt->lastc = -5;
  288. else if (c == CTRL ('Y') || f == rl_yank) /* XXX */
  289. cxt->lastc = -6;
  290. }
  291. /* If we changed the keymap earlier while translating a key sequence into
  292. a command, restore it now that we've succeeded. */
  293. if (cxt->sflags & SF_CHGKMAP)
  294. {
  295. cxt->keymap = cxt->okeymap;
  296. cxt->sflags &= ~SF_CHGKMAP;
  297. }
  298. /* The characters in isearch_terminators (set from the user-settable
  299. variable isearch-terminators) are used to terminate the search but
  300. not subsequently execute the character as a command. The default
  301. value is "\033\012" (ESC and C-J). */
  302. if (cxt->lastc > 0 && strchr (cxt->search_terminators, cxt->lastc))
  303. {
  304. /* ESC still terminates the search, but if there is pending
  305. input or if input arrives within 0.1 seconds (on systems
  306. with select(2)) it is used as a prefix character
  307. with rl_execute_next. WATCH OUT FOR THIS! This is intended
  308. to allow the arrow keys to be used like ^F and ^B are used
  309. to terminate the search and execute the movement command.
  310. XXX - since _rl_input_available depends on the application-
  311. settable keyboard timeout value, this could alternatively
  312. use _rl_input_queued(100000) */
  313. if (cxt->lastc == ESC && _rl_input_available ())
  314. rl_execute_next (ESC);
  315. return (0);
  316. }
  317. #define ENDSRCH_CHAR(c) \
  318. ((CTRL_CHAR (c) || META_CHAR (c) || (c) == RUBOUT) && ((c) != CTRL ('G')))
  319. #if defined (HANDLE_MULTIBYTE)
  320. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  321. {
  322. if (cxt->lastc >= 0 && (cxt->mb[0] && cxt->mb[1] == '\0') && ENDSRCH_CHAR (cxt->lastc))
  323. {
  324. /* This sets rl_pending_input to LASTC; it will be picked up the next
  325. time rl_read_key is called. */
  326. rl_execute_next (cxt->lastc);
  327. return (0);
  328. }
  329. }
  330. else
  331. #endif
  332. if (cxt->lastc >= 0 && ENDSRCH_CHAR (cxt->lastc))
  333. {
  334. /* This sets rl_pending_input to LASTC; it will be picked up the next
  335. time rl_read_key is called. */
  336. rl_execute_next (cxt->lastc);
  337. return (0);
  338. }
  339. /* Now dispatch on the character. `Opcodes' affect the search string or
  340. state. Other characters are added to the string. */
  341. switch (cxt->lastc)
  342. {
  343. /* search again */
  344. case -1:
  345. if (cxt->search_string_index == 0)
  346. {
  347. if (last_isearch_string)
  348. {
  349. cxt->search_string_size = 64 + last_isearch_string_len;
  350. cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
  351. strcpy (cxt->search_string, last_isearch_string);
  352. cxt->search_string_index = last_isearch_string_len;
  353. rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1);
  354. break;
  355. }
  356. return (1);
  357. }
  358. else if (cxt->sflags & SF_REVERSE)
  359. cxt->sline_index--;
  360. else if (cxt->sline_index != cxt->sline_len)
  361. cxt->sline_index++;
  362. else
  363. rl_ding ();
  364. break;
  365. /* switch directions */
  366. case -2:
  367. cxt->direction = -cxt->direction;
  368. if (cxt->direction < 0)
  369. cxt->sflags |= SF_REVERSE;
  370. else
  371. cxt->sflags &= ~SF_REVERSE;
  372. break;
  373. /* delete character from search string. */
  374. case -3: /* C-H, DEL */
  375. /* This is tricky. To do this right, we need to keep a
  376. stack of search positions for the current search, with
  377. sentinels marking the beginning and end. But this will
  378. do until we have a real isearch-undo. */
  379. if (cxt->search_string_index == 0)
  380. rl_ding ();
  381. else
  382. cxt->search_string[--cxt->search_string_index] = '\0';
  383. break;
  384. case -4: /* C-G, abort */
  385. rl_replace_line (cxt->lines[cxt->save_line], 0);
  386. rl_point = cxt->save_point;
  387. rl_mark = cxt->save_mark;
  388. rl_restore_prompt();
  389. rl_clear_message ();
  390. return -1;
  391. case -5: /* C-W */
  392. /* skip over portion of line we already matched and yank word */
  393. wstart = rl_point + cxt->search_string_index;
  394. if (wstart >= rl_end)
  395. {
  396. rl_ding ();
  397. break;
  398. }
  399. /* if not in a word, move to one. */
  400. cval = _rl_char_value (rl_line_buffer, wstart);
  401. if (_rl_walphabetic (cval) == 0)
  402. {
  403. rl_ding ();
  404. break;
  405. }
  406. n = MB_NEXTCHAR (rl_line_buffer, wstart, 1, MB_FIND_NONZERO);;
  407. while (n < rl_end)
  408. {
  409. cval = _rl_char_value (rl_line_buffer, n);
  410. if (_rl_walphabetic (cval) == 0)
  411. break;
  412. n = MB_NEXTCHAR (rl_line_buffer, n, 1, MB_FIND_NONZERO);;
  413. }
  414. wlen = n - wstart + 1;
  415. if (cxt->search_string_index + wlen + 1 >= cxt->search_string_size)
  416. {
  417. cxt->search_string_size += wlen + 1;
  418. cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
  419. }
  420. for (; wstart < n; wstart++)
  421. cxt->search_string[cxt->search_string_index++] = rl_line_buffer[wstart];
  422. cxt->search_string[cxt->search_string_index] = '\0';
  423. break;
  424. case -6: /* C-Y */
  425. /* skip over portion of line we already matched and yank rest */
  426. wstart = rl_point + cxt->search_string_index;
  427. if (wstart >= rl_end)
  428. {
  429. rl_ding ();
  430. break;
  431. }
  432. n = rl_end - wstart + 1;
  433. if (cxt->search_string_index + n + 1 >= cxt->search_string_size)
  434. {
  435. cxt->search_string_size += n + 1;
  436. cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
  437. }
  438. for (n = wstart; n < rl_end; n++)
  439. cxt->search_string[cxt->search_string_index++] = rl_line_buffer[n];
  440. cxt->search_string[cxt->search_string_index] = '\0';
  441. break;
  442. /* Add character to search string and continue search. */
  443. default:
  444. if (cxt->search_string_index + 2 >= cxt->search_string_size)
  445. {
  446. cxt->search_string_size += 128;
  447. cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
  448. }
  449. #if defined (HANDLE_MULTIBYTE)
  450. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  451. {
  452. int j, l;
  453. for (j = 0, l = strlen (cxt->mb); j < l; )
  454. cxt->search_string[cxt->search_string_index++] = cxt->mb[j++];
  455. }
  456. else
  457. #endif
  458. cxt->search_string[cxt->search_string_index++] = c;
  459. cxt->search_string[cxt->search_string_index] = '\0';
  460. break;
  461. }
  462. for (cxt->sflags &= ~(SF_FOUND|SF_FAILED);; )
  463. {
  464. limit = cxt->sline_len - cxt->search_string_index + 1;
  465. /* Search the current line. */
  466. while ((cxt->sflags & SF_REVERSE) ? (cxt->sline_index >= 0) : (cxt->sline_index < limit))
  467. {
  468. if (STREQN (cxt->search_string, cxt->sline + cxt->sline_index, cxt->search_string_index))
  469. {
  470. cxt->sflags |= SF_FOUND;
  471. break;
  472. }
  473. else
  474. cxt->sline_index += cxt->direction;
  475. }
  476. if (cxt->sflags & SF_FOUND)
  477. break;
  478. /* Move to the next line, but skip new copies of the line
  479. we just found and lines shorter than the string we're
  480. searching for. */
  481. do
  482. {
  483. /* Move to the next line. */
  484. cxt->history_pos += cxt->direction;
  485. /* At limit for direction? */
  486. if ((cxt->sflags & SF_REVERSE) ? (cxt->history_pos < 0) : (cxt->history_pos == cxt->hlen))
  487. {
  488. cxt->sflags |= SF_FAILED;
  489. break;
  490. }
  491. /* We will need these later. */
  492. cxt->sline = cxt->lines[cxt->history_pos];
  493. cxt->sline_len = strlen (cxt->sline);
  494. }
  495. while ((cxt->prev_line_found && STREQ (cxt->prev_line_found, cxt->lines[cxt->history_pos])) ||
  496. (cxt->search_string_index > cxt->sline_len));
  497. if (cxt->sflags & SF_FAILED)
  498. break;
  499. /* Now set up the line for searching... */
  500. cxt->sline_index = (cxt->sflags & SF_REVERSE) ? cxt->sline_len - cxt->search_string_index : 0;
  501. }
  502. if (cxt->sflags & SF_FAILED)
  503. {
  504. /* We cannot find the search string. Ding the bell. */
  505. rl_ding ();
  506. cxt->history_pos = cxt->last_found_line;
  507. return 1;
  508. }
  509. /* We have found the search string. Just display it. But don't
  510. actually move there in the history list until the user accepts
  511. the location. */
  512. if (cxt->sflags & SF_FOUND)
  513. {
  514. cxt->prev_line_found = cxt->lines[cxt->history_pos];
  515. rl_replace_line (cxt->lines[cxt->history_pos], 0);
  516. rl_point = cxt->sline_index;
  517. cxt->last_found_line = cxt->history_pos;
  518. rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), (cxt->history_pos == cxt->save_line) ? -1 : cxt->history_pos);
  519. }
  520. return 1;
  521. }
  522. static int
  523. _rl_isearch_cleanup (cxt, r)
  524. _rl_search_cxt *cxt;
  525. int r;
  526. {
  527. if (r >= 0)
  528. _rl_isearch_fini (cxt);
  529. _rl_scxt_dispose (cxt, 0);
  530. _rl_iscxt = 0;
  531. RL_UNSETSTATE(RL_STATE_ISEARCH);
  532. return (r != 0);
  533. }
  534. /* Search through the history looking for an interactively typed string.
  535. This is analogous to i-search. We start the search in the current line.
  536. DIRECTION is which direction to search; >= 0 means forward, < 0 means
  537. backwards. */
  538. static int
  539. rl_search_history (direction, invoking_key)
  540. int direction, invoking_key;
  541. {
  542. _rl_search_cxt *cxt; /* local for now, but saved globally */
  543. int c, r;
  544. RL_SETSTATE(RL_STATE_ISEARCH);
  545. cxt = _rl_isearch_init (direction);
  546. rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1);
  547. /* If we are using the callback interface, all we do is set up here and
  548. return. The key is that we leave RL_STATE_ISEARCH set. */
  549. if (RL_ISSTATE (RL_STATE_CALLBACK))
  550. return (0);
  551. r = -1;
  552. for (;;)
  553. {
  554. c = _rl_search_getchar (cxt);
  555. /* We might want to handle EOF here (c == 0) */
  556. r = _rl_isearch_dispatch (cxt, cxt->lastc);
  557. if (r <= 0)
  558. break;
  559. }
  560. /* The searching is over. The user may have found the string that she
  561. was looking for, or else she may have exited a failing search. If
  562. LINE_INDEX is -1, then that shows that the string searched for was
  563. not found. We use this to determine where to place rl_point. */
  564. return (_rl_isearch_cleanup (cxt, r));
  565. }
  566. #if defined (READLINE_CALLBACKS)
  567. /* Called from the callback functions when we are ready to read a key. The
  568. callback functions know to call this because RL_ISSTATE(RL_STATE_ISEARCH).
  569. If _rl_isearch_dispatch finishes searching, this function is responsible
  570. for turning off RL_STATE_ISEARCH, which it does using _rl_isearch_cleanup. */
  571. int
  572. _rl_isearch_callback (cxt)
  573. _rl_search_cxt *cxt;
  574. {
  575. int c, r;
  576. c = _rl_search_getchar (cxt);
  577. /* We might want to handle EOF here */
  578. r = _rl_isearch_dispatch (cxt, cxt->lastc);
  579. return (r <= 0) ? _rl_isearch_cleanup (cxt, r) : 0;
  580. }
  581. #endif