Enhance jumping

This commit is contained in:
Matúš Púll 2025-04-20 11:56:02 +02:00
parent 8e29aad9e0
commit 61ce077ea0

View file

@ -18,6 +18,18 @@ struct position {
position operator+(count_type offset) { return {r + offset, c}; } position operator+(count_type offset) { return {r + offset, c}; }
}; };
// Utilities
count_type min(count_type a, count_type b) {
if(a <= b)
return a;
return b;
}
count_type max(count_type a, count_type b) {
if(a >= b)
return a;
return b;
}
// Global variables // Global variables
treap file; treap file;
position cur = {0, 0}; position cur = {0, 0};
@ -233,17 +245,9 @@ void move_cursor(char ch) {
} }
// Jump to line // Jump to line
void jump(count_type r) { void jump(count_type r) {
count_type last_start = file.size()-LINES; if(r - file_offset > LINES || r < file_offset)
if(last_start < 0) file_offset = max(0, min(file.size()-LINES, r));
last_start = 0; cur.r = max(0, r - file_offset);
cur.r = 0;
file_offset = r;
if(file_offset > last_start) {
file_offset = last_start;
cur.r = r - file_offset;
if(cur.r >= LINES)
cur.r = LINES-1;
}
jump_line_end(); jump_line_end();
} }
// Jump to position // Jump to position