62 #define SCREEN_WIDTH 80
63 #define SCREEN_ROWS 24
67 #define CTL(x) (char)((x) & 0x1F)
68 #define ISCTL(x) ((x) && (x) < ' ')
69 #define UNCTL(x) (char)((x) + 64)
70 #define META(x) (char)((x) | 0x80)
71 #define ISMETA(x) ((x) & 0x80)
72 #define UNMETA(x) (char)((x) & 0x7F)
75 int editline_histsize=256;
76 char *editline_history_file;
79 EL_USER_COMPLETION_FUNCTION_TYPE*el_user_completion_function = NULL;
85 TOupper, TOlower, TOcapitalize
91 typedef struct _KEYMAP {
99 typedef struct _HISTORY {
114 extern CONST ECHAR *el_Input;
115 STATIC ECHAR *Line = NULL;
116 STATIC CONST
char *Prompt = NULL;
117 STATIC ECHAR *Yanked = NULL;
118 STATIC
char *Screen = NULL;
127 extern int el_PushBack;
128 extern int el_Pushed;
129 FORWARD KEYMAP Map[33];
130 FORWARD KEYMAP MetaMap[64];
131 STATIC ESIZE_T Length;
132 STATIC ESIZE_T ScreenCount;
133 STATIC ESIZE_T ScreenSize;
134 STATIC ECHAR *backspace = NULL;
135 STATIC ECHAR *upline = NULL;
136 STATIC ECHAR *clrpage = NULL;
137 STATIC ECHAR *downline = NULL;
138 STATIC ECHAR *move_right = NULL;
139 STATIC ECHAR *newline = NULL;
140 STATIC ECHAR *bol = NULL;
141 STATIC ECHAR *nextline = NULL;
144 STATIC
int RequireNLforWrap = 1;
145 STATIC
int el_intr_pending = 0;
149 extern void TTYflush();
150 STATIC
void TTYput(ECHAR c);
151 STATIC
void TTYputs(ECHAR *p);
152 STATIC
void TTYshow(ECHAR c);
153 STATIC
void TTYstring(ECHAR *p);
154 extern unsigned int TTYget();
155 STATIC
void TTYinfo();
156 STATIC
void print_columns(
int ac,
char **av);
157 STATIC
void reposition(
int reset);
158 STATIC
void left(STATUS Change);
159 STATIC
void right(STATUS Change);
160 STATIC STATUS ring_bell();
162 STATIC STATUS do_macro(
unsigned int c);
164 STATIC STATUS do_forward(STATUS move);
165 STATIC STATUS do_case(ECHAR type);
166 STATIC STATUS case_down_word();
167 STATIC STATUS case_up_word();
169 STATIC
void clear_line();
170 STATIC STATUS insert_string(ECHAR *p);
171 STATIC ECHAR *next_hist();
172 STATIC ECHAR *prev_hist();
173 STATIC STATUS do_insert_hist(ECHAR *p);
174 STATIC STATUS do_hist(ECHAR *(*move)());
175 STATIC STATUS h_next();
176 STATIC STATUS h_prev();
177 STATIC STATUS h_first();
178 STATIC STATUS h_last();
179 STATIC
int substrcmp(
char *text,
char *pat,
int len);
180 STATIC ECHAR *search_hist(ECHAR *search, ECHAR *(*move)());
181 STATIC STATUS h_search();
182 STATIC STATUS fd_char();
183 STATIC
void save_yank(
int begin,
int i);
184 STATIC STATUS delete_string(
int count);
185 STATIC STATUS bk_char();
186 STATIC STATUS bk_del_char();
187 STATIC STATUS redisplay();
188 STATIC STATUS kill_line();
189 STATIC
char *rsearch_hist(
char *patt,
int *lpos,
int *cpos);
190 STATIC STATUS h_risearch();
191 STATIC STATUS insert_char(
int c);
192 STATIC STATUS meta();
193 STATIC STATUS emacs(
unsigned int c);
194 STATIC STATUS TTYspecial(
unsigned int c);
195 STATIC ECHAR *editinput();
196 STATIC
void hist_add(ECHAR *p);
197 STATIC STATUS beg_line();
198 STATIC STATUS del_char();
199 STATIC STATUS end_line();
200 STATIC ECHAR *find_word();
201 STATIC STATUS c_complete();
202 STATIC STATUS c_possible();
203 STATIC STATUS accept_line();
204 STATIC STATUS transpose();
205 STATIC STATUS quote();
206 STATIC STATUS wipe();
207 STATIC STATUS mk_set();
208 STATIC STATUS exchange();
209 STATIC STATUS yank();
210 STATIC STATUS copy_region();
211 STATIC STATUS move_to_char();
212 STATIC STATUS fd_word();
213 STATIC STATUS fd_kill_word();
214 STATIC STATUS bk_word();
215 STATIC STATUS bk_kill_word();
216 STATIC
int argify(ECHAR *line, ECHAR ***avp);
217 STATIC STATUS last_argument();
220 int rl_meta_chars = 0;
225 STATIC ECHAR *editinput();
226 #if defined(USE_TERMCAP)
227 extern char *getenv();
228 extern char *tgetstr();
229 extern int tgetent();
230 extern int tgetnum();
241 (void)write(1, Screen, ScreenCount);
246 STATIC
void TTYput(ECHAR c)
248 Screen[ScreenCount] = c;
249 if (++ScreenCount >= ScreenSize - 1) {
250 ScreenSize += SCREEN_INC;
251 RENEW(Screen,
char, ScreenSize);
255 STATIC
void TTYputs(ECHAR *p)
261 STATIC
void TTYshow(ECHAR c)
271 else if (rl_meta_chars && ISMETA(c)) {
280 STATIC
void TTYstring(ECHAR *p)
288 #define TTYback() (backspace ? TTYputs((ECHAR *)backspace) : TTYput('\b'))
291 STATIC
int printlen(CONST
char *p)
296 if ((*p == DEL) || (ISCTL(*p)))
298 else if (rl_meta_chars && ISMETA(*p))
306 STATIC
int screen_pos()
310 int p = strlen(Prompt);
313 for (i=0; i < Point; i++)
314 if ((Line[i] == DEL) ||
317 else if (rl_meta_chars && ISMETA(Line[i]))
325 STATIC
void TTYback()
329 int sp = screen_pos();
331 if (upline && sp && (sp%TTYwidth == 0))
335 for (i=0; i < TTYwidth; i++)
339 TTYputs((ECHAR *)backspace);
344 STATIC
void TTYinfo()
347 #if defined(USE_TERMCAP)
353 #if defined(TIOCGWINSZ)
358 #if defined(TIOCGWINSZ)
360 if (ioctl(0, TIOCGWINSZ, &W) >= 0
361 && W.ws_col > 0 && W.ws_row > 0) {
362 TTYwidth = (int)W.ws_col;
363 TTYrows = (
int)W.ws_row;
370 TTYwidth = TTYrows = 0;
371 #if defined(USE_TERMCAP)
372 buff = walloc(
char,2048);
373 buff2 = walloc(
char,2048);
375 if ((term = getenv(
"TERM")) == NULL)
377 if (tgetent(buff, term) < 0) {
378 TTYwidth = SCREEN_WIDTH;
379 TTYrows = SCREEN_ROWS;
382 backspace = (ECHAR *)tgetstr(
"le", &bp);
383 upline = (ECHAR *)tgetstr(
"up", &bp);
384 clrpage = (ECHAR *)tgetstr(
"cl", &bp);
385 nextline = (ECHAR *)tgetstr(
"nl", &bp);
387 nextline = (ECHAR *)
"\n";
388 if (strncmp(term,
"pcansi", 6)==0 || strncmp(term,
"cygwin", 6)==0)
390 bol = (ECHAR *)
"\033[0G";
391 RequireNLforWrap = 0;
394 bol = (ECHAR *)tgetstr(
"cr", &bp);
398 newline= walloc(ECHAR, 20);
399 strcpy((
char *)newline,(
char *)bol);
400 strcat((
char *)newline,(
char *)nextline);
402 downline = (ECHAR *)newline;
403 move_right = (ECHAR *)tgetstr(
"nd", &bp);
404 if (!move_right || !downline)
406 TTYwidth = tgetnum(
"co");
407 TTYrows = tgetnum(
"li");
410 #if defined(TIOCGWINSZ)
411 if (ioctl(0, TIOCGWINSZ, &W) >= 0) {
412 TTYwidth = (int)W.ws_col;
413 TTYrows = (
int)W.ws_row;
417 if (TTYwidth <= 0 || TTYrows <= 0) {
418 TTYwidth = SCREEN_WIDTH;
419 TTYrows = SCREEN_ROWS;
427 STATIC
void print_columns(
int ac,
char **av)
441 TTYputs((ECHAR *)newline);
442 sprintf(info1,
"There are %d possibilities. Do you really \n",ac);
443 TTYputs((ECHAR *)info1);
444 TTYputs((ECHAR *)
"want to see them all (y/n) ? ");
445 while (((c = TTYget()) != EOF) && ((strchr(
"YyNn ",c) == NULL)))
447 if (strchr(
"Nn",c) != NULL)
449 TTYputs((ECHAR *)newline);
455 for (longest = 0, i = 0; i < ac; i++)
456 if ((j = strlen((
char *)av[i])) > longest)
458 cols = TTYwidth / (longest + 3);
459 if (cols < 1) cols = 1;
461 TTYputs((ECHAR *)newline);
462 for (skip = ac / cols + 1, i = 0; i < skip; i++) {
463 for (j = i; j < ac; j += skip) {
464 for (p = (ECHAR *)av[j], len = strlen((
char *)p), k = len;
468 while (++len < longest + 3)
471 TTYputs((ECHAR *)newline);
475 STATIC
void reposition(
int reset)
484 for (i=screen_pos()/TTYwidth; i > 0; i--)
485 if (upline) TTYputs(upline);
487 TTYputs((ECHAR *)Prompt);
488 pos = printlen(Prompt);
490 for (i = 0; i < End; i++)
494 pos += printlen(ppp);
495 if ((pos%TTYwidth) == 0)
496 if (RequireNLforWrap && downline) TTYputs(downline);
503 if (rl_meta_chars && ISMETA(Line[Point]))
508 else if (ISCTL(Line[Point]))
515 STATIC
void left(STATUS Change)
519 if (ISCTL(Line[Point - 1]))
521 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
526 if (Change == CSmove)
530 STATIC
void right(STATUS Change)
532 TTYshow(Line[Point]);
533 if (Change == CSmove)
535 if ((screen_pos())%TTYwidth == 0)
536 if (downline && RequireNLforWrap) TTYputs(downline);
539 STATIC STATUS ring_bell()
547 STATIC STATUS do_macro(
unsigned int c)
556 if ((el_Input = (ECHAR *)getenv((
char *)name)) == NULL) {
564 STATIC STATUS do_forward(STATUS move)
573 for ( ; Point < End && (*p ==
' ' || !isalnum(*p)); p++)
576 for (; Point < End && isalnum(*p); p++)
581 }
while (++i < Repeat);
586 STATIC STATUS do_case(ECHAR type)
595 (void)do_forward(CSstay);
597 if ((count = Point - OP) < 0)
599 for ( ; Point > OP; Point --)
601 if ((end = Point + count) > End)
603 for (i = Point, p = &Line[Point]; Point < end; p++) {
604 if ((type == TOupper) ||
605 ((type == TOcapitalize) && (Point == i)))
610 else if (isupper(*p))
618 STATIC STATUS case_down_word()
620 return do_case(TOlower);
623 STATIC STATUS case_up_word()
625 return do_case(TOupper);
628 STATIC STATUS case_cap_word()
630 return do_case(TOcapitalize);
640 for (extras = 0, i = Point, p = &Line[i]; i < End; i++, p++) {
647 else if (rl_meta_chars && ISMETA(*p)) {
652 else if ((screen_pos())%TTYwidth == 0)
653 if (downline && RequireNLforWrap) TTYputs(downline);
661 if (rl_meta_chars && ISMETA(Line[Point-1]))
666 else if (ISCTL(Line[Point-1]))
674 STATIC
void clear_line()
678 for (i=screen_pos()/TTYwidth; i > 0; i--)
679 if (upline) TTYputs(upline);
680 for (i=0; i < strlen(Prompt); i++)
686 for (i=screen_pos()/TTYwidth; i > 0; i--)
687 if (upline) TTYputs(upline);
693 STATIC STATUS insert_string(ECHAR *p)
700 len = strlen((
char *)p);
701 if (End + len >= Length) {
702 if ((
new = NEW(ECHAR, Length + len + MEM_INC)) == NULL)
705 COPYFROMTO(
new, Line, Length);
709 Length += len + MEM_INC;
712 for (q = &Line[Point], i = End - Point; --i >= 0; )
714 COPYFROMTO(&Line[Point], p, len);
718 pos1 = printlen((
char *)&Line[Point]);
719 TTYstring(&Line[Point]);
721 if ((pos0+pos1)%TTYwidth == 0)
722 if (downline && RequireNLforWrap) TTYputs(downline);
726 if (upline && (Point != End))
729 pos1 = printlen((
char *)&Line[Point]);
730 for (i=((pos0%TTYwidth)+pos1)/TTYwidth; i > 0; i--)
731 if (upline) TTYputs(upline);
733 for (i=0 ; i < (pos0%TTYwidth); i++)
737 return Point == End ? CSstay : CSmove;
741 STATIC ECHAR *next_hist()
743 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
746 STATIC ECHAR *prev_hist()
748 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
751 STATIC STATUS do_insert_hist(ECHAR *p)
756 for (i=screen_pos()/TTYwidth; i > 0; i--)
757 if (upline) TTYputs(upline);
762 return insert_string(p);
765 STATIC STATUS do_hist(ECHAR *(*move)())
772 if ((p = (*move)()) == NULL)
774 }
while (++i < Repeat);
775 return do_insert_hist(p);
778 STATIC STATUS h_next()
780 return do_hist(next_hist);
783 STATIC STATUS h_prev()
785 return do_hist(prev_hist);
788 STATIC STATUS h_first()
790 return do_insert_hist(H.Lines[H.Pos = 0]);
793 STATIC STATUS h_last()
795 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
801 STATIC
int substrcmp(
char *text,
char *pat,
int len)
805 if ((c = *pat) ==
'\0')
806 return *text ==
'\0';
807 for ( ; *text; text++)
808 if (*text == c && strncmp(text, pat, len) == 0)
813 STATIC ECHAR *search_hist(ECHAR *search, ECHAR *(*move)())
815 static ECHAR *old_search;
822 if (search && *search) {
825 old_search = (ECHAR *)STRDUP((
const char *)search);
828 if (old_search == NULL || *old_search ==
'\0')
834 if (*search ==
'^') {
836 pat = (
char *)(search + 1);
840 pat = (
char *)search;
844 for (pos = H.Pos; (*move)() != NULL; )
845 if ((*match)((
char *)H.Lines[H.Pos], pat, len) == 0)
846 return H.Lines[H.Pos];
851 STATIC STATUS h_search()
853 static int Searching;
854 CONST
char *old_prompt;
865 TTYputs((ECHAR *)Prompt);
866 move = Repeat == NO_ARG ? prev_hist : next_hist;
867 p = search_hist(editinput(), move);
870 TTYputs((ECHAR *)Prompt);
873 return do_insert_hist(p);
876 STATIC STATUS fd_char()
885 }
while (++i < Repeat);
889 STATIC
void save_yank(
int begin,
int i)
899 if ((Yanked = NEW(ECHAR, (ESIZE_T)i + 1)) != NULL) {
900 COPYFROMTO(Yanked, &Line[begin], i);
905 STATIC STATUS delete_string(
int count)
911 if (count <= 0 || End == Point)
914 if (Point + count > End && (count = End - Point) <= 0)
918 save_yank(Point, count);
920 tLine = STRDUP((
char *)Line);
922 for (q = Point, i = End - (Point + count) + 1; --i >= 0; q++)
923 Line[q] = tLine[q+count];
927 pos1 = printlen((
char *)&Line[Point]);
928 TTYstring(&Line[Point]);
929 if ((pos1 > 0) && (pos0+pos1)%TTYwidth == 0)
930 if (downline && RequireNLforWrap) TTYputs(downline);
936 for (i=((pos0%TTYwidth)+pos1)/TTYwidth; i > 0; i--)
937 if (upline) TTYputs(upline);
939 for (i=0 ; i < (pos0%TTYwidth); i++)
946 STATIC STATUS bk_char()
955 }
while (++i < Repeat);
960 STATIC STATUS bk_del_char()
969 }
while (++i < Repeat);
971 return delete_string(i);
974 STATIC STATUS redisplay()
976 if (clrpage) TTYputs(clrpage);
978 TTYputs((ECHAR *)newline);
984 STATIC STATUS kill_line()
988 if (Repeat != NO_ARG) {
989 if (Repeat < Point) {
993 (void)delete_string(i - Point);
995 else if (Repeat > Point) {
997 (void)delete_string(Repeat - Point - 1);
1002 save_yank(Point, End - Point);
1009 STATIC
char *rsearch_hist(
char *patt,
int *lpos,
int *cpos)
1013 for (; *lpos > 0; (*lpos)--)
1015 for ( ; (*cpos) >= 0 ; (*cpos)--)
1018 if (strncmp(patt,(
char *)H.Lines[*lpos]+*cpos,strlen(patt)) == 0)
1020 return (
char *)H.Lines[*lpos];
1024 *cpos = strlen((
char *)H.Lines[(*lpos)-1]);
1029 STATIC STATUS h_risearch()
1032 CONST
char *old_prompt;
1033 char *pat, *hist, *nhist;
1039 old_prompt = Prompt;
1041 nprompt = walloc(
char,80+160);
1042 pat = walloc(
char,80);
1047 cpos = strlen((
char *)H.Lines[lpos]);
1050 sprintf(nprompt,
"(reverse-i-search)`%s': ",pat);
1053 do_insert_hist((ECHAR *)hist);
1055 for (i=strlen((
char *)H.Lines[lpos]); i>cpos; i--) bk_char();
1057 if ((c >=
' ') || (c == CTL(
'R')))
1061 else if (patend < 79)
1072 nhist = rsearch_hist(pat,&lpos,&cpos);
1088 }
while ((c >=
' ') || (c == CTL(
'R')));
1092 Prompt = old_prompt;
1093 TTYputs((ECHAR *)Prompt);
1097 s = do_insert_hist((ECHAR *)hist);
1099 for (i=strlen((
char *)H.Lines[lpos]); i>cpos; i--) s = bk_char();
1106 STATIC STATUS insert_char(
int c)
1114 if (Repeat == NO_ARG || Repeat < 2) {
1117 return insert_string(buff);
1120 if ((p = NEW(ECHAR, Repeat + 1)) == NULL)
1122 for (i = Repeat, q = p; --i >= 0; )
1126 s = insert_string(p);
1131 STATIC STATUS meta()
1136 if ((c = TTYget()) == EOF)
1138 #if defined(ANSI_ARROWS)
1140 if (c ==
'[' || c ==
'O')
1141 switch (c = TTYget()) {
1142 default:
return ring_bell();
1143 case EOF:
return CSeof;
1144 case 'A':
return h_prev();
1145 case 'B':
return h_next();
1146 case 'C':
return fd_char();
1147 case 'D':
return bk_char();
1152 for (Repeat = c -
'0'; (c = TTYget()) != EOF && isdigit(c); )
1153 Repeat = Repeat * 10 + c -
'0';
1161 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
1163 return (*kp->Function)();
1164 if (rl_meta_chars == 0)
1166 insert_char(META(c));
1173 STATIC STATUS emacs(
unsigned int c)
1178 if (ISMETA(c) && rl_meta_chars)
1181 el_PushBack = UNMETA(c);
1184 for (kp = Map; kp->Function; kp++)
1187 s = kp->Function ? (*kp->Function)() : insert_char((
int)c);
1194 STATIC STATUS TTYspecial(
unsigned int c)
1201 if (c == rl_erase || c == DEL)
1202 return bk_del_char();
1205 for (i=screen_pos()/TTYwidth; i > 0; i--)
1206 if (upline) TTYputs(upline);
1213 if (c == rl_intr || c == rl_quit) {
1218 el_intr_pending = 1;
1224 if (c == rl_eof && Point == 0 && End == 0)
1230 STATIC ECHAR *editinput()
1235 OldPoint = Point = Mark = End = 0;
1238 while ((c = TTYget()) != EOF)
1240 switch (TTYspecial(c)) {
1269 STATIC
void hist_add(ECHAR *p)
1273 if ((p = (ECHAR *)STRDUP((
char *)p)) == NULL)
1275 if (H.Size < editline_histsize)
1276 H.Lines[H.Size++] = p;
1278 DISPOSE(H.Lines[0]);
1279 for (i = 0; i < editline_histsize - 1; i++)
1280 H.Lines[i] = H.Lines[i + 1];
1287 void write_history(
const char *history_file)
1292 if ((fd = fopen(history_file,
"wb")) == NULL)
1294 fprintf(stderr,
"editline: can't access history file \"%s\"\n",
1299 for (i=0; i < H.Size; i++)
1300 fprintf(fd,
"%s\n",H.Lines[i]);
1304 void read_history(
const char *history_file)
1310 H.Lines = NEW(ECHAR *,editline_histsize);
1314 if ((fd = fopen(history_file,
"rb")) == NULL)
1317 while ((c=getc(fd)) != EOF)
1320 for (i=0; ((c=getc(fd)) !=
'\n') && (c != EOF); i++)
1335 rl_reset_terminal(
char *p)
1344 char *readline(CONST
char *prompt)
1350 if ((Line = NEW(ECHAR, Length)) == NULL)
1357 ScreenSize = SCREEN_INC;
1358 Screen = NEW(
char, ScreenSize);
1359 Prompt = prompt ? prompt : (
char *)el_NIL;
1360 el_intr_pending = 0;
1361 if (el_no_echo == 1)
1364 TTYputs((ECHAR *)Prompt);
1369 TTYputs((ECHAR *)Prompt);
1372 line = (ECHAR *)STRDUP((
char *)line);
1373 TTYputs((ECHAR *)newline);
1378 DISPOSE(H.Lines[--H.Size]);
1379 if (el_intr_pending)
1381 return (
char *)line;
1388 if (p == NULL || *p ==
'\0')
1391 #if defined(UNIQUE_HISTORY)
1392 if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
1395 hist_add((ECHAR *)p);
1399 STATIC STATUS beg_line()
1403 for (i=screen_pos()/TTYwidth; i > 0; i--)
1404 if (upline) TTYputs(upline);
1411 STATIC STATUS del_char()
1413 return delete_string(Repeat == NO_ARG ? 1 : Repeat);
1416 STATIC STATUS end_line()
1421 TTYput(Line[Point]);
1433 STATIC ECHAR *find_word()
1435 static char SEPS[] =
"#;&|^$=`'{}()<>\n\t ";
1440 for (p = &Line[Point]; p > Line && strchr(SEPS, (
char)p[-1]) == NULL; p--)
1442 len = Point - (p - Line) + 1;
1443 if ((
new = NEW(ECHAR, len)) == NULL)
1445 COPYFROMTO(
new, p, len);
1446 new[len - 1] =
'\0';
1455 char *el_current_sym()
1458 char *symbol = NULL;
1470 (strchr(
"()' \t\n\r",Line[i]) != NULL));
1476 for (; i < End; i++)
1477 if (strchr(
"()' \t\n\r\"",Line[i]) != NULL)
1479 for (j=i-1; j >=0; j--)
1480 if (strchr(
"()' \t\n\r\"",Line[j]) != NULL)
1483 symbol = walloc(
char,i-j);
1484 strncpy(symbol,(
char *)&Line[j+1],i-(j+1));
1485 symbol[i-(j+1)] =
'\0';
1490 static char *completion_to_ambiguity(
int index,
char **possibles)
1501 for ( ; extending; e++)
1503 for (i=0; possibles[i] != NULL; i++)
1504 if (possibles[i][e] != possibles[0][e])
1516 p = walloc(
char,(e-index)+1);
1517 strncpy(p,possibles[0]+index,e-index);
1523 static char **el_file_completion_function(
char * text,
int start,
int end)
1532 word = walloc(
char,(end-start)+1);
1533 strncpy(word,text+start,end-start);
1534 word[end-start]=
'\0';
1536 ac = rl_list_possib(word,&matches1);
1542 matches2 = walloc(
char *,ac+1);
1543 for (i=0; i < ac; i++)
1544 matches2[i] = matches1[i];
1551 STATIC STATUS c_complete()
1558 char **possibles=NULL;
1560 int started_with_quote = 0;
1564 for (start=Point; start > 0; start--)
1565 if (strchr(
"()' \t\n\r\"",Line[start-1]) != NULL)
1567 word = walloc(
char,(Point-start)+1);
1568 strncpy(word,(
char *)(Line+start),Point-start);
1569 word[Point-start]=
'\0';
1570 if ((start > 0) && (Line[start-1] ==
'"'))
1571 started_with_quote = 1;
1573 if (el_user_completion_function)
1575 possibles = el_user_completion_function((
char *)Line,start,Point);
1576 if (possibles == NULL)
1578 possibles = el_file_completion_function((
char *)Line,start,Point);
1581 for (start=Point; start > 0; start--)
1582 if (strchr(
"()' \t\n\r\"/",Line[start-1]) != NULL)
1586 for (possiblesc=0; possibles[possiblesc] != NULL; possiblesc++);
1588 if ((!possibles) || (possiblesc == 0))
1590 else if (possiblesc == 1)
1592 p = walloc(
char,strlen(possibles[0])-(Point-start)+2);
1593 sprintf(p,
"%s ",possibles[0]+(Point-start));
1594 if ((strlen(p) > 1) && (p[strlen(p)-2] ==
'/'))
1595 p[strlen(p)-1] =
'\0';
1596 else if (started_with_quote)
1597 p[strlen(p)-1] =
'"';
1599 s = insert_string((ECHAR *)p);
1602 else if ((p = completion_to_ambiguity(Point-start,possibles)) != NULL)
1604 s = insert_string((ECHAR *)p);
1610 print_columns(possiblesc,possibles);
1615 for (i=0; possibles && possibles[i] != NULL; i++)
1616 wfree(possibles[i]);
1625 STATIC STATUS c_complete_old()
1633 p = (ECHAR *)rl_complete((
char *)word, &unique);
1637 s = insert_string(p);
1647 STATIC STATUS c_possible()
1657 ac = rl_list_possib((
char *)word, (
char ***) ((
void *) &av));
1661 print_columns(ac, (
char **)av);
1671 STATIC STATUS accept_line()
1677 #ifdef SYSTEM_IS_WIN32
1678 STATIC STATUS end_of_input()
1685 STATIC STATUS transpose()
1692 c = Line[Point - 1];
1694 Line[Point - 1] = Line[Point];
1695 TTYshow(Line[Point - 1]);
1702 STATIC STATUS quote()
1706 return (c = TTYget()) == EOF ? CSeof : insert_char((
int)c);
1709 STATIC STATUS wipe()
1723 return delete_string(Mark - Point);
1726 STATIC STATUS mk_set()
1732 STATIC STATUS exchange()
1736 if ((c = TTYget()) != CTL(
'X'))
1737 return c == EOF ? CSeof : ring_bell();
1739 if ((c = Mark) <= End) {
1747 STATIC STATUS yank()
1749 if (Yanked && *Yanked)
1750 return insert_string(Yanked);
1754 STATIC STATUS copy_region()
1760 save_yank(Mark, Point - Mark);
1762 save_yank(Point, Mark - Point);
1767 STATIC STATUS move_to_char()
1773 if ((c = TTYget()) == EOF)
1775 for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
1783 STATIC STATUS fd_word()
1785 return do_forward(CSmove);
1788 STATIC STATUS fd_kill_word()
1794 (void)do_forward(CSmove);
1797 for ( ; Point > OP; Point --)
1799 return delete_string(i);
1804 STATIC STATUS bk_word()
1811 for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
1814 for (; p > Line && p[-1] !=
' ' && isalnum(p[-1]); p--)
1819 }
while (++i < Repeat);
1824 STATIC STATUS bk_kill_word()
1827 if (OldPoint != Point)
1828 return delete_string(OldPoint - Point);
1832 STATIC
int argify(ECHAR *line, ECHAR ***avp)
1841 if ((*avp = p = NEW(ECHAR*, i))== NULL)
1844 for (c = line; isspace(*c); c++)
1846 if (*c ==
'\n' || *c ==
'\0')
1849 for (ac = 0, p[ac++] = c; *c && *c !=
'\n'; ) {
1852 if (*c && *c !=
'\n') {
1854 new = NEW(ECHAR*, i + MEM_INC);
1859 COPYFROMTO(
new, p, i *
sizeof (
char **));
1875 STATIC STATUS last_argument()
1882 if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
1885 if ((p = (ECHAR *)STRDUP((
char *)p)) == NULL)
1887 ac = argify(p, &av);
1889 if (Repeat != NO_ARG)
1890 s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
1892 s = ac ? insert_string(av[ac - 1]) : CSstay;
1900 STATIC KEYMAP Map[33] = {
1901 { CTL(
'@'), ring_bell },
1902 { CTL(
'A'), beg_line },
1903 { CTL(
'B'), bk_char },
1904 { CTL(
'D'), del_char },
1905 { CTL(
'E'), end_line },
1906 { CTL(
'F'), fd_char },
1907 { CTL(
'G'), ring_bell },
1908 { CTL(
'H'), bk_del_char },
1909 { CTL(
'I'), c_complete },
1910 { CTL(
'J'), accept_line },
1911 { CTL(
'K'), kill_line },
1912 { CTL(
'L'), redisplay },
1913 { CTL(
'M'), accept_line },
1914 { CTL(
'N'), h_next },
1915 { CTL(
'O'), ring_bell },
1916 { CTL(
'P'), h_prev },
1917 { CTL(
'Q'), ring_bell },
1918 { CTL(
'R'), h_risearch },
1919 { CTL(
'S'), h_search },
1920 { CTL(
'T'), transpose },
1921 { CTL(
'U'), ring_bell },
1922 { CTL(
'V'), quote },
1924 { CTL(
'X'), exchange },
1926 #ifdef SYSTEM_IS_WIN32
1927 { CTL(
'Z'), end_of_input },
1929 { CTL(
'Z'), ring_bell },
1932 { CTL(
']'), move_to_char },
1933 { CTL(
'^'), ring_bell },
1934 { CTL(
'_'), ring_bell },
1938 STATIC KEYMAP MetaMap[64]= {
1939 { CTL(
'H'), bk_kill_word },
1940 { DEL, bk_kill_word },
1942 {
'.', last_argument },
1945 {
'?', c_possible },
1947 {
'c', case_cap_word },
1948 {
'd', fd_kill_word },
1950 {
'l', case_down_word },
1951 {
'u', case_up_word },
1953 {
'w', copy_region },
1957 void el_bind_key_in_metamap(
char c, Keymap_Function func)
1962 for (i=0; MetaMap[i].Key != 0; i++)
1964 if (MetaMap[i].Key == c)
1966 MetaMap[i].Function = func;
1974 fprintf(stderr,
"editline: MetaMap table full, requires increase\n");
1978 MetaMap[i].Function = func;
1980 MetaMap[i+1].Function = 0;
1981 MetaMap[i+1].Key = 0;