undo.h - ledit - Text editor (WIP) | |
git clone git://lumidify.org/ledit.git (fast, but not encrypted) | |
git clone https://lumidify.org/ledit.git (encrypted, but very slow) | |
git clone git://4kcetb7mo7hj6grozzybxtotsub5bempzo4lirzc3437amof2c2impyd.onion/… | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
undo.h (4580B) | |
--- | |
1 #ifndef _UNDO_H_ | |
2 #define _UNDO_H_ | |
3 | |
4 #include <stddef.h> | |
5 #include "common.h" | |
6 #include "txtbuf.h" | |
7 | |
8 /* | |
9 * This handles undo and redo. | |
10 * | |
11 * Some things need to be explained: | |
12 * | |
13 * Each stack entry contains text, the text range, and the cursor range. | |
14 * The text is the text that was inserted or deleted, the text range is | |
15 * the exact line and byte range of the text before it was deleted or | |
16 * after it was inserted. The cursor range stores the position of the | |
17 * cursor before and after the operation so it can be restored. | |
18 * | |
19 * Each stack entry has a group, a mode group, and the actual mode store… | |
20 * When pushing an operation onto the stack, start_group can be specifie… | |
21 * If this is 0, the operation belongs together with the previous operat… | |
22 * i.e. they will always be undone or redone together. | |
23 * The current mode of the view is stored with each stack entry so that | |
24 * all operations belonging to the last "session" in insert mode can be | |
25 * undone together. | |
26 * Additionally, undo_change_mode_group should be called when the mode of | |
27 * the view is changed. This is needed to separate different insert sess… | |
28 * so multiple sessions aren't undone together if the user just left ins… | |
29 * mode and then returned immediately to insert mode without doing anyth… | |
30 * in normal mode. | |
31 */ | |
32 | |
33 typedef enum { | |
34 UNDO_NORMAL, | |
35 UNDO_OLDEST_CHANGE, | |
36 UNDO_NEWEST_CHANGE | |
37 } undo_status; | |
38 | |
39 typedef struct undo_stack undo_stack; | |
40 | |
41 /* | |
42 * Callback used for inserting text. | |
43 * This is called with (respectively) the given callback data, the line … | |
44 * the byte index, the text, and the length of the text. | |
45 */ | |
46 typedef void (*undo_insert_callback)(void *, size_t, size_t, char *, siz… | |
47 | |
48 /* | |
49 * Callback used for deleting text. | |
50 * This is called with (respectively) the given callback data, the line … | |
51 * byte index of the beginning of the range, and the line and byte index… | |
52 * the end of the range. | |
53 */ | |
54 typedef void (*undo_delete_callback)(void *, size_t, size_t, size_t, siz… | |
55 | |
56 /* | |
57 * Create an empty undo stack. | |
58 */ | |
59 undo_stack *undo_stack_create(void); | |
60 | |
61 /* | |
62 * Destroy an undo stack. | |
63 */ | |
64 void undo_stack_destroy(undo_stack *undo); | |
65 | |
66 /* | |
67 * Change the mode group. | |
68 * This does not actually change the mode group yet, it just sets a bit | |
69 * so the mode group is changed the next time an item is pushed on the s… | |
70 */ | |
71 void undo_change_mode_group(undo_stack *undo); | |
72 | |
73 /* | |
74 * Push an insert action onto the undo stack. | |
75 * See documentation at top for details on the other arguments. | |
76 * 'text' is copied, so the original should be freed. | |
77 */ | |
78 void undo_push_insert( | |
79 undo_stack *undo, txtbuf *text, | |
80 ledit_range insert_range, ledit_range cursor_range, | |
81 int start_group, ledit_mode mode | |
82 ); | |
83 | |
84 /* | |
85 * Push an delete action onto the undo stack. | |
86 * See documentation at top for details on the other arguments. | |
87 * 'text' is copied, so the original should be freed. | |
88 */ | |
89 void undo_push_delete( | |
90 undo_stack *undo, txtbuf *text, | |
91 ledit_range delete_range, ledit_range cursor_range, | |
92 int start_group, ledit_mode mode | |
93 ); | |
94 | |
95 /* | |
96 * Undo one step. | |
97 * 'cur_line_ret' and 'cur_index_ret' are set to the new cursor position. | |
98 * 'min_line_ret' is set to the minimum line that was touched so the lin… | |
99 * can be recalculated properly. | |
100 * WARNING: If nothing was changed, 'min_line_ret' will be SIZE_MAX. | |
101 */ | |
102 undo_status ledit_undo( | |
103 undo_stack *undo, ledit_mode mode, | |
104 void *callback_data, undo_insert_callback insert_cb, undo_delete_cal… | |
105 size_t *cur_line_ret, size_t *cur_index_ret, size_t *min_line_ret | |
106 ); | |
107 | |
108 /* | |
109 * Redo one step. | |
110 * 'cur_line_ret' and 'cur_index_ret' are set to the new cursor position. | |
111 * 'min_line_ret' is set to the minimum line that was touched so the lin… | |
112 * can be recalculated properly. | |
113 * WARNING: If nothing was changed, 'min_line_ret' will be SIZE_MAX. | |
114 */ | |
115 undo_status ledit_redo( | |
116 undo_stack *undo, ledit_mode mode, | |
117 void *callback_data, undo_insert_callback insert_cb, undo_delete_cal… | |
118 size_t *cur_line_ret, size_t *cur_index_ret, size_t *min_line_ret | |
119 ); | |
120 | |
121 /* | |
122 * Change the cursor range stored for the last operation. | |
123 * This is sort of a hack used by view_delete_range and some other | |
124 * functions to set the cursor range later if it isn't known yet before | |
125 * inserting/deleting text. | |
126 * Fails silently if the stack is empty. | |
127 */ | |
128 void undo_change_last_cur_range(undo_stack *undo, ledit_range cur_range); | |
129 | |
130 /* | |
131 * Get a string corresponding to an undo_status. | |
132 * This string should not be freed. | |
133 */ | |
134 char *undo_state_to_str(undo_status s); | |
135 | |
136 #if TEST | |
137 void dump_undo_stack(FILE *file, undo_stack *undo); | |
138 #endif | |
139 | |
140 #endif |