diff --git a/src/nnn.c b/src/nnn.c index 4615db50..3d66b1dd 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -109,6 +109,8 @@ #define LEN(x) (sizeof(x) / sizeof(*(x))) #undef MIN #define MIN(x, y) ((x) < (y) ? (x) : (y)) +#undef MAX +#define MAX(x, y) ((x) > (y) ? (x) : (y)) #define ISODD(x) ((x) & 1) #define ISBLANK(x) ((x) == ' ' || (x) == '\t') #define TOUPPER(ch) \ @@ -3155,19 +3157,20 @@ nochange: // Handle clicking on a file: if (2 <= event.y && event.y < xlines - 2) { - r = event.y - 2; + // Get index of the first file listed on-screen: + r = MAX(0, MIN(cur-((xlines-4)>>1), ndents-(xlines-4))); + // Add the mouse click position to get the clicked file: + r += event.y - 2; if (r >= ndents) goto nochange; - if (ndents > (xlines - 4) && cur >= ((xlines - 4) >> 1)) - cur -= ((xlines - 4) >> 1) - r; - else - cur = r; + cur = r; // Single click just selects, double click also opens if (event.bstate != BUTTON1_DOUBLE_CLICKED) break; + // fallthrough to select the file } else goto nochange; // fallthrough case SEL_NAV_IN: // fallthrough