tUpdate TODO, perform find on raw strings - 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 7b9298fa4a55f2d4c7fdfcddf3a34c01748f2819
 (DIR) parent 7c68f24f6652203a84104651ceb7ee4790ff8906
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Wed,  7 Aug 2019 14:54:53 +0200
       
       Update TODO, perform find on raw strings
       
       Diffstat:
         M TODO                                |       2 ++
         M find.c                              |      45 ++++++++++++++++++++++++-------
       
       2 files changed, 37 insertions(+), 10 deletions(-)
       ---
 (DIR) diff --git a/TODO b/TODO
       t@@ -11,6 +11,8 @@ Core functionality:
                - Implement syntax highlighting
                - Full unicode support
                - Keep indentation level, adjust with > and <
       +        - Hard-wrap (insert newline)
       +        - Soft-wrap (wrap display of long lines)
        
        Interfacing:
                - Allow call of external processes with commands (sed?, sam?)
 (DIR) diff --git a/find.c b/find.c
       t@@ -10,13 +10,28 @@
        #include "row.h"
        #include "output.h"
        
       +/* reverse of strstr (3) */
       +char*
       +strrstr(char *haystack, char *needle,
       +        ssize_t haystack_length, ssize_t needle_length)
       +{
       +  char *cp;
       +  for (cp = haystack + haystack_length - needle_length;
       +       cp >= haystack;
       +       cp--) {
       +    if (strncmp(cp, needle, needle_length) == 0)
       +        return cp;
       +  }
       +  return NULL;
       +} 
       +
        /* find E.find_query from current cursor position moving in E.direction 
         * if opposite_direction = 0, and opposite E.direction if
         * opposite_direction = 1 */
        void
        editor_find_next_occurence(int opposite_direction)
        {
       -        int y, y_inc, x_offset;
       +        int y, y_inc, x_offset, query_len;
                eRow *row;
                char *match;
        
       t@@ -30,6 +45,7 @@ editor_find_next_occurence(int opposite_direction)
                        y_inc = -1;
        
                x_offset = 0;
       +        query_len = strlen(E.find_query);
                /* from cursor until end of document */
                for (y = E.cursor_y;
                     y != ((y_inc == +1) ? E.num_rows : -1);
       t@@ -37,33 +53,42 @@ editor_find_next_occurence(int opposite_direction)
                        row = &E.row[y];
        
                        if (y == E.cursor_y)
       -                        x_offset = y_inc + editor_row_cursor_x_to_rx(&E.row[E.cursor_y],
       -                                                             E.cursor_x);
       +                        x_offset = y_inc + E.cursor_x;
                        else
                                x_offset = 0;
        
       -                /* TODO: lines are not searched in reverse with opposite_direction */
       -                match = strstr(row->rchars + x_offset, E.find_query);
       -                if (match)
       -                        break;
       +                if (y_inc == +1) {
       +                        match = strstr(row->chars + x_offset, E.find_query);
       +                        editor_set_status_message("'%s', x_offset=%d",
       +                                                  row->chars + x_offset, x_offset);
       +                        if (match)
       +                                break;
       +                } else {
       +                        match = strrstr(row->chars, E.find_query,
       +                                        row->size - x_offset, query_len);
       +                        if (match)
       +                                break;
       +                }
                }
       +
                if (match) {
                        E.cursor_y = y;
       -                E.cursor_x = editor_row_cursor_rx_to_x(row, match - row->rchars);
       +                E.cursor_x = match - row->chars;
                        /* E.row_offset = E.num_rows; */ /* put line to top of screen */
       +
                } else {
                        /* from other end of file until cursor */
                        for (y = (y_inc == +1) ? 0 : E.num_rows - 1;
                             y != E.cursor_y + y_inc;
                             y += y_inc) {
                                row = &E.row[y];
       -                        match = strstr(row->rchars, E.find_query);
       +                        match = strstr(row->chars, E.find_query);
                                if (match)
                                        break;
                        }
                        if (match) {
                                E.cursor_y = y;
       -                        E.cursor_x = editor_row_cursor_rx_to_x(row, match - row->rchars);
       +                        E.cursor_x = editor_row_cursor_rx_to_x(row, match - row->chars);
                        }
                }
        }