comparison src/buffer.c @ 20868:83f23c9f7c4d

(advance_to_char_boundary): New function. (Fset_buffer_multibyte): Advance all byte-positions to char boundaries. Clear undo list.
author Richard M. Stallman <rms@gnu.org>
date Sun, 08 Feb 1998 21:10:31 +0000
parents 58bb74217a10
children 2fc5eb0799fe
comparison
equal deleted inserted replaced
20867:fccf74829150 20868:83f23c9f7c4d
1670 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e) 1670 if (!(BEGV <= XINT (*b) && XINT (*b) <= XINT (*e)
1671 && XINT (*e) <= ZV)) 1671 && XINT (*e) <= ZV))
1672 args_out_of_range (*b, *e); 1672 args_out_of_range (*b, *e);
1673 } 1673 }
1674 1674
1675 /* Advance BYTE_POS up to a character boundary
1676 and return the adjusted position. */
1677
1678 static int
1679 advance_to_char_boundary (byte_pos)
1680 int byte_pos;
1681 {
1682 int pos = byte_pos;
1683
1684 while (1)
1685 {
1686 int c = FETCH_BYTE (pos);
1687 if (SINGLE_BYTE_CHAR_P (c))
1688 break;
1689 if (c == LEADING_CODE_COMPOSITION)
1690 break;
1691 if (BYTES_BY_CHAR_HEAD (c) > 1)
1692 break;
1693 pos++;
1694 }
1695
1696 return pos;
1697 }
1698
1675 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte, 1699 DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
1676 1, 1, 0, 1700 1, 1, 0,
1677 "Set the multibyte flag of the current buffer to FLAG.\n\ 1701 "Set the multibyte flag of the current buffer to FLAG.\n\
1678 If FLAG is t, this makes the buffer a multibyte buffer.\n\ 1702 If FLAG is t, this makes the buffer a multibyte buffer.\n\
1679 If FLAG is nil, this makes the buffer a single-byte buffer.\n\ 1703 If FLAG is nil, this makes the buffer a single-byte buffer.\n\
1682 (flag) 1706 (flag)
1683 Lisp_Object flag; 1707 Lisp_Object flag;
1684 { 1708 {
1685 Lisp_Object tail, markers; 1709 Lisp_Object tail, markers;
1686 1710
1711 /* It would be better to update the list,
1712 but this is good enough for now. */
1713 if (! EQ (current_buffer->undo_list, Qt))
1714 current_buffer->undo_list = Qnil;
1715
1687 /* If the cached position is for this buffer, clear it out. */ 1716 /* If the cached position is for this buffer, clear it out. */
1688 clear_charpos_cache (current_buffer); 1717 clear_charpos_cache (current_buffer);
1689 1718
1690 if (NILP (flag)) 1719 if (NILP (flag))
1691 { 1720 {
1712 { 1741 {
1713 /* Do this first, so that chars_in_text asks the right question. 1742 /* Do this first, so that chars_in_text asks the right question.
1714 set_intervals_multibyte needs it too. */ 1743 set_intervals_multibyte needs it too. */
1715 current_buffer->enable_multibyte_characters = Qt; 1744 current_buffer->enable_multibyte_characters = Qt;
1716 1745
1746 GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
1717 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG; 1747 GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
1748
1718 Z = chars_in_text (GPT_ADDR, Z_BYTE - GPT_BYTE) + GPT; 1749 Z = chars_in_text (GPT_ADDR, Z_BYTE - GPT_BYTE) + GPT;
1750
1751 BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
1719 if (BEGV_BYTE > GPT_BYTE) 1752 if (BEGV_BYTE > GPT_BYTE)
1720 BEGV = chars_in_text (GPT_ADDR, BEGV_BYTE - GPT_BYTE) + GPT; 1753 BEGV = chars_in_text (GPT_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
1721 else 1754 else
1722 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG; 1755 BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
1756
1757 ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
1723 if (ZV_BYTE > GPT_BYTE) 1758 if (ZV_BYTE > GPT_BYTE)
1724 ZV = chars_in_text (GPT_ADDR, ZV_BYTE - GPT_BYTE) + GPT; 1759 ZV = chars_in_text (GPT_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
1725 else 1760 else
1726 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG; 1761 ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
1727 if (PT_BYTE > GPT_BYTE) 1762
1728 current_buffer->pt = chars_in_text (GPT_ADDR, PT_BYTE - GPT_BYTE) + GPT; 1763 {
1729 else 1764 int pt_byte = advance_to_char_boundary (PT_BYTE);
1730 current_buffer->pt = chars_in_text (BEG_ADDR, PT_BYTE - BEG_BYTE) + BEG; 1765 int pt;
1766
1767 if (pt_byte > GPT_BYTE)
1768 pt = chars_in_text (GPT_ADDR, pt_byte - GPT_BYTE) + GPT;
1769 else
1770 pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
1771 TEMP_SET_PT_BOTH (pt, pt_byte);
1772 }
1731 1773
1732 tail = markers = BUF_MARKERS (current_buffer); 1774 tail = markers = BUF_MARKERS (current_buffer);
1733 BUF_MARKERS (current_buffer) = Qnil; 1775 BUF_MARKERS (current_buffer) = Qnil;
1734 1776
1735 while (XSYMBOL (tail) != XSYMBOL (Qnil)) 1777 while (XSYMBOL (tail) != XSYMBOL (Qnil))
1736 { 1778 {
1779 XMARKER (tail)->bytepos
1780 = advance_to_char_boundary (XMARKER (tail)->bytepos);
1737 XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos); 1781 XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
1782
1738 tail = XMARKER (tail)->chain; 1783 tail = XMARKER (tail)->chain;
1739 } 1784 }
1740 BUF_MARKERS (current_buffer) = markers; 1785 BUF_MARKERS (current_buffer) = markers;
1741 1786
1742 /* Do this last, so it can calculate the new correspondences 1787 /* Do this last, so it can calculate the new correspondences