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