(auto-mode-alist): Add .am as makefile-mode.
[bpt/emacs.git] / lisp / buff-menu.el
index 32ae224..b03ccdb 100644 (file)
@@ -1,6 +1,6 @@
 ;;; buff-menu.el --- buffer menu main function and support functions.
 
-;; Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 
@@ -17,8 +17,9 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
@@ -90,7 +91,8 @@
   (define-key Buffer-menu-mode-map "m" 'Buffer-menu-mark)
   (define-key Buffer-menu-mode-map "t" 'Buffer-menu-visit-tags-table)
   (define-key Buffer-menu-mode-map "%" 'Buffer-menu-toggle-read-only)
-  (define-key Buffer-menu-mode-map "g" 'revert-buffer)
+  (define-key Buffer-menu-mode-map "b" 'Buffer-menu-bury)
+  (define-key Buffer-menu-mode-map "g" 'Buffer-menu-revert)
   (define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-mouse-select)
 )
 
@@ -122,50 +124,41 @@ Letters do not insert themselves; instead, they are commands.
 \\[Buffer-menu-unmark] -- remove all kinds of marks from current line.
   With prefix argument, also move up one line.
 \\[Buffer-menu-backup-unmark] -- back up a line and remove marks.
-\\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line."
+\\[Buffer-menu-toggle-read-only] -- toggle read-only status of buffer on this line.
+\\[Buffer-menu-revert] -- update the list of buffers.
+\\[Buffer-menu-bury] -- bury the buffer listed on this line."
   (kill-all-local-variables)
   (use-local-map Buffer-menu-mode-map)
   (setq major-mode 'Buffer-menu-mode)
   (setq mode-name "Buffer Menu")
-  (save-excursion
-    (goto-char (point-min))
-    (search-forward "Buffer")
-    (backward-word 1)
-    (setq Buffer-menu-buffer-column (current-column))
-    (forward-line 2)
-    (while (not (eobp))
-      (let ((where (Buffer-menu-buffer-name-position)))
-       (put-text-property (car where) (cdr where) 'mouse-face 'highlight))
-      (forward-line 1)))
   (make-local-variable 'revert-buffer-function)
   (setq revert-buffer-function 'Buffer-menu-revert-function)
   (setq truncate-lines t)
   (setq buffer-read-only t)
   (run-hooks 'buffer-menu-mode-hook))
 
+(defun Buffer-menu-revert ()
+  "Update the list of buffers."
+  (interactive)
+  (revert-buffer))
+
 (defun Buffer-menu-revert-function (ignore1 ignore2)
   (list-buffers))
 \f
 (defun Buffer-menu-buffer (error-if-non-existent-p)
   "Return buffer described by this line of buffer menu."
-  (let* ((where (Buffer-menu-buffer-name-position))
-        (string (buffer-substring (car where) (cdr where))))
-    (set-text-properties 0 (length string) nil string)
-    (or (get-buffer string)
-       (if error-if-non-existent-p
-           (error "No buffer named \"%s\"" string)
-         nil))))
-
-;; Find the start and end positions of the buffer name on this line.
-;; Returns a cons (START . END).
-(defun Buffer-menu-buffer-name-position ()
-  (save-excursion
-    (beginning-of-line)
-    (forward-char Buffer-menu-buffer-column)
-    (let ((start (point)))
-      (re-search-forward "\t\\|  ")
-      (skip-chars-backward " \t")
-      (cons start (point)))))
+  (let* ((where (save-excursion
+                 (beginning-of-line)
+                 (+ (point) Buffer-menu-buffer-column)))
+        (name (and (not (eobp)) (get-text-property where 'buffer-name))))
+    (if name
+       (or (get-buffer name)
+           (if error-if-non-existent-p
+               (error "No buffer named `%s'" name)
+             nil))
+      (if error-if-non-existent-p
+         (error "No buffer on this line")
+       nil))))
 \f
 (defun buffer-menu (&optional arg)
   "Make a menu of buffers so you can save, delete or select them.
@@ -434,6 +427,21 @@ The current window remains selected."
             (delete-char 1)
             (insert char))))))
 
+(defun Buffer-menu-bury ()
+  "Bury the buffer listed on this line."
+  (interactive)
+  (beginning-of-line)
+  (if (looking-at " [-M]")             ;header lines
+      (ding)
+    (save-excursion
+      (beginning-of-line)
+      (bury-buffer (Buffer-menu-buffer t))
+      (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))
+            (buffer-read-only nil))
+        (delete-region (point) (progn (forward-line -1) (point)))
+        (goto-char (point-max))
+        (insert line))
+      (message "Buried buffer moved to the end"))))
 \f
 
 (define-key ctl-x-map "\C-b" 'list-buffers)
@@ -469,11 +477,14 @@ The R column contains a % for buffers that are read-only."
  MR Buffer           Size  Mode         File
  -- ------           ----  ----         ----
 ")
+      ;; Record the column where buffer names start.
+      (setq Buffer-menu-buffer-column 4)
       (let ((bl (buffer-list)))
        (while bl
          (let* ((buffer (car bl))
                 (name (buffer-name buffer))
                 (file (buffer-file-name buffer))
+                this-buffer-line-start
                 this-buffer-read-only
                 this-buffer-size
                 this-buffer-mode-name
@@ -498,6 +509,7 @@ The R column contains a % for buffers that are read-only."
             ((and files-only (not file)))
             ;; Otherwise output info.
             (t
+             (setq this-buffer-line-start (point))
              ;; Identify current buffer.
              (if (eq buffer old-buffer)
                  (progn
@@ -507,14 +519,24 @@ The R column contains a % for buffers that are read-only."
              ;; Identify modified buffers.
              (princ (if (buffer-modified-p buffer) "*" " "))
              ;; Handle readonly status.  The output buffer is special
-             ;; cased to be readonly; it is actually made so at a later
+             ;; cased to appear readonly; it is actually made so at a later
              ;; date.
              (princ (if (or (eq buffer standard-output)
                             this-buffer-read-only)
                         "% "
                       "  "))
              (princ name)
-             (indent-to 17 2)
+             ;; Put the buffer name into a text property
+             ;; so we don't have to extract it from the text.
+             ;; This way we avoid problems with unusual buffer names.
+             (setq this-buffer-line-start
+                   (+ this-buffer-line-start Buffer-menu-buffer-column))
+             (let ((name-end (point)))
+               (indent-to 17 2)
+               (put-text-property this-buffer-line-start name-end
+                                  'buffer-name name)
+               (put-text-property this-buffer-line-start name-end
+                                  'mouse-face 'highlight))
              (let (size
                    mode
                    (excess (- (current-column) 17)))