-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathundo.c
3367 lines (3068 loc) · 83 KB
/
undo.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
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* undo.c: multi level undo facility
*
* The saved lines are stored in a list of lists (one for each buffer):
*
* b_u_oldhead------------------------------------------------+
* |
* V
* +--------------+ +--------------+ +--------------+
* b_u_newhead--->| u_header | | u_header | | u_header |
* | uh_next------>| uh_next------>| uh_next---->NULL
* NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
* | uh_entry | | uh_entry | | uh_entry |
* +--------|-----+ +--------|-----+ +--------|-----+
* | | |
* V V V
* +--------------+ +--------------+ +--------------+
* | u_entry | | u_entry | | u_entry |
* | ue_next | | ue_next | | ue_next |
* +--------|-----+ +--------|-----+ +--------|-----+
* | | |
* V V V
* +--------------+ NULL NULL
* | u_entry |
* | ue_next |
* +--------|-----+
* |
* V
* etc.
*
* Each u_entry list contains the information for one undo or redo.
* curbuf->b_u_curhead points to the header of the last undo (the next redo),
* or is NULL if nothing has been undone (end of the branch).
*
* For keeping alternate undo/redo branches the uh_alt field is used. Thus at
* each point in the list a branch may appear for an alternate to redo. The
* uh_seq field is numbered sequentially to be able to find a newer or older
* branch.
*
* +---------------+ +---------------+
* b_u_oldhead --->| u_header | | u_header |
* | uh_alt_next ---->| uh_alt_next ----> NULL
* NULL <----- uh_alt_prev |<------ uh_alt_prev |
* | uh_prev | | uh_prev |
* +-----|---------+ +-----|---------+
* | |
* V V
* +---------------+ +---------------+
* | u_header | | u_header |
* | uh_alt_next | | uh_alt_next |
* b_u_newhead --->| uh_alt_prev | | uh_alt_prev |
* | uh_prev | | uh_prev |
* +-----|---------+ +-----|---------+
* | |
* V V
* NULL +---------------+ +---------------+
* | u_header | | u_header |
* | uh_alt_next ---->| uh_alt_next |
* | uh_alt_prev |<------ uh_alt_prev |
* | uh_prev | | uh_prev |
* +-----|---------+ +-----|---------+
* | |
* etc. etc.
*
*
* All data is allocated and will all be freed when the buffer is unloaded.
*/
/* Uncomment the next line for including the u_check() function. This warns
* for errors in the debug information. */
/* #define U_DEBUG 1 */
#define UH_MAGIC 0x18dade /* value for uh_magic when in use */
#define UE_MAGIC 0xabc123 /* value for ue_magic when in use */
#include "vim.h"
static long get_undolevel __ARGS((void));
static void u_unch_branch __ARGS((u_header_T *uhp));
static u_entry_T *u_get_headentry __ARGS((void));
static void u_getbot __ARGS((void));
static void u_doit __ARGS((int count));
static void u_undoredo __ARGS((int undo));
static void u_undo_end __ARGS((int did_undo, int absolute));
static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
static void u_freeentry __ARGS((u_entry_T *, long));
#ifdef FEAT_PERSISTENT_UNDO
static void corruption_error __ARGS((char *mesg, char_u *file_name));
static void u_free_uhp __ARGS((u_header_T *uhp));
static size_t fwrite_crypt __ARGS((buf_T *buf UNUSED, char_u *ptr, size_t len, FILE *fp));
static char_u *read_string_decrypt __ARGS((buf_T *buf UNUSED, FILE *fd, int len));
static int serialize_header __ARGS((FILE *fp, buf_T *buf, char_u *hash));
static int serialize_uhp __ARGS((FILE *fp, buf_T *buf, u_header_T *uhp));
static u_header_T *unserialize_uhp __ARGS((FILE *fp, char_u *file_name));
static int serialize_uep __ARGS((FILE *fp, buf_T *buf, u_entry_T *uep));
static u_entry_T *unserialize_uep __ARGS((FILE *fp, int *error, char_u *file_name));
static void serialize_pos __ARGS((pos_T pos, FILE *fp));
static void unserialize_pos __ARGS((pos_T *pos, FILE *fp));
static void serialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
static void put_header_ptr __ARGS((FILE *fp, u_header_T *uhp));
#endif
#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
static char_u *u_save_line __ARGS((linenr_T));
/* used in undo_end() to report number of added and deleted lines */
static long u_newcount, u_oldcount;
/*
* When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
* the action that "u" should do.
*/
static int undo_undoes = FALSE;
static int lastmark = 0;
#if defined(U_DEBUG) || defined(PROTO)
/*
* Check the undo structures for being valid. Print a warning when something
* looks wrong.
*/
static int seen_b_u_curhead;
static int seen_b_u_newhead;
static int header_count;
static void
u_check_tree(u_header_T *uhp,
u_header_T *exp_uh_next,
u_header_T *exp_uh_alt_prev)
{
u_entry_T *uep;
if (uhp == NULL)
return;
++header_count;
if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
{
EMSG("b_u_curhead found twice (looping?)");
return;
}
if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
{
EMSG("b_u_newhead found twice (looping?)");
return;
}
if (uhp->uh_magic != UH_MAGIC)
EMSG("uh_magic wrong (may be using freed memory)");
else
{
/* Check pointers back are correct. */
if (uhp->uh_next.ptr != exp_uh_next)
{
EMSG("uh_next wrong");
smsg((char_u *)"expected: 0x%x, actual: 0x%x",
exp_uh_next, uhp->uh_next.ptr);
}
if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
{
EMSG("uh_alt_prev wrong");
smsg((char_u *)"expected: 0x%x, actual: 0x%x",
exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
}
/* Check the undo tree at this header. */
for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
{
if (uep->ue_magic != UE_MAGIC)
{
EMSG("ue_magic wrong (may be using freed memory)");
break;
}
}
/* Check the next alt tree. */
u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
/* Check the next header in this branch. */
u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
}
}
static void
u_check(int newhead_may_be_NULL)
{
seen_b_u_newhead = 0;
seen_b_u_curhead = 0;
header_count = 0;
u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
&& !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
if (header_count != curbuf->b_u_numhead)
{
EMSG("b_u_numhead invalid");
smsg((char_u *)"expected: %ld, actual: %ld",
(long)header_count, (long)curbuf->b_u_numhead);
}
}
#endif
/*
* Save the current line for both the "u" and "U" command.
* Careful: may trigger autocommands that reload the buffer.
* Returns OK or FAIL.
*/
int
u_save_cursor()
{
return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
(linenr_T)(curwin->w_cursor.lnum + 1)));
}
/*
* Save the lines between "top" and "bot" for both the "u" and "U" command.
* "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
* Careful: may trigger autocommands that reload the buffer.
* Returns FAIL when lines could not be saved, OK otherwise.
*/
int
u_save(top, bot)
linenr_T top, bot;
{
if (undo_off)
return OK;
if (top > curbuf->b_ml.ml_line_count
|| top >= bot
|| bot > curbuf->b_ml.ml_line_count + 1)
return FALSE; /* rely on caller to do error messages */
if (top + 2 == bot)
u_saveline((linenr_T)(top + 1));
return (u_savecommon(top, bot, (linenr_T)0, FALSE));
}
/*
* Save the line "lnum" (used by ":s" and "~" command).
* The line is replaced, so the new bottom line is lnum + 1.
* Careful: may trigger autocommands that reload the buffer.
* Returns FAIL when lines could not be saved, OK otherwise.
*/
int
u_savesub(lnum)
linenr_T lnum;
{
if (undo_off)
return OK;
return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
}
/*
* A new line is inserted before line "lnum" (used by :s command).
* The line is inserted, so the new bottom line is lnum + 1.
* Careful: may trigger autocommands that reload the buffer.
* Returns FAIL when lines could not be saved, OK otherwise.
*/
int
u_inssub(lnum)
linenr_T lnum;
{
if (undo_off)
return OK;
return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
}
/*
* Save the lines "lnum" - "lnum" + nlines (used by delete command).
* The lines are deleted, so the new bottom line is lnum, unless the buffer
* becomes empty.
* Careful: may trigger autocommands that reload the buffer.
* Returns FAIL when lines could not be saved, OK otherwise.
*/
int
u_savedel(lnum, nlines)
linenr_T lnum;
long nlines;
{
if (undo_off)
return OK;
return (u_savecommon(lnum - 1, lnum + nlines,
nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
}
/*
* Return TRUE when undo is allowed. Otherwise give an error message and
* return FALSE.
*/
int
undo_allowed()
{
/* Don't allow changes when 'modifiable' is off. */
if (!curbuf->b_p_ma)
{
EMSG(_(e_modifiable));
return FALSE;
}
#ifdef HAVE_SANDBOX
/* In the sandbox it's not allowed to change the text. */
if (sandbox != 0)
{
EMSG(_(e_sandbox));
return FALSE;
}
#endif
/* Don't allow changes in the buffer while editing the cmdline. The
* caller of getcmdline() may get confused. */
if (textlock != 0)
{
EMSG(_(e_secure));
return FALSE;
}
return TRUE;
}
/*
* Get the undolevle value for the current buffer.
*/
static long
get_undolevel()
{
if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
return p_ul;
return curbuf->b_p_ul;
}
/*
* Common code for various ways to save text before a change.
* "top" is the line above the first changed line.
* "bot" is the line below the last changed line.
* "newbot" is the new bottom line. Use zero when not known.
* "reload" is TRUE when saving for a buffer reload.
* Careful: may trigger autocommands that reload the buffer.
* Returns FAIL when lines could not be saved, OK otherwise.
*/
int
u_savecommon(top, bot, newbot, reload)
linenr_T top, bot;
linenr_T newbot;
int reload;
{
linenr_T lnum;
long i;
u_header_T *uhp;
u_header_T *old_curhead;
u_entry_T *uep;
u_entry_T *prev_uep;
long size;
if (!reload)
{
/* When making changes is not allowed return FAIL. It's a crude way
* to make all change commands fail. */
if (!undo_allowed())
return FAIL;
#ifdef FEAT_NETBEANS_INTG
/*
* Netbeans defines areas that cannot be modified. Bail out here when
* trying to change text in a guarded area.
*/
if (netbeans_active())
{
if (netbeans_is_guarded(top, bot))
{
EMSG(_(e_guarded));
return FAIL;
}
if (curbuf->b_p_ro)
{
EMSG(_(e_nbreadonly));
return FAIL;
}
}
#endif
#ifdef FEAT_AUTOCMD
/*
* Saving text for undo means we are going to make a change. Give a
* warning for a read-only file before making the change, so that the
* FileChangedRO event can replace the buffer with a read-write version
* (e.g., obtained from a source control system).
*/
change_warning(0);
if (bot > curbuf->b_ml.ml_line_count + 1)
{
/* This happens when the FileChangedRO autocommand changes the
* file in a way it becomes shorter. */
EMSG(_("E881: Line count changed unexpectedly"));
return FAIL;
}
#endif
}
#ifdef U_DEBUG
u_check(FALSE);
#endif
size = bot - top - 1;
/*
* If curbuf->b_u_synced == TRUE make a new header.
*/
if (curbuf->b_u_synced)
{
#ifdef FEAT_JUMPLIST
/* Need to create new entry in b_changelist. */
curbuf->b_new_change = TRUE;
#endif
if (get_undolevel() >= 0)
{
/*
* Make a new header entry. Do this first so that we don't mess
* up the undo info when out of memory.
*/
uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
if (uhp == NULL)
goto nomem;
#ifdef U_DEBUG
uhp->uh_magic = UH_MAGIC;
#endif
}
else
uhp = NULL;
/*
* If we undid more than we redid, move the entry lists before and
* including curbuf->b_u_curhead to an alternate branch.
*/
old_curhead = curbuf->b_u_curhead;
if (old_curhead != NULL)
{
curbuf->b_u_newhead = old_curhead->uh_next.ptr;
curbuf->b_u_curhead = NULL;
}
/*
* free headers to keep the size right
*/
while (curbuf->b_u_numhead > get_undolevel()
&& curbuf->b_u_oldhead != NULL)
{
u_header_T *uhfree = curbuf->b_u_oldhead;
if (uhfree == old_curhead)
/* Can't reconnect the branch, delete all of it. */
u_freebranch(curbuf, uhfree, &old_curhead);
else if (uhfree->uh_alt_next.ptr == NULL)
/* There is no branch, only free one header. */
u_freeheader(curbuf, uhfree, &old_curhead);
else
{
/* Free the oldest alternate branch as a whole. */
while (uhfree->uh_alt_next.ptr != NULL)
uhfree = uhfree->uh_alt_next.ptr;
u_freebranch(curbuf, uhfree, &old_curhead);
}
#ifdef U_DEBUG
u_check(TRUE);
#endif
}
if (uhp == NULL) /* no undo at all */
{
if (old_curhead != NULL)
u_freebranch(curbuf, old_curhead, NULL);
curbuf->b_u_synced = FALSE;
return OK;
}
uhp->uh_prev.ptr = NULL;
uhp->uh_next.ptr = curbuf->b_u_newhead;
uhp->uh_alt_next.ptr = old_curhead;
if (old_curhead != NULL)
{
uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
if (uhp->uh_alt_prev.ptr != NULL)
uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
old_curhead->uh_alt_prev.ptr = uhp;
if (curbuf->b_u_oldhead == old_curhead)
curbuf->b_u_oldhead = uhp;
}
else
uhp->uh_alt_prev.ptr = NULL;
if (curbuf->b_u_newhead != NULL)
curbuf->b_u_newhead->uh_prev.ptr = uhp;
uhp->uh_seq = ++curbuf->b_u_seq_last;
curbuf->b_u_seq_cur = uhp->uh_seq;
uhp->uh_time = time(NULL);
uhp->uh_save_nr = 0;
curbuf->b_u_time_cur = uhp->uh_time + 1;
uhp->uh_walk = 0;
uhp->uh_entry = NULL;
uhp->uh_getbot_entry = NULL;
uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
#ifdef FEAT_VIRTUALEDIT
if (virtual_active() && curwin->w_cursor.coladd > 0)
uhp->uh_cursor_vcol = getviscol();
else
uhp->uh_cursor_vcol = -1;
#endif
/* save changed and buffer empty flag for undo */
uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
/* save named marks and Visual marks for undo */
mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
uhp->uh_visual = curbuf->b_visual;
curbuf->b_u_newhead = uhp;
if (curbuf->b_u_oldhead == NULL)
curbuf->b_u_oldhead = uhp;
++curbuf->b_u_numhead;
}
else
{
if (get_undolevel() < 0) /* no undo at all */
return OK;
/*
* When saving a single line, and it has been saved just before, it
* doesn't make sense saving it again. Saves a lot of memory when
* making lots of changes inside the same line.
* This is only possible if the previous change didn't increase or
* decrease the number of lines.
* Check the ten last changes. More doesn't make sense and takes too
* long.
*/
if (size == 1)
{
uep = u_get_headentry();
prev_uep = NULL;
for (i = 0; i < 10; ++i)
{
if (uep == NULL)
break;
/* If lines have been inserted/deleted we give up.
* Also when the line was included in a multi-line save. */
if ((curbuf->b_u_newhead->uh_getbot_entry != uep
? (uep->ue_top + uep->ue_size + 1
!= (uep->ue_bot == 0
? curbuf->b_ml.ml_line_count + 1
: uep->ue_bot))
: uep->ue_lcount != curbuf->b_ml.ml_line_count)
|| (uep->ue_size > 1
&& top >= uep->ue_top
&& top + 2 <= uep->ue_top + uep->ue_size + 1))
break;
/* If it's the same line we can skip saving it again. */
if (uep->ue_size == 1 && uep->ue_top == top)
{
if (i > 0)
{
/* It's not the last entry: get ue_bot for the last
* entry now. Following deleted/inserted lines go to
* the re-used entry. */
u_getbot();
curbuf->b_u_synced = FALSE;
/* Move the found entry to become the last entry. The
* order of undo/redo doesn't matter for the entries
* we move it over, since they don't change the line
* count and don't include this line. It does matter
* for the found entry if the line count is changed by
* the executed command. */
prev_uep->ue_next = uep->ue_next;
uep->ue_next = curbuf->b_u_newhead->uh_entry;
curbuf->b_u_newhead->uh_entry = uep;
}
/* The executed command may change the line count. */
if (newbot != 0)
uep->ue_bot = newbot;
else if (bot > curbuf->b_ml.ml_line_count)
uep->ue_bot = 0;
else
{
uep->ue_lcount = curbuf->b_ml.ml_line_count;
curbuf->b_u_newhead->uh_getbot_entry = uep;
}
return OK;
}
prev_uep = uep;
uep = uep->ue_next;
}
}
/* find line number for ue_bot for previous u_save() */
u_getbot();
}
#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
/*
* With Amiga and MSDOS 16 bit we can't handle big undo's, because
* then u_alloc_line would have to allocate a block larger than 32K
*/
if (size >= 8000)
goto nomem;
#endif
/*
* add lines in front of entry list
*/
uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
if (uep == NULL)
goto nomem;
vim_memset(uep, 0, sizeof(u_entry_T));
#ifdef U_DEBUG
uep->ue_magic = UE_MAGIC;
#endif
uep->ue_size = size;
uep->ue_top = top;
if (newbot != 0)
uep->ue_bot = newbot;
/*
* Use 0 for ue_bot if bot is below last line.
* Otherwise we have to compute ue_bot later.
*/
else if (bot > curbuf->b_ml.ml_line_count)
uep->ue_bot = 0;
else
{
uep->ue_lcount = curbuf->b_ml.ml_line_count;
curbuf->b_u_newhead->uh_getbot_entry = uep;
}
if (size > 0)
{
if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
sizeof(char_u *) * size)) == NULL)
{
u_freeentry(uep, 0L);
goto nomem;
}
for (i = 0, lnum = top + 1; i < size; ++i)
{
fast_breakcheck();
if (got_int)
{
u_freeentry(uep, i);
return FAIL;
}
if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
{
u_freeentry(uep, i);
goto nomem;
}
}
}
else
uep->ue_array = NULL;
uep->ue_next = curbuf->b_u_newhead->uh_entry;
curbuf->b_u_newhead->uh_entry = uep;
curbuf->b_u_synced = FALSE;
undo_undoes = FALSE;
#ifdef U_DEBUG
u_check(FALSE);
#endif
return OK;
nomem:
msg_silent = 0; /* must display the prompt */
if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
== 'y')
{
undo_off = TRUE; /* will be reset when character typed */
return OK;
}
do_outofmem_msg((long_u)0);
return FAIL;
}
#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
# define UF_START_MAGIC_LEN 9
# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
# define UF_VERSION 2 /* 2-byte undofile version number */
# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
/* extra fields for header */
# define UF_LAST_SAVE_NR 1
/* extra fields for uhp */
# define UHP_SAVE_NR 1
static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
/*
* Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
*/
void
u_compute_hash(hash)
char_u *hash;
{
context_sha256_T ctx;
linenr_T lnum;
char_u *p;
sha256_start(&ctx);
for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
{
p = ml_get(lnum);
sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
}
sha256_finish(&ctx, hash);
}
/*
* Return an allocated string of the full path of the target undofile.
* When "reading" is TRUE find the file to read, go over all directories in
* 'undodir'.
* When "reading" is FALSE use the first name where the directory exists.
* Returns NULL when there is no place to write or no file to read.
*/
char_u *
u_get_undo_file_name(buf_ffname, reading)
char_u *buf_ffname;
int reading;
{
char_u *dirp;
char_u dir_name[IOSIZE + 1];
char_u *munged_name = NULL;
char_u *undo_file_name = NULL;
int dir_len;
char_u *p;
struct stat st;
char_u *ffname = buf_ffname;
#ifdef HAVE_READLINK
char_u fname_buf[MAXPATHL];
#endif
if (ffname == NULL)
return NULL;
#ifdef HAVE_READLINK
/* Expand symlink in the file name, so that we put the undo file with the
* actual file instead of with the symlink. */
if (resolve_symlink(ffname, fname_buf) == OK)
ffname = fname_buf;
#endif
/* Loop over 'undodir'. When reading find the first file that exists.
* When not reading use the first directory that exists or ".". */
dirp = p_udir;
while (*dirp != NUL)
{
dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
if (dir_len == 1 && dir_name[0] == '.')
{
/* Use same directory as the ffname,
* "dir/name" -> "dir/.name.un~" */
undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
if (undo_file_name == NULL)
break;
p = gettail(undo_file_name);
#ifdef VMS
/* VMS can not handle more than one dot in the filenames
* use "dir/name" -> "dir/_un_name" - add _un_
* at the beginning to keep the extension */
mch_memmove(p + 4, p, STRLEN(p) + 1);
mch_memmove(p, "_un_", 4);
#else
/* Use same directory as the ffname,
* "dir/name" -> "dir/.name.un~" */
mch_memmove(p + 1, p, STRLEN(p) + 1);
*p = '.';
STRCAT(p, ".un~");
#endif
}
else
{
dir_name[dir_len] = NUL;
if (mch_isdir(dir_name))
{
if (munged_name == NULL)
{
munged_name = vim_strsave(ffname);
if (munged_name == NULL)
return NULL;
for (p = munged_name; *p != NUL; mb_ptr_adv(p))
if (vim_ispathsep(*p))
*p = '%';
}
undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
}
}
/* When reading check if the file exists. */
if (undo_file_name != NULL && (!reading
|| mch_stat((char *)undo_file_name, &st) >= 0))
break;
vim_free(undo_file_name);
undo_file_name = NULL;
}
vim_free(munged_name);
return undo_file_name;
}
static void
corruption_error(mesg, file_name)
char *mesg;
char_u *file_name;
{
EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
}
static void
u_free_uhp(uhp)
u_header_T *uhp;
{
u_entry_T *nuep;
u_entry_T *uep;
uep = uhp->uh_entry;
while (uep != NULL)
{
nuep = uep->ue_next;
u_freeentry(uep, uep->ue_size);
uep = nuep;
}
vim_free(uhp);
}
/*
* Like fwrite() but crypt the bytes when 'key' is set.
* Returns 1 if successful.
*/
static size_t
fwrite_crypt(buf, ptr, len, fp)
buf_T *buf UNUSED;
char_u *ptr;
size_t len;
FILE *fp;
{
#ifdef FEAT_CRYPT
char_u *copy;
char_u small_buf[100];
size_t i;
if (*buf->b_p_key == NUL)
return fwrite(ptr, len, (size_t)1, fp);
if (len < 100)
copy = small_buf; /* no malloc()/free() for short strings */
else
{
copy = lalloc(len, FALSE);
if (copy == NULL)
return 0;
}
crypt_encode(ptr, len, copy);
i = fwrite(copy, len, (size_t)1, fp);
if (copy != small_buf)
vim_free(copy);
return i;
#else
return fwrite(ptr, len, (size_t)1, fp);
#endif
}
/*
* Read a string of length "len" from "fd".
* When 'key' is set decrypt the bytes.
*/
static char_u *
read_string_decrypt(buf, fd, len)
buf_T *buf UNUSED;
FILE *fd;
int len;
{
char_u *ptr;
ptr = read_string(fd, len);
#ifdef FEAT_CRYPT
if (ptr != NULL && *buf->b_p_key != NUL)
crypt_decode(ptr, len);
#endif
return ptr;
}
static int
serialize_header(fp, buf, hash)
FILE *fp;
buf_T *buf;
char_u *hash;
{
int len;
/* Start writing, first the magic marker and undo info version. */
if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
return FAIL;
/* If the buffer is encrypted then all text bytes following will be
* encrypted. Numbers and other info is not crypted. */
#ifdef FEAT_CRYPT
if (*buf->b_p_key != NUL)
{
char_u *header;
int header_len;
put_bytes(fp, (long_u)UF_VERSION_CRYPT, 2);
header = prepare_crypt_write(buf, &header_len);
if (header == NULL)
return FAIL;
len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
vim_free(header);
if (len != 1)
{
crypt_pop_state();
return FAIL;
}
}
else
#endif
put_bytes(fp, (long_u)UF_VERSION, 2);
/* Write a hash of the buffer text, so that we can verify it is still the
* same when reading the buffer text. */
if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
return FAIL;
/* buffer-specific data */
put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
put_bytes(fp, (long_u)len, 4);
if (len > 0 && fwrite_crypt(buf, buf->b_u_line_ptr, (size_t)len, fp) != 1)
return FAIL;
put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
/* Undo structures header data */
put_header_ptr(fp, buf->b_u_oldhead);
put_header_ptr(fp, buf->b_u_newhead);
put_header_ptr(fp, buf->b_u_curhead);
put_bytes(fp, (long_u)buf->b_u_numhead, 4);
put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
put_time(fp, buf->b_u_time_cur);
/* Optional fields. */
putc(4, fp);
putc(UF_LAST_SAVE_NR, fp);
put_bytes(fp, (long_u)buf->b_u_save_nr_last, 4);
putc(0, fp); /* end marker */
return OK;
}
static int
serialize_uhp(fp, buf, uhp)
FILE *fp;
buf_T *buf;
u_header_T *uhp;
{
int i;
u_entry_T *uep;
if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
return FAIL;
put_header_ptr(fp, uhp->uh_next.ptr);