-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuffer.c
More file actions
2462 lines (1930 loc) · 66.9 KB
/
buffer.c
File metadata and controls
2462 lines (1930 loc) · 66.9 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 Richard Burke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <assert.h>
#include "session.h"
#include "buffer.h"
#include "util.h"
#include "status.h"
#include "file.h"
#include "config.h"
#include "encoding.h"
#define FILE_BUF_SIZE 1024
#define DETECT_FF_LINE_NUM 5
static Status bf_add_new_line_at_buffer_end(Buffer *);
static Status bf_input_stream_read(InputStream *, char buf[], size_t buf_len,
size_t *bytes_read);
static Status bf_input_stream_close(InputStream *);
static Status bf_output_stream_write(OutputStream *, const char buf[],
size_t buf_len, size_t *bytes_written);
static Status bf_output_stream_close(OutputStream *);
static int is_selection(Direction *);
static void bf_default_movement_selection_handler(Buffer *, int is_select,
Direction *);
static Status bf_change_real_line(Buffer *, BufferPos *,
Direction, int is_cursor);
static Status bf_change_screen_line(Buffer *, BufferPos *,
Direction, int is_cursor);
static Status bf_advance_bp_to_line_offset(Buffer *, BufferPos *,
int is_select);
static void bf_update_line_col_offset(Buffer *, const BufferPos *);
static Status bf_add_mark(Buffer *, Mark *);
static Mark *bf_get_mark(const Buffer *, const BufferPos *);
static int bf_remove_pos_mark(Buffer *, const BufferPos *, int free);
static int bf_remove_mark(Buffer *, Mark *, int free);
static Status bf_update_marks(Buffer *, const BufferPos *change_pos,
TextChangeType change_type, size_t change_length,
size_t change_lines);
static void bf_update_mark(Mark *, const BufferPos *change_pos,
TextChangeType change_type, size_t change_length,
size_t change_lines);
static Status bf_insert_expanded_tab(Buffer *, int advance_cursor);
static Status bf_auto_indent(Buffer *, int advance_cursor);
static Status bf_convert_fileformat(TextSelection *in_ts, TextSelection *out_ts,
int *conversion_perfomed);
static Status bf_mask_allows_input(const Buffer *, const char *str,
size_t str_len, int *input_allowed);
Buffer *bf_new(const FileInfo *file_info, const HashMap *config)
{
assert(file_info != NULL);
Buffer *buffer = malloc(sizeof(Buffer));
RETURN_IF_NULL(buffer);
memset(buffer, 0, sizeof(Buffer));
if ((buffer->config = new_hashmap()) == NULL) {
bf_free(buffer);
return NULL;
}
if ((buffer->marks = new_hashmap()) == NULL) {
bf_free(buffer);
return NULL;
}
if ((buffer->data = gb_new(GAP_INCREMENT)) == NULL) {
bf_free(buffer);
return NULL;
}
if (!cf_populate_config(config, buffer->config, CL_BUFFER)) {
bf_free(buffer);
return NULL;
}
buffer->file_info = *file_info;
buffer->file_format = FF_UNIX;
bp_init(&buffer->pos, buffer->data, &buffer->file_format, buffer->config);
bp_init(&buffer->select_start, buffer->data,
&buffer->file_format, buffer->config);
bf_select_reset(buffer);
bs_init_default_opt(&buffer->search);
bc_init(&buffer->changes);
buffer->change_state = bc_get_current_state(&buffer->changes);
if ((buffer->bv = bv_new(24, 80, &buffer->pos)) == NULL) {
bf_free(buffer);
return NULL;
}
if (!STATUS_IS_SUCCESS(bf_add_new_mark(buffer, &buffer->bv->screen_start,
MP_NO_ADJUST_ON_BUFFER_POS))) {
bf_free(buffer);
return 0;
}
return buffer;
}
Buffer *bf_new_empty(const char *file_name, const HashMap *config)
{
FileInfo file_info;
if (!fi_init_empty(&file_info, file_name)) {
return NULL;
}
Buffer *buffer = bf_new(&file_info, config);
RETURN_IF_NULL(buffer);
return buffer;
}
void bf_free(Buffer *buffer)
{
if (buffer == NULL) {
return;
}
bs_free(&buffer->search);
fi_free(&buffer->file_info);
cf_free_config(buffer->config);
gb_free(buffer->data);
bc_free(&buffer->changes);
free_hashmap_values(buffer->marks, (void (*)(void *))bp_free_mark);
free_hashmap(buffer->marks);
bv_free(buffer->bv);
free(buffer);
}
void bf_free_syntax_match_cache(Buffer *buffer)
{
bv_free_syntax_match_cache(buffer->bv);
}
Status bf_clear(Buffer *buffer)
{
BufferPos *pos = &buffer->pos;
bf_select_reset(buffer);
bp_to_buffer_start(pos);
return bf_delete(buffer, bf_length(buffer));
}
Status bf_reset(Buffer *buffer)
{
bc_disable(&buffer->changes);
Status status = bf_clear(buffer);
bc_enable(&buffer->changes);
RETURN_IF_FAIL(status);
bc_free(&buffer->changes);
bc_init(&buffer->changes);
return status;
}
/* Look at the first couple of lines to determine
* if buffer has Unix or windows line endings */
FileFormat bf_detect_fileformat(const Buffer *buffer)
{
FileFormat file_format = FF_UNIX;
if (gb_lines(buffer->data) > 0) {
size_t lines = MIN(gb_lines(buffer->data), DETECT_FF_LINE_NUM);
size_t unix_le = 0;
size_t dos_le = 0;
size_t point = 0;
do {
if (!gb_find_next(buffer->data, point, &point, '\n')) {
break;
}
if (point > 0 && gb_get_at(buffer->data, point - 1) == '\r') {
dos_le++;
} else {
unix_le++;
}
point++;
} while (--lines != 0);
if (dos_le > unix_le) {
file_format = FF_WINDOWS;
}
}
return file_format;
}
/* Reset buffer and load configured file into buffer */
Status bf_load_file(Buffer *buffer)
{
RETURN_IF_FAIL(bf_reset(buffer));
if (!fi_file_exists(&buffer->file_info)) {
/* If the file represented by this buffer doesn't exist
* then the buffer content is empty */
return STATUS_SUCCESS;
}
/* We don't want the inital load into the buffer to be undoable */
bc_disable(&buffer->changes);
Status status = bf_read_file(buffer, &buffer->file_info);
bc_enable(&buffer->changes);
return status;
}
/* Read file content info buffer at current position */
Status bf_read_file(Buffer *buffer, const FileInfo *file_info)
{
if (!fi_file_exists(file_info)) {
return st_get_error(ERR_FILE_DOESNT_EXIST, "File doesn't exist: %s",
file_info->rel_path);
}
FILE *input_file = fopen(file_info->abs_path, "rb");
if (input_file == NULL) {
return st_get_error(ERR_UNABLE_TO_OPEN_FILE,
"Unable to open file %s for reading - %s",
file_info->file_name, strerror(errno));
}
size_t old_size = bf_length(buffer);
size_t new_size = old_size + file_info->file_stat.st_size;
/* Attempt to allocate necessary memory before loading into gap buffer */
if (!gb_preallocate(buffer->data, new_size)) {
return OUT_OF_MEMORY("File is too large to load into memory");
}
Status status = STATUS_SUCCESS;
char buf[FILE_BUF_SIZE];
size_t read;
gb_set_point(buffer->data, buffer->pos.offset);
do {
read = fread(buf, sizeof(char), FILE_BUF_SIZE, input_file);
if (ferror(input_file)) {
status = st_get_error(ERR_UNABLE_TO_READ_FILE,
"Unable to read from file %s - %s",
file_info->file_name, strerror(errno));
break;
}
/* Add text to gap buffer and advance internal point */
if (!gb_add(buffer->data, buf, read)) {
status = OUT_OF_MEMORY("Unable to populate buffer");
break;
}
} while (read == FILE_BUF_SIZE);
fclose(input_file);
size_t bytes_inserted = bf_length(buffer) - old_size;
if (bytes_inserted > 0) {
ONLY_OVERWRITE_SUCCESS(
status,
bc_add_text_insert(&buffer->changes, bytes_inserted, &buffer->pos)
);
}
bf_set_is_draw_dirty(buffer, 1);
return status;
}
/* Add new line to buffer end if one doesn't exist */
static Status bf_add_new_line_at_buffer_end(Buffer *buffer)
{
size_t buffer_len = gb_length(buffer->data);
if (buffer_len == 0 ||
gb_get_at(buffer->data, buffer_len - 1) == '\n') {
return STATUS_SUCCESS;
}
const char *new_line = bf_new_line_str(buffer->file_format);
BufferPos tmp = buffer->pos;
bp_to_buffer_end(&buffer->pos);
Status status = bf_insert_string(buffer, new_line, strlen(new_line), 0);
buffer->pos = tmp;
return status;
}
/* Write buffer to temporary file in same directory as file_path
* then rename temporary file to file_path */
Status bf_write_file(Buffer *buffer, const char *file_path)
{
assert(!is_null_or_empty(file_path));
RETURN_IF_FAIL(bf_add_new_line_at_buffer_end(buffer));
const FileInfo *file_info = &buffer->file_info;
size_t tmp_file_path_len = strlen(file_path) + 6 + 1;
char *tmp_file_path = malloc(tmp_file_path_len);
if (tmp_file_path == NULL) {
return OUT_OF_MEMORY("Unable to create temporary file path");
}
snprintf(tmp_file_path, tmp_file_path_len, "%sXXXXXX", file_path);
int output_file = mkstemp(tmp_file_path);
if (output_file == -1) {
free(tmp_file_path);
return st_get_error(ERR_UNABLE_TO_OPEN_FILE,
"Unable to open temporary file for writing - %s",
strerror(errno));
}
Status status = STATUS_SUCCESS;
size_t buffer_len = gb_length(buffer->data);
size_t bytes_remaining = buffer_len;
size_t bytes_retrieved;
char buf[FILE_BUF_SIZE];
/* Read text from gap buffer and write to temporary file */
while (bytes_remaining > 0) {
bytes_retrieved = gb_get_range(buffer->data,
buffer_len - bytes_remaining,
buf, FILE_BUF_SIZE);
if (write(output_file, buf, bytes_retrieved) <= 0) {
status = st_get_error(ERR_UNABLE_TO_WRITE_TO_FILE,
"Unable to write to temporary file - %s",
strerror(errno));
break;
}
bytes_remaining -= bytes_retrieved;
}
close(output_file);
if (!STATUS_IS_SUCCESS(status)) {
goto cleanup;
}
struct stat file_stat;
if (stat(file_path, &file_stat) == 0) {
/* Set permissions and ownership of temporary file
* to match existing file */
if (chmod(tmp_file_path, file_stat.st_mode) == -1) {
status = st_get_error(ERR_UNABLE_TO_WRITE_TO_FILE,
"Unable to set file permissions - %s",
strerror(errno));
goto cleanup;
}
if (chown(tmp_file_path, file_stat.st_uid, file_stat.st_gid) == -1) {
status = st_get_error(ERR_UNABLE_TO_WRITE_TO_FILE,
"Unable to set owner - %s",
strerror(errno));
goto cleanup;
}
}
/* Overwrite existing file atomically */
if (rename(tmp_file_path, file_path) == -1) {
status = st_get_error(ERR_UNABLE_TO_WRITE_TO_FILE,
"Unable to overwrite file %s - %s",
file_info->rel_path, strerror(errno));
}
cleanup:
if (STATUS_IS_SUCCESS(status)) {
buffer->change_state = bc_get_current_state(&buffer->changes);
} else {
remove(tmp_file_path);
}
free(tmp_file_path);
return status;
}
char *bf_to_string(const Buffer *buffer)
{
size_t buffer_len = gb_length(buffer->data);
char *str = malloc(buffer_len + 1);
if (str == NULL) {
return NULL;
}
gb_get_range(buffer->data, 0, str, buffer_len);
*(str + buffer_len) = '\0';
return str;
}
/* TODO This is a simple but somewhat inefficient implementation */
char *bf_join_lines_string(const Buffer *buffer, const char *seperator)
{
assert(seperator != NULL);
char *buffer_str = bf_to_string(buffer);
if (buffer_str == NULL) {
return NULL;
}
const char *new_line = bf_new_line_str(buffer->file_format);
char *joined = replace(buffer_str, new_line, seperator);
free(buffer_str);
return joined;
}
int bf_is_empty(const Buffer *buffer)
{
return gb_length(buffer->data) == 0;
}
size_t bf_lines(const Buffer *buffer)
{
return gb_lines(buffer->data) + 1;
}
size_t bf_length(const Buffer *buffer)
{
return gb_length(buffer->data);
}
int bf_is_view_initialised(const Buffer *buffer)
{
return buffer->bv != NULL;
}
int bf_is_dirty(const Buffer *buffer)
{
return bc_has_state_changed(&buffer->changes, buffer->change_state);
}
int bf_is_draw_dirty(const Buffer *buffer)
{
return buffer->is_draw_dirty;
}
void bf_set_is_draw_dirty(Buffer *buffer, int is_draw_dirty)
{
buffer->is_draw_dirty = is_draw_dirty;
}
int bf_get_range(Buffer *buffer, Range *range)
{
if (!bf_selection_started(buffer)) {
return 0;
} else if (bp_compare(&buffer->pos, &buffer->select_start) == 0) {
/* Cannot have empty selection */
bf_select_reset(buffer);
return 0;
}
range->start = bp_min(&buffer->pos, &buffer->select_start);
range->end = bp_max(&buffer->pos, &buffer->select_start);
return 1;
}
int bf_bp_in_range(const Range *range, const BufferPos *pos)
{
if (bp_compare(pos, &range->start) < 0 ||
bp_compare(pos, &range->end) >= 0) {
return 0;
}
return 1;
}
int bf_offset_in_range(const Range *range, size_t offset)
{
return (offset >= range->start.offset && offset < range->end.offset);
}
static Status bf_input_stream_read(InputStream *is, char buf[], size_t buf_len,
size_t *bytes_read)
{
BufferInputStream *bis = (BufferInputStream *)is;
size_t offset = bis->read_pos.offset;
size_t limit_offset = bis->end_pos.offset;
if (offset < limit_offset) {
buf_len = MIN(buf_len, limit_offset - offset);
*bytes_read = gb_get_range(bis->buffer->data, offset, buf, buf_len);
bis->read_pos.offset += *bytes_read;
} else {
*bytes_read = 0;
}
return STATUS_SUCCESS;
}
static Status bf_input_stream_close(InputStream *is)
{
BufferInputStream *bis = (BufferInputStream *)is;
bf_remove_pos_mark(bis->buffer, &bis->read_pos, 1);
bf_remove_pos_mark(bis->buffer, &bis->end_pos, 1);
return STATUS_SUCCESS;
}
Status bf_get_buffer_input_stream(BufferInputStream *bis, Buffer *buffer,
const Range *range)
{
*bis = (BufferInputStream) {
.is = {
.read = bf_input_stream_read,
.close = bf_input_stream_close
},
.buffer = buffer,
.read_pos = range->start,
.end_pos = range->end
};
RETURN_IF_FAIL(bf_add_new_mark(buffer, &bis->read_pos,
MP_ADJUST_OFFSET_ONLY));
Status status = bf_add_new_mark(buffer, &bis->end_pos,
MP_ADJUST_OFFSET_ONLY);
if (!STATUS_IS_SUCCESS(status)) {
bf_remove_pos_mark(buffer, &bis->read_pos, 1);
}
return status;
}
static Status bf_output_stream_write(OutputStream *os, const char buf[],
size_t buf_len, size_t *bytes_written)
{
BufferOutputStream *bos = (BufferOutputStream *)os;
BufferPos pos = bos->buffer->pos;
bos->buffer->pos = bos->write_pos;
size_t replace_len;
if (bos->replace_mode) {
size_t max_replace_len = bf_length(bos->buffer) - bos->write_pos.offset;
replace_len = MIN(max_replace_len, buf_len);
} else {
replace_len = 0;
}
Status status = bf_replace_string(bos->buffer, replace_len,
buf, buf_len, 1);
if (STATUS_IS_SUCCESS(status)) {
bos->write_pos = bos->buffer->pos;
*bytes_written = buf_len;
}
bos->buffer->pos = pos;
return status;
}
static Status bf_output_stream_close(OutputStream *os)
{
(void)os;
return STATUS_SUCCESS;
}
Status bf_get_buffer_output_stream(BufferOutputStream *bos, Buffer *buffer,
const BufferPos *write_pos,
int replace_mode)
{
*bos = (BufferOutputStream) {
.os = {
.write = bf_output_stream_write,
.close = bf_output_stream_close
},
.buffer = buffer,
.write_pos = *write_pos,
.replace_mode = replace_mode
};
return STATUS_SUCCESS;
}
/* TODO Consider UTF-8 punctuation and whitespace */
CharacterClass bf_character_class(const Buffer *buffer, const BufferPos *pos)
{
CharInfo char_info;
en_utf8_char_info(&char_info, CIP_DEFAULT, pos, buffer->config);
if (char_info.byte_length == 1) {
uchar character = bp_get_uchar(pos);
if (isspace(character)) {
return CCLASS_WHITESPACE;
} else if (ispunct(character)) {
return CCLASS_PUNCTUATION;
}
}
return CCLASS_WORD;
}
FileFormat bf_get_fileformat(const Buffer *buffer)
{
return buffer->file_format;
}
int bf_determine_fileformat(const char *ff_name, FileFormat *file_format)
{
assert(!is_null_or_empty(ff_name));
if (strncmp(ff_name, "unix", 5) == 0) {
*file_format = FF_UNIX;
return 1;
} else if (strncmp(ff_name, "windows", 8) == 0 ||
strncmp(ff_name, "dos", 4) == 0) {
*file_format = FF_WINDOWS;
return 1;
}
return 0;
}
const char *bf_determine_fileformat_str(FileFormat file_format)
{
if (file_format == FF_UNIX) {
return "unix";
} else if (file_format == FF_WINDOWS) {
return "windows";
}
assert(!"Invalid FileFormat");
return "unix";
}
void bf_set_fileformat(Buffer *buffer, FileFormat file_format)
{
assert(file_format == FF_UNIX || file_format == FF_WINDOWS);
buffer->file_format = file_format;
}
const char *bf_new_line_str(FileFormat file_format)
{
if (file_format == FF_UNIX) {
return "\n";
} else if (file_format == FF_WINDOWS) {
return "\r\n";
}
assert(!"Invalid FileFormat");
return "\n";
}
int bf_bp_at_screen_line_start(const Buffer *buffer, const BufferPos *pos)
{
if (cf_bool(buffer->config, CV_LINEWRAP)) {
size_t screen_col_no = (pos->col_no - 1) % buffer->bv->cols;
if (screen_col_no == 0) {
return 1;
}
BufferPos prev_char = *pos;
bp_prev_char(&prev_char);
size_t prev_screen_col_no =
(prev_char.col_no - 1) % buffer->bv->cols;
if (prev_screen_col_no == 0) {
return 0;
}
/* Handle screen lines that end with characters that
* take up multiple columns and wrap onto the next
* screen line */
return screen_col_no < prev_screen_col_no;
}
return bp_at_line_start(pos);
}
int bf_bp_at_screen_line_end(const Buffer *buffer, const BufferPos *pos)
{
if (cf_bool(buffer->config, CV_LINEWRAP)) {
size_t screen_col_no = pos->col_no % buffer->bv->cols;
if (screen_col_no == 0) {
return 1;
}
BufferPos next_char = *pos;
bp_next_char(&next_char);
size_t next_screen_col_no = next_char.col_no % buffer->bv->cols;
if (next_screen_col_no == 0) {
return 0;
}
return screen_col_no > next_screen_col_no;
}
return bp_at_line_end(pos);
}
int bf_bp_move_past_buffer_extremes(const BufferPos *pos, Direction direction)
{
return ((direction == DIRECTION_LEFT && bp_at_buffer_start(pos)) ||
(direction == DIRECTION_RIGHT && bp_at_buffer_end(pos)));
}
/* Zero out DIRECTION_WITH_SELECT bit and return true if it was set */
static int is_selection(Direction *direction)
{
if (direction == NULL) {
return 0;
}
int is_select = *direction & DIRECTION_WITH_SELECT;
*direction &= ~DIRECTION_WITH_SELECT;
return is_select;
}
int bf_selection_started(const Buffer *buffer)
{
/* select_start.line_no is set to 0 to
* indicate no selection exists */
return buffer->select_start.line_no > 0;
}
static void bf_default_movement_selection_handler(Buffer *buffer, int is_select,
Direction *direction)
{
if (is_select) {
if (direction != NULL) {
*direction |= DIRECTION_WITH_SELECT;
}
bf_select_continue(buffer);
} else if (bf_selection_started(buffer)) {
bf_select_reset(buffer);
}
}
Status bf_set_bp(Buffer *buffer, const BufferPos *pos, int is_select)
{
assert(pos->data == buffer->data);
assert(pos->offset <= gb_length(pos->data));
if (pos->data != buffer->data ||
pos->offset > gb_length(pos->data)) {
return st_get_error(ERR_INVALID_BUFFERPOS, "Invalid Buffer Position");
}
buffer->pos = *pos;
if (is_select) {
bf_select_continue(buffer);
} else if (bf_selection_started(buffer)) {
bf_select_reset(buffer);
}
bf_update_line_col_offset(buffer, &buffer->pos);
return STATUS_SUCCESS;
}
/* Move cursor up or down a line keeping the offset into the line the same
* or as close to the original if possible */
Status bf_change_line(Buffer *buffer, BufferPos *pos, Direction direction,
int is_cursor)
{
if (cf_bool(buffer->config, CV_LINEWRAP)) {
return bf_change_screen_line(buffer, pos, direction, is_cursor);
}
return bf_change_real_line(buffer, pos, direction, is_cursor);
}
static Status bf_change_real_line(Buffer *buffer, BufferPos *pos,
Direction direction, int is_cursor)
{
int is_select = is_selection(&direction);
assert(direction == DIRECTION_UP || direction == DIRECTION_DOWN);
if (!(direction == DIRECTION_UP || direction == DIRECTION_DOWN)) {
return STATUS_SUCCESS;
}
if (is_cursor) {
bf_default_movement_selection_handler(buffer, is_select, NULL);
}
if ((direction == DIRECTION_DOWN && bp_at_last_line(pos)) ||
(direction == DIRECTION_UP && bp_at_first_line(pos))) {
return STATUS_SUCCESS;
}
if (direction == DIRECTION_DOWN) {
bp_next_line(pos);
} else {
bp_prev_line(pos);
}
if (is_cursor) {
return bf_advance_bp_to_line_offset(buffer, pos, is_select);
}
return STATUS_SUCCESS;
}
/* Move cursor up or down a screen line keeping the cursor column as close to
* the starting value as possible. For lines which don't wrap this function
* behaves the same as by bf_change_real_line. For lines which wrap this
* allows a user to scroll up or down to a different part of the line displayed
* as a different line on the screen. Therefore this function is dependent
* on the width of the screen. */
static Status bf_change_screen_line(Buffer *buffer, BufferPos *pos,
Direction direction, int is_cursor)
{
int is_select = is_selection(&direction);
assert(direction == DIRECTION_UP || direction == DIRECTION_DOWN);
if (!(direction == DIRECTION_UP || direction == DIRECTION_DOWN)) {
return STATUS_SUCCESS;
}
Direction pos_direction = (direction == DIRECTION_DOWN ?
DIRECTION_RIGHT : DIRECTION_LEFT);
if (is_cursor) {
bf_default_movement_selection_handler(buffer, is_select,
&pos_direction);
}
size_t start_col = bv_screen_col_no(buffer, pos);
if (direction == DIRECTION_UP) {
if (!bf_bp_at_screen_line_start(buffer, pos)) {
RETURN_IF_FAIL(bf_bp_to_screen_line_start(buffer, pos,
is_select, 0));
}
RETURN_IF_FAIL(bf_change_char(buffer, pos, pos_direction, 0));
while (!bf_bp_at_screen_line_start(buffer, pos) &&
bv_screen_col_no(buffer, pos) > start_col) {
RETURN_IF_FAIL(bf_change_char(buffer, pos, pos_direction, 0));
}
} else {
if (!bf_bp_at_screen_line_end(buffer, pos)) {
RETURN_IF_FAIL(bf_bp_to_screen_line_end(buffer, pos, is_select, 0));
}
RETURN_IF_FAIL(bf_change_char(buffer, pos, pos_direction, 0));
while (!bp_at_line_end(pos) &&
!bf_bp_at_screen_line_end(buffer, pos) &&
bv_screen_col_no(buffer, pos) < start_col) {
RETURN_IF_FAIL(bf_change_char(buffer, pos, pos_direction, 0));
}
}
if (is_cursor) {
RETURN_IF_FAIL(bf_advance_bp_to_line_offset(buffer, pos, is_select));
}
return STATUS_SUCCESS;
}
static Status bf_advance_bp_to_line_offset(Buffer *buffer, BufferPos *pos,
int is_select)
{
size_t global_col_offset = buffer->line_col_offset;
size_t current_col_offset = bv_screen_col_no(buffer, pos) - 1;
Direction direction = DIRECTION_RIGHT;
if (is_select) {
direction |= DIRECTION_WITH_SELECT;
}
size_t last_col;
Status status = STATUS_SUCCESS;
while (STATUS_IS_SUCCESS(status) &&
!bp_at_line_end(pos) &&
current_col_offset < global_col_offset) {
last_col = pos->col_no;
status = bf_change_char(buffer, pos, direction, 1);
current_col_offset += pos->col_no - last_col;
}
buffer->line_col_offset = global_col_offset;
return status;
}
Status bf_change_multi_line(Buffer *buffer, BufferPos *pos, Direction direction,
size_t offset, int is_cursor)
{
if (offset == 0) {
return STATUS_SUCCESS;
}
Status status;
for (size_t k = 0; k < offset; k++) {
status = bf_change_line(buffer, pos, direction, is_cursor);
RETURN_IF_FAIL(status);
}
return STATUS_SUCCESS;
}
/* Move cursor a character to the left or right */
Status bf_change_char(Buffer *buffer, BufferPos *pos, Direction direction,
int is_cursor)
{
int is_select = is_selection(&direction);
assert(direction == DIRECTION_LEFT || direction == DIRECTION_RIGHT);
if (!(direction == DIRECTION_LEFT || direction == DIRECTION_RIGHT)) {
return STATUS_SUCCESS;
}
if (is_cursor) {
if (is_select) {
if (!bf_bp_move_past_buffer_extremes(pos, direction)) {
bf_select_continue(buffer);
}
} else if (bf_selection_started(buffer)) {
/* Clear selection and move to whichever selection
* end point is in the correct direction */
Range select_range;
BufferPos new_pos;
bf_get_range(buffer, &select_range);
if (direction == DIRECTION_LEFT) {
new_pos = select_range.start;
} else {
new_pos = select_range.end;
}
bf_select_reset(buffer);
buffer->pos = new_pos;
bf_update_line_col_offset(buffer, &buffer->pos);
return STATUS_SUCCESS;
}
}
if (bf_bp_move_past_buffer_extremes(pos, direction)) {
return STATUS_SUCCESS;
}