src/bookmark.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include "bookmark.h" | ||
| 4 | #include "buffer.h" | ||
| 5 | #include "move.h" | ||
| 6 | #include "search.h" | ||
| 7 | #include "selection.h" | ||
| 8 | #include "util/debug.h" | ||
| 9 | |||
| 10 | 1 | FileLocation *get_current_file_location(const View *v) | |
| 11 | { | ||
| 12 | 1 | const Buffer *b = v->buffer; | |
| 13 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1 time.
|
1 | char *filename = b->abs_filename ? xstrdup(b->abs_filename) : NULL; |
| 14 | 1 | return new_file_location(filename, b->id, v->cy + 1, v->cx_char + 1); | |
| 15 | } | ||
| 16 | |||
| 17 | 1 | bool file_location_go(Window *window, ErrorBuffer *ebuf, const FileLocation *loc) | |
| 18 | { | ||
| 19 | 1 | View *view = window_open_buffer(window, loc->filename, true, NULL); | |
| 20 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 13 not taken.
|
1 | if (!view) { |
| 21 | // Failed to open file; error message should be visible | ||
| 22 | return false; | ||
| 23 | } | ||
| 24 | |||
| 25 |
1/2✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 7 not taken.
|
1 | if (window->view != view) { |
| 26 | 1 | set_view(view); | |
| 27 | // Force centering view to cursor, because file changed | ||
| 28 | 1 | view->force_center = true; | |
| 29 | } | ||
| 30 | |||
| 31 |
1/2✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 10 not taken.
|
1 | if (loc->pattern) { |
| 32 |
1/2✓ Branch 9 → 12 taken 1 time.
✗ Branch 9 → 13 not taken.
|
1 | if (!search_tag(view, ebuf, loc->pattern)) { |
| 33 | return false; | ||
| 34 | } | ||
| 35 | ✗ | } else if (loc->line > 0) { | |
| 36 | ✗ | move_to_filepos(view, loc->line, MAX(loc->column, 1)); | |
| 37 | } | ||
| 38 | |||
| 39 | 1 | return unselect(view); | |
| 40 | } | ||
| 41 | |||
| 42 | 1 | static bool file_location_return(Window *window, const FileLocation *loc, const PointerArray *buffers) | |
| 43 | { | ||
| 44 | 1 | Buffer *buffer = find_buffer_by_id(buffers, loc->buffer_id); | |
| 45 | 1 | View *view; | |
| 46 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
|
1 | if (buffer) { |
| 47 | 1 | view = window_find_or_create_view(window, buffer); | |
| 48 | } else { | ||
| 49 | ✗ | if (!loc->filename) { | |
| 50 | // Can't restore closed buffer that had no filename; try again | ||
| 51 | return false; | ||
| 52 | } | ||
| 53 | ✗ | view = window_open_buffer(window, loc->filename, true, NULL); | |
| 54 | } | ||
| 55 | |||
| 56 |
0/2✗ Branch 7 → 8 not taken.
✗ Branch 7 → 11 not taken.
|
1 | if (!view) { |
| 57 | // Open failed; don't try again | ||
| 58 | return true; | ||
| 59 | } | ||
| 60 | |||
| 61 | 1 | set_view(view); | |
| 62 | 1 | move_to_filepos(view, loc->line, loc->column); | |
| 63 | 1 | return unselect(view); | |
| 64 | } | ||
| 65 | |||
| 66 | 303 | void file_location_free(FileLocation *loc) | |
| 67 | { | ||
| 68 | 303 | free(loc->filename); | |
| 69 | 303 | free(loc->pattern); | |
| 70 | 303 | free(loc); | |
| 71 | 303 | } | |
| 72 | |||
| 73 | 302 | size_t bookmark_push(PointerArray *bookmarks, FileLocation *loc) | |
| 74 | { | ||
| 75 | 302 | const size_t max_entries = 256; | |
| 76 |
2/2✓ Branch 2 → 3 taken 44 times.
✓ Branch 2 → 5 taken 258 times.
|
302 | if (bookmarks->count == max_entries) { |
| 77 | 44 | file_location_free(ptr_array_remove_index(bookmarks, 0)); | |
| 78 | } | ||
| 79 | |||
| 80 | 302 | BUG_ON(bookmarks->count >= max_entries); | |
| 81 | 302 | ptr_array_append(bookmarks, loc); | |
| 82 | 302 | return bookmarks->count; | |
| 83 | } | ||
| 84 | |||
| 85 | 1 | size_t bookmark_pop(PointerArray *bookmarks, Window *window, const PointerArray *buffers) | |
| 86 | { | ||
| 87 | 1 | void **ptrs = bookmarks->ptrs; | |
| 88 | 1 | size_t count = bookmarks->count; | |
| 89 | 1 | size_t popped = 0; | |
| 90 | |||
| 91 |
2/2✓ Branch 6 → 3 taken 1 time.
✓ Branch 6 → 7 taken 1 time.
|
2 | for (bool go = true; count > 0 && go; popped++) { |
| 92 | 1 | FileLocation *loc = ptrs[--count]; | |
| 93 | 1 | go = !file_location_return(window, loc, buffers); | |
| 94 | 1 | file_location_free(loc); | |
| 95 | } | ||
| 96 | |||
| 97 | 1 | bookmarks->count = count; | |
| 98 | 1 | return popped; | |
| 99 | } | ||
| 100 |