tBegin implementing find. Enter on blank prompt also cancels - ve - a minimal text editor (work in progress)
 (HTM) git clone git://src.adamsgaard.dk/ve
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 132aa10bd388dc5e4a3901bfcc6d8cc204d6cce9
 (DIR) parent 1a85107c4a21f432db608e9028181cc8b29901a7
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Tue,  6 Aug 2019 22:17:16 +0200
       
       Begin implementing find. Enter on blank prompt also cancels
       
       Diffstat:
         A find.c                              |      29 +++++++++++++++++++++++++++++
         A find.h                              |       6 ++++++
         M input.c                             |       7 ++++++-
         M row.c                               |      16 ++++++++++++++++
         M row.h                               |       1 +
       
       5 files changed, 58 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/find.c b/find.c
       t@@ -0,0 +1,29 @@
       +#include <stdlib.h>
       +#include <string.h>
       +#include "input.h"
       +#include "ve.h"
       +#include "row.h"
       +
       +void
       +editor_find()
       +{
       +        char *query, *match;
       +        int i;
       +        eRow *row;
       +
       +        query = editor_prompt("/%s");
       +        if (query == NULL)
       +                return;
       +
       +        for (i=0; i<E.num_rows; ++i) {
       +                row = &E.row[i];
       +                match = strstr(row->rchars, query);
       +                if (match) {
       +                        E.cursor_y = i;
       +                        E.cursor_x = editor_row_cursor_rx_to_x(row, match - row->rchars);
       +                        E.row_offset = E.num_rows;
       +                        break;
       +                }
       +        }
       +        free(query);
       +}
 (DIR) diff --git a/find.h b/find.h
       t@@ -0,0 +1,6 @@
       +#ifndef FIND_H_
       +#define FIND_H_
       +
       +void editor_find();
       +
       +#endif
 (DIR) diff --git a/input.c b/input.c
       t@@ -8,6 +8,7 @@
        #include "output.h"
        #include "io.h"
        #include "row.h"
       +#include "find.h"
        
        #define CTRL_KEY(k) ((k) & 0x1f)
        
       t@@ -34,7 +35,7 @@ editor_prompt(char *prompt)
                        if (c == CTRL_KEY('h') || c == 127) { /* detect backspace */
                                if (buflen != 0)
                                        buf[--buflen] = '\0';
       -                } else if (c == '\x1b') { /* detect escape */
       +                } else if (c == '\x1b' || (c == '\r' && !buflen)) { /* detect escape */
                                editor_set_status_message("");
                                free(buf);
                                return NULL;
       t@@ -206,6 +207,10 @@ editor_process_keypress()
                                                E.cursor_x = E.row[E.cursor_y].size;
                                        E.mode = 1;
                                        break;
       +
       +                        case '/':
       +                                editor_find();
       +                                break;
                        }
                } else if (E.mode == 1) {  /* insert mode */
                        switch (c) {
 (DIR) diff --git a/row.c b/row.c
       t@@ -16,6 +16,22 @@ editor_row_cursor_x_to_rx(eRow *row, int cursor_x)
                return rx;
        }
        
       +/* translate on-screen position to data position */
       +int
       +editor_row_cursor_rx_to_x(eRow *row, int cursor_rx)
       +{
       +        int cur_rx, cx;
       +        for (cx=0; cx<row->size; ++cx) {
       +                if (row->chars[cx] == '\t')
       +                        cur_rx += (TAB_WIDTH - 1) - (cur_rx % TAB_WIDTH);
       +                cur_rx++;
       +
       +                if (cur_rx > cursor_rx)
       +                        return cx;
       +        }
       +        return cx;
       +}
       +
        /* translate tabs before display */
        void
        editor_row_update(eRow* row)
 (DIR) diff --git a/row.h b/row.h
       t@@ -4,6 +4,7 @@
        #include "ve.h"
        
        int editor_row_cursor_x_to_rx(eRow *row, int cursor_x);
       +int editor_row_cursor_rx_to_x(eRow *row, int cursor_rx);
        void editor_row_update(eRow* row);
        void editor_row_append_string(eRow *row, char *s, size_t len);
        void editor_row_insert(int i, char *s, size_t len);