comparison src/editfns.c @ 57644:c0f2bdd90b5d

(Ftranslate_region_internal): New function. (syms_of_editfns): Defsubr it.
author Kenichi Handa <handa@m17n.org>
date Fri, 22 Oct 2004 13:09:39 +0000
parents 6a526f57129e
children c0675c413aa3
comparison
equal deleted inserted replaced
57643:3b8eabf02bad 57644:c0f2bdd90b5d
2734 2734
2735 unbind_to (count, Qnil); 2735 unbind_to (count, Qnil);
2736 return Qnil; 2736 return Qnil;
2737 } 2737 }
2738 2738
2739 DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0, 2739 DEFUN ("translate-region-internal", Ftranslate_region_internal,
2740 doc: /* From START to END, translate characters according to TABLE. 2740 Stranslate_region_internal, 3, 3, 0,
2741 doc: /* Internal use only.
2742 From START to END, translate characters according to TABLE.
2741 TABLE is a string; the Nth character in it is the mapping 2743 TABLE is a string; the Nth character in it is the mapping
2742 for the character with code N. 2744 for the character with code N.
2743 It returns the number of characters changed. */) 2745 It returns the number of characters changed. */)
2744 (start, end, table) 2746 (start, end, table)
2745 Lisp_Object start; 2747 Lisp_Object start;
2748 { 2750 {
2749 register unsigned char *tt; /* Trans table. */ 2751 register unsigned char *tt; /* Trans table. */
2750 register int nc; /* New character. */ 2752 register int nc; /* New character. */
2751 int cnt; /* Number of changes made. */ 2753 int cnt; /* Number of changes made. */
2752 int size; /* Size of translate table. */ 2754 int size; /* Size of translate table. */
2753 int pos, pos_byte; 2755 int pos, pos_byte, end_pos;
2754 int multibyte = !NILP (current_buffer->enable_multibyte_characters); 2756 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
2755 int string_multibyte; 2757 int string_multibyte;
2756 2758
2757 validate_region (&start, &end); 2759 validate_region (&start, &end);
2758 CHECK_STRING (table); 2760 if (CHAR_TABLE_P (table))
2759 2761 {
2760 if (multibyte != (SCHARS (table) < SBYTES (table))) 2762 size = MAX_CHAR;
2761 table = (multibyte 2763 tt = NULL;
2762 ? string_make_multibyte (table) 2764 }
2763 : string_make_unibyte (table)); 2765 else
2764 string_multibyte = SCHARS (table) < SBYTES (table); 2766 {
2765 2767 CHECK_STRING (table);
2766 size = SCHARS (table); 2768
2767 tt = SDATA (table); 2769 if (! multibyte && (SCHARS (table) < SBYTES (table)))
2770 table = string_make_unibyte (table);
2771 string_multibyte = SCHARS (table) < SBYTES (table);
2772 size = SCHARS (table);
2773 tt = SDATA (table);
2774 }
2768 2775
2769 pos = XINT (start); 2776 pos = XINT (start);
2770 pos_byte = CHAR_TO_BYTE (pos); 2777 pos_byte = CHAR_TO_BYTE (pos);
2778 end_pos = XINT (end);
2771 modify_region (current_buffer, pos, XINT (end)); 2779 modify_region (current_buffer, pos, XINT (end));
2772 2780
2773 cnt = 0; 2781 cnt = 0;
2774 for (; pos < XINT (end); ) 2782 for (; pos < end_pos; )
2775 { 2783 {
2776 register unsigned char *p = BYTE_POS_ADDR (pos_byte); 2784 register unsigned char *p = BYTE_POS_ADDR (pos_byte);
2777 unsigned char *str; 2785 unsigned char *str, buf[MAX_MULTIBYTE_LENGTH];
2778 int len, str_len; 2786 int len, str_len;
2779 int oc; 2787 int oc;
2780 2788
2781 if (multibyte) 2789 if (multibyte)
2782 oc = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, len); 2790 oc = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, len);
2783 else 2791 else
2784 oc = *p, len = 1; 2792 oc = *p, len = 1;
2785 if (oc < size) 2793 if (oc < size)
2786 { 2794 {
2787 if (string_multibyte) 2795 if (tt)
2788 { 2796 {
2789 str = tt + string_char_to_byte (table, oc); 2797 if (string_multibyte)
2790 nc = STRING_CHAR_AND_LENGTH (str, MAX_MULTIBYTE_LENGTH, str_len); 2798 {
2799 str = tt + string_char_to_byte (table, oc);
2800 nc = STRING_CHAR_AND_LENGTH (str, MAX_MULTIBYTE_LENGTH,
2801 str_len);
2802 }
2803 else
2804 {
2805 nc = tt[oc];
2806 if (! ASCII_BYTE_P (nc) && multibyte)
2807 {
2808 str_len = CHAR_STRING (nc, buf);
2809 str = buf;
2810 }
2811 else
2812 {
2813 str_len = 1;
2814 str = tt + oc;
2815 }
2816 }
2791 } 2817 }
2792 else 2818 else
2793 { 2819 {
2794 str = tt + oc; 2820 Lisp_Object val;
2795 nc = tt[oc], str_len = 1; 2821 int c;
2822
2823 nc = oc;
2824 val = CHAR_TABLE_REF (table, oc);
2825 if (INTEGERP (val)
2826 && (c = XINT (val), CHAR_VALID_P (c, 0)))
2827 {
2828 nc = c;
2829 str_len = CHAR_STRING (nc, buf);
2830 str = buf;
2831 }
2796 } 2832 }
2833
2797 if (nc != oc) 2834 if (nc != oc)
2798 { 2835 {
2799 if (len != str_len) 2836 if (len != str_len)
2800 { 2837 {
2801 Lisp_Object string; 2838 Lisp_Object string;
4288 defsubr (&Sformat); 4325 defsubr (&Sformat);
4289 4326
4290 defsubr (&Sinsert_buffer_substring); 4327 defsubr (&Sinsert_buffer_substring);
4291 defsubr (&Scompare_buffer_substrings); 4328 defsubr (&Scompare_buffer_substrings);
4292 defsubr (&Ssubst_char_in_region); 4329 defsubr (&Ssubst_char_in_region);
4293 defsubr (&Stranslate_region); 4330 defsubr (&Stranslate_region_internal);
4294 defsubr (&Sdelete_region); 4331 defsubr (&Sdelete_region);
4295 defsubr (&Sdelete_and_extract_region); 4332 defsubr (&Sdelete_and_extract_region);
4296 defsubr (&Swiden); 4333 defsubr (&Swiden);
4297 defsubr (&Snarrow_to_region); 4334 defsubr (&Snarrow_to_region);
4298 defsubr (&Ssave_restriction); 4335 defsubr (&Ssave_restriction);