comparison src/undo.c @ 7835:4d7ce6c03fda v7.4.1214

commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jan 30 21:10:09 2016 +0100 patch 7.4.1214 Problem: Using old style function declarations. Solution: Change to new style function declarations. (script by Hirohito Higashi)
author Christian Brabandt <cb@256bit.org>
date Sat, 30 Jan 2016 21:15:04 +0100
parents 0b6c37dd858d
children 985cd5917560
comparison
equal deleted inserted replaced
7834:2d1dc9ec41ce 7835:4d7ce6c03fda
244 * Save the current line for both the "u" and "U" command. 244 * Save the current line for both the "u" and "U" command.
245 * Careful: may trigger autocommands that reload the buffer. 245 * Careful: may trigger autocommands that reload the buffer.
246 * Returns OK or FAIL. 246 * Returns OK or FAIL.
247 */ 247 */
248 int 248 int
249 u_save_cursor() 249 u_save_cursor(void)
250 { 250 {
251 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1), 251 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
252 (linenr_T)(curwin->w_cursor.lnum + 1))); 252 (linenr_T)(curwin->w_cursor.lnum + 1)));
253 } 253 }
254 254
257 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1. 257 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
258 * Careful: may trigger autocommands that reload the buffer. 258 * Careful: may trigger autocommands that reload the buffer.
259 * Returns FAIL when lines could not be saved, OK otherwise. 259 * Returns FAIL when lines could not be saved, OK otherwise.
260 */ 260 */
261 int 261 int
262 u_save(top, bot) 262 u_save(linenr_T top, linenr_T bot)
263 linenr_T top, bot;
264 { 263 {
265 if (undo_off) 264 if (undo_off)
266 return OK; 265 return OK;
267 266
268 if (top > curbuf->b_ml.ml_line_count 267 if (top > curbuf->b_ml.ml_line_count
281 * The line is replaced, so the new bottom line is lnum + 1. 280 * The line is replaced, so the new bottom line is lnum + 1.
282 * Careful: may trigger autocommands that reload the buffer. 281 * Careful: may trigger autocommands that reload the buffer.
283 * Returns FAIL when lines could not be saved, OK otherwise. 282 * Returns FAIL when lines could not be saved, OK otherwise.
284 */ 283 */
285 int 284 int
286 u_savesub(lnum) 285 u_savesub(linenr_T lnum)
287 linenr_T lnum;
288 { 286 {
289 if (undo_off) 287 if (undo_off)
290 return OK; 288 return OK;
291 289
292 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE)); 290 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
297 * The line is inserted, so the new bottom line is lnum + 1. 295 * The line is inserted, so the new bottom line is lnum + 1.
298 * Careful: may trigger autocommands that reload the buffer. 296 * Careful: may trigger autocommands that reload the buffer.
299 * Returns FAIL when lines could not be saved, OK otherwise. 297 * Returns FAIL when lines could not be saved, OK otherwise.
300 */ 298 */
301 int 299 int
302 u_inssub(lnum) 300 u_inssub(linenr_T lnum)
303 linenr_T lnum;
304 { 301 {
305 if (undo_off) 302 if (undo_off)
306 return OK; 303 return OK;
307 304
308 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE)); 305 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
314 * becomes empty. 311 * becomes empty.
315 * Careful: may trigger autocommands that reload the buffer. 312 * Careful: may trigger autocommands that reload the buffer.
316 * Returns FAIL when lines could not be saved, OK otherwise. 313 * Returns FAIL when lines could not be saved, OK otherwise.
317 */ 314 */
318 int 315 int
319 u_savedel(lnum, nlines) 316 u_savedel(linenr_T lnum, long nlines)
320 linenr_T lnum;
321 long nlines;
322 { 317 {
323 if (undo_off) 318 if (undo_off)
324 return OK; 319 return OK;
325 320
326 return (u_savecommon(lnum - 1, lnum + nlines, 321 return (u_savecommon(lnum - 1, lnum + nlines,
330 /* 325 /*
331 * Return TRUE when undo is allowed. Otherwise give an error message and 326 * Return TRUE when undo is allowed. Otherwise give an error message and
332 * return FALSE. 327 * return FALSE.
333 */ 328 */
334 int 329 int
335 undo_allowed() 330 undo_allowed(void)
336 { 331 {
337 /* Don't allow changes when 'modifiable' is off. */ 332 /* Don't allow changes when 'modifiable' is off. */
338 if (!curbuf->b_p_ma) 333 if (!curbuf->b_p_ma)
339 { 334 {
340 EMSG(_(e_modifiable)); 335 EMSG(_(e_modifiable));
363 358
364 /* 359 /*
365 * Get the undolevle value for the current buffer. 360 * Get the undolevle value for the current buffer.
366 */ 361 */
367 static long 362 static long
368 get_undolevel() 363 get_undolevel(void)
369 { 364 {
370 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL) 365 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
371 return p_ul; 366 return p_ul;
372 return curbuf->b_p_ul; 367 return curbuf->b_p_ul;
373 } 368 }
380 * "reload" is TRUE when saving for a buffer reload. 375 * "reload" is TRUE when saving for a buffer reload.
381 * Careful: may trigger autocommands that reload the buffer. 376 * Careful: may trigger autocommands that reload the buffer.
382 * Returns FAIL when lines could not be saved, OK otherwise. 377 * Returns FAIL when lines could not be saved, OK otherwise.
383 */ 378 */
384 int 379 int
385 u_savecommon(top, bot, newbot, reload) 380 u_savecommon(
386 linenr_T top, bot; 381 linenr_T top,
387 linenr_T newbot; 382 linenr_T bot,
388 int reload; 383 linenr_T newbot,
384 int reload)
389 { 385 {
390 linenr_T lnum; 386 linenr_T lnum;
391 long i; 387 long i;
392 u_header_T *uhp; 388 u_header_T *uhp;
393 u_header_T *old_curhead; 389 u_header_T *old_curhead;
747 743
748 /* 744 /*
749 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE]. 745 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
750 */ 746 */
751 void 747 void
752 u_compute_hash(hash) 748 u_compute_hash(char_u *hash)
753 char_u *hash;
754 { 749 {
755 context_sha256_T ctx; 750 context_sha256_T ctx;
756 linenr_T lnum; 751 linenr_T lnum;
757 char_u *p; 752 char_u *p;
758 753
771 * 'undodir'. 766 * 'undodir'.
772 * When "reading" is FALSE use the first name where the directory exists. 767 * When "reading" is FALSE use the first name where the directory exists.
773 * Returns NULL when there is no place to write or no file to read. 768 * Returns NULL when there is no place to write or no file to read.
774 */ 769 */
775 char_u * 770 char_u *
776 u_get_undo_file_name(buf_ffname, reading) 771 u_get_undo_file_name(char_u *buf_ffname, int reading)
777 char_u *buf_ffname;
778 int reading;
779 { 772 {
780 char_u *dirp; 773 char_u *dirp;
781 char_u dir_name[IOSIZE + 1]; 774 char_u dir_name[IOSIZE + 1];
782 char_u *munged_name = NULL; 775 char_u *munged_name = NULL;
783 char_u *undo_file_name = NULL; 776 char_u *undo_file_name = NULL;
857 vim_free(munged_name); 850 vim_free(munged_name);
858 return undo_file_name; 851 return undo_file_name;
859 } 852 }
860 853
861 static void 854 static void
862 corruption_error(mesg, file_name) 855 corruption_error(char *mesg, char_u *file_name)
863 char *mesg;
864 char_u *file_name;
865 { 856 {
866 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name); 857 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
867 } 858 }
868 859
869 static void 860 static void
870 u_free_uhp(uhp) 861 u_free_uhp(u_header_T *uhp)
871 u_header_T *uhp;
872 { 862 {
873 u_entry_T *nuep; 863 u_entry_T *nuep;
874 u_entry_T *uep; 864 u_entry_T *uep;
875 865
876 uep = uhp->uh_entry; 866 uep = uhp->uh_entry;
887 * Write a sequence of bytes to the undo file. 877 * Write a sequence of bytes to the undo file.
888 * Buffers and encrypts as needed. 878 * Buffers and encrypts as needed.
889 * Returns OK or FAIL. 879 * Returns OK or FAIL.
890 */ 880 */
891 static int 881 static int
892 undo_write(bi, ptr, len) 882 undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
893 bufinfo_T *bi;
894 char_u *ptr;
895 size_t len;
896 { 883 {
897 #ifdef FEAT_CRYPT 884 #ifdef FEAT_CRYPT
898 if (bi->bi_buffer != NULL) 885 if (bi->bi_buffer != NULL)
899 { 886 {
900 size_t len_todo = len; 887 size_t len_todo = len;
924 return OK; 911 return OK;
925 } 912 }
926 913
927 #ifdef FEAT_CRYPT 914 #ifdef FEAT_CRYPT
928 static int 915 static int
929 undo_flush(bi) 916 undo_flush(bufinfo_T *bi)
930 bufinfo_T *bi;
931 { 917 {
932 if (bi->bi_buffer != NULL && bi->bi_used > 0) 918 if (bi->bi_buffer != NULL && bi->bi_used > 0)
933 { 919 {
934 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used); 920 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
935 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1) 921 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
943 /* 929 /*
944 * Write "ptr[len]" and crypt the bytes when needed. 930 * Write "ptr[len]" and crypt the bytes when needed.
945 * Returns OK or FAIL. 931 * Returns OK or FAIL.
946 */ 932 */
947 static int 933 static int
948 fwrite_crypt(bi, ptr, len) 934 fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
949 bufinfo_T *bi;
950 char_u *ptr;
951 size_t len;
952 { 935 {
953 #ifdef FEAT_CRYPT 936 #ifdef FEAT_CRYPT
954 char_u *copy; 937 char_u *copy;
955 char_u small_buf[100]; 938 char_u small_buf[100];
956 size_t i; 939 size_t i;
980 * Write a number, MSB first, in "len" bytes. 963 * Write a number, MSB first, in "len" bytes.
981 * Must match with undo_read_?c() functions. 964 * Must match with undo_read_?c() functions.
982 * Returns OK or FAIL. 965 * Returns OK or FAIL.
983 */ 966 */
984 static int 967 static int
985 undo_write_bytes(bi, nr, len) 968 undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
986 bufinfo_T *bi;
987 long_u nr;
988 int len;
989 { 969 {
990 char_u buf[8]; 970 char_u buf[8];
991 int i; 971 int i;
992 int bufi = 0; 972 int bufi = 0;
993 973
999 /* 979 /*
1000 * Write the pointer to an undo header. Instead of writing the pointer itself 980 * Write the pointer to an undo header. Instead of writing the pointer itself
1001 * we use the sequence number of the header. This is converted back to 981 * we use the sequence number of the header. This is converted back to
1002 * pointers when reading. */ 982 * pointers when reading. */
1003 static void 983 static void
1004 put_header_ptr(bi, uhp) 984 put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
1005 bufinfo_T *bi;
1006 u_header_T *uhp;
1007 { 985 {
1008 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); 986 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1009 } 987 }
1010 988
1011 static int 989 static int
1012 undo_read_4c(bi) 990 undo_read_4c(bufinfo_T *bi)
1013 bufinfo_T *bi;
1014 { 991 {
1015 #ifdef FEAT_CRYPT 992 #ifdef FEAT_CRYPT
1016 if (bi->bi_buffer != NULL) 993 if (bi->bi_buffer != NULL)
1017 { 994 {
1018 char_u buf[4]; 995 char_u buf[4];
1025 #endif 1002 #endif
1026 return get4c(bi->bi_fp); 1003 return get4c(bi->bi_fp);
1027 } 1004 }
1028 1005
1029 static int 1006 static int
1030 undo_read_2c(bi) 1007 undo_read_2c(bufinfo_T *bi)
1031 bufinfo_T *bi;
1032 { 1008 {
1033 #ifdef FEAT_CRYPT 1009 #ifdef FEAT_CRYPT
1034 if (bi->bi_buffer != NULL) 1010 if (bi->bi_buffer != NULL)
1035 { 1011 {
1036 char_u buf[2]; 1012 char_u buf[2];
1043 #endif 1019 #endif
1044 return get2c(bi->bi_fp); 1020 return get2c(bi->bi_fp);
1045 } 1021 }
1046 1022
1047 static int 1023 static int
1048 undo_read_byte(bi) 1024 undo_read_byte(bufinfo_T *bi)
1049 bufinfo_T *bi;
1050 { 1025 {
1051 #ifdef FEAT_CRYPT 1026 #ifdef FEAT_CRYPT
1052 if (bi->bi_buffer != NULL) 1027 if (bi->bi_buffer != NULL)
1053 { 1028 {
1054 char_u buf[1]; 1029 char_u buf[1];
1059 #endif 1034 #endif
1060 return getc(bi->bi_fp); 1035 return getc(bi->bi_fp);
1061 } 1036 }
1062 1037
1063 static time_t 1038 static time_t
1064 undo_read_time(bi) 1039 undo_read_time(bufinfo_T *bi)
1065 bufinfo_T *bi;
1066 { 1040 {
1067 #ifdef FEAT_CRYPT 1041 #ifdef FEAT_CRYPT
1068 if (bi->bi_buffer != NULL) 1042 if (bi->bi_buffer != NULL)
1069 { 1043 {
1070 char_u buf[8]; 1044 char_u buf[8];
1083 /* 1057 /*
1084 * Read "buffer[size]" from the undo file. 1058 * Read "buffer[size]" from the undo file.
1085 * Return OK or FAIL. 1059 * Return OK or FAIL.
1086 */ 1060 */
1087 static int 1061 static int
1088 undo_read(bi, buffer, size) 1062 undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
1089 bufinfo_T *bi;
1090 char_u *buffer;
1091 size_t size;
1092 { 1063 {
1093 #ifdef FEAT_CRYPT 1064 #ifdef FEAT_CRYPT
1094 if (bi->bi_buffer != NULL) 1065 if (bi->bi_buffer != NULL)
1095 { 1066 {
1096 int size_todo = (int)size; 1067 int size_todo = (int)size;
1136 * Decrypt the bytes if needed. 1107 * Decrypt the bytes if needed.
1137 * Append a NUL. 1108 * Append a NUL.
1138 * Returns a pointer to allocated memory or NULL for failure. 1109 * Returns a pointer to allocated memory or NULL for failure.
1139 */ 1110 */
1140 static char_u * 1111 static char_u *
1141 read_string_decrypt(bi, len) 1112 read_string_decrypt(bufinfo_T *bi, int len)
1142 bufinfo_T *bi;
1143 int len;
1144 { 1113 {
1145 char_u *ptr = alloc((unsigned)len + 1); 1114 char_u *ptr = alloc((unsigned)len + 1);
1146 1115
1147 if (ptr != NULL) 1116 if (ptr != NULL)
1148 { 1117 {
1162 1131
1163 /* 1132 /*
1164 * Writes the (not encrypted) header and initializes encryption if needed. 1133 * Writes the (not encrypted) header and initializes encryption if needed.
1165 */ 1134 */
1166 static int 1135 static int
1167 serialize_header(bi, hash) 1136 serialize_header(bufinfo_T *bi, char_u *hash)
1168 bufinfo_T *bi;
1169 char_u *hash;
1170 { 1137 {
1171 int len; 1138 int len;
1172 buf_T *buf = bi->bi_buf; 1139 buf_T *buf = bi->bi_buf;
1173 FILE *fp = bi->bi_fp; 1140 FILE *fp = bi->bi_fp;
1174 char_u time_buf[8]; 1141 char_u time_buf[8];
1250 1217
1251 return OK; 1218 return OK;
1252 } 1219 }
1253 1220
1254 static int 1221 static int
1255 serialize_uhp(bi, uhp) 1222 serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
1256 bufinfo_T *bi;
1257 u_header_T *uhp;
1258 { 1223 {
1259 int i; 1224 int i;
1260 u_entry_T *uep; 1225 u_entry_T *uep;
1261 char_u time_buf[8]; 1226 char_u time_buf[8];
1262 1227
1299 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2); 1264 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
1300 return OK; 1265 return OK;
1301 } 1266 }
1302 1267
1303 static u_header_T * 1268 static u_header_T *
1304 unserialize_uhp(bi, file_name) 1269 unserialize_uhp(bufinfo_T *bi, char_u *file_name)
1305 bufinfo_T *bi;
1306 char_u *file_name;
1307 { 1270 {
1308 u_header_T *uhp; 1271 u_header_T *uhp;
1309 int i; 1272 int i;
1310 u_entry_T *uep, *last_uep; 1273 u_entry_T *uep, *last_uep;
1311 int c; 1274 int c;
1391 1354
1392 /* 1355 /*
1393 * Serialize "uep". 1356 * Serialize "uep".
1394 */ 1357 */
1395 static int 1358 static int
1396 serialize_uep(bi, uep) 1359 serialize_uep(
1397 bufinfo_T *bi; 1360 bufinfo_T *bi,
1398 u_entry_T *uep; 1361 u_entry_T *uep)
1399 { 1362 {
1400 int i; 1363 int i;
1401 size_t len; 1364 size_t len;
1402 1365
1403 undo_write_bytes(bi, (long_u)uep->ue_top, 4); 1366 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1414 } 1377 }
1415 return OK; 1378 return OK;
1416 } 1379 }
1417 1380
1418 static u_entry_T * 1381 static u_entry_T *
1419 unserialize_uep(bi, error, file_name) 1382 unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
1420 bufinfo_T *bi;
1421 int *error;
1422 char_u *file_name;
1423 { 1383 {
1424 int i; 1384 int i;
1425 u_entry_T *uep; 1385 u_entry_T *uep;
1426 char_u **array; 1386 char_u **array;
1427 char_u *line; 1387 char_u *line;
1474 1434
1475 /* 1435 /*
1476 * Serialize "pos". 1436 * Serialize "pos".
1477 */ 1437 */
1478 static void 1438 static void
1479 serialize_pos(bi, pos) 1439 serialize_pos(bufinfo_T *bi, pos_T pos)
1480 bufinfo_T *bi;
1481 pos_T pos;
1482 { 1440 {
1483 undo_write_bytes(bi, (long_u)pos.lnum, 4); 1441 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1484 undo_write_bytes(bi, (long_u)pos.col, 4); 1442 undo_write_bytes(bi, (long_u)pos.col, 4);
1485 #ifdef FEAT_VIRTUALEDIT 1443 #ifdef FEAT_VIRTUALEDIT
1486 undo_write_bytes(bi, (long_u)pos.coladd, 4); 1444 undo_write_bytes(bi, (long_u)pos.coladd, 4);
1491 1449
1492 /* 1450 /*
1493 * Unserialize the pos_T at the current position. 1451 * Unserialize the pos_T at the current position.
1494 */ 1452 */
1495 static void 1453 static void
1496 unserialize_pos(bi, pos) 1454 unserialize_pos(bufinfo_T *bi, pos_T *pos)
1497 bufinfo_T *bi;
1498 pos_T *pos;
1499 { 1455 {
1500 pos->lnum = undo_read_4c(bi); 1456 pos->lnum = undo_read_4c(bi);
1501 if (pos->lnum < 0) 1457 if (pos->lnum < 0)
1502 pos->lnum = 0; 1458 pos->lnum = 0;
1503 pos->col = undo_read_4c(bi); 1459 pos->col = undo_read_4c(bi);
1514 1470
1515 /* 1471 /*
1516 * Serialize "info". 1472 * Serialize "info".
1517 */ 1473 */
1518 static void 1474 static void
1519 serialize_visualinfo(bi, info) 1475 serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
1520 bufinfo_T *bi;
1521 visualinfo_T *info;
1522 { 1476 {
1523 serialize_pos(bi, info->vi_start); 1477 serialize_pos(bi, info->vi_start);
1524 serialize_pos(bi, info->vi_end); 1478 serialize_pos(bi, info->vi_end);
1525 undo_write_bytes(bi, (long_u)info->vi_mode, 4); 1479 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1526 undo_write_bytes(bi, (long_u)info->vi_curswant, 4); 1480 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
1528 1482
1529 /* 1483 /*
1530 * Unserialize the visualinfo_T at the current position. 1484 * Unserialize the visualinfo_T at the current position.
1531 */ 1485 */
1532 static void 1486 static void
1533 unserialize_visualinfo(bi, info) 1487 unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
1534 bufinfo_T *bi;
1535 visualinfo_T *info;
1536 { 1488 {
1537 unserialize_pos(bi, &info->vi_start); 1489 unserialize_pos(bi, &info->vi_start);
1538 unserialize_pos(bi, &info->vi_end); 1490 unserialize_pos(bi, &info->vi_end);
1539 info->vi_mode = undo_read_4c(bi); 1491 info->vi_mode = undo_read_4c(bi);
1540 info->vi_curswant = undo_read_4c(bi); 1492 info->vi_curswant = undo_read_4c(bi);
1548 * permissions. 1500 * permissions.
1549 * "forceit" is TRUE for ":wundo!", FALSE otherwise. 1501 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1550 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text. 1502 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1551 */ 1503 */
1552 void 1504 void
1553 u_write_undo(name, forceit, buf, hash) 1505 u_write_undo(
1554 char_u *name; 1506 char_u *name,
1555 int forceit; 1507 int forceit,
1556 buf_T *buf; 1508 buf_T *buf,
1557 char_u *hash; 1509 char_u *hash)
1558 { 1510 {
1559 u_header_T *uhp; 1511 u_header_T *uhp;
1560 char_u *file_name; 1512 char_u *file_name;
1561 int mark; 1513 int mark;
1562 #ifdef U_DEBUG 1514 #ifdef U_DEBUG
1822 * a bit more verbose. 1774 * a bit more verbose.
1823 * Otherwise use curbuf->b_ffname to generate the undo file name. 1775 * Otherwise use curbuf->b_ffname to generate the undo file name.
1824 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text. 1776 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1825 */ 1777 */
1826 void 1778 void
1827 u_read_undo(name, hash, orig_name) 1779 u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
1828 char_u *name;
1829 char_u *hash;
1830 char_u *orig_name;
1831 { 1780 {
1832 char_u *file_name; 1781 char_u *file_name;
1833 FILE *fp; 1782 FILE *fp;
1834 long version, str_len; 1783 long version, str_len;
1835 char_u *line_ptr = NULL; 1784 char_u *line_ptr = NULL;
2186 /* 2135 /*
2187 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible). 2136 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2188 * If 'cpoptions' does not contain 'u': Always undo. 2137 * If 'cpoptions' does not contain 'u': Always undo.
2189 */ 2138 */
2190 void 2139 void
2191 u_undo(count) 2140 u_undo(int count)
2192 int count;
2193 { 2141 {
2194 /* 2142 /*
2195 * If we get an undo command while executing a macro, we behave like the 2143 * If we get an undo command while executing a macro, we behave like the
2196 * original vi. If this happens twice in one macro the result will not 2144 * original vi. If this happens twice in one macro the result will not
2197 * be compatible. 2145 * be compatible.
2212 /* 2160 /*
2213 * If 'cpoptions' contains 'u': Repeat the previous undo or redo. 2161 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2214 * If 'cpoptions' does not contain 'u': Always redo. 2162 * If 'cpoptions' does not contain 'u': Always redo.
2215 */ 2163 */
2216 void 2164 void
2217 u_redo(count) 2165 u_redo(int count)
2218 int count;
2219 { 2166 {
2220 if (vim_strchr(p_cpo, CPO_UNDO) == NULL) 2167 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2221 undo_undoes = FALSE; 2168 undo_undoes = FALSE;
2222 u_doit(count); 2169 u_doit(count);
2223 } 2170 }
2224 2171
2225 /* 2172 /*
2226 * Undo or redo, depending on 'undo_undoes', 'count' times. 2173 * Undo or redo, depending on 'undo_undoes', 'count' times.
2227 */ 2174 */
2228 static void 2175 static void
2229 u_doit(startcount) 2176 u_doit(int startcount)
2230 int startcount;
2231 { 2177 {
2232 int count = startcount; 2178 int count = startcount;
2233 2179
2234 if (!undo_allowed()) 2180 if (!undo_allowed())
2235 return; 2181 return;
2302 * When "file" is TRUE use "step" as a number of file writes. 2248 * When "file" is TRUE use "step" as a number of file writes.
2303 * When "absolute" is TRUE use "step" as the sequence number to jump to. 2249 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2304 * "sec" must be FALSE then. 2250 * "sec" must be FALSE then.
2305 */ 2251 */
2306 void 2252 void
2307 undo_time(step, sec, file, absolute) 2253 undo_time(
2308 long step; 2254 long step,
2309 int sec; 2255 int sec,
2310 int file; 2256 int file,
2311 int absolute; 2257 int absolute)
2312 { 2258 {
2313 long target; 2259 long target;
2314 long closest; 2260 long closest;
2315 long closest_start; 2261 long closest_start;
2316 long closest_seq = 0; 2262 long closest_seq = 0;
2643 * list for the next undo/redo. 2589 * list for the next undo/redo.
2644 * 2590 *
2645 * When "undo" is TRUE we go up in the tree, when FALSE we go down. 2591 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
2646 */ 2592 */
2647 static void 2593 static void
2648 u_undoredo(undo) 2594 u_undoredo(int undo)
2649 int undo;
2650 { 2595 {
2651 char_u **newarray = NULL; 2596 char_u **newarray = NULL;
2652 linenr_T oldsize; 2597 linenr_T oldsize;
2653 linenr_T newsize; 2598 linenr_T newsize;
2654 linenr_T top, bot; 2599 linenr_T top, bot;
2931 * If we deleted or added lines, report the number of less/more lines. 2876 * If we deleted or added lines, report the number of less/more lines.
2932 * Otherwise, report the number of changes (this may be incorrect 2877 * Otherwise, report the number of changes (this may be incorrect
2933 * in some cases, but it's better than nothing). 2878 * in some cases, but it's better than nothing).
2934 */ 2879 */
2935 static void 2880 static void
2936 u_undo_end(did_undo, absolute) 2881 u_undo_end(
2937 int did_undo; /* just did an undo */ 2882 int did_undo, /* just did an undo */
2938 int absolute; /* used ":undo N" */ 2883 int absolute) /* used ":undo N" */
2939 { 2884 {
2940 char *msgstr; 2885 char *msgstr;
2941 u_header_T *uhp; 2886 u_header_T *uhp;
2942 char_u msgbuf[80]; 2887 char_u msgbuf[80];
2943 2888
3014 2959
3015 /* 2960 /*
3016 * u_sync: stop adding to the current entry list 2961 * u_sync: stop adding to the current entry list
3017 */ 2962 */
3018 void 2963 void
3019 u_sync(force) 2964 u_sync(
3020 int force; /* Also sync when no_u_sync is set. */ 2965 int force) /* Also sync when no_u_sync is set. */
3021 { 2966 {
3022 /* Skip it when already synced or syncing is disabled. */ 2967 /* Skip it when already synced or syncing is disabled. */
3023 if (curbuf->b_u_synced || (!force && no_u_sync > 0)) 2968 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
3024 return; 2969 return;
3025 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) 2970 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3037 2982
3038 /* 2983 /*
3039 * ":undolist": List the leafs of the undo tree 2984 * ":undolist": List the leafs of the undo tree
3040 */ 2985 */
3041 void 2986 void
3042 ex_undolist(eap) 2987 ex_undolist(exarg_T *eap UNUSED)
3043 exarg_T *eap UNUSED;
3044 { 2988 {
3045 garray_T ga; 2989 garray_T ga;
3046 u_header_T *uhp; 2990 u_header_T *uhp;
3047 int mark; 2991 int mark;
3048 int nomark; 2992 int nomark;
3144 3088
3145 /* 3089 /*
3146 * Put the timestamp of an undo header in "buf[buflen]" in a nice format. 3090 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3147 */ 3091 */
3148 static void 3092 static void
3149 u_add_time(buf, buflen, tt) 3093 u_add_time(char_u *buf, size_t buflen, time_t tt)
3150 char_u *buf;
3151 size_t buflen;
3152 time_t tt;
3153 { 3094 {
3154 #ifdef HAVE_STRFTIME 3095 #ifdef HAVE_STRFTIME
3155 struct tm *curtime; 3096 struct tm *curtime;
3156 3097
3157 if (time(NULL) - tt >= 100) 3098 if (time(NULL) - tt >= 100)
3172 3113
3173 /* 3114 /*
3174 * ":undojoin": continue adding to the last entry list 3115 * ":undojoin": continue adding to the last entry list
3175 */ 3116 */
3176 void 3117 void
3177 ex_undojoin(eap) 3118 ex_undojoin(exarg_T *eap UNUSED)
3178 exarg_T *eap UNUSED;
3179 { 3119 {
3180 if (curbuf->b_u_newhead == NULL) 3120 if (curbuf->b_u_newhead == NULL)
3181 return; /* nothing changed before */ 3121 return; /* nothing changed before */
3182 if (curbuf->b_u_curhead != NULL) 3122 if (curbuf->b_u_curhead != NULL)
3183 { 3123 {
3199 /* 3139 /*
3200 * Called after writing or reloading the file and setting b_changed to FALSE. 3140 * Called after writing or reloading the file and setting b_changed to FALSE.
3201 * Now an undo means that the buffer is modified. 3141 * Now an undo means that the buffer is modified.
3202 */ 3142 */
3203 void 3143 void
3204 u_unchanged(buf) 3144 u_unchanged(buf_T *buf)
3205 buf_T *buf;
3206 { 3145 {
3207 u_unch_branch(buf->b_u_oldhead); 3146 u_unch_branch(buf->b_u_oldhead);
3208 buf->b_did_warn = FALSE; 3147 buf->b_did_warn = FALSE;
3209 } 3148 }
3210 3149
3211 /* 3150 /*
3212 * After reloading a buffer which was saved for 'undoreload': Find the first 3151 * After reloading a buffer which was saved for 'undoreload': Find the first
3213 * line that was changed and set the cursor there. 3152 * line that was changed and set the cursor there.
3214 */ 3153 */
3215 void 3154 void
3216 u_find_first_changed() 3155 u_find_first_changed(void)
3217 { 3156 {
3218 u_header_T *uhp = curbuf->b_u_newhead; 3157 u_header_T *uhp = curbuf->b_u_newhead;
3219 u_entry_T *uep; 3158 u_entry_T *uep;
3220 linenr_T lnum; 3159 linenr_T lnum;
3221 3160
3247 /* 3186 /*
3248 * Increase the write count, store it in the last undo header, what would be 3187 * Increase the write count, store it in the last undo header, what would be
3249 * used for "u". 3188 * used for "u".
3250 */ 3189 */
3251 void 3190 void
3252 u_update_save_nr(buf) 3191 u_update_save_nr(buf_T *buf)
3253 buf_T *buf;
3254 { 3192 {
3255 u_header_T *uhp; 3193 u_header_T *uhp;
3256 3194
3257 ++buf->b_u_save_nr_last; 3195 ++buf->b_u_save_nr_last;
3258 buf->b_u_save_nr_cur = buf->b_u_save_nr_last; 3196 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3264 if (uhp != NULL) 3202 if (uhp != NULL)
3265 uhp->uh_save_nr = buf->b_u_save_nr_last; 3203 uhp->uh_save_nr = buf->b_u_save_nr_last;
3266 } 3204 }
3267 3205
3268 static void 3206 static void
3269 u_unch_branch(uhp) 3207 u_unch_branch(u_header_T *uhp)
3270 u_header_T *uhp;
3271 { 3208 {
3272 u_header_T *uh; 3209 u_header_T *uh;
3273 3210
3274 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr) 3211 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
3275 { 3212 {
3282 /* 3219 /*
3283 * Get pointer to last added entry. 3220 * Get pointer to last added entry.
3284 * If it's not valid, give an error message and return NULL. 3221 * If it's not valid, give an error message and return NULL.
3285 */ 3222 */
3286 static u_entry_T * 3223 static u_entry_T *
3287 u_get_headentry() 3224 u_get_headentry(void)
3288 { 3225 {
3289 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL) 3226 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3290 { 3227 {
3291 EMSG(_("E439: undo list corrupt")); 3228 EMSG(_("E439: undo list corrupt"));
3292 return NULL; 3229 return NULL;
3297 /* 3234 /*
3298 * u_getbot(): compute the line number of the previous u_save 3235 * u_getbot(): compute the line number of the previous u_save
3299 * It is called only when b_u_synced is FALSE. 3236 * It is called only when b_u_synced is FALSE.
3300 */ 3237 */
3301 static void 3238 static void
3302 u_getbot() 3239 u_getbot(void)
3303 { 3240 {
3304 u_entry_T *uep; 3241 u_entry_T *uep;
3305 linenr_T extra; 3242 linenr_T extra;
3306 3243
3307 uep = u_get_headentry(); /* check for corrupt undo list */ 3244 uep = u_get_headentry(); /* check for corrupt undo list */
3335 3272
3336 /* 3273 /*
3337 * Free one header "uhp" and its entry list and adjust the pointers. 3274 * Free one header "uhp" and its entry list and adjust the pointers.
3338 */ 3275 */
3339 static void 3276 static void
3340 u_freeheader(buf, uhp, uhpp) 3277 u_freeheader(
3341 buf_T *buf; 3278 buf_T *buf,
3342 u_header_T *uhp; 3279 u_header_T *uhp,
3343 u_header_T **uhpp; /* if not NULL reset when freeing this header */ 3280 u_header_T **uhpp) /* if not NULL reset when freeing this header */
3344 { 3281 {
3345 u_header_T *uhap; 3282 u_header_T *uhap;
3346 3283
3347 /* When there is an alternate redo list free that branch completely, 3284 /* When there is an alternate redo list free that branch completely,
3348 * because we can never go there. */ 3285 * because we can never go there. */
3370 3307
3371 /* 3308 /*
3372 * Free an alternate branch and any following alternate branches. 3309 * Free an alternate branch and any following alternate branches.
3373 */ 3310 */
3374 static void 3311 static void
3375 u_freebranch(buf, uhp, uhpp) 3312 u_freebranch(
3376 buf_T *buf; 3313 buf_T *buf,
3377 u_header_T *uhp; 3314 u_header_T *uhp,
3378 u_header_T **uhpp; /* if not NULL reset when freeing this header */ 3315 u_header_T **uhpp) /* if not NULL reset when freeing this header */
3379 { 3316 {
3380 u_header_T *tofree, *next; 3317 u_header_T *tofree, *next;
3381 3318
3382 /* If this is the top branch we may need to use u_freeheader() to update 3319 /* If this is the top branch we may need to use u_freeheader() to update
3383 * all the pointers. */ 3320 * all the pointers. */
3405 /* 3342 /*
3406 * Free all the undo entries for one header and the header itself. 3343 * Free all the undo entries for one header and the header itself.
3407 * This means that "uhp" is invalid when returning. 3344 * This means that "uhp" is invalid when returning.
3408 */ 3345 */
3409 static void 3346 static void
3410 u_freeentries(buf, uhp, uhpp) 3347 u_freeentries(
3411 buf_T *buf; 3348 buf_T *buf,
3412 u_header_T *uhp; 3349 u_header_T *uhp,
3413 u_header_T **uhpp; /* if not NULL reset when freeing this header */ 3350 u_header_T **uhpp) /* if not NULL reset when freeing this header */
3414 { 3351 {
3415 u_entry_T *uep, *nuep; 3352 u_entry_T *uep, *nuep;
3416 3353
3417 /* Check for pointers to the header that become invalid now. */ 3354 /* Check for pointers to the header that become invalid now. */
3418 if (buf->b_u_curhead == uhp) 3355 if (buf->b_u_curhead == uhp)
3437 3374
3438 /* 3375 /*
3439 * free entry 'uep' and 'n' lines in uep->ue_array[] 3376 * free entry 'uep' and 'n' lines in uep->ue_array[]
3440 */ 3377 */
3441 static void 3378 static void
3442 u_freeentry(uep, n) 3379 u_freeentry(u_entry_T *uep, long n)
3443 u_entry_T *uep;
3444 long n;
3445 { 3380 {
3446 while (n > 0) 3381 while (n > 0)
3447 vim_free(uep->ue_array[--n]); 3382 vim_free(uep->ue_array[--n]);
3448 vim_free((char_u *)uep->ue_array); 3383 vim_free((char_u *)uep->ue_array);
3449 #ifdef U_DEBUG 3384 #ifdef U_DEBUG
3454 3389
3455 /* 3390 /*
3456 * invalidate the undo buffer; called when storage has already been released 3391 * invalidate the undo buffer; called when storage has already been released
3457 */ 3392 */
3458 void 3393 void
3459 u_clearall(buf) 3394 u_clearall(buf_T *buf)
3460 buf_T *buf;
3461 { 3395 {
3462 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL; 3396 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3463 buf->b_u_synced = TRUE; 3397 buf->b_u_synced = TRUE;
3464 buf->b_u_numhead = 0; 3398 buf->b_u_numhead = 0;
3465 buf->b_u_line_ptr = NULL; 3399 buf->b_u_line_ptr = NULL;
3468 3402
3469 /* 3403 /*
3470 * save the line "lnum" for the "U" command 3404 * save the line "lnum" for the "U" command
3471 */ 3405 */
3472 void 3406 void
3473 u_saveline(lnum) 3407 u_saveline(linenr_T lnum)
3474 linenr_T lnum;
3475 { 3408 {
3476 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */ 3409 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3477 return; 3410 return;
3478 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */ 3411 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
3479 return; 3412 return;
3490 /* 3423 /*
3491 * clear the line saved for the "U" command 3424 * clear the line saved for the "U" command
3492 * (this is used externally for crossing a line while in insert mode) 3425 * (this is used externally for crossing a line while in insert mode)
3493 */ 3426 */
3494 void 3427 void
3495 u_clearline() 3428 u_clearline(void)
3496 { 3429 {
3497 if (curbuf->b_u_line_ptr != NULL) 3430 if (curbuf->b_u_line_ptr != NULL)
3498 { 3431 {
3499 vim_free(curbuf->b_u_line_ptr); 3432 vim_free(curbuf->b_u_line_ptr);
3500 curbuf->b_u_line_ptr = NULL; 3433 curbuf->b_u_line_ptr = NULL;
3507 * Differentiation from vi: "U" can be undone with the next "U". 3440 * Differentiation from vi: "U" can be undone with the next "U".
3508 * We also allow the cursor to be in another line. 3441 * We also allow the cursor to be in another line.
3509 * Careful: may trigger autocommands that reload the buffer. 3442 * Careful: may trigger autocommands that reload the buffer.
3510 */ 3443 */
3511 void 3444 void
3512 u_undoline() 3445 u_undoline(void)
3513 { 3446 {
3514 colnr_T t; 3447 colnr_T t;
3515 char_u *oldp; 3448 char_u *oldp;
3516 3449
3517 if (undo_off) 3450 if (undo_off)
3549 3482
3550 /* 3483 /*
3551 * Free all allocated memory blocks for the buffer 'buf'. 3484 * Free all allocated memory blocks for the buffer 'buf'.
3552 */ 3485 */
3553 void 3486 void
3554 u_blockfree(buf) 3487 u_blockfree(buf_T *buf)
3555 buf_T *buf;
3556 { 3488 {
3557 while (buf->b_u_oldhead != NULL) 3489 while (buf->b_u_oldhead != NULL)
3558 u_freeheader(buf, buf->b_u_oldhead, NULL); 3490 u_freeheader(buf, buf->b_u_oldhead, NULL);
3559 vim_free(buf->b_u_line_ptr); 3491 vim_free(buf->b_u_line_ptr);
3560 } 3492 }
3562 /* 3494 /*
3563 * u_save_line(): allocate memory and copy line 'lnum' into it. 3495 * u_save_line(): allocate memory and copy line 'lnum' into it.
3564 * Returns NULL when out of memory. 3496 * Returns NULL when out of memory.
3565 */ 3497 */
3566 static char_u * 3498 static char_u *
3567 u_save_line(lnum) 3499 u_save_line(linenr_T lnum)
3568 linenr_T lnum;
3569 { 3500 {
3570 return vim_strsave(ml_get(lnum)); 3501 return vim_strsave(ml_get(lnum));
3571 } 3502 }
3572 3503
3573 /* 3504 /*
3574 * Check if the 'modified' flag is set, or 'ff' has changed (only need to 3505 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3575 * check the first character, because it can only be "dos", "unix" or "mac"). 3506 * check the first character, because it can only be "dos", "unix" or "mac").
3576 * "nofile" and "scratch" type buffers are considered to always be unchanged. 3507 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3577 */ 3508 */
3578 int 3509 int
3579 bufIsChanged(buf) 3510 bufIsChanged(buf_T *buf)
3580 buf_T *buf;
3581 { 3511 {
3582 return 3512 return
3583 #ifdef FEAT_QUICKFIX 3513 #ifdef FEAT_QUICKFIX
3584 !bt_dontwrite(buf) && 3514 !bt_dontwrite(buf) &&
3585 #endif 3515 #endif
3586 (buf->b_changed || file_ff_differs(buf, TRUE)); 3516 (buf->b_changed || file_ff_differs(buf, TRUE));
3587 } 3517 }
3588 3518
3589 int 3519 int
3590 curbufIsChanged() 3520 curbufIsChanged(void)
3591 { 3521 {
3592 return 3522 return
3593 #ifdef FEAT_QUICKFIX 3523 #ifdef FEAT_QUICKFIX
3594 !bt_dontwrite(curbuf) && 3524 !bt_dontwrite(curbuf) &&
3595 #endif 3525 #endif
3600 /* 3530 /*
3601 * For undotree(): Append the list of undo blocks at "first_uhp" to "list". 3531 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3602 * Recursive. 3532 * Recursive.
3603 */ 3533 */
3604 void 3534 void
3605 u_eval_tree(first_uhp, list) 3535 u_eval_tree(u_header_T *first_uhp, list_T *list)
3606 u_header_T *first_uhp;
3607 list_T *list;
3608 { 3536 {
3609 u_header_T *uhp = first_uhp; 3537 u_header_T *uhp = first_uhp;
3610 dict_T *dict; 3538 dict_T *dict;
3611 3539
3612 while (uhp != NULL) 3540 while (uhp != NULL)