-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.c
3193 lines (2690 loc) · 90.2 KB
/
text.c
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
/**************************************************************************
* text.c -- This file is part of GNU nano. *
* *
* Copyright (C) 1999-2011, 2013-2020 Free Software Foundation, Inc. *
* Copyright (C) 2014-2015 Mark Majeres *
* Copyright (C) 2016 Mike Scalora *
* Copyright (C) 2016 Sumedh Pendurkar *
* Copyright (C) 2018 Marco Diego Aurélio Mesquita *
* Copyright (C) 2015-2020 Benno Schulenberg *
* *
* GNU nano 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 3 of the License, *
* or (at your option) any later version. *
* *
* GNU nano 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, see http://www.gnu.org/licenses/. *
* *
**************************************************************************/
#include "prototypes.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#if defined(__APPLE__) && !defined(st_mtim)
#define st_mtim st_mtimespec
#endif
#ifdef ENABLE_WORDCOMPLETION
static int pletion_x = 0;
/* The x position in pletion_line of the last found completion. */
static completion_word *list_of_completions;
/* A linked list of the completions that have been attempted. */
#endif
#ifndef NANO_TINY
/* Toggle the mark. */
void do_mark(void)
{
if (!openfile->mark) {
openfile->mark = openfile->current;
openfile->mark_x = openfile->current_x;
statusbar(_("Mark Set"));
openfile->kind_of_mark = HARDMARK;
} else {
openfile->mark = NULL;
statusbar(_("Mark Unset"));
refresh_needed = TRUE;
}
}
#endif /* !NANO_TINY */
/* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number
* of spaces that a tab would normally take up. */
void do_tab(void)
{
#ifdef ENABLE_COLOR
if (openfile->syntax && openfile->syntax->tab)
inject(openfile->syntax->tab, strlen(openfile->syntax->tab));
else
#endif
#ifndef NANO_TINY
if (ISSET(TABS_TO_SPACES)) {
char *spaces = nmalloc(tabsize + 1);
size_t length = tabsize - (xplustabs() % tabsize);
memset(spaces, ' ', length);
spaces[length] = '\0';
inject(spaces, length);
free(spaces);
} else
#endif
inject((char *)"\t", 1);
}
#ifndef NANO_TINY
/* Add an indent to the given line. */
void indent_a_line(linestruct *line, char *indentation)
{
size_t length = strlen(line->data);
size_t indent_len = strlen(indentation);
/* If the requested indentation is empty, don't change the line. */
if (indent_len == 0)
return;
/* Add the fabricated indentation to the beginning of the line. */
line->data = nrealloc(line->data, length + indent_len + 1);
memmove(line->data + indent_len, line->data, length + 1);
memcpy(line->data, indentation, indent_len);
openfile->totsize += indent_len;
if (ISSET(SOFTWRAP))
line->extrarows = extra_chunks_in(line);
/* Compensate for the change in the current line. */
if (line == openfile->mark && openfile->mark_x > 0)
openfile->mark_x += indent_len;
if (line == openfile->current && openfile->current_x > 0) {
openfile->current_x += indent_len;
openfile->placewewant = xplustabs();
}
}
/* Indent the current line (or the marked lines) by tabsize columns.
* This inserts either a tab character or a tab's worth of spaces,
* depending on whether --tabstospaces is in effect. */
void do_indent(void)
{
char *indentation;
linestruct *top, *bot, *line;
/* Use either all the marked lines or just the current line. */
get_range(&top, &bot);
/* Skip any leading empty lines. */
while (top != bot->next && top->data[0] == '\0')
top = top->next;
/* If all lines are empty, there is nothing to do. */
if (top == bot->next)
return;
indentation = nmalloc(tabsize + 1);
/* Set the indentation to either a bunch of spaces or a single tab. */
#ifdef ENABLE_COLOR
if (openfile->syntax && openfile->syntax->tab)
indentation = mallocstrcpy(indentation, openfile->syntax->tab);
else
#endif
if (ISSET(TABS_TO_SPACES)) {
memset(indentation, ' ', tabsize);
indentation[tabsize] = '\0';
} else {
indentation[0] = '\t';
indentation[1] = '\0';
}
add_undo(INDENT, NULL);
/* Go through each of the lines, adding an indent to the non-empty ones,
* and recording whatever was added in the undo item. */
for (line = top; line != bot->next; line = line->next) {
char *real_indent = (line->data[0] == '\0') ? "" : indentation;
indent_a_line(line, real_indent);
update_multiline_undo(line->lineno, real_indent);
}
free(indentation);
set_modified();
ensure_firstcolumn_is_aligned();
refresh_needed = TRUE;
shift_held = TRUE;
}
/* Return the number of bytes of whitespace at the start of the given text,
* but at most a tab's worth. */
size_t length_of_white(const char *text)
{
size_t white_count = 0;
#ifdef ENABLE_COLOR
if (openfile->syntax && openfile->syntax->tab) {
size_t thelength = strlen(openfile->syntax->tab);
while (text[white_count] == openfile->syntax->tab[white_count])
if (++white_count == thelength)
return thelength;
white_count = 0;
}
#endif
while (TRUE) {
if (*text == '\t')
return ++white_count;
if (*text != ' ')
return white_count;
if (++white_count == tabsize)
return tabsize;
text++;
}
}
/* Adjust the positions of mark and cursor when they are on the given line. */
void compensate_leftward(linestruct *line, size_t leftshift)
{
if (line == openfile->mark) {
if (openfile->mark_x < leftshift)
openfile->mark_x = 0;
else
openfile->mark_x -= leftshift;
}
if (line == openfile->current) {
if (openfile->current_x < leftshift)
openfile->current_x = 0;
else
openfile->current_x -= leftshift;
openfile->placewewant = xplustabs();
}
}
/* Remove an indent from the given line. */
void unindent_a_line(linestruct *line, size_t indent_len)
{
size_t length = strlen(line->data);
/* If the indent is empty, don't change the line. */
if (indent_len == 0)
return;
/* Remove the first tab's worth of whitespace from this line. */
memmove(line->data, line->data + indent_len, length - indent_len + 1);
openfile->totsize -= indent_len;
if (ISSET(SOFTWRAP))
line->extrarows = extra_chunks_in(line);
/* Adjust the positions of mark and cursor, when they are affected. */
compensate_leftward(line, indent_len);
}
/* Unindent the current line (or the marked lines) by tabsize columns.
* The removed indent can be a mixture of spaces plus at most one tab. */
void do_unindent(void)
{
linestruct *top, *bot, *line;
/* Use either all the marked lines or just the current line. */
get_range(&top, &bot);
/* Skip any leading lines that cannot be unindented. */
while (top != bot->next && length_of_white(top->data) == 0)
top = top->next;
/* If none of the lines can be unindented, there is nothing to do. */
if (top == bot->next)
return;
add_undo(UNINDENT, NULL);
/* Go through each of the lines, removing their leading indent where
* possible, and saving the removed whitespace in the undo item. */
for (line = top; line != bot->next; line = line->next) {
size_t indent_len = length_of_white(line->data);
char *indentation = measured_copy(line->data, indent_len);
unindent_a_line(line, indent_len);
update_multiline_undo(line->lineno, indentation);
free(indentation);
}
set_modified();
ensure_firstcolumn_is_aligned();
refresh_needed = TRUE;
shift_held = TRUE;
}
/* Perform an undo or redo for an indent or unindent action. */
void handle_indent_action(undostruct *u, bool undoing, bool add_indent)
{
groupstruct *group = u->grouping;
linestruct *line = line_from_number(group->top_line);
if (group->next != NULL)
die("Multiple groups -- please report a bug\n");
/* When redoing, reposition the cursor and let the indenter adjust it. */
if (!undoing)
goto_line_posx(u->head_lineno, u->head_x);
/* For each line in the group, add or remove the individual indent. */
while (line != NULL && line->lineno <= group->bottom_line) {
char *blanks = group->indentations[line->lineno - group->top_line];
if (undoing ^ add_indent)
indent_a_line(line, blanks);
else
unindent_a_line(line, strlen(blanks));
line = line->next;
}
/* When undoing, reposition the cursor to the recorded location. */
if (undoing)
goto_line_posx(u->head_lineno, u->head_x);
refresh_needed = TRUE;
}
#endif /* !NANO_TINY */
#ifdef ENABLE_COMMENT
/* Test whether the given line can be uncommented, or add or remove a comment,
* depending on action. Return TRUE if the line is uncommentable, or when
* anything was added or removed; FALSE otherwise. */
bool comment_line(undo_type action, linestruct *line, const char *comment_seq)
{
size_t comment_seq_len = strlen(comment_seq);
const char *post_seq = strchr(comment_seq, '|');
/* The postfix, if this is a bracketing type comment sequence. */
size_t pre_len = post_seq ? post_seq++ - comment_seq : comment_seq_len;
/* Length of prefix. */
size_t post_len = post_seq ? comment_seq_len - pre_len - 1 : 0;
/* Length of postfix. */
size_t line_len = strlen(line->data);
if (!ISSET(NO_NEWLINES) && line == openfile->filebot)
return FALSE;
if (action == COMMENT) {
/* Make room for the comment sequence(s), move the text right and
* copy them in. */
line->data = nrealloc(line->data, line_len + pre_len + post_len + 1);
memmove(line->data + pre_len, line->data, line_len + 1);
memmove(line->data, comment_seq, pre_len);
if (post_len > 0)
memmove(line->data + pre_len + line_len, post_seq, post_len + 1);
openfile->totsize += pre_len + post_len;
/* If needed, adjust the position of the mark and of the cursor. */
if (line == openfile->mark && openfile->mark_x > 0)
openfile->mark_x += pre_len;
if (line == openfile->current && openfile->current_x > 0) {
openfile->current_x += pre_len;
openfile->placewewant = xplustabs();
}
return TRUE;
}
/* If the line is commented, report it as uncommentable, or uncomment it. */
if (strncmp(line->data, comment_seq, pre_len) == 0 && (post_len == 0 ||
strcmp(line->data + line_len - post_len, post_seq) == 0)) {
if (action == PREFLIGHT)
return TRUE;
/* Erase the comment prefix by moving the non-comment part. */
memmove(line->data, line->data + pre_len, line_len - pre_len);
/* Truncate the postfix if there was one. */
line->data[line_len - pre_len - post_len] = '\0';
openfile->totsize -= pre_len + post_len;
/* Adjust the positions of mark and cursor, when needed. */
compensate_leftward(line, pre_len);
return TRUE;
}
return FALSE;
}
/* Comment or uncomment the current line or the marked lines. */
void do_comment(void)
{
const char *comment_seq = GENERAL_COMMENT_CHARACTER;
undo_type action = UNCOMMENT;
linestruct *top, *bot, *line;
bool empty, all_empty = TRUE;
#ifdef ENABLE_COLOR
if (openfile->syntax)
comment_seq = openfile->syntax->comment;
if (*comment_seq == '\0') {
statusbar(_("Commenting is not supported for this file type"));
return;
}
#endif
/* Determine which lines to work on. */
get_range(&top, &bot);
/* If only the magic line is selected, don't do anything. */
if (top == bot && bot == openfile->filebot && !ISSET(NO_NEWLINES)) {
statusbar(_("Cannot comment past end of file"));
return;
}
/* Figure out whether to comment or uncomment the selected line or lines. */
for (line = top; line != bot->next; line = line->next) {
empty = white_string(line->data);
/* If this line is not blank and not commented, we comment all. */
if (!empty && !comment_line(PREFLIGHT, line, comment_seq)) {
action = COMMENT;
break;
}
all_empty = all_empty && empty;
}
/* If all selected lines are blank, we comment them. */
action = all_empty ? COMMENT : action;
add_undo(action, NULL);
/* Store the comment sequence used for the operation, because it could
* change when the file name changes; we need to know what it was. */
openfile->current_undo->strdata = copy_of(comment_seq);
/* Comment/uncomment each of the selected lines when possible, and
* store undo data when a line changed. */
for (line = top; line != bot->next; line = line->next) {
if (comment_line(action, line, comment_seq)) {
#ifndef NANO_TINY
if (ISSET(SOFTWRAP))
line->extrarows = extra_chunks_in(line);
#endif
update_multiline_undo(line->lineno, "");
}
}
set_modified();
ensure_firstcolumn_is_aligned();
refresh_needed = TRUE;
shift_held = TRUE;
}
/* Perform an undo or redo for a comment or uncomment action. */
void handle_comment_action(undostruct *u, bool undoing, bool add_comment)
{
groupstruct *group = u->grouping;
/* When redoing, reposition the cursor and let the commenter adjust it. */
if (!undoing)
goto_line_posx(u->head_lineno, u->head_x);
while (group) {
linestruct *line = line_from_number(group->top_line);
while (line != NULL && line->lineno <= group->bottom_line) {
comment_line(undoing ^ add_comment ?
COMMENT : UNCOMMENT, line, u->strdata);
line = line->next;
}
group = group->next;
}
/* When undoing, reposition the cursor to the recorded location. */
if (undoing)
goto_line_posx(u->head_lineno, u->head_x);
refresh_needed = TRUE;
}
#endif /* ENABLE_COMMENT */
#ifndef NANO_TINY
#define redo_paste undo_cut
#define undo_paste redo_cut
/* Undo a cut, or redo a paste. */
void undo_cut(undostruct *u)
{
goto_line_posx(u->head_lineno, (u->xflags & WAS_WHOLE_LINE) ? 0 : u->head_x);
if (u->cutbuffer)
copy_from_buffer(u->cutbuffer);
/* If originally the last line was cut too, remove an extra magic line. */
if ((u->xflags & INCLUDED_LAST_LINE) && !ISSET(NO_NEWLINES) &&
openfile->filebot != openfile->current &&
openfile->filebot->prev->data[0] == '\0')
remove_magicline();
if (u->xflags & CURSOR_WAS_AT_HEAD)
goto_line_posx(u->head_lineno, u->head_x);
}
/* Redo a cut, or undo a paste. */
void redo_cut(undostruct *u)
{
linestruct *oldcutbuffer = cutbuffer;
cutbuffer = NULL;
openfile->mark = line_from_number(u->head_lineno);
openfile->mark_x = (u->xflags & WAS_WHOLE_LINE) ? 0 : u->head_x;
goto_line_posx(u->tail_lineno, u->tail_x);
do_snip(TRUE, FALSE, u->type == ZAP);
free_lines(cutbuffer);
cutbuffer = oldcutbuffer;
}
/* Undo the last thing(s) we did. */
void do_undo(void)
{
undostruct *u = openfile->current_undo;
linestruct *line = NULL, *intruder;
linestruct *oldcutbuffer;
char *data, *undidmsg = NULL;
size_t original_x, regain_from_x;
if (u == NULL) {
statusbar(_("Nothing to undo"));
return;
}
if (u->type <= REPLACE)
line = line_from_number(u->tail_lineno);
switch (u->type) {
case ADD:
/* TRANSLATORS: The next thirteen strings describe actions
* that are undone or redone. They are all nouns, not verbs. */
undidmsg = _("addition");
if ((u->xflags & INCLUDED_LAST_LINE) && !ISSET(NO_NEWLINES))
remove_magicline();
memmove(line->data + u->head_x, line->data + u->head_x + strlen(u->strdata),
strlen(line->data + u->head_x) - strlen(u->strdata) + 1);
goto_line_posx(u->head_lineno, u->head_x);
break;
case ENTER:
undidmsg = _("line break");
/* An <Enter> at the end of leading whitespace while autoindenting has
* deleted the whitespace, and stored an x position of zero. In that
* case, adjust the positions to return to and to scoop data from. */
original_x = (u->head_x == 0) ? u->tail_x : u->head_x;
regain_from_x = (u->head_x == 0) ? 0 : u->tail_x;
line->data = nrealloc(line->data, strlen(line->data) +
strlen(&u->strdata[regain_from_x]) + 1);
strcat(line->data, &u->strdata[regain_from_x]);
line->has_anchor |= line->next->has_anchor;
unlink_node(line->next);
renumber_from(line);
goto_line_posx(u->head_lineno, original_x);
break;
case BACK:
case DEL:
undidmsg = _("deletion");
data = nmalloc(strlen(line->data) + strlen(u->strdata) + 1);
strncpy(data, line->data, u->head_x);
strcpy(&data[u->head_x], u->strdata);
strcpy(&data[u->head_x + strlen(u->strdata)], &line->data[u->head_x]);
free(line->data);
line->data = data;
goto_line_posx(u->tail_lineno, u->tail_x);
break;
case JOIN:
undidmsg = _("line join");
/* When the join was done by a Backspace at the tail of the file,
* and the nonewlines flag isn't set, do not re-add a newline that
* wasn't actually deleted; just position the cursor. */
if ((u->xflags & WAS_BACKSPACE_AT_EOF) && !ISSET(NO_NEWLINES)) {
goto_line_posx(openfile->filebot->lineno, 0);
break;
}
line->data[u->tail_x] = '\0';
if (ISSET(SOFTWRAP))
line->extrarows = extra_chunks_in(line);
intruder = make_new_node(line);
intruder->data = copy_of(u->strdata);
splice_node(line, intruder);
renumber_from(intruder);
goto_line_posx(u->head_lineno, u->head_x);
break;
case REPLACE:
undidmsg = _("replacement");
data = u->strdata;
u->strdata = line->data;
line->data = data;
goto_line_posx(u->head_lineno, u->head_x);
break;
#ifdef ENABLE_WRAPPING
case SPLIT_BEGIN:
undidmsg = _("addition");
break;
case SPLIT_END:
openfile->current_undo = openfile->current_undo->next;
while (openfile->current_undo->type != SPLIT_BEGIN)
do_undo();
u = openfile->current_undo;
break;
#endif
case ZAP:
undidmsg = _("erasure");
undo_cut(u);
break;
case CUT_TO_EOF:
case CUT:
/* TRANSLATORS: Remember: these are nouns, NOT verbs. */
undidmsg = _("cut");
undo_cut(u);
break;
case PASTE:
undidmsg = _("paste");
undo_paste(u);
if ((u->xflags & INCLUDED_LAST_LINE) && !ISSET(NO_NEWLINES) &&
openfile->filebot != openfile->current)
remove_magicline();
break;
case INSERT:
undidmsg = _("insertion");
oldcutbuffer = cutbuffer;
cutbuffer = NULL;
goto_line_posx(u->head_lineno, u->head_x);
openfile->mark = line_from_number(u->tail_lineno);
openfile->mark_x = u->tail_x;
cut_marked_region();
u->cutbuffer = cutbuffer;
cutbuffer = oldcutbuffer;
if ((u->xflags & INCLUDED_LAST_LINE) && !ISSET(NO_NEWLINES) &&
openfile->filebot != openfile->current)
remove_magicline();
break;
case COUPLE_BEGIN:
undidmsg = u->strdata;
goto_line_posx(u->head_lineno, u->head_x);
openfile->current_y = u->tail_lineno;
adjust_viewport(STATIONARY);
break;
case COUPLE_END:
/* Remember the row of the cursor for a possible redo. */
openfile->current_undo->head_lineno = openfile->current_y;
openfile->current_undo = openfile->current_undo->next;
do_undo();
do_undo();
do_undo();
return;
case INDENT:
handle_indent_action(u, TRUE, TRUE);
undidmsg = _("indent");
break;
case UNINDENT:
handle_indent_action(u, TRUE, FALSE);
undidmsg = _("unindent");
break;
#ifdef ENABLE_COMMENT
case COMMENT:
handle_comment_action(u, TRUE, TRUE);
undidmsg = _("comment");
break;
case UNCOMMENT:
handle_comment_action(u, TRUE, FALSE);
undidmsg = _("uncomment");
break;
#endif
default:
break;
}
if (undidmsg && !pletion_line)
statusline(HUSH, _("Undid %s"), undidmsg);
openfile->current_undo = openfile->current_undo->next;
openfile->last_action = OTHER;
openfile->mark = NULL;
openfile->placewewant = xplustabs();
if (ISSET(SOFTWRAP))
openfile->current->extrarows = extra_chunks_in(openfile->current);
openfile->totsize = u->wassize;
/* When at the point where the file was last saved, unset "Modified". */
if (openfile->current_undo == openfile->last_saved) {
openfile->modified = FALSE;
titlebar(NULL);
} else
set_modified();
}
/* Redo the last thing(s) we undid. */
void do_redo(void)
{
linestruct *line = NULL, *intruder;
char *data, *redidmsg = NULL;
bool suppress_modification = FALSE;
undostruct *u = openfile->undotop;
if (u == NULL || u == openfile->current_undo) {
statusbar(_("Nothing to redo"));
return;
}
/* Find the item before the current one in the undo stack. */
while (u->next != openfile->current_undo)
u = u->next;
if (u->type <= REPLACE)
line = line_from_number(u->tail_lineno);
switch (u->type) {
case ADD:
redidmsg = _("addition");
if ((u->xflags & INCLUDED_LAST_LINE) && !ISSET(NO_NEWLINES))
new_magicline();
data = nmalloc(strlen(line->data) + strlen(u->strdata) + 1);
strncpy(data, line->data, u->head_x);
strcpy(&data[u->head_x], u->strdata);
strcpy(&data[u->head_x + strlen(u->strdata)], &line->data[u->head_x]);
free(line->data);
line->data = data;
goto_line_posx(u->tail_lineno, u->tail_x);
break;
case ENTER:
redidmsg = _("line break");
line->data[u->head_x] = '\0';
if (ISSET(SOFTWRAP))
line->extrarows = extra_chunks_in(line);
intruder = make_new_node(line);
intruder->data = copy_of(u->strdata);
splice_node(line, intruder);
renumber_from(intruder);
goto_line_posx(u->head_lineno + 1, u->tail_x);
break;
case BACK:
case DEL:
redidmsg = _("deletion");
memmove(line->data + u->head_x, line->data + u->head_x + strlen(u->strdata),
strlen(line->data + u->head_x) - strlen(u->strdata) + 1);
goto_line_posx(u->head_lineno, u->head_x);
break;
case JOIN:
redidmsg = _("line join");
/* When the join was done by a Backspace at the tail of the file,
* and the nonewlines flag isn't set, do not join anything, as
* nothing was actually deleted; just position the cursor. */
if ((u->xflags & WAS_BACKSPACE_AT_EOF) && !ISSET(NO_NEWLINES)) {
goto_line_posx(u->tail_lineno, u->tail_x);
break;
}
line->data = nrealloc(line->data, strlen(line->data) + strlen(u->strdata) + 1);
strcat(line->data, u->strdata);
unlink_node(line->next);
renumber_from(line);
goto_line_posx(u->tail_lineno, u->tail_x);
break;
case REPLACE:
redidmsg = _("replacement");
data = u->strdata;
u->strdata = line->data;
line->data = data;
goto_line_posx(u->head_lineno, u->head_x);
break;
#ifdef ENABLE_WRAPPING
case SPLIT_BEGIN:
openfile->current_undo = u;
while (openfile->current_undo->type != SPLIT_END)
do_redo();
u = openfile->current_undo;
goto_line_posx(u->head_lineno, u->head_x);
break;
case SPLIT_END:
redidmsg = _("addition");
break;
#endif
case ZAP:
redidmsg = _("erasure");
redo_cut(u);
break;
case CUT_TO_EOF:
case CUT:
redidmsg = _("cut");
redo_cut(u);
break;
case PASTE:
redidmsg = _("paste");
redo_paste(u);
break;
case INSERT:
redidmsg = _("insertion");
goto_line_posx(u->head_lineno, u->head_x);
if (u->cutbuffer)
copy_from_buffer(u->cutbuffer);
else
suppress_modification = TRUE;
free_lines(u->cutbuffer);
u->cutbuffer = NULL;
break;
case COUPLE_BEGIN:
openfile->current_undo = u;
do_redo();
do_redo();
do_redo();
return;
case COUPLE_END:
redidmsg = u->strdata;
goto_line_posx(u->tail_lineno, u->tail_x);
openfile->current_y = u->head_lineno;
adjust_viewport(STATIONARY);
break;
case INDENT:
handle_indent_action(u, FALSE, TRUE);
redidmsg = _("indent");
break;
case UNINDENT:
handle_indent_action(u, FALSE, FALSE);
redidmsg = _("unindent");
break;
#ifdef ENABLE_COMMENT
case COMMENT:
handle_comment_action(u, FALSE, TRUE);
redidmsg = _("comment");
break;
case UNCOMMENT:
handle_comment_action(u, FALSE, FALSE);
redidmsg = _("uncomment");
break;
#endif
default:
break;
}
if (redidmsg)
statusline(HUSH, _("Redid %s"), redidmsg);
openfile->current_undo = u;
openfile->last_action = OTHER;
openfile->mark = NULL;
openfile->placewewant = xplustabs();
if (ISSET(SOFTWRAP))
openfile->current->extrarows = extra_chunks_in(openfile->current);
openfile->totsize = u->newsize;
/* When at the point where the file was last saved, unset "Modified". */
if (openfile->current_undo == openfile->last_saved) {
openfile->modified = FALSE;
titlebar(NULL);
} else if (!suppress_modification)
set_modified();
}
#endif /* !NANO_TINY */
/* Break the current line at the cursor position. */
void do_enter(void)
{
linestruct *newnode = make_new_node(openfile->current);
size_t extra = 0;
#ifndef NANO_TINY
linestruct *sampleline = openfile->current;
bool allblanks = FALSE;
if (ISSET(AUTOINDENT)) {
#ifdef ENABLE_JUSTIFY
/* When doing automatic long-line wrapping and the next line is
* in this same paragraph, use its indentation as the model. */
if (ISSET(BREAK_LONG_LINES) && sampleline->next != NULL &&
inpar(sampleline->next) && !begpar(sampleline->next, 0))
sampleline = sampleline->next;
#endif
extra = indent_length(sampleline->data);
/* When breaking in the indentation, limit the automatic one. */
if (extra > openfile->current_x)
extra = openfile->current_x;
else if (extra == openfile->current_x)
allblanks = TRUE;
}
#endif /* NANO_TINY */
newnode->data = nmalloc(strlen(openfile->current->data +
openfile->current_x) + extra + 1);
strcpy(&newnode->data[extra], openfile->current->data +
openfile->current_x);
#ifndef NANO_TINY
if (ISSET(AUTOINDENT)) {
/* Copy the whitespace from the sample line to the new one. */
strncpy(newnode->data, sampleline->data, extra);
/* If there were only blanks before the cursor, trim them. */
if (allblanks)
openfile->current_x = 0;
}
#endif
/* Make the current line end at the cursor position. */
openfile->current->data[openfile->current_x] = '\0';
#ifndef NANO_TINY
add_undo(ENTER, NULL);
/* Adjust the mark if it was on the current line after the cursor. */
if (openfile->mark == openfile->current &&
openfile->mark_x > openfile->current_x) {
openfile->mark = newnode;
openfile->mark_x += extra - openfile->current_x;
}
if (ISSET(SOFTWRAP)) {
openfile->current->extrarows = extra_chunks_in(openfile->current);
newnode->extrarows = extra_chunks_in(newnode);
}
#endif
/* Insert the newly created line after the current one and renumber. */
splice_node(openfile->current, newnode);
renumber_from(newnode);
/* Put the cursor on the new line, after any automatic whitespace. */
openfile->current = newnode;
openfile->current_x = extra;
openfile->placewewant = xplustabs();
openfile->totsize++;
set_modified();
#ifndef NANO_TINY
if (ISSET(AUTOINDENT) && !allblanks)
openfile->totsize += extra;
update_undo(ENTER);
#endif
refresh_needed = TRUE;
focusing = FALSE;
}
#ifndef NANO_TINY
/* Discard undo items that are newer than the given one, or all if NULL. */
void discard_until(const undostruct *thisitem)
{
undostruct *dropit = openfile->undotop;
groupstruct *group;
while (dropit != NULL && dropit != thisitem) {
openfile->undotop = dropit->next;
free(dropit->strdata);
free_lines(dropit->cutbuffer);
group = dropit->grouping;
while (group != NULL) {
groupstruct *next = group->next;
free_chararray(group->indentations,
group->bottom_line - group->top_line);
free(group);
group = next;
}
free(dropit);
dropit = openfile->undotop;
}
/* Adjust the pointer to the top of the undo stack. */
openfile->current_undo = (undostruct *)thisitem;
/* Prevent a chain of editing actions from continuing. */
openfile->last_action = OTHER;
}
/* Add a new undo item of the given type to the top of the current pile. */
void add_undo(undo_type action, const char *message)
{
undostruct *u = nmalloc(sizeof(undostruct));
linestruct *thisline = openfile->current;
/* Initialize the newly allocated undo item. */
u->type = action;
u->strdata = NULL;
u->cutbuffer = NULL;
u->head_lineno = thisline->lineno;
u->head_x = openfile->current_x;
u->tail_lineno = thisline->lineno;
u->tail_x = openfile->current_x;
u->wassize = openfile->totsize;
u->newsize = openfile->totsize;
u->grouping = NULL;
u->xflags = 0;
/* Blow away any undone items. */
discard_until(openfile->current_undo);
#ifdef ENABLE_WRAPPING
/* If some action caused automatic long-line wrapping, insert the
* SPLIT_BEGIN item underneath that action's undo item. Otherwise,
* just add the new item to the top of the undo stack. */
if (u->type == SPLIT_BEGIN) {
action = openfile->undotop->type;
u->wassize = openfile->undotop->wassize;
u->next = openfile->undotop->next;
openfile->undotop->next = u;
} else
#endif
{
u->next = openfile->undotop;
openfile->undotop = u;
openfile->current_undo = u;