vi: code shrink; save/restore errno in signal handlers
function old new delta query_screen_dimensions - 54 +54 suspend_sig 50 64 +14 cont_sig 65 66 +1 catch_sig 42 32 -10 winch_sig 88 60 -28 edit_file 719 671 -48 refresh 848 767 -81 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 2/4 up/down: 69/-167) Total: -98 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
6f69f2dbc6
commit
2bb651ae10
46
editors/vi.c
46
editors/vi.c
@ -502,6 +502,19 @@ static int init_text_buffer(char *fn)
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ENABLE_FEATURE_VI_WIN_RESIZE
|
||||||
|
static void query_screen_dimensions(void)
|
||||||
|
{
|
||||||
|
get_terminal_width_height(STDIN_FILENO, &columns, &rows);
|
||||||
|
if (rows > MAX_SCR_ROWS)
|
||||||
|
rows = MAX_SCR_ROWS;
|
||||||
|
if (columns > MAX_SCR_COLS)
|
||||||
|
columns = MAX_SCR_COLS;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
# define query_screen_dimensions() ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
static void edit_file(char *fn)
|
static void edit_file(char *fn)
|
||||||
{
|
{
|
||||||
#if ENABLE_FEATURE_VI_YANKMARK
|
#if ENABLE_FEATURE_VI_YANKMARK
|
||||||
@ -518,11 +531,7 @@ static void edit_file(char *fn)
|
|||||||
rows = 24;
|
rows = 24;
|
||||||
columns = 80;
|
columns = 80;
|
||||||
size = 0;
|
size = 0;
|
||||||
if (ENABLE_FEATURE_VI_WIN_RESIZE) {
|
query_screen_dimensions();
|
||||||
get_terminal_width_height(0, &columns, &rows);
|
|
||||||
if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
|
|
||||||
if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
|
|
||||||
}
|
|
||||||
new_screen(rows, columns); // get memory for virtual screen
|
new_screen(rows, columns); // get memory for virtual screen
|
||||||
init_text_buffer(fn);
|
init_text_buffer(fn);
|
||||||
|
|
||||||
@ -537,7 +546,7 @@ static void edit_file(char *fn)
|
|||||||
ccol = 0;
|
ccol = 0;
|
||||||
|
|
||||||
#if ENABLE_FEATURE_VI_USE_SIGNALS
|
#if ENABLE_FEATURE_VI_USE_SIGNALS
|
||||||
catch_sig(0);
|
signal(SIGINT, catch_sig);
|
||||||
signal(SIGWINCH, winch_sig);
|
signal(SIGWINCH, winch_sig);
|
||||||
signal(SIGTSTP, suspend_sig);
|
signal(SIGTSTP, suspend_sig);
|
||||||
sig = sigsetjmp(restart, 1);
|
sig = sigsetjmp(restart, 1);
|
||||||
@ -563,7 +572,7 @@ static void edit_file(char *fn)
|
|||||||
char *p, *q;
|
char *p, *q;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
while ((p = initial_cmds[n])) {
|
while ((p = initial_cmds[n]) != NULL) {
|
||||||
do {
|
do {
|
||||||
q = p;
|
q = p;
|
||||||
p = strchr(q, '\n');
|
p = strchr(q, '\n');
|
||||||
@ -2143,50 +2152,51 @@ static void cookmode(void)
|
|||||||
tcsetattr_stdin_TCSANOW(&term_orig);
|
tcsetattr_stdin_TCSANOW(&term_orig);
|
||||||
}
|
}
|
||||||
|
|
||||||
//----- Come here when we get a window resize signal ---------
|
|
||||||
#if ENABLE_FEATURE_VI_USE_SIGNALS
|
#if ENABLE_FEATURE_VI_USE_SIGNALS
|
||||||
|
//----- Come here when we get a window resize signal ---------
|
||||||
static void winch_sig(int sig UNUSED_PARAM)
|
static void winch_sig(int sig UNUSED_PARAM)
|
||||||
{
|
{
|
||||||
|
int save_errno = errno;
|
||||||
// FIXME: do it in main loop!!!
|
// FIXME: do it in main loop!!!
|
||||||
signal(SIGWINCH, winch_sig);
|
signal(SIGWINCH, winch_sig);
|
||||||
if (ENABLE_FEATURE_VI_WIN_RESIZE) {
|
query_screen_dimensions();
|
||||||
get_terminal_width_height(0, &columns, &rows);
|
|
||||||
if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
|
|
||||||
if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
|
|
||||||
}
|
|
||||||
new_screen(rows, columns); // get memory for virtual screen
|
new_screen(rows, columns); // get memory for virtual screen
|
||||||
redraw(TRUE); // re-draw the screen
|
redraw(TRUE); // re-draw the screen
|
||||||
|
errno = save_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
//----- Come here when we get a continue signal -------------------
|
//----- Come here when we get a continue signal -------------------
|
||||||
static void cont_sig(int sig UNUSED_PARAM)
|
static void cont_sig(int sig UNUSED_PARAM)
|
||||||
{
|
{
|
||||||
|
int save_errno = errno;
|
||||||
rawmode(); // terminal to "raw"
|
rawmode(); // terminal to "raw"
|
||||||
last_status_cksum = 0; // force status update
|
last_status_cksum = 0; // force status update
|
||||||
redraw(TRUE); // re-draw the screen
|
redraw(TRUE); // re-draw the screen
|
||||||
|
|
||||||
signal(SIGTSTP, suspend_sig);
|
signal(SIGTSTP, suspend_sig);
|
||||||
signal(SIGCONT, SIG_DFL);
|
signal(SIGCONT, SIG_DFL);
|
||||||
kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
|
//kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
|
||||||
|
errno = save_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
//----- Come here when we get a Suspend signal -------------------
|
//----- Come here when we get a Suspend signal -------------------
|
||||||
static void suspend_sig(int sig UNUSED_PARAM)
|
static void suspend_sig(int sig UNUSED_PARAM)
|
||||||
{
|
{
|
||||||
|
int save_errno = errno;
|
||||||
go_bottom_and_clear_to_eol();
|
go_bottom_and_clear_to_eol();
|
||||||
cookmode(); // terminal to "cooked"
|
cookmode(); // terminal to "cooked"
|
||||||
|
|
||||||
signal(SIGCONT, cont_sig);
|
signal(SIGCONT, cont_sig);
|
||||||
signal(SIGTSTP, SIG_DFL);
|
signal(SIGTSTP, SIG_DFL);
|
||||||
kill(my_pid, SIGTSTP);
|
kill(my_pid, SIGTSTP);
|
||||||
|
errno = save_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
//----- Come here when we get a signal ---------------------------
|
//----- Come here when we get a signal ---------------------------
|
||||||
static void catch_sig(int sig)
|
static void catch_sig(int sig)
|
||||||
{
|
{
|
||||||
signal(SIGINT, catch_sig);
|
signal(SIGINT, catch_sig);
|
||||||
if (sig)
|
siglongjmp(restart, sig);
|
||||||
siglongjmp(restart, sig);
|
|
||||||
}
|
}
|
||||||
#endif /* FEATURE_VI_USE_SIGNALS */
|
#endif /* FEATURE_VI_USE_SIGNALS */
|
||||||
|
|
||||||
@ -2781,9 +2791,7 @@ static void refresh(int full_screen)
|
|||||||
|
|
||||||
if (ENABLE_FEATURE_VI_WIN_RESIZE) {
|
if (ENABLE_FEATURE_VI_WIN_RESIZE) {
|
||||||
unsigned c = columns, r = rows;
|
unsigned c = columns, r = rows;
|
||||||
get_terminal_width_height(0, &columns, &rows);
|
query_screen_dimensions();
|
||||||
if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
|
|
||||||
if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
|
|
||||||
full_screen |= (c - columns) | (r - rows);
|
full_screen |= (c - columns) | (r - rows);
|
||||||
}
|
}
|
||||||
sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
|
sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user