diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bace10..08769c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## v0.1 * Switched build to CMake * Completely overhauled code structure + * Separate code into logical blocks + * Purge global variables as much as possible * Add a desktop entry when installing based on [desktopentry](https://st.suckless.org/patches/desktopentry/) diff --git a/src/st/selection.c b/src/st/selection.c new file mode 100644 index 0000000..fd03117 --- /dev/null +++ b/src/st/selection.c @@ -0,0 +1,68 @@ +#include "selection.h" +#include "macros.h" + +void selnormalize() { + int i; + + if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) { + sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x; + sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x; + + } else { + sel.nb.x = MIN(sel.ob.x, sel.oe.x); + sel.ne.x = MAX(sel.ob.x, sel.oe.x); + } + + sel.nb.y = MIN(sel.ob.y, sel.oe.y); + sel.ne.y = MAX(sel.ob.y, sel.oe.y); + + selsnap(&sel.nb.x, &sel.nb.y, -1); + selsnap(&sel.ne.x, &sel.ne.y, +1); + + /* expand selection over line breaks */ + if (sel.type == SEL_RECTANGULAR) + return; + + i = tlinelen(sel.nb.y); + if (i < sel.nb.x) + sel.nb.x = i; + + if (tlinelen(sel.ne.y) <= sel.ne.x) + sel.ne.x = term.col - 1; +} + +void selscroll(int orig, int n) { + if (sel.ob.x == -1) + return; + + if (BETWEEN(sel.nb.y, orig, term.bot) != + BETWEEN(sel.ne.y, orig, term.bot)) { + selclear(); + + } else if (BETWEEN(sel.nb.y, orig, term.bot)) { + sel.ob.y += n; + sel.oe.y += n; + + if (sel.ob.y < term.top || sel.ob.y > term.bot || sel.oe.y < term.top || + sel.oe.y > term.bot) { + selclear(); + + } else { + selnormalize(); + } + } +} + +void selclear(void) { + if (sel.ob.x == -1) + return; + sel.mode = SEL_IDLE; + sel.ob.x = -1; + tsetdirt(sel.nb.y, sel.ne.y); +} + +void selinit(void) { + sel.mode = SEL_IDLE; + sel.snap = 0; + sel.ob.x = -1; +} diff --git a/src/st/selection.h b/src/st/selection.h new file mode 100644 index 0000000..97cdbe3 --- /dev/null +++ b/src/st/selection.h @@ -0,0 +1,36 @@ +#ifndef SELECTION_H +#define SELECTION_H + +enum selection_mode { SEL_IDLE = 0, SEL_EMPTY = 1, SEL_READY = 2 }; +enum selection_type { SEL_REGULAR = 1, SEL_RECTANGULAR = 2 }; +enum selection_snap { SNAP_WORD = 1, SNAP_LINE = 2 }; + +typedef struct { + int mode; + int type; + int snap; + /* + * Selection variables: + * nb – normalized coordinates of the beginning of the selection + * ne – normalized coordinates of the end of the selection + * ob – original coordinates of the beginning of the selection + * oe – original coordinates of the end of the selection + */ + struct { + int x, y; + } nb, ne, ob, oe; + + int alt; +} Selection; + +void selinit(void); +void selstart(int, int, int); +void selnormalize(); +void selscroll(int, int); +void selsnap(int *, int *, int); +void selclear(void); + +// Global variable for now +static Selection sel; + +#endif diff --git a/src/st/st.c b/src/st/st.c index 4f1ee8e..22f7fe0 100644 --- a/src/st/st.c +++ b/src/st/st.c @@ -17,10 +17,11 @@ #include #include -#include "utf8.h" #include "../win.h" #include "macros.h" +#include "selection.h" #include "st.h" +#include "utf8.h" #if defined(__linux) #include @@ -88,24 +89,6 @@ typedef struct { char state; } TCursor; -typedef struct { - int mode; - int type; - int snap; - /* - * Selection variables: - * nb – normalized coordinates of the beginning of the selection - * ne – normalized coordinates of the end of the selection - * ob – original coordinates of the beginning of the selection - * oe – original coordinates of the end of the selection - */ - struct { - int x, y; - } nb, ne, ob, oe; - - int alt; -} Selection; - /* Internal representation of the screen */ typedef struct { int row; /* nb row */ @@ -200,11 +183,6 @@ static void tstrsequence(uchar); static void drawregion(int, int, int, int); -static void selnormalize(void); -static void selscroll(int, int); -static void selsnap(int *, int *, int); - - static char *base64dec(const char *); static char base64dec_getc(const char **); @@ -212,7 +190,6 @@ static ssize_t xwrite(int, const char *, size_t); /* Globals */ static Term term; -static Selection sel; static CSIEscape csiescseq; static STREscape strescseq; static int iofd = 1; @@ -340,12 +317,6 @@ char *base64dec(const char *src) { return result; } -void selinit(void) { - sel.mode = SEL_IDLE; - sel.snap = 0; - sel.ob.x = -1; -} - int tlinelen(int y) { int i = term.col; @@ -401,32 +372,6 @@ void selextend(int col, int row, int type, int done) { sel.mode = done ? SEL_IDLE : SEL_READY; } -void selnormalize(void) { - int i; - - if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) { - sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x; - sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x; - } else { - sel.nb.x = MIN(sel.ob.x, sel.oe.x); - sel.ne.x = MAX(sel.ob.x, sel.oe.x); - } - sel.nb.y = MIN(sel.ob.y, sel.oe.y); - sel.ne.y = MAX(sel.ob.y, sel.oe.y); - - selsnap(&sel.nb.x, &sel.nb.y, -1); - selsnap(&sel.ne.x, &sel.ne.y, +1); - - /* expand selection over line breaks */ - if (sel.type == SEL_RECTANGULAR) - return; - i = tlinelen(sel.nb.y); - if (i < sel.nb.x) - sel.nb.x = i; - if (tlinelen(sel.ne.y) <= sel.ne.x) - sel.ne.x = term.col - 1; -} - int selected(int x, int y) { if (sel.mode == SEL_EMPTY || sel.ob.x == -1 || sel.alt != IS_SET(MODE_ALTSCREEN)) @@ -561,14 +506,6 @@ char *getsel(void) { return str; } -void selclear(void) { - if (sel.ob.x == -1) - return; - sel.mode = SEL_IDLE; - sel.ob.x = -1; - tsetdirt(sel.nb.y, sel.ne.y); -} - void die(const char *errstr, ...) { va_list ap; @@ -970,25 +907,6 @@ void tscrollup(int orig, int n) { selscroll(orig, -n); } -void selscroll(int orig, int n) { - if (sel.ob.x == -1) - return; - - if (BETWEEN(sel.nb.y, orig, term.bot) != - BETWEEN(sel.ne.y, orig, term.bot)) { - selclear(); - } else if (BETWEEN(sel.nb.y, orig, term.bot)) { - sel.ob.y += n; - sel.oe.y += n; - if (sel.ob.y < term.top || sel.ob.y > term.bot || sel.oe.y < term.top || - sel.oe.y > term.bot) { - selclear(); - } else { - selnormalize(); - } - } -} - void tnewline(int first_col) { int y = term.c.y; diff --git a/src/st/st.h b/src/st/st.h index a5667da..f177c08 100644 --- a/src/st/st.h +++ b/src/st/st.h @@ -26,13 +26,6 @@ enum glyph_attribute { ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT, }; -enum selection_mode { SEL_IDLE = 0, SEL_EMPTY = 1, SEL_READY = 2 }; - -enum selection_type { SEL_REGULAR = 1, SEL_RECTANGULAR = 2 }; - -enum selection_snap { SNAP_WORD = 1, SNAP_LINE = 2 }; - - #define Glyph Glyph_ typedef struct { Rune u; /* character code */ diff --git a/src/st/utf8.c b/src/st/utf8.c index f6e3a08..c9553b2 100644 --- a/src/st/utf8.c +++ b/src/st/utf8.c @@ -64,7 +64,7 @@ Rune utf8decodebyte(char p_char, size_t *p_i) { * * @param p_rune rune to encode * @param p_char char array to encode to - * @return + * @return */ size_t utf8encode(Rune p_rune, char *p_char) { size_t len, i; @@ -88,14 +88,16 @@ char utf8encodebyte(Rune u, size_t i) { return utfbyte[i] | (u & ~utfmask[i]); } * Check if a given rune is a valid UTF-8 rune * * @param p_rune rune to validate - * @param i + * @param i */ size_t utf8validate(Rune *p_rune, size_t i) { - if (!BETWEEN(*p_rune, utfmin[i], utfmax[i]) || BETWEEN(*p_rune, 0xD800, 0xDFFF)) + if (!BETWEEN(*p_rune, utfmin[i], utfmax[i]) || + BETWEEN(*p_rune, 0xD800, 0xDFFF)) *p_rune = UTF_INVALID; // Count up i until you find a utfmax entry that's greater than *p_rune - for (i = 1; *p_rune > utfmax[i]; ++i); + for (i = 1; *p_rune > utfmax[i]; ++i) + ; return i; } diff --git a/src/st/utf8.h b/src/st/utf8.h index ed8e5c1..343fd8e 100644 --- a/src/st/utf8.h +++ b/src/st/utf8.h @@ -1,8 +1,8 @@ #ifndef UTF8_H #define UTF8_H -#include #include +#include #define UTF_INVALID 0xFFFD #define UTF_SIZE 4