2 * Copyright (C) 1984-2023 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * User-level command processor.
16 #if MSDOS_COMPILER==WIN32C
23 extern int erase_char, erase2_char, kill_char;
25 extern int quit_if_one_screen;
26 extern int one_screen;
32 extern int jump_sline;
35 extern int top_scroll;
36 extern int ignore_eoi;
40 extern int proc_backspace;
42 extern int status_col;
43 extern POSITION highest_hilite;
44 extern POSITION start_attnpos;
45 extern POSITION end_attnpos;
46 extern char *every_first_cmd;
47 extern char version[];
48 extern struct scrpos initial_scrpos;
49 extern IFILE curr_ifile;
50 extern void *ml_search;
51 extern void *ml_examine;
52 extern int wheel_lines;
53 extern int header_lines;
54 extern int def_search_type;
55 extern int updown_match;
56 #if SHELL_ESCAPE || PIPEC
57 extern void *ml_shell;
61 extern char *editproto;
63 extern int screen_trashed; /* The screen has been overwritten */
64 extern int shift_count;
66 extern int forw_prompt;
67 extern int incr_search;
68 extern int full_screen;
69 #if MSDOS_COMPILER==WIN32C
74 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
76 static int mca; /* The multicharacter command (action) */
77 static int search_type; /* The previous type of search */
78 static int last_search_type; /* Type of last executed search */
79 static LINENUM number; /* The number typed by the user */
80 static long fraction; /* The fractional part of the number */
81 static struct loption *curropt;
84 static int optgetname;
85 static POSITION bottompos;
86 static int save_hshift;
87 static int save_bs_mode;
88 static int save_proc_backspace;
93 /* Stack of ungotten chars (via ungetcc) */
95 struct ungot *ug_next;
98 static struct ungot* ungot = NULL;
100 static void multi_search (char *pattern, int n, int silent);
103 * Move the cursor to start of prompt line before executing a command.
104 * This looks nicer if the command takes a long time before
105 * updating the screen.
107 static void cmd_exec(void)
115 * Indicate we are reading a multi-character command.
117 static void set_mca(int action)
125 * Indicate we are not reading a multi-character command.
127 static void clear_mca(void)
135 * Set up the display to start a new multi-character command.
137 static void start_mca(int action, constant char *prompt, void *mlist, int cmdflags)
141 set_mlist(mlist, cmdflags);
144 public int in_mca(void)
146 return (mca != 0 && mca != A_PREFIX);
150 * Set up the display to start a new search command.
152 static void mca_search1(void)
157 if (search_type & SRCH_FILTER)
161 if (search_type & SRCH_FORW)
166 if (search_type & SRCH_NO_MATCH)
167 cmd_putstr("Non-match ");
168 if (search_type & SRCH_FIRST_FILE)
169 cmd_putstr("First-file ");
170 if (search_type & SRCH_PAST_EOF)
171 cmd_putstr("EOF-ignore ");
172 if (search_type & SRCH_NO_MOVE)
173 cmd_putstr("Keep-pos ");
174 if (search_type & SRCH_NO_REGEX)
175 cmd_putstr("Regex-off ");
176 if (search_type & SRCH_WRAP)
178 for (i = 1; i <= NUM_SEARCH_COLORS; i++)
180 if (search_type & SRCH_SUBSEARCH(i))
182 char buf[INT_STRLEN_BOUND(int)+8];
183 SNPRINTF1(buf, sizeof(buf), "Sub-%d ", i);
189 if (search_type & SRCH_FILTER)
193 if (search_type & SRCH_FORW)
200 static void mca_search(void)
203 set_mlist(ml_search, 0);
207 * Set up the display to start a new toggle-option command.
209 static void mca_opt_toggle(void)
215 no_prompt = (optflag & OPT_NO_PROMPT);
216 flag = (optflag & ~OPT_NO_PROMPT);
217 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
219 set_mca(A_OPT_TOGGLE);
239 * Execute a multicharacter command.
241 static void exec_mca(void)
254 multi_search(cbuf, (int) number, 0);
258 search_type ^= SRCH_NO_MATCH;
259 set_filter_pattern(cbuf, search_type);
264 * Skip leading spaces or + signs in the string.
266 while (*cbuf == '+' || *cbuf == ' ')
268 if (every_first_cmd != NULL)
269 free(every_first_cmd);
271 every_first_cmd = NULL;
273 every_first_cmd = save(cbuf);
276 toggle_option(curropt, opt_lower, cbuf, optflag);
280 match_brac(cbuf[0], cbuf[1], 1, (int) number);
283 match_brac(cbuf[1], cbuf[0], 0, (int) number);
291 /* If tag structure is loaded then clean it up. */
299 * !! just uses whatever is in shellcmd.
300 * Otherwise, copy cmdbuf to shellcmd,
301 * expanding any special characters ("%" or "#").
305 if (shellcmd != NULL)
307 shellcmd = fexpand(cbuf);
312 if (shellcmd == NULL)
313 lsystem("", "!done");
315 lsystem(shellcmd, "!done");
320 lsystem(pr_expand(cbuf), "#done");
327 (void) pipe_mark(pipec, cbuf);
328 error("|done", NULL_PARG);
335 * Is a character an erase or kill char?
337 static int is_erase_char(int c)
339 return (c == erase_char || c == erase2_char || c == kill_char);
343 * Is a character a carriage return or newline?
345 static int is_newline_char(int c)
347 return (c == '\n' || c == '\r');
351 * Handle the first char of an option (after the initial dash).
353 static int mca_opt_first_char(int c)
355 int no_prompt = (optflag & OPT_NO_PROMPT);
356 int flag = (optflag & ~OPT_NO_PROMPT);
357 if (flag == OPT_NO_TOGGLE)
362 /* "__" = long option name. */
373 optflag = no_prompt | ((flag == OPT_UNSET) ?
374 OPT_TOGGLE : OPT_UNSET);
379 optflag = no_prompt | ((flag == OPT_SET) ?
380 OPT_TOGGLE : OPT_SET);
384 optflag ^= OPT_NO_PROMPT;
388 /* "--" = long option name. */
394 /* Char was not handled here. */
399 * Add a char to a long option name.
400 * See if we've got a match for an option name yet.
401 * If so, display the complete name and stop
402 * accepting chars until user hits RETURN.
404 static int mca_opt_nonfirst_char(int c)
413 * Already have a match for the name.
414 * Don't accept anything but erase/kill.
416 if (is_erase_char(c))
421 * Add char to cmd buffer and try to match
424 if (cmd_char(c) == CC_QUIT)
429 opt_lower = ASCII_IS_LOWER(p[0]);
431 curropt = findopt_name(&p, &oname, &err);
436 * Remember the option and
437 * display the full option name.
441 for (p = oname; *p != '\0'; p++)
444 if (!opt_lower && ASCII_IS_LOWER(c))
445 c = ASCII_TO_UPPER(c);
446 if (cmd_char(c) != CC_OK)
449 } else if (err != OPT_AMBIG)
457 * Handle a char of an option toggle command.
459 static int mca_opt_char(int c)
464 * This may be a short option (single char),
465 * or one char of a long option name,
466 * or one char of the option parameter.
468 if (curropt == NULL && len_cmdbuf() == 0)
470 int ret = mca_opt_first_char(c);
476 /* We're getting a long option name. */
477 if (!is_newline_char(c) && c != '=')
478 return (mca_opt_nonfirst_char(c));
481 parg.p_string = get_cmdbuf();
482 if (parg.p_string == NULL)
484 error("There is no --%s option", &parg);
491 if (is_erase_char(c))
494 /* We're getting the option parameter. */
496 curropt = findopt(c);
499 parg.p_string = propt(c);
500 error("There is no %s option", &parg);
503 opt_lower = ASCII_IS_LOWER(c);
506 * If the option which was entered does not take a
507 * parameter, toggle the option immediately,
508 * so user doesn't have to hit RETURN.
510 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
511 !opt_has_param(curropt))
513 toggle_option(curropt, opt_lower, "", optflag);
517 * Display a prompt appropriate for the option parameter.
519 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
524 * Normalize search type.
526 public int norm_search_type(int st)
528 /* WRAP and PAST_EOF are mutually exclusive. */
529 if ((st & (SRCH_PAST_EOF|SRCH_WRAP)) == (SRCH_PAST_EOF|SRCH_WRAP))
535 * Handle a char of a search command.
537 static int mca_search_char(int c)
542 * Certain characters as the first char of
543 * the pattern have special meaning:
544 * ! Toggle the NO_MATCH flag
545 * * Toggle the PAST_EOF flag
546 * @ Toggle the FIRST_FILE flag
548 if (len_cmdbuf() > 0)
553 case CONTROL('E'): /* ignore END of file */
556 flag = SRCH_PAST_EOF;
557 search_type &= ~SRCH_WRAP;
559 case CONTROL('F'): /* FIRST file */
562 flag = SRCH_FIRST_FILE;
564 case CONTROL('K'): /* KEEP position */
568 case CONTROL('S'): { /* SUBSEARCH */
569 char buf[INT_STRLEN_BOUND(int)+24];
570 SNPRINTF1(buf, sizeof(buf), "Sub-pattern (1-%d):", NUM_SEARCH_COLORS);
575 if (c >= '1' && c <= '0'+NUM_SEARCH_COLORS)
576 flag = SRCH_SUBSEARCH(c-'0');
578 flag = -1; /* calls mca_search() below to repaint */
580 case CONTROL('W'): /* WRAP around */
584 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
585 flag = SRCH_NO_REGEX;
587 case CONTROL('N'): /* NOT match */
589 flag = SRCH_NO_MATCH;
596 search_type = norm_search_type(search_type ^ flag);
604 * Handle a character of a multi-character command.
606 static int mca_char(int c)
614 * We're not in a multicharacter command.
620 * In the prefix of a command.
621 * This not considered a multichar command
622 * (even tho it uses cmdbuf, etc.).
623 * It is handled in the commands() switch.
629 * Entering digits of a number.
630 * Terminated by a non-digit.
632 if ((c >= '0' && c <= '9') || c == '.')
634 switch (editchar(c, ECF_PEEK|ECF_NOHISTORY|ECF_NOCOMPLETE|ECF_NORIGHTLEFT))
638 * Ignore this char and get another one.
643 * Not part of the number.
644 * End the number and treat this char
645 * as a normal command character.
647 number = cmd_int(&fraction);
655 ret = mca_opt_char(c);
663 ret = mca_search_char(c);
669 /* Other multicharacter command. */
674 * The multichar command is terminated by a newline.
676 if (is_newline_char(c))
679 * Execute the command.
686 * Append the char to the command buffer.
688 if (cmd_char(c) == CC_QUIT)
690 * Abort the multi-char command.
698 if (len_cmdbuf() >= 2)
701 * Special case for the bracket-matching commands.
702 * Execute the command after getting exactly two
703 * characters from the user.
713 /* Incremental search: do a search after every input char. */
714 int st = (search_type & (SRCH_FORW|SRCH_BACK|SRCH_NO_MATCH|SRCH_NO_REGEX|SRCH_NO_MOVE|SRCH_WRAP|SRCH_SUBSEARCH_ALL));
715 char *pattern = get_cmdbuf();
719 * Must save updown_match because mca_search
720 * reinits it. That breaks history scrolling.
721 * {{ This is ugly. mca_search probably shouldn't call set_mlist. }}
723 int save_updown_match = updown_match;
725 if (*pattern == '\0')
727 /* User has backspaced to an empty pattern. */
731 if (search(st | SRCH_INCR, pattern, 1) != 0)
732 /* No match, invalid pattern, etc. */
735 /* Redraw the search prompt and search string. */
742 updown_match = save_updown_match;
749 * Need another character.
755 * Discard any buffered file data.
757 static void clear_buffers(void)
759 if (!(ch_getflags() & CH_CANSEEK))
769 * Make sure the screen is displayed.
771 static void make_display(void)
774 * If not full_screen, we can't rely on scrolling to fill the screen.
775 * We need to clear and repaint screen before any change.
777 if (!full_screen && !(quit_if_one_screen && one_screen))
780 * If nothing is displayed yet, display starting from initial_scrpos.
784 if (initial_scrpos.pos == NULL_POSITION)
785 jump_loc(ch_zero(), 1);
787 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
788 } else if (screen_trashed || !full_screen)
790 int save_top_scroll = top_scroll;
791 int save_ignore_eoi = ignore_eoi;
794 if (screen_trashed == 2)
796 /* Special case used by ignore_eoi: re-open the input file
797 * and jump to the end of the file. */
802 top_scroll = save_top_scroll;
803 ignore_eoi = save_ignore_eoi;
808 * Display the appropriate prompt.
810 static void prompt(void)
814 if (ungot != NULL && ungot->ug_char != CHAR_END_COMMAND)
817 * No prompt necessary if commands are from
818 * ungotten chars rather than from the user.
824 * Make sure the screen is displayed.
827 bottompos = position(BOTTOM_PLUS_ONE);
830 * If we've hit EOF on the last file and the -E flag is set, quit.
832 if (get_quit_at_eof() == OPT_ONPLUS &&
833 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
834 next_ifile(curr_ifile) == NULL_IFILE)
838 * If the entire file is displayed and the -F flag is set, quit.
840 if (quit_if_one_screen &&
841 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
842 next_ifile(curr_ifile) == NULL_IFILE)
844 quit_if_one_screen = FALSE; /* only get one chance at this */
846 #if MSDOS_COMPILER==WIN32C
848 * In Win32, display the file name in the window title.
850 if (!(ch_getflags() & CH_HELPFILE))
852 WCHAR w[MAX_PATH+16];
853 p = pr_expand("Less?f - %f.");
854 MultiByteToWideChar(CP_ACP, 0, p, -1, w, sizeof(w)/sizeof(*w));
860 * Select the proper prompt and display it.
863 * If the previous action was a forward movement,
864 * don't clear the bottom line of the display;
865 * just print the prompt since the forward movement guarantees
866 * that we're in the right position to display the prompt.
867 * Clearing the line could cause a problem: for example, if the last
868 * line displayed ended at the right screen edge without a newline,
869 * then clearing would clear the last displayed line rather than
881 if (p == NULL || *p == '\0')
883 at_enter(AT_NORMAL|AT_COLOR_PROMPT);
888 #if MSDOS_COMPILER==WIN32C
891 MultiByteToWideChar(CP_ACP, 0, p, -1, w, sizeof(w)/sizeof(*w));
892 WideCharToMultiByte(utf_mode ? CP_UTF8 : GetConsoleOutputCP(),
893 0, w, -1, a, sizeof(a), NULL, NULL);
903 * Display the less version message.
905 public void dispversion(void)
909 parg.p_string = version;
910 error("less %s", &parg);
914 * Return a character to complete a partial command, if possible.
916 static LWCHAR getcc_end_command(void)
921 /* We have a number but no command. Treat as #g. */
926 /* We have "/string" but no newline. Add the \n. */
929 /* Some other incomplete command. Let user complete it. */
930 return ((ungot == NULL) ? getchr() : 0);
935 * Get command character.
936 * The character normally comes from the keyboard,
937 * but may come from ungotten characters
938 * (characters previously given to ungetcc or ungetsc).
940 static LWCHAR getccu(void)
947 /* Normal case: no ungotten chars.
948 * Get char from the user. */
952 /* Ungotten chars available:
953 * Take the top of stack (most recent). */
954 struct ungot *ug = ungot;
959 if (c == CHAR_END_COMMAND)
960 c = getcc_end_command();
967 * Get a command character, but if we receive the orig sequence,
968 * convert it to the repl sequence.
970 static LWCHAR getcc_repl(char constant *orig, char constant *repl, LWCHAR (*gr_getc)(void), void (*gr_ungetc)(LWCHAR))
977 if (orig == NULL || orig[0] == '\0')
982 if (c != orig[ki] || ki >= sizeof(keys)-1)
984 /* This is not orig we have been receiving.
985 * If we have stashed chars in keys[],
986 * unget them and return the first one. */
988 (*gr_ungetc)(keys[ki--]);
991 if (orig[++ki] == '\0')
993 /* We've received the full orig sequence.
994 * Return the repl sequence. */
997 (*gr_ungetc)(repl[ki--]);
1000 /* We've received a partial orig sequence (ki chars of it).
1001 * Get next char and see if it continues to match orig. */
1007 * Get command character.
1009 public int getcc(void)
1011 /* Replace kent (keypad Enter) with a newline. */
1012 return getcc_repl(kent, "\n", getccu, ungetcc);
1016 * "Unget" a command character.
1017 * The next getcc() will return this character.
1019 public void ungetcc(LWCHAR c)
1021 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
1024 ug->ug_next = ungot;
1029 * "Unget" a command character.
1030 * If any other chars are already ungotten, put this one after those.
1032 public void ungetcc_back(LWCHAR c)
1034 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
1042 for (pu = ungot; pu->ug_next != NULL; pu = pu->ug_next)
1049 * Unget a whole string of command characters.
1050 * The next sequence of getcc()'s will return this string.
1052 public void ungetsc(char *s)
1059 * Peek the next command character, without consuming it.
1061 public LWCHAR peekcc(void)
1069 * Search for a pattern, possibly in multiple files.
1070 * If SRCH_FIRST_FILE is set, begin searching at the first file.
1071 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
1073 static void multi_search(char *pattern, int n, int silent)
1080 save_ifile = save_curr_ifile();
1082 if ((search_type & (SRCH_FORW|SRCH_BACK)) == 0)
1083 search_type |= SRCH_FORW;
1084 if (search_type & SRCH_FIRST_FILE)
1087 * Start at the first (or last) file
1088 * in the command line list.
1090 if (search_type & SRCH_FORW)
1091 nomore = edit_first();
1093 nomore = edit_last();
1096 unsave_ifile(save_ifile);
1100 search_type &= ~SRCH_FIRST_FILE;
1105 n = search(search_type, pattern, n);
1107 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
1108 * after being used once. This allows "n" to work after
1109 * using a /@@ search.
1111 search_type &= ~SRCH_NO_MOVE;
1112 last_search_type = search_type;
1118 unsave_ifile(save_ifile);
1124 * Some kind of error in the search.
1125 * Error message has been printed by search().
1129 if ((search_type & SRCH_PAST_EOF) == 0)
1131 * We didn't find a match, but we're
1132 * supposed to search only one file.
1136 * Move on to the next file.
1138 if (search_type & SRCH_FORW)
1139 nomore = edit_next(1);
1141 nomore = edit_prev(1);
1149 * Print an error message if we haven't already.
1151 if (n > 0 && !silent)
1152 error("Pattern not found", NULL_PARG);
1157 * Restore the file we were originally viewing.
1159 reedit_ifile(save_ifile);
1162 unsave_ifile(save_ifile);
1167 * Forward forever, or until a highlighted line appears.
1169 static int forw_loop(int until_hilite)
1173 if (ch_getflags() & CH_HELPFILE)
1174 return (A_NOACTION);
1177 jump_forw_buffered();
1178 curr_len = ch_length();
1179 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
1183 if (until_hilite && highest_hilite > curr_len)
1195 * This gets us back in "F mode" after processing
1196 * a non-abort signal (e.g. window-change).
1198 if (sigs && !ABORT_SIGS())
1199 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
1201 return (A_NOACTION);
1205 * Main command processor.
1206 * Accept and execute commands until a quit command.
1208 public void commands(void)
1214 int save_jump_sline;
1215 int save_search_type;
1223 search_type = SRCH_FORW;
1224 wscroll = (sc_height + 1) / 2;
1225 newaction = A_NOACTION;
1235 * See if any signals need processing.
1241 quit(QUIT_SAVED_STATUS);
1245 * See if window size changed, for systems that don't
1246 * generate SIGWINCH.
1251 * Display prompt and accept a character.
1257 if (newaction == A_NOACTION)
1264 if (newaction != A_NOACTION)
1267 newaction = A_NOACTION;
1271 * If we are in a multicharacter command, call mca_char.
1272 * Otherwise we call fcmd_decode to determine the
1273 * action to be performed.
1276 switch (mca_char(c))
1280 * Need another character.
1286 * Command has been handled by mca_char.
1287 * Start clean with a prompt.
1292 * Not a multi-char command
1293 * (at least, not anymore).
1299 * Decode the command character and decide what to do.
1304 * We're in a multichar command.
1305 * Add the character to the command buffer
1306 * and display it on the screen.
1307 * If the user backspaces past the start
1308 * of the line, abort the command.
1310 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1312 cbuf = get_cmdbuf();
1318 * Don't use cmd_char if we're starting fresh
1319 * at the beginning of a command, because we
1320 * don't want to echo the command until we know
1321 * it is a multichar command. We also don't
1322 * want erase_char/kill_char to be treated
1323 * as line editing characters.
1330 action = fcmd_decode(cbuf, &extra);
1332 * If an "extra" string was returned,
1333 * process it as a string of command characters.
1339 * Clear the cmdbuf string.
1340 * (But not if we're in the prefix of a command,
1341 * because the partial command string is kept there.)
1343 if (action != A_PREFIX)
1350 * First digit of a number.
1352 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1357 * Forward one window (and set the window size).
1360 swindow = (int) number;
1364 * Forward one screen.
1367 number = get_swindow();
1370 set_attnpos(bottompos);
1371 forward((int) number, 0, 1);
1376 * Backward one window (and set the window size).
1379 swindow = (int) number;
1383 * Backward one screen.
1386 number = get_swindow();
1388 backward((int) number, 0, 1);
1393 * Forward N (default 1) line.
1398 if (show_attn == OPT_ONPLUS && number > 1)
1399 set_attnpos(bottompos);
1400 forward((int) number, 0, 0);
1405 * Backward N (default 1) line.
1410 backward((int) number, 0, 0);
1415 * Forward wheel_lines lines.
1418 forward(wheel_lines, 0, 0);
1423 * Backward wheel_lines lines.
1426 backward(wheel_lines, 0, 0);
1431 * Force forward N (default 1) line.
1436 if (show_attn == OPT_ONPLUS && number > 1)
1437 set_attnpos(bottompos);
1438 forward((int) number, 1, 0);
1443 * Force backward N (default 1) line.
1448 backward((int) number, 1, 0);
1453 * Force forward one screen.
1456 number = get_swindow();
1458 if (show_attn == OPT_ONPLUS)
1459 set_attnpos(bottompos);
1460 forward((int) number, 1, 0);
1465 * Forward forever, ignoring EOF.
1468 set_attnpos(bottompos);
1469 newaction = forw_loop(0);
1472 case A_F_UNTIL_HILITE:
1473 newaction = forw_loop(1);
1479 * (default same as last 'd' or 'u' command).
1482 wscroll = (int) number;
1484 if (show_attn == OPT_ONPLUS)
1485 set_attnpos(bottompos);
1486 forward(wscroll, 0, 0);
1492 * (default same as last 'd' or 'u' command).
1495 wscroll = (int) number;
1497 backward(wscroll, 0, 0);
1502 * Flush buffers, then repaint screen.
1503 * Don't flush the buffers on a pipe!
1517 * Go to line N, default beginning of file.
1518 * If N <= 0, ignore jump_sline in order to avoid
1519 * empty lines before the beginning of the file.
1521 save_jump_sline = jump_sline;
1529 jump_sline = save_jump_sline;
1534 * Go to a specified percentage into the file.
1541 if (number > 100 || (number == 100 && fraction != 0))
1547 jump_percent((int) number, fraction);
1552 * Go to line N, default end of file.
1563 * Go to line N, default last buffered byte.
1567 jump_forw_buffered();
1574 * Go to a specified byte position in the file.
1579 jump_line_loc((POSITION) number, jump_sline);
1584 * Print file name, etc.
1586 if (ch_getflags() & CH_HELPFILE)
1589 parg.p_string = eq_message();
1595 * Print version number.
1605 if (curr_ifile != NULL_IFILE &&
1606 ch_getflags() & CH_HELPFILE)
1609 * Quit while viewing the help file
1610 * just means return to viewing the
1613 hshift = save_hshift;
1614 bs_mode = save_bs_mode;
1615 proc_backspace = save_proc_backspace;
1616 if (edit_prev(1) == 0)
1625 * Define abbreviation for a commonly used sequence below.
1627 #define DO_SEARCH() \
1628 if (number <= 0) number = 1; \
1631 multi_search((char *)NULL, (int) number, 0);
1635 * Search forward for a pattern.
1636 * Get the first char of the pattern.
1638 search_type = SRCH_FORW | def_search_type;
1647 * Search backward for a pattern.
1648 * Get the first char of the pattern.
1650 search_type = SRCH_BACK | def_search_type;
1659 search_type = SRCH_FORW | SRCH_FILTER;
1664 error("Command not available", NULL_PARG);
1668 case A_AGAIN_SEARCH:
1670 * Repeat previous search.
1672 search_type = last_search_type;
1676 case A_T_AGAIN_SEARCH:
1678 * Repeat previous search, multiple files.
1680 search_type = last_search_type | SRCH_PAST_EOF;
1684 case A_REVERSE_SEARCH:
1686 * Repeat previous search, in reverse direction.
1688 save_search_type = search_type = last_search_type;
1689 search_type = SRCH_REVERSE(search_type);
1691 last_search_type = save_search_type;
1694 case A_T_REVERSE_SEARCH:
1696 * Repeat previous search,
1697 * multiple files in reverse direction.
1699 save_search_type = search_type = last_search_type;
1700 search_type = SRCH_REVERSE(search_type) | SRCH_PAST_EOF;
1702 last_search_type = save_search_type;
1708 * Clear search string highlighting.
1710 undo_search(action == A_CLR_SEARCH);
1717 if (ch_getflags() & CH_HELPFILE)
1720 save_hshift = hshift;
1722 save_bs_mode = bs_mode;
1723 bs_mode = BS_SPECIAL;
1724 save_proc_backspace = proc_backspace;
1725 proc_backspace = OPT_OFF;
1726 (void) edit(FAKE_HELPFILE);
1731 * Edit a new file. Get the filename.
1736 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1741 error("Command not available", NULL_PARG);
1746 * Invoke an editor on the input file.
1751 if (ch_getflags() & CH_HELPFILE)
1753 if (strcmp(get_filename(curr_ifile), "-") == 0)
1755 error("Cannot edit standard input", NULL_PARG);
1758 if (get_altfilename(curr_ifile) != NULL)
1760 error("WARNING: This file was viewed via LESSOPEN",
1763 start_mca(A_SHELL, "!", ml_shell, 0);
1765 * Expand the editor prototype string
1766 * and pass it to the system to execute.
1767 * (Make sure the screen is displayed so the
1768 * expansion of "+%lm" works.)
1772 lsystem(pr_expand(editproto), (char*)NULL);
1776 error("Command not available", NULL_PARG);
1781 * Examine next file.
1786 error("No next file", NULL_PARG);
1792 if (edit_next((int) number))
1794 if (get_quit_at_eof() && eof_displayed() &&
1795 !(ch_getflags() & CH_HELPFILE))
1797 parg.p_string = (number > 1) ? "(N-th) " : "";
1798 error("No %snext file", &parg);
1804 * Examine previous file.
1809 error("No previous file", NULL_PARG);
1815 if (edit_prev((int) number))
1817 parg.p_string = (number > 1) ? "(N-th) " : "";
1818 error("No %sprevious file", &parg);
1824 * Jump to the next tag in the current tag list.
1829 tagfile = nexttag((int) number);
1830 if (tagfile == NULL)
1832 error("No next tag", NULL_PARG);
1836 if (edit(tagfile) == 0)
1838 POSITION pos = tagsearch();
1839 if (pos != NULL_POSITION)
1840 jump_loc(pos, jump_sline);
1843 error("Command not available", NULL_PARG);
1849 * Jump to the previous tag in the current tag list.
1854 tagfile = prevtag((int) number);
1855 if (tagfile == NULL)
1857 error("No previous tag", NULL_PARG);
1861 if (edit(tagfile) == 0)
1863 POSITION pos = tagsearch();
1864 if (pos != NULL_POSITION)
1865 jump_loc(pos, jump_sline);
1868 error("Command not available", NULL_PARG);
1874 * Examine a particular file.
1878 if (edit_index((int) number))
1879 error("No such file", NULL_PARG);
1884 * Remove a file from the input file list.
1886 if (ch_getflags() & CH_HELPFILE)
1888 old_ifile = curr_ifile;
1889 new_ifile = getoff_ifile(curr_ifile);
1890 if (new_ifile == NULL_IFILE)
1895 if (edit_ifile(new_ifile) != 0)
1897 reedit_ifile(old_ifile);
1900 del_ifile(old_ifile);
1905 * Change the setting of an option.
1907 optflag = OPT_TOGGLE;
1911 cbuf = opt_toggle_disallowed(c);
1914 error(cbuf, NULL_PARG);
1921 * Report the setting of an option.
1923 optflag = OPT_NO_TOGGLE;
1931 * Set an initial command for new files.
1933 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1945 start_mca(action, (action == A_SHELL) ? "!" : "#", ml_shell, 0);
1950 error("Command not available", NULL_PARG);
1958 if (ch_getflags() & CH_HELPFILE)
1960 start_mca(A_SETMARK, "set mark: ", (void*)NULL, 0);
1962 if (is_erase_char(c) || is_newline_char(c))
1964 setmark(c, action == A_SETMARKBOT ? BOTTOM : TOP);
1972 start_mca(A_CLRMARK, "clear mark: ", (void*)NULL, 0);
1974 if (is_erase_char(c) || is_newline_char(c))
1982 * Jump to a marked position.
1984 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1986 if (is_erase_char(c) || is_newline_char(c))
1994 * Write part of the input to a pipe to a shell command.
1999 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
2001 if (is_erase_char(c))
2003 if (is_newline_char(c))
2008 start_mca(A_PIPE, "!", ml_shell, 0);
2013 error("Command not available", NULL_PARG);
2018 start_mca(action, "Brackets: ", (void*)NULL, 0);
2027 shift_count = number;
2029 number = (shift_count > 0) ?
2030 shift_count : sc_width / 2;
2031 if (number > hshift)
2042 shift_count = number;
2044 number = (shift_count > 0) ?
2045 shift_count : sc_width / 2;
2052 * Shift view left to margin.
2060 * Shift view right to view rightmost char on screen.
2068 * The command is incomplete (more chars are needed).
2069 * Display the current char, so the user knows
2070 * what's going on, and get another character.
2072 if (mca != A_PREFIX)
2075 start_mca(A_PREFIX, " ", (void*)NULL,