]> code.delx.au - gnu-emacs/blobdiff - lisp/uniquify.el
(uniquify-buffer-file-name): Minor change.
[gnu-emacs] / lisp / uniquify.el
index acf6f93e27b7b4a139b49cd6fdc7581a97d08bfa..33d31987979933487e2901fdfaa110ccfeb51312 100644 (file)
@@ -1,6 +1,6 @@
-;;; uniquify.el --- unique buffer names dependent on pathname
+;;; uniquify.el --- unique buffer names dependent on file name
 
-;; Copyright (c) 1989, 1995 Free Software Foundation, Inc.
+;; Copyright (c) 1989, 1995, 1996, 1997 Free Software Foundation, Inc.
 
 ;; Author: Dick King <king@reasoning.com>
 ;; Maintainer: Michael Ernst <mernst@theory.lcs.mit.edu>
 ;; Emacs's standard method for making buffer names unique adds <2>, <3>,
 ;; etc. to the end of (all but one of) the buffers.  This file replaces
 ;; that behavior, for buffers visiting files and dired buffers, with a
-;; uniquification that adds parts of the pathname until the buffer names
+;; uniquification that adds parts of the file name until the buffer names
 ;; are unique.  For instance, buffers visiting /u/mernst/tmp/Makefile and
 ;; /usr/projects/zaphod/Makefile would be named Makefile|tmp and
 ;; Makefile|zaphod, respectively (instead of Makefile and Makefile<2>).
 ;; Other buffer name styles are also available.
 
-;; To use this file, just load it.
-;; To disable it after loading, set variable uniquify-buffer-name-style to nil.
+;; To use this file, do (require 'uniquify)
+;; and set uniquify-buffer-name-style to one of its non-nil alternative values.
+
 ;; For other options, see "User-visible variables", below.
 
 ;; A version of uniquify.el that works under Emacs 18, Emacs 19, XEmacs,
 ;; and InfoDock is available from the maintainer.
 
+;; Doesn't correctly handle buffer names created by M-x write-file in Emacs 18.
+;; Doesn't work under NT when backslash is used as a path separator (forward
+;;   slash path separator works fine).  To fix, check system-type against
+;;   'windows-nt, write a routine that breaks paths down into components.
+;;   (Surprisingly, there isn't one built in.)
+
 ;;; Change Log:
 
 ;; Originally by Dick King <king@reasoning.com> 15 May 86
 ;;  uniquify-buffer-name-style; add 'forward and 'post-forward-angle-brackets
 ;;  styles; remove uniquify-reverse-dir-content-p; add
 ;;  uniquify-trailing-separator-p.  mernst 4 Aug 95
+;; Don't call expand-file-name on nil.  mernst 7 Jan 96
+;; Check whether list-buffers-directory is bound.  mernst 11 Oct 96
+;; Ignore non-file non-dired buffers. Colin Rafferty <craffert@ml.com> 3 Mar 97
 
 ;; Valuable feedback was provided by
 ;; Paul Smith <psmith@baynetworks.com>,
 ;; Alastair Burt <burt@dfki.uni-kl.de>,
 ;; Bob Weiner <weiner@footloose.sps.mot.com>,
 ;; Albert L. Ting <alt@vlibs.com>,
-;; gyro@reasoning.com.
+;; gyro@reasoning.com,
+;; Bryan O'Sullivan <bos@eng.sun.com>.
 
 
 ;;; Code:
 
 ;;; User-visible variables
 
-(defvar uniquify-buffer-name-style 'post-forward
+(defgroup uniquify nil
+  "Unique buffer names dependent on file name"
+  :group 'applications)
+
+
+(defcustom uniquify-buffer-name-style nil
   "*If non-nil, buffer names are uniquified with parts of directory name.
-The value determines the buffer name style and is one of 'forward,
-'reverse, 'post-forward (the default), or 'post-forward-angle-brackets.
-For example, files /foo/bar/mumble/name and /baz/quux/mumble/name
-would be in the following buffers:
+The value determines the buffer name style and is one of `forward',
+`reverse', `post-forward' (the default), or `post-forward-angle-brackets'.
+For example, files `/foo/bar/mumble/name' and `/baz/quux/mumble/name'
+would have the following buffer names in the various styles:
   forward        bar/mumble/name  quux/mumble/name
   reverse        name\\mumble\\bar  name\\mumble\\quux
   post-forward   name|bar/mumble  name|quux/mumble
   post-forward-angle-brackets   name<bar/mumble>  name<quux/mumble>
-  nil            name  name<2>")
-
-(defvar uniquify-after-kill-buffer-p nil
+  nil            name  name<2>"
+  :type '(radio (const forward)
+               (const reverse)
+               (const post-forward)
+               (const podt-forward-angle-brackets)
+               (const :tag "standard Emacs behavior (nil)" nil))
+  :group 'uniquify)
+
+(defcustom uniquify-after-kill-buffer-p nil
   "*If non-nil, rerationalize buffer names after a buffer has been killed.
 This can be dangerous if Emacs Lisp code is keeping track of buffers by their
-names (rather than keeping pointers to the buffers themselves).")
+names (rather than keeping pointers to the buffers themselves)."
+  :type 'boolean
+  :group 'uniquify)
 
-(defconst uniquify-ask-about-buffer-names-p nil
+(defcustom uniquify-ask-about-buffer-names-p nil
   "*If non-nil, permit user to choose names for buffers with same base file.
 If the user chooses to name a buffer, uniquification is preempted and no
-other buffer names are changed.")
+other buffer names are changed."
+  :type 'boolean
+  :group 'uniquify)
 
-(defvar uniquify-min-dir-content 0
-  "*Minimum parts of directory pathname included in buffer name.")
+(defcustom uniquify-min-dir-content 0
+  "*Minimum parts of directory name included in buffer name."
+  :type 'integer
+  :group 'uniquify)
 
-(defvar uniquify-separator nil
+(defcustom uniquify-separator nil
   "*String separator for buffer name components.
-When `uniquify-buffer-name-style' is 'post-forward, separates
+When `uniquify-buffer-name-style' is `post-forward', separates
 base file name from directory part in buffer names (default \"|\").
-When `uniquify-buffer-name-style' is 'reverse, separates all
-pathname components (default \"\\\").")
+When `uniquify-buffer-name-style' is `reverse', separates all
+file name components (default \"\\\")."
+  :type '(choice (const nil) string)
+  :group 'uniquify)
 
-(defvar uniquify-trailing-separator-p nil
-  "*If non-nil, add a pathname separator to dired buffer names.
-If `uniquify-buffer-name-style' is 'forward, add the separator at the end;
-if it's is 'reverse, add the separator at the beginning; otherwise, this
-variable is ignored.")
+(defcustom uniquify-trailing-separator-p nil
+  "*If non-nil, add a file name separator to dired buffer names.
+If `uniquify-buffer-name-style' is `forward', add the separator at the end;
+if it is `reverse', add the separator at the beginning; otherwise, this
+variable is ignored."
+  :type 'boolean
+  :group 'uniquify)
 
 
 ;;; Utilities
@@ -137,9 +169,9 @@ variable is ignored.")
 ;;; Main entry point.
 
 (defun uniquify-rationalize-file-buffer-names (&optional newbuffile newbuf)
-  "Makes file buffer names unique by adding segments from pathname.
+  "Makes file buffer names unique by adding segments from file name.
 If `uniquify-min-dir-content' > 0, always pulls that many
-pathname elements.  Arguments cause only a subset of buffers to be renamed."
+file name elements.  Arguments cause only a subset of buffers to be renamed."
   (interactive)
   (let (fix-list
        uniquify-non-file-buffer-names
@@ -172,15 +204,27 @@ pathname elements.  Arguments cause only a subset of buffers to be renamed."
 ;; uniquify's version of buffer-file-name
 (defun uniquify-buffer-file-name (buffer)
   "Return name of file BUFFER is visiting, or nil if none.
-Works on dired buffers as well as ordinary file-visiting buffers."
+Works on dired buffers as well as ordinary file-visiting buffers,
+but no others."
   (or (buffer-file-name buffer)
+      (and (featurep 'dired)
       (save-excursion
        (set-buffer buffer)
-       list-buffers-directory)))
+       (when (eq major-mode 'dired-mode) ; do nothing if not a dired buffer
+         (if (boundp 'list-buffers-directory) ; XEmacs mightn't define this
+             list-buffers-directory
+           ;; don't use default-directory if dired-directory is nil
+           (and dired-directory
+                (expand-file-name
+                 (directory-file-name
+                  (if (consp dired-directory)
+                      (car dired-directory)
+                    dired-directory))))))))))
 
 (defun uniquify-fix-list-filename-lessp (fixlist1 fixlist2)
   (uniquify-filename-lessp
-   (uniquify-fix-list-filename fixlist1) (uniquify-fix-list-filename fixlist2)))
+   (uniquify-fix-list-filename fixlist1
+                              ) (uniquify-fix-list-filename fixlist2)))
 
 ;; This examines the filename components in reverse order.
 (defun uniquify-filename-lessp (s1 s2)
@@ -316,7 +360,7 @@ Works on dired buffers as well as ordinary file-visiting buffers."
 
 ;;; Hooks from the rest of Emacs
 
-;; Emacs 19 (GNU Emacs or XEmacs)
+;; Emacs 19 (Emacs or XEmacs)
 
 ;; The logical place to put all this code is in generate-new-buffer-name.
 ;; It's written in C, so we would add a generate-new-buffer-name-function
@@ -369,7 +413,9 @@ For use on, eg, `kill-buffer-hook', to rationalize *after* buffer deletion."
   (if (and uniquify-buffer-name-style
           uniquify-after-kill-buffer-p)
       (add-hook 'post-command-hook
-               'delayed-uniquify-rationalize-file-buffer-names)))
+               'delayed-uniquify-rationalize-file-buffer-names)
+    (remove-hook 'kill-buffer-hook
+                'delay-uniquify-rationalize-file-buffer-names)))
 
 (defun delayed-uniquify-rationalize-file-buffer-names ()
   "Rerationalize buffer names and remove self from `post-command-hook'.
@@ -381,4 +427,3 @@ See also `delay-rationalize-file-buffer-names' for hook setter."
 (add-hook 'kill-buffer-hook 'delay-uniquify-rationalize-file-buffer-names)
 
 ;;; uniquify.el ends here
-