xref: /freebsd/contrib/less/command.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1 /* $FreeBSD$ */
2 /*
3  * Copyright (C) 1984-2023  Mark Nudelman
4  *
5  * You may distribute under the terms of either the GNU General Public
6  * License or the Less License, as specified in the README file.
7  *
8  * For more information, see the README file.
9  */
10 
11 
12 /*
13  * User-level command processor.
14  */
15 
16 #include "less.h"
17 #if MSDOS_COMPILER==WIN32C
18 #include <windows.h>
19 #endif
20 #include "position.h"
21 #include "option.h"
22 #include "cmd.h"
23 
24 extern int erase_char, erase2_char, kill_char;
25 extern int sigs;
26 extern int quit_if_one_screen;
27 extern int one_screen;
28 extern int squished;
29 extern int sc_width;
30 extern int sc_height;
31 extern char *kent;
32 extern int swindow;
33 extern int jump_sline;
34 extern int quitting;
35 extern int wscroll;
36 extern int top_scroll;
37 extern int ignore_eoi;
38 extern int secure;
39 extern int hshift;
40 extern int bs_mode;
41 extern int proc_backspace;
42 extern int show_attn;
43 extern int less_is_more;
44 extern int status_col;
45 extern POSITION highest_hilite;
46 extern POSITION start_attnpos;
47 extern POSITION end_attnpos;
48 extern char *every_first_cmd;
49 extern char version[];
50 extern struct scrpos initial_scrpos;
51 extern IFILE curr_ifile;
52 extern void *ml_search;
53 extern void *ml_examine;
54 extern int wheel_lines;
55 extern int header_lines;
56 extern int def_search_type;
57 extern int updown_match;
58 #if SHELL_ESCAPE || PIPEC
59 extern void *ml_shell;
60 #endif
61 #if EDITOR
62 extern char *editor;
63 extern char *editproto;
64 #endif
65 extern int screen_trashed;      /* The screen has been overwritten */
66 extern int shift_count;
67 extern int oldbot;
68 extern int forw_prompt;
69 extern int incr_search;
70 extern int full_screen;
71 #if MSDOS_COMPILER==WIN32C
72 extern int utf_mode;
73 #endif
74 
75 #if SHELL_ESCAPE
76 static char *shellcmd = NULL;   /* For holding last shell command for "!!" */
77 #endif
78 static int mca;                 /* The multicharacter command (action) */
79 static int search_type;         /* The previous type of search */
80 static int last_search_type;    /* Type of last executed search */
81 static LINENUM number;          /* The number typed by the user */
82 static long fraction;           /* The fractional part of the number */
83 static struct loption *curropt;
84 static int opt_lower;
85 static int optflag;
86 static int optgetname;
87 static POSITION bottompos;
88 static int save_hshift;
89 static int save_bs_mode;
90 static int save_proc_backspace;
91 #if PIPEC
92 static char pipec;
93 #endif
94 
95 /* Stack of ungotten chars (via ungetcc) */
96 struct ungot {
97 	struct ungot *ug_next;
98 	LWCHAR ug_char;
99 };
100 static struct ungot* ungot = NULL;
101 
102 static void multi_search (char *pattern, int n, int silent);
103 
104 /*
105  * Move the cursor to start of prompt line before executing a command.
106  * This looks nicer if the command takes a long time before
107  * updating the screen.
108  */
109 static void cmd_exec(void)
110 {
111 	clear_attn();
112 	clear_bot();
113 	flush();
114 }
115 
116 /*
117  * Indicate we are reading a multi-character command.
118  */
119 static void set_mca(int action)
120 {
121 	mca = action;
122 	clear_bot();
123 	clear_cmd();
124 }
125 
126 /*
127  * Indicate we are not reading a multi-character command.
128  */
129 static void clear_mca(void)
130 {
131 	if (mca == 0)
132 		return;
133 	mca = 0;
134 }
135 
136 /*
137  * Set up the display to start a new multi-character command.
138  */
139 static void start_mca(int action, constant char *prompt, void *mlist, int cmdflags)
140 {
141 	set_mca(action);
142 	cmd_putstr(prompt);
143 	set_mlist(mlist, cmdflags);
144 }
145 
146 public int in_mca(void)
147 {
148 	return (mca != 0 && mca != A_PREFIX);
149 }
150 
151 /*
152  * Set up the display to start a new search command.
153  */
154 static void mca_search1(void)
155 {
156 	int i;
157 
158 #if HILITE_SEARCH
159 	if (search_type & SRCH_FILTER)
160 		set_mca(A_FILTER);
161 	else
162 #endif
163 	if (search_type & SRCH_FORW)
164 		set_mca(A_F_SEARCH);
165 	else
166 		set_mca(A_B_SEARCH);
167 
168 	if (search_type & SRCH_NO_MATCH)
169 		cmd_putstr("Non-match ");
170 	if (search_type & SRCH_FIRST_FILE)
171 		cmd_putstr("First-file ");
172 	if (search_type & SRCH_PAST_EOF)
173 		cmd_putstr("EOF-ignore ");
174 	if (search_type & SRCH_NO_MOVE)
175 		cmd_putstr("Keep-pos ");
176 	if (search_type & SRCH_NO_REGEX)
177 		cmd_putstr("Regex-off ");
178 	if (search_type & SRCH_WRAP)
179 		cmd_putstr("Wrap ");
180 	for (i = 1; i <= NUM_SEARCH_COLORS; i++)
181 	{
182 		if (search_type & SRCH_SUBSEARCH(i))
183 		{
184 			char buf[8];
185 			SNPRINTF1(buf, sizeof(buf), "Sub-%d ", i);
186 			cmd_putstr(buf);
187 		}
188 	}
189 
190 #if HILITE_SEARCH
191 	if (search_type & SRCH_FILTER)
192 		cmd_putstr("&/");
193 	else
194 #endif
195 	if (search_type & SRCH_FORW)
196 		cmd_putstr("/");
197 	else
198 		cmd_putstr("?");
199 	forw_prompt = 0;
200 }
201 
202 static void mca_search(void)
203 {
204 	mca_search1();
205 	set_mlist(ml_search, 0);
206 }
207 
208 /*
209  * Set up the display to start a new toggle-option command.
210  */
211 static void mca_opt_toggle(void)
212 {
213 	int no_prompt;
214 	int flag;
215 	char *dash;
216 
217 	no_prompt = (optflag & OPT_NO_PROMPT);
218 	flag = (optflag & ~OPT_NO_PROMPT);
219 	dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
220 
221 	set_mca(A_OPT_TOGGLE);
222 	cmd_putstr(dash);
223 	if (optgetname)
224 		cmd_putstr(dash);
225 	if (no_prompt)
226 		cmd_putstr("(P)");
227 	switch (flag)
228 	{
229 	case OPT_UNSET:
230 		cmd_putstr("+");
231 		break;
232 	case OPT_SET:
233 		cmd_putstr("!");
234 		break;
235 	}
236 	forw_prompt = 0;
237 	set_mlist(NULL, 0);
238 }
239 
240 /*
241  * Execute a multicharacter command.
242  */
243 static void exec_mca(void)
244 {
245 	char *cbuf;
246 
247 	cmd_exec();
248 	cbuf = get_cmdbuf();
249 	if (cbuf == NULL)
250 		return;
251 
252 	switch (mca)
253 	{
254 	case A_F_SEARCH:
255 	case A_B_SEARCH:
256 		multi_search(cbuf, (int) number, 0);
257 		break;
258 #if HILITE_SEARCH
259 	case A_FILTER:
260 		search_type ^= SRCH_NO_MATCH;
261 		set_filter_pattern(cbuf, search_type);
262 		break;
263 #endif
264 	case A_FIRSTCMD:
265 		/*
266 		 * Skip leading spaces or + signs in the string.
267 		 */
268 		while (*cbuf == '+' || *cbuf == ' ')
269 			cbuf++;
270 		if (every_first_cmd != NULL)
271 			free(every_first_cmd);
272 		if (*cbuf == '\0')
273 			every_first_cmd = NULL;
274 		else
275 			every_first_cmd = save(cbuf);
276 		break;
277 	case A_OPT_TOGGLE:
278 		toggle_option(curropt, opt_lower, cbuf, optflag);
279 		curropt = NULL;
280 		break;
281 	case A_F_BRACKET:
282 		match_brac(cbuf[0], cbuf[1], 1, (int) number);
283 		break;
284 	case A_B_BRACKET:
285 		match_brac(cbuf[1], cbuf[0], 0, (int) number);
286 		break;
287 #if EXAMINE
288 	case A_EXAMINE:
289 		if (secure)
290 			break;
291 		edit_list(cbuf);
292 #if TAGS
293 		/* If tag structure is loaded then clean it up. */
294 		cleantags();
295 #endif
296 		break;
297 #endif
298 #if SHELL_ESCAPE
299 	case A_SHELL:
300 		/*
301 		 * !! just uses whatever is in shellcmd.
302 		 * Otherwise, copy cmdbuf to shellcmd,
303 		 * expanding any special characters ("%" or "#").
304 		 */
305 		if (*cbuf != '!')
306 		{
307 			if (shellcmd != NULL)
308 				free(shellcmd);
309 			shellcmd = fexpand(cbuf);
310 		}
311 
312 		if (secure)
313 			break;
314 		if (shellcmd == NULL)
315 			lsystem("", "!done");
316 		else
317 			lsystem(shellcmd, "!done");
318 		break;
319 	case A_PSHELL:
320 		if (secure)
321 			break;
322 		lsystem(pr_expand(cbuf), "#done");
323 		break;
324 #endif
325 #if PIPEC
326 	case A_PIPE:
327 		if (secure)
328 			break;
329 		(void) pipe_mark(pipec, cbuf);
330 		error("|done", NULL_PARG);
331 		break;
332 #endif
333 	}
334 }
335 
336 /*
337  * Is a character an erase or kill char?
338  */
339 static int is_erase_char(int c)
340 {
341 	return (c == erase_char || c == erase2_char || c == kill_char);
342 }
343 
344 /*
345  * Is a character a carriage return or newline?
346  */
347 static int is_newline_char(int c)
348 {
349 	return (c == '\n' || c == '\r');
350 }
351 
352 /*
353  * Handle the first char of an option (after the initial dash).
354  */
355 static int mca_opt_first_char(int c)
356 {
357 	int no_prompt = (optflag & OPT_NO_PROMPT);
358 	int flag = (optflag & ~OPT_NO_PROMPT);
359 	if (flag == OPT_NO_TOGGLE)
360 	{
361 		switch (c)
362 		{
363 		case '_':
364 			/* "__" = long option name. */
365 			optgetname = TRUE;
366 			mca_opt_toggle();
367 			return (MCA_MORE);
368 		}
369 	} else
370 	{
371 		switch (c)
372 		{
373 		case '+':
374 			/* "-+" = UNSET. */
375 			optflag = no_prompt | ((flag == OPT_UNSET) ?
376 				OPT_TOGGLE : OPT_UNSET);
377 			mca_opt_toggle();
378 			return (MCA_MORE);
379 		case '!':
380 			/* "-!" = SET */
381 			optflag = no_prompt | ((flag == OPT_SET) ?
382 				OPT_TOGGLE : OPT_SET);
383 			mca_opt_toggle();
384 			return (MCA_MORE);
385 		case CONTROL('P'):
386 			optflag ^= OPT_NO_PROMPT;
387 			mca_opt_toggle();
388 			return (MCA_MORE);
389 		case '-':
390 			/* "--" = long option name. */
391 			optgetname = TRUE;
392 			mca_opt_toggle();
393 			return (MCA_MORE);
394 		}
395 	}
396 	/* Char was not handled here. */
397 	return (NO_MCA);
398 }
399 
400 /*
401  * Add a char to a long option name.
402  * See if we've got a match for an option name yet.
403  * If so, display the complete name and stop
404  * accepting chars until user hits RETURN.
405  */
406 static int mca_opt_nonfirst_char(int c)
407 {
408 	char *p;
409 	char *oname;
410 	int err;
411 
412 	if (curropt != NULL)
413 	{
414 		/*
415 		 * Already have a match for the name.
416 		 * Don't accept anything but erase/kill.
417 		 */
418 		if (is_erase_char(c))
419 			return (MCA_DONE);
420 		return (MCA_MORE);
421 	}
422 	/*
423 	 * Add char to cmd buffer and try to match
424 	 * the option name.
425 	 */
426 	if (cmd_char(c) == CC_QUIT)
427 		return (MCA_DONE);
428 	p = get_cmdbuf();
429 	if (p == NULL)
430 		return (MCA_MORE);
431 	opt_lower = ASCII_IS_LOWER(p[0]);
432 	err = 0;
433 	curropt = findopt_name(&p, &oname, &err);
434 	if (curropt != NULL)
435 	{
436 		/*
437 		 * Got a match.
438 		 * Remember the option and
439 		 * display the full option name.
440 		 */
441 		cmd_reset();
442 		mca_opt_toggle();
443 		for (p = oname;  *p != '\0';  p++)
444 		{
445 			c = *p;
446 			if (!opt_lower && ASCII_IS_LOWER(c))
447 				c = ASCII_TO_UPPER(c);
448 			if (cmd_char(c) != CC_OK)
449 				return (MCA_DONE);
450 		}
451 	} else if (err != OPT_AMBIG)
452 	{
453 		bell();
454 	}
455 	return (MCA_MORE);
456 }
457 
458 /*
459  * Handle a char of an option toggle command.
460  */
461 static int mca_opt_char(int c)
462 {
463 	PARG parg;
464 
465 	/*
466 	 * This may be a short option (single char),
467 	 * or one char of a long option name,
468 	 * or one char of the option parameter.
469 	 */
470 	if (curropt == NULL && len_cmdbuf() == 0)
471 	{
472 		int ret = mca_opt_first_char(c);
473 		if (ret != NO_MCA)
474 			return (ret);
475 	}
476 	if (optgetname)
477 	{
478 		/* We're getting a long option name.  */
479 		if (!is_newline_char(c) && c != '=')
480 			return (mca_opt_nonfirst_char(c));
481 		if (curropt == NULL)
482 		{
483 			parg.p_string = get_cmdbuf();
484 			if (parg.p_string == NULL)
485 				return (MCA_MORE);
486 			error("There is no --%s option", &parg);
487 			return (MCA_DONE);
488 		}
489 		optgetname = FALSE;
490 		cmd_reset();
491 	} else
492 	{
493 		if (is_erase_char(c))
494 			return (NO_MCA);
495 		if (curropt != NULL)
496 			/* We're getting the option parameter. */
497 			return (NO_MCA);
498 		curropt = findopt(c);
499 		if (curropt == NULL)
500 		{
501 			parg.p_string = propt(c);
502 			error("There is no %s option", &parg);
503 			return (MCA_DONE);
504 		}
505 		opt_lower = ASCII_IS_LOWER(c);
506 	}
507 	/*
508 	 * If the option which was entered does not take a
509 	 * parameter, toggle the option immediately,
510 	 * so user doesn't have to hit RETURN.
511 	 */
512 	if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
513 	    !opt_has_param(curropt))
514 	{
515 		toggle_option(curropt, opt_lower, "", optflag);
516 		return (MCA_DONE);
517 	}
518 	/*
519 	 * Display a prompt appropriate for the option parameter.
520 	 */
521 	start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
522 	return (MCA_MORE);
523 }
524 
525 /*
526  * Normalize search type.
527  */
528 public int norm_search_type(int st)
529 {
530 	/* WRAP and PAST_EOF are mutually exclusive. */
531 	if ((st & (SRCH_PAST_EOF|SRCH_WRAP)) == (SRCH_PAST_EOF|SRCH_WRAP))
532 		st ^= SRCH_PAST_EOF;
533 	return st;
534 }
535 
536 /*
537  * Handle a char of a search command.
538  */
539 static int mca_search_char(int c)
540 {
541 	int flag = 0;
542 
543 	/*
544 	 * Certain characters as the first char of
545 	 * the pattern have special meaning:
546 	 *      !  Toggle the NO_MATCH flag
547 	 *      *  Toggle the PAST_EOF flag
548 	 *      @  Toggle the FIRST_FILE flag
549 	 */
550 	if (len_cmdbuf() > 0)
551 		return (NO_MCA);
552 
553 	switch (c)
554 	{
555 	case '*':
556 		if (less_is_more)
557 			break;
558 	case CONTROL('E'): /* ignore END of file */
559 		if (mca != A_FILTER)
560 			flag = SRCH_PAST_EOF;
561 		break;
562 	case '@':
563 		if (less_is_more)
564 			break;
565 	case CONTROL('F'): /* FIRST file */
566 		if (mca != A_FILTER)
567 			flag = SRCH_FIRST_FILE;
568 		break;
569 	case CONTROL('K'): /* KEEP position */
570 		if (mca != A_FILTER)
571 			flag = SRCH_NO_MOVE;
572 		break;
573 	case CONTROL('S'): { /* SUBSEARCH */
574 		char buf[32];
575 		SNPRINTF1(buf, sizeof(buf), "Sub-pattern (1-%d):", NUM_SEARCH_COLORS);
576 		clear_bot();
577 		cmd_putstr(buf);
578 		flush();
579 		c = getcc();
580 		if (c >= '1' && c <= '0'+NUM_SEARCH_COLORS)
581 			flag = SRCH_SUBSEARCH(c-'0');
582 		else
583 			flag = -1; /* calls mca_search() below to repaint */
584 		break; }
585 	case CONTROL('W'): /* WRAP around */
586 		if (mca != A_FILTER)
587 			flag = SRCH_WRAP;
588 		break;
589 	case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
590 		flag = SRCH_NO_REGEX;
591 		break;
592 	case CONTROL('N'): /* NOT match */
593 	case '!':
594 		flag = SRCH_NO_MATCH;
595 		break;
596 	}
597 
598 	if (flag != 0)
599 	{
600 		if (flag != -1)
601 			search_type = norm_search_type(search_type ^ flag);
602 		mca_search();
603 		return (MCA_MORE);
604 	}
605 	return (NO_MCA);
606 }
607 
608 /*
609  * Handle a character of a multi-character command.
610  */
611 static int mca_char(int c)
612 {
613 	int ret;
614 
615 	switch (mca)
616 	{
617 	case 0:
618 		/*
619 		 * We're not in a multicharacter command.
620 		 */
621 		return (NO_MCA);
622 
623 	case A_PREFIX:
624 		/*
625 		 * In the prefix of a command.
626 		 * This not considered a multichar command
627 		 * (even tho it uses cmdbuf, etc.).
628 		 * It is handled in the commands() switch.
629 		 */
630 		return (NO_MCA);
631 
632 	case A_DIGIT:
633 		/*
634 		 * Entering digits of a number.
635 		 * Terminated by a non-digit.
636 		 */
637 		if ((c >= '0' && c <= '9') || c == '.')
638 			break;
639 		switch (editchar(c, ECF_PEEK|ECF_NOHISTORY|ECF_NOCOMPLETE|ECF_NORIGHTLEFT))
640 		{
641 		case A_NOACTION:
642 			/*
643 			 * Ignore this char and get another one.
644 			 */
645 			return (MCA_MORE);
646 		case A_INVALID:
647 			/*
648 			 * Not part of the number.
649 			 * End the number and treat this char
650 			 * as a normal command character.
651 			 */
652 			number = cmd_int(&fraction);
653 			clear_mca();
654 			cmd_accept();
655 			return (NO_MCA);
656 		}
657 		break;
658 
659 	case A_OPT_TOGGLE:
660 		ret = mca_opt_char(c);
661 		if (ret != NO_MCA)
662 			return (ret);
663 		break;
664 
665 	case A_F_SEARCH:
666 	case A_B_SEARCH:
667 	case A_FILTER:
668 		ret = mca_search_char(c);
669 		if (ret != NO_MCA)
670 			return (ret);
671 		break;
672 
673 	default:
674 		/* Other multicharacter command. */
675 		break;
676 	}
677 
678 	/*
679 	 * The multichar command is terminated by a newline.
680 	 */
681 	if (is_newline_char(c))
682 	{
683 		/*
684 		 * Execute the command.
685 		 */
686 		exec_mca();
687 		return (MCA_DONE);
688 	}
689 
690 	/*
691 	 * Append the char to the command buffer.
692 	 */
693 	if (cmd_char(c) == CC_QUIT)
694 		/*
695 		 * Abort the multi-char command.
696 		 */
697 		return (MCA_DONE);
698 
699 	switch (mca)
700 	{
701 	case A_F_BRACKET:
702 	case A_B_BRACKET:
703 		if (len_cmdbuf() >= 2)
704 		{
705 			/*
706 			 * Special case for the bracket-matching commands.
707 			 * Execute the command after getting exactly two
708 			 * characters from the user.
709 			 */
710 			exec_mca();
711 			return (MCA_DONE);
712 		}
713 		break;
714 	case A_F_SEARCH:
715 	case A_B_SEARCH:
716 		if (incr_search)
717 		{
718 			/* Incremental search: do a search after every input char. */
719 			int st = (search_type & (SRCH_FORW|SRCH_BACK|SRCH_NO_MATCH|SRCH_NO_REGEX|SRCH_NO_MOVE|SRCH_WRAP|SRCH_SUBSEARCH_ALL));
720 			char *pattern = get_cmdbuf();
721 			if (pattern == NULL)
722 				return (MCA_MORE);
723 			/*
724 			 * Must save updown_match because mca_search
725 			 * reinits it. That breaks history scrolling.
726 			 * {{ This is ugly. mca_search probably shouldn't call set_mlist. }}
727 			 */
728 			int save_updown_match = updown_match;
729 			cmd_exec();
730 			if (*pattern == '\0')
731 			{
732 				/* User has backspaced to an empty pattern. */
733 				undo_search(1);
734 			} else
735 			{
736 				if (search(st | SRCH_INCR, pattern, 1) != 0)
737 					/* No match, invalid pattern, etc. */
738 					undo_search(1);
739 			}
740 			/* Redraw the search prompt and search string. */
741 			if (!full_screen)
742 			{
743 				clear();
744 				repaint();
745 			}
746 			mca_search1();
747 			updown_match = save_updown_match;
748 			cmd_repaint(NULL);
749 		}
750 		break;
751 	}
752 
753 	/*
754 	 * Need another character.
755 	 */
756 	return (MCA_MORE);
757 }
758 
759 /*
760  * Discard any buffered file data.
761  */
762 static void clear_buffers(void)
763 {
764 	if (!(ch_getflags() & CH_CANSEEK))
765 		return;
766 	ch_flush();
767 	clr_linenum();
768 #if HILITE_SEARCH
769 	clr_hilite();
770 #endif
771 }
772 
773 /*
774  * Make sure the screen is displayed.
775  */
776 static void make_display(void)
777 {
778 	/*
779 	 * If not full_screen, we can't rely on scrolling to fill the screen.
780 	 * We need to clear and repaint screen before any change.
781 	 */
782 	if (!full_screen && !(quit_if_one_screen && one_screen))
783 		clear();
784 	/*
785 	 * If nothing is displayed yet, display starting from initial_scrpos.
786 	 */
787 	if (empty_screen())
788 	{
789 		if (initial_scrpos.pos == NULL_POSITION)
790 			jump_loc(ch_zero(), 1);
791 		else
792 			jump_loc(initial_scrpos.pos, initial_scrpos.ln);
793 	} else if (screen_trashed || !full_screen)
794 	{
795 		int save_top_scroll = top_scroll;
796 		int save_ignore_eoi = ignore_eoi;
797 		top_scroll = 1;
798 		ignore_eoi = 0;
799 		if (screen_trashed == 2)
800 		{
801 			/* Special case used by ignore_eoi: re-open the input file
802 			 * and jump to the end of the file. */
803 			reopen_curr_ifile();
804 			jump_forw();
805 		}
806 		repaint();
807 		top_scroll = save_top_scroll;
808 		ignore_eoi = save_ignore_eoi;
809 	}
810 }
811 
812 /*
813  * Display the appropriate prompt.
814  */
815 static void prompt(void)
816 {
817 	constant char *p;
818 
819 	if (ungot != NULL && ungot->ug_char != CHAR_END_COMMAND)
820 	{
821 		/*
822 		 * No prompt necessary if commands are from
823 		 * ungotten chars rather than from the user.
824 		 */
825 		return;
826 	}
827 
828 	/*
829 	 * Make sure the screen is displayed.
830 	 */
831 	make_display();
832 	bottompos = position(BOTTOM_PLUS_ONE);
833 
834 	/*
835 	 * If we've hit EOF on the last file and the -E flag is set, quit.
836 	 */
837 	if (get_quit_at_eof() == OPT_ONPLUS &&
838 	    eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
839 	    next_ifile(curr_ifile) == NULL_IFILE)
840 		quit(QUIT_OK);
841 
842 	/*
843 	 * If the entire file is displayed and the -F flag is set, quit.
844 	 */
845 	if (quit_if_one_screen &&
846 	    entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
847 	    next_ifile(curr_ifile) == NULL_IFILE)
848 		quit(QUIT_OK);
849 	quit_if_one_screen = FALSE; /* only get one chance at this */
850 
851 #if MSDOS_COMPILER==WIN32C
852 	/*
853 	 * In Win32, display the file name in the window title.
854 	 */
855 	if (!(ch_getflags() & CH_HELPFILE))
856 	{
857 		WCHAR w[MAX_PATH+16];
858 		p = pr_expand("Less?f - %f.");
859 		MultiByteToWideChar(CP_ACP, 0, p, -1, w, sizeof(w)/sizeof(*w));
860 		SetConsoleTitleW(w);
861 	}
862 #endif
863 
864 	/*
865 	 * Select the proper prompt and display it.
866 	 */
867 	/*
868 	 * If the previous action was a forward movement,
869 	 * don't clear the bottom line of the display;
870 	 * just print the prompt since the forward movement guarantees
871 	 * that we're in the right position to display the prompt.
872 	 * Clearing the line could cause a problem: for example, if the last
873 	 * line displayed ended at the right screen edge without a newline,
874 	 * then clearing would clear the last displayed line rather than
875 	 * the prompt line.
876 	 */
877 	if (!forw_prompt)
878 		clear_bot();
879 	clear_cmd();
880 	forw_prompt = 0;
881 	p = pr_string();
882 #if HILITE_SEARCH
883 	if (is_filtering())
884 		putstr("& ");
885 #endif
886 	if (p == NULL || *p == '\0')
887 	{
888 		at_enter(AT_NORMAL|AT_COLOR_PROMPT);
889 		putchr(':');
890 		at_exit();
891 	} else
892 	{
893 #if MSDOS_COMPILER==WIN32C
894 		WCHAR w[MAX_PATH*2];
895 		char  a[MAX_PATH*2];
896 		MultiByteToWideChar(CP_ACP, 0, p, -1, w, sizeof(w)/sizeof(*w));
897 		WideCharToMultiByte(utf_mode ? CP_UTF8 : GetConsoleOutputCP(),
898 		                    0, w, -1, a, sizeof(a), NULL, NULL);
899 		p = a;
900 #endif
901 		load_line(p);
902 		put_line();
903 	}
904 	clear_eol();
905 }
906 
907 /*
908  * Display the less version message.
909  */
910 public void dispversion(void)
911 {
912 	PARG parg;
913 
914 	parg.p_string = version;
915 	error("less %s", &parg);
916 }
917 
918 /*
919  * Return a character to complete a partial command, if possible.
920  */
921 static LWCHAR getcc_end_command(void)
922 {
923 	switch (mca)
924 	{
925 	case A_DIGIT:
926 		/* We have a number but no command.  Treat as #g. */
927 		return ('g');
928 	case A_F_SEARCH:
929 	case A_B_SEARCH:
930 	case A_FILTER:
931 		/* We have "/string" but no newline.  Add the \n. */
932 		return ('\n');
933 	default:
934 		/* Some other incomplete command.  Let user complete it. */
935 		return ((ungot == NULL) ? getchr() : 0);
936 	}
937 }
938 
939 /*
940  * Get command character.
941  * The character normally comes from the keyboard,
942  * but may come from ungotten characters
943  * (characters previously given to ungetcc or ungetsc).
944  */
945 static LWCHAR getccu(void)
946 {
947 	LWCHAR c = 0;
948 	while (c == 0)
949 	{
950 		if (ungot == NULL)
951 		{
952 			/* Normal case: no ungotten chars.
953 			 * Get char from the user. */
954 			c = getchr();
955 		} else
956 		{
957 			/* Ungotten chars available:
958 			 * Take the top of stack (most recent). */
959 			struct ungot *ug = ungot;
960 			c = ug->ug_char;
961 			ungot = ug->ug_next;
962 			free(ug);
963 
964 			if (c == CHAR_END_COMMAND)
965 				c = getcc_end_command();
966 		}
967 	}
968 	return (c);
969 }
970 
971 /*
972  * Get a command character, but if we receive the orig sequence,
973  * convert it to the repl sequence.
974  */
975 static LWCHAR getcc_repl(char constant *orig, char constant *repl, LWCHAR (*gr_getc)(void), void (*gr_ungetc)(LWCHAR))
976 {
977 	LWCHAR c;
978 	LWCHAR keys[16];
979 	int ki = 0;
980 
981 	c = (*gr_getc)();
982 	if (orig == NULL || orig[0] == '\0')
983 		return c;
984 	for (;;)
985 	{
986 		keys[ki] = c;
987 		if (c != orig[ki] || ki >= sizeof(keys)-1)
988 		{
989 			/* This is not orig we have been receiving.
990 			 * If we have stashed chars in keys[],
991 			 * unget them and return the first one. */
992 			while (ki > 0)
993 				(*gr_ungetc)(keys[ki--]);
994 			return keys[0];
995 		}
996 		if (orig[++ki] == '\0')
997 		{
998 			/* We've received the full orig sequence.
999 			 * Return the repl sequence. */
1000 			ki = strlen(repl)-1;
1001 			while (ki > 0)
1002 				(*gr_ungetc)(repl[ki--]);
1003 			return repl[0];
1004 		}
1005 		/* We've received a partial orig sequence (ki chars of it).
1006 		 * Get next char and see if it continues to match orig. */
1007 		c = (*gr_getc)();
1008 	}
1009 }
1010 
1011 /*
1012  * Get command character.
1013  */
1014 public int getcc(void)
1015 {
1016 	/* Replace kent (keypad Enter) with a newline. */
1017 	return getcc_repl(kent, "\n", getccu, ungetcc);
1018 }
1019 
1020 /*
1021  * "Unget" a command character.
1022  * The next getcc() will return this character.
1023  */
1024 public void ungetcc(LWCHAR c)
1025 {
1026 	struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
1027 
1028 	ug->ug_char = c;
1029 	ug->ug_next = ungot;
1030 	ungot = ug;
1031 }
1032 
1033 /*
1034  * "Unget" a command character.
1035  * If any other chars are already ungotten, put this one after those.
1036  */
1037 public void ungetcc_back(LWCHAR c)
1038 {
1039 	struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
1040 	ug->ug_char = c;
1041 	ug->ug_next = NULL;
1042 	if (ungot == NULL)
1043 		ungot = ug;
1044 	else
1045 	{
1046 		struct ungot *pu;
1047 		for (pu = ungot; pu->ug_next != NULL; pu = pu->ug_next)
1048 			continue;
1049 		pu->ug_next = ug;
1050 	}
1051 }
1052 
1053 /*
1054  * Unget a whole string of command characters.
1055  * The next sequence of getcc()'s will return this string.
1056  */
1057 public void ungetsc(char *s)
1058 {
1059 	while (*s != '\0')
1060 		ungetcc_back(*s++);
1061 }
1062 
1063 /*
1064  * Peek the next command character, without consuming it.
1065  */
1066 public LWCHAR peekcc(void)
1067 {
1068 	LWCHAR c = getcc();
1069 	ungetcc(c);
1070 	return c;
1071 }
1072 
1073 /*
1074  * Search for a pattern, possibly in multiple files.
1075  * If SRCH_FIRST_FILE is set, begin searching at the first file.
1076  * If SRCH_PAST_EOF is set, continue the search thru multiple files.
1077  */
1078 static void multi_search(char *pattern, int n, int silent)
1079 {
1080 	int nomore;
1081 	IFILE save_ifile;
1082 	int changed_file;
1083 
1084 	changed_file = 0;
1085 	save_ifile = save_curr_ifile();
1086 
1087 	if ((search_type & (SRCH_FORW|SRCH_BACK)) == 0)
1088 		search_type |= SRCH_FORW;
1089 	if (search_type & SRCH_FIRST_FILE)
1090 	{
1091 		/*
1092 		 * Start at the first (or last) file
1093 		 * in the command line list.
1094 		 */
1095 		if (search_type & SRCH_FORW)
1096 			nomore = edit_first();
1097 		else
1098 			nomore = edit_last();
1099 		if (nomore)
1100 		{
1101 			unsave_ifile(save_ifile);
1102 			return;
1103 		}
1104 		changed_file = 1;
1105 		search_type &= ~SRCH_FIRST_FILE;
1106 	}
1107 
1108 	for (;;)
1109 	{
1110 		n = search(search_type, pattern, n);
1111 		/*
1112 		 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
1113 		 * after being used once.  This allows "n" to work after
1114 		 * using a /@@ search.
1115 		 */
1116 		search_type &= ~SRCH_NO_MOVE;
1117 		last_search_type = search_type;
1118 		if (n == 0)
1119 		{
1120 			/*
1121 			 * Found it.
1122 			 */
1123 			unsave_ifile(save_ifile);
1124 			return;
1125 		}
1126 
1127 		if (n < 0)
1128 			/*
1129 			 * Some kind of error in the search.
1130 			 * Error message has been printed by search().
1131 			 */
1132 			break;
1133 
1134 		if ((search_type & SRCH_PAST_EOF) == 0)
1135 			/*
1136 			 * We didn't find a match, but we're
1137 			 * supposed to search only one file.
1138 			 */
1139 			break;
1140 		/*
1141 		 * Move on to the next file.
1142 		 */
1143 		if (search_type & SRCH_FORW)
1144 			nomore = edit_next(1);
1145 		else
1146 			nomore = edit_prev(1);
1147 		if (nomore)
1148 			break;
1149 		changed_file = 1;
1150 	}
1151 
1152 	/*
1153 	 * Didn't find it.
1154 	 * Print an error message if we haven't already.
1155 	 */
1156 	if (n > 0 && !silent)
1157 		error("Pattern not found", NULL_PARG);
1158 
1159 	if (changed_file)
1160 	{
1161 		/*
1162 		 * Restore the file we were originally viewing.
1163 		 */
1164 		reedit_ifile(save_ifile);
1165 	} else
1166 	{
1167 		unsave_ifile(save_ifile);
1168 	}
1169 }
1170 
1171 /*
1172  * Forward forever, or until a highlighted line appears.
1173  */
1174 static int forw_loop(int until_hilite)
1175 {
1176 	POSITION curr_len;
1177 
1178 	if (ch_getflags() & CH_HELPFILE)
1179 		return (A_NOACTION);
1180 
1181 	cmd_exec();
1182 	jump_forw_buffered();
1183 	curr_len = ch_length();
1184 	highest_hilite = until_hilite ? curr_len : NULL_POSITION;
1185 	ignore_eoi = 1;
1186 	while (!sigs)
1187 	{
1188 		if (until_hilite && highest_hilite > curr_len)
1189 		{
1190 			bell();
1191 			break;
1192 		}
1193 		make_display();
1194 		forward(1, 0, 0);
1195 	}
1196 	ignore_eoi = 0;
1197 	ch_set_eof();
1198 
1199 	/*
1200 	 * This gets us back in "F mode" after processing
1201 	 * a non-abort signal (e.g. window-change).
1202 	 */
1203 	if (sigs && !ABORT_SIGS())
1204 		return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
1205 
1206 	return (A_NOACTION);
1207 }
1208 
1209 /*
1210  * Main command processor.
1211  * Accept and execute commands until a quit command.
1212  */
1213 public void commands(void)
1214 {
1215 	int c;
1216 	int action;
1217 	char *cbuf;
1218 	int newaction;
1219 	int save_jump_sline;
1220 	int save_search_type;
1221 	char *extra;
1222 	char tbuf[2];
1223 	PARG parg;
1224 	IFILE old_ifile;
1225 	IFILE new_ifile;
1226 	char *tagfile;
1227 
1228 	search_type = SRCH_FORW;
1229 	wscroll = (sc_height + 1) / 2;
1230 	newaction = A_NOACTION;
1231 
1232 	for (;;)
1233 	{
1234 		clear_mca();
1235 		cmd_accept();
1236 		number = 0;
1237 		curropt = NULL;
1238 
1239 		/*
1240 		 * See if any signals need processing.
1241 		 */
1242 		if (sigs)
1243 		{
1244 			psignals();
1245 			if (quitting)
1246 				quit(QUIT_SAVED_STATUS);
1247 		}
1248 
1249 		/*
1250 		 * See if window size changed, for systems that don't
1251 		 * generate SIGWINCH.
1252 		 */
1253 		check_winch();
1254 
1255 		/*
1256 		 * Display prompt and accept a character.
1257 		 */
1258 		cmd_reset();
1259 		prompt();
1260 		if (sigs)
1261 			continue;
1262 		if (newaction == A_NOACTION)
1263 			c = getcc();
1264 
1265 	again:
1266 		if (sigs)
1267 			continue;
1268 
1269 		if (newaction != A_NOACTION)
1270 		{
1271 			action = newaction;
1272 			newaction = A_NOACTION;
1273 		} else
1274 		{
1275 			/*
1276 			 * If we are in a multicharacter command, call mca_char.
1277 			 * Otherwise we call fcmd_decode to determine the
1278 			 * action to be performed.
1279 			 */
1280 			if (mca)
1281 				switch (mca_char(c))
1282 				{
1283 				case MCA_MORE:
1284 					/*
1285 					 * Need another character.
1286 					 */
1287 					c = getcc();
1288 					goto again;
1289 				case MCA_DONE:
1290 					/*
1291 					 * Command has been handled by mca_char.
1292 					 * Start clean with a prompt.
1293 					 */
1294 					continue;
1295 				case NO_MCA:
1296 					/*
1297 					 * Not a multi-char command
1298 					 * (at least, not anymore).
1299 					 */
1300 					break;
1301 				}
1302 
1303 			/*
1304 			 * Decode the command character and decide what to do.
1305 			 */
1306 			if (mca)
1307 			{
1308 				/*
1309 				 * We're in a multichar command.
1310 				 * Add the character to the command buffer
1311 				 * and display it on the screen.
1312 				 * If the user backspaces past the start
1313 				 * of the line, abort the command.
1314 				 */
1315 				if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1316 					continue;
1317 				cbuf = get_cmdbuf();
1318 				if (cbuf == NULL)
1319 					continue;
1320 			} else
1321 			{
1322 				/*
1323 				 * Don't use cmd_char if we're starting fresh
1324 				 * at the beginning of a command, because we
1325 				 * don't want to echo the command until we know
1326 				 * it is a multichar command.  We also don't
1327 				 * want erase_char/kill_char to be treated
1328 				 * as line editing characters.
1329 				 */
1330 				tbuf[0] = c;
1331 				tbuf[1] = '\0';
1332 				cbuf = tbuf;
1333 			}
1334 			extra = NULL;
1335 			action = fcmd_decode(cbuf, &extra);
1336 			/*
1337 			 * If an "extra" string was returned,
1338 			 * process it as a string of command characters.
1339 			 */
1340 			if (extra != NULL)
1341 				ungetsc(extra);
1342 		}
1343 		/*
1344 		 * Clear the cmdbuf string.
1345 		 * (But not if we're in the prefix of a command,
1346 		 * because the partial command string is kept there.)
1347 		 */
1348 		if (action != A_PREFIX)
1349 			cmd_reset();
1350 
1351 		switch (action)
1352 		{
1353 		case A_DIGIT:
1354 			/*
1355 			 * First digit of a number.
1356 			 */
1357 			start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1358 			goto again;
1359 
1360 		case A_F_WINDOW:
1361 			/*
1362 			 * Forward one window (and set the window size).
1363 			 */
1364 			if (number > 0)
1365 				swindow = (int) number;
1366 			/* FALLTHRU */
1367 		case A_F_SCREEN:
1368 			/*
1369 			 * Forward one screen.
1370 			 */
1371 			if (number <= 0)
1372 				number = get_swindow();
1373 			cmd_exec();
1374 			if (show_attn)
1375 				set_attnpos(bottompos);
1376 			forward((int) number, 0, 1);
1377 			break;
1378 
1379 		case A_B_WINDOW:
1380 			/*
1381 			 * Backward one window (and set the window size).
1382 			 */
1383 			if (number > 0)
1384 				swindow = (int) number;
1385 			/* FALLTHRU */
1386 		case A_B_SCREEN:
1387 			/*
1388 			 * Backward one screen.
1389 			 */
1390 			if (number <= 0)
1391 				number = get_swindow();
1392 			cmd_exec();
1393 			backward((int) number, 0, 1);
1394 			break;
1395 
1396 		case A_F_LINE:
1397 			/*
1398 			 * Forward N (default 1) line.
1399 			 */
1400 			if (number <= 0)
1401 				number = 1;
1402 			cmd_exec();
1403 			if (show_attn == OPT_ONPLUS && number > 1)
1404 				set_attnpos(bottompos);
1405 			forward((int) number, 0, 0);
1406 			break;
1407 
1408 		case A_B_LINE:
1409 			/*
1410 			 * Backward N (default 1) line.
1411 			 */
1412 			if (number <= 0)
1413 				number = 1;
1414 			cmd_exec();
1415 			backward((int) number, 0, 0);
1416 			break;
1417 
1418 		case A_F_MOUSE:
1419 			/*
1420 			 * Forward wheel_lines lines.
1421 			 */
1422 			cmd_exec();
1423 			forward(wheel_lines, 0, 0);
1424 			break;
1425 
1426 		case A_B_MOUSE:
1427 			/*
1428 			 * Backward wheel_lines lines.
1429 			 */
1430 			cmd_exec();
1431 			backward(wheel_lines, 0, 0);
1432 			break;
1433 
1434 		case A_FF_LINE:
1435 			/*
1436 			 * Force forward N (default 1) line.
1437 			 */
1438 			if (number <= 0)
1439 				number = 1;
1440 			cmd_exec();
1441 			if (show_attn == OPT_ONPLUS && number > 1)
1442 				set_attnpos(bottompos);
1443 			forward((int) number, 1, 0);
1444 			break;
1445 
1446 		case A_BF_LINE:
1447 			/*
1448 			 * Force backward N (default 1) line.
1449 			 */
1450 			if (number <= 0)
1451 				number = 1;
1452 			cmd_exec();
1453 			backward((int) number, 1, 0);
1454 			break;
1455 
1456 		case A_FF_SCREEN:
1457 			/*
1458 			 * Force forward one screen.
1459 			 */
1460 			if (number <= 0)
1461 				number = get_swindow();
1462 			cmd_exec();
1463 			if (show_attn == OPT_ONPLUS)
1464 				set_attnpos(bottompos);
1465 			forward((int) number, 1, 0);
1466 			break;
1467 
1468 		case A_F_FOREVER:
1469 			/*
1470 			 * Forward forever, ignoring EOF.
1471 			 */
1472 			if (show_attn)
1473 				set_attnpos(bottompos);
1474 			newaction = forw_loop(0);
1475 			break;
1476 
1477 		case A_F_UNTIL_HILITE:
1478 			newaction = forw_loop(1);
1479 			break;
1480 
1481 		case A_F_SCROLL:
1482 			/*
1483 			 * Forward N lines
1484 			 * (default same as last 'd' or 'u' command).
1485 			 */
1486 			if (number > 0)
1487 				wscroll = (int) number;
1488 			cmd_exec();
1489 			if (show_attn == OPT_ONPLUS)
1490 				set_attnpos(bottompos);
1491 			forward(wscroll, 0, 0);
1492 			break;
1493 
1494 		case A_B_SCROLL:
1495 			/*
1496 			 * Forward N lines
1497 			 * (default same as last 'd' or 'u' command).
1498 			 */
1499 			if (number > 0)
1500 				wscroll = (int) number;
1501 			cmd_exec();
1502 			backward(wscroll, 0, 0);
1503 			break;
1504 
1505 		case A_FREPAINT:
1506 			/*
1507 			 * Flush buffers, then repaint screen.
1508 			 * Don't flush the buffers on a pipe!
1509 			 */
1510 			clear_buffers();
1511 			/* FALLTHRU */
1512 		case A_REPAINT:
1513 			/*
1514 			 * Repaint screen.
1515 			 */
1516 			cmd_exec();
1517 			repaint();
1518 			break;
1519 
1520 		case A_GOLINE:
1521 			/*
1522 			 * Go to line N, default beginning of file.
1523 			 * If N <= 0, ignore jump_sline in order to avoid
1524 			 * empty lines before the beginning of the file.
1525 			 */
1526 			save_jump_sline = jump_sline;
1527 			if (number <= 0)
1528 			{
1529 				number = 1;
1530 				jump_sline = 0;
1531 			}
1532 			cmd_exec();
1533 			jump_back(number);
1534 			jump_sline = save_jump_sline;
1535 			break;
1536 
1537 		case A_PERCENT:
1538 			/*
1539 			 * Go to a specified percentage into the file.
1540 			 */
1541 			if (number < 0)
1542 			{
1543 				number = 0;
1544 				fraction = 0;
1545 			}
1546 			if (number > 100 || (number == 100 && fraction != 0))
1547 			{
1548 				number = 100;
1549 				fraction = 0;
1550 			}
1551 			cmd_exec();
1552 			jump_percent((int) number, fraction);
1553 			break;
1554 
1555 		case A_GOEND:
1556 			/*
1557 			 * Go to line N, default end of file.
1558 			 */
1559 			cmd_exec();
1560 			if (number <= 0)
1561 				jump_forw();
1562 			else
1563 				jump_back(number);
1564 			break;
1565 
1566 		case A_GOEND_BUF:
1567 			/*
1568 			 * Go to line N, default last buffered byte.
1569 			 */
1570 			cmd_exec();
1571 			if (number <= 0)
1572 				jump_forw_buffered();
1573 			else
1574 				jump_back(number);
1575 			break;
1576 
1577 		case A_GOPOS:
1578 			/*
1579 			 * Go to a specified byte position in the file.
1580 			 */
1581 			cmd_exec();
1582 			if (number < 0)
1583 				number = 0;
1584 			jump_line_loc((POSITION) number, jump_sline);
1585 			break;
1586 
1587 		case A_STAT:
1588 			/*
1589 			 * Print file name, etc.
1590 			 */
1591 			if (ch_getflags() & CH_HELPFILE)
1592 				break;
1593 			cmd_exec();
1594 			parg.p_string = eq_message();
1595 			error("%s", &parg);
1596 			break;
1597 
1598 		case A_VERSION:
1599 			/*
1600 			 * Print version number.
1601 			 */
1602 			cmd_exec();
1603 			dispversion();
1604 			break;
1605 
1606 		case A_QUIT:
1607 			/*
1608 			 * Exit.
1609 			 */
1610 			if (curr_ifile != NULL_IFILE &&
1611 			    ch_getflags() & CH_HELPFILE)
1612 			{
1613 				/*
1614 				 * Quit while viewing the help file
1615 				 * just means return to viewing the
1616 				 * previous file.
1617 				 */
1618 				hshift = save_hshift;
1619 				bs_mode = save_bs_mode;
1620 				proc_backspace = save_proc_backspace;
1621 				if (edit_prev(1) == 0)
1622 					break;
1623 			}
1624 			if (extra != NULL)
1625 				quit(*extra);
1626 			quit(QUIT_OK);
1627 			break;
1628 
1629 /*
1630  * Define abbreviation for a commonly used sequence below.
1631  */
1632 #define DO_SEARCH() \
1633 			if (number <= 0) number = 1;    \
1634 			mca_search();                   \
1635 			cmd_exec();                     \
1636 			multi_search((char *)NULL, (int) number, 0);
1637 
1638 		case A_F_SEARCH:
1639 			/*
1640 			 * Search forward for a pattern.
1641 			 * Get the first char of the pattern.
1642 			 */
1643 			search_type = SRCH_FORW | def_search_type;
1644 			if (number <= 0)
1645 				number = 1;
1646 			mca_search();
1647 			c = getcc();
1648 			goto again;
1649 
1650 		case A_B_SEARCH:
1651 			/*
1652 			 * Search backward for a pattern.
1653 			 * Get the first char of the pattern.
1654 			 */
1655 			search_type = SRCH_BACK | def_search_type;
1656 			if (number <= 0)
1657 				number = 1;
1658 			mca_search();
1659 			c = getcc();
1660 			goto again;
1661 
1662 		case A_FILTER:
1663 #if HILITE_SEARCH
1664 			search_type = SRCH_FORW | SRCH_FILTER;
1665 			mca_search();
1666 			c = getcc();
1667 			goto again;
1668 #else
1669 			error("Command not available", NULL_PARG);
1670 			break;
1671 #endif
1672 
1673 		case A_AGAIN_SEARCH:
1674 			/*
1675 			 * Repeat previous search.
1676 			 */
1677 			search_type = last_search_type;
1678 			DO_SEARCH();
1679 			break;
1680 
1681 		case A_T_AGAIN_SEARCH:
1682 			/*
1683 			 * Repeat previous search, multiple files.
1684 			 */
1685 			search_type = last_search_type | SRCH_PAST_EOF;
1686 			DO_SEARCH();
1687 			break;
1688 
1689 		case A_REVERSE_SEARCH:
1690 			/*
1691 			 * Repeat previous search, in reverse direction.
1692 			 */
1693 			save_search_type = search_type = last_search_type;
1694 			search_type = SRCH_REVERSE(search_type);
1695 			DO_SEARCH();
1696 			last_search_type = save_search_type;
1697 			break;
1698 
1699 		case A_T_REVERSE_SEARCH:
1700 			/*
1701 			 * Repeat previous search,
1702 			 * multiple files in reverse direction.
1703 			 */
1704 			save_search_type = search_type = last_search_type;
1705 			search_type = SRCH_REVERSE(search_type) | SRCH_PAST_EOF;
1706 			DO_SEARCH();
1707 			last_search_type = save_search_type;
1708 			break;
1709 
1710 		case A_UNDO_SEARCH:
1711 		case A_CLR_SEARCH:
1712 			/*
1713 			 * Clear search string highlighting.
1714 			 */
1715 			undo_search(action == A_CLR_SEARCH);
1716 			break;
1717 
1718 		case A_HELP:
1719 			/*
1720 			 * Help.
1721 			 */
1722 			if (ch_getflags() & CH_HELPFILE)
1723 				break;
1724 			cmd_exec();
1725 			save_hshift = hshift;
1726 			hshift = 0;
1727 			save_bs_mode = bs_mode;
1728 			bs_mode = BS_SPECIAL;
1729 			save_proc_backspace = proc_backspace;
1730 			proc_backspace = OPT_OFF;
1731 			(void) edit(FAKE_HELPFILE);
1732 			break;
1733 
1734 		case A_EXAMINE:
1735 			/*
1736 			 * Edit a new file.  Get the filename.
1737 			 */
1738 #if EXAMINE
1739 			if (!secure)
1740 			{
1741 				start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1742 				c = getcc();
1743 				goto again;
1744 			}
1745 #endif
1746 			error("Command not available", NULL_PARG);
1747 			break;
1748 
1749 		case A_VISUAL:
1750 			/*
1751 			 * Invoke an editor on the input file.
1752 			 */
1753 #if EDITOR
1754 			if (!secure)
1755 			{
1756 				if (ch_getflags() & CH_HELPFILE)
1757 					break;
1758 				if (strcmp(get_filename(curr_ifile), "-") == 0)
1759 				{
1760 					error("Cannot edit standard input", NULL_PARG);
1761 					break;
1762 				}
1763 				if (get_altfilename(curr_ifile) != NULL)
1764 				{
1765 					error("WARNING: This file was viewed via LESSOPEN",
1766 						NULL_PARG);
1767 				}
1768 				start_mca(A_SHELL, "!", ml_shell, 0);
1769 				/*
1770 				 * Expand the editor prototype string
1771 				 * and pass it to the system to execute.
1772 				 * (Make sure the screen is displayed so the
1773 				 * expansion of "+%lm" works.)
1774 				 */
1775 				make_display();
1776 				cmd_exec();
1777 				lsystem(pr_expand(editproto), (char*)NULL);
1778 				break;
1779 			}
1780 #endif
1781 			error("Command not available", NULL_PARG);
1782 			break;
1783 
1784 		case A_NEXT_FILE:
1785 			/*
1786 			 * Examine next file.
1787 			 */
1788 #if TAGS
1789 			if (ntags())
1790 			{
1791 				error("No next file", NULL_PARG);
1792 				break;
1793 			}
1794 #endif
1795 			if (number <= 0)
1796 				number = 1;
1797 			if (edit_next((int) number))
1798 			{
1799 				if (get_quit_at_eof() && eof_displayed() &&
1800 				    !(ch_getflags() & CH_HELPFILE))
1801 					quit(QUIT_OK);
1802 				parg.p_string = (number > 1) ? "(N-th) " : "";
1803 				error("No %snext file", &parg);
1804 			}
1805 			break;
1806 
1807 		case A_PREV_FILE:
1808 			/*
1809 			 * Examine previous file.
1810 			 */
1811 #if TAGS
1812 			if (ntags())
1813 			{
1814 				error("No previous file", NULL_PARG);
1815 				break;
1816 			}
1817 #endif
1818 			if (number <= 0)
1819 				number = 1;
1820 			if (edit_prev((int) number))
1821 			{
1822 				parg.p_string = (number > 1) ? "(N-th) " : "";
1823 				error("No %sprevious file", &parg);
1824 			}
1825 			break;
1826 
1827 		case A_NEXT_TAG:
1828 			/*
1829 			 * Jump to the next tag in the current tag list.
1830 			 */
1831 #if TAGS
1832 			if (number <= 0)
1833 				number = 1;
1834 			tagfile = nexttag((int) number);
1835 			if (tagfile == NULL)
1836 			{
1837 				error("No next tag", NULL_PARG);
1838 				break;
1839 			}
1840 			cmd_exec();
1841 			if (edit(tagfile) == 0)
1842 			{
1843 				POSITION pos = tagsearch();
1844 				if (pos != NULL_POSITION)
1845 					jump_loc(pos, jump_sline);
1846 			}
1847 #else
1848 			error("Command not available", NULL_PARG);
1849 #endif
1850 			break;
1851 
1852 		case A_PREV_TAG:
1853 			/*
1854 			 * Jump to the previous tag in the current tag list.
1855 			 */
1856 #if TAGS
1857 			if (number <= 0)
1858 				number = 1;
1859 			tagfile = prevtag((int) number);
1860 			if (tagfile == NULL)
1861 			{
1862 				error("No previous tag", NULL_PARG);
1863 				break;
1864 			}
1865 			cmd_exec();
1866 			if (edit(tagfile) == 0)
1867 			{
1868 				POSITION pos = tagsearch();
1869 				if (pos != NULL_POSITION)
1870 					jump_loc(pos, jump_sline);
1871 			}
1872 #else
1873 			error("Command not available", NULL_PARG);
1874 #endif
1875 			break;
1876 
1877 		case A_INDEX_FILE:
1878 			/*
1879 			 * Examine a particular file.
1880 			 */
1881 			if (number <= 0)
1882 				number = 1;
1883 			if (edit_index((int) number))
1884 				error("No such file", NULL_PARG);
1885 			break;
1886 
1887 		case A_REMOVE_FILE:
1888 			/*
1889 			 * Remove a file from the input file list.
1890 			 */
1891 			if (ch_getflags() & CH_HELPFILE)
1892 				break;
1893 			old_ifile = curr_ifile;
1894 			new_ifile = getoff_ifile(curr_ifile);
1895 			if (new_ifile == NULL_IFILE)
1896 			{
1897 				bell();
1898 				break;
1899 			}
1900 			if (edit_ifile(new_ifile) != 0)
1901 			{
1902 				reedit_ifile(old_ifile);
1903 				break;
1904 			}
1905 			del_ifile(old_ifile);
1906 			break;
1907 
1908 		case A_OPT_TOGGLE:
1909 			/*
1910 			 * Change the setting of an  option.
1911 			 */
1912 			optflag = OPT_TOGGLE;
1913 			optgetname = FALSE;
1914 			mca_opt_toggle();
1915 			c = getcc();
1916 			cbuf = opt_toggle_disallowed(c);
1917 			if (cbuf != NULL)
1918 			{
1919 				error(cbuf, NULL_PARG);
1920 				break;
1921 			}
1922 			goto again;
1923 
1924 		case A_DISP_OPTION:
1925 			/*
1926 			 * Report the setting of an option.
1927 			 */
1928 			optflag = OPT_NO_TOGGLE;
1929 			optgetname = FALSE;
1930 			mca_opt_toggle();
1931 			c = getcc();
1932 			goto again;
1933 
1934 		case A_FIRSTCMD:
1935 			/*
1936 			 * Set an initial command for new files.
1937 			 */
1938 			start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1939 			c = getcc();
1940 			goto again;
1941 
1942 		case A_SHELL:
1943 		case A_PSHELL:
1944 			/*
1945 			 * Shell escape.
1946 			 */
1947 #if SHELL_ESCAPE
1948 			if (!secure)
1949 			{
1950 				start_mca(action, (action == A_SHELL) ? "!" : "#", ml_shell, 0);
1951 				c = getcc();
1952 				goto again;
1953 			}
1954 #endif
1955 			error("Command not available", NULL_PARG);
1956 			break;
1957 
1958 		case A_SETMARK:
1959 		case A_SETMARKBOT:
1960 			/*
1961 			 * Set a mark.
1962 			 */
1963 			if (ch_getflags() & CH_HELPFILE)
1964 				break;
1965 			start_mca(A_SETMARK, "set mark: ", (void*)NULL, 0);
1966 			c = getcc();
1967 			if (is_erase_char(c) || is_newline_char(c))
1968 				break;
1969 			setmark(c, action == A_SETMARKBOT ? BOTTOM : TOP);
1970 			repaint();
1971 			break;
1972 
1973 		case A_CLRMARK:
1974 			/*
1975 			 * Clear a mark.
1976 			 */
1977 			start_mca(A_CLRMARK, "clear mark: ", (void*)NULL, 0);
1978 			c = getcc();
1979 			if (is_erase_char(c) || is_newline_char(c))
1980 				break;
1981 			clrmark(c);
1982 			repaint();
1983 			break;
1984 
1985 		case A_GOMARK:
1986 			/*
1987 			 * Jump to a marked position.
1988 			 */
1989 			start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1990 			c = getcc();
1991 			if (is_erase_char(c) || is_newline_char(c))
1992 				break;
1993 			cmd_exec();
1994 			gomark(c);
1995 			break;
1996 
1997 		case A_PIPE:
1998 			/*
1999 			 * Write part of the input to a pipe to a shell command.
2000 			 */
2001 #if PIPEC
2002 			if (!secure)
2003 			{
2004 				start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
2005 				c = getcc();
2006 				if (is_erase_char(c))
2007 					break;
2008 				if (is_newline_char(c))
2009 					c = '.';
2010 				if (badmark(c))
2011 					break;
2012 				pipec = c;
2013 				start_mca(A_PIPE, "!", ml_shell, 0);
2014 				c = getcc();
2015 				goto again;
2016 			}
2017 #endif
2018 			error("Command not available", NULL_PARG);
2019 			break;
2020 
2021 		case A_B_BRACKET:
2022 		case A_F_BRACKET:
2023 			start_mca(action, "Brackets: ", (void*)NULL, 0);
2024 			c = getcc();
2025 			goto again;
2026 
2027 		case A_LSHIFT:
2028 			/*
2029 			 * Shift view left.
2030 			 */
2031 			if (number > 0)
2032 				shift_count = number;
2033 			else
2034 				number = (shift_count > 0) ?
2035 					shift_count : sc_width / 2;
2036 			if (number > hshift)
2037 				number = hshift;
2038 			hshift -= number;
2039 			screen_trashed = 1;
2040 			break;
2041 
2042 		case A_RSHIFT:
2043 			/*
2044 			 * Shift view right.
2045 			 */
2046 			if (number > 0)
2047 				shift_count = number;
2048 			else
2049 				number = (shift_count > 0) ?
2050 					shift_count : sc_width / 2;
2051 			hshift += number;
2052 			screen_trashed = 1;
2053 			break;
2054 
2055 		case A_LLSHIFT:
2056 			/*
2057 			 * Shift view left to margin.
2058 			 */
2059 			hshift = 0;
2060 			screen_trashed = 1;
2061 			break;
2062 
2063 		case A_RRSHIFT:
2064 			/*
2065 			 * Shift view right to view rightmost char on screen.
2066 			 */
2067 			hshift = rrshift();
2068 			screen_trashed = 1;
2069 			break;
2070 
2071 		case A_PREFIX:
2072 			/*
2073 			 * The command is incomplete (more chars are needed).
2074 			 * Display the current char, so the user knows
2075 			 * what's going on, and get another character.
2076 			 */
2077 			if (mca != A_PREFIX)
2078 			{
2079 				cmd_reset();
2080 				start_mca(A_PREFIX, " ", (void*)NULL,
2081 					CF_QUIT_ON_ERASE);
2082 				(void) cmd_char(c);
2083 			}
2084 			c = getcc();
2085 			goto again;
2086 
2087 		case A_NOACTION:
2088 			break;
2089 
2090 		default:
2091 			bell();
2092 			break;
2093 		}
2094 	}
2095 }
2096