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