[bim] Various updates, included backwards search

This commit is contained in:
K. Lange 2018-08-30 22:02:55 +09:00
parent 5a2d292d3c
commit c4334830e5

View File

@ -31,6 +31,11 @@
* - Character selection
*/
#define _XOPEN_SOURCE 500
#define _DARWIN_C_SOURCE 1
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#define _SORTIX_SOURCE
#define __BSD_VISIBLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -3643,6 +3648,36 @@ void find_match(int from_line, int from_col, int * out_line, int * out_col, char
}
}
/**
* Search backwards for matching string.
*/
void find_match_backwards(int from_line, int from_col, int * out_line, int * out_col, char * str) {
int col = from_col;
for (int i = from_line; i >= 1; --i) {
line_t * line = env->lines[i-1];
int j = col - 1;
while (j > -1) {
int k = j;
char * match = str;
while (k < line->actual + 1) {
if (*match == '\0') {
*out_line = i;
*out_col = j + 1;
return;
}
/* TODO search for UTF-8 sequences? */
if (*match != line->text[k].codepoint) break;
match++;
k++;
}
j--;
}
col = (i > 1) ? (env->lines[i-2]->actual) : -1;
}
}
/**
* Draw the matched search result.
*/
@ -3714,8 +3749,6 @@ void search_mode(void) {
find_match(prev_line, prev_col, &line, &col, buffer);
if (line != -1) {
env->coffset = 0;
env->offset = line - 1;
env->col_no = col;
env->line_no = line;
}
@ -3744,8 +3777,6 @@ void search_mode(void) {
find_match(prev_line, prev_col, &line, &col, buffer);
if (line != -1) {
env->coffset = 0;
env->offset = line - 1;
env->col_no = col;
env->line_no = line;
} else {
@ -3773,8 +3804,25 @@ void search_next(void) {
if (line == -1) return;
}
env->coffset = 0;
env->offset = line - 1;
env->col_no = col;
env->line_no = line;
draw_search_match(line, env->search, 0);
}
/**
* Find the previous search result, or loop to the end of the file.
*/
void search_prev(void) {
if (!env->search) return;
int line = -1, col = -1;
find_match_backwards(env->line_no, env->col_no-1, &line, &col, env->search);
if (line == -1) {
render_error("no match");
find_match_backwards(env->line_count, env->lines[env->line_count-1]->actual, &line, &col, env->search);
if (line == -1) return;
}
env->col_no = col;
env->line_no = line;
draw_search_match(line, env->search, 0);
@ -4601,6 +4649,9 @@ void line_selection_mode(void) {
case 'n':
search_next();
break;
case 'N':
search_prev();
break;
case 'j':
cursor_down();
break;
@ -5176,6 +5227,9 @@ int main(int argc, char * argv[]) {
case 'n':
search_next();
break;
case 'N':
search_prev();
break;
case 'j':
cursor_down();
break;
@ -5188,16 +5242,6 @@ int main(int argc, char * argv[]) {
case 'l':
cursor_right();
break;
case 'd':
remove_line(env->lines, env->line_no-1);
env->col_no = 1;
if (env->line_no > env->line_count) {
env->line_no--;
}
redraw_text();
set_modified();
place_cursor_actual();
break;
case ' ':
goto_line(env->line_no + global_config.term_height - 6);
break;