aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2003-05-07 16:03:14 +0000
committerStefan Monnier2003-05-07 16:03:14 +0000
commit518e35e6d1bfa72a4321dd2f50ff3e221a07142b (patch)
tree4b9cceae1157d8939091b986d06a63ef57ce11e0
parentbf0e40d484f2ddbe9ccc52f7bc71a4e8902a018f (diff)
downloademacs-518e35e6d1bfa72a4321dd2f50ff3e221a07142b.tar.gz
emacs-518e35e6d1bfa72a4321dd2f50ff3e221a07142b.zip
(uniquify-rationalize): New fun. Store the fix-list in uniquify-managed.
(uniquify-rationalize-file-buffer-names): Use it and make the args non-optional (i.e. don't support "re-rationalize all" any more). (uniquify-rerationalize-w/o-cb): New fun. (uniquify-maybe-rerationalize-w/o-cb): Use it to rerationalize immediately and only the relevant buffers. Merged from uniquify-delay-rationalize-file-buffer-names and uniquify-delayed-rationalize-file-buffer-names. (kill-buffer-hook, rename-buffer): Use it.
-rw-r--r--lisp/uniquify.el84
1 files changed, 45 insertions, 39 deletions
diff --git a/lisp/uniquify.el b/lisp/uniquify.el
index 4ef051d4e2d..cf5155c9615 100644
--- a/lisp/uniquify.el
+++ b/lisp/uniquify.el
@@ -167,25 +167,25 @@ contains the name of the directory which the buffer is visiting.")
167(defvar uniquify-possibly-resolvable nil) 167(defvar uniquify-possibly-resolvable nil)
168 168
169(defvar uniquify-managed nil 169(defvar uniquify-managed nil
170 "Non-nil if the name of this buffer is managed by uniquify.") 170 "Non-nil if the name of this buffer is managed by uniquify.
171It actually holds the list of `uniquify-item's corresponding to the conflict.")
171(make-variable-buffer-local 'uniquify-managed) 172(make-variable-buffer-local 'uniquify-managed)
172(put 'uniquify-managed 'permanent-local t) 173(put 'uniquify-managed 'permanent-local t)
173 174
174;;; Main entry point. 175;;; Main entry point.
175 176
176(defun uniquify-rationalize-file-buffer-names (&optional newbuffile newbuf) 177(defun uniquify-rationalize-file-buffer-names (newbuffile newbuf)
177 "Make file buffer names unique by adding segments from file name. 178 "Make file buffer names unique by adding segments from file name.
178If `uniquify-min-dir-content' > 0, always pulls that many 179If `uniquify-min-dir-content' > 0, always pulls that many
179file name elements. 180file name elements.
180Arguments NEWBUFFILE and NEWBUF cause only a subset of buffers to be renamed." 181Arguments NEWBUFFILE and NEWBUF cause only a subset of buffers to be renamed."
181 (interactive) 182 (interactive)
182 (when newbuffile 183 (setq newbuffile (expand-file-name (directory-file-name newbuffile)))
183 (setq newbuffile (expand-file-name (directory-file-name newbuffile))))
184 (let ((fix-list nil) 184 (let ((fix-list nil)
185 (base (and newbuffile (file-name-nondirectory newbuffile)))) 185 (base (file-name-nondirectory newbuffile)))
186 (dolist (buffer (buffer-list)) 186 (dolist (buffer (buffer-list))
187 (let ((bufname (buffer-name buffer)) 187 (let ((bufname (buffer-name buffer))
188 bfn rawname) 188 bfn)
189 (when (and (not (and uniquify-ignore-buffers-re 189 (when (and (not (and uniquify-ignore-buffers-re
190 (string-match uniquify-ignore-buffers-re 190 (string-match uniquify-ignore-buffers-re
191 bufname))) 191 bufname)))
@@ -194,20 +194,15 @@ Arguments NEWBUFFILE and NEWBUF cause only a subset of buffers to be renamed."
194 (eq buffer newbuf)) 194 (eq buffer newbuf))
195 (setq bfn (if (eq buffer newbuf) newbuffile 195 (setq bfn (if (eq buffer newbuf) newbuffile
196 (uniquify-buffer-file-name buffer))) 196 (uniquify-buffer-file-name buffer)))
197 (setq rawname (file-name-nondirectory bfn)) 197 (equal (file-name-nondirectory bfn) base))
198 (or (null base) (equal rawname base)))
199 (when (setq bfn (file-name-directory bfn)) ;Strip off the `base'. 198 (when (setq bfn (file-name-directory bfn)) ;Strip off the `base'.
200 (setq bfn (directory-file-name bfn))) ;Strip trailing slash. 199 (setq bfn (directory-file-name bfn))) ;Strip trailing slash.
201 (push (uniquify-make-item rawname bfn buffer 200 (push (uniquify-make-item base bfn buffer
202 (uniquify-get-proposed-name rawname bfn)) 201 (uniquify-get-proposed-name base bfn))
203 fix-list)))) 202 fix-list))))
204 ;; Mark the new buffer as managed.
205 (when newbuf
206 (with-current-buffer newbuf
207 (setq uniquify-managed t)))
208 ;; selects buffers whose names may need changing, and others that 203 ;; selects buffers whose names may need changing, and others that
209 ;; may conflict, then bring conflicting names together 204 ;; may conflict, then bring conflicting names together
210 (uniquify-rationalize-a-list fix-list))) 205 (uniquify-rationalize fix-list)))
211 206
212;; uniquify's version of buffer-file-name; result never contains trailing slash 207;; uniquify's version of buffer-file-name; result never contains trailing slash
213(defun uniquify-buffer-file-name (buffer) 208(defun uniquify-buffer-file-name (buffer)
@@ -229,6 +224,30 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil."
229 (car dired-directory) 224 (car dired-directory)
230 dired-directory))))))))) 225 dired-directory)))))))))
231 226
227(defun uniquify-rerationalize-w/o-cb (fix-list)
228 "Re-rationalize the buffers in FIX-LIST, but ignoring current-buffer."
229 (let ((new-fix-list nil))
230 (dolist (item fix-list)
231 (let ((buf (uniquify-item-buffer item)))
232 (unless (or (eq buf (current-buffer)) (not (buffer-live-p buf)))
233 ;; Reset the proposed names.
234 (setf (uniquify-item-proposed item)
235 (uniquify-get-proposed-name (uniquify-item-base item)
236 (uniquify-item-dirname item)))
237 (push item new-fix-list))))
238 (when new-fix-list
239 (uniquify-rationalize new-fix-list))))
240
241(defun uniquify-rationalize (fix-list)
242 ;; Set up uniquify to re-rationalize after killing/renaming
243 ;; if there is a conflict.
244 (dolist (fix fix-list)
245 (with-current-buffer (uniquify-item-buffer fix)
246 (setq uniquify-managed fix-list)))
247 ;; If uniquify-min-dir-content is 0, this will end up just
248 ;; passing fix-list to uniquify-rationalize-conflicting-sublist.
249 (uniquify-rationalize-a-list fix-list))
250
232(defun uniquify-item-greaterp (item1 item2) 251(defun uniquify-item-greaterp (item1 item2)
233 (string-lessp (uniquify-item-proposed item2) 252 (string-lessp (uniquify-item-proposed item2)
234 (uniquify-item-proposed item1))) 253 (uniquify-item-proposed item1)))
@@ -356,17 +375,14 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil."
356 375
357(defadvice rename-buffer (after rename-buffer-uniquify activate) 376(defadvice rename-buffer (after rename-buffer-uniquify activate)
358 "Uniquify buffer names with parts of directory name." 377 "Uniquify buffer names with parts of directory name."
378 (uniquify-maybe-rerationalize-w/o-cb)
359 (if (null (ad-get-arg 1)) ; no UNIQUE argument. 379 (if (null (ad-get-arg 1)) ; no UNIQUE argument.
360 ;; Mark this buffer so it won't be renamed by uniquify. 380 ;; Mark this buffer so it won't be renamed by uniquify.
361 (setq uniquify-managed nil) 381 (setq uniquify-managed nil)
362 (when uniquify-buffer-name-style 382 (when uniquify-buffer-name-style
363 (if uniquify-after-kill-buffer-p 383 ;; Rerationalize w.r.t the new name.
364 ;; call with no argument; rationalize vs. old name as well as new 384 (uniquify-rationalize-file-buffer-names
365 (progn (setq uniquify-managed t) 385 (uniquify-buffer-file-name (current-buffer)) (current-buffer))
366 (uniquify-rationalize-file-buffer-names))
367 ;; call with argument: rationalize vs. new name only
368 (uniquify-rationalize-file-buffer-names
369 (uniquify-buffer-file-name (current-buffer)) (current-buffer)))
370 (setq ad-return-value (buffer-name (current-buffer)))))) 386 (setq ad-return-value (buffer-name (current-buffer))))))
371 387
372(defadvice create-file-buffer (after create-file-buffer-uniquify activate) 388(defadvice create-file-buffer (after create-file-buffer-uniquify activate)
@@ -385,27 +401,17 @@ in `uniquify-list-buffers-directory-modes', otherwise returns nil."
385;; (This ought to set some global variables so the work is done only for 401;; (This ought to set some global variables so the work is done only for
386;; buffers with names similar to the deleted buffer. -MDE) 402;; buffers with names similar to the deleted buffer. -MDE)
387 403
388(defun uniquify-delay-rationalize-file-buffer-names () 404(defun uniquify-maybe-rerationalize-w/o-cb ()
389 "Add `delayed-uniquify-rationalize-file-buffer-names' to `post-command-hook'. 405 "Re-rationalize buffer names, ignoring current buffer.
390For use on, eg, `kill-buffer-hook', to rationalize *after* buffer deletion." 406For use on `kill-buffer-hook'."
391 (if (and uniquify-managed 407 (if (and (cdr uniquify-managed)
392 uniquify-buffer-name-style 408 uniquify-buffer-name-style
393 uniquify-after-kill-buffer-p 409 uniquify-after-kill-buffer-p)
394 ;; Rationalizing is costly, so don't do it for temp buffers. 410 (uniquify-rerationalize-w/o-cb uniquify-managed)))
395 (uniquify-buffer-file-name (current-buffer)))
396 (add-hook 'post-command-hook
397 'uniquify-delayed-rationalize-file-buffer-names)))
398
399(defun uniquify-delayed-rationalize-file-buffer-names ()
400 "Rerationalize buffer names and remove self from `post-command-hook'.
401See also `delay-rationalize-file-buffer-names' for hook setter."
402 (uniquify-rationalize-file-buffer-names)
403 (remove-hook 'post-command-hook
404 'uniquify-delayed-rationalize-file-buffer-names))
405 411
406;; Ideally we'd like to add it buffer-locally, but that doesn't work 412;; Ideally we'd like to add it buffer-locally, but that doesn't work
407;; because kill-buffer-hook is not permanent-local :-( 413;; because kill-buffer-hook is not permanent-local :-(
408(add-hook 'kill-buffer-hook 'uniquify-delay-rationalize-file-buffer-names) 414(add-hook 'kill-buffer-hook 'uniquify-maybe-rerationalize-w/o-cb)
409 415
410(provide 'uniquify) 416(provide 'uniquify)
411;;; uniquify.el ends here 417;;; uniquify.el ends here