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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
|
/* Buffer handling functions, including allocation, deallocation, and I/O.
Copyright (C) 1993-1998 Sebastiano Vigna
Copyright (C) 1999-2013 Todd M. Lewis and Sebastiano Vigna
This file is part of ne, the nice editor.
This library 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.
This library 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 "ne.h"
/* The standard pool allocation dimension. */
#define STD_POOL_SIZE (16 * 1024)
/* The standard line descriptor pool allocation dimension (in lines). */
#define STD_LINE_DESC_POOL_SIZE (512)
/* The amount by which we increment the first pool dimension, with respect
to the size of the given file. */
#define STANDARD_INCREMENT (8 * 1024)
/* The number of lines by which we increment the first line descriptor
pool dimension, with respect to the number of lines of the given file. */
#define STANDARD_LINE_INCREMENT (256)
/* The size of the space array. Batch printing of spaces happens in blocks of
this size. */
#define MAX_STACK_SPACES (256)
/* The length of the block used in order to optimize saves. */
#define SAVE_BLOCK_LEN (64 * 1024 - 1)
/* Detects (heuristically) the encoding of a buffer. */
encoding_type detect_buffer_encoding(const buffer * const b) {
line_desc *ld = (line_desc *)b->line_desc_list.head, *next;
encoding_type encoding = ENC_ASCII, e;
while(next = (line_desc *)ld->ld_node.next) {
e = detect_encoding(ld->line, ld->line_len);
if (e != ENC_ASCII) {
if (encoding == ENC_ASCII) encoding = e;
if (e == ENC_8_BIT) encoding = ENC_8_BIT;
}
ld = next;
}
return encoding;
}
/* These functions allocate and deallocate character pools. The size of the
pool is the number of characters, and it is forced to be at least
STD_POOL_SIZE. */
char_pool *alloc_char_pool(int size) {
char_pool *cp;
if (size < STD_POOL_SIZE) size = STD_POOL_SIZE;
if (cp = calloc(1, sizeof(char_pool))) {
if (cp->pool = calloc(sizeof(char), size)) {
cp->size = size;
return cp;
}
free(cp);
}
return NULL;
}
void free_char_pool(char_pool * const cp) {
if (cp == NULL) return;
free(cp->pool);
free(cp);
}
/* Given a pointer in a character pool and a buffer, this function returns the
respective pool. It can return NULL if the pointer wasn't in any pool, but
this condition denotes a severe malfunctioning. */
char_pool *get_char_pool(buffer * const b, unsigned char * const p) {
char_pool *cp;
cp = (char_pool *)b->char_pool_list.head;
while(cp->cp_node.next) {
assert_char_pool(cp);
if (p >= cp->pool && p < cp->pool + cp->size) return cp;
cp = (char_pool *)cp->cp_node.next;
}
assert(FALSE);
return NULL;
}
/* These functions allocate and deallocate line descriptor pools. The size of
the pool is the number of lines, and is forced to be at least
STD_LINE_DESC_POOL_SIZE. */
line_desc_pool *alloc_line_desc_pool(int pool_size) {
line_desc_pool *ldp;
if (pool_size < STD_LINE_DESC_POOL_SIZE) pool_size = STD_LINE_DESC_POOL_SIZE;
if (ldp = calloc(1, sizeof(line_desc_pool))) {
if (ldp->pool = calloc(pool_size, do_syntax ? sizeof(line_desc) : sizeof(no_syntax_line_desc))) {
int i;
ldp->size = pool_size;
new_list(&ldp->free_list);
for(i = 0; i < pool_size; i++)
if (do_syntax) add_tail(&ldp->free_list, &ldp->pool[i].ld_node);
else add_tail(&ldp->free_list, &((no_syntax_line_desc *)ldp->pool)[i].ld_node);
return ldp;
}
free(ldp);
}
return NULL;
}
void free_line_desc_pool(line_desc_pool * const ldp) {
if (ldp == NULL) return;
assert_line_desc_pool(ldp);
free(ldp->pool);
free(ldp);
}
/* These functions allocate and deallocate a buffer. Note that on allocation
we have to initialize the list pointers, and on dellocation we have to free
all the lists. Moreover, on allocation a buffer pointer can be passed so
that the new buffer can inherit various user flags. */
buffer *alloc_buffer(const buffer * const cur_b) {
buffer *b;
if (b = calloc(1, sizeof(buffer))) {
new_list(&b->line_desc_pool_list);
new_list(&b->line_desc_list);
new_list(&b->char_pool_list);
b->cur_macro = alloc_char_stream(0);
b->opt.tab_size = 8;
b->opt.insert =
b->opt.tabs =
b->opt.shift_tabs =
b->opt.automatch =
b->opt.do_undo =
b->opt.auto_prefs = 1;
b->opt.utf8auto = io_utf8;
b->attr_len = -1;
if (cur_b) {
b->opt.cur_clip = cur_b->opt.cur_clip;
b->opt.tab_size = cur_b->opt.tab_size;
b->opt.tabs = cur_b->opt.tabs;
b->opt.del_tabs = cur_b->opt.del_tabs;
b->opt.shift_tabs = cur_b->opt.shift_tabs;
b->opt.automatch = cur_b->opt.automatch;
b->opt.right_margin = cur_b->opt.right_margin;
b->opt.free_form = cur_b->opt.free_form;
b->opt.hex_code = cur_b->opt.hex_code;
b->opt.word_wrap = cur_b->opt.word_wrap;
b->opt.auto_indent = cur_b->opt.auto_indent;
b->opt.preserve_cr = cur_b->opt.preserve_cr;
b->opt.do_undo = cur_b->opt.do_undo;
b->opt.auto_prefs = cur_b->opt.auto_prefs;
b->opt.no_file_req = cur_b->opt.no_file_req;
b->opt.case_search = cur_b->opt.case_search;
b->opt.binary = cur_b->opt.binary;
b->opt.utf8auto = cur_b->opt.utf8auto;
b->opt.visual_bell = cur_b->opt.visual_bell;
}
/* This leaves out onlyopt.read_only and opt.search_back, which are
implicitly set to 0 by the calloc(). */
return b;
}
return NULL;
}
/* This function is useful when resetting a buffer, but not really
destroying it. Since it modifies some lists, it cannot be interrupted
from a signal. Note that the search, replace and command_line strings are
not cleared. */
void free_buffer_contents(buffer * const b) {
if (!b) return;
block_signals();
free_list(&b->line_desc_pool_list, free_line_desc_pool);
free_list(&b->char_pool_list, free_char_pool);
new_list(&b->line_desc_list);
b->cur_line_desc = b->top_line_desc = NULL;
b->allocated_chars = b->free_chars = 0;
b->is_CRLF = FALSE;
b->encoding = ENC_ASCII;
b->bookmark_mask = 0;
free_char_stream(b->last_deleted);
b->last_deleted = NULL;
free(b->filename);
b->filename = NULL;
reset_undo_buffer(&b->undo);
b->is_modified = b->marking = b->recording = b->x_wanted = 0;
release_signals();
}
/* Removes all data in a buffer, but leaves in the current macro, the search,
replace and command_line strings, and an empty line. */
void clear_buffer(buffer * const b) {
line_desc *ld;
if (!b) return;
block_signals();
free_buffer_contents(b);
ld = alloc_line_desc(b);
add_head(&b->line_desc_list, &ld->ld_node);
if (do_syntax) {
ld->highlight_state.state = 0;
ld->highlight_state.stack = NULL;
ld->highlight_state.saved_s[0] = 0;
}
b->num_lines = 1;
reset_position_to_sof(b);
assert_buffer(b);
release_signals();
}
/* Frees all the data associated to a buffer. */
void free_buffer(buffer * const b) {
if (b == NULL) return;
assert_buffer(b);
free_buffer_contents(b);
free_char_stream(b->cur_macro);
free(b->find_string);
free(b->replace_string);
free(b->command_line);
if (b->attr_buf) free(b->attr_buf);
free(b);
}
/* Computes how many characters have been "lost" in a buffer, that is, how many
free characters lie inside the first and last used characters of the
character pools. This characters can only be allocated by
alloc_chars_around(). */
int calc_lost_chars(const buffer * const b) {
int n = 0;
char_pool *cp = (char_pool *)b->char_pool_list.head;
while(cp->cp_node.next) {
n += cp->size - (cp->last_used - cp->first_used + 1);
cp = (char_pool *)cp->cp_node.next;
}
return b->free_chars - n;
}
/* Returns the nth buffer in the global buffer list, or NULL if less than n
buffers are available. */
buffer *get_nth_buffer(int n) {
buffer *b = (buffer *)buffers.head;
while(b->b_node.next) {
if (!n--) return b;
b = (buffer *)b->b_node.next;
}
return NULL;
}
/* Returns a buffer, given its name (i.e., the name of the file it
contains). Note that file_part() is applied *both* to the string passed
*and* to the buffer names, so that the path is immaterial. */
buffer *get_buffer_named(const char *p) {
buffer *b = (buffer *)buffers.head;
if (!p) return NULL;
p = file_part((char *)p);
while(b->b_node.next) {
if (b->filename && !strcmp(file_part(b->filename), p)) return b;
b = (buffer *)b->b_node.next;
}
return NULL;
}
/* Returns TRUE if any of the buffers has been modified since the last save. */
int modified_buffers(void) {
buffer *b = (buffer *)buffers.head;
while(b->b_node.next) {
if (b->is_modified) return TRUE;
b = (buffer *)b->b_node.next;
}
return FALSE;
}
/* Saves all buffers which have been modified since the last save. Returns an
error if a save is unsuccessful, or if a buffer has no name. */
int save_all_modified_buffers(void) {
buffer *b = (buffer *)buffers.head;
while(b->b_node.next) {
if (b->is_modified)
if (save_buffer_to_file(b, NULL)) return ERROR;
b = (buffer *)b->b_node.next;
}
return 0;
}
/* Now we have the much more sophisticated allocation functions which create
small elements such as lines and line descriptors. The strategy is rather
complex. All the operations are in the context of a given buffer. Most of
these functions are protected internally against being interrupted by
signals, since auto_save could die miserably because of the inconsistent
state of a list. */
/* Allocates a line descriptor from the pools available in the given buffer. A
new pool is allocated and linked if necessary. New line descriptors are
created with an invalid syntax state, so they will always force an update. */
line_desc *alloc_line_desc(buffer * const b) {
line_desc_pool *ldp;
line_desc *ld;
ldp = (line_desc_pool *)b->line_desc_pool_list.head;
block_signals();
while(ldp->ldp_node.next) {
assert_line_desc_pool(ldp);
if (ldp->free_list.head->next) {
ld = (line_desc *)ldp->free_list.head;
rem(&ld->ld_node);
if (!ldp->free_list.head->next) {
rem(&ldp->ldp_node);
add_tail(&b->line_desc_pool_list, &ldp->ldp_node);
}
ldp->allocated_items++;
ld->line = NULL;
ld->line_len = 0;
if (do_syntax) ld->highlight_state.state = -1;
release_signals();
return ld;
}
ldp = (line_desc_pool *)ldp->ldp_node.next;
}
/* No chances, all pools are full. Let's allocate a new one,
using the standard pool size, and let's put it at the start
of the list, so that it is always scanned first. */
if (ldp = alloc_line_desc_pool(0)) {
add_head(&b->line_desc_pool_list, &ldp->ldp_node);
rem(&(ld = (line_desc *)ldp->free_list.head)->ld_node);
ldp->allocated_items = 1;
if (do_syntax) ld->highlight_state.state = -1;
release_signals();
return ld;
}
release_signals();
return NULL;
}
/* Frees a line descriptor, (and the line descriptor pool containing it, should
it become empty). */
void free_line_desc(buffer * const b, line_desc * const ld) {
line_desc_pool *ldp;
ldp = (line_desc_pool *)b->line_desc_pool_list.head;
assert(ldp != NULL);
/* We scan the pool list in order to find where the given
line descriptor lives. */
while(ldp->ldp_node.next) {
assert_line_desc_pool(ldp);
if (ld >= ldp->pool && (do_syntax && ld < ldp->pool + ldp->size || !do_syntax && ld < (line_desc*)((no_syntax_line_desc *)ldp->pool + ldp->size))) break;
ldp = (line_desc_pool *)ldp->ldp_node.next;
}
assert(ldp->ldp_node.next != NULL);
block_signals();
add_head(&ldp->free_list, &ld->ld_node);
if (--ldp->allocated_items == 0) {
rem(&ldp->ldp_node);
free_line_desc_pool(ldp);
}
release_signals();
}
/* Allocates len characters from the character pools of the
given buffer. If necessary, a new pool is allocated. */
unsigned char *alloc_chars(buffer * const b, const int len) {
char_pool *cp;
if (!len || !b) return NULL;
assert_buffer(b);
cp = (char_pool *)b->char_pool_list.head;
assert(cp != NULL);
block_signals();
while(cp->cp_node.next) {
assert_char_pool(cp);
/* We try to allocate before the first used character,
or after the last used character. If we succeed with a
pool which is not the head of the list, we move it to
the head in order to optimize the next try. */
if (cp->first_used >= len) {
cp->first_used -= len;
b->free_chars -= len;
if (cp != (char_pool *)b->char_pool_list.head) {
rem(&cp->cp_node);
add_head(&b->char_pool_list, &cp->cp_node);
}
release_signals();
return cp->pool + cp->first_used;
}
else if (cp->size - cp->last_used > len) {
cp->last_used += len;
b->free_chars -= len;
if (cp != (char_pool *)b->char_pool_list.head) {
rem(&cp->cp_node);
add_head(&b->char_pool_list, &cp->cp_node);
}
release_signals();
return cp->pool + cp->last_used - len + 1;
}
cp = (char_pool *)cp->cp_node.next;
}
/* If no free space has been found, we allocate a new pool which is guaranteed
to contain at least len characters. The pool is added to the head of the list. */
if (cp = alloc_char_pool(len)) {
add_head(&b->char_pool_list, &cp->cp_node);
cp->last_used = len - 1;
b->allocated_chars += cp->size;
b->free_chars += cp->size - len;
release_signals();
return cp->pool;
}
release_signals();
return NULL;
}
/* This function is very important, since it embeds all the philosophy behind
ne's character pool management. It performs an allocation *locally*, that
is, it tries to see if there are enough free characters around the line
pointed to by a line descriptor by looking at non-nullness of surrounding
characters (if a character is set to 0, it is free). First the characters
after the line are checked, then the characters before (this can be reversed
via the check_first_before flag). The number of characters available *after*
the line is returned, or ERROR if the allocation failed. The caller can
recover the characters available before the line since he knows the length
of the allocation. Note that it is *only* through this function that the
"lost" characters can be allocated, but being editing a local activity, this
is what happens usually. */
int alloc_chars_around(buffer * const b, line_desc * const ld, const int n, const int check_first_before) {
unsigned char *before, *after;
char_pool *cp;
assert(ld->line != NULL);
cp = get_char_pool(b, ld->line);
assert_char_pool(cp);
block_signals();
before = ld->line - 1;
after = ld->line + ld->line_len;
if (check_first_before) {
while(before >= cp->pool && !*before && (ld->line - 1) - before < n)
before--;
while(after < cp->pool + cp->size && !*after && (after - (ld->line + ld->line_len)) + ((ld->line - 1) - before)<n)
after++;
}
else {
while(after < cp->pool + cp->size && !*after && after - (ld->line + ld->line_len)<n)
after++;
while(before >= cp->pool && !*before && (after - (ld->line + ld->line_len)) + ((ld->line - 1) - before)<n)
before--;
}
assert(((ld->line - 1) - before) + (after - (ld->line + ld->line_len)) <= n);
assert(((ld->line - 1) - before) + (after - (ld->line + ld->line_len)) >= 0);
if (((ld->line - 1) - before) + (after - (ld->line + ld->line_len)) == n) {
if (cp->pool + cp->first_used == ld->line) cp->first_used = (before + 1) - cp->pool;
if (cp->pool + cp->last_used == ld->line + ld->line_len - 1) cp->last_used = (after - 1) - cp->pool;
b->free_chars -= n;
release_signals();
return after - (ld->line + ld->line_len);
}
release_signals();
return ERROR;
}
/* Frees a block of len characters pointed to by p. If the char pool containing
the block becomes completely free, it is removed from the list. */
void free_chars(buffer *const b, unsigned char *const p, const int len) {
char_pool *cp;
if (!b || !p || !len) return;
cp = get_char_pool(b, p);
assert_char_pool(cp);
assert(*p);
assert(p[len - 1]);
block_signals();
memset(p, 0, len);
b->free_chars += len;
if (p == &cp->pool[cp->first_used]) while(cp->first_used <= cp->last_used && !cp->pool[cp->first_used]) cp->first_used++;
if (p + len - 1 == &cp->pool[cp->last_used]) while(!cp->pool[cp->last_used] && cp->first_used <= cp->last_used) cp->last_used--;
if (cp->last_used < cp->first_used) {
rem(&cp->cp_node);
b->allocated_chars -= cp->size;
b->free_chars -= cp->size;
free_char_pool(cp);
release_signals();
return;
}
assert_char_pool(cp);
release_signals();
}
/* The following functions represent the only legal way of modifying a
buffer. They are all based on insert_stream and delete_stream (except for
the I/O functions). A stream is a sequence of NULL-terminated strings. The
semantics associated is that each string is a separate line terminated by a
line feed, *except for the last one*. Thus, a NULL-terminated string is a
line with no linefeed. All the functions accept a position specified via a
line descriptor and a position (which is the offset to be applied to the
line pointer of the line descriptor). Also the line number is usually
supplied, since it is necessary for recording the operation in the undo
buffer. */
/* Inserts a line at the current position. The effect is obtained by inserting
a stream containing one NULL. */
int insert_one_line(buffer * const b, line_desc * const ld, const int line, const int pos) {
return insert_stream(b, ld, line, pos, "", 1);
}
/* Deletes a whole line, putting it in the temporary line buffer used by the
UndelLine command. */
int delete_one_line(buffer * const b, line_desc * const ld, const int line) {
int error;
assert_line_desc(ld, b->encoding);
assert_buffer(b);
block_signals();
if (ld->line_len && (b->last_deleted = reset_stream(b->last_deleted))) add_to_stream(b->last_deleted, ld->line, ld->line_len);
/* We delete a line by delete_stream()ing its length plus one. However, if
we are on the last line of text, there is no terminating line feed. */
error = delete_stream(b, ld, line, 0, ld->line_len + (ld->ld_node.next->next ? 1 : 0));
release_signals();
return error;
}
/* Undeletes the last deleted line, using the last_deleted stream. */
int undelete_line(buffer * const b) {
line_desc *ld = b->cur_line_desc;
if (!b->last_deleted) return ERROR;
start_undo_chain(b);
if (b->cur_pos > ld->line_len)
insert_spaces(b, ld, b->cur_line, ld->line_len, b->win_x + b->cur_x - calc_width(ld, ld->line_len, b->opt.tab_size, b->encoding));
insert_one_line(b, ld, b->cur_line, b->cur_pos);
insert_stream(b, ld, b->cur_line, b->cur_pos, b->last_deleted->stream, b->last_deleted->len);
end_undo_chain(b);
return OK;
}
/* Deletes a line up to its end. */
void delete_to_eol(buffer * const b, line_desc * const ld, const int line, const int pos) {
if (!ld || pos >= ld->line_len) return;
delete_stream(b, ld, line, pos, ld->line_len - pos);
}
/* Inserts a stream in a line at a given position. The position has to be
smaller or equal to the line length. Since the stream can contain many
lines, this function can be used for manipulating all insertions. It also
record the inverse operation in the undo buffer if b->opt.do_undo is
TRUE. */
int insert_stream(buffer * const b, line_desc * ld, int line, int pos, const unsigned char * const stream, const int stream_len) {
int i, len, mask;
unsigned char *p;
const unsigned char *s = stream;
if (!b || !ld || !stream || stream_len < 1 || pos > ld->line_len) return ERROR;
assert_line_desc(ld, b->encoding);
assert_buffer(b);
block_signals();
if (b->opt.do_undo && !(b->undoing || b->redoing)) {
int error = add_undo_step(b, line, pos, -stream_len);
if (error) {
release_signals();
return error;
}
}
while(s - stream < stream_len) {
if (len = strnlen_ne(s, stream_len - (s - stream))) {
/* First case; there is no character allocated on this line. We
have to freshly allocate the line. */
if (!ld->line) {
if (ld->line = alloc_chars(b, len)) {
memcpy(ld->line, s, len);
ld->line_len = len;
}
else {
release_signals();
return OUT_OF_MEMORY;
}
}
/* Second case. There are not enough characters around ld->line. Note
that the value of the check_first_before parameter depends on
the position at which the insertion will be done, and it is chosen
in such a way to minimize the number of characters to move. */
else if ((i = alloc_chars_around(b, ld, len, pos < ld->line_len / 2))<0) {
if (p = alloc_chars(b, ld->line_len + len)) {
memcpy(p, ld->line, pos);
memcpy(&p[pos], s, len);
memcpy(&p[pos + len], ld->line + pos, ld->line_len - pos);
free_chars(b, ld->line, ld->line_len);
ld->line = p;
ld->line_len += len;
}
else {
release_signals();
return OUT_OF_MEMORY;
}
}
/* Third case. There are enough free characters around ld->line. */
else {
if (len - i) memmove(ld->line - (len - i), ld->line, pos);
if (i) memmove(ld->line + pos + i, ld->line + pos, ld->line_len - pos);
memcpy(ld->line - (len - i) + pos, s, len);
ld->line -= (len - i);
ld->line_len += len;
}
b->is_modified = 1;
/* We just inserted len chars at (line,pos); adjust bookmarks and mark accordingly. */
if (b->marking && b->block_start_line == line && b->block_start_pos > pos) b->block_start_pos += len;
for (i=0, mask=b->bookmark_mask; mask; i++, mask >>= 1)
if ((mask & 1) && b->bookmark[i].line == line && b->bookmark[i].pos > pos) b->bookmark[i].pos += len;
}
/* If the string we have inserted has a NULL at the end, we create a new
line under the current one and set ld to point to it. */
if (len + (s - stream) < stream_len) {
line_desc *new_ld;
if (new_ld = alloc_line_desc(b)) {
add(&new_ld->ld_node, &ld->ld_node);
b->num_lines++;
if (pos + len < ld->line_len) {
new_ld->line_len = ld->line_len - pos - len;
new_ld->line = &ld->line[pos + len];
ld->line_len = pos + len;
if (pos + len == 0) ld->line = NULL;
}
b->is_modified = 1;
ld = new_ld;
/* We just inserted a line break at (line,pos);
adjust the buffer bookmarks and mark accordingly. */
if (b->marking) {
if (b->block_start_line == line && b->block_start_pos > pos) {
b->block_start_pos -= pos;
b->block_start_line++;
}
else if (b->block_start_line > line) b->block_start_line++;
}
for (i=0, mask=b->bookmark_mask; mask; i++, mask >>= 1) {
if (mask & 1) {
if (b->bookmark[i].line == line && b->bookmark[i].pos > pos) {
b->bookmark[i].pos -= pos;
b->bookmark[i].line++;
}
else if (b->bookmark[i].line > line) b->bookmark[i].line++;
}
}
pos = 0;
line++;
}
else {
release_signals();
return OUT_OF_MEMORY;
}
}
s += len + 1;
}
release_signals();
return OK;
}
/* Inserts a single ISO 10646 character (it creates, if necessary, a suitable
temporary stream). The character must be compatible with the current buffer
encoding. */
int insert_one_char(buffer * const b, line_desc * const ld, const int line, const int pos, const int c) {
static unsigned char t[8];
assert(b->encoding == ENC_8_BIT || b->encoding == ENC_UTF8 || c <= 127);
assert(b->encoding == ENC_UTF8 || c <= 255);
assert(c != 0);
if (b->encoding == ENC_UTF8) t[utf8str(c, t)] = 0;
else t[0] = c, t[1] = 0;
return insert_stream(b, ld, line, pos, t, strlen(t));
}
/* Inserts a number of spaces. */
int insert_spaces(buffer * const b, line_desc * const ld, const int line, const int pos, int n) {
static unsigned char spaces[MAX_STACK_SPACES];
int result = OK, i;
if (!spaces[0]) memset(spaces, ' ', sizeof spaces);
while(result == OK && n > 0) {
i = min(n, MAX_STACK_SPACES);
result = insert_stream(b, ld, line, pos, spaces, i);
n -= i;
}
assert(result != OK || n == 0);
return result;
}
/* Deletes a stream of len bytes, that is, deletes len bytes from the given
position, counting line feeds as a byte. The operation is recorded in the
undo buffer. */
int delete_stream(buffer * const b, line_desc * const ld, const int line, const int pos, int len) {
int n, m, mask;
assert_buffer(b);
assert_line_desc(ld, b->encoding);
/* If we are in no man's land, we return. */
if (!b || !ld || !len || pos > ld->line_len || pos == ld->line_len && !ld->ld_node.next->next) return ERROR;
block_signals();
if (b->opt.do_undo && !(b->undoing || b->redoing)) {
const int error = add_undo_step(b, line, pos, len);
if (error) {
release_signals();
return error;
}
}
while(len) {
/* First case: we are just on the end of a line. We join the current
line with the following one (if it's there of course). If, however,
the current line is empty, we rather remove it. The only difference
is in the resulting syntax state. */
if (pos == ld->line_len) {
unsigned char *p;
line_desc *next_ld = (line_desc *)ld->ld_node.next;
/* There's nothing more to do--we are at the end of the file. */
if (next_ld->ld_node.next == NULL) break;
/* We're about to join line+1 to line; adjust mark and bookmarks accordingly. */
if (b->marking) {
if (b->block_start_line == line+1) {
b->block_start_line--;
b->block_start_pos += ld->line_len;
}
else if (b->block_start_line > line) b->block_start_line--;
}
for (n=0, mask=b->bookmark_mask; mask; n++, mask >>= 1) {
if (mask & 1) {
if (b->bookmark[n].line == line+1) {
b->bookmark[n].line--;
b->bookmark[n].pos += ld->line_len;
}
else if (b->bookmark[n].line > line) b->bookmark[n].line--;
}
}
/* If one of the lines is empty, or their contents are adjacent,
we either do nothing or simply set a pointer. */
if (!ld->line || !next_ld->line || ld->line + ld->line_len == next_ld->line) {
if (!ld->line) ld->line = next_ld->line;
}
else if ((n = alloc_chars_around(b, ld, next_ld->line_len, FALSE))<0 && (m = alloc_chars_around(b, next_ld, ld->line_len, TRUE))<0) {
/* We try to allocate characters around one line or the other
one; if we fail, we allocate enough space for both lines elsewhere. */
if (p = alloc_chars(b, ld->line_len + next_ld->line_len)) {
memcpy(p, ld->line, ld->line_len);
memcpy(p + ld->line_len, next_ld->line, next_ld->line_len);
free_chars(b, ld->line, ld->line_len);
free_chars(b, next_ld->line, next_ld->line_len);
ld->line = p;
}
else {
release_signals();
if (b->opt.do_undo && !(b->undoing || b->redoing)) fix_last_undo_step(b, -len);
return OUT_OF_MEMORY;
}
}
/* In case one of the alloc_chars_around succeeds, we have just to
move the lines in the right place. */
else if (n >= 0) {
if (n < next_ld->line_len) memmove(ld->line + (n - next_ld->line_len), ld->line, ld->line_len);
ld->line += (n - next_ld->line_len);
memcpy(ld->line + ld->line_len, next_ld->line, next_ld->line_len);
free_chars(b, next_ld->line, next_ld->line_len);
}
else {
if (m) memmove(next_ld->line + m, next_ld->line, next_ld->line_len);
next_ld->line += m;
memcpy(next_ld->line - ld->line_len, ld->line, ld->line_len);
free_chars(b, ld->line, ld->line_len);
ld->line = next_ld->line - ld->line_len;
}
ld->line_len += next_ld->line_len;
b->num_lines--;
rem(&next_ld->ld_node);
free_line_desc(b, next_ld);
len--;
if (!b->redoing) {
if (b->undoing) add_to_stream(&b->undo.redo, "", 1);
else if (b->opt.do_undo) add_to_undo_stream(&b->undo, "", 1);
}
}
/* Second case: we are inside a line. We delete len bytes or, if
there are less then len bytes to delete, we delete up to the end
of the line. In the latter case, we simply set the line length and
free the corresponding bytes. Otherwise, the number of bytes to
move is minimized. */
else {
n = len > ld->line_len - pos ? ld->line_len - pos : len;
/* We're about to erase n chars at (line,pos); adjust mark and bookmarks accordingly. */
if (b->marking)
if (b->block_start_line == line)
if (b->block_start_pos >= pos)
if (b->block_start_pos < pos + n)
b->block_start_pos = pos;
else
b->block_start_pos -= n;
for (m=0, mask=b->bookmark_mask; mask; m++, mask>>=1) {
if (mask & 1) {
if (b->bookmark[m].line == line)
if (b->bookmark[m].pos >= pos)
if (b->bookmark[m].pos < pos + n) b->bookmark[m].pos = pos;
else b->bookmark[m].pos -= n;
}
}
if (!b->redoing) {
if (b->undoing) add_to_stream(&b->undo.redo, &ld->line[pos], n);
else if (b->opt.do_undo) add_to_undo_stream(&b->undo, &ld->line[pos], n);
}
if (n == ld->line_len - pos) free_chars(b, &ld->line[pos], n);
else {
if (pos < ld->line_len / 2) {
memmove(ld->line + n, ld->line, pos);
free_chars(b, ld->line, n);
ld->line += n;
}
else {
memmove(ld->line + pos, ld->line + pos + n, ld->line_len - pos - n);
free_chars(b, &ld->line[ld->line_len - n], n);
}
}
if (!(ld->line_len -= n)) ld->line = NULL;
len -= n;
assert_line_desc(ld, b->encoding);
}
b->is_modified = 1;
}
if (b->opt.do_undo && !(b->undoing || b->redoing)) fix_last_undo_step(b, -len);
release_signals();
return OK;
}
/* Deletes a single character. */
int delete_one_char(buffer * const b, line_desc * const ld, const int line, const int pos) {
return delete_stream(b, ld, line, pos, b->encoding == ENC_UTF8 && pos < ld->line_len ? utf8len(ld->line[pos]) : 1);
}
/* Changes the buffer file name to the given string, which must have been
obtained through malloc(). */
void change_filename(buffer * const b, char * const name) {
assert(name != NULL);
if (b->filename) free(b->filename);
b->filename = name;
}
/* Here we load a file into a given buffer. The buffer lists are deallocated
first. If there is not write access to the file, the read-only flag is set.
Note that we consider line feeds 0x0A's, 0x0D's and 0x00's (the last being
made necessary by the way the pools are handled), unless the binary flag is
set, in which case we consider only the 0x00's. */
int load_file_in_buffer(buffer * const b, const char *name) {
int fh, result;
if (!b) return ERROR;
assert_buffer(b);
name = tilde_expand(name);
if (is_directory(name)) return FILE_IS_DIRECTORY;
if (is_migrated(name)) return FILE_IS_MIGRATED;
if ((fh = open(name, READ_FLAGS)) >= 0) {
result = load_fh_in_buffer(b, fh);
close(fh);
if (!result) b->opt.read_only = (access(name, W_OK) != 0);
return result;
}
return CANT_OPEN_FILE;
}
/* This function, together with insert_stream and delete_stream, is the only
way of modifying the contents of a buffer. While loading a file could have
passed through insert_stream, it would have been intolerably slow for large
files. The flexible pool struture of ne allows to load the file with a
single read in a big pool. */
int load_fh_in_buffer(buffer *b, int fh) {
off_t len;
int i, num_lines;
encoding_type encoding;
unsigned char *p, *q;
char_pool *cp;
line_desc_pool *ldp;
unsigned char terminators[] = { 0x0d, 0x0a };
if (b->opt.preserve_cr) terminators[0] = 0;
len = lseek(fh, 0, SEEK_END);
if (len < 0 || lseek(fh, 0, SEEK_SET) < 0) return IO_ERROR;
if (len > INT_MAX) return FILE_IS_TOO_LARGE;
free_buffer_contents(b);
if (!len) {
clear_buffer(b);
b->encoding = ENC_ASCII;
if (b->opt.do_undo) b->undo.last_save_step = 0;
return OK;
}
block_signals();
if (cp = alloc_char_pool(len + STANDARD_INCREMENT)) {
if (read(fh, cp->pool, len) < len) {
free_char_pool(cp);
release_signals();
clear_buffer(b);
return IO_ERROR;
}
b->allocated_chars = cp->size;
b->free_chars = cp->size - len;
p = cp->pool;
/* This is the first pass on the data we just read. We count the number
of lines. If we meet a CR/LF sequence and we did not ask for binary
files, we decide the file is of CR/LF type. Note that this cannot happen
if preserve_cr is set. */
for(num_lines = i=0; i < len; i++,p++)
if (!b->opt.binary && (*p == terminators[0] || *p == terminators[1]) || !*p) {
if (i < len - 1 && !b->opt.preserve_cr && p[0] == '\r' && p[1] == '\n') {
b->is_CRLF = TRUE;
p++, i++;
b->free_chars++;
}
num_lines++;
b->free_chars++;
}
num_lines++;
/* Now, if UTF-8 auto-detection is enabled, we try to guess whether this
buffer is in UTF-8. */
encoding = detect_encoding(cp->pool, len);
if (encoding == ENC_ASCII) b->encoding = ENC_ASCII;
else {
if (b->opt.utf8auto && encoding == ENC_UTF8) b->encoding = ENC_UTF8;
else b->encoding = ENC_8_BIT;
}
if (ldp = alloc_line_desc_pool(num_lines + STANDARD_LINE_INCREMENT)) {
p = cp->pool;
/* This is the second pass. Here we find the actual lines, and set to
NUL the line terminators if necessary, following the same rationale of
the first pass (this is important, as b->free_chars has been computed
on the first pass).*/
for(i = 0; i < num_lines; i++) {
line_desc *ld = do_syntax ? &ldp->pool[i] : (line_desc *)&((no_syntax_line_desc *)ldp->pool)[i];
rem(&ld->ld_node);
add_tail(&b->line_desc_list, &ld->ld_node);
/* ultima riga */
if (i == num_lines - 1) {
if (p - cp->pool < len) {
assert(*p && *p != terminators[0] && *p != terminators[1]);
ld->line = p;
ld->line_len = len - (p - cp->pool);
}
}
else {
q = p;
while((b->opt.binary || *q != terminators[0] && *q != terminators[1]) && *q) q++;
ld->line_len = q - p;
ld->line = q - p ? p : NULL;
if (q - cp->pool < len - 1 && !b->opt.preserve_cr && q[0] == '\r' && q[1] == '\n') *q++ = 0;
*q++ = 0;
p = q;
}
}
ldp->allocated_items = num_lines;
/* We set correctly the offsets of the first and last character used. If no
character is used (i.e., we have a file of line feeds), the char pool is
freed. */
if (b->free_chars < b->allocated_chars) {
cp->last_used = len;
while(!cp->pool[cp->first_used]) cp->first_used++;
while(!cp->pool[cp->last_used]) cp->last_used--;
add_head(&b->char_pool_list, &cp->cp_node);
assert_char_pool(cp);
}
else free_char_pool(cp);
add_head(&b->line_desc_pool_list, &ldp->ldp_node);
b->num_lines = num_lines;
reset_position_to_sof(b);
if (b->opt.do_undo) b->undo.last_save_step = 0;
reset_syntax_states(b);
release_signals();
return OK;
}
free_char_pool(cp);
}
release_signals();
clear_buffer(b);
return OUT_OF_MEMORY;
}
/* Recomputes initial states for all lines in a buffer. */
void reset_syntax_states(buffer *b) {
if (b->syn) {
line_desc *ld;
HIGHLIGHT_STATE next_line_state = { 0, 0, "" };
ld = (line_desc *)b->line_desc_list.head;
while(ld->ld_node.next) {
ld->highlight_state = next_line_state;
next_line_state = parse(b->syn, ld, next_line_state, b->encoding == ENC_UTF8);
ld = (line_desc *)ld->ld_node.next;
}
}
}
/* Ensures that the attribute buffer of this buffer is large enough. */
void ensure_attr_buf(buffer * const b, const int capacity) {
/* attr_buf already exists? */
if (!b->attr_buf) {
b->attr_size = capacity;
b->attr_buf = malloc(b->attr_size * sizeof *b->attr_buf);
}
else if (capacity > b->attr_size) {
b->attr_size = capacity;
b->attr_buf = realloc(b->attr_buf, b->attr_size * sizeof *b->attr_buf);
}
}
/* Here we save a buffer to a given file. If no file is specified, the
buffer filename field is used. The is_modified flag is set to 0. */
int save_buffer_to_file(buffer *b, const char *name) {
int fh, error = OK;
char *p;
line_desc *ld = (line_desc *)b->line_desc_list.head;
if (!b) return ERROR;
assert_buffer(b);
if (name == NULL) name = b->filename;
if (!name) return ERROR;
name = tilde_expand(name);
if (is_directory(name)) return FILE_IS_DIRECTORY;
if (is_migrated(name)) return FILE_IS_MIGRATED;
block_signals();
if ((fh = open(name, WRITE_FLAGS, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) >= 0) {
/* If we can allocate SAVE_BLOCK_LEN bytes, we will use
them as a buffer for our saves. */
if (p = malloc(SAVE_BLOCK_LEN + 1)) {
/* used keeps track of the number of bytes used in the buffer. l, len
specify the pointer to the block of characters to save, and
its length. In case of very long lines, or buffer border crossing,
they could point in the middle of a line descriptor. */
int used = 0, len;
char *l;
while(ld->ld_node.next) {
l = ld->line;
len = ld->line_len;
while(len > 0) {
if (SAVE_BLOCK_LEN - used > len) {
memcpy(p + used, l, len);
used += len;
len = 0;
}
else {
memcpy(p + used, l, SAVE_BLOCK_LEN - used);
len -= SAVE_BLOCK_LEN - used;
l += SAVE_BLOCK_LEN - used;
used = 0;
if (write(fh, p, SAVE_BLOCK_LEN) < SAVE_BLOCK_LEN) {
error = IO_ERROR;
break;
}
}
}
if (error) break;
ld = (line_desc *)ld->ld_node.next;
/* Note that the two previous blocks never leave used == SAVE_BLOCK_LEN.
Thus, we can always assume there are two free bytes at p+used. */
if (ld->ld_node.next) {
if (b->opt.binary) p[used++] = 0;
else {
if (b->is_CRLF) p[used++] = '\r';
p[used++] = '\n';
}
}
if (used >= SAVE_BLOCK_LEN) {
if (write(fh, p, used) < used) {
error = IO_ERROR;
break;
}
else used = 0;
}
}
if (!error && used && write(fh, p, used) < used) error = IO_ERROR;
free(p);
}
else {
/* If the buffer is not available, just save line by line. */
while(ld->ld_node.next) {
if (ld->line) {
if (write(fh, ld->line, ld->line_len) < ld->line_len) {
error = IO_ERROR;
break;
}
}
ld = (line_desc *)ld->ld_node.next;
if (ld->ld_node.next) {
if (!b->opt.binary && b->is_CRLF && write(fh, "\r", 1) < 1) {
error = IO_ERROR;
break;
}
if (write(fh, b->opt.binary ? "\0" : "\n", 1) < 1) {
error = IO_ERROR;
break;
}
}
}
}
if (close(fh)) error = IO_ERROR;
if (error == OK) b->is_modified = 0;
}
else error = CANT_OPEN_FILE;
release_signals();
return error;
}
/* Autosaves a given buffer. If the buffer has a name, a '#' is prefixed to
it. If the buffer has no name, a fake name is generated using the PID of ne
and the pointer to the buffer structure. This ensures uniqueness. Autosave
never writes on the original file, also because it can be called during an
emergency exit caused by a signal. */
void auto_save(buffer *b) {
char *p;
if (b->is_modified) {
if (b->filename) {
if (p = malloc(strlen(file_part(b->filename)) + 2)) {
strcpy(p, "#");
strcat(p, file_part(b->filename));
}
}
else {
if (p = malloc(MAX_INT_LEN * 2)) sprintf(p, "%p.%x", b, getpid());
}
save_buffer_to_file(b, p);
free(p);
}
}
|