X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/ab1dc14b220747e527d507d40905a24ba5c692d9..50650cb6887d99b01eeb1e686fc1f695c2a0c64a:/lisp/uniquify.el?ds=sidebyside diff --git a/lisp/uniquify.el b/lisp/uniquify.el index 3619d49941..a70c9188f5 100644 --- a/lisp/uniquify.el +++ b/lisp/uniquify.el @@ -1,9 +1,10 @@ ;;; uniquify.el --- unique buffer names dependent on file name -*- lexical-binding: t -*- -;; Copyright (C) 1989, 1995-1997, 2001-2012 Free Software Foundation, Inc. +;; Copyright (C) 1989, 1995-1997, 2001-2016 Free Software Foundation, +;; Inc. ;; Author: Dick King -;; Maintainer: FSF +;; Maintainer: emacs-devel@gnu.org ;; Keywords: files ;; Created: 15 May 86 ;; Package: emacs @@ -25,7 +26,7 @@ ;;; Commentary: -;; Emacs's standard method for making buffer names unique adds <2>, <3>, +;; Emacs's traditional 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 file name until the buffer names @@ -92,38 +93,34 @@ :group 'files) -(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', or `post-forward-angle-brackets'. -For example, files `/foo/bar/mumble/name' and `/baz/quux/mumble/name' +(defcustom uniquify-buffer-name-style 'post-forward-angle-brackets + "How to construct unique buffer names for files with the same base name. +The value can be one of: `forward', `reverse', `post-forward', +`post-forward-angle-brackets', or nil. + +For example, the 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 name - nil name name<2> -Of course, the \"mumble\" part may be stripped as well, depending on the setting -of `uniquify-strip-common-suffix'." + + 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 name + nil name name<2> + +The \"mumble\" part may be stripped as well, depending on the +setting of `uniquify-strip-common-suffix'. For more options that +you can set, browse the `uniquify' custom group." :type '(radio (const forward) (const reverse) (const post-forward) (const post-forward-angle-brackets) - (const :tag "standard Emacs behavior (nil)" nil)) - :require 'uniquify - :group 'uniquify) + (const :tag "numeric suffixes" nil)) + :version "24.4" + :require 'uniquify) (defcustom uniquify-after-kill-buffer-p t "If non-nil, rerationalize buffer names after a buffer has been killed." - :type 'boolean - :group 'uniquify) - -(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." - :type 'boolean - :group 'uniquify) + :type 'boolean) ;; The default value matches certain Gnus buffers. (defcustom uniquify-ignore-buffers-re nil @@ -131,13 +128,11 @@ other buffer names are changed." For instance, set this to \"^draft-[0-9]+$\" to avoid having uniquify rename draft buffers even if `uniquify-after-kill-buffer-p' is non-nil and the visited file name isn't the same as that of the buffer." - :type '(choice (const :tag "Uniquify all buffers" nil) regexp) - :group 'uniquify) + :type '(choice (const :tag "Uniquify all buffers" nil) regexp)) (defcustom uniquify-min-dir-content 0 "Minimum number of directory name components included in buffer name." - :type 'integer - :group 'uniquify) + :type 'integer) (defcustom uniquify-separator nil "String separator for buffer name components. @@ -145,16 +140,14 @@ 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 file name components (default \"\\\")." - :type '(choice (const nil) string) - :group 'uniquify) + :type '(choice (const nil) string)) (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) + :type 'boolean) (defcustom uniquify-strip-common-suffix ;; Using it when uniquify-min-dir-content>0 doesn't make much sense. @@ -163,8 +156,7 @@ variable is ignored." E.g. if you open /a1/b/c/d and /a2/b/c/d, the buffer names will say \"d|a1\" and \"d|a2\" instead of \"d|a1/b/c\" and \"d|a2/b/c\". This can be handy when you have deep parallel hierarchies." - :type 'boolean - :group 'uniquify) + :type 'boolean) (defvar uniquify-list-buffers-directory-modes '(dired-mode cvs-mode vc-dir-mode) "List of modes for which uniquify should obey `list-buffers-directory'. @@ -183,10 +175,9 @@ contains the name of the directory which the buffer is visiting.") ;; Internal variables used free (defvar uniquify-possibly-resolvable nil) -(defvar uniquify-managed nil +(defvar-local uniquify-managed nil "Non-nil if the name of this buffer is managed by uniquify. It actually holds the list of `uniquify-item's corresponding to the conflict.") -(make-variable-buffer-local 'uniquify-managed) (put 'uniquify-managed 'permanent-local t) ;; Used in desktop.el to save the non-uniquified buffer name @@ -464,27 +455,34 @@ For use on `kill-buffer-hook'." ;; rename-buffer and create-file-buffer. (Setting find-file-hook isn't ;; sufficient.) -(defadvice rename-buffer (after rename-buffer-uniquify activate) +(advice-add 'rename-buffer :around #'uniquify--rename-buffer-advice) +(defun uniquify--rename-buffer-advice (rb-fun newname &optional unique &rest args) "Uniquify buffer names with parts of directory name." + (let ((retval (apply rb-fun newname unique args))) (uniquify-maybe-rerationalize-w/o-cb) - (if (null (ad-get-arg 1)) ; no UNIQUE argument. + (if (null unique) ;; Mark this buffer so it won't be renamed by uniquify. (setq uniquify-managed nil) (when uniquify-buffer-name-style ;; Rerationalize w.r.t the new name. (uniquify-rationalize-file-buffer-names - (ad-get-arg 0) + newname (uniquify-buffer-file-name (current-buffer)) (current-buffer)) - (setq ad-return-value (buffer-name (current-buffer)))))) + (setq retval (buffer-name (current-buffer))))) + retval)) + -(defadvice create-file-buffer (after create-file-buffer-uniquify activate) +(advice-add 'create-file-buffer :around #'uniquify--create-file-buffer-advice) +(defun uniquify--create-file-buffer-advice (cfb-fun filename &rest args) "Uniquify buffer names with parts of directory name." + (let ((retval (apply cfb-fun filename args))) (if uniquify-buffer-name-style - (let ((filename (expand-file-name (directory-file-name (ad-get-arg 0))))) + (let ((filename (expand-file-name (directory-file-name filename)))) (uniquify-rationalize-file-buffer-names (file-name-nondirectory filename) - (file-name-directory filename) ad-return-value)))) + (file-name-directory filename) retval))) + retval)) ;;; The End @@ -496,9 +494,8 @@ For use on `kill-buffer-hook'." (set-buffer buf) (when uniquify-managed (push (cons buf (uniquify-item-base (car uniquify-managed))) buffers))) - (dolist (fun '(rename-buffer create-file-buffer)) - (ad-remove-advice fun 'after (intern (concat (symbol-name fun) "-uniquify"))) - (ad-update fun)) + (advice-remove 'rename-buffer #'uniquify--rename-buffer-advice) + (advice-remove 'create-file-buffer #'uniquify--create-file-buffer-advice) (dolist (buf buffers) (set-buffer (car buf)) (rename-buffer (cdr buf) t))))