Mercurial > hg > xemacs-beta
comparison src/insdel.c @ 98:0d2f883870bc r20-1b1
Import from CVS: tag r20-1b1
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:13:56 +0200 |
parents | 131b0175ea99 |
children | 8619ce7e4c50 |
comparison
equal
deleted
inserted
replaced
97:498bf5da1c90 | 98:0d2f883870bc |
---|---|
230 /* Set the address of beginning of buffer. */ | 230 /* Set the address of beginning of buffer. */ |
231 #define SET_BUF_BEG_ADDR(buf, addr) do { (buf)->text->beg = (addr); } while (0) | 231 #define SET_BUF_BEG_ADDR(buf, addr) do { (buf)->text->beg = (addr); } while (0) |
232 | 232 |
233 /* Gap size. */ | 233 /* Gap size. */ |
234 #define BUF_GAP_SIZE(buf) ((buf)->text->gap_size + 0) | 234 #define BUF_GAP_SIZE(buf) ((buf)->text->gap_size + 0) |
235 | 235 #define BUF_END_GAP_SIZE(buf) ((buf)->text->end_gap_size + 0) |
236 /* Set gap size. */ | 236 /* Set gap size. */ |
237 #define SET_BUF_GAP_SIZE(buf, value) \ | 237 #define SET_BUF_GAP_SIZE(buf, value) \ |
238 do { (buf)->text->gap_size = (value); } while (0) | 238 do { (buf)->text->gap_size = (value); } while (0) |
239 #define SET_BUF_END_GAP_SIZE(buf, value) \ | |
240 do { (buf)->text->end_gap_size = (value); } while (0) | |
239 | 241 |
240 /* Gap location. */ | 242 /* Gap location. */ |
241 #define BI_BUF_GPT(buf) ((buf)->text->gpt + 0) | 243 #define BI_BUF_GPT(buf) ((buf)->text->gpt + 0) |
242 #define BUF_GPT_ADDR(buf) (BUF_BEG_ADDR (buf) + BI_BUF_GPT (buf) - 1) | 244 #define BUF_GPT_ADDR(buf) (BUF_BEG_ADDR (buf) + BI_BUF_GPT (buf) - 1) |
243 | 245 |
1799 SET_GAP_SENTINEL (buf); | 1801 SET_GAP_SENTINEL (buf); |
1800 #ifdef ERROR_CHECK_EXTENTS | 1802 #ifdef ERROR_CHECK_EXTENTS |
1801 sledgehammer_extent_check (make_buffer (buf)); | 1803 sledgehammer_extent_check (make_buffer (buf)); |
1802 #endif | 1804 #endif |
1803 } | 1805 } |
1806 if (pos == BI_BUF_Z (buf)) | |
1807 { | |
1808 /* merge gap with end gap */ | |
1809 | |
1810 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + BUF_END_GAP_SIZE (buf)); | |
1811 SET_BUF_END_GAP_SIZE (buf, 0); | |
1812 SET_END_SENTINEL (buf); | |
1813 } | |
1814 | |
1804 QUIT; | 1815 QUIT; |
1805 } | 1816 } |
1806 | 1817 |
1807 /* Move gap to position `pos'. | 1818 /* Move gap to position `pos'. |
1808 Note that this can quit! */ | 1819 Note that this can quit! */ |
1816 gap_left (buf, pos); | 1827 gap_left (buf, pos); |
1817 else if (pos > BI_BUF_GPT (buf)) | 1828 else if (pos > BI_BUF_GPT (buf)) |
1818 gap_right (buf, pos); | 1829 gap_right (buf, pos); |
1819 } | 1830 } |
1820 | 1831 |
1832 /* Merge the end gap into the gap */ | |
1833 | |
1834 static void | |
1835 merge_gap_with_end_gap (struct buffer *buf) | |
1836 { | |
1837 Lisp_Object tem; | |
1838 Bytind real_gap_loc; | |
1839 Bytecount old_gap_size; | |
1840 Bytecount increment; | |
1841 | |
1842 increment = BUF_END_GAP_SIZE (buf); | |
1843 SET_BUF_END_GAP_SIZE (buf, 0); | |
1844 | |
1845 if (increment > 0) | |
1846 { | |
1847 /* Prevent quitting in move_gap. */ | |
1848 tem = Vinhibit_quit; | |
1849 Vinhibit_quit = Qt; | |
1850 | |
1851 real_gap_loc = BI_BUF_GPT (buf); | |
1852 old_gap_size = BUF_GAP_SIZE (buf); | |
1853 | |
1854 /* Pretend the end gap is the gap */ | |
1855 SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf)); | |
1856 SET_BUF_GAP_SIZE (buf, increment); | |
1857 | |
1858 /* Move the new gap down to be consecutive with the end of the old one. | |
1859 This adjusts the markers properly too. */ | |
1860 gap_left (buf, real_gap_loc + old_gap_size); | |
1861 | |
1862 /* Now combine the two into one large gap. */ | |
1863 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + old_gap_size); | |
1864 SET_BI_BUF_GPT (buf, real_gap_loc); | |
1865 SET_GAP_SENTINEL (buf); | |
1866 | |
1867 /* We changed the total size of the buffer (including gap), | |
1868 so we need to fix up the end sentinel. */ | |
1869 SET_END_SENTINEL (buf); | |
1870 | |
1871 Vinhibit_quit = tem; | |
1872 } | |
1873 } | |
1874 | |
1821 /* Make the gap INCREMENT bytes longer. */ | 1875 /* Make the gap INCREMENT bytes longer. */ |
1822 | 1876 |
1823 static void | 1877 static void |
1824 make_gap (struct buffer *buf, Bytecount increment) | 1878 make_gap (struct buffer *buf, Bytecount increment) |
1825 { | 1879 { |
1830 | 1884 |
1831 /* If we have to get more space, get enough to last a while. We use | 1885 /* If we have to get more space, get enough to last a while. We use |
1832 a geometric progession that saves on realloc space. */ | 1886 a geometric progession that saves on realloc space. */ |
1833 increment += 2000 + ((BI_BUF_Z (buf) - BI_BUF_BEG (buf)) / 8); | 1887 increment += 2000 + ((BI_BUF_Z (buf) - BI_BUF_BEG (buf)) / 8); |
1834 | 1888 |
1835 /* Don't allow a buffer size that won't fit in an int | 1889 if (increment > BUF_END_GAP_SIZE (buf)) |
1836 even if it will fit in a Lisp integer. | 1890 { |
1837 That won't work because so many places use `int'. */ | 1891 /* Don't allow a buffer size that won't fit in an int |
1838 | 1892 even if it will fit in a Lisp integer. |
1839 if (BUF_Z (buf) - BUF_BEG (buf) + BUF_GAP_SIZE (buf) + increment | 1893 That won't work because so many places use `int'. */ |
1840 >= ((unsigned) 1 << (min (INTBITS, VALBITS) - 1))) | 1894 |
1841 error ("Buffer exceeds maximum size"); | 1895 if (BUF_Z (buf) - BUF_BEG (buf) + BUF_GAP_SIZE (buf) + increment |
1842 | 1896 >= ((unsigned) 1 << (min (INTBITS, VALBITS) - 1))) |
1843 result = BUFFER_REALLOC (buf->text->beg, | 1897 error ("Buffer exceeds maximum size"); |
1844 BI_BUF_Z (buf) - BI_BUF_BEG (buf) + | 1898 |
1845 BUF_GAP_SIZE (buf) + increment + | 1899 result = BUFFER_REALLOC (buf->text->beg, |
1846 BUF_END_SENTINEL_SIZE); | 1900 BI_BUF_Z (buf) - BI_BUF_BEG (buf) + |
1847 if (result == 0) | 1901 BUF_GAP_SIZE (buf) + increment + |
1848 memory_full (); | 1902 BUF_END_SENTINEL_SIZE); |
1849 SET_BUF_BEG_ADDR (buf, result); | 1903 if (result == 0) |
1850 | 1904 memory_full (); |
1905 | |
1906 SET_BUF_BEG_ADDR (buf, result); | |
1907 } | |
1908 else | |
1909 increment = BUF_END_GAP_SIZE (buf); | |
1910 | |
1851 /* Prevent quitting in move_gap. */ | 1911 /* Prevent quitting in move_gap. */ |
1852 tem = Vinhibit_quit; | 1912 tem = Vinhibit_quit; |
1853 Vinhibit_quit = Qt; | 1913 Vinhibit_quit = Qt; |
1854 | 1914 |
1855 real_gap_loc = BI_BUF_GPT (buf); | 1915 real_gap_loc = BI_BUF_GPT (buf); |
1856 old_gap_size = BUF_GAP_SIZE (buf); | 1916 old_gap_size = BUF_GAP_SIZE (buf); |
1857 | 1917 |
1858 /* Call the newly allocated space a gap at the end of the whole space. */ | 1918 /* Call the newly allocated space a gap at the end of the whole space. */ |
1859 SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf)); | 1919 SET_BI_BUF_GPT (buf, BI_BUF_Z (buf) + BUF_GAP_SIZE (buf)); |
1860 SET_BUF_GAP_SIZE (buf, increment); | 1920 SET_BUF_GAP_SIZE (buf, increment); |
1921 | |
1922 SET_BUF_END_GAP_SIZE (buf, 0); | |
1861 | 1923 |
1862 /* Move the new gap down to be consecutive with the end of the old one. | 1924 /* Move the new gap down to be consecutive with the end of the old one. |
1863 This adjusts the markers properly too. */ | 1925 This adjusts the markers properly too. */ |
1864 gap_left (buf, real_gap_loc + old_gap_size); | 1926 gap_left (buf, real_gap_loc + old_gap_size); |
1865 | 1927 |
2373 /* #### if debug-on-quit is invoked and the user changes the | 2435 /* #### if debug-on-quit is invoked and the user changes the |
2374 buffer, bad things can happen. This is a rampant problem | 2436 buffer, bad things can happen. This is a rampant problem |
2375 in Emacs. */ | 2437 in Emacs. */ |
2376 move_gap (buf, ind); /* may QUIT */ | 2438 move_gap (buf, ind); /* may QUIT */ |
2377 if (! GAP_CAN_HOLD_SIZE_P (buf, length)) | 2439 if (! GAP_CAN_HOLD_SIZE_P (buf, length)) |
2378 make_gap (buf, length - BUF_GAP_SIZE (buf)); | 2440 { |
2441 if (BUF_END_GAP_SIZE (buf) >= length) | |
2442 merge_gap_with_end_gap (buf); | |
2443 else | |
2444 make_gap (buf, length - BUF_GAP_SIZE (buf)); | |
2445 } | |
2379 | 2446 |
2380 record_insert (buf, pos, cclen); | 2447 record_insert (buf, pos, cclen); |
2381 BUF_MODIFF (buf)++; | 2448 BUF_MODIFF (buf)++; |
2382 MARK_BUFFERS_CHANGED; | 2449 MARK_BUFFERS_CHANGED; |
2383 | 2450 |
2562 | 2629 |
2563 bi_from = bufpos_to_bytind (buf, from); | 2630 bi_from = bufpos_to_bytind (buf, from); |
2564 bi_to = bufpos_to_bytind (buf, to); | 2631 bi_to = bufpos_to_bytind (buf, to); |
2565 bc_numdel = bi_to - bi_from; | 2632 bc_numdel = bi_to - bi_from; |
2566 | 2633 |
2567 /* Make sure the gap is somewhere in or next to what we are deleting. */ | 2634 if (to == BUF_Z (buf) && |
2568 if (bi_to < BI_BUF_GPT (buf)) | 2635 bi_from > BI_BUF_GPT (buf)) |
2569 gap_left (buf, bi_to); | 2636 { |
2570 if (bi_from > BI_BUF_GPT (buf)) | 2637 /* avoid moving the gap just to delete from the bottom. */ |
2571 gap_right (buf, bi_from); | 2638 |
2572 | 2639 record_delete (buf, from, numdel); |
2573 record_delete (buf, from, numdel); | 2640 BUF_MODIFF (buf)++; |
2574 BUF_MODIFF (buf)++; | 2641 MARK_BUFFERS_CHANGED; |
2575 MARK_BUFFERS_CHANGED; | 2642 |
2576 | 2643 /* Relocate point as if it were a marker. */ |
2577 /* Relocate point as if it were a marker. */ | 2644 if (bi_from < BI_BUF_PT (buf)) |
2578 if (bi_from < BI_BUF_PT (buf)) | 2645 { |
2579 { | 2646 if (BI_BUF_PT (buf) < bi_to) |
2580 if (BI_BUF_PT (buf) < bi_to) | 2647 JUST_SET_POINT (buf, from, bi_from); |
2581 JUST_SET_POINT (buf, from, bi_from); | 2648 else |
2582 else | 2649 JUST_SET_POINT (buf, BUF_PT (buf) - numdel, |
2583 JUST_SET_POINT (buf, BUF_PT (buf) - numdel, | 2650 BI_BUF_PT (buf) - bc_numdel); |
2584 BI_BUF_PT (buf) - bc_numdel); | 2651 } |
2585 } | 2652 |
2586 | 2653 /* Detach any extents that are completely within the range [FROM, TO], |
2587 /* Detach any extents that are completely within the range [FROM, TO], | 2654 if the extents are detachable. |
2588 if the extents are detachable. | 2655 |
2589 | 2656 This must come AFTER record_delete(), so that the appropriate extents |
2590 This must come AFTER record_delete(), so that the appropriate extents | 2657 will be present to be recorded, and BEFORE the gap size is increased, |
2591 will be present to be recorded, and BEFORE the gap size is increased, | 2658 as otherwise we will be confused about where the extents end. */ |
2592 as otherwise we will be confused about where the extents end. */ | 2659 process_extents_for_deletion (bufobj, bi_from, bi_to, 0); |
2593 process_extents_for_deletion (bufobj, bi_from, bi_to, 0); | 2660 |
2594 | 2661 /* Relocate all markers pointing into the new, larger gap |
2595 /* Relocate all markers pointing into the new, larger gap | 2662 to point at the end of the text before the gap. */ |
2596 to point at the end of the text before the gap. */ | 2663 adjust_markers (buf, |
2597 adjust_markers (buf, | 2664 (bi_to + BUF_GAP_SIZE (buf)), |
2598 (bi_to + BUF_GAP_SIZE (buf)), | 2665 (bi_to + BUF_GAP_SIZE (buf)), |
2599 (bi_to + BUF_GAP_SIZE (buf)), | 2666 (- bc_numdel)); |
2600 (- bc_numdel - BUF_GAP_SIZE (buf))); | 2667 |
2601 | 2668 /* Relocate any extent endpoints just like markers. */ |
2602 /* Relocate any extent endpoints just like markers. */ | 2669 adjust_extents_for_deletion (bufobj, bi_from, bi_to, |
2603 adjust_extents_for_deletion (bufobj, bi_from, bi_to, BUF_GAP_SIZE (buf), | 2670 BUF_GAP_SIZE (buf), bc_numdel, 0); |
2604 bc_numdel); | 2671 SET_BUF_END_GAP_SIZE (buf, BUF_END_GAP_SIZE (buf) + bc_numdel); |
2605 | 2672 |
2606 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + bc_numdel); | 2673 SET_BOTH_BUF_ZV (buf, BUF_ZV (buf) - numdel, BI_BUF_ZV (buf) - bc_numdel); |
2607 SET_BOTH_BUF_ZV (buf, BUF_ZV (buf) - numdel, BI_BUF_ZV (buf) - bc_numdel); | 2674 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel); |
2608 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel); | 2675 SET_GAP_SENTINEL (buf); |
2609 SET_BI_BUF_GPT (buf, bi_from); | 2676 } |
2610 SET_GAP_SENTINEL (buf); | 2677 else |
2678 { | |
2679 /* Make sure the gap is somewhere in or next to what we are deleting. */ | |
2680 if (bi_to < BI_BUF_GPT (buf)) | |
2681 gap_left (buf, bi_to); | |
2682 if (bi_from > BI_BUF_GPT (buf)) | |
2683 gap_right (buf, bi_from); | |
2684 | |
2685 record_delete (buf, from, numdel); | |
2686 BUF_MODIFF (buf)++; | |
2687 MARK_BUFFERS_CHANGED; | |
2688 | |
2689 /* Relocate point as if it were a marker. */ | |
2690 if (bi_from < BI_BUF_PT (buf)) | |
2691 { | |
2692 if (BI_BUF_PT (buf) < bi_to) | |
2693 JUST_SET_POINT (buf, from, bi_from); | |
2694 else | |
2695 JUST_SET_POINT (buf, BUF_PT (buf) - numdel, | |
2696 BI_BUF_PT (buf) - bc_numdel); | |
2697 } | |
2698 | |
2699 /* Detach any extents that are completely within the range [FROM, TO], | |
2700 if the extents are detachable. | |
2701 | |
2702 This must come AFTER record_delete(), so that the appropriate extents | |
2703 will be present to be recorded, and BEFORE the gap size is increased, | |
2704 as otherwise we will be confused about where the extents end. */ | |
2705 process_extents_for_deletion (bufobj, bi_from, bi_to, 0); | |
2706 | |
2707 /* Relocate all markers pointing into the new, larger gap | |
2708 to point at the end of the text before the gap. */ | |
2709 adjust_markers (buf, | |
2710 (bi_to + BUF_GAP_SIZE (buf)), | |
2711 (bi_to + BUF_GAP_SIZE (buf)), | |
2712 (- bc_numdel - BUF_GAP_SIZE (buf))); | |
2713 | |
2714 /* Relocate any extent endpoints just like markers. */ | |
2715 adjust_extents_for_deletion (bufobj, bi_from, bi_to, BUF_GAP_SIZE (buf), | |
2716 bc_numdel, BUF_GAP_SIZE (buf)); | |
2717 | |
2718 SET_BUF_GAP_SIZE (buf, BUF_GAP_SIZE (buf) + bc_numdel); | |
2719 SET_BOTH_BUF_ZV (buf, BUF_ZV (buf) - numdel, BI_BUF_ZV (buf) - bc_numdel); | |
2720 SET_BOTH_BUF_Z (buf, BUF_Z (buf) - numdel, BI_BUF_Z (buf) - bc_numdel); | |
2721 SET_BI_BUF_GPT (buf, bi_from); | |
2722 SET_GAP_SENTINEL (buf); | |
2723 } | |
2611 | 2724 |
2612 #ifdef MULE | 2725 #ifdef MULE |
2613 buffer_mule_signal_deleted_region (buf, from, to, bi_from, bi_to); | 2726 buffer_mule_signal_deleted_region (buf, from, to, bi_from, bi_to); |
2614 #endif | 2727 #endif |
2615 | 2728 |
2955 SET_BUF_GAP_SIZE (b, 20); | 3068 SET_BUF_GAP_SIZE (b, 20); |
2956 (void) BUFFER_ALLOC (b->text->beg, | 3069 (void) BUFFER_ALLOC (b->text->beg, |
2957 BUF_GAP_SIZE (b) + BUF_END_SENTINEL_SIZE); | 3070 BUF_GAP_SIZE (b) + BUF_END_SENTINEL_SIZE); |
2958 if (! BUF_BEG_ADDR (b)) | 3071 if (! BUF_BEG_ADDR (b)) |
2959 memory_full (); | 3072 memory_full (); |
2960 | 3073 |
3074 SET_BUF_END_GAP_SIZE (b, 0); | |
2961 SET_BI_BUF_GPT (b, 1); | 3075 SET_BI_BUF_GPT (b, 1); |
2962 SET_BOTH_BUF_Z (b, 1, 1); | 3076 SET_BOTH_BUF_Z (b, 1, 1); |
2963 SET_GAP_SENTINEL (b); | 3077 SET_GAP_SENTINEL (b); |
2964 SET_END_SENTINEL (b); | 3078 SET_END_SENTINEL (b); |
2965 #ifdef MULE | 3079 #ifdef MULE |