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