changeset 50814:28d06c5071e1

Move provide to end of file. (uniquify-ref-base, uniquify-ref-filename, uniquify-ref-buffer) (uniquify-ref-proposed, uniquify-set-proposed): Remove. (uniquify-item): New struct. Update users of uniquify-ref-*. (uniquify-get-proposed-name, uniquify-rationalize-a-list): Make `depth' optional. (uniquify-non-file-buffer-names): Remove. (uniquify-rationalize-file-buffer-names): Don't set it up. Use uniquify-make-item and don't pass the now-optional depth. (uniquify-rationalize-conflicting-sublist): Check the new buffer name directly instead of relying on uniquify-non-file-buffer-names. (uniquify-rename-buffer): Use with-current-buffer and pass the `unique' arg to rename-buffer.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sun, 04 May 2003 21:30:25 +0000
parents 7d0357cde5ff
children 4972b38db032
files lisp/uniquify.el
diffstat 1 files changed, 33 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/uniquify.el	Sun May 04 20:24:33 2003 +0000
+++ b/lisp/uniquify.el	Sun May 04 21:30:25 2003 +0000
@@ -84,7 +84,6 @@
 
 ;;; Code:
 
-(provide 'uniquify)
 (eval-when-compile (require 'cl))
 
 ;;; User-visible variables
@@ -170,14 +169,12 @@
   (file-name-nondirectory (directory-file-name file-name)))
 
 ;; uniquify-fix-list data structure
-(defsubst uniquify-ref-base (x) (aref x 0))
-(defsubst uniquify-ref-filename (x) (aref x 1))
-(defsubst uniquify-ref-buffer (x) (aref x 2))
-(defsubst uniquify-ref-proposed (x) (aref x 3))
-(defsubst uniquify-set-proposed (x p) (aset x 3 p))
+(defstruct (uniquify-item
+	    (:constructor nil) (:copier nil)
+	    (:constructor uniquify-make-item (base filename buffer proposed)))
+  base filename buffer proposed)
 
 ;; Internal variables used free
-(defvar uniquify-non-file-buffer-names nil)
 (defvar uniquify-possibly-resolvable nil)
 
 ;;; Main entry point.
@@ -189,7 +186,6 @@
 Arguments NEWBUFFILE and NEWBUF cause only a subset of buffers to be renamed."
   (interactive)
   (let (fix-list
-	uniquify-non-file-buffer-names
 	(newbuffile-nd (and newbuffile
 			    (uniquify-file-name-nondirectory newbuffile))))
     (dolist (buffer (buffer-list))
@@ -206,13 +202,11 @@
 		 (setq rawname (uniquify-file-name-nondirectory bfn))
 		 (or (not newbuffile)
 		     (equal rawname newbuffile-nd))
-		 (setq proposed (uniquify-get-proposed-name
-				 rawname bfn uniquify-min-dir-content)))
-	    (push (vector rawname bfn buffer proposed) fix-list)
-	  (push bufname uniquify-non-file-buffer-names))))
+		 (setq proposed (uniquify-get-proposed-name rawname bfn)))
+	    (push (uniquify-make-item rawname bfn buffer proposed) fix-list))))
     ;; selects buffers whose names may need changing, and others that
     ;; may conflict, then bring conflicting names together
-    (uniquify-rationalize-a-list fix-list uniquify-min-dir-content)))
+    (uniquify-rationalize-a-list fix-list)))
 
 ;; uniquify's version of buffer-file-name; result never contains trailing slash
 (defun uniquify-buffer-file-name (buffer)
@@ -235,15 +229,18 @@
 		    dired-directory)))))))))
 
 (defun uniquify-item-greaterp (item1 item2)
-  (string-lessp (uniquify-ref-proposed item2)
-		(uniquify-ref-proposed item1)))
+  (string-lessp (uniquify-item-proposed item2)
+		(uniquify-item-proposed item1)))
 
-(defun uniquify-rationalize-a-list (fix-list depth)
+(defun uniquify-rationalize-a-list (fix-list &optional depth)
+  (unless depth (setq depth uniquify-min-dir-content))
   (let (conflicting-sublist	; all elements have the same proposed name
 	(old-proposed "")
 	proposed)
+    ;; Divide fix-list into items with same proposed names and pass them
+    ;; to uniquify-rationalize-conflicting-sublist.
     (dolist (item (sort fix-list 'uniquify-item-greaterp))
-      (setq proposed (uniquify-ref-proposed item))
+      (setq proposed (uniquify-item-proposed item))
       (unless (equal proposed old-proposed)
 	(uniquify-rationalize-conflicting-sublist conflicting-sublist
 						  old-proposed depth)
@@ -253,7 +250,8 @@
     (uniquify-rationalize-conflicting-sublist conflicting-sublist
 					      old-proposed depth)))
 
-(defun uniquify-get-proposed-name (base filename depth)
+(defun uniquify-get-proposed-name (base filename &optional depth)
+  (unless depth (setq depth uniquify-min-dir-content))
   (assert (equal base (uniquify-file-name-nondirectory filename)))
   (assert (equal (directory-file-name filename) filename))
 
@@ -315,29 +313,31 @@
 (defun uniquify-rationalize-conflicting-sublist (conf-list old-name depth)
   (when conf-list
     (if (or (cdr conf-list)
-	    (member old-name uniquify-non-file-buffer-names))
+	    ;; Check that the proposed name doesn't conflict with some
+	    ;; existing buffer.
+	    (let ((buf (get-buffer old-name)))
+	      (and buf (not (eq buf (uniquify-item-buffer (car conf-list)))))))
 	(when uniquify-possibly-resolvable
 	  (setq uniquify-possibly-resolvable nil
 		depth (1+ depth))
 	  (dolist (item conf-list)
-	    (uniquify-set-proposed item (uniquify-get-proposed-name
-					 (uniquify-ref-base item)
-					 (uniquify-ref-filename item)
-					 depth)))
+	    (setf (uniquify-item-proposed item)
+		  (uniquify-get-proposed-name
+		   (uniquify-item-base item)
+		   (uniquify-item-filename item)
+		   depth)))
 	  (uniquify-rationalize-a-list conf-list depth))
       (unless (string= old-name "")
 	(uniquify-rename-buffer (car conf-list) old-name)))))
 
 
 (defun uniquify-rename-buffer (item newname)
-  (let ((buffer (uniquify-ref-buffer item)))
+  (let ((buffer (uniquify-item-buffer item)))
     (unless (equal newname (buffer-name buffer))
-      (let ((unset (current-buffer))
-	    ;; avoid hooks on rename-buffer
-	    (uniquify-buffer-name-style nil))
-	(set-buffer buffer)
-	(rename-buffer newname)
-	(set-buffer unset)))))
+      (with-current-buffer buffer
+	(let ((uniquify-buffer-name-style nil))	;Avoid hooks on rename-buffer.
+	  ;; Pass the `unique' arg, just in case.
+	  (rename-buffer newname t))))))
 
 ;;; Hooks from the rest of Emacs
 
@@ -403,6 +403,9 @@
   (remove-hook 'post-command-hook
 	       'uniquify-delayed-rationalize-file-buffer-names))
 
+;; Ideally we'd like to add it buffer-locally, but that doesn't work
+;; because kill-buffer-hook is not permanent-local :-(
 (add-hook 'kill-buffer-hook 'uniquify-delay-rationalize-file-buffer-names)
 
+(provide 'uniquify)
 ;;; uniquify.el ends here