Mercurial > emacs
annotate lisp/uniquify.el @ 13851:922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 25 Dec 1995 18:45:37 +0000 |
parents | f683f93b94e9 |
children | 41802d268ffb |
rev | line source |
---|---|
13814 | 1 ;;; uniquify.el --- unique buffer names dependent on pathname |
2 | |
3 ;; Copyright (c) 1989, 1995 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Dick King <king@reasoning.com> | |
6 ;; Maintainer: Michael Ernst <mernst@theory.lcs.mit.edu> | |
7 ;; Created: 15 May 86 | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; Emacs's standard method for making buffer names unique adds <2>, <3>, | |
28 ;; etc. to the end of (all but one of) the buffers. This file replaces | |
29 ;; that behavior, for buffers visiting files and dired buffers, with a | |
30 ;; uniquification that adds parts of the pathname until the buffer names | |
31 ;; are unique. For instance, buffers visiting /u/mernst/tmp/Makefile and | |
32 ;; /usr/projects/zaphod/Makefile would be named Makefile|tmp and | |
33 ;; Makefile|zaphod, respectively (instead of Makefile and Makefile<2>). | |
34 ;; Other buffer name styles are also available. | |
35 | |
36 ;; To use this file, just load it. | |
37 ;; To disable it after loading, set variable uniquify-buffer-name-style to nil. | |
38 ;; For other options, see "User-visible variables", below. | |
39 | |
13851
922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
Richard M. Stallman <rms@gnu.org>
parents:
13814
diff
changeset
|
40 ;; A version of uniquify.el that works under Emacs 18, Emacs 19, XEmacs, |
922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
Richard M. Stallman <rms@gnu.org>
parents:
13814
diff
changeset
|
41 ;; and InfoDock is available from the maintainer. |
13814 | 42 |
43 ;;; Change Log: | |
44 | |
45 ;; Originally by Dick King <king@reasoning.com> 15 May 86 | |
46 ;; Converted for Emacs 18 by Stephen Gildea <gildea@lcs.mit.edu> | |
47 ;; Make uniquify-min-dir-content 0 truly non-invasive. gildea 23 May 89 | |
48 ;; Some cleanup. uniquify-min-dir-content default 0. gildea 01 Jun 89 | |
49 ;; Don't rename to "". Michael Ernst <mernst@theory.lcs.mit.edu> 15 Jun 94 | |
50 ;; Add kill-buffer-hook. Kenneth Manheimer <ken.manheimer@nist.gov> 09 May 95 | |
51 ;; Add advice for rename-buffer and create-file-buffer, handle dired buffers, | |
52 ;; kill-buffer-rationalize-buffer-names-p, documentation. mernst 24 May 95 | |
53 ;; Remove free variables, fix typos. mernst 5 Jun 95 | |
54 ;; Efficiently support Emacs 19.27 & earlier. ken.manheimer, mernst 10 Jun 95 | |
55 ;; Rename user options to "uniquify-...", add uniquify-reverse-dir-content-p, | |
56 ;; add uniquify-ask-about-buffer-names-p. king, mernst 13 Jun 95 | |
57 ;; Prefix functions by "uniquify-..."; rename mnemonic-buffer-names to | |
58 ;; uniquify-buffer-name-style; add 'forward and 'post-forward-angle-brackets | |
59 ;; styles; remove uniquify-reverse-dir-content-p; add | |
60 ;; uniquify-trailing-separator-p. mernst 4 Aug 95 | |
61 | |
62 ;; Valuable feedback was provided by | |
63 ;; Paul Smith <psmith@baynetworks.com>, | |
64 ;; Alastair Burt <burt@dfki.uni-kl.de>, | |
65 ;; Bob Weiner <weiner@footloose.sps.mot.com>, | |
66 ;; Albert L. Ting <alt@vlibs.com>, | |
67 ;; gyro@reasoning.com. | |
68 | |
69 | |
70 ;;; Code: | |
71 | |
72 (provide 'uniquify) | |
73 | |
74 ;;; User-visible variables | |
75 | |
76 (defvar uniquify-buffer-name-style 'post-forward | |
77 "*If non-nil, buffer names are uniquified with parts of directory name. | |
78 The value determines the buffer name style and is one of 'forward, | |
79 'reverse, 'post-forward (the default), or 'post-forward-angle-brackets. | |
80 For example, files /foo/bar/mumble/name and /baz/quux/mumble/name | |
81 would be in the following buffers: | |
82 forward bar/mumble/name quux/mumble/name | |
83 reverse name\\mumble\\bar name\\mumble\\quux | |
84 post-forward name|bar/mumble name|quux/mumble | |
85 post-forward-angle-brackets name<bar/mumble> name<quux/mumble> | |
86 nil name name<2>") | |
87 | |
88 (defvar uniquify-after-kill-buffer-p nil | |
89 "*If non-nil, rerationalize buffer names after a buffer has been killed. | |
90 This can be dangerous if Emacs Lisp code is keeping track of buffers by their | |
91 names (rather than keeping pointers to the buffers themselves).") | |
92 | |
93 (defconst uniquify-ask-about-buffer-names-p nil | |
94 "*If non-nil, permit user to choose names for buffers with same base file. | |
95 If the user chooses to name a buffer, uniquification is preempted and no | |
96 other buffer names are changed.") | |
97 | |
98 (defvar uniquify-min-dir-content 0 | |
99 "*Minimum parts of directory pathname included in buffer name.") | |
100 | |
101 (defvar uniquify-separator nil | |
102 "*String separator for buffer name components. | |
103 When `uniquify-buffer-name-style' is 'post-forward, separates | |
104 base file name from directory part in buffer names (default \"|\"). | |
105 When `uniquify-buffer-name-style' is 'reverse, separates all | |
106 pathname components (default \"\\\").") | |
107 | |
108 (defvar uniquify-trailing-separator-p nil | |
109 "*If non-nil, add a pathname separator to dired buffer names. | |
110 If `uniquify-buffer-name-style' is 'forward, add the separator at the end; | |
111 if it's is 'reverse, add the separator at the beginning; otherwise, this | |
112 variable is ignored.") | |
113 | |
114 | |
115 ;;; Utilities | |
116 | |
117 (defmacro uniquify-push (item list) | |
118 (` (setq (, list) (cons (, item) (, list))))) | |
119 | |
120 (defmacro uniquify-fix-list-base (a) | |
121 (` (car (, a)))) | |
122 | |
123 (defmacro uniquify-fix-list-filename (a) | |
124 (` (car (cdr (, a))))) | |
125 | |
126 (defmacro uniquify-fix-list-buffer (a) | |
127 (` (car (cdr (cdr (, a)))))) | |
128 | |
129 (defmacro uniquify-cadddr (a) | |
130 (` (car (cdr (cdr (cdr (, a))))))) | |
131 | |
132 ;; Internal variables used free | |
133 (defvar uniquify-non-file-buffer-names nil) | |
134 (defvar uniquify-possibly-resolvable nil) | |
135 | |
136 ;;; Main entry point. | |
137 | |
138 (defun uniquify-rationalize-file-buffer-names (&optional newbuffile newbuf) | |
139 "Makes file buffer names unique by adding segments from pathname. | |
140 If `uniquify-min-dir-content' > 0, always pulls that many | |
141 pathname elements. Arguments cause only a subset of buffers to be renamed." | |
142 (interactive) | |
143 (let (fix-list | |
144 uniquify-non-file-buffer-names | |
145 (depth uniquify-min-dir-content)) | |
146 (let ((buffers (buffer-list))) | |
147 (while buffers | |
148 (let* ((buffer (car buffers)) | |
149 (bfn (if (eq buffer newbuf) | |
150 (expand-file-name newbuffile) | |
151 (uniquify-buffer-file-name buffer))) | |
152 (rawname (and bfn (file-name-nondirectory bfn))) | |
153 (deserving (and rawname | |
154 (or (not newbuffile) | |
155 (equal rawname | |
156 (file-name-nondirectory newbuffile)))))) | |
157 (if deserving | |
158 (uniquify-push (list rawname bfn buffer nil) fix-list) | |
159 (uniquify-push (list (buffer-name buffer)) | |
160 uniquify-non-file-buffer-names))) | |
161 (setq buffers (cdr buffers)))) | |
162 ;; selects buffers whose names may need changing, and others that | |
163 ;; may conflict. | |
164 (setq fix-list | |
13851
922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
Richard M. Stallman <rms@gnu.org>
parents:
13814
diff
changeset
|
165 (sort fix-list 'uniquify-fix-list-filename-lessp)) |
13814 | 166 ;; bringing conflicting names together |
167 (uniquify-rationalize-a-list fix-list depth) | |
168 (mapcar 'uniquify-unrationalized-buffer fix-list))) | |
169 | |
13851
922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
Richard M. Stallman <rms@gnu.org>
parents:
13814
diff
changeset
|
170 ;; uniquify's version of buffer-file-name |
13814 | 171 (defun uniquify-buffer-file-name (buffer) |
172 "Return name of file BUFFER is visiting, or nil if none. | |
173 Works on dired buffers as well as ordinary file-visiting buffers." | |
174 (or (buffer-file-name buffer) | |
175 (save-excursion | |
176 (set-buffer buffer) | |
177 list-buffers-directory))) | |
178 | |
13851
922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
Richard M. Stallman <rms@gnu.org>
parents:
13814
diff
changeset
|
179 (defun uniquify-fix-list-filename-lessp (fixlist1 fixlist2) |
13814 | 180 (uniquify-filename-lessp |
13851
922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
Richard M. Stallman <rms@gnu.org>
parents:
13814
diff
changeset
|
181 (uniquify-fix-list-filename fixlist1) (uniquify-fix-list-filename fixlist2))) |
13814 | 182 |
13851
922f62608a55
(uniquify-fix-list-filename-lessp): Renamed from uniquify-filename-sort.
Richard M. Stallman <rms@gnu.org>
parents:
13814
diff
changeset
|
183 ;; This examines the filename components in reverse order. |
13814 | 184 (defun uniquify-filename-lessp (s1 s2) |
185 (let ((s1f (file-name-nondirectory s1)) | |
186 (s2f (file-name-nondirectory s2))) | |
187 (and (not (equal s2f "")) | |
188 (or (string-lessp s1f s2f) | |
189 (and (equal s1f s2f) | |
190 (let ((s1d (file-name-directory s1)) | |
191 (s2d (file-name-directory s2))) | |
192 (and (not (<= (length s2d) 1)) | |
193 (or (<= (length s1d) 1) | |
194 (uniquify-filename-lessp | |
195 (substring s1d 0 -1) | |
196 (substring s2d 0 -1)))))))))) | |
197 | |
198 ;; Was named do-the-buffers-you-couldnt-rationalize | |
199 (defun uniquify-unrationalized-buffer (item) | |
200 (or (uniquify-cadddr item) nil)) ;maybe better in the future | |
201 | |
202 (defun uniquify-rationalize-a-list (fix-list depth) | |
203 (let (conflicting-sublist | |
204 (old-name "") | |
205 proposed-name uniquify-possibly-resolvable) | |
206 (while fix-list | |
207 (let ((item (car fix-list))) | |
208 (setq proposed-name (uniquify-get-proposed-name item depth)) | |
209 (if (not (equal proposed-name old-name)) | |
210 (progn | |
211 (uniquify-rationalize-conflicting-sublist | |
212 conflicting-sublist old-name depth) | |
213 (setq conflicting-sublist nil))) | |
214 (uniquify-push item conflicting-sublist) | |
215 (setq old-name proposed-name)) | |
216 (setq fix-list (cdr fix-list))) | |
217 (uniquify-rationalize-conflicting-sublist | |
218 conflicting-sublist old-name depth))) | |
219 | |
220 (defun uniquify-get-proposed-name (item depth) | |
221 (let (index | |
222 (extra-string "") | |
223 (n depth) | |
224 (base (uniquify-fix-list-base item)) | |
225 (fn (uniquify-fix-list-filename item))) | |
226 (while (and (> n 0) | |
227 (setq index (string-match | |
228 (concat "\\(^\\|/[^/]*\\)/" | |
229 (regexp-quote extra-string) | |
230 (regexp-quote base) | |
231 "\\'") | |
232 fn))) | |
233 (setq extra-string (substring fn | |
234 (if (zerop index) 0 (1+ index)) | |
235 ;; (- (length base)) fails for base = "". | |
236 ;; Equivalently, we could have used | |
237 ;; (apply 'substring ... | |
238 ;; (and (not (string= "" base)) | |
239 ;; (list (- (length base))))) | |
240 (- (length fn) (length base))) | |
241 n (1- n))) | |
242 (if (zerop n) (setq uniquify-possibly-resolvable t)) | |
243 | |
244 | |
245 ;; Distinguish directories by adding extra separator. | |
246 (if (and uniquify-trailing-separator-p | |
247 (file-directory-p fn) | |
248 (not (string-equal base ""))) | |
249 (cond ((eq uniquify-buffer-name-style 'forward) | |
250 (setq base (concat base "/"))) | |
251 ((eq uniquify-buffer-name-style 'reverse) | |
252 (setq base (concat (or uniquify-separator "\\") base))))) | |
253 | |
254 ;; Trim trailing separator on directory part | |
255 (if (and (not (string-equal extra-string "")) | |
256 (or (eq uniquify-buffer-name-style 'post-forward) | |
257 (eq uniquify-buffer-name-style 'post-forward-angle-brackets))) | |
258 (setq extra-string (substring extra-string 0 | |
259 (- (length extra-string) 1)))) | |
260 | |
261 (cond ((string-equal extra-string "") | |
262 base) | |
263 ((string-equal base "") | |
264 extra-string) | |
265 ((eq uniquify-buffer-name-style 'forward) | |
266 (concat extra-string base)) | |
267 ((eq uniquify-buffer-name-style 'reverse) | |
268 (concat base (uniquify-reverse-components extra-string))) | |
269 ((eq uniquify-buffer-name-style 'post-forward) | |
270 (concat base (or uniquify-separator "|") extra-string)) | |
271 ((eq uniquify-buffer-name-style 'post-forward-angle-brackets) | |
272 (concat base "<" extra-string ">")) | |
273 (t (error "Bad value for uniquify-buffer-name-style: %s" | |
274 uniquify-buffer-name-style))))) | |
275 | |
276 | |
277 ;; Deal with conflicting-sublist, which is set by uniquify-rationalize-a-list. | |
278 ;; This is only called by uniquify-rationalize-a-list. | |
279 (defun uniquify-rationalize-conflicting-sublist (conflicting-sublist old-name depth) | |
280 (or (null conflicting-sublist) | |
281 (and (null (cdr conflicting-sublist)) | |
282 (not (assoc old-name uniquify-non-file-buffer-names)) | |
283 (or (and (not (string= old-name "")) | |
284 (uniquify-rename-buffer (car conflicting-sublist) old-name)) | |
285 t)) | |
286 (if uniquify-possibly-resolvable | |
287 (uniquify-rationalize-a-list conflicting-sublist (1+ depth))))) | |
288 | |
289 (defun uniquify-rename-buffer (item newname) | |
290 (let ((buffer (uniquify-fix-list-buffer item))) | |
291 (if (not (equal newname (buffer-name buffer))) | |
292 (let ((unset (current-buffer)) | |
293 ;; avoid hooks on rename-buffer | |
294 (uniquify-buffer-name-style nil)) | |
295 (set-buffer buffer) | |
296 (rename-buffer newname) | |
297 (set-buffer unset)))) | |
298 (rplaca (nthcdr 3 item) t)) | |
299 | |
300 (defun uniquify-reverse-components (instring) | |
301 (let ((sofar '()) | |
302 (cursor 0) | |
303 (len (length instring)) | |
304 (sep (or uniquify-separator "\\"))) | |
305 (while (< cursor len) | |
306 (if (= (aref instring cursor) ?/) | |
307 (setq sofar (cons sep sofar) | |
308 cursor (1+ cursor)) | |
309 (let ((first-slash (or (string-match "/" instring cursor) len))) | |
310 (setq sofar (cons (substring instring cursor first-slash) sofar) | |
311 cursor first-slash)))) | |
312 (apply (function concat) sofar))) | |
313 | |
314 | |
315 ;;; Hooks from the rest of Emacs | |
316 | |
317 ;; Emacs 19 (GNU Emacs or XEmacs) | |
318 | |
319 ;; The logical place to put all this code is in generate-new-buffer-name. | |
320 ;; It's written in C, so we would add a generate-new-buffer-name-function | |
321 ;; which, if non-nil, would be called instead of the C. One problem with | |
322 ;; that is that generate-new-buffer-name takes a potential buffer name as | |
323 ;; its argument -- not other information, such as what file the buffer will | |
324 ;; visit. | |
325 | |
326 ;; The below solution works because generate-new-buffer-name is called | |
327 ;; only by rename-buffer (which, as of 19.29, is never called from C) and | |
328 ;; generate-new-buffer, which is called only by Lisp functions | |
329 ;; create-file-buffer and rename-uniquely. Rename-uniquely generally | |
330 ;; isn't used for buffers visiting files, so it's sufficient to hook | |
331 ;; rename-buffer and create-file-buffer. (Setting find-file-hooks isn't | |
332 ;; sufficient.) | |
333 | |
334 (defadvice rename-buffer (after rename-buffer-uniquify activate) | |
335 "Uniquify buffer names with parts of directory name." | |
336 (if (and uniquify-buffer-name-style | |
337 ;; UNIQUE argument | |
338 (ad-get-arg 1)) | |
339 (progn | |
340 (if uniquify-after-kill-buffer-p | |
341 ;; call with no argument; rationalize vs. old name as well as new | |
342 (uniquify-rationalize-file-buffer-names) | |
343 ;; call with argument: rationalize vs. new name only | |
344 (uniquify-rationalize-file-buffer-names | |
345 (uniquify-buffer-file-name (current-buffer)) (current-buffer))) | |
346 (setq ad-return-value (buffer-name (current-buffer)))))) | |
347 | |
348 (defadvice create-file-buffer (after create-file-buffer-uniquify activate) | |
349 "Uniquify buffer names with parts of directory name." | |
350 (if uniquify-buffer-name-style | |
351 (uniquify-rationalize-file-buffer-names (ad-get-arg 0) ad-return-value))) | |
352 | |
353 ;; Buffer deletion | |
354 ;; Rerationalize after a buffer is killed, to reduce coinciding buffer names. | |
355 ;; This mechanism uses `kill-buffer-hook', which runs *before* deletion. | |
356 ;; That means that the kill-buffer-hook function cannot just delete the | |
357 ;; buffer -- it has to set something to do the rationalization *later*. | |
358 ;; It actually puts another function on `post-command-hook'. This other | |
359 ;; function runs the rationalization and then removes itself from the hook. | |
360 ;; Is there a better way to accomplish this? | |
361 ;; (This ought to set some global variables so the work is done only for | |
362 ;; buffers with names similar to the deleted buffer. -MDE) | |
363 | |
364 (defun delay-uniquify-rationalize-file-buffer-names () | |
365 "Add `delayed-uniquify-rationalize-file-buffer-names' to `post-command-hook'. | |
366 For use on, eg, `kill-buffer-hook', to rationalize *after* buffer deletion." | |
367 (if (and uniquify-buffer-name-style | |
368 uniquify-after-kill-buffer-p) | |
369 (add-hook 'post-command-hook | |
370 'delayed-uniquify-rationalize-file-buffer-names))) | |
371 | |
372 (defun delayed-uniquify-rationalize-file-buffer-names () | |
373 "Rerationalize buffer names and remove self from `post-command-hook'. | |
374 See also `delay-rationalize-file-buffer-names' for hook setter." | |
375 (uniquify-rationalize-file-buffer-names) | |
376 (remove-hook 'post-command-hook | |
377 'delayed-uniquify-rationalize-file-buffer-names)) | |
378 | |
379 (add-hook 'kill-buffer-hook 'delay-uniquify-rationalize-file-buffer-names) | |
380 | |
381 ;;; uniquify.el ends here | |
382 |