comparison lisp/subr.el @ 4199:3660d327399f

[xemacs-hg @ 2007-10-01 08:07:39 by stephent] Implement subexpression replacement in replace-match. <87ejgf6yy9.fsf@uwakimon.sk.tsukuba.ac.jp>
author stephent
date Mon, 01 Oct 2007 08:07:57 +0000
parents b4f4e0cc90f1
children c5a2b80bc4fa
comparison
equal deleted inserted replaced
4198:fb83e69ce80a 4199:3660d327399f
789 fixedcase literal subexp start) 789 fixedcase literal subexp start)
790 "Replace all matches for REGEXP with REP in STRING. 790 "Replace all matches for REGEXP with REP in STRING.
791 791
792 Return a new string containing the replacements. 792 Return a new string containing the replacements.
793 793
794 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the 794 Optional arguments FIXEDCASE and LITERAL are like the arguments with
795 arguments with the same names of function `replace-match'. If START 795 the same names of function `replace-match'. If START is non-nil,
796 is non-nil, start replacements at that index in STRING. 796 start replacements at that index in STRING.
797
798 For compatibility with old XEmacs code and with recent GNU Emacs, the
799 interpretation of SUBEXP is somewhat complicated. If SUBEXP is a
800 buffer, it is interpreted as the buffer which provides syntax tables
801 and case tables for the match and replacement. If it is not a buffer,
802 the current buffer is used. If SUBEXP is an integer, it is the index
803 of the subexpression of REGEXP which is to be replaced.
797 804
798 REP is either a string used as the NEWTEXT arg of `replace-match' or a 805 REP is either a string used as the NEWTEXT arg of `replace-match' or a
799 function. If it is a function it is applied to each match to generate 806 function. If it is a function it is applied to each match to generate
800 the replacement passed to `replace-match'; the match-data at this 807 the replacement passed to `replace-match'; the match-data at this
801 point are such that match 0 is the function's argument. 808 point are such that `(match-string SUBEXP STRING)' is the function's
809 argument if SUBEXP is an integer \(otherwise the whole match is passed
810 and replaced).
802 811
803 To replace only the first match (if any), make REGEXP match up to \\' 812 To replace only the first match (if any), make REGEXP match up to \\'
804 and replace a sub-expression, e.g. 813 and replace a sub-expression, e.g.
805 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1) 814 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
806 => \" bar foo\" 815 => \" bar foo\"
807 " 816
817 Signals `invalid-argument' if SUBEXP is not an integer, buffer, or nil;
818 or is an integer, but the indicated subexpression was not matched.
819 Signals `invalid-argument' if STRING is nil but the last text matched was a string,
820 or if STRING is a string but the last text matched was a buffer."
808 821
809 ;; To avoid excessive consing from multiple matches in long strings, 822 ;; To avoid excessive consing from multiple matches in long strings,
810 ;; don't just call `replace-match' continually. Walk down the 823 ;; don't just call `replace-match' continually. Walk down the
811 ;; string looking for matches of REGEXP and building up a (reversed) 824 ;; string looking for matches of REGEXP and building up a (reversed)
812 ;; list MATCHES. This comprises segments of STRING which weren't 825 ;; list MATCHES. This comprises segments of STRING which weren't
815 ;; operate in a temporary buffer; we can't tell from the function's 828 ;; operate in a temporary buffer; we can't tell from the function's
816 ;; args whether to choose the buffer-based implementation, though it 829 ;; args whether to choose the buffer-based implementation, though it
817 ;; might be reasonable to do so for long enough STRING.] 830 ;; might be reasonable to do so for long enough STRING.]
818 (let ((l (length string)) 831 (let ((l (length string))
819 (start (or start 0)) 832 (start (or start 0))
833 (expndx (if (integerp subexp) subexp 0))
820 matches str mb me) 834 matches str mb me)
821 (save-match-data 835 (save-match-data
822 (while (and (< start l) (string-match regexp string start)) 836 (while (and (< start l) (string-match regexp string start))
823 (setq mb (match-beginning 0) 837 (setq mb (match-beginning 0)
824 me (match-end 0)) 838 me (match-end 0))
831 ;; match data directly in Lisp. 845 ;; match data directly in Lisp.
832 (string-match regexp (setq str (substring string mb me))) 846 (string-match regexp (setq str (substring string mb me)))
833 (setq matches 847 (setq matches
834 (cons (replace-match (if (stringp rep) 848 (cons (replace-match (if (stringp rep)
835 rep 849 rep
836 (funcall rep (match-string 0 str))) 850 (funcall rep (match-string expndx str)))
851 ;; no, this subexp shouldn't be expndx
837 fixedcase literal str subexp) 852 fixedcase literal str subexp)
838 (cons (substring string start mb) ; unmatched prefix 853 (cons (substring string start mb) ; unmatched prefix
839 matches))) 854 matches)))
840 (setq start me)) 855 (setq start me))
841 ;; Reconstruct a string from the pieces. 856 ;; Reconstruct a string from the pieces.