annotate lisp/efs/dired-grep.el @ 42:8b8b7f3559a2 r19-15b104

Import from CVS: tag r19-15b104
author cvs
date Mon, 13 Aug 2007 08:54:51 +0200
parents 7e54bd776075
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
1 ;; -*-Emacs-Lisp-*-
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
3 ;;
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
4 ;; File: dired-grep.el
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
5 ;; RCS:
42
8b8b7f3559a2 Import from CVS: tag r19-15b104
cvs
parents: 40
diff changeset
6 ;; Dired Version: #Revision: 7.9 $
22
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
7 ;; Description: Support for running grep on marked files in a dired buffer.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
8 ;; Author: Sandy Rutherford <sandy@ibm550.sissa.it>
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
9 ;; Created: Tue Jul 13 22:59:37 1993 by sandy on ibm550
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
10 ;;
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
11 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
12
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
13 ;;; Copyright (C) 1993 Sandy Rutherford
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
14
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
15 ;;; This program is free software; you can redistribute it and/or modify
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
16 ;;; it under the terms of the GNU General Public License as published by
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
17 ;;; the Free Software Foundation; either version 1, or (at your option)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
18 ;;; any later version.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
19 ;;;
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
20 ;;; This program is distributed in the hope that it will be useful,
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
21 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
22 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
23 ;;; GNU General Public License for more details.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
24 ;;;
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
25 ;;; A copy of the GNU General Public License can be obtained from this
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
26 ;;; program's author (send electronic mail to sandy@ibm550.sissa.it) or
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
27 ;;; from the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
28 ;;; MA 02139, USA.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
29
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
30 ;;; The user-level command in this file is dired-grep-file. The command
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
31 ;;; grep is defined in compile.el. This file does not change that command.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
32
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
33 ;;; Requirements and provisions
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
34
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
35 (provide 'dired-grep)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
36 (or (fboundp 'file-local-copy) (require 'emacs-19))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
37 (or (fboundp 'generate-new-buffer) (require 'emacs-19))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
38 (require 'dired)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
39
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
40 ;;; Variables
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
41
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
42 (defvar dired-grep-program "grep"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
43 "Name of program to use to grep files.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
44 When used with the \"-n\" flag, program must precede each match with \"###:\",
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
45 where \"###\" is the line number of the match.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
46 If there are grep programs which don't do this, we'll try to think of
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
47 some way to accomodate them.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
48
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
49 (defvar dired-grep-switches nil
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
50 "*Switches to pass to the grep program.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
51 This may be either a string or a list of strings. It is not necessary to
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
52 include \"-n\" as that switch is always used.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
53
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
54 (defvar dired-grep-zcat-program "zcat"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
55 "Name of program to cat compressed files.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
56
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
57 (defvar dired-grep-compressed-file ".\\.\\(gz\\|[zZ]\\)$"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
58 "Regexp to match names of compressed files.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
59
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
60 (defvar dired-grep-pop-up-buffer t
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
61 "*If non-nil, the grep output is displayed in the other window upon
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
62 completion of the grep process.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
63
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
64 (defvar dired-grep-results-buffer "*Dired Grep*"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
65 "Name of buffer where grep results are logged.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
66
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
67 (defvar dired-grep-mode-hook nil
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
68 "Hook run after going into grep-mode")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
69
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
70 (defvar grep-history nil
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
71 "History of previous grep patterns used.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
72
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
73 (defvar dired-grep-parse-flags-cache nil)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
74 (defvar dired-grep-parse-flags-cache-result nil)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
75
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
76 (defvar dired-grep-mode-map nil
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
77 "Keymap for dired-grep-mode buffers.")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
78
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
79 (if dired-grep-mode-map
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
80 ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
81 (setq dired-grep-mode-map (make-keymap))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
82 (suppress-keymap dired-grep-mode-map)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
83 (define-key dired-grep-mode-map "[" 'backward-page)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
84 (define-key dired-grep-mode-map "]" 'forward-page)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
85 (define-key dired-grep-mode-map ">" 'dired-grep-next-hit)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
86 (define-key dired-grep-mode-map "<" 'dired-grep-previous-hit)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
87 (define-key dired-grep-mode-map "n" 'dired-grep-advertized-next-hit)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
88 (define-key dired-grep-mode-map "p" 'dired-grep-advertized-previous-hit)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
89 (define-key dired-grep-mode-map "k" 'dired-grep-delete-line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
90 (define-key dired-grep-mode-map "d" 'dired-grep-delete-page)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
91 (define-key dired-grep-mode-map "^" 'dired-grep-delete-preceding-pages)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
92 (define-key dired-grep-mode-map "f" 'dired-grep-find-file)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
93 (define-key dired-grep-mode-map "e" 'dired-grep-find-file)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
94 (define-key dired-grep-mode-map "m" 'dired-grep-delete-misses)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
95 (define-key dired-grep-mode-map "o" 'dired-grep-find-file-other-window)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
96 (define-key dired-grep-mode-map "v" 'dired-grep-view-file)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
97 (define-key dired-grep-mode-map "w" 'dired-grep-delete-grep-for)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
98 (define-key dired-grep-mode-map "\C-_" 'dired-grep-undo)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
99 (define-key dired-grep-mode-map "\C-xu" 'dired-grep-undo))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
100
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
101 ;;; Entry functions from dired.el
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
102
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
103 (defun dired-grep (pattern flags)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
104 ;; grep the file on the current line for PATTERN, using grep flags FLAGS.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
105 ;; Return nil on success. Offending filename otherwise.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
106 (let* ((file (dired-get-filename))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
107 (result (dired-grep-file pattern file flags)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
108 (and result
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
109 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
110 (dired-log (buffer-name (current-buffer)) (concat result "\n"))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
111 file))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
112
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
113 (defun dired-do-grep (pattern &optional flags arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
114 "Grep marked files for a pattern. With a \C-u prefix prompts for grep flags."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
115 (interactive
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
116 (let* ((switches (if (consp current-prefix-arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
117 (read-string "Switches for grep: ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
118 dired-grep-switches))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
119 (prompt (format "grep %sfor pattern"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
120 (if (stringp switches)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
121 (if (string-equal switches "")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
122 switches
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
123 (concat switches " "))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
124 (if switches
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
125 (concat (mapconcat 'identity switches " ") " ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
126 ""))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
127 (pattern (dired-read-with-history (concat prompt ": ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
128 nil 'grep-history)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
129 (list pattern switches
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
130 (and (not (consp current-prefix-arg)) current-prefix-arg))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
131 (dired-map-over-marks-check
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
132 (function
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
133 (lambda ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
134 (dired-grep pattern flags)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
135 arg 'grep (concat "grep " flags (if flags " \"" "\"") pattern "\"") t))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
136
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
137 ;;; Utility functions
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
138
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
139 (defun dired-grep-get-results-buffer ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
140 ;; Return the buffer object of the dired-grep-results-buffer, creating and
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
141 ;; initializing it if necessary.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
142 (let ((buffer (get-buffer dired-grep-results-buffer)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
143 (or buffer
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
144 (save-excursion
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
145 (set-buffer (setq buffer (get-buffer-create dired-grep-results-buffer)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
146 (dired-grep-mode)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
147 buffer))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
148
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
149 ;; Only define if undefined, in case efs has got to it already.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
150 (or (fboundp 'dired-grep-delete-local-temp-file)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
151 (defun dired-grep-delete-local-temp-file (file)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
152 (condition-case nil (delete-file file) (error nil))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
153
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
154 ;;; Commands in the dired-grep-results-buffer buffer.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
155
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
156 (defun dired-grep-mode ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
157 "\\<dired-grep-mode-map>Mode for perusing grep output generated from dired.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
158 The output is divided into pages, one page per grepped file.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
159
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
160 Summary of commands:
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
161
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
162 Move to next grep hit \\[dired-grep-advertized-next-hit], \\[dired-grep-next-hit]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
163 Move to previous grep hit \\[dired-grep-advertized-previous-hit], \\[dired-grep-previous-hit]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
164 Move to output for next file \\[forward-page]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
165 Move to output for previous file \\[backward-page]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
166
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
167 Delete the current grep line \\[dired-grep-delete-line]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
168 Delete all output for current file \\[dired-grep-delete-page]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
169 Delete all preceding pages \\[dired-grep-delete-preceding-pages]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
170 Delete all pages for files with no hits \\[dired-grep-delete-misses]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
171 Delete all pages which grep for the
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
172 same pattern as the current page \\[dired-grep-delete-grep-for]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
173
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
174 Find current grep hit in file \\[dired-grep-find-file]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
175 Find current grep hit in other window \\[dired-grep-find-file-other-window]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
176 View current grep hit \\[dired-grep-view-file]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
177
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
178 Undo changes to the grep buffer \\[dired-grep-undo]
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
179
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
180 Keybindings:
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
181 \\{dired-grep-mode-map}"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
182 (kill-all-local-variables)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
183 (use-local-map dired-grep-mode-map)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
184 (setq major-mode 'dired-grep-mode
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
185 mode-name "Dired-Grep"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
186 buffer-read-only t)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
187 (set (make-local-variable 'page-delimiter) "\n\n")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
188 (run-hooks 'dired-grep-mode-hook))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
189
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
190 (defun dired-grep-current-file-and-line ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
191 ;; Returns a list \(FILENAME . LINE\) corresponding to the filename
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
192 ;; and line number associated with the position of the point in a
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
193 ;; grep buffer. Returns nil if there is none.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
194 (save-excursion
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
195 (let (file line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
196 (and
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
197 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
198 (beginning-of-line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
199 (looking-at "[0-9]+:"))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
200 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
201 (setq line (string-to-int (buffer-substring (point)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
202 (1- (match-end 0)))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
203 (if (search-backward "\n\n" nil 'move) (forward-char 2))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
204 (looking-at "Hits for "))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
205 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
206 (forward-line 1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
207 (looking-at " "))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
208 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
209 (setq file (buffer-substring (match-end 0)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
210 (progn (end-of-line) (1- (point)))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
211 (cons file line))))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
212
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
213 (defun dired-grep-find-file ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
214 (interactive)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
215 (let ((file (dired-grep-current-file-and-line)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
216 (if file
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
217 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
218 (find-file (car file))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
219 (goto-line (cdr file))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
220 (recenter '(4)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
221 (error "No file specified by this line."))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
222
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
223 (defun dired-grep-find-file-other-window ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
224 (interactive)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
225 (let ((file (dired-grep-current-file-and-line)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
226 (if file
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
227 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
228 (find-file-other-window (car file))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
229 (goto-line (cdr file))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
230 (recenter '(4)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
231 (error "No file specified by this line."))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
232
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
233 (defun dired-grep-view-file ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
234 (interactive)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
235 (let ((file (dired-grep-current-file-and-line)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
236 (if file
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
237 (let* ((fun (function
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
238 (lambda () (goto-line (cdr file)) (recenter '(4)))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
239 (view-hook
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
240 (if (boundp 'view-hook)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
241 (if (and (listp view-hook)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
242 (not (eq (car view-hook) 'lambda)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
243 (cons fun view-hook)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
244 (list fun view-hook))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
245 fun)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
246 (view-file (car file)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
247 (error "No file specified by this line."))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
248
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
249 (defun dired-grep-next-hit (arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
250 "Moves to the next, or next ARGth, grep hit."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
251 (interactive "p")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
252 (forward-line 1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
253 (if (re-search-forward "^[0-9]" nil 'move arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
254 (goto-char (match-beginning 0))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
255 (error "No further grep hits")))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
256
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
257 (defun dired-grep-previous-hit (arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
258 "Moves to the previous, or previous ARGth, grep hit."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
259 (interactive "p")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
260 (beginning-of-line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
261 (or (re-search-backward "^[0-9]" nil 'move arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
262 (error "No further grep hits")))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
263
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
264 ;; These are only so we can get a decent looking help buffer.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
265 (fset 'dired-grep-advertized-next-hit 'dired-grep-next-hit)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
266 (fset 'dired-grep-advertized-previous-hit 'dired-grep-previous-hit)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
267
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
268 (defun dired-grep-delete-page (arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
269 "Deletes the current and ARG - 1 following grep output pages.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
270 If ARG is negative, deletes preceding pages."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
271 (interactive "p")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
272 (let ((done 0)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
273 (buffer-read-only nil)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
274 (backward (< arg 0))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
275 start)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
276 (if backward (setq arg (- arg)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
277 (while (and (< done arg) (not (if backward (bobp) (eobp))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
278 (or (looking-at "^\n")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
279 (if (search-backward "\n\n" nil 'move) (forward-char 1)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
280 (setq start (point))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
281 (if (search-forward "\n\n" nil 'move) (forward-char -1))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
282 (delete-region start (point))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
283 (and (bobp) (not (eobp)) (delete-char 1))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
284 (if backward (skip-chars-backward "\n"))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
285 (setq done (1+ done)))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
286
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
287 (defun dired-grep-delete-preceding-pages ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
288 "Deletes the current, and all preceding pages from the grep buffer."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
289 (interactive)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
290 (let ((buffer-read-only nil))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
291 (if (looking-at "^\n")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
292 (forward-char 1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
293 (search-forward "\n\n" nil 'move))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
294 (delete-region (point-min) (point))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
295
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
296 (defun dired-grep-delete-line (arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
297 "Deletes the current line and ARG following lines from the grep buffer.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
298 Only operates on lines which correspond to file lines for grep hits."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
299 (interactive "p")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
300 (let ((opoint (point))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
301 (buffer-read-only nil)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
302 (backward (< arg 0))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
303 (done 0))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
304 (beginning-of-line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
305 (if backward (setq arg (- arg)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
306 (if (looking-at "[0-9]+:")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
307 (while (< done arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
308 (delete-region (point) (progn (forward-line 1) (point)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
309 (if backward (forward-line -1))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
310 (if (looking-at "[0-9]+:")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
311 (setq done (1+ done))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
312 (setq done arg)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
313 ;; Do nothing.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
314 (goto-char opoint))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
315
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
316 (defun dired-grep-delete-grep-for ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
317 "Deletes all pages which grep some file for the pattern of the current page."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
318 (interactive)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
319 (save-excursion
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
320 ;; In case we happen to be right at the beginning of a page.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
321 (or (eobp) (eolp) (forward-char 1))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
322 (forward-page -1) ; gets to the beginning of the page.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
323 (let* ((eol (save-excursion (end-of-line) (point)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
324 (line (and (search-forward " grep " eol t)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
325 (buffer-substring (point) eol))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
326 (if line
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
327 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
328 (goto-char (point-min))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
329 (while (not (eobp))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
330 (let* ((eol (save-excursion (end-of-line) (point)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
331 (this-line (and (search-forward " grep " eol t)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
332 (buffer-substring (point) eol))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
333 (if (equal line this-line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
334 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
335 (dired-grep-delete-page 1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
336 (skip-chars-forward "\n"))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
337 (or (eobp) (forward-page 1))))))))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
338
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
339 (defun dired-grep-delete-misses ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
340 "Delete all pages for which there were no grep hits.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
341 Deletes pages for which grep failed because of an error too."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
342 (interactive)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
343 (save-excursion
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
344 (goto-char (point-min))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
345 (while (not (eobp))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
346 (if (looking-at "Grep failed \\|No hits ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
347 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
348 (dired-grep-delete-page 1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
349 (skip-chars-forward "\n"))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
350 (forward-page 1)))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
351
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
352 (defun dired-grep-undo ()
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
353 "Undoes deletions in a grep buffer."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
354 (interactive)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
355 (let (buffer-read-only)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
356 (undo)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
357
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
358 ;;; Commands for grepping files.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
359
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
360 (defun dired-grep-parse-flags (string)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
361 ;; Breaks a string of switches into a list.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
362 (if (equal dired-grep-parse-flags-cache string)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
363 dired-grep-parse-flags-cache-result
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
364 (let ((length (length string))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
365 (pointer 0)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
366 (start 0)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
367 (result nil))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
368 (while (and (< pointer length) (= (aref string pointer) ?\ ))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
369 (setq pointer (1+ pointer)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
370 (while (< pointer length)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
371 (setq start pointer)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
372 (while (and (< pointer length) (/= (aref string pointer) ?\ ))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
373 (setq pointer (1+ pointer)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
374 (setq result (cons (substring string start pointer) result))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
375 (while (and (< pointer length) (= (aref string pointer) ?\ ))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
376 (setq pointer (1+ pointer))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
377 (setq dired-grep-parse-flags-cache string
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
378 dired-grep-parse-flags-cache-result (nreverse result)))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
379
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
380 (defun dired-grep-file (pattern file &optional flags)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
381 "Grep for PATTERN in FILE.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
382 Optional FLAGS are flags to pass to the grep program.
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
383 When used interactively, will prompt for FLAGS if a prefix argument is used."
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
384 (interactive
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
385 (let* ((switches (if (consp current-prefix-arg)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
386 (read-string "Switches for grep: ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
387 dired-grep-switches))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
388 (prompt (format "grep %sfor pattern"
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
389 (if (stringp switches)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
390 (if (string-match switches "^ *$")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
391 ""
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
392 (concat switches " "))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
393 (if switches
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
394 (concat (mapconcat 'identity switches " ") " ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
395 ""))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
396 (pattern (dired-read-with-history (concat prompt ": ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
397 nil 'grep-history))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
398 (file (read-file-name (concat prompt " \"" pattern "\" in file :"))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
399 (list pattern file switches)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
400 (setq file (expand-file-name file))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
401 (if (listp flags)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
402 (setq flags (mapconcat 'identity flags " "))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
403 (if (string-match "^ +$" flags)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
404 (setq flags "")))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
405 (let ((file-buff (get-file-buffer file)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
406 (if (and file-buff (buffer-modified-p file-buff))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
407 (if (y-or-n-p (format "Save buffer %s? " (buffer-name file-buff)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
408 (save-excursion
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
409 (set-buffer file-buff)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
410 (save-buffer)))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
411 (let ((buffer (dired-grep-get-results-buffer))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
412 (compressed (string-match dired-grep-compressed-file file))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
413 failed temp-file jka-compr-compression-info-list)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
414 (setq temp-file
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
415 (condition-case err
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
416 (file-local-copy file)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
417 (error (progn (setq failed (format "%s" err)) nil))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
418 (or failed
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
419 (save-excursion
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
420 (set-buffer buffer)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
421 (goto-char (point-max))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
422 (let ((buffer-read-only nil)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
423 pos-1 pos-2)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
424 (or (bobp) (insert "\n"))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
425 (setq pos-1 (point))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
426 (insert "Hits for grep ")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
427 (or (string-equal flags "") (insert flags " "))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
428 (insert "\"" pattern "\" in\n " file ":\n")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
429 (setq pos-2 (point))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
430 (condition-case err
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
431 (apply
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
432 'call-process
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
433 (if compressed "sh" dired-grep-program)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
434 (or temp-file file)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
435 buffer t
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
436 (if compressed
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
437 (list "-c" (concat dired-grep-zcat-program
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
438 " |" dired-grep-program
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
439 " " flags " -n '" pattern "'"))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
440 (append (dired-grep-parse-flags flags)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
441 (list "-n" pattern))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
442 (error (setq failed (format "%s" err))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
443 (if failed
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
444 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
445 (if (= pos-2 (point-max))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
446 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
447 (goto-char (1- pos-2))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
448 (delete-char -1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
449 (insert ".")))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
450 (goto-char pos-1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
451 (delete-char 4)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
452 (insert "Grep failed")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
453 failed)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
454 (if (= pos-2 (point-max))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
455 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
456 (goto-char pos-1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
457 (delete-char 1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
458 (insert "No h")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
459 (forward-line 1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
460 (end-of-line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
461 (delete-char -1)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
462 (insert "."))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
463 (goto-char pos-2)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
464 (or (looking-at "[0-9]+:")
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
465 (setq failed (buffer-substring pos-2
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
466 (progn (end-of-line)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
467 (point))))))))))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
468 (let ((curr-wind (selected-window)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
469 (unwind-protect
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
470 (progn
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
471 (pop-to-buffer buffer)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
472 (goto-char (point-max)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
473 (select-window curr-wind)))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
474 (if temp-file
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
475 (dired-grep-delete-local-temp-file temp-file))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
476 failed))
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
477
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
478 ;;; Run the load hook
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
479
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
480 (run-hooks 'dired-grep-load-hook)
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
481
8fc7fe29b841 Import from CVS: tag r19-15b94
cvs
parents:
diff changeset
482 ;;; end of dired-grep.el