-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_eol.cpp
More file actions
140 lines (122 loc) · 4.92 KB
/
4coder_eol.cpp
File metadata and controls
140 lines (122 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* 4coder_eol.cpp - Commands and routines for controling the end-of-line encoding
* of files.
*/
// TOP
function void
rewrite_lines_to_crlf(Application_Links *app, Buffer_ID buffer){
ProfileScope(app, "rewrite lines to crlf");
Scratch_Block scratch(app);
i64 size = buffer_get_size(app, buffer);
Batch_Edit *first = 0;
Batch_Edit *last = 0;
ProfileBlockNamed(app, "build batch edit", profile_batch);
i64 pos = -1;
Character_Predicate pred_cr = character_predicate_from_character('\r');
Character_Predicate pred_lf = character_predicate_from_character('\n');
Character_Predicate pred = character_predicate_or(&pred_cr, &pred_lf);
for (;;){
String_Match match = buffer_seek_character_class(app, buffer, &pred,
Scan_Forward, pos);
if (match.range.min == match.range.max){
break;
}
pos = match.range.min;
u8 c1 = buffer_get_char(app, buffer, pos);
u8 c2 = buffer_get_char(app, buffer, pos + 1);
if (c1 == '\r'){
if (pos + 1 == size || c2 != '\n'){
Batch_Edit *edit = push_array(scratch, Batch_Edit, 1);
sll_queue_push(first, last, edit);
edit->edit.text = string_u8_litexpr("");
edit->edit.range = match.range;
}
else{
pos += 1;
}
}
else{
Batch_Edit *edit = push_array(scratch, Batch_Edit, 1);
sll_queue_push(first, last, edit);
edit->edit.text = string_u8_litexpr("\r");
edit->edit.range = Ii64(pos);
}
}
ProfileCloseNow(profile_batch);
buffer_batch_edit(app, buffer, first);
}
function void
rewrite_lines_to_lf(Application_Links *app, Buffer_ID buffer){
ProfileScope(app, "rewrite lines to lf");
Scratch_Block scratch(app);
Batch_Edit *first = 0;
Batch_Edit *last = 0;
ProfileBlockNamed(app, "build batch edit", profile_batch);
i64 pos = -1;
Character_Predicate pred = character_predicate_from_character('\r');
for (;;){
String_Match match = buffer_seek_character_class(app, buffer, &pred,
Scan_Forward, pos);
if (match.range.min == match.range.max){
break;
}
pos = match.range.min;
Batch_Edit *edit = push_array(scratch, Batch_Edit, 1);
sll_queue_push(first, last, edit);
edit->edit.text = string_u8_litexpr("");
edit->edit.range = match.range;
}
ProfileCloseNow(profile_batch);
buffer_batch_edit(app, buffer, first);
}
////////////////////////////////
CUSTOM_COMMAND_SIG(set_eol_mode_to_crlf)
CUSTOM_DOC("Puts the buffer in crlf line ending mode.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
Managed_Scope scope = buffer_get_managed_scope(app, buffer);
Line_Ending_Kind *eol_setting = scope_attachment(app, scope, buffer_eol_setting,
Line_Ending_Kind);
if (eol_setting != 0){
*eol_setting = LineEndingKind_CRLF;
}
}
CUSTOM_COMMAND_SIG(set_eol_mode_to_lf)
CUSTOM_DOC("Puts the buffer in lf line ending mode.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
Managed_Scope scope = buffer_get_managed_scope(app, buffer);
Line_Ending_Kind *eol_setting = scope_attachment(app, scope, buffer_eol_setting,
Line_Ending_Kind);
if (eol_setting != 0){
*eol_setting = LineEndingKind_LF;
}
}
CUSTOM_COMMAND_SIG(set_eol_mode_to_binary)
CUSTOM_DOC("Puts the buffer in bin line ending mode.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
Managed_Scope scope = buffer_get_managed_scope(app, buffer);
Line_Ending_Kind *eol_setting = scope_attachment(app, scope, buffer_eol_setting,
Line_Ending_Kind);
if (eol_setting != 0){
*eol_setting = LineEndingKind_Binary;
}
}
CUSTOM_COMMAND_SIG(set_eol_mode_from_contents)
CUSTOM_DOC("Sets the buffer's line ending mode to match the contents of the buffer.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
Line_Ending_Kind setting = guess_line_ending_kind_from_buffer(app, buffer);
Managed_Scope scope = buffer_get_managed_scope(app, buffer);
Line_Ending_Kind *eol_setting = scope_attachment(app, scope, buffer_eol_setting,
Line_Ending_Kind);
if (eol_setting != 0){
*eol_setting = setting;
}
}
// BOTTOM