1*838f5788Ssimonb /* $NetBSD: command.c,v 1.6 2023/10/06 05:49:49 simonb Exp $ */
220006a0bStron
320006a0bStron /*
4*838f5788Ssimonb * Copyright (C) 1984-2023 Mark Nudelman
520006a0bStron *
620006a0bStron * You may distribute under the terms of either the GNU General Public
720006a0bStron * License or the Less License, as specified in the README file.
820006a0bStron *
9ec18bca0Stron * For more information, see the README file.
1020006a0bStron */
1120006a0bStron
1220006a0bStron
1320006a0bStron /*
1420006a0bStron * User-level command processor.
1520006a0bStron */
1620006a0bStron
1720006a0bStron #include "less.h"
1820006a0bStron #if MSDOS_COMPILER==WIN32C
1920006a0bStron #include <windows.h>
2020006a0bStron #endif
2120006a0bStron #include "position.h"
2220006a0bStron #include "option.h"
2320006a0bStron #include "cmd.h"
2420006a0bStron
2520006a0bStron extern int erase_char, erase2_char, kill_char;
2620006a0bStron extern int sigs;
2720006a0bStron extern int quit_if_one_screen;
28*838f5788Ssimonb extern int one_screen;
2920006a0bStron extern int squished;
3020006a0bStron extern int sc_width;
3120006a0bStron extern int sc_height;
32*838f5788Ssimonb extern char *kent;
3320006a0bStron extern int swindow;
3420006a0bStron extern int jump_sline;
3520006a0bStron extern int quitting;
3620006a0bStron extern int wscroll;
3720006a0bStron extern int top_scroll;
3820006a0bStron extern int ignore_eoi;
3920006a0bStron extern int secure;
4020006a0bStron extern int hshift;
41*838f5788Ssimonb extern int bs_mode;
42*838f5788Ssimonb extern int proc_backspace;
4320006a0bStron extern int show_attn;
44*838f5788Ssimonb extern int status_col;
45ec18bca0Stron extern POSITION highest_hilite;
46*838f5788Ssimonb extern POSITION start_attnpos;
47*838f5788Ssimonb extern POSITION end_attnpos;
4820006a0bStron extern char *every_first_cmd;
4920006a0bStron extern char version[];
5020006a0bStron extern struct scrpos initial_scrpos;
5120006a0bStron extern IFILE curr_ifile;
52*838f5788Ssimonb extern void *ml_search;
53*838f5788Ssimonb extern void *ml_examine;
54*838f5788Ssimonb extern int wheel_lines;
55*838f5788Ssimonb extern int header_lines;
56*838f5788Ssimonb extern int def_search_type;
57*838f5788Ssimonb extern int updown_match;
5820006a0bStron #if SHELL_ESCAPE || PIPEC
59*838f5788Ssimonb extern void *ml_shell;
6020006a0bStron #endif
6120006a0bStron #if EDITOR
6220006a0bStron extern char *editor;
6320006a0bStron extern char *editproto;
6420006a0bStron #endif
6520006a0bStron extern int screen_trashed; /* The screen has been overwritten */
6620006a0bStron extern int shift_count;
6720006a0bStron extern int oldbot;
6820006a0bStron extern int forw_prompt;
69*838f5788Ssimonb extern int incr_search;
70*838f5788Ssimonb extern int full_screen;
71824a88bbStron extern int be_helpful;
72824a88bbStron extern int more_mode;
73*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
74*838f5788Ssimonb extern int utf_mode;
75*838f5788Ssimonb #endif
7620006a0bStron
7720006a0bStron #if SHELL_ESCAPE
7820006a0bStron static char *shellcmd = NULL; /* For holding last shell command for "!!" */
7920006a0bStron #endif
8020006a0bStron static int mca; /* The multicharacter command (action) */
8120006a0bStron static int search_type; /* The previous type of search */
82*838f5788Ssimonb static int last_search_type; /* Type of last executed search */
8320006a0bStron static LINENUM number; /* The number typed by the user */
8420006a0bStron static long fraction; /* The fractional part of the number */
85*838f5788Ssimonb static int helpprompt;
8620006a0bStron static struct loption *curropt;
8720006a0bStron static int opt_lower;
8820006a0bStron static int optflag;
8920006a0bStron static int optgetname;
9020006a0bStron static POSITION bottompos;
9120006a0bStron static int save_hshift;
92*838f5788Ssimonb static int save_bs_mode;
93*838f5788Ssimonb static int save_proc_backspace;
9420006a0bStron #if PIPEC
9520006a0bStron static char pipec;
9620006a0bStron #endif
9720006a0bStron
98*838f5788Ssimonb /* Stack of ungotten chars (via ungetcc) */
9920006a0bStron struct ungot {
10020006a0bStron struct ungot *ug_next;
101*838f5788Ssimonb LWCHAR ug_char;
10220006a0bStron };
10320006a0bStron static struct ungot* ungot = NULL;
10420006a0bStron
105*838f5788Ssimonb static void multi_search (char *pattern, int n, int silent);
10620006a0bStron
10720006a0bStron /*
10820006a0bStron * Move the cursor to start of prompt line before executing a command.
10920006a0bStron * This looks nicer if the command takes a long time before
11020006a0bStron * updating the screen.
11120006a0bStron */
cmd_exec(void)112*838f5788Ssimonb static void cmd_exec(void)
11320006a0bStron {
11420006a0bStron clear_attn();
11520006a0bStron clear_bot();
11620006a0bStron flush();
11720006a0bStron }
11820006a0bStron
11920006a0bStron /*
120*838f5788Ssimonb * Indicate we are reading a multi-character command.
12120006a0bStron */
set_mca(int action)122*838f5788Ssimonb static void set_mca(int action)
12320006a0bStron {
12420006a0bStron mca = action;
12520006a0bStron clear_bot();
12620006a0bStron clear_cmd();
127*838f5788Ssimonb }
128*838f5788Ssimonb
129*838f5788Ssimonb /*
130*838f5788Ssimonb * Indicate we are not reading a multi-character command.
131*838f5788Ssimonb */
clear_mca(void)132*838f5788Ssimonb static void clear_mca(void)
133*838f5788Ssimonb {
134*838f5788Ssimonb if (mca == 0)
135*838f5788Ssimonb return;
136*838f5788Ssimonb mca = 0;
137*838f5788Ssimonb }
138*838f5788Ssimonb
139*838f5788Ssimonb /*
140*838f5788Ssimonb * Set up the display to start a new multi-character command.
141*838f5788Ssimonb */
start_mca(int action,constant char * prompt,void * mlist,int cmdflags)142*838f5788Ssimonb static void start_mca(int action, constant char *prompt, void *mlist, int cmdflags)
143*838f5788Ssimonb {
144*838f5788Ssimonb set_mca(action);
14520006a0bStron cmd_putstr(prompt);
14620006a0bStron set_mlist(mlist, cmdflags);
14720006a0bStron }
14820006a0bStron
in_mca(void)149*838f5788Ssimonb public int in_mca(void)
15020006a0bStron {
15120006a0bStron return (mca != 0 && mca != A_PREFIX);
15220006a0bStron }
15320006a0bStron
15420006a0bStron /*
15520006a0bStron * Set up the display to start a new search command.
15620006a0bStron */
mca_search1(void)157*838f5788Ssimonb static void mca_search1(void)
15820006a0bStron {
159*838f5788Ssimonb int i;
160*838f5788Ssimonb
16120006a0bStron #if HILITE_SEARCH
16220006a0bStron if (search_type & SRCH_FILTER)
163*838f5788Ssimonb set_mca(A_FILTER);
16420006a0bStron else
16520006a0bStron #endif
16620006a0bStron if (search_type & SRCH_FORW)
167*838f5788Ssimonb set_mca(A_F_SEARCH);
16820006a0bStron else
169*838f5788Ssimonb set_mca(A_B_SEARCH);
17020006a0bStron
17120006a0bStron if (search_type & SRCH_NO_MATCH)
17220006a0bStron cmd_putstr("Non-match ");
17320006a0bStron if (search_type & SRCH_FIRST_FILE)
17420006a0bStron cmd_putstr("First-file ");
17520006a0bStron if (search_type & SRCH_PAST_EOF)
17620006a0bStron cmd_putstr("EOF-ignore ");
17720006a0bStron if (search_type & SRCH_NO_MOVE)
17820006a0bStron cmd_putstr("Keep-pos ");
17920006a0bStron if (search_type & SRCH_NO_REGEX)
18020006a0bStron cmd_putstr("Regex-off ");
181*838f5788Ssimonb if (search_type & SRCH_WRAP)
182*838f5788Ssimonb cmd_putstr("Wrap ");
183*838f5788Ssimonb for (i = 1; i <= NUM_SEARCH_COLORS; i++)
184*838f5788Ssimonb {
185*838f5788Ssimonb if (search_type & SRCH_SUBSEARCH(i))
186*838f5788Ssimonb {
187*838f5788Ssimonb char buf[INT_STRLEN_BOUND(int)+8];
188*838f5788Ssimonb SNPRINTF1(buf, sizeof(buf), "Sub-%d ", i);
189*838f5788Ssimonb cmd_putstr(buf);
190*838f5788Ssimonb }
191*838f5788Ssimonb }
19220006a0bStron
19320006a0bStron #if HILITE_SEARCH
19420006a0bStron if (search_type & SRCH_FILTER)
19520006a0bStron cmd_putstr("&/");
19620006a0bStron else
19720006a0bStron #endif
19820006a0bStron if (search_type & SRCH_FORW)
19920006a0bStron cmd_putstr("/");
20020006a0bStron else
20120006a0bStron cmd_putstr("?");
202*838f5788Ssimonb forw_prompt = 0;
203*838f5788Ssimonb }
204*838f5788Ssimonb
mca_search(void)205*838f5788Ssimonb static void mca_search(void)
206*838f5788Ssimonb {
207*838f5788Ssimonb mca_search1();
20820006a0bStron set_mlist(ml_search, 0);
20920006a0bStron }
21020006a0bStron
21120006a0bStron /*
21220006a0bStron * Set up the display to start a new toggle-option command.
21320006a0bStron */
mca_opt_toggle(void)214*838f5788Ssimonb static void mca_opt_toggle(void)
21520006a0bStron {
21620006a0bStron int no_prompt;
21720006a0bStron int flag;
21820006a0bStron char *dash;
21920006a0bStron
22020006a0bStron no_prompt = (optflag & OPT_NO_PROMPT);
22120006a0bStron flag = (optflag & ~OPT_NO_PROMPT);
22220006a0bStron dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
22320006a0bStron
224*838f5788Ssimonb set_mca(A_OPT_TOGGLE);
22520006a0bStron cmd_putstr(dash);
22620006a0bStron if (optgetname)
22720006a0bStron cmd_putstr(dash);
22820006a0bStron if (no_prompt)
22920006a0bStron cmd_putstr("(P)");
23020006a0bStron switch (flag)
23120006a0bStron {
23220006a0bStron case OPT_UNSET:
23320006a0bStron cmd_putstr("+");
23420006a0bStron break;
23520006a0bStron case OPT_SET:
23620006a0bStron cmd_putstr("!");
23720006a0bStron break;
23820006a0bStron }
239*838f5788Ssimonb forw_prompt = 0;
24020006a0bStron set_mlist(NULL, 0);
24120006a0bStron }
24220006a0bStron
24320006a0bStron /*
24420006a0bStron * Execute a multicharacter command.
24520006a0bStron */
exec_mca(void)246*838f5788Ssimonb static void exec_mca(void)
24720006a0bStron {
248*838f5788Ssimonb char *cbuf;
24920006a0bStron
25020006a0bStron cmd_exec();
25120006a0bStron cbuf = get_cmdbuf();
252*838f5788Ssimonb if (cbuf == NULL)
253*838f5788Ssimonb return;
25420006a0bStron
25520006a0bStron switch (mca)
25620006a0bStron {
25720006a0bStron case A_F_SEARCH:
25820006a0bStron case A_B_SEARCH:
259*838f5788Ssimonb multi_search(cbuf, (int) number, 0);
26020006a0bStron break;
26120006a0bStron #if HILITE_SEARCH
26220006a0bStron case A_FILTER:
26320006a0bStron search_type ^= SRCH_NO_MATCH;
26420006a0bStron set_filter_pattern(cbuf, search_type);
26520006a0bStron break;
26620006a0bStron #endif
26720006a0bStron case A_FIRSTCMD:
26820006a0bStron /*
26920006a0bStron * Skip leading spaces or + signs in the string.
27020006a0bStron */
27120006a0bStron while (*cbuf == '+' || *cbuf == ' ')
27220006a0bStron cbuf++;
27320006a0bStron if (every_first_cmd != NULL)
27420006a0bStron free(every_first_cmd);
27520006a0bStron if (*cbuf == '\0')
27620006a0bStron every_first_cmd = NULL;
27720006a0bStron else
27820006a0bStron every_first_cmd = save(cbuf);
27920006a0bStron break;
28020006a0bStron case A_OPT_TOGGLE:
28120006a0bStron toggle_option(curropt, opt_lower, cbuf, optflag);
28220006a0bStron curropt = NULL;
28320006a0bStron break;
28420006a0bStron case A_F_BRACKET:
28520006a0bStron match_brac(cbuf[0], cbuf[1], 1, (int) number);
28620006a0bStron break;
28720006a0bStron case A_B_BRACKET:
28820006a0bStron match_brac(cbuf[1], cbuf[0], 0, (int) number);
28920006a0bStron break;
29020006a0bStron #if EXAMINE
29120006a0bStron case A_EXAMINE:
29220006a0bStron if (secure)
29320006a0bStron break;
29420006a0bStron edit_list(cbuf);
29520006a0bStron #if TAGS
29620006a0bStron /* If tag structure is loaded then clean it up. */
29720006a0bStron cleantags();
29820006a0bStron #endif
29920006a0bStron break;
30020006a0bStron #endif
30120006a0bStron #if SHELL_ESCAPE
30220006a0bStron case A_SHELL:
30320006a0bStron /*
30420006a0bStron * !! just uses whatever is in shellcmd.
30520006a0bStron * Otherwise, copy cmdbuf to shellcmd,
30620006a0bStron * expanding any special characters ("%" or "#").
30720006a0bStron */
30820006a0bStron if (*cbuf != '!')
30920006a0bStron {
31020006a0bStron if (shellcmd != NULL)
31120006a0bStron free(shellcmd);
31220006a0bStron shellcmd = fexpand(cbuf);
31320006a0bStron }
31420006a0bStron
31520006a0bStron if (secure)
31620006a0bStron break;
31720006a0bStron if (shellcmd == NULL)
31820006a0bStron lsystem("", "!done");
31920006a0bStron else
32020006a0bStron lsystem(shellcmd, "!done");
32120006a0bStron break;
322*838f5788Ssimonb case A_PSHELL:
323*838f5788Ssimonb if (secure)
324*838f5788Ssimonb break;
325*838f5788Ssimonb lsystem(pr_expand(cbuf), "#done");
326*838f5788Ssimonb break;
32720006a0bStron #endif
32820006a0bStron #if PIPEC
32920006a0bStron case A_PIPE:
33020006a0bStron if (secure)
33120006a0bStron break;
33220006a0bStron (void) pipe_mark(pipec, cbuf);
33320006a0bStron error("|done", NULL_PARG);
33420006a0bStron break;
33520006a0bStron #endif
33620006a0bStron }
33720006a0bStron }
33820006a0bStron
33920006a0bStron /*
34020006a0bStron * Is a character an erase or kill char?
34120006a0bStron */
is_erase_char(int c)342*838f5788Ssimonb static int is_erase_char(int c)
34320006a0bStron {
34420006a0bStron return (c == erase_char || c == erase2_char || c == kill_char);
34520006a0bStron }
34620006a0bStron
34720006a0bStron /*
348*838f5788Ssimonb * Is a character a carriage return or newline?
349*838f5788Ssimonb */
is_newline_char(int c)350*838f5788Ssimonb static int is_newline_char(int c)
351*838f5788Ssimonb {
352*838f5788Ssimonb return (c == '\n' || c == '\r');
353*838f5788Ssimonb }
354*838f5788Ssimonb
355*838f5788Ssimonb /*
35620006a0bStron * Handle the first char of an option (after the initial dash).
35720006a0bStron */
mca_opt_first_char(int c)358*838f5788Ssimonb static int mca_opt_first_char(int c)
35920006a0bStron {
360*838f5788Ssimonb int no_prompt = (optflag & OPT_NO_PROMPT);
36120006a0bStron int flag = (optflag & ~OPT_NO_PROMPT);
36220006a0bStron if (flag == OPT_NO_TOGGLE)
36320006a0bStron {
36420006a0bStron switch (c)
36520006a0bStron {
36620006a0bStron case '_':
36720006a0bStron /* "__" = long option name. */
36820006a0bStron optgetname = TRUE;
36920006a0bStron mca_opt_toggle();
37020006a0bStron return (MCA_MORE);
37120006a0bStron }
37220006a0bStron } else
37320006a0bStron {
37420006a0bStron switch (c)
37520006a0bStron {
37620006a0bStron case '+':
37720006a0bStron /* "-+" = UNSET. */
378*838f5788Ssimonb optflag = no_prompt | ((flag == OPT_UNSET) ?
379*838f5788Ssimonb OPT_TOGGLE : OPT_UNSET);
38020006a0bStron mca_opt_toggle();
38120006a0bStron return (MCA_MORE);
38220006a0bStron case '!':
38320006a0bStron /* "-!" = SET */
384*838f5788Ssimonb optflag = no_prompt | ((flag == OPT_SET) ?
385*838f5788Ssimonb OPT_TOGGLE : OPT_SET);
38620006a0bStron mca_opt_toggle();
38720006a0bStron return (MCA_MORE);
38820006a0bStron case CONTROL('P'):
38920006a0bStron optflag ^= OPT_NO_PROMPT;
39020006a0bStron mca_opt_toggle();
39120006a0bStron return (MCA_MORE);
39220006a0bStron case '-':
39320006a0bStron /* "--" = long option name. */
39420006a0bStron optgetname = TRUE;
39520006a0bStron mca_opt_toggle();
39620006a0bStron return (MCA_MORE);
39720006a0bStron }
39820006a0bStron }
39920006a0bStron /* Char was not handled here. */
40020006a0bStron return (NO_MCA);
40120006a0bStron }
40220006a0bStron
40320006a0bStron /*
40420006a0bStron * Add a char to a long option name.
40520006a0bStron * See if we've got a match for an option name yet.
40620006a0bStron * If so, display the complete name and stop
40720006a0bStron * accepting chars until user hits RETURN.
40820006a0bStron */
mca_opt_nonfirst_char(int c)409*838f5788Ssimonb static int mca_opt_nonfirst_char(int c)
41020006a0bStron {
41120006a0bStron char *p;
41220006a0bStron char *oname;
413*838f5788Ssimonb int err;
41420006a0bStron
41520006a0bStron if (curropt != NULL)
41620006a0bStron {
41720006a0bStron /*
41820006a0bStron * Already have a match for the name.
41920006a0bStron * Don't accept anything but erase/kill.
42020006a0bStron */
42120006a0bStron if (is_erase_char(c))
42220006a0bStron return (MCA_DONE);
42320006a0bStron return (MCA_MORE);
42420006a0bStron }
42520006a0bStron /*
42620006a0bStron * Add char to cmd buffer and try to match
42720006a0bStron * the option name.
42820006a0bStron */
42920006a0bStron if (cmd_char(c) == CC_QUIT)
43020006a0bStron return (MCA_DONE);
43120006a0bStron p = get_cmdbuf();
432*838f5788Ssimonb if (p == NULL)
433*838f5788Ssimonb return (MCA_MORE);
43420006a0bStron opt_lower = ASCII_IS_LOWER(p[0]);
435*838f5788Ssimonb err = 0;
436*838f5788Ssimonb curropt = findopt_name(&p, &oname, &err);
43720006a0bStron if (curropt != NULL)
43820006a0bStron {
43920006a0bStron /*
44020006a0bStron * Got a match.
44120006a0bStron * Remember the option and
44220006a0bStron * display the full option name.
44320006a0bStron */
44420006a0bStron cmd_reset();
44520006a0bStron mca_opt_toggle();
44620006a0bStron for (p = oname; *p != '\0'; p++)
44720006a0bStron {
44820006a0bStron c = *p;
44920006a0bStron if (!opt_lower && ASCII_IS_LOWER(c))
45020006a0bStron c = ASCII_TO_UPPER(c);
45120006a0bStron if (cmd_char(c) != CC_OK)
45220006a0bStron return (MCA_DONE);
45320006a0bStron }
454*838f5788Ssimonb } else if (err != OPT_AMBIG)
455*838f5788Ssimonb {
456*838f5788Ssimonb bell();
45720006a0bStron }
45820006a0bStron return (MCA_MORE);
45920006a0bStron }
46020006a0bStron
46120006a0bStron /*
46220006a0bStron * Handle a char of an option toggle command.
46320006a0bStron */
mca_opt_char(int c)464*838f5788Ssimonb static int mca_opt_char(int c)
46520006a0bStron {
46620006a0bStron PARG parg;
46720006a0bStron
46820006a0bStron /*
46920006a0bStron * This may be a short option (single char),
47020006a0bStron * or one char of a long option name,
47120006a0bStron * or one char of the option parameter.
47220006a0bStron */
47320006a0bStron if (curropt == NULL && len_cmdbuf() == 0)
47420006a0bStron {
47520006a0bStron int ret = mca_opt_first_char(c);
47620006a0bStron if (ret != NO_MCA)
47720006a0bStron return (ret);
47820006a0bStron }
47920006a0bStron if (optgetname)
48020006a0bStron {
48120006a0bStron /* We're getting a long option name. */
482*838f5788Ssimonb if (!is_newline_char(c) && c != '=')
48320006a0bStron return (mca_opt_nonfirst_char(c));
48420006a0bStron if (curropt == NULL)
48520006a0bStron {
48620006a0bStron parg.p_string = get_cmdbuf();
487*838f5788Ssimonb if (parg.p_string == NULL)
488*838f5788Ssimonb return (MCA_MORE);
48920006a0bStron error("There is no --%s option", &parg);
49020006a0bStron return (MCA_DONE);
49120006a0bStron }
49220006a0bStron optgetname = FALSE;
49320006a0bStron cmd_reset();
49420006a0bStron } else
49520006a0bStron {
49620006a0bStron if (is_erase_char(c))
49720006a0bStron return (NO_MCA);
49820006a0bStron if (curropt != NULL)
49920006a0bStron /* We're getting the option parameter. */
50020006a0bStron return (NO_MCA);
50120006a0bStron curropt = findopt(c);
50220006a0bStron if (curropt == NULL)
50320006a0bStron {
50420006a0bStron parg.p_string = propt(c);
50520006a0bStron error("There is no %s option", &parg);
50620006a0bStron return (MCA_DONE);
50720006a0bStron }
508*838f5788Ssimonb opt_lower = ASCII_IS_LOWER(c);
50920006a0bStron }
51020006a0bStron /*
51120006a0bStron * If the option which was entered does not take a
51220006a0bStron * parameter, toggle the option immediately,
51320006a0bStron * so user doesn't have to hit RETURN.
51420006a0bStron */
51520006a0bStron if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
51620006a0bStron !opt_has_param(curropt))
51720006a0bStron {
518*838f5788Ssimonb toggle_option(curropt, opt_lower, "", optflag);
51920006a0bStron return (MCA_DONE);
52020006a0bStron }
52120006a0bStron /*
52220006a0bStron * Display a prompt appropriate for the option parameter.
52320006a0bStron */
52420006a0bStron start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
52520006a0bStron return (MCA_MORE);
52620006a0bStron }
52720006a0bStron
52820006a0bStron /*
529*838f5788Ssimonb * Normalize search type.
530*838f5788Ssimonb */
norm_search_type(int st)531*838f5788Ssimonb public int norm_search_type(int st)
532*838f5788Ssimonb {
533*838f5788Ssimonb /* WRAP and PAST_EOF are mutually exclusive. */
534*838f5788Ssimonb if ((st & (SRCH_PAST_EOF|SRCH_WRAP)) == (SRCH_PAST_EOF|SRCH_WRAP))
535*838f5788Ssimonb st ^= SRCH_PAST_EOF;
536*838f5788Ssimonb return st;
537*838f5788Ssimonb }
538*838f5788Ssimonb
539*838f5788Ssimonb /*
54020006a0bStron * Handle a char of a search command.
54120006a0bStron */
mca_search_char(int c)542*838f5788Ssimonb static int mca_search_char(int c)
54320006a0bStron {
54420006a0bStron int flag = 0;
54520006a0bStron
54620006a0bStron /*
54720006a0bStron * Certain characters as the first char of
54820006a0bStron * the pattern have special meaning:
54920006a0bStron * ! Toggle the NO_MATCH flag
550824a88bbStron * * Toggle the PAST_EOF flag (less extension)
551824a88bbStron * @ Toggle the FIRST_FILE flag (less extension)
55220006a0bStron */
55320006a0bStron if (len_cmdbuf() > 0)
55420006a0bStron return (NO_MCA);
55520006a0bStron
55620006a0bStron switch (c)
55720006a0bStron {
55820006a0bStron case '*':
559824a88bbStron if (more_mode)
560824a88bbStron break;
561824a88bbStron case CONTROL('E'): /* ignore END of file */
56220006a0bStron if (mca != A_FILTER)
56320006a0bStron flag = SRCH_PAST_EOF;
564*838f5788Ssimonb search_type &= ~SRCH_WRAP;
56520006a0bStron break;
56620006a0bStron case '@':
567824a88bbStron if (more_mode)
568824a88bbStron break;
569824a88bbStron case CONTROL('F'): /* FIRST file */
57020006a0bStron if (mca != A_FILTER)
57120006a0bStron flag = SRCH_FIRST_FILE;
57220006a0bStron break;
57320006a0bStron case CONTROL('K'): /* KEEP position */
57420006a0bStron if (mca != A_FILTER)
57520006a0bStron flag = SRCH_NO_MOVE;
57620006a0bStron break;
577*838f5788Ssimonb case CONTROL('S'): { /* SUBSEARCH */
578*838f5788Ssimonb char buf[INT_STRLEN_BOUND(int)+24];
579*838f5788Ssimonb SNPRINTF1(buf, sizeof(buf), "Sub-pattern (1-%d):", NUM_SEARCH_COLORS);
580*838f5788Ssimonb clear_bot();
581*838f5788Ssimonb cmd_putstr(buf);
582*838f5788Ssimonb flush();
583*838f5788Ssimonb c = getcc();
584*838f5788Ssimonb if (c >= '1' && c <= '0'+NUM_SEARCH_COLORS)
585*838f5788Ssimonb flag = SRCH_SUBSEARCH(c-'0');
586*838f5788Ssimonb else
587*838f5788Ssimonb flag = -1; /* calls mca_search() below to repaint */
588*838f5788Ssimonb break; }
589*838f5788Ssimonb case CONTROL('W'): /* WRAP around */
590*838f5788Ssimonb if (mca != A_FILTER)
591*838f5788Ssimonb flag = SRCH_WRAP;
592*838f5788Ssimonb break;
59320006a0bStron case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
59420006a0bStron flag = SRCH_NO_REGEX;
59520006a0bStron break;
59620006a0bStron case CONTROL('N'): /* NOT match */
59720006a0bStron case '!':
59820006a0bStron flag = SRCH_NO_MATCH;
59920006a0bStron break;
60020006a0bStron }
60120006a0bStron
60220006a0bStron if (flag != 0)
60320006a0bStron {
604*838f5788Ssimonb if (flag != -1)
605*838f5788Ssimonb search_type = norm_search_type(search_type ^ flag);
60620006a0bStron mca_search();
60720006a0bStron return (MCA_MORE);
60820006a0bStron }
60920006a0bStron return (NO_MCA);
61020006a0bStron }
61120006a0bStron
61220006a0bStron /*
61320006a0bStron * Handle a character of a multi-character command.
61420006a0bStron */
mca_char(int c)615*838f5788Ssimonb static int mca_char(int c)
61620006a0bStron {
61720006a0bStron int ret;
61820006a0bStron
61920006a0bStron switch (mca)
62020006a0bStron {
62120006a0bStron case 0:
62220006a0bStron /*
62320006a0bStron * We're not in a multicharacter command.
62420006a0bStron */
62520006a0bStron return (NO_MCA);
62620006a0bStron
62720006a0bStron case A_PREFIX:
62820006a0bStron /*
62920006a0bStron * In the prefix of a command.
63020006a0bStron * This not considered a multichar command
63120006a0bStron * (even tho it uses cmdbuf, etc.).
63220006a0bStron * It is handled in the commands() switch.
63320006a0bStron */
63420006a0bStron return (NO_MCA);
63520006a0bStron
63620006a0bStron case A_DIGIT:
63720006a0bStron /*
63820006a0bStron * Entering digits of a number.
63920006a0bStron * Terminated by a non-digit.
64020006a0bStron */
641*838f5788Ssimonb if ((c >= '0' && c <= '9') || c == '.')
642*838f5788Ssimonb break;
643*838f5788Ssimonb switch (editchar(c, ECF_PEEK|ECF_NOHISTORY|ECF_NOCOMPLETE|ECF_NORIGHTLEFT))
64420006a0bStron {
645*838f5788Ssimonb case A_NOACTION:
646*838f5788Ssimonb /*
647*838f5788Ssimonb * Ignore this char and get another one.
648*838f5788Ssimonb */
649*838f5788Ssimonb return (MCA_MORE);
650*838f5788Ssimonb case A_INVALID:
65120006a0bStron /*
65220006a0bStron * Not part of the number.
65320006a0bStron * End the number and treat this char
65420006a0bStron * as a normal command character.
65520006a0bStron */
65620006a0bStron number = cmd_int(&fraction);
657*838f5788Ssimonb clear_mca();
65820006a0bStron cmd_accept();
65920006a0bStron return (NO_MCA);
66020006a0bStron }
66120006a0bStron break;
66220006a0bStron
66320006a0bStron case A_OPT_TOGGLE:
66420006a0bStron ret = mca_opt_char(c);
66520006a0bStron if (ret != NO_MCA)
66620006a0bStron return (ret);
66720006a0bStron break;
66820006a0bStron
66920006a0bStron case A_F_SEARCH:
67020006a0bStron case A_B_SEARCH:
67120006a0bStron case A_FILTER:
67220006a0bStron ret = mca_search_char(c);
67320006a0bStron if (ret != NO_MCA)
67420006a0bStron return (ret);
67520006a0bStron break;
67620006a0bStron
67720006a0bStron default:
67820006a0bStron /* Other multicharacter command. */
67920006a0bStron break;
68020006a0bStron }
68120006a0bStron
68220006a0bStron /*
68320006a0bStron * The multichar command is terminated by a newline.
68420006a0bStron */
685*838f5788Ssimonb if (is_newline_char(c))
68620006a0bStron {
68720006a0bStron /*
68820006a0bStron * Execute the command.
68920006a0bStron */
69020006a0bStron exec_mca();
69120006a0bStron return (MCA_DONE);
69220006a0bStron }
69320006a0bStron
69420006a0bStron /*
69520006a0bStron * Append the char to the command buffer.
69620006a0bStron */
69720006a0bStron if (cmd_char(c) == CC_QUIT)
69820006a0bStron /*
69920006a0bStron * Abort the multi-char command.
70020006a0bStron */
70120006a0bStron return (MCA_DONE);
70220006a0bStron
703*838f5788Ssimonb switch (mca)
704*838f5788Ssimonb {
705*838f5788Ssimonb case A_F_BRACKET:
706*838f5788Ssimonb case A_B_BRACKET:
707*838f5788Ssimonb if (len_cmdbuf() >= 2)
70820006a0bStron {
70920006a0bStron /*
71020006a0bStron * Special case for the bracket-matching commands.
71120006a0bStron * Execute the command after getting exactly two
71220006a0bStron * characters from the user.
71320006a0bStron */
71420006a0bStron exec_mca();
71520006a0bStron return (MCA_DONE);
71620006a0bStron }
717*838f5788Ssimonb break;
718*838f5788Ssimonb case A_F_SEARCH:
719*838f5788Ssimonb case A_B_SEARCH:
720*838f5788Ssimonb if (incr_search)
721*838f5788Ssimonb {
722*838f5788Ssimonb /* Incremental search: do a search after every input char. */
723*838f5788Ssimonb int st = (search_type & (SRCH_FORW|SRCH_BACK|SRCH_NO_MATCH|SRCH_NO_REGEX|SRCH_NO_MOVE|SRCH_WRAP|SRCH_SUBSEARCH_ALL));
724*838f5788Ssimonb char *pattern = get_cmdbuf();
725*838f5788Ssimonb if (pattern == NULL)
726*838f5788Ssimonb return (MCA_MORE);
727*838f5788Ssimonb /*
728*838f5788Ssimonb * Must save updown_match because mca_search
729*838f5788Ssimonb * reinits it. That breaks history scrolling.
730*838f5788Ssimonb * {{ This is ugly. mca_search probably shouldn't call set_mlist. }}
731*838f5788Ssimonb */
732*838f5788Ssimonb int save_updown_match = updown_match;
733*838f5788Ssimonb cmd_exec();
734*838f5788Ssimonb if (*pattern == '\0')
735*838f5788Ssimonb {
736*838f5788Ssimonb /* User has backspaced to an empty pattern. */
737*838f5788Ssimonb undo_search(1);
738*838f5788Ssimonb } else
739*838f5788Ssimonb {
740*838f5788Ssimonb if (search(st | SRCH_INCR, pattern, 1) != 0)
741*838f5788Ssimonb /* No match, invalid pattern, etc. */
742*838f5788Ssimonb undo_search(1);
743*838f5788Ssimonb }
744*838f5788Ssimonb /* Redraw the search prompt and search string. */
745*838f5788Ssimonb if (!full_screen)
746*838f5788Ssimonb {
747*838f5788Ssimonb clear();
748*838f5788Ssimonb repaint();
749*838f5788Ssimonb }
750*838f5788Ssimonb mca_search1();
751*838f5788Ssimonb updown_match = save_updown_match;
752*838f5788Ssimonb cmd_repaint(NULL);
753*838f5788Ssimonb }
754*838f5788Ssimonb break;
755*838f5788Ssimonb }
75620006a0bStron
75720006a0bStron /*
75820006a0bStron * Need another character.
75920006a0bStron */
76020006a0bStron return (MCA_MORE);
76120006a0bStron }
76220006a0bStron
76320006a0bStron /*
76420006a0bStron * Discard any buffered file data.
76520006a0bStron */
clear_buffers(void)766*838f5788Ssimonb static void clear_buffers(void)
76720006a0bStron {
76820006a0bStron if (!(ch_getflags() & CH_CANSEEK))
76920006a0bStron return;
77020006a0bStron ch_flush();
77120006a0bStron clr_linenum();
77220006a0bStron #if HILITE_SEARCH
77320006a0bStron clr_hilite();
77420006a0bStron #endif
77520006a0bStron }
77620006a0bStron
77720006a0bStron /*
77820006a0bStron * Make sure the screen is displayed.
77920006a0bStron */
make_display(void)780*838f5788Ssimonb static void make_display(void)
78120006a0bStron {
78220006a0bStron /*
783*838f5788Ssimonb * If not full_screen, we can't rely on scrolling to fill the screen.
784*838f5788Ssimonb * We need to clear and repaint screen before any change.
785*838f5788Ssimonb */
786*838f5788Ssimonb if (!full_screen && !(quit_if_one_screen && one_screen))
787*838f5788Ssimonb clear();
788*838f5788Ssimonb /*
78920006a0bStron * If nothing is displayed yet, display starting from initial_scrpos.
79020006a0bStron */
79120006a0bStron if (empty_screen())
79220006a0bStron {
79320006a0bStron if (initial_scrpos.pos == NULL_POSITION)
79420006a0bStron jump_loc(ch_zero(), 1);
79520006a0bStron else
79620006a0bStron jump_loc(initial_scrpos.pos, initial_scrpos.ln);
797*838f5788Ssimonb } else if (screen_trashed || !full_screen)
79820006a0bStron {
79920006a0bStron int save_top_scroll = top_scroll;
80020006a0bStron int save_ignore_eoi = ignore_eoi;
80120006a0bStron top_scroll = 1;
80220006a0bStron ignore_eoi = 0;
80320006a0bStron if (screen_trashed == 2)
80420006a0bStron {
80520006a0bStron /* Special case used by ignore_eoi: re-open the input file
80620006a0bStron * and jump to the end of the file. */
80720006a0bStron reopen_curr_ifile();
80820006a0bStron jump_forw();
80920006a0bStron }
81020006a0bStron repaint();
81120006a0bStron top_scroll = save_top_scroll;
81220006a0bStron ignore_eoi = save_ignore_eoi;
81320006a0bStron }
81420006a0bStron }
81520006a0bStron
81620006a0bStron /*
81720006a0bStron * Display the appropriate prompt.
81820006a0bStron */
prompt(void)819*838f5788Ssimonb static void prompt(void)
82020006a0bStron {
821*838f5788Ssimonb constant char *p;
82220006a0bStron
823*838f5788Ssimonb if (ungot != NULL && ungot->ug_char != CHAR_END_COMMAND)
82420006a0bStron {
82520006a0bStron /*
82620006a0bStron * No prompt necessary if commands are from
82720006a0bStron * ungotten chars rather than from the user.
82820006a0bStron */
82920006a0bStron return;
83020006a0bStron }
83120006a0bStron
83220006a0bStron /*
83320006a0bStron * Make sure the screen is displayed.
83420006a0bStron */
83520006a0bStron make_display();
83620006a0bStron bottompos = position(BOTTOM_PLUS_ONE);
83720006a0bStron
83820006a0bStron /*
83920006a0bStron * If we've hit EOF on the last file and the -E flag is set, quit.
84020006a0bStron */
84120006a0bStron if (get_quit_at_eof() == OPT_ONPLUS &&
84220006a0bStron eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
84320006a0bStron next_ifile(curr_ifile) == NULL_IFILE)
84420006a0bStron quit(QUIT_OK);
84520006a0bStron
84620006a0bStron /*
84720006a0bStron * If the entire file is displayed and the -F flag is set, quit.
84820006a0bStron */
84920006a0bStron if (quit_if_one_screen &&
85020006a0bStron entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
85120006a0bStron next_ifile(curr_ifile) == NULL_IFILE)
85220006a0bStron quit(QUIT_OK);
853*838f5788Ssimonb quit_if_one_screen = FALSE; /* only get one chance at this */
85420006a0bStron
85520006a0bStron #if MSDOS_COMPILER==WIN32C
85620006a0bStron /*
85720006a0bStron * In Win32, display the file name in the window title.
85820006a0bStron */
85920006a0bStron if (!(ch_getflags() & CH_HELPFILE))
860*838f5788Ssimonb {
861*838f5788Ssimonb WCHAR w[MAX_PATH+16];
862*838f5788Ssimonb p = pr_expand("Less?f - %f.");
863*838f5788Ssimonb MultiByteToWideChar(CP_ACP, 0, p, -1, w, sizeof(w)/sizeof(*w));
864*838f5788Ssimonb SetConsoleTitleW(w);
865*838f5788Ssimonb }
86620006a0bStron #endif
867*838f5788Ssimonb
86820006a0bStron /*
86920006a0bStron * Select the proper prompt and display it.
87020006a0bStron */
87120006a0bStron /*
87220006a0bStron * If the previous action was a forward movement,
87320006a0bStron * don't clear the bottom line of the display;
87420006a0bStron * just print the prompt since the forward movement guarantees
87520006a0bStron * that we're in the right position to display the prompt.
87620006a0bStron * Clearing the line could cause a problem: for example, if the last
87720006a0bStron * line displayed ended at the right screen edge without a newline,
87820006a0bStron * then clearing would clear the last displayed line rather than
87920006a0bStron * the prompt line.
88020006a0bStron */
88120006a0bStron if (!forw_prompt)
88220006a0bStron clear_bot();
88320006a0bStron clear_cmd();
88420006a0bStron forw_prompt = 0;
885*838f5788Ssimonb if (helpprompt)
886*838f5788Ssimonb {
887824a88bbStron at_enter(AT_STANDOUT);
888824a88bbStron putstr("[Press 'h' for instructions.]");
889824a88bbStron at_exit();
890824a88bbStron helpprompt = 0;
891*838f5788Ssimonb } else
892*838f5788Ssimonb {
89320006a0bStron p = pr_string();
894*838f5788Ssimonb #if HILITE_SEARCH
89520006a0bStron if (is_filtering())
89620006a0bStron putstr("& ");
897*838f5788Ssimonb #endif
89820006a0bStron if (p == NULL || *p == '\0')
89920006a0bStron {
900*838f5788Ssimonb at_enter(AT_NORMAL|AT_COLOR_PROMPT);
901*838f5788Ssimonb putchr(':');
90220006a0bStron at_exit();
903*838f5788Ssimonb } else
904*838f5788Ssimonb {
905*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
906*838f5788Ssimonb WCHAR w[MAX_PATH*2];
907*838f5788Ssimonb char a[MAX_PATH*2];
908*838f5788Ssimonb MultiByteToWideChar(CP_ACP, 0, p, -1, w, sizeof(w)/sizeof(*w));
909*838f5788Ssimonb WideCharToMultiByte(utf_mode ? CP_UTF8 : GetConsoleOutputCP(),
910*838f5788Ssimonb 0, w, -1, a, sizeof(a), NULL, NULL);
911*838f5788Ssimonb p = a;
912*838f5788Ssimonb #endif
913*838f5788Ssimonb load_line(p);
914*838f5788Ssimonb put_line();
91520006a0bStron }
916824a88bbStron }
91720006a0bStron clear_eol();
91820006a0bStron }
91920006a0bStron
92020006a0bStron /*
92120006a0bStron * Display the less version message.
92220006a0bStron */
dispversion(void)923*838f5788Ssimonb public void dispversion(void)
92420006a0bStron {
92520006a0bStron PARG parg;
92620006a0bStron
92720006a0bStron parg.p_string = version;
92820006a0bStron error("less %s", &parg);
92920006a0bStron }
93020006a0bStron
93120006a0bStron /*
932*838f5788Ssimonb * Return a character to complete a partial command, if possible.
933*838f5788Ssimonb */
getcc_end_command(void)934*838f5788Ssimonb static LWCHAR getcc_end_command(void)
935*838f5788Ssimonb {
936*838f5788Ssimonb switch (mca)
937*838f5788Ssimonb {
938*838f5788Ssimonb case A_DIGIT:
939*838f5788Ssimonb /* We have a number but no command. Treat as #g. */
940*838f5788Ssimonb return ('g');
941*838f5788Ssimonb case A_F_SEARCH:
942*838f5788Ssimonb case A_B_SEARCH:
943*838f5788Ssimonb case A_FILTER:
944*838f5788Ssimonb /* We have "/string" but no newline. Add the \n. */
945*838f5788Ssimonb return ('\n');
946*838f5788Ssimonb default:
947*838f5788Ssimonb /* Some other incomplete command. Let user complete it. */
948*838f5788Ssimonb return ((ungot == NULL) ? getchr() : 0);
949*838f5788Ssimonb }
950*838f5788Ssimonb }
951*838f5788Ssimonb
952*838f5788Ssimonb /*
95320006a0bStron * Get command character.
95420006a0bStron * The character normally comes from the keyboard,
95520006a0bStron * but may come from ungotten characters
95620006a0bStron * (characters previously given to ungetcc or ungetsc).
95720006a0bStron */
getccu(void)958*838f5788Ssimonb static LWCHAR getccu(void)
95920006a0bStron {
960*838f5788Ssimonb LWCHAR c = 0;
961*838f5788Ssimonb while (c == 0)
96220006a0bStron {
96320006a0bStron if (ungot == NULL)
96420006a0bStron {
965*838f5788Ssimonb /* Normal case: no ungotten chars.
966*838f5788Ssimonb * Get char from the user. */
967*838f5788Ssimonb c = getchr();
968*838f5788Ssimonb } else
969*838f5788Ssimonb {
970*838f5788Ssimonb /* Ungotten chars available:
971*838f5788Ssimonb * Take the top of stack (most recent). */
972*838f5788Ssimonb struct ungot *ug = ungot;
973*838f5788Ssimonb c = ug->ug_char;
974*838f5788Ssimonb ungot = ug->ug_next;
975*838f5788Ssimonb free(ug);
976*838f5788Ssimonb
977*838f5788Ssimonb if (c == CHAR_END_COMMAND)
978*838f5788Ssimonb c = getcc_end_command();
979*838f5788Ssimonb }
980*838f5788Ssimonb }
981*838f5788Ssimonb return (c);
98220006a0bStron }
98320006a0bStron
98420006a0bStron /*
985*838f5788Ssimonb * Get a command character, but if we receive the orig sequence,
986*838f5788Ssimonb * convert it to the repl sequence.
98720006a0bStron */
getcc_repl(char constant * orig,char constant * repl,LWCHAR (* gr_getc)(void),void (* gr_ungetc)(LWCHAR))988*838f5788Ssimonb static LWCHAR getcc_repl(char constant *orig, char constant *repl, LWCHAR (*gr_getc)(void), void (*gr_ungetc)(LWCHAR))
98920006a0bStron {
990*838f5788Ssimonb LWCHAR c;
991*838f5788Ssimonb LWCHAR keys[16];
992*838f5788Ssimonb int ki = 0;
993*838f5788Ssimonb
994*838f5788Ssimonb c = (*gr_getc)();
995*838f5788Ssimonb if (orig == NULL || orig[0] == '\0')
996*838f5788Ssimonb return c;
997*838f5788Ssimonb for (;;)
998*838f5788Ssimonb {
999*838f5788Ssimonb keys[ki] = c;
1000*838f5788Ssimonb if (c != orig[ki] || ki >= sizeof(keys)-1)
1001*838f5788Ssimonb {
1002*838f5788Ssimonb /* This is not orig we have been receiving.
1003*838f5788Ssimonb * If we have stashed chars in keys[],
1004*838f5788Ssimonb * unget them and return the first one. */
1005*838f5788Ssimonb while (ki > 0)
1006*838f5788Ssimonb (*gr_ungetc)(keys[ki--]);
1007*838f5788Ssimonb return keys[0];
100820006a0bStron }
1009*838f5788Ssimonb if (orig[++ki] == '\0')
1010*838f5788Ssimonb {
1011*838f5788Ssimonb /* We've received the full orig sequence.
1012*838f5788Ssimonb * Return the repl sequence. */
1013*838f5788Ssimonb ki = strlen(repl)-1;
1014*838f5788Ssimonb while (ki > 0)
1015*838f5788Ssimonb (*gr_ungetc)(repl[ki--]);
1016*838f5788Ssimonb return repl[0];
1017*838f5788Ssimonb }
1018*838f5788Ssimonb /* We've received a partial orig sequence (ki chars of it).
1019*838f5788Ssimonb * Get next char and see if it continues to match orig. */
1020*838f5788Ssimonb c = (*gr_getc)();
1021*838f5788Ssimonb }
1022*838f5788Ssimonb }
1023*838f5788Ssimonb
1024*838f5788Ssimonb /*
1025*838f5788Ssimonb * Get command character.
1026*838f5788Ssimonb */
getcc(void)1027*838f5788Ssimonb public int getcc(void)
1028*838f5788Ssimonb {
1029*838f5788Ssimonb /* Replace kent (keypad Enter) with a newline. */
1030*838f5788Ssimonb return getcc_repl(kent, "\n", getccu, ungetcc);
103120006a0bStron }
103220006a0bStron
103320006a0bStron /*
103420006a0bStron * "Unget" a command character.
103520006a0bStron * The next getcc() will return this character.
103620006a0bStron */
ungetcc(LWCHAR c)1037*838f5788Ssimonb public void ungetcc(LWCHAR c)
103820006a0bStron {
103920006a0bStron struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
104020006a0bStron
104120006a0bStron ug->ug_char = c;
104220006a0bStron ug->ug_next = ungot;
104320006a0bStron ungot = ug;
1044*838f5788Ssimonb }
1045*838f5788Ssimonb
1046*838f5788Ssimonb /*
1047*838f5788Ssimonb * "Unget" a command character.
1048*838f5788Ssimonb * If any other chars are already ungotten, put this one after those.
1049*838f5788Ssimonb */
ungetcc_back(LWCHAR c)1050*838f5788Ssimonb public void ungetcc_back(LWCHAR c)
1051*838f5788Ssimonb {
1052*838f5788Ssimonb struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
1053*838f5788Ssimonb ug->ug_char = c;
1054*838f5788Ssimonb ug->ug_next = NULL;
1055*838f5788Ssimonb if (ungot == NULL)
1056*838f5788Ssimonb ungot = ug;
1057*838f5788Ssimonb else
1058*838f5788Ssimonb {
1059*838f5788Ssimonb struct ungot *pu;
1060*838f5788Ssimonb for (pu = ungot; pu->ug_next != NULL; pu = pu->ug_next)
1061*838f5788Ssimonb continue;
1062*838f5788Ssimonb pu->ug_next = ug;
1063*838f5788Ssimonb }
106420006a0bStron }
106520006a0bStron
106620006a0bStron /*
106720006a0bStron * Unget a whole string of command characters.
106820006a0bStron * The next sequence of getcc()'s will return this string.
106920006a0bStron */
ungetsc(char * s)1070*838f5788Ssimonb public void ungetsc(char *s)
107120006a0bStron {
1072*838f5788Ssimonb while (*s != '\0')
1073*838f5788Ssimonb ungetcc_back(*s++);
1074*838f5788Ssimonb }
107520006a0bStron
1076*838f5788Ssimonb /*
1077*838f5788Ssimonb * Peek the next command character, without consuming it.
1078*838f5788Ssimonb */
peekcc(void)1079*838f5788Ssimonb public LWCHAR peekcc(void)
1080*838f5788Ssimonb {
1081*838f5788Ssimonb LWCHAR c = getcc();
1082*838f5788Ssimonb ungetcc(c);
1083*838f5788Ssimonb return c;
108420006a0bStron }
108520006a0bStron
108620006a0bStron /*
108720006a0bStron * Search for a pattern, possibly in multiple files.
108820006a0bStron * If SRCH_FIRST_FILE is set, begin searching at the first file.
108920006a0bStron * If SRCH_PAST_EOF is set, continue the search thru multiple files.
109020006a0bStron */
multi_search(char * pattern,int n,int silent)1091*838f5788Ssimonb static void multi_search(char *pattern, int n, int silent)
109220006a0bStron {
1093*838f5788Ssimonb int nomore;
109420006a0bStron IFILE save_ifile;
109520006a0bStron int changed_file;
109620006a0bStron
109720006a0bStron changed_file = 0;
109820006a0bStron save_ifile = save_curr_ifile();
109920006a0bStron
1100*838f5788Ssimonb if ((search_type & (SRCH_FORW|SRCH_BACK)) == 0)
1101*838f5788Ssimonb search_type |= SRCH_FORW;
110220006a0bStron if (search_type & SRCH_FIRST_FILE)
110320006a0bStron {
110420006a0bStron /*
110520006a0bStron * Start at the first (or last) file
110620006a0bStron * in the command line list.
110720006a0bStron */
110820006a0bStron if (search_type & SRCH_FORW)
110920006a0bStron nomore = edit_first();
111020006a0bStron else
111120006a0bStron nomore = edit_last();
111220006a0bStron if (nomore)
111320006a0bStron {
111420006a0bStron unsave_ifile(save_ifile);
111520006a0bStron return;
111620006a0bStron }
111720006a0bStron changed_file = 1;
111820006a0bStron search_type &= ~SRCH_FIRST_FILE;
111920006a0bStron }
112020006a0bStron
112120006a0bStron for (;;)
112220006a0bStron {
112320006a0bStron n = search(search_type, pattern, n);
112420006a0bStron /*
112520006a0bStron * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
112620006a0bStron * after being used once. This allows "n" to work after
112720006a0bStron * using a /@@ search.
112820006a0bStron */
112920006a0bStron search_type &= ~SRCH_NO_MOVE;
1130*838f5788Ssimonb last_search_type = search_type;
113120006a0bStron if (n == 0)
113220006a0bStron {
113320006a0bStron /*
113420006a0bStron * Found it.
113520006a0bStron */
113620006a0bStron unsave_ifile(save_ifile);
113720006a0bStron return;
113820006a0bStron }
113920006a0bStron
114020006a0bStron if (n < 0)
114120006a0bStron /*
114220006a0bStron * Some kind of error in the search.
114320006a0bStron * Error message has been printed by search().
114420006a0bStron */
114520006a0bStron break;
114620006a0bStron
114720006a0bStron if ((search_type & SRCH_PAST_EOF) == 0)
114820006a0bStron /*
114920006a0bStron * We didn't find a match, but we're
115020006a0bStron * supposed to search only one file.
115120006a0bStron */
115220006a0bStron break;
115320006a0bStron /*
115420006a0bStron * Move on to the next file.
115520006a0bStron */
115620006a0bStron if (search_type & SRCH_FORW)
115720006a0bStron nomore = edit_next(1);
115820006a0bStron else
115920006a0bStron nomore = edit_prev(1);
116020006a0bStron if (nomore)
116120006a0bStron break;
116220006a0bStron changed_file = 1;
116320006a0bStron }
116420006a0bStron
116520006a0bStron /*
116620006a0bStron * Didn't find it.
116720006a0bStron * Print an error message if we haven't already.
116820006a0bStron */
1169*838f5788Ssimonb if (n > 0 && !silent)
117020006a0bStron error("Pattern not found", NULL_PARG);
117120006a0bStron
117220006a0bStron if (changed_file)
117320006a0bStron {
117420006a0bStron /*
117520006a0bStron * Restore the file we were originally viewing.
117620006a0bStron */
117720006a0bStron reedit_ifile(save_ifile);
117820006a0bStron } else
117920006a0bStron {
118020006a0bStron unsave_ifile(save_ifile);
118120006a0bStron }
118220006a0bStron }
118320006a0bStron
118420006a0bStron /*
1185ec18bca0Stron * Forward forever, or until a highlighted line appears.
1186ec18bca0Stron */
forw_loop(int until_hilite)1187*838f5788Ssimonb static int forw_loop(int until_hilite)
1188ec18bca0Stron {
1189ec18bca0Stron POSITION curr_len;
1190ec18bca0Stron
1191ec18bca0Stron if (ch_getflags() & CH_HELPFILE)
1192ec18bca0Stron return (A_NOACTION);
1193ec18bca0Stron
1194ec18bca0Stron cmd_exec();
1195*838f5788Ssimonb jump_forw_buffered();
1196ec18bca0Stron curr_len = ch_length();
1197ec18bca0Stron highest_hilite = until_hilite ? curr_len : NULL_POSITION;
1198ec18bca0Stron ignore_eoi = 1;
1199ec18bca0Stron while (!sigs)
1200ec18bca0Stron {
1201ec18bca0Stron if (until_hilite && highest_hilite > curr_len)
1202ec18bca0Stron {
1203ec18bca0Stron bell();
1204ec18bca0Stron break;
1205ec18bca0Stron }
1206ec18bca0Stron make_display();
1207ec18bca0Stron forward(1, 0, 0);
1208ec18bca0Stron }
1209ec18bca0Stron ignore_eoi = 0;
1210ec18bca0Stron ch_set_eof();
1211ec18bca0Stron
1212ec18bca0Stron /*
1213ec18bca0Stron * This gets us back in "F mode" after processing
1214ec18bca0Stron * a non-abort signal (e.g. window-change).
1215ec18bca0Stron */
1216ec18bca0Stron if (sigs && !ABORT_SIGS())
1217ec18bca0Stron return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
1218ec18bca0Stron
1219ec18bca0Stron return (A_NOACTION);
1220ec18bca0Stron }
1221ec18bca0Stron
1222ec18bca0Stron /*
122320006a0bStron * Main command processor.
122420006a0bStron * Accept and execute commands until a quit command.
122520006a0bStron */
commands(void)1226*838f5788Ssimonb public void commands(void)
122720006a0bStron {
1228*838f5788Ssimonb int c;
1229*838f5788Ssimonb int action;
1230*838f5788Ssimonb char *cbuf;
123120006a0bStron int newaction;
1232*838f5788Ssimonb int save_jump_sline;
123320006a0bStron int save_search_type;
123420006a0bStron char *extra;
123520006a0bStron char tbuf[2];
123620006a0bStron PARG parg;
123720006a0bStron IFILE old_ifile;
123820006a0bStron IFILE new_ifile;
123920006a0bStron char *tagfile;
124020006a0bStron
124120006a0bStron search_type = SRCH_FORW;
124220006a0bStron wscroll = (sc_height + 1) / 2;
124320006a0bStron newaction = A_NOACTION;
124420006a0bStron
124520006a0bStron for (;;)
124620006a0bStron {
1247*838f5788Ssimonb clear_mca();
124820006a0bStron cmd_accept();
124920006a0bStron number = 0;
125020006a0bStron curropt = NULL;
125120006a0bStron
125220006a0bStron /*
125320006a0bStron * See if any signals need processing.
125420006a0bStron */
125520006a0bStron if (sigs)
125620006a0bStron {
125720006a0bStron psignals();
125820006a0bStron if (quitting)
125920006a0bStron quit(QUIT_SAVED_STATUS);
126020006a0bStron }
126120006a0bStron
126220006a0bStron /*
126320006a0bStron * See if window size changed, for systems that don't
126420006a0bStron * generate SIGWINCH.
126520006a0bStron */
126620006a0bStron check_winch();
126720006a0bStron
126820006a0bStron /*
126920006a0bStron * Display prompt and accept a character.
127020006a0bStron */
127120006a0bStron cmd_reset();
127220006a0bStron prompt();
127320006a0bStron if (sigs)
127420006a0bStron continue;
127520006a0bStron if (newaction == A_NOACTION)
127620006a0bStron c = getcc();
127720006a0bStron
127820006a0bStron again:
127920006a0bStron if (sigs)
128020006a0bStron continue;
128120006a0bStron
128220006a0bStron if (newaction != A_NOACTION)
128320006a0bStron {
128420006a0bStron action = newaction;
128520006a0bStron newaction = A_NOACTION;
128620006a0bStron } else
128720006a0bStron {
128820006a0bStron /*
128920006a0bStron * If we are in a multicharacter command, call mca_char.
129020006a0bStron * Otherwise we call fcmd_decode to determine the
129120006a0bStron * action to be performed.
129220006a0bStron */
129320006a0bStron if (mca)
129420006a0bStron switch (mca_char(c))
129520006a0bStron {
129620006a0bStron case MCA_MORE:
129720006a0bStron /*
129820006a0bStron * Need another character.
129920006a0bStron */
130020006a0bStron c = getcc();
130120006a0bStron goto again;
130220006a0bStron case MCA_DONE:
130320006a0bStron /*
130420006a0bStron * Command has been handled by mca_char.
130520006a0bStron * Start clean with a prompt.
130620006a0bStron */
130720006a0bStron continue;
130820006a0bStron case NO_MCA:
130920006a0bStron /*
131020006a0bStron * Not a multi-char command
131120006a0bStron * (at least, not anymore).
131220006a0bStron */
131320006a0bStron break;
131420006a0bStron }
131520006a0bStron
131620006a0bStron /*
131720006a0bStron * Decode the command character and decide what to do.
131820006a0bStron */
131920006a0bStron if (mca)
132020006a0bStron {
132120006a0bStron /*
132220006a0bStron * We're in a multichar command.
132320006a0bStron * Add the character to the command buffer
132420006a0bStron * and display it on the screen.
132520006a0bStron * If the user backspaces past the start
132620006a0bStron * of the line, abort the command.
132720006a0bStron */
132820006a0bStron if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
132920006a0bStron continue;
133020006a0bStron cbuf = get_cmdbuf();
1331*838f5788Ssimonb if (cbuf == NULL)
1332*838f5788Ssimonb continue;
133320006a0bStron } else
133420006a0bStron {
133520006a0bStron /*
133620006a0bStron * Don't use cmd_char if we're starting fresh
133720006a0bStron * at the beginning of a command, because we
133820006a0bStron * don't want to echo the command until we know
133920006a0bStron * it is a multichar command. We also don't
134020006a0bStron * want erase_char/kill_char to be treated
134120006a0bStron * as line editing characters.
134220006a0bStron */
134320006a0bStron tbuf[0] = c;
134420006a0bStron tbuf[1] = '\0';
134520006a0bStron cbuf = tbuf;
134620006a0bStron }
134720006a0bStron extra = NULL;
134820006a0bStron action = fcmd_decode(cbuf, &extra);
134920006a0bStron /*
135020006a0bStron * If an "extra" string was returned,
135120006a0bStron * process it as a string of command characters.
135220006a0bStron */
135320006a0bStron if (extra != NULL)
135420006a0bStron ungetsc(extra);
135520006a0bStron }
135620006a0bStron /*
135720006a0bStron * Clear the cmdbuf string.
135820006a0bStron * (But not if we're in the prefix of a command,
135920006a0bStron * because the partial command string is kept there.)
136020006a0bStron */
136120006a0bStron if (action != A_PREFIX)
136220006a0bStron cmd_reset();
136320006a0bStron
136420006a0bStron switch (action)
136520006a0bStron {
136620006a0bStron case A_DIGIT:
136720006a0bStron /*
136820006a0bStron * First digit of a number.
136920006a0bStron */
137020006a0bStron start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
137120006a0bStron goto again;
137220006a0bStron
137320006a0bStron case A_F_WINDOW:
137420006a0bStron /*
137520006a0bStron * Forward one window (and set the window size).
137620006a0bStron */
137720006a0bStron if (number > 0)
137820006a0bStron swindow = (int) number;
137920006a0bStron /* FALLTHRU */
138020006a0bStron case A_F_SCREEN:
138120006a0bStron /*
138220006a0bStron * Forward one screen.
138320006a0bStron */
138420006a0bStron if (number <= 0)
138520006a0bStron number = get_swindow();
138620006a0bStron cmd_exec();
138720006a0bStron if (show_attn)
138820006a0bStron set_attnpos(bottompos);
138920006a0bStron forward((int) number, 0, 1);
139020006a0bStron break;
139120006a0bStron
139220006a0bStron case A_B_WINDOW:
139320006a0bStron /*
139420006a0bStron * Backward one window (and set the window size).
139520006a0bStron */
139620006a0bStron if (number > 0)
139720006a0bStron swindow = (int) number;
139820006a0bStron /* FALLTHRU */
139920006a0bStron case A_B_SCREEN:
140020006a0bStron /*
140120006a0bStron * Backward one screen.
140220006a0bStron */
140320006a0bStron if (number <= 0)
140420006a0bStron number = get_swindow();
140520006a0bStron cmd_exec();
140620006a0bStron backward((int) number, 0, 1);
140720006a0bStron break;
140820006a0bStron
140920006a0bStron case A_F_LINE:
141020006a0bStron /*
141120006a0bStron * Forward N (default 1) line.
141220006a0bStron */
141320006a0bStron if (number <= 0)
141420006a0bStron number = 1;
141520006a0bStron cmd_exec();
141620006a0bStron if (show_attn == OPT_ONPLUS && number > 1)
141720006a0bStron set_attnpos(bottompos);
141820006a0bStron forward((int) number, 0, 0);
141920006a0bStron break;
142020006a0bStron
142120006a0bStron case A_B_LINE:
142220006a0bStron /*
142320006a0bStron * Backward N (default 1) line.
142420006a0bStron */
142520006a0bStron if (number <= 0)
142620006a0bStron number = 1;
142720006a0bStron cmd_exec();
142820006a0bStron backward((int) number, 0, 0);
142920006a0bStron break;
143020006a0bStron
1431*838f5788Ssimonb case A_F_MOUSE:
1432*838f5788Ssimonb /*
1433*838f5788Ssimonb * Forward wheel_lines lines.
1434*838f5788Ssimonb */
1435*838f5788Ssimonb cmd_exec();
1436*838f5788Ssimonb forward(wheel_lines, 0, 0);
1437*838f5788Ssimonb break;
1438*838f5788Ssimonb
1439*838f5788Ssimonb case A_B_MOUSE:
1440*838f5788Ssimonb /*
1441*838f5788Ssimonb * Backward wheel_lines lines.
1442*838f5788Ssimonb */
1443*838f5788Ssimonb cmd_exec();
1444*838f5788Ssimonb backward(wheel_lines, 0, 0);
1445*838f5788Ssimonb break;
1446*838f5788Ssimonb
144720006a0bStron case A_FF_LINE:
144820006a0bStron /*
144920006a0bStron * Force forward N (default 1) line.
145020006a0bStron */
145120006a0bStron if (number <= 0)
145220006a0bStron number = 1;
145320006a0bStron cmd_exec();
145420006a0bStron if (show_attn == OPT_ONPLUS && number > 1)
145520006a0bStron set_attnpos(bottompos);
145620006a0bStron forward((int) number, 1, 0);
145720006a0bStron break;
145820006a0bStron
145920006a0bStron case A_BF_LINE:
146020006a0bStron /*
146120006a0bStron * Force backward N (default 1) line.
146220006a0bStron */
146320006a0bStron if (number <= 0)
146420006a0bStron number = 1;
146520006a0bStron cmd_exec();
146620006a0bStron backward((int) number, 1, 0);
146720006a0bStron break;
146820006a0bStron
146920006a0bStron case A_FF_SCREEN:
147020006a0bStron /*
147120006a0bStron * Force forward one screen.
147220006a0bStron */
147320006a0bStron if (number <= 0)
147420006a0bStron number = get_swindow();
147520006a0bStron cmd_exec();
147620006a0bStron if (show_attn == OPT_ONPLUS)
147720006a0bStron set_attnpos(bottompos);
147820006a0bStron forward((int) number, 1, 0);
147920006a0bStron break;
148020006a0bStron
148120006a0bStron case A_F_FOREVER:
148220006a0bStron /*
148320006a0bStron * Forward forever, ignoring EOF.
148420006a0bStron */
1485*838f5788Ssimonb if (show_attn)
1486*838f5788Ssimonb set_attnpos(bottompos);
1487ec18bca0Stron newaction = forw_loop(0);
148820006a0bStron break;
1489ec18bca0Stron
1490ec18bca0Stron case A_F_UNTIL_HILITE:
1491ec18bca0Stron newaction = forw_loop(1);
149220006a0bStron break;
149320006a0bStron
149420006a0bStron case A_F_SCROLL:
149520006a0bStron /*
149620006a0bStron * Forward N lines
149720006a0bStron * (default same as last 'd' or 'u' command).
149820006a0bStron */
149920006a0bStron if (number > 0)
150020006a0bStron wscroll = (int) number;
150120006a0bStron cmd_exec();
150220006a0bStron if (show_attn == OPT_ONPLUS)
150320006a0bStron set_attnpos(bottompos);
150420006a0bStron forward(wscroll, 0, 0);
150520006a0bStron break;
150620006a0bStron
150720006a0bStron case A_B_SCROLL:
150820006a0bStron /*
150920006a0bStron * Forward N lines
151020006a0bStron * (default same as last 'd' or 'u' command).
151120006a0bStron */
151220006a0bStron if (number > 0)
151320006a0bStron wscroll = (int) number;
151420006a0bStron cmd_exec();
151520006a0bStron backward(wscroll, 0, 0);
151620006a0bStron break;
151720006a0bStron
151820006a0bStron case A_FREPAINT:
151920006a0bStron /*
152020006a0bStron * Flush buffers, then repaint screen.
152120006a0bStron * Don't flush the buffers on a pipe!
152220006a0bStron */
152320006a0bStron clear_buffers();
152420006a0bStron /* FALLTHRU */
152520006a0bStron case A_REPAINT:
152620006a0bStron /*
152720006a0bStron * Repaint screen.
152820006a0bStron */
152920006a0bStron cmd_exec();
153020006a0bStron repaint();
153120006a0bStron break;
153220006a0bStron
153320006a0bStron case A_GOLINE:
153420006a0bStron /*
153520006a0bStron * Go to line N, default beginning of file.
1536*838f5788Ssimonb * If N <= 0, ignore jump_sline in order to avoid
1537*838f5788Ssimonb * empty lines before the beginning of the file.
153820006a0bStron */
1539*838f5788Ssimonb save_jump_sline = jump_sline;
154020006a0bStron if (number <= 0)
1541*838f5788Ssimonb {
154220006a0bStron number = 1;
1543*838f5788Ssimonb jump_sline = 0;
1544*838f5788Ssimonb }
154520006a0bStron cmd_exec();
154620006a0bStron jump_back(number);
1547*838f5788Ssimonb jump_sline = save_jump_sline;
154820006a0bStron break;
154920006a0bStron
155020006a0bStron case A_PERCENT:
155120006a0bStron /*
155220006a0bStron * Go to a specified percentage into the file.
155320006a0bStron */
155420006a0bStron if (number < 0)
155520006a0bStron {
155620006a0bStron number = 0;
155720006a0bStron fraction = 0;
155820006a0bStron }
1559*838f5788Ssimonb if (number > 100 || (number == 100 && fraction != 0))
156020006a0bStron {
156120006a0bStron number = 100;
156220006a0bStron fraction = 0;
156320006a0bStron }
156420006a0bStron cmd_exec();
156520006a0bStron jump_percent((int) number, fraction);
156620006a0bStron break;
156720006a0bStron
156820006a0bStron case A_GOEND:
156920006a0bStron /*
157020006a0bStron * Go to line N, default end of file.
157120006a0bStron */
157220006a0bStron cmd_exec();
157320006a0bStron if (number <= 0)
157420006a0bStron jump_forw();
157520006a0bStron else
157620006a0bStron jump_back(number);
157720006a0bStron break;
157820006a0bStron
1579*838f5788Ssimonb case A_GOEND_BUF:
1580*838f5788Ssimonb /*
1581*838f5788Ssimonb * Go to line N, default last buffered byte.
1582*838f5788Ssimonb */
1583*838f5788Ssimonb cmd_exec();
1584*838f5788Ssimonb if (number <= 0)
1585*838f5788Ssimonb jump_forw_buffered();
1586*838f5788Ssimonb else
1587*838f5788Ssimonb jump_back(number);
1588*838f5788Ssimonb break;
1589*838f5788Ssimonb
159020006a0bStron case A_GOPOS:
159120006a0bStron /*
159220006a0bStron * Go to a specified byte position in the file.
159320006a0bStron */
159420006a0bStron cmd_exec();
159520006a0bStron if (number < 0)
159620006a0bStron number = 0;
159720006a0bStron jump_line_loc((POSITION) number, jump_sline);
159820006a0bStron break;
159920006a0bStron
160020006a0bStron case A_STAT:
160120006a0bStron /*
160220006a0bStron * Print file name, etc.
160320006a0bStron */
160420006a0bStron if (ch_getflags() & CH_HELPFILE)
160520006a0bStron break;
160620006a0bStron cmd_exec();
160720006a0bStron parg.p_string = eq_message();
160820006a0bStron error("%s", &parg);
160920006a0bStron break;
161020006a0bStron
161120006a0bStron case A_VERSION:
161220006a0bStron /*
1613*838f5788Ssimonb * Print version number.
161420006a0bStron */
161520006a0bStron cmd_exec();
161620006a0bStron dispversion();
161720006a0bStron break;
161820006a0bStron
161920006a0bStron case A_QUIT:
162020006a0bStron /*
162120006a0bStron * Exit.
162220006a0bStron */
162320006a0bStron if (curr_ifile != NULL_IFILE &&
162420006a0bStron ch_getflags() & CH_HELPFILE)
162520006a0bStron {
162620006a0bStron /*
162720006a0bStron * Quit while viewing the help file
162820006a0bStron * just means return to viewing the
162920006a0bStron * previous file.
163020006a0bStron */
163120006a0bStron hshift = save_hshift;
1632*838f5788Ssimonb bs_mode = save_bs_mode;
1633*838f5788Ssimonb proc_backspace = save_proc_backspace;
163420006a0bStron if (edit_prev(1) == 0)
163520006a0bStron break;
163620006a0bStron }
163720006a0bStron if (extra != NULL)
163820006a0bStron quit(*extra);
163920006a0bStron quit(QUIT_OK);
164020006a0bStron break;
164120006a0bStron
164220006a0bStron /*
164320006a0bStron * Define abbreviation for a commonly used sequence below.
164420006a0bStron */
164520006a0bStron #define DO_SEARCH() \
164620006a0bStron if (number <= 0) number = 1; \
164720006a0bStron mca_search(); \
164820006a0bStron cmd_exec(); \
1649*838f5788Ssimonb multi_search((char *)NULL, (int) number, 0);
165020006a0bStron
165120006a0bStron case A_F_SEARCH:
165220006a0bStron /*
165320006a0bStron * Search forward for a pattern.
165420006a0bStron * Get the first char of the pattern.
165520006a0bStron */
1656*838f5788Ssimonb search_type = SRCH_FORW | def_search_type;
165720006a0bStron if (number <= 0)
165820006a0bStron number = 1;
165920006a0bStron mca_search();
166020006a0bStron c = getcc();
166120006a0bStron goto again;
166220006a0bStron
166320006a0bStron case A_B_SEARCH:
166420006a0bStron /*
166520006a0bStron * Search backward for a pattern.
166620006a0bStron * Get the first char of the pattern.
166720006a0bStron */
1668*838f5788Ssimonb search_type = SRCH_BACK | def_search_type;
166920006a0bStron if (number <= 0)
167020006a0bStron number = 1;
167120006a0bStron mca_search();
167220006a0bStron c = getcc();
167320006a0bStron goto again;
167420006a0bStron
167520006a0bStron case A_FILTER:
167620006a0bStron #if HILITE_SEARCH
167720006a0bStron search_type = SRCH_FORW | SRCH_FILTER;
167820006a0bStron mca_search();
167920006a0bStron c = getcc();
168020006a0bStron goto again;
168120006a0bStron #else
168220006a0bStron error("Command not available", NULL_PARG);
168320006a0bStron break;
168420006a0bStron #endif
168520006a0bStron
168620006a0bStron case A_AGAIN_SEARCH:
168720006a0bStron /*
168820006a0bStron * Repeat previous search.
168920006a0bStron */
1690*838f5788Ssimonb search_type = last_search_type;
169120006a0bStron DO_SEARCH();
169220006a0bStron break;
169320006a0bStron
169420006a0bStron case A_T_AGAIN_SEARCH:
169520006a0bStron /*
169620006a0bStron * Repeat previous search, multiple files.
169720006a0bStron */
1698*838f5788Ssimonb search_type = last_search_type | SRCH_PAST_EOF;
169920006a0bStron DO_SEARCH();
170020006a0bStron break;
170120006a0bStron
170220006a0bStron case A_REVERSE_SEARCH:
170320006a0bStron /*
170420006a0bStron * Repeat previous search, in reverse direction.
170520006a0bStron */
1706*838f5788Ssimonb save_search_type = search_type = last_search_type;
170720006a0bStron search_type = SRCH_REVERSE(search_type);
170820006a0bStron DO_SEARCH();
1709*838f5788Ssimonb last_search_type = save_search_type;
171020006a0bStron break;
171120006a0bStron
171220006a0bStron case A_T_REVERSE_SEARCH:
171320006a0bStron /*
171420006a0bStron * Repeat previous search,
171520006a0bStron * multiple files in reverse direction.
171620006a0bStron */
1717*838f5788Ssimonb save_search_type = search_type = last_search_type;
1718*838f5788Ssimonb search_type = SRCH_REVERSE(search_type) | SRCH_PAST_EOF;
171920006a0bStron DO_SEARCH();
1720*838f5788Ssimonb last_search_type = save_search_type;
172120006a0bStron break;
172220006a0bStron
172320006a0bStron case A_UNDO_SEARCH:
1724*838f5788Ssimonb case A_CLR_SEARCH:
1725*838f5788Ssimonb /*
1726*838f5788Ssimonb * Clear search string highlighting.
1727*838f5788Ssimonb */
1728*838f5788Ssimonb undo_search(action == A_CLR_SEARCH);
172920006a0bStron break;
173020006a0bStron
173120006a0bStron case A_HELP:
173220006a0bStron /*
173320006a0bStron * Help.
173420006a0bStron */
173520006a0bStron if (ch_getflags() & CH_HELPFILE)
173620006a0bStron break;
173720006a0bStron cmd_exec();
173820006a0bStron save_hshift = hshift;
173920006a0bStron hshift = 0;
1740*838f5788Ssimonb save_bs_mode = bs_mode;
1741*838f5788Ssimonb bs_mode = BS_SPECIAL;
1742*838f5788Ssimonb save_proc_backspace = proc_backspace;
1743*838f5788Ssimonb proc_backspace = OPT_OFF;
174420006a0bStron (void) edit(FAKE_HELPFILE);
174520006a0bStron break;
174620006a0bStron
174720006a0bStron case A_EXAMINE:
174820006a0bStron /*
174920006a0bStron * Edit a new file. Get the filename.
175020006a0bStron */
1751*838f5788Ssimonb #if EXAMINE
1752*838f5788Ssimonb if (!secure)
175320006a0bStron {
175420006a0bStron start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
175520006a0bStron c = getcc();
175620006a0bStron goto again;
1757*838f5788Ssimonb }
1758*838f5788Ssimonb #endif
175920006a0bStron error("Command not available", NULL_PARG);
176020006a0bStron break;
176120006a0bStron
176220006a0bStron case A_VISUAL:
176320006a0bStron /*
176420006a0bStron * Invoke an editor on the input file.
176520006a0bStron */
176620006a0bStron #if EDITOR
1767*838f5788Ssimonb if (!secure)
176820006a0bStron {
176920006a0bStron if (ch_getflags() & CH_HELPFILE)
177020006a0bStron break;
177120006a0bStron if (strcmp(get_filename(curr_ifile), "-") == 0)
177220006a0bStron {
177320006a0bStron error("Cannot edit standard input", NULL_PARG);
177420006a0bStron break;
177520006a0bStron }
1776*838f5788Ssimonb if (get_altfilename(curr_ifile) != NULL)
177720006a0bStron {
177820006a0bStron error("WARNING: This file was viewed via LESSOPEN",
177920006a0bStron NULL_PARG);
178020006a0bStron }
178120006a0bStron start_mca(A_SHELL, "!", ml_shell, 0);
178220006a0bStron /*
178320006a0bStron * Expand the editor prototype string
178420006a0bStron * and pass it to the system to execute.
178520006a0bStron * (Make sure the screen is displayed so the
178620006a0bStron * expansion of "+%lm" works.)
178720006a0bStron */
178820006a0bStron make_display();
178920006a0bStron cmd_exec();
1790*838f5788Ssimonb lsystem(pr_expand(editproto), (char*)NULL);
179120006a0bStron break;
1792*838f5788Ssimonb }
1793*838f5788Ssimonb #endif
179420006a0bStron error("Command not available", NULL_PARG);
179520006a0bStron break;
179620006a0bStron
179720006a0bStron case A_NEXT_FILE:
179820006a0bStron /*
179920006a0bStron * Examine next file.
180020006a0bStron */
180120006a0bStron #if TAGS
180220006a0bStron if (ntags())
180320006a0bStron {
180420006a0bStron error("No next file", NULL_PARG);
180520006a0bStron break;
180620006a0bStron }
180720006a0bStron #endif
180820006a0bStron if (number <= 0)
180920006a0bStron number = 1;
181020006a0bStron if (edit_next((int) number))
181120006a0bStron {
181220006a0bStron if (get_quit_at_eof() && eof_displayed() &&
181320006a0bStron !(ch_getflags() & CH_HELPFILE))
181420006a0bStron quit(QUIT_OK);
181520006a0bStron parg.p_string = (number > 1) ? "(N-th) " : "";
181620006a0bStron error("No %snext file", &parg);
181720006a0bStron }
181820006a0bStron break;
181920006a0bStron
182020006a0bStron case A_PREV_FILE:
182120006a0bStron /*
182220006a0bStron * Examine previous file.
182320006a0bStron */
182420006a0bStron #if TAGS
182520006a0bStron if (ntags())
182620006a0bStron {
182720006a0bStron error("No previous file", NULL_PARG);
182820006a0bStron break;
182920006a0bStron }
183020006a0bStron #endif
183120006a0bStron if (number <= 0)
183220006a0bStron number = 1;
183320006a0bStron if (edit_prev((int) number))
183420006a0bStron {
183520006a0bStron parg.p_string = (number > 1) ? "(N-th) " : "";
183620006a0bStron error("No %sprevious file", &parg);
183720006a0bStron }
183820006a0bStron break;
183920006a0bStron
184020006a0bStron case A_NEXT_TAG:
1841*838f5788Ssimonb /*
1842*838f5788Ssimonb * Jump to the next tag in the current tag list.
1843*838f5788Ssimonb */
184420006a0bStron #if TAGS
184520006a0bStron if (number <= 0)
184620006a0bStron number = 1;
184720006a0bStron tagfile = nexttag((int) number);
184820006a0bStron if (tagfile == NULL)
184920006a0bStron {
185020006a0bStron error("No next tag", NULL_PARG);
185120006a0bStron break;
185220006a0bStron }
1853*838f5788Ssimonb cmd_exec();
185420006a0bStron if (edit(tagfile) == 0)
185520006a0bStron {
185620006a0bStron POSITION pos = tagsearch();
185720006a0bStron if (pos != NULL_POSITION)
185820006a0bStron jump_loc(pos, jump_sline);
185920006a0bStron }
186020006a0bStron #else
186120006a0bStron error("Command not available", NULL_PARG);
186220006a0bStron #endif
186320006a0bStron break;
186420006a0bStron
186520006a0bStron case A_PREV_TAG:
1866*838f5788Ssimonb /*
1867*838f5788Ssimonb * Jump to the previous tag in the current tag list.
1868*838f5788Ssimonb */
186920006a0bStron #if TAGS
187020006a0bStron if (number <= 0)
187120006a0bStron number = 1;
187220006a0bStron tagfile = prevtag((int) number);
187320006a0bStron if (tagfile == NULL)
187420006a0bStron {
187520006a0bStron error("No previous tag", NULL_PARG);
187620006a0bStron break;
187720006a0bStron }
1878*838f5788Ssimonb cmd_exec();
187920006a0bStron if (edit(tagfile) == 0)
188020006a0bStron {
188120006a0bStron POSITION pos = tagsearch();
188220006a0bStron if (pos != NULL_POSITION)
188320006a0bStron jump_loc(pos, jump_sline);
188420006a0bStron }
188520006a0bStron #else
188620006a0bStron error("Command not available", NULL_PARG);
188720006a0bStron #endif
188820006a0bStron break;
188920006a0bStron
189020006a0bStron case A_INDEX_FILE:
189120006a0bStron /*
189220006a0bStron * Examine a particular file.
189320006a0bStron */
189420006a0bStron if (number <= 0)
189520006a0bStron number = 1;
189620006a0bStron if (edit_index((int) number))
189720006a0bStron error("No such file", NULL_PARG);
189820006a0bStron break;
189920006a0bStron
190020006a0bStron case A_REMOVE_FILE:
1901*838f5788Ssimonb /*
1902*838f5788Ssimonb * Remove a file from the input file list.
1903*838f5788Ssimonb */
190420006a0bStron if (ch_getflags() & CH_HELPFILE)
190520006a0bStron break;
190620006a0bStron old_ifile = curr_ifile;
190720006a0bStron new_ifile = getoff_ifile(curr_ifile);
190820006a0bStron if (new_ifile == NULL_IFILE)
190920006a0bStron {
191020006a0bStron bell();
191120006a0bStron break;
191220006a0bStron }
191320006a0bStron if (edit_ifile(new_ifile) != 0)
191420006a0bStron {
191520006a0bStron reedit_ifile(old_ifile);
191620006a0bStron break;
191720006a0bStron }
191820006a0bStron del_ifile(old_ifile);
191920006a0bStron break;
192020006a0bStron
192120006a0bStron case A_OPT_TOGGLE:
1922*838f5788Ssimonb /*
1923*838f5788Ssimonb * Change the setting of an option.
1924*838f5788Ssimonb */
192520006a0bStron optflag = OPT_TOGGLE;
192620006a0bStron optgetname = FALSE;
192720006a0bStron mca_opt_toggle();
192820006a0bStron c = getcc();
1929*838f5788Ssimonb cbuf = opt_toggle_disallowed(c);
1930*838f5788Ssimonb if (cbuf != NULL)
1931*838f5788Ssimonb {
1932*838f5788Ssimonb error(cbuf, NULL_PARG);
1933*838f5788Ssimonb break;
1934*838f5788Ssimonb }
193520006a0bStron goto again;
193620006a0bStron
193720006a0bStron case A_DISP_OPTION:
193820006a0bStron /*
1939*838f5788Ssimonb * Report the setting of an option.
194020006a0bStron */
194120006a0bStron optflag = OPT_NO_TOGGLE;
194220006a0bStron optgetname = FALSE;
194320006a0bStron mca_opt_toggle();
194420006a0bStron c = getcc();
194520006a0bStron goto again;
194620006a0bStron
194720006a0bStron case A_FIRSTCMD:
194820006a0bStron /*
194920006a0bStron * Set an initial command for new files.
195020006a0bStron */
195120006a0bStron start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
195220006a0bStron c = getcc();
195320006a0bStron goto again;
195420006a0bStron
195520006a0bStron case A_SHELL:
1956*838f5788Ssimonb case A_PSHELL:
195720006a0bStron /*
195820006a0bStron * Shell escape.
195920006a0bStron */
196020006a0bStron #if SHELL_ESCAPE
1961*838f5788Ssimonb if (!secure)
196220006a0bStron {
1963*838f5788Ssimonb start_mca(action, (action == A_SHELL) ? "!" : "#", ml_shell, 0);
196420006a0bStron c = getcc();
196520006a0bStron goto again;
1966*838f5788Ssimonb }
1967*838f5788Ssimonb #endif
196820006a0bStron error("Command not available", NULL_PARG);
196920006a0bStron break;
197020006a0bStron
197120006a0bStron case A_SETMARK:
1972*838f5788Ssimonb case A_SETMARKBOT:
197320006a0bStron /*
197420006a0bStron * Set a mark.
197520006a0bStron */
197620006a0bStron if (ch_getflags() & CH_HELPFILE)
197720006a0bStron break;
1978*838f5788Ssimonb start_mca(A_SETMARK, "set mark: ", (void*)NULL, 0);
197920006a0bStron c = getcc();
1980*838f5788Ssimonb if (is_erase_char(c) || is_newline_char(c))
198120006a0bStron break;
1982*838f5788Ssimonb setmark(c, action == A_SETMARKBOT ? BOTTOM : TOP);
1983*838f5788Ssimonb repaint();
1984*838f5788Ssimonb break;
1985*838f5788Ssimonb
1986*838f5788Ssimonb case A_CLRMARK:
1987*838f5788Ssimonb /*
1988*838f5788Ssimonb * Clear a mark.
1989*838f5788Ssimonb */
1990*838f5788Ssimonb start_mca(A_CLRMARK, "clear mark: ", (void*)NULL, 0);
1991*838f5788Ssimonb c = getcc();
1992*838f5788Ssimonb if (is_erase_char(c) || is_newline_char(c))
1993*838f5788Ssimonb break;
1994*838f5788Ssimonb clrmark(c);
1995*838f5788Ssimonb repaint();
199620006a0bStron break;
199720006a0bStron
199820006a0bStron case A_GOMARK:
199920006a0bStron /*
2000*838f5788Ssimonb * Jump to a marked position.
200120006a0bStron */
200220006a0bStron start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
200320006a0bStron c = getcc();
2004*838f5788Ssimonb if (is_erase_char(c) || is_newline_char(c))
200520006a0bStron break;
200620006a0bStron cmd_exec();
200720006a0bStron gomark(c);
200820006a0bStron break;
200920006a0bStron
201020006a0bStron case A_PIPE:
2011*838f5788Ssimonb /*
2012*838f5788Ssimonb * Write part of the input to a pipe to a shell command.
2013*838f5788Ssimonb */
201420006a0bStron #if PIPEC
2015*838f5788Ssimonb if (!secure)
201620006a0bStron {
201720006a0bStron start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
201820006a0bStron c = getcc();
2019*838f5788Ssimonb if (is_erase_char(c))
202020006a0bStron break;
2021*838f5788Ssimonb if (is_newline_char(c))
202220006a0bStron c = '.';
202320006a0bStron if (badmark(c))
202420006a0bStron break;
202520006a0bStron pipec = c;
202620006a0bStron start_mca(A_PIPE, "!", ml_shell, 0);
202720006a0bStron c = getcc();
202820006a0bStron goto again;
2029*838f5788Ssimonb }
2030*838f5788Ssimonb #endif
203120006a0bStron error("Command not available", NULL_PARG);
203220006a0bStron break;
203320006a0bStron
203420006a0bStron case A_B_BRACKET:
203520006a0bStron case A_F_BRACKET:
203620006a0bStron start_mca(action, "Brackets: ", (void*)NULL, 0);
203720006a0bStron c = getcc();
203820006a0bStron goto again;
203920006a0bStron
204020006a0bStron case A_LSHIFT:
2041*838f5788Ssimonb /*
2042*838f5788Ssimonb * Shift view left.
2043*838f5788Ssimonb */
204420006a0bStron if (number > 0)
204520006a0bStron shift_count = number;
204620006a0bStron else
204720006a0bStron number = (shift_count > 0) ?
204820006a0bStron shift_count : sc_width / 2;
204920006a0bStron if (number > hshift)
205020006a0bStron number = hshift;
205120006a0bStron hshift -= number;
205220006a0bStron screen_trashed = 1;
205320006a0bStron break;
205420006a0bStron
205520006a0bStron case A_RSHIFT:
2056*838f5788Ssimonb /*
2057*838f5788Ssimonb * Shift view right.
2058*838f5788Ssimonb */
205920006a0bStron if (number > 0)
206020006a0bStron shift_count = number;
206120006a0bStron else
206220006a0bStron number = (shift_count > 0) ?
206320006a0bStron shift_count : sc_width / 2;
206420006a0bStron hshift += number;
206520006a0bStron screen_trashed = 1;
206620006a0bStron break;
206720006a0bStron
2068*838f5788Ssimonb case A_LLSHIFT:
2069*838f5788Ssimonb /*
2070*838f5788Ssimonb * Shift view left to margin.
2071*838f5788Ssimonb */
2072*838f5788Ssimonb hshift = 0;
2073*838f5788Ssimonb screen_trashed = 1;
2074*838f5788Ssimonb break;
2075*838f5788Ssimonb
2076*838f5788Ssimonb case A_RRSHIFT:
2077*838f5788Ssimonb /*
2078*838f5788Ssimonb * Shift view right to view rightmost char on screen.
2079*838f5788Ssimonb */
2080*838f5788Ssimonb hshift = rrshift();
2081*838f5788Ssimonb screen_trashed = 1;
2082*838f5788Ssimonb break;
2083*838f5788Ssimonb
208420006a0bStron case A_PREFIX:
208520006a0bStron /*
208620006a0bStron * The command is incomplete (more chars are needed).
208720006a0bStron * Display the current char, so the user knows
208820006a0bStron * what's going on, and get another character.
208920006a0bStron */
209020006a0bStron if (mca != A_PREFIX)
209120006a0bStron {
209220006a0bStron cmd_reset();
209320006a0bStron start_mca(A_PREFIX, " ", (void*)NULL,
209420006a0bStron CF_QUIT_ON_ERASE);
209520006a0bStron (void) cmd_char(c);
209620006a0bStron }
209720006a0bStron c = getcc();
209820006a0bStron goto again;
209920006a0bStron
210020006a0bStron case A_NOACTION:
210120006a0bStron break;
210220006a0bStron
210320006a0bStron default:
2104824a88bbStron if (be_helpful)
2105824a88bbStron helpprompt = 1;
2106824a88bbStron else
210720006a0bStron bell();
210820006a0bStron break;
210920006a0bStron }
211020006a0bStron }
211120006a0bStron }
2112