comparison lisp/replace.el @ 864:fe5f6b7c9727

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Fri, 24 Jul 1992 08:17:31 +0000
parents 46630543d659
children ae5c412a32ec
comparison
equal deleted inserted replaced
863:427299469901 864:fe5f6b7c9727
21 ;;; Code: 21 ;;; Code:
22 22
23 (defconst case-replace t "\ 23 (defconst case-replace t "\
24 *Non-nil means query-replace should preserve case in replacements.") 24 *Non-nil means query-replace should preserve case in replacements.")
25 25
26 (defvar query-replace-history nil)
27
28 (defun query-replace-read-args (string)
29 (let (from to)
30 (setq from (read-from-minibuffer (format "%s: " string)
31 nil nil nil
32 'query-replace-history))
33 (setq to (read-from-minibuffer (format "%s %s with: " string from)
34 nil nil nil
35 'query-replace-history))
36 (list from to current-prefix-arg)))
37
26 (defun query-replace (from-string to-string &optional arg) 38 (defun query-replace (from-string to-string &optional arg)
27 "Replace some occurrences of FROM-STRING with TO-STRING. 39 "Replace some occurrences of FROM-STRING with TO-STRING.
28 As each match is found, the user must type a character saying 40 As each match is found, the user must type a character saying
29 what to do with it. For directions, type \\[help-command] at that time. 41 what to do with it. For directions, type \\[help-command] at that time.
30 42
31 Preserves case in each replacement if case-replace and case-fold-search 43 Preserves case in each replacement if case-replace and case-fold-search
32 are non-nil and FROM-STRING has no uppercase letters. 44 are non-nil and FROM-STRING has no uppercase letters.
33 Third arg DELIMITED (prefix arg if interactive) non-nil means replace 45 Third arg DELIMITED (prefix arg if interactive) non-nil means replace
34 only matches surrounded by word boundaries." 46 only matches surrounded by word boundaries."
35 (interactive "sQuery replace: \nsQuery replace %s with: \nP") 47 (interactive (query-replace-read-args "Query replace"))
36 (perform-replace from-string to-string t nil arg) 48 (perform-replace from-string to-string t nil arg)
37 (message "Done")) 49 (message "Done"))
38 (define-key esc-map "%" 'query-replace) 50 (define-key esc-map "%" 'query-replace)
39 51
40 (defun query-replace-regexp (regexp to-string &optional arg) 52 (defun query-replace-regexp (regexp to-string &optional arg)
46 are non-nil and REGEXP has no uppercase letters. 58 are non-nil and REGEXP has no uppercase letters.
47 Third arg DELIMITED (prefix arg if interactive) non-nil means replace 59 Third arg DELIMITED (prefix arg if interactive) non-nil means replace
48 only matches surrounded by word boundaries. 60 only matches surrounded by word boundaries.
49 In TO-STRING, \\& means insert what matched REGEXP, 61 In TO-STRING, \\& means insert what matched REGEXP,
50 and \\=\\<n> means insert what matched <n>th \\(...\\) in REGEXP." 62 and \\=\\<n> means insert what matched <n>th \\(...\\) in REGEXP."
51 (interactive "sQuery replace regexp: \nsQuery replace regexp %s with: \nP") 63 (interactive (query-replace-read-args "Query replace regexp"))
52 (perform-replace regexp to-string t t arg) 64 (perform-replace regexp to-string t t arg)
53 (message "Done")) 65 (message "Done"))
54 66
55 (defun map-query-replace-regexp (regexp to-strings &optional arg) 67 (defun map-query-replace-regexp (regexp to-strings &optional arg)
56 "Replace some matches for REGEXP with various strings, in rotation. 68 "Replace some matches for REGEXP with various strings, in rotation.
61 73
62 Non-interactively, TO-STRINGS may be a list of replacement strings. 74 Non-interactively, TO-STRINGS may be a list of replacement strings.
63 75
64 A prefix argument N says to use each replacement string N times 76 A prefix argument N says to use each replacement string N times
65 before rotating to the next." 77 before rotating to the next."
66 (interactive "sMap query replace (regexp): \nsQuery replace %s with (space-separated strings): \nP") 78 (interactive
79 (let (from to)
80 (setq from (read-from-minibuffer "Map query replace (regexp): "
81 nil nil nil
82 'query-replace-history))
83 (setq to (read-from-minibuffer
84 (format "Query replace %s with (space-separated strings): "
85 from)
86 nil nil nil
87 'query-replace-history))
88 (list from to current-prefix-arg)))
67 (let (replacements) 89 (let (replacements)
68 (if (listp to-strings) 90 (if (listp to-strings)
69 (setq replacements to-strings) 91 (setq replacements to-strings)
70 (while (/= (length to-strings) 0) 92 (while (/= (length to-strings) 0)
71 (if (string-match " " to-strings) 93 (if (string-match " " to-strings)
90 This function is usually the wrong thing to use in a Lisp program. 112 This function is usually the wrong thing to use in a Lisp program.
91 What you probably want is a loop like this: 113 What you probably want is a loop like this:
92 (while (search-forward OLD-STRING nil t) 114 (while (search-forward OLD-STRING nil t)
93 (replace-match REPLACEMENT nil t)) 115 (replace-match REPLACEMENT nil t))
94 which will run faster and will not set the mark or print anything." 116 which will run faster and will not set the mark or print anything."
95 (interactive "sReplace string: \nsReplace string %s with: \nP") 117 (interactive (query-replace-read-args "Replace string"))
96 (perform-replace from-string to-string nil nil delimited) 118 (perform-replace from-string to-string nil nil delimited)
97 (message "Done")) 119 (message "Done"))
98 120
99 (defun replace-regexp (regexp to-string &optional delimited) 121 (defun replace-regexp (regexp to-string &optional delimited)
100 "Replace things after point matching REGEXP with TO-STRING. 122 "Replace things after point matching REGEXP with TO-STRING.
108 This function is usually the wrong thing to use in a Lisp program. 130 This function is usually the wrong thing to use in a Lisp program.
109 What you probably want is a loop like this: 131 What you probably want is a loop like this:
110 (while (re-search-forward REGEXP nil t) 132 (while (re-search-forward REGEXP nil t)
111 (replace-match REPLACEMENT nil nil)) 133 (replace-match REPLACEMENT nil nil))
112 which will run faster and will not set the mark or print anything." 134 which will run faster and will not set the mark or print anything."
113 (interactive "sReplace regexp: \nsReplace regexp %s with: \nP") 135 (interactive (query-replace-read-args "Replace regexp"))
114 (perform-replace regexp to-string nil t delimited) 136 (perform-replace regexp to-string nil t delimited)
115 (message "Done")) 137 (message "Done"))
116 138
117 (fset 'delete-non-matching-lines 'keep-lines) 139 (fset 'delete-non-matching-lines 'keep-lines)
118 (defun keep-lines (regexp) 140 (defun keep-lines (regexp)