annotate lisp/packages/igrep.el @ 24:4103f0995bd7 r19-15b95

Import from CVS: tag r19-15b95
author cvs
date Mon, 13 Aug 2007 08:51:03 +0200
parents
children 34a5b81f86ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
1 ;;;; igrep.el --- An improved interface to `grep`.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
2
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
3 ;;; Description:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
4 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
5 ;;; Define the \[igrep] command, which is like \[grep] except that it
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
6 ;;; takes three required arguments (PROGRAM, EXPRESSION, and FILES) and
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
7 ;;; an optional argument (OPTIONS) instead of just one argument (COMMAND).
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
8 ;;; Also define the analogous \[egrep] and \[fgrep] commands for convenience.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
9 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
10 ;;; Define the \[igrep-find] command, which is like \[igrep] except that
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
11 ;;; it uses `find` to recursively `grep` a directory. Also define the
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
12 ;;; analogous \[egrep-find] and \[fgrep-find] commands for convenience.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
13 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
14 ;;; \[igrep] and \[igrep-find] (and their analogues) provide defaults
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
15 ;;; for the EXPRESSION and FILES arguments when called interactively,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
16 ;;; and there are global variables that control the syntax of the `grep`
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
17 ;;; and `find` shell commands that are executed. A user option controls
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
18 ;;; whether the corresponding GNU (gzip) "zPROGRAM" script is used, to
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
19 ;;; `grep` compressed files.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
20 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
21 ;;; \[agrep] and \[agrep-find] are also defined as convenient interfaces
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
22 ;;; to the approximate `grep` utility, which is distributed with the
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
23 ;;; `glimpse' indexing and query tool (available from
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
24 ;;; <URL:http://glimpse.cs.arizona.edu:1994/>).
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
25 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
26 ;;; \[grep] itself is advised to provide the \[igrep] interface when
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
27 ;;; called interactively (when called programmatically, it still uses
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
28 ;;; the original argument list). \[grep-find] is defined as an alias
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
29 ;;; for \[igrep-find].
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
30 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
31 ;;; When run interactively from Dired mode, the various \[igrep]
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
32 ;;; commands provide defaults for the EXPRESSION and FILES arguments
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
33 ;;; that are based on the visited directory (including any inserted
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
34 ;;; subdirectories) and the current file. In addition, the
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
35 ;;; \[dired-do-igrep] and \[dired-do-igrep-find] commands are defined
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
36 ;;; that respect the `dired-do-*' command calling conventions: a prefix
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
37 ;;; argument is interpreted as the number of succeeding files to `grep`,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
38 ;;; otherwise all the marked files are `grep`ed. \[dired-do-grep] and
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
39 ;;; \[dired-do-grep-find] are defined as aliases for \[dired-do-igrep]
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
40 ;;; and \[dired-do-igrep-find].
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
41
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
42 ;;; Copyright:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
43 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
44 ;;; Copyright © 1994,1995,1996,1997 Kevin Rodgers
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
45 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
46 ;;; This program is free software; you can redistribute it and/or modify
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
47 ;;; it under the terms of the GNU General Public License as published by
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
48 ;;; the Free Software Foundation; either version 2 of the License, or
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
49 ;;; at your option) any later version.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
50 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
51 ;;; This program is distributed in the hope that it will be useful,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
52 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
53 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
54 ;;; GNU General Public License for more details.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
55 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
56 ;;; You should have received a copy of the GNU General Public License
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
57 ;;; along with this program; if not, write to the Free Software
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
58 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
59 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
60 ;;; Neither my former nor current employer (Martin Marietta and
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
61 ;;; Information Handling Services, respectively) has disclaimed any
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
62 ;;; copyright interest in igrep.el.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
63 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
64 ;;; Kevin Rodgers <kevinr@ihs.com> Project Engineer
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
65 ;;; Information Handling Services Electronic Systems Development
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
66 ;;; 15 Inverness Way East, M/S A201
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
67 ;;; Englewood CO 80112 USA (303)397-2807[voice]/-2779[fax]
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
68
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
69 ;;; Installation:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
70 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
71 ;;; 1. Put this file in a directory that is a member of load-path, and
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
72 ;;; byte-compile it (e.g. with `M-x byte-compile-file') for better
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
73 ;;; performance. You can ignore any warnings about references to free
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
74 ;;; variables and "not known to be defined" functions.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
75 ;;; 2. Put these forms in default.el or ~/.emacs:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
76 ;;; (autoload (function igrep) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
77 ;;; "*Run `grep` PROGRAM to match EXPRESSION in FILES..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
78 ;;; (autoload (function igrep-find) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
79 ;;; "*Run `grep` via `find`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
80 ;;; (autoload (function dired-do-igrep) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
81 ;;; "*Run `grep` on the marked (or next prefix ARG) files." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
82 ;;; (autoload (function dired-do-igrep-find) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
83 ;;; "*Run `grep` via `find` on the marked (or next prefix ARG) directories." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
84 ;;; 2. a. For completeness, you can add these forms as well:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
85 ;;; (autoload (function grep) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
86 ;;; "*Run `grep` PROGRAM to match EXPRESSION in FILES..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
87 ;;; (autoload (function egrep) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
88 ;;; "*Run `egrep`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
89 ;;; (autoload (function fgrep) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
90 ;;; "*Run `fgrep`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
91 ;;; (autoload (function agrep) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
92 ;;; "*Run `agrep`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
93 ;;; (autoload (function grep-find) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
94 ;;; "*Run `grep` via `find`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
95 ;;; (autoload (function egrep-find) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
96 ;;; "*Run `egrep` via `find`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
97 ;;; (autoload (function fgrep-find) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
98 ;;; "*Run `fgrep` via `find`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
99 ;;; (autoload (function agrep-find) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
100 ;;; "*Run `agrep` via `find`..." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
101 ;;; (autoload (function dired-do-grep) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
102 ;;; "*Run `grep` on the marked (or next prefix ARG) files." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
103 ;;; (autoload (function dired-do-grep-find) "igrep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
104 ;;; "*Run `grep` via `find` on the marked (or next prefix ARG) directories." t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
105 ;;; 3. If you are running Windows 95/NT, you should install findutils
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
106 ;;; and grep from release 17.1 (or higher) of the Cygnus GNU-Win32
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
107 ;;; distribution. See <URL:http://www.cygnus.com/misc/gnu-win32/>.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
108
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
109 ;;; Usage:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
110 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
111 ;;; M-x igrep M-x igrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
112 ;;; M-x grep M-x grep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
113 ;;; M-x egrep M-x egrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
114 ;;; M-x fgrep M-x fgrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
115 ;;; M-x agrep M-x agrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
116 ;;; M-x dired-do-igrep M-x dired-do-igrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
117 ;;; M-x dired-do-grep M-x dired-do-grep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
118 ;;; (Each of the 10 igrep commands accepts 1, 2, or 3 `C-u' prefix arguments.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
119 ;;; The 2 Dired commands interpret a prefix argument like the regular `dired-do'
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
120 ;;; commands.)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
121 ;;; C-x ` (M-x next-error) C-c C-c (M-x compile-goto-error) [in *igrep*]
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
122
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
123 ;;; Customization examples:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
124 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
125 ;;; To ignore case by default:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
126 ;;; (setq igrep-options "-i")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
127 ;;; To search subdirectories by default:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
128 ;;; (setq igrep-find t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
129 ;;; To search files with the GNU (gzip) zgrep script:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
130 ;;; (setq igrep-use-zgrep t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
131 ;;; or define new igrep commands (this works for zegrep and zfgrep as well):
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
132 ;;; (igrep-define zgrep) ; M-x zgrep
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
133 ;;; (igrep-find-define zgrep) ; M-x zgrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
134 ;;; To avoid exceeding some shells' limit on command argument length
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
135 ;;; (this only works when grep'ing files in the current directory):
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
136 ;;; (setq igrep-find t
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
137 ;;; igrep-find-prune-option "\\! -name .")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
138
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
139 ;;; To do:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
140 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
141 ;;; 1. Delete the *-program variables (except for igrep-program) and
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
142 ;;; replace igrep-options with a table that maps igrep-program to the
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
143 ;;; appropriate options.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
144 ;;; 2. Generalize support for the -prune find clause (e.g. -fstype nfs).
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
145 ;;; 3. Provide support for `glimpse`.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
146 ;;; 4. Add a menu interface.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
147
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
148 ;;; LCD Archive Entry:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
149 ;;;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
150 ;;; igrep|Kevin Rodgers|kevinr@ihs.com|
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
151 ;;; An improved interface to grep/egrep/fgrep; plus recursive `find` versions.|
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
152 ;;; 97/02/10|2.56|~/misc/igrep.el.Z|
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
153
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
154
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
155 ;;; Package interface:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
156
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
157 (provide 'igrep)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
158
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
159 (require 'backquote) ; igrep-with-default-in-history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
160 (require 'compile) ; compile-internal and grep-regexp-
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
161 ; alist
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
162
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
163 (defconst igrep-version "2.56"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
164 "Version of igrep.el")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
165
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
166
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
167 ;;; User options:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
168
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
169 (defvar igrep-options nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
170 "*The options passed by \\[igrep] to `igrep-program', or nil.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
171
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
172 `-n' will automatically be passed to `igrep-program', to generate the
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
173 output expected by \\[next-error] and \\[compile-goto-error].
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
174 `-e' will automatically be passed to `igrep-program', if it supports
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
175 that option.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
176
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
177 (defvar igrep-read-options nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
178 "*If non-nil, `\\[igrep]' always prompts for options;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
179 otherwise, it only prompts when 1 or 3 `C-u's are given as a prefix arg.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
180
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
181 (defvar igrep-read-multiple-files nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
182 "*If non-nil, `\\[igrep]' always prompts for multiple-files;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
183 otherwise, it only prompts when 2 or 3 `C-u's are given as a prefix arg.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
184
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
185 (defvar igrep-verbose-prompts t
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
186 "*If t, \\[igrep] prompts for arguments verbosely;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
187 if not t but non-nil, \\[igrep] prompts for arguments semi-verbosely;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
188 if nil, \\[igrep] prompts for arguments tersely.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
189
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
190 (defvar igrep-save-buffers 'query
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
191 "*If t, \\[igrep] first saves each modified file buffer;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
192 if not t but non-nil, \\[igrep] offers to save each modified file buffer.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
193
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
194 (defvar igrep-program-table ; referenced by igrep-use-zgrep
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
195 (let ((exec-directories exec-path)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
196 (program-obarray (make-vector 11 0)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
197 (while exec-directories
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
198 (if (and (car exec-directories)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
199 (file-directory-p (car exec-directories)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
200 (let ((grep-programs
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
201 (directory-files (car exec-directories)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
202 nil "grep\\(\\.exe\\)?\\'")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
203 (while grep-programs
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
204 ;; Check `(file-executable-p (car grep-programs))'?
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
205 (if (save-match-data
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
206 (string-match "\\.exe\\'" (car grep-programs)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
207 (intern (substring (car grep-programs) 0 -4) program-obarray)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
208 (intern (car grep-programs) program-obarray))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
209 (setq grep-programs (cdr grep-programs)))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
210 (setq exec-directories (cdr exec-directories)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
211 program-obarray)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
212 "An obarray of available `grep` programs, passed by `igrep-read-program'
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
213 to `completing-read' when `igrep-program' is nil.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
214
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
215 (defvar igrep-use-zgrep
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
216 (if (intern-soft "zgrep" igrep-program-table)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
217 'files)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
218 "If t, \\[igrep] searches files using the GNU (gzip) `zPROGRAM` script;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
219 If not t but non-nil, \\[igrep] searches compressed FILES using `zPROGRAM`;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
220 if nil, \\[igrep] searches files with `PROGRAM`.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
221
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
222
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
223 ;;; User variables:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
224
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
225 (defvar igrep-program "grep"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
226 "The default shell program run by \\[igrep] and \\[igrep-find].
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
227 It must take a `grep` expression argument and one or more file names.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
228 If nil, \\[igrep] prompts for the program to run.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
229
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
230 (defvar igrep-expression-quote-char
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
231 (if (memq system-type '(ms-dos windows-95 windows-nt emx))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
232 ?\"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
233 ?')
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
234 "The character used to delimit the EXPRESSION argument to \\[igrep],
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
235 to protect it from shell filename expansion.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
236
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
237 (defvar igrep-expression-option
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
238 (if (or (eq system-type 'berkeley-unix)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
239 (save-match-data
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
240 (string-match "-sco" system-configuration)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
241 "-e")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
242 "If non-nil, the option used to specify the EXPRESSION argument to \\[igrep],
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
243 to protect an initial `-' from option processing.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
244
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
245 (defvar igrep-parenthesis-escape-char
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
246 (if (memq system-type '(ms-dos windows-95 windows-nt emx))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
247 nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
248 ?\\)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
249 "If non-nil, the character used by \\[igrep] to escape parentheses,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
250 to protect them from shell interpretation.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
251
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
252 (defvar igrep-find nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
253 "If non-nil, \\[igrep] searches directories using `find`.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
254 See \\[igrep-find].")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
255
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
256 (defvar igrep-find-prune-options
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
257 (if (not (save-match-data
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
258 (string-match "-sco" system-configuration)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
259 "-name SCCS -o -name RCS")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
260 "The `find` clause used to prune directories, or nil;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
261 see \\[igrep-find].")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
262
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
263 (defvar igrep-find-file-options "-type f -o -type l"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
264 "The `find` clause used to filter files passed to `grep`, or nil;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
265 see \\[igrep-find].")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
266
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
267 (defvar igrep-find-use-xargs
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
268 (if (equal (call-process "find" nil nil nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
269 (if (boundp 'grep-null-device)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
270 grep-null-device
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
271 "/dev/null")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
272 "-print0")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
273 0)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
274 'gnu)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
275 "If `gnu', \\[igrep-find] executes
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
276 `find ... -print0 | xargs -0 -e grep ...`;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
277 if not `gnu' but non-nil, \\[igrep-find] executes
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
278 `find ... -print | xargs -e grep ...`;
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
279 if nil, \\[igrep-find] executes
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
280 `find ... -exec grep ...`.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
281
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
282 (defvar igrep-program-default nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
283 "The default `grep` program, passed by `igrep-read-program'
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
284 to `completing-read' when `igrep-program' is nil.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
285
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
286 (defvar igrep-expression-history '()
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
287 "The minibuffer history list for \\[igrep]'s EXPRESSION argument.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
288
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
289 (defvar igrep-files-history '()
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
290 "The minibuffer history list for \\[igrep]'s FILES argument.")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
291
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
292
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
293 ;;; Commands:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
294
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
295 ;; ;;;###autoload Not ready to replace compile.el's grep yet. -sb
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
296 (defadvice grep (around igrep-interface first (&rest grep-args) activate)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
297 "If called interactively, use the \\[igrep] interface instead,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
298 where GREP-ARGS is (PROGRAM EXPRESSION FILES OPTIONS);
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
299 if called programmatically, GREP-ARGS is still (COMMAND)."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
300 (interactive (igrep-read-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
301 (if (interactive-p)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
302 (apply (function igrep) grep-args)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
303 ad-do-it))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
304
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
305 (defalias 'grep-find 'igrep-find)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
306
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
307 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
308 (defun igrep (program expression files &optional options)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
309 "*Run `grep` PROGRAM to match EXPRESSION in FILES.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
310 The output is displayed in the *igrep* buffer, which \\[next-error] and
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
311 \\[compile-goto-error] parse to find each line of matched text.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
312
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
313 PROGRAM may be nil, in which case it defaults to `igrep-program'.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
314
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
315 EXPRESSION is automatically delimited by `igrep-expression-quote-char'.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
316
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
317 FILES is either a file name pattern (expanded by the shell named by
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
318 `shell-file-name') or a list of file name patterns.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
319
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
320 Optional OPTIONS is also passed to PROGRAM; it defaults to `igrep-options'.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
321
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
322 If a prefix argument \
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
323 \(\\[universal-argument]\) \
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
324 is given when called interactively,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
325 or if `igrep-read-options' is set, OPTIONS is read from the minibuffer.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
326
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
327 If two prefix arguments \
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
328 \(\\[universal-argument] \\[universal-argument]\) \
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
329 are given when called interactively,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
330 or if `igrep-read-multiple-files' is set, FILES is read from the minibuffer
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
331 multiple times.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
332
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
333 If three prefix arguments \
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
334 \(\\[universal-argument] \\[universal-argument] \\[universal-argument]\) \
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
335 are given when called interactively,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
336 or if `igrep-read-options' and `igrep-read-multiple-files' are set,
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
337 OPTIONS is read and FILES is read multiple times.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
338
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
339 If `igrep-find' is non-nil, the directory or directories
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
340 containing FILES is recursively searched for files whose name matches
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
341 the file name component of FILES \(and whose contents match
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
342 EXPRESSION\)."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
343 (interactive
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
344 (igrep-read-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
345 (if (null program)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
346 (setq program (or igrep-program "grep")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
347 (if (null options)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
348 (setq options igrep-options))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
349 (if (not (listp files)) ; (stringp files)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
350 (setq files (list files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
351 (if (string-match "^[rj]?sh$" (file-name-nondirectory shell-file-name))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
352 ;; (restricted, job-control, or standard) Bourne shell doesn't expand ~:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
353 (setq files
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
354 (mapcar 'expand-file-name files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
355 (let* ((win32-quote-process-args nil) ; work around NT Emacs hack
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
356 (use-zgrep (cond ((eq igrep-use-zgrep t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
357 (igrep-use-zgrep
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
358 (let ((files files)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
359 (compressed-p nil))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
360 (while (and files (not compressed-p))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
361 (if (save-match-data
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
362 (string-match "\\.g?[zZ]\\'" (car files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
363 (setq compressed-p t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
364 (setq files (cdr files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
365 compressed-p))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
366 (t nil)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
367 (command (format "%s -n %s %s %c%s%c %s %s"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
368 (if (and use-zgrep
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
369 (save-match-data
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
370 (not (string-match "\\`z" program))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
371 (setq program (concat "z" program))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
372 program)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
373 (or options "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
374 (or igrep-expression-option
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
375 (progn
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
376 (if (save-match-data
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
377 (string-match "\\`-" expression))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
378 (setq expression (concat "\\" expression)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
379 ""))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
380 igrep-expression-quote-char
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
381 expression
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
382 igrep-expression-quote-char
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
383 (if igrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
384 (if igrep-find-use-xargs
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
385 ""
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
386 "\"{}\"")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
387 (mapconcat (function identity) files " "))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
388 (if (boundp 'grep-null-device)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
389 grep-null-device
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
390 "/dev/null"))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
391 (if igrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
392 (setq command
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
393 (igrep-format-find-command command files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
394 (cond ((eq igrep-save-buffers t) (save-some-buffers t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
395 (igrep-save-buffers (save-some-buffers)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
396 (compile-internal command
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
397 (format "No more %c%s%c matches"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
398 igrep-expression-quote-char
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
399 program
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
400 igrep-expression-quote-char)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
401 "igrep" nil grep-regexp-alist)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
402
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
403 ;; Analogue commands:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
404
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
405 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
406 (defmacro igrep-define (analogue-command &rest igrep-bindings)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
407 "Define ANALOGUE-COMMAND as an `igrep' analogue command.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
408 Optional (VARIABLE VALUE) arguments specify temporary bindings for the command."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
409 ;;; (interactive "SCommand: ") ; C-u => read bindings?
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
410 (let ((analogue-program (symbol-name analogue-command)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
411 (` (defun (, analogue-command) (&rest igrep-args)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
412 (, (format "*Run `%s` via \\[igrep].
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
413 All arguments \(including prefix arguments, when called interactively\)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
414 are handled by `igrep'."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
415 analogue-program))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
416 (interactive
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
417 (let ((igrep-program (if igrep-program (, analogue-program)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
418 (igrep-program-default (, analogue-program)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
419 (igrep-read-args)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
420 (let ( (,@ igrep-bindings))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
421 (apply (function igrep)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
422 (cond ((interactive-p) (car igrep-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
423 ((car igrep-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
424 (t (, analogue-program)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
425 (cdr igrep-args)))))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
426
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
427 (igrep-define egrep)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
428 (igrep-define fgrep)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
429 (igrep-define agrep
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
430 (igrep-use-zgrep nil)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
431 (igrep-expression-option "-e"))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
432
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
433
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
434 ;; Recursive (`find`) commands:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
435
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
436 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
437 (defun igrep-find (&rest igrep-args)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
438 "*Run `grep` via `find`; see \\[igrep] and `igrep-find'.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
439 All arguments \(including prefix arguments, when called interactively\)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
440 are handled by `igrep'."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
441 (interactive
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
442 (let ((igrep-find t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
443 (igrep-read-args)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
444 (let ((igrep-find t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
445 (apply (function igrep) igrep-args)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
446
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
447 ;; Analogue recursive (`find`) commands:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
448
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
449 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
450 (defmacro igrep-find-define (analogue-command &rest igrep-bindings)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
451 "Define ANALOGUE-COMMAND-find as an `igrep' analogue `find` command.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
452 Optional (VARIABLE VALUE) arguments specify temporary bindings for the command."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
453 ;;; (interactive "SCommand: ") ; C-u => read bindings?
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
454 (let ((analogue-program (symbol-name analogue-command)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
455 (setq analogue-command
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
456 (intern (format "%s-find" analogue-command)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
457 (` (defun (, analogue-command) (&rest igrep-args)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
458 (, (format "*Run `%s` via \\[igrep-find].
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
459 All arguments \(including prefix arguments, when called interactively\)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
460 are handled by `igrep'."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
461 analogue-program))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
462 (interactive
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
463 (let ((igrep-program (if igrep-program (, analogue-program)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
464 (igrep-program-default (, analogue-program))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
465 (igrep-find t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
466 (igrep-read-args)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
467 (let ( (,@ igrep-bindings))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
468 (apply (function igrep-find)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
469 (cond ((interactive-p) (car igrep-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
470 ((car igrep-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
471 (t (, analogue-program)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
472 (cdr igrep-args)))))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
473
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
474 (igrep-find-define egrep)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
475 (igrep-find-define fgrep)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
476 (igrep-find-define agrep
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
477 (igrep-use-zgrep nil)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
478 (igrep-expression-option "-e"))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
479
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
480
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
481 ;; Dired commands:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
482
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
483 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
484 (defun dired-do-igrep (program expression &optional options arg)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
485 "*Run `grep` PROGRAM to match EXPRESSION (with optional OPTIONS)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
486 on the marked (or next prefix ARG) files."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
487 (interactive
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
488 (let* ((current-prefix-arg nil)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
489 (igrep-args (igrep-read-args t)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
490 ;; Delete FILES:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
491 (setcdr (nthcdr 1 igrep-args) (nthcdr 3 igrep-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
492 ;; Append ARG:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
493 (nconc igrep-args (list current-prefix-arg))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
494 (igrep program
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
495 expression
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
496 (funcall (cond ((fboundp 'dired-get-marked-files) ; GNU Emacs
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
497 'dired-get-marked-files)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
498 ((fboundp 'dired-mark-get-files) ; XEmacs
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
499 'dired-mark-get-files))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
500 t arg)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
501 options))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
502
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
503 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
504 (defalias 'dired-do-grep 'dired-do-igrep)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
505
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
506 ;; Dired recursive (`find`) commands:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
507
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
508 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
509 (defun dired-do-igrep-find (program expression &optional options arg)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
510 "*Run `grep` PROGRAM to match EXPRESSION (with optional OPTIONS)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
511 on the marked (or next prefix ARG) directories."
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
512 (interactive
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
513 (let* ((current-prefix-arg nil)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
514 (igrep-find t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
515 (igrep-args (igrep-read-args t)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
516 ;; Delete FILES:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
517 (setcdr (nthcdr 1 igrep-args) (nthcdr 3 igrep-args))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
518 ;; Append ARG:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
519 (nconc igrep-args (list current-prefix-arg))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
520 (let ((igrep-find t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
521 (dired-do-igrep program expression options arg)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
522
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
523 ;;;###autoload
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
524 (defalias 'dired-do-grep-find 'dired-do-igrep-find)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
525
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
526
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
527 ;;; Utilities:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
528
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
529 (defsubst igrep-file-directory (name)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
530 ;; Return the directory component of NAME, or "." if it has no
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
531 ;; directory component.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
532 (directory-file-name (or (file-name-directory name)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
533 (file-name-as-directory "."))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
534
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
535 (defsubst igrep-file-pattern (name)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
536 ;; Return the file component of NAME, or "*" if it has no file
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
537 ;; component.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
538 (let ((pattern (file-name-nondirectory name)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
539 (if (string= pattern "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
540 "*"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
541 pattern)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
542
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
543 (defun igrep-format-find-command (command files)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
544 ;; Format `grep` COMMAND to be invoked via `find` on FILES.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
545 (let ((directories '())
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
546 (patterns '()))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
547 (while files
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
548 (let ((dir (igrep-file-directory (car files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
549 (pat (igrep-file-pattern (car files))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
550 (if (and (not (string= dir "."))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
551 (file-symlink-p dir))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
552 (setq dir (concat dir "/.")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
553 (if (not (member dir directories))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
554 (setq directories (cons dir directories)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
555 (cond ((equal pat "*")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
556 (setq patterns t))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
557 ((and (listp patterns)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
558 (not (member pat patterns)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
559 (setq patterns (cons pat patterns)))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
560 (setq files (cdr files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
561 (format (cond ((eq igrep-find-use-xargs 'gnu)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
562 ;; | \\\n
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
563 "find %s %s %s %s -print0 | xargs -0 -e %s")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
564 (igrep-find-use-xargs
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
565 ;; | \\\n
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
566 "find %s %s %s %s -print | xargs -e %s")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
567 ;;; ((memq system-type '(ms-dos windows-95 windows-nt emx))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
568 ;;; "find %s %s %s %s -exec %s ;")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
569 (t
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
570 "find %s %s %s %s -exec %s \\;"))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
571 (mapconcat (function identity) (nreverse directories)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
572 " ")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
573 (if igrep-find-prune-options
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
574 (format "-type d %c( %s %c) -prune -o"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
575 (or igrep-parenthesis-escape-char ? )
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
576 igrep-find-prune-options
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
577 (or igrep-parenthesis-escape-char ? ))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
578 "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
579 (if igrep-find-file-options
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
580 (format "%c( %s %c)"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
581 (or igrep-parenthesis-escape-char ? )
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
582 igrep-find-file-options
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
583 (or igrep-parenthesis-escape-char ? ))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
584 "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
585 (if (listp patterns)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
586 (if (cdr patterns) ; (> (length patterns) 1)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
587 (format "%c( %s %c)"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
588 (or igrep-parenthesis-escape-char " ")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
589 (mapconcat (function (lambda (pat)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
590 (format "-name \"%s\"" pat)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
591 (nreverse patterns)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
592 " -o ")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
593 (or igrep-parenthesis-escape-char " "))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
594 (format "-name \"%s\"" (car patterns)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
595 "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
596 command)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
597
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
598 (defun igrep-read-args (&optional no-files)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
599 ;; Read and return a list: (PROGRAM EXPRESSION FILES OPTIONS).
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
600 ;; If NO-FILES is non-nil, then FILES is not read and nil is returned
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
601 ;; in its place.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
602 (let* ((program (igrep-read-program (if igrep-verbose-prompts
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
603 (if igrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
604 "[find] "))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
605 (prompt-prefix (if igrep-verbose-prompts
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
606 (apply (function concat)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
607 (if igrep-find
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
608 "[find] ")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
609 (if (eq igrep-verbose-prompts t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
610 (list program " ")))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
611 (options (igrep-read-options prompt-prefix)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
612 (if (eq igrep-verbose-prompts t)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
613 (setq prompt-prefix
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
614 (concat prompt-prefix options " ")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
615 (list program
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
616 (igrep-read-expression prompt-prefix)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
617 (if (not no-files)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
618 (igrep-read-files prompt-prefix))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
619 options)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
620
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
621 (defsubst igrep-prefix (prefix string)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
622 ;; If PREFIX is non-nil, concatenate it and STRING; otherwise, return STRING.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
623 (if prefix
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
624 (concat prefix string)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
625 string))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
626
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
627 (defun igrep-read-program (&optional prompt-prefix)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
628 ;; If igrep-program is nil, read and return a program name from the
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
629 ;; minibuffer; otherwise, return igrep-program.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
630 ;; Optional PROMPT-PREFIX is prepended to the "Program: " prompt.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
631 (or igrep-program
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
632 (let ((prompt "Program: "))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
633 (completing-read (igrep-prefix prompt-prefix prompt) igrep-program-table
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
634 nil t (or igrep-program-default "grep")))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
635
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
636 (defun igrep-read-options (&optional prompt-prefix)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
637 ;; If current-prefix-arg is '(4) or '(64), read and return an options
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
638 ;; string from the minibuffer; otherwise, return igrep-options.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
639 ;; Optional PROMPT-PREFIX is prepended to the "Options: " prompt.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
640 (if (or igrep-read-options
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
641 (and (consp current-prefix-arg)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
642 (memq (prefix-numeric-value current-prefix-arg)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
643 '(4 64))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
644 (let ((prompt "Options: "))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
645 (read-string (igrep-prefix prompt-prefix prompt)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
646 (or igrep-options "-")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
647 igrep-options))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
648
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
649 (defun igrep-read-expression (&optional prompt-prefix)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
650 ;; Read and return a `grep` expression string from the minibuffer.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
651 ;; Optional PROMPT-PREFIX is prepended to the "Expression: " prompt.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
652 (let ((default-expression (igrep-default-expression)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
653 (if (string= default-expression "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
654 (read-from-minibuffer (igrep-prefix prompt-prefix "Expression: ")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
655 nil nil nil 'igrep-expression-history)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
656 (let ((expression
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
657 (igrep-read-string-with-default-in-history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
658 (igrep-prefix prompt-prefix (format "Expression (default %s): "
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
659 default-expression))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
660 default-expression
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
661 'igrep-expression-history)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
662 (if (string= expression "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
663 default-expression
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
664 expression)))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
665
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
666 (defun igrep-default-expression ()
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
667 (if (eq major-mode 'dired-mode)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
668 (let ((dired-file (dired-get-filename nil t)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
669 (save-excursion
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
670 (set-buffer (or (and dired-file (get-file-buffer dired-file))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
671 (other-buffer (current-buffer) t)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
672 (current-word)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
673 (current-word)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
674
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
675 (defsubst igrep-default-key ()
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
676 ;; Return the key bound to `exit-minibuffer', preferably "\r".
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
677 (if (eq (lookup-key minibuffer-local-completion-map "\r")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
678 (function exit-minibuffer))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
679 "\r"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
680 (where-is-internal (function exit-minibuffer)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
681 minibuffer-local-completion-map
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
682 t)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
683
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
684 (defun igrep-read-files (&optional prompt-prefix)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
685 ;; Read and return a file name pattern from the minibuffer. If
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
686 ;; current-prefix-arg is '(16) or '(64), read multiple file name
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
687 ;; patterns and return them in a list. Optional PROMPT-PREFIX is
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
688 ;; prepended to the "File(s): " prompt.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
689 (let* ((dired-subdirectory (if (eq major-mode 'dired-mode)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
690 (dired-current-directory t)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
691 (default-files (concat dired-subdirectory
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
692 (igrep-default-file-pattern)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
693 (prompt (format "File(s) (default %s): " default-files))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
694 (insert-default-directory nil) ; use relative path names
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
695 (file (igrep-read-file-name-with-default-in-history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
696 (igrep-prefix prompt-prefix prompt)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
697 default-files
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
698 nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
699 'igrep-files-history)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
700 (if (or igrep-read-multiple-files
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
701 (and (consp current-prefix-arg)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
702 (memq (prefix-numeric-value current-prefix-arg)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
703 '(16 64))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
704 (let ((files (list file)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
705 (setq prompt
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
706 (igrep-prefix prompt-prefix
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
707 (if igrep-verbose-prompts
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
708 (format "File(s): [Type `%s' when done] "
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
709 (key-description (igrep-default-key)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
710 "File(s): ")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
711 (while (not (string= (setq file
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
712 (igrep-read-file-name prompt
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
713 nil "" nil nil
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
714 'igrep-files-history))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
715 ""))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
716 (setq files (cons file files)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
717 (nreverse files))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
718 file)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
719
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
720 (defmacro igrep-with-default-in-history (default history &rest body)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
721 ;; Temporarily append DEFAULT to HISTORY, and execute BODY forms.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
722 (` (progn
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
723 ;; Append default to history:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
724 (set history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
725 (cons (, default)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
726 (if (boundp (, history))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
727 (symbol-value (, history))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
728 '())))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
729 (unwind-protect ; Make sure the history is restored.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
730 ;; Execute body:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
731 (progn (,@ body))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
732 ;; Delete default from history (undo the append above):
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
733 (setcdr (symbol-value (, history))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
734 (nthcdr 2 (symbol-value (, history))))))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
735
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
736 (defun igrep-read-string-with-default-in-history (prompt default history)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
737 ;; Read a string from the minibuffer, prompting with string PROMPT.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
738 ;; DEFAULT can be inserted into the minibuffer with `previous-
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
739 ;; history-element'; HISTORY is a symbol whose value (if bound) is a
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
740 ;; list of previous results, most recent first.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
741 (let ((string (igrep-with-default-in-history default history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
742 (read-from-minibuffer prompt nil nil nil history))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
743 ;; Replace empty string in history with default:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
744 (if (string= string "")
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
745 (setcar (symbol-value history) default))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
746 string))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
747
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
748 (defun igrep-read-file-name-with-default-in-history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
749 (prompt &optional default initial history)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
750 ;; Read a file name from the minibuffer, prompting with string PROMPT.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
751 ;; DEFAULT can be inserted into the minibuffer with `previous-
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
752 ;; history-element'; HISTORY is a symbol whose value (if any) is a
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
753 ;; list of previous results, most recent first.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
754 (igrep-with-default-in-history default history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
755 (igrep-read-file-name prompt nil default nil initial history)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
756
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
757 (defun igrep-read-file-name (prompt
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
758 &optional directory default existing initial history)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
759 ;; Just like read-file-name, but with optional HISTORY.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
760 ;; Also: convert DIRECTORY to DIRECTORY/* file name pattern.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
761 (let ((file-name
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
762 (if history
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
763 (let ((file-name-history (symbol-value history)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
764 (prog1 (read-file-name prompt directory default existing initial)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
765 (set history file-name-history)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
766 (read-file-name prompt directory default existing initial))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
767 (if (and (not (string-equal file-name ""))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
768 (file-directory-p file-name))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
769 (expand-file-name "*" file-name)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
770 file-name)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
771
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
772 (defun igrep-default-file-pattern ()
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
773 ;; Return a shell file name pattern that matches files with the same
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
774 ;; extension as the file being visited in the current buffer.
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
775 ;; (Based on other-possibly-interesting-files in ~/as-is/unix.el, by
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
776 ;; Wolfgang Rupprecht <wolfgang@mgm.mit.edu>.)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
777 (if (eq major-mode 'dired-mode)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
778 (cond ((stringp dired-directory)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
779 (if (file-directory-p dired-directory)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
780 "*"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
781 (file-name-nondirectory dired-directory))) ; wildcard
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
782 ((consp dired-directory) ; (DIR FILE ...)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
783 (mapconcat 'identity (cdr dired-directory) " ")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
784 (if buffer-file-name
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
785 (let ((file-name (file-name-nondirectory buffer-file-name)))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
786 (concat "*"
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
787 (save-match-data
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
788 (if (string-match "\\.[^.]+\\(\\.g?[zZ]\\)?$" file-name)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
789 (substring file-name
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
790 (match-beginning 0) (match-end 0))))))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
791 "*")))
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
792
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
793 ;;; Local Variables:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
794 ;;; eval: (put 'igrep-with-default-in-history 'lisp-indent-hook 2)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
795 ;;; eval: (put 'igrep-define 'lisp-indent-hook 1)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
796 ;;; eval: (put 'igrep-find-define 'lisp-indent-hook 1)
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
797 ;;; End:
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
798
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
799 ;;;; igrep.el ends here
4103f0995bd7 Import from CVS: tag r19-15b95
cvs
parents:
diff changeset
800