From e10bc606f03750a211ae430b6e2079d8e5b1ed85 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Sun, 10 Jul 2016 08:05:19 -0300 Subject: [PATCH] Handle SIGWINCH signal to properly resize editor --- kilo.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/kilo.c b/kilo.c index fbb30f9..c29f626 100644 --- a/kilo.c +++ b/kilo.c @@ -34,7 +34,9 @@ #define KILO_VERSION "0.0.1" +#ifdef __GLIBC__ #define _POSIX_C_SOURCE 200809L +#endif #include #include @@ -49,6 +51,7 @@ #include #include #include +#include /* Syntax highlight types */ #define HL_NORMAL 0 @@ -1244,6 +1247,22 @@ int editorFileWasModified(void) { return E.dirty; } +void updateWindowSize(void) { + if (getWindowSize(STDIN_FILENO,STDOUT_FILENO, + &E.screenrows,&E.screencols) == -1) { + perror("Unable to query the screen for size (columns / rows)"); + exit(1); + } + E.screenrows -= 2; /* Get room for status bar. */ +} + +void handleSigWinCh(int unused __attribute__((unused))) { + updateWindowSize(); + if (E.cy > E.screenrows) E.cy = E.screenrows - 1; + if (E.cx > E.screencols) E.cx = E.screencols - 1; + editorRefreshScreen(); +} + void initEditor(void) { E.cx = 0; E.cy = 0; @@ -1254,13 +1273,8 @@ void initEditor(void) { E.dirty = 0; E.filename = NULL; E.syntax = NULL; - if (getWindowSize(STDIN_FILENO,STDOUT_FILENO, - &E.screenrows,&E.screencols) == -1) - { - perror("Unable to query the screen for size (columns / rows)"); - exit(1); - } - E.screenrows -= 2; /* Get room for status bar. */ + updateWindowSize(); + signal(SIGWINCH, handleSigWinCh); } int main(int argc, char **argv) {