aboutsummaryrefslogtreecommitdiffstats
path: root/src/editfns.c
diff options
context:
space:
mode:
authorRichard M. Stallman1993-02-11 06:00:51 +0000
committerRichard M. Stallman1993-02-11 06:00:51 +0000
commite9cf2084dd2528ba64186835a3e6fdeeffbf4466 (patch)
treedf958620772d1c6957f620532475d35a3db4b86e /src/editfns.c
parent3220bb911390ac319e2da80639b4f756a63d9137 (diff)
downloademacs-e9cf2084dd2528ba64186835a3e6fdeeffbf4466.tar.gz
emacs-e9cf2084dd2528ba64186835a3e6fdeeffbf4466.zip
(Fcompare_buffer_substrings): New function.
Diffstat (limited to 'src/editfns.c')
-rw-r--r--src/editfns.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/editfns.c b/src/editfns.c
index d804bbb1032..1e23171984e 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -817,6 +817,122 @@ They default to the beginning and the end of BUFFER.")
817 817
818 return Qnil; 818 return Qnil;
819} 819}
820
821DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings,
822 6, 6, 0,
823 "Compare two substrings of two buffers; return result as number.\n\
824the value is -N if first string is less after N-1 chars,\n\
825+N if first string is greater after N-1 chars, or 0 if strings match.\n\
826Each substring is represented as three arguments: BUFFER, START and END.\n\
827That makes six args in all, three for each substring.\n\n\
828The value of `case-fold-search' in the current buffer\n\
829determines whether case is significant or ignored.")
830 (buffer1, start1, end1, buffer2, start2, end2)
831 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
832{
833 register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i;
834 register struct buffer *bp1, *bp2;
835 register unsigned char *trt
836 = (!NILP (current_buffer->case_fold_search)
837 ? XSTRING (current_buffer->case_canon_table)->data : 0);
838
839 /* Find the first buffer and its substring. */
840
841 if (NILP (buffer1))
842 bp1 = current_buffer;
843 else
844 {
845 buffer1 = Fget_buffer (buffer1);
846 bp1 = XBUFFER (buffer1);
847 }
848
849 if (NILP (start1))
850 begp1 = BUF_BEGV (bp1);
851 else
852 {
853 CHECK_NUMBER_COERCE_MARKER (start1, 1);
854 begp1 = XINT (start1);
855 }
856 if (NILP (end1))
857 endp1 = BUF_ZV (bp1);
858 else
859 {
860 CHECK_NUMBER_COERCE_MARKER (end1, 2);
861 endp1 = XINT (end1);
862 }
863
864 if (begp1 > endp1)
865 temp = begp1, begp1 = endp1, endp1 = temp;
866
867 if (!(BUF_BEGV (bp1) <= begp1
868 && begp1 <= endp1
869 && endp1 <= BUF_ZV (bp1)))
870 args_out_of_range (start1, end1);
871
872 /* Likewise for second substring. */
873
874 if (NILP (buffer2))
875 bp2 = current_buffer;
876 else
877 {
878 buffer2 = Fget_buffer (buffer2);
879 bp2 = XBUFFER (buffer2);
880 }
881
882 if (NILP (start2))
883 begp2 = BUF_BEGV (bp2);
884 else
885 {
886 CHECK_NUMBER_COERCE_MARKER (start2, 4);
887 begp2 = XINT (start2);
888 }
889 if (NILP (end2))
890 endp2 = BUF_ZV (bp2);
891 else
892 {
893 CHECK_NUMBER_COERCE_MARKER (end2, 5);
894 endp2 = XINT (end2);
895 }
896
897 if (begp2 > endp2)
898 temp = begp2, begp2 = endp2, endp2 = temp;
899
900 if (!(BUF_BEGV (bp2) <= begp2
901 && begp2 <= endp2
902 && endp2 <= BUF_ZV (bp2)))
903 args_out_of_range (start2, end2);
904
905 len1 = endp1 - begp1;
906 len2 = endp2 - begp2;
907 length = len1;
908 if (len2 < length)
909 length = len2;
910
911 for (i = 0; i < length; i++)
912 {
913 int c1 = *BUF_CHAR_ADDRESS (bp1, begp1 + i);
914 int c2 = *BUF_CHAR_ADDRESS (bp2, begp2 + i);
915 if (trt)
916 {
917 c1 = trt[c1];
918 c2 = trt[c2];
919 }
920 if (c1 < c2)
921 return make_number (- 1 - i);
922 if (c1 > c2)
923 return make_number (i + 1);
924 }
925
926 /* The strings match as far as they go.
927 If one is shorter, that one is less. */
928 if (length < len1)
929 return make_number (length + 1);
930 else if (length < len2)
931 return make_number (- length - 1);
932
933 /* Same length too => they are equal. */
934 return make_number (0);
935}
820 936
821DEFUN ("subst-char-in-region", Fsubst_char_in_region, 937DEFUN ("subst-char-in-region", Fsubst_char_in_region,
822 Ssubst_char_in_region, 4, 5, 0, 938 Ssubst_char_in_region, 4, 5, 0,
@@ -1319,6 +1435,7 @@ syms_of_editfns ()
1319 defsubr (&Sformat); 1435 defsubr (&Sformat);
1320 1436
1321 defsubr (&Sinsert_buffer_substring); 1437 defsubr (&Sinsert_buffer_substring);
1438 defsubr (&Scompare_buffer_substrings);
1322 defsubr (&Ssubst_char_in_region); 1439 defsubr (&Ssubst_char_in_region);
1323 defsubr (&Stranslate_region); 1440 defsubr (&Stranslate_region);
1324 defsubr (&Sdelete_region); 1441 defsubr (&Sdelete_region);