search.c (136644) | search.c (157184) |
---|---|
1/* search.c - code for non-incremental searching in emacs and vi modes. */ 2 | 1/* search.c - code for non-incremental searching in emacs and vi modes. */ 2 |
3/* Copyright (C) 1992 Free Software Foundation, Inc. | 3/* Copyright (C) 1992-2005 Free Software Foundation, Inc. |
4 5 This file is part of the Readline Library (the Library), a set of 6 routines for providing Emacs style line input to programs that ask 7 for it. 8 9 The Library is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2, or (at your option) --- 36 unchanged lines hidden (view full) --- 48#include "rlprivate.h" 49#include "xmalloc.h" 50 51#ifdef abs 52# undef abs 53#endif 54#define abs(x) (((x) >= 0) ? (x) : -(x)) 55 | 4 5 This file is part of the Readline Library (the Library), a set of 6 routines for providing Emacs style line input to programs that ask 7 for it. 8 9 The Library is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2, or (at your option) --- 36 unchanged lines hidden (view full) --- 48#include "rlprivate.h" 49#include "xmalloc.h" 50 51#ifdef abs 52# undef abs 53#endif 54#define abs(x) (((x) >= 0) ? (x) : -(x)) 55 |
56_rl_search_cxt *_rl_nscxt = 0; 57 |
|
56extern HIST_ENTRY *_rl_saved_line_for_history; 57 58/* Functions imported from the rest of the library. */ 59extern int _rl_free_history_entry PARAMS((HIST_ENTRY *)); 60 61static char *noninc_search_string = (char *) NULL; 62static int noninc_history_pos; 63 64static char *prev_line_found = (char *) NULL; 65 66static int rl_history_search_len; 67static int rl_history_search_pos; 68static char *history_search_string; 69static int history_string_size; 70 | 58extern HIST_ENTRY *_rl_saved_line_for_history; 59 60/* Functions imported from the rest of the library. */ 61extern int _rl_free_history_entry PARAMS((HIST_ENTRY *)); 62 63static char *noninc_search_string = (char *) NULL; 64static int noninc_history_pos; 65 66static char *prev_line_found = (char *) NULL; 67 68static int rl_history_search_len; 69static int rl_history_search_pos; 70static char *history_search_string; 71static int history_string_size; 72 |
73static UNDO_LIST *noninc_saved_undo_list; |
|
71static void make_history_line_current PARAMS((HIST_ENTRY *)); 72static int noninc_search_from_pos PARAMS((char *, int, int)); | 74static void make_history_line_current PARAMS((HIST_ENTRY *)); 75static int noninc_search_from_pos PARAMS((char *, int, int)); |
73static void noninc_dosearch PARAMS((char *, int)); 74static void noninc_search PARAMS((int, int)); | 76static int noninc_dosearch PARAMS((char *, int)); 77static int noninc_search PARAMS((int, int)); |
75static int rl_history_search_internal PARAMS((int, int)); 76static void rl_history_search_reinit PARAMS((void)); 77 | 78static int rl_history_search_internal PARAMS((int, int)); 79static void rl_history_search_reinit PARAMS((void)); 80 |
81static _rl_search_cxt *_rl_nsearch_init PARAMS((int, int)); 82static int _rl_nsearch_cleanup PARAMS((_rl_search_cxt *, int)); 83static void _rl_nsearch_abort PARAMS((_rl_search_cxt *)); 84static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt *, int)); 85 |
|
78/* Make the data from the history entry ENTRY be the contents of the 79 current line. This doesn't do anything with rl_point; the caller 80 must set it. */ 81static void 82make_history_line_current (entry) 83 HIST_ENTRY *entry; 84{ | 86/* Make the data from the history entry ENTRY be the contents of the 87 current line. This doesn't do anything with rl_point; the caller 88 must set it. */ 89static void 90make_history_line_current (entry) 91 HIST_ENTRY *entry; 92{ |
85#if 0 86 rl_replace_line (entry->line, 1); 87 rl_undo_list = (UNDO_LIST *)entry->data; 88#else | |
89 _rl_replace_text (entry->line, 0, rl_end); 90 _rl_fix_point (1); | 93 _rl_replace_text (entry->line, 0, rl_end); 94 _rl_fix_point (1); |
95#if defined (VI_MODE) 96 if (rl_editing_mode == vi_mode) 97 /* POSIX.2 says that the `U' command doesn't affect the copy of any 98 command lines to the edit line. We're going to implement that by 99 making the undo list start after the matching line is copied to the 100 current editing buffer. */ 101 rl_free_undo_list (); |
|
91#endif 92 93 if (_rl_saved_line_for_history) 94 _rl_free_history_entry (_rl_saved_line_for_history); 95 _rl_saved_line_for_history = (HIST_ENTRY *)NULL; 96} 97 98/* Search the history list for STRING starting at absolute history position --- 26 unchanged lines hidden (view full) --- 125 ret = where_history (); 126 127 history_set_pos (old); 128 return (ret); 129} 130 131/* Search for a line in the history containing STRING. If DIR is < 0, the 132 search is backwards through previous entries, else through subsequent | 102#endif 103 104 if (_rl_saved_line_for_history) 105 _rl_free_history_entry (_rl_saved_line_for_history); 106 _rl_saved_line_for_history = (HIST_ENTRY *)NULL; 107} 108 109/* Search the history list for STRING starting at absolute history position --- 26 unchanged lines hidden (view full) --- 136 ret = where_history (); 137 138 history_set_pos (old); 139 return (ret); 140} 141 142/* Search for a line in the history containing STRING. If DIR is < 0, the 143 search is backwards through previous entries, else through subsequent |
133 entries. */ 134static void | 144 entries. Returns 1 if the search was successful, 0 otherwise. */ 145static int |
135noninc_dosearch (string, dir) 136 char *string; 137 int dir; 138{ 139 int oldpos, pos; 140 HIST_ENTRY *entry; 141 142 if (string == 0 || *string == '\0' || noninc_history_pos < 0) 143 { 144 rl_ding (); | 146noninc_dosearch (string, dir) 147 char *string; 148 int dir; 149{ 150 int oldpos, pos; 151 HIST_ENTRY *entry; 152 153 if (string == 0 || *string == '\0' || noninc_history_pos < 0) 154 { 155 rl_ding (); |
145 return; | 156 return 0; |
146 } 147 148 pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir); 149 if (pos == -1) 150 { 151 /* Search failed, current history position unchanged. */ 152 rl_maybe_unsave_line (); 153 rl_clear_message (); 154 rl_point = 0; 155 rl_ding (); | 157 } 158 159 pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir); 160 if (pos == -1) 161 { 162 /* Search failed, current history position unchanged. */ 163 rl_maybe_unsave_line (); 164 rl_clear_message (); 165 rl_point = 0; 166 rl_ding (); |
156 return; | 167 return 0; |
157 } 158 159 noninc_history_pos = pos; 160 161 oldpos = where_history (); 162 history_set_pos (noninc_history_pos); 163 entry = current_history (); 164#if defined (VI_MODE) 165 if (rl_editing_mode != vi_mode) 166#endif | 168 } 169 170 noninc_history_pos = pos; 171 172 oldpos = where_history (); 173 history_set_pos (noninc_history_pos); 174 entry = current_history (); 175#if defined (VI_MODE) 176 if (rl_editing_mode != vi_mode) 177#endif |
167 history_set_pos (oldpos); | 178 history_set_pos (oldpos); |
168 169 make_history_line_current (entry); 170 171 rl_point = 0; 172 rl_mark = rl_end; 173 174 rl_clear_message (); | 179 180 make_history_line_current (entry); 181 182 rl_point = 0; 183 rl_mark = rl_end; 184 185 rl_clear_message (); |
186 return 1; |
|
175} 176 | 187} 188 |
177/* Search non-interactively through the history list. DIR < 0 means to 178 search backwards through the history of previous commands; otherwise 179 the search is for commands subsequent to the current position in the 180 history list. PCHAR is the character to use for prompting when reading 181 the search string; if not specified (0), it defaults to `:'. */ 182static void 183noninc_search (dir, pchar) 184 int dir; 185 int pchar; | 189static _rl_search_cxt * 190_rl_nsearch_init (dir, pchar) 191 int dir, pchar; |
186{ | 192{ |
187 int saved_point, saved_mark, c; | 193 _rl_search_cxt *cxt; |
188 char *p; | 194 char *p; |
189#if defined (HANDLE_MULTIBYTE) 190 char mb[MB_LEN_MAX]; 191#endif | |
192 | 195 |
196 cxt = _rl_scxt_alloc (RL_SEARCH_NSEARCH, 0); 197 if (dir < 0) 198 cxt->sflags |= SF_REVERSE; /* not strictly needed */ 199 200 cxt->direction = dir; 201 cxt->history_pos = cxt->save_line; 202 |
|
193 rl_maybe_save_line (); | 203 rl_maybe_save_line (); |
194 saved_point = rl_point; 195 saved_mark = rl_mark; | |
196 197 /* Clear the undo list, since reading the search string should create its 198 own undo list, and the whole list will end up being freed when we 199 finish reading the search string. */ 200 rl_undo_list = 0; 201 202 /* Use the line buffer to read the search string. */ 203 rl_line_buffer[0] = 0; 204 rl_end = rl_point = 0; 205 206 p = _rl_make_prompt_for_search (pchar ? pchar : ':'); 207 rl_message (p, 0, 0); 208 free (p); 209 | 204 205 /* Clear the undo list, since reading the search string should create its 206 own undo list, and the whole list will end up being freed when we 207 finish reading the search string. */ 208 rl_undo_list = 0; 209 210 /* Use the line buffer to read the search string. */ 211 rl_line_buffer[0] = 0; 212 rl_end = rl_point = 0; 213 214 p = _rl_make_prompt_for_search (pchar ? pchar : ':'); 215 rl_message (p, 0, 0); 216 free (p); 217 |
210#define SEARCH_RETURN rl_restore_prompt (); RL_UNSETSTATE(RL_STATE_NSEARCH); return 211 | |
212 RL_SETSTATE(RL_STATE_NSEARCH); | 218 RL_SETSTATE(RL_STATE_NSEARCH); |
213 /* Read the search string. */ 214 while (1) 215 { 216 RL_SETSTATE(RL_STATE_MOREINPUT); 217 c = rl_read_key (); 218 RL_UNSETSTATE(RL_STATE_MOREINPUT); | |
219 | 219 |
220#if defined (HANDLE_MULTIBYTE) 221 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 222 c = _rl_read_mbstring (c, mb, MB_LEN_MAX); 223#endif | 220 _rl_nscxt = cxt; |
224 | 221 |
225 if (c == 0) 226 break; | 222 return cxt; 223} |
227 | 224 |
228 switch (c) 229 { 230 case CTRL('H'): 231 case RUBOUT: 232 if (rl_point == 0) 233 { 234 rl_maybe_unsave_line (); 235 rl_clear_message (); 236 rl_point = saved_point; 237 rl_mark = saved_mark; 238 SEARCH_RETURN; 239 } 240 _rl_rubout_char (1, c); 241 break; | 225static int 226_rl_nsearch_cleanup (cxt, r) 227 _rl_search_cxt *cxt; 228 int r; 229{ 230 _rl_scxt_dispose (cxt, 0); 231 _rl_nscxt = 0; |
242 | 232 |
243 case CTRL('W'): 244 rl_unix_word_rubout (1, c); 245 break; | 233 RL_UNSETSTATE(RL_STATE_NSEARCH); |
246 | 234 |
247 case CTRL('U'): 248 rl_unix_line_discard (1, c); 249 break; | 235 return (r != 1); 236} |
250 | 237 |
251 case RETURN: 252 case NEWLINE: 253 goto dosearch; 254 /* NOTREACHED */ 255 break; | 238static void 239_rl_nsearch_abort (cxt) 240 _rl_search_cxt *cxt; 241{ 242 rl_maybe_unsave_line (); 243 rl_clear_message (); 244 rl_point = cxt->save_point; 245 rl_mark = cxt->save_mark; 246 rl_restore_prompt (); |
256 | 247 |
257 case CTRL('C'): 258 case CTRL('G'): 259 rl_maybe_unsave_line (); 260 rl_clear_message (); 261 rl_point = saved_point; 262 rl_mark = saved_mark; 263 rl_ding (); 264 SEARCH_RETURN; | 248 RL_UNSETSTATE (RL_STATE_NSEARCH); 249} |
265 | 250 |
266 default: | 251/* Process just-read character C according to search context CXT. Return -1 252 if the caller should abort the search, 0 if we should break out of the 253 loop, and 1 if we should continue to read characters. */ 254static int 255_rl_nsearch_dispatch (cxt, c) 256 _rl_search_cxt *cxt; 257 int c; 258{ 259 switch (c) 260 { 261 case CTRL('W'): 262 rl_unix_word_rubout (1, c); 263 break; 264 265 case CTRL('U'): 266 rl_unix_line_discard (1, c); 267 break; 268 269 case RETURN: 270 case NEWLINE: 271 return 0; 272 273 case CTRL('H'): 274 case RUBOUT: 275 if (rl_point == 0) 276 { 277 _rl_nsearch_abort (cxt); 278 return -1; 279 } 280 _rl_rubout_char (1, c); 281 break; 282 283 case CTRL('C'): 284 case CTRL('G'): 285 rl_ding (); 286 _rl_nsearch_abort (cxt); 287 return -1; 288 289 default: |
267#if defined (HANDLE_MULTIBYTE) | 290#if defined (HANDLE_MULTIBYTE) |
268 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 269 rl_insert_text (mb); 270 else | 291 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 292 rl_insert_text (cxt->mb); 293 else |
271#endif | 294#endif |
272 _rl_insert_char (1, c); 273 break; 274 } 275 (*rl_redisplay_function) (); | 295 _rl_insert_char (1, c); 296 break; |
276 } 277 | 297 } 298 |
278 dosearch: 279 rl_mark = saved_mark; | 299 (*rl_redisplay_function) (); 300 return 1; 301} |
280 | 302 |
303/* Perform one search according to CXT, using NONINC_SEARCH_STRING. Return 304 -1 if the search should be aborted, any other value means to clean up 305 using _rl_nsearch_cleanup (). Returns 1 if the search was successful, 306 0 otherwise. */ 307static int 308_rl_nsearch_dosearch (cxt) 309 _rl_search_cxt *cxt; 310{ 311 rl_mark = cxt->save_mark; 312 |
|
281 /* If rl_point == 0, we want to re-use the previous search string and 282 start from the saved history position. If there's no previous search 283 string, punt. */ 284 if (rl_point == 0) 285 { | 313 /* If rl_point == 0, we want to re-use the previous search string and 314 start from the saved history position. If there's no previous search 315 string, punt. */ 316 if (rl_point == 0) 317 { |
286 if (!noninc_search_string) | 318 if (noninc_search_string == 0) |
287 { 288 rl_ding (); | 319 { 320 rl_ding (); |
289 SEARCH_RETURN; | 321 rl_restore_prompt (); 322 RL_UNSETSTATE (RL_STATE_NSEARCH); 323 return -1; |
290 } 291 } 292 else 293 { 294 /* We want to start the search from the current history position. */ | 324 } 325 } 326 else 327 { 328 /* We want to start the search from the current history position. */ |
295 noninc_history_pos = where_history (); | 329 noninc_history_pos = cxt->save_line; |
296 FREE (noninc_search_string); 297 noninc_search_string = savestring (rl_line_buffer); | 330 FREE (noninc_search_string); 331 noninc_search_string = savestring (rl_line_buffer); |
332 333 /* If we don't want the subsequent undo list generated by the search 334 matching a history line to include the contents of the search string, 335 we need to clear rl_line_buffer here. For now, we just clear the 336 undo list generated by reading the search string. (If the search 337 fails, the old undo list will be restored by rl_maybe_unsave_line.) */ 338 rl_free_undo_list (); |
|
298 } 299 300 rl_restore_prompt (); | 339 } 340 341 rl_restore_prompt (); |
301 noninc_dosearch (noninc_search_string, dir); 302 RL_UNSETSTATE(RL_STATE_NSEARCH); | 342 return (noninc_dosearch (noninc_search_string, cxt->direction)); |
303} 304 | 343} 344 |
345/* Search non-interactively through the history list. DIR < 0 means to 346 search backwards through the history of previous commands; otherwise 347 the search is for commands subsequent to the current position in the 348 history list. PCHAR is the character to use for prompting when reading 349 the search string; if not specified (0), it defaults to `:'. */ 350static int 351noninc_search (dir, pchar) 352 int dir; 353 int pchar; 354{ 355 _rl_search_cxt *cxt; 356 int c, r; 357 358 cxt = _rl_nsearch_init (dir, pchar); 359 360 if (RL_ISSTATE (RL_STATE_CALLBACK)) 361 return (0); 362 363 /* Read the search string. */ 364 r = 0; 365 while (1) 366 { 367 c = _rl_search_getchar (cxt); 368 369 if (c == 0) 370 break; 371 372 r = _rl_nsearch_dispatch (cxt, c); 373 if (r < 0) 374 return 1; 375 else if (r == 0) 376 break; 377 } 378 379 r = _rl_nsearch_dosearch (cxt); 380 return ((r >= 0) ? _rl_nsearch_cleanup (cxt, r) : (r != 1)); 381} 382 |
|
305/* Search forward through the history list for a string. If the vi-mode 306 code calls this, KEY will be `?'. */ 307int 308rl_noninc_forward_search (count, key) 309 int count, key; 310{ | 383/* Search forward through the history list for a string. If the vi-mode 384 code calls this, KEY will be `?'. */ 385int 386rl_noninc_forward_search (count, key) 387 int count, key; 388{ |
311 noninc_search (1, (key == '?') ? '?' : 0); 312 return 0; | 389 return noninc_search (1, (key == '?') ? '?' : 0); |
313} 314 315/* Reverse search the history list for a string. If the vi-mode code 316 calls this, KEY will be `/'. */ 317int 318rl_noninc_reverse_search (count, key) 319 int count, key; 320{ | 390} 391 392/* Reverse search the history list for a string. If the vi-mode code 393 calls this, KEY will be `/'. */ 394int 395rl_noninc_reverse_search (count, key) 396 int count, key; 397{ |
321 noninc_search (-1, (key == '/') ? '/' : 0); 322 return 0; | 398 return noninc_search (-1, (key == '/') ? '/' : 0); |
323} 324 325/* Search forward through the history list for the last string searched 326 for. If there is no saved search string, abort. */ 327int 328rl_noninc_forward_search_again (count, key) 329 int count, key; 330{ | 399} 400 401/* Search forward through the history list for the last string searched 402 for. If there is no saved search string, abort. */ 403int 404rl_noninc_forward_search_again (count, key) 405 int count, key; 406{ |
407 int r; 408 |
|
331 if (!noninc_search_string) 332 { 333 rl_ding (); 334 return (-1); 335 } | 409 if (!noninc_search_string) 410 { 411 rl_ding (); 412 return (-1); 413 } |
336 noninc_dosearch (noninc_search_string, 1); 337 return 0; | 414 r = noninc_dosearch (noninc_search_string, 1); 415 return (r != 1); |
338} 339 340/* Reverse search in the history list for the last string searched 341 for. If there is no saved search string, abort. */ 342int 343rl_noninc_reverse_search_again (count, key) 344 int count, key; 345{ | 416} 417 418/* Reverse search in the history list for the last string searched 419 for. If there is no saved search string, abort. */ 420int 421rl_noninc_reverse_search_again (count, key) 422 int count, key; 423{ |
424 int r; 425 |
|
346 if (!noninc_search_string) 347 { 348 rl_ding (); 349 return (-1); 350 } | 426 if (!noninc_search_string) 427 { 428 rl_ding (); 429 return (-1); 430 } |
351 noninc_dosearch (noninc_search_string, -1); 352 return 0; | 431 r = noninc_dosearch (noninc_search_string, -1); 432 return (r != 1); |
353} 354 | 433} 434 |
435#if defined (READLINE_CALLBACKS) 436int 437_rl_nsearch_callback (cxt) 438 _rl_search_cxt *cxt; 439{ 440 int c, r; 441 442 c = _rl_search_getchar (cxt); 443 r = _rl_nsearch_dispatch (cxt, c); 444 if (r != 0) 445 return 1; 446 447 r = _rl_nsearch_dosearch (cxt); 448 return ((r >= 0) ? _rl_nsearch_cleanup (cxt, r) : (r != 1)); 449} 450#endif 451 |
|
355static int 356rl_history_search_internal (count, dir) 357 int count, dir; 358{ 359 HIST_ENTRY *temp; 360 int ret, oldpos; 361 362 rl_maybe_save_line (); --- 113 unchanged lines hidden --- | 452static int 453rl_history_search_internal (count, dir) 454 int count, dir; 455{ 456 HIST_ENTRY *temp; 457 int ret, oldpos; 458 459 rl_maybe_save_line (); --- 113 unchanged lines hidden --- |