annotate lisp/replace.el @ 1292:f3437b56874d

[xemacs-hg @ 2003-02-13 09:57:04 by ben] profile updates profile.c: Major reworking. Keep track of new information -- total function timing (includes descendants), GC usage, total GC usage (includes descendants). New functions to be called appropriately from eval.c, alloc.c to keep track of this information. Keep track of when we're actually in a function vs. in its profile, for more accurate timing counts. Track profile overhead separately. Create new mechanism for specifying "internal sections" that are tracked just like regular Lisp functions and even appear in the backtrace if `backtrace-with-internal-sections' is non-nil (t by default for error-checking builds). Add some KKCC information for the straight (non-Elisp) hash table used by profile, which contains Lisp objects in its keys -- but not used yet. Remove old ad-hoc methods for tracking garbage collection, redisplay (which was incorrect anyway when Lisp was called within these sections). Don't record any tick info when blocking under MS Windows, since the timer there is in real time rather than in process time. Make `start-profiling', `stop-profiling' interactive. Be consistent wrt. recursive functions and functions currently on the stack when starting or stopping -- together these make implementing the `total' values extremely difficult. When we start profiling, we act as if we just entered all the functions currently on the stack. Likewise when exiting. Create vars in_profile for tracking time spent inside of profiling, and profiling_lock for setting exclusive access to the main hash table when reading from it or modifying it. (protects against getting screwed up by the signal handle going off at the same time. profile.h: New file. Create macros for declaring internal profiling sections. lisp.h: Move profile-related stuff to profile.h. alloc.c: Keep track of total consing, for profile. Tell profile when we are consing. Use new profile-section method for noting garbage-collection. alloc.c: Abort if we attempt to call the allocator reentrantly. backtrace.h, eval.c: Add info for use by profile in the backtrace frame and transfer PUSH_BACKTRACE/POP_BACKTRACE from eval.c, for use with profile. elhash.c: Author comment. eval.c, lisp.h: New Lisp var `backtrace-with-internal-sections'. Set to t when error-checking is on. eval.c: When unwinding, eval.c: Report to profile when we are about-to-call and just-called wrt. a function. alloc.c, eval.c: Allow for "fake" backtrace frames, for internal sections (used by profile and `backtrace-with-internal-sections'. event-Xt.c, event-gtk.c, event-msw.c, event-tty.c: Record when we are actually blocking on an event, for profile's sake. event-stream.c: Record internal profiling sections for getting, dispatching events. extents.c: Record internal profiling sections for map_extents. hash.c, hash.h: Add pregrow_hash_table_if_necessary(). (Used in profile code since the signal handler is the main grower but can't allow a realloc(). We make sure, at critical points, that the table is large enough.) lread.c: Create internal profiling sections for `load' (which may be triggered internally by autoload, etc.). redisplay.c: Remove old profile_redisplay_flag. Use new macros to declare internal profiling section for redisplay. text.c: Use new macros to declare internal profiling sections for char-byte conversion and internal-external conversion. SEMI-UNRELATED CHANGES: ----------------------- text.c: Update the long comments.
author ben
date Thu, 13 Feb 2003 09:57:08 +0000
parents 13daf40fb997
children 1b0339b048ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 ;;; replace.el --- search and replace commands for XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985-7, 1992, 1994, 1997 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 ;; Maintainer: XEmacs Development Team
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Keywords: dumped, matching
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 ;; under the terms of the GNU General Public License as published by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ;; any later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 ;; General Public License for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 ;; 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 ;;; Synched up with: FSF 19.34 [Partially].
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 ;; This file is dumped with XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 ;; This package supplies the string and regular-expression replace functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 ;; documented in the XEmacs Reference Manual.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 ;; All the gettext calls are for XEmacs I18N3 message catalog support.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 ;; (This is hopelessly broken and we should remove it. -sb)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 ;;; Code:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 (defvar case-replace t "\
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 *Non-nil means `query-replace' should preserve case in replacements.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 What this means is that `query-replace' will change the case of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 replacement text so that it matches the text that was replaced.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 If this variable is nil, the replacement text will be inserted
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 exactly as it was specified by the user, irrespective of the case
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 of the text that was replaced.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 Note that this flag has no effect if `case-fold-search' is nil,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 or if the replacement text has any uppercase letters in it.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 (defvar query-replace-history nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 (defvar query-replace-interactive nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 "Non-nil means `query-replace' uses the last search string.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 That becomes the \"string to replace\".")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 (defvar replace-search-function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 (lambda (str limit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 (search-forward str limit t))
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
59 "Function used by perform-replace to search forward for a string. It will be
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 called with two arguments: the string to search for and a limit bounding the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 search.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 (defvar replace-re-search-function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 (lambda (regexp limit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 (re-search-forward regexp limit t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 "Function used by perform-replace to search forward for a regular
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 expression. It will be called with two arguments: the regexp to search for and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 a limit bounding the search.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 (defun query-replace-read-args (string regexp-flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 (let (from to)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 (if query-replace-interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 (setq from (car (if regexp-flag regexp-search-ring search-ring)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 (setq from (read-from-minibuffer (format "%s: " (gettext string))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 'query-replace-history)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 (setq to (read-from-minibuffer (format "%s %s with: " (gettext string)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 from)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 'query-replace-history))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 (list from to current-prefix-arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 ;; As per suggestion from Per Abrahamsen, limit replacement to the region
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 ;; if the region is active.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 (defun query-replace (from-string to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 "Replace some occurrences of FROM-STRING with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 As each match is found, the user must type a character saying
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 what to do with it. For directions, type \\[help-command] at that time.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 string is used as FROM-STRING--you don't have to specify it with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 Preserves case in each replacement if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 are non-nil and FROM-STRING has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 \(Preserving case means that if the string matched is all caps, or capitalized,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 then its replacement is upcased or capitalized.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 To customize possible responses, change the \"bindings\" in `query-replace-map'."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 (interactive (query-replace-read-args "Query replace" nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 (perform-replace from-string to-string t nil delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 (defun query-replace-regexp (regexp to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 "Replace some things after point matching REGEXP with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 As each match is found, the user must type a character saying
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 what to do with it. For directions, type \\[help-command] at that time.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 regexp is used as REGEXP--you don't have to specify it with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 Preserves case in each replacement if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 are non-nil and REGEXP has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 and `\\=\\N' (where N is a digit) stands for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 whatever what matched the Nth `\\(...\\)' in REGEXP."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 (interactive (query-replace-read-args "Query replace regexp" t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 (perform-replace regexp to-string t t delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 ;;#### Not patently useful
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 (defun map-query-replace-regexp (regexp to-strings &optional arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 "Replace some matches for REGEXP with various strings, in rotation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 The second argument TO-STRINGS contains the replacement strings, separated
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 by spaces. This command works like `query-replace-regexp' except
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 that each successive replacement uses the next successive replacement string,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 wrapping around from the last such string to the first.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 Non-interactively, TO-STRINGS may be a list of replacement strings.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 A prefix argument N says to use each replacement string N times
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 before rotating to the next."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 (interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 (let (from to)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 (setq from (if query-replace-interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 (car regexp-search-ring)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 (read-from-minibuffer "Map query replace (regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 'query-replace-history)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 (setq to (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 (format "Query replace %s with (space-separated strings): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 from)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 'query-replace-history))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 (list from to current-prefix-arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 (let (replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 (if (listp to-strings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 (setq replacements to-strings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 (while (/= (length to-strings) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 (if (string-match " " to-strings)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 (setq replacements
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 (append replacements
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 (list (substring to-strings 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 (string-match " " to-strings))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 to-strings (substring to-strings
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 (1+ (string-match " " to-strings))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 (setq replacements (append replacements (list to-strings))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 to-strings ""))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 (perform-replace regexp replacements t t nil arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 (defun replace-string (from-string to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 "Replace occurrences of FROM-STRING with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 Preserve case in each match if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 are non-nil and FROM-STRING has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 \(Preserving case means that if the string matched is all caps, or capitalized,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 then its replacement is upcased or capitalized.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 string is used as FROM-STRING--you don't have to specify it with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 This function is usually the wrong thing to use in a Lisp program.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 What you probably want is a loop like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 (while (search-forward FROM-STRING nil t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 (replace-match TO-STRING nil t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 which will run faster and will not set the mark or print anything."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 (interactive (query-replace-read-args "Replace string" nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 (perform-replace from-string to-string nil nil delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 (defun replace-regexp (regexp to-string &optional delimited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 "Replace things after point matching REGEXP with TO-STRING.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 Preserve case in each match if `case-replace' and `case-fold-search'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 are non-nil and REGEXP has no uppercase letters.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 \(Preserving case means that if the string matched is all caps, or capitalized,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 then its replacement is upcased or capitalized.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 only matches surrounded by word boundaries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 and `\\=\\N' (where N is a digit) stands for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 whatever what matched the Nth `\\(...\\)' in REGEXP.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 If `query-replace-interactive' is non-nil, the last incremental search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 regexp is used as REGEXP--you don't have to specify it with the minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 This function is usually the wrong thing to use in a Lisp program.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 What you probably want is a loop like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 (while (re-search-forward REGEXP nil t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 (replace-match TO-STRING nil nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 which will run faster and will not set the mark or print anything."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 (interactive (query-replace-read-args "Replace regexp" t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 (perform-replace regexp to-string nil t delimited))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
215
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
216 ;; gse wonders: Is there a better place for this to go? Might other packages
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
217 ;; want to use it?
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 (defvar regexp-history nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 "History list for some commands that read regular expressions.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
221 (defun operate-on-non-matching-lines (regexp delete kill &optional beg end)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
222 "Internal function used by delete-non-matching-lines,
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
223 kill-non-matching-lines, and copy-matching-lines.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
224
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
225 REGEXP is a regular expression to *not* match when performing
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
226 operations.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
227
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
228 If DELETE is non-nil, the lines of text are deleted. It doesn't make
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
229 sense to set this to nil if KILL is nil -- nothing will happen.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
230
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
231 If KILL is non-nil, the lines of text are stored in the kill ring (as
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
232 one block of text).
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
233
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
234 BEG and END, if non-nil, specify the start and end locations to work
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
235 within. If these are nil, point and point-max are used.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
236
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
237 A match split across lines preserves all the lines it lies in.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
238 Applies to all lines after point.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
239
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
240 Returns the number of lines matched."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
241 (with-search-caps-disable-folding regexp t
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
242 (save-excursion
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
243 ;; Move to a beginning point if specified.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
244 (when beg (goto-char beg))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
245 ;; Always start on the beginning of a line.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
246 (or (bolp) (forward-line 1))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
247
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
248 (let ((matched-text nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
249 (curmatch-start (point))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
250 (limit (copy-marker (point-max))))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
251 ;; Limit search if limits were specified.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
252 (when end (setq limit (copy-marker end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
253
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
254 ;; Search. Stop if we are at end of buffer or outside the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
255 ;; limit.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
256 (while (not (or
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
257 (eobp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
258 (and limit (>= (point) limit))))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
259 ;; curmatch-start is first char not preserved by previous match.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
260 (if (not (re-search-forward regexp limit 'move))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
261 (let ((curmatch-end limit))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
262 (setq matched-text (concat matched-text (buffer-substring curmatch-start curmatch-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
263 (if delete (delete-region curmatch-start curmatch-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
264 (let ((curmatch-end (save-excursion (goto-char (match-beginning 0))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
265 (beginning-of-line)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
266 (point))))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
267 ;; Now curmatch-end is first char preserved by the new match.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
268 (if (< curmatch-start curmatch-end)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
269 (progn
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
270 (setq matched-text (concat matched-text (buffer-substring curmatch-start curmatch-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
271 (if delete (delete-region curmatch-start curmatch-end))))))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
272 (setq curmatch-start (save-excursion (forward-line 1)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
273 (point)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
274 ;; If the match was empty, avoid matching again at same place.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
275 (and (not (eobp)) (= (match-beginning 0) (match-end 0))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
276 (forward-char 1)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
277
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
278 ;; If any lines were matched and KILL is non-nil, insert the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
279 ;; matched lines into the kill ring.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
280 (if (and matched-text kill) (kill-new matched-text))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
281
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
282 ;; Return the number of matched lines.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
283 (with-temp-buffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
284 ;; Use concat to make a string even if matched-text is nil.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
285 (insert (concat matched-text))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
286 (count-lines (point-min) (point-max)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
287 ))))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
288
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 (define-function 'keep-lines 'delete-non-matching-lines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 (defun delete-non-matching-lines (regexp)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
291 "Delete lines that do not match REGEXP, from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
292 buffer (or within the region, if it is active)."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 (interactive (list (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 "Keep lines (containing match for regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 nil nil nil 'regexp-history)))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
296 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
297 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
298 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
299 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
300 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
301 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
302 (setq count (operate-on-non-matching-lines regexp t nil beg end))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
303 (message "%i lines deleted" count)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
304
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
305 (defun kill-non-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
306 "Delete the lines that do not match REGEXP, from point to the end of
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
307 the buffer (or within the region, if it is active). The deleted lines
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
308 are placed in the kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
309 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
310 "Kill non-matching lines (regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
311 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
312 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
313 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
314 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
315 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
316 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
317 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
318 (setq count (operate-on-non-matching-lines regexp t t beg end))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
319 (message "%i lines killed" count)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
320
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
321 (defun copy-non-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
322 "Find all lines that do not match REGEXP from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
323 buffer (or within the region, if it is active), and place them in the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
324 kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
325 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
326 "Copy non-matching lines (regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
327 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
328 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
329 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
330 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
331 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
332 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
333 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
334 (setq count (operate-on-non-matching-lines regexp nil t beg end))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
335 (message "%i lines copied" count)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
336
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
337 (defun operate-on-matching-lines (regexp delete kill &optional beg end)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
338 "Internal function used by delete-matching-lines, kill-matching-lines,
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
339 and copy-matching-lines.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
340
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
341 If DELETE is non-nil, the lines of text are deleted. It doesn't make
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
342 sense to set this to nil if KILL is nil -- nothing will happen.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
343
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
344 If KILL is non-nil, the lines of text are stored in the kill ring (as
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
345 one block of text).
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
346
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
347 BEG and END, if non-nil, specify the start and end locations to work
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
348 within. If these are nil, point and point-max are used.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
349
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
350 If a match is split across lines, all the lines it lies in are deleted.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
351 Applies to lines after point.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
352 Returns the number of lines matched."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
353 (with-search-caps-disable-folding regexp t
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 (save-excursion
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
355 (let ((matched-text nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
356 (curmatch-start nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
357 (curmatch-end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
358 (limit nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
359
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
360 ;; Limit search if limits were specified.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
361 (when beg (goto-char beg))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
362 (when end (setq limit (copy-marker end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
363
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
364 (while (and (not (eobp))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
365 (re-search-forward regexp limit t))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
366 (setq curmatch-start (save-excursion (goto-char (match-beginning 0))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
367 (beginning-of-line)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
368 (point)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
369 (setq curmatch-end (progn (forward-line 1) (point)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
370 (setq matched-text (concat matched-text (buffer-substring curmatch-start curmatch-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
371 (if delete (delete-region curmatch-start curmatch-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
372
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
373 (if (and matched-text kill) (kill-new matched-text))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
374
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
375 ;; Return the number of matched lines.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
376 (with-temp-buffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
377 ;; Use concat to make a string even if matched-text is nil.
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
378 (insert (concat matched-text))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
379 (count-lines (point-min) (point-max)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
380 ))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 (define-function 'flush-lines 'delete-matching-lines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 (defun delete-matching-lines (regexp)
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
384 "Delete the lines that match REGEXP, from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
385 buffer (or within the region, if it is active)."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 (interactive (list (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 "Flush lines (containing match for regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 nil nil nil 'regexp-history)))
1069
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
389 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
390 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
391 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
392 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
393 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
394 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
395 (setq count (operate-on-matching-lines regexp t nil beg end))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
396 (message "%i lines deleted" count)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
397
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
398 (defun kill-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
399 "Delete the lines that match REGEXP, from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
400 buffer (or within the region, if it is active). The deleted lines are
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
401 placed in the kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
402 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
403 "Kill lines (containing match for regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
404 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
405 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
406 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
407 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
408 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
409 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
410 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
411 (setq count (operate-on-matching-lines regexp t t beg end))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
412 (message "%i lines killed" count)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
413
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
414 (defun copy-matching-lines (regexp)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
415 "Find all lines that match REGEXP from point to the end of the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
416 buffer (or within the region, if it is active), and place them in the
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
417 kill ring as one block of text."
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
418 (interactive (list (read-from-minibuffer
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
419 "Copy lines (containing match for regexp): "
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
420 nil nil nil 'regexp-history)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
421 (let ((beg nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
422 (end nil)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
423 (count nil))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
424 (when (region-active-p)
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
425 (setq beg (region-beginning))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
426 (setq end (region-end)))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
427 (setq count (operate-on-matching-lines regexp nil t beg end))
13daf40fb997 [xemacs-hg @ 2002-10-24 14:59:22 by youngs]
youngs
parents: 456
diff changeset
428 (message "%i lines copied" count)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 (define-function 'how-many 'count-matches)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 (defun count-matches (regexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 "Print number of matches for REGEXP following point."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 (interactive (list (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 "How many matches for (regexp): "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 nil nil nil 'regexp-history)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 (with-interactive-search-caps-disable-folding regexp t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 (let ((count 0) opoint)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 (while (and (not (eobp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 (progn (setq opoint (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 (re-search-forward regexp nil t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 (if (= opoint (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 (forward-char 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 (setq count (1+ count))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 (message "%d occurrences" count)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 (defvar occur-mode-map ())
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 (if occur-mode-map
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 (setq occur-mode-map (make-sparse-keymap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 (set-keymap-name occur-mode-map 'occur-mode-map) ; XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 (define-key occur-mode-map 'button2 'occur-mode-mouse-goto) ; XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 (define-key occur-mode-map "\C-c\C-c" 'occur-mode-goto-occurrence)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 (define-key occur-mode-map "\C-m" 'occur-mode-goto-occurrence))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 (defvar occur-buffer nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 (defvar occur-nlines nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 (defvar occur-pos-list nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 (defun occur-mode ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 "Major mode for output from \\[occur].
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 \\<occur-mode-map>Move point to one of the items in this buffer, then use
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 \\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 \\{occur-mode-map}"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 (kill-all-local-variables)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 (use-local-map occur-mode-map)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 (setq major-mode 'occur-mode)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 (setq mode-name (gettext "Occur")) ; XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 (make-local-variable 'occur-buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 (make-local-variable 'occur-nlines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 (make-local-variable 'occur-pos-list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 (require 'mode-motion) ; XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 (setq mode-motion-hook 'mode-motion-highlight-line) ; XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 (run-hooks 'occur-mode-hook))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 ;; FSF Version of next function:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 ; (let (buffer pos)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 ; (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 ; (set-buffer (window-buffer (posn-window (event-end event))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 ; (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 ; (goto-char (posn-point (event-end event)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 ; (setq pos (occur-mode-find-occurrence))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 ; (setq buffer occur-buffer)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 ; (pop-to-buffer buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 ; (goto-char (marker-position pos))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 (defun occur-mode-mouse-goto (event)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 "Go to the occurrence highlighted by mouse.
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
492 This function should be bound to a mouse key in the `*Occur*' buffer."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 (interactive "e")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 (let ((window-save (selected-window))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 (frame-save (selected-frame)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 ;; preserve the window/frame setup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 (unwind-protect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 (mouse-set-point event)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 (occur-mode-goto-occurrence))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 (select-frame frame-save)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 (select-window window-save))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 ;; Called occur-mode-find-occurrence in FSF
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 (defun occur-mode-goto-occurrence ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 "Go to the occurrence the current line describes."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 (interactive)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 (if (or (null occur-buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 (null (buffer-name occur-buffer)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 (setq occur-buffer nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 occur-pos-list nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 (error "Buffer in which occurrences were found is deleted")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 (let* ((line-count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 (count-lines (point-min)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 (beginning-of-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 (point))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 (occur-number (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 (beginning-of-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 (/ (1- line-count)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 (cond ((< occur-nlines 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 (- 2 occur-nlines))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 ((> occur-nlines 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 (+ 2 (* 2 occur-nlines)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 (t 1)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 (pos (nth occur-number occur-pos-list))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 ;; removed t arg from Bob Weiner, 10/6/95
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 (window (get-buffer-window occur-buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 (occur-source-buffer occur-buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 (if (< line-count 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 (error "No occurrence on this line"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 (or pos
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 (error "No occurrence on this line"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 ;; XEmacs: don't raise window unless it isn't visible
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 ;; allow for the possibility that the occur buffer is on another frame
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 (or (and window
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 (window-live-p window)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 (frame-visible-p (window-frame window))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 (set-buffer occur-source-buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 (and (pop-to-buffer occur-source-buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 (setq window (get-buffer-window occur-source-buffer))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 (goto-char pos)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 (set-window-point window pos)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 (defvar list-matching-lines-default-context-lines 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 "*Default number of context lines to include around a `list-matching-lines'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 match. A negative number means to include that many lines before the match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 A positive number means to include that many lines both before and after.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 ;; XEmacs addition
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 ;;; Damn you Jamie, this is utter trash.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 (defvar list-matching-lines-whole-buffer t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 "If t, occur operates on whole buffer, otherwise occur starts from point.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 default is t.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 (define-function 'occur 'list-matching-lines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 (defun list-matching-lines (regexp &optional nlines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 "Show all lines in the current buffer containing a match for REGEXP.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 If a match spreads across multiple lines, all those lines are shown.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563
448
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents: 444
diff changeset
564 If variable `list-matching-lines-whole-buffer' is non-nil, the entire
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents: 444
diff changeset
565 buffer is searched, otherwise search begins at point.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 Each line is displayed with NLINES lines before and after, or -NLINES
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 before if NLINES is negative.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 NLINES defaults to `list-matching-lines-default-context-lines'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 Interactively it is the prefix arg.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 The lines are shown in a buffer named `*Occur*'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 It serves as a menu to find any of the occurrences in this buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 \\[describe-mode] in that buffer will explain how."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 (interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 ;; XEmacs change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 (list (let* ((default (or (symbol-near-point)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 (and regexp-history
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 (car regexp-history))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 (minibuffer-history-minimum-string-length 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 (input
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 (if default
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 ;; rewritten for I18N3 snarfing
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 (format "List lines matching regexp (default `%s'): "
456
e7ef97881643 Import from CVS: tag r21-2-43
cvs
parents: 448
diff changeset
586 default) nil nil nil 'regexp-history nil
e7ef97881643 Import from CVS: tag r21-2-43
cvs
parents: 448
diff changeset
587 default)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 (read-from-minibuffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 "List lines matching regexp: "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 nil nil nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 'regexp-history))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 (if (and (equal input "") default)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 (setq input default)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 (setcar regexp-history default)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 ;; clear extra entries
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 (setcdr regexp-history (delete (car regexp-history)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 (cdr regexp-history)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 input)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 current-prefix-arg))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 (if (equal regexp "")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 (error "Must pass non-empty regexp to `list-matching-lines'"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 (setq nlines (if nlines (prefix-numeric-value nlines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 list-matching-lines-default-context-lines))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 (let ((first t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 (dir default-directory)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 (buffer (current-buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 (linenum 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 (prevpos (point-min))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 ;; The rest of this function is very different from FSF.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 ;; Presumably that's due to Jamie's misfeature
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 (final-context-start (make-marker)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 (if (not list-matching-lines-whole-buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 (beginning-of-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 (setq linenum (1+ (count-lines (point-min) (point))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 (setq prevpos (point))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 (with-output-to-temp-buffer "*Occur*"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 (set-buffer standard-output)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 (setq default-directory dir)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 ;; We will insert the number of lines, and "lines", later.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 ;; #### Needs fixing for I18N3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 (let ((print-escape-newlines t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 (insert (format " matching %s in buffer %s.\n"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
626 regexp (buffer-name buffer))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627 (occur-mode)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 (setq occur-buffer buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 (setq occur-nlines nlines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 (setq occur-pos-list ()))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 (if (eq buffer standard-output)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 (goto-char (point-max)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 (with-interactive-search-caps-disable-folding regexp t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 (if list-matching-lines-whole-buffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 (beginning-of-buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 (message "Searching for %s ..." regexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 ;; Find next match, but give up if prev match was at end of buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 (while (and (not (= prevpos (point-max)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 (re-search-forward regexp nil t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 (goto-char (match-beginning 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 (beginning-of-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643 (save-match-data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 (setq linenum (+ linenum (count-lines prevpos (point)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 (setq prevpos (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 (goto-char (match-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647 (let* ((start (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 (goto-char (match-beginning 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649 (forward-line (if (< nlines 0) nlines (- nlines)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 (point)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 (end (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 (goto-char (match-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 (if (> nlines 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654 (forward-line (1+ nlines))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 (forward-line 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 (point)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 (tag (format "%5d" linenum))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 (empty (make-string (length tag) ?\ ))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 tem)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661 (setq tem (make-marker))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 (set-marker tem (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 (set-buffer standard-output)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664 (setq occur-pos-list (cons tem occur-pos-list))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 (or first (zerop nlines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 (insert "--------\n"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 (setq first nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 (insert-buffer-substring buffer start end)
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
669 (set-marker final-context-start
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 (- (point) (- end (match-end 0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 (backward-char (- end start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672 (setq tem (if (< nlines 0) (- nlines) nlines))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 (while (> tem 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 (insert empty ?:)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 (forward-line 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 (setq tem (1- tem)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 (let ((this-linenum linenum))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 (while (< (point) final-context-start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 (if (null tag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680 (setq tag (format "%5d" this-linenum)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 (insert tag ?:)
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
682 ;; FSFmacs --
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 ;; we handle this using mode-motion-highlight-line, above.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684 ;; (put-text-property (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 ;; (beginning-of-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 ;; (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 ;; (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 ;; (end-of-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689 ;; (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 ;; 'mouse-face 'highlight)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691 (forward-line 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 (setq tag nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 (setq this-linenum (1+ this-linenum)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 (while (<= (point) final-context-start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 (insert empty ?:)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 (forward-line 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697 (setq this-linenum (1+ this-linenum))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 (while (< tem nlines)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 (insert empty ?:)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700 (forward-line 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 (setq tem (1+ tem)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 (goto-char (point-max)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
703 (forward-line 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704 (set-buffer standard-output)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705 ;; Put positions in increasing order to go with buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706 (setq occur-pos-list (nreverse occur-pos-list))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
707 (goto-char (point-min))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
708 (if (= (length occur-pos-list) 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
709 (insert "1 line")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 (insert (format "%d lines" (length occur-pos-list))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711 (if (interactive-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712 (message "%d matching lines." (length occur-pos-list))))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714 ;; It would be nice to use \\[...], but there is no reasonable way
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 ;; to make that display both SPC and Y.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
716 (defconst query-replace-help
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
717 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718 RET or `q' to exit, Period to replace one match and exit,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 Comma to replace but not move point immediately,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721 C-w to delete match and recursive edit,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722 C-l to clear the frame, redisplay, and offer same replacement again,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 ! to replace all remaining matches with no more questions,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 ^ to move point back to previous match."
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
725
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 "Help message while in query-replace")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728 (defvar query-replace-map nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 "Keymap that defines the responses to questions in `query-replace'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 The \"bindings\" in this map are not commands; they are answers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 The valid answers include `act', `skip', `act-and-show',
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 `automatic', `backup', `exit-prefix', and `help'.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735 ;; Why does it seem that ever file has a different method of doing this?
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 (if query-replace-map
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 (let ((map (make-sparse-keymap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 (set-keymap-name map 'query-replace-map)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 (define-key map " " 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 (define-key map "\d" 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742 (define-key map [delete] 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 (define-key map [backspace] 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 (define-key map "y" 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 (define-key map "n" 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 (define-key map "Y" 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747 (define-key map "N" 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
748 (define-key map "," 'act-and-show)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 (define-key map [escape] 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 (define-key map "q" 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751 (define-key map [return] 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 (define-key map "." 'act-and-exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 (define-key map "\C-r" 'edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 (define-key map "\C-w" 'delete-and-edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755 (define-key map "\C-l" 'recenter)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756 (define-key map "!" 'automatic)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 (define-key map "^" 'backup)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 (define-key map [(control h)] 'help) ;; XEmacs change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 (define-key map [f1] 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 (define-key map [help] 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 (define-key map "?" 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762 (define-key map "\C-g" 'quit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 (define-key map "\C-]" 'quit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 ;FSFmacs (define-key map "\e" 'exit-prefix)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765 (define-key map [escape] 'exit-prefix)
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
766
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 (setq query-replace-map map)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 ;; isearch-mode is dumped, so don't autoload.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 ;(autoload 'isearch-highlight "isearch")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 ;; XEmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 (defun perform-replace-next-event (event)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
774 (if search-highlight
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 (let ((aborted t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 (unwind-protect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 (if (match-beginning 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 (isearch-highlight (match-beginning 0) (match-end 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 (next-command-event event)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 (setq aborted nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782 (isearch-dehighlight aborted)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 (next-command-event event)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785 (defun perform-replace (from-string replacements
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 query-flag regexp-flag delimited-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787 &optional repeat-count map)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788 "Subroutine of `query-replace'. Its complexity handles interactive queries.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 Don't use this in your own program unless you want to query and set the mark
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790 just as `query-replace' does. Instead, write a simple loop like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 (while (re-search-forward \"foo[ \t]+bar\" nil t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 (replace-match \"foobar\" nil nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 which will run faster and probably do exactly what you want.
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
794 When searching for a match, this function uses
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
795 `replace-search-function' and `replace-re-search-function'."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
796 (or map (setq map query-replace-map))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 (let* ((event (make-event))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798 (nocasify (not (and case-fold-search case-replace
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
799 (string-equal from-string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
800 (downcase from-string)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801 (literal (not regexp-flag))
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
802 (search-function (if regexp-flag
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
803 replace-re-search-function
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804 replace-search-function))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
805 (search-string from-string)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 (real-match-data nil) ; the match data for the current match
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 (next-replacement nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
808 (replacement-index 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
809 (keep-going t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 (stack nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811 (next-rotate-count 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
812 (replace-count 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813 (lastrepl nil) ;Position after last match considered.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 ;; If non-nil, it is marker saying where in the buffer to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815 ;; stop.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816 (limit nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817 (match-again t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 ;; XEmacs addition
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
819 (qr-case-fold-search
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820 (if (and case-fold-search search-caps-disable-folding)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821 (no-upper-case-p search-string regexp-flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 case-fold-search))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823 (message
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
824 (if query-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 (substitute-command-keys
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826 "Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827 ;; If the region is active, operate on region.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828 (when (region-active-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829 ;; Original Per Abrahamsen's code simply narrowed the region,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 ;; thus providing a visual indication of the search boundary.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831 ;; Stallman, on the other hand, handles it like this.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 (setq limit (copy-marker (region-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 (goto-char (region-beginning))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
834 (zmacs-deactivate-region))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 (if (stringp replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
836 (setq next-replacement replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837 (or repeat-count (setq repeat-count 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 (if delimited-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839 (setq search-function replace-re-search-function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 search-string (concat "\\b"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841 (if regexp-flag from-string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 (regexp-quote from-string))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 "\\b")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
844 (push-mark)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 (undo-boundary)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846 (unwind-protect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847 ;; Loop finding occurrences that perhaps should be replaced.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
848 (while (and keep-going
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
849 (not (eobp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 (or (null limit) (< (point) limit))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851 (let ((case-fold-search qr-case-fold-search))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
852 (funcall search-function search-string limit))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 ;; If the search string matches immediately after
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854 ;; the previous match, but it did not match there
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855 ;; before the replacement was done, ignore the match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
856 (if (or (eq lastrepl (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
857 (and regexp-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
858 (eq lastrepl (match-beginning 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859 (not match-again)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
860 (if (or (eobp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
861 (and limit (>= (point) limit)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
862 nil
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
863 ;; Don't replace the null string
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
864 ;; right after end of previous replacement.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
865 (forward-char 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
866 (let ((case-fold-search qr-case-fold-search))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
867 (funcall search-function search-string limit)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
868 t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
869
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
870 ;; Save the data associated with the real match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
871 (setq real-match-data (match-data))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
872
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
873 ;; Before we make the replacement, decide whether the search string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
874 ;; can match again just after this match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
875 (if regexp-flag
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
876 (progn
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
877 (setq match-again (looking-at search-string))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
878 ;; XEmacs addition
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
879 (store-match-data real-match-data)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
880 ;; If time for a change, advance to next replacement string.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
881 (if (and (listp replacements)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
882 (= next-rotate-count replace-count))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
883 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
884 (setq next-rotate-count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
885 (+ next-rotate-count repeat-count))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
886 (setq next-replacement (nth replacement-index replacements))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
887 (setq replacement-index (% (1+ replacement-index) (length replacements)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
888 (if (not query-flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
889 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890 (store-match-data real-match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891 (replace-match next-replacement nocasify literal)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
892 (setq replace-count (1+ replace-count)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 (undo-boundary)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
894 (let ((help-form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
895 '(concat (format "Query replacing %s%s with %s.\n\n"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
896 (if regexp-flag (gettext "regexp ") "")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897 from-string next-replacement)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 (substitute-command-keys query-replace-help)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 done replaced def)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900 ;; Loop reading commands until one of them sets done,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 ;; which means it has finished handling this occurrence.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
902 (while (not done)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
903 ;; Don't fill up the message log
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
904 ;; with a bunch of identical messages.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
905 ;; XEmacs change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
906 (display-message 'prompt
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
907 (format message from-string next-replacement))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
908 (perform-replace-next-event event)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
909 (setq def (lookup-key map (vector event)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
910 ;; Restore the match data while we process the command.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
911 (store-match-data real-match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
912 (cond ((eq def 'help)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
913 (with-output-to-temp-buffer (gettext "*Help*")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
914 (princ (concat
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
915 (format "Query replacing %s%s with %s.\n\n"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
916 (if regexp-flag "regexp " "")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
917 from-string next-replacement)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
918 (substitute-command-keys
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
919 query-replace-help)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
920 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
921 (set-buffer standard-output)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
922 (help-mode))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
923 ((eq def 'exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
924 (setq keep-going nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
925 (setq done t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
926 ((eq def 'backup)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
927 (if stack
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
928 (let ((elt (car stack)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
929 (goto-char (car elt))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
930 (setq replaced (eq t (cdr elt)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
931 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
932 (store-match-data (cdr elt)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
933 (setq stack (cdr stack)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
934 (message "No previous match")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
935 (ding 'no-terminate)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
936 (sit-for 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
937 ((eq def 'act)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
938 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
939 (replace-match next-replacement nocasify literal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
940 (setq done t replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
941 ((eq def 'act-and-exit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
942 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
943 (replace-match next-replacement nocasify literal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
944 (setq keep-going nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
945 (setq done t replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
946 ((eq def 'act-and-show)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
947 (if (not replaced)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
948 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
949 (replace-match next-replacement nocasify literal)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
950 (store-match-data nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
951 (setq replaced t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
952 ((eq def 'automatic)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
953 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
954 (replace-match next-replacement nocasify literal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
955 (setq done t query-flag nil replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
956 ((eq def 'skip)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
957 (setq done t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
958 ((eq def 'recenter)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
959 (recenter nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
960 ((eq def 'edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
961 (store-match-data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
962 (prog1 (match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
963 (save-excursion (recursive-edit))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
964 ;; Before we make the replacement,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
965 ;; decide whether the search string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
966 ;; can match again just after this match.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
967 (if regexp-flag
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
968 (setq match-again (looking-at search-string))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
969 ((eq def 'delete-and-edit)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
970 (delete-region (match-beginning 0) (match-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
971 (store-match-data (prog1 (match-data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
972 (save-excursion (recursive-edit))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
973 (setq replaced t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
974 ;; Note: we do not need to treat `exit-prefix'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
975 ;; specially here, since we reread
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
976 ;; any unrecognized character.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
977 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
978 (setq this-command 'mode-exited)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
979 (setq keep-going nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
980 (setq unread-command-events
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
981 (cons event unread-command-events))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
982 (setq done t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
983 ;; Record previous position for ^ when we move on.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
984 ;; Change markers to numbers in the match data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
985 ;; since lots of markers slow down editing.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
986 (setq stack
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
987 (cons (cons (point)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
988 (or replaced
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
989 (match-data t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
990 stack))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
991 (if replaced (setq replace-count (1+ replace-count)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
992 (setq lastrepl (point)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
993 ;; Useless in XEmacs. We handle (de)highlighting through
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
994 ;; perform-replace-next-event.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
995 ;(replace-dehighlight)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
996 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
997 (or unread-command-events
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
998 (message "Replaced %d occurrence%s"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
999 replace-count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1000 (if (= replace-count 1) "" "s")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1001 (and keep-going stack)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1002
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1003 ;; FSFmacs code: someone should port it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1004
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1005 ;(defvar query-replace-highlight nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1006 ; "*Non-nil means to highlight words during query replacement.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1007
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1008 ;(defvar replace-overlay nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1009
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1010 ;(defun replace-dehighlight ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1011 ; (and replace-overlay
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1012 ; (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1013 ; (delete-overlay replace-overlay)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1014 ; (setq replace-overlay nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1015
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1016 ;(defun replace-highlight (start end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1017 ; (and query-replace-highlight
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1018 ; (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1019 ; (or replace-overlay
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1020 ; (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1021 ; (setq replace-overlay (make-overlay start end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1022 ; (overlay-put replace-overlay 'face
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1023 ; (if (internal-find-face 'query-replace)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1024 ; 'query-replace 'region))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1025 ; (move-overlay replace-overlay start end (current-buffer)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1026
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1027 (defun match-string (num &optional string)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1028 "Return string of text matched by last search.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1029 NUM specifies which parenthesized expression in the last regexp.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1030 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1031 Zero means the entire text matched by the whole regexp or whole string.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1032 STRING should be given if the last search was by `string-match' on STRING."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1033 (if (match-beginning num)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1034 (if string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1035 (substring string (match-beginning num) (match-end num))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1036 (buffer-substring (match-beginning num) (match-end num)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1037
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1038 (defmacro save-match-data (&rest body)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1039 "Execute BODY forms, restoring the global value of the match data."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1040 (let ((original (make-symbol "match-data")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1041 (list 'let (list (list original '(match-data)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1042 (list 'unwind-protect
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1043 (cons 'progn body)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1044 (list 'store-match-data original)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1045
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1046 ;;; replace.el ends here