Mercurial > emacs
annotate lisp/ibuf-ext.el @ 44315:61313ed69c33
*** empty log message ***
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Mon, 01 Apr 2002 19:50:51 +0000 |
parents | 598fbedb891e |
children | 3e16ea61b8c4 |
rev | line source |
---|---|
42813
2fa4141b1dee
Enable byte-compile-dynamic.
Richard M. Stallman <rms@gnu.org>
parents:
42804
diff
changeset
|
1 ;;; ibuf-ext.el --- extensions for ibuffer -*-byte-compile-dynamic: t;-*- |
42702 | 2 |
42771
ed597889bfc8
(toplevel): Remove X-RCS, URL, Compatibility headers. Update
Colin Walters <walters@gnu.org>
parents:
42702
diff
changeset
|
3 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. |
42702 | 4 |
5 ;; Author: Colin Walters <walters@verbum.org> | |
6 ;; Created: 2 Dec 2001 | |
7 ;; Keywords: buffer, convenience | |
8 | |
9 ;; This file is not currently part of GNU Emacs. | |
10 | |
11 ;; This program is free software; you can redistribute it and/or | |
12 ;; modify it under the terms of the GNU General Public License as | |
13 ;; published by the Free Software Foundation; either version 2, or (at | |
14 ;; your option) any later version. | |
15 | |
16 ;; This program is distributed in the hope that it will be useful, but | |
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 ;; General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with this program ; see the file COPYING. If not, write to | |
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; These functions should be automatically loaded when called, but you | |
29 ;; can explicity (require 'ibuf-ext) in your ~/.emacs to have them | |
30 ;; preloaded. | |
31 | |
32 ;;; Code: | |
33 | |
34 (require 'ibuffer) | |
35 | |
36 (eval-when-compile | |
37 (require 'derived) | |
38 (require 'ibuf-macs) | |
39 (require 'cl)) | |
40 | |
41 ;;; Utility functions | |
42 (defun ibuffer-delete-alist (key alist) | |
43 "Delete all entries in ALIST that have a key equal to KEY." | |
44 (let (entry) | |
45 (while (setq entry (assoc key alist)) | |
46 (setq alist (delete entry alist))) | |
47 alist)) | |
48 | |
49 (defun ibuffer-depropertize-string (str &optional nocopy) | |
50 "Return a copy of STR with text properties removed. | |
51 If optional argument NOCOPY is non-nil, actually modify the string directly." | |
52 (let ((str (if nocopy | |
53 str | |
54 (copy-sequence str)))) | |
55 (set-text-properties 0 (length str) nil str) | |
56 str)) | |
57 | |
58 (defcustom ibuffer-never-show-predicates nil | |
59 "A list of predicates (a regexp or function) for buffers not to display. | |
60 If a regexp, then it will be matched against the buffer's name. | |
61 If a function, it will be called with the buffer as an argument, and | |
62 should return non-nil if this buffer should not be shown." | |
63 :type '(repeat (choice regexp function)) | |
64 :group 'ibuffer) | |
65 | |
66 (defcustom ibuffer-always-show-predicates nil | |
67 "A list of predicates (a regexp or function) for buffers to always display. | |
68 If a regexp, then it will be matched against the buffer's name. | |
69 If a function, it will be called with the buffer as an argument, and | |
70 should return non-nil if this buffer should be shown. | |
71 Note that buffers matching one of these predicates will be shown | |
72 regardless of any active filters in this buffer." | |
73 :type '(repeat (choice regexp function)) | |
74 :group 'ibuffer) | |
75 | |
76 (defvar ibuffer-tmp-hide-regexps nil | |
77 "A list of regexps which should match buffer names to not show.") | |
78 | |
79 (defvar ibuffer-tmp-show-regexps nil | |
80 "A list of regexps which should match buffer names to always show.") | |
81 | |
82 (defvar ibuffer-auto-mode nil | |
83 "If non-nil, Ibuffer auto-mode should be enabled for this buffer. | |
84 Do not set this variable directly! Use the function | |
85 `ibuffer-auto-mode' instead.") | |
86 | |
87 (defvar ibuffer-auto-buffers-changed nil) | |
88 | |
89 (defcustom ibuffer-occur-match-face 'font-lock-warning-face | |
90 "Face used for displaying matched strings for `ibuffer-do-occur'." | |
91 :type 'face | |
92 :group 'ibuffer) | |
93 | |
94 (defcustom ibuffer-saved-filters '(("gnus" | |
95 ((or (mode . message-mode) | |
96 (mode . mail-mode) | |
97 (mode . gnus-group-mode) | |
98 (mode . gnus-summary-mode) | |
99 (mode . gnus-article-mode)))) | |
100 ("programming" | |
101 ((or (mode . emacs-lisp-mode) | |
102 (mode . cperl-mode) | |
103 (mode . c-mode) | |
104 (mode . java-mode) | |
105 (mode . idl-mode) | |
106 (mode . lisp-mode))))) | |
107 | |
108 "An alist of filter qualifiers to switch between. | |
109 | |
110 This variable should look like ((\"STRING\" QUALIFIERS) | |
111 (\"STRING\" QUALIFIERS) ...), where | |
112 QUALIFIERS is a list of the same form as | |
113 `ibuffer-filtering-qualifiers'. | |
114 See also the variables `ibuffer-filtering-qualifiers', | |
115 `ibuffer-filtering-alist', and the functions | |
116 `ibuffer-switch-to-saved-filters', `ibuffer-save-filters'." | |
117 :type '(repeat sexp) | |
118 :group 'ibuffer) | |
119 | |
120 (defvar ibuffer-filtering-qualifiers nil | |
121 "A list like (SYMBOL . QUALIFIER) which filters the current buffer list. | |
122 See also `ibuffer-filtering-alist'.") | |
123 | |
124 ;; This is now frobbed by `define-ibuffer-filter'. | |
125 (defvar ibuffer-filtering-alist nil | |
126 "An alist of (SYMBOL DESCRIPTION FUNCTION) which describes a filter. | |
127 | |
128 You most likely do not want to modify this variable directly; see | |
129 `define-ibuffer-filter'. | |
130 | |
131 SYMBOL is the symbolic name of the filter. DESCRIPTION is used when | |
132 displaying information to the user. FUNCTION is given a buffer and | |
133 the value of the qualifier, and returns non-nil if and only if the | |
134 buffer should be displayed.") | |
135 | |
43102
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
136 (defcustom ibuffer-filter-format-alist nil |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
137 "An alist which has special formats used when a filter is active. |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
138 The contents of this variable should look like: |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
139 ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...) |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
140 |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
141 For example, suppose that when you add a filter for buffers whose |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
142 major mode is `emacs-lisp-mode', you only want to see the mark and the |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
143 name of the buffer. You could accomplish that by adding: |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
144 (mode ((mark \" \" name))) |
43769
72eb9658393c
(ibuffer-filter-format-alist): Add type and group.
Colin Walters <walters@gnu.org>
parents:
43567
diff
changeset
|
145 to this variable." |
72eb9658393c
(ibuffer-filter-format-alist): Add type and group.
Colin Walters <walters@gnu.org>
parents:
43567
diff
changeset
|
146 :type '(repeat (list :tag "Association" (symbol :tag "Filter") |
72eb9658393c
(ibuffer-filter-format-alist): Add type and group.
Colin Walters <walters@gnu.org>
parents:
43567
diff
changeset
|
147 (list :tag "Formats" (repeat (sexp :tag "Format"))))) |
72eb9658393c
(ibuffer-filter-format-alist): Add type and group.
Colin Walters <walters@gnu.org>
parents:
43567
diff
changeset
|
148 :group 'ibuffer) |
43102
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
149 |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
150 (defvar ibuffer-cached-filter-formats nil) |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
151 (defvar ibuffer-compiled-filter-formats nil) |
280c82e97c2a
(ibuffer-filter-format-alist, ibuffer-cached-filter-formats,
Colin Walters <walters@gnu.org>
parents:
42813
diff
changeset
|
152 |
43490
a08f4763cba4
(ibuffer-old-time): Change to hours.
Colin Walters <walters@gnu.org>
parents:
43379
diff
changeset
|
153 (defcustom ibuffer-old-time 72 |
a08f4763cba4
(ibuffer-old-time): Change to hours.
Colin Walters <walters@gnu.org>
parents:
43379
diff
changeset
|
154 "The number of hours before a buffer is considered \"old\"." |
a08f4763cba4
(ibuffer-old-time): Change to hours.
Colin Walters <walters@gnu.org>
parents:
43379
diff
changeset
|
155 :type '(choice (const :tag "72 hours (3 days)" 72) |
a08f4763cba4
(ibuffer-old-time): Change to hours.
Colin Walters <walters@gnu.org>
parents:
43379
diff
changeset
|
156 (const :tag "48 hours (2 days)" 48) |
a08f4763cba4
(ibuffer-old-time): Change to hours.
Colin Walters <walters@gnu.org>
parents:
43379
diff
changeset
|
157 (const :tag "24 hours (1 day)" 24) |
a08f4763cba4
(ibuffer-old-time): Change to hours.
Colin Walters <walters@gnu.org>
parents:
43379
diff
changeset
|
158 (integer :tag "hours")) |
42702 | 159 :group 'ibuffer) |
160 | |
161 (defcustom ibuffer-save-with-custom t | |
162 "If non-nil, then use Custom to save interactively changed variables. | |
163 Currently, this only applies to `ibuffer-saved-filters'." | |
164 :type 'boolean | |
165 :group 'ibuffer) | |
166 | |
167 (defun ibuffer-ext-visible-p (buf all &optional ibuffer-buf) | |
168 (or | |
169 (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps) | |
170 (and (not | |
171 (or | |
172 (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps) | |
173 (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates))) | |
174 (or all | |
175 (not | |
176 (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates))) | |
177 (or ibuffer-view-ibuffer | |
178 (and ibuffer-buf | |
179 (not (eq ibuffer-buf buf)))) | |
180 (or | |
181 (ibuffer-included-in-filters-p buf ibuffer-filtering-qualifiers) | |
182 (ibuffer-buf-matches-predicates buf ibuffer-always-show-predicates))))) | |
183 | |
184 (defun ibuffer-auto-update-changed () | |
185 (when ibuffer-auto-buffers-changed | |
186 (setq ibuffer-auto-buffers-changed nil) | |
187 (mapcar #'(lambda (buf) | |
188 (ignore-errors | |
189 (with-current-buffer buf | |
190 (when (and ibuffer-auto-mode | |
191 (eq major-mode 'ibuffer-mode)) | |
192 (ibuffer-update nil t))))) | |
193 (buffer-list)))) | |
194 | |
195 ;;;###autoload | |
196 (defun ibuffer-auto-mode (&optional arg) | |
197 "Toggle use of Ibuffer's auto-update facility. | |
198 With numeric ARG, enable auto-update if and only if ARG is positive." | |
199 (interactive) | |
200 (unless (eq major-mode 'ibuffer-mode) | |
201 (error "This buffer is not in Ibuffer mode")) | |
202 (set (make-local-variable 'ibuffer-auto-mode) | |
203 (if arg | |
204 (plusp arg) | |
205 (not ibuffer-auto-mode))) | |
206 (defadvice get-buffer-create (after ibuffer-notify-create activate) | |
207 (setq ibuffer-auto-buffers-changed t)) | |
208 (defadvice kill-buffer (after ibuffer-notify-kill activate) | |
209 (setq ibuffer-auto-buffers-changed t)) | |
210 (add-hook 'post-command-hook 'ibuffer-auto-update-changed) | |
211 (ibuffer-update-mode-name)) | |
212 | |
213 ;;;###autoload | |
214 (defun ibuffer-mouse-filter-by-mode (event) | |
215 "Enable or disable filtering by the major mode chosen via mouse." | |
216 (interactive "e") | |
217 (ibuffer-interactive-filter-by-mode event)) | |
218 | |
219 ;;;###autoload | |
220 (defun ibuffer-interactive-filter-by-mode (event-or-point) | |
221 "Enable or disable filtering by the major mode at point." | |
222 (interactive "d") | |
223 (if (eventp event-or-point) | |
224 (mouse-set-point event-or-point) | |
225 (goto-char event-or-point)) | |
226 (let ((buf (ibuffer-current-buffer))) | |
227 (if (assq 'mode ibuffer-filtering-qualifiers) | |
228 (setq ibuffer-filtering-qualifiers | |
229 (ibuffer-delete-alist 'mode ibuffer-filtering-qualifiers)) | |
230 (ibuffer-push-filter (cons 'mode | |
231 (with-current-buffer buf | |
232 major-mode))))) | |
233 (ibuffer-update nil t)) | |
234 | |
235 (define-ibuffer-op shell-command-pipe (command) | |
236 "Pipe the contents of each marked buffer to shell command COMMAND." | |
237 (:interactive "sPipe to shell command: " | |
238 :opstring "Shell command executed on" | |
239 :modifier-p nil) | |
240 (shell-command-on-region | |
241 (point-min) (point-max) command | |
242 (get-buffer-create "* ibuffer-shell-output*"))) | |
243 | |
244 (define-ibuffer-op shell-command-pipe-replace (command) | |
245 "Replace the contents of marked buffers with output of pipe to COMMAND." | |
246 (:interactive "sPipe to shell command (replace): " | |
247 :opstring "Buffer contents replaced in" | |
248 :active-opstring "replace buffer contents in" | |
249 :dangerous t | |
250 :modifier-p t) | |
251 (with-current-buffer buf | |
252 (shell-command-on-region (point-min) (point-max) | |
253 command nil t))) | |
254 | |
255 (define-ibuffer-op shell-command-file (command) | |
256 "Run shell command COMMAND separately on files of marked buffers." | |
257 (:interactive "sShell command on buffer's file: " | |
258 :opstring "Shell command executed on" | |
259 :modifier-p nil) | |
260 (shell-command (concat command " " | |
261 (shell-quote-argument | |
262 (if buffer-file-name | |
263 buffer-file-name | |
264 (make-temp-file | |
265 (substring (buffer-name) 0 (min 10 (length (buffer-name)))))))))) | |
266 | |
267 (define-ibuffer-op eval (form) | |
268 "Evaluate FORM in each of the buffers. | |
269 Does not display the buffer during evaluation. See | |
270 `ibuffer-do-view-and-eval' for that." | |
271 (:interactive "xEval in buffers (form): " | |
272 :opstring "evaluated in" | |
273 :modifier-p :maybe) | |
274 (eval form)) | |
275 | |
276 (define-ibuffer-op view-and-eval (form) | |
277 "Evaluate FORM while displaying each of the marked buffers. | |
278 To evaluate a form without viewing the buffer, see `ibuffer-do-eval'." | |
279 (:interactive "xEval viewing buffers (form): " | |
280 :opstring "evaluated in" | |
281 :complex t | |
282 :modifier-p :maybe) | |
283 (let ((ibuffer-buf (current-buffer))) | |
284 (unwind-protect | |
285 (progn | |
286 (switch-to-buffer buf) | |
287 (eval form)) | |
288 (switch-to-buffer ibuffer-buf)))) | |
289 | |
290 (define-ibuffer-op rename-uniquely () | |
291 "Rename marked buffers as with `rename-uniquely'." | |
292 (:opstring "renamed" | |
293 :modifier-p t) | |
294 (rename-uniquely)) | |
295 | |
296 (define-ibuffer-op revert () | |
297 "Revert marked buffers as with `revert-buffer'." | |
298 (:dangerous t | |
299 :opstring "reverted" | |
300 :active-opstring "revert" | |
301 :modifier-p :maybe) | |
302 (revert-buffer t t)) | |
303 | |
304 (define-ibuffer-op replace-regexp (from-str to-str) | |
305 "Perform a `replace-regexp' in marked buffers." | |
306 (:interactive | |
307 (let* ((from-str (read-from-minibuffer "Replace regexp: ")) | |
308 (to-str (read-from-minibuffer (concat "Replace " from-str | |
309 " with: ")))) | |
310 (list from-str to-str)) | |
311 :opstring "replaced in" | |
312 :complex t | |
313 :modifier-p :maybe) | |
314 (save-window-excursion | |
315 (switch-to-buffer buf) | |
316 (save-excursion | |
317 (goto-char (point-min)) | |
318 (let ((case-fold-search ibuffer-case-fold-search)) | |
319 (while (re-search-forward from-str nil t) | |
320 (replace-match to-str)))) | |
321 t)) | |
322 | |
323 (define-ibuffer-op query-replace (&rest args) | |
324 "Perform a `query-replace' in marked buffers." | |
325 (:interactive | |
44190
598fbedb891e
(operation query-replace): Use new `noerror' arg for
Colin Walters <walters@gnu.org>
parents:
44186
diff
changeset
|
326 (query-replace-read-args "Query replace" t t) |
42702 | 327 :opstring "replaced in" |
328 :complex t | |
329 :modifier-p :maybe) | |
330 (save-window-excursion | |
331 (switch-to-buffer buf) | |
332 (save-excursion | |
333 (let ((case-fold-search ibuffer-case-fold-search)) | |
334 (goto-char (point-min)) | |
335 (apply #'query-replace args))) | |
336 t)) | |
337 | |
338 (define-ibuffer-op query-replace-regexp (&rest args) | |
339 "Perform a `query-replace-regexp' in marked buffers." | |
340 (:interactive | |
44190
598fbedb891e
(operation query-replace): Use new `noerror' arg for
Colin Walters <walters@gnu.org>
parents:
44186
diff
changeset
|
341 (query-replace-read-args "Query replace regexp" t t) |
42702 | 342 :opstring "replaced in" |
343 :complex t | |
344 :modifier-p :maybe) | |
345 (save-window-excursion | |
346 (switch-to-buffer buf) | |
347 (save-excursion | |
348 (let ((case-fold-search ibuffer-case-fold-search)) | |
349 (goto-char (point-min)) | |
350 (apply #'query-replace-regexp args))) | |
351 t)) | |
352 | |
353 (define-ibuffer-op print () | |
354 "Print marked buffers as with `print-buffer'." | |
355 (:opstring "printed" | |
356 :modifier-p nil) | |
357 (print-buffer)) | |
358 | |
359 ;;;###autoload | |
360 (defun ibuffer-included-in-filters-p (buf filters) | |
361 (not | |
362 (memq nil ;; a filter will return nil if it failed | |
363 (mapcar | |
364 ;; filter should be like (TYPE . QUALIFIER), or | |
365 ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...) | |
366 #'(lambda (qual) | |
367 (ibuffer-included-in-filter-p buf qual)) | |
368 filters)))) | |
369 | |
370 (defun ibuffer-included-in-filter-p (buf filter) | |
371 (if (eq (car filter) 'not) | |
372 (not (ibuffer-included-in-filter-p-1 buf (cdr filter))) | |
373 (ibuffer-included-in-filter-p-1 buf filter))) | |
374 | |
375 (defun ibuffer-included-in-filter-p-1 (buf filter) | |
376 (not | |
377 (not | |
378 (case (car filter) | |
379 (or | |
380 (memq t (mapcar #'(lambda (x) | |
381 (ibuffer-included-in-filter-p buf x)) | |
382 (cdr filter)))) | |
383 (saved | |
384 (let ((data | |
385 (assoc (cdr filter) | |
386 ibuffer-saved-filters))) | |
387 (unless data | |
388 (ibuffer-filter-disable) | |
389 (error "Unknown saved filter %s" (cdr filter))) | |
390 (ibuffer-included-in-filters-p buf (cadr data)))) | |
391 (t | |
392 (let ((filterdat (assq (car filter) | |
393 ibuffer-filtering-alist))) | |
394 ;; filterdat should be like (TYPE DESCRIPTION FUNC) | |
395 ;; just a sanity check | |
396 (unless filterdat | |
397 (ibuffer-filter-disable) | |
398 (error "Undefined filter %s" (car filter))) | |
399 (not | |
400 (not | |
401 (funcall (caddr filterdat) | |
402 buf | |
403 (cdr filter)))))))))) | |
404 | |
405 ;;;###autoload | |
406 (defun ibuffer-filter-disable () | |
407 "Disable all filters currently in effect in this buffer." | |
408 (interactive) | |
409 (setq ibuffer-filtering-qualifiers nil) | |
410 (ibuffer-update nil t)) | |
411 | |
412 ;;;###autoload | |
413 (defun ibuffer-pop-filter () | |
414 "Remove the top filter in this buffer." | |
415 (interactive) | |
416 (when (null ibuffer-filtering-qualifiers) | |
417 (error "No filters in effect")) | |
418 (pop ibuffer-filtering-qualifiers) | |
419 (ibuffer-update nil t)) | |
420 | |
421 (defun ibuffer-push-filter (qualifier) | |
422 "Add QUALIFIER to `ibuffer-filtering-qualifiers'." | |
423 (push qualifier ibuffer-filtering-qualifiers)) | |
424 | |
425 ;;;###autoload | |
426 (defun ibuffer-decompose-filter () | |
427 "Separate the top compound filter (OR, NOT, or SAVED) in this buffer. | |
428 | |
429 This means that the topmost filter on the filtering stack, which must | |
430 be a complex filter like (OR [name: foo] [mode: bar-mode]), will be | |
431 turned into two separate filters [name: foo] and [mode: bar-mode]." | |
432 (interactive) | |
433 (when (null ibuffer-filtering-qualifiers) | |
434 (error "No filters in effect")) | |
435 (let ((lim (pop ibuffer-filtering-qualifiers))) | |
436 (case (car lim) | |
437 (or | |
438 (setq ibuffer-filtering-qualifiers (append | |
439 (cdr lim) | |
440 ibuffer-filtering-qualifiers))) | |
441 (saved | |
442 (let ((data | |
443 (assoc (cdr lim) | |
444 ibuffer-saved-filters))) | |
445 (unless data | |
446 (ibuffer-filter-disable) | |
447 (error "Unknown saved filter %s" (cdr lim))) | |
448 (setq ibuffer-filtering-qualifiers (append | |
449 (cadr data) | |
450 ibuffer-filtering-qualifiers)))) | |
451 (not | |
452 (push (cdr lim) | |
453 ibuffer-filtering-qualifiers)) | |
454 (t | |
455 (error "Filter type %s is not compound" (car lim))))) | |
456 (ibuffer-update nil t)) | |
457 | |
458 ;;;###autoload | |
459 (defun ibuffer-exchange-filters () | |
460 "Exchange the top two filters on the stack in this buffer." | |
461 (interactive) | |
462 (when (< (length ibuffer-filtering-qualifiers) | |
463 2) | |
464 (error "Need two filters to exchange")) | |
465 (let ((first (pop ibuffer-filtering-qualifiers)) | |
466 (second (pop ibuffer-filtering-qualifiers))) | |
467 (push first ibuffer-filtering-qualifiers) | |
468 (push second ibuffer-filtering-qualifiers)) | |
469 (ibuffer-update nil t)) | |
470 | |
471 ;;;###autoload | |
472 (defun ibuffer-negate-filter () | |
473 "Negate the sense of the top filter in the current buffer." | |
474 (interactive) | |
475 (when (null ibuffer-filtering-qualifiers) | |
476 (error "No filters in effect")) | |
477 (let ((lim (pop ibuffer-filtering-qualifiers))) | |
478 (push (if (eq (car lim) 'not) | |
479 (cdr lim) | |
480 (cons 'not lim)) | |
481 ibuffer-filtering-qualifiers)) | |
482 (ibuffer-update nil t)) | |
483 | |
484 ;;;###autoload | |
485 (defun ibuffer-or-filter (&optional reverse) | |
486 "Replace the top two filters in this buffer with their logical OR. | |
487 If optional argument REVERSE is non-nil, instead break the top OR | |
488 filter into parts." | |
489 (interactive "P") | |
490 (if reverse | |
491 (progn | |
492 (when (or (null ibuffer-filtering-qualifiers) | |
493 (not (eq 'or (caar ibuffer-filtering-qualifiers)))) | |
494 (error "Top filter is not an OR")) | |
495 (let ((lim (pop ibuffer-filtering-qualifiers))) | |
496 (setq ibuffer-filtering-qualifiers (nconc (cdr lim) ibuffer-filtering-qualifiers)))) | |
497 (when (< (length ibuffer-filtering-qualifiers) 2) | |
498 (error "Need two filters to OR")) | |
499 ;; If the second filter is an OR, just add to it. | |
500 (let ((first (pop ibuffer-filtering-qualifiers)) | |
501 (second (pop ibuffer-filtering-qualifiers))) | |
502 (if (eq 'or (car second)) | |
503 (push (nconc (list 'or first) (cdr second)) ibuffer-filtering-qualifiers) | |
504 (push (list 'or first second) | |
505 ibuffer-filtering-qualifiers)))) | |
506 (ibuffer-update nil t)) | |
507 | |
508 (defun ibuffer-maybe-save-saved-filters () | |
509 (when ibuffer-save-with-custom | |
510 (if (fboundp 'customize-save-variable) | |
511 (progn | |
512 (customize-save-variable 'ibuffer-saved-filters | |
513 ibuffer-saved-filters)) | |
514 (message "Not saved permanently: Customize not available")))) | |
515 | |
516 ;;;###autoload | |
517 (defun ibuffer-save-filters (name filters) | |
518 "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'. | |
519 Interactively, prompt for NAME, and use the current filters." | |
520 (interactive | |
521 (if (null ibuffer-filtering-qualifiers) | |
522 (error "No filters currently in effect") | |
523 (list | |
524 (read-from-minibuffer "Save current filters as: ") | |
525 ibuffer-filtering-qualifiers))) | |
526 (ibuffer-aif (assoc name ibuffer-saved-filters) | |
527 (setcdr it filters) | |
528 (push (list name filters) ibuffer-saved-filters)) | |
529 (ibuffer-maybe-save-saved-filters) | |
530 (ibuffer-update-mode-name)) | |
531 | |
532 ;;;###autoload | |
533 (defun ibuffer-delete-saved-filters (name) | |
534 "Delete saved filters with NAME from `ibuffer-saved-filters'." | |
535 (interactive | |
536 (list | |
537 (if (null ibuffer-saved-filters) | |
538 (error "No saved filters") | |
539 (completing-read "Delete saved filters: " | |
540 ibuffer-saved-filters nil t)))) | |
541 (setq ibuffer-saved-filters | |
542 (ibuffer-delete-alist name ibuffer-saved-filters)) | |
543 (ibuffer-maybe-save-saved-filters) | |
544 (ibuffer-update nil t)) | |
545 | |
546 ;;;###autoload | |
547 (defun ibuffer-add-saved-filters (name) | |
548 "Add saved filters from `ibuffer-saved-filters' to this buffer's filters." | |
549 (interactive | |
550 (list | |
551 (if (null ibuffer-saved-filters) | |
552 (error "No saved filters") | |
553 (completing-read "Add saved filters: " | |
554 ibuffer-saved-filters nil t)))) | |
555 (push (cons 'saved name) ibuffer-filtering-qualifiers) | |
556 (ibuffer-update nil t)) | |
557 | |
558 ;;;###autoload | |
559 (defun ibuffer-switch-to-saved-filters (name) | |
560 "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'. | |
561 If prefix argument ADD is non-nil, then add the saved filters instead | |
562 of replacing the current filters." | |
563 (interactive | |
564 (list | |
565 (if (null ibuffer-saved-filters) | |
566 (error "No saved filters") | |
567 (completing-read "Switch to saved filters: " | |
568 ibuffer-saved-filters nil t)))) | |
569 (setq ibuffer-filtering-qualifiers (list (cons 'saved name))) | |
570 (ibuffer-update nil t)) | |
571 | |
572 (defun ibuffer-format-qualifier (qualifier) | |
573 (if (eq (car-safe qualifier) 'not) | |
574 (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier)) "]") | |
575 (ibuffer-format-qualifier-1 qualifier))) | |
576 | |
577 (defun ibuffer-format-qualifier-1 (qualifier) | |
578 (case (car qualifier) | |
579 (saved | |
580 (concat " [filter: " (cdr qualifier) "]")) | |
581 (or | |
582 (concat " [OR" (mapconcat #'ibuffer-format-qualifier | |
583 (cdr qualifier) "") "]")) | |
584 (t | |
585 (let ((type (assq (car qualifier) ibuffer-filtering-alist))) | |
586 (unless qualifier | |
587 (error "Ibuffer: bad qualifier %s" qualifier)) | |
588 (concat " [" (cadr type) ": " (format "%s]" (cdr qualifier))))))) | |
589 | |
590 ;;; Extra operation definitions | |
591 | |
592 (define-ibuffer-filter mode | |
593 "Toggle current view to buffers with major mode QUALIFIER." | |
594 (:description "major mode" | |
595 :reader | |
596 (intern | |
597 (completing-read "Filter by major mode: " obarray | |
598 #'(lambda (e) | |
599 (string-match "-mode$" | |
600 (symbol-name e))) | |
601 t | |
602 (let ((buf (ibuffer-current-buffer))) | |
603 (if (and buf (buffer-live-p buf)) | |
604 (with-current-buffer buf | |
605 (symbol-name major-mode)) | |
606 ""))))) | |
607 (eq qualifier (with-current-buffer buf major-mode))) | |
608 | |
609 (define-ibuffer-filter name | |
610 "Toggle current view to buffers with name matching QUALIFIER." | |
611 (:description "buffer name" | |
612 :reader | |
613 (read-from-minibuffer "Filter by name (regexp): ")) | |
614 (string-match qualifier (buffer-name buf))) | |
615 | |
616 (define-ibuffer-filter filename | |
617 "Toggle current view to buffers with filename matching QUALIFIER." | |
618 (:description "filename" | |
619 :reader | |
620 (read-from-minibuffer "Filter by filename (regexp): ")) | |
621 (ibuffer-awhen (buffer-file-name buf) | |
622 (string-match qualifier it))) | |
623 | |
624 (define-ibuffer-filter size-gt | |
625 "Toggle current view to buffers with size greater than QUALIFIER." | |
626 (:description "size greater than" | |
627 :reader | |
628 (string-to-number (read-from-minibuffer "Filter by size greater than: "))) | |
629 (> (with-current-buffer buf (buffer-size)) | |
630 qualifier)) | |
631 | |
632 (define-ibuffer-filter size-lt | |
633 "Toggle current view to buffers with size less than QUALIFIER." | |
634 (:description "size less than" | |
635 :reader | |
636 (string-to-number (read-from-minibuffer "Filter by size less than: "))) | |
637 (< (with-current-buffer buf (buffer-size)) | |
638 qualifier)) | |
639 | |
640 (define-ibuffer-filter content | |
641 "Toggle current view to buffers whose contents match QUALIFIER." | |
642 (:description "content" | |
643 :reader | |
644 (read-from-minibuffer "Filter by content (regexp): ")) | |
645 (with-current-buffer buf | |
646 (save-excursion | |
647 (goto-char (point-min)) | |
648 (re-search-forward qualifier nil t)))) | |
649 | |
650 (define-ibuffer-filter predicate | |
651 "Toggle current view to buffers for which QUALIFIER returns non-nil." | |
652 (:description "predicate" | |
653 :reader | |
654 (read-minibuffer "Filter by predicate (form): ")) | |
655 (with-current-buffer buf | |
656 (eval qualifier))) | |
657 | |
658 ;;; Sorting | |
659 | |
660 ;;;###autoload | |
661 (defun ibuffer-toggle-sorting-mode () | |
662 "Toggle the current sorting mode. | |
43379
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
663 Default sorting modes are: |
42702 | 664 Recency - the last time the buffer was viewed |
665 Name - the name of the buffer | |
666 Major Mode - the name of the major mode of the buffer | |
667 Size - the size of the buffer" | |
668 (interactive) | |
43379
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
669 (let ((modes (mapcar 'car ibuffer-sorting-functions-alist))) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
670 (add-to-list 'modes 'recency) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
671 (setq modes (sort modes 'string-lessp)) |
44020
784c6f7e7ed3
(ibuffer-toggle-sorting-mode): Remove `find-if' so we don't require
Colin Walters <walters@gnu.org>
parents:
43769
diff
changeset
|
672 (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes))) |
43379
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
673 (car modes)))) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
674 (setq ibuffer-sorting-mode next) |
3fce95e542b1
(ibuffer-toggle-sorting-mode): Make it work. Patch from John Paul
Colin Walters <walters@gnu.org>
parents:
43102
diff
changeset
|
675 (message "Sorting by %s" next))) |
42702 | 676 (ibuffer-redisplay t)) |
677 | |
678 ;;;###autoload | |
679 (defun ibuffer-invert-sorting () | |
680 "Toggle whether or not sorting is in reverse order." | |
681 (interactive) | |
682 (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep)) | |
683 (message "Sorting order %s" | |
684 (if ibuffer-sorting-reversep | |
685 "reversed" | |
686 "normal")) | |
687 (ibuffer-redisplay t)) | |
688 | |
689 (define-ibuffer-sorter major-mode | |
690 "Sort the buffers by major modes. | |
691 Ordering is lexicographic." | |
692 (:description "major mode") | |
693 (string-lessp (downcase | |
694 (symbol-name (with-current-buffer | |
695 (car a) | |
696 major-mode))) | |
697 (downcase | |
698 (symbol-name (with-current-buffer | |
699 (car b) | |
700 major-mode))))) | |
701 | |
43567 | 702 (define-ibuffer-sorter mode-name |
703 "Sort the buffers by their mode name. | |
704 Ordering is lexicographic." | |
44186
9ea4e6199d1c
(sorter mode-name): Doc fix.
Colin Walters <walters@gnu.org>
parents:
44020
diff
changeset
|
705 (:description "major mode name") |
43567 | 706 (string-lessp (downcase |
44020
784c6f7e7ed3
(ibuffer-toggle-sorting-mode): Remove `find-if' so we don't require
Colin Walters <walters@gnu.org>
parents:
43769
diff
changeset
|
707 (with-current-buffer |
784c6f7e7ed3
(ibuffer-toggle-sorting-mode): Remove `find-if' so we don't require
Colin Walters <walters@gnu.org>
parents:
43769
diff
changeset
|
708 (car a) |
784c6f7e7ed3
(ibuffer-toggle-sorting-mode): Remove `find-if' so we don't require
Colin Walters <walters@gnu.org>
parents:
43769
diff
changeset
|
709 mode-name)) |
43567 | 710 (downcase |
44020
784c6f7e7ed3
(ibuffer-toggle-sorting-mode): Remove `find-if' so we don't require
Colin Walters <walters@gnu.org>
parents:
43769
diff
changeset
|
711 (with-current-buffer |
784c6f7e7ed3
(ibuffer-toggle-sorting-mode): Remove `find-if' so we don't require
Colin Walters <walters@gnu.org>
parents:
43769
diff
changeset
|
712 (car b) |
784c6f7e7ed3
(ibuffer-toggle-sorting-mode): Remove `find-if' so we don't require
Colin Walters <walters@gnu.org>
parents:
43769
diff
changeset
|
713 mode-name)))) |
43567 | 714 |
42702 | 715 (define-ibuffer-sorter alphabetic |
716 "Sort the buffers by their names. | |
717 Ordering is lexicographic." | |
718 (:description "buffer name") | |
719 (string-lessp | |
720 (buffer-name (car a)) | |
721 (buffer-name (car b)))) | |
722 | |
723 (define-ibuffer-sorter size | |
724 "Sort the buffers by their size." | |
725 (:description "size") | |
726 (< (with-current-buffer (car a) | |
727 (buffer-size)) | |
728 (with-current-buffer (car b) | |
729 (buffer-size)))) | |
730 | |
731 ;;; Functions to emulate bs.el | |
732 | |
733 ;;;###autoload | |
734 (defun ibuffer-bs-show () | |
735 "Emulate `bs-show' from the bs.el package." | |
736 (interactive) | |
737 (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t) | |
738 (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all)) | |
739 | |
740 (defun ibuffer-bs-toggle-all () | |
741 "Emulate `bs-toggle-show-all' from the bs.el package." | |
742 (interactive) | |
743 (if ibuffer-filtering-qualifiers | |
744 (ibuffer-pop-filter) | |
745 (progn (ibuffer-push-filter '(filename . ".*")) | |
746 (ibuffer-update nil t)))) | |
747 | |
748 ;;; Handy functions | |
749 | |
750 ;;;###autoload | |
751 (defun ibuffer-add-to-tmp-hide (regexp) | |
752 "Add REGEXP to `ibuffer-tmp-hide-regexps'. | |
753 This means that buffers whose name matches REGEXP will not be shown | |
754 for this ibuffer session." | |
755 (interactive | |
756 (list | |
757 (read-from-minibuffer "Never show buffers matching: " | |
758 (regexp-quote (buffer-name (ibuffer-current-buffer t)))))) | |
759 (push regexp ibuffer-tmp-hide-regexps)) | |
760 | |
761 ;;;###autoload | |
762 (defun ibuffer-add-to-tmp-show (regexp) | |
763 "Add REGEXP to `ibuffer-tmp-show-regexps'. | |
764 This means that buffers whose name matches REGEXP will always be shown | |
765 for this ibuffer session." | |
766 (interactive | |
767 (list | |
768 (read-from-minibuffer "Always show buffers matching: " | |
769 (regexp-quote (buffer-name (ibuffer-current-buffer t)))))) | |
770 (push regexp ibuffer-tmp-show-regexps)) | |
771 | |
772 ;;;###autoload | |
773 (defun ibuffer-forward-next-marked (&optional count mark direction) | |
774 "Move forward by COUNT marked buffers (default 1). | |
775 | |
776 If MARK is non-nil, it should be a character denoting the type of mark | |
777 to move by. The default is `ibuffer-marked-char'. | |
778 | |
779 If DIRECTION is non-nil, it should be an integer; negative integers | |
780 mean move backwards, non-negative integers mean move forwards." | |
781 (interactive "P") | |
782 (unless count | |
783 (setq count 1)) | |
784 (unless mark | |
785 (setq mark ibuffer-marked-char)) | |
786 (unless direction | |
787 (setq direction 1)) | |
788 ;; Skip the title | |
789 (ibuffer-forward-line 0) | |
790 (let ((opos (point)) | |
791 curmark) | |
792 (ibuffer-forward-line direction) | |
793 (while (not (or (= (point) opos) | |
794 (eq (setq curmark (ibuffer-current-mark)) | |
795 mark))) | |
796 (ibuffer-forward-line direction)) | |
797 (when (and (= (point) opos) | |
798 (not (eq (ibuffer-current-mark) mark))) | |
799 (error "No buffers with mark %c" mark)))) | |
800 | |
801 ;;;###autoload | |
802 (defun ibuffer-backwards-next-marked (&optional count mark) | |
803 "Move backwards by COUNT marked buffers (default 1). | |
804 | |
805 If MARK is non-nil, it should be a character denoting the type of mark | |
806 to move by. The default is `ibuffer-marked-char'." | |
807 (interactive "P") | |
808 (ibuffer-forward-next-marked count mark -1)) | |
809 | |
810 ;;;###autoload | |
811 (defun ibuffer-do-kill-lines () | |
812 "Hide all of the currently marked lines." | |
813 (interactive) | |
814 (if (= (ibuffer-count-marked-lines) 0) | |
815 (message "No buffers marked; use 'm' to mark a buffer") | |
816 (let ((count | |
817 (ibuffer-map-marked-lines | |
818 #'(lambda (buf mark beg end) | |
819 'kill)))) | |
820 (message "Killed %s lines" count)))) | |
821 | |
822 ;;;###autoload | |
823 (defun ibuffer-jump-to-buffer (name) | |
824 "Move point to the buffer whose name is NAME." | |
825 (interactive (list nil)) | |
826 (let ((table (mapcar #'(lambda (x) | |
827 (cons (buffer-name (car x)) | |
828 (caddr x))) | |
829 (ibuffer-current-state-list t)))) | |
830 (when (null table) | |
831 (error "No buffers!")) | |
832 (when (interactive-p) | |
833 (setq name (completing-read "Jump to buffer: " table nil t))) | |
834 (ibuffer-aif (assoc name table) | |
835 (goto-char (cdr it)) | |
836 (error "No buffer with name %s" name)))) | |
837 | |
838 ;;;###autoload | |
839 (defun ibuffer-diff-with-file () | |
840 "View the differences between this buffer and its associated file. | |
841 This requires the external program \"diff\" to be in your `exec-path'." | |
842 (interactive) | |
843 (let* ((buf (ibuffer-current-buffer)) | |
844 (buf-filename (with-current-buffer buf | |
845 buffer-file-name))) | |
846 (unless (buffer-live-p buf) | |
847 (error "Buffer %s has been killed" buf)) | |
848 (unless buf-filename | |
849 (error "Buffer %s has no associated file" buf)) | |
850 (let ((diff-buf (get-buffer-create "*Ibuffer-diff*"))) | |
851 (with-current-buffer diff-buf | |
852 (setq buffer-read-only nil) | |
853 (erase-buffer)) | |
854 (let ((tempfile (make-temp-file "ibuffer-diff-"))) | |
855 (unwind-protect | |
856 (progn | |
857 (with-current-buffer buf | |
858 (write-region (point-min) (point-max) tempfile nil 'nomessage)) | |
859 (if (zerop | |
860 (apply #'call-process "diff" nil diff-buf nil | |
861 (append | |
862 (when (and (boundp 'ediff-custom-diff-options) | |
863 (stringp ediff-custom-diff-options)) | |
864 (list ediff-custom-diff-options)) | |
865 (list buf-filename tempfile)))) | |
866 (message "No differences found") | |
867 (progn | |
868 (with-current-buffer diff-buf | |
869 (goto-char (point-min)) | |
870 (if (fboundp 'diff-mode) | |
871 (diff-mode) | |
872 (fundamental-mode))) | |
873 (display-buffer diff-buf)))) | |
874 (when (file-exists-p tempfile) | |
875 (delete-file tempfile))))) | |
876 nil)) | |
877 | |
878 ;;;###autoload | |
879 (defun ibuffer-copy-filename-as-kill (&optional arg) | |
880 "Copy filenames of marked buffers into the kill ring. | |
881 The names are separated by a space. | |
882 If a buffer has no filename, it is ignored. | |
883 With a zero prefix arg, use the complete pathname of each marked file. | |
884 | |
885 You can then feed the file name(s) to other commands with C-y. | |
886 | |
887 [ This docstring shamelessly stolen from the | |
888 `dired-copy-filename-as-kill' in \"dired-x\". ]" | |
889 ;; Add to docstring later: | |
890 ;; With C-u, use the relative pathname of each marked file. | |
891 (interactive "P") | |
892 (if (= (ibuffer-count-marked-lines) 0) | |
893 (message "No buffers marked; use 'm' to mark a buffer") | |
894 (let ((ibuffer-copy-filename-as-kill-result "") | |
895 (type (cond ((eql arg 0) | |
896 'full) | |
897 ;; ((eql arg 4) | |
898 ;; 'relative) | |
899 (t | |
900 'name)))) | |
901 (ibuffer-map-marked-lines | |
902 #'(lambda (buf mark beg end) | |
903 (setq ibuffer-copy-filename-as-kill-result | |
904 (concat ibuffer-copy-filename-as-kill-result | |
905 (let ((name (buffer-file-name buf))) | |
906 (if name | |
907 (case type | |
908 (full | |
909 name) | |
910 (t | |
911 (file-name-nondirectory name))) | |
912 "")) | |
913 " ")))) | |
914 (push ibuffer-copy-filename-as-kill-result kill-ring)))) | |
915 | |
916 (defun ibuffer-mark-on-buffer (func) | |
917 (let ((count | |
918 (ibuffer-map-lines | |
919 #'(lambda (buf mark beg end) | |
920 (when (funcall func buf) | |
921 (ibuffer-set-mark-1 ibuffer-marked-char) | |
922 t))))) | |
923 (ibuffer-redisplay t) | |
924 (message "Marked %s buffers" count))) | |
925 | |
926 ;;;###autoload | |
927 (defun ibuffer-mark-by-name-regexp (regexp) | |
928 "Mark all buffers whose name matches REGEXP." | |
929 (interactive "sMark by name (regexp): ") | |
930 (ibuffer-mark-on-buffer | |
931 #'(lambda (buf) | |
932 (string-match regexp (buffer-name buf))))) | |
933 | |
934 ;;;###autoload | |
935 (defun ibuffer-mark-by-mode-regexp (regexp) | |
936 "Mark all buffers whose major mode matches REGEXP." | |
937 (interactive "sMark by major mode (regexp): ") | |
938 (ibuffer-mark-on-buffer | |
939 #'(lambda (buf) | |
940 (with-current-buffer buf | |
941 (string-match regexp mode-name))))) | |
942 | |
943 ;;;###autoload | |
944 (defun ibuffer-mark-by-file-name-regexp (regexp) | |
945 "Mark all buffers whose file name matches REGEXP." | |
946 (interactive "sMark by file name (regexp): ") | |
947 (ibuffer-mark-on-buffer | |
948 #'(lambda (buf) | |
949 (let ((name (or (buffer-file-name buf) | |
950 (with-current-buffer buf | |
951 (and | |
952 (boundp 'dired-directory) | |
953 (stringp dired-directory) | |
954 dired-directory))))) | |
955 (when name | |
956 (string-match regexp name)))))) | |
957 | |
958 ;;;###autoload | |
959 (defun ibuffer-mark-by-mode (mode) | |
960 "Mark all buffers whose major mode equals MODE." | |
961 (interactive | |
962 (list (intern (completing-read "Mark by major mode: " obarray | |
963 #'(lambda (e) | |
964 ;; kind of a hack... | |
965 (and (fboundp e) | |
966 (string-match "-mode$" | |
967 (symbol-name e)))) | |
968 t | |
969 (let ((buf (ibuffer-current-buffer))) | |
970 (if (and buf (buffer-live-p buf)) | |
971 (with-current-buffer buf | |
972 (cons (symbol-name major-mode) | |
973 0)) | |
974 "")))))) | |
975 (ibuffer-mark-on-buffer | |
976 #'(lambda (buf) | |
977 (with-current-buffer buf | |
978 (eq major-mode mode))))) | |
979 | |
980 ;;;###autoload | |
981 (defun ibuffer-mark-modified-buffers () | |
982 "Mark all modified buffers." | |
983 (interactive) | |
984 (ibuffer-mark-on-buffer | |
985 #'(lambda (buf) (buffer-modified-p buf)))) | |
986 | |
987 ;;;###autoload | |
988 (defun ibuffer-mark-unsaved-buffers () | |
989 "Mark all modified buffers that have an associated file." | |
990 (interactive) | |
991 (ibuffer-mark-on-buffer | |
992 #'(lambda (buf) (and (with-current-buffer buf buffer-file-name) | |
993 (buffer-modified-p buf))))) | |
994 | |
995 ;;;###autoload | |
996 (defun ibuffer-mark-dissociated-buffers () | |
997 "Mark all buffers whose associated file does not exist." | |
998 (interactive) | |
999 (ibuffer-mark-on-buffer | |
1000 #'(lambda (buf) | |
1001 (with-current-buffer buf | |
1002 (or | |
1003 (and buffer-file-name | |
1004 (not (file-exists-p buffer-file-name))) | |
1005 (and (eq major-mode 'dired-mode) | |
1006 (boundp 'dired-directory) | |
1007 (stringp dired-directory) | |
1008 (not (file-exists-p (file-name-directory dired-directory))))))))) | |
1009 | |
1010 ;;;###autoload | |
1011 (defun ibuffer-mark-help-buffers () | |
1012 "Mark buffers like *Help*, *Apropos*, *Info*." | |
1013 (interactive) | |
1014 (ibuffer-mark-on-buffer | |
1015 #'(lambda (buf) | |
1016 (with-current-buffer buf | |
44186
9ea4e6199d1c
(sorter mode-name): Doc fix.
Colin Walters <walters@gnu.org>
parents:
44020
diff
changeset
|
1017 (memq major-mode ibuffer-help-buffer-modes))))) |
42702 | 1018 |
1019 ;;;###autoload | |
1020 (defun ibuffer-mark-old-buffers () | |
1021 "Mark buffers which have not been viewed in `ibuffer-old-time' days." | |
1022 (interactive) | |
1023 (ibuffer-mark-on-buffer | |
1024 #'(lambda (buf) | |
1025 (with-current-buffer buf | |
1026 ;; hacked from midnight.el | |
1027 (when buffer-display-time | |
1028 (let* ((tm (current-time)) | |
1029 (now (+ (* (float (ash 1 16)) (car tm)) | |
1030 (float (cadr tm)) (* 0.0000001 (caddr tm)))) | |
1031 (then (+ (* (float (ash 1 16)) | |
1032 (car buffer-display-time)) | |
1033 (float (cadr buffer-display-time)) | |
1034 (* 0.0000001 (caddr buffer-display-time))))) | |
43490
a08f4763cba4
(ibuffer-old-time): Change to hours.
Colin Walters <walters@gnu.org>
parents:
43379
diff
changeset
|
1035 (> (- now then) (* 60 60 ibuffer-old-time)))))))) |
42702 | 1036 |
1037 ;;;###autoload | |
1038 (defun ibuffer-mark-special-buffers () | |
1039 "Mark all buffers whose name begins and ends with '*'." | |
1040 (interactive) | |
1041 (ibuffer-mark-on-buffer | |
1042 #'(lambda (buf) (string-match "^\\*.+\\*$" | |
1043 (buffer-name buf))))) | |
1044 | |
1045 ;;;###autoload | |
1046 (defun ibuffer-mark-read-only-buffers () | |
1047 "Mark all read-only buffers." | |
1048 (interactive) | |
1049 (ibuffer-mark-on-buffer | |
1050 #'(lambda (buf) | |
1051 (with-current-buffer buf | |
1052 buffer-read-only)))) | |
1053 | |
1054 ;;;###autoload | |
1055 (defun ibuffer-mark-dired-buffers () | |
1056 "Mark all `dired' buffers." | |
1057 (interactive) | |
1058 (ibuffer-mark-on-buffer | |
1059 #'(lambda (buf) | |
1060 (with-current-buffer buf | |
1061 (eq major-mode 'dired-mode))))) | |
1062 | |
1063 ;;; An implementation of multi-buffer `occur' | |
1064 | |
1065 (defvar ibuffer-occur-props nil) | |
1066 | |
1067 (define-derived-mode ibuffer-occur-mode occur-mode "Ibuffer-Occur" | |
1068 "A special form of Occur mode for multiple buffers. | |
1069 Note this major mode is not meant for interactive use! | |
1070 See also `occur-mode'." | |
1071 (define-key ibuffer-occur-mode-map (kbd "n") 'forward-line) | |
1072 (define-key ibuffer-occur-mode-map (kbd "q") 'bury-buffer) | |
1073 (define-key ibuffer-occur-mode-map (kbd "p") 'previous-line) | |
1074 (define-key ibuffer-occur-mode-map (kbd "RET") 'ibuffer-occur-display-occurence) | |
1075 (define-key ibuffer-occur-mode-map (kbd "f") 'ibuffer-occur-goto-occurence) | |
1076 (define-key ibuffer-occur-mode-map [(mouse-2)] 'ibuffer-occur-mouse-display-occurence) | |
1077 (set (make-local-variable 'revert-buffer-function) | |
1078 #'ibuffer-occur-revert-buffer-function) | |
1079 (set (make-local-variable 'ibuffer-occur-props) nil) | |
1080 (setq buffer-read-only nil) | |
1081 (erase-buffer) | |
1082 (setq buffer-read-only t) | |
1083 (message (concat | |
1084 "Use RET " | |
1085 (if (or (and (< 21 emacs-major-version) | |
1086 window-system) | |
1087 (featurep 'mouse)) | |
1088 "or mouse-2 ") | |
1089 "to display an occurence."))) | |
1090 | |
1091 (defun ibuffer-occur-mouse-display-occurence (e) | |
1092 "Display occurence on this line in another window." | |
1093 (interactive "e") | |
1094 (let* ((occurbuf (save-window-excursion (mouse-select-window e) | |
1095 (selected-window))) | |
1096 (target (with-current-buffer occurbuf | |
1097 (get-text-property (save-excursion | |
1098 (mouse-set-point e) | |
1099 (point)) | |
1100 'ibuffer-occur-target)))) | |
1101 (unless target | |
1102 (error "No occurence on this line")) | |
1103 (let ((buf (car target)) | |
1104 (line (cdr target))) | |
1105 (switch-to-buffer occurbuf) | |
1106 (delete-other-windows) | |
1107 (pop-to-buffer buf) | |
1108 (goto-line line)))) | |
1109 | |
1110 (defun ibuffer-occur-goto-occurence () | |
1111 "Switch to the buffer which has the occurence on this line." | |
1112 (interactive) | |
1113 (ibuffer-occur-display-occurence t)) | |
1114 | |
1115 (defun ibuffer-occur-display-occurence (&optional goto) | |
1116 "Display occurence on this line in another window." | |
1117 (interactive "P") | |
1118 (let ((target (get-text-property (point) 'ibuffer-occur-target))) | |
1119 (unless target | |
1120 (error "No occurence on this line")) | |
1121 (let ((buf (car target)) | |
1122 (line (cdr target))) | |
1123 (delete-other-windows) | |
1124 (if goto | |
1125 (switch-to-buffer buf) | |
1126 (pop-to-buffer buf)) | |
1127 (goto-line line)))) | |
1128 | |
1129 ;;;###autoload | |
1130 (defun ibuffer-do-occur (regexp &optional nlines) | |
1131 "View lines which match REGEXP in all marked buffers. | |
1132 Optional argument NLINES says how many lines of context to display: it | |
1133 defaults to one." | |
1134 (interactive | |
1135 (list (let* ((default (car regexp-history)) | |
1136 (input | |
1137 (read-from-minibuffer | |
1138 (if default | |
1139 (format "List lines matching regexp (default `%s'): " | |
1140 default) | |
1141 "List lines matching regexp: ") | |
1142 nil | |
1143 nil | |
1144 nil | |
1145 'regexp-history))) | |
1146 (if (equal input "") | |
1147 default | |
1148 input)) | |
1149 current-prefix-arg)) | |
1150 (if (or (not (integerp nlines)) | |
1151 (< nlines 0)) | |
1152 (setq nlines 1)) | |
1153 (when (zerop (ibuffer-count-marked-lines)) | |
43567 | 1154 (ibuffer-set-mark ibuffer-marked-char)) |
42702 | 1155 (let ((ibuffer-do-occur-bufs nil)) |
1156 ;; Accumulate a list of marked buffers | |
1157 (ibuffer-map-marked-lines | |
1158 #'(lambda (buf mark beg end) | |
1159 (push buf ibuffer-do-occur-bufs))) | |
1160 (ibuffer-do-occur-1 regexp ibuffer-do-occur-bufs | |
1161 (get-buffer-create "*Ibuffer-occur*") | |
1162 nlines))) | |
1163 | |
1164 (defun ibuffer-do-occur-1 (regexp buffers out-buf nlines) | |
1165 (let ((count (ibuffer-occur-engine regexp buffers out-buf nlines))) | |
1166 (if (> count 0) | |
1167 (progn | |
1168 (switch-to-buffer out-buf) | |
1169 (setq buffer-read-only t) | |
1170 (delete-other-windows) | |
1171 (goto-char (point-min)) | |
1172 (message "Found %s matches in %s buffers" count (length buffers))) | |
1173 (message "No matches found")))) | |
1174 | |
1175 | |
1176 (defun ibuffer-occur-revert-buffer-function (ignore-auto noconfirm) | |
1177 "Update the *Ibuffer occur* buffer." | |
1178 (assert (eq major-mode 'ibuffer-occur-mode)) | |
1179 (ibuffer-do-occur-1 (car ibuffer-occur-props) | |
1180 (cadr ibuffer-occur-props) | |
1181 (current-buffer) | |
1182 (caddr ibuffer-occur-props))) | |
1183 | |
1184 (defun ibuffer-occur-engine (regexp buffers out-buf nlines) | |
1185 (macrolet ((insert-get-point | |
1186 (&rest args) | |
1187 `(progn | |
1188 (insert ,@args) | |
1189 (point))) | |
1190 (maybe-put-overlay | |
1191 (over prop value) | |
1192 `(when (ibuffer-use-fontification) | |
1193 (overlay-put ,over ,prop ,value))) | |
1194 (maybe-ibuffer-propertize | |
1195 (obj &rest args) | |
1196 (let ((objsym (gensym "--maybe-ibuffer-propertize-"))) | |
1197 `(let ((,objsym ,obj)) | |
1198 (if (ibuffer-use-fontification) | |
1199 (propertize ,objsym ,@args) | |
1200 ,objsym))))) | |
1201 (with-current-buffer out-buf | |
1202 (ibuffer-occur-mode) | |
1203 (setq buffer-read-only nil) | |
1204 (let ((globalcount 0)) | |
1205 ;; Map over all the buffers | |
1206 (dolist (buf buffers) | |
1207 (when (buffer-live-p buf) | |
1208 (let ((c 0) ;; count of matched lines | |
1209 (l 1) ;; line count | |
1210 (headerpt (with-current-buffer out-buf (point)))) | |
1211 (save-excursion | |
1212 (set-buffer buf) | |
1213 (save-excursion | |
1214 (goto-char (point-min)) ;; begin searching in the buffer | |
1215 (while (not (eobp)) | |
1216 ;; The line we're matching against | |
1217 (let ((curline (buffer-substring | |
1218 (line-beginning-position) | |
1219 (line-end-position)))) | |
1220 (when (string-match regexp curline) | |
1221 (incf c) ;; increment match count | |
1222 (incf globalcount) | |
1223 ;; Depropertize the string, and maybe highlight the matches | |
1224 (setq curline | |
1225 (progn | |
1226 (ibuffer-depropertize-string curline t) | |
1227 (when (ibuffer-use-fontification) | |
1228 (let ((len (length curline)) | |
1229 (start 0)) | |
1230 (while (and (< start len) | |
1231 (string-match regexp curline start)) | |
1232 (put-text-property (match-beginning 0) | |
1233 (match-end 0) | |
1234 'face ibuffer-occur-match-face | |
1235 curline) | |
1236 (setq start (match-end 0))))) | |
1237 curline)) | |
1238 ;; Generate the string to insert for this match | |
1239 (let ((data | |
1240 (if (= nlines 1) | |
1241 ;; The simple display style | |
1242 (concat (maybe-ibuffer-propertize | |
1243 (format "%-6d:" l) | |
1244 'face 'bold) | |
1245 curline | |
1246 "\n") | |
1247 ;; The complex multi-line display style | |
1248 (let ((prevlines (nreverse | |
1249 (ibuffer-accumulate-lines (- nlines)))) | |
1250 (nextlines (ibuffer-accumulate-lines nlines)) | |
1251 ;; The lack of `flet' seriously sucks. | |
1252 (fun #'(lambda (lines) | |
1253 (mapcar | |
1254 #'(lambda (line) | |
1255 (concat " :" line "\n")) | |
1256 lines)))) | |
1257 (setq prevlines (funcall fun prevlines)) | |
1258 (setq nextlines (funcall fun nextlines)) | |
1259 ;; Yes, I am trying to win the award for the | |
1260 ;; most consing. | |
1261 (apply #'concat | |
1262 (nconc | |
1263 prevlines | |
1264 (list | |
1265 (concat | |
1266 (maybe-ibuffer-propertize | |
1267 (format "%-6d" l) | |
1268 'face 'bold) | |
1269 ":" | |
1270 curline | |
1271 "\n")) | |
1272 nextlines)))))) | |
1273 ;; Actually insert the match display data | |
1274 (with-current-buffer out-buf | |
1275 (let ((beg (point)) | |
1276 (end (insert-get-point | |
1277 data))) | |
1278 (unless (= nlines 1) | |
1279 (insert "-------\n")) | |
1280 (put-text-property | |
1281 beg (1- end) 'ibuffer-occur-target (cons buf l)) | |
1282 (put-text-property | |
1283 beg (1- end) 'mouse-face 'highlight)))))) | |
1284 ;; On to the next line... | |
1285 (incf l) | |
1286 (forward-line 1)))) | |
1287 (when (not (zerop c)) ;; is the count zero? | |
1288 (with-current-buffer out-buf | |
1289 (goto-char headerpt) | |
1290 (let ((beg (point)) | |
1291 (end (insert-get-point | |
1292 (format "%d lines matching \"%s\" in buffer %s\n" | |
1293 c regexp (buffer-name buf))))) | |
1294 (let ((o (make-overlay beg end))) | |
1295 (maybe-put-overlay o 'face 'underline))) | |
1296 (goto-char (point-max))))))) | |
1297 (setq ibuffer-occur-props (list regexp buffers nlines)) | |
1298 ;; Return the number of matches | |
1299 globalcount)))) | |
1300 | |
1301 (provide 'ibuf-ext) | |
1302 | |
1303 ;;; ibuf-ext.el ends here |