+ int console_width = getmaxx(stdscr);
+ int console_height = getmaxy(stdscr);
+
+ int nb_printed_lines = 1, last_printer_line = -1;
+
+ // First, we find a visible line. In priority: The current, or the
+ // first visible after it, or the first visible before it.
+
+ int new_line;
+ if(match(lines[*current_line], regexp)) {
+ new_line = *current_line;
+ } else {
+ new_line = next_visible(*current_line, nb_lines, lines, regexp);
+ if(new_line < 0) {
+ new_line = previous_visible(*current_line, nb_lines, lines, regexp);
+ }
+ }
+
+ // If we found a visible line and we should move, let's move
+
+ if(new_line >= 0 && motion != 0) {
+ int l = new_line;
+ l += motion;
+
+ if(motion > 0) {
+ // We want to go down, let's find the first visible line below
+ l = next_visible(new_line, nb_lines, lines, regexp);
+ if(l >= 0) {
+ new_line = l;
+ }
+ } else {
+ // We want to go up, let's find the first visible line above
+ l = previous_visible(new_line, nb_lines, lines, regexp);
+ if(l >= 0) {
+ new_line = l;
+ }
+ }
+ }