TheGNUGeneralPublicLicenseisoftenshippedwithGNUsoftware,and isgenerallykeptinafilecalledCOPYINGorLICENSE.Ifyoudonot haveacopyofthelicense,writetotheFreeSoftwareFoundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA. */ #define READLINE_LIBRARY
#ifdefined (HAVE_CONFIG_H) # include "config_readline.h" #endif
#include <sys/types.h>
#include <stdio.h>
#ifdefined (HAVE_UNISTD_H) # include <unistd.h> #endif
#ifdefined (HAVE_STDLIB_H) # include <stdlib.h> #else # include "ansi_stdlib.h" #endif
#include"rldefs.h" #include"rlmbutil.h"
#include"readline.h" #include"history.h"
#include"rlprivate.h" #include"xmalloc.h"
/* Variables exported to other files in the readline library. */ char *_rl_isearch_terminators = (char *)NULL;
_rl_search_cxt *_rl_iscxt = 0;
/* Variables imported from other files in the readline library. */ extern HIST_ENTRY *_rl_saved_line_for_history;
/* Last line found by the current incremental search, so we don't `find'
identical lines many times in a row. Now part of isearch context. */ /* static char *prev_line_found; */
/* Last search string and its length. */ staticchar *last_isearch_string; staticint last_isearch_string_len;
/* Search backwards through the history looking for a string which is typed
interactively. Start with the current line. */ int
rl_reverse_search_history (sign, key) int sign, key;
{ return (rl_search_history (-sign, key));
}
/* Search forwards through the history looking for a string which is typed
interactively. Start with the current line. */ int
rl_forward_search_history (sign, key) int sign, key;
{ return (rl_search_history (sign, key));
}
/* Display the current state of the search in the echo-area. SEARCH_STRINGcontainsthestringthatisbeingsearchedfor, DIRECTIONiszeroforforward,ornon-zeroforreverse, WHEREisthehistorylistnumberofthecurrentline.Ifitis
-1, then this line is the starting one. */ staticvoid
rl_display_search (search_string, reverse_p, where) char *search_string; int reverse_p, where __attribute__((unused));
{ char *message; int msglen, searchlen;
/* Create an arrary of pointers to the lines that we want to search. */
hlist = history_list ();
rl_maybe_replace_line ();
i = 0; if (hlist) for (i = 0; hlist[i]; i++);
/* Allocate space for this many lines, +1 for the current input line,
and remember those lines. */
cxt->lines = (char **)xmalloc ((1 + (cxt->hlen = i)) * sizeof (char *)); for (i = 0; i < cxt->hlen; i++)
cxt->lines[i] = hlist[i]->line;
if (_rl_saved_line_for_history)
cxt->lines[i] = _rl_saved_line_for_history->line; else
{ /* Keep track of this so we can free it. */
cxt->allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer));
strcpy (cxt->allocated_line, &rl_line_buffer[0]);
cxt->lines[i] = cxt->allocated_line;
}
cxt->hlen++;
/* The line where we start the search. */
cxt->history_pos = cxt->save_line;
staticvoid
_rl_isearch_fini (cxt)
_rl_search_cxt *cxt;
{ /* First put back the original state. */
strcpy (rl_line_buffer, cxt->lines[cxt->save_line]);
rl_restore_prompt ();
/* Save the search string for possible later use. */
FREE (last_isearch_string);
last_isearch_string = cxt->search_string;
last_isearch_string_len = cxt->search_string_index;
cxt->search_string = 0;
/* If the string was not found, put point at the end of the last matching line.Iflast_found_line==orig_line,wedidn'tfindanymatching
history lines at all, so put point back in its original position. */ if (cxt->sline_index < 0)
{ if (cxt->last_found_line == cxt->save_line)
cxt->sline_index = cxt->save_point; else
cxt->sline_index = strlen (rl_line_buffer);
rl_mark = cxt->save_mark;
}
rl_point = cxt->sline_index; /* Don't worry about where to put the mark here; rl_get_previous_history
and rl_get_next_history take care of it. */
rl_clear_message ();
}
int
_rl_search_getchar (cxt)
_rl_search_cxt *cxt;
{ int c;
/* Read a key and decide how to proceed. */
RL_SETSTATE(RL_STATE_MOREINPUT);
c = cxt->lastc = rl_read_key ();
RL_UNSETSTATE(RL_STATE_MOREINPUT);
#ifdefined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
c = cxt->lastc = _rl_read_mbstring (cxt->lastc, cxt->mb, MB_LEN_MAX); #endif
return c;
}
/* Process just-read character C according to isearch context CXT. Return -1ifthecallershouldjustfreethecontextandreturn,0ifweshould
break out of the loop, and 1 if we should continue to read characters. */ int
_rl_isearch_dispatch (cxt, c)
_rl_search_cxt *cxt; int c;
{ int n, wstart, wlen, limit, cval;
rl_command_func_t *f;
f = (rl_command_func_t *)NULL;
/* Translate the keys we do something with to opcodes. */ if (c >= 0 && _rl_keymap[c].type == ISFUNC)
{
f = _rl_keymap[c].function;
/* The characters in isearch_terminators (set from the user-settable variableisearch-terminators)areusedtoterminatethesearchbut notsubsequentlyexecutethecharacterasacommand.Thedefault
value is "\033\012" (ESC and C-J). */ if (strchr (cxt->search_terminators, cxt->lastc))
{ /* ESC still terminates the search, but if there is pending inputorifinputarriveswithin0.1seconds(onsystems withselect(2))itisusedasaprefixcharacter withrl_execute_next.WATCHOUTFORTHIS!Thisisintended toallowthearrowkeystobeusedlike^Fand^Bareused toterminatethesearchandexecutethemovementcommand. XXX-since_rl_input_availabledependsontheapplication- settablekeyboardtimeoutvalue,thiscouldalternatively
use _rl_input_queued(100000) */ if (cxt->lastc == ESC && _rl_input_available ())
rl_execute_next (ESC); return (0);
}
#ifdefined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
{ if (cxt->lastc >= 0 && (cxt->mb[0] && cxt->mb[1] == '\0') && ENDSRCH_CHAR (cxt->lastc))
{ /* This sets rl_pending_input to c; it will be picked up the next
time rl_read_key is called. */
rl_execute_next (cxt->lastc); return (0);
}
} else #endif if (cxt->lastc >= 0 && ENDSRCH_CHAR (cxt->lastc))
{ /* This sets rl_pending_input to LASTC; it will be picked up the next
time rl_read_key is called. */
rl_execute_next (cxt->lastc); return (0);
}
/* Now dispatch on the character. `Opcodes' affect the search string or
state. Other characters are added to the string. */ switch (cxt->lastc)
{ /* search again */ case -1: if (cxt->search_string_index == 0)
{ if (last_isearch_string)
{
cxt->search_string_size = 64 + last_isearch_string_len;
cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
strcpy (cxt->search_string, last_isearch_string);
cxt->search_string_index = last_isearch_string_len;
rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1); break;
} return (1);
} elseif (cxt->sflags & SF_REVERSE)
cxt->sline_index--; elseif (cxt->sline_index != cxt->sline_len)
cxt->sline_index++; else
rl_ding (); break;
/* delete character from search string. */ case -3: /* C-H, DEL */ /* This is tricky. To do this right, we need to keep a stackofsearchpositionsforthecurrentsearch,with sentinelsmarkingthebeginningandend.Butthiswill
do until we have a real isearch-undo. */ if (cxt->search_string_index == 0)
rl_ding (); else
cxt->search_string[--cxt->search_string_index] = '\0'; break;
case -5: /* C-W */ /* skip over portion of line we already matched and yank word */
wstart = rl_point + cxt->search_string_index; if (wstart >= rl_end)
{
rl_ding (); break;
}
/* if not in a word, move to one. */
cval = _rl_char_value (rl_line_buffer, wstart); if (_rl_walphabetic (cval) == 0)
{
rl_ding (); break;
}
n = MB_NEXTCHAR (rl_line_buffer, wstart, 1, MB_FIND_NONZERO);; while (n < rl_end)
{
cval = _rl_char_value (rl_line_buffer, n); if (_rl_walphabetic (cval) == 0) break;
n = MB_NEXTCHAR (rl_line_buffer, n, 1, MB_FIND_NONZERO);;
}
wlen = n - wstart + 1; if (cxt->search_string_index + wlen + 1 >= cxt->search_string_size)
{
cxt->search_string_size += wlen + 1;
cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
} for (; wstart < n; wstart++)
cxt->search_string[cxt->search_string_index++] = rl_line_buffer[wstart];
cxt->search_string[cxt->search_string_index] = '\0'; break;
case -6: /* C-Y */ /* skip over portion of line we already matched and yank rest */
wstart = rl_point + cxt->search_string_index; if (wstart >= rl_end)
{
rl_ding (); break;
}
n = rl_end - wstart + 1; if (cxt->search_string_index + n + 1 >= cxt->search_string_size)
{
cxt->search_string_size += n + 1;
cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
} for (n = wstart; n < rl_end; n++)
cxt->search_string[cxt->search_string_index++] = rl_line_buffer[n];
cxt->search_string[cxt->search_string_index] = '\0'; break;
/* Add character to search string and continue search. */ default: if (cxt->search_string_index + 2 >= cxt->search_string_size)
{
cxt->search_string_size += 128;
cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
} #ifdefined (HANDLE_MULTIBYTE) if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
{ int j, l; for (j = 0, l = strlen (cxt->mb); j < l; )
cxt->search_string[cxt->search_string_index++] = cxt->mb[j++];
} else #endif
cxt->search_string[cxt->search_string_index++] = c;
cxt->search_string[cxt->search_string_index] = '\0'; break;
}
/* Search the current line. */ while ((cxt->sflags & SF_REVERSE) ? (cxt->sline_index >= 0) : (cxt->sline_index < limit))
{ if (STREQN (cxt->search_string, cxt->sline + cxt->sline_index, cxt->search_string_index))
{
cxt->sflags |= SF_FOUND; break;
} else
cxt->sline_index += cxt->direction;
} if (cxt->sflags & SF_FOUND) break;
/* Move to the next line, but skip new copies of the line wejustfoundandlinesshorterthanthestringwe're
searching for. */ do
{ /* Move to the next line. */
cxt->history_pos += cxt->direction;
/* At limit for direction? */ if ((cxt->sflags & SF_REVERSE) ? (cxt->history_pos < 0) : (cxt->history_pos == cxt->hlen))
{
cxt->sflags |= SF_FAILED; break;
}
/* We will need these later. */
cxt->sline = cxt->lines[cxt->history_pos];
cxt->sline_len = strlen (cxt->sline);
} while ((cxt->prev_line_found && STREQ (cxt->prev_line_found, cxt->lines[cxt->history_pos])) ||
(cxt->search_string_index > cxt->sline_len));
if (cxt->sflags & SF_FAILED) break;
/* Now set up the line for searching... */
cxt->sline_index = (cxt->sflags & SF_REVERSE) ? cxt->sline_len - cxt->search_string_index : 0;
}
if (cxt->sflags & SF_FAILED)
{ /* We cannot find the search string. Ding the bell. */
rl_ding ();
cxt->history_pos = cxt->last_found_line; return1;
}
/* We have found the search string. Just display it. But don't actuallymovethereinthehistorylistuntiltheuseraccepts
the location. */ if (cxt->sflags & SF_FOUND)
{
cxt->prev_line_found = cxt->lines[cxt->history_pos];
rl_replace_line (cxt->lines[cxt->history_pos], 0);
rl_point = cxt->sline_index;
cxt->last_found_line = cxt->history_pos;
rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), (cxt->history_pos == cxt->save_line) ? -1 : cxt->history_pos);
}
/* Search through the history looking for an interactively typed string. Thisisanalogoustoi-search.Westartthesearchinthecurrentline. DIRECTIONiswhichdirectiontosearch;>=0meansforward,<0means
backwards. */ staticint
rl_search_history (direction, invoking_key) int direction, invoking_key __attribute__((unused));
{
_rl_search_cxt *cxt; /* local for now, but saved globally */ int r;
/* If we are using the callback interface, all we do is set up here and
return. The key is that we leave RL_STATE_ISEARCH set. */ if (RL_ISSTATE (RL_STATE_CALLBACK)) return (0);
r = -1; for (;;)
{
_rl_search_getchar (cxt); /* We might want to handle EOF here (c == 0) */
r = _rl_isearch_dispatch (cxt, cxt->lastc); if (r <= 0) break;
}
/* The searching is over. The user may have found the string that she waslookingfor,orelseshemayhaveexitedafailingsearch.If LINE_INDEXis-1,thenthatshowsthatthestringsearchedforwas
not found. We use this to determine where to place rl_point. */ return (_rl_isearch_cleanup (cxt, r));
}
#ifdefined (READLINE_CALLBACKS) /* Called from the callback functions when we are ready to read a key. The callbackfunctionsknowtocallthisbecauseRL_ISSTATE(RL_STATE_ISEARCH). If_rl_isearch_dispatchfinishessearching,thisfunctionisresponsible
for turning off RL_STATE_ISEARCH, which it does using _rl_isearch_cleanup. */ int
_rl_isearch_callback (cxt)
_rl_search_cxt *cxt;
{ int r;
_rl_search_getchar (cxt); /* We might want to handle EOF here */
r = _rl_isearch_dispatch (cxt, cxt->lastc);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.