Mercurial > hg > xemacs-beta
annotate lisp/files.el @ 4640:8cef85a39d2c
Make CDPATH handling portable, accept entries not matching "/$".
2009-06-06 Aidan Kehoe <kehoea@parhasard.net>
* files.el (cd):
Make CDPATH handling portable, accept entries without trailing
slashes within it. Some style corrections from Stephen Turnbull,
thank you Stephen.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 06 Jun 2009 17:20:21 +0100 |
parents | 555e21a66d51 |
children | f2a991ff6db0 |
rev | line source |
---|---|
428 | 1 ;;; files.el --- file input and output commands for XEmacs. |
2 | |
3 ;; Copyright (C) 1985-1987, 1992-1995, 1997 Free Software Foundation, Inc. | |
4 ;; Copyright (C) 1995 Sun Microsystems. | |
1333 | 5 ;; Copyright (C) 2001, 2002, 2003 Ben Wing. |
428 | 6 |
7 ;; Maintainer: XEmacs Development Team | |
8 ;; Keywords: extensions, dumped | |
9 | |
10 ;; This file is part of XEmacs. | |
11 | |
12 ;; XEmacs is free software; you can redistribute it and/or modify it | |
13 ;; under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; XEmacs is distributed in the hope that it will be useful, but | |
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 ;; General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
24 ;; Software Foundation, Inc. 59 Temple Place - Suite 330, Boston, MA | |
25 ;; 02111-1307, USA. | |
26 | |
1333 | 27 ;;; [[ Synched up with: FSF 20.3 (but diverging) |
28 ;;; Warning: Merging this file is tough. Beware.]] | |
29 | |
30 ;;; Beware of sync messages with 20.x or 21.x! (Unless I did them, of | |
31 ;;; course ... :-) Those who did these synchronizations did not do proper | |
32 ;;; jobs and often left out lots of changes. In practice you need to do a | |
33 ;;; line-by-line comparison, and whenever encountering differences, see | |
34 ;;; what FSF 19.34 looks like to see if the changes are intentional or just | |
35 ;;; regressions. In at least one case below, our code was unchanged from | |
36 ;;; FSF 19.30! --ben | |
37 | |
38 ;;; Mostly synched to FSF 21.2 by Ben Wing using a line-by-line comparison, | |
39 ;;; except some really hard parts that have changed almost completely. | |
428 | 40 |
41 ;;; Commentary: | |
42 | |
43 ;; This file is dumped with XEmacs. | |
44 | |
1333 | 45 ;; BEGIN SYNC WITH FSF 21.2. |
46 | |
428 | 47 ;; Defines most of XEmacs's file- and directory-handling functions, |
48 ;; including basic file visiting, backup generation, link handling, | |
49 ;; ITS-id version control, load- and write-hook handling, and the like. | |
50 | |
51 ;;; Code: | |
52 | |
53 ;; XEmacs: Avoid compilation warnings. | |
54 (defvar coding-system-for-read) | |
55 (defvar buffer-file-coding-system) | |
56 | |
57 (defgroup files nil | |
58 "Support editing files." | |
59 :group 'emacs) | |
60 | |
61 (defgroup backup nil | |
62 "Backups of edited data files." | |
63 :group 'files) | |
64 | |
65 (defgroup find-file nil | |
66 "Finding and editing files." | |
67 :group 'files) | |
68 | |
1333 | 69 ;; XEmacs: In buffer.c (also) |
70 (defcustom delete-auto-save-files t | |
71 "*Non-nil means delete auto-save file when a buffer is saved or killed. | |
72 | |
73 Note that auto-save file will not be deleted if the buffer is killed | |
74 when it has unsaved changes." | |
75 :type 'boolean | |
76 :group 'auto-save) | |
428 | 77 |
78 ;; FSF has automount-dir-prefix. Our directory-abbrev-alist is more general. | |
79 ;; note: tmp_mnt bogosity conversion is established in paths.el. | |
80 (defcustom directory-abbrev-alist nil | |
81 "*Alist of abbreviations for file directories. | |
82 A list of elements of the form (FROM . TO), each meaning to replace | |
83 FROM with TO when it appears in a directory name. | |
84 This replacement is done when setting up the default directory of a | |
85 newly visited file. *Every* FROM string should start with \\\\` or ^. | |
86 | |
1333 | 87 Do not use `~' in the TO strings. |
88 They should be ordinary absolute directory names. | |
89 | |
428 | 90 Use this feature when you have directories which you normally refer to |
91 via absolute symbolic links or to eliminate automounter mount points | |
92 from the beginning of your filenames. Make TO the name of the link, | |
93 and FROM the name it is linked to." | |
94 :type '(repeat (cons :format "%v" | |
95 :value ("\\`" . "") | |
96 (regexp :tag "From") | |
97 (regexp :tag "To"))) | |
98 :group 'find-file) | |
99 | |
100 (defcustom make-backup-files t | |
101 "*Non-nil means make a backup of a file the first time it is saved. | |
102 This can be done by renaming the file or by copying. | |
103 | |
104 Renaming means that XEmacs renames the existing file so that it is a | |
105 backup file, then writes the buffer into a new file. Any other names | |
106 that the old file had will now refer to the backup file. The new file | |
107 is owned by you and its group is defaulted. | |
108 | |
109 Copying means that XEmacs copies the existing file into the backup | |
110 file, then writes the buffer on top of the existing file. Any other | |
111 names that the old file had will now refer to the new (edited) file. | |
112 The file's owner and group are unchanged. | |
113 | |
114 The choice of renaming or copying is controlled by the variables | |
115 `backup-by-copying', `backup-by-copying-when-linked' and | |
1333 | 116 `backup-by-copying-when-mismatch' and |
117 `backup-by-copying-when-privileged-mismatch'. See also `backup-inhibited'." | |
428 | 118 :type 'boolean |
119 :group 'backup) | |
120 | |
121 ;; Do this so that local variables based on the file name | |
122 ;; are not overridden by the major mode. | |
123 (defvar backup-inhibited nil | |
124 "Non-nil means don't make a backup, regardless of the other parameters. | |
125 This variable is intended for use by making it local to a buffer. | |
126 But it is local only if you make it local.") | |
127 (put 'backup-inhibited 'permanent-local t) | |
128 | |
129 (defcustom backup-by-copying nil | |
130 "*Non-nil means always use copying to create backup files. | |
131 See documentation of variable `make-backup-files'." | |
132 :type 'boolean | |
133 :group 'backup) | |
134 | |
135 (defcustom backup-by-copying-when-linked nil | |
136 "*Non-nil means use copying to create backups for files with multiple names. | |
137 This causes the alternate names to refer to the latest version as edited. | |
138 This variable is relevant only if `backup-by-copying' is nil." | |
139 :type 'boolean | |
140 :group 'backup) | |
141 | |
142 (defcustom backup-by-copying-when-mismatch nil | |
143 "*Non-nil means create backups by copying if this preserves owner or group. | |
144 Renaming may still be used (subject to control of other variables) | |
145 when it would not result in changing the owner or group of the file; | |
146 that is, for files which are owned by you and whose group matches | |
147 the default for a new file created there by you. | |
148 This variable is relevant only if `backup-by-copying' is nil." | |
149 :type 'boolean | |
150 :group 'backup) | |
151 | |
1333 | 152 (defcustom backup-by-copying-when-privileged-mismatch 200 |
153 "*Non-nil means create backups by copying to preserve a privileged owner. | |
154 Renaming may still be used (subject to control of other variables) | |
155 when it would not result in changing the owner of the file or if the owner | |
156 has a user id greater than the value of this variable. This is useful | |
157 when low-numbered uid's are used for special system users (such as root) | |
158 that must maintain ownership of certain files. | |
159 This variable is relevant only if `backup-by-copying' and | |
160 `backup-by-copying-when-mismatch' are nil." | |
161 :type '(choice (const nil) integer) | |
162 :group 'backup) | |
163 | |
164 (defun normal-backup-enable-predicate (name) | |
165 "Default `backup-enable-predicate' function. | |
4266 | 166 Checks for files in the directory returned by `temp-directory' or specified |
167 by `small-temporary-file-directory'." | |
1333 | 168 (let ((temporary-file-directory (temp-directory))) |
169 (not (or (let ((comp (compare-strings temporary-file-directory 0 nil | |
170 name 0 nil))) | |
171 ;; Directory is under temporary-file-directory. | |
172 (and (not (eq comp t)) | |
173 (< comp (- (length temporary-file-directory))))) | |
174 (if small-temporary-file-directory | |
175 (let ((comp (compare-strings small-temporary-file-directory | |
176 0 nil | |
177 name 0 nil))) | |
178 ;; Directory is under small-temporary-file-directory. | |
179 (and (not (eq comp t)) | |
180 (< comp (- (length small-temporary-file-directory)))))))))) | |
181 | |
182 (defvar backup-enable-predicate 'normal-backup-enable-predicate | |
428 | 183 "Predicate that looks at a file name and decides whether to make backups. |
184 Called with an absolute file name as argument, it returns t to enable backup.") | |
185 | |
186 (defcustom buffer-offer-save nil | |
1333 | 187 "*Non-nil in a buffer means always offer to save buffer on exit. |
188 Do so even if the buffer is not visiting a file. | |
428 | 189 Automatically local in all buffers." |
190 :type 'boolean | |
191 :group 'find-file) | |
192 (make-variable-buffer-local 'buffer-offer-save) | |
193 | |
194 ;; FSF uses normal defconst | |
195 (defvaralias 'find-file-visit-truename 'find-file-use-truenames) | |
196 (defvaralias 'find-file-existing-other-name 'find-file-compare-truenames) | |
197 | |
198 (defcustom revert-without-query nil | |
199 "*Specify which files should be reverted without query. | |
200 The value is a list of regular expressions. | |
201 If the file name matches one of these regular expressions, | |
202 then `revert-buffer' reverts the file without querying | |
203 if the file has changed on disk and you have not edited the buffer." | |
204 :type '(repeat (regexp "")) | |
205 :group 'find-file) | |
206 | |
207 (defvar buffer-file-number nil | |
208 "The device number and file number of the file visited in the current buffer. | |
209 The value is a list of the form (FILENUM DEVNUM). | |
210 This pair of numbers uniquely identifies the file. | |
211 If the buffer is visiting a new file, the value is nil.") | |
212 (make-variable-buffer-local 'buffer-file-number) | |
213 (put 'buffer-file-number 'permanent-local t) | |
214 | |
215 (defvar buffer-file-numbers-unique (not (memq system-type '(windows-nt))) | |
216 "Non-nil means that buffer-file-number uniquely identifies files.") | |
217 | |
1333 | 218 ;; FSF 21.2. We use (temp-directory). |
219 ; (defvar temporary-file-directory | |
220 ; (file-name-as-directory | |
221 ; (cond ((memq system-type '(ms-dos windows-nt)) | |
222 ; (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp")) | |
223 ; ((memq system-type '(vax-vms axp-vms)) | |
224 ; (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:")) | |
225 ; (t | |
226 ; (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp")))) | |
227 ; "The directory for writing temporary files.") | |
228 | |
229 (defvar small-temporary-file-directory | |
230 (if (eq system-type 'ms-dos) (getenv "TMPDIR")) | |
231 "The directory for writing small temporary files. | |
232 If non-nil, this directory is used instead of `temporary-file-directory' | |
233 by programs that create small temporary files. This is for systems that | |
234 have fast storage with limited space, such as a RAM disk.") | |
235 | |
236 ; (defvar file-name-invalid-regexp | |
237 ; (cond ((and (eq system-type 'ms-dos) (not (msdos-long-file-names))) | |
238 ; (concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive | |
239 ; "[+, ;=|<>\"?*]\\|\\[\\|\\]\\|" ; invalid characters | |
240 ; "[\000-\031]\\|" ; control characters | |
241 ; "\\(/\\.\\.?[^/]\\)\\|" ; leading dots | |
242 ; "\\(/[^/.]+\\.[^/.]*\\.\\)")) ; more than a single dot | |
243 ; ((memq system-type '(ms-dos windows-nt)) | |
244 ; (concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive | |
245 ; "[|<>\"?*\000-\031]")) ; invalid characters | |
246 ; (t "[\000]")) | |
247 ; "Regexp recognizing file names which aren't allowed by the filesystem.") | |
248 | |
428 | 249 (defcustom file-precious-flag nil |
250 "*Non-nil means protect against I/O errors while saving files. | |
251 Some modes set this non-nil in particular buffers. | |
252 | |
253 This feature works by writing the new contents into a temporary file | |
254 and then renaming the temporary file to replace the original. | |
255 In this way, any I/O error in writing leaves the original untouched, | |
256 and there is never any instant where the file is nonexistent. | |
257 | |
258 Note that this feature forces backups to be made by copying. | |
259 Yet, at the same time, saving a precious file | |
260 breaks any hard links between it and other files." | |
261 :type 'boolean | |
262 :group 'backup) | |
263 | |
264 (defcustom version-control nil | |
265 "*Control use of version numbers for backup files. | |
266 t means make numeric backup versions unconditionally. | |
267 nil means make them for files that have some already. | |
268 `never' means do not make them." | |
1333 | 269 :type '(choice (const :tag "Never" never) |
270 (const :tag "If existing" nil) | |
271 (other :tag "Always" t)) | |
428 | 272 :group 'backup |
273 :group 'vc) | |
274 | |
275 ;; This is now defined in efs. | |
1333 | 276 ; (defcustom dired-kept-versions 2 |
277 ; "*When cleaning directory, number of versions to keep." | |
278 ; :type 'integer | |
279 ; :group 'backup | |
280 ; :group 'dired) | |
428 | 281 |
2103 | 282 (defcustom delete-old-versions (when noninteractive 'leave) |
428 | 283 "*If t, delete excess backup versions silently. |
284 If nil, ask confirmation. Any other value prevents any trimming." | |
285 :type '(choice (const :tag "Delete" t) | |
286 (const :tag "Ask" nil) | |
287 (sexp :tag "Leave" :format "%t\n" other)) | |
288 :group 'backup) | |
289 | |
290 (defcustom kept-old-versions 2 | |
291 "*Number of oldest versions to keep when a new numbered backup is made." | |
292 :type 'integer | |
293 :group 'backup) | |
294 | |
295 (defcustom kept-new-versions 2 | |
296 "*Number of newest versions to keep when a new numbered backup is made. | |
297 Includes the new backup. Must be > 0" | |
298 :type 'integer | |
299 :group 'backup) | |
300 | |
301 (defcustom require-final-newline nil | |
302 "*Value of t says silently ensure a file ends in a newline when it is saved. | |
303 Non-nil but not t says ask user whether to add a newline when there isn't one. | |
304 nil means don't add newlines." | |
305 :type '(choice (const :tag "Off" nil) | |
306 (const :tag "Add" t) | |
307 (sexp :tag "Ask" :format "%t\n" ask)) | |
308 :group 'editing-basics) | |
309 | |
310 (defcustom auto-save-default t | |
311 "*Non-nil says by default do auto-saving of every file-visiting buffer." | |
312 :type 'boolean | |
313 :group 'auto-save) | |
314 | |
315 (defcustom auto-save-visited-file-name nil | |
316 "*Non-nil says auto-save a buffer in the file it is visiting, when practical. | |
317 Normally auto-save files are written under other names." | |
318 :type 'boolean | |
319 :group 'auto-save) | |
320 | |
1333 | 321 (defcustom auto-save-file-name-transforms |
322 `(("\\`/[^/]*:\\(.+/\\)*\\(.*\\)" | |
323 ,(expand-file-name "\\2" (temp-directory)))) | |
324 "*Transforms to apply to buffer file name before making auto-save file name. | |
325 Each transform is a list (REGEXP REPLACEMENT): | |
326 REGEXP is a regular expression to match against the file name. | |
327 If it matches, `replace-match' is used to replace the | |
328 matching part with REPLACEMENT. | |
329 All the transforms in the list are tried, in the order they are listed. | |
330 When one transform applies, its result is final; | |
331 no further transforms are tried. | |
332 | |
4266 | 333 The default value is set up to put the auto-save file into the temporary |
334 directory (see the function `temp-directory') for editing a remote file." | |
1333 | 335 :group 'auto-save |
336 :type '(repeat (list (string :tag "Regexp") (string :tag "Replacement"))) | |
337 ;:version "21.1" | |
338 ) | |
339 | |
428 | 340 (defcustom save-abbrevs nil |
1333 | 341 "*Non-nil means save word abbrevs too when files are saved. |
342 If `silently', don't ask the user before saving. | |
1337 | 343 Loading an abbrev file sets this to t." |
1333 | 344 :type '(choice (const t) (const nil) (const silently)) |
1337 | 345 :group 'abbrev) |
346 | |
428 | 347 (defcustom find-file-run-dired t |
1333 | 348 "*Non-nil means allow `find-file' to visit directories. |
349 To visit the directory, `find-file' runs `find-directory-functions'." | |
350 :type 'boolean | |
351 :group 'find-file) | |
352 | |
353 (defcustom find-directory-functions '(cvs-dired-noselect dired-noselect) | |
354 "*List of functions to try in sequence to visit a directory. | |
355 Each function is called with the directory name as the sole argument | |
356 and should return either a buffer or nil." | |
357 :type '(hook :options (cvs-dired-noselect dired-noselect)) | |
428 | 358 :group 'find-file) |
359 | |
360 ;;;It is not useful to make this a local variable. | |
361 ;;;(put 'find-file-not-found-hooks 'permanent-local t) | |
362 (defvar find-file-not-found-hooks nil | |
363 "List of functions to be called for `find-file' on nonexistent file. | |
364 These functions are called as soon as the error is detected. | |
1333 | 365 Variable `buffer-file-name' is already set up. |
428 | 366 The functions are called in the order given until one of them returns non-nil.") |
367 | |
368 ;;;It is not useful to make this a local variable. | |
369 ;;;(put 'find-file-hooks 'permanent-local t) | |
370 (defvar find-file-hooks nil | |
371 "List of functions to be called after a buffer is loaded from a file. | |
372 The buffer's local variables (if any) will have been processed before the | |
373 functions are called.") | |
374 | |
375 (defvar write-file-hooks nil | |
376 "List of functions to be called before writing out a buffer to a file. | |
377 If one of them returns non-nil, the file is considered already written | |
378 and the rest are not called. | |
379 These hooks are considered to pertain to the visited file. | |
1333 | 380 So any buffer-local binding of `write-file-hooks' is |
381 discarded if you change the visited file name with \\[set-visited-file-name]. | |
382 | |
383 Don't make this variable buffer-local; instead, use `local-write-file-hooks'. | |
428 | 384 See also `write-contents-hooks' and `continue-save-buffer'.") |
385 ;;; However, in case someone does make it local... | |
386 (put 'write-file-hooks 'permanent-local t) | |
387 | |
388 (defvar local-write-file-hooks nil | |
389 "Just like `write-file-hooks', except intended for per-buffer use. | |
390 The functions in this list are called before the ones in | |
391 `write-file-hooks'. | |
392 | |
393 This variable is meant to be used for hooks that have to do with a | |
394 particular visited file. Therefore, it is a permanent local, so that | |
395 changing the major mode does not clear it. However, calling | |
396 `set-visited-file-name' does clear it.") | |
397 (make-variable-buffer-local 'local-write-file-hooks) | |
398 (put 'local-write-file-hooks 'permanent-local t) | |
399 | |
400 | |
401 ;; #### think about this (added by Sun). | |
402 (put 'after-set-visited-file-name-hooks 'permanent-local t) | |
403 (defvar after-set-visited-file-name-hooks nil | |
404 "List of functions to be called after \\[set-visited-file-name] | |
405 or during \\[write-file]. | |
444 | 406 You can use this hook to restore local values of `write-file-hooks', |
407 `after-save-hook', and `revert-buffer-function', which pertain | |
428 | 408 to a specific file and therefore are normally killed by a rename. |
444 | 409 Put hooks pertaining to the buffer contents on `write-contents-hooks' |
410 and `revert-buffer-insert-file-contents-function'.") | |
428 | 411 |
412 (defvar write-contents-hooks nil | |
413 "List of functions to be called before writing out a buffer to a file. | |
414 If one of them returns non-nil, the file is considered already written | |
415 and the rest are not called. | |
1333 | 416 |
417 This variable is meant to be used for hooks that pertain to the | |
418 buffer's contents, not to the particular visited file; thus, | |
419 `set-visited-file-name' does not clear this variable; but changing the | |
420 major mode does clear it. | |
421 | |
422 This variable automatically becomes buffer-local whenever it is set. | |
423 If you use `add-hook' to add elements to the list, use nil for the | |
424 LOCAL argument. | |
425 | |
428 | 426 See also `write-file-hooks' and `continue-save-buffer'.") |
1333 | 427 (make-variable-buffer-local 'write-contents-hooks) |
428 | 428 |
429 ;; XEmacs addition | |
430 ;; Energize needed this to hook into save-buffer at a lower level; we need | |
431 ;; to provide a new output method, but don't want to have to duplicate all | |
432 ;; of the backup file and file modes logic.that does not occur if one uses | |
433 ;; a write-file-hook which returns non-nil. | |
434 (put 'write-file-data-hooks 'permanent-local t) | |
435 (defvar write-file-data-hooks nil | |
436 "List of functions to be called to put the bytes on disk. | |
437 These functions receive the name of the file to write to as argument. | |
438 The default behavior is to call | |
439 (write-region (point-min) (point-max) filename nil t) | |
440 If one of them returns non-nil, the file is considered already written | |
441 and the rest are not called. | |
442 These hooks are considered to pertain to the visited file. | |
1333 | 443 So any buffer-local binding of `write-file-data-hooks' is |
444 discarded if you change the visited file name with \\[set-visited-file-name]. | |
428 | 445 See also `write-file-hooks'.") |
446 | |
447 (defcustom enable-local-variables t | |
1333 | 448 "*Control use of local variables in files you visit. |
428 | 449 The value can be t, nil or something else. |
1333 | 450 A value of t means file local variables specifications are obeyed; |
428 | 451 nil means they are ignored; anything else means query. |
1333 | 452 This variable also controls use of major modes specified in |
453 a -*- line. | |
454 | |
455 The command \\[normal-mode], when used interactively, | |
456 always obeys file local variable specifications and the -*- line, | |
428 | 457 and ignores this variable." |
458 :type '(choice (const :tag "Obey" t) | |
459 (const :tag "Ignore" nil) | |
460 (sexp :tag "Query" :format "%t\n" other)) | |
461 :group 'find-file) | |
462 | |
1333 | 463 ; (defvar local-enable-local-variables t |
464 ; "Like `enable-local-variables' but meant for buffer-local bindings. | |
465 ; The meaningful values are nil and non-nil. The default is non-nil. | |
466 ; If a major mode sets this to nil, buffer-locally, then any local | |
467 ; variables list in the file will be ignored. | |
468 | |
469 ; This variable does not affect the use of major modes | |
470 ; specified in a -*- line.") | |
471 | |
428 | 472 (defcustom enable-local-eval 'maybe |
473 "*Control processing of the \"variable\" `eval' in a file's local variables. | |
474 The value can be t, nil or something else. | |
475 A value of t means obey `eval' variables; | |
476 nil means ignore them; anything else means query. | |
477 | |
478 The command \\[normal-mode] always obeys local-variables lists | |
479 and ignores this variable." | |
480 :type '(choice (const :tag "Obey" t) | |
481 (const :tag "Ignore" nil) | |
482 (sexp :tag "Query" :format "%t\n" other)) | |
483 :group 'find-file) | |
484 | |
485 ;; Avoid losing in versions where CLASH_DETECTION is disabled. | |
486 (or (fboundp 'lock-buffer) | |
487 (defalias 'lock-buffer 'ignore)) | |
488 (or (fboundp 'unlock-buffer) | |
489 (defalias 'unlock-buffer 'ignore)) | |
1333 | 490 (or (fboundp 'file-locked-p) |
491 (defalias 'file-locked-p 'ignore)) | |
492 | |
493 (defvar view-read-only nil | |
494 "*Non-nil means buffers visiting files read-only, do it in view mode.") | |
428 | 495 |
496 ;;FSFmacs bastardized ange-ftp cruft | |
497 ;(defun ange-ftp-completion-hook-function (op &rest args) | |
1333 | 498 ; "Provides support for ange-ftp host name completion. |
499 ;Runs the usual ange-ftp hook, but only for completion operations." | |
500 ; ;; Having this here avoids the need to load ange-ftp when it's not | |
501 ; ;; really in use. | |
428 | 502 ; (if (memq op '(file-name-completion file-name-all-completions)) |
503 ; (apply 'ange-ftp-hook-function op args) | |
504 ; (let ((inhibit-file-name-handlers | |
505 ; (cons 'ange-ftp-completion-hook-function | |
506 ; (and (eq inhibit-file-name-operation op) | |
507 ; inhibit-file-name-handlers))) | |
508 ; (inhibit-file-name-operation op)) | |
509 ; (apply op args)) | |
510 | |
1333 | 511 ;; FSF 21.2: |
512 ;This function's standard definition is trivial; it just returns the argument. | |
513 ;However, on some systems, the function is redefined with a definition | |
514 ;that really does change some file names to canonicalize certain | |
515 ;patterns and to guarantee valid names." | |
428 | 516 (defun convert-standard-filename (filename) |
464 | 517 "Convert a standard file's name to something suitable for the current OS." |
518 (if (eq system-type 'windows-nt) | |
519 (let ((name (copy-sequence filename)) | |
520 (start 0)) | |
521 ;; leave ':' if part of drive specifier | |
819 | 522 (if (and (> (length name) 1) |
523 (eq (aref name 1) ?:)) | |
464 | 524 (setq start 2)) |
525 ;; destructively replace invalid filename characters with ! | |
526 (while (string-match "[?*:<>|\"\000-\037]" name start) | |
527 (aset name (match-beginning 0) ?!) | |
528 (setq start (match-end 0))) | |
529 ;; FSF: [convert directory separators to Windows format ...] | |
530 ;; unneeded in XEmacs. | |
531 name) | |
532 filename)) | |
533 | |
428 | 534 |
535 (defun pwd () | |
536 "Show the current default directory." | |
537 (interactive nil) | |
538 (message "Directory %s" default-directory)) | |
539 | |
540 (defvar cd-path nil | |
541 "Value of the CDPATH environment variable, as a list. | |
542 Not actually set up until the first time you use it.") | |
543 | |
544 (defvar cdpath-previous nil | |
545 "Prior value of the CDPATH environment variable.") | |
546 | |
547 (defun parse-colon-path (cd-path) | |
548 "Explode a colon-separated search path into a list of directory names. | |
549 | |
550 If you think you want to use this, you probably don't. This function | |
551 is provided for backward compatibility. A more robust implementation | |
552 of the same functionality is available as `split-path', which see." | |
553 (and cd-path | |
554 (let (cd-list (cd-start 0) cd-colon) | |
555 (setq cd-path (concat cd-path path-separator)) | |
556 (while (setq cd-colon (string-match path-separator cd-path cd-start)) | |
557 (setq cd-list | |
558 (nconc cd-list | |
559 (list (if (= cd-start cd-colon) | |
560 nil | |
561 (substitute-in-file-name | |
562 (file-name-as-directory | |
563 (substring cd-path cd-start cd-colon))))))) | |
564 (setq cd-start (+ cd-colon 1))) | |
565 cd-list))) | |
566 | |
567 (defun cd-absolute (dir) | |
568 "Change current directory to given absolute file name DIR." | |
569 ;; Put the name into directory syntax now, | |
570 ;; because otherwise expand-file-name may give some bad results. | |
571 (setq dir (file-name-as-directory dir)) | |
572 ;; XEmacs change: stig@hackvan.com | |
573 (if find-file-use-truenames | |
574 (setq dir (file-truename dir))) | |
575 (setq dir (abbreviate-file-name (expand-file-name dir))) | |
576 (cond ((not (file-directory-p dir)) | |
1333 | 577 (if (file-exists-p dir) |
578 (error "%s is not a directory" dir) | |
579 (error "%s: no such directory" dir))) | |
428 | 580 ;; this breaks ange-ftp, which doesn't (can't?) overload `file-executable-p'. |
581 ;;((not (file-executable-p dir)) | |
582 ;; (error "Cannot cd to %s: Permission denied" dir)) | |
583 (t | |
584 (setq default-directory dir)))) | |
585 | |
586 (defun cd (dir) | |
587 "Make DIR become the current buffer's default directory. | |
588 If your environment includes a `CDPATH' variable, try each one of that | |
589 colon-separated list of directories when resolving a relative directory name." | |
590 (interactive | |
591 ;; XEmacs change? (read-file-name => read-directory-name) | |
592 (list (read-directory-name "Change default directory: " | |
593 default-directory default-directory | |
594 (and (member cd-path '(nil ("./"))) | |
595 (null (getenv "CDPATH")))))) | |
596 (if (file-name-absolute-p dir) | |
597 (cd-absolute (expand-file-name dir)) | |
4640
8cef85a39d2c
Make CDPATH handling portable, accept entries not matching "/$".
Aidan Kehoe <kehoea@parhasard.net>
parents:
4400
diff
changeset
|
598 ;; XEmacs change. I'm not sure respecting CDPATH is the right thing to |
8cef85a39d2c
Make CDPATH handling portable, accept entries not matching "/$".
Aidan Kehoe <kehoea@parhasard.net>
parents:
4400
diff
changeset
|
599 ;; do under Windows. |
428 | 600 (unless (and cd-path (equal (getenv "CDPATH") cdpath-previous)) |
4640
8cef85a39d2c
Make CDPATH handling portable, accept entries not matching "/$".
Aidan Kehoe <kehoea@parhasard.net>
parents:
4400
diff
changeset
|
601 (let ((trypath (split-path (setq cdpath-previous (getenv "CDPATH"))))) |
8cef85a39d2c
Make CDPATH handling portable, accept entries not matching "/$".
Aidan Kehoe <kehoea@parhasard.net>
parents:
4400
diff
changeset
|
602 (setq cd-path (or (and trypath |
8cef85a39d2c
Make CDPATH handling portable, accept entries not matching "/$".
Aidan Kehoe <kehoea@parhasard.net>
parents:
4400
diff
changeset
|
603 (mapcar #'file-name-as-directory trypath)) |
8cef85a39d2c
Make CDPATH handling portable, accept entries not matching "/$".
Aidan Kehoe <kehoea@parhasard.net>
parents:
4400
diff
changeset
|
604 (file-name-as-directory ""))))) |
428 | 605 (or (catch 'found |
606 (mapcar #'(lambda (x) | |
607 (let ((f (expand-file-name (concat x dir)))) | |
608 (if (file-directory-p f) | |
609 (progn | |
610 (cd-absolute f) | |
611 (throw 'found t))))) | |
612 cd-path) | |
613 nil) | |
614 ;; jwz: give a better error message to those of us with the | |
615 ;; good taste not to use a kludge like $CDPATH. | |
616 (if (equal cd-path '("./")) | |
617 (error "No such directory: %s" (expand-file-name dir)) | |
618 (error "Directory not found in $CDPATH: %s" dir))))) | |
619 | |
620 (defun load-file (file) | |
621 "Load the Lisp file named FILE." | |
1333 | 622 ;; This is a case where .elc makes a lot of sense. |
623 (interactive (list (let ((completion-ignored-extensions | |
624 (remove ".elc" completion-ignored-extensions))) | |
625 (read-file-name "Load file: ")))) | |
428 | 626 (load (expand-file-name file) nil nil t)) |
627 | |
628 ; We now dump utils/lib-complete.el which has improved versions of this. | |
629 ;(defun load-library (library) | |
630 ; "Load the library named LIBRARY. | |
631 ;This is an interface to the function `load'." | |
632 ; (interactive "sLoad library: ") | |
633 ; (load library)) | |
634 ; | |
635 ;(defun find-library (library) | |
636 ; "Find the library of Lisp code named LIBRARY. | |
637 ;This searches `load-path' for a file named either \"LIBRARY\" or \"LIBRARY.el\"." | |
638 ; (interactive "sFind library file: ") | |
639 ; (let ((f (locate-file library load-path ":.el:"))) | |
640 ; (if f | |
641 ; (find-file f) | |
642 ; (error "Couldn't locate library %s" library)))) | |
643 | |
1333 | 644 (defun file-local-copy (file) |
428 | 645 "Copy the file FILE into a temporary file on this machine. |
646 Returns the name of the local copy, or nil, if FILE is directly | |
647 accessible." | |
1333 | 648 ;; This formerly had an optional BUFFER argument that wasn't used by |
649 ;; anything. | |
428 | 650 (let ((handler (find-file-name-handler file 'file-local-copy))) |
651 (if handler | |
652 (funcall handler 'file-local-copy file) | |
653 nil))) | |
654 | |
655 ;; XEmacs change block | |
656 ; We have this in C and use the realpath() system call. | |
657 | |
658 ;(defun file-truename (filename &optional counter prev-dirs) | |
659 ; [... lots of code snipped ...] | |
660 ; filename)) | |
661 | |
662 ;; XEmacs addition. Called from `insert-file-contents-internal' | |
663 ;; at the appropriate time. | |
664 (defun compute-buffer-file-truename (&optional buffer) | |
665 "Recompute BUFFER's value of `buffer-file-truename' | |
666 based on the current value of `buffer-file-name'. | |
667 BUFFER defaults to the current buffer if unspecified." | |
668 (save-excursion | |
669 (set-buffer (or buffer (current-buffer))) | |
670 (cond ((null buffer-file-name) | |
671 (setq buffer-file-truename nil)) | |
672 ((setq buffer-file-truename (file-truename buffer-file-name)) | |
673 ;; it exists, we're done. | |
674 nil) | |
675 (t | |
676 ;; the file doesn't exist, but maybe the directory does. | |
677 (let* ((dir (file-name-directory buffer-file-name)) | |
678 (truedir (file-truename dir))) | |
679 (if truedir (setq dir truedir)) | |
680 (setq buffer-file-truename | |
681 (expand-file-name (file-name-nondirectory buffer-file-name) | |
682 dir))))) | |
683 (if (and find-file-use-truenames buffer-file-truename) | |
684 (setq buffer-file-name (abbreviate-file-name buffer-file-truename) | |
685 default-directory (file-name-directory buffer-file-name))) | |
686 buffer-file-truename)) | |
687 ;; End XEmacs change block | |
688 | |
689 (defun file-chase-links (filename) | |
690 "Chase links in FILENAME until a name that is not a link. | |
691 Does not examine containing directories for links, | |
692 unlike `file-truename'." | |
693 (let (tem (count 100) (newname filename)) | |
694 (while (setq tem (file-symlink-p newname)) | |
695 (save-match-data | |
696 (if (= count 0) | |
697 (error "Apparent cycle of symbolic links for %s" filename)) | |
698 ;; In the context of a link, `//' doesn't mean what XEmacs thinks. | |
699 (while (string-match "//+" tem) | |
1333 | 700 (setq tem (replace-match "/" nil nil tem))) |
428 | 701 ;; Handle `..' by hand, since it needs to work in the |
702 ;; target of any directory symlink. | |
703 ;; This code is not quite complete; it does not handle | |
704 ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose. | |
705 (while (string-match "\\`\\.\\./" tem) ;#### Unix specific | |
706 (setq tem (substring tem 3)) | |
707 (setq newname (file-name-as-directory | |
708 ;; Do the .. by hand. | |
709 (directory-file-name | |
710 (file-name-directory | |
711 ;; Chase links in the default dir of the symlink. | |
712 (file-chase-links | |
713 (directory-file-name | |
714 (file-name-directory newname)))))))) | |
715 (setq newname (expand-file-name tem (file-name-directory newname))) | |
716 (setq count (1- count)))) | |
717 newname)) | |
4266 | 718 |
719 (defun make-temp-file (prefix &optional dir-flag suffix) | |
720 "Create a temporary file. | |
721 The returned file name (created by appending some random characters at the | |
722 end of PREFIX, and expanding against the return value of `temp-directory' if | |
723 necessary), is guaranteed to point to a newly created empty file. You can | |
724 then use `write-region' to write new data into the file. | |
725 | |
726 If DIR-FLAG is non-nil, create a new empty directory instead of a file. | |
727 | |
728 If SUFFIX is non-nil, add that at the end of the file name. | |
729 | |
730 This function is analagous to mkstemp(3) under POSIX, avoiding the race | |
731 condition between testing for the existence of the generated filename (under | |
732 POSIX with mktemp(3), under Emacs Lisp with `make-temp-name') and creating | |
733 it." | |
734 (let ((umask (default-file-modes)) | |
735 (temporary-file-directory (temp-directory)) | |
736 file) | |
737 (unwind-protect | |
738 (progn | |
739 ;; Create temp files with strict access rights. It's easy to | |
740 ;; loosen them later, whereas it's impossible to close the | |
741 ;; time-window of loose permissions otherwise. | |
742 (set-default-file-modes #o700) | |
743 (while (condition-case () | |
744 (progn | |
745 (setq file | |
746 (make-temp-name | |
747 (expand-file-name prefix | |
748 temporary-file-directory))) | |
749 (if suffix | |
750 (setq file (concat file suffix))) | |
751 (if dir-flag | |
752 (make-directory file) | |
753 (write-region "" nil file nil 'silent nil 'excl)) | |
754 nil) | |
755 (file-already-exists t)) | |
756 ;; the file was somehow created by someone else between | |
757 ;; `make-temp-name' and `write-region', let's try again. | |
758 nil) | |
759 file) | |
760 ;; Reset the umask. | |
761 (set-default-file-modes umask)))) | |
762 | |
428 | 763 |
764 (defun switch-to-other-buffer (arg) | |
765 "Switch to the previous buffer. With a numeric arg, n, switch to the nth | |
766 most recent buffer. With an arg of 0, buries the current buffer at the | |
767 bottom of the buffer stack." | |
768 (interactive "p") | |
769 (if (eq arg 0) | |
770 (bury-buffer (current-buffer))) | |
771 (switch-to-buffer | |
772 (if (<= arg 1) (other-buffer (current-buffer)) | |
773 (nth (1+ arg) (buffer-list))))) | |
774 | |
1333 | 775 ;;FSF 21.2 |
776 ;Optional second arg NORECORD non-nil means | |
777 ;do not put this buffer at the front of the list of recently selected ones. | |
778 (defun switch-to-buffer-other-window (buffer) ;;FSF 21.2: &optional norecord | |
779 "Select buffer BUFFER in another window. | |
780 | |
781 This uses the function `display-buffer' as a subroutine; see its | |
782 documentation for additional customization information." | |
783 (interactive "BSwitch to buffer in other window: ") | |
428 | 784 (let ((pop-up-windows t)) |
785 ;; XEmacs: this used to have (selected-frame) as the third argument, | |
786 ;; but this is obnoxious. If the user wants the buffer in a | |
787 ;; different frame, then it should be this way. | |
788 | |
789 ;; Change documented above undone --mrb | |
790 (pop-to-buffer buffer t (selected-frame)))) | |
1333 | 791 ;(pop-to-buffer buffer t norecord))) |
792 | |
793 ;; FSF 21.2: | |
794 ; (defun switch-to-buffer-other-frame (buffer &optional norecord) | |
795 ; "Switch to buffer BUFFER in another frame. | |
796 ; Optional second arg NORECORD non-nil means | |
797 ; do not put this buffer at the front of the list of recently selected ones. | |
798 | |
799 ; This uses the function `display-buffer' as a subroutine; see its | |
800 ; documentation for additional customization information." | |
801 ; (interactive "BSwitch to buffer in other frame: ") | |
802 ; (let ((pop-up-frames t)) | |
803 ; (pop-to-buffer buffer t norecord) | |
804 ; (raise-frame (window-frame (selected-window))))) | |
428 | 805 |
806 (defun switch-to-buffer-other-frame (buffer) | |
1333 | 807 "Switch to buffer BUFFER in a newly-created frame. |
808 | |
809 This uses the function `display-buffer' as a subroutine; see its | |
810 documentation for additional customization information." | |
428 | 811 (interactive "BSwitch to buffer in other frame: ") |
812 (let* ((name (get-frame-name-for-buffer buffer)) | |
813 (frame (make-frame (if name | |
814 (list (cons 'name (symbol-name name))))))) | |
815 (pop-to-buffer buffer t frame) | |
816 (make-frame-visible frame) | |
817 buffer)) | |
818 | |
771 | 819 (defun switch-to-next-buffer (&optional n) |
820 "Switch to the next-most-recent buffer. | |
821 This essentially rotates the buffer list forward. | |
822 N (interactively, the prefix arg) specifies how many times to rotate | |
823 forward, and defaults to 1. Buffers whose name begins with a space | |
824 \(i.e. \"invisible\" buffers) are ignored." | |
825 ;; Here is a different interactive spec. Look up the function | |
826 ;; `interactive' (i.e. `C-h f interactive') to understand how this | |
827 ;; all works. | |
828 (interactive "p") | |
829 (dotimes (n (or n 1)) | |
830 (loop | |
831 do (bury-buffer (car (buffer-list))) | |
832 while (funcall buffers-tab-omit-function (car (buffer-list)))) | |
833 (switch-to-buffer (car (buffer-list))))) | |
834 | |
835 (defun switch-to-previous-buffer (&optional n) | |
836 "Switch to the previously most-recent buffer. | |
837 This essentially rotates the buffer list backward. | |
838 N (interactively, the prefix arg) specifies how many times to rotate | |
839 backward, and defaults to 1. Buffers whose name begins with a space | |
840 \(i.e. \"invisible\" buffers) are ignored." | |
841 (interactive "p") | |
842 (dotimes (n (or n 1)) | |
843 (loop | |
844 do (switch-to-buffer (car (last (buffer-list)))) | |
845 while (funcall buffers-tab-omit-function (car (buffer-list)))))) | |
846 | |
847 (defun switch-to-next-buffer-in-group (&optional n) | |
848 "Switch to the next-most-recent buffer in the current group. | |
849 This essentially rotates the buffer list forward. | |
850 N (interactively, the prefix arg) specifies how many times to rotate | |
851 forward, and defaults to 1. Buffers whose name begins with a space | |
852 \(i.e. \"invisible\" buffers) are ignored." | |
853 (interactive "p") | |
854 (dotimes (n (or n 1)) | |
855 (let ((curbuf (car (buffer-list)))) | |
856 (loop | |
857 do (bury-buffer (car (buffer-list))) | |
858 while (or (funcall buffers-tab-omit-function (car (buffer-list))) | |
859 (not (funcall buffers-tab-selection-function | |
860 curbuf (car (buffer-list))))))) | |
861 (switch-to-buffer (car (buffer-list))))) | |
862 | |
863 (defun switch-to-previous-buffer-in-group (&optional n) | |
864 "Switch to the previously most-recent buffer in the current group. | |
865 This essentially rotates the buffer list backward. | |
866 N (interactively, the prefix arg) specifies how many times to rotate | |
867 backward, and defaults to 1. Buffers whose name begins with a space | |
868 \(i.e. \"invisible\" buffers) are ignored." | |
869 (interactive "p") | |
870 (dotimes (n (or n 1)) | |
871 (let ((curbuf (car (buffer-list)))) | |
872 (loop | |
873 do (switch-to-buffer (car (last (buffer-list)))) | |
874 while (or (funcall buffers-tab-omit-function (car (buffer-list))) | |
875 (not (funcall buffers-tab-selection-function | |
876 curbuf (car (buffer-list))))))))) | |
877 | |
1333 | 878 (defun find-file (filename &optional codesys wildcards) |
428 | 879 "Edit file FILENAME. |
771 | 880 Switch to a buffer visiting file FILENAME, creating one if none already |
881 exists. Optional second argument specifies the coding system to use when | |
882 decoding the file. Interactively, with a prefix argument, you will be | |
883 prompted for the coding system. | |
884 | |
885 If you do not explicitly specify a coding system, the coding system | |
886 is determined as follows: | |
887 | |
888 1. `coding-system-for-read', if non-nil. (This is used by Lisp programs to | |
889 temporarily set an overriding coding system and should almost never | |
890 apply here in `find-file'.) | |
891 2. The result of `insert-file-contents-pre-hook', if non-nil. (This is a | |
892 complex interface for handling special cases.) | |
893 3. The matching value for this filename from `file-coding-system-alist', | |
894 if any. (This lets you specify the coding system to be used for | |
895 files with particular extensions, names, etc.) | |
896 4. `buffer-file-coding-system-for-read', if non-nil. (This is the global | |
897 default -- normally `undecided', so the built-in auto-detection | |
898 mechanism can do its thing.) | |
899 5. The coding system 'raw-text. | |
900 | |
901 See `insert-file-contents' for more details about how the process of | |
1333 | 902 determining the coding system works. |
903 | |
904 Interactively, or if WILDCARDS is non-nil in a call from Lisp, | |
905 expand wildcards (if any) and visit multiple files. Wildcard expansion | |
1745 | 906 can be suppressed by setting `find-file-wildcards' to `nil'." |
1333 | 907 (interactive (list (read-file-name "Find file: ") |
908 (and current-prefix-arg | |
909 (read-coding-system "Coding system: ")) | |
910 t)) | |
428 | 911 (if codesys |
2718 | 912 (let* ((coding-system-for-read (get-coding-system codesys)) |
913 (value (find-file-noselect filename nil nil wildcards)) | |
914 (bufname (if (listp value) (car (nreverse value)) value))) | |
915 ;; If a user explicitly specified the coding system with a prefix | |
916 ;; argument when opening a nonexistent file, insert-file-contents | |
917 ;; hasn't preserved that coding system as the local | |
918 ;; buffer-file-coding-system. Do that ourselves. | |
919 (unless (and bufname | |
920 (file-exists-p (buffer-file-name bufname)) | |
921 (local-variable-p 'buffer-file-coding-system bufname)) | |
922 (save-excursion | |
923 (set-buffer bufname) | |
924 (setq buffer-file-coding-system coding-system-for-read))) | |
925 (switch-to-buffer bufname)) | |
1333 | 926 (let ((value (find-file-noselect filename nil nil wildcards))) |
927 (if (listp value) | |
928 (mapcar 'switch-to-buffer (nreverse value)) | |
929 (switch-to-buffer value))))) | |
930 | |
931 (defun find-file-other-window (filename &optional codesys wildcards) | |
428 | 932 "Edit file FILENAME, in another window. |
771 | 933 May create a new window, or reuse an existing one. See the function |
934 `display-buffer'. Optional second argument specifies the coding system to | |
935 use when decoding the file. Interactively, with a prefix argument, you | |
936 will be prompted for the coding system." | |
1333 | 937 (interactive (list (read-file-name "Find file in other window: ") |
938 (and current-prefix-arg | |
939 (read-coding-system "Coding system: ")) | |
940 t)) | |
428 | 941 (if codesys |
942 (let ((coding-system-for-read | |
943 (get-coding-system codesys))) | |
1333 | 944 (let ((value (find-file-noselect filename nil nil wildcards))) |
945 (if (listp value) | |
946 (progn | |
947 (setq value (nreverse value)) | |
948 (switch-to-buffer-other-window (car value)) | |
949 (mapcar 'switch-to-buffer (cdr value))) | |
950 (switch-to-buffer-other-window value)))) | |
951 (let ((value (find-file-noselect filename nil nil wildcards))) | |
952 (if (listp value) | |
953 (progn | |
954 (setq value (nreverse value)) | |
955 (switch-to-buffer-other-window (car value)) | |
956 (mapcar 'switch-to-buffer (cdr value))) | |
957 (switch-to-buffer-other-window value))))) | |
958 | |
959 (defun find-file-other-frame (filename &optional codesys wildcards) | |
428 | 960 "Edit file FILENAME, in a newly-created frame. |
771 | 961 Optional second argument specifies the coding system to use when decoding |
962 the file. Interactively, with a prefix argument, you will be prompted for | |
963 the coding system." | |
1333 | 964 (interactive (list (read-file-name "Find file in other frame: ") |
965 (and current-prefix-arg | |
966 (read-coding-system "Coding system: ")) | |
967 t)) | |
428 | 968 (if codesys |
969 (let ((coding-system-for-read | |
970 (get-coding-system codesys))) | |
1333 | 971 (let ((value (find-file-noselect filename nil nil wildcards))) |
972 (if (listp value) | |
973 (progn | |
974 (setq value (nreverse value)) | |
975 (switch-to-buffer-other-frame (car value)) | |
976 (mapcar 'switch-to-buffer (cdr value))) | |
977 (switch-to-buffer-other-frame value)))) | |
978 (let ((value (find-file-noselect filename nil nil wildcards))) | |
979 (if (listp value) | |
980 (progn | |
981 (setq value (nreverse value)) | |
982 (switch-to-buffer-other-frame (car value)) | |
983 (mapcar 'switch-to-buffer (cdr value))) | |
984 (switch-to-buffer-other-frame value))))) | |
985 | |
986 (defun find-file-read-only (filename &optional codesys wildcards) | |
428 | 987 "Edit file FILENAME but don't allow changes. |
988 Like \\[find-file] but marks buffer as read-only. | |
989 Use \\[toggle-read-only] to permit editing. | |
771 | 990 Optional second argument specifies the coding system to use when decoding |
991 the file. Interactively, with a prefix argument, you will be prompted for | |
992 the coding system." | |
1333 | 993 (interactive (list (read-file-name "Find file read-only: ") |
994 (and current-prefix-arg | |
995 (read-coding-system "Coding system: ")) | |
996 t)) | |
428 | 997 (if codesys |
998 (let ((coding-system-for-read | |
999 (get-coding-system codesys))) | |
1333 | 1000 (find-file filename nil wildcards)) |
1001 (find-file filename nil wildcards)) | |
428 | 1002 (setq buffer-read-only t) |
1003 (current-buffer)) | |
1004 | |
1333 | 1005 (defun find-file-read-only-other-window (filename &optional codesys wildcards) |
428 | 1006 "Edit file FILENAME in another window but don't allow changes. |
1007 Like \\[find-file-other-window] but marks buffer as read-only. | |
1008 Use \\[toggle-read-only] to permit editing. | |
771 | 1009 Optional second argument specifies the coding system to use when decoding |
1010 the file. Interactively, with a prefix argument, you will be prompted for | |
1011 the coding system." | |
1333 | 1012 (interactive (list (read-file-name "Find file read-only other window: ") |
1013 (and current-prefix-arg | |
1014 (read-coding-system "Coding system: ")) | |
1015 t)) | |
428 | 1016 (if codesys |
1017 (let ((coding-system-for-read | |
1018 (get-coding-system codesys))) | |
1019 (find-file-other-window filename)) | |
1020 (find-file-other-window filename)) | |
1021 (setq buffer-read-only t) | |
1022 (current-buffer)) | |
1023 | |
1333 | 1024 (defun find-file-read-only-other-frame (filename &optional codesys wildcards) |
428 | 1025 "Edit file FILENAME in another frame but don't allow changes. |
1026 Like \\[find-file-other-frame] but marks buffer as read-only. | |
1027 Use \\[toggle-read-only] to permit editing. | |
771 | 1028 Optional second argument specifies the coding system to use when decoding |
1029 the file. Interactively, with a prefix argument, you will be prompted for | |
1030 the coding system." | |
1333 | 1031 (interactive (list (read-file-name "Find file read-only other frame: ") |
1032 (and current-prefix-arg | |
1033 (read-coding-system "Coding system: ")) | |
1034 t)) | |
428 | 1035 (if codesys |
1036 (let ((coding-system-for-read | |
1037 (get-coding-system codesys))) | |
1038 (find-file-other-frame filename)) | |
1039 (find-file-other-frame filename)) | |
1040 (setq buffer-read-only t) | |
1041 (current-buffer)) | |
1042 | |
1043 (defun find-alternate-file-other-window (filename &optional codesys) | |
1044 "Find file FILENAME as a replacement for the file in the next window. | |
771 | 1045 This command does not select that window. Optional second argument |
1046 specifies the coding system to use when decoding the file. Interactively, | |
428 | 1047 with a prefix argument, you will be prompted for the coding system." |
1048 (interactive | |
1049 (save-selected-window | |
1050 (other-window 1) | |
1051 (let ((file buffer-file-name) | |
1052 (file-name nil) | |
1053 (file-dir nil)) | |
1054 (and file | |
1055 (setq file-name (file-name-nondirectory file) | |
1056 file-dir (file-name-directory file))) | |
1057 (list (read-file-name | |
1058 "Find alternate file: " file-dir nil nil file-name) | |
771 | 1059 (if current-prefix-arg (read-coding-system "Coding-system: ")))))) |
428 | 1060 (if (one-window-p) |
1061 (find-file-other-window filename) | |
1062 (save-selected-window | |
1063 (other-window 1) | |
1064 (find-alternate-file filename codesys)))) | |
1065 | |
1066 (defun find-alternate-file (filename &optional codesys) | |
1067 "Find file FILENAME, select its buffer, kill previous buffer. | |
1068 If the current buffer now contains an empty file that you just visited | |
771 | 1069 \(presumably by mistake), use this command to visit the file you really |
1070 want. Optional second argument specifies the coding system to use when | |
1071 decoding the file. Interactively, with a prefix argument, you will be | |
1072 prompted for the coding system." | |
428 | 1073 (interactive |
1074 (let ((file buffer-file-name) | |
1075 (file-name nil) | |
1076 (file-dir nil)) | |
1077 (and file | |
1078 (setq file-name (file-name-nondirectory file) | |
1079 file-dir (file-name-directory file))) | |
1080 (list (read-file-name | |
1081 "Find alternate file: " file-dir nil nil file-name) | |
771 | 1082 (if current-prefix-arg (read-coding-system "Coding-system: "))))) |
428 | 1083 (and (buffer-modified-p) (buffer-file-name) |
1084 ;; (not buffer-read-only) | |
1085 (not (yes-or-no-p (format | |
1086 "Buffer %s is modified; kill anyway? " | |
1087 (buffer-name)))) | |
1088 (error "Aborted")) | |
1089 (let ((obuf (current-buffer)) | |
1090 (ofile buffer-file-name) | |
1091 (onum buffer-file-number) | |
1092 (otrue buffer-file-truename) | |
1093 (oname (buffer-name))) | |
1094 (if (get-buffer " **lose**") | |
1095 (kill-buffer " **lose**")) | |
1096 (rename-buffer " **lose**") | |
1097 (setq buffer-file-name nil) | |
1098 (setq buffer-file-number nil) | |
1099 (setq buffer-file-truename nil) | |
1100 (unwind-protect | |
1101 (progn | |
1102 (unlock-buffer) | |
1103 (if codesys | |
1104 (let ((coding-system-for-read | |
1105 (get-coding-system codesys))) | |
1106 (find-file filename)) | |
1107 (find-file filename))) | |
1108 (cond ((eq obuf (current-buffer)) | |
1109 (setq buffer-file-name ofile) | |
1110 (setq buffer-file-number onum) | |
1111 (setq buffer-file-truename otrue) | |
1112 (lock-buffer) | |
1113 (rename-buffer oname)))) | |
1114 (or (eq (current-buffer) obuf) | |
1115 (kill-buffer obuf)))) | |
1333 | 1116 |
428 | 1117 (defun create-file-buffer (filename) |
1118 "Create a suitably named buffer for visiting FILENAME, and return it. | |
1119 FILENAME (sans directory) is used unchanged if that name is free; | |
1120 otherwise a string <2> or <3> or ... is appended to get an unused name." | |
1121 (let ((handler (find-file-name-handler filename 'create-file-buffer))) | |
1122 (if handler | |
1123 (funcall handler 'create-file-buffer filename) | |
1124 (let ((lastname (file-name-nondirectory filename))) | |
1125 (if (string= lastname "") | |
1126 (setq lastname filename)) | |
1127 (generate-new-buffer lastname))))) | |
1128 | |
1129 (defun generate-new-buffer (name) | |
1130 "Create and return a buffer with a name based on NAME. | |
1131 Choose the buffer's name using `generate-new-buffer-name'." | |
1132 (get-buffer-create (generate-new-buffer-name name))) | |
1133 | |
1134 (defvar abbreviated-home-dir nil | |
1135 "The user's homedir abbreviated according to `directory-abbrev-alist'.") | |
1136 | |
1137 (defun abbreviate-file-name (filename &optional hack-homedir) | |
1138 "Return a version of FILENAME shortened using `directory-abbrev-alist'. | |
1333 | 1139 Type \\[describe-variable] directory-abbrev-alist RET for more information. |
428 | 1140 If optional argument HACK-HOMEDIR is non-nil, then this also substitutes |
1141 \"~\" for the user's home directory." | |
1142 (let ((handler (find-file-name-handler filename 'abbreviate-file-name))) | |
1143 (if handler | |
1144 (funcall handler 'abbreviate-file-name filename hack-homedir) | |
1145 ;; Get rid of the prefixes added by the automounter. | |
1146 ;;(if (and (string-match automount-dir-prefix filename) | |
1147 ;; (file-exists-p (file-name-directory | |
1148 ;; (substring filename (1- (match-end 0)))))) | |
1149 ;; (setq filename (substring filename (1- (match-end 0))))) | |
1150 (let ((tail directory-abbrev-alist)) | |
1151 ;; If any elt of directory-abbrev-alist matches this name, | |
1152 ;; abbreviate accordingly. | |
1153 (while tail | |
1154 (when (string-match (car (car tail)) filename) | |
1155 (setq filename | |
1156 (concat (cdr (car tail)) (substring filename (match-end 0))))) | |
1157 (setq tail (cdr tail)))) | |
1158 (when hack-homedir | |
1159 ;; Compute and save the abbreviated homedir name. | |
440 | 1160 ;; We defer computing this until the first time it's needed, |
1161 ;; to give time for directory-abbrev-alist to be set properly. | |
1162 ;; We include the separator at the end, to avoid spurious | |
1163 ;; matches such as `/usr/foobar' when the home dir is | |
1164 ;; `/usr/foo'. | |
428 | 1165 (or abbreviated-home-dir |
1166 (setq abbreviated-home-dir | |
1167 (let ((abbreviated-home-dir "$foo")) | |
440 | 1168 (concat "\\`" |
1169 (regexp-quote | |
1170 (abbreviate-file-name (expand-file-name "~"))) | |
1171 "\\(" | |
1172 (regexp-quote (string directory-sep-char)) | |
1173 "\\|\\'\\)")))) | |
428 | 1174 ;; If FILENAME starts with the abbreviated homedir, |
1175 ;; make it start with `~' instead. | |
1176 (if (and (string-match abbreviated-home-dir filename) | |
1177 ;; If the home dir is just /, don't change it. | |
440 | 1178 (not (and (= (match-end 0) 1) |
1179 (= (aref filename 0) directory-sep-char))) | |
1180 (not (and (eq system-type 'windows-nt) | |
428 | 1181 (save-match-data |
440 | 1182 (string-match (concat "\\`[a-zA-Z]:" |
1183 (regexp-quote | |
1184 (string directory-sep-char)) | |
1185 "\\'") | |
1186 filename))))) | |
428 | 1187 (setq filename |
1188 (concat "~" | |
440 | 1189 (match-string 1 filename) |
428 | 1190 (substring filename (match-end 0)))))) |
1191 filename))) | |
1192 | |
1193 (defcustom find-file-not-true-dirname-list nil | |
1194 "*List of logical names for which visiting shouldn't save the true dirname." | |
1195 :type '(repeat (string :tag "Name")) | |
1196 :group 'find-file) | |
1197 | |
1198 ;; This function is needed by FSF vc.el. I hope somebody can make it | |
1199 ;; work for XEmacs. -sb. | |
1200 ;; #### In what way does it not work? --hniksic | |
1201 (defun find-buffer-visiting (filename) | |
1202 "Return the buffer visiting file FILENAME (a string). | |
1203 This is like `get-file-buffer', except that it checks for any buffer | |
1204 visiting the same file, possibly under a different name. | |
1205 If there is no such live buffer, return nil." | |
1206 (let ((buf (get-file-buffer filename)) | |
1207 (truename (abbreviate-file-name (file-truename filename)))) | |
1208 (or buf | |
1209 (let ((list (buffer-list)) found) | |
1210 (while (and (not found) list) | |
1211 (save-excursion | |
1212 (set-buffer (car list)) | |
1213 (if (and buffer-file-name | |
1214 (string= buffer-file-truename truename)) | |
1215 (setq found (car list)))) | |
1216 (setq list (cdr list))) | |
1217 found) | |
1333 | 1218 (let* ((attributes (file-attributes truename)) |
1219 (number (nthcdr 10 attributes)) | |
1220 (list (buffer-list)) found) | |
428 | 1221 (and buffer-file-numbers-unique |
1222 number | |
1223 (while (and (not found) list) | |
1333 | 1224 (with-current-buffer (car list) |
1225 (if (and buffer-file-name | |
1226 (equal buffer-file-number number) | |
428 | 1227 ;; Verify this buffer's file number |
1228 ;; still belongs to its file. | |
1229 (file-exists-p buffer-file-name) | |
1333 | 1230 (equal (file-attributes buffer-file-name) |
1231 attributes)) | |
428 | 1232 (setq found (car list)))) |
1233 (setq list (cdr list)))) | |
1234 found)))) | |
1333 | 1235 |
1236 (defcustom find-file-wildcards t | |
1237 "*Non-nil means file-visiting commands should handle wildcards. | |
1238 For example, if you specify `*.c', that would visit all the files | |
1239 whose names match the pattern." | |
1240 :group 'files | |
1241 ; :version "20.4" | |
1242 :type 'boolean) | |
1243 | |
1244 (defcustom find-file-suppress-same-file-warnings nil | |
1245 "*Non-nil means suppress warning messages for symlinked files. | |
1246 When nil, Emacs prints a warning when visiting a file that is already | |
1247 visited, but with a different name. Setting this option to t | |
1248 suppresses this warning." | |
1249 :group 'files | |
1250 ; :version "21.1" | |
1251 :type 'boolean) | |
1252 | |
1253 (defun find-file-noselect (filename &optional nowarn rawfile wildcards) | |
1254 "Read file FILENAME into a buffer and return the buffer. | |
1255 If a buffer exists visiting FILENAME, return that one, but | |
1256 verify that the file has not changed since visited or saved. | |
1257 The buffer is not selected, just returned to the caller. | |
1258 If NOWARN is non-nil, warning messages will be suppressed. | |
1259 If RAWFILE is non-nil, the file is read literally." | |
1260 (setq filename | |
1261 (abbreviate-file-name | |
1262 (expand-file-name filename))) | |
1263 (if (file-directory-p filename) | |
1264 (or (and find-file-run-dired | |
1265 (loop for fn in find-directory-functions | |
1266 for x = (and (fboundp fn) | |
1267 (funcall fn | |
1268 (if find-file-use-truenames | |
1269 (abbreviate-file-name | |
1270 (file-truename filename)) | |
1271 filename))) | |
1272 if x | |
1273 return x)) | |
1274 (error "%s is a directory" filename)) | |
1275 (if (and wildcards | |
1276 find-file-wildcards | |
1277 (not (string-match "\\`/:" filename)) | |
1278 (string-match "[[*?]" filename)) | |
1279 (let ((files (condition-case nil | |
1280 (file-expand-wildcards filename t) | |
1281 (error (list filename)))) | |
1282 (find-file-wildcards nil)) | |
1283 (if (null files) | |
1284 (find-file-noselect filename) | |
1285 (mapcar #'find-file-noselect files))) | |
1286 (let* ((buf (get-file-buffer filename)) | |
1287 (truename (abbreviate-file-name (file-truename filename))) | |
1288 (number (nthcdr 10 (file-attributes truename))) | |
1289 ; ;; Find any buffer for a file which has same truename. | |
1290 ; (other (and (not buf) (find-buffer-visiting filename))) | |
1346 | 1291 ) |
1333 | 1292 |
1293 ; ;; Let user know if there is a buffer with the same truename. | |
1294 ; (if other | |
1295 ; (progn | |
1296 ; (or nowarn | |
1297 ; find-file-suppress-same-file-warnings | |
1298 ; (string-equal filename (buffer-file-name other)) | |
1299 ; (message "%s and %s are the same file" | |
1300 ; filename (buffer-file-name other))) | |
1301 ; ;; Optionally also find that buffer. | |
1302 ; (if (or find-file-existing-other-name find-file-visit-truename) | |
1303 ; (setq buf other)))) | |
1304 | |
1305 (when (and buf | |
1306 (or find-file-compare-truenames find-file-use-truenames) | |
1307 (not find-file-suppress-same-file-warnings) | |
1308 (not nowarn)) | |
1309 (save-excursion | |
1310 (set-buffer buf) | |
1311 (if (not (string-equal buffer-file-name filename)) | |
1312 (message "%s and %s are the same file (%s)" | |
1313 filename buffer-file-name | |
1314 buffer-file-truename)))) | |
1315 | |
1316 (if buf | |
1317 (progn | |
1318 (or nowarn | |
1319 (verify-visited-file-modtime buf) | |
1320 (cond ((not (file-exists-p filename)) | |
1321 (error "File %s no longer exists!" filename)) | |
1322 ;; Certain files should be reverted automatically | |
1323 ;; if they have changed on disk and not in the buffer. | |
1324 ((and (not (buffer-modified-p buf)) | |
1325 (dolist (rx revert-without-query nil) | |
1326 (when (string-match rx filename) | |
1327 (return t)))) | |
1328 (with-current-buffer buf | |
1329 (message "Reverting file %s..." filename) | |
1330 (revert-buffer t t) | |
1331 (message "Reverting file %s... done" filename))) | |
1332 ((yes-or-no-p | |
1333 (if (string= (file-name-nondirectory filename) | |
1334 (buffer-name buf)) | |
1335 (format | |
1336 (if (buffer-modified-p buf) | |
1337 (gettext "File %s changed on disk. Discard your edits? ") | |
1338 (gettext "File %s changed on disk. Reread from disk? ")) | |
1339 (file-name-nondirectory filename)) | |
1340 (format | |
1341 (if (buffer-modified-p buf) | |
1342 (gettext "File %s changed on disk. Discard your edits in %s? ") | |
1343 (gettext "File %s changed on disk. Reread from disk into %s? ")) | |
1344 (file-name-nondirectory filename) | |
1345 (buffer-name buf)))) | |
1346 (with-current-buffer buf | |
1347 (revert-buffer t t))))) | |
1348 (when (not (eq rawfile (not (null find-file-literally)))) | |
1349 (with-current-buffer buf | |
1350 (if (buffer-modified-p) | |
1351 (if (y-or-n-p (if rawfile | |
1352 "Save file and revisit literally? " | |
1353 "Save file and revisit non-literally? ")) | |
1354 (progn | |
1355 (save-buffer) | |
1356 (find-file-noselect-1 buf filename nowarn | |
1357 rawfile truename number)) | |
1358 (if (y-or-n-p (if rawfile | |
1359 "Discard your edits and revisit file literally? " | |
1360 "Discard your edits and revisit file non-literally? ")) | |
1361 (find-file-noselect-1 buf filename nowarn | |
1362 rawfile truename number) | |
1363 (error (if rawfile "File already visited non-literally" | |
1364 "File already visited literally")))) | |
1365 (if (y-or-n-p (if rawfile | |
1366 "Revisit file literally? " | |
1367 "Revisit file non-literally? ")) | |
1368 (find-file-noselect-1 buf filename nowarn | |
1369 rawfile truename number) | |
1370 (error (if rawfile "File already visited non-literally" | |
1371 "File already visited literally")))))) | |
1372 ;; Return the buffer we are using. | |
1373 buf) | |
1374 ;; Create a new buffer. | |
1375 (setq buf (create-file-buffer filename)) | |
1376 ;; Catch various signals, such as QUIT, and kill the buffer | |
1377 ;; in that case. | |
1378 (condition-case data | |
1379 (progn | |
1380 (set-buffer-major-mode buf) | |
1381 ;; find-file-noselect-1 may use a different buffer. | |
1382 (find-file-noselect-1 buf filename nowarn | |
1383 rawfile truename number)) | |
1384 (t | |
1385 (kill-buffer buf) | |
1386 (signal (car data) (cdr data))))))))) | |
1387 | |
1388 (defun find-file-noselect-1 (buf filename nowarn rawfile truename number) | |
1389 (let ((inhibit-read-only t) | |
1390 error) | |
1391 (with-current-buffer buf | |
1392 (kill-local-variable 'find-file-literally) | |
1393 ;; Needed in case we are re-visiting the file with a different | |
1394 ;; text representation. | |
1395 (kill-local-variable 'buffer-file-coding-system) | |
1396 (erase-buffer) | |
1397 ; (and (default-value 'enable-multibyte-characters) | |
1398 ; (not rawfile) | |
1399 ; (set-buffer-multibyte t)) | |
1400 (condition-case () | |
1401 (if rawfile | |
1402 (insert-file-contents-literally filename t) | |
1403 (insert-file-contents filename t)) | |
1404 (file-error | |
1405 (when (and (file-exists-p filename) | |
1406 (not (file-readable-p filename))) | |
1407 (signal 'file-error (list "File is not readable" filename))) | |
1408 (if rawfile | |
1409 ;; Unconditionally set error | |
1410 (setq error t) | |
1411 (or | |
1412 ;; Run find-file-not-found-hooks until one returns non-nil. | |
1413 (run-hook-with-args-until-success 'find-file-not-found-hooks) | |
1414 ;; If they fail too, set error. | |
1415 (setq error t))))) | |
1416 ;; Find the file's truename, and maybe use that as visited name. | |
1417 ;; automatically computed in XEmacs, unless jka-compr was used! | |
1418 (unless buffer-file-truename | |
1419 (setq buffer-file-truename truename)) | |
1420 (setq buffer-file-number number) | |
1421 (and find-file-use-truenames | |
1422 ;; This should be in C. Put pathname | |
1423 ;; abbreviations that have been explicitly | |
1424 ;; requested back into the pathname. Most | |
1425 ;; importantly, strip out automounter /tmp_mnt | |
1426 ;; directories so that auto-save will work | |
1427 (setq buffer-file-name (abbreviate-file-name buffer-file-name))) | |
1428 ;; Set buffer's default directory to that of the file. | |
1429 (setq default-directory (file-name-directory buffer-file-name)) | |
1430 ;; Turn off backup files for certain file names. Since | |
1431 ;; this is a permanent local, the major mode won't eliminate it. | |
1432 (and (not (funcall backup-enable-predicate buffer-file-name)) | |
1433 (progn | |
1434 (make-local-variable 'backup-inhibited) | |
1435 (setq backup-inhibited t))) | |
1436 (if rawfile | |
1437 (progn | |
1438 (setq buffer-file-coding-system 'no-conversion) | |
1439 (make-local-variable 'find-file-literally) | |
1440 (setq find-file-literally t)) | |
1441 (after-find-file error (not nowarn)) | |
1442 (setq buf (current-buffer))) | |
1443 (current-buffer)))) | |
1444 | |
444 | 1445 (defun insert-file-contents-literally (filename &optional visit start end replace) |
1333 | 1446 "Like `insert-file-contents', but only reads in the file literally. |
1447 A buffer may be modified in several ways after reading into the buffer, | |
1448 due to Emacs features such as format decoding, character code | |
1449 conversion, `find-file-hooks', automatic uncompression, etc. | |
1450 | |
1451 This function ensures that none of these modifications will take place." | |
442 | 1452 (let ((wrap-func (find-file-name-handler filename |
1453 'insert-file-contents-literally))) | |
444 | 1454 (if wrap-func |
442 | 1455 (funcall wrap-func 'insert-file-contents-literally filename |
444 | 1456 visit start end replace) |
442 | 1457 (let ((file-name-handler-alist nil) |
1458 (format-alist nil) | |
1459 (after-insert-file-functions nil) | |
1460 (coding-system-for-read 'binary) | |
1461 (coding-system-for-write 'binary) | |
1462 (find-buffer-file-type-function | |
1463 (if (fboundp 'find-buffer-file-type) | |
1464 (symbol-function 'find-buffer-file-type) | |
1333 | 1465 nil)) |
1466 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler)) | |
1467 (inhibit-file-name-operation 'insert-file-contents)) | |
442 | 1468 (unwind-protect |
1469 (progn | |
1470 (fset 'find-buffer-file-type (lambda (filename) t)) | |
444 | 1471 (insert-file-contents filename visit start end replace)) |
442 | 1472 (if find-buffer-file-type-function |
1473 (fset 'find-buffer-file-type find-buffer-file-type-function) | |
1474 (fmakunbound 'find-buffer-file-type))))))) | |
428 | 1475 |
1333 | 1476 (defun insert-file-literally (filename) |
1477 "Insert contents of file FILENAME into buffer after point with no conversion. | |
1478 | |
1479 This function is meant for the user to run interactively. | |
1480 Don't call it from programs! Use `insert-file-contents-literally' instead. | |
1481 \(Its calling sequence is different; see its documentation)." | |
1482 (interactive "*fInsert file literally: ") | |
428 | 1483 (if (file-directory-p filename) |
1333 | 1484 (signal 'file-error (list "Opening input file" "file is a directory" |
1485 filename))) | |
1486 (let ((tem (insert-file-contents-literally filename))) | |
1487 (push-mark (+ (point) (car (cdr tem)))))) | |
1488 | |
1489 (defvar find-file-literally nil | |
1490 "Non-nil if this buffer was made by `find-file-literally' or equivalent. | |
1491 This is a permanent local.") | |
1492 (put 'find-file-literally 'permanent-local t) | |
1493 | |
1494 (defun find-file-literally (filename) | |
1495 "Visit file FILENAME with no conversion of any kind. | |
1496 Format conversion and character code conversion are both disabled, | |
1497 and multibyte characters are disabled in the resulting buffer. | |
1498 The major mode used is Fundamental mode regardless of the file name, | |
1499 and local variable specifications in the file are ignored. | |
1500 Automatic uncompression and adding a newline at the end of the | |
1501 file due to `require-final-newline' is also disabled. | |
1502 | |
1503 You cannot absolutely rely on this function to result in | |
1504 visiting the file literally. If Emacs already has a buffer | |
1505 which is visiting the file, you get the existing buffer, | |
1506 regardless of whether it was created literally or not. | |
1507 | |
1508 In a Lisp program, if you want to be sure of accessing a file's | |
1509 contents literally, you should create a temporary buffer and then read | |
1510 the file contents into it using `insert-file-contents-literally'." | |
1511 (interactive "FFind file literally: ") | |
1512 (switch-to-buffer (find-file-noselect filename nil t))) | |
428 | 1513 |
1514 (defvar after-find-file-from-revert-buffer nil) | |
1515 | |
1516 (defun after-find-file (&optional error warn noauto | |
1517 after-find-file-from-revert-buffer | |
1518 nomodes) | |
1519 "Called after finding a file and by the default revert function. | |
1520 Sets buffer mode, parses local variables. | |
1521 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an | |
1522 error in reading the file. WARN non-nil means warn if there | |
1523 exists an auto-save file more recent than the visited file. | |
1524 NOAUTO means don't mess with auto-save mode. | |
1525 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil | |
1526 means this call was from `revert-buffer'. | |
1527 Fifth arg NOMODES non-nil means don't alter the file's modes. | |
1333 | 1528 Finishes by calling the functions in `find-file-hooks' |
1529 unless NOMODES is non-nil." | |
428 | 1530 (setq buffer-read-only (not (file-writable-p buffer-file-name))) |
1531 (if noninteractive | |
1532 nil | |
1533 (let* (not-serious | |
1534 (msg | |
1333 | 1535 (cond |
1536 ((not warn) nil) | |
1537 ((and error (file-attributes buffer-file-name)) | |
1538 (setq buffer-read-only t) | |
1539 (gettext "File exists, but cannot be read.")) | |
1540 ((not buffer-read-only) | |
1541 (if (and warn | |
1542 (file-newer-than-file-p (make-auto-save-file-name) | |
1543 buffer-file-name)) | |
1544 (format "%s has auto save data; consider M-x recover-file" | |
1545 (file-name-nondirectory buffer-file-name)) | |
1546 (setq not-serious t) | |
1547 (if error (gettext "(New file)") nil))) | |
1548 ((not error) | |
1549 (setq not-serious t) | |
1550 (gettext "Note: file is write protected")) | |
1551 ((file-attributes (directory-file-name default-directory)) | |
1552 (gettext "File not found and directory write-protected")) | |
1553 ((file-exists-p (file-name-directory buffer-file-name)) | |
1554 (setq buffer-read-only nil)) | |
1555 (t | |
1556 ;; If the directory the buffer is in doesn't exist, | |
1557 ;; offer to create it. It's better to do this now | |
1558 ;; than when we save the buffer, because we want | |
1559 ;; autosaving to work. | |
1560 (setq buffer-read-only nil) | |
1561 ;; XEmacs | |
1562 (or (file-exists-p (file-name-directory buffer-file-name)) | |
1563 (condition-case nil | |
1564 (if (yes-or-no-p | |
1565 (format | |
1566 "\ | |
428 | 1567 The directory containing %s does not exist. Create? " |
1333 | 1568 (abbreviate-file-name buffer-file-name))) |
1569 (make-directory (file-name-directory | |
1570 buffer-file-name) | |
1571 t)) | |
1572 (quit | |
1573 (kill-buffer (current-buffer)) | |
1574 (signal 'quit nil)))) | |
1575 nil)))) | |
428 | 1576 (if msg |
1577 (progn | |
1578 (message "%s" msg) | |
1579 (or not-serious (sit-for 1 t))))) | |
1333 | 1580 (when (and auto-save-default (not noauto)) |
428 | 1581 (auto-save-mode t))) |
1333 | 1582 ;; Make people do a little extra work (C-x C-q) |
1583 ;; before altering a backup file. | |
1584 (when (backup-file-name-p buffer-file-name) | |
1585 (setq buffer-read-only t)) | |
428 | 1586 (unless nomodes |
1333 | 1587 ;; #### No view-mode-disable. |
1588 ; (when view-read-only | |
1589 ; (and-boundp 'view-mode (view-mode-disable))) | |
428 | 1590 (normal-mode t) |
1333 | 1591 (when (and buffer-read-only |
1592 view-read-only | |
1593 (not (eq (get major-mode 'mode-class) 'special))) | |
1594 (view-mode)) | |
428 | 1595 (run-hooks 'find-file-hooks))) |
1596 | |
1597 (defun normal-mode (&optional find-file) | |
1598 "Choose the major mode for this buffer automatically. | |
1599 Also sets up any specified local variables of the file. | |
1600 Uses the visited file name, the -*- line, and the local variables spec. | |
1601 | |
1602 This function is called automatically from `find-file'. In that case, | |
1333 | 1603 we may set up the file-specified mode and local variables, |
1604 depending on the value of `enable-local-variables': if it is t, we do; | |
1605 if it is nil, we don't; otherwise, we query. | |
1606 In addition, if `local-enable-local-variables' is nil, we do | |
1607 not set local variables (though we do notice a mode specified with -*-.) | |
1608 | |
1609 `enable-local-variables' is ignored if you run `normal-mode' interactively, | |
1610 or from Lisp without specifying the optional argument FIND-FILE; | |
1611 in that case, this function acts as if `enable-local-variables' were t." | |
428 | 1612 (interactive) |
1613 (or find-file (funcall (or default-major-mode 'fundamental-mode))) | |
793 | 1614 (and (with-trapping-errors |
1615 :operation "File mode specification" | |
1616 :class 'file-mode-spec | |
1617 :error-form nil | |
1618 (set-auto-mode) | |
1619 t) | |
1620 (with-trapping-errors | |
1621 :operation "File local-variables" | |
1622 :class 'local-variables | |
1623 :error-form nil | |
1333 | 1624 ;; FSF 21.2: |
1625 ; (let ((enable-local-variables (or (not find-file) | |
1626 ; enable-local-variables))) | |
1627 ; (hack-local-variables)) | |
793 | 1628 (hack-local-variables (not find-file))))) |
428 | 1629 |
1333 | 1630 ;; END SYNC WITH FSF 21.2. |
1631 | |
1024 | 1632 ;; `auto-mode-alist' used to contain entries for modes in core and in packages. |
1633 ;; The applicable entries are now located in the corresponding modes in | |
1634 ;; packages, the ones here are for core modes. Ditto for | |
1635 ;; `interpreter-mode-alist' below. | |
1636 ;; Per Abrahamsen suggested splitting auto-mode-alist to | |
428 | 1637 ;; several distinct variables such as, in order of precedence, |
1638 ;; `user-auto-mode-alist' for users, `package-auto-mode-alist' for | |
1639 ;; packages and `auto-mode-alist' (which might also be called | |
1640 ;; `default-auto-mode-alist') for default stuff, such as some of the | |
1641 ;; entries below. | |
1642 | |
1643 (defvar auto-mode-alist | |
1644 '(("\\.te?xt\\'" . text-mode) | |
1645 ("\\.el\\'" . emacs-lisp-mode) | |
1646 ("\\.c?l\\(?:i?sp\\)?\\'" . lisp-mode) | |
1647 ("\\.article\\'" . text-mode) | |
1648 ("\\.letter\\'" . text-mode) | |
1649 ;; Mailer puts message to be edited in /tmp/Re.... or Message | |
1650 ;; #### Unix-specific! | |
1651 ("\\`/tmp/Re" . text-mode) | |
1652 ("/Message[0-9]*\\'" . text-mode) | |
1653 ;; some news reader is reported to use this | |
1654 ("^/tmp/fol/" . text-mode) | |
1655 ;; .emacs following a directory delimiter in either Unix or | |
1656 ;; Windows syntax. | |
1657 ("[/\\][._].*emacs\\'" . emacs-lisp-mode) | |
1658 ("\\.ml\\'" . lisp-mode) | |
1659 ) | |
1660 "Alist of filename patterns vs. corresponding major mode functions. | |
1661 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL). | |
1662 \(NON-NIL stands for anything that is not nil; the value does not matter.) | |
1663 Visiting a file whose name matches REGEXP specifies FUNCTION as the | |
1664 mode function to use. FUNCTION will be called, unless it is nil. | |
1665 | |
1666 If the element has the form (REGEXP FUNCTION NON-NIL), then after | |
1667 calling FUNCTION (if it's not nil), we delete the suffix that matched | |
1668 REGEXP and search the list again for another match.") | |
1669 | |
1670 (defvar interpreter-mode-alist | |
1024 | 1671 '(("emacs" . emacs-lisp-mode)) |
428 | 1672 "Alist mapping interpreter names to major modes. |
1673 This alist is used to guess the major mode of a file based on the | |
1674 contents of the first line. This line often contains something like: | |
1675 #!/bin/sh | |
1676 but may contain something more imaginative like | |
1677 #! /bin/env python | |
1678 or | |
1679 eval 'exec perl -w -S $0 ${1+\"$@\"}'. | |
1680 | |
1681 Each alist element looks like (INTERPRETER . MODE). | |
1682 The car of each element is a regular expression which is compared | |
1683 with the name of the interpreter specified in the first line. | |
1684 If it matches, mode MODE is selected.") | |
1685 | |
1686 (defvar binary-file-regexps | |
444 | 1687 '("\\.\\(?:bz2\\|elc\\|g\\(if\\|z\\)\\|jp\\(eg\\|g\\)\\|png\\|t\\(ar\\|gz\\|iff\\)\\|[Zo]\\)\\'") |
428 | 1688 "List of regexps of filenames containing binary (non-text) data.") |
1689 | |
1690 ; (eval-when-compile | |
1691 ; (require 'regexp-opt) | |
1692 ; (list | |
1693 ; (format "\\.\\(?:%s\\)\\'" | |
1694 ; (regexp-opt | |
1695 ; '("tar" | |
1696 ; "tgz" | |
1697 ; "gz" | |
1698 ; "bz2" | |
1699 ; "Z" | |
1700 ; "o" | |
1701 ; "elc" | |
1702 ; "png" | |
1703 ; "gif" | |
1704 ; "tiff" | |
1705 ; "jpg" | |
1706 ; "jpeg")))))) | |
444 | 1707 |
428 | 1708 (defvar inhibit-first-line-modes-regexps |
444 | 1709 binary-file-regexps |
428 | 1710 "List of regexps; if one matches a file name, don't look for `-*-'.") |
1711 | |
1712 (defvar inhibit-first-line-modes-suffixes nil | |
1713 "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'. | |
1714 When checking `inhibit-first-line-modes-regexps', we first discard | |
1715 from the end of the file name anything that matches one of these regexps.") | |
1716 | |
1333 | 1717 ;; Junk from FSF 21.2. Unnecessary in XEmacs, since `interpreter-mode-alist' |
1718 ;; can have regexps. | |
1719 ; (defvar auto-mode-interpreter-regexp | |
1720 ; "#![ \t]?\\([^ \t\n]*\ | |
1721 ; /bin/env[ \t]\\)?\\([^ \t\n]+\\)" | |
1722 ; "Regular expression matching interpreters, for file mode determination. | |
1723 ; This regular expression is matched against the first line of a file | |
1724 ; to determine the file's mode in `set-auto-mode' when Emacs can't deduce | |
1725 ; a mode from the file's name. If it matches, the file is assumed to | |
1726 ; be interpreted by the interpreter matched by the second group of the | |
1727 ; regular expression. The mode is then determined as the mode associated | |
1728 ; with that interpreter in `interpreter-mode-alist'.") | |
1729 | |
428 | 1730 (defvar user-init-file |
1731 nil ; set by command-line | |
1732 "File name including directory of user's initialization file.") | |
1733 | |
1734 (defun set-auto-mode (&optional just-from-file-name) | |
1735 "Select major mode appropriate for current buffer. | |
1736 This checks for a -*- mode tag in the buffer's text, | |
1737 compares the filename against the entries in `auto-mode-alist', | |
1738 or checks the interpreter that runs this file against | |
1739 `interpreter-mode-alist'. | |
1740 | |
1741 It does not check for the `mode:' local variable in the | |
1742 Local Variables section of the file; for that, use `hack-local-variables'. | |
1743 | |
1744 If `enable-local-variables' is nil, this function does not check for a | |
1745 -*- mode tag. | |
1746 | |
1747 If the optional argument JUST-FROM-FILE-NAME is non-nil, | |
1748 then we do not set anything but the major mode, | |
1749 and we don't even do that unless it would come from the file name." | |
1750 (save-excursion | |
1751 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- | |
1752 ;; Do this by calling the hack-local-variables helper to avoid redundancy. | |
1753 ;; We bind enable-local-variables to nil this time because we're going to | |
1754 ;; call hack-local-variables-prop-line again later, "for real." Note that | |
1755 ;; this temporary binding does not prevent hack-local-variables-prop-line | |
1756 ;; from setting the major mode. | |
1757 (or (and enable-local-variables | |
1758 (let ((enable-local-variables nil)) | |
1759 (hack-local-variables-prop-line nil)) | |
1760 ) | |
1761 ;; It's not in the -*- line, so check the auto-mode-alist, unless | |
1762 ;; this buffer isn't associated with a file. | |
1763 (null buffer-file-name) | |
1764 (let ((name (file-name-sans-versions buffer-file-name)) | |
1765 (keep-going t)) | |
1766 (while keep-going | |
1767 (setq keep-going nil) | |
1768 (let ((alist auto-mode-alist) | |
1769 (mode nil)) | |
440 | 1770 |
428 | 1771 ;; Find first matching alist entry. |
440 | 1772 |
1773 ;; #### This is incorrect. In NT, case sensitivity is a volume | |
1774 ;; property. For instance, NFS mounts *are* case sensitive. | |
1775 ;; Need internal function (file-name-case-sensitive f), F | |
1776 ;; being file or directory name. - kkm | |
428 | 1777 (let ((case-fold-search |
440 | 1778 (eq system-type 'windows-nt))) |
428 | 1779 (while (and (not mode) alist) |
1780 (if (string-match (car (car alist)) name) | |
1781 (if (and (consp (cdr (car alist))) | |
1782 (nth 2 (car alist))) | |
1783 (progn | |
1784 (setq mode (car (cdr (car alist))) | |
1785 name (substring name 0 (match-beginning 0)) | |
1786 keep-going t)) | |
1787 (setq mode (cdr (car alist)) | |
1788 keep-going nil))) | |
1789 (setq alist (cdr alist)))) | |
1790 (unless just-from-file-name | |
1791 ;; If we can't deduce a mode from the file name, | |
1792 ;; look for an interpreter specified in the first line. | |
1793 (if (and (null mode) | |
1794 (save-excursion ; XEmacs | |
1795 (goto-char (point-min)) | |
1796 (looking-at "#!"))) | |
1797 (let ((firstline | |
1798 (buffer-substring | |
1799 (point-min) | |
1800 (save-excursion | |
1801 (goto-char (point-min)) (end-of-line) (point))))) | |
1802 (setq alist interpreter-mode-alist) | |
1803 (while alist | |
1804 (if (string-match (car (car alist)) firstline) | |
1805 (progn | |
1806 (setq mode (cdr (car alist))) | |
1807 (setq alist nil)) | |
1808 (setq alist (cdr alist))))))) | |
1809 (if mode | |
1810 (if (not (fboundp mode)) | |
1811 (let ((name (package-get-package-provider mode))) | |
1812 (if name | |
1813 (message "Mode %s is not installed. Download package %s" mode name) | |
1814 (message "Mode %s either doesn't exist or is not a known package" mode)) | |
1815 (sit-for 2) | |
1816 (error "%s" mode)) | |
1817 (unless (and just-from-file-name | |
1818 (or | |
1819 ;; Don't reinvoke major mode. | |
1820 (eq mode major-mode) | |
1821 ;; Don't lose on minor modes. | |
1822 (assq mode minor-mode-alist))) | |
1823 (funcall mode)))))))))) | |
1824 | |
1825 (defvar hack-local-variables-hook nil | |
1826 "Normal hook run after processing a file's local variables specs. | |
1827 Major modes can use this to examine user-specified local variables | |
1828 in order to initialize other data structure based on them. | |
1829 | |
1830 This hook runs even if there were no local variables or if their | |
1831 evaluation was suppressed. See also `enable-local-variables' and | |
1832 `enable-local-eval'.") | |
1833 | |
1834 (defun hack-local-variables (&optional force) | |
1835 "Parse, and bind or evaluate as appropriate, any local variables | |
1836 for current buffer." | |
1837 ;; Don't look for -*- if this file name matches any | |
1838 ;; of the regexps in inhibit-first-line-modes-regexps. | |
1839 (if (or (null buffer-file-name) ; don't lose if buffer has no file! | |
1840 (not (let ((temp inhibit-first-line-modes-regexps) | |
1841 (name (if buffer-file-name | |
1842 (file-name-sans-versions buffer-file-name) | |
1843 (buffer-name)))) | |
1844 (while (let ((sufs inhibit-first-line-modes-suffixes)) | |
1845 (while (and sufs (not | |
1846 (string-match (car sufs) name))) | |
1847 (setq sufs (cdr sufs))) | |
1848 sufs) | |
1849 (setq name (substring name 0 (match-beginning 0)))) | |
1850 (while (and temp | |
1851 (not (string-match (car temp) name))) | |
1852 (setq temp (cdr temp)) | |
1853 temp)))) | |
1854 (progn | |
1855 ;; Look for variables in the -*- line. | |
1856 (hack-local-variables-prop-line force) | |
1857 ;; Look for "Local variables:" block in last page. | |
1858 (hack-local-variables-last-page force))) | |
1859 (run-hooks 'hack-local-variables-hook)) | |
1860 | |
1861 ;;; Local variables may be specified in the last page of the file (within 3k | |
1862 ;;; from the end of the file and after the last ^L) in the form | |
1863 ;;; | |
1864 ;;; Local variables: | |
1865 ;;; variable-name: variable-value | |
1866 ;;; end: | |
1867 ;;; | |
1868 ;;; The lines may begin with a common prefix, like ";;; " in the above | |
1869 ;;; example. They may also have a common suffix (" */" for example). In | |
1870 ;;; this form, the local variable "mode" can be used to change the major | |
1871 ;;; mode, and the local variable "eval" can be used to evaluate an arbitrary | |
1872 ;;; form. | |
1873 ;;; | |
1874 ;;; Local variables may also be specified in the first line of the file. | |
1875 ;;; Embedded in this line are a pair of "-*-" sequences. What lies between | |
1876 ;;; them are variable-name/variable-value pairs, like: | |
1877 ;;; | |
1878 ;;; -*- mode: emacs-lisp -*- | |
1879 ;;; or -*- mode: postscript; version-control: never -*- | |
1880 ;;; or -*- tags-file-name: "/foo/bar/TAGS" -*- | |
1881 ;;; | |
1882 ;;; The local variable "eval" is not used with this form. For hysterical | |
1883 ;;; reasons, the syntax "-*- modename -*-" is allowed as well. | |
1884 ;;; | |
1885 | |
1886 (defun hack-local-variables-p (modeline) | |
1887 (or (eq enable-local-variables t) | |
1888 (and enable-local-variables | |
1889 (save-window-excursion | |
1890 (condition-case nil | |
1891 (switch-to-buffer (current-buffer)) | |
1892 (error | |
1893 ;; If we fail to switch in the selected window, | |
1894 ;; it is probably a minibuffer. | |
1895 ;; So try another window. | |
1896 (condition-case nil | |
1897 (switch-to-buffer-other-window (current-buffer)) | |
1898 (error | |
1899 (switch-to-buffer-other-frame (current-buffer)))))) | |
1900 (or modeline (save-excursion | |
1901 (beginning-of-line) | |
1902 (set-window-start (selected-window) (point)))) | |
1903 (y-or-n-p (format | |
1904 "Set local variables as specified %s of %s? " | |
1905 (if modeline "in -*- line" "at end") | |
1906 (if buffer-file-name | |
1907 (file-name-nondirectory buffer-file-name) | |
1908 (concat "buffer " (buffer-name))))))))) | |
1909 | |
1910 (defun hack-local-variables-last-page (&optional force) | |
1911 ;; Set local variables set in the "Local Variables:" block of the last page. | |
1912 (save-excursion | |
1913 (goto-char (point-max)) | |
1914 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) | |
1915 (if (let ((case-fold-search t)) | |
1916 (and (search-forward "Local Variables:" nil t) | |
1917 (or force | |
1918 (hack-local-variables-p nil)))) | |
1919 (let ((continue t) | |
444 | 1920 prefix prefixlen suffix start |
428 | 1921 (enable-local-eval enable-local-eval)) |
1922 ;; The prefix is what comes before "local variables:" in its line. | |
1923 ;; The suffix is what comes after "local variables:" in its line. | |
1924 (skip-chars-forward " \t") | |
1925 (or (eolp) | |
1926 (setq suffix (buffer-substring (point) | |
1927 (progn (end-of-line) (point))))) | |
1928 (goto-char (match-beginning 0)) | |
1929 (or (bolp) | |
1930 (setq prefix | |
1931 (buffer-substring (point) | |
1932 (progn (beginning-of-line) (point))))) | |
1933 (if prefix (setq prefixlen (length prefix) | |
1934 prefix (regexp-quote prefix))) | |
1935 (if suffix (setq suffix (concat (regexp-quote suffix) "$"))) | |
1936 (while continue | |
1937 ;; Look at next local variable spec. | |
1938 (if selective-display (re-search-forward "[\n\C-m]") | |
1939 (forward-line 1)) | |
1940 ;; Skip the prefix, if any. | |
1941 (if prefix | |
1942 (if (looking-at prefix) | |
1943 (forward-char prefixlen) | |
1944 (error "Local variables entry is missing the prefix"))) | |
1945 ;; Find the variable name; strip whitespace. | |
1946 (skip-chars-forward " \t") | |
444 | 1947 (setq start (point)) |
428 | 1948 (skip-chars-forward "^:\n") |
1949 (if (eolp) (error "Missing colon in local variables entry")) | |
1950 (skip-chars-backward " \t") | |
444 | 1951 (let* ((str (buffer-substring start (point))) |
428 | 1952 (var (read str)) |
1953 val) | |
1954 ;; Setting variable named "end" means end of list. | |
801 | 1955 (if (equalp str "end") |
428 | 1956 (setq continue nil) |
1957 ;; Otherwise read the variable value. | |
1958 (skip-chars-forward "^:") | |
1959 (forward-char 1) | |
1960 (setq val (read (current-buffer))) | |
1961 (skip-chars-backward "\n") | |
1962 (skip-chars-forward " \t") | |
1963 (or (if suffix (looking-at suffix) (eolp)) | |
1964 (error "Local variables entry is terminated incorrectly")) | |
1965 ;; Set the variable. "Variables" mode and eval are funny. | |
1966 (hack-one-local-variable var val)))))))) | |
1967 | |
1968 ;; jwz - New Version 20.1/19.15 | |
1969 (defun hack-local-variables-prop-line (&optional force) | |
1970 ;; Set local variables specified in the -*- line. | |
1971 ;; Returns t if mode was set. | |
1972 (let ((result nil)) | |
1973 (save-excursion | |
1974 (goto-char (point-min)) | |
1975 (skip-chars-forward " \t\n\r") | |
1976 (let ((end (save-excursion | |
1977 ;; If the file begins with "#!" | |
1978 ;; (un*x exec interpreter magic), look | |
1979 ;; for mode frobs in the first two | |
1980 ;; lines. You cannot necessarily | |
1981 ;; put them in the first line of | |
1982 ;; such a file without screwing up | |
1983 ;; the interpreter invocation. | |
1984 (end-of-line (and (looking-at "^#!") 2)) | |
1985 (point)))) | |
1986 ;; Parse the -*- line into the `result' alist. | |
1987 (cond ((not (search-forward "-*-" end t)) | |
1988 ;; doesn't have one. | |
1989 (setq force t)) | |
442 | 1990 ((looking-at "[ \t]*\\([^ \t\n\r:;]+?\\)\\([ \t]*-\\*-\\)") |
428 | 1991 ;; Antiquated form: "-*- ModeName -*-". |
1992 (setq result | |
1993 (list (cons 'mode | |
1994 (intern (buffer-substring | |
1995 (match-beginning 1) | |
1996 (match-end 1))))) | |
1997 )) | |
1998 (t | |
1999 ;; Usual form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' | |
2000 ;; (last ";" is optional). | |
2001 (save-excursion | |
2002 (if (search-forward "-*-" end t) | |
2003 (setq end (- (point) 3)) | |
2004 (error "-*- not terminated before end of line"))) | |
2005 (while (< (point) end) | |
2006 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*") | |
2007 (error "malformed -*- line")) | |
2008 (goto-char (match-end 0)) | |
2009 ;; There used to be a downcase here, | |
2010 ;; but the manual didn't say so, | |
2011 ;; and people want to set var names that aren't all lc. | |
2012 (let ((key (intern (buffer-substring | |
2013 (match-beginning 1) | |
2014 (match-end 1)))) | |
2015 (val (save-restriction | |
2016 (narrow-to-region (point) end) | |
2017 (read (current-buffer))))) | |
2018 ;; Case sensitivity! Icepicks in my forehead! | |
801 | 2019 (if (equalp (symbol-name key) "mode") |
428 | 2020 (setq key 'mode)) |
2021 (setq result (cons (cons key val) result)) | |
2022 (skip-chars-forward " \t;"))) | |
2023 (setq result (nreverse result)))))) | |
2024 | |
2025 (let ((set-any-p (or force | |
2026 ;; It's OK to force null specifications. | |
2027 (null result) | |
2028 ;; It's OK to force mode-only specifications. | |
2029 (let ((remaining result) | |
2030 (mode-specs-only t)) | |
2031 (while remaining | |
2032 (if (eq (car (car remaining)) 'mode) | |
2033 (setq remaining (cdr remaining)) | |
2034 ;; Otherwise, we have a real local. | |
2035 (setq mode-specs-only nil | |
2036 remaining nil)) | |
2037 ) | |
2038 mode-specs-only) | |
2039 ;; Otherwise, check. | |
2040 (hack-local-variables-p t))) | |
2041 (mode-p nil)) | |
2042 (while result | |
2043 (let ((key (car (car result))) | |
2044 (val (cdr (car result)))) | |
2045 (cond ((eq key 'mode) | |
2046 (setq mode-p t) | |
2047 (let ((mode (intern (concat (downcase (symbol-name val)) | |
2048 "-mode")))) | |
2049 ;; Without this guard, `normal-mode' would potentially run | |
2050 ;; the major mode function twice: once via `set-auto-mode' | |
2051 ;; and once via `hack-local-variables'. | |
2052 (if (not (eq mode major-mode)) | |
2053 (funcall mode)) | |
2054 )) | |
2055 (set-any-p | |
2056 (hack-one-local-variable key val)) | |
2057 (t | |
2058 nil))) | |
2059 (setq result (cdr result))) | |
2060 mode-p))) | |
2061 | |
1333 | 2062 ;; BEGIN SYNC WITH FSF 21.2. |
2063 | |
428 | 2064 (defconst ignored-local-variables |
2065 (list 'enable-local-eval) | |
2066 "Variables to be ignored in a file's local variable spec.") | |
2067 | |
2068 ;; Get confirmation before setting these variables as locals in a file. | |
2069 (put 'debugger 'risky-local-variable t) | |
2070 (put 'enable-local-eval 'risky-local-variable t) | |
2071 (put 'ignored-local-variables 'risky-local-variable t) | |
2072 (put 'eval 'risky-local-variable t) | |
2073 (put 'file-name-handler-alist 'risky-local-variable t) | |
2074 (put 'minor-mode-map-alist 'risky-local-variable t) | |
2075 (put 'after-load-alist 'risky-local-variable t) | |
2076 (put 'buffer-file-name 'risky-local-variable t) | |
2077 (put 'buffer-auto-save-file-name 'risky-local-variable t) | |
2078 (put 'buffer-file-truename 'risky-local-variable t) | |
2079 (put 'exec-path 'risky-local-variable t) | |
2080 (put 'load-path 'risky-local-variable t) | |
2081 (put 'exec-directory 'risky-local-variable t) | |
2082 (put 'process-environment 'risky-local-variable t) | |
1333 | 2083 (put 'dabbrev-case-fold-search 'risky-local-variable t) |
2084 (put 'dabbrev-case-replace 'risky-local-variable t) | |
428 | 2085 ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode. |
2086 (put 'outline-level 'risky-local-variable t) | |
2087 (put 'rmail-output-file-alist 'risky-local-variable t) | |
2088 | |
2089 ;; This one is safe because the user gets to check it before it is used. | |
2090 (put 'compile-command 'safe-local-variable t) | |
2091 | |
1333 | 2092 (defun hack-one-local-variable-quotep (exp) |
2093 (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp)))) | |
2094 | |
428 | 2095 (defun hack-one-local-variable (var val) |
1333 | 2096 "\"Set\" one variable in a local variables spec. |
2097 A few variable names are treated specially." | |
428 | 2098 (cond ((eq var 'mode) |
2099 (funcall (intern (concat (downcase (symbol-name val)) | |
2100 "-mode")))) | |
1333 | 2101 ((eq var 'coding) |
2102 ;; We have already handled coding: tag in set-auto-coding. | |
2103 nil) | |
428 | 2104 ((memq var ignored-local-variables) |
2105 nil) | |
2106 ;; "Setting" eval means either eval it or do nothing. | |
2107 ;; Likewise for setting hook variables. | |
2108 ((or (get var 'risky-local-variable) | |
2109 (and | |
1333 | 2110 (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$" |
428 | 2111 (symbol-name var)) |
2112 (not (get var 'safe-local-variable)))) | |
1333 | 2113 ;; Permit evalling a put of a harmless property. |
2114 ;; if the args do nothing tricky. | |
2115 (if (or (and (eq var 'eval) | |
2116 (consp val) | |
2117 (eq (car val) 'put) | |
2118 (hack-one-local-variable-quotep (nth 1 val)) | |
2119 (hack-one-local-variable-quotep (nth 2 val)) | |
2120 ;; Only allow safe values of lisp-indent-hook; | |
2121 ;; not functions. | |
2122 (or (numberp (nth 3 val)) | |
2123 (equal (nth 3 val) ''defun)) | |
2124 (memq (nth 1 (nth 2 val)) | |
2125 '(lisp-indent-hook))) | |
2126 ;; Permit eval if not root and user says ok. | |
2127 (and (not (zerop (user-uid))) | |
2128 (or (eq enable-local-eval t) | |
2129 (and enable-local-eval | |
2130 (save-window-excursion | |
2131 (switch-to-buffer (current-buffer)) | |
2132 (save-excursion | |
2133 (beginning-of-line) | |
2134 (set-window-start (selected-window) (point))) | |
2135 (setq enable-local-eval | |
2136 (y-or-n-p (format "Process `eval' or hook local variables in %s? " | |
2137 (if buffer-file-name | |
2138 (concat "file " (file-name-nondirectory buffer-file-name)) | |
2139 (concat "buffer " (buffer-name))))))))))) | |
428 | 2140 (if (eq var 'eval) |
2141 (save-excursion (eval val)) | |
2142 (make-local-variable var) | |
2143 (set var val)) | |
1333 | 2144 (message "Ignoring `eval:' in the local variables list"))) |
428 | 2145 ;; Ordinary variable, really set it. |
2146 (t (make-local-variable var) | |
2147 (set var val)))) | |
502 | 2148 |
2149 (defun find-coding-system-magic-cookie-in-file (file) | |
2150 "Look for the coding-system magic cookie in FILE. | |
2151 The coding-system magic cookie is either the local variable specification | |
2152 -*- ... coding: ... -*- on the first line, or the exact string | |
2153 \";;;###coding system: \" somewhere within the first 3000 characters | |
2154 of the file. If found, the coding system name (as a string) is returned; | |
2155 otherwise nil is returned. Note that it is extremely unlikely that | |
2156 either such string would occur coincidentally as the result of encoding | |
2157 some characters in a non-ASCII charset, and that the spaces make it | |
2158 even less likely since the space character is not a valid octet in any | |
2159 ISO 2022 encoding of most non-ASCII charsets." | |
2160 (save-excursion | |
2161 (with-temp-buffer | |
2162 (let ((coding-system-for-read 'raw-text)) | |
2434 | 2163 (insert-file-contents file nil 0 3000)) |
502 | 2164 (goto-char (point-min)) |
2165 (or (and (looking-at | |
2166 "^[^\n]*-\\*-[^\n]*coding: \\([^ \t\n;]+\\)[^\n]*-\\*-") | |
2167 (buffer-substring (match-beginning 1) (match-end 1))) | |
2168 ;; (save-excursion | |
2169 ;; (let (start end) | |
2170 ;; (and (re-search-forward "^;+[ \t]*Local Variables:" nil t) | |
2171 ;; (setq start (match-end 0)) | |
2172 ;; (re-search-forward "\n;+[ \t]*End:") | |
2173 ;; (setq end (match-beginning 0)) | |
2174 ;; (save-restriction | |
2175 ;; (narrow-to-region start end) | |
2176 ;; (goto-char start) | |
2177 ;; (re-search-forward "^;;; coding: \\([^\n]+\\)$" nil t) | |
2178 ;; ) | |
2179 ;; (let ((codesys | |
2180 ;; (intern (buffer-substring | |
2181 ;; (match-beginning 1)(match-end 1))))) | |
2182 ;; (if (find-coding-system codesys) codesys)) | |
2183 ;; ))) | |
2184 (let ((case-fold-search nil)) | |
2185 (if (search-forward | |
2186 ";;;###coding system: " (+ (point-min) 3000) t) | |
2187 (let ((start (point)) | |
2188 (end (progn | |
2189 (skip-chars-forward "^ \t\n\r") | |
2190 (point)))) | |
2191 (if (> end start) (buffer-substring start end)) | |
2192 ))) | |
2193 )))) | |
428 | 2194 |
1333 | 2195 |
428 | 2196 (defcustom change-major-mode-with-file-name t |
2197 "*Non-nil means \\[write-file] should set the major mode from the file name. | |
2198 However, the mode will not be changed if | |
2199 \(1) a local variables list or the `-*-' line specifies a major mode, or | |
2200 \(2) the current major mode is a \"special\" mode, | |
2201 \ not suitable for ordinary files, or | |
2202 \(3) the new file name does not particularly specify any mode." | |
2203 :type 'boolean | |
2204 :group 'editing-basics) | |
2205 | |
2206 (defun set-visited-file-name (filename &optional no-query along-with-file) | |
2207 "Change name of file visited in current buffer to FILENAME. | |
2208 The next time the buffer is saved it will go in the newly specified file. | |
2209 nil or empty string as argument means make buffer not be visiting any file. | |
2210 Remember to delete the initial contents of the minibuffer | |
2211 if you wish to pass an empty string as the argument. | |
2212 | |
2213 The optional second argument NO-QUERY, if non-nil, inhibits asking for | |
2214 confirmation in the case where another buffer is already visiting FILENAME. | |
2215 | |
2216 The optional third argument ALONG-WITH-FILE, if non-nil, means that | |
2217 the old visited file has been renamed to the new name FILENAME." | |
2218 (interactive "FSet visited file name: ") | |
2219 (if (buffer-base-buffer) | |
2220 (error "An indirect buffer cannot visit a file")) | |
2221 (let (truename) | |
2222 (if filename | |
2223 (setq filename | |
2224 (if (string-equal filename "") | |
2225 nil | |
2226 (expand-file-name filename)))) | |
2227 (if filename | |
2228 (progn | |
2229 (setq truename (file-truename filename)) | |
2230 ;; #### Do we need to check if truename is non-nil? | |
2231 (if find-file-use-truenames | |
2232 (setq filename truename)))) | |
2233 (let ((buffer (and filename (find-buffer-visiting filename)))) | |
2234 (and buffer (not (eq buffer (current-buffer))) | |
2235 (not no-query) | |
2236 (not (y-or-n-p (message "A buffer is visiting %s; proceed? " | |
2237 filename))) | |
2238 (error "Aborted"))) | |
2239 (or (equal filename buffer-file-name) | |
2240 (progn | |
2241 (and filename (lock-buffer filename)) | |
2242 (unlock-buffer))) | |
2243 (setq buffer-file-name filename) | |
2244 (if filename ; make buffer name reflect filename. | |
2245 (let ((new-name (file-name-nondirectory buffer-file-name))) | |
2246 (if (string= new-name "") | |
2247 (error "Empty file name")) | |
2248 (setq default-directory (file-name-directory buffer-file-name)) | |
2249 (or (string= new-name (buffer-name)) | |
2250 (rename-buffer new-name t)))) | |
2251 (setq buffer-backed-up nil) | |
2252 (or along-with-file | |
2253 (clear-visited-file-modtime)) | |
2254 (compute-buffer-file-truename) ; insert-file-contents does this too. | |
2255 ; ;; Abbreviate the file names of the buffer. | |
2256 ; (if truename | |
2257 ; (progn | |
2258 ; (setq buffer-file-truename (abbreviate-file-name truename)) | |
2259 ; (if find-file-visit-truename | |
2260 ; (setq buffer-file-name buffer-file-truename)))) | |
2261 (setq buffer-file-number | |
2262 (if filename | |
2263 (nthcdr 10 (file-attributes buffer-file-name)) | |
2264 nil))) | |
2265 ;; write-file-hooks is normally used for things like ftp-find-file | |
2266 ;; that visit things that are not local files as if they were files. | |
2267 ;; Changing to visit an ordinary local file instead should flush the hook. | |
2268 (kill-local-variable 'write-file-hooks) | |
2269 (kill-local-variable 'after-save-hook) | |
2270 (kill-local-variable 'local-write-file-hooks) | |
2271 (kill-local-variable 'write-file-data-hooks) | |
2272 (kill-local-variable 'revert-buffer-function) | |
2273 (kill-local-variable 'backup-inhibited) | |
2274 ;; If buffer was read-only because of version control, | |
2275 ;; that reason is gone now, so make it writable. | |
502 | 2276 (if-boundp 'vc-mode |
2277 (progn | |
2278 (if vc-mode | |
2279 (setq buffer-read-only nil)) | |
2280 (kill-local-variable 'vc-mode))) | |
428 | 2281 ;; Turn off backup files for certain file names. |
2282 ;; Since this is a permanent local, the major mode won't eliminate it. | |
2283 (and buffer-file-name | |
2284 (not (funcall backup-enable-predicate buffer-file-name)) | |
2285 (progn | |
2286 (make-local-variable 'backup-inhibited) | |
2287 (setq backup-inhibited t))) | |
2288 (let ((oauto buffer-auto-save-file-name)) | |
2289 ;; If auto-save was not already on, turn it on if appropriate. | |
2290 (if (not buffer-auto-save-file-name) | |
2291 (and buffer-file-name auto-save-default | |
2292 (auto-save-mode t)) | |
2293 ;; If auto save is on, start using a new name. | |
2294 ;; We deliberately don't rename or delete the old auto save | |
2295 ;; for the old visited file name. This is because perhaps | |
2296 ;; the user wants to save the new state and then compare with the | |
2297 ;; previous state from the auto save file. | |
2298 (setq buffer-auto-save-file-name | |
2299 (make-auto-save-file-name))) | |
2300 ;; Rename the old auto save file if any. | |
2301 (and oauto buffer-auto-save-file-name | |
2302 (file-exists-p oauto) | |
2303 (rename-file oauto buffer-auto-save-file-name t))) | |
2304 (if buffer-file-name | |
2305 (not along-with-file) | |
2306 (set-buffer-modified-p t)) | |
2307 ;; Update the major mode, if the file name determines it. | |
2308 (condition-case nil | |
2309 ;; Don't change the mode if it is special. | |
2310 (or (not change-major-mode-with-file-name) | |
2311 (get major-mode 'mode-class) | |
2312 ;; Don't change the mode if the local variable list specifies it. | |
2313 (hack-local-variables t) | |
2314 (set-auto-mode t)) | |
2315 (error nil)) | |
1333 | 2316 ;; #### ?? not in FSF. |
428 | 2317 (run-hooks 'after-set-visited-file-name-hooks)) |
2318 | |
2319 (defun write-file (filename &optional confirm codesys) | |
2320 "Write current buffer into file FILENAME. | |
1333 | 2321 This makes the buffer visit that file, and marks it as not modified. |
2322 | |
2323 If you specify just a directory name as FILENAME, that means to use | |
2324 the default file name but in that directory. You can also yank | |
2325 the default file name into the minibuffer to edit it, using M-n. | |
2326 | |
2327 If the buffer is not already visiting a file, the default file name | |
2328 for the output file is the buffer name. | |
2329 | |
2330 If optional second arg CONFIRM is non-nil, this function | |
2331 asks for confirmation before overwriting an existing file. | |
2332 Interactively, this is always the case. | |
771 | 2333 |
2334 Optional third argument specifies the coding system to use when encoding | |
2335 the file. Interactively, with a prefix argument, you will be prompted for | |
2336 the coding system." | |
428 | 2337 ;; (interactive "FWrite file: ") |
2338 (interactive | |
2339 (list (if buffer-file-name | |
2340 (read-file-name "Write file: " | |
2341 nil nil nil nil) | |
1333 | 2342 (read-file-name "Write file: " default-directory |
2343 (expand-file-name | |
2344 (file-name-nondirectory (buffer-name)) | |
2345 default-directory) | |
2346 nil nil)) | |
428 | 2347 t |
771 | 2348 (if current-prefix-arg (read-coding-system "Coding system: ")))) |
428 | 2349 (and (eq (current-buffer) mouse-grabbed-buffer) |
2350 (error "Can't write minibuffer window")) | |
2351 (or (null filename) (string-equal filename "") | |
2352 (progn | |
2353 ;; If arg is just a directory, | |
1333 | 2354 ;; use the default file name, but in that directory. |
2355 (if (file-directory-p filename) | |
428 | 2356 (setq filename (concat (file-name-as-directory filename) |
1333 | 2357 (file-name-nondirectory |
2358 (or buffer-file-name (buffer-name)))))) | |
428 | 2359 (and confirm |
2360 (file-exists-p filename) | |
2361 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename)) | |
2362 (error "Canceled"))) | |
1333 | 2363 (set-visited-file-name filename (not confirm)))) |
428 | 2364 (set-buffer-modified-p t) |
1333 | 2365 ;; Make buffer writable if file is writable. |
2366 (and buffer-file-name | |
2367 (file-writable-p buffer-file-name) | |
2368 (setq buffer-read-only nil)) | |
428 | 2369 (if codesys |
2370 (let ((buffer-file-coding-system (get-coding-system codesys))) | |
2371 (save-buffer)) | |
2372 (save-buffer))) | |
2373 | |
1333 | 2374 |
428 | 2375 (defun backup-buffer () |
2376 "Make a backup of the disk file visited by the current buffer, if appropriate. | |
2377 This is normally done before saving the buffer the first time. | |
1333 | 2378 If the value is non-nil, it is the result of `file-modes' on the original |
2379 file; this means that the caller, after saving the buffer, should change | |
2380 the modes of the new file to agree with the old modes. | |
2381 | |
2382 A backup may be done by renaming or by copying; see documentation of | |
2383 variable `make-backup-files'. If it's done by renaming, then the file is | |
2384 no longer accessible under its old name." | |
428 | 2385 (if buffer-file-name |
2386 (let ((handler (find-file-name-handler buffer-file-name 'backup-buffer))) | |
2387 (if handler | |
2388 (funcall handler 'backup-buffer) | |
2389 (if (and make-backup-files | |
2390 (not backup-inhibited) | |
2391 (not buffer-backed-up) | |
2392 (file-exists-p buffer-file-name) | |
2393 (memq (aref (elt (file-attributes buffer-file-name) 8) 0) | |
2394 '(?- ?l))) | |
2395 (let ((real-file-name buffer-file-name) | |
2396 backup-info backupname targets setmodes) | |
2397 ;; If specified name is a symbolic link, chase it to the target. | |
2398 ;; Thus we make the backups in the directory where the real file is. | |
2399 (setq real-file-name (file-chase-links real-file-name)) | |
2400 (setq backup-info (find-backup-file-name real-file-name) | |
2401 backupname (car backup-info) | |
2402 targets (cdr backup-info)) | |
2403 ;;; (if (file-directory-p buffer-file-name) | |
2404 ;;; (error "Cannot save buffer in directory %s" buffer-file-name)) | |
2405 (if backup-info | |
2406 (condition-case () | |
2407 (let ((delete-old-versions | |
2408 ;; If have old versions to maybe delete, | |
2409 ;; ask the user to confirm now, before doing anything. | |
440 | 2410 ;; But don't actually delete till later. |
428 | 2411 (and targets |
2412 (or (eq delete-old-versions t) | |
2413 (eq delete-old-versions nil)) | |
2414 (or delete-old-versions | |
2415 (y-or-n-p (format "Delete excess backup versions of %s? " | |
2416 real-file-name)))))) | |
2417 ;; Actually write the back up file. | |
2418 (condition-case () | |
2419 (if (or file-precious-flag | |
2420 ; (file-symlink-p buffer-file-name) | |
2421 backup-by-copying | |
2422 (and backup-by-copying-when-linked | |
2423 (> (file-nlinks real-file-name) 1)) | |
1333 | 2424 (and (or backup-by-copying-when-mismatch |
2425 (integerp backup-by-copying-when-privileged-mismatch)) | |
428 | 2426 (let ((attr (file-attributes real-file-name))) |
1333 | 2427 (and (or backup-by-copying-when-mismatch |
2428 (and (integerp (nth 2 attr)) | |
2429 (integerp backup-by-copying-when-privileged-mismatch) | |
2430 (<= (nth 2 attr) backup-by-copying-when-privileged-mismatch))) | |
2431 (or (nth 9 attr) | |
2432 (not (file-ownership-preserved-p real-file-name))))))) | |
428 | 2433 (condition-case () |
2434 (copy-file real-file-name backupname t t) | |
2435 (file-error | |
2436 ;; If copying fails because file BACKUPNAME | |
2437 ;; is not writable, delete that file and try again. | |
2438 (if (and (file-exists-p backupname) | |
2439 (not (file-writable-p backupname))) | |
2440 (delete-file backupname)) | |
2441 (copy-file real-file-name backupname t t))) | |
2442 ;; rename-file should delete old backup. | |
2443 (rename-file real-file-name backupname t) | |
2444 (setq setmodes (file-modes backupname))) | |
2445 (file-error | |
2710 | 2446 ;; If trouble writing the backup, write |
2447 ;; it in `auto-save-directory'. Fall | |
2448 ;; back to $HOME if that's not possible. | |
464 | 2449 (setq backupname |
2710 | 2450 (expand-file-name "%backup%~" |
2451 (or (when (and auto-save-directory | |
2452 (file-writable-p auto-save-directory)) | |
2453 auto-save-directory) | |
2454 (getenv "HOME")))) | |
1333 | 2455 (lwarn 'file 'alert "Cannot write backup file; backing up in %s" |
2456 (file-name-nondirectory backupname)) | |
428 | 2457 (sleep-for 1) |
2458 (condition-case () | |
2459 (copy-file real-file-name backupname t t) | |
2460 (file-error | |
2461 ;; If copying fails because file BACKUPNAME | |
2462 ;; is not writable, delete that file and try again. | |
2463 (if (and (file-exists-p backupname) | |
2464 (not (file-writable-p backupname))) | |
2465 (delete-file backupname)) | |
2466 (copy-file real-file-name backupname t t))))) | |
2467 (setq buffer-backed-up t) | |
2468 ;; Now delete the old versions, if desired. | |
2469 (if delete-old-versions | |
2470 (while targets | |
2471 (ignore-file-errors (delete-file (car targets))) | |
2472 (setq targets (cdr targets)))) | |
2473 setmodes) | |
2474 (file-error nil))))))))) | |
2475 | |
2476 (defun file-name-sans-versions (name &optional keep-backup-version) | |
2477 "Return FILENAME sans backup versions or strings. | |
2478 This is a separate procedure so your site-init or startup file can | |
2479 redefine it. | |
2480 If the optional argument KEEP-BACKUP-VERSION is non-nil, | |
2481 we do not remove backup version numbers, only true file version numbers." | |
2482 (let ((handler (find-file-name-handler name 'file-name-sans-versions))) | |
2483 (if handler | |
2484 (funcall handler 'file-name-sans-versions name keep-backup-version) | |
2485 (substring name 0 | |
2486 (if keep-backup-version | |
2487 (length name) | |
2488 (or (string-match "\\.~[0-9.]+~\\'" name) | |
2489 ;; XEmacs - VC uses extensions like ".~tagname~" or ".~1.1.5.2~" | |
2490 (let ((pos (string-match "\\.~\\([^.~ \t]+\\|[0-9.]+\\)~\\'" name))) | |
2491 (and pos | |
2492 ;; #### - is this filesystem check too paranoid? | |
2493 (file-exists-p (substring name 0 pos)) | |
2494 pos)) | |
2495 (string-match "~\\'" name) | |
2496 (length name))))))) | |
2497 | |
2498 (defun file-ownership-preserved-p (file) | |
2499 "Return t if deleting FILE and rewriting it would preserve the owner." | |
2500 (let ((handler (find-file-name-handler file 'file-ownership-preserved-p))) | |
2501 (if handler | |
2502 (funcall handler 'file-ownership-preserved-p file) | |
2503 (let ((attributes (file-attributes file))) | |
2504 ;; Return t if the file doesn't exist, since it's true that no | |
2505 ;; information would be lost by an (attempted) delete and create. | |
2506 (or (null attributes) | |
2507 (= (nth 2 attributes) (user-uid))))))) | |
2508 | |
2509 (defun file-name-sans-extension (filename) | |
2510 "Return FILENAME sans final \"extension\". | |
2511 The extension, in a file name, is the part that follows the last `.'." | |
2512 (save-match-data | |
2513 (let ((file (file-name-sans-versions (file-name-nondirectory filename))) | |
2514 directory) | |
2515 (if (string-match "\\.[^.]*\\'" file) | |
2516 (if (setq directory (file-name-directory filename)) | |
2517 (expand-file-name (substring file 0 (match-beginning 0)) | |
2518 directory) | |
2519 (substring file 0 (match-beginning 0))) | |
2520 filename)))) | |
2521 | |
2522 (defun file-name-extension (filename &optional period) | |
2523 "Return FILENAME's final \"extension\". | |
2524 The extension, in a file name, is the part that follows the last `.'. | |
2525 Return nil for extensionless file names such as `foo'. | |
3061 | 2526 Return the empty string for file names such as `foo'. |
428 | 2527 |
2528 If PERIOD is non-nil, then the returned value includes the period | |
2529 that delimits the extension, and if FILENAME has no extension, | |
2530 the value is \"\"." | |
2531 (save-match-data | |
2532 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))) | |
2533 (if (string-match "\\.[^.]*\\'" file) | |
2534 (substring file (+ (match-beginning 0) (if period 0 1))) | |
2535 (if period | |
2536 ""))))) | |
2537 | |
1333 | 2538 (defcustom make-backup-file-name-function nil |
2539 "A function to use instead of the default `make-backup-file-name'. | |
2540 A value of nil gives the default `make-backup-file-name' behaviour. | |
2541 | |
2542 This could be buffer-local to do something special for specific | |
2543 files. If you define it, you may need to change `backup-file-name-p' | |
2544 and `file-name-sans-versions' too. | |
2545 | |
2546 See also `backup-directory-alist'." | |
2547 :group 'backup | |
2548 :type '(choice (const :tag "Default" nil) | |
2549 (function :tag "Your function"))) | |
2550 | |
2551 (defcustom backup-directory-alist nil | |
2552 "Alist of filename patterns and backup directory names. | |
2553 Each element looks like (REGEXP . DIRECTORY). Backups of files with | |
2554 names matching REGEXP will be made in DIRECTORY. DIRECTORY may be | |
2555 relative or absolute. If it is absolute, so that all matching files | |
2556 are backed up into the same directory, the file names in this | |
2557 directory will be the full name of the file backed up with all | |
2558 directory separators changed to `!' to prevent clashes. This will not | |
2559 work correctly if your filesystem truncates the resulting name. | |
2560 | |
2561 For the common case of all backups going into one directory, the alist | |
2562 should contain a single element pairing \".\" with the appropriate | |
2563 directory name. | |
2564 | |
2565 If this variable is nil, or it fails to match a filename, the backup | |
2566 is made in the original file's directory. | |
2567 | |
2568 On MS-DOS filesystems without long names this variable is always | |
2569 ignored." | |
2570 :group 'backup | |
2571 :type '(repeat (cons (regexp :tag "Regexp matching filename") | |
2572 (directory :tag "Backup directory name")))) | |
2573 | |
428 | 2574 (defun make-backup-file-name (file) |
2575 "Create the non-numeric backup file name for FILE. | |
1333 | 2576 Normally this will just be the file's name with `~' appended. |
2577 Customization hooks are provided as follows. | |
2578 | |
2579 If the variable `make-backup-file-name-function' is non-nil, its value | |
2580 should be a function which will be called with FILE as its argument; | |
2581 the resulting name is used. | |
2582 | |
2583 Otherwise a match for FILE is sought in `backup-directory-alist'; see | |
2584 the documentation of that variable. If the directory for the backup | |
2585 doesn't exist, it is created." | |
2586 (if make-backup-file-name-function | |
2587 (funcall make-backup-file-name-function file) | |
2588 ; (if (and (eq system-type 'ms-dos) | |
2589 ; (not (msdos-long-file-names))) | |
2590 ; (let ((fn (file-name-nondirectory file))) | |
2591 ; (concat (file-name-directory file) | |
2592 ; (or (and (string-match "\\`[^.]+\\'" fn) | |
2593 ; (concat (match-string 0 fn) ".~")) | |
2594 ; (and (string-match "\\`[^.]+\\.\\(..?\\)?" fn) | |
2595 ; (concat (match-string 0 fn) "~"))))) | |
2596 (concat (make-backup-file-name-1 file) "~"))) | |
2597 | |
2598 (defun make-backup-file-name-1 (file) | |
2599 "Subroutine of `make-backup-file-name' and `find-backup-file-name'." | |
2600 (let ((alist backup-directory-alist) | |
2601 elt backup-directory dir-sep-string) | |
2602 (while alist | |
2603 (setq elt (pop alist)) | |
2604 (if (string-match (car elt) file) | |
2605 (setq backup-directory (cdr elt) | |
2606 alist nil))) | |
2607 (if (null backup-directory) | |
2608 file | |
2609 (unless (file-exists-p backup-directory) | |
2610 (condition-case nil | |
2611 (make-directory backup-directory 'parents) | |
2612 (file-error file))) | |
2613 (if (file-name-absolute-p backup-directory) | |
2614 (progn | |
2615 (when (memq system-type '(windows-nt ms-dos)) | |
2616 ;; Normalize DOSish file names: convert all slashes to | |
2617 ;; directory-sep-char, downcase the drive letter, if any, | |
2618 ;; and replace the leading "x:" with "/drive_x". | |
2619 (or (file-name-absolute-p file) | |
2620 (setq file (expand-file-name file))) ; make defaults explicit | |
2621 ;; Replace any invalid file-name characters (for the | |
2622 ;; case of backing up remote files). | |
2623 (setq file (expand-file-name (convert-standard-filename file))) | |
2624 (setq dir-sep-string (char-to-string directory-sep-char)) | |
2625 (if (eq (aref file 1) ?:) | |
2626 (setq file (concat dir-sep-string | |
2627 "drive_" | |
2628 (char-to-string (downcase (aref file 0))) | |
2629 (if (eq (aref file 2) directory-sep-char) | |
2630 "" | |
2631 dir-sep-string) | |
2632 (substring file 2))))) | |
2633 ;; Make the name unique by substituting directory | |
2634 ;; separators. It may not really be worth bothering about | |
2635 ;; doubling `!'s in the original name... | |
2636 (expand-file-name | |
2637 (subst-char-in-string | |
2638 directory-sep-char ?! | |
2639 (replace-regexp-in-string "!" "!!" file)) | |
2640 backup-directory)) | |
2641 (expand-file-name (file-name-nondirectory file) | |
2642 (file-name-as-directory | |
2643 (expand-file-name backup-directory | |
2644 (file-name-directory file)))))))) | |
428 | 2645 |
2646 (defun backup-file-name-p (file) | |
2647 "Return non-nil if FILE is a backup file name (numeric or not). | |
2648 This is a separate function so you can redefine it for customization. | |
2649 You may need to redefine `file-name-sans-versions' as well." | |
440 | 2650 (string-match "~\\'" file)) |
428 | 2651 |
1333 | 2652 (defvar backup-extract-version-start) |
2653 | |
428 | 2654 ;; This is used in various files. |
1333 | 2655 ;; The usage of backup-extract-version-start is not very clean, |
2656 ;; but I can't see a good alternative, so as of now I am leaving it alone. | |
428 | 2657 (defun backup-extract-version (fn) |
1333 | 2658 "Given the name of a numeric backup file, FN, return the backup number. |
2659 Uses the free variable `backup-extract-version-start', whose value should be | |
428 | 2660 the index in the name where the version number begins." |
1333 | 2661 (if (and (string-match "[0-9]+~$" fn backup-extract-version-start) |
2662 (= (match-beginning 0) backup-extract-version-start)) | |
2663 (string-to-int (substring fn backup-extract-version-start -1)) | |
428 | 2664 0)) |
2665 | |
1333 | 2666 ;; [[ FSF 21.2 says: |
2667 ;; I believe there is no need to alter this behavior for VMS; | |
2668 ;; since backup files are not made on VMS, it should not get called. ]] | |
428 | 2669 (defun find-backup-file-name (fn) |
1333 | 2670 "Find a file name for a backup file FN, and suggestions for deletions. |
428 | 2671 Value is a list whose car is the name for the backup file |
1333 | 2672 and whose cdr is a list of old versions to consider deleting now. |
2673 If the value is nil, don't make a backup. | |
2674 Uses `backup-directory-alist' in the same way as does | |
2675 `make-backup-file-name'." | |
428 | 2676 (let ((handler (find-file-name-handler fn 'find-backup-file-name))) |
2677 ;; Run a handler for this function so that ange-ftp can refuse to do it. | |
2678 (if handler | |
2679 (funcall handler 'find-backup-file-name fn) | |
1333 | 2680 (if (or (eq version-control 'never) |
2681 ;; We don't support numbered backups on plain MS-DOS | |
2682 ;; when long file names are unavailable. | |
2683 ; (and (eq system-type 'ms-dos) | |
2684 ; (not (msdos-long-file-names))) | |
2685 ) | |
428 | 2686 (list (make-backup-file-name fn)) |
1333 | 2687 (let* ((basic-name (make-backup-file-name-1 fn)) |
2688 (base-versions (concat (file-name-nondirectory basic-name) | |
2689 ".~")) | |
2690 (backup-extract-version-start (length base-versions)) | |
428 | 2691 (high-water-mark 0) |
1333 | 2692 (number-to-delete 0) |
2693 possibilities deserve-versions-p versions) | |
428 | 2694 (condition-case () |
2695 (setq possibilities (file-name-all-completions | |
2696 base-versions | |
1333 | 2697 (file-name-directory basic-name)) |
2698 versions (sort (mapcar #'backup-extract-version | |
2699 possibilities) | |
2700 #'<) | |
2701 high-water-mark (apply 'max 0 versions) | |
428 | 2702 deserve-versions-p (or version-control |
2703 (> high-water-mark 0)) | |
2704 number-to-delete (- (length versions) | |
1333 | 2705 kept-old-versions |
2706 kept-new-versions | |
2707 -1)) | |
2708 (file-error (setq possibilities nil))) | |
428 | 2709 (if (not deserve-versions-p) |
2710 (list (make-backup-file-name fn)) | |
1333 | 2711 (cons (format "%s.~%d~" basic-name (1+ high-water-mark)) |
428 | 2712 (if (and (> number-to-delete 0) |
2713 ;; Delete nothing if there is overflow | |
2714 ;; in the number of versions to keep. | |
2715 (>= (+ kept-new-versions kept-old-versions -1) 0)) | |
1333 | 2716 (mapcar (lambda (n) |
2717 (format "%s.~%d~" basic-name n)) | |
428 | 2718 (let ((v (nthcdr kept-old-versions versions))) |
2719 (rplacd (nthcdr (1- number-to-delete) v) ()) | |
2720 v)))))))))) | |
2721 | |
2722 (defun file-nlinks (filename) | |
2723 "Return number of names file FILENAME has." | |
2724 (car (cdr (file-attributes filename)))) | |
2725 | |
2726 (defun file-relative-name (filename &optional directory) | |
1333 | 2727 "Convert FILENAME to be relative to DIRECTORY (default: `default-directory'). |
428 | 2728 This function returns a relative file name which is equivalent to FILENAME |
2729 when used with that default directory as the default. | |
442 | 2730 If this is impossible (which can happen on MS Windows when the file name |
2731 and directory use different drive names) then it returns FILENAME." | |
428 | 2732 (save-match-data |
2733 (let ((fname (expand-file-name filename))) | |
2734 (setq directory (file-name-as-directory | |
2735 (expand-file-name (or directory default-directory)))) | |
2736 ;; On Microsoft OSes, if FILENAME and DIRECTORY have different | |
2737 ;; drive names, they can't be relative, so return the absolute name. | |
440 | 2738 (if (and (eq system-type 'windows-nt) |
428 | 2739 (not (string-equal (substring fname 0 2) |
2740 (substring directory 0 2)))) | |
2741 filename | |
2742 (let ((ancestor ".") | |
2743 (fname-dir (file-name-as-directory fname))) | |
2744 (while (and (not (string-match (concat "^" (regexp-quote directory)) | |
2745 fname-dir)) | |
2746 (not (string-match (concat "^" (regexp-quote directory)) fname))) | |
2747 (setq directory (file-name-directory (substring directory 0 -1)) | |
2748 ancestor (if (equal ancestor ".") | |
2749 ".." | |
2750 (concat "../" ancestor)))) | |
2751 ;; Now ancestor is empty, or .., or ../.., etc. | |
2752 (if (string-match (concat "^" (regexp-quote directory)) fname) | |
2753 ;; We matched within FNAME's directory part. | |
2754 ;; Add the rest of FNAME onto ANCESTOR. | |
2755 (let ((rest (substring fname (match-end 0)))) | |
2756 (if (and (equal ancestor ".") | |
2757 (not (equal rest ""))) | |
2758 ;; But don't bother with ANCESTOR if it would give us `./'. | |
2759 rest | |
2760 (concat (file-name-as-directory ancestor) rest))) | |
2761 ;; We matched FNAME's directory equivalent. | |
2762 ancestor)))))) | |
2763 | |
2764 (defun save-buffer (&optional args) | |
2765 "Save current buffer in visited file if modified. Versions described below. | |
2766 By default, makes the previous version into a backup file | |
2767 if previously requested or if this is the first save. | |
1333 | 2768 With 1 \\[universal-argument], marks this version |
428 | 2769 to become a backup when the next save is done. |
1333 | 2770 With 2 \\[universal-argument]'s, |
428 | 2771 unconditionally makes the previous version into a backup file. |
1333 | 2772 With 3 \\[universal-argument]'s, marks this version |
2773 to become a backup when the next save is done, | |
2774 and unconditionally makes the previous version into a backup file. | |
2775 | |
2776 With argument of 0, never make the previous version into a backup file. | |
428 | 2777 |
2778 If a file's name is FOO, the names of its numbered backup versions are | |
2779 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~. | |
2780 Numeric backups (rather than FOO~) will be made if value of | |
2781 `version-control' is not the atom `never' and either there are already | |
2782 numeric versions of the file being backed up, or `version-control' is | |
2783 non-nil. | |
2784 We don't want excessive versions piling up, so there are variables | |
1333 | 2785 `kept-old-versions', which tells Emacs how many oldest versions to keep, |
428 | 2786 and `kept-new-versions', which tells how many newest versions to keep. |
2787 Defaults are 2 old versions and 2 new. | |
2788 `dired-kept-versions' controls dired's clean-directory (.) command. | |
2789 If `delete-old-versions' is nil, system will query user | |
1333 | 2790 before trimming versions. Otherwise it does it silently. |
2791 | |
2792 If `vc-make-backup-files' is nil, which is the default, | |
2793 no backup files are made for files managed by version control. | |
2794 (This is because the version control system itself records previous versions.) | |
2795 | |
2796 See the subroutine `basic-save-buffer' for more information." | |
428 | 2797 (interactive "_p") |
2798 (let ((modp (buffer-modified-p)) | |
2799 (large (> (buffer-size) 50000)) | |
2800 (make-backup-files (or (and make-backup-files (not (eq args 0))) | |
2801 (memq args '(16 64))))) | |
2802 (and modp (memq args '(16 64)) (setq buffer-backed-up nil)) | |
1333 | 2803 (if (and modp large (buffer-file-name)) |
2804 (display-message 'progress (format "Saving file %s..." | |
2805 (buffer-file-name)))) | |
428 | 2806 (basic-save-buffer) |
2807 (and modp (memq args '(4 64)) (setq buffer-backed-up nil)))) | |
2808 | |
2809 (defun delete-auto-save-file-if-necessary (&optional force) | |
2810 "Delete auto-save file for current buffer if `delete-auto-save-files' is t. | |
1333 | 2811 Normally delete only if the file was written by this XEmacs since |
2812 the last real save, but optional arg FORCE non-nil means delete anyway." | |
428 | 2813 (and buffer-auto-save-file-name delete-auto-save-files |
2814 (not (string= buffer-file-name buffer-auto-save-file-name)) | |
2815 (or force (recent-auto-save-p)) | |
2816 (progn | |
2817 (ignore-file-errors (delete-file buffer-auto-save-file-name)) | |
2818 (set-buffer-auto-saved)))) | |
2819 | |
2820 ;; XEmacs change (from Sun) | |
2821 ;; used to communicate with continue-save-buffer: | |
2822 (defvar continue-save-buffer-hooks-tail nil) | |
2823 | |
2824 ;; Not in FSFmacs | |
2825 (defun basic-write-file-data (realname truename) | |
2826 ;; call the hooks until the bytes are put | |
2827 ;; call write-region as a last resort | |
2828 (let ((region-written nil) | |
2829 (hooks write-file-data-hooks)) | |
2830 (while (and hooks (not region-written)) | |
2831 (setq region-written (funcall (car hooks) realname) | |
2832 hooks (cdr hooks))) | |
2833 (if (not region-written) | |
2834 (write-region (point-min) (point-max) realname nil t truename)))) | |
2835 | |
1333 | 2836 ; (defvar auto-save-hook nil |
2837 ; "Normal hook run just before auto-saving.") | |
2838 | |
428 | 2839 (put 'after-save-hook 'permanent-local t) |
2840 (defvar after-save-hook nil | |
2841 "Normal hook that is run after a buffer is saved to its file. | |
2842 These hooks are considered to pertain to the visited file. | |
2843 So this list is cleared if you change the visited file name.") | |
2844 | |
1333 | 2845 (defvar save-buffer-coding-system nil |
2846 "If non-nil, use this coding system for saving the buffer. | |
2847 More precisely, use this coding system in place of the | |
2848 value of `buffer-file-coding-system', when saving the buffer. | |
2849 Calling `write-region' for any purpose other than saving the buffer | |
2850 will still use `buffer-file-coding-system'; this variable has no effect | |
2851 in such cases.") | |
2852 | |
2853 (make-variable-buffer-local 'save-buffer-coding-system) | |
2854 (put 'save-buffer-coding-system 'permanent-local t) | |
2855 | |
428 | 2856 (defun files-fetch-hook-value (hook) |
4156 | 2857 (let ((localval (copy-list (symbol-value hook))) |
2858 (globalval (copy-list (default-value hook)))) | |
428 | 2859 (if (memq t localval) |
2860 (setq localval (append (delq t localval) (delq t globalval)))) | |
2861 localval)) | |
2862 | |
2863 (defun basic-save-buffer () | |
2864 "Save the current buffer in its visited file, if it has been modified. | |
1333 | 2865 The hooks `write-contents-hooks', `local-write-file-hooks' and |
2866 `write-file-hooks' get a chance to do the job of saving; if they do not, | |
2116 | 2867 then the buffer is saved in the visited file in the usual way. |
1333 | 2868 After saving the buffer, this function runs `after-save-hook'." |
428 | 2869 (interactive) |
1333 | 2870 (save-current-buffer |
428 | 2871 ;; In an indirect buffer, save its base buffer instead. |
2872 (if (buffer-base-buffer) | |
2873 (set-buffer (buffer-base-buffer))) | |
2874 (if (buffer-modified-p) | |
2875 (let ((recent-save (recent-auto-save-p))) | |
2876 ;; If buffer has no file name, ask user for one. | |
2877 (or buffer-file-name | |
2878 (let ((filename | |
2879 (expand-file-name | |
2880 (read-file-name "File to save in: ") nil))) | |
2881 (and (file-exists-p filename) | |
2882 (or (y-or-n-p (format "File `%s' exists; overwrite? " | |
2883 filename)) | |
2884 (error "Canceled"))) | |
2885 (set-visited-file-name filename))) | |
2886 (or (verify-visited-file-modtime (current-buffer)) | |
2887 (not (file-exists-p buffer-file-name)) | |
2888 (yes-or-no-p | |
2889 (format "%s has changed since visited or saved. Save anyway? " | |
2890 (file-name-nondirectory buffer-file-name))) | |
2891 (error "Save not confirmed")) | |
2892 (save-restriction | |
2893 (widen) | |
1333 | 2894 (save-excursion |
2895 (and (> (point-max) 1) | |
2896 (not find-file-literally) | |
2897 (not (eq (char-after (1- (point-max))) ?\n)) | |
2898 (not (and (eq selective-display t) | |
2899 (eq (char-after (1- (point-max))) ?\r))) | |
2900 (or (eq require-final-newline t) | |
2901 (and require-final-newline | |
2902 (y-or-n-p | |
2903 (format "Buffer %s does not end in newline. Add one? " | |
2904 (buffer-name))))) | |
2905 (save-excursion | |
2906 (goto-char (point-max)) | |
2907 (insert ?\n)))) | |
2908 | |
2909 ;; Support VC version backups. | |
2910 (if-fboundp 'vc-before-save | |
2911 (vc-before-save)) | |
434 | 2912 |
444 | 2913 ;; Run the write-file-hooks until one returns non-nil. |
428 | 2914 ;; Bind after-save-hook to nil while running the |
2915 ;; write-file-hooks so that if this function is called | |
2916 ;; recursively (from inside a write-file-hook) the | |
2917 ;; after-hooks will only get run once (from the | |
2918 ;; outermost call). | |
2919 ;; | |
2920 ;; Ugh, have to duplicate logic of run-hook-with-args-until-success | |
2921 (let ((hooks (append (files-fetch-hook-value 'write-contents-hooks) | |
2922 (files-fetch-hook-value | |
2923 'local-write-file-hooks) | |
2924 (files-fetch-hook-value 'write-file-hooks))) | |
2925 (after-save-hook nil) | |
2926 (local-write-file-hooks nil) | |
2927 (write-contents-hooks nil) | |
2928 (write-file-hooks nil) | |
2929 done) | |
2930 (while (and hooks | |
2931 (let ((continue-save-buffer-hooks-tail hooks)) | |
2932 (not (setq done (funcall (car hooks)))))) | |
2933 (setq hooks (cdr hooks))) | |
2934 ;; If a hook returned t, file is already "written". | |
2935 ;; Otherwise, write it the usual way now. | |
2936 (if (not done) | |
2937 (basic-save-buffer-1))) | |
2938 ;; XEmacs: next two clauses (buffer-file-number setting and | |
1333 | 2939 ;; set-file-modes) moved into basic-save-buffer-1 for use by |
2940 ;; continue-save-buffer. | |
428 | 2941 ) |
2942 ;; If the auto-save file was recent before this command, | |
2943 ;; delete it now. | |
2944 (delete-auto-save-file-if-necessary recent-save) | |
2945 ;; Support VC `implicit' locking. | |
502 | 2946 (if-fboundp 'vc-after-save |
2947 (vc-after-save)) | |
428 | 2948 (run-hooks 'after-save-hook)) |
2949 (display-message 'no-log "(No changes need to be saved)")))) | |
2950 | |
2951 ;; This does the "real job" of writing a buffer into its visited file | |
2952 ;; and making a backup file. This is what is normally done | |
2953 ;; but inhibited if one of write-file-hooks returns non-nil. | |
2954 ;; It returns a value to store in setmodes. | |
2955 (defun basic-save-buffer-1 () | |
1333 | 2956 (if save-buffer-coding-system |
2957 (let ((coding-system-for-write save-buffer-coding-system)) | |
2958 (basic-save-buffer-2)) | |
2959 (basic-save-buffer-2))) | |
2960 | |
2961 (defun basic-save-buffer-2 () | |
428 | 2962 (let (setmodes tempsetmodes) |
2963 (if (not (file-writable-p buffer-file-name)) | |
2964 (let ((dir (file-name-directory buffer-file-name))) | |
2965 (if (not (file-directory-p dir)) | |
1333 | 2966 (if (file-exists-p dir) |
2967 (error "%s is not a directory" dir) | |
2968 (error "%s: no such directory" buffer-file-name)) | |
428 | 2969 (if (not (file-exists-p buffer-file-name)) |
2970 (error "Directory %s write-protected" dir) | |
2971 (if (yes-or-no-p | |
2972 (format "File %s is write-protected; try to save anyway? " | |
2973 (file-name-nondirectory | |
2974 buffer-file-name))) | |
2975 (setq tempsetmodes t) | |
2976 (error | |
2977 "Attempt to save to a file which you aren't allowed to write")))))) | |
2978 (or buffer-backed-up | |
2979 (setq setmodes (backup-buffer))) | |
2980 (let ((dir (file-name-directory buffer-file-name))) | |
2981 (if (and file-precious-flag | |
2982 (file-writable-p dir)) | |
2983 ;; If file is precious, write temp name, then rename it. | |
2984 ;; This requires write access to the containing dir, | |
2985 ;; which is why we don't try it if we don't have that access. | |
2986 (let ((realname buffer-file-name) | |
2987 tempname nogood i succeed | |
2988 (old-modtime (visited-file-modtime))) | |
2989 (setq i 0) | |
2990 (setq nogood t) | |
2991 ;; Find the temporary name to write under. | |
2992 (while nogood | |
2993 (setq tempname (format "%s#tmp#%d" dir i)) | |
2994 (setq nogood (file-exists-p tempname)) | |
2995 (setq i (1+ i))) | |
2996 (unwind-protect | |
2997 (progn (clear-visited-file-modtime) | |
2998 (write-region (point-min) (point-max) | |
2999 tempname nil realname | |
3000 buffer-file-truename) | |
3001 (setq succeed t)) | |
3002 ;; If writing the temp file fails, | |
3003 ;; delete the temp file. | |
3004 (or succeed | |
3005 (progn | |
1333 | 3006 (ignore-file-errors |
3007 (delete-file tempname)) | |
428 | 3008 (set-visited-file-modtime old-modtime)))) |
3009 ;; Since we have created an entirely new file | |
3010 ;; and renamed it, make sure it gets the | |
3011 ;; right permission bits set. | |
3012 (setq setmodes (file-modes buffer-file-name)) | |
3013 ;; We succeeded in writing the temp file, | |
3014 ;; so rename it. | |
3015 (rename-file tempname buffer-file-name t)) | |
3016 ;; If file not writable, see if we can make it writable | |
3017 ;; temporarily while we write it. | |
3018 ;; But no need to do so if we have just backed it up | |
3019 ;; (setmodes is set) because that says we're superseding. | |
3020 (cond ((and tempsetmodes (not setmodes)) | |
3021 ;; Change the mode back, after writing. | |
3022 (setq setmodes (file-modes buffer-file-name)) | |
1333 | 3023 (set-file-modes buffer-file-name (logior setmodes 128)))) |
428 | 3024 (basic-write-file-data buffer-file-name buffer-file-truename))) |
1333 | 3025 ;; #### FSF 21.2. We don't have last-coding-system-used. |
3026 ; ;; Now we have saved the current buffer. Let's make sure | |
3027 ; ;; that buffer-file-coding-system is fixed to what | |
3028 ; ;; actually used for saving by binding it locally. | |
3029 ; (if save-buffer-coding-system | |
3030 ; (setq save-buffer-coding-system last-coding-system-used) | |
3031 ; (setq buffer-file-coding-system last-coding-system-used)) | |
428 | 3032 (setq buffer-file-number |
3033 (if buffer-file-name | |
3034 (nth 10 (file-attributes buffer-file-name)) | |
3035 nil)) | |
3036 (if setmodes | |
3037 (condition-case () | |
3038 (set-file-modes buffer-file-name setmodes) | |
3039 (error nil))))) | |
3040 | |
3041 ;; XEmacs change, from Sun | |
3042 (defun continue-save-buffer () | |
3043 "Provide a clean way for a write-file-hook to wrap AROUND | |
3044 the execution of the remaining hooks and writing to disk. | |
3045 Do not call this function except from a functions | |
444 | 3046 on the `write-file-hooks' or `write-contents-hooks' list. |
428 | 3047 A hook that calls this function must return non-nil, |
444 | 3048 to signal completion to its caller. `continue-save-buffer' |
428 | 3049 always returns non-nil." |
3050 (let ((hooks (cdr (or continue-save-buffer-hooks-tail | |
3051 (error | |
3052 "continue-save-buffer called outside a write-file-hook!")))) | |
3053 (done nil)) | |
3054 ;; Do something like this: | |
3055 ;; (let ((write-file-hooks hooks)) (basic-save-buffer)) | |
3056 ;; First run the rest of the hooks. | |
3057 (while (and hooks | |
3058 (let ((continue-save-buffer-hooks-tail hooks)) | |
3059 (not (setq done (funcall (car hooks)))))) | |
3060 (setq hooks (cdr hooks))) | |
3061 ;; | |
3062 ;; If a hook returned t, file is already "written". | |
3063 (if (not done) | |
3064 (basic-save-buffer-1)) | |
3065 'continue-save-buffer)) | |
3066 | |
3067 (defcustom save-some-buffers-query-display-buffer t | |
3068 "*Non-nil makes `\\[save-some-buffers]' switch to the buffer offered for saving." | |
3069 :type 'boolean | |
3070 :group 'editing-basics) | |
3071 | |
1333 | 3072 (defun save-some-buffers (&optional arg pred) |
428 | 3073 "Save some modified file-visiting buffers. Asks user about each one. |
3074 Optional argument (the prefix) non-nil means save all with no questions. | |
1333 | 3075 Optional second argument PRED determines which buffers are considered: |
3076 If PRED is nil, all the file-visiting buffers are considered. | |
3077 If PRED is t, then certain non-file buffers will also be considered. | |
3078 If PRED is a zero-argument function, it indicates for each buffer whether | |
3079 to consider it or not when called with that buffer current." | |
428 | 3080 (interactive "P") |
3081 (save-excursion | |
3082 ;; `delete-other-windows' can bomb during autoloads generation, so | |
3083 ;; guard it well. | |
3084 (if (or noninteractive | |
3085 (eq (selected-window) (minibuffer-window)) | |
3086 (not save-some-buffers-query-display-buffer)) | |
3087 ;; If playing with windows is unsafe or undesired, just do the | |
3088 ;; usual drill. | |
1333 | 3089 (save-some-buffers-1 arg pred nil) |
428 | 3090 ;; Else, protect the windows. |
3091 (when (save-window-excursion | |
1333 | 3092 (save-some-buffers-1 arg pred t)) |
428 | 3093 ;; Force redisplay. |
3094 (sit-for 0))))) | |
3095 | |
3096 ;; XEmacs - do not use queried flag | |
1333 | 3097 (defun save-some-buffers-1 (arg pred switch-buffer) |
428 | 3098 (let* ((switched nil) |
612 | 3099 (last-buffer nil) |
428 | 3100 (files-done |
3101 (map-y-or-n-p | |
3102 (lambda (buffer) | |
612 | 3103 (prog1 |
3104 (and (buffer-modified-p buffer) | |
3105 (not (buffer-base-buffer buffer)) | |
3106 ;; XEmacs addition: | |
3107 (not (symbol-value-in-buffer 'save-buffers-skip buffer)) | |
3108 (or | |
3109 (buffer-file-name buffer) | |
1333 | 3110 (and pred |
612 | 3111 (progn |
3112 (set-buffer buffer) | |
3113 (and buffer-offer-save (> (buffer-size) 0))))) | |
1333 | 3114 (or (not (functionp pred)) |
3115 (with-current-buffer buffer (funcall pred))) | |
612 | 3116 (if arg |
3117 t | |
3118 ;; #### We should provide a per-buffer means to | |
3119 ;; disable the switching. For instance, you might | |
3120 ;; want to turn it off for buffers the contents of | |
3121 ;; which is meaningless to humans, such as | |
3122 ;; `.newsrc.eld'. | |
3123 (when (and switch-buffer | |
3124 ;; map-y-or-n-p is displaying help | |
3125 (not (eq last-buffer buffer))) | |
3126 (unless (one-window-p) | |
3127 (delete-other-windows)) | |
3128 (setq switched t) | |
3129 ;; #### Consider using `display-buffer' here for 21.1! | |
3130 ;;(display-buffer buffer nil (selected-frame))) | |
3131 (switch-to-buffer buffer t)) | |
3132 (if (buffer-file-name buffer) | |
3133 (format "Save file %s? " | |
3134 (buffer-file-name buffer)) | |
3135 (format "Save buffer %s? " | |
3136 (buffer-name buffer))))) | |
3137 (setq last-buffer buffer))) | |
428 | 3138 (lambda (buffer) |
3139 (set-buffer buffer) | |
3140 (condition-case () | |
3141 (save-buffer) | |
3142 (error nil))) | |
3143 (buffer-list) | |
3144 '("buffer" "buffers" "save") | |
3145 ;;instead of this we just say "yes all", "no all", etc. | |
3146 ;;"save all the rest" | |
3147 ;;"save only this buffer" "save no more buffers") | |
3148 ;; this is rather bogus. --ben | |
3149 ;; (it makes the dialog box too big, and you get an error | |
3150 ;; "wrong type argument: framep, nil" when you hit q after | |
3151 ;; choosing the option from the dialog box) | |
3152 | |
3153 ;; We should fix the dialog box rather than disabling | |
3154 ;; this! --hniksic | |
3155 (list (list ?\C-r (lambda (buf) | |
3156 ;; #### FSF has an EXIT-ACTION argument | |
3157 ;; to `view-buffer'. | |
1333 | 3158 (view-buffer buf |
3159 ; (function | |
3160 ; (lambda (ignore) | |
3161 ; (exit-recursive-edit)))) | |
3162 ) | |
502 | 3163 (with-boundp 'view-exit-action |
3164 (setq view-exit-action | |
3165 (lambda (ignore) | |
3166 (exit-recursive-edit)))) | |
428 | 3167 (recursive-edit) |
3168 ;; Return nil to ask about BUF again. | |
3169 nil) | |
442 | 3170 "%_Display Buffer")))) |
428 | 3171 (abbrevs-done |
3172 (and save-abbrevs abbrevs-changed | |
3173 (progn | |
3174 (if (or arg | |
1333 | 3175 (eq save-abbrevs 'silently) |
428 | 3176 (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name))) |
3177 (write-abbrev-file nil)) | |
3178 ;; Don't keep bothering user if he says no. | |
3179 (setq abbrevs-changed nil) | |
3180 t)))) | |
3181 (or (> files-done 0) abbrevs-done | |
3182 (display-message 'no-log "(No files need saving)")) | |
3183 switched)) | |
3184 | |
3185 | |
1333 | 3186 |
428 | 3187 (defun not-modified (&optional arg) |
3188 "Mark current buffer as unmodified, not needing to be saved. | |
3189 With prefix arg, mark buffer as modified, so \\[save-buffer] will save. | |
3190 | |
3191 It is not a good idea to use this function in Lisp programs, because it | |
3192 prints a message in the minibuffer. Instead, use `set-buffer-modified-p'." | |
3193 (interactive "_P") | |
3194 (if arg ;; rewritten for I18N3 snarfing | |
3195 (display-message 'command "Modification-flag set") | |
3196 (display-message 'command "Modification-flag cleared")) | |
3197 (set-buffer-modified-p arg)) | |
3198 | |
3199 (defun toggle-read-only (&optional arg) | |
1333 | 3200 "Change whether this buffer is visiting its file read-only. |
3201 With arg, set read-only iff arg is positive. | |
3202 If visiting file read-only and `view-read-only' is non-nil, enter view mode." | |
3203 (interactive "P") | |
3204 (cond | |
3205 ((and arg (if (> (prefix-numeric-value arg) 0) buffer-read-only | |
3206 (not buffer-read-only))) ; If buffer-read-only is set correctly, | |
3207 nil) ; do nothing. | |
3208 ;; Toggle. | |
3209 ((and buffer-read-only view-minor-mode) | |
3210 ;(View-exit-and-edit) | |
3211 (view-mode) | |
3212 (make-local-variable 'view-read-only) | |
3213 (setq view-read-only t)) ; Must leave view mode. | |
3214 ((and (not buffer-read-only) view-read-only | |
3215 (not (eq (get major-mode 'mode-class) 'special))) | |
3216 ;(view-mode-enter) | |
3217 (view-mode)) | |
3218 (t (setq buffer-read-only (not buffer-read-only)) | |
3219 (force-mode-line-update)))) | |
428 | 3220 |
3221 (defun insert-file (filename &optional codesys) | |
3222 "Insert contents of file FILENAME into buffer after point. | |
3223 Set mark after the inserted text. | |
3224 | |
771 | 3225 Optional second argument specifies the coding system to use when decoding |
3226 the file. Interactively, with a prefix argument, you will be prompted for | |
3227 the coding system. | |
428 | 3228 |
771 | 3229 This function is meant for the user to run interactively. Don't call it |
3230 from programs! Use `insert-file-contents' instead. \(Its calling sequence | |
3231 is different; see its documentation)." | |
428 | 3232 (interactive "*fInsert file: \nZCoding system: ") |
3233 (if (file-directory-p filename) | |
3234 (signal 'file-error (list "Opening input file" "file is a directory" | |
3235 filename))) | |
3236 (let ((tem | |
3237 (if codesys | |
3238 (let ((coding-system-for-read | |
3239 (get-coding-system codesys))) | |
3240 (insert-file-contents filename)) | |
3241 (insert-file-contents filename)))) | |
3242 (push-mark (+ (point) (car (cdr tem)))))) | |
3243 | |
3244 (defun append-to-file (start end filename &optional codesys) | |
3245 "Append the contents of the region to the end of file FILENAME. | |
771 | 3246 When called from a function, expects three arguments, START, END and |
3247 FILENAME. START and END are buffer positions saying what text to write. | |
3248 Optional fourth argument specifies the coding system to use when encoding | |
3249 the file. Interactively, with a prefix argument, you will be prompted for | |
3250 the coding system." | |
428 | 3251 (interactive "r\nFAppend to file: \nZCoding system: ") |
3252 (if codesys | |
3253 (let ((buffer-file-coding-system (get-coding-system codesys))) | |
3254 (write-region start end filename t)) | |
3255 (write-region start end filename t))) | |
3256 | |
3257 (defun file-newest-backup (filename) | |
3258 "Return most recent backup file for FILENAME or nil if no backups exist." | |
1333 | 3259 ;; `make-backup-file-name' will get us the right directory for |
3260 ;; ordinary or numeric backups. It might create a directory for | |
3261 ;; backups as a side-effect, according to `backup-directory-alist'. | |
3262 (let* ((filename (file-name-sans-versions | |
3263 (make-backup-file-name filename))) | |
428 | 3264 (file (file-name-nondirectory filename)) |
3265 (dir (file-name-directory filename)) | |
3266 (comp (file-name-all-completions file dir)) | |
1333 | 3267 (newest nil) |
3268 tem) | |
428 | 3269 (while comp |
1333 | 3270 (setq tem (pop comp)) |
3271 (cond ((and (backup-file-name-p tem) | |
3272 (string= (file-name-sans-versions tem) file)) | |
3273 (setq tem (concat dir tem)) | |
3274 (if (or (null newest) | |
3275 (file-newer-than-file-p tem newest)) | |
3276 (setq newest tem))))) | |
428 | 3277 newest)) |
3278 | |
3279 (defun rename-uniquely () | |
3280 "Rename current buffer to a similar name not already taken. | |
3281 This function is useful for creating multiple shell process buffers | |
3282 or multiple mail buffers, etc." | |
3283 (interactive) | |
3284 (save-match-data | |
1333 | 3285 (let ((base-name (buffer-name))) |
3286 (and (string-match "<[0-9]+>\\'" base-name) | |
3287 (not (and buffer-file-name | |
3288 (string= base-name | |
3289 (file-name-nondirectory buffer-file-name)))) | |
3290 ;; If the existing buffer name has a <NNN>, | |
3291 ;; which isn't part of the file name (if any), | |
3292 ;; then get rid of that. | |
3293 (setq base-name (substring base-name 0 (match-beginning 0)))) | |
3294 (rename-buffer (generate-new-buffer-name base-name)) | |
3295 (force-mode-line-update)))) | |
428 | 3296 |
3297 (defun make-directory-path (path) | |
3298 "Create all the directories along path that don't exist yet." | |
3299 (interactive "Fdirectory path to create: ") | |
3300 (make-directory path t)) | |
3301 | |
3302 (defun make-directory (dir &optional parents) | |
3303 "Create the directory DIR and any nonexistent parent dirs. | |
3304 Interactively, the default choice of directory to create | |
3305 is the current default directory for file names. | |
3306 That is useful when you have visited a file in a nonexistent directory. | |
3307 | |
3308 Noninteractively, the second (optional) argument PARENTS says whether | |
3309 to create parent directories if they don't exist." | |
3310 (interactive (list (let ((current-prefix-arg current-prefix-arg)) | |
3311 (read-directory-name "Create directory: ")) | |
3312 current-prefix-arg)) | |
3313 (let ((handler (find-file-name-handler dir 'make-directory))) | |
3314 (if handler | |
3315 (funcall handler 'make-directory dir parents) | |
3316 (if (not parents) | |
3317 (make-directory-internal dir) | |
3318 (let ((dir (directory-file-name (expand-file-name dir))) | |
3319 create-list) | |
3320 (while (not (file-exists-p dir)) | |
3321 (setq create-list (cons dir create-list) | |
3322 dir (directory-file-name (file-name-directory dir)))) | |
3323 (while create-list | |
3324 (make-directory-internal (car create-list)) | |
3325 (setq create-list (cdr create-list)))))))) | |
3326 | |
3327 (put 'revert-buffer-function 'permanent-local t) | |
3328 (defvar revert-buffer-function nil | |
3329 "Function to use to revert this buffer, or nil to do the default. | |
3330 The function receives two arguments IGNORE-AUTO and NOCONFIRM, | |
3331 which are the arguments that `revert-buffer' received.") | |
3332 | |
3333 (put 'revert-buffer-insert-file-contents-function 'permanent-local t) | |
3334 (defvar revert-buffer-insert-file-contents-function nil | |
3335 "Function to use to insert contents when reverting this buffer. | |
3336 Gets two args, first the nominal file name to use, | |
849 | 3337 and second, t if reading the auto-save file. |
3338 If the current buffer contents are to be discarded, the function must do | |
1333 | 3339 so itself. |
3340 | |
3341 The function you specify is responsible for updating (or preserving) point.") | |
428 | 3342 |
3343 (defvar before-revert-hook nil | |
3344 "Normal hook for `revert-buffer' to run before reverting. | |
3345 If `revert-buffer-function' is used to override the normal revert | |
3346 mechanism, this hook is not used.") | |
3347 | |
3348 (defvar after-revert-hook nil | |
3349 "Normal hook for `revert-buffer' to run after reverting. | |
3350 Note that the hook value that it runs is the value that was in effect | |
3351 before reverting; that makes a difference if you have buffer-local | |
3352 hook functions. | |
3353 | |
3354 If `revert-buffer-function' is used to override the normal revert | |
3355 mechanism, this hook is not used.") | |
3356 | |
3357 (defvar revert-buffer-internal-hook nil | |
3358 "Don't use this.") | |
3359 | |
1333 | 3360 ;; END SYNC WITH FSF 21.2. |
3361 | |
428 | 3362 (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes) |
3363 "Replace the buffer text with the text of the visited file on disk. | |
3364 This undoes all changes since the file was visited or saved. | |
3365 With a prefix argument, offer to revert from latest auto-save file, if | |
3366 that is more recent than the visited file. | |
3367 | |
849 | 3368 This command also refreshes certain special buffers that contain text |
3369 which doesn't come from a file, but reflects some other data base | |
3370 instead: for example, Dired buffers and buffer-list buffers. This is | |
3371 implemented by having the modes set `revert-buffer-function'. | |
428 | 3372 |
3373 When called from Lisp, the first argument is IGNORE-AUTO; only offer | |
3374 to revert from the auto-save file when this is nil. Note that the | |
3375 sense of this argument is the reverse of the prefix argument, for the | |
3376 sake of backward compatibility. IGNORE-AUTO is optional, defaulting | |
3377 to nil. | |
3378 | |
3379 Optional second argument NOCONFIRM means don't ask for confirmation at | |
3380 all. | |
3381 | |
3382 Optional third argument PRESERVE-MODES non-nil means don't alter | |
849 | 3383 the buffer's modes. Otherwise, reinitialize them using `normal-mode'. |
428 | 3384 |
3385 If the value of `revert-buffer-function' is non-nil, it is called to | |
3386 do all the work for this command. Otherwise, the hooks | |
3387 `before-revert-hook' and `after-revert-hook' are run at the beginning | |
3388 and the end, and if `revert-buffer-insert-file-contents-function' is | |
819 | 3389 non-nil, it is called instead of rereading visited file contents. |
3390 | |
849 | 3391 If the buffer-modified flag is nil, and we are not reverting from an |
3392 auto-save file, then compare the contents of the buffer and the file. | |
3393 Revert only if they differ." | |
428 | 3394 |
3395 ;; I admit it's odd to reverse the sense of the prefix argument, but | |
3396 ;; there is a lot of code out there which assumes that the first | |
3397 ;; argument should be t to avoid consulting the auto-save file, and | |
3398 ;; there's no straightforward way to encourage authors to notice a | |
3399 ;; reversal of the argument sense. So I'm just changing the user | |
3400 ;; interface, but leaving the programmatic interface the same. | |
3401 (interactive (list (not current-prefix-arg))) | |
3402 (if revert-buffer-function | |
3403 (funcall revert-buffer-function ignore-auto noconfirm) | |
3404 (let* ((opoint (point)) | |
819 | 3405 (newbuf nil) |
988 | 3406 (found nil) |
819 | 3407 (delay-prompt nil) |
428 | 3408 (auto-save-p (and (not ignore-auto) |
3409 (recent-auto-save-p) | |
3410 buffer-auto-save-file-name | |
3411 (file-readable-p buffer-auto-save-file-name) | |
3412 (y-or-n-p | |
3413 "Buffer has been auto-saved recently. Revert from auto-save file? "))) | |
3414 (file-name (if auto-save-p | |
3415 buffer-auto-save-file-name | |
3416 buffer-file-name))) | |
3417 (cond ((null file-name) | |
3418 (error "Buffer does not seem to be associated with any file")) | |
3419 ((or noconfirm | |
3420 (and (not (buffer-modified-p)) | |
988 | 3421 (dolist (rx revert-without-query found) |
3422 (when (string-match rx file-name) | |
3423 (setq found t)))) | |
819 | 3424 ;; If we might perform an optimized revert then we |
3425 ;; want to delay prompting in case we don't need to | |
3426 ;; do it at all | |
3427 (and (not auto-save-p) | |
3428 (not (buffer-modified-p)) | |
3429 (setq delay-prompt t)) | |
428 | 3430 (yes-or-no-p (format "Revert buffer from file %s? " |
3431 file-name))) | |
3432 (run-hooks 'before-revert-hook) | |
819 | 3433 ;; Only perform our optimized revert if nothing obvious |
3434 ;; has changed. | |
3435 (cond ((or auto-save-p | |
3436 (buffer-modified-p) | |
3437 (and (setq newbuf (revert-buffer-internal | |
3438 file-name)) | |
988 | 3439 (or noconfirm found |
838 | 3440 (and delay-prompt |
3441 (yes-or-no-p | |
3442 (format "Revert buffer from file %s? " | |
3443 file-name)))))) | |
819 | 3444 ;; If file was backed up but has changed since, |
3445 ;; we should make another backup. | |
3446 (and (not auto-save-p) | |
3447 (not (verify-visited-file-modtime (current-buffer))) | |
3448 (setq buffer-backed-up nil)) | |
3449 ;; Get rid of all undo records for this buffer. | |
3450 (or (eq buffer-undo-list t) | |
3451 (setq buffer-undo-list nil)) | |
3452 ;; Effectively copy the after-revert-hook status, | |
3453 ;; since after-find-file will clobber it. | |
3454 (let ((global-hook (default-value 'after-revert-hook)) | |
3455 (local-hook-p (local-variable-p 'after-revert-hook | |
3456 (current-buffer))) | |
3457 (local-hook (and (local-variable-p 'after-revert-hook | |
3458 (current-buffer)) | |
3459 after-revert-hook))) | |
3460 (let (buffer-read-only | |
3461 ;; Don't make undo records for the reversion. | |
3462 (buffer-undo-list t)) | |
3463 (if revert-buffer-insert-file-contents-function | |
3464 (funcall revert-buffer-insert-file-contents-function | |
3465 file-name auto-save-p) | |
3466 (if (not (file-exists-p file-name)) | |
3467 (error "File %s no longer exists!" file-name)) | |
3468 ;; Bind buffer-file-name to nil | |
3469 ;; so that we don't try to lock the file. | |
3470 (let ((buffer-file-name nil)) | |
3471 (or auto-save-p | |
3472 (unlock-buffer))) | |
3473 (widen) | |
3474 ;; When reading in an autosave, it's encoded using | |
3475 ;; `escape-quoted', so we need to use it. (It is always | |
3476 ;; safe to specify `escape-quoted': | |
3477 ;; | |
3478 ;; 1. If file-coding but no Mule, `escape-quoted' is | |
3479 ;; aliased to `binary'. | |
3480 ;; 2. If no file-coding, all coding systems devolve into | |
3481 ;; `binary'. | |
3482 ;; 3. ASCII and ISO8859-1 are encoded the same in both | |
3483 ;; `binary' and `escape-quoted', so they will be | |
3484 ;; compatible for the most part.) | |
3485 ;; | |
3486 ;; Otherwise, use coding-system-for-read if explicitly | |
3487 ;; given (e.g. the "Revert Buffer with Specified | |
3488 ;; Encoding" menu entries), or use the coding system | |
3489 ;; that the file was loaded as. | |
3490 (let* ((coding-system-for-read | |
3491 (if auto-save-p 'escape-quoted | |
3492 (or coding-system-for-read | |
3493 buffer-file-coding-system-when-loaded))) | |
3494 ;; If the bfcs wasn't changed from its original | |
3495 ;; value (other than possible EOL change), then we | |
3496 ;; should update it for the new coding system. | |
3497 (should-update-bfcs | |
3498 (eq (coding-system-base | |
3499 buffer-file-coding-system-when-loaded) | |
3500 (coding-system-base | |
3501 buffer-file-coding-system))) | |
3502 (old-bfcs buffer-file-coding-system) | |
3503 ;; But if the EOL was changed, match it in the new | |
3504 ;; value of bfcs. | |
3505 (adjust-eol | |
3506 (and should-update-bfcs | |
3507 (not | |
3508 (eq (get-coding-system | |
3509 buffer-file-coding-system-when-loaded) | |
3510 (get-coding-system | |
3511 buffer-file-coding-system)))))) | |
3512 (insert-file-contents file-name (not auto-save-p) | |
3513 nil nil t) | |
3514 (when should-update-bfcs | |
3515 (setq buffer-file-coding-system old-bfcs) | |
3516 (set-buffer-file-coding-system | |
3517 (if adjust-eol | |
3518 (coding-system-base | |
3519 buffer-file-coding-system-when-loaded) | |
771 | 3520 buffer-file-coding-system-when-loaded) |
4024 | 3521 (not adjust-eol) t))))) |
819 | 3522 (goto-char (min opoint (point-max))) |
3523 ;; Recompute the truename in case changes in symlinks | |
3524 ;; have changed the truename. | |
3525 ;;XEmacs: already done by insert-file-contents | |
3526 ;;(setq buffer-file-truename | |
3527 ;;(abbreviate-file-name (file-truename buffer-file-name))) | |
3528 (after-find-file nil nil t t preserve-modes) | |
3529 ;; Run after-revert-hook as it was before we reverted. | |
3530 (setq-default revert-buffer-internal-hook global-hook) | |
3531 (if local-hook-p | |
3532 (progn | |
3533 (make-local-variable 'revert-buffer-internal-hook) | |
3534 (setq revert-buffer-internal-hook local-hook)) | |
3535 (kill-local-variable 'revert-buffer-internal-hook)) | |
3536 (run-hooks 'revert-buffer-internal-hook))) | |
3537 ((null newbuf) | |
3538 ;; The resultant buffer is identical, alter | |
3539 ;; modtime, update mods and exit | |
3540 (set-visited-file-modtime) | |
2030 | 3541 (after-find-file nil nil t t t) |
3542 ;; We preserved modes above so fixup the local | |
3543 ;; variables manually | |
3544 (condition-case err | |
3545 (hack-local-variables) | |
3546 (error (lwarn 'local-variables 'warning | |
3547 "File local-variables error: %s" | |
3548 (error-message-string err))))) | |
819 | 3549 (t t)) |
428 | 3550 t))))) |
3551 | |
2030 | 3552 ;; #### wouldn't something like `revert-buffer-compare-with-file' be a |
3553 ;; better name? | |
3554 ;; #### why is the argument optional? | |
819 | 3555 (defun revert-buffer-internal (&optional file-name) |
849 | 3556 "Read contents of FILE-NAME into a buffer, and compare to current buffer. |
3557 Return nil if identical, and the new buffer if different." | |
3558 | |
819 | 3559 (let* ((newbuf (get-buffer-create " *revert*")) |
3714 | 3560 bmin bmax |
3561 ;; #### b-f-c-s is _not necessarily_ the coding system that | |
3562 ;; was used to read in the file. See its docstring. | |
3563 (coding-system buffer-file-coding-system)) | |
819 | 3564 (save-excursion |
3565 (set-buffer newbuf) | |
826 | 3566 (with-obsolete-variable '(before-change-function after-change-function) |
3567 (let (buffer-read-only | |
3568 (buffer-undo-list t) | |
3569 after-change-function | |
3570 after-change-functions | |
3571 before-change-function | |
3638 | 3572 before-change-functions |
3714 | 3573 (coding-system-for-read coding-system) |
3574 ) | |
826 | 3575 (if revert-buffer-insert-file-contents-function |
3576 (funcall revert-buffer-insert-file-contents-function | |
3577 file-name nil) | |
3578 (if (not (file-exists-p file-name)) | |
3579 (error "File %s no longer exists!" file-name)) | |
3580 (widen) | |
863 | 3581 (insert-file-contents file-name nil nil nil t) |
826 | 3582 (setq bmin (point-min) |
3583 bmax (point-max)))))) | |
819 | 3584 (if (not (and (eq bmin (point-min)) |
3585 (eq bmax (point-max)) | |
3586 (eq (compare-buffer-substrings | |
3587 newbuf bmin bmax (current-buffer) bmin bmax) 0))) | |
3588 newbuf | |
863 | 3589 (and (kill-buffer newbuf) nil)))) |
819 | 3590 |
1333 | 3591 ;; BEGIN SYNC WITH FSF 21.2. |
3592 | |
844 | 3593 (defvar recover-file-diff-program "diff" |
3594 "Absolute or relative name of the `diff' program used by `recover-file'.") | |
3595 (defvar recover-file-diff-arguments '("-c") | |
3596 "List of arguments (switches) to pass to `diff' by `recover-file'.") | |
3597 | |
428 | 3598 (defun recover-file (file) |
3599 "Visit file FILE, but get contents from its last auto-save file." | |
3600 ;; Actually putting the file name in the minibuffer should be used | |
3601 ;; only rarely. | |
3602 ;; Not just because users often use the default. | |
3603 (interactive "FRecover file: ") | |
3604 (setq file (expand-file-name file)) | |
3605 (let ((handler (or (find-file-name-handler file 'recover-file) | |
3606 (find-file-name-handler | |
3607 (let ((buffer-file-name file)) | |
3608 (make-auto-save-file-name)) | |
3609 'recover-file)))) | |
3610 (if handler | |
3611 (funcall handler 'recover-file file) | |
464 | 3612 (if (auto-save-file-name-p (file-name-nondirectory file)) |
428 | 3613 (error "%s is an auto-save file" file)) |
3614 (let ((file-name (let ((buffer-file-name file)) | |
3615 (make-auto-save-file-name)))) | |
3616 (cond ((if (file-exists-p file) | |
3617 (not (file-newer-than-file-p file-name file)) | |
3618 (not (file-exists-p file-name))) | |
3619 (error "Auto-save file %s not current" file-name)) | |
844 | 3620 (t |
3621 (save-window-excursion | |
464 | 3622 ;; XEmacs change: use insert-directory instead of |
844 | 3623 ;; calling ls directly. Add option for diff. |
464 | 3624 (with-output-to-temp-buffer "*Directory*" |
3625 (buffer-disable-undo standard-output) | |
3626 (save-excursion | |
1346 | 3627 (let ((switches |
3628 (declare-boundp dired-listing-switches))) | |
1333 | 3629 (if (file-symlink-p file) |
3630 (setq switches (concat switches "L"))) | |
3631 (set-buffer standard-output) | |
3632 ;; XEmacs had the following line, not in FSF. | |
3633 (setq default-directory (file-name-directory file)) | |
3634 ;; Use insert-directory-safely, not insert-directory, | |
3635 ;; because these files might not exist. In particular, | |
3636 ;; FILE might not exist if the auto-save file was for | |
3637 ;; a buffer that didn't visit a file, such as "*mail*". | |
3638 ;; The code in v20.x called `ls' directly, so we need | |
3639 ;; to emulate what `ls' did in that case. | |
3640 (insert-directory-safely file switches) | |
3641 (insert-directory-safely file-name switches)))) | |
844 | 3642 (block nil |
3643 (while t | |
3644 (case (get-user-response | |
3645 nil | |
3646 ;; Formerly included file name. Useless now that | |
3647 ;; we display an ls of the files, and potentially | |
3648 ;; fills up the minibuffer, esp. with autosaves | |
3649 ;; all in one directory. | |
3650 "Recover auto save file? " | |
3651 '(("yes" "%_Yes" yes) | |
3652 ("no" "%_No" no) | |
3653 ("diff" "%_Diff" diff))) | |
3654 (no (error "Recover-file cancelled.")) | |
3655 (yes | |
3656 (switch-to-buffer (find-file-noselect file t)) | |
1333 | 3657 (let ((buffer-read-only nil) |
3658 ;; Keep the current buffer-file-coding-system. | |
3659 (coding-system buffer-file-coding-system) | |
3660 ;; Auto-saved file shoule be read without any code conversion. | |
3661 (coding-system-for-read 'escape-quoted)) | |
844 | 3662 (erase-buffer) |
1333 | 3663 (insert-file-contents file-name nil) |
4024 | 3664 (set-buffer-file-coding-system coding-system |
3665 nil t)) | |
844 | 3666 (after-find-file nil nil t) |
3667 (return nil)) | |
3668 (diff | |
3669 ;; rather than just diff the two files (which would | |
3670 ;; be easy), we have to deal with the fact that | |
3671 ;; they may be in different formats, since | |
3672 ;; auto-saves are always in escape-quoted. so, we | |
3673 ;; read the file into a buffer (#### should we look | |
3674 ;; at or use a file if it's already in a buffer? | |
3675 ;; maybe we would find hints as to the encoding of | |
3676 ;; the file?), then we save the resulting buffer in | |
3677 ;; escape-quoted, do the diff (between two files | |
3678 ;; both in escape-quoted) and read in the results | |
3679 ;; using coding system escape-quoted. That way, we | |
3680 ;; should get what's correct most of the time. | |
3681 (let ((buffer (generate-new-buffer "*recover*")) | |
3682 (temp | |
3683 (make-temp-name | |
3684 (concat (file-name-as-directory | |
3685 (temp-directory)) | |
3686 (file-name-nondirectory file) "-")))) | |
3687 (unwind-protect | |
3688 (progn | |
3689 (save-current-buffer | |
3690 (set-buffer buffer) | |
3691 (insert-file-contents file) | |
3692 (let ((coding-system-for-write | |
3693 'escape-quoted)) | |
3694 (write-region (point-min) (point-max) | |
3695 temp nil 'silent))) | |
3696 (with-output-to-temp-buffer "*Autosave Diff*" | |
3697 (buffer-disable-undo standard-output) | |
3698 (let ((coding-system-for-read | |
3699 'escape-quoted)) | |
3700 (condition-case ferr | |
3701 (apply #'call-process | |
3702 recover-file-diff-program | |
3703 nil standard-output nil | |
3704 (append | |
3705 recover-file-diff-arguments | |
3706 (list temp file-name))) | |
3707 (io-error | |
3708 (save-excursion | |
1346 | 3709 (let ((switches |
3710 (declare-boundp | |
3711 dired-listing-switches))) | |
1333 | 3712 (if (file-symlink-p file) |
3713 (setq switches (concat switches "L"))) | |
3714 (set-buffer standard-output) | |
3715 ;; XEmacs had the following line, not in FSF. | |
3716 (setq default-directory (file-name-directory file)) | |
1346 | 3717 ;; Use insert-directory-safely, |
3718 ;; not insert-directory, because | |
3719 ;; these files might not exist. | |
3720 ;; In particular, FILE might not | |
3721 ;; exist if the auto-save file | |
3722 ;; was for a buffer that didn't | |
3723 ;; visit a file, such as | |
3724 ;; "*mail*". The code in v20.x | |
3725 ;; called `ls' directly, so we | |
3726 ;; need to emulate what `ls' did | |
3727 ;; in that case. | |
1333 | 3728 (insert-directory-safely file switches) |
3729 (insert-directory-safely file-name switches)) | |
844 | 3730 (terpri) |
3731 (princ "Error during diff: ") | |
3732 (display-error ferr | |
3733 standard-output))))))) | |
3734 (ignore-errors (kill-buffer buffer)) | |
3735 (ignore-file-errors | |
3736 (delete-file temp))))))))))))))) | |
428 | 3737 |
3738 (defun recover-session () | |
3739 "Recover auto save files from a previous Emacs session. | |
3740 This command first displays a Dired buffer showing you the | |
3741 previous sessions that you could recover from. | |
3742 To choose one, move point to the proper line and then type C-c C-c. | |
3743 Then you'll be asked about a number of files to recover." | |
3744 (interactive) | |
3745 (unless (fboundp 'dired) | |
3746 (error "recover-session requires dired")) | |
3747 (if (null auto-save-list-file-prefix) | |
3748 (error | |
3749 "You set `auto-save-list-file-prefix' to disable making session files")) | |
1333 | 3750 (let ((dir (file-name-directory auto-save-list-file-prefix))) |
3751 (unless (file-directory-p dir) | |
3752 (make-directory dir t))) | |
851 | 3753 (let* ((auto-save-list-dir |
3754 (file-name-directory auto-save-list-file-prefix)) | |
3755 (files (directory-files | |
3756 auto-save-list-dir | |
3757 t | |
3758 (concat "^" (regexp-quote (file-name-nondirectory | |
3759 auto-save-list-file-prefix))))) | |
3760 (files (sort (delete-if-not #'Recover-session-files-from-auto-save-list-file | |
3761 files) #'file-newer-than-file-p))) | |
3762 (unless files | |
3763 (error "No sessions can be recovered now")) | |
3764 (declare-fboundp (dired (cons auto-save-list-dir files))) | |
1333 | 3765 (save-excursion |
3766 (goto-char (point-min)) | |
3767 (or (looking-at "Move to the session you want to recover,") | |
3768 (let ((inhibit-read-only t)) | |
3769 (delete-matching-lines "^[ \t]*total.*$") | |
3770 (insert "Move to the session you want to recover,\n" | |
3771 "then type C-c C-c to select it.\n\n" | |
3772 "You can also delete some of these files;\n" | |
3773 "type d on a line to mark that file for deletion.\n\n")))) | |
851 | 3774 (use-local-map (let ((map (make-sparse-keymap))) |
3775 (set-keymap-parents map (list (current-local-map))) | |
3776 map)) | |
3777 (define-key (current-local-map) "\C-c\C-c" 'recover-session-finish))) | |
428 | 3778 |
851 | 3779 (defun Recover-session-files-from-auto-save-list-file (file) |
3780 "Return the auto save files in list file FILE that are current." | |
3781 (let (files | |
428 | 3782 (buffer (get-buffer-create " *recover*"))) |
3783 (unwind-protect | |
3784 (save-excursion | |
3785 ;; Read in the auto-save-list file. | |
3786 (set-buffer buffer) | |
3787 (erase-buffer) | |
771 | 3788 (let ((coding-system-for-read 'escape-quoted)) |
3789 (insert-file-contents file)) | |
428 | 3790 ;; Loop thru the text of that file |
3791 ;; and get out the names of the files to recover. | |
3792 (while (not (eobp)) | |
3793 (let (thisfile autofile) | |
3794 (if (eolp) | |
3795 ;; This is a pair of lines for a non-file-visiting buffer. | |
3796 ;; Get the auto-save file name and manufacture | |
3797 ;; a "visited file name" from that. | |
3798 (progn | |
3799 (forward-line 1) | |
3800 (setq autofile | |
3801 (buffer-substring-no-properties | |
3802 (point) | |
3803 (save-excursion | |
3804 (end-of-line) | |
3805 (point)))) | |
3806 (setq thisfile | |
3807 (expand-file-name | |
3808 (substring | |
3809 (file-name-nondirectory autofile) | |
3810 1 -1) | |
3811 (file-name-directory autofile))) | |
3812 (forward-line 1)) | |
3813 ;; This pair of lines is a file-visiting | |
3814 ;; buffer. Use the visited file name. | |
3815 (progn | |
3816 (setq thisfile | |
3817 (buffer-substring-no-properties | |
3818 (point) (progn (end-of-line) (point)))) | |
3819 (forward-line 1) | |
3820 (setq autofile | |
3821 (buffer-substring-no-properties | |
3822 (point) (progn (end-of-line) (point)))) | |
3823 (forward-line 1))) | |
3824 ;; Ignore a file if its auto-save file does not exist now. | |
3825 (if (file-exists-p autofile) | |
3826 (setq files (cons thisfile files))))) | |
851 | 3827 (setq files (nreverse files))) |
428 | 3828 (kill-buffer buffer)))) |
3829 | |
851 | 3830 (defun recover-session-finish () |
3831 "Choose one saved session to recover auto-save files from. | |
3832 This command is used in the special Dired buffer created by | |
3833 \\[recover-session]." | |
3834 (interactive) | |
3835 ;; Get the name of the session file to recover from. | |
3836 (let ((file (declare-fboundp (dired-get-filename)))) | |
1346 | 3837 (declare-fboundp (dired-unmark 1)) |
851 | 3838 ;; #### dired-do-flagged-delete in FSF. |
3839 ;; This version is for ange-ftp | |
1346 | 3840 ;;(declare-fboundp (dired-do-deletions t)) |
851 | 3841 ;; This version is for efs |
3842 (declare-fboundp (dired-expunge-deletions)) | |
3843 (let ((files (Recover-session-files-from-auto-save-list-file file))) | |
3844 ;; The file contains a pair of line for each auto-saved buffer. | |
3845 ;; The first line of the pair contains the visited file name | |
3846 ;; or is empty if the buffer was not visiting a file. | |
3847 ;; The second line is the auto-save file name. | |
3848 (if files | |
3849 (map-y-or-n-p "Recover %s? " | |
3850 (lambda (file) | |
3851 (condition-case nil | |
3852 (save-excursion (recover-file file)) | |
3853 (error | |
3854 (lwarn 'recover 'alert | |
3855 "Failed to recover `%s'" file)))) | |
3856 files | |
3857 '("file" "files" "recover")) | |
3858 (message "No files can be recovered from this session now"))))) | |
3859 | |
428 | 3860 (defun kill-some-buffers (&optional list) |
3861 "For each buffer in LIST, ask whether to kill it. | |
3862 LIST defaults to all existing live buffers." | |
3863 (interactive) | |
3864 (if (null list) | |
3865 (setq list (buffer-list))) | |
3866 (while list | |
3867 (let* ((buffer (car list)) | |
3868 (name (buffer-name buffer))) | |
3869 (and (not (string-equal name "")) | |
1333 | 3870 (not (eq (aref name 0) ?\ )) |
428 | 3871 (yes-or-no-p |
3872 (format | |
3873 (if (buffer-modified-p buffer) | |
3874 (gettext "Buffer %s HAS BEEN EDITED. Kill? ") | |
3875 (gettext "Buffer %s is unmodified. Kill? ")) | |
3876 name)) | |
3877 (kill-buffer buffer))) | |
3878 (setq list (cdr list)))) | |
3879 | |
3880 (defun auto-save-mode (arg) | |
3881 "Toggle auto-saving of contents of current buffer. | |
3882 With prefix argument ARG, turn auto-saving on if positive, else off." | |
3883 (interactive "P") | |
3884 (setq buffer-auto-save-file-name | |
3885 (and (if (null arg) | |
3886 (or (not buffer-auto-save-file-name) | |
3887 ;; If autosave is off because buffer has shrunk, | |
3888 ;; then toggling should turn it on. | |
3889 (< buffer-saved-size 0)) | |
3890 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))) | |
3891 (if (and buffer-file-name auto-save-visited-file-name | |
3892 (not buffer-read-only)) | |
3893 buffer-file-name | |
3894 (make-auto-save-file-name)))) | |
3895 ;; If -1 was stored here, to temporarily turn off saving, | |
3896 ;; turn it back on. | |
3897 (and (< buffer-saved-size 0) | |
3898 (setq buffer-saved-size 0)) | |
3899 (if (interactive-p) | |
3900 (if buffer-auto-save-file-name ;; rewritten for I18N3 snarfing | |
3901 (display-message 'command "Auto-save on (in this buffer)") | |
3902 (display-message 'command "Auto-save off (in this buffer)"))) | |
3903 buffer-auto-save-file-name) | |
3904 | |
3905 (defun rename-auto-save-file () | |
3906 "Adjust current buffer's auto save file name for current conditions. | |
3907 Also rename any existing auto save file, if it was made in this session." | |
3908 (let ((osave buffer-auto-save-file-name)) | |
3909 (setq buffer-auto-save-file-name | |
3910 (make-auto-save-file-name)) | |
3911 (if (and osave buffer-auto-save-file-name | |
3912 (not (string= buffer-auto-save-file-name buffer-file-name)) | |
3913 (not (string= buffer-auto-save-file-name osave)) | |
3914 (file-exists-p osave) | |
3915 (recent-auto-save-p)) | |
3916 (rename-file osave buffer-auto-save-file-name t)))) | |
3917 | |
1333 | 3918 ;; END SYNC WITH FSF 21.2. |
3919 | |
464 | 3920 ;; make-auto-save-file-name and auto-save-file-name-p are now only in |
3921 ;; auto-save.el. | |
428 | 3922 |
3923 | |
1333 | 3924 ;; BEGIN SYNC WITH FSF 21.2. |
3925 | |
428 | 3926 (defun wildcard-to-regexp (wildcard) |
3927 "Given a shell file name pattern WILDCARD, return an equivalent regexp. | |
3928 The generated regexp will match a filename iff the filename | |
3929 matches that wildcard according to shell rules. Only wildcards known | |
3930 by `sh' are supported." | |
3931 (let* ((i (string-match "[[.*+\\^$?]" wildcard)) | |
3932 ;; Copy the initial run of non-special characters. | |
3933 (result (substring wildcard 0 i)) | |
3934 (len (length wildcard))) | |
3935 ;; If no special characters, we're almost done. | |
3936 (if i | |
3937 (while (< i len) | |
3938 (let ((ch (aref wildcard i)) | |
3939 j) | |
3940 (setq | |
3941 result | |
3942 (concat result | |
3943 (cond | |
1333 | 3944 ((and (eq ch ?\[) |
3945 (< (1+ i) len) | |
3946 (eq (aref wildcard (1+ i)) ?\])) | |
3947 "\\[") | |
428 | 3948 ((eq ch ?\[) ; [...] maps to regexp char class |
3949 (progn | |
3950 (setq i (1+ i)) | |
3951 (concat | |
3952 (cond | |
3953 ((eq (aref wildcard i) ?!) ; [!...] -> [^...] | |
3954 (progn | |
3955 (setq i (1+ i)) | |
3956 (if (eq (aref wildcard i) ?\]) | |
3957 (progn | |
3958 (setq i (1+ i)) | |
3959 "[^]") | |
3960 "[^"))) | |
3961 ((eq (aref wildcard i) ?^) | |
3962 ;; Found "[^". Insert a `\0' character | |
3963 ;; (which cannot happen in a filename) | |
3964 ;; into the character class, so that `^' | |
3965 ;; is not the first character after `[', | |
3966 ;; and thus non-special in a regexp. | |
3967 (progn | |
3968 (setq i (1+ i)) | |
3969 "[\000^")) | |
3970 ((eq (aref wildcard i) ?\]) | |
3971 ;; I don't think `]' can appear in a | |
3972 ;; character class in a wildcard, but | |
3973 ;; let's be general here. | |
3974 (progn | |
3975 (setq i (1+ i)) | |
3976 "[]")) | |
3977 (t "[")) | |
3978 (prog1 ; copy everything upto next `]'. | |
3979 (substring wildcard | |
3980 i | |
3981 (setq j (string-match | |
3982 "]" wildcard i))) | |
3983 (setq i (if j (1- j) (1- len))))))) | |
3984 ((eq ch ?.) "\\.") | |
3985 ((eq ch ?*) "[^\000]*") | |
3986 ((eq ch ?+) "\\+") | |
3987 ((eq ch ?^) "\\^") | |
3988 ((eq ch ?$) "\\$") | |
3989 ((eq ch ?\\) "\\\\") ; probably cannot happen... | |
3990 ((eq ch ??) "[^\000]") | |
3991 (t (char-to-string ch))))) | |
3992 (setq i (1+ i))))) | |
3993 ;; Shell wildcards should match the entire filename, | |
3994 ;; not its part. Make the regexp say so. | |
3995 (concat "\\`" result "\\'"))) | |
3996 | |
3997 (defcustom list-directory-brief-switches "-CF" | |
3998 "*Switches for list-directory to pass to `ls' for brief listing." | |
3999 :type 'string | |
4000 :group 'dired) | |
4001 | |
4002 (defcustom list-directory-verbose-switches "-l" | |
4003 "*Switches for list-directory to pass to `ls' for verbose listing," | |
4004 :type 'string | |
4005 :group 'dired) | |
4006 | |
1333 | 4007 (defun file-expand-wildcards (pattern &optional full) |
4008 "Expand wildcard pattern PATTERN. | |
4009 This returns a list of file names which match the pattern. | |
4010 | |
4011 If PATTERN is written as an absolute relative file name, | |
4012 the values are absolute also. | |
4013 | |
4014 If PATTERN is written as a relative file name, it is interpreted | |
4015 relative to the current default directory, `default-directory'. | |
4016 The file names returned are normally also relative to the current | |
4017 default directory. However, if FULL is non-nil, they are absolute." | |
4018 (let* ((nondir (file-name-nondirectory pattern)) | |
4019 (dirpart (file-name-directory pattern)) | |
4020 ;; A list of all dirs that DIRPART specifies. | |
4021 ;; This can be more than one dir | |
4022 ;; if DIRPART contains wildcards. | |
4023 (dirs (if (and dirpart (string-match "[[*?]" dirpart)) | |
4024 (mapcar 'file-name-as-directory | |
4025 (file-expand-wildcards (directory-file-name dirpart))) | |
4026 (list dirpart))) | |
4027 contents) | |
4028 (while dirs | |
4029 (when (or (null (car dirs)) ; Possible if DIRPART is not wild. | |
4030 (file-directory-p (directory-file-name (car dirs)))) | |
4031 (let ((this-dir-contents | |
4032 ;; Filter out "." and ".." | |
4033 (delq nil | |
4034 (mapcar #'(lambda (name) | |
4035 (unless (string-match "\\`\\.\\.?\\'" | |
4036 (file-name-nondirectory name)) | |
4037 name)) | |
4038 (directory-files (or (car dirs) ".") full | |
4039 (wildcard-to-regexp nondir)))))) | |
4040 (setq contents | |
4041 (nconc | |
4042 (if (and (car dirs) (not full)) | |
4043 (mapcar (function (lambda (name) (concat (car dirs) name))) | |
4044 this-dir-contents) | |
4045 this-dir-contents) | |
4046 contents)))) | |
4047 (setq dirs (cdr dirs))) | |
4048 contents)) | |
4049 | |
428 | 4050 (defun list-directory (dirname &optional verbose) |
4051 "Display a list of files in or matching DIRNAME, a la `ls'. | |
4052 DIRNAME is globbed by the shell if necessary. | |
4053 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'. | |
4054 Actions controlled by variables `list-directory-brief-switches' | |
4055 and `list-directory-verbose-switches'." | |
4056 (interactive (let ((pfx current-prefix-arg)) | |
4057 (list (read-file-name (if pfx (gettext "List directory (verbose): ") | |
4058 (gettext "List directory (brief): ")) | |
4059 nil default-directory nil) | |
4060 pfx))) | |
4061 (let ((switches (if verbose list-directory-verbose-switches | |
4062 list-directory-brief-switches))) | |
4063 (or dirname (setq dirname default-directory)) | |
4064 (setq dirname (expand-file-name dirname)) | |
4065 (with-output-to-temp-buffer "*Directory*" | |
4066 (buffer-disable-undo standard-output) | |
4067 (princ "Directory ") | |
4068 (princ dirname) | |
4069 (terpri) | |
4070 (save-excursion | |
4071 (set-buffer "*Directory*") | |
1333 | 4072 (setq default-directory |
4073 (if (file-directory-p dirname) | |
4074 (file-name-as-directory dirname) | |
4075 (file-name-directory dirname))) | |
428 | 4076 (let ((wildcard (not (file-directory-p dirname)))) |
4077 (insert-directory dirname switches wildcard (not wildcard))))))) | |
4078 | |
1333 | 4079 (defun shell-quote-wildcard-pattern (pattern) |
4080 "Quote characters special to the shell in PATTERN, leave wildcards alone. | |
4081 | |
4082 PATTERN is assumed to represent a file-name wildcard suitable for the | |
4083 underlying filesystem. For Unix and GNU/Linux, the characters from the | |
4084 set [ \\t\\n;<>&|()#$] are quoted with a backslash; for DOS/Windows, all | |
4085 the parts of the pattern which don't include wildcard characters are | |
4086 quoted with double quotes. | |
4087 Existing quote characters in PATTERN are left alone, so you can pass | |
4088 PATTERN that already quotes some of the special characters." | |
4089 (save-match-data | |
4090 (cond | |
4091 ((memq system-type '(ms-dos windows-nt)) | |
4092 ;; DOS/Windows don't allow `"' in file names. So if the | |
4093 ;; argument has quotes, we can safely assume it is already | |
4094 ;; quoted by the caller. | |
4095 (if (or (string-match "[\"]" pattern) | |
4096 ;; We quote [&()#$'] in case their shell is a port of a | |
4097 ;; Unixy shell. We quote [,=+] because stock DOS and | |
4098 ;; Windows shells require that in some cases, such as | |
4099 ;; passing arguments to batch files that use positional | |
4100 ;; arguments like %1. | |
4101 (not (string-match "[ \t;&()#$',=+]" pattern))) | |
4102 pattern | |
4103 (let ((result "\"") | |
4104 (beg 0) | |
4105 end) | |
4106 (while (string-match "[*?]+" pattern beg) | |
4107 (setq end (match-beginning 0) | |
4108 result (concat result (substring pattern beg end) | |
4109 "\"" | |
4110 (substring pattern end (match-end 0)) | |
4111 "\"") | |
4112 beg (match-end 0))) | |
4113 (concat result (substring pattern beg) "\"")))) | |
4114 (t | |
4115 (let ((beg 0)) | |
4116 (while (string-match "[ \t\n;<>&|()#$]" pattern beg) | |
4117 (setq pattern | |
4118 (concat (substring pattern 0 (match-beginning 0)) | |
4119 "\\" | |
4120 (substring pattern (match-beginning 0))) | |
4121 beg (1+ (match-end 0))))) | |
4122 pattern)))) | |
4123 | |
4124 | |
428 | 4125 (defvar insert-directory-program "ls" |
4126 "Absolute or relative name of the `ls' program used by `insert-directory'.") | |
4127 | |
4128 ;; insert-directory | |
4129 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and | |
4130 ;; FULL-DIRECTORY-P is nil. | |
4131 ;; The single line of output must display FILE's name as it was | |
4132 ;; given, namely, an absolute path name. | |
4133 ;; - must insert exactly one line for each file if WILDCARD or | |
4134 ;; FULL-DIRECTORY-P is t, plus one optional "total" line | |
4135 ;; before the file lines, plus optional text after the file lines. | |
4136 ;; Lines are delimited by "\n", so filenames containing "\n" are not | |
4137 ;; allowed. | |
4138 ;; File lines should display the basename. | |
4139 ;; - must be consistent with | |
4140 ;; - functions dired-move-to-filename, (these two define what a file line is) | |
4141 ;; dired-move-to-end-of-filename, | |
4142 ;; dired-between-files, (shortcut for (not (dired-move-to-filename))) | |
4143 ;; dired-insert-headerline | |
4144 ;; dired-after-subdir-garbage (defines what a "total" line is) | |
4145 ;; - variable dired-subdir-regexp | |
1606 | 4146 ;; - may be passed "--dired" as argument in SWITCHES. |
4147 ;; Filename handlers might have to remove this switch if their | |
4148 ;; "ls" command does not support it. | |
1333 | 4149 |
4150 ;; END SYNC WITH FSF 21.2. | |
4151 | |
2671 | 4152 (defvar insert-directory-ls-version 'unknown) |
4153 | |
428 | 4154 (defun insert-directory (file switches &optional wildcard full-directory-p) |
4155 "Insert directory listing for FILE, formatted according to SWITCHES. | |
4156 Leaves point after the inserted text. | |
4157 SWITCHES may be a string of options, or a list of strings. | |
4158 Optional third arg WILDCARD means treat FILE as shell wildcard. | |
4159 Optional fourth arg FULL-DIRECTORY-P means file is a directory and | |
4160 switches do not contain `d', so that a full listing is expected. | |
4161 | |
4162 This works by running a directory listing program | |
4163 whose name is in the variable `insert-directory-program'. | |
4164 If WILDCARD, it also runs the shell specified by `shell-file-name'." | |
4165 ;; We need the directory in order to find the right handler. | |
4166 (let ((handler (find-file-name-handler (expand-file-name file) | |
4167 'insert-directory))) | |
1606 | 4168 (cond |
4169 (handler | |
4170 (funcall handler 'insert-directory file switches | |
4171 wildcard full-directory-p)) | |
4172 ;; [mswindows-insert-directory should be called | |
4173 ;; nt-insert-directory - kkm]. not true any more according to | |
4174 ;; my new naming scheme. --ben | |
4175 ((and (fboundp 'mswindows-insert-directory) | |
4176 (eq system-type 'windows-nt)) | |
4177 (declare-fboundp (mswindows-insert-directory | |
4178 file switches wildcard full-directory-p))) | |
4179 (t | |
4180 (let* ((beg (point)) | |
4400
555e21a66d51
2008-01-13 Michael Sperber <mike@xemacs.org>
Mike Sperber <sperber@deinprogramm.de>
parents:
4266
diff
changeset
|
4181 ;; on Unix, assume that ls will output in what the |
555e21a66d51
2008-01-13 Michael Sperber <mike@xemacs.org>
Mike Sperber <sperber@deinprogramm.de>
parents:
4266
diff
changeset
|
4182 ;; file-name coding system specifies |
555e21a66d51
2008-01-13 Michael Sperber <mike@xemacs.org>
Mike Sperber <sperber@deinprogramm.de>
parents:
4266
diff
changeset
|
4183 (coding-system-for-read (get-coding-system 'file-name)) |
1606 | 4184 (result |
4185 (if wildcard | |
4186 ;; Run ls in the directory of the file pattern we asked for. | |
4187 (let ((default-directory | |
4188 (if (file-name-absolute-p file) | |
4189 (file-name-directory file) | |
4190 (file-name-directory (expand-file-name file)))) | |
4191 (pattern (file-name-nondirectory file)) | |
4192 (start 0)) | |
4193 ;; Quote some characters that have special meanings in shells; | |
4194 ;; but don't quote the wildcards--we want them to be special. | |
4195 ;; We also currently don't quote the quoting characters | |
4196 ;; in case people want to use them explicitly to quote | |
4197 ;; wildcard characters. | |
4198 ;;#### Unix-specific | |
4199 (while (string-match "[ \t\n;<>&|()#$]" pattern start) | |
4200 (setq pattern | |
4201 (concat (substring pattern 0 (match-beginning 0)) | |
4202 "\\" | |
4203 (substring pattern (match-beginning 0))) | |
4204 start (1+ (match-end 0)))) | |
4205 (call-process shell-file-name nil t nil | |
4206 "-c" (concat "\\" ;; Disregard shell aliases! | |
4207 insert-directory-program | |
4208 " -d " | |
4209 (if (stringp switches) | |
4210 switches | |
4211 (mapconcat 'identity switches " ")) | |
4212 " " | |
4213 pattern))) | |
4214 ;; SunOS 4.1.3, SVr4 and others need the "." to list the | |
4215 ;; directory if FILE is a symbolic link. | |
4216 (apply 'call-process | |
4217 insert-directory-program nil t nil | |
4218 (let (list) | |
4219 (if (listp switches) | |
4220 (setq list switches) | |
4221 (if (not (equal switches "")) | |
4222 (let ((switches switches)) | |
4223 ;; Split the switches at any spaces | |
4224 ;; so we can pass separate options as separate args. | |
4225 (while (string-match " " switches) | |
4226 (setq list (cons (substring switches 0 (match-beginning 0)) | |
4227 list) | |
4228 switches (substring switches (match-end 0)))) | |
4229 (setq list (cons switches list))))) | |
4230 (append list | |
4231 (list | |
4232 (if full-directory-p | |
4233 (concat (file-name-as-directory file) | |
4234 ;;#### Unix-specific | |
4235 ".") | |
4236 file)))))))) | |
2671 | 4237 |
4238 ;; If we got "//DIRED//" in the output, it means we got a real | |
4239 ;; directory listing, even if `ls' returned nonzero. | |
4240 ;; So ignore any errors. | |
4241 (when (if (stringp switches) | |
4242 (string-match "--dired\\>" switches) | |
4243 (member "--dired" switches)) | |
4244 (save-excursion | |
4245 (forward-line -2) | |
4246 (when (looking-at "//SUBDIRED//") | |
4247 (forward-line -1)) | |
4248 (if (looking-at "//DIRED//") | |
4249 (setq result 0)))) | |
4250 | |
4251 (when (and (not (eq 0 result)) | |
4252 (eq insert-directory-ls-version 'unknown)) | |
4253 ;; The first time ls returns an error, | |
4254 ;; find the version numbers of ls, | |
4255 ;; and set insert-directory-ls-version | |
4256 ;; to > if it is more than 5.2.1, < if it is less, nil if it | |
4257 ;; is equal or if the info cannot be obtained. | |
4258 ;; (That can mean it isn't GNU ls.) | |
4259 (let ((version-out | |
4260 (with-temp-buffer | |
4261 (call-process "ls" nil t nil "--version") | |
4262 (buffer-string)))) | |
4263 (if (string-match "ls (.*utils) \\([0-9.]*\\)$" version-out) | |
4264 (let* ((version (match-string 1 version-out)) | |
4265 (split (split-string version "[.]")) | |
4266 (numbers (mapcar 'string-to-int split)) | |
4267 (min '(5 2 1)) | |
4268 comparison) | |
4269 (while (and (not comparison) (or numbers min)) | |
4270 (cond ((null min) | |
4271 (setq comparison '>)) | |
4272 ((null numbers) | |
4273 (setq comparison '<)) | |
4274 ((> (car numbers) (car min)) | |
4275 (setq comparison '>)) | |
4276 ((< (car numbers) (car min)) | |
4277 (setq comparison '<)) | |
4278 (t | |
4279 (setq numbers (cdr numbers) | |
4280 min (cdr min))))) | |
4281 (setq insert-directory-ls-version (or comparison '=))) | |
4282 (setq insert-directory-ls-version nil)))) | |
4283 | |
4284 ;; For GNU ls versions 5.2.2 and up, ignore minor errors. | |
4285 (when (and (eq 1 result) (eq insert-directory-ls-version '>)) | |
4286 (setq result 0)) | |
4287 | |
1606 | 4288 ;; If `insert-directory-program' failed, signal an error. |
2671 | 4289 (unless (eq 0 result) |
4290 ;; Delete the error message it may have output. | |
4291 (delete-region beg (point)) | |
4292 ;; On non-Posix systems, we cannot open a directory, so | |
4293 ;; don't even try, because that will always result in | |
4294 ;; the ubiquitous "Access denied". Instead, show the | |
4295 ;; command line so the user can try to guess what went wrong. | |
4296 (if (and (file-directory-p file) | |
4297 (memq system-type '(ms-dos windows-nt))) | |
4298 (error | |
4299 "Reading directory: \"%s %s -- %s\" exited with status %s" | |
4300 insert-directory-program | |
4301 (if (listp switches) (concat switches) switches) | |
4302 file result) | |
4303 (error "Listing directory failed"))) | |
1606 | 4304 |
4305 (when (or (and (listp switches) | |
4306 (member "--dired" switches)) | |
4307 (string-match "--dired\\>" switches)) | |
4308 (forward-line -2) | |
4309 (when (looking-at "//SUBDIRED//") | |
4310 (delete-region (point) (progn (forward-line 1) (point))) | |
4311 (forward-line -1)) | |
2671 | 4312 (if (looking-at "//DIRED//") |
4313 (let ((end (line-end-position)) | |
4314 (linebeg (point)) | |
4315 error-lines) | |
4316 ;; Find all the lines that are error messages, | |
4317 ;; and record the bounds of each one. | |
4318 (goto-char beg) | |
4319 (while (< (point) linebeg) | |
4320 (or (eql (following-char) ?\s) | |
4321 (push (list (point) (line-end-position)) error-lines)) | |
4322 (forward-line 1)) | |
4323 (setq error-lines (nreverse error-lines)) | |
4324 ;; Now read the numeric positions of file names. | |
4325 (goto-char linebeg) | |
4326 (forward-word 1) | |
4327 (forward-char 3) | |
4328 (while (< (point) end) | |
4329 (let ((start (insert-directory-adj-pos | |
4330 (+ beg (read (current-buffer))) | |
4331 error-lines)) | |
4332 (end (insert-directory-adj-pos | |
4333 (+ beg (read (current-buffer))) | |
4334 error-lines))) | |
4335 (if (memq (char-after end) '(?\n ?\ )) | |
4336 ;; End is followed by \n or by " -> ". | |
4337 (let ((filename-extent (make-extent start end))) | |
4338 (set-extent-property filename-extent 'dired-file-name t) | |
4339 (set-extent-property filename-extent 'start-open t) | |
4340 (set-extent-property filename-extent 'end-open t)) | |
4341 ;; It seems that we can't trust ls's output as to | |
4342 ;; byte positions of filenames. | |
4343 (map-extents | |
4344 #'(lambda (extent maparg) | |
4345 (delete-extent extent) | |
4346 nil) | |
4347 nil beg (point) nil nil 'dired-file-name) | |
4348 (end-of-line)))) | |
4349 (goto-char end) | |
4350 (beginning-of-line) | |
4351 (delete-region (point) (progn (forward-line 1) (point)))) | |
4352 ;; Take care of the case where the ls output contains a | |
4353 ;; "//DIRED-OPTIONS//"-line, but no "//DIRED//"-line | |
4354 ;; and we went one line too far back (see above). | |
4355 (forward-line 1)) | |
4356 (if (looking-at "//DIRED-OPTIONS//") | |
4357 (delete-region (point) (progn (forward-line 1) (point)))))))))) | |
4358 | |
4359 (defun insert-directory-adj-pos (pos error-lines) | |
4360 "Convert `ls --dired' file name position value POS to a buffer position. | |
4361 File name position values returned in ls --dired output | |
4362 count only stdout; they don't count the error messages sent to stderr. | |
4363 So this function converts to them to real buffer positions. | |
4364 ERROR-LINES is a list of buffer positions of error message lines, | |
4365 of the form (START END)." | |
4366 (while (and error-lines (< (caar error-lines) pos)) | |
4367 (setq pos (+ pos (- (nth 1 (car error-lines)) (nth 0 (car error-lines))))) | |
4368 (pop error-lines)) | |
4369 pos) | |
428 | 4370 |
1333 | 4371 ;; BEGIN SYNC WITH FSF 21.2. |
4372 | |
4373 (defun insert-directory-safely (file switches | |
4374 &optional wildcard full-directory-p) | |
4375 "Insert directory listing for FILE, formatted according to SWITCHES. | |
4376 | |
4377 Like `insert-directory', but if FILE does not exist, it inserts a | |
4378 message to that effect instead of signaling an error." | |
4379 (if (file-exists-p file) | |
4380 (insert-directory file switches wildcard full-directory-p) | |
4381 ;; Simulate the message printed by `ls'. | |
4382 (insert (format "%s: No such file or directory\n" file)))) | |
4383 | |
428 | 4384 (defvar kill-emacs-query-functions nil |
4385 "Functions to call with no arguments to query about killing XEmacs. | |
4386 If any of these functions returns nil, killing Emacs is cancelled. | |
4387 `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions, | |
4388 but `kill-emacs', the low level primitive, does not. | |
4389 See also `kill-emacs-hook'.") | |
4390 | |
1333 | 4391 (defcustom confirm-kill-emacs nil |
4392 "How to ask for confirmation when leaving Emacs. | |
4393 If nil, the default, don't ask at all. If the value is non-nil, it should | |
4394 be a predicate function such as `yes-or-no-p'." | |
4395 :type '(choice (const :tag "Ask with yes-or-no-p" yes-or-no-p) | |
4396 (const :tag "Ask with y-or-n-p" y-or-n-p) | |
4397 (const :tag "Don't confirm" nil)) | |
4398 :group 'emacs | |
4399 ;:version "21.1" | |
4400 ) | |
4401 | |
428 | 4402 (defun save-buffers-kill-emacs (&optional arg) |
4403 "Offer to save each buffer, then kill this XEmacs process. | |
4404 With prefix arg, silently save all file-visiting buffers, then kill." | |
4405 (interactive "P") | |
4406 (save-some-buffers arg t) | |
4407 (and (or (not (memq t (mapcar #'(lambda (buf) (and (buffer-file-name buf) | |
4408 (buffer-modified-p buf))) | |
4409 (buffer-list)))) | |
4410 (yes-or-no-p "Modified buffers exist; exit anyway? ")) | |
4411 (or (not (fboundp 'process-list)) | |
4412 ;; process-list is not defined on VMS. | |
4413 (let ((processes (process-list)) | |
4414 active) | |
4415 (while processes | |
4416 (and (memq (process-status (car processes)) '(run stop open)) | |
4417 (let ((val (process-kill-without-query (car processes)))) | |
4418 (process-kill-without-query (car processes) val) | |
4419 val) | |
4420 (setq active t)) | |
4421 (setq processes (cdr processes))) | |
4422 (or | |
4423 (not active) | |
4424 (save-excursion | |
4425 (save-window-excursion | |
4426 (delete-other-windows) | |
4427 (list-processes) | |
4428 (yes-or-no-p | |
4429 "Active processes exist; kill them and exit anyway? ")))))) | |
4430 ;; Query the user for other things, perhaps. | |
4431 (run-hook-with-args-until-failure 'kill-emacs-query-functions) | |
1333 | 4432 (or (null confirm-kill-emacs) |
4433 (funcall confirm-kill-emacs "Really exit Emacs? ")) | |
428 | 4434 (kill-emacs))) |
4435 | |
4436 (defun symlink-expand-file-name (filename) | |
4437 "If FILENAME is a symlink, return its non-symlink equivalent. | |
4438 Unlike `file-truename', this doesn't chase symlinks in directory | |
4439 components of the file or expand a relative pathname into an | |
4440 absolute one." | |
4441 (let ((count 20)) | |
4442 (while (and (> count 0) (file-symlink-p filename)) | |
4443 (setq filename (file-symlink-p filename) | |
4444 count (1- count))) | |
4445 (if (> count 0) | |
4446 filename | |
4447 (error "Apparently circular symlink path")))) | |
4448 | |
4449 ;; Suggested by Michael Kifer <kifer@CS.SunySB.EDU> | |
4450 (defun file-remote-p (file-name) | |
4451 "Test whether FILE-NAME is looked for on a remote system." | |
776 | 4452 (cond ((not (declare-boundp allow-remote-paths)) nil) |
502 | 4453 ((fboundp 'ange-ftp-ftp-path) |
4454 (declare-fboundp (ange-ftp-ftp-path file-name))) | |
4455 ((fboundp 'efs-ftp-path) | |
4456 (declare-fboundp (efs-ftp-path file-name))) | |
428 | 4457 (t nil))) |
4458 | |
1333 | 4459 |
4460 ;; We use /: as a prefix to "quote" a file name | |
4461 ;; so that magic file name handlers will not apply to it. | |
4462 | |
4463 (setq file-name-handler-alist | |
4464 (cons '("\\`/:" . file-name-non-special) | |
4465 file-name-handler-alist)) | |
4466 | |
4467 ;; We depend on being the last handler on the list, | |
4468 ;; so that anything else which does need handling | |
4469 ;; has been handled already. | |
4470 ;; So it is safe for us to inhibit *all* magic file name handlers. | |
4471 | |
4472 (defun file-name-non-special (operation &rest arguments) | |
4473 (let ((file-name-handler-alist nil) | |
4474 (default-directory | |
4475 (if (eq operation 'insert-directory) | |
4476 (directory-file-name | |
4477 (expand-file-name | |
4478 (unhandled-file-name-directory default-directory))) | |
4479 default-directory)) | |
4480 ;; Get a list of the indices of the args which are file names. | |
4481 (file-arg-indices | |
4482 (cdr (or (assq operation | |
4483 ;; The first four are special because they | |
4484 ;; return a file name. We want to include the /: | |
4485 ;; in the return value. | |
4486 ;; So just avoid stripping it in the first place. | |
4487 '((expand-file-name . nil) | |
4488 ;; `identity' means just return the first arg | |
4489 ;; as stripped of its quoting. | |
4490 (substitute-in-file-name . identity) | |
4491 (file-name-directory . nil) | |
4492 (file-name-as-directory . nil) | |
4493 (directory-file-name . nil) | |
4494 (file-name-completion 0 1) | |
4495 (file-name-all-completions 0 1) | |
4496 (rename-file 0 1) | |
4497 (copy-file 0 1) | |
4498 (make-symbolic-link 0 1) | |
4499 (add-name-to-file 0 1))) | |
4500 ;; For all other operations, treat the first argument only | |
4501 ;; as the file name. | |
4502 '(nil 0)))) | |
4503 ;; Copy ARGUMENTS so we can replace elements in it. | |
4504 (arguments (copy-sequence arguments))) | |
4505 ;; Strip off the /: from the file names that have this handler. | |
4506 (save-match-data | |
4507 (while (consp file-arg-indices) | |
4508 (let ((pair (nthcdr (car file-arg-indices) arguments))) | |
4509 (and (car pair) | |
4510 (string-match "\\`/:" (car pair)) | |
4511 (setcar pair | |
4512 (if (= (length (car pair)) 2) | |
4513 "/" | |
4514 (substring (car pair) 2))))) | |
4515 (setq file-arg-indices (cdr file-arg-indices)))) | |
4516 (if (eq file-arg-indices 'identity) | |
4517 (car arguments) | |
4518 (apply operation arguments)))) | |
4519 | |
4520 ;; END SYNC WITH FSF 21.2. | |
428 | 4521 |
4522 ;;; files.el ends here |