comparison lisp/uniquify.el @ 50877:c701779986a1

(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.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Wed, 07 May 2003 16:03:14 +0000
parents 3311a93359a2
children 7b95d60a7574
comparison
equal deleted inserted replaced
50876:3311a93359a2 50877:c701779986a1
165 165
166 ;; Internal variables used free 166 ;; Internal variables used free
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.
171 It 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.
178 If `uniquify-min-dir-content' > 0, always pulls that many 179 If `uniquify-min-dir-content' > 0, always pulls that many
179 file name elements. 180 file name elements.
180 Arguments NEWBUFFILE and NEWBUF cause only a subset of buffers to be renamed." 181 Arguments 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)))
192 ;; Only try to rename buffers we actually manage. 192 ;; Only try to rename buffers we actually manage.
193 (or (buffer-local-value 'uniquify-managed buffer) 193 (or (buffer-local-value 'uniquify-managed buffer)
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)
214 "Return name of file BUFFER is visiting, or nil if none. 209 "Return name of file BUFFER is visiting, or nil if none.
215 Works on ordinary file-visiting buffers and buffers whose mode is mentioned 210 Works on ordinary file-visiting buffers and buffers whose mode is mentioned
226 (expand-file-name 221 (expand-file-name
227 (directory-file-name 222 (directory-file-name
228 (if (consp dired-directory) 223 (if (consp dired-directory)
229 (car dired-directory) 224 (car dired-directory)
230 dired-directory))))))))) 225 dired-directory)))))))))
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))
231 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)))
235 254
354 ;; rename-buffer and create-file-buffer. (Setting find-file-hook isn't 373 ;; rename-buffer and create-file-buffer. (Setting find-file-hook isn't
355 ;; sufficient.) 374 ;; sufficient.)
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)
373 "Uniquify buffer names with parts of directory name." 389 "Uniquify buffer names with parts of directory name."
374 (if uniquify-buffer-name-style 390 (if uniquify-buffer-name-style
383 ;; function runs the rationalization and then removes itself from the hook. 399 ;; function runs the rationalization and then removes itself from the hook.
384 ;; Is there a better way to accomplish this? 400 ;; Is there a better way to accomplish this?
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.
390 For use on, eg, `kill-buffer-hook', to rationalize *after* buffer deletion." 406 For 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'.
401 See 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