3 ** Main editing routines for editline library.
10 ** Manifest constants.
12 #define SCREEN_WIDTH 80
13 #define SCREEN_ROWS 24
16 #define CTL(x) ((x) & 0x1F)
17 #define ISCTL(x) ((x) && (x) < ' ')
18 #define UNCTL(x) ((x) + 64)
19 #define META(x) ((x) | 0x80)
20 #define ISMETA(x) ((x) & 0x80)
21 #define UNMETA(x) ((x) & 0x7F)
22 #if !defined(HIST_SIZE)
24 #endif /* !defined(HIST_SIZE) */
27 ** Command status codes.
29 typedef enum _STATUS
{
30 CSdone
, CSeof
, CSmove
, CSdispatch
, CSstay
34 ** The type of case-changing to perform.
41 ** Key to command mapping.
43 typedef struct _KEYMAP
{
49 ** Command history structure.
51 typedef struct _HISTORY
{
54 CHAR
*Lines
[HIST_SIZE
];
65 STATIC CHAR NIL
[] = "";
66 STATIC CONST CHAR
*Input
= NIL
;
68 STATIC CONST
char *Prompt
;
71 STATIC
char NEWLINE
[]= CRLF
;
81 STATIC KEYMAP Map
[33];
82 STATIC KEYMAP MetaMap
[16];
84 STATIC SIZE_T ScreenCount
;
85 STATIC SIZE_T ScreenSize
;
86 STATIC
char *backspace
;
90 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
91 int rl_meta_chars
= 1;
96 STATIC CHAR
*editinput();
99 #if defined(USE_TERMCAP)
100 extern char *getenv();
101 extern char *tgetstr();
102 extern int tgetent();
103 #endif /* defined(USE_TERMCAP) */
106 ** TTY input/output functions.
113 (void)write(1, Screen
, ScreenCount
);
122 Screen
[ScreenCount
] = c
;
123 if (++ScreenCount
>= ScreenSize
- 1) {
124 ScreenSize
+= SCREEN_INC
;
125 RENEW(Screen
, char, ScreenSize
);
149 else if (rl_meta_chars
&& ISMETA(c
)) {
178 return read(0, &c
, (SIZE_T
)1) == 1 ? c
: EOF
;
181 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
195 #if defined(USE_TERMCAP)
199 #endif /* defined(USE_TERMCAP) */
200 #if defined(TIOCGWINSZ)
202 #endif /* defined(TIOCGWINSZ) */
205 #if defined(TIOCGWINSZ)
206 /* Perhaps we got resized. */
207 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0
208 && W
.ws_col
> 0 && W
.ws_row
> 0) {
209 TTYwidth
= (int)W
.ws_col
;
210 TTYrows
= (int)W
.ws_row
;
212 #endif /* defined(TIOCGWINSZ) */
217 TTYwidth
= TTYrows
= 0;
218 #if defined(USE_TERMCAP)
220 if ((term
= getenv("TERM")) == NULL
)
222 if (tgetent(buff
, term
) < 0) {
223 TTYwidth
= SCREEN_WIDTH
;
224 TTYrows
= SCREEN_ROWS
;
227 backspace
= tgetstr("le", &bp
);
228 TTYwidth
= tgetnum("co");
229 TTYrows
= tgetnum("li");
230 #endif /* defined(USE_TERMCAP) */
232 #if defined(TIOCGWINSZ)
233 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0) {
234 TTYwidth
= (int)W
.ws_col
;
235 TTYrows
= (int)W
.ws_row
;
237 #endif /* defined(TIOCGWINSZ) */
239 if (TTYwidth
<= 0 || TTYrows
<= 0) {
240 TTYwidth
= SCREEN_WIDTH
;
241 TTYrows
= SCREEN_ROWS
;
254 TTYputs((CHAR
*)Prompt
);
255 for (i
= Point
, p
= Line
; --i
>= 0; p
++)
265 if (ISCTL(Line
[Point
- 1]))
267 else if (rl_meta_chars
&& ISMETA(Line
[Point
- 1])) {
272 if (Change
== CSmove
)
280 TTYshow(Line
[Point
]);
281 if (Change
== CSmove
)
304 if ((Input
= (CHAR
*)getenv((char *)name
)) == NULL
) {
321 for ( ; Point
< End
&& (*p
== ' ' || !isalnum(*p
)); Point
++, p
++)
325 for (; Point
< End
&& isalnum(*p
); Point
++, p
++)
331 } while (++i
< Repeat
);
345 (void)do_forward(CSstay
);
346 if (OldPoint
!= Point
) {
347 if ((count
= Point
- OldPoint
) < 0)
350 if ((end
= Point
+ count
) > End
)
352 for (i
= Point
, p
= &Line
[i
]; i
< end
; i
++, p
++) {
353 if (type
== TOupper
) {
357 else if (isupper(*p
))
368 return do_case(TOlower
);
374 return do_case(TOupper
);
384 for (extras
= 0, i
= Point
, p
= &Line
[i
]; i
<= End
; i
++, p
++) {
390 else if (rl_meta_chars
&& ISMETA(*p
)) {
397 for (i
+= extras
; i
> Point
; i
--)
404 Point
= -strlen(Prompt
);
421 len
= strlen((char *)p
);
422 if (End
+ len
>= Length
) {
423 if ((new = NEW(CHAR
, Length
+ len
+ MEM_INC
)) == NULL
)
426 COPYFROMTO(new, Line
, Length
);
430 Length
+= len
+ MEM_INC
;
433 for (q
= &Line
[Point
], i
= End
- Point
; --i
>= 0; )
435 COPYFROMTO(&Line
[Point
], p
, len
);
438 TTYstring(&Line
[Point
]);
441 return Point
== End
? CSstay
: CSmove
;
448 return H
.Pos
>= H
.Size
- 1 ? NULL
: H
.Lines
[++H
.Pos
];
454 return H
.Pos
== 0 ? NULL
: H
.Lines
[--H
.Pos
];
467 return insert_string(p
);
479 if ((p
= (*move
)()) == NULL
)
481 } while (++i
< Repeat
);
482 return do_insert_hist(p
);
488 return do_hist(next_hist
);
494 return do_hist(prev_hist
);
500 return do_insert_hist(H
.Lines
[H
.Pos
= 0]);
506 return do_insert_hist(H
.Lines
[H
.Pos
= H
.Size
- 1]);
510 ** Return zero if pat appears as a substring in text.
513 substrcmp(text
, pat
, len
)
520 if ((c
= *pat
) == '\0')
521 return *text
== '\0';
522 for ( ; *text
; text
++)
523 if ((CHAR
)*text
== c
&& strncmp(text
, pat
, len
) == 0)
529 search_hist(search
, move
)
533 static CHAR
*old_search
;
539 /* Save or get remembered search pattern. */
540 if (search
&& *search
) {
543 old_search
= (CHAR
*)strdup((char *)search
);
546 if (old_search
== NULL
|| *old_search
== '\0')
551 /* Set up pattern-finder. */
552 if (*search
== '^') {
554 pat
= (char *)(search
+ 1);
558 pat
= (char *)search
;
562 for (pos
= H
.Pos
; (*move
)() != NULL
; )
563 if ((*match
)((char *)H
.Lines
[H
.Pos
], pat
, len
) == 0)
564 return H
.Lines
[H
.Pos
];
572 static int Searching
;
573 CONST
char *old_prompt
;
584 TTYputs((CHAR
*)Prompt
);
585 move
= Repeat
== NO_ARG
? prev_hist
: next_hist
;
586 p
= search_hist(editinput(), move
);
589 TTYputs((CHAR
*)Prompt
);
592 return do_insert_hist(p
);
605 } while (++i
< Repeat
);
622 if ((Yanked
= NEW(CHAR
, (SIZE_T
)i
+ 1)) != NULL
) {
623 COPYFROMTO(Yanked
, &Line
[begin
], i
);
635 if (count
<= 0 || End
== Point
)
638 if (count
== 1 && Point
== End
- 1) {
639 /* Optimize common case of delete at end of line. */
648 else if (rl_meta_chars
&& ISMETA(*p
)) {
657 if (Point
+ count
> End
&& (count
= End
- Point
) <= 0)
661 save_yank(Point
, count
);
663 for (p
= &Line
[Point
], i
= End
- (Point
+ count
) + 1; --i
>= 0; p
++)
667 TTYstring(&Line
[Point
]);
681 } while (++i
< Repeat
);
696 } while (++i
< Repeat
);
698 return delete_string(i
);
704 TTYputs((CHAR
*)NEWLINE
);
705 TTYputs((CHAR
*)Prompt
);
715 if (Repeat
!= NO_ARG
) {
716 if (Repeat
< Point
) {
720 (void)delete_string(i
- Point
);
722 else if (Repeat
> Point
) {
724 (void)delete_string(Repeat
- Point
- 1);
729 save_yank(Point
, End
- Point
);
746 if (Repeat
== NO_ARG
|| Repeat
< 2) {
749 return insert_string(buff
);
752 if ((p
= NEW(CHAR
, Repeat
+ 1)) == NULL
)
754 for (i
= Repeat
, q
= p
; --i
>= 0; )
758 s
= insert_string(p
);
769 if ((c
= TTYget()) == EOF
)
771 #if defined(ANSI_ARROWS)
772 /* Also include VT-100 arrows. */
773 if (c
== '[' || c
== 'O')
774 switch (c
= TTYget()) {
775 default: return ring_bell();
776 case EOF
: return CSeof
;
777 case 'A': return h_prev();
778 case 'B': return h_next();
779 case 'C': return fd_char();
780 case 'D': return bk_char();
782 #endif /* defined(ANSI_ARROWS) */
785 for (Repeat
= c
- '0'; (c
= TTYget()) != EOF
&& isdigit(c
); )
786 Repeat
= Repeat
* 10 + c
- '0';
794 for (OldPoint
= Point
, kp
= MetaMap
; kp
->Function
; kp
++)
796 return (*kp
->Function
)();
810 PushBack
= UNMETA(c
);
813 for (kp
= Map
; kp
->Function
; kp
++)
816 s
= kp
->Function
? (*kp
->Function
)() : insert_char((int)c
);
818 /* No pushback means no repeat count; hacky, but true. */
830 if (c
== rl_erase
|| c
== DEL
)
831 return bk_del_char();
840 if (c
== rl_intr
|| c
== rl_quit
) {
845 if (c
== rl_eof
&& Point
== 0 && End
== 0)
857 OldPoint
= Point
= Mark
= End
= 0;
860 while ((c
= TTYget()) != EOF
)
861 switch (TTYspecial(c
)) {
895 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
897 if (H
.Size
< HIST_SIZE
)
898 H
.Lines
[H
.Size
++] = p
;
901 for (i
= 0; i
< HIST_SIZE
- 1; i
++)
902 H
.Lines
[i
] = H
.Lines
[i
+ 1];
909 ** For compatibility with FSF readline.
931 if ((Line
= NEW(CHAR
, Length
)) == NULL
)
938 ScreenSize
= SCREEN_INC
;
939 Screen
= NEW(char, ScreenSize
);
940 Prompt
= prompt
? prompt
: (char *)NIL
;
941 TTYputs((CHAR
*)Prompt
);
942 if ((line
= editinput()) != NULL
) {
943 line
= (CHAR
*)strdup((char *)line
);
944 TTYputs((CHAR
*)NEWLINE
);
949 DISPOSE(H
.Lines
[--H
.Size
]);
957 if (p
== NULL
|| *p
== '\0')
960 #if defined(UNIQUE_HISTORY)
961 if (H
.Pos
&& strcmp(p
, H
.Lines
[H
.Pos
- 1]) == 0)
963 #endif /* defined(UNIQUE_HISTORY) */
981 return delete_string(Repeat
== NO_ARG
? 1 : Repeat
);
1009 c
= Line
[Point
- 1];
1011 Line
[Point
- 1] = Line
[Point
];
1012 TTYshow(Line
[Point
- 1]);
1024 return (c
= TTYget()) == EOF
? CSeof
: insert_char((int)c
);
1042 return delete_string(Mark
- Point
);
1057 if ((c
= TTYget()) != CTL('X'))
1058 return c
== EOF
? CSeof
: ring_bell();
1060 if ((c
= Mark
) <= End
) {
1071 if (Yanked
&& *Yanked
)
1072 return insert_string(Yanked
);
1083 save_yank(Mark
, Point
- Mark
);
1085 save_yank(Point
, Mark
- Point
);
1097 if ((c
= TTYget()) == EOF
)
1099 for (i
= Point
+ 1, p
= &Line
[i
]; i
< End
; i
++, p
++)
1110 return do_forward(CSmove
);
1118 (void)do_forward(CSstay
);
1119 if (OldPoint
!= Point
) {
1120 i
= Point
- OldPoint
;
1122 return delete_string(i
);
1135 for (p
= &Line
[Point
]; p
> Line
&& !isalnum(p
[-1]); p
--)
1138 for (; p
> Line
&& p
[-1] != ' ' && isalnum(p
[-1]); p
--)
1143 } while (++i
< Repeat
);
1152 if (OldPoint
!= Point
)
1153 return delete_string(OldPoint
- Point
);
1169 if ((*avp
= p
= NEW(CHAR
*, i
))== NULL
)
1172 for (c
= line
; isspace(*c
); c
++)
1174 if (*c
== '\n' || *c
== '\0')
1177 for (ac
= 0, p
[ac
++] = c
; *c
&& *c
!= '\n'; ) {
1180 if (*c
&& *c
!= '\n') {
1182 new = NEW(CHAR
*, i
+ MEM_INC
);
1187 COPYFROMTO(new, p
, i
* sizeof (char **));
1211 if (H
.Size
== 1 || (p
= H
.Lines
[H
.Size
- 2]) == NULL
)
1214 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
1216 ac
= argify(p
, &av
);
1218 if (Repeat
!= NO_ARG
)
1219 s
= Repeat
< ac
? insert_string(av
[Repeat
]) : ring_bell();
1221 s
= ac
? insert_string(av
[ac
- 1]) : CSstay
;
1229 STATIC KEYMAP Map
[33] = {
1230 { CTL('@'), ring_bell
},
1231 { CTL('A'), beg_line
},
1232 { CTL('B'), bk_char
},
1233 { CTL('D'), del_char
},
1234 { CTL('E'), end_line
},
1235 { CTL('F'), fd_char
},
1236 { CTL('G'), ring_bell
},
1237 { CTL('H'), bk_del_char
},
1238 { CTL('I'), ring_bell
},
1239 { CTL('J'), accept_line
},
1240 { CTL('K'), kill_line
},
1241 { CTL('L'), redisplay
},
1242 { CTL('M'), accept_line
},
1243 { CTL('N'), h_next
},
1244 { CTL('O'), ring_bell
},
1245 { CTL('P'), h_prev
},
1246 { CTL('Q'), ring_bell
},
1247 { CTL('R'), h_search
},
1248 { CTL('S'), ring_bell
},
1249 { CTL('T'), transpose
},
1250 { CTL('U'), ring_bell
},
1251 { CTL('V'), quote
},
1253 { CTL('X'), exchange
},
1255 { CTL('Z'), ring_bell
},
1257 { CTL(']'), move_to_char
},
1258 { CTL('^'), ring_bell
},
1259 { CTL('_'), ring_bell
},
1263 STATIC KEYMAP MetaMap
[16]= {
1264 { CTL('H'), bk_kill_word
},
1265 { DEL
, bk_kill_word
},
1267 { '.', last_argument
},
1272 { 'd', fd_kill_word
},
1274 { 'l', case_down_word
},
1275 { 'u', case_up_word
},
1277 { 'w', copy_region
},