ibuf-ext.el 50.4 KB
Newer Older
1
;;; ibuf-ext.el --- extensions for ibuffer
Colin Walters's avatar
Colin Walters committed
2

3 4
;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
;;   2008 Free Software Foundation, Inc.
Colin Walters's avatar
Colin Walters committed
5 6

;; Author: Colin Walters <walters@verbum.org>
7
;; Maintainer: John Paul Wallington <jpw@gnu.org>
Colin Walters's avatar
Colin Walters committed
8 9 10
;; Created: 2 Dec 2001
;; Keywords: buffer, convenience

11
;; This file is part of GNU Emacs.
Colin Walters's avatar
Colin Walters committed
12

13 14 15 16
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Colin Walters's avatar
Colin Walters committed
17

18 19 20 21
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
Colin Walters's avatar
Colin Walters committed
22 23

;; You should have received a copy of the GNU General Public License
24
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Colin Walters's avatar
Colin Walters committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

;;; Commentary:

;; These functions should be automatically loaded when called, but you
;; can explicity (require 'ibuf-ext) in your ~/.emacs to have them
;; preloaded.

;;; Code:

(require 'ibuffer)

(eval-when-compile
  (require 'ibuf-macs)
  (require 'cl))

;;; Utility functions
(defun ibuffer-delete-alist (key alist)
  "Delete all entries in ALIST that have a key equal to KEY."
  (let (entry)
    (while (setq entry (assoc key alist))
      (setq alist (delete entry alist)))
    alist))

48 49 50 51 52 53 54 55 56 57 58
;; borrowed from Gnus
(defun ibuffer-remove-duplicates (list)
  "Return a copy of LIST with duplicate elements removed."
  (let ((new nil)
	(tail list))
    (while tail
      (or (member (car tail) new)
	  (setq new (cons (car tail) new)))
      (setq tail (cdr tail)))
    (nreverse new)))

59 60 61 62
(defun ibuffer-split-list (ibuffer-split-list-fn ibuffer-split-list-elts)
  (let ((hip-crowd nil)
	(lamers nil))
    (dolist (ibuffer-split-list-elt ibuffer-split-list-elts)
63
      (if (funcall ibuffer-split-list-fn ibuffer-split-list-elt)
64 65 66 67 68
	  (push ibuffer-split-list-elt hip-crowd)
	(push ibuffer-split-list-elt lamers)))
    ;; Too bad Emacs Lisp doesn't have multiple values.
    (list (nreverse hip-crowd) (nreverse lamers))))

Colin Walters's avatar
Colin Walters committed
69 70 71 72 73 74
(defcustom ibuffer-never-show-predicates nil
  "A list of predicates (a regexp or function) for buffers not to display.
If a regexp, then it will be matched against the buffer's name.
If a function, it will be called with the buffer as an argument, and
should return non-nil if this buffer should not be shown."
  :type '(repeat (choice regexp function))
75
  :require 'ibuf-ext
Colin Walters's avatar
Colin Walters committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89
  :group 'ibuffer)

(defcustom ibuffer-always-show-predicates nil
  "A list of predicates (a regexp or function) for buffers to always display.
If a regexp, then it will be matched against the buffer's name.
If a function, it will be called with the buffer as an argument, and
should return non-nil if this buffer should be shown.
Note that buffers matching one of these predicates will be shown
regardless of any active filters in this buffer."
  :type '(repeat (choice regexp function))
  :group 'ibuffer)

(defvar ibuffer-tmp-hide-regexps nil
  "A list of regexps which should match buffer names to not show.")
90

Colin Walters's avatar
Colin Walters committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104
(defvar ibuffer-tmp-show-regexps nil
  "A list of regexps which should match buffer names to always show.")

(defvar ibuffer-auto-mode nil
  "If non-nil, Ibuffer auto-mode should be enabled for this buffer.
Do not set this variable directly!  Use the function
`ibuffer-auto-mode' instead.")

(defvar ibuffer-auto-buffers-changed nil)

(defcustom ibuffer-saved-filters '(("gnus"
				    ((or (mode . message-mode)
					 (mode . mail-mode)
					 (mode . gnus-group-mode)
105
					 (mode . gnus-summary-mode)
Colin Walters's avatar
Colin Walters committed
106 107 108 109 110
					 (mode . gnus-article-mode))))
				   ("programming"
				    ((or (mode . emacs-lisp-mode)
					 (mode . cperl-mode)
					 (mode . c-mode)
111
					 (mode . java-mode)
Colin Walters's avatar
Colin Walters committed
112 113
					 (mode . idl-mode)
					 (mode . lisp-mode)))))
114

Colin Walters's avatar
Colin Walters committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  "An alist of filter qualifiers to switch between.

This variable should look like ((\"STRING\" QUALIFIERS)
                                (\"STRING\" QUALIFIERS) ...), where
QUALIFIERS is a list of the same form as
`ibuffer-filtering-qualifiers'.
See also the variables `ibuffer-filtering-qualifiers',
`ibuffer-filtering-alist', and the functions
`ibuffer-switch-to-saved-filters', `ibuffer-save-filters'."
  :type '(repeat sexp)
  :group 'ibuffer)

(defvar ibuffer-filtering-qualifiers nil
  "A list like (SYMBOL . QUALIFIER) which filters the current buffer list.
See also `ibuffer-filtering-alist'.")

;; This is now frobbed by `define-ibuffer-filter'.
(defvar ibuffer-filtering-alist nil
  "An alist of (SYMBOL DESCRIPTION FUNCTION) which describes a filter.

You most likely do not want to modify this variable directly; see
`define-ibuffer-filter'.

SYMBOL is the symbolic name of the filter.  DESCRIPTION is used when
displaying information to the user.  FUNCTION is given a buffer and
the value of the qualifier, and returns non-nil if and only if the
buffer should be displayed.")

143 144 145 146 147 148 149 150 151
(defcustom ibuffer-filter-format-alist nil
  "An alist which has special formats used when a filter is active.
The contents of this variable should look like:
 ((FILTER (FORMAT FORMAT ...)) (FILTER (FORMAT FORMAT ...)) ...)

For example, suppose that when you add a filter for buffers whose
major mode is `emacs-lisp-mode', you only want to see the mark and the
name of the buffer.  You could accomplish that by adding:
 (mode ((mark \" \" name)))
152 153 154 155
to this variable."
  :type '(repeat (list :tag "Association" (symbol :tag "Filter")
                       (list :tag "Formats" (repeat (sexp :tag "Format")))))
  :group 'ibuffer)
156 157

(defvar ibuffer-cached-filter-formats nil)
158
(defvar ibuffer-compiled-filter-formats nil)
159

160
(defvar ibuffer-filter-groups nil
161
  "A list like ((\"NAME\" ((SYMBOL . QUALIFIER) ...) ...) which groups buffers.
162 163 164 165 166 167 168 169 170
The SYMBOL should be one from `ibuffer-filtering-alist'.
The QUALIFIER should be the same as QUALIFIER in
`ibuffer-filtering-qualifiers'.")

(defcustom ibuffer-show-empty-filter-groups t
  "If non-nil, then show the names of filter groups which are empty."
  :type 'boolean
  :group 'ibuffer)

171
(defcustom ibuffer-saved-filter-groups nil
172 173 174 175 176 177 178 179 180
  "An alist of filtering groups to switch between.

This variable should look like ((\"STRING\" QUALIFIERS)
                                (\"STRING\" QUALIFIERS) ...), where
QUALIFIERS is a list of the same form as
`ibuffer-filtering-qualifiers'.

See also the variables `ibuffer-filter-groups',
`ibuffer-filtering-qualifiers', `ibuffer-filtering-alist', and the
181 182
functions `ibuffer-switch-to-saved-filter-groups',
`ibuffer-save-filter-groups'."
183 184
  :type '(repeat sexp)
  :group 'ibuffer)
185

186
(defvar ibuffer-hidden-filter-groups nil
187 188
  "A list of filtering groups which are currently hidden.")

189 190
(defvar ibuffer-filter-group-kill-ring nil)

191 192 193 194 195 196
(defcustom ibuffer-old-time 72
  "The number of hours before a buffer is considered \"old\"."
  :type '(choice (const :tag "72 hours (3 days)" 72)
 		 (const :tag "48 hours (2 days)" 48)
 		 (const :tag "24 hours (1 day)" 24)
		 (integer :tag "hours"))
Colin Walters's avatar
Colin Walters committed
197 198 199 200
  :group 'ibuffer)

(defcustom ibuffer-save-with-custom t
  "If non-nil, then use Custom to save interactively changed variables.
201
Currently, this only applies to `ibuffer-saved-filters' and
202
`ibuffer-saved-filter-groups'."
Colin Walters's avatar
Colin Walters committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216
  :type 'boolean
  :group 'ibuffer)

(defun ibuffer-ext-visible-p (buf all &optional ibuffer-buf)
  (or
   (ibuffer-buf-matches-predicates buf ibuffer-tmp-show-regexps)
   (and (not
	 (or
	  (ibuffer-buf-matches-predicates buf ibuffer-tmp-hide-regexps)
	  (ibuffer-buf-matches-predicates buf ibuffer-never-show-predicates)))
	(or all
	    (not
	     (ibuffer-buf-matches-predicates buf ibuffer-maybe-show-predicates)))
	(or ibuffer-view-ibuffer
217
	    (and ibuffer-buf
Colin Walters's avatar
Colin Walters committed
218 219 220 221 222 223
		 (not (eq ibuffer-buf buf))))
	(or
	 (ibuffer-included-in-filters-p buf ibuffer-filtering-qualifiers)
	 (ibuffer-buf-matches-predicates buf ibuffer-always-show-predicates)))))

(defun ibuffer-auto-update-changed ()
224
  (when (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed)
225 226 227 228
    (dolist (buf (buffer-list))
      (ignore-errors
	(with-current-buffer buf
	  (when (and ibuffer-auto-mode
229
		     (derived-mode-p 'ibuffer-mode))
230
	    (ibuffer-update nil t)))))))
Colin Walters's avatar
Colin Walters committed
231 232 233 234 235 236

;;;###autoload
(defun ibuffer-auto-mode (&optional arg)
  "Toggle use of Ibuffer's auto-update facility.
With numeric ARG, enable auto-update if and only if ARG is positive."
  (interactive)
237
  (unless (derived-mode-p 'ibuffer-mode)
Colin Walters's avatar
Colin Walters committed
238 239 240 241 242
    (error "This buffer is not in Ibuffer mode"))
  (set (make-local-variable 'ibuffer-auto-mode)
       (if arg
	   (plusp arg)
	 (not ibuffer-auto-mode)))
243
  (frame-or-buffer-changed-p 'ibuffer-auto-buffers-changed) ; Initialize state vector
244
  (add-hook 'post-command-hook 'ibuffer-auto-update-changed))
Colin Walters's avatar
Colin Walters committed
245 246 247 248 249 250 251 252 253 254 255 256

;;;###autoload
(defun ibuffer-mouse-filter-by-mode (event)
  "Enable or disable filtering by the major mode chosen via mouse."
  (interactive "e")
  (ibuffer-interactive-filter-by-mode event))

;;;###autoload
(defun ibuffer-interactive-filter-by-mode (event-or-point)
  "Enable or disable filtering by the major mode at point."
  (interactive "d")
  (if (eventp event-or-point)
257
      (posn-set-point (event-end event-or-point))
Colin Walters's avatar
Colin Walters committed
258 259 260 261 262
    (goto-char event-or-point))
  (let ((buf (ibuffer-current-buffer)))
    (if (assq 'mode ibuffer-filtering-qualifiers)
	(setq ibuffer-filtering-qualifiers
	      (ibuffer-delete-alist 'mode ibuffer-filtering-qualifiers))
263
      (ibuffer-push-filter (cons 'mode
Colin Walters's avatar
Colin Walters committed
264 265 266 267
				(with-current-buffer buf
				  major-mode)))))
  (ibuffer-update nil t))

268 269 270 271 272 273 274 275 276 277 278
;;;###autoload
(defun ibuffer-mouse-toggle-filter-group (event)
  "Toggle the display status of the filter group chosen with the mouse."
  (interactive "e")
  (ibuffer-toggle-filter-group-1 (save-excursion
				   (mouse-set-point event)
				   (point))))

;;;###autoload
(defun ibuffer-toggle-filter-group ()
  "Toggle the display status of the filter group on this line."
279
  (interactive)
280 281
  (ibuffer-toggle-filter-group-1 (point)))

282
(defun ibuffer-toggle-filter-group-1 (posn)
283 284 285
  (let ((name (get-text-property posn 'ibuffer-filter-group-name)))
    (unless (stringp name)
      (error "No filtering group name present"))
286 287 288 289
    (if (member name ibuffer-hidden-filter-groups)
	(setq ibuffer-hidden-filter-groups
	      (delete name ibuffer-hidden-filter-groups))
      (push name ibuffer-hidden-filter-groups))
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    (ibuffer-update nil t)))

;;;###autoload
(defun ibuffer-forward-filter-group (&optional count)
  "Move point forwards by COUNT filtering groups."
  (interactive "P")
  (unless count
    (setq count 1))
  (when (> count 0)
    (when (get-text-property (point) 'ibuffer-filter-group-name)
      (goto-char (next-single-property-change
		  (point) 'ibuffer-filter-group-name
		  nil (point-max))))
    (goto-char (next-single-property-change
		(point) 'ibuffer-filter-group-name
		nil (point-max)))
    (ibuffer-forward-filter-group (1- count)))
  (ibuffer-forward-line 0))

;;;###autoload
(defun ibuffer-backward-filter-group (&optional count)
  "Move point backwards by COUNT filtering groups."
  (interactive "P")
  (unless count
    (setq count 1))
  (when (> count 0)
    (when (get-text-property (point) 'ibuffer-filter-group-name)
      (goto-char (previous-single-property-change
		  (point) 'ibuffer-filter-group-name
		  nil (point-min))))
    (goto-char (previous-single-property-change
		(point) 'ibuffer-filter-group-name
		nil (point-min)))
    (ibuffer-backward-filter-group (1- count)))
  (when (= (point) (point-min))
    (goto-char (point-max))
    (ibuffer-backward-filter-group 1))
  (ibuffer-forward-line 0))

329
;;;###autoload (autoload 'ibuffer-do-shell-command-pipe "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
330 331 332 333 334 335 336 337 338
(define-ibuffer-op shell-command-pipe (command)
  "Pipe the contents of each marked buffer to shell command COMMAND."
  (:interactive "sPipe to shell command: "
   :opstring "Shell command executed on"
   :modifier-p nil)
  (shell-command-on-region
   (point-min) (point-max) command
   (get-buffer-create "* ibuffer-shell-output*")))

339
;;;###autoload (autoload 'ibuffer-do-shell-command-pipe-replace "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
340 341 342 343 344 345 346 347 348 349 350
(define-ibuffer-op shell-command-pipe-replace (command)
  "Replace the contents of marked buffers with output of pipe to COMMAND."
  (:interactive "sPipe to shell command (replace): "
   :opstring "Buffer contents replaced in"
   :active-opstring "replace buffer contents in"
   :dangerous t
   :modifier-p t)
  (with-current-buffer buf
    (shell-command-on-region (point-min) (point-max)
			     command nil t)))

351
;;;###autoload (autoload 'ibuffer-do-shell-command-file "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
352 353 354 355 356 357 358 359 360 361 362
(define-ibuffer-op shell-command-file (command)
  "Run shell command COMMAND separately on files of marked buffers."
  (:interactive "sShell command on buffer's file: "
   :opstring "Shell command executed on"
   :modifier-p nil)
  (shell-command (concat command " "
			 (shell-quote-argument
			  (if buffer-file-name
			      buffer-file-name
			    (make-temp-file
			     (substring (buffer-name) 0 (min 10 (length (buffer-name))))))))))
363

364
;;;###autoload (autoload 'ibuffer-do-eval "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
365 366 367 368
(define-ibuffer-op eval (form)
  "Evaluate FORM in each of the buffers.
Does not display the buffer during evaluation. See
`ibuffer-do-view-and-eval' for that."
369 370 371 372 373
  (:interactive
   (list
    (read-from-minibuffer
     "Eval in buffers (form): "
     nil read-expression-map t 'read-expression-history))
Colin Walters's avatar
Colin Walters committed
374 375 376 377
   :opstring "evaluated in"
   :modifier-p :maybe)
  (eval form))

378
;;;###autoload (autoload 'ibuffer-do-view-and-eval "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
379 380 381
(define-ibuffer-op view-and-eval (form)
  "Evaluate FORM while displaying each of the marked buffers.
To evaluate a form without viewing the buffer, see `ibuffer-do-eval'."
382 383 384 385 386
  (:interactive
   (list
    (read-from-minibuffer
     "Eval viewing in buffers (form): "
     nil read-expression-map t 'read-expression-history))
Colin Walters's avatar
Colin Walters committed
387 388 389 390 391 392 393 394 395 396
   :opstring "evaluated in"
   :complex t
   :modifier-p :maybe)
  (let ((ibuffer-buf (current-buffer)))
    (unwind-protect
	(progn
	  (switch-to-buffer buf)
	  (eval form))
      (switch-to-buffer ibuffer-buf))))

397
;;;###autoload (autoload 'ibuffer-do-rename-uniquely "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
398 399 400 401 402 403
(define-ibuffer-op rename-uniquely ()
  "Rename marked buffers as with `rename-uniquely'."
  (:opstring "renamed"
   :modifier-p t)
  (rename-uniquely))

404
;;;###autoload (autoload 'ibuffer-do-revert "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
405 406 407 408 409 410 411 412
(define-ibuffer-op revert ()
  "Revert marked buffers as with `revert-buffer'."
  (:dangerous t
   :opstring "reverted"
   :active-opstring "revert"
   :modifier-p :maybe)
  (revert-buffer t t))

413
;;;###autoload (autoload 'ibuffer-do-replace-regexp "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
(define-ibuffer-op replace-regexp (from-str to-str)
  "Perform a `replace-regexp' in marked buffers."
  (:interactive
   (let* ((from-str (read-from-minibuffer "Replace regexp: "))
	  (to-str (read-from-minibuffer (concat "Replace " from-str
						" with: "))))
     (list from-str to-str))
   :opstring "replaced in"
   :complex t
   :modifier-p :maybe)
  (save-window-excursion
    (switch-to-buffer buf)
    (save-excursion
      (goto-char (point-min))
      (let ((case-fold-search ibuffer-case-fold-search))
	(while (re-search-forward from-str nil t)
	  (replace-match to-str))))
    t))

433
;;;###autoload (autoload 'ibuffer-do-query-replace "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
434 435 436
(define-ibuffer-op query-replace (&rest args)
  "Perform a `query-replace' in marked buffers."
  (:interactive
437
   (query-replace-read-args "Query replace" t t)
Colin Walters's avatar
Colin Walters committed
438 439 440 441 442 443 444 445 446 447 448
   :opstring "replaced in"
   :complex t
   :modifier-p :maybe)
  (save-window-excursion
    (switch-to-buffer buf)
    (save-excursion
      (let ((case-fold-search ibuffer-case-fold-search))
	(goto-char (point-min))
	(apply #'query-replace args)))
    t))

449
;;;###autoload (autoload 'ibuffer-do-query-replace-regexp "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
450 451 452
(define-ibuffer-op query-replace-regexp (&rest args)
  "Perform a `query-replace-regexp' in marked buffers."
  (:interactive
453
   (query-replace-read-args "Query replace regexp" t t)
Colin Walters's avatar
Colin Walters committed
454 455 456 457 458 459 460 461 462 463 464
   :opstring "replaced in"
   :complex t
   :modifier-p :maybe)
  (save-window-excursion
    (switch-to-buffer buf)
    (save-excursion
      (let ((case-fold-search ibuffer-case-fold-search))
	(goto-char (point-min))
	(apply #'query-replace-regexp args)))
    t))

465
;;;###autoload (autoload 'ibuffer-do-print "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
(define-ibuffer-op print ()
  "Print marked buffers as with `print-buffer'."
  (:opstring "printed"
   :modifier-p nil)
  (print-buffer))

;;;###autoload
(defun ibuffer-included-in-filters-p (buf filters)
  (not
   (memq nil ;; a filter will return nil if it failed
	 (mapcar
	  ;; filter should be like (TYPE . QUALIFIER), or
	  ;; (or (TYPE . QUALIFIER) (TYPE . QUALIFIER) ...)
	  #'(lambda (qual)
	      (ibuffer-included-in-filter-p buf qual))
	  filters))))

(defun ibuffer-included-in-filter-p (buf filter)
  (if (eq (car filter) 'not)
      (not (ibuffer-included-in-filter-p-1 buf (cdr filter)))
    (ibuffer-included-in-filter-p-1 buf filter)))

(defun ibuffer-included-in-filter-p-1 (buf filter)
  (not
   (not
    (case (car filter)
      (or
       (memq t (mapcar #'(lambda (x)
			   (ibuffer-included-in-filter-p buf x))
		       (cdr filter))))
      (saved
       (let ((data
	      (assoc (cdr filter)
		     ibuffer-saved-filters)))
	 (unless data
	   (ibuffer-filter-disable)
	   (error "Unknown saved filter %s" (cdr filter)))
	 (ibuffer-included-in-filters-p buf (cadr data))))
      (t
       (let ((filterdat (assq (car filter)
			      ibuffer-filtering-alist)))
	 ;; filterdat should be like (TYPE DESCRIPTION FUNC)
	 ;; just a sanity check
	(unless filterdat
	  (ibuffer-filter-disable)
	  (error "Undefined filter %s" (car filter)))
	(not
	 (not
	  (funcall (caddr filterdat)
		   buf
		   (cdr filter))))))))))

518 519 520 521 522
(defun ibuffer-generate-filter-groups (bmarklist &optional noempty nodefault)
  (let ((filter-group-alist (if nodefault
				ibuffer-filter-groups
			      (append ibuffer-filter-groups
				      (list (cons "Default" nil))))))
523 524 525 526
;;     (dolist (hidden ibuffer-hidden-filter-groups)
;;       (setq filter-group-alist (ibuffer-delete-alist
;; 				   hidden filter-group-alist)))
    (let ((vec (make-vector (length filter-group-alist) nil))
527
	  (i 0))
528
      (dolist (filtergroup filter-group-alist)
529 530 531 532 533 534 535 536 537
	(let ((filterset (cdr filtergroup)))
	  (multiple-value-bind (hip-crowd lamers)
	      (ibuffer-split-list (lambda (bufmark)
				    (ibuffer-included-in-filters-p (car bufmark)
								   filterset))
				  bmarklist)
	    (aset vec i hip-crowd)
	    (incf i)
	    (setq bmarklist lamers))))
538
      (let (ret)
539
	(dotimes (j i ret)
540 541 542 543 544
	  (let ((bufs (aref vec j)))
	    (unless (and noempty (null bufs))
	      (push (cons (car (nth j filter-group-alist))
			  bufs)
		    ret))))))))
545 546 547 548 549 550 551

;;;###autoload
(defun ibuffer-filters-to-filter-group (name)
  "Make the current filters into a filtering group."
  (interactive "sName for filtering group: ")
  (when (null ibuffer-filtering-qualifiers)
    (error "No filters in effect"))
552
  (push (cons name ibuffer-filtering-qualifiers) ibuffer-filter-groups)
553 554
  (ibuffer-filter-disable))

555 556 557 558 559
;;;###autoload
(defun ibuffer-set-filter-groups-by-mode ()
  "Set the current filter groups to filter by mode."
  (interactive)
  (setq ibuffer-filter-groups
560 561 562
        (mapcar (lambda (mode)
                  (cons (format "%s" mode) `((mode . ,mode))))
                (let ((modes
563
                       (ibuffer-remove-duplicates
564
                        (mapcar (lambda (buf)
565
				  (with-current-buffer buf major-mode))
566 567 568 569
                                (buffer-list)))))
                  (if ibuffer-view-ibuffer
		      modes
		    (delq 'ibuffer-mode modes)))))
570 571
  (ibuffer-update nil t))

572 573
;;;###autoload
(defun ibuffer-pop-filter-group ()
574
  "Remove the first filter group."
575
  (interactive)
576
  (when (null ibuffer-filter-groups)
577
    (error "No filter groups active"))
578 579 580
  (setq ibuffer-hidden-filter-groups
	(delete (pop ibuffer-filter-groups)
		ibuffer-hidden-filter-groups))
581 582
  (ibuffer-update nil t))

583 584 585
(defun ibuffer-read-filter-group-name (msg &optional nodefault noerror)
  (when (and (not noerror) (null ibuffer-filter-groups))
    (error "No filter groups active"))
586 587 588 589 590 591 592 593 594 595 596 597 598
  ;; `ibuffer-generate-filter-groups' returns all non-hidden filter
  ;; groups, possibly excluding empty groups or Default.
  ;; We add `ibuffer-hidden-filter-groups' to the list, excluding
  ;; Default if necessary.
  (completing-read msg (nconc
			(ibuffer-generate-filter-groups
			 (ibuffer-current-state-list)
			 (not ibuffer-show-empty-filter-groups)
			 nodefault)
			(if nodefault
			    (remove "Default" ibuffer-hidden-filter-groups)
			  ibuffer-hidden-filter-groups))
		   nil t))
599 600 601 602

;;;###autoload
(defun ibuffer-decompose-filter-group (group)
  "Decompose the filter group GROUP into active filters."
603
  (interactive
604
   (list (ibuffer-read-filter-group-name "Decompose filter group: " t)))
605 606 607 608 609 610
  (let ((data (cdr (assoc group ibuffer-filter-groups))))
    (setq ibuffer-filter-groups (ibuffer-delete-alist
				 group ibuffer-filter-groups)
	  ibuffer-filtering-qualifiers data))
  (ibuffer-update nil t))

611 612
;;;###autoload
(defun ibuffer-clear-filter-groups ()
613
  "Remove all filter groups."
614
  (interactive)
615 616
  (setq ibuffer-filter-groups nil
	ibuffer-hidden-filter-groups nil)
617 618
  (ibuffer-update nil t))

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
(defun ibuffer-current-filter-groups-with-position ()
  (save-excursion
    (goto-char (point-min))
    (let ((pos nil)
	  (result nil))
      (while (and (not (eobp))
		  (setq pos (next-single-property-change
			     (point) 'ibuffer-filter-group-name)))
	(goto-char pos)
	(push (cons (get-text-property (point) 'ibuffer-filter-group-name)
		    pos)
	      result)
	(goto-char (next-single-property-change
		    pos 'ibuffer-filter-group-name)))
      (nreverse result))))

635 636 637
;;;###autoload
(defun ibuffer-jump-to-filter-group (name)
  "Move point to the filter group whose name is NAME."
638
  (interactive
639
   (list (ibuffer-read-filter-group-name "Jump to filter group: ")))
640 641 642
  (ibuffer-aif (assoc name (ibuffer-current-filter-groups-with-position))
      (goto-char (cdr it))
    (error "No filter group with name %s" name)))
643

644 645
;;;###autoload
(defun ibuffer-kill-filter-group (name)
646
  "Kill the filter group named NAME.
647
The group will be added to `ibuffer-filter-group-kill-ring'."
648
  (interactive (list (ibuffer-read-filter-group-name "Kill filter group: " t)))
649
  (when (equal name "Default")
650
    (error "Can't kill default filter group"))
651
  (ibuffer-aif (assoc name ibuffer-filter-groups)
652
      (progn
653
	(push (copy-tree it) ibuffer-filter-group-kill-ring)
654 655 656
	(setq ibuffer-filter-groups (ibuffer-delete-alist
				     name ibuffer-filter-groups))
	(setq ibuffer-hidden-filter-groups
657
	      (delete name ibuffer-hidden-filter-groups)))
658 659 660 661
    (error "No filter group with name \"%s\"" name))
  (ibuffer-update nil t))

;;;###autoload
662
(defun ibuffer-kill-line (&optional arg interactive-p)
663
  "Kill the filter group at point.
664
See also `ibuffer-kill-filter-group'."
665
  (interactive "P\np")
666 667 668 669 670
  (ibuffer-aif (save-excursion
		 (ibuffer-forward-line 0)
		 (get-text-property (point) 'ibuffer-filter-group-name))
      (progn
	(ibuffer-kill-filter-group it))
671
      (funcall (if interactive-p #'call-interactively #'funcall)
672 673
	       #'kill-line arg)))

674
(defun ibuffer-insert-filter-group-before (newgroup group)
675 676 677 678 679 680 681 682 683 684 685
  (let* ((found nil)
	 (pos (let ((groups (mapcar #'car ibuffer-filter-groups))
		    (res 0))
		(while groups
		  (if (equal (car groups) group)
		      (setq found t
			    groups nil)
		    (incf res)
		    (setq groups (cdr groups))))
		res)))
    (cond ((not found)
686 687
	   (setq ibuffer-filter-groups
		 (nconc ibuffer-filter-groups (list newgroup))))
688 689
	  ((zerop pos)
	   (push newgroup ibuffer-filter-groups))
690 691 692 693 694
	  (t
	   (let ((cell (nthcdr pos ibuffer-filter-groups)))
	     (setf (cdr cell) (cons (car cell) (cdr cell)))
	     (setf (car cell) newgroup))))))

695
;;;###autoload
696 697 698 699 700 701 702 703 704 705 706
(defun ibuffer-yank ()
  "Yank the last killed filter group before group at point."
  (interactive)
  (ibuffer-yank-filter-group
   (or (get-text-property (point) 'ibuffer-filter-group-name)
       (get-text-property (point) 'ibuffer-filter-group)
       (error "No filter group at point"))))

;;;###autoload
(defun ibuffer-yank-filter-group (name)
  "Yank the last killed filter group before group named NAME."
707 708 709 710
  (interactive (list (ibuffer-read-filter-group-name
			"Yank filter group before group: ")))
  (unless ibuffer-filter-group-kill-ring
    (error "The Ibuffer filter group kill-ring is empty"))
711 712
  (save-excursion
    (ibuffer-forward-line 0)
713 714
    (ibuffer-insert-filter-group-before (pop ibuffer-filter-group-kill-ring)
					name))
715 716 717
  (ibuffer-update nil t))

;;;###autoload
718
(defun ibuffer-save-filter-groups (name groups)
719 720 721 722 723 724 725 726 727 728 729
  "Save all active filter groups GROUPS as NAME.
They are added to `ibuffer-saved-filter-groups'.  Interactively,
prompt for NAME, and use the current filters."
  (interactive
   (if (null ibuffer-filter-groups)
       (error "No filter groups active")
     (list
      (read-from-minibuffer "Save current filter groups as: ")
      ibuffer-filter-groups)))
  (ibuffer-aif (assoc name ibuffer-saved-filter-groups)
      (setcdr it groups)
730
    (push (cons name groups) ibuffer-saved-filter-groups))
731
  (ibuffer-maybe-save-stuff))
732 733 734 735 736 737 738 739

;;;###autoload
(defun ibuffer-delete-saved-filter-groups (name)
  "Delete saved filter groups with NAME.
They are removed from `ibuffer-saved-filter-groups'."
  (interactive
   (list
    (if (null ibuffer-saved-filter-groups)
740 741
	(error "No saved filter groups")
      (completing-read "Delete saved filter group: "
742 743 744 745 746 747 748 749 750
		       ibuffer-saved-filter-groups nil t))))
  (setq ibuffer-saved-filter-groups
	(ibuffer-delete-alist name ibuffer-saved-filter-groups))
  (ibuffer-maybe-save-stuff)
  (ibuffer-update nil t))

;;;###autoload
(defun ibuffer-switch-to-saved-filter-groups (name)
  "Set this buffer's filter groups to saved version with NAME.
751
The value from `ibuffer-saved-filter-groups' is used."
752 753 754 755 756 757
  (interactive
   (list
    (if (null ibuffer-saved-filter-groups)
	(error "No saved filters")
      (completing-read "Switch to saved filter group: "
		       ibuffer-saved-filter-groups nil t))))
758 759
  (setq ibuffer-filter-groups (cdr (assoc name ibuffer-saved-filter-groups))
	ibuffer-hidden-filter-groups nil)
760 761
  (ibuffer-update nil t))

Colin Walters's avatar
Colin Walters committed
762 763 764 765 766
;;;###autoload
(defun ibuffer-filter-disable ()
  "Disable all filters currently in effect in this buffer."
  (interactive)
  (setq ibuffer-filtering-qualifiers nil)
767 768 769 770
  (let ((buf (ibuffer-current-buffer)))
    (ibuffer-update nil t)
    (when buf
      (ibuffer-jump-to-buffer (buffer-name buf)))))
Colin Walters's avatar
Colin Walters committed
771 772 773 774 775 776 777 778

;;;###autoload
(defun ibuffer-pop-filter ()
  "Remove the top filter in this buffer."
  (interactive)
  (when (null ibuffer-filtering-qualifiers)
    (error "No filters in effect"))
  (pop ibuffer-filtering-qualifiers)
779 780 781 782
  (let ((buf (ibuffer-current-buffer)))
    (ibuffer-update nil t)
    (when buf
      (ibuffer-jump-to-buffer (buffer-name buf)))))
Colin Walters's avatar
Colin Walters committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796

(defun ibuffer-push-filter (qualifier)
  "Add QUALIFIER to `ibuffer-filtering-qualifiers'."
  (push qualifier ibuffer-filtering-qualifiers))

;;;###autoload
(defun ibuffer-decompose-filter ()
  "Separate the top compound filter (OR, NOT, or SAVED) in this buffer.

This means that the topmost filter on the filtering stack, which must
be a complex filter like (OR [name: foo] [mode: bar-mode]), will be
turned into two separate filters [name: foo] and [mode: bar-mode]."
  (interactive)
  (when (null ibuffer-filtering-qualifiers)
797
    (error "No filters in effect"))
Colin Walters's avatar
Colin Walters committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
  (let ((lim (pop ibuffer-filtering-qualifiers)))
    (case (car lim)
      (or
       (setq ibuffer-filtering-qualifiers (append
					  (cdr lim)
					  ibuffer-filtering-qualifiers)))
      (saved
       (let ((data
	      (assoc (cdr lim)
		     ibuffer-saved-filters)))
	 (unless data
	   (ibuffer-filter-disable)
	   (error "Unknown saved filter %s" (cdr lim)))
	 (setq ibuffer-filtering-qualifiers (append
					    (cadr data)
					    ibuffer-filtering-qualifiers))))
      (not
       (push (cdr lim)
	     ibuffer-filtering-qualifiers))
      (t
       (error "Filter type %s is not compound" (car lim)))))
  (ibuffer-update nil t))

;;;###autoload
(defun ibuffer-exchange-filters ()
  "Exchange the top two filters on the stack in this buffer."
  (interactive)
  (when (< (length ibuffer-filtering-qualifiers)
	   2)
    (error "Need two filters to exchange"))
  (let ((first (pop ibuffer-filtering-qualifiers))
	(second (pop ibuffer-filtering-qualifiers)))
    (push first ibuffer-filtering-qualifiers)
    (push second ibuffer-filtering-qualifiers))
  (ibuffer-update nil t))

;;;###autoload
(defun ibuffer-negate-filter ()
  "Negate the sense of the top filter in the current buffer."
  (interactive)
  (when (null ibuffer-filtering-qualifiers)
    (error "No filters in effect"))
  (let ((lim (pop ibuffer-filtering-qualifiers)))
    (push (if (eq (car lim) 'not)
	      (cdr lim)
	    (cons 'not lim))
	  ibuffer-filtering-qualifiers))
  (ibuffer-update nil t))

;;;###autoload
(defun ibuffer-or-filter (&optional reverse)
  "Replace the top two filters in this buffer with their logical OR.
If optional argument REVERSE is non-nil, instead break the top OR
filter into parts."
  (interactive "P")
  (if reverse
      (progn
	(when (or (null ibuffer-filtering-qualifiers)
		  (not (eq 'or (caar ibuffer-filtering-qualifiers))))
	  (error "Top filter is not an OR"))
	(let ((lim (pop ibuffer-filtering-qualifiers)))
859
	  (setq ibuffer-filtering-qualifiers
860
		(nconc (cdr lim) ibuffer-filtering-qualifiers))))
Colin Walters's avatar
Colin Walters committed
861 862 863 864 865 866
    (when (< (length ibuffer-filtering-qualifiers) 2)
      (error "Need two filters to OR"))
    ;; If the second filter is an OR, just add to it.
    (let ((first (pop ibuffer-filtering-qualifiers))
	  (second (pop ibuffer-filtering-qualifiers)))
      (if (eq 'or (car second))
867 868
	  (push (nconc (list 'or first) (cdr second))
		ibuffer-filtering-qualifiers)
Colin Walters's avatar
Colin Walters committed
869 870 871 872
	(push (list 'or first second)
	      ibuffer-filtering-qualifiers))))
  (ibuffer-update nil t))

873
(defun ibuffer-maybe-save-stuff ()
Colin Walters's avatar
Colin Walters committed
874 875 876 877
  (when ibuffer-save-with-custom
    (if (fboundp 'customize-save-variable)
	(progn
	  (customize-save-variable 'ibuffer-saved-filters
878 879 880
				   ibuffer-saved-filters)
	  (customize-save-variable 'ibuffer-saved-filter-groups
				   ibuffer-saved-filter-groups))
Colin Walters's avatar
Colin Walters committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894
      (message "Not saved permanently: Customize not available"))))

;;;###autoload
(defun ibuffer-save-filters (name filters)
  "Save FILTERS in this buffer with name NAME in `ibuffer-saved-filters'.
Interactively, prompt for NAME, and use the current filters."
  (interactive
   (if (null ibuffer-filtering-qualifiers)
       (error "No filters currently in effect")
     (list
      (read-from-minibuffer "Save current filters as: ")
      ibuffer-filtering-qualifiers)))
  (ibuffer-aif (assoc name ibuffer-saved-filters)
      (setcdr it filters)
895
    (push (list name filters) ibuffer-saved-filters))
896
  (ibuffer-maybe-save-stuff))
Colin Walters's avatar
Colin Walters committed
897 898 899 900 901 902 903 904 905 906 907 908

;;;###autoload
(defun ibuffer-delete-saved-filters (name)
  "Delete saved filters with NAME from `ibuffer-saved-filters'."
  (interactive
   (list
    (if (null ibuffer-saved-filters)
	(error "No saved filters")
      (completing-read "Delete saved filters: "
		       ibuffer-saved-filters nil t))))
  (setq ibuffer-saved-filters
	(ibuffer-delete-alist name ibuffer-saved-filters))
909
  (ibuffer-maybe-save-stuff)
Colin Walters's avatar
Colin Walters committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
  (ibuffer-update nil t))

;;;###autoload
(defun ibuffer-add-saved-filters (name)
  "Add saved filters from `ibuffer-saved-filters' to this buffer's filters."
  (interactive
   (list
    (if (null ibuffer-saved-filters)
	(error "No saved filters")
      (completing-read "Add saved filters: "
		       ibuffer-saved-filters nil t))))
  (push (cons 'saved name) ibuffer-filtering-qualifiers)
  (ibuffer-update nil t))

;;;###autoload
(defun ibuffer-switch-to-saved-filters (name)
926
  "Set this buffer's filters to filters with NAME from `ibuffer-saved-filters'."
Colin Walters's avatar
Colin Walters committed
927 928 929 930 931 932 933 934
  (interactive
   (list
    (if (null ibuffer-saved-filters)
	(error "No saved filters")
      (completing-read "Switch to saved filters: "
		       ibuffer-saved-filters nil t))))
  (setq ibuffer-filtering-qualifiers (list (cons 'saved name)))
  (ibuffer-update nil t))
935 936 937 938

(defun ibuffer-format-filter-group-data (filter)
  (if (equal filter "Default")
      ""
939 940 941
    (concat "Filter:" (mapconcat #'ibuffer-format-qualifier
				 (cdr (assq filter ibuffer-filter-groups))
				 " "))))
942

Colin Walters's avatar
Colin Walters committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
(defun ibuffer-format-qualifier (qualifier)
  (if (eq (car-safe qualifier) 'not)
      (concat " [NOT" (ibuffer-format-qualifier-1 (cdr qualifier)) "]")
    (ibuffer-format-qualifier-1 qualifier)))

(defun ibuffer-format-qualifier-1 (qualifier)
  (case (car qualifier)
    (saved
     (concat " [filter: " (cdr qualifier) "]"))
    (or
     (concat " [OR" (mapconcat #'ibuffer-format-qualifier
			       (cdr qualifier) "") "]"))
    (t
     (let ((type (assq (car qualifier) ibuffer-filtering-alist)))
       (unless qualifier
	 (error "Ibuffer: bad qualifier %s" qualifier))
       (concat " [" (cadr type) ": " (format "%s]" (cdr qualifier)))))))
960

961 962 963

(defun ibuffer-list-buffer-modes ()
  "Create an alist of buffer modes currently in use.
964
The list returned will be of the form (\"MODE-NAME\" . MODE-SYMBOL)."
965 966 967 968
  (let ((bufs (buffer-list))
	(modes)
	(this-mode))
    (while bufs
969 970
      (setq this-mode
	    (with-current-buffer
971 972 973
		(car bufs)
	      major-mode)
	    bufs (cdr bufs))
974
      (add-to-list
975
       'modes
976
       `(,(symbol-name this-mode) .
977
	 ,this-mode)))
978
    modes))
979 980


Colin Walters's avatar
Colin Walters committed
981 982
;;; Extra operation definitions

983
;;;###autoload (autoload 'ibuffer-filter-by-mode "ibuf-ext")
984
(define-ibuffer-filter mode
Colin Walters's avatar
Colin Walters committed
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
  "Toggle current view to buffers with major mode QUALIFIER."
  (:description "major mode"
   :reader
   (intern
    (completing-read "Filter by major mode: " obarray
		     #'(lambda (e)
			 (string-match "-mode$"
				       (symbol-name e)))
		     t
		     (let ((buf (ibuffer-current-buffer)))
		       (if (and buf (buffer-live-p buf))
			   (with-current-buffer buf
			     (symbol-name major-mode))
			 "")))))
  (eq qualifier (with-current-buffer buf major-mode)))

1001
;;;###autoload (autoload 'ibuffer-filter-by-used-mode "ibuf-ext")
1002
(define-ibuffer-filter used-mode
1003 1004 1005 1006 1007
  "Toggle current view to buffers with major mode QUALIFIER.
Called interactively, this function allows selection of modes
currently used by buffers."
  (:description "major mode in use"
		:reader
1008 1009
		(intern
		 (completing-read "Filter by major mode: "
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
				  (ibuffer-list-buffer-modes)
				  nil
				  t
				  (let ((buf (ibuffer-current-buffer)))
				    (if (and buf (buffer-live-p buf))
					(with-current-buffer buf
					  (symbol-name major-mode))
				      "")))))
  (eq qualifier (with-current-buffer buf major-mode)))

1020
;;;###autoload (autoload 'ibuffer-filter-by-name "ibuf-ext")
1021
(define-ibuffer-filter name
Colin Walters's avatar
Colin Walters committed
1022 1023
  "Toggle current view to buffers with name matching QUALIFIER."
  (:description "buffer name"
1024
   :reader (read-from-minibuffer "Filter by name (regexp): "))
Colin Walters's avatar
Colin Walters committed
1025 1026
  (string-match qualifier (buffer-name buf)))

1027
;;;###autoload (autoload 'ibuffer-filter-by-filename "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
1028 1029 1030
(define-ibuffer-filter filename
  "Toggle current view to buffers with filename matching QUALIFIER."
  (:description "filename"
1031
   :reader (read-from-minibuffer "Filter by filename (regexp): "))
1032
  (ibuffer-awhen (with-current-buffer buf
1033
		   (ibuffer-buffer-file-name))
1034
		 (string-match qualifier it)))
Colin Walters's avatar
Colin Walters committed
1035

1036
;;;###autoload (autoload 'ibuffer-filter-by-size-gt  "ibuf-ext")
1037
(define-ibuffer-filter size-gt
Colin Walters's avatar
Colin Walters committed
1038 1039 1040 1041 1042 1043 1044
  "Toggle current view to buffers with size greater than QUALIFIER."
  (:description "size greater than"
   :reader
   (string-to-number (read-from-minibuffer "Filter by size greater than: ")))
  (> (with-current-buffer buf (buffer-size))
     qualifier))

1045
;;;###autoload (autoload 'ibuffer-filter-by-size-lt  "ibuf-ext")
1046
(define-ibuffer-filter size-lt
Colin Walters's avatar
Colin Walters committed
1047 1048 1049 1050 1051 1052
   "Toggle current view to buffers with size less than QUALIFIER."
  (:description "size less than"
   :reader
   (string-to-number (read-from-minibuffer "Filter by size less than: ")))
  (< (with-current-buffer buf (buffer-size))
     qualifier))
1053

1054
;;;###autoload (autoload 'ibuffer-filter-by-content "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
1055 1056 1057
(define-ibuffer-filter content
   "Toggle current view to buffers whose contents match QUALIFIER."
  (:description "content"
1058
   :reader (read-from-minibuffer "Filter by content (regexp): "))
Colin Walters's avatar
Colin Walters committed
1059 1060 1061 1062 1063
  (with-current-buffer buf
    (save-excursion
      (goto-char (point-min))
      (re-search-forward qualifier nil t))))

1064
;;;###autoload (autoload 'ibuffer-filter-by-predicate "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
1065 1066 1067
(define-ibuffer-filter predicate
   "Toggle current view to buffers for which QUALIFIER returns non-nil."
  (:description "predicate"
1068
   :reader (read-minibuffer "Filter by predicate (form): "))
Colin Walters's avatar
Colin Walters committed
1069 1070 1071 1072 1073 1074 1075 1076
  (with-current-buffer buf
    (eval qualifier)))

;;; Sorting

;;;###autoload
(defun ibuffer-toggle-sorting-mode ()
  "Toggle the current sorting mode.
1077
Default sorting modes are:
Colin Walters's avatar
Colin Walters committed
1078 1079 1080 1081 1082
 Recency - the last time the buffer was viewed
 Name - the name of the buffer
 Major Mode - the name of the major mode of the buffer
 Size - the size of the buffer"
  (interactive)
1083 1084 1085
  (let ((modes (mapcar 'car ibuffer-sorting-functions-alist)))
    (add-to-list 'modes 'recency)
    (setq modes (sort modes 'string-lessp))
1086
    (let ((next (or (car-safe (cdr-safe (memq ibuffer-sorting-mode modes)))
1087 1088 1089
                    (car modes))))
      (setq ibuffer-sorting-mode next)
      (message "Sorting by %s" next)))
Colin Walters's avatar
Colin Walters committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  (ibuffer-redisplay t))

;;;###autoload
(defun ibuffer-invert-sorting ()
  "Toggle whether or not sorting is in reverse order."
  (interactive)
  (setq ibuffer-sorting-reversep (not ibuffer-sorting-reversep))
  (message "Sorting order %s"
	   (if ibuffer-sorting-reversep
	       "reversed"
	     "normal"))
  (ibuffer-redisplay t))

1103
;;;###autoload (autoload 'ibuffer-do-sort-by-major-mode "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
(define-ibuffer-sorter major-mode
  "Sort the buffers by major modes.
Ordering is lexicographic."
  (:description "major mode")
  (string-lessp (downcase
		 (symbol-name (with-current-buffer
				  (car a)
				major-mode)))
		(downcase
		 (symbol-name (with-current-buffer
				  (car b)
				major-mode)))))

1117
;;;###autoload (autoload 'ibuffer-do-sort-by-mode-name "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
1118 1119 1120
(define-ibuffer-sorter mode-name
  "Sort the buffers by their mode name.
Ordering is lexicographic."
Colin Walters's avatar
Colin Walters committed
1121
  (:description "major mode name")
Colin Walters's avatar
Colin Walters committed
1122
  (string-lessp (downcase
1123 1124
		  (with-current-buffer
		      (car a)
1125
		    (format-mode-line mode-name)))
Colin Walters's avatar
Colin Walters committed
1126
		(downcase
1127 1128
		 (with-current-buffer
		     (car b)
1129
		   (format-mode-line mode-name)))))
Colin Walters's avatar
Colin Walters committed
1130

1131
;;;###autoload (autoload 'ibuffer-do-sort-by-alphabetic "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
1132 1133 1134 1135 1136 1137 1138 1139
(define-ibuffer-sorter alphabetic
  "Sort the buffers by their names.
Ordering is lexicographic."
  (:description "buffer name")
  (string-lessp
   (buffer-name (car a))
   (buffer-name (car b))))

1140
;;;###autoload (autoload 'ibuffer-do-sort-by-size "ibuf-ext")
Colin Walters's avatar
Colin Walters committed
1141 1142 1143 1144 1145 1146 1147 1148
(define-ibuffer-sorter size
 "Sort the buffers by their size."
  (:description "size")
  (< (with-current-buffer (car a)
       (buffer-size))
     (with-current-buffer (car b)
       (buffer-size))))

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
;;;###autoload (autoload 'ibuffer-do-sort-by-filename/process "ibuf-ext")
(define-ibuffer-sorter filename/process
 "Sort the buffers by their file name/process name."
  (:description "file name")
  (string-lessp
   ;; FIXME: For now just compare the file name and the process name
   ;; (if it exists).  Is there a better way to do this?
   (or (buffer-file-name (car a)) 
       (let ((pr-a (get-buffer-process (car a))))
	 (and (processp pr-a) (process-name pr-a))))
   (or (buffer-file-name (car b)) 
       (let ((pr-b (get-buffer-process (car b))))
	 (and (processp pr-b) (process-name pr-b))))))

Colin Walters's avatar
Colin Walters committed
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
;;; Functions to emulate bs.el

;;;###autoload
(defun ibuffer-bs-show ()
  "Emulate `bs-show' from the bs.el package."
  (interactive)
  (ibuffer t "*Ibuffer-bs*" '((filename . ".*")) nil t)
  (define-key (current-local-map) "a" 'ibuffer-bs-toggle-all))

(defun ibuffer-bs-toggle-all ()
  "Emulate `bs-toggle-show-all' from the bs.el package."
  (interactive)
  (if ibuffer-filtering-qualifiers
      (ibuffer-pop-filter)
    (progn (ibuffer-push-filter '(filename . ".*"))
	   (ibuffer-update nil t))))

;;; Handy functions

;;;###autoload
(defun ibuffer-add-to-tmp-hide (regexp)
  "Add REGEXP to `ibuffer-tmp-hide-regexps'.
This means that buffers whose name matches REGEXP will not be shown
1186
for this Ibuffer session."
Colin Walters's avatar
Colin Walters committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
  (interactive
   (list
    (read-from-minibuffer "Never show buffers matching: "
			  (regexp-quote (buffer-name (ibuffer-current-buffer t))))))
  (push regexp ibuffer-tmp-hide-regexps))

;;;###autoload
(defun ibuffer-add-to-tmp-show (regexp)
  "Add REGEXP to `ibuffer-tmp-show-regexps'.
This means that buffers whose name matches REGEXP will always be shown
1197
for this Ibuffer session."
Colin Walters's avatar
Colin Walters committed
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231