Home | History | Annotate | Line # | Download | only in ksh
emacs.c revision 1.11
      1  1.11       sjg /*	$NetBSD: emacs.c,v 1.11 2002/01/25 23:40:51 sjg Exp $	*/
      2   1.2       tls 
      3   1.1       jtc /*
      4   1.1       jtc  *  Emacs-like command line editing and history
      5   1.1       jtc  *
      6   1.1       jtc  *  created by Ron Natalie at BRL
      7   1.1       jtc  *  modified by Doug Kingston, Doug Gwyn, and Lou Salkind
      8   1.1       jtc  *  adapted to PD ksh by Eric Gisin
      9   1.1       jtc  */
     10   1.1       jtc 
     11   1.1       jtc #include "config.h"
     12   1.1       jtc #ifdef EMACS
     13   1.1       jtc 
     14   1.1       jtc #include "sh.h"
     15   1.1       jtc #include "ksh_stat.h"
     16   1.1       jtc #include "ksh_dir.h"
     17   1.1       jtc #include <ctype.h>
     18   1.1       jtc #include "edit.h"
     19   1.1       jtc 
     20   1.1       jtc static	Area	aedit;
     21   1.1       jtc #define	AEDIT	&aedit		/* area for kill ring and macro defns */
     22   1.1       jtc 
     23   1.1       jtc #undef CTRL			/* _BSD brain damage */
     24   1.1       jtc #define	CTRL(x)		((x) == '?' ? 0x7F : (x) & 0x1F)	/* ASCII */
     25   1.1       jtc #define	UNCTRL(x)	((x) == 0x7F ? '?' : (x) | 0x40)	/* ASCII */
     26   1.1       jtc 
     27   1.1       jtc 
     28   1.1       jtc /* values returned by keyboard functions */
     29   1.1       jtc #define	KSTD	0
     30   1.1       jtc #define	KEOL	1		/* ^M, ^J */
     31   1.1       jtc #define	KINTR	2		/* ^G, ^C */
     32   1.1       jtc 
     33   1.1       jtc struct	x_ftab  {
     34   1.1       jtc 	int		(*xf_func) ARGS((int c));
     35   1.1       jtc 	const char	*xf_name;
     36   1.1       jtc 	short		xf_flags;
     37   1.1       jtc };
     38   1.1       jtc 
     39   1.1       jtc /* index into struct x_ftab x_ftab[] - small is good */
     40   1.1       jtc typedef unsigned char Findex;
     41   1.1       jtc 
     42   1.1       jtc struct x_defbindings {
     43   1.1       jtc 	Findex		xdb_func;	/* XFUNC_* */
     44   1.6   hubertf 	unsigned char	xdb_tab;
     45   1.1       jtc 	unsigned char	xdb_char;
     46   1.1       jtc };
     47   1.1       jtc 
     48   1.1       jtc #define XF_ARG		1	/* command takes number prefix */
     49   1.1       jtc #define	XF_NOBIND	2	/* not allowed to bind to function */
     50   1.1       jtc #define	XF_PREFIX	4	/* function sets prefix */
     51   1.1       jtc 
     52   1.1       jtc /* Separator for completion */
     53   1.1       jtc #define	is_cfs(c)	(c == ' ' || c == '\t' || c == '"' || c == '\'')
     54   1.5  christos #define	is_mfs(c)	(!(isalnum((unsigned char)c) || c == '_' || c == '$'))  /* Separator for motion */
     55   1.1       jtc 
     56   1.1       jtc #ifdef OS2
     57   1.1       jtc   /* Deal with 8 bit chars & an extra prefix for function key (these two
     58   1.1       jtc    * changes increase memory usage from 9,216 bytes to 24,416 bytes...)
     59   1.1       jtc    */
     60   1.1       jtc # define CHARMASK	0xFF		/* 8-bit ASCII character mask */
     61   1.1       jtc # define X_NTABS	4		/* normal, meta1, meta2, meta3 */
     62   1.1       jtc static int	x_prefix3 = 0xE0;
     63   1.1       jtc #else /* OS2 */
     64   1.6   hubertf # define CHARMASK	0xFF		/* 8-bit character mask */
     65   1.1       jtc # define X_NTABS	3		/* normal, meta1, meta2 */
     66   1.1       jtc #endif /* OS2 */
     67   1.6   hubertf #define X_TABSZ		(CHARMASK+1)	/* size of keydef tables etc */
     68   1.1       jtc 
     69   1.1       jtc /* Arguments for do_complete()
     70   1.1       jtc  * 0 = enumerate  M-= complete as much as possible and then list
     71   1.1       jtc  * 1 = complete   M-Esc
     72   1.1       jtc  * 2 = list       M-?
     73   1.1       jtc  */
     74   1.1       jtc typedef enum { CT_LIST, 	/* list the possible completions */
     75   1.1       jtc 		 CT_COMPLETE,	/* complete to longest prefix */
     76   1.1       jtc 		 CT_COMPLIST	/* complete and then list (if non-exact) */
     77   1.1       jtc 	} Comp_type;
     78   1.1       jtc 
     79   1.1       jtc /* { from 4.9 edit.h */
     80   1.1       jtc /*
     81   1.1       jtc  * The following are used for my horizontal scrolling stuff
     82   1.1       jtc  */
     83   1.1       jtc static char   *xbuf;		/* beg input buffer */
     84   1.1       jtc static char   *xend;		/* end input buffer */
     85   1.1       jtc static char    *xcp;		/* current position */
     86   1.1       jtc static char    *xep;		/* current end */
     87   1.1       jtc static char    *xbp;		/* start of visible portion of input buffer */
     88   1.1       jtc static char    *xlp;		/* last char visible on screen */
     89   1.1       jtc static int	x_adj_ok;
     90   1.1       jtc /*
     91   1.1       jtc  * we use x_adj_done so that functions can tell
     92   1.1       jtc  * whether x_adjust() has been called while they are active.
     93   1.1       jtc  */
     94   1.1       jtc static int	x_adj_done;
     95   1.1       jtc 
     96   1.1       jtc static int	xx_cols;
     97   1.1       jtc static int	x_col;
     98   1.1       jtc static int	x_displen;
     99   1.1       jtc static int	x_arg;		/* general purpose arg */
    100   1.1       jtc static int	x_arg_defaulted;/* x_arg not explicitly set; defaulted to 1 */
    101   1.1       jtc 
    102   1.1       jtc static int	xlp_valid;
    103   1.1       jtc /* end from 4.9 edit.h } */
    104   1.1       jtc 
    105   1.1       jtc static	int	x_prefix1 = CTRL('['), x_prefix2 = CTRL('X');
    106   1.1       jtc static	char   **x_histp;	/* history position */
    107   1.1       jtc static	int	x_nextcmd;	/* for newline-and-next */
    108   1.1       jtc static	char	*xmp;		/* mark pointer */
    109   1.1       jtc static	Findex   x_last_command;
    110   1.1       jtc static	Findex (*x_tab)[X_TABSZ];	/* key definition */
    111   1.1       jtc static	char    *(*x_atab)[X_TABSZ];	/* macro definitions */
    112   1.6   hubertf static	unsigned char	x_bound[(X_TABSZ * X_NTABS + 7) / 8];
    113   1.1       jtc #define	KILLSIZE	20
    114   1.1       jtc static	char    *killstack[KILLSIZE];
    115   1.1       jtc static	int	killsp, killtp;
    116   1.1       jtc static	int	x_curprefix;
    117   1.1       jtc static	char    *macroptr;
    118   1.1       jtc static	int	prompt_skip;
    119   1.1       jtc 
    120   1.1       jtc static int      x_ins       ARGS((char *cp));
    121   1.1       jtc static void     x_delete    ARGS((int nc, int force_push));
    122   1.1       jtc static int	x_bword     ARGS((void));
    123   1.1       jtc static int	x_fword     ARGS((void));
    124   1.1       jtc static void     x_goto      ARGS((char *cp));
    125   1.1       jtc static void     x_bs        ARGS((int c));
    126   1.1       jtc static int      x_size_str  ARGS((char *cp));
    127   1.1       jtc static int      x_size      ARGS((int c));
    128   1.1       jtc static void     x_zots      ARGS((char *str));
    129   1.1       jtc static void     x_zotc      ARGS((int c));
    130   1.1       jtc static void     x_load_hist ARGS((char **hp));
    131   1.1       jtc static int      x_search    ARGS((char *pat, int sameline, int offset));
    132   1.1       jtc static int      x_match     ARGS((char *str, char *pat));
    133   1.1       jtc static void	x_redraw    ARGS((int limit));
    134   1.1       jtc static void     x_push      ARGS((int nchars));
    135   1.1       jtc static char *   x_mapin     ARGS((const char *cp));
    136   1.1       jtc static char *   x_mapout    ARGS((int c));
    137   1.1       jtc static void     x_print     ARGS((int prefix, int key));
    138   1.1       jtc static void	x_adjust    ARGS((void));
    139   1.1       jtc static void	x_e_ungetc  ARGS((int c));
    140   1.1       jtc static int	x_e_getc    ARGS((void));
    141   1.1       jtc static void	x_e_putc    ARGS((int c));
    142   1.1       jtc static void	x_e_puts    ARGS((const char *s));
    143   1.1       jtc static int	x_fold_case ARGS((int c));
    144   1.1       jtc static char	*x_lastcp ARGS((void));
    145   1.1       jtc static void	do_complete ARGS((int flags, Comp_type type));
    146   1.3  christos static int 	x_do_ins    ARGS((const char *, int));
    147   1.7   hubertf static void	bind_if_not_bound ARGS((int, int, int));
    148   1.8  jdolecek static int	x_emacs_putbuf	ARGS((const char *s, size_t len));
    149   1.1       jtc 
    150   1.1       jtc 
    151   1.1       jtc /* The lines between START-FUNC-TAB .. END-FUNC-TAB are run through a
    152   1.1       jtc  * script (emacs-gen.sh) that generates emacs.out which contains:
    153   1.1       jtc  *	- function declarations for x_* functions
    154   1.1       jtc  *	- defines of the form XFUNC_<name> where <name> is function
    155   1.1       jtc  *	  name, sans leading x_.
    156   1.1       jtc  * Note that the script treats #ifdef and { 0, 0, 0} specially - use with
    157   1.1       jtc  * caution.
    158   1.1       jtc  */
    159   1.1       jtc #include "emacs.out"
    160   1.1       jtc static const struct x_ftab x_ftab[] = {
    161   1.1       jtc /* @START-FUNC-TAB@ */
    162   1.1       jtc 	{ x_abort,		"abort",			0 },
    163   1.1       jtc 	{ x_beg_hist,		"beginning-of-history",		0 },
    164   1.1       jtc 	{ x_comp_comm,		"complete-command",		0 },
    165   1.1       jtc 	{ x_comp_file,		"complete-file",		0 },
    166   1.1       jtc 	{ x_complete,		"complete",			0 },
    167   1.1       jtc 	{ x_del_back,		"delete-char-backward",		XF_ARG },
    168   1.1       jtc 	{ x_del_bword,		"delete-word-backward",		XF_ARG },
    169   1.1       jtc 	{ x_del_char,		"delete-char-forward",		XF_ARG },
    170   1.1       jtc 	{ x_del_fword,		"delete-word-forward",		XF_ARG },
    171   1.1       jtc 	{ x_del_line,		"kill-line",			0 },
    172   1.1       jtc 	{ x_draw_line,		"redraw",			0 },
    173   1.1       jtc 	{ x_end_hist,		"end-of-history",		0 },
    174   1.1       jtc 	{ x_end_of_text,	"eot",				0 },
    175   1.1       jtc 	{ x_enumerate,		"list",				0 },
    176   1.1       jtc 	{ x_eot_del,		"eot-or-delete",		XF_ARG },
    177   1.1       jtc 	{ x_error,		"error",			0 },
    178   1.1       jtc 	{ x_goto_hist,		"goto-history",			XF_ARG },
    179   1.1       jtc 	{ x_ins_string,		"macro-string",			XF_NOBIND },
    180   1.1       jtc 	{ x_insert,		"auto-insert",			XF_ARG },
    181   1.1       jtc 	{ x_kill,		"kill-to-eol",			XF_ARG },
    182   1.1       jtc 	{ x_kill_region,	"kill-region",			0 },
    183   1.1       jtc 	{ x_list_comm,		"list-command",			0 },
    184   1.1       jtc 	{ x_list_file,		"list-file",			0 },
    185   1.1       jtc 	{ x_literal,		"quote",			0 },
    186   1.1       jtc 	{ x_meta1,		"prefix-1",			XF_PREFIX },
    187   1.1       jtc 	{ x_meta2,		"prefix-2",			XF_PREFIX },
    188   1.1       jtc 	{ x_meta_yank,		"yank-pop",			0 },
    189   1.1       jtc 	{ x_mv_back,		"backward-char",		XF_ARG },
    190   1.1       jtc 	{ x_mv_begin,		"beginning-of-line",		0 },
    191   1.1       jtc 	{ x_mv_bword,		"backward-word",		XF_ARG },
    192   1.1       jtc 	{ x_mv_end,		"end-of-line",			0 },
    193   1.1       jtc 	{ x_mv_forw,		"forward-char",			XF_ARG },
    194   1.1       jtc 	{ x_mv_fword,		"forward-word",			XF_ARG },
    195   1.1       jtc 	{ x_newline,		"newline",			0 },
    196   1.1       jtc 	{ x_next_com,		"down-history",			XF_ARG },
    197   1.1       jtc 	{ x_nl_next_com,	"newline-and-next",		0 },
    198   1.1       jtc 	{ x_noop,		"no-op",			0 },
    199   1.1       jtc 	{ x_prev_com,		"up-history",			XF_ARG },
    200   1.1       jtc 	{ x_prev_histword,	"prev-hist-word",		XF_ARG },
    201   1.1       jtc 	{ x_search_char_forw,	"search-character-forward",	XF_ARG },
    202   1.1       jtc 	{ x_search_char_back,	"search-character-backward",	XF_ARG },
    203   1.1       jtc 	{ x_search_hist,	"search-history",		0 },
    204   1.1       jtc 	{ x_set_mark,		"set-mark-command",		0 },
    205   1.1       jtc 	{ x_stuff,		"stuff",			0 },
    206   1.1       jtc 	{ x_stuffreset,		"stuff-reset",			0 },
    207   1.1       jtc 	{ x_transpose,		"transpose-chars",		0 },
    208   1.1       jtc 	{ x_version,		"version",			0 },
    209   1.1       jtc 	{ x_xchg_point_mark,	"exchange-point-and-mark",	0 },
    210   1.1       jtc 	{ x_yank,		"yank",				0 },
    211   1.1       jtc         { x_comp_list,		"complete-list",		0 },
    212   1.1       jtc         { x_expand,		"expand-file",			0 },
    213   1.1       jtc         { x_fold_capitialize,	"capitalize-word",		XF_ARG },
    214   1.1       jtc         { x_fold_lower,		"downcase-word",		XF_ARG },
    215   1.1       jtc         { x_fold_upper,		"upcase-word",			XF_ARG },
    216   1.1       jtc         { x_set_arg,		"set-arg",			XF_NOBIND },
    217   1.1       jtc         { x_comment,		"comment",			0 },
    218   1.1       jtc #ifdef SILLY
    219   1.1       jtc 	{ x_game_of_life,	"play-game-of-life",		0 },
    220   1.1       jtc #else
    221   1.1       jtc 	{ 0, 0, 0 },
    222   1.1       jtc #endif
    223   1.1       jtc #ifdef DEBUG
    224   1.1       jtc         { x_debug_info,		"debug-info",			0 },
    225   1.1       jtc #else
    226   1.1       jtc 	{ 0, 0, 0 },
    227   1.1       jtc #endif
    228   1.1       jtc #ifdef OS2
    229   1.1       jtc 	{ x_meta3,		"prefix-3",			XF_PREFIX },
    230   1.1       jtc #else
    231   1.1       jtc 	{ 0, 0, 0 },
    232   1.1       jtc #endif
    233   1.1       jtc /* @END-FUNC-TAB@ */
    234   1.1       jtc     };
    235   1.1       jtc 
    236   1.1       jtc static	struct x_defbindings const x_defbindings[] = {
    237   1.1       jtc 	{ XFUNC_del_back,		0, CTRL('?') },
    238   1.1       jtc 	{ XFUNC_del_bword,		1, CTRL('?') },
    239   1.1       jtc 	{ XFUNC_eot_del,		0, CTRL('D') },
    240   1.1       jtc 	{ XFUNC_del_back,		0, CTRL('H') },
    241   1.1       jtc 	{ XFUNC_del_bword,		1, CTRL('H') },
    242   1.1       jtc 	{ XFUNC_del_bword,		1,      'h'  },
    243   1.1       jtc 	{ XFUNC_mv_bword,		1,      'b'  },
    244   1.1       jtc 	{ XFUNC_mv_fword,		1,      'f'  },
    245   1.1       jtc 	{ XFUNC_del_fword,		1,      'd'  },
    246   1.1       jtc 	{ XFUNC_mv_back,		0, CTRL('B') },
    247   1.1       jtc 	{ XFUNC_mv_forw,		0, CTRL('F') },
    248   1.1       jtc 	{ XFUNC_search_char_forw,	0, CTRL(']') },
    249   1.1       jtc 	{ XFUNC_search_char_back,	1, CTRL(']') },
    250   1.1       jtc 	{ XFUNC_newline,		0, CTRL('M') },
    251   1.1       jtc 	{ XFUNC_newline,		0, CTRL('J') },
    252   1.1       jtc 	{ XFUNC_end_of_text,		0, CTRL('_') },
    253   1.1       jtc 	{ XFUNC_abort,			0, CTRL('G') },
    254   1.1       jtc 	{ XFUNC_prev_com,		0, CTRL('P') },
    255   1.1       jtc 	{ XFUNC_next_com,		0, CTRL('N') },
    256   1.1       jtc 	{ XFUNC_nl_next_com,		0, CTRL('O') },
    257   1.1       jtc 	{ XFUNC_search_hist,		0, CTRL('R') },
    258   1.1       jtc 	{ XFUNC_beg_hist,		1,      '<'  },
    259   1.1       jtc 	{ XFUNC_end_hist,		1,      '>'  },
    260   1.1       jtc 	{ XFUNC_goto_hist,		1,      'g'  },
    261   1.1       jtc 	{ XFUNC_mv_end,			0, CTRL('E') },
    262   1.1       jtc 	{ XFUNC_mv_begin,		0, CTRL('A') },
    263   1.1       jtc 	{ XFUNC_draw_line,		0, CTRL('L') },
    264   1.1       jtc 	{ XFUNC_meta1,			0, CTRL('[') },
    265   1.1       jtc 	{ XFUNC_meta2,			0, CTRL('X') },
    266   1.1       jtc 	{ XFUNC_kill,			0, CTRL('K') },
    267   1.1       jtc 	{ XFUNC_yank,			0, CTRL('Y') },
    268   1.1       jtc 	{ XFUNC_meta_yank,		1,      'y'  },
    269   1.1       jtc 	{ XFUNC_literal,		0, CTRL('^') },
    270   1.1       jtc         { XFUNC_comment,		1,	'#'  },
    271   1.1       jtc #if defined(BRL) && defined(TIOCSTI)
    272   1.1       jtc 	{ XFUNC_stuff,			0, CTRL('T') },
    273   1.1       jtc #else
    274   1.1       jtc 	{ XFUNC_transpose,		0, CTRL('T') },
    275   1.1       jtc #endif
    276   1.1       jtc 	{ XFUNC_complete,		1, CTRL('[') },
    277   1.1       jtc         { XFUNC_comp_list,		1,	'='  },
    278   1.1       jtc 	{ XFUNC_enumerate,		1,	'?'  },
    279   1.1       jtc         { XFUNC_expand,			1,	'*'  },
    280   1.1       jtc 	{ XFUNC_comp_file,		1, CTRL('X') },
    281   1.1       jtc 	{ XFUNC_comp_comm,		2, CTRL('[') },
    282   1.1       jtc 	{ XFUNC_list_comm,		2,	'?'  },
    283   1.1       jtc 	{ XFUNC_list_file,		2, CTRL('Y') },
    284   1.1       jtc 	{ XFUNC_set_mark,		1,	' '  },
    285   1.1       jtc 	{ XFUNC_kill_region,		0, CTRL('W') },
    286   1.1       jtc 	{ XFUNC_xchg_point_mark,	2, CTRL('X') },
    287   1.1       jtc 	{ XFUNC_version,		0, CTRL('V') },
    288   1.1       jtc #ifdef DEBUG
    289   1.1       jtc         { XFUNC_debug_info,		1, CTRL('H') },
    290   1.1       jtc #endif
    291   1.1       jtc 	{ XFUNC_prev_histword,		1,	'.'  },
    292   1.1       jtc 	{ XFUNC_prev_histword,		1,	'_'  },
    293   1.1       jtc         { XFUNC_set_arg,		1,	'0'  },
    294   1.1       jtc         { XFUNC_set_arg,		1,	'1'  },
    295   1.1       jtc         { XFUNC_set_arg,		1,	'2'  },
    296   1.1       jtc         { XFUNC_set_arg,		1,	'3'  },
    297   1.1       jtc         { XFUNC_set_arg,		1,	'4'  },
    298   1.1       jtc         { XFUNC_set_arg,		1,	'5'  },
    299   1.1       jtc         { XFUNC_set_arg,		1,	'6'  },
    300   1.1       jtc         { XFUNC_set_arg,		1,	'7'  },
    301   1.1       jtc         { XFUNC_set_arg,		1,	'8'  },
    302   1.1       jtc         { XFUNC_set_arg,		1,	'9'  },
    303   1.1       jtc         { XFUNC_fold_upper,		1,	'U'  },
    304   1.1       jtc         { XFUNC_fold_upper,		1,	'u'  },
    305   1.1       jtc         { XFUNC_fold_lower,		1,	'L'  },
    306   1.1       jtc         { XFUNC_fold_lower,		1,	'l'  },
    307   1.1       jtc         { XFUNC_fold_capitialize,	1,	'C'  },
    308   1.1       jtc         { XFUNC_fold_capitialize,	1,	'c'  },
    309   1.1       jtc #ifdef OS2
    310   1.1       jtc 	{ XFUNC_meta3,			0,	0xE0 },
    311   1.1       jtc 	{ XFUNC_mv_back,		3,	'K'  },
    312   1.1       jtc 	{ XFUNC_mv_forw,		3,	'M'  },
    313   1.1       jtc 	{ XFUNC_next_com,		3,	'P'  },
    314   1.1       jtc 	{ XFUNC_prev_com,		3,	'H'  },
    315   1.6   hubertf #endif /* OS2 */
    316   1.1       jtc 	/* These for ansi arrow keys: arguablely shouldn't be here by
    317   1.1       jtc 	 * default, but its simpler/faster/smaller than using termcap
    318   1.1       jtc 	 * entries.
    319   1.1       jtc 	 */
    320   1.1       jtc         { XFUNC_meta2,			1,	'['  },
    321   1.1       jtc 	{ XFUNC_prev_com,		2,	'A'  },
    322   1.1       jtc 	{ XFUNC_next_com,		2,	'B'  },
    323   1.1       jtc 	{ XFUNC_mv_forw,		2,	'C'  },
    324   1.1       jtc 	{ XFUNC_mv_back,		2,	'D'  },
    325   1.1       jtc };
    326   1.1       jtc 
    327   1.1       jtc int
    328   1.1       jtc x_emacs(buf, len)
    329   1.1       jtc 	char *buf;
    330   1.1       jtc 	size_t len;
    331   1.1       jtc {
    332   1.1       jtc 	int	c;
    333   1.1       jtc 	const char *p;
    334   1.1       jtc 	int	i;
    335   1.1       jtc 	Findex	f;
    336   1.1       jtc 
    337   1.1       jtc 	xbp = xbuf = buf; xend = buf + len;
    338   1.1       jtc 	xlp = xcp = xep = buf;
    339   1.1       jtc 	*xcp = 0;
    340   1.1       jtc 	xlp_valid = TRUE;
    341   1.1       jtc 	xmp = NULL;
    342   1.1       jtc 	x_curprefix = 0;
    343   1.1       jtc 	macroptr = (char *) 0;
    344   1.1       jtc 	x_histp = histptr + 1;
    345   1.1       jtc 	x_last_command = XFUNC_error;
    346   1.1       jtc 
    347   1.1       jtc 	xx_cols = x_cols;
    348   1.1       jtc 	x_col = promptlen(prompt, &p);
    349   1.1       jtc 	prompt_skip = p - prompt;
    350   1.1       jtc 	x_adj_ok = 1;
    351   1.1       jtc 	x_displen = xx_cols - 2 - x_col;
    352   1.1       jtc 	x_adj_done = 0;
    353   1.1       jtc 
    354   1.1       jtc 	pprompt(prompt, 0);
    355   1.1       jtc 
    356   1.1       jtc 	if (x_nextcmd >= 0) {
    357   1.1       jtc 		int off = source->line - x_nextcmd;
    358   1.1       jtc 		if (histptr - history >= off)
    359   1.1       jtc 			x_load_hist(histptr - off);
    360   1.1       jtc 		x_nextcmd = -1;
    361   1.1       jtc 	}
    362   1.1       jtc 
    363   1.1       jtc 	while (1) {
    364   1.1       jtc 		x_flush();
    365   1.1       jtc 		if ((c = x_e_getc()) < 0)
    366   1.1       jtc 			return 0;
    367   1.1       jtc 
    368   1.1       jtc 		f = x_curprefix == -1 ? XFUNC_insert
    369   1.1       jtc 			: x_tab[x_curprefix][c&CHARMASK];
    370   1.1       jtc 
    371   1.1       jtc 		if (!(x_ftab[f].xf_flags & XF_PREFIX)
    372   1.1       jtc 		    && x_last_command != XFUNC_set_arg)
    373   1.1       jtc 		{
    374   1.1       jtc 			x_arg = 1;
    375   1.1       jtc 			x_arg_defaulted = 1;
    376   1.1       jtc 		}
    377   1.1       jtc 		i = c | (x_curprefix << 8);
    378   1.1       jtc 		x_curprefix = 0;
    379   1.1       jtc 		switch (i = (*x_ftab[f].xf_func)(i))  {
    380   1.1       jtc 		  case KSTD:
    381   1.1       jtc 			if (!(x_ftab[f].xf_flags & XF_PREFIX))
    382   1.1       jtc 				x_last_command = f;
    383   1.1       jtc 			break;
    384   1.1       jtc 		  case KEOL:
    385   1.1       jtc 			i = xep - xbuf;
    386   1.1       jtc 			return i;
    387   1.1       jtc 		  case KINTR:	/* special case for interrupt */
    388   1.1       jtc 			trapsig(SIGINT);
    389   1.1       jtc 			x_mode(FALSE);
    390   1.1       jtc 			unwind(LSHELL);
    391   1.1       jtc 		}
    392   1.1       jtc 	}
    393   1.1       jtc }
    394   1.1       jtc 
    395   1.1       jtc static int
    396   1.1       jtc x_insert(c)
    397   1.1       jtc 	int c;
    398   1.1       jtc {
    399   1.1       jtc 	char	str[2];
    400   1.1       jtc 
    401   1.1       jtc 	/*
    402   1.1       jtc 	 *  Should allow tab and control chars.
    403   1.1       jtc 	 */
    404   1.1       jtc 	if (c == 0)  {
    405   1.1       jtc 		x_e_putc(BEL);
    406   1.1       jtc 		return KSTD;
    407   1.1       jtc 	}
    408   1.1       jtc 	str[0] = c;
    409   1.1       jtc 	str[1] = '\0';
    410   1.1       jtc 	while (x_arg--)
    411   1.1       jtc 		x_ins(str);
    412   1.1       jtc 	return KSTD;
    413   1.1       jtc }
    414   1.1       jtc 
    415   1.1       jtc static int
    416   1.1       jtc x_ins_string(c)
    417   1.1       jtc 	int c;
    418   1.1       jtc {
    419   1.1       jtc 	if (macroptr)   {
    420   1.1       jtc 		x_e_putc(BEL);
    421   1.1       jtc 		return KSTD;
    422   1.1       jtc 	}
    423   1.1       jtc 	macroptr = x_atab[c>>8][c & CHARMASK];
    424   1.1       jtc 	if (macroptr && !*macroptr) {
    425   1.1       jtc 		/* XXX bell? */
    426   1.1       jtc 		macroptr = (char *) 0;
    427   1.1       jtc 	}
    428   1.1       jtc 	return KSTD;
    429   1.1       jtc }
    430   1.1       jtc 
    431   1.1       jtc static int
    432   1.1       jtc x_do_ins(cp, len)
    433   1.1       jtc 	const char *cp;
    434   1.1       jtc 	int len;
    435   1.1       jtc {
    436   1.1       jtc 	if (xep+len >= xend) {
    437   1.1       jtc 		x_e_putc(BEL);
    438   1.1       jtc 		return -1;
    439   1.1       jtc 	}
    440   1.1       jtc 
    441   1.1       jtc 	memmove(xcp+len, xcp, xep - xcp + 1);
    442   1.1       jtc 	memmove(xcp, cp, len);
    443   1.1       jtc 	xcp += len;
    444   1.1       jtc 	xep += len;
    445   1.1       jtc 	return 0;
    446   1.1       jtc }
    447   1.1       jtc 
    448   1.1       jtc static int
    449   1.1       jtc x_ins(s)
    450   1.1       jtc 	char	*s;
    451   1.1       jtc {
    452   1.1       jtc 	char *cp = xcp;
    453   1.1       jtc 	register int	adj = x_adj_done;
    454   1.1       jtc 
    455   1.1       jtc 	if (x_do_ins(s, strlen(s)) < 0)
    456   1.1       jtc 		return -1;
    457   1.1       jtc 	/*
    458   1.1       jtc 	 * x_zots() may result in a call to x_adjust()
    459   1.1       jtc 	 * we want xcp to reflect the new position.
    460   1.1       jtc 	 */
    461   1.1       jtc 	xlp_valid = FALSE;
    462   1.1       jtc 	x_lastcp();
    463   1.1       jtc 	x_adj_ok = (xcp >= xlp);
    464   1.1       jtc 	x_zots(cp);
    465   1.1       jtc 	if (adj == x_adj_done)	/* has x_adjust() been called? */
    466   1.1       jtc 	{
    467   1.1       jtc 	  /* no */
    468   1.1       jtc 	  for (cp = xlp; cp > xcp; )
    469   1.1       jtc 	    x_bs(*--cp);
    470   1.1       jtc 	}
    471   1.1       jtc 
    472   1.1       jtc 	x_adj_ok = 1;
    473   1.1       jtc 	return 0;
    474   1.1       jtc }
    475   1.1       jtc 
    476   1.8  jdolecek /*
    477   1.8  jdolecek  * this is used for x_escape() in do_complete()
    478   1.8  jdolecek  */
    479   1.8  jdolecek static int
    480   1.8  jdolecek x_emacs_putbuf(s, len)
    481   1.8  jdolecek 	const char *s;
    482   1.8  jdolecek 	size_t len;
    483   1.8  jdolecek {
    484   1.8  jdolecek 	int rval;
    485   1.8  jdolecek 
    486   1.8  jdolecek 	if ((rval = x_do_ins(s, len)) != 0)
    487   1.8  jdolecek 		return (rval);
    488   1.8  jdolecek 	return (rval);
    489   1.8  jdolecek }
    490   1.8  jdolecek 
    491   1.1       jtc static int
    492   1.1       jtc x_del_back(c)
    493   1.1       jtc 	int c;
    494   1.1       jtc {
    495   1.1       jtc 	int col = xcp - xbuf;
    496   1.1       jtc 
    497   1.1       jtc 	if (col == 0)  {
    498   1.1       jtc 		x_e_putc(BEL);
    499   1.1       jtc 		return KSTD;
    500   1.1       jtc 	}
    501   1.1       jtc 	if (x_arg > col)
    502   1.1       jtc 		x_arg = col;
    503   1.1       jtc 	x_goto(xcp - x_arg);
    504   1.1       jtc 	x_delete(x_arg, FALSE);
    505   1.1       jtc 	return KSTD;
    506   1.1       jtc }
    507   1.1       jtc 
    508   1.1       jtc static int
    509   1.1       jtc x_del_char(c)
    510   1.1       jtc 	int c;
    511   1.1       jtc {
    512   1.1       jtc 	int nleft = xep - xcp;
    513   1.1       jtc 
    514   1.1       jtc 	if (!nleft) {
    515   1.1       jtc 		x_e_putc(BEL);
    516   1.1       jtc 		return KSTD;
    517   1.1       jtc 	}
    518   1.1       jtc 	if (x_arg > nleft)
    519   1.1       jtc 		x_arg = nleft;
    520   1.1       jtc 	x_delete(x_arg, FALSE);
    521   1.1       jtc 	return KSTD;
    522   1.1       jtc }
    523   1.1       jtc 
    524   1.1       jtc /* Delete nc chars to the right of the cursor (including cursor position) */
    525   1.1       jtc static void
    526   1.1       jtc x_delete(nc, force_push)
    527   1.1       jtc 	int nc;
    528   1.1       jtc 	int force_push;
    529   1.1       jtc {
    530   1.1       jtc 	int	i,j;
    531   1.1       jtc 	char	*cp;
    532   1.1       jtc 
    533   1.1       jtc 	if (nc == 0)
    534   1.1       jtc 		return;
    535   1.1       jtc 	if (xmp != NULL && xmp > xcp) {
    536   1.1       jtc 		if (xcp + nc > xmp)
    537   1.1       jtc 			xmp = xcp;
    538   1.1       jtc 		else
    539   1.1       jtc 			xmp -= nc;
    540   1.1       jtc 	}
    541   1.1       jtc 
    542   1.1       jtc 	/*
    543   1.1       jtc 	 * This lets us yank a word we have deleted.
    544   1.1       jtc 	 */
    545   1.1       jtc 	if (nc > 1 || force_push)
    546   1.1       jtc 		x_push(nc);
    547   1.1       jtc 
    548   1.1       jtc 	xep -= nc;
    549   1.1       jtc 	cp = xcp;
    550   1.1       jtc 	j = 0;
    551   1.1       jtc 	i = nc;
    552   1.1       jtc 	while (i--)  {
    553   1.1       jtc 		j += x_size(*cp++);
    554   1.1       jtc 	}
    555   1.1       jtc 	memmove(xcp, xcp+nc, xep - xcp + 1);	/* Copies the null */
    556   1.1       jtc 	x_adj_ok = 0;			/* don't redraw */
    557   1.1       jtc 	x_zots(xcp);
    558   1.1       jtc 	/*
    559   1.1       jtc 	 * if we are already filling the line,
    560   1.1       jtc 	 * there is no need to ' ','\b'.
    561   1.1       jtc 	 * But if we must, make sure we do the minimum.
    562   1.1       jtc 	 */
    563   1.1       jtc 	if ((i = xx_cols - 2 - x_col) > 0)
    564   1.1       jtc 	{
    565   1.1       jtc 	  j = (j < i) ? j : i;
    566   1.1       jtc 	  i = j;
    567   1.1       jtc 	  while (i--)
    568   1.1       jtc 	    x_e_putc(' ');
    569   1.1       jtc 	  i = j;
    570   1.1       jtc 	  while (i--)
    571   1.1       jtc 	    x_e_putc('\b');
    572   1.1       jtc 	}
    573   1.1       jtc 	/*x_goto(xcp);*/
    574   1.1       jtc 	x_adj_ok = 1;
    575   1.1       jtc 	xlp_valid = FALSE;
    576   1.1       jtc 	for (cp = x_lastcp(); cp > xcp; )
    577   1.1       jtc 		x_bs(*--cp);
    578   1.1       jtc 
    579   1.1       jtc 	return;
    580   1.1       jtc }
    581   1.1       jtc 
    582   1.1       jtc static int
    583   1.1       jtc x_del_bword(c)
    584   1.1       jtc 	int c;
    585   1.1       jtc {
    586   1.1       jtc 	x_delete(x_bword(), FALSE);
    587   1.1       jtc 	return KSTD;
    588   1.1       jtc }
    589   1.1       jtc 
    590   1.1       jtc static int
    591   1.1       jtc x_mv_bword(c)
    592   1.1       jtc 	int c;
    593   1.1       jtc {
    594   1.1       jtc 	(void)x_bword();
    595   1.1       jtc 	return KSTD;
    596   1.1       jtc }
    597   1.1       jtc 
    598   1.1       jtc static int
    599   1.1       jtc x_mv_fword(c)
    600   1.1       jtc 	int c;
    601   1.1       jtc {
    602   1.1       jtc 	x_goto(xcp + x_fword());
    603   1.1       jtc 	return KSTD;
    604   1.1       jtc }
    605   1.1       jtc 
    606   1.1       jtc static int
    607   1.1       jtc x_del_fword(c)
    608   1.1       jtc 	int c;
    609   1.1       jtc {
    610   1.1       jtc 	x_delete(x_fword(), FALSE);
    611   1.1       jtc 	return KSTD;
    612   1.1       jtc }
    613   1.1       jtc 
    614   1.1       jtc static int
    615   1.1       jtc x_bword()
    616   1.1       jtc {
    617   1.1       jtc 	int	nc = 0;
    618   1.1       jtc 	register char *cp = xcp;
    619   1.1       jtc 
    620   1.1       jtc 	if (cp == xbuf)  {
    621   1.1       jtc 		x_e_putc(BEL);
    622   1.1       jtc 		return 0;
    623   1.1       jtc 	}
    624   1.1       jtc 	while (x_arg--)
    625   1.1       jtc 	{
    626   1.1       jtc 	  while (cp != xbuf && is_mfs(cp[-1]))
    627   1.1       jtc 	  {
    628   1.1       jtc 	    cp--;
    629   1.1       jtc 	    nc++;
    630   1.1       jtc 	  }
    631   1.1       jtc 	  while (cp != xbuf && !is_mfs(cp[-1]))
    632   1.1       jtc 	  {
    633   1.1       jtc 	    cp--;
    634   1.1       jtc 	    nc++;
    635   1.1       jtc 	  }
    636   1.1       jtc 	}
    637   1.1       jtc 	x_goto(cp);
    638   1.1       jtc 	return nc;
    639   1.1       jtc }
    640   1.1       jtc 
    641   1.1       jtc static int
    642   1.1       jtc x_fword()
    643   1.1       jtc {
    644   1.1       jtc 	int	nc = 0;
    645   1.1       jtc 	register char	*cp = xcp;
    646   1.1       jtc 
    647   1.1       jtc 	if (cp == xep)  {
    648   1.1       jtc 		x_e_putc(BEL);
    649   1.1       jtc 		return 0;
    650   1.1       jtc 	}
    651   1.1       jtc 	while (x_arg--)
    652   1.1       jtc 	{
    653   1.1       jtc 	  while (cp != xep && is_mfs(*cp))
    654   1.1       jtc 	  {
    655   1.1       jtc 	    cp++;
    656   1.1       jtc 	    nc++;
    657   1.1       jtc 	  }
    658   1.1       jtc 	  while (cp != xep && !is_mfs(*cp))
    659   1.1       jtc 	  {
    660   1.1       jtc 	    cp++;
    661   1.1       jtc 	    nc++;
    662   1.1       jtc 	  }
    663   1.1       jtc 	}
    664   1.1       jtc 	return nc;
    665   1.1       jtc }
    666   1.1       jtc 
    667   1.1       jtc static void
    668   1.1       jtc x_goto(cp)
    669   1.1       jtc 	register char *cp;
    670   1.1       jtc {
    671   1.1       jtc   if (cp < xbp || cp >= (xbp + x_displen))
    672   1.1       jtc   {
    673   1.1       jtc     /* we are heading off screen */
    674   1.1       jtc     xcp = cp;
    675   1.1       jtc     x_adjust();
    676   1.1       jtc   }
    677   1.1       jtc   else
    678   1.1       jtc   {
    679   1.1       jtc     if (cp < xcp)		/* move back */
    680   1.1       jtc     {
    681   1.1       jtc       while (cp < xcp)
    682   1.1       jtc 	x_bs(*--xcp);
    683   1.1       jtc     }
    684   1.1       jtc     else
    685   1.1       jtc     {
    686   1.1       jtc       if (cp > xcp)		/* move forward */
    687   1.1       jtc       {
    688   1.1       jtc 	while (cp > xcp)
    689   1.1       jtc 	  x_zotc(*xcp++);
    690   1.1       jtc       }
    691   1.1       jtc     }
    692   1.1       jtc   }
    693   1.1       jtc }
    694   1.1       jtc 
    695   1.1       jtc static void
    696   1.1       jtc x_bs(c)
    697   1.1       jtc 	int c;
    698   1.1       jtc {
    699   1.6   hubertf 	register int i;
    700   1.1       jtc 	i = x_size(c);
    701   1.1       jtc 	while (i--)
    702   1.1       jtc 		x_e_putc('\b');
    703   1.1       jtc }
    704   1.1       jtc 
    705   1.1       jtc static int
    706   1.1       jtc x_size_str(cp)
    707   1.1       jtc 	register char *cp;
    708   1.1       jtc {
    709   1.6   hubertf 	register int size = 0;
    710   1.1       jtc 	while (*cp)
    711   1.1       jtc 		size += x_size(*cp++);
    712   1.1       jtc 	return size;
    713   1.1       jtc }
    714   1.1       jtc 
    715   1.1       jtc static int
    716   1.1       jtc x_size(c)
    717   1.1       jtc 	int c;
    718   1.1       jtc {
    719   1.1       jtc 	if (c=='\t')
    720   1.1       jtc 		return 4;	/* Kludge, tabs are always four spaces. */
    721   1.6   hubertf 	if (iscntrl(c))		/* control char */
    722   1.1       jtc 		return 2;
    723   1.1       jtc 	return 1;
    724   1.1       jtc }
    725   1.1       jtc 
    726   1.1       jtc static void
    727   1.1       jtc x_zots(str)
    728   1.1       jtc 	register char *str;
    729   1.1       jtc {
    730   1.1       jtc   register int	adj = x_adj_done;
    731   1.1       jtc 
    732   1.1       jtc   x_lastcp();
    733   1.1       jtc   while (*str && str < xlp && adj == x_adj_done)
    734   1.1       jtc     x_zotc(*str++);
    735   1.1       jtc }
    736   1.1       jtc 
    737   1.1       jtc static void
    738   1.1       jtc x_zotc(c)
    739   1.1       jtc 	int c;
    740   1.1       jtc {
    741   1.1       jtc 	if (c == '\t')  {
    742   1.1       jtc 		/*  Kludge, tabs are always four spaces.  */
    743   1.1       jtc 		x_e_puts("    ");
    744   1.6   hubertf 	} else if (iscntrl(c))  {
    745   1.1       jtc 		x_e_putc('^');
    746   1.1       jtc 		x_e_putc(UNCTRL(c));
    747   1.1       jtc 	} else
    748   1.1       jtc 		x_e_putc(c);
    749   1.1       jtc }
    750   1.1       jtc 
    751   1.1       jtc static int
    752   1.1       jtc x_mv_back(c)
    753   1.1       jtc 	int c;
    754   1.1       jtc {
    755   1.1       jtc 	int col = xcp - xbuf;
    756   1.1       jtc 
    757   1.1       jtc 	if (col == 0)  {
    758   1.1       jtc 		x_e_putc(BEL);
    759   1.1       jtc 		return KSTD;
    760   1.1       jtc 	}
    761   1.1       jtc 	if (x_arg > col)
    762   1.1       jtc 		x_arg = col;
    763   1.1       jtc 	x_goto(xcp - x_arg);
    764   1.1       jtc 	return KSTD;
    765   1.1       jtc }
    766   1.1       jtc 
    767   1.1       jtc static int
    768   1.1       jtc x_mv_forw(c)
    769   1.1       jtc 	int c;
    770   1.1       jtc {
    771   1.1       jtc 	int nleft = xep - xcp;
    772   1.1       jtc 
    773   1.1       jtc 	if (!nleft) {
    774   1.1       jtc 		x_e_putc(BEL);
    775   1.1       jtc 		return KSTD;
    776   1.1       jtc 	}
    777   1.1       jtc 	if (x_arg > nleft)
    778   1.1       jtc 		x_arg = nleft;
    779   1.1       jtc 	x_goto(xcp + x_arg);
    780   1.1       jtc 	return KSTD;
    781   1.1       jtc }
    782   1.1       jtc 
    783   1.1       jtc static int
    784   1.1       jtc x_search_char_forw(c)
    785   1.1       jtc 	int c;
    786   1.1       jtc {
    787   1.1       jtc 	char *cp = xcp;
    788   1.1       jtc 
    789   1.1       jtc 	*xep = '\0';
    790   1.1       jtc 	c = x_e_getc();
    791   1.1       jtc 	while (x_arg--) {
    792   1.1       jtc 	    if (c < 0
    793   1.1       jtc 	       || ((cp = (cp == xep) ? NULL : strchr(cp + 1, c)) == NULL
    794   1.1       jtc 		   && (cp = strchr(xbuf, c)) == NULL))
    795   1.1       jtc 	    {
    796   1.1       jtc 		    x_e_putc(BEL);
    797   1.1       jtc 		    return KSTD;
    798   1.1       jtc 	    }
    799   1.1       jtc 	}
    800   1.1       jtc 	x_goto(cp);
    801   1.1       jtc 	return KSTD;
    802   1.1       jtc }
    803   1.1       jtc 
    804   1.1       jtc static int
    805   1.1       jtc x_search_char_back(c)
    806   1.1       jtc 	int c;
    807   1.1       jtc {
    808   1.1       jtc 	char *cp = xcp, *p;
    809   1.1       jtc 
    810   1.1       jtc 	c = x_e_getc();
    811   1.1       jtc 	for (; x_arg--; cp = p)
    812   1.1       jtc 		for (p = cp; ; ) {
    813   1.1       jtc 			if (p-- == xbuf)
    814   1.1       jtc 				p = xep;
    815   1.1       jtc 			if (c < 0 || p == cp) {
    816   1.1       jtc 				x_e_putc(BEL);
    817   1.1       jtc 				return KSTD;
    818   1.1       jtc 			}
    819   1.1       jtc 			if (*p == c)
    820   1.1       jtc 				break;
    821   1.1       jtc 		}
    822   1.1       jtc 	x_goto(cp);
    823   1.1       jtc 	return KSTD;
    824   1.1       jtc }
    825   1.1       jtc 
    826   1.1       jtc static int
    827   1.1       jtc x_newline(c)
    828   1.1       jtc 	int c;
    829   1.1       jtc {
    830   1.1       jtc 	x_e_putc('\r');
    831   1.1       jtc 	x_e_putc('\n');
    832   1.1       jtc 	x_flush();
    833   1.1       jtc 	*xep++ = '\n';
    834   1.1       jtc 	return KEOL;
    835   1.1       jtc }
    836   1.1       jtc 
    837   1.1       jtc static int
    838   1.1       jtc x_end_of_text(c)
    839   1.1       jtc 	int c;
    840   1.1       jtc {
    841   1.1       jtc 	return KEOL;
    842   1.1       jtc }
    843   1.1       jtc 
    844   1.1       jtc static int x_beg_hist(c) int c; { x_load_hist(history); return KSTD;}
    845   1.1       jtc 
    846   1.1       jtc static int x_end_hist(c) int c; { x_load_hist(histptr); return KSTD;}
    847   1.1       jtc 
    848   1.1       jtc static int x_prev_com(c) int c; { x_load_hist(x_histp - x_arg); return KSTD;}
    849   1.1       jtc 
    850   1.1       jtc static int x_next_com(c) int c; { x_load_hist(x_histp + x_arg); return KSTD;}
    851   1.1       jtc 
    852   1.1       jtc /* Goto a particular history number obtained from argument.
    853   1.1       jtc  * If no argument is given history 1 is probably not what you
    854   1.1       jtc  * want so we'll simply go to the oldest one.
    855   1.1       jtc  */
    856   1.1       jtc static int
    857   1.1       jtc x_goto_hist(c)
    858   1.1       jtc 	int c;
    859   1.1       jtc {
    860   1.1       jtc 	if (x_arg_defaulted)
    861   1.1       jtc 		x_load_hist(history);
    862   1.1       jtc 	else
    863   1.1       jtc 		x_load_hist(histptr + x_arg - source->line);
    864   1.1       jtc 	return KSTD;
    865   1.1       jtc }
    866   1.1       jtc 
    867   1.1       jtc static void
    868   1.1       jtc x_load_hist(hp)
    869   1.1       jtc 	register char **hp;
    870   1.1       jtc {
    871   1.1       jtc 	int	oldsize;
    872   1.1       jtc 
    873   1.1       jtc 	if (hp < history || hp > histptr) {
    874   1.1       jtc 		x_e_putc(BEL);
    875   1.1       jtc 		return;
    876   1.1       jtc 	}
    877   1.1       jtc 	x_histp = hp;
    878   1.1       jtc 	oldsize = x_size_str(xbuf);
    879   1.1       jtc 	(void)strcpy(xbuf, *hp);
    880   1.1       jtc 	xbp = xbuf;
    881   1.1       jtc 	xep = xcp = xbuf + strlen(*hp);
    882   1.1       jtc 	xlp_valid = FALSE;
    883   1.1       jtc 	if (xep > x_lastcp())
    884   1.1       jtc 	  x_goto(xep);
    885   1.1       jtc 	else
    886   1.1       jtc 	  x_redraw(oldsize);
    887   1.1       jtc }
    888   1.1       jtc 
    889   1.1       jtc static int
    890   1.1       jtc x_nl_next_com(c)
    891   1.1       jtc 	int	c;
    892   1.1       jtc {
    893   1.1       jtc 	x_nextcmd = source->line - (histptr - x_histp) + 1;
    894   1.1       jtc 	return (x_newline(c));
    895   1.1       jtc }
    896   1.1       jtc 
    897   1.1       jtc static int
    898   1.1       jtc x_eot_del(c)
    899   1.1       jtc 	int	c;
    900   1.1       jtc {
    901   1.1       jtc 	if (xep == xbuf && x_arg_defaulted)
    902   1.1       jtc 		return (x_end_of_text(c));
    903   1.1       jtc 	else
    904   1.1       jtc 		return (x_del_char(c));
    905   1.1       jtc }
    906   1.1       jtc 
    907   1.1       jtc /* reverse incremental history search */
    908   1.1       jtc static int
    909   1.1       jtc x_search_hist(c)
    910   1.1       jtc 	int c;
    911   1.1       jtc {
    912   1.1       jtc 	int offset = -1;	/* offset of match in xbuf, else -1 */
    913   1.1       jtc 	char pat [256+1];	/* pattern buffer */
    914   1.1       jtc 	register char *p = pat;
    915   1.1       jtc 	Findex f;
    916   1.1       jtc 
    917   1.1       jtc 	*p = '\0';
    918   1.1       jtc 	while (1) {
    919   1.1       jtc 		if (offset < 0) {
    920   1.1       jtc 			x_e_puts("\nI-search: ");
    921   1.1       jtc 			x_e_puts(pat);
    922   1.1       jtc 		}
    923   1.1       jtc 		x_flush();
    924   1.1       jtc 		if ((c = x_e_getc()) < 0)
    925   1.1       jtc 			return KSTD;
    926   1.1       jtc 		f = x_tab[0][c&CHARMASK];
    927   1.1       jtc 		if (c == CTRL('['))
    928   1.1       jtc 			break;
    929   1.1       jtc 		else if (f == XFUNC_search_hist)
    930   1.1       jtc 			offset = x_search(pat, 0, offset);
    931   1.1       jtc 		else if (f == XFUNC_del_back) {
    932   1.1       jtc 			if (p == pat) {
    933   1.1       jtc 				offset = -1;
    934   1.1       jtc 				break;
    935   1.1       jtc 			}
    936   1.1       jtc 			if (p > pat)
    937   1.1       jtc 				*--p = '\0';
    938   1.1       jtc 			if (p == pat)
    939   1.1       jtc 				offset = -1;
    940   1.1       jtc 			else
    941   1.1       jtc 				offset = x_search(pat, 1, offset);
    942   1.1       jtc 			continue;
    943   1.1       jtc 		} else if (f == XFUNC_insert) {
    944   1.1       jtc 			/* add char to pattern */
    945   1.1       jtc 			/* overflow check... */
    946   1.1       jtc 			if (p >= &pat[sizeof(pat) - 1]) {
    947   1.1       jtc 				x_e_putc(BEL);
    948   1.1       jtc 				continue;
    949   1.1       jtc 			}
    950   1.1       jtc 			*p++ = c, *p = '\0';
    951   1.1       jtc 			if (offset >= 0) {
    952   1.1       jtc 				/* already have partial match */
    953   1.1       jtc 				offset = x_match(xbuf, pat);
    954   1.1       jtc 				if (offset >= 0) {
    955   1.1       jtc 					x_goto(xbuf + offset + (p - pat) - (*pat == '^'));
    956   1.1       jtc 					continue;
    957   1.1       jtc 				}
    958   1.1       jtc 			}
    959   1.1       jtc 			offset = x_search(pat, 0, offset);
    960   1.1       jtc 		} else { /* other command */
    961   1.1       jtc 			x_e_ungetc(c);
    962   1.1       jtc 			break;
    963   1.1       jtc 		}
    964   1.1       jtc 	}
    965   1.1       jtc 	if (offset < 0)
    966   1.1       jtc 		x_redraw(-1);
    967   1.1       jtc 	return KSTD;
    968   1.1       jtc }
    969   1.1       jtc 
    970   1.1       jtc /* search backward from current line */
    971   1.1       jtc static int
    972   1.1       jtc x_search(pat, sameline, offset)
    973   1.1       jtc 	char *pat;
    974   1.1       jtc 	int sameline;
    975   1.1       jtc 	int offset;
    976   1.1       jtc {
    977   1.1       jtc 	register char **hp;
    978   1.1       jtc 	int i;
    979   1.1       jtc 
    980   1.1       jtc 	for (hp = x_histp - (sameline ? 0 : 1) ; hp >= history; --hp) {
    981   1.1       jtc 		i = x_match(*hp, pat);
    982   1.1       jtc 		if (i >= 0) {
    983   1.1       jtc 			if (offset < 0)
    984   1.1       jtc 				x_e_putc('\n');
    985   1.1       jtc 			x_load_hist(hp);
    986   1.1       jtc 			x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
    987   1.1       jtc 			return i;
    988   1.1       jtc 		}
    989   1.1       jtc 	}
    990   1.1       jtc 	x_e_putc(BEL);
    991   1.1       jtc 	x_histp = histptr;
    992   1.1       jtc 	return -1;
    993   1.1       jtc }
    994   1.1       jtc 
    995   1.1       jtc /* return position of first match of pattern in string, else -1 */
    996   1.1       jtc static int
    997   1.1       jtc x_match(str, pat)
    998   1.1       jtc 	char *str, *pat;
    999   1.1       jtc {
   1000   1.1       jtc 	if (*pat == '^') {
   1001   1.1       jtc 		return (strncmp(str, pat+1, strlen(pat+1)) == 0) ? 0 : -1;
   1002   1.1       jtc 	} else {
   1003   1.1       jtc 		char *q = strstr(str, pat);
   1004   1.1       jtc 		return (q == NULL) ? -1 : q - str;
   1005   1.1       jtc 	}
   1006   1.1       jtc }
   1007   1.1       jtc 
   1008   1.1       jtc static int
   1009   1.1       jtc x_del_line(c)
   1010   1.1       jtc 	int c;
   1011   1.1       jtc {
   1012   1.1       jtc 	int	i, j;
   1013   1.1       jtc 
   1014   1.1       jtc 	*xep = 0;
   1015   1.1       jtc 	i = xep- xbuf;
   1016   1.1       jtc 	j = x_size_str(xbuf);
   1017   1.1       jtc 	xcp = xbuf;
   1018   1.1       jtc 	x_push(i);
   1019   1.1       jtc 	xlp = xbp = xep = xbuf;
   1020   1.1       jtc 	xlp_valid = TRUE;
   1021   1.1       jtc 	*xcp = 0;
   1022   1.1       jtc 	xmp = NULL;
   1023   1.1       jtc 	x_redraw(j);
   1024   1.1       jtc 	return KSTD;
   1025   1.1       jtc }
   1026   1.1       jtc 
   1027   1.1       jtc static int
   1028   1.1       jtc x_mv_end(c)
   1029   1.1       jtc 	int c;
   1030   1.1       jtc {
   1031   1.1       jtc 	x_goto(xep);
   1032   1.1       jtc 	return KSTD;
   1033   1.1       jtc }
   1034   1.1       jtc 
   1035   1.1       jtc static int
   1036   1.1       jtc x_mv_begin(c)
   1037   1.1       jtc 	int c;
   1038   1.1       jtc {
   1039   1.1       jtc 	x_goto(xbuf);
   1040   1.1       jtc 	return KSTD;
   1041   1.1       jtc }
   1042   1.1       jtc 
   1043   1.1       jtc static int
   1044   1.1       jtc x_draw_line(c)
   1045   1.1       jtc 	int c;
   1046   1.1       jtc {
   1047   1.1       jtc 	x_redraw(-1);
   1048   1.1       jtc 	return KSTD;
   1049   1.1       jtc 
   1050   1.1       jtc }
   1051   1.1       jtc 
   1052   1.1       jtc /* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
   1053   1.1       jtc  * on a NEW line, otherwise limit is the screen column up to which needs
   1054   1.1       jtc  * redrawing.
   1055   1.1       jtc  */
   1056   1.1       jtc static void
   1057   1.1       jtc x_redraw(limit)
   1058   1.1       jtc   int limit;
   1059   1.1       jtc {
   1060   1.1       jtc 	int	i, j;
   1061   1.1       jtc 	char	*cp;
   1062   1.1       jtc 
   1063   1.1       jtc 	x_adj_ok = 0;
   1064   1.1       jtc 	if (limit == -1)
   1065   1.1       jtc 		x_e_putc('\n');
   1066   1.1       jtc 	else
   1067   1.1       jtc 		x_e_putc('\r');
   1068   1.1       jtc 	x_flush();
   1069   1.1       jtc 	if (xbp == xbuf)
   1070   1.1       jtc 	{
   1071   1.1       jtc 	  pprompt(prompt + prompt_skip, 0);
   1072   1.1       jtc 	  x_col = promptlen(prompt, (const char **) 0);
   1073   1.1       jtc 	}
   1074   1.1       jtc 	x_displen = xx_cols - 2 - x_col;
   1075   1.1       jtc 	xlp_valid = FALSE;
   1076   1.1       jtc 	cp = x_lastcp();
   1077   1.1       jtc 	x_zots(xbp);
   1078   1.1       jtc 	if (xbp != xbuf || xep > xlp)
   1079   1.1       jtc 	  limit = xx_cols;
   1080   1.1       jtc 	if (limit >= 0)
   1081   1.1       jtc 	{
   1082   1.1       jtc 	  if (xep > xlp)
   1083   1.1       jtc 	    i = 0;			/* we fill the line */
   1084   1.1       jtc 	  else
   1085   1.1       jtc 	    i = limit - (xlp - xbp);
   1086   1.1       jtc 
   1087   1.1       jtc 	  for (j = 0; j < i && x_col < (xx_cols - 2); j++)
   1088   1.1       jtc 	    x_e_putc(' ');
   1089   1.1       jtc 	  i = ' ';
   1090   1.1       jtc 	  if (xep > xlp)		/* more off screen */
   1091   1.1       jtc 	  {
   1092   1.1       jtc 	    if (xbp > xbuf)
   1093   1.1       jtc 	      i = '*';
   1094   1.1       jtc 	    else
   1095   1.1       jtc 	      i = '>';
   1096   1.1       jtc 	  }
   1097   1.1       jtc 	  else
   1098   1.1       jtc 	    if (xbp > xbuf)
   1099   1.1       jtc 	      i = '<';
   1100   1.1       jtc 	  x_e_putc(i);
   1101   1.1       jtc 	  j++;
   1102   1.1       jtc 	  while (j--)
   1103   1.1       jtc 	    x_e_putc('\b');
   1104   1.1       jtc 	}
   1105   1.1       jtc 	for (cp = xlp; cp > xcp; )
   1106   1.1       jtc 	  x_bs(*--cp);
   1107   1.1       jtc 	x_adj_ok = 1;
   1108   1.1       jtc 	D__(x_flush();)
   1109   1.1       jtc 	return;
   1110   1.1       jtc }
   1111   1.1       jtc 
   1112   1.1       jtc static int
   1113   1.1       jtc x_transpose(c)
   1114   1.1       jtc 	int c;
   1115   1.1       jtc {
   1116   1.1       jtc 	char	tmp;
   1117   1.1       jtc 
   1118   1.1       jtc 	/* What transpose is meant to do seems to be up for debate. This
   1119   1.1       jtc 	 * is a general summary of the options; the text is abcd with the
   1120   1.1       jtc 	 * upper case character or underscore indicating the cursor positiion:
   1121   1.1       jtc 	 *     Who			Before	After  Before	After
   1122   1.1       jtc 	 *     at&t ksh in emacs mode:	abCd	abdC   abcd_	(bell)
   1123   1.1       jtc 	 *     at&t ksh in gmacs mode:	abCd	baCd   abcd_	abdc_
   1124   1.1       jtc 	 *     gnu emacs:		abCd	acbD   abcd_	abdc_
   1125   1.1       jtc 	 * Pdksh currently goes with GNU behavior since I believe this is the
   1126   1.1       jtc 	 * most common version of emacs, unless in gmacs mode, in which case
   1127   1.1       jtc 	 * it does the at&t ksh gmacs mdoe.
   1128   1.1       jtc 	 * This should really be broken up into 3 functions so users can bind
   1129   1.1       jtc 	 * to the one they want.
   1130   1.1       jtc 	 */
   1131   1.1       jtc 	if (xcp == xbuf) {
   1132   1.1       jtc 		x_e_putc(BEL);
   1133   1.1       jtc 		return KSTD;
   1134   1.1       jtc 	} else if (xcp == xep || Flag(FGMACS)) {
   1135   1.1       jtc 		if (xcp - xbuf == 1) {
   1136   1.1       jtc 			x_e_putc(BEL);
   1137   1.1       jtc 			return KSTD;
   1138   1.1       jtc 		}
   1139   1.1       jtc 		/* Gosling/Unipress emacs style: Swap two characters before the
   1140   1.1       jtc 		 * cursor, do not change cursor position
   1141   1.1       jtc 		 */
   1142   1.1       jtc 		x_bs(xcp[-1]);
   1143   1.1       jtc 		x_bs(xcp[-2]);
   1144   1.1       jtc 		x_zotc(xcp[-1]);
   1145   1.1       jtc 		x_zotc(xcp[-2]);
   1146   1.1       jtc 		tmp = xcp[-1];
   1147   1.1       jtc 		xcp[-1] = xcp[-2];
   1148   1.1       jtc 		xcp[-2] = tmp;
   1149   1.1       jtc 	} else {
   1150   1.1       jtc 		/* GNU emacs style: Swap the characters before and under the
   1151   1.1       jtc 		 * cursor, move cursor position along one.
   1152   1.1       jtc 		 */
   1153   1.1       jtc 		x_bs(xcp[-1]);
   1154   1.1       jtc 		x_zotc(xcp[0]);
   1155   1.1       jtc 		x_zotc(xcp[-1]);
   1156   1.1       jtc 		tmp = xcp[-1];
   1157   1.1       jtc 		xcp[-1] = xcp[0];
   1158   1.1       jtc 		xcp[0] = tmp;
   1159   1.1       jtc 		x_bs(xcp[0]);
   1160   1.1       jtc 		x_goto(xcp + 1);
   1161   1.1       jtc 	}
   1162   1.1       jtc 	return KSTD;
   1163   1.1       jtc }
   1164   1.1       jtc 
   1165   1.1       jtc static int
   1166   1.1       jtc x_literal(c)
   1167   1.1       jtc 	int c;
   1168   1.1       jtc {
   1169   1.1       jtc 	x_curprefix = -1;
   1170   1.1       jtc 	return KSTD;
   1171   1.1       jtc }
   1172   1.1       jtc 
   1173   1.1       jtc static int
   1174   1.1       jtc x_meta1(c)
   1175   1.1       jtc 	int c;
   1176   1.1       jtc {
   1177   1.1       jtc 	x_curprefix = 1;
   1178   1.1       jtc 	return KSTD;
   1179   1.1       jtc }
   1180   1.1       jtc 
   1181   1.1       jtc static int
   1182   1.1       jtc x_meta2(c)
   1183   1.1       jtc 	int c;
   1184   1.1       jtc {
   1185   1.1       jtc 	x_curprefix = 2;
   1186   1.1       jtc 	return KSTD;
   1187   1.1       jtc }
   1188   1.1       jtc 
   1189   1.1       jtc #ifdef OS2
   1190   1.1       jtc static int
   1191   1.1       jtc x_meta3(c)
   1192   1.1       jtc 	int c;
   1193   1.1       jtc {
   1194   1.1       jtc 	x_curprefix = 3;
   1195   1.1       jtc 	return KSTD;
   1196   1.1       jtc }
   1197   1.1       jtc #endif /* OS2 */
   1198   1.1       jtc 
   1199   1.1       jtc static int
   1200   1.1       jtc x_kill(c)
   1201   1.1       jtc 	int c;
   1202   1.1       jtc {
   1203   1.1       jtc 	int col = xcp - xbuf;
   1204   1.1       jtc 	int lastcol = xep - xbuf;
   1205   1.1       jtc 	int ndel;
   1206   1.1       jtc 
   1207   1.1       jtc 	if (x_arg_defaulted)
   1208   1.1       jtc 		x_arg = lastcol;
   1209   1.1       jtc 	else if (x_arg > lastcol)
   1210   1.1       jtc 		x_arg = lastcol;
   1211   1.1       jtc 	ndel = x_arg - col;
   1212   1.1       jtc 	if (ndel < 0) {
   1213   1.1       jtc 		x_goto(xbuf + x_arg);
   1214   1.1       jtc 		ndel = -ndel;
   1215   1.1       jtc 	}
   1216   1.1       jtc 	x_delete(ndel, TRUE);
   1217   1.1       jtc 	return KSTD;
   1218   1.1       jtc }
   1219   1.1       jtc 
   1220   1.1       jtc static void
   1221   1.1       jtc x_push(nchars)
   1222   1.1       jtc 	int nchars;
   1223   1.1       jtc {
   1224   1.1       jtc 	char	*cp = str_nsave(xcp, nchars, AEDIT);
   1225   1.1       jtc 	if (killstack[killsp])
   1226   1.1       jtc 		afree((void *)killstack[killsp], AEDIT);
   1227   1.1       jtc 	killstack[killsp] = cp;
   1228   1.1       jtc 	killsp = (killsp + 1) % KILLSIZE;
   1229   1.1       jtc }
   1230   1.1       jtc 
   1231   1.1       jtc static int
   1232   1.1       jtc x_yank(c)
   1233   1.1       jtc 	int c;
   1234   1.1       jtc {
   1235   1.1       jtc 	if (killsp == 0)
   1236   1.1       jtc 		killtp = KILLSIZE;
   1237   1.1       jtc 	else
   1238   1.1       jtc 		killtp = killsp;
   1239   1.1       jtc 	killtp --;
   1240   1.1       jtc 	if (killstack[killtp] == 0)  {
   1241   1.1       jtc 		x_e_puts("\nnothing to yank");
   1242   1.1       jtc 		x_redraw(-1);
   1243   1.1       jtc 		return KSTD;
   1244   1.1       jtc 	}
   1245   1.1       jtc 	xmp = xcp;
   1246   1.1       jtc 	x_ins(killstack[killtp]);
   1247   1.1       jtc 	return KSTD;
   1248   1.1       jtc }
   1249   1.1       jtc 
   1250   1.1       jtc static int
   1251   1.1       jtc x_meta_yank(c)
   1252   1.1       jtc 	int c;
   1253   1.1       jtc {
   1254   1.1       jtc 	int	len;
   1255   1.1       jtc 	if (x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank) {
   1256   1.1       jtc 		x_e_puts("\nyank something first");
   1257   1.1       jtc 		x_redraw(-1);
   1258   1.1       jtc 		return KSTD;
   1259   1.1       jtc 	}
   1260   1.1       jtc 	len = strlen(killstack[killtp]);
   1261   1.1       jtc 	x_goto(xcp - len);
   1262   1.1       jtc 	x_delete(len, FALSE);
   1263   1.6   hubertf 	do {
   1264   1.1       jtc 		if (killtp == 0)
   1265   1.1       jtc 			killtp = KILLSIZE - 1;
   1266   1.1       jtc 		else
   1267   1.1       jtc 			killtp--;
   1268   1.6   hubertf 	} while (killstack[killtp] == 0);
   1269   1.1       jtc 	x_ins(killstack[killtp]);
   1270   1.1       jtc 	return KSTD;
   1271   1.1       jtc }
   1272   1.1       jtc 
   1273   1.1       jtc static int
   1274   1.1       jtc x_abort(c)
   1275   1.1       jtc 	int c;
   1276   1.1       jtc {
   1277   1.1       jtc 	/* x_zotc(c); */
   1278   1.1       jtc 	xlp = xep = xcp = xbp = xbuf;
   1279   1.1       jtc 	xlp_valid = TRUE;
   1280   1.1       jtc 	*xcp = 0;
   1281   1.1       jtc 	return KINTR;
   1282   1.1       jtc }
   1283   1.1       jtc 
   1284   1.1       jtc static int
   1285   1.1       jtc x_error(c)
   1286   1.1       jtc 	int c;
   1287   1.1       jtc {
   1288   1.1       jtc 	x_e_putc(BEL);
   1289   1.1       jtc 	return KSTD;
   1290   1.1       jtc }
   1291   1.1       jtc 
   1292   1.1       jtc static int
   1293   1.1       jtc x_stuffreset(c)
   1294   1.1       jtc 	int c;
   1295   1.1       jtc {
   1296   1.1       jtc #ifdef TIOCSTI
   1297   1.1       jtc 	(void)x_stuff(c);
   1298   1.1       jtc 	return KINTR;
   1299   1.1       jtc #else
   1300   1.1       jtc 	x_zotc(c);
   1301   1.1       jtc 	xlp = xcp = xep = xbp = xbuf;
   1302   1.1       jtc 	xlp_valid = TRUE;
   1303   1.1       jtc 	*xcp = 0;
   1304   1.1       jtc 	x_redraw(-1);
   1305   1.1       jtc 	return KSTD;
   1306   1.1       jtc #endif
   1307   1.1       jtc }
   1308   1.1       jtc 
   1309   1.1       jtc static int
   1310   1.1       jtc x_stuff(c)
   1311   1.1       jtc 	int c;
   1312   1.1       jtc {
   1313   1.1       jtc #if 0 || defined TIOCSTI
   1314   1.1       jtc 	char	ch = c;
   1315   1.1       jtc 	bool_t	savmode = x_mode(FALSE);
   1316   1.1       jtc 
   1317   1.1       jtc 	(void)ioctl(TTY, TIOCSTI, &ch);
   1318   1.1       jtc 	(void)x_mode(savmode);
   1319   1.1       jtc 	x_redraw(-1);
   1320   1.1       jtc #endif
   1321   1.1       jtc 	return KSTD;
   1322   1.1       jtc }
   1323   1.1       jtc 
   1324   1.1       jtc static char *
   1325   1.1       jtc x_mapin(cp)
   1326   1.1       jtc 	const char *cp;
   1327   1.1       jtc {
   1328   1.1       jtc 	char *new, *op;
   1329   1.1       jtc 
   1330   1.1       jtc 	op = new = str_save(cp, ATEMP);
   1331   1.1       jtc 	while (*cp)  {
   1332   1.1       jtc 		/* XXX -- should handle \^ escape? */
   1333   1.1       jtc 		if (*cp == '^')  {
   1334   1.1       jtc 			cp++;
   1335   1.1       jtc #ifdef OS2
   1336   1.1       jtc 			if (*cp == '0')	/* To define function keys */
   1337   1.1       jtc 				*op++ = 0xE0;
   1338   1.1       jtc 			else
   1339   1.1       jtc #endif /* OS2 */
   1340   1.1       jtc 			if (*cp >= '?')	/* includes '?'; ASCII */
   1341   1.1       jtc 				*op++ = CTRL(*cp);
   1342   1.1       jtc 			else  {
   1343   1.1       jtc 				*op++ = '^';
   1344   1.1       jtc 				cp--;
   1345   1.1       jtc 			}
   1346   1.1       jtc 		} else
   1347   1.1       jtc 			*op++ = *cp;
   1348   1.1       jtc 		cp++;
   1349   1.1       jtc 	}
   1350   1.1       jtc 	*op = '\0';
   1351   1.1       jtc 
   1352   1.1       jtc 	return new;
   1353   1.1       jtc }
   1354   1.1       jtc 
   1355   1.1       jtc static char *
   1356   1.1       jtc x_mapout(c)
   1357   1.1       jtc 	int c;
   1358   1.1       jtc {
   1359   1.1       jtc 	static char buf[8];
   1360   1.1       jtc 	register char *p = buf;
   1361   1.1       jtc 
   1362   1.1       jtc #ifdef OS2
   1363   1.6   hubertf 	if (c == 0xE0) {
   1364   1.1       jtc 		*p++ = '^';
   1365   1.1       jtc 		*p++ = '0';
   1366   1.6   hubertf 	} else
   1367   1.1       jtc #endif /* OS2 */
   1368   1.6   hubertf 	if (iscntrl(c))  {
   1369   1.6   hubertf 		*p++ = '^';
   1370   1.6   hubertf 		*p++ = UNCTRL(c);
   1371   1.1       jtc 	} else
   1372   1.1       jtc 		*p++ = c;
   1373   1.1       jtc 	*p = 0;
   1374   1.1       jtc 	return buf;
   1375   1.1       jtc }
   1376   1.1       jtc 
   1377   1.1       jtc static void
   1378   1.1       jtc x_print(prefix, key)
   1379   1.1       jtc 	int prefix, key;
   1380   1.1       jtc {
   1381   1.1       jtc 	if (prefix == 1)
   1382   1.1       jtc 		shprintf("%s", x_mapout(x_prefix1));
   1383   1.1       jtc 	if (prefix == 2)
   1384   1.1       jtc 		shprintf("%s", x_mapout(x_prefix2));
   1385   1.1       jtc #ifdef OS2
   1386   1.1       jtc 	if (prefix == 3)
   1387   1.1       jtc 		shprintf("%s", x_mapout(x_prefix3));
   1388   1.1       jtc #endif /* OS2 */
   1389   1.1       jtc 	shprintf("%s = ", x_mapout(key));
   1390   1.1       jtc 	if (x_tab[prefix][key] != XFUNC_ins_string)
   1391   1.1       jtc 		shprintf("%s\n", x_ftab[x_tab[prefix][key]].xf_name);
   1392   1.1       jtc 	else
   1393   1.1       jtc 		shprintf("'%s'\n", x_atab[prefix][key]);
   1394   1.1       jtc }
   1395   1.1       jtc 
   1396   1.1       jtc int
   1397   1.1       jtc x_bind(a1, a2, macro, list)
   1398   1.1       jtc 	const char *a1, *a2;
   1399   1.1       jtc 	int macro;		/* bind -m */
   1400   1.1       jtc 	int list;		/* bind -l */
   1401   1.1       jtc {
   1402   1.1       jtc 	Findex f;
   1403   1.1       jtc 	int prefix, key;
   1404   1.1       jtc 	char *sp = NULL;
   1405   1.1       jtc 	char *m1, *m2;
   1406   1.1       jtc 
   1407   1.1       jtc 	if (x_tab == NULL) {
   1408   1.1       jtc 		bi_errorf("cannot bind, not a tty");
   1409   1.1       jtc 		return 1;
   1410   1.1       jtc 	}
   1411   1.1       jtc 
   1412   1.1       jtc 	/* List function names */
   1413   1.1       jtc 	if (list) {
   1414   1.1       jtc 		for (f = 0; f < NELEM(x_ftab); f++)
   1415   1.1       jtc 			if (x_ftab[f].xf_name
   1416   1.1       jtc 			    && !(x_ftab[f].xf_flags & XF_NOBIND))
   1417   1.1       jtc 				shprintf("%s\n", x_ftab[f].xf_name);
   1418   1.1       jtc 		return 0;
   1419   1.1       jtc 	}
   1420   1.1       jtc 
   1421   1.1       jtc 	if (a1 == NULL) {
   1422   1.1       jtc 		for (prefix = 0; prefix < X_NTABS; prefix++)
   1423   1.1       jtc 			for (key = 0; key < X_TABSZ; key++) {
   1424   1.1       jtc 				f = x_tab[prefix][key];
   1425   1.1       jtc 				if (f == XFUNC_insert || f == XFUNC_error
   1426   1.1       jtc 				    || (macro && f != XFUNC_ins_string))
   1427   1.1       jtc 					continue;
   1428   1.1       jtc 				x_print(prefix, key);
   1429   1.1       jtc 			}
   1430   1.1       jtc 		return 0;
   1431   1.1       jtc 	}
   1432   1.1       jtc 
   1433   1.1       jtc 	m1 = x_mapin(a1);
   1434   1.1       jtc 	prefix = key = 0;
   1435   1.1       jtc 	for (;; m1++) {
   1436   1.1       jtc 		key = *m1 & CHARMASK;
   1437   1.1       jtc 		if (x_tab[prefix][key] == XFUNC_meta1)
   1438   1.1       jtc 			prefix = 1;
   1439   1.1       jtc 		else if (x_tab[prefix][key] == XFUNC_meta2)
   1440   1.1       jtc 			prefix = 2;
   1441   1.1       jtc #ifdef OS2
   1442   1.1       jtc 		else if (x_tab[prefix][key] == XFUNC_meta3)
   1443   1.1       jtc 			prefix = 3;
   1444   1.1       jtc #endif /* OS2 */
   1445   1.1       jtc 		else
   1446   1.1       jtc 			break;
   1447   1.1       jtc 	}
   1448   1.1       jtc 
   1449   1.1       jtc 	if (a2 == NULL) {
   1450   1.1       jtc 		x_print(prefix, key);
   1451   1.1       jtc 		return 0;
   1452   1.1       jtc 	}
   1453   1.1       jtc 
   1454   1.1       jtc 	if (*a2 == 0)
   1455   1.1       jtc 		f = XFUNC_insert;
   1456   1.1       jtc 	else if (!macro) {
   1457   1.1       jtc 		for (f = 0; f < NELEM(x_ftab); f++)
   1458   1.1       jtc 			if (x_ftab[f].xf_name
   1459   1.1       jtc 			    && strcmp(x_ftab[f].xf_name, a2) == 0)
   1460   1.1       jtc 				break;
   1461   1.1       jtc 		if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
   1462   1.1       jtc 			bi_errorf("%s: no such function", a2);
   1463   1.1       jtc 			return 1;
   1464   1.1       jtc 		}
   1465   1.1       jtc #if 0		/* This breaks the bind commands that map arrow keys */
   1466   1.1       jtc 		if (f == XFUNC_meta1)
   1467   1.1       jtc 			x_prefix1 = key;
   1468   1.1       jtc 		if (f == XFUNC_meta2)
   1469   1.1       jtc 			x_prefix2 = key;
   1470   1.1       jtc #endif /* 0 */
   1471   1.1       jtc 	} else {
   1472   1.1       jtc 		f = XFUNC_ins_string;
   1473   1.1       jtc 		m2 = x_mapin(a2);
   1474   1.1       jtc 		sp = str_save(m2, AEDIT);
   1475   1.1       jtc 	}
   1476   1.1       jtc 
   1477   1.1       jtc 	if (x_tab[prefix][key] == XFUNC_ins_string && x_atab[prefix][key])
   1478   1.1       jtc 		afree((void *)x_atab[prefix][key], AEDIT);
   1479   1.1       jtc 	x_tab[prefix][key] = f;
   1480   1.1       jtc 	x_atab[prefix][key] = sp;
   1481   1.1       jtc 
   1482   1.6   hubertf 	/* Track what the user has bound so x_emacs_keys() won't toast things */
   1483   1.6   hubertf 	if (f == XFUNC_insert)
   1484   1.6   hubertf 		x_bound[(prefix * X_TABSZ + key) / 8] &=
   1485   1.6   hubertf 			~(1 << ((prefix * X_TABSZ + key) % 8));
   1486   1.6   hubertf 	else
   1487   1.6   hubertf 		x_bound[(prefix * X_TABSZ + key) / 8] |=
   1488   1.6   hubertf 			(1 << ((prefix * X_TABSZ + key) % 8));
   1489   1.6   hubertf 
   1490   1.1       jtc 	return 0;
   1491   1.1       jtc }
   1492   1.1       jtc 
   1493   1.1       jtc void
   1494   1.1       jtc x_init_emacs()
   1495   1.1       jtc {
   1496   1.1       jtc 	register int i, j;
   1497   1.1       jtc 
   1498   1.1       jtc 	ainit(AEDIT);
   1499   1.1       jtc 	x_nextcmd = -1;
   1500   1.1       jtc 
   1501   1.1       jtc 	x_tab = (Findex (*)[X_TABSZ]) alloc(sizeofN(*x_tab, X_NTABS), AEDIT);
   1502   1.1       jtc 	for (j = 0; j < X_TABSZ; j++)
   1503   1.1       jtc 		x_tab[0][j] = XFUNC_insert;
   1504   1.1       jtc 	for (i = 1; i < X_NTABS; i++)
   1505   1.1       jtc 		for (j = 0; j < X_TABSZ; j++)
   1506   1.1       jtc 			x_tab[i][j] = XFUNC_error;
   1507   1.1       jtc 	for (i = 0; i < NELEM(x_defbindings); i++)
   1508   1.4       mrg 		x_tab[(int)x_defbindings[i].xdb_tab][(int)x_defbindings[i].xdb_char]
   1509   1.1       jtc 			= x_defbindings[i].xdb_func;
   1510   1.1       jtc 
   1511   1.1       jtc 	x_atab = (char *(*)[X_TABSZ]) alloc(sizeofN(*x_atab, X_NTABS), AEDIT);
   1512   1.1       jtc 	for (i = 1; i < X_NTABS; i++)
   1513   1.1       jtc 		for (j = 0; j < X_TABSZ; j++)
   1514   1.1       jtc 			x_atab[i][j] = NULL;
   1515   1.1       jtc }
   1516   1.1       jtc 
   1517   1.6   hubertf static void
   1518   1.6   hubertf bind_if_not_bound(p, k, func)
   1519   1.6   hubertf 	int p, k;
   1520   1.6   hubertf 	int func;
   1521   1.6   hubertf {
   1522   1.6   hubertf 	/* Has user already bound this key?  If so, don't override it */
   1523   1.6   hubertf 	if (x_bound[((p) * X_TABSZ + (k)) / 8]
   1524   1.6   hubertf 	    & (1 << (((p) * X_TABSZ + (k)) % 8)))
   1525   1.6   hubertf 		return;
   1526   1.6   hubertf 
   1527   1.6   hubertf 	x_tab[p][k] = func;
   1528   1.6   hubertf }
   1529   1.6   hubertf 
   1530   1.1       jtc void
   1531   1.1       jtc x_emacs_keys(ec)
   1532   1.1       jtc 	X_chars *ec;
   1533   1.1       jtc {
   1534   1.6   hubertf 	if (ec->erase >= 0) {
   1535   1.6   hubertf 		bind_if_not_bound(0, ec->erase, XFUNC_del_back);
   1536   1.6   hubertf 		bind_if_not_bound(1, ec->erase, XFUNC_del_bword);
   1537   1.6   hubertf 	}
   1538   1.6   hubertf 	if (ec->kill >= 0)
   1539   1.6   hubertf 		bind_if_not_bound(0, ec->kill, XFUNC_del_line);
   1540   1.6   hubertf 	if (ec->werase >= 0)
   1541   1.6   hubertf 		bind_if_not_bound(0, ec->werase, XFUNC_del_bword);
   1542   1.6   hubertf 	if (ec->intr >= 0)
   1543   1.6   hubertf 		bind_if_not_bound(0, ec->intr, XFUNC_abort);
   1544   1.6   hubertf 	if (ec->quit >= 0)
   1545   1.6   hubertf 		bind_if_not_bound(0, ec->quit, XFUNC_noop);
   1546   1.1       jtc }
   1547   1.1       jtc 
   1548   1.1       jtc static int
   1549   1.1       jtc x_set_mark(c)
   1550   1.1       jtc 	int c;
   1551   1.1       jtc {
   1552   1.1       jtc 	xmp = xcp;
   1553   1.1       jtc 	return KSTD;
   1554   1.1       jtc }
   1555   1.1       jtc 
   1556   1.1       jtc static int
   1557   1.1       jtc x_kill_region(c)
   1558   1.1       jtc 	int c;
   1559   1.1       jtc {
   1560   1.1       jtc 	int	rsize;
   1561   1.1       jtc 	char	*xr;
   1562   1.1       jtc 
   1563   1.1       jtc 	if (xmp == NULL) {
   1564   1.1       jtc 		x_e_putc(BEL);
   1565   1.1       jtc 		return KSTD;
   1566   1.1       jtc 	}
   1567   1.1       jtc 	if (xmp > xcp) {
   1568   1.1       jtc 		rsize = xmp - xcp;
   1569   1.1       jtc 		xr = xcp;
   1570   1.1       jtc 	} else {
   1571   1.1       jtc 		rsize = xcp - xmp;
   1572   1.1       jtc 		xr = xmp;
   1573   1.1       jtc 	}
   1574   1.1       jtc 	x_goto(xr);
   1575   1.1       jtc 	x_delete(rsize, TRUE);
   1576   1.1       jtc 	xmp = xr;
   1577   1.1       jtc 	return KSTD;
   1578   1.1       jtc }
   1579   1.1       jtc 
   1580   1.1       jtc static int
   1581   1.1       jtc x_xchg_point_mark(c)
   1582   1.1       jtc 	int c;
   1583   1.1       jtc {
   1584   1.1       jtc 	char	*tmp;
   1585   1.1       jtc 
   1586   1.1       jtc 	if (xmp == NULL) {
   1587   1.1       jtc 		x_e_putc(BEL);
   1588   1.1       jtc 		return KSTD;
   1589   1.1       jtc 	}
   1590   1.1       jtc 	tmp = xmp;
   1591   1.1       jtc 	xmp = xcp;
   1592   1.1       jtc 	x_goto( tmp );
   1593   1.1       jtc 	return KSTD;
   1594   1.1       jtc }
   1595   1.1       jtc 
   1596   1.1       jtc static int
   1597   1.1       jtc x_version(c)
   1598   1.1       jtc 	int c;
   1599   1.1       jtc {
   1600   1.1       jtc 	char *o_xbuf = xbuf, *o_xend = xend;
   1601   1.1       jtc 	char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
   1602   1.1       jtc 	int lim = x_lastcp() - xbp;
   1603   1.1       jtc 
   1604   1.1       jtc 	xbuf = xbp = xcp = (char *) ksh_version + 4;
   1605   1.1       jtc 	xend = xep = (char *) ksh_version + 4 + strlen(ksh_version + 4);
   1606   1.1       jtc 	x_redraw(lim);
   1607   1.1       jtc 	x_flush();
   1608   1.1       jtc 
   1609   1.1       jtc 	c = x_e_getc();
   1610   1.1       jtc 	xbuf = o_xbuf;
   1611   1.1       jtc 	xend = o_xend;
   1612   1.1       jtc 	xbp = o_xbp;
   1613   1.1       jtc 	xep = o_xep;
   1614   1.1       jtc 	xcp = o_xcp;
   1615   1.1       jtc 	x_redraw(strlen(ksh_version));
   1616   1.1       jtc 
   1617   1.1       jtc 	if (c < 0)
   1618   1.1       jtc 		return KSTD;
   1619   1.1       jtc 	/* This is what at&t ksh seems to do...  Very bizarre */
   1620   1.1       jtc 	if (c != ' ')
   1621   1.1       jtc 		x_e_ungetc(c);
   1622   1.1       jtc 
   1623   1.1       jtc 	return KSTD;
   1624   1.1       jtc }
   1625   1.1       jtc 
   1626   1.1       jtc static int
   1627   1.1       jtc x_noop(c)
   1628   1.1       jtc 	int c;
   1629   1.1       jtc {
   1630   1.1       jtc 	return KSTD;
   1631   1.1       jtc }
   1632   1.1       jtc 
   1633   1.1       jtc #ifdef SILLY
   1634   1.1       jtc static int
   1635   1.1       jtc x_game_of_life(c)
   1636   1.1       jtc 	int c;
   1637   1.1       jtc {
   1638   1.1       jtc 	char	newbuf [256+1];
   1639   1.1       jtc 	register char *ip, *op;
   1640   1.1       jtc 	int	i, len;
   1641   1.1       jtc 
   1642   1.1       jtc 	i = xep - xbuf;
   1643   1.1       jtc 	*xep = 0;
   1644   1.1       jtc 	len = x_size_str(xbuf);
   1645   1.1       jtc 	xcp = xbp = xbuf;
   1646   1.1       jtc 	memmove(newbuf+1, xbuf, i);
   1647   1.1       jtc 	newbuf[0] = 'A';
   1648   1.1       jtc 	newbuf[i] = 'A';
   1649   1.1       jtc 	for (ip = newbuf+1, op = xbuf; --i >= 0; ip++, op++)  {
   1650   1.1       jtc 		/*  Empty space  */
   1651   1.1       jtc 		if (*ip < '@' || *ip == '_' || *ip == 0x7F)  {
   1652   1.1       jtc 			/*  Two adults, make whoopee */
   1653   1.1       jtc 			if (ip[-1] < '_' && ip[1] < '_')  {
   1654   1.1       jtc 				/*  Make kid look like parents.  */
   1655   1.1       jtc 				*op = '`' + ((ip[-1] + ip[1])/2)%32;
   1656   1.1       jtc 				if (*op == 0x7F) /* Birth defect */
   1657   1.1       jtc 					*op = '`';
   1658   1.1       jtc 			}
   1659   1.1       jtc 			else
   1660   1.1       jtc 				*op = ' ';	/* nothing happens */
   1661   1.1       jtc 			continue;
   1662   1.1       jtc 		}
   1663   1.1       jtc 		/*  Child */
   1664   1.1       jtc 		if (*ip > '`')  {
   1665   1.1       jtc 			/*  All alone, dies  */
   1666   1.1       jtc 			if (ip[-1] == ' ' && ip[1] == ' ')
   1667   1.1       jtc 				*op = ' ';
   1668   1.1       jtc 			else	/*  Gets older */
   1669   1.1       jtc 				*op = *ip-'`'+'@';
   1670   1.1       jtc 			continue;
   1671   1.1       jtc 		}
   1672   1.1       jtc 		/*  Adult  */
   1673   1.1       jtc 		/*  Overcrowded, dies */
   1674   1.1       jtc 		if (ip[-1] >= '@' && ip[1] >= '@')  {
   1675   1.1       jtc 			*op = ' ';
   1676   1.1       jtc 			continue;
   1677   1.1       jtc 		}
   1678   1.1       jtc 		*op = *ip;
   1679   1.1       jtc 	}
   1680   1.1       jtc 	*op = 0;
   1681   1.1       jtc 	x_redraw(len);
   1682   1.1       jtc 	return KSTD;
   1683   1.1       jtc }
   1684   1.1       jtc #endif
   1685   1.1       jtc 
   1686   1.1       jtc /*
   1687   1.1       jtc  *	File/command name completion routines
   1688   1.1       jtc  */
   1689   1.1       jtc 
   1690   1.1       jtc 
   1691   1.1       jtc static int
   1692   1.1       jtc x_comp_comm(c)
   1693   1.1       jtc 	int c;
   1694   1.1       jtc {
   1695   1.1       jtc 	do_complete(XCF_COMMAND, CT_COMPLETE);
   1696   1.1       jtc 	return KSTD;
   1697   1.1       jtc }
   1698   1.1       jtc static int
   1699   1.1       jtc x_list_comm(c)
   1700   1.1       jtc 	int c;
   1701   1.1       jtc {
   1702   1.1       jtc 	do_complete(XCF_COMMAND, CT_LIST);
   1703   1.1       jtc 	return KSTD;
   1704   1.1       jtc }
   1705   1.1       jtc static int
   1706   1.1       jtc x_complete(c)
   1707   1.1       jtc 	int c;
   1708   1.1       jtc {
   1709   1.1       jtc 	do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
   1710   1.1       jtc 	return KSTD;
   1711   1.1       jtc }
   1712   1.1       jtc static int
   1713   1.1       jtc x_enumerate(c)
   1714   1.1       jtc 	int c;
   1715   1.1       jtc {
   1716   1.1       jtc 	do_complete(XCF_COMMAND_FILE, CT_LIST);
   1717   1.1       jtc 	return KSTD;
   1718   1.1       jtc }
   1719   1.1       jtc static int
   1720   1.1       jtc x_comp_file(c)
   1721   1.1       jtc 	int c;
   1722   1.1       jtc {
   1723   1.1       jtc 	do_complete(XCF_FILE, CT_COMPLETE);
   1724   1.1       jtc 	return KSTD;
   1725   1.1       jtc }
   1726   1.1       jtc static int
   1727   1.1       jtc x_list_file(c)
   1728   1.1       jtc 	int c;
   1729   1.1       jtc {
   1730   1.1       jtc 	do_complete(XCF_FILE, CT_LIST);
   1731   1.1       jtc 	return KSTD;
   1732   1.1       jtc }
   1733   1.1       jtc static int
   1734   1.1       jtc x_comp_list(c)
   1735   1.1       jtc 	int c;
   1736   1.1       jtc {
   1737   1.1       jtc 	do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
   1738   1.1       jtc 	return KSTD;
   1739   1.1       jtc }
   1740   1.1       jtc static int
   1741   1.1       jtc x_expand(c)
   1742   1.1       jtc 	int c;
   1743   1.1       jtc {
   1744   1.1       jtc 	char **words;
   1745   1.1       jtc 	int nwords = 0;
   1746   1.1       jtc 	int start, end;
   1747   1.1       jtc 	int is_command;
   1748   1.1       jtc 	int i;
   1749   1.1       jtc 
   1750   1.1       jtc 	nwords = x_cf_glob(XCF_FILE,
   1751   1.1       jtc 		xbuf, xep - xbuf, xcp - xbuf,
   1752   1.1       jtc 		&start, &end, &words, &is_command);
   1753   1.1       jtc 
   1754   1.1       jtc 	if (nwords == 0) {
   1755   1.1       jtc 		x_e_putc(BEL);
   1756   1.1       jtc 		return KSTD;
   1757   1.1       jtc 	}
   1758   1.1       jtc 
   1759   1.1       jtc 	x_goto(xbuf + start);
   1760   1.1       jtc 	x_delete(end - start, FALSE);
   1761   1.1       jtc 	for (i = 0; i < nwords; i++)
   1762   1.1       jtc 		if (x_ins(words[i]) < 0 || (i < nwords - 1 && x_ins(space) < 0))
   1763   1.1       jtc 		{
   1764   1.1       jtc 			x_e_putc(BEL);
   1765   1.1       jtc 			return KSTD;
   1766   1.1       jtc 		}
   1767   1.1       jtc 
   1768   1.1       jtc 	return KSTD;
   1769   1.1       jtc }
   1770   1.1       jtc 
   1771  1.11       sjg 
   1772  1.11       sjg static int
   1773  1.11       sjg is_dir(const char *path)
   1774  1.11       sjg {
   1775  1.11       sjg 	struct stat st;
   1776  1.11       sjg 
   1777  1.11       sjg 	if (stat(path, &st) == 0)
   1778  1.11       sjg 		return S_ISDIR(st.st_mode);
   1779  1.11       sjg 	return 0;
   1780  1.11       sjg }
   1781  1.11       sjg 
   1782   1.1       jtc /* type == 0 for list, 1 for complete and 2 for complete-list */
   1783   1.1       jtc static void
   1784   1.1       jtc do_complete(flags, type)
   1785   1.1       jtc 	int flags;	/* XCF_{COMMAND,FILE,COMMAND_FILE} */
   1786   1.1       jtc 	Comp_type type;
   1787   1.1       jtc {
   1788   1.1       jtc 	char **words;
   1789   1.1       jtc 	int nwords = 0;
   1790   1.1       jtc 	int start, end;
   1791   1.1       jtc 	int is_command;
   1792   1.1       jtc 	int do_glob = 1;
   1793   1.1       jtc 	Comp_type t = type;
   1794   1.1       jtc 	char *comp_word = (char *) 0;
   1795   1.1       jtc 
   1796   1.1       jtc 	if (type == CT_COMPLIST) {
   1797   1.1       jtc 		do_glob = 0;
   1798   1.1       jtc 		/* decide what we will do */
   1799   1.1       jtc 		nwords = x_cf_glob(flags,
   1800   1.1       jtc 			xbuf, xep - xbuf, xcp - xbuf,
   1801   1.1       jtc 			&start, &end, &words, &is_command);
   1802   1.1       jtc 		if (nwords > 0) {
   1803   1.1       jtc 			if (nwords > 1) {
   1804   1.1       jtc 				int len = x_longest_prefix(nwords, words);
   1805   1.1       jtc 
   1806   1.1       jtc 				t = CT_LIST;
   1807   1.1       jtc 				/* Do completion if prefix matches original
   1808   1.1       jtc 				 * prefix (ie, no globbing chars), otherwise
   1809   1.1       jtc 				 * don't bother
   1810   1.1       jtc 				 */
   1811   1.1       jtc 				if (strncmp(words[0], xbuf + start, end - start)
   1812   1.1       jtc 									== 0)
   1813   1.1       jtc 					comp_word = str_nsave(words[0], len,
   1814   1.1       jtc 						ATEMP);
   1815   1.1       jtc 				else
   1816   1.1       jtc 					type = CT_LIST;
   1817   1.1       jtc 				/* Redo globing to show full paths if this
   1818   1.1       jtc 				 * is a command.
   1819   1.1       jtc 				 */
   1820   1.1       jtc 				if (is_command) {
   1821   1.1       jtc 					do_glob = 1;
   1822   1.1       jtc 					x_free_words(nwords, words);
   1823   1.1       jtc 				}
   1824   1.1       jtc 			} else
   1825   1.1       jtc 				type = t = CT_COMPLETE;
   1826   1.1       jtc 		}
   1827   1.1       jtc 	}
   1828   1.1       jtc 	if (do_glob)
   1829   1.1       jtc 		nwords = x_cf_glob(flags | (t == CT_LIST ? XCF_FULLPATH : 0),
   1830   1.1       jtc 			xbuf, xep - xbuf, xcp - xbuf,
   1831   1.1       jtc 			&start, &end, &words, &is_command);
   1832   1.1       jtc 	if (nwords == 0) {
   1833   1.1       jtc 		x_e_putc(BEL);
   1834   1.1       jtc 		return;
   1835   1.1       jtc 	}
   1836   1.1       jtc 	switch (type) {
   1837   1.1       jtc 	  case CT_LIST:
   1838   1.1       jtc 		x_print_expansions(nwords, words, is_command);
   1839   1.1       jtc 		x_redraw(0);
   1840   1.1       jtc 		break;
   1841   1.1       jtc 
   1842   1.1       jtc 	  case CT_COMPLIST:
   1843   1.1       jtc 		/* Only get here if nwords > 1 && comp_word is set */
   1844   1.1       jtc 		{
   1845   1.1       jtc 			int olen = end - start;
   1846   1.1       jtc 			int nlen = strlen(comp_word);
   1847   1.1       jtc 
   1848   1.1       jtc 			x_print_expansions(nwords, words, is_command);
   1849   1.1       jtc 			xcp = xbuf + end;
   1850   1.9  jdolecek 			x_escape(comp_word + olen, nlen - olen, x_emacs_putbuf);
   1851   1.1       jtc 			x_redraw(0);
   1852   1.1       jtc 		}
   1853   1.1       jtc 		break;
   1854   1.1       jtc 
   1855   1.1       jtc 	  case CT_COMPLETE:
   1856   1.1       jtc 		{
   1857   1.1       jtc 			int nlen = x_longest_prefix(nwords, words);
   1858   1.1       jtc 
   1859   1.1       jtc 			if (nlen > 0) {
   1860   1.1       jtc 				x_goto(xbuf + start);
   1861   1.1       jtc 				x_delete(end - start, FALSE);
   1862   1.8  jdolecek 				x_escape(words[0], nlen, x_emacs_putbuf);
   1863   1.8  jdolecek 				x_adjust();
   1864   1.1       jtc 				/* If single match is not a directory, add a
   1865   1.1       jtc 				 * space to the end...
   1866   1.1       jtc 				 */
   1867   1.1       jtc 				if (nwords == 1
   1868  1.11       sjg 				    && !ISDIRSEP(words[0][nlen - 1])) {
   1869  1.11       sjg 					/*
   1870  1.11       sjg 					 * we may be here because we
   1871  1.11       sjg 					 * just expanded $HOME in
   1872  1.11       sjg 					 * which case adding '/' is
   1873  1.11       sjg 					 * correct.
   1874  1.11       sjg 					 */
   1875  1.11       sjg 					x_ins(is_dir(words[0]) ? slash : space);
   1876  1.11       sjg 				}
   1877   1.1       jtc 			} else
   1878   1.1       jtc 				x_e_putc(BEL);
   1879   1.1       jtc 		}
   1880   1.1       jtc 		break;
   1881   1.1       jtc 	}
   1882   1.1       jtc }
   1883   1.1       jtc 
   1884   1.1       jtc /* NAME:
   1885   1.1       jtc  *      x_adjust - redraw the line adjusting starting point etc.
   1886   1.1       jtc  *
   1887   1.1       jtc  * DESCRIPTION:
   1888   1.1       jtc  *      This function is called when we have exceeded the bounds
   1889   1.1       jtc  *      of the edit window.  It increments x_adj_done so that
   1890   1.1       jtc  *      functions like x_ins and x_delete know that we have been
   1891   1.1       jtc  *      called and can skip the x_bs() stuff which has already
   1892   1.1       jtc  *      been done by x_redraw.
   1893   1.1       jtc  *
   1894   1.1       jtc  * RETURN VALUE:
   1895   1.1       jtc  *      None
   1896   1.1       jtc  */
   1897   1.1       jtc 
   1898   1.1       jtc static void
   1899   1.1       jtc x_adjust()
   1900   1.1       jtc {
   1901   1.1       jtc   x_adj_done++;			/* flag the fact that we were called. */
   1902   1.1       jtc   /*
   1903   1.1       jtc    * we had a problem if the prompt length > xx_cols / 2
   1904   1.1       jtc    */
   1905   1.1       jtc   if ((xbp = xcp - (x_displen / 2)) < xbuf)
   1906   1.1       jtc     xbp = xbuf;
   1907   1.1       jtc   xlp_valid = FALSE;
   1908   1.1       jtc   x_redraw(xx_cols);
   1909   1.1       jtc   x_flush();
   1910   1.1       jtc }
   1911   1.1       jtc 
   1912   1.1       jtc static int unget_char = -1;
   1913   1.1       jtc 
   1914   1.1       jtc static void
   1915   1.1       jtc x_e_ungetc(c)
   1916   1.1       jtc 	int c;
   1917   1.1       jtc {
   1918   1.1       jtc 	unget_char = c;
   1919   1.1       jtc }
   1920   1.1       jtc 
   1921   1.1       jtc static int
   1922   1.1       jtc x_e_getc()
   1923   1.1       jtc {
   1924   1.1       jtc 	int c;
   1925   1.1       jtc 
   1926   1.1       jtc 	if (unget_char >= 0) {
   1927   1.1       jtc 		c = unget_char;
   1928   1.1       jtc 		unget_char = -1;
   1929   1.1       jtc 	} else {
   1930   1.1       jtc 		if (macroptr)  {
   1931   1.1       jtc 			c = *macroptr++;
   1932   1.1       jtc 			if (!*macroptr)
   1933   1.1       jtc 				macroptr = (char *) 0;
   1934   1.1       jtc 		} else
   1935   1.1       jtc 			c = x_getc();
   1936   1.1       jtc 	}
   1937   1.1       jtc 
   1938   1.1       jtc 	return c <= CHARMASK ? c : (c & CHARMASK);
   1939   1.1       jtc }
   1940   1.1       jtc 
   1941   1.1       jtc static void
   1942   1.1       jtc x_e_putc(c)
   1943   1.1       jtc 	int c;
   1944   1.1       jtc {
   1945   1.1       jtc   if (c == '\r' || c == '\n')
   1946   1.1       jtc     x_col = 0;
   1947   1.1       jtc   if (x_col < xx_cols)
   1948   1.1       jtc   {
   1949   1.1       jtc     x_putc(c);
   1950   1.1       jtc     switch(c)
   1951   1.1       jtc     {
   1952   1.1       jtc     case BEL:
   1953   1.1       jtc       break;
   1954   1.1       jtc     case '\r':
   1955   1.1       jtc     case '\n':
   1956   1.1       jtc     break;
   1957   1.1       jtc     case '\b':
   1958   1.1       jtc       x_col--;
   1959   1.1       jtc       break;
   1960   1.1       jtc     default:
   1961   1.1       jtc       x_col++;
   1962   1.1       jtc       break;
   1963   1.1       jtc     }
   1964   1.1       jtc   }
   1965   1.1       jtc   if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
   1966   1.1       jtc   {
   1967   1.1       jtc     x_adjust();
   1968   1.1       jtc   }
   1969   1.1       jtc }
   1970   1.1       jtc 
   1971   1.1       jtc #ifdef DEBUG
   1972   1.1       jtc static int
   1973   1.1       jtc x_debug_info(c)
   1974   1.1       jtc 	int c;
   1975   1.1       jtc {
   1976   1.1       jtc 	x_flush();
   1977   1.1       jtc 	shellf("\nksh debug:\n");
   1978   1.1       jtc 	shellf("\tx_col == %d,\t\tx_cols == %d,\tx_displen == %d\n",
   1979   1.1       jtc 		 x_col, xx_cols, x_displen);
   1980   1.1       jtc 	shellf("\txcp == 0x%lx,\txep == 0x%lx\n", (long) xcp, (long) xep);
   1981   1.1       jtc 	shellf("\txbp == 0x%lx,\txbuf == 0x%lx\n", (long) xbp, (long) xbuf);
   1982   1.1       jtc 	shellf("\txlp == 0x%lx\n", (long) xlp);
   1983   1.1       jtc 	shellf("\txlp == 0x%lx\n", (long) x_lastcp());
   1984   1.1       jtc 	shellf(newline);
   1985   1.1       jtc 	x_redraw(-1);
   1986   1.1       jtc 	return 0;
   1987   1.1       jtc }
   1988   1.1       jtc #endif
   1989   1.1       jtc 
   1990   1.1       jtc static void
   1991   1.1       jtc x_e_puts(s)
   1992   1.1       jtc 	const char *s;
   1993   1.1       jtc {
   1994   1.1       jtc   register int	adj = x_adj_done;
   1995   1.1       jtc 
   1996   1.1       jtc   while (*s && adj == x_adj_done)
   1997   1.1       jtc     x_e_putc(*s++);
   1998   1.1       jtc }
   1999   1.1       jtc 
   2000   1.1       jtc /* NAME:
   2001   1.1       jtc  *      x_set_arg - set an arg value for next function
   2002   1.1       jtc  *
   2003   1.1       jtc  * DESCRIPTION:
   2004   1.1       jtc  *      This is a simple implementation of M-[0-9].
   2005   1.1       jtc  *
   2006   1.1       jtc  * RETURN VALUE:
   2007   1.1       jtc  *      KSTD
   2008   1.1       jtc  */
   2009   1.1       jtc 
   2010   1.1       jtc static int
   2011   1.1       jtc x_set_arg(c)
   2012   1.1       jtc 	int c;
   2013   1.1       jtc {
   2014   1.1       jtc 	int n = 0;
   2015   1.1       jtc 	int first = 1;
   2016   1.1       jtc 
   2017   1.1       jtc 	c &= CHARMASK;	/* strip command prefix */
   2018   1.1       jtc 	for (; c >= 0 && isdigit(c); c = x_e_getc(), first = 0)
   2019   1.1       jtc 		n = n * 10 + (c - '0');
   2020   1.1       jtc 	if (c < 0 || first) {
   2021   1.1       jtc 		x_e_putc(BEL);
   2022   1.1       jtc 		x_arg = 1;
   2023   1.1       jtc 		x_arg_defaulted = 1;
   2024   1.1       jtc 	} else {
   2025   1.1       jtc 		x_e_ungetc(c);
   2026   1.1       jtc 		x_arg = n;
   2027   1.1       jtc 		x_arg_defaulted = 0;
   2028   1.1       jtc 	}
   2029   1.1       jtc 	return KSTD;
   2030   1.1       jtc }
   2031   1.1       jtc 
   2032   1.1       jtc 
   2033   1.1       jtc /* Comment or uncomment the current line. */
   2034   1.1       jtc static int
   2035   1.1       jtc x_comment(c)
   2036   1.1       jtc 	int c;
   2037   1.1       jtc {
   2038   1.1       jtc 	int oldsize = x_size_str(xbuf);
   2039   1.1       jtc 	int len = xep - xbuf;
   2040   1.1       jtc 	int ret = x_do_comment(xbuf, xend - xbuf, &len);
   2041   1.1       jtc 
   2042   1.1       jtc 	if (ret < 0)
   2043   1.1       jtc 		x_e_putc(BEL);
   2044   1.1       jtc 	else {
   2045   1.1       jtc 		xep = xbuf + len;
   2046   1.1       jtc 		*xep = '\0';
   2047   1.1       jtc 		xcp = xbp = xbuf;
   2048   1.1       jtc 		x_redraw(oldsize);
   2049   1.1       jtc 		if (ret > 0)
   2050   1.1       jtc 			return x_newline('\n');
   2051   1.1       jtc 	}
   2052   1.1       jtc 	return KSTD;
   2053   1.1       jtc }
   2054   1.1       jtc 
   2055   1.1       jtc 
   2056   1.1       jtc /* NAME:
   2057   1.1       jtc  *      x_prev_histword - recover word from prev command
   2058   1.1       jtc  *
   2059   1.1       jtc  * DESCRIPTION:
   2060   1.1       jtc  *      This function recovers the last word from the previous
   2061   1.1       jtc  *      command and inserts it into the current edit line.  If a
   2062   1.1       jtc  *      numeric arg is supplied then the n'th word from the
   2063   1.1       jtc  *      start of the previous command is used.
   2064   1.1       jtc  *
   2065   1.1       jtc  *      Bound to M-.
   2066   1.1       jtc  *
   2067   1.1       jtc  * RETURN VALUE:
   2068   1.1       jtc  *      KSTD
   2069   1.1       jtc  */
   2070   1.1       jtc 
   2071   1.1       jtc static int
   2072   1.1       jtc x_prev_histword(c)
   2073   1.1       jtc 	int c;
   2074   1.1       jtc {
   2075   1.1       jtc   register char *rcp;
   2076   1.1       jtc   char *cp;
   2077   1.1       jtc 
   2078   1.6   hubertf   cp = *histptr;
   2079   1.6   hubertf   if (!cp)
   2080   1.1       jtc     x_e_putc(BEL);
   2081   1.6   hubertf   else if (x_arg_defaulted) {
   2082   1.1       jtc     rcp = &cp[strlen(cp) - 1];
   2083   1.1       jtc     /*
   2084   1.1       jtc      * ignore white-space after the last word
   2085   1.1       jtc      */
   2086   1.1       jtc     while (rcp > cp && is_cfs(*rcp))
   2087   1.1       jtc       rcp--;
   2088   1.1       jtc     while (rcp > cp && !is_cfs(*rcp))
   2089   1.1       jtc       rcp--;
   2090   1.1       jtc     if (is_cfs(*rcp))
   2091   1.1       jtc       rcp++;
   2092   1.1       jtc     x_ins(rcp);
   2093   1.1       jtc   } else {
   2094   1.1       jtc     int c;
   2095   1.1       jtc 
   2096   1.1       jtc     rcp = cp;
   2097   1.1       jtc     /*
   2098   1.1       jtc      * ignore white-space at start of line
   2099   1.1       jtc      */
   2100   1.1       jtc     while (*rcp && is_cfs(*rcp))
   2101   1.1       jtc       rcp++;
   2102   1.1       jtc     while (x_arg-- > 1)
   2103   1.1       jtc     {
   2104   1.1       jtc       while (*rcp && !is_cfs(*rcp))
   2105   1.1       jtc 	rcp++;
   2106   1.1       jtc       while (*rcp && is_cfs(*rcp))
   2107   1.1       jtc 	rcp++;
   2108   1.1       jtc     }
   2109   1.1       jtc     cp = rcp;
   2110   1.1       jtc     while (*rcp && !is_cfs(*rcp))
   2111   1.1       jtc       rcp++;
   2112   1.1       jtc     c = *rcp;
   2113   1.1       jtc     *rcp = '\0';
   2114   1.1       jtc     x_ins(cp);
   2115   1.1       jtc     *rcp = c;
   2116   1.1       jtc   }
   2117   1.1       jtc   return KSTD;
   2118   1.1       jtc }
   2119   1.1       jtc 
   2120   1.1       jtc /* Uppercase N(1) words */
   2121   1.1       jtc static int
   2122   1.1       jtc x_fold_upper(c)
   2123   1.1       jtc   int c;
   2124   1.1       jtc {
   2125   1.1       jtc 	return x_fold_case('U');
   2126   1.1       jtc }
   2127   1.1       jtc 
   2128   1.1       jtc /* Lowercase N(1) words */
   2129   1.1       jtc static int
   2130   1.1       jtc x_fold_lower(c)
   2131   1.1       jtc   int c;
   2132   1.1       jtc {
   2133   1.1       jtc 	return x_fold_case('L');
   2134   1.1       jtc }
   2135   1.1       jtc 
   2136   1.1       jtc /* Lowercase N(1) words */
   2137   1.1       jtc static int
   2138   1.1       jtc x_fold_capitialize(c)
   2139   1.1       jtc   int c;
   2140   1.1       jtc {
   2141   1.1       jtc 	return x_fold_case('C');
   2142   1.1       jtc }
   2143   1.1       jtc 
   2144   1.1       jtc /* NAME:
   2145   1.1       jtc  *      x_fold_case - convert word to UPPER/lower/Capitial case
   2146   1.1       jtc  *
   2147   1.1       jtc  * DESCRIPTION:
   2148   1.1       jtc  *      This function is used to implement M-U,M-u,M-L,M-l,M-C and M-c
   2149   1.1       jtc  *      to UPPER case, lower case or Capitalize words.
   2150   1.1       jtc  *
   2151   1.1       jtc  * RETURN VALUE:
   2152   1.1       jtc  *      None
   2153   1.1       jtc  */
   2154   1.1       jtc 
   2155   1.1       jtc static int
   2156   1.1       jtc x_fold_case(c)
   2157   1.1       jtc 	int c;
   2158   1.1       jtc {
   2159   1.1       jtc 	char *cp = xcp;
   2160   1.1       jtc 
   2161   1.1       jtc 	if (cp == xep) {
   2162   1.1       jtc 		x_e_putc(BEL);
   2163   1.1       jtc 		return KSTD;
   2164   1.1       jtc 	}
   2165   1.1       jtc 	while (x_arg--) {
   2166   1.1       jtc 		/*
   2167   1.1       jtc 		 * fisrt skip over any white-space
   2168   1.1       jtc 		 */
   2169   1.1       jtc 		while (cp != xep && is_mfs(*cp))
   2170   1.1       jtc 			cp++;
   2171   1.1       jtc 		/*
   2172   1.1       jtc 		 * do the first char on its own since it may be
   2173   1.1       jtc 		 * a different action than for the rest.
   2174   1.1       jtc 		 */
   2175   1.1       jtc 		if (cp != xep) {
   2176   1.1       jtc 			if (c == 'L') {		/* lowercase */
   2177   1.5  christos 				if (isupper((unsigned char)*cp))
   2178   1.1       jtc 					*cp = tolower(*cp);
   2179   1.1       jtc 			} else {		/* uppercase, capitialize */
   2180   1.5  christos 				if (islower((unsigned char)*cp))
   2181   1.1       jtc 					*cp = toupper(*cp);
   2182   1.1       jtc 			}
   2183   1.1       jtc 			cp++;
   2184   1.1       jtc 		}
   2185   1.1       jtc 		/*
   2186   1.1       jtc 		 * now for the rest of the word
   2187   1.1       jtc 		 */
   2188   1.5  christos 		while (cp != xep && !is_mfs((unsigned char)*cp)) {
   2189   1.1       jtc 			if (c == 'U') {		/* uppercase */
   2190   1.5  christos 				if (islower((unsigned char)*cp))
   2191   1.1       jtc 					*cp = toupper(*cp);
   2192   1.1       jtc 			} else {		/* lowercase, capitialize */
   2193   1.5  christos 				if (isupper((unsigned char)*cp))
   2194   1.1       jtc 					*cp = tolower(*cp);
   2195   1.1       jtc 			}
   2196   1.1       jtc 			cp++;
   2197   1.1       jtc 		}
   2198   1.1       jtc 	}
   2199   1.1       jtc 	x_goto(cp);
   2200   1.1       jtc 	return KSTD;
   2201   1.1       jtc }
   2202   1.1       jtc 
   2203   1.1       jtc /* NAME:
   2204   1.1       jtc  *      x_lastcp - last visible char
   2205   1.1       jtc  *
   2206   1.1       jtc  * SYNOPSIS:
   2207   1.1       jtc  *      x_lastcp()
   2208   1.1       jtc  *
   2209   1.1       jtc  * DESCRIPTION:
   2210   1.1       jtc  *      This function returns a pointer to that  char in the
   2211   1.1       jtc  *      edit buffer that will be the last displayed on the
   2212   1.1       jtc  *      screen.  The sequence:
   2213   1.1       jtc  *
   2214   1.1       jtc  *      for (cp = x_lastcp(); cp > xcp; cp)
   2215   1.1       jtc  *        x_bs(*--cp);
   2216   1.1       jtc  *
   2217   1.1       jtc  *      Will position the cursor correctly on the screen.
   2218   1.1       jtc  *
   2219   1.1       jtc  * RETURN VALUE:
   2220   1.1       jtc  *      cp or NULL
   2221   1.1       jtc  */
   2222   1.1       jtc 
   2223   1.1       jtc static char *
   2224   1.1       jtc x_lastcp()
   2225   1.1       jtc {
   2226   1.1       jtc   register char *rcp;
   2227   1.1       jtc   register int i;
   2228   1.1       jtc 
   2229   1.1       jtc   if (!xlp_valid)
   2230   1.1       jtc   {
   2231   1.1       jtc     for (i = 0, rcp = xbp; rcp < xep && i < x_displen; rcp++)
   2232   1.1       jtc       i += x_size(*rcp);
   2233   1.1       jtc     xlp = rcp;
   2234   1.1       jtc   }
   2235   1.1       jtc   xlp_valid = TRUE;
   2236   1.1       jtc   return (xlp);
   2237   1.1       jtc }
   2238   1.1       jtc 
   2239   1.1       jtc #endif /* EDIT */
   2240