#include "less.h"
#if MSDOS_COMPILER==WIN32C
#include <windows.h>
#endif
#include "position.h"
#include "option.h"
#include "cmd.h"
extern int erase_char, erase2_char, kill_char;
extern int sigs;
extern int quit_if_one_screen;
extern int one_screen;
extern int sc_width;
extern int sc_height;
extern char *kent;
extern int swindow;
extern int jump_sline;
extern lbool quitting;
extern int wscroll;
extern int top_scroll;
extern int ignore_eoi;
extern int hshift;
extern int bs_mode;
extern int proc_backspace;
extern int show_attn;
extern int less_is_more;
extern int chopline;
extern POSITION highest_hilite;
extern char *every_first_cmd;
extern char version[];
extern struct scrpos initial_scrpos;
extern IFILE curr_ifile;
extern void *ml_search;
extern void *ml_examine;
extern int wheel_lines;
extern int def_search_type;
extern lbool search_wrapped;
extern int no_paste;
extern lbool pasting;
extern int no_edit_warn;
extern POSITION soft_eof;
#if SHELL_ESCAPE || PIPEC
extern void *ml_shell;
#endif
#if EDITOR
extern constant char *editproto;
#endif
#if OSC8_LINK
extern char *osc8_uri;
#endif
extern int shift_count;
extern int forw_prompt;
extern int incr_search;
extern int full_screen;
#if MSDOS_COMPILER==WIN32C
extern int utf_mode;
extern unsigned less_acp;
#endif
#if SHELL_ESCAPE
static char *shellcmd = NULL;
#endif
static int mca;
static int search_type;
static int last_search_type;
static LINENUM number;
static long fraction;
static struct loption *curropt;
static lbool opt_lower;
static int optflag;
static lbool optgetname;
static POSITION bottompos;
static int save_hshift;
static int save_bs_mode;
static int save_proc_backspace;
static int screen_trashed_value = 0;
static lbool literal_char = FALSE;
static lbool ignoring_input = FALSE;
#if HAVE_TIME
static time_type ignoring_input_time;
#endif
#if PIPEC
static char pipec;
#endif
struct ungot {
struct ungot *ug_next;
char ug_char;
lbool ug_end_command;
};
static struct ungot* ungot = NULL;
static void multi_search(constant char *pattern, int n, int silent);
public void cmd_exec(void)
{
clear_attn();
clear_bot();
flush();
}
static void set_mca(int action)
{
mca = action;
clear_bot();
clear_cmd();
}
static void clear_mca(void)
{
if (mca == 0)
return;
mca = 0;
}
static void start_mca(int action, constant char *prompt, void *mlist, int cmdflags)
{
set_mca(action);
cmd_putstr(prompt);
set_mlist(mlist, cmdflags);
}
public int in_mca(void)
{
return (mca != 0 && mca != A_PREFIX);
}
static void mca_search1(void)
{
int i;
#if HILITE_SEARCH
if (search_type & SRCH_FILTER)
set_mca(A_FILTER);
else
#endif
if (search_type & SRCH_FORW)
set_mca(A_F_SEARCH);
else
set_mca(A_B_SEARCH);
if (search_type & SRCH_NO_MATCH)
cmd_putstr("Non-match ");
if (search_type & SRCH_FIRST_FILE)
cmd_putstr("First-file ");
if (search_type & SRCH_PAST_EOF)
cmd_putstr("EOF-ignore ");
if (search_type & SRCH_NO_MOVE)
cmd_putstr("Keep-pos ");
if (search_type & SRCH_NO_REGEX)
cmd_putstr("Regex-off ");
if (search_type & SRCH_WRAP)
cmd_putstr("Wrap ");
for (i = 1; i <= NUM_SEARCH_COLORS; i++)
{
if (search_type & SRCH_SUBSEARCH(i))
{
char buf[INT_STRLEN_BOUND(int)+8];
SNPRINTF1(buf, sizeof(buf), "Sub-%d ", i);
cmd_putstr(buf);
}
}
if (literal_char)
cmd_putstr("Lit ");
#if HILITE_SEARCH
if (search_type & SRCH_FILTER)
cmd_putstr("&/");
else
#endif
if (search_type & SRCH_FORW)
cmd_putstr("/");
else
cmd_putstr("?");
forw_prompt = 0;
}
static void mca_search(void)
{
mca_search1();
set_mlist(ml_search, 0);
}
static void mca_opt_toggle(void)
{
int no_prompt = (optflag & OPT_NO_PROMPT);
int flag = (optflag & ~OPT_NO_PROMPT);
constant char *dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
set_mca(A_OPT_TOGGLE);
cmd_putstr(dash);
if (optgetname)
cmd_putstr(dash);
if (no_prompt)
cmd_putstr("(P)");
switch (flag)
{
case OPT_UNSET:
cmd_putstr("+");
break;
case OPT_SET:
cmd_putstr("!");
break;
}
forw_prompt = 0;
set_mlist(NULL, CF_OPTION);
}
static void exec_mca(void)
{
constant char *cbuf;
cmd_exec();
cbuf = get_cmdbuf();
if (cbuf == NULL)
return;
switch (mca)
{
case A_F_SEARCH:
case A_B_SEARCH:
multi_search(cbuf, (int) number, 0);
break;
#if HILITE_SEARCH
case A_FILTER:
search_type ^= SRCH_NO_MATCH;
set_filter_pattern(cbuf, search_type);
soft_eof = NULL_POSITION;
break;
#endif
case A_FIRSTCMD:
while (*cbuf == '+' || *cbuf == ' ')
cbuf++;
if (every_first_cmd != NULL)
free(every_first_cmd);
if (*cbuf == '\0')
every_first_cmd = NULL;
else
every_first_cmd = save(cbuf);
break;
case A_OPT_TOGGLE:
toggle_option(curropt, opt_lower, cbuf, optflag);
curropt = NULL;
break;
case A_F_BRACKET:
match_brac(cbuf[0], cbuf[1], 1, (int) number);
break;
case A_B_BRACKET:
match_brac(cbuf[1], cbuf[0], 0, (int) number);
break;
#if EXAMINE
case A_EXAMINE: {
char *p;
if (!secure_allow(SF_EXAMINE))
break;
p = save(cbuf);
edit_list(p);
free(p);
#if TAGS
cleantags();
#endif
break; }
#endif
#if SHELL_ESCAPE
case A_SHELL: {
constant char *done_msg = (*cbuf == CONTROL('P')) ? NULL : "!done";
if (done_msg == NULL)
++cbuf;
if (*cbuf != '!')
{
if (shellcmd != NULL)
free(shellcmd);
shellcmd = fexpand(cbuf);
}
if (!secure_allow(SF_SHELL))
break;
if (shellcmd == NULL)
shellcmd = "";
lsystem(shellcmd, done_msg);
break; }
case A_PSHELL: {
constant char *done_msg = (*cbuf == CONTROL('P')) ? NULL : "#done";
if (done_msg == NULL)
++cbuf;
if (!secure_allow(SF_SHELL))
break;
lsystem(pr_expand(cbuf), done_msg);
break; }
#endif
#if PIPEC
case A_PIPE: {
constant char *done_msg = (*cbuf == CONTROL('P')) ? NULL : "|done";
if (done_msg == NULL)
++cbuf;
if (!secure_allow(SF_PIPE))
break;
(void) pipe_mark(pipec, cbuf);
if (done_msg != NULL)
error(done_msg, NULL_PARG);
break; }
#endif
}
}
static lbool is_erase_char(char c)
{
return (c == erase_char || c == erase2_char || c == kill_char);
}
static lbool is_newline_char(char c)
{
return (c == '\n' || c == '\r');
}
static int mca_opt_first_char(char c)
{
int no_prompt = (optflag & OPT_NO_PROMPT);
int flag = (optflag & ~OPT_NO_PROMPT);
if (flag == OPT_NO_TOGGLE)
{
switch (c)
{
case '_':
optgetname = TRUE;
mca_opt_toggle();
return (MCA_MORE);
}
} else
{
switch (c)
{
case '+':
optflag = no_prompt | ((flag == OPT_UNSET) ?
OPT_TOGGLE : OPT_UNSET);
mca_opt_toggle();
return (MCA_MORE);
case '!':
optflag = no_prompt | ((flag == OPT_SET) ?
OPT_TOGGLE : OPT_SET);
mca_opt_toggle();
return (MCA_MORE);
case CONTROL('P'):
optflag ^= OPT_NO_PROMPT;
mca_opt_toggle();
return (MCA_MORE);
case '-':
optgetname = TRUE;
mca_opt_toggle();
return (MCA_MORE);
}
}
return (NO_MCA);
}
static int mca_opt_nonfirst_char(char c)
{
constant char *p;
constant char *oname;
lbool ambig;
struct loption *was_curropt;
if (curropt != NULL)
{
if (is_erase_char(c))
return (MCA_DONE);
if (c != '\t')
return (MCA_MORE);
}
if (cmd_char(c) == CC_QUIT)
return (MCA_DONE);
p = get_cmdbuf();
if (p == NULL || p[0] == '\0')
return (MCA_MORE);
opt_lower = ASCII_IS_LOWER(p[0]);
was_curropt = curropt;
curropt = findopt_name(&p, &oname, &ambig);
if (curropt != NULL)
{
if (was_curropt == NULL)
{
cmd_reset();
mca_opt_toggle();
cmd_setstring(oname, !opt_lower);
}
} else if (!ambig)
{
bell();
}
return (MCA_MORE);
}
static int mca_opt_char(char c)
{
PARG parg;
if (curropt == NULL && cmdbuf_empty())
{
int ret = mca_opt_first_char(c);
if (ret != NO_MCA)
return (ret);
}
if (optgetname)
{
if (!is_newline_char(c) && c != '=')
return (mca_opt_nonfirst_char(c));
if (curropt == NULL)
{
parg.p_string = get_cmdbuf();
if (parg.p_string == NULL)
return (MCA_MORE);
error("There is no --%s option", &parg);
return (MCA_DONE);
}
optgetname = FALSE;
cmd_reset();
} else
{
if (is_erase_char(c))
return (NO_MCA);
if (curropt != NULL)
return (NO_MCA);
curropt = findopt(c);
if (curropt == NULL)
{
parg.p_string = propt(c);
error("There is no %s option", &parg);
return (MCA_DONE);
}
opt_lower = ASCII_IS_LOWER(c);
}
if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
!opt_has_param(curropt))
{
toggle_option(curropt, opt_lower, "", optflag);
return (MCA_DONE);
}
start_mca(A_OPT_TOGGLE, opt_prompt(curropt), NULL, CF_OPTION);
return (MCA_MORE);
}
public int norm_search_type(int st)
{
if ((st & (SRCH_PAST_EOF|SRCH_WRAP)) == (SRCH_PAST_EOF|SRCH_WRAP))
st ^= SRCH_PAST_EOF;
return st;
}
static int mca_search_char(char c)
{
int flag = 0;
if (!cmdbuf_empty() || literal_char)
{
literal_char = FALSE;
return (NO_MCA);
}
switch (c)
{
case '*':
if (less_is_more)
break;
case CONTROL('E'):
if (mca != A_FILTER)
flag = SRCH_PAST_EOF;
search_type &= ~SRCH_WRAP;
break;
case '@':
if (less_is_more)
break;
case CONTROL('F'):
if (mca != A_FILTER)
flag = SRCH_FIRST_FILE;
break;
case CONTROL('K'):
if (mca != A_FILTER)
flag = SRCH_NO_MOVE;
break;
case CONTROL('S'): {
char buf[INT_STRLEN_BOUND(int)+24];
SNPRINTF1(buf, sizeof(buf), "Sub-pattern (1-%d):", NUM_SEARCH_COLORS);
clear_bot();
cmd_putstr(buf);
flush();
c = getcc();
if (c >= '1' && c <= '0'+NUM_SEARCH_COLORS)
flag = SRCH_SUBSEARCH(c-'0');
else
flag = -1;
break; }
case CONTROL('W'):
if (mca != A_FILTER)
flag = SRCH_WRAP;
break;
case CONTROL('R'):
flag = SRCH_NO_REGEX;
break;
case CONTROL('N'):
case '!':
flag = SRCH_NO_MATCH;
break;
case CONTROL('L'):
literal_char = TRUE;
flag = -1;
break;
}
if (flag != 0)
{
if (flag != -1)
search_type = norm_search_type(search_type ^ flag);
mca_search();
return (MCA_MORE);
}
return (NO_MCA);
}
static int mca_char(char c)
{
int ret;
switch (mca)
{
case 0:
return (NO_MCA);
case A_PREFIX:
return (NO_MCA);
case A_DIGIT:
if ((c >= '0' && c <= '9') || c == '.')
break;
switch (editchar(c, ECF_PEEK|ECF_NOHISTORY|ECF_NOCOMPLETE|ECF_NORIGHTLEFT))
{
case A_NOACTION:
return (MCA_MORE);
case A_INVALID:
number = cmd_int(&fraction);
clear_mca();
cmd_accept();
return (NO_MCA);
}
break;
case A_OPT_TOGGLE:
ret = mca_opt_char(c);
if (ret != NO_MCA)
return (ret);
break;
case A_F_SEARCH:
case A_B_SEARCH:
case A_FILTER:
ret = mca_search_char(c);
if (ret != NO_MCA)
return (ret);
break;
default:
break;
}
if (is_newline_char(c))
{
if (pasting && no_paste)
{
start_ignoring_input();
return (MCA_MORE);
}
exec_mca();
return (MCA_DONE);
}
if (cmd_char(c) == CC_QUIT)
return (MCA_DONE);
switch (mca)
{
case A_F_BRACKET:
case A_B_BRACKET:
if (len_cmdbuf() >= 2)
{
exec_mca();
return (MCA_DONE);
}
break;
case A_F_SEARCH:
case A_B_SEARCH:
if (incr_search)
{
int st = (search_type & (SRCH_FORW|SRCH_BACK|SRCH_NO_MATCH|SRCH_NO_REGEX|SRCH_NO_MOVE|SRCH_WRAP|SRCH_SUBSEARCH_ALL));
ssize_t save_updown;
constant char *pattern = get_cmdbuf();
if (pattern == NULL)
return (MCA_MORE);
save_updown = save_updown_match();
cmd_exec();
if (*pattern == '\0')
{
undo_search(1);
} else
{
if (search(st | SRCH_INCR, pattern, 1) != 0)
undo_search(1);
}
if (is_screen_trashed() || !full_screen)
{
clear();
repaint();
}
mca_search1();
restore_updown_match(save_updown);
cmd_repaint(NULL);
}
break;
}
return (MCA_MORE);
}
static void clear_buffers(void)
{
if (!(ch_getflags() & CH_CANSEEK))
return;
ch_flush();
clr_linenum();
#if HILITE_SEARCH
clr_hilite();
#endif
}
public void screen_trashed_num(int trashed)
{
screen_trashed_value = trashed;
}
public void screen_trashed(void)
{
screen_trashed_num(1);
}
public int is_screen_trashed(void)
{
return screen_trashed_value;
}
static void make_display(void)
{
if (!full_screen && !(quit_if_one_screen && one_screen))
clear();
if (empty_screen())
{
if (initial_scrpos.pos == NULL_POSITION)
jump_loc(ch_zero(), 1);
else
jump_loc(initial_scrpos.pos, initial_scrpos.ln);
} else if (is_screen_trashed() || !full_screen)
{
int save_top_scroll = top_scroll;
int save_ignore_eoi = ignore_eoi;
top_scroll = 1;
ignore_eoi = 0;
if (is_screen_trashed() == 2)
{
reopen_curr_ifile();
jump_forw();
}
repaint();
top_scroll = save_top_scroll;
ignore_eoi = save_ignore_eoi;
}
}
static void prompt(void)
{
constant char *p;
if (ungot != NULL && !ungot->ug_end_command)
{
return;
}
make_display();
bottompos = position(BOTTOM_PLUS_ONE);
if (get_quit_at_eof() == OPT_ONPLUS &&
eof_displayed(FALSE) && !(ch_getflags() & CH_HELPFILE) &&
next_ifile(curr_ifile) == NULL_IFILE)
quit(QUIT_OK);
if (quit_if_one_screen &&
entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
next_ifile(curr_ifile) == NULL_IFILE)
quit(QUIT_OK);
quit_if_one_screen = FALSE;
#if MSDOS_COMPILER==WIN32C
if (!(ch_getflags() & CH_HELPFILE))
{
WCHAR w[MAX_PATH+16];
p = pr_expand("Less?f - %f.");
MultiByteToWideChar(less_acp, 0, p, -1, w, countof(w));
SetConsoleTitleW(w);
}
#endif
if (!forw_prompt)
clear_bot();
clear_cmd();
forw_prompt = 0;
p = pr_string();
#if HILITE_SEARCH
if (is_filtering())
putstr("& ");
#endif
if (search_wrapped)
{
if (search_type & SRCH_BACK)
error("Search hit top; continuing at bottom", NULL_PARG);
else
error("Search hit bottom; continuing at top", NULL_PARG);
search_wrapped = FALSE;
}
#if OSC8_LINK
if (osc8_uri != NULL)
{
PARG parg;
parg.p_string = osc8_uri;
error("Link: %s", &parg);
free(osc8_uri);
osc8_uri = NULL;
}
#endif
if (p == NULL || *p == '\0')
{
at_enter(AT_NORMAL|AT_COLOR_PROMPT);
putchr(':');
at_exit();
} else
{
#if MSDOS_COMPILER==WIN32C
WCHAR w[MAX_PATH*2];
char a[MAX_PATH*2];
MultiByteToWideChar(less_acp, 0, p, -1, w, countof(w));
WideCharToMultiByte(utf_mode ? CP_UTF8 : GetConsoleOutputCP(),
0, w, -1, a, sizeof(a), NULL, NULL);
p = a;
#endif
load_line(p);
put_line(FALSE);
}
clear_eol();
}
public void dispversion(void)
{
PARG parg;
parg.p_string = version;
error("less %s", &parg);
}
static char getcc_end_command(void)
{
int ch;
switch (mca)
{
case A_DIGIT:
return ('g');
case A_F_SEARCH:
case A_B_SEARCH:
case A_FILTER:
return ('\n');
default:
if (ungot != NULL)
return ('\0');
ch = getchr();
if (ch < 0) ch = '\0';
return (char) ch;
}
}
static char get_ungot(lbool *p_end_command)
{
struct ungot *ug = ungot;
char c = ug->ug_char;
if (p_end_command != NULL)
*p_end_command = ug->ug_end_command;
ungot = ug->ug_next;
free(ug);
return c;
}
public void getcc_clear(void)
{
while (ungot != NULL)
(void) get_ungot(NULL);
}
static char getccu(void)
{
int c = 0;
while (c == 0 && sigs == 0)
{
if (ungot == NULL)
{
c = getchr();
if (c < 0) c = '\0';
} else
{
lbool end_command;
c = get_ungot(&end_command);
if (end_command)
c = getcc_end_command();
}
}
return ((char) c);
}
static char getcc_repl(char constant *orig, char constant *repl, char (*gr_getc)(void), void (*gr_ungetc)(char))
{
char c;
char keys[16];
size_t ki = 0;
c = (*gr_getc)();
if (orig == NULL || orig[0] == '\0')
return c;
for (;;)
{
keys[ki] = c;
if (c != orig[ki] || ki >= sizeof(keys)-1)
{
while (ki > 0)
(*gr_ungetc)(keys[ki--]);
return keys[0];
}
if (orig[++ki] == '\0')
{
ki = strlen(repl)-1;
while (ki > 0)
(*gr_ungetc)(repl[ki--]);
return repl[0];
}
c = (*gr_getc)();
}
}
public char getcc(void)
{
return getcc_repl(kent, "\n", getccu, ungetcc);
}
public void ungetcc(char c)
{
struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
ug->ug_char = c;
ug->ug_next = ungot;
ungot = ug;
}
static void ungetcc_back1(char c, lbool end_command)
{
struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
ug->ug_char = c;
ug->ug_end_command = end_command;
ug->ug_next = NULL;
if (ungot == NULL)
ungot = ug;
else
{
struct ungot *pu;
for (pu = ungot; pu->ug_next != NULL; pu = pu->ug_next)
continue;
pu->ug_next = ug;
}
}
public void ungetcc_back(char c)
{
ungetcc_back1(c, FALSE);
}
public void ungetcc_end_command(void)
{
ungetcc_back1('\0', TRUE);
}
public void ungetsc(constant char *s)
{
while (*s != '\0')
ungetcc_back(*s++);
}
public char peekcc(void)
{
char c = getcc();
ungetcc(c);
return c;
}
static void multi_search(constant char *pattern, int n, int silent)
{
int nomore;
IFILE save_ifile;
lbool changed_file;
changed_file = FALSE;
save_ifile = save_curr_ifile();
if ((search_type & (SRCH_FORW|SRCH_BACK)) == 0)
search_type |= SRCH_FORW;
if (search_type & SRCH_FIRST_FILE)
{
if (search_type & SRCH_FORW)
nomore = edit_first();
else
nomore = edit_last();
if (nomore)
{
unsave_ifile(save_ifile);
return;
}
changed_file = TRUE;
search_type &= ~SRCH_FIRST_FILE;
}
for (;;)
{
n = search(search_type, pattern, n);
search_type &= ~SRCH_NO_MOVE;
last_search_type = search_type;
if (n == 0)
{
unsave_ifile(save_ifile);
return;
}
if (n < 0)
break;
if ((search_type & SRCH_PAST_EOF) == 0)
break;
if (search_type & SRCH_FORW)
nomore = edit_next(1);
else
nomore = edit_prev(1);
if (nomore)
break;
changed_file = TRUE;
}
if (n > 0 && !silent)
error("Pattern not found", NULL_PARG);
if (changed_file)
{
reedit_ifile(save_ifile);
} else
{
unsave_ifile(save_ifile);
}
}
static int forw_loop(int until_hilite)
{
POSITION curr_len;
if (ch_getflags() & CH_HELPFILE)
return (A_NOACTION);
cmd_exec();
jump_forw_buffered();
curr_len = ch_length();
highest_hilite = until_hilite ? curr_len : NULL_POSITION;
ignore_eoi = 1;
while (!sigs)
{
if (until_hilite && highest_hilite > curr_len)
{
bell();
break;
}
make_display();
forward(1, FALSE, FALSE, FALSE);
}
ignore_eoi = 0;
ch_set_eof();
if (sigs && !ABORT_SIGS())
return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
return (A_NOACTION);
}
public void start_ignoring_input()
{
ignoring_input = TRUE;
#if HAVE_TIME
ignoring_input_time = get_time();
#endif
}
public void stop_ignoring_input()
{
ignoring_input = FALSE;
pasting = FALSE;
}
public lbool is_ignoring_input(int action)
{
if (!ignoring_input)
return FALSE;
if (action == A_END_PASTE)
stop_ignoring_input();
#if HAVE_TIME
if (get_time() >= ignoring_input_time + MAX_PASTE_IGNORE_SEC)
stop_ignoring_input();
#endif
return (action != A_PREFIX);
}
public void commands(void)
{
char c;
int action;
constant char *cbuf;
constant char *msg;
int newaction;
int save_jump_sline;
int save_search_type;
constant char *extra;
PARG parg;
IFILE old_ifile;
IFILE new_ifile;
#if TAGS
constant char *tagfile;
#endif
search_type = SRCH_FORW;
wscroll = (sc_height + 1) / 2;
newaction = A_NOACTION;
for (;;)
{
clear_mca();
cmd_accept();
number = 0;
curropt = NULL;
if (sigs)
{
psignals();
if (quitting)
quit(QUIT_SAVED_STATUS);
}
check_winch();
cmd_reset();
prompt();
if (sigs)
continue;
if (newaction == A_NOACTION)
c = getcc();
again:
if (sigs)
continue;
if (newaction != A_NOACTION)
{
action = newaction;
newaction = A_NOACTION;
} else
{
if (mca)
switch (mca_char(c))
{
case MCA_MORE:
c = getcc();
goto again;
case MCA_DONE:
continue;
case NO_MCA:
break;
}
extra = NULL;
if (mca)
{
if (cmd_char(c) == CC_QUIT || cmdbuf_empty())
continue;
cbuf = get_cmdbuf();
if (cbuf == NULL)
{
c = getcc();
goto again;
}
action = fcmd_decode(cbuf, &extra);
} else
{
constant char tbuf[2] = { c, '\0' };
action = fcmd_decode(tbuf, &extra);
}
if (extra != NULL)
ungetsc(extra);
}
if (action != A_PREFIX)
cmd_reset();
if (is_ignoring_input(action))
continue;
switch (action)
{
case A_START_PASTE:
if (no_paste)
start_ignoring_input();
break;
case A_DIGIT:
start_mca(A_DIGIT, ":", NULL, CF_QUIT_ON_ERASE);
goto again;
case A_F_WINDOW:
if (number > 0)
swindow = (int) number;
case A_F_SCREEN:
if (number <= 0)
number = get_swindow();
cmd_exec();
if (show_attn)
set_attnpos(bottompos);
forward((int) number, FALSE, TRUE, FALSE);
break;
case A_B_WINDOW:
if (number > 0)
swindow = (int) number;
case A_B_SCREEN:
if (number <= 0)
number = get_swindow();
cmd_exec();
backward((int) number, FALSE, TRUE, FALSE);
break;
case A_F_LINE:
case A_F_NEWLINE:
if (number <= 0)
number = 1;
cmd_exec();
if (show_attn == OPT_ONPLUS && number > 1)
set_attnpos(bottompos);
forward((int) number, FALSE, FALSE, action == A_F_NEWLINE && !chopline);
break;
case A_B_LINE:
case A_B_NEWLINE:
if (number <= 0)
number = 1;
cmd_exec();
backward((int) number, FALSE, FALSE, action == A_B_NEWLINE && !chopline);
break;
case A_F_MOUSE:
cmd_exec();
forward(wheel_lines, FALSE, FALSE, FALSE);
break;
case A_B_MOUSE:
cmd_exec();
backward(wheel_lines, FALSE, FALSE, FALSE);
break;
case A_FF_LINE:
if (number <= 0)
number = 1;
cmd_exec();
if (show_attn == OPT_ONPLUS && number > 1)
set_attnpos(bottompos);
forward((int) number, TRUE, FALSE, FALSE);
break;
case A_BF_LINE:
if (number <= 0)
number = 1;
cmd_exec();
backward((int) number, TRUE, FALSE, FALSE);
break;
case A_FF_SCREEN:
if (number <= 0)
number = get_swindow();
cmd_exec();
if (show_attn == OPT_ONPLUS)
set_attnpos(bottompos);
forward((int) number, TRUE, FALSE, FALSE);
break;
case A_BF_SCREEN:
if (number <= 0)
number = get_swindow();
cmd_exec();
backward((int) number, TRUE, FALSE, FALSE);
break;
case A_F_FOREVER:
if (get_altfilename(curr_ifile) != NULL)
error("Warning: command may not work correctly when file is viewed via LESSOPEN", NULL_PARG);
if (show_attn)
set_attnpos(bottompos);
newaction = forw_loop(0);
break;
case A_F_UNTIL_HILITE:
newaction = forw_loop(1);
break;
case A_F_SCROLL:
if (number > 0)
wscroll = (int) number;
cmd_exec();
if (show_attn == OPT_ONPLUS)
set_attnpos(bottompos);
forward(wscroll, FALSE, FALSE, FALSE);
break;
case A_B_SCROLL:
if (number > 0)
wscroll = (int) number;
cmd_exec();
backward(wscroll, FALSE, FALSE, FALSE);
break;
case A_FREPAINT:
clear_buffers();
case A_REPAINT:
cmd_exec();
repaint();
break;
case A_GOLINE:
save_jump_sline = jump_sline;
if (number <= 0)
{
number = 1;
jump_sline = 0;
}
cmd_exec();
jump_back(number);
jump_sline = save_jump_sline;
break;
case A_PERCENT:
if (number < 0)
{
number = 0;
fraction = 0;
}
if (number > 100 || (number == 100 && fraction != 0))
{
number = 100;
fraction = 0;
}
cmd_exec();
jump_percent((int) number, fraction);
break;
case A_GOEND:
cmd_exec();
if (number <= 0)
jump_forw();
else
jump_back(number);
break;
case A_GOEND_BUF:
cmd_exec();
if (number <= 0)
jump_forw_buffered();
else
jump_back(number);
break;
case A_GOPOS:
cmd_exec();
if (number < 0)
number = 0;
jump_line_loc((POSITION) number, jump_sline);
break;
case A_STAT:
if (ch_getflags() & CH_HELPFILE)
break;
cmd_exec();
parg.p_string = eq_message();
error("%s", &parg);
break;
case A_VERSION:
cmd_exec();
dispversion();
break;
case A_QUIT:
if (curr_ifile != NULL_IFILE &&
ch_getflags() & CH_HELPFILE)
{
hshift = save_hshift;
bs_mode = save_bs_mode;
proc_backspace = save_proc_backspace;
if (edit_prev(1) == 0)
break;
}
if (extra != NULL)
quit(*extra);
quit(QUIT_OK);
break;
#define DO_SEARCH() \
if (number <= 0) number = 1; \
mca_search(); \
cmd_exec(); \
multi_search(NULL, (int) number, 0);
case A_F_SEARCH:
search_type = SRCH_FORW | def_search_type;
if (number <= 0)
number = 1;
literal_char = FALSE;
mca_search();
c = getcc();
goto again;
case A_B_SEARCH:
search_type = SRCH_BACK | def_search_type;
if (number <= 0)
number = 1;
literal_char = FALSE;
mca_search();
c = getcc();
goto again;
case A_OSC8_F_SEARCH:
#if OSC8_LINK
cmd_exec();
if (number <= 0)
number = 1;
osc8_search(SRCH_FORW, NULL, number);
#else
error("Command not available", NULL_PARG);
#endif
break;
case A_OSC8_B_SEARCH:
#if OSC8_LINK
cmd_exec();
if (number <= 0)
number = 1;
osc8_search(SRCH_BACK, NULL, number);
#else
error("Command not available", NULL_PARG);
#endif
break;
case A_OSC8_OPEN:
#if OSC8_LINK
if (secure_allow(SF_OSC8_OPEN))
{
cmd_exec();
osc8_open();
break;
}
#endif
error("Command not available", NULL_PARG);
break;
case A_OSC8_JUMP:
#if OSC8_LINK
cmd_exec();
osc8_jump();
#else
error("Command not available", NULL_PARG);
#endif
break;
case A_FILTER:
#if HILITE_SEARCH
search_type = SRCH_FORW | SRCH_FILTER;
literal_char = FALSE;
mca_search();
c = getcc();
goto again;
#else
error("Command not available", NULL_PARG);
break;
#endif
case A_AGAIN_SEARCH:
search_type = last_search_type;
DO_SEARCH();
break;
case A_T_AGAIN_SEARCH:
search_type = last_search_type | SRCH_PAST_EOF;
DO_SEARCH();
break;
case A_REVERSE_SEARCH:
save_search_type = search_type = last_search_type;
search_type = SRCH_REVERSE(search_type);
DO_SEARCH();
last_search_type = save_search_type;
break;
case A_T_REVERSE_SEARCH:
save_search_type = search_type = last_search_type;
search_type = SRCH_REVERSE(search_type) | SRCH_PAST_EOF;
DO_SEARCH();
last_search_type = save_search_type;
break;
case A_UNDO_SEARCH:
case A_CLR_SEARCH:
undo_search(action == A_CLR_SEARCH);
break;
case A_HELP:
if (ch_getflags() & CH_HELPFILE)
break;
cmd_exec();
save_hshift = hshift;
hshift = 0;
save_bs_mode = bs_mode;
bs_mode = BS_SPECIAL;
save_proc_backspace = proc_backspace;
proc_backspace = OPT_OFF;
(void) edit(FAKE_HELPFILE);
break;
case A_EXAMINE:
#if EXAMINE
if (secure_allow(SF_EXAMINE))
{
start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
c = getcc();
goto again;
}
#endif
error("Command not available", NULL_PARG);
break;
case A_VISUAL:
#if EDITOR
if (secure_allow(SF_EDIT))
{
if (ch_getflags() & CH_HELPFILE)
break;
if (strcmp(get_filename(curr_ifile), "-") == 0)
{
error("Cannot edit standard input", NULL_PARG);
break;
}
if (!no_edit_warn && get_altfilename(curr_ifile) != NULL)
{
error("WARNING: This file was viewed via LESSOPEN", NULL_PARG);
}
start_mca(A_SHELL, "!", ml_shell, 0);
make_display();
cmd_exec();
lsystem(pr_expand(editproto), NULL);
break;
}
#endif
error("Command not available", NULL_PARG);
break;
case A_NEXT_FILE:
#if TAGS
if (ntags())
{
error("No next file", NULL_PARG);
break;
}
#endif
if (number <= 0)
number = 1;
cmd_exec();
if (edit_next((int) number))
{
if (get_quit_at_eof() && eof_displayed(FALSE) &&
!(ch_getflags() & CH_HELPFILE))
quit(QUIT_OK);
parg.p_string = (number > 1) ? "(N-th) " : "";
error("No %snext file", &parg);
}
break;
case A_PREV_FILE:
#if TAGS
if (ntags())
{
error("No previous file", NULL_PARG);
break;
}
#endif
if (number <= 0)
number = 1;
cmd_exec();
if (edit_prev((int) number))
{
parg.p_string = (number > 1) ? "(N-th) " : "";
error("No %sprevious file", &parg);
}
break;
case A_NEXT_TAG:
#if TAGS
if (number <= 0)
number = 1;
tagfile = nexttag((int) number);
if (tagfile == NULL)
{
error("No next tag", NULL_PARG);
break;
}
cmd_exec();
if (edit(tagfile) == 0)
{
POSITION pos = tagsearch();
if (pos != NULL_POSITION)
jump_loc(pos, jump_sline);
}
#else
error("Command not available", NULL_PARG);
#endif
break;
case A_PREV_TAG:
#if TAGS
if (number <= 0)
number = 1;
tagfile = prevtag((int) number);
if (tagfile == NULL)
{
error("No previous tag", NULL_PARG);
break;
}
cmd_exec();
if (edit(tagfile) == 0)
{
POSITION pos = tagsearch();
if (pos != NULL_POSITION)
jump_loc(pos, jump_sline);
}
#else
error("Command not available", NULL_PARG);
#endif
break;
case A_INDEX_FILE:
if (number <= 0)
number = 1;
cmd_exec();
if (edit_index((int) number))
error("No such file", NULL_PARG);
break;
case A_REMOVE_FILE:
if (ch_getflags() & CH_HELPFILE)
break;
old_ifile = curr_ifile;
new_ifile = getoff_ifile(curr_ifile);
cmd_exec();
if (new_ifile == NULL_IFILE)
{
bell();
break;
}
if (edit_ifile(new_ifile) != 0)
{
reedit_ifile(old_ifile);
break;
}
del_ifile(old_ifile);
break;
case A_OPT_TOGGLE:
optflag = OPT_TOGGLE;
optgetname = FALSE;
mca_opt_toggle();
c = getcc();
msg = opt_toggle_disallowed(c);
if (msg != NULL)
{
error(msg, NULL_PARG);
break;
}
goto again;
case A_DISP_OPTION:
optflag = OPT_NO_TOGGLE;
optgetname = FALSE;
mca_opt_toggle();
c = getcc();
goto again;
case A_FIRSTCMD:
start_mca(A_FIRSTCMD, "+", NULL, 0);
c = getcc();
goto again;
case A_SHELL:
case A_PSHELL:
#if SHELL_ESCAPE
if (secure_allow(SF_SHELL))
{
start_mca(action, (action == A_SHELL) ? "!" : "#", ml_shell, 0);
c = getcc();
goto again;
}
#endif
error("Command not available", NULL_PARG);
break;
case A_SETMARK:
case A_SETMARKBOT:
if (ch_getflags() & CH_HELPFILE)
{
if (ungot != NULL)
{
(void) getcc();
}
break;
}
start_mca(A_SETMARK, "set mark: ", NULL, 0);
c = getcc();
if (is_erase_char(c) || is_newline_char(c))
break;
setmark(c, action == A_SETMARKBOT ? BOTTOM : TOP);
repaint();
break;
case A_CLRMARK:
start_mca(A_CLRMARK, "clear mark: ", NULL, 0);
c = getcc();
if (is_erase_char(c) || is_newline_char(c))
break;
clrmark(c);
repaint();
break;
case A_GOMARK:
start_mca(A_GOMARK, "goto mark: ", NULL, 0);
c = getcc();
if (is_erase_char(c) || is_newline_char(c))
break;
cmd_exec();
gomark(c);
break;
case A_PIPE:
#if PIPEC
if (secure_allow(SF_PIPE))
{
start_mca(A_PIPE, "|mark: ", NULL, 0);
c = getcc();
if (is_erase_char(c))
break;
if (is_newline_char(c))
c = '.';
if (badmark(c))
break;
pipec = c;
start_mca(A_PIPE, "!", ml_shell, 0);
c = getcc();
goto again;
}
#endif
error("Command not available", NULL_PARG);
break;
case A_B_BRACKET:
case A_F_BRACKET:
start_mca(action, "Brackets: ", NULL, 0);
c = getcc();
goto again;
case A_LSHIFT:
if (number > 0)
shift_count = (int) number;
else
number = (shift_count > 0) ? shift_count : sc_width / 2;
if (number > hshift)
number = hshift;
pos_rehead();
hshift -= (int) number;
screen_trashed();
break;
case A_RSHIFT:
if (number > 0)
shift_count = (int) number;
else
number = (shift_count > 0) ? shift_count : sc_width / 2;
pos_rehead();
hshift += (int) number;
screen_trashed();
break;
case A_LLSHIFT:
pos_rehead();
hshift = 0;
screen_trashed();
break;
case A_RRSHIFT:
pos_rehead();
hshift = rrshift();
screen_trashed();
break;
case A_PREFIX:
if (mca != A_PREFIX)
{
cmd_reset();
start_mca(A_PREFIX, " ", NULL, CF_QUIT_ON_ERASE);
(void) cmd_char(c);
}
c = getcc();
goto again;
case A_NOACTION:
break;
default:
bell();
break;
}
}
}