4coder»Forums
8 posts
Repeat last search
Edited by nephewtom on Reason: Initial post
Hi.
Is it possible to repeat last search?
Let's say I hit ctrl f and type a string, for example Interactive.
Then quit and do some other stuff.

Is there a way to hit some key and repeat the same search for Interactive?
Or may be hit ctrl f and other key, and repeat the search for it.

Best Regards.
Simon Anciaux
1337 posts
Repeat last search
Edited by Simon Anciaux on Reason: Correction
EDIT: actually there is a way: start a search and pres CTRL + Return or CTRL + Tab to search the last term.

By default you can't do that. It possible to implement yourself in the custom layer if you have the paid version.
8 posts
Repeat last search
I have it. Any hints?
Recently got 4coder and just have changed some key bindings to match Emacs (C-n, C-p, C-a, C-e, C-k ...)

Regards.
Simon Anciaux
1337 posts
Repeat last search
Edited by Simon Anciaux on Reason: wording
The search functions are implemented in 4coder_base_commands.cpp. The "search", "reverse_search", "search_identifier", "reverse_search_identifier" functions call "isearch" and "isearch__update_highlight" to do the actual search. So you could copy the search_xx and reverse_search_xx function and modify them to search for the last search term.

Here are my search functions. I don't guarantee that they will work or that they have the same behavior as 4coder or other editor. It's intended to be somewhat similar to VIM search. It uses global variables, I never took the time to use the managed scopes to re-implement them.

The history part is a "module" that I re-use in other places of my custom layer.
You need to bind:
- "search_query" to start a search by typing a search term and go to the first instance forward;
- "search_query_identifier" to start a search with the word under the cursor and go to the first instance forward;
- "search_next" to go to the next instance of the search term;
- "search_previous" to go to the previous instance of the search term;
- "search_return_to_start" to go back to where you started the search;
- "search_clear" to clear the search term and remove the highlight in the file (if you use the second code sample);

When the end of the file is reached, the search will continue from the top of the file.
When you use search_query you can use UP and DOWN arrow to find previous term you searched (it may behave weird).
If you switch buffer you can use search_next and search_previous to search the term in the new buffer.
search_return_to_start will only go back in the last buffer you searched.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// MARK history

typedef struct History {
    
    char* buffer;
    int bufferSize;
    int maxEntryCount;
    int entryMaxLength;
    int lastWrittenEntry;
    int lastReadEntry;
    int entryCount;
} History;

History history_init( void* buffer, int bufferSize, int entryMaxLength ) {
    
    History h = { };
    
    h.buffer = ( char* ) buffer;
    h.bufferSize = bufferSize;
    h.entryMaxLength = entryMaxLength;
    h.maxEntryCount = bufferSize / entryMaxLength;
    h.lastWrittenEntry = -1;
    h.lastReadEntry = -1;
    h.entryCount = 0;
    
    return h;
}

int history_push( History* history, char* string, int length, int entry = -1 ) {
    
    if ( entry >= 0 && entry < history->entryCount ) {
        
        history->lastWrittenEntry = entry;
        
    } else {
        
        if ( history->entryCount < history->maxEntryCount - 1 ) {
            history->entryCount++;
        }
        
        if ( history->lastWrittenEntry < history->entryCount - 1 ) {
            history->lastWrittenEntry++;
        } else {
            history->lastWrittenEntry = 0;
        }
    }
    
    char* destination = history->buffer + ( history->entryMaxLength * history->lastWrittenEntry );
    length = ( length > history->entryMaxLength - 1 ) ? history->entryMaxLength - 1 : length;
    
    for ( int c = 0; c < length; c++ ) {
        *destination++ = *string++;
    }
    
    *destination = '\0';
    
    history->lastReadEntry = history->lastWrittenEntry;
    
    return history->lastWrittenEntry;
}

void history_pop( History* history ) {
    
    if ( history->entryCount > 0 ) {
        
        for ( int i = history->lastWrittenEntry; i < history->entryCount - 1; i++ ) {
            
            char* destination = history->buffer + ( history->entryMaxLength * i );
            char* source = destination + history->entryMaxLength;
            
            while ( *source != '\0' ) {
                *destination++ = *source++;
            }
            
            ( *destination ) = '\0';
        }
        
        history->entryCount--;
        history->lastWrittenEntry--;
        
        if ( history->lastWrittenEntry < 0 ) {
            history->lastWrittenEntry = history->entryCount - 1;
        }
    }
}

int history_getLast( History* history, char* string, int* length ) {
    
    *length = 0;
    
    if ( history->entryCount > 0 ) {
        
        history->lastReadEntry = history->lastWrittenEntry;
        char* source = history->buffer + ( history->entryMaxLength * history->lastReadEntry );
        
        while ( *source != '\0' ) {
            *string++ = *source++;
            ( *length )++;
        }
    }
    
    return history->lastReadEntry;
}

int history_getOlder( History* history, char* string, int* length ) {
    
    *length = 0;
    
    if ( history->entryCount > 0 ) {
        
        history->lastReadEntry--;
        
        if ( history->lastReadEntry < 0 ) {
            history->lastReadEntry = history->entryCount - 1;
        }
        
        char* source = history->buffer + ( history->entryMaxLength * history->lastReadEntry );
        
        while ( *source != '\0' ) {
            *string++ = *source++;
            ( *length )++;
        }
    }
    
    return history->lastReadEntry;
}

int history_getNewer( History* history, char* string, int* length ) {
    
    *length = 0;
    
    if ( history->entryCount > 0 ) {
        
        history->lastReadEntry++;
        
        if ( history->lastReadEntry >= history->entryCount ) {
            history->lastReadEntry = 0;
        }
        
        char* source = history->buffer + ( history->entryMaxLength * history->lastReadEntry );
        
        while ( *source != '\0' ) {
            *string++ = *source++;
            ( *length )++;
        }
    }
    
    return history->lastReadEntry;
}

// MARK search

#define MAX_SEARCH_LENGTH 256
static char searchBuffer[ 10 ][ MAX_SEARCH_LENGTH ] = { };
History searchHistory = history_init( searchBuffer, sizeof( searchBuffer ), MAX_SEARCH_LENGTH );

int32_t search_buffer_id = 0;
int32_t search_start_position = 0;
int32_t search_last_position = 0;
char search_query_space[ MAX_SEARCH_LENGTH ];
int32_t search_query_size;

static void search_query_( Application_Links* app, String query ) {
    
    View_Summary view = get_active_view( app, AccessProtected );
    Buffer_Summary buffer = get_buffer( app, view.buffer_id, AccessProtected );
    
    if ( buffer.exists ) {
        
        search_buffer_id = buffer.buffer_id;
        Query_Bar bar = { 0 };
        
        if ( start_query_bar( app, &bar, 0 ) != 0 ) {
            
            int32_t start_pos = view.cursor.pos;
            search_start_position = start_pos;
            Range match = make_range( start_pos, start_pos );
            
            if ( query.size > 0 ) {
                match.max += query.size;
            }
            
            bar.string = make_fixed_width_string( search_query_space );
            copy_ss( &bar.string, query );
            bar.prompt = make_lit_string( "Search: " );
            int32_t entry = -1;
            
            Managed_Scope view_scope = view_get_managed_scope( app, view.view_id );
            Managed_Object highlight = alloc_buffer_markers_on_buffer( app, buffer.buffer_id, 2, &view_scope );
            Marker_Visual visual = create_marker_visual( app, highlight );
            marker_visual_set_effect( app, visual,
                                     VisualType_CharacterHighlightRanges,
                                     SymbolicColorFromPalette( Stag_Highlight ),
                                     SymbolicColorFromPalette( Stag_At_Highlight ), 0 );
            marker_visual_set_view_key( app, visual, view.view_id );
            isearch__update_highlight( app, &view, highlight, match.start, match.end );
            
            User_Input in = { 0 };
            
            for ( ; ; ) {
                
                b32 suppress_highligh_update = false;
                
                if ( match.min != match.max ) {
                    center_view( app );
                }
                
                in = get_user_input( app, EventOnAnyKey, EventOnEsc );
                
                if ( in.abort ) {
                    break;
                }
                
                uint8_t character[ 4 ];
                uint32_t length = to_writable_character( in, character );
                
                if ( in.key.keycode == '\n' ) {
                    
                    break;
                    
                } else if ( length != 0 && key_is_unmodified( &in.key ) ) {
                    
                    append_ss( &bar.string, make_string( character, length ) );
                    
                } else if ( in.key.keycode == key_back ) {
                    
                    backspace_utf8( &bar.string );
                    
                } else if ( in.key.keycode == key_up ) {
                    
                    int32_t savedEntry = entry;
                    entry = history_getOlder( &searchHistory, bar.string.str, &bar.string.size );
                    
                    if ( savedEntry == entry ) {
                        entry = -1;
                        bar.string.size = 0;
                    }
                    
                } else if ( in.key.keycode == key_down ) {
                    
                    int32_t savedEntry = entry;
                    entry = history_getNewer( &searchHistory, bar.string.str, &bar.string.size );
                    
                    if ( savedEntry == entry ) {
                        entry = -1;
                        bar.string.size = 0;
                    }
                }
                
                search_query_size = bar.string.size;
                
                if ( in.command.command == mouse_wheel_scroll ) {
                    mouse_wheel_scroll( app );
                    suppress_highligh_update = true;
                }
                
                int32_t new_pos = 0;
                buffer_seek_string_insensitive_forward( app, &buffer, start_pos, 0, bar.string.str, bar.string.size, &new_pos );
                
                if ( new_pos >= buffer.size ) {
                    buffer_seek_string_insensitive_backward( app, &buffer, start_pos, 0, bar.string.str, bar.string.size, &new_pos );
                }
                
                if ( new_pos < buffer.size && new_pos >= 0 ) {
                    match.min = new_pos;
                    match.max = match.min + bar.string.size;
                }
                
                if ( !suppress_highligh_update ){
                    isearch__update_highlight( app, &view, highlight, match.start, match.end );
                }
            }
            
            managed_object_free( app, highlight );
            
            if ( in.abort ) {
                
                view_set_cursor( app, &view, seek_pos( search_start_position ), true );
                center_view( app );
                
            } else {
                
                if ( match.max - match.min > 0 ) {
                    view_set_cursor( app, &view, seek_pos( match.min ), true );
                    search_query_size = bar.string.size;
                    search_last_position = match.min;
                    history_push( &searchHistory, bar.string.str, bar.string.size, -1 );
                }
            }
        }
    }
}

CUSTOM_COMMAND_SIG( search_next ) {
    
    uint32_t access = AccessProtected;
    
    View_Summary view = get_active_view( app, access );
    Buffer_Summary buffer = get_buffer( app, view.buffer_id, access );
    
    if ( buffer.exists ) {
        
        if ( search_buffer_id != buffer.buffer_id ) {
            search_start_position = search_last_position = view.cursor.pos;
            search_buffer_id = buffer.buffer_id;
        }
        
        if ( search_query_size > 0 ) {
            
            int32_t new_pos = 0;
            buffer_seek_string_insensitive_forward( app, &buffer, search_last_position + 1, 0, search_query_space, search_query_size, &new_pos );
            
            if ( new_pos >= buffer.size && search_last_position != 0 ) {
                buffer_seek_string_insensitive_forward( app, &buffer, 0, 0, search_query_space, search_query_size, &new_pos );
            }
            
            if ( new_pos < buffer.size ) {
                
                Range match = { 0 };
                match.min = new_pos;
                match.max = match.min + search_query_size;
                search_last_position = match.min;
                
                view_set_cursor( app, &view, seek_pos( match.min ), true );
                center_view( app );
            }
        }
    }
}

CUSTOM_COMMAND_SIG( search_previous ) {
    
    uint32_t access = AccessProtected;
    
    View_Summary view = get_active_view( app, access );
    Buffer_Summary buffer = get_buffer( app, view.buffer_id, access );
    
    if ( buffer.exists ) {
        
        if ( search_buffer_id != buffer.buffer_id ) {
            search_start_position = search_last_position = view.cursor.pos;
            search_buffer_id = buffer.buffer_id;
        }
        
        if ( search_query_size > 0 ) {
            
            int32_t new_pos = 0;
            buffer_seek_string_insensitive_backward( app, &buffer, search_last_position - 1, 0, search_query_space, search_query_size, &new_pos );
            
            if ( new_pos < 0 && search_last_position != buffer.size ) {
                buffer_seek_string_insensitive_backward( app, &buffer, buffer.size - 1, 0, search_query_space, search_query_size, &new_pos );
            }
            
            if ( new_pos >= 0 ) {
                
                Range match = { 0 };
                match.min = new_pos;
                match.max = match.min + search_query_size;
                search_last_position = match.min;
                
                view_set_cursor( app, &view, seek_pos( match.min ), true );
                center_view( app );
            }
        }
    }
}

CUSTOM_COMMAND_SIG( search_query ) {
    String query = { 0 };
    search_query_( app, query );
}

CUSTOM_COMMAND_SIG( search_query_identifier ) {
    
    View_Summary view = get_active_view( app, AccessProtected );
    Buffer_Summary buffer = get_buffer( app, view.buffer_id, AccessProtected );
    
    if ( buffer.exists ) {
        
        Range range = { 0 };
        String query = read_identifier_at_pos( app, &buffer, view.cursor.pos, search_query_space, MAX_SEARCH_LENGTH, &range );
        view_set_cursor( app, &view, seek_pos( range.min ), true );
        
        search_buffer_id = buffer.buffer_id;
        search_start_position = range.min;
        search_last_position = range.min;
        search_query_size = query.size;
        history_push( &searchHistory, query.str, query.size, -1 );
        search_next( app );
    }
}

CUSTOM_COMMAND_SIG( search_return_to_start ) {
    
    View_Summary view = get_active_view( app, AccessProtected );
    Buffer_Summary buffer = get_buffer( app, view.buffer_id, AccessProtected );
    
    if ( buffer.exists && search_buffer_id == buffer.buffer_id ) {
        view_set_cursor( app, &view, seek_pos( search_start_position ), true );
        center_view( app );
    }
}

CUSTOM_COMMAND_SIG( search_clear ) {
    search_query_size = 0;
}


And if you want search highlight you need to copy the following code at the end of the render_caller (create a copy of default_render_caller from 4coder_default_hooks.cpp), just before "do_core_render(app);".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// NOTE simon: highlight search
    if ( search_query_size ) {
        
        Theme_Color color;
        color.tag = Stag_Text_Cycle_3;
        get_theme_colors(app, &color, 1);
        
        Temp_Memory temp = begin_temp_memory(scratch);
        int32_t text_size = on_screen_range.one_past_last - on_screen_range.first;
        char *text = push_array(scratch, char, text_size);
        buffer_read_range(app, &buffer, on_screen_range.first, on_screen_range.one_past_last, text);
        
        Highlight_Record *records = push_array(scratch, Highlight_Record, 0);
        String tail = make_string(text, text_size);
        for (int32_t i = 0; i < text_size; tail.str += 1, tail.size -= 1, i += 1){
            if (match_part_insensitive(tail, make_string( search_query_space, search_query_size ))){
                Highlight_Record *record = push_array(scratch, Highlight_Record, 1);
                record->first = i + on_screen_range.first;
                record->one_past_last = record->first + search_query_size;
                record->color = color.color;
                tail.str += search_query_size - 1;
                tail.size -= search_query_size - 1;
                i += search_query_size - 1;
            }
        }
        int32_t record_count = (int32_t)(push_array(scratch, Highlight_Record, 0) - records);
        push_array(scratch, Highlight_Record, 1);
        
        if (record_count > 0){
            sort_highlight_record(records, 0, record_count);
            Temp_Memory marker_temp = begin_temp_memory(scratch);
            Marker *markers = push_array(scratch, Marker, 0);
            int_color current_color = records[0].color;
            {
                Marker *marker = push_array(scratch, Marker, 2);
                marker[0].pos = records[0].first;
                marker[1].pos = records[0].one_past_last;
            }
            for (int32_t i = 1; i <= record_count; i += 1){
                bool32 do_emit = i == record_count || (records[i].color != current_color);
                if (do_emit){
                    int32_t marker_count = (int32_t)(push_array(scratch, Marker, 0) - markers);
                    Managed_Object o = alloc_buffer_markers_on_buffer(app, buffer.buffer_id, marker_count, &render_scope);
                    managed_object_store_data(app, o, 0, marker_count, markers);
                    Marker_Visual v = create_marker_visual(app, o);
                    marker_visual_set_effect(app, v,
                                             VisualType_CharacterHighlightRanges,
                                             SymbolicColor_Transparent, current_color, 0);
                    marker_visual_set_priority(app, v, VisualPriority_Lowest);
                    end_temp_memory(marker_temp);
                    current_color = records[i].color;
                }
                
                Marker *marker = push_array(scratch, Marker, 2);
                marker[0].pos = records[i].first;
                marker[1].pos = records[i].one_past_last;
            }
        }
        
        end_temp_memory(temp);
    }
John
46 posts
Repeat last search
@nephewtom, Simon(mrmixer) knows 4coder inside-out, so his answer is the best one could ask!

I've made my own naive but simple modification to the isearch function in 4coder_base_commands.cpp, which seems to work but I don't know its implications. I replaced the line:
1
copy(&bar.string, query_init);

with
1
copy(&bar.string, previous_isearch_query);


And speaking of modifications to 4coder's search, I'd love to know if someone has managed to allow pasting from clipboard to the search prompt.
Simon Anciaux
1337 posts
Repeat last search
I just look at the recent version of the code and there is a way by default to search for the last term.

Just start a search and press CTRL + Return or CTRL + TAB.

Sorry for saying it wasn't possible.
8 posts
Repeat last search
mrmixer
I just look at the recent version of the code and there is a way by default to search for the last term.

Just start a search and press CTRL + Return or CTRL + TAB.

Sorry for saying it wasn't possible.


Yes, that made it.
Anyway, thanks for the explanations about the code.

Regards.