Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef BUFFER_H | ||
2 | #define BUFFER_H | ||
3 | |||
4 | #include <limits.h> | ||
5 | #include <stdbool.h> | ||
6 | #include <stdint.h> | ||
7 | #include <sys/types.h> | ||
8 | #include <time.h> | ||
9 | #include "block-iter.h" | ||
10 | #include "change.h" | ||
11 | #include "lock.h" | ||
12 | #include "options.h" | ||
13 | #include "syntax/syntax.h" | ||
14 | #include "util/debug.h" | ||
15 | #include "util/list.h" | ||
16 | #include "util/macros.h" | ||
17 | #include "util/ptr-array.h" | ||
18 | #include "util/string-view.h" | ||
19 | |||
20 | /* | ||
21 | * Subset of stat(3) struct, to minimize size bloat. | ||
22 | * Example sizes (glibc 2.38, x86_64): | ||
23 | * | ||
24 | * • sizeof(FileInfo): 56 | ||
25 | * • sizeof(struct stat): 144 | ||
26 | * • Difference: 88 bytes (per Buffer) | ||
27 | */ | ||
28 | typedef struct { | ||
29 | dev_t dev; | ||
30 | ino_t ino; | ||
31 | mode_t mode; | ||
32 | uid_t uid; | ||
33 | gid_t gid; | ||
34 | off_t size; | ||
35 | struct timespec mtime; | ||
36 | } FileInfo; | ||
37 | |||
38 | /* | ||
39 | * A representation of a specific file, as it pertains to editing, | ||
40 | * including text contents, filename (if saved), undo history and | ||
41 | * some file-specific metadata and options. | ||
42 | */ | ||
43 | typedef struct Buffer { | ||
44 | ListHead blocks; // Doubly linked list of Blocks, forming the text contents | ||
45 | Change change_head; | ||
46 | Change *cur_change; | ||
47 | Change *saved_change; // Used to determine if there are unsaved changes | ||
48 | FileInfo file; // File metadata, taken from the most recent stat(3) call | ||
49 | unsigned long id; // Needed for identifying buffers whose filename is NULL | ||
50 | size_t nl; // Total number of lines (sum of all Block::nl counts) | ||
51 | PointerArray views; // Views pointing to this buffer | ||
52 | char *display_filename; // Short filename, as displayed in the tab bar | ||
53 | char *abs_filename; | ||
54 | bool readonly; | ||
55 | bool temporary; | ||
56 | bool stdout_buffer; | ||
57 | bool locked; | ||
58 | bool setup; | ||
59 | bool crlf_newlines; | ||
60 | bool bom; | ||
61 | const char *encoding; // Encoding of the file (buffer always contains UTF-8) | ||
62 | LocalOptions options; | ||
63 | Syntax *syntax; | ||
64 | long changed_line_min; | ||
65 | long changed_line_max; | ||
66 | // Index 0 is always syn->states.ptrs[0]. | ||
67 | // Lowest bit of an invalidated value is 1. | ||
68 | PointerArray line_start_states; | ||
69 | } Buffer; | ||
70 | |||
71 | 21 | static inline void mark_all_lines_changed(Buffer *buffer) | |
72 | { | ||
73 | 21 | buffer->changed_line_min = 0; | |
74 | 21 | buffer->changed_line_max = LONG_MAX; | |
75 | 21 | } | |
76 | |||
77 | 19 | static inline bool buffer_modified(const Buffer *buffer) | |
78 | { | ||
79 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
|
19 | return buffer->saved_change != buffer->cur_change && !buffer->temporary; |
80 | } | ||
81 | |||
82 | 2 | static inline struct View *buffer_get_first_view(const Buffer *buffer) | |
83 | { | ||
84 | 2 | BUG_ON(buffer->views.count == 0); | |
85 | 2 | return buffer->views.ptrs[0]; | |
86 | } | ||
87 | |||
88 | 106 | static inline BlockIter block_iter(Buffer *buffer) | |
89 | { | ||
90 | 106 | return (BlockIter) { | |
91 | 106 | .blk = BLOCK(buffer->blocks.next), | |
92 | 106 | .head = &buffer->blocks, | |
93 | .offset = 0 | ||
94 | }; | ||
95 | } | ||
96 | |||
97 | struct EditorState; | ||
98 | |||
99 | const char *buffer_filename(const Buffer *buffer) NONNULL_ARGS_AND_RETURN; | ||
100 | void buffer_mark_lines_changed(Buffer *buffer, long min, long max) NONNULL_ARGS; | ||
101 | void buffer_set_encoding(Buffer *buffer, const char *encoding, bool utf8_bom) NONNULL_ARGS; | ||
102 | void buffer_set_display_filename(Buffer *buffer, char *name) NONNULL_ARG(1); | ||
103 | void buffer_update_short_filename_cwd(Buffer *buffer, const StringView *home, const char *cwd) NONNULL_ARG(1, 2); | ||
104 | void buffer_update_short_filename(Buffer *buffer, const StringView *home) NONNULL_ARGS; | ||
105 | bool buffer_detect_filetype(Buffer *buffer, const PointerArray *filetypes) NONNULL_ARGS; | ||
106 | void buffer_update_syntax(struct EditorState *e, Buffer *buffer) NONNULL_ARGS; | ||
107 | void buffer_setup(struct EditorState *e, Buffer *buffer) NONNULL_ARGS; | ||
108 | void buffer_count_blocks_and_bytes(const Buffer *buffer, uintmax_t counts[2]) NONNULL_ARGS; | ||
109 | void buffer_remove_unlock_and_free(PointerArray *buffers, Buffer *buffer, const FileLocksContext *locks) NONNULL_ARGS; | ||
110 | void free_blocks(Buffer *buffer) NONNULL_ARGS; | ||
111 | |||
112 | Buffer *find_buffer(const PointerArray *buffers, const char *abs_filename) NONNULL_ARGS; | ||
113 | Buffer *find_buffer_by_id(const PointerArray *buffers, unsigned long id) NONNULL_ARGS; | ||
114 | Buffer *buffer_new(PointerArray *buffers, const GlobalOptions *gopts, const char *encoding) RETURNS_NONNULL NONNULL_ARG(1, 2); | ||
115 | Buffer *open_empty_buffer(PointerArray *buffers, const GlobalOptions *gopts) NONNULL_ARGS_AND_RETURN; | ||
116 | void free_buffers(PointerArray *buffers, const FileLocksContext *locks) NONNULL_ARGS; | ||
117 | |||
118 | #endif | ||
119 |