Commit | Line | Data |
---|---|---|
cea3be93 | 1 | ;;; ibuf-ext.el --- extensions for ibuffer -*-byte-compile-dynamic: t;-*- |
25d2f683 | 2 | |
30c93a61 | 3 | ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. |
25d2f683 CW |
4 | |
5 | ;; Author: Colin Walters <walters@verbum.org> | |
6 | ;; Created: 2 Dec 2001 | |
25d2f683 CW |
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 | ||
d98be487 CW |
136 | (defcustom ibuffer-filter-format-alist nil |
137 | "An alist which has special formats used when a filter is active. | |
138 | The contents of this variable should look like: | |
139 | ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...) | |
140 | ||
141 | For example, suppose that when you add a filter for buffers whose | |
142 | major mode is `emacs-lisp-mode', you only want to see the mark and the | |
143 | name of the buffer. You could accomplish that by adding: | |
144 | (mode ((mark \" \" name))) | |
6d63dcf5 CW |
145 | to this variable." |
146 | :type '(repeat (list :tag "Association" (symbol :tag "Filter") | |
147 | (list :tag "Formats" (repeat (sexp :tag "Format"))))) | |
148 | :group 'ibuffer) | |
d98be487 CW |
149 | |
150 | (defvar ibuffer-cached-filter-formats nil) | |
151 | (defvar ibuffer-compiled-filter-formats nil) | |
152 | ||
2c1bb3d3 CW |
153 | (defcustom ibuffer-old-time 72 |
154 | "The number of hours before a buffer is considered \"old\"." | |
155 | :type '(choice (const :tag "72 hours (3 days)" 72) | |
156 | (const :tag "48 hours (2 days)" 48) | |
157 | (const :tag "24 hours (1 day)" 24) | |
158 | (integer :tag "hours")) | |
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
323 | (define-ibuffer-op query-replace (&rest args) |
324 | "Perform a `query-replace' in marked buffers." | |
325 | (:interactive | |
326 | (query-replace-read-args "Query replace" t) | |
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 | ||
25d2f683 CW |
338 | (define-ibuffer-op query-replace-regexp (&rest args) |
339 | "Perform a `query-replace-regexp' in marked buffers." | |
340 | (:interactive | |
341 | (query-replace-read-args "Query replace regexp" t) | |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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 | ||
25d2f683 CW |
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. | |
13e14c51 | 663 | Default sorting modes are: |
25d2f683 CW |
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) | |
13e14c51 CW |
669 | (let ((modes (mapcar 'car ibuffer-sorting-functions-alist))) |
670 | (add-to-list 'modes 'recency) | |
671 | (setq modes (sort modes 'string-lessp)) | |
672 | (let ((next (or (find-if | |
673 | (lambda (x) (string-lessp ibuffer-sorting-mode x)) modes) | |
674 | (car modes)))) | |
675 | (setq ibuffer-sorting-mode next) | |
676 | (message "Sorting by %s" next))) | |
25d2f683 CW |
677 | (ibuffer-redisplay t)) |
678 | ||
679 | ;;;###autoload | |
680 | (defun ibuffer-invert-sorting () | |
681 | "Toggle whether or not sorting is in reverse order." | |
682 | (interactive) | |
683 | (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep)) | |
684 | (message "Sorting order %s" | |
685 | (if ibuffer-sorting-reversep | |
686 | "reversed" | |
687 | "normal")) | |
688 | (ibuffer-redisplay t)) | |
689 | ||
25d2f683 CW |
690 | (define-ibuffer-sorter major-mode |
691 | "Sort the buffers by major modes. | |
692 | Ordering is lexicographic." | |
693 | (:description "major mode") | |
694 | (string-lessp (downcase | |
695 | (symbol-name (with-current-buffer | |
696 | (car a) | |
697 | major-mode))) | |
698 | (downcase | |
699 | (symbol-name (with-current-buffer | |
700 | (car b) | |
701 | major-mode))))) | |
702 | ||
10cf9a43 CW |
703 | (define-ibuffer-sorter mode-name |
704 | "Sort the buffers by their mode name. | |
705 | Ordering is lexicographic." | |
706 | (:description "mode name") | |
707 | (string-lessp (downcase | |
708 | (symbol-name (with-current-buffer | |
709 | (car a) | |
710 | mode-name))) | |
711 | (downcase | |
712 | (symbol-name (with-current-buffer | |
713 | (car b) | |
714 | mode-name))))) | |
715 | ||
25d2f683 CW |
716 | (define-ibuffer-sorter alphabetic |
717 | "Sort the buffers by their names. | |
718 | Ordering is lexicographic." | |
719 | (:description "buffer name") | |
720 | (string-lessp | |
721 | (buffer-name (car a)) | |
722 | (buffer-name (car b)))) | |
723 | ||
25d2f683 CW |
724 | (define-ibuffer-sorter size |
725 | "Sort the buffers by their size." | |
726 | (:description "size") | |
727 | (< (with-current-buffer (car a) | |
728 | (buffer-size)) | |
729 | (with-current-buffer (car b) | |
730 | (buffer-size)))) | |
731 | ||
732 | ;;; Functions to emulate bs.el | |
733 | ||
734 | ;;;###autoload | |
735 | (defun ibuffer-bs-show () | |
736 | "Emulate `bs-show' from the bs.el package." | |
737 | (interactive) | |
738 | (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t) | |
739 | (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all)) | |
740 | ||
741 | (defun ibuffer-bs-toggle-all () | |
742 | "Emulate `bs-toggle-show-all' from the bs.el package." | |
743 | (interactive) | |
744 | (if ibuffer-filtering-qualifiers | |
745 | (ibuffer-pop-filter) | |
746 | (progn (ibuffer-push-filter '(filename . ".*")) | |
747 | (ibuffer-update nil t)))) | |
748 | ||
749 | ;;; Handy functions | |
750 | ||
751 | ;;;###autoload | |
752 | (defun ibuffer-add-to-tmp-hide (regexp) | |
753 | "Add REGEXP to `ibuffer-tmp-hide-regexps'. | |
754 | This means that buffers whose name matches REGEXP will not be shown | |
755 | for this ibuffer session." | |
756 | (interactive | |
757 | (list | |
758 | (read-from-minibuffer "Never show buffers matching: " | |
759 | (regexp-quote (buffer-name (ibuffer-current-buffer t)))))) | |
760 | (push regexp ibuffer-tmp-hide-regexps)) | |
761 | ||
762 | ;;;###autoload | |
763 | (defun ibuffer-add-to-tmp-show (regexp) | |
764 | "Add REGEXP to `ibuffer-tmp-show-regexps'. | |
765 | This means that buffers whose name matches REGEXP will always be shown | |
766 | for this ibuffer session." | |
767 | (interactive | |
768 | (list | |
769 | (read-from-minibuffer "Always show buffers matching: " | |
770 | (regexp-quote (buffer-name (ibuffer-current-buffer t)))))) | |
771 | (push regexp ibuffer-tmp-show-regexps)) | |
772 | ||
773 | ;;;###autoload | |
774 | (defun ibuffer-forward-next-marked (&optional count mark direction) | |
775 | "Move forward by COUNT marked buffers (default 1). | |
776 | ||
777 | If MARK is non-nil, it should be a character denoting the type of mark | |
778 | to move by. The default is `ibuffer-marked-char'. | |
779 | ||
780 | If DIRECTION is non-nil, it should be an integer; negative integers | |
781 | mean move backwards, non-negative integers mean move forwards." | |
782 | (interactive "P") | |
783 | (unless count | |
784 | (setq count 1)) | |
785 | (unless mark | |
786 | (setq mark ibuffer-marked-char)) | |
787 | (unless direction | |
788 | (setq direction 1)) | |
789 | ;; Skip the title | |
790 | (ibuffer-forward-line 0) | |
791 | (let ((opos (point)) | |
792 | curmark) | |
793 | (ibuffer-forward-line direction) | |
794 | (while (not (or (= (point) opos) | |
795 | (eq (setq curmark (ibuffer-current-mark)) | |
796 | mark))) | |
797 | (ibuffer-forward-line direction)) | |
798 | (when (and (= (point) opos) | |
799 | (not (eq (ibuffer-current-mark) mark))) | |
800 | (error "No buffers with mark %c" mark)))) | |
801 | ||
802 | ;;;###autoload | |
803 | (defun ibuffer-backwards-next-marked (&optional count mark) | |
804 | "Move backwards by COUNT marked buffers (default 1). | |
805 | ||
806 | If MARK is non-nil, it should be a character denoting the type of mark | |
807 | to move by. The default is `ibuffer-marked-char'." | |
808 | (interactive "P") | |
809 | (ibuffer-forward-next-marked count mark -1)) | |
810 | ||
811 | ;;;###autoload | |
812 | (defun ibuffer-do-kill-lines () | |
813 | "Hide all of the currently marked lines." | |
814 | (interactive) | |
815 | (if (= (ibuffer-count-marked-lines) 0) | |
816 | (message "No buffers marked; use 'm' to mark a buffer") | |
817 | (let ((count | |
818 | (ibuffer-map-marked-lines | |
819 | #'(lambda (buf mark beg end) | |
820 | 'kill)))) | |
821 | (message "Killed %s lines" count)))) | |
822 | ||
823 | ;;;###autoload | |
824 | (defun ibuffer-jump-to-buffer (name) | |
825 | "Move point to the buffer whose name is NAME." | |
826 | (interactive (list nil)) | |
827 | (let ((table (mapcar #'(lambda (x) | |
828 | (cons (buffer-name (car x)) | |
829 | (caddr x))) | |
830 | (ibuffer-current-state-list t)))) | |
831 | (when (null table) | |
832 | (error "No buffers!")) | |
833 | (when (interactive-p) | |
834 | (setq name (completing-read "Jump to buffer: " table nil t))) | |
835 | (ibuffer-aif (assoc name table) | |
836 | (goto-char (cdr it)) | |
837 | (error "No buffer with name %s" name)))) | |
838 | ||
839 | ;;;###autoload | |
840 | (defun ibuffer-diff-with-file () | |
841 | "View the differences between this buffer and its associated file. | |
842 | This requires the external program \"diff\" to be in your `exec-path'." | |
843 | (interactive) | |
844 | (let* ((buf (ibuffer-current-buffer)) | |
845 | (buf-filename (with-current-buffer buf | |
846 | buffer-file-name))) | |
847 | (unless (buffer-live-p buf) | |
848 | (error "Buffer %s has been killed" buf)) | |
849 | (unless buf-filename | |
850 | (error "Buffer %s has no associated file" buf)) | |
851 | (let ((diff-buf (get-buffer-create "*Ibuffer-diff*"))) | |
852 | (with-current-buffer diff-buf | |
853 | (setq buffer-read-only nil) | |
854 | (erase-buffer)) | |
855 | (let ((tempfile (make-temp-file "ibuffer-diff-"))) | |
856 | (unwind-protect | |
857 | (progn | |
858 | (with-current-buffer buf | |
859 | (write-region (point-min) (point-max) tempfile nil 'nomessage)) | |
860 | (if (zerop | |
861 | (apply #'call-process "diff" nil diff-buf nil | |
862 | (append | |
863 | (when (and (boundp 'ediff-custom-diff-options) | |
864 | (stringp ediff-custom-diff-options)) | |
865 | (list ediff-custom-diff-options)) | |
866 | (list buf-filename tempfile)))) | |
867 | (message "No differences found") | |
868 | (progn | |
869 | (with-current-buffer diff-buf | |
870 | (goto-char (point-min)) | |
871 | (if (fboundp 'diff-mode) | |
872 | (diff-mode) | |
873 | (fundamental-mode))) | |
874 | (display-buffer diff-buf)))) | |
875 | (when (file-exists-p tempfile) | |
876 | (delete-file tempfile))))) | |
877 | nil)) | |
878 | ||
879 | ;;;###autoload | |
880 | (defun ibuffer-copy-filename-as-kill (&optional arg) | |
881 | "Copy filenames of marked buffers into the kill ring. | |
882 | The names are separated by a space. | |
883 | If a buffer has no filename, it is ignored. | |
884 | With a zero prefix arg, use the complete pathname of each marked file. | |
885 | ||
886 | You can then feed the file name(s) to other commands with C-y. | |
887 | ||
888 | [ This docstring shamelessly stolen from the | |
889 | `dired-copy-filename-as-kill' in \"dired-x\". ]" | |
890 | ;; Add to docstring later: | |
891 | ;; With C-u, use the relative pathname of each marked file. | |
892 | (interactive "P") | |
893 | (if (= (ibuffer-count-marked-lines) 0) | |
894 | (message "No buffers marked; use 'm' to mark a buffer") | |
895 | (let ((ibuffer-copy-filename-as-kill-result "") | |
896 | (type (cond ((eql arg 0) | |
897 | 'full) | |
898 | ;; ((eql arg 4) | |
899 | ;; 'relative) | |
900 | (t | |
901 | 'name)))) | |
902 | (ibuffer-map-marked-lines | |
903 | #'(lambda (buf mark beg end) | |
904 | (setq ibuffer-copy-filename-as-kill-result | |
905 | (concat ibuffer-copy-filename-as-kill-result | |
906 | (let ((name (buffer-file-name buf))) | |
907 | (if name | |
908 | (case type | |
909 | (full | |
910 | name) | |
911 | (t | |
912 | (file-name-nondirectory name))) | |
913 | "")) | |
914 | " ")))) | |
915 | (push ibuffer-copy-filename-as-kill-result kill-ring)))) | |
916 | ||
917 | (defun ibuffer-mark-on-buffer (func) | |
918 | (let ((count | |
919 | (ibuffer-map-lines | |
920 | #'(lambda (buf mark beg end) | |
921 | (when (funcall func buf) | |
922 | (ibuffer-set-mark-1 ibuffer-marked-char) | |
923 | t))))) | |
924 | (ibuffer-redisplay t) | |
925 | (message "Marked %s buffers" count))) | |
926 | ||
927 | ;;;###autoload | |
928 | (defun ibuffer-mark-by-name-regexp (regexp) | |
929 | "Mark all buffers whose name matches REGEXP." | |
930 | (interactive "sMark by name (regexp): ") | |
931 | (ibuffer-mark-on-buffer | |
932 | #'(lambda (buf) | |
933 | (string-match regexp (buffer-name buf))))) | |
934 | ||
935 | ;;;###autoload | |
936 | (defun ibuffer-mark-by-mode-regexp (regexp) | |
937 | "Mark all buffers whose major mode matches REGEXP." | |
938 | (interactive "sMark by major mode (regexp): ") | |
939 | (ibuffer-mark-on-buffer | |
940 | #'(lambda (buf) | |
941 | (with-current-buffer buf | |
942 | (string-match regexp mode-name))))) | |
943 | ||
944 | ;;;###autoload | |
945 | (defun ibuffer-mark-by-file-name-regexp (regexp) | |
946 | "Mark all buffers whose file name matches REGEXP." | |
947 | (interactive "sMark by file name (regexp): ") | |
948 | (ibuffer-mark-on-buffer | |
949 | #'(lambda (buf) | |
950 | (let ((name (or (buffer-file-name buf) | |
951 | (with-current-buffer buf | |
952 | (and | |
953 | (boundp 'dired-directory) | |
954 | (stringp dired-directory) | |
955 | dired-directory))))) | |
956 | (when name | |
957 | (string-match regexp name)))))) | |
958 | ||
959 | ;;;###autoload | |
960 | (defun ibuffer-mark-by-mode (mode) | |
961 | "Mark all buffers whose major mode equals MODE." | |
962 | (interactive | |
963 | (list (intern (completing-read "Mark by major mode: " obarray | |
964 | #'(lambda (e) | |
965 | ;; kind of a hack... | |
966 | (and (fboundp e) | |
967 | (string-match "-mode$" | |
968 | (symbol-name e)))) | |
969 | t | |
970 | (let ((buf (ibuffer-current-buffer))) | |
971 | (if (and buf (buffer-live-p buf)) | |
972 | (with-current-buffer buf | |
973 | (cons (symbol-name major-mode) | |
974 | 0)) | |
975 | "")))))) | |
976 | (ibuffer-mark-on-buffer | |
977 | #'(lambda (buf) | |
978 | (with-current-buffer buf | |
979 | (eq major-mode mode))))) | |
980 | ||
981 | ;;;###autoload | |
982 | (defun ibuffer-mark-modified-buffers () | |
983 | "Mark all modified buffers." | |
984 | (interactive) | |
985 | (ibuffer-mark-on-buffer | |
986 | #'(lambda (buf) (buffer-modified-p buf)))) | |
987 | ||
988 | ;;;###autoload | |
989 | (defun ibuffer-mark-unsaved-buffers () | |
990 | "Mark all modified buffers that have an associated file." | |
991 | (interactive) | |
992 | (ibuffer-mark-on-buffer | |
993 | #'(lambda (buf) (and (with-current-buffer buf buffer-file-name) | |
994 | (buffer-modified-p buf))))) | |
995 | ||
996 | ;;;###autoload | |
997 | (defun ibuffer-mark-dissociated-buffers () | |
998 | "Mark all buffers whose associated file does not exist." | |
999 | (interactive) | |
1000 | (ibuffer-mark-on-buffer | |
1001 | #'(lambda (buf) | |
1002 | (with-current-buffer buf | |
1003 | (or | |
1004 | (and buffer-file-name | |
1005 | (not (file-exists-p buffer-file-name))) | |
1006 | (and (eq major-mode 'dired-mode) | |
1007 | (boundp 'dired-directory) | |
1008 | (stringp dired-directory) | |
1009 | (not (file-exists-p (file-name-directory dired-directory))))))))) | |
1010 | ||
1011 | ;;;###autoload | |
1012 | (defun ibuffer-mark-help-buffers () | |
1013 | "Mark buffers like *Help*, *Apropos*, *Info*." | |
1014 | (interactive) | |
1015 | (ibuffer-mark-on-buffer | |
1016 | #'(lambda (buf) | |
1017 | (with-current-buffer buf | |
1018 | (or | |
1019 | (eq major-mode 'apropos-mode) | |
1020 | (eq major-mode 'help-mode) | |
1021 | (eq major-mode 'info-mode)))))) | |
1022 | ||
1023 | ;;;###autoload | |
1024 | (defun ibuffer-mark-old-buffers () | |
1025 | "Mark buffers which have not been viewed in `ibuffer-old-time' days." | |
1026 | (interactive) | |
1027 | (ibuffer-mark-on-buffer | |
1028 | #'(lambda (buf) | |
1029 | (with-current-buffer buf | |
1030 | ;; hacked from midnight.el | |
1031 | (when buffer-display-time | |
1032 | (let* ((tm (current-time)) | |
1033 | (now (+ (* (float (ash 1 16)) (car tm)) | |
1034 | (float (cadr tm)) (* 0.0000001 (caddr tm)))) | |
1035 | (then (+ (* (float (ash 1 16)) | |
1036 | (car buffer-display-time)) | |
1037 | (float (cadr buffer-display-time)) | |
1038 | (* 0.0000001 (caddr buffer-display-time))))) | |
2c1bb3d3 | 1039 | (> (- now then) (* 60 60 ibuffer-old-time)))))))) |
25d2f683 CW |
1040 | |
1041 | ;;;###autoload | |
1042 | (defun ibuffer-mark-special-buffers () | |
1043 | "Mark all buffers whose name begins and ends with '*'." | |
1044 | (interactive) | |
1045 | (ibuffer-mark-on-buffer | |
1046 | #'(lambda (buf) (string-match "^\\*.+\\*$" | |
1047 | (buffer-name buf))))) | |
1048 | ||
1049 | ;;;###autoload | |
1050 | (defun ibuffer-mark-read-only-buffers () | |
1051 | "Mark all read-only buffers." | |
1052 | (interactive) | |
1053 | (ibuffer-mark-on-buffer | |
1054 | #'(lambda (buf) | |
1055 | (with-current-buffer buf | |
1056 | buffer-read-only)))) | |
1057 | ||
1058 | ;;;###autoload | |
1059 | (defun ibuffer-mark-dired-buffers () | |
1060 | "Mark all `dired' buffers." | |
1061 | (interactive) | |
1062 | (ibuffer-mark-on-buffer | |
1063 | #'(lambda (buf) | |
1064 | (with-current-buffer buf | |
1065 | (eq major-mode 'dired-mode))))) | |
1066 | ||
1067 | ;;; An implementation of multi-buffer `occur' | |
1068 | ||
1069 | (defvar ibuffer-occur-props nil) | |
1070 | ||
1071 | (define-derived-mode ibuffer-occur-mode occur-mode "Ibuffer-Occur" | |
1072 | "A special form of Occur mode for multiple buffers. | |
1073 | Note this major mode is not meant for interactive use! | |
1074 | See also `occur-mode'." | |
1075 | (define-key ibuffer-occur-mode-map (kbd "n") 'forward-line) | |
1076 | (define-key ibuffer-occur-mode-map (kbd "q") 'bury-buffer) | |
1077 | (define-key ibuffer-occur-mode-map (kbd "p") 'previous-line) | |
1078 | (define-key ibuffer-occur-mode-map (kbd "RET") 'ibuffer-occur-display-occurence) | |
1079 | (define-key ibuffer-occur-mode-map (kbd "f") 'ibuffer-occur-goto-occurence) | |
1080 | (define-key ibuffer-occur-mode-map [(mouse-2)] 'ibuffer-occur-mouse-display-occurence) | |
1081 | (set (make-local-variable 'revert-buffer-function) | |
1082 | #'ibuffer-occur-revert-buffer-function) | |
1083 | (set (make-local-variable 'ibuffer-occur-props) nil) | |
1084 | (setq buffer-read-only nil) | |
1085 | (erase-buffer) | |
1086 | (setq buffer-read-only t) | |
1087 | (message (concat | |
1088 | "Use RET " | |
1089 | (if (or (and (< 21 emacs-major-version) | |
1090 | window-system) | |
1091 | (featurep 'mouse)) | |
1092 | "or mouse-2 ") | |
1093 | "to display an occurence."))) | |
1094 | ||
1095 | (defun ibuffer-occur-mouse-display-occurence (e) | |
1096 | "Display occurence on this line in another window." | |
1097 | (interactive "e") | |
1098 | (let* ((occurbuf (save-window-excursion (mouse-select-window e) | |
1099 | (selected-window))) | |
1100 | (target (with-current-buffer occurbuf | |
1101 | (get-text-property (save-excursion | |
1102 | (mouse-set-point e) | |
1103 | (point)) | |
1104 | 'ibuffer-occur-target)))) | |
1105 | (unless target | |
1106 | (error "No occurence on this line")) | |
1107 | (let ((buf (car target)) | |
1108 | (line (cdr target))) | |
1109 | (switch-to-buffer occurbuf) | |
1110 | (delete-other-windows) | |
1111 | (pop-to-buffer buf) | |
1112 | (goto-line line)))) | |
1113 | ||
1114 | (defun ibuffer-occur-goto-occurence () | |
1115 | "Switch to the buffer which has the occurence on this line." | |
1116 | (interactive) | |
1117 | (ibuffer-occur-display-occurence t)) | |
1118 | ||
1119 | (defun ibuffer-occur-display-occurence (&optional goto) | |
1120 | "Display occurence on this line in another window." | |
1121 | (interactive "P") | |
1122 | (let ((target (get-text-property (point) 'ibuffer-occur-target))) | |
1123 | (unless target | |
1124 | (error "No occurence on this line")) | |
1125 | (let ((buf (car target)) | |
1126 | (line (cdr target))) | |
1127 | (delete-other-windows) | |
1128 | (if goto | |
1129 | (switch-to-buffer buf) | |
1130 | (pop-to-buffer buf)) | |
1131 | (goto-line line)))) | |
1132 | ||
1133 | ;;;###autoload | |
1134 | (defun ibuffer-do-occur (regexp &optional nlines) | |
1135 | "View lines which match REGEXP in all marked buffers. | |
1136 | Optional argument NLINES says how many lines of context to display: it | |
1137 | defaults to one." | |
1138 | (interactive | |
1139 | (list (let* ((default (car regexp-history)) | |
1140 | (input | |
1141 | (read-from-minibuffer | |
1142 | (if default | |
1143 | (format "List lines matching regexp (default `%s'): " | |
1144 | default) | |
1145 | "List lines matching regexp: ") | |
1146 | nil | |
1147 | nil | |
1148 | nil | |
1149 | 'regexp-history))) | |
1150 | (if (equal input "") | |
1151 | default | |
1152 | input)) | |
1153 | current-prefix-arg)) | |
1154 | (if (or (not (integerp nlines)) | |
1155 | (< nlines 0)) | |
1156 | (setq nlines 1)) | |
1157 | (when (zerop (ibuffer-count-marked-lines)) | |
10cf9a43 | 1158 | (ibuffer-set-mark ibuffer-marked-char)) |
25d2f683 CW |
1159 | (let ((ibuffer-do-occur-bufs nil)) |
1160 | ;; Accumulate a list of marked buffers | |
1161 | (ibuffer-map-marked-lines | |
1162 | #'(lambda (buf mark beg end) | |
1163 | (push buf ibuffer-do-occur-bufs))) | |
1164 | (ibuffer-do-occur-1 regexp ibuffer-do-occur-bufs | |
1165 | (get-buffer-create "*Ibuffer-occur*") | |
1166 | nlines))) | |
1167 | ||
1168 | (defun ibuffer-do-occur-1 (regexp buffers out-buf nlines) | |
1169 | (let ((count (ibuffer-occur-engine regexp buffers out-buf nlines))) | |
1170 | (if (> count 0) | |
1171 | (progn | |
1172 | (switch-to-buffer out-buf) | |
1173 | (setq buffer-read-only t) | |
1174 | (delete-other-windows) | |
1175 | (goto-char (point-min)) | |
1176 | (message "Found %s matches in %s buffers" count (length buffers))) | |
1177 | (message "No matches found")))) | |
1178 | ||
1179 | ||
1180 | (defun ibuffer-occur-revert-buffer-function (ignore-auto noconfirm) | |
1181 | "Update the *Ibuffer occur* buffer." | |
1182 | (assert (eq major-mode 'ibuffer-occur-mode)) | |
1183 | (ibuffer-do-occur-1 (car ibuffer-occur-props) | |
1184 | (cadr ibuffer-occur-props) | |
1185 | (current-buffer) | |
1186 | (caddr ibuffer-occur-props))) | |
1187 | ||
1188 | (defun ibuffer-occur-engine (regexp buffers out-buf nlines) | |
1189 | (macrolet ((insert-get-point | |
1190 | (&rest args) | |
1191 | `(progn | |
1192 | (insert ,@args) | |
1193 | (point))) | |
1194 | (maybe-put-overlay | |
1195 | (over prop value) | |
1196 | `(when (ibuffer-use-fontification) | |
1197 | (overlay-put ,over ,prop ,value))) | |
1198 | (maybe-ibuffer-propertize | |
1199 | (obj &rest args) | |
1200 | (let ((objsym (gensym "--maybe-ibuffer-propertize-"))) | |
1201 | `(let ((,objsym ,obj)) | |
1202 | (if (ibuffer-use-fontification) | |
1203 | (propertize ,objsym ,@args) | |
1204 | ,objsym))))) | |
1205 | (with-current-buffer out-buf | |
1206 | (ibuffer-occur-mode) | |
1207 | (setq buffer-read-only nil) | |
1208 | (let ((globalcount 0)) | |
1209 | ;; Map over all the buffers | |
1210 | (dolist (buf buffers) | |
1211 | (when (buffer-live-p buf) | |
1212 | (let ((c 0) ;; count of matched lines | |
1213 | (l 1) ;; line count | |
1214 | (headerpt (with-current-buffer out-buf (point)))) | |
1215 | (save-excursion | |
1216 | (set-buffer buf) | |
1217 | (save-excursion | |
1218 | (goto-char (point-min)) ;; begin searching in the buffer | |
1219 | (while (not (eobp)) | |
1220 | ;; The line we're matching against | |
1221 | (let ((curline (buffer-substring | |
1222 | (line-beginning-position) | |
1223 | (line-end-position)))) | |
1224 | (when (string-match regexp curline) | |
1225 | (incf c) ;; increment match count | |
1226 | (incf globalcount) | |
1227 | ;; Depropertize the string, and maybe highlight the matches | |
1228 | (setq curline | |
1229 | (progn | |
1230 | (ibuffer-depropertize-string curline t) | |
1231 | (when (ibuffer-use-fontification) | |
1232 | (let ((len (length curline)) | |
1233 | (start 0)) | |
1234 | (while (and (< start len) | |
1235 | (string-match regexp curline start)) | |
1236 | (put-text-property (match-beginning 0) | |
1237 | (match-end 0) | |
1238 | 'face ibuffer-occur-match-face | |
1239 | curline) | |
1240 | (setq start (match-end 0))))) | |
1241 | curline)) | |
1242 | ;; Generate the string to insert for this match | |
1243 | (let ((data | |
1244 | (if (= nlines 1) | |
1245 | ;; The simple display style | |
1246 | (concat (maybe-ibuffer-propertize | |
1247 | (format "%-6d:" l) | |
1248 | 'face 'bold) | |
1249 | curline | |
1250 | "\n") | |
1251 | ;; The complex multi-line display style | |
1252 | (let ((prevlines (nreverse | |
1253 | (ibuffer-accumulate-lines (- nlines)))) | |
1254 | (nextlines (ibuffer-accumulate-lines nlines)) | |
1255 | ;; The lack of `flet' seriously sucks. | |
1256 | (fun #'(lambda (lines) | |
1257 | (mapcar | |
1258 | #'(lambda (line) | |
1259 | (concat " :" line "\n")) | |
1260 | lines)))) | |
1261 | (setq prevlines (funcall fun prevlines)) | |
1262 | (setq nextlines (funcall fun nextlines)) | |
1263 | ;; Yes, I am trying to win the award for the | |
1264 | ;; most consing. | |
1265 | (apply #'concat | |
1266 | (nconc | |
1267 | prevlines | |
1268 | (list | |
1269 | (concat | |
1270 | (maybe-ibuffer-propertize | |
1271 | (format "%-6d" l) | |
1272 | 'face 'bold) | |
1273 | ":" | |
1274 | curline | |
1275 | "\n")) | |
1276 | nextlines)))))) | |
1277 | ;; Actually insert the match display data | |
1278 | (with-current-buffer out-buf | |
1279 | (let ((beg (point)) | |
1280 | (end (insert-get-point | |
1281 | data))) | |
1282 | (unless (= nlines 1) | |
1283 | (insert "-------\n")) | |
1284 | (put-text-property | |
1285 | beg (1- end) 'ibuffer-occur-target (cons buf l)) | |
1286 | (put-text-property | |
1287 | beg (1- end) 'mouse-face 'highlight)))))) | |
1288 | ;; On to the next line... | |
1289 | (incf l) | |
1290 | (forward-line 1)))) | |
1291 | (when (not (zerop c)) ;; is the count zero? | |
1292 | (with-current-buffer out-buf | |
1293 | (goto-char headerpt) | |
1294 | (let ((beg (point)) | |
1295 | (end (insert-get-point | |
1296 | (format "%d lines matching \"%s\" in buffer %s\n" | |
1297 | c regexp (buffer-name buf))))) | |
1298 | (let ((o (make-overlay beg end))) | |
1299 | (maybe-put-overlay o 'face 'underline))) | |
1300 | (goto-char (point-max))))))) | |
1301 | (setq ibuffer-occur-props (list regexp buffers nlines)) | |
1302 | ;; Return the number of matches | |
1303 | globalcount)))) | |
1304 | ||
1305 | (provide 'ibuf-ext) | |
1306 | ||
1307 | ;;; ibuf-ext.el ends here |