comparison src/search.c @ 17463:bb9ae80d22e2

(looking_at_1): Set re_match_object. (string_match_1): Set re_match_object. (fast_string_match): Set re_match_object. (Fskip_chars_forward): Move to syntax.c. (Fskip_chars_backward): Move to syntax.c. (Fskip_syntax_forward): Move to syntax.c. (Fskip_syntax_backward): Move to syntax.c. (skip_chars): Move to syntax.c. (search_buffer): Set re_match_object. (syms_of_search): Move initialization of skip* to syntax.c.
author Richard M. Stallman <rms@gnu.org>
date Tue, 15 Apr 1997 04:52:38 +0000
parents 74fcc6a43092
children 27a0ced43e7e
comparison
equal deleted inserted replaced
17462:a609ccd7eefb 17463:bb9ae80d22e2
247 if (s2 < 0) 247 if (s2 < 0)
248 { 248 {
249 s1 = ZV - BEGV; 249 s1 = ZV - BEGV;
250 s2 = 0; 250 s2 = 0;
251 } 251 }
252
253 re_match_object = Qnil;
252 254
253 i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2, 255 i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2,
254 PT - BEGV, &search_regs, 256 PT - BEGV, &search_regs,
255 ZV - BEGV); 257 ZV - BEGV);
256 if (i == -2) 258 if (i == -2)
323 bufp = compile_pattern (regexp, &search_regs, 325 bufp = compile_pattern (regexp, &search_regs,
324 (!NILP (current_buffer->case_fold_search) 326 (!NILP (current_buffer->case_fold_search)
325 ? DOWNCASE_TABLE : 0), 327 ? DOWNCASE_TABLE : 0),
326 posix); 328 posix);
327 immediate_quit = 1; 329 immediate_quit = 1;
330 re_match_object = string;
331
328 val = re_search (bufp, (char *) XSTRING (string)->data, 332 val = re_search (bufp, (char *) XSTRING (string)->data,
329 XSTRING (string)->size, s, XSTRING (string)->size - s, 333 XSTRING (string)->size, s, XSTRING (string)->size - s,
330 &search_regs); 334 &search_regs);
331 immediate_quit = 0; 335 immediate_quit = 0;
332 last_thing_searched = Qt; 336 last_thing_searched = Qt;
372 int val; 376 int val;
373 struct re_pattern_buffer *bufp; 377 struct re_pattern_buffer *bufp;
374 378
375 bufp = compile_pattern (regexp, 0, 0, 0); 379 bufp = compile_pattern (regexp, 0, 0, 0);
376 immediate_quit = 1; 380 immediate_quit = 1;
381 re_match_object = string;
382
377 val = re_search (bufp, (char *) XSTRING (string)->data, 383 val = re_search (bufp, (char *) XSTRING (string)->data,
378 XSTRING (string)->size, 0, XSTRING (string)->size, 384 XSTRING (string)->size, 0, XSTRING (string)->size,
379 0); 385 0);
380 immediate_quit = 0; 386 immediate_quit = 0;
381 return val; 387 return val;
667 673
668 if (shortage == 0) 674 if (shortage == 0)
669 pos--; 675 pos--;
670 676
671 return pos; 677 return pos;
672 }
673
674 Lisp_Object skip_chars ();
675
676 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
677 "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\
678 STRING is like the inside of a `[...]' in a regular expression\n\
679 except that `]' is never special and `\\' quotes `^', `-' or `\\'.\n\
680 Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\
681 With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\
682 Returns the distance traveled, either zero or positive.")
683 (string, lim)
684 Lisp_Object string, lim;
685 {
686 return skip_chars (1, 0, string, lim);
687 }
688
689 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
690 "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\
691 See `skip-chars-forward' for details.\n\
692 Returns the distance traveled, either zero or negative.")
693 (string, lim)
694 Lisp_Object string, lim;
695 {
696 return skip_chars (0, 0, string, lim);
697 }
698
699 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
700 "Move point forward across chars in specified syntax classes.\n\
701 SYNTAX is a string of syntax code characters.\n\
702 Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\
703 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
704 This function returns the distance traveled, either zero or positive.")
705 (syntax, lim)
706 Lisp_Object syntax, lim;
707 {
708 return skip_chars (1, 1, syntax, lim);
709 }
710
711 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
712 "Move point backward across chars in specified syntax classes.\n\
713 SYNTAX is a string of syntax code characters.\n\
714 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\
715 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
716 This function returns the distance traveled, either zero or negative.")
717 (syntax, lim)
718 Lisp_Object syntax, lim;
719 {
720 return skip_chars (0, 1, syntax, lim);
721 }
722
723 Lisp_Object
724 skip_chars (forwardp, syntaxp, string, lim)
725 int forwardp, syntaxp;
726 Lisp_Object string, lim;
727 {
728 register unsigned char *p, *pend;
729 register unsigned int c;
730 register int ch;
731 unsigned char fastmap[0400];
732 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
733 of which codes don't fit in FASTMAP. In that case, we set the
734 first byte of multibyte form (i.e. base leading-code) in FASTMAP
735 and set the actual ranges of characters in CHAR_RANGES. In the
736 form "X-Y" of STRING, both X and Y must belong to the same
737 character set because a range striding across character sets is
738 meaningless. */
739 int *char_ranges
740 = (int *) alloca (XSTRING (string)->size * (sizeof (int)) * 2);
741 int n_char_ranges = 0;
742 int negate = 0;
743 register int i;
744 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
745
746 CHECK_STRING (string, 0);
747
748 if (NILP (lim))
749 XSETINT (lim, forwardp ? ZV : BEGV);
750 else
751 CHECK_NUMBER_COERCE_MARKER (lim, 1);
752
753 /* In any case, don't allow scan outside bounds of buffer. */
754 /* jla turned this off, for no known reason.
755 bfox turned the ZV part on, and rms turned the
756 BEGV part back on. */
757 if (XINT (lim) > ZV)
758 XSETFASTINT (lim, ZV);
759 if (XINT (lim) < BEGV)
760 XSETFASTINT (lim, BEGV);
761
762 p = XSTRING (string)->data;
763 pend = p + XSTRING (string)->size;
764 bzero (fastmap, sizeof fastmap);
765
766 if (p != pend && *p == '^')
767 {
768 negate = 1; p++;
769 }
770
771 /* Find the characters specified and set their elements of fastmap.
772 If syntaxp, each character counts as itself.
773 Otherwise, handle backslashes and ranges specially. */
774
775 while (p != pend)
776 {
777 c = *p;
778 if (multibyte)
779 {
780 ch = STRING_CHAR (p, pend - p);
781 p += BYTES_BY_CHAR_HEAD (*p);
782 }
783 else
784 {
785 ch = c;
786 p++;
787 }
788 if (syntaxp)
789 fastmap[syntax_spec_code[c]] = 1;
790 else
791 {
792 if (c == '\\')
793 {
794 if (p == pend) break;
795 c = *p++;
796 }
797 if (p != pend && *p == '-')
798 {
799 unsigned int ch2;
800
801 p++;
802 if (p == pend) break;
803 if (SINGLE_BYTE_CHAR_P (ch))
804 while (c <= *p)
805 {
806 fastmap[c] = 1;
807 c++;
808 }
809 else
810 {
811 fastmap[c] = 1; /* C is the base leading-code. */
812 ch2 = STRING_CHAR (p, pend - p);
813 if (ch <= ch2)
814 char_ranges[n_char_ranges++] = ch,
815 char_ranges[n_char_ranges++] = ch2;
816 }
817 p += multibyte ? BYTES_BY_CHAR_HEAD (*p) : 1;
818 }
819 else
820 {
821 fastmap[c] = 1;
822 if (!SINGLE_BYTE_CHAR_P (ch))
823 char_ranges[n_char_ranges++] = ch,
824 char_ranges[n_char_ranges++] = ch;
825 }
826 }
827 }
828
829 /* If ^ was the first character, complement the fastmap. In
830 addition, as all multibyte characters have possibility of
831 matching, set all entries for base leading codes, which is
832 harmless even if SYNTAXP is 1. */
833
834 if (negate)
835 for (i = 0; i < sizeof fastmap; i++)
836 {
837 if (!multibyte || !BASE_LEADING_CODE_P (i))
838 fastmap[i] ^= 1;
839 else
840 fastmap[i] = 1;
841 }
842
843 {
844 int start_point = PT;
845 int pos = PT;
846
847 immediate_quit = 1;
848 if (syntaxp)
849 {
850 if (forwardp)
851 {
852 if (multibyte)
853 while (pos < XINT (lim)
854 && fastmap[(int) SYNTAX (FETCH_CHAR (pos))])
855 INC_POS (pos);
856 else
857 while (pos < XINT (lim)
858 && fastmap[(int) SYNTAX (FETCH_BYTE (pos))])
859 pos++;
860 }
861 else
862 {
863 if (multibyte)
864 while (pos > XINT (lim))
865 {
866 int savepos = pos;
867 DEC_POS (pos);
868 if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos))])
869 {
870 pos = savepos;
871 break;
872 }
873 }
874 else
875 while (pos > XINT (lim)
876 && fastmap[(int) SYNTAX (FETCH_BYTE (pos - 1))])
877 pos--;
878 }
879 }
880 else
881 {
882 if (forwardp)
883 {
884 if (multibyte)
885 while (pos < XINT (lim) && fastmap[(c = FETCH_BYTE (pos))])
886 {
887 if (!BASE_LEADING_CODE_P (c))
888 pos++;
889 else if (n_char_ranges)
890 {
891 /* We much check CHAR_RANGES for a multibyte
892 character. */
893 ch = FETCH_MULTIBYTE_CHAR (pos);
894 for (i = 0; i < n_char_ranges; i += 2)
895 if ((ch >= char_ranges[i] && ch <= char_ranges[i + 1]))
896 break;
897 if (!(negate ^ (i < n_char_ranges)))
898 break;
899
900 INC_POS (pos);
901 }
902 else
903 {
904 if (!negate) break;
905 INC_POS (pos);
906 }
907 }
908 else
909 while (pos < XINT (lim) && fastmap[FETCH_BYTE (pos)])
910 pos++;
911 }
912 else
913 {
914 if (multibyte)
915 while (pos > XINT (lim))
916 {
917 int savepos = pos;
918 DEC_POS (pos);
919 if (fastmap[(c = FETCH_BYTE (pos))])
920 {
921 if (!BASE_LEADING_CODE_P (c))
922 ;
923 else if (n_char_ranges)
924 {
925 /* We much check CHAR_RANGES for a multibyte
926 character. */
927 ch = FETCH_MULTIBYTE_CHAR (pos);
928 for (i = 0; i < n_char_ranges; i += 2)
929 if (ch >= char_ranges[i] && ch <= char_ranges[i + 1])
930 break;
931 if (!(negate ^ (i < n_char_ranges)))
932 {
933 pos = savepos;
934 break;
935 }
936 }
937 else
938 if (!negate)
939 {
940 pos = savepos;
941 break;
942 }
943 }
944 else
945 {
946 pos = savepos;
947 break;
948 }
949 }
950 else
951 while (pos > XINT (lim) && fastmap[FETCH_BYTE (pos - 1)])
952 pos--;
953 }
954 }
955 if (multibyte
956 /* INC_POS or DEC_POS might have moved POS over LIM. */
957 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
958 pos = XINT (lim);
959
960 SET_PT (pos);
961 immediate_quit = 0;
962
963 return make_number (PT - start_point);
964 }
965 } 678 }
966 679
967 /* Subroutines of Lisp buffer search functions. */ 680 /* Subroutines of Lisp buffer search functions. */
968 681
969 static Lisp_Object 682 static Lisp_Object
1143 if (s2 < 0) 856 if (s2 < 0)
1144 { 857 {
1145 s1 = ZV - BEGV; 858 s1 = ZV - BEGV;
1146 s2 = 0; 859 s2 = 0;
1147 } 860 }
861 re_match_object = Qnil;
862
1148 while (n < 0) 863 while (n < 0)
1149 { 864 {
1150 int val; 865 int val;
1151 val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2, 866 val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2,
1152 pos - BEGV, lim - pos, &search_regs, 867 pos - BEGV, lim - pos, &search_regs,
2271 1986
2272 defsubr (&Slooking_at); 1987 defsubr (&Slooking_at);
2273 defsubr (&Sposix_looking_at); 1988 defsubr (&Sposix_looking_at);
2274 defsubr (&Sstring_match); 1989 defsubr (&Sstring_match);
2275 defsubr (&Sposix_string_match); 1990 defsubr (&Sposix_string_match);
2276 defsubr (&Sskip_chars_forward);
2277 defsubr (&Sskip_chars_backward);
2278 defsubr (&Sskip_syntax_forward);
2279 defsubr (&Sskip_syntax_backward);
2280 defsubr (&Ssearch_forward); 1991 defsubr (&Ssearch_forward);
2281 defsubr (&Ssearch_backward); 1992 defsubr (&Ssearch_backward);
2282 defsubr (&Sword_search_forward); 1993 defsubr (&Sword_search_forward);
2283 defsubr (&Sword_search_backward); 1994 defsubr (&Sword_search_backward);
2284 defsubr (&Sre_search_forward); 1995 defsubr (&Sre_search_forward);