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