Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; files.el --- file input and output commands for Emacs |
2 | ||
8c0e7b73 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
b4da00e9 | 4 | |
3a801d0c ER |
5 | ;; Maintainer: FSF |
6 | ||
b4da00e9 RM |
7 | ;; This file is part of GNU Emacs. |
8 | ||
9 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 11 | ;; the Free Software Foundation; either version 2, or (at your option) |
b4da00e9 RM |
12 | ;; any later version. |
13 | ||
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | ||
e41b2db1 ER |
23 | ;;; Commentary: |
24 | ||
25 | ;; Defines most of Emacs's file- and directory-handling functions, | |
26 | ;; including basic file visiting, backup generation, link handling, | |
27 | ;; ITS-id version control, load- and write-hook handling, and the like. | |
28 | ||
e5167999 ER |
29 | ;;; Code: |
30 | ||
b4da00e9 RM |
31 | (defconst delete-auto-save-files t |
32 | "*Non-nil means delete a buffer's auto-save file when the buffer is saved.") | |
33 | ||
34 | (defconst directory-abbrev-alist | |
35 | nil | |
36 | "*Alist of abbreviations for file directories. | |
37 | A list of elements of the form (FROM . TO), each meaning to replace | |
38 | FROM with TO when it appears in a directory name. This replacement is | |
39 | done when setting up the default directory of a newly visited file. | |
40 | *Every* FROM string should start with `^'. | |
41 | ||
42 | Use this feature when you have directories which you normally refer to | |
43 | via absolute symbolic links. Make TO the name of the link, and FROM | |
44 | the name it is linked to.") | |
45 | ||
46 | ;;; Turn off backup files on VMS since it has version numbers. | |
47 | (defconst make-backup-files (not (eq system-type 'vax-vms)) | |
48 | "*Create a backup of each file when it is saved for the first time. | |
49 | This can be done by renaming the file or by copying. | |
50 | ||
51 | Renaming means that Emacs renames the existing file so that it is a | |
52 | backup file, then writes the buffer into a new file. Any other names | |
53 | that the old file had will now refer to the backup file. The new file | |
54 | is owned by you and its group is defaulted. | |
55 | ||
56 | Copying means that Emacs copies the existing file into the backup | |
57 | file, then writes the buffer on top of the existing file. Any other | |
58 | names that the old file had will now refer to the new (edited) file. | |
59 | The file's owner and group are unchanged. | |
60 | ||
61 | The choice of renaming or copying is controlled by the variables | |
62 | `backup-by-copying', `backup-by-copying-when-linked' and | |
63 | `backup-by-copying-when-mismatch'.") | |
64 | ||
65 | ;; Do this so that local variables based on the file name | |
66 | ;; are not overridden by the major mode. | |
67 | (defvar backup-inhibited nil | |
68 | "Non-nil means don't make a backup file for this buffer.") | |
69 | (put 'backup-inhibited 'permanent-local t) | |
70 | ||
71 | (defconst backup-by-copying nil | |
72 | "*Non-nil means always use copying to create backup files. | |
73 | See documentation of variable `make-backup-files'.") | |
74 | ||
75 | (defconst backup-by-copying-when-linked nil | |
76 | "*Non-nil means use copying to create backups for files with multiple names. | |
77 | This causes the alternate names to refer to the latest version as edited. | |
78 | This variable is relevant only if `backup-by-copying' is nil.") | |
79 | ||
80 | (defconst backup-by-copying-when-mismatch nil | |
81 | "*Non-nil means create backups by copying if this preserves owner or group. | |
82 | Renaming may still be used (subject to control of other variables) | |
83 | when it would not result in changing the owner or group of the file; | |
84 | that is, for files which are owned by you and whose group matches | |
85 | the default for a new file created there by you. | |
86 | This variable is relevant only if `backup-by-copying' is nil.") | |
87 | ||
88 | (defvar backup-enable-predicate | |
89 | '(lambda (name) | |
90 | (or (< (length name) 5) | |
91 | (not (string-equal "/tmp/" (substring name 0 5))))) | |
92 | "Predicate that looks at a file name and decides whether to make backups. | |
93 | Called with an absolute file name as argument, it returns t to enable backup.") | |
94 | ||
95 | (defconst buffer-offer-save nil | |
96 | "*Non-nil in a buffer means offer to save the buffer on exit | |
97 | even if the buffer is not visiting a file. | |
98 | Automatically local in all buffers.") | |
99 | (make-variable-buffer-local 'buffer-offer-save) | |
100 | ||
f3e23606 RS |
101 | (defconst find-file-existing-other-name nil |
102 | "*Non-nil means find a file under alternative names, in existing buffers. | |
103 | This means if any existing buffer is visiting the file you want | |
104 | under another name, you get the existing buffer instead of a new buffer.") | |
105 | ||
106 | (defconst find-file-visit-truename nil | |
107 | "*Non-nil means visit a file under its truename. | |
108 | The truename of a file is found by chasing all links | |
109 | both at the file level and at the levels of the containing directories.") | |
110 | ||
111 | (defvar buffer-file-truename nil | |
112 | "The truename of the file visited in the current buffer. | |
113 | This variable is automatically local in all buffers, when non-nil.") | |
114 | (make-variable-buffer-local 'buffer-file-truename) | |
115 | (put 'buffer-file-truename 'permanent-local t) | |
116 | ||
117 | (defvar buffer-file-number nil | |
118 | "The device number and file number of the file visited in the current buffer. | |
119 | The value is a list of the form (FILENUM DEVNUM). | |
120 | This pair of numbers uniquely identifies the file. | |
121 | If the buffer is visiting a new file, the value is nil.") | |
122 | (make-variable-buffer-local 'buffer-file-number) | |
123 | (put 'buffer-file-number 'permanent-local t) | |
124 | ||
b4da00e9 RM |
125 | (defconst file-precious-flag nil |
126 | "*Non-nil means protect against I/O errors while saving files. | |
127 | Some modes set this non-nil in particular buffers.") | |
128 | ||
129 | (defvar version-control nil | |
130 | "*Control use of version numbers for backup files. | |
131 | t means make numeric backup versions unconditionally. | |
132 | nil means make them for files that have some already. | |
133 | never means do not make them.") | |
134 | ||
135 | (defvar dired-kept-versions 2 | |
136 | "*When cleaning directory, number of versions to keep.") | |
137 | ||
138 | (defvar trim-versions-without-asking nil | |
6d74b528 ER |
139 | "*If t, deletes excess backup versions silently. |
140 | If nil, asks confirmation. Any other value prevents any trimming.") | |
b4da00e9 RM |
141 | |
142 | (defvar kept-old-versions 2 | |
143 | "*Number of oldest versions to keep when a new numbered backup is made.") | |
144 | ||
145 | (defvar kept-new-versions 2 | |
146 | "*Number of newest versions to keep when a new numbered backup is made. | |
147 | Includes the new backup. Must be > 0") | |
148 | ||
149 | (defconst require-final-newline nil | |
150 | "*Value of t says silently ensure a file ends in a newline when it is saved. | |
151 | Non-nil but not t says ask user whether to add a newline when there isn't one. | |
152 | nil means don't add newlines.") | |
153 | ||
154 | (defconst auto-save-default t | |
155 | "*Non-nil says by default do auto-saving of every file-visiting buffer.") | |
156 | ||
157 | (defconst auto-save-visited-file-name nil | |
158 | "*Non-nil says auto-save a buffer in the file it is visiting, when practical. | |
159 | Normally auto-save files are written under other names.") | |
160 | ||
161 | (defconst save-abbrevs nil | |
162 | "*Non-nil means save word abbrevs too when files are saved. | |
163 | Loading an abbrev file sets this to t.") | |
164 | ||
165 | (defconst find-file-run-dired t | |
166 | "*Non-nil says run dired if find-file is given the name of a directory.") | |
167 | ||
92966e6f RS |
168 | ;;;It is not useful to make this a local variable. |
169 | ;;;(put 'find-file-not-found-hooks 'permanent-local t) | |
b4da00e9 RM |
170 | (defvar find-file-not-found-hooks nil |
171 | "List of functions to be called for `find-file' on nonexistent file. | |
172 | These functions are called as soon as the error is detected. | |
173 | `buffer-file-name' is already set up. | |
174 | The functions are called in the order given until one of them returns non-nil.") | |
175 | ||
92966e6f RS |
176 | ;;;It is not useful to make this a local variable. |
177 | ;;;(put 'find-file-hooks 'permanent-local t) | |
b4da00e9 RM |
178 | (defvar find-file-hooks nil |
179 | "List of functions to be called after a buffer is loaded from a file. | |
180 | The buffer's local variables (if any) will have been processed before the | |
181 | functions are called.") | |
182 | ||
c9dca4e0 | 183 | ;;; In case someone does make it local. |
b4da00e9 RM |
184 | (put 'write-file-hooks 'permanent-local t) |
185 | (defvar write-file-hooks nil | |
186 | "List of functions to be called before writing out a buffer to a file. | |
187 | If one of them returns non-nil, the file is considered already written | |
8c0e7b73 JB |
188 | and the rest are not called. |
189 | These hooks are considered to pertain to the visited file. | |
190 | So this list is cleared if you change the visited file name. | |
c9dca4e0 RS |
191 | See also `write-contents-hooks'. |
192 | Don't make this variable buffer-local; instead, use `local-write-file-hooks'.") | |
193 | ||
194 | (put 'local-write-file-hooks 'permanent-local t) | |
195 | (defvar local-write-file-hooks nil | |
196 | "Just like `write-file-hooks', except intended for per-buffer use. | |
197 | The functions in this list are called before the ones in | |
198 | `write-file-hooks'.") | |
8c0e7b73 JB |
199 | |
200 | (defvar write-contents-hooks nil | |
201 | "List of functions to be called before writing out a buffer to a file. | |
202 | If one of them returns non-nil, the file is considered already written | |
203 | and the rest are not called. | |
204 | These hooks are considered to pertain to the buffer's contents, | |
205 | not to the particular visited file; thus, `set-visited-file-name' does | |
206 | not clear this variable, but changing the major mode does clear it. | |
207 | See also `write-file-hooks'.") | |
b4da00e9 RM |
208 | |
209 | (defconst enable-local-variables t | |
210 | "*Control use of local-variables lists in files you visit. | |
211 | The value can be t, nil or something else. | |
212 | A value of t means local-variables lists are obeyed; | |
213 | nil means they are ignored; anything else means query. | |
214 | ||
215 | The command \\[normal-mode] always obeys local-variables lists | |
216 | and ignores this variable.") | |
217 | ||
2bba782c | 218 | (defconst enable-local-eval 'maybe |
d207b766 RS |
219 | "*Control processing of the \"variable\" `eval' in a file's local variables. |
220 | The value can be t, nil or something else. | |
221 | A value of t means obey `eval' variables; | |
222 | nil means ignore them; anything else means query. | |
223 | ||
224 | The command \\[normal-mode] always obeys local-variables lists | |
225 | and ignores this variable.") | |
b4da00e9 RM |
226 | |
227 | ;; Avoid losing in versions where CLASH_DETECTION is disabled. | |
228 | (or (fboundp 'lock-buffer) | |
229 | (fset 'lock-buffer 'ignore)) | |
230 | (or (fboundp 'unlock-buffer) | |
231 | (fset 'unlock-buffer 'ignore)) | |
232 | \f | |
233 | (defun pwd () | |
234 | "Show the current default directory." | |
235 | (interactive nil) | |
236 | (message "Directory %s" default-directory)) | |
237 | ||
238 | (defun cd (dir) | |
239 | "Make DIR become the current buffer's default directory." | |
240 | (interactive "DChange default directory: ") | |
241 | (setq dir (expand-file-name dir)) | |
242 | (if (not (eq system-type 'vax-vms)) | |
243 | (setq dir (file-name-as-directory dir))) | |
244 | (if (not (file-directory-p dir)) | |
245 | (error "%s is not a directory" dir) | |
246 | (if (file-executable-p dir) | |
247 | (setq default-directory dir) | |
248 | (error "Cannot cd to %s: Permission denied" dir))) | |
5bbbceb1 JB |
249 | ;; We used to call pwd at this point. That's not terribly helpful |
250 | ;; when we're invoking cd interactively, and the new cmushell-based | |
251 | ;; shell has its own (better) facilities for this. | |
252 | ) | |
b4da00e9 RM |
253 | |
254 | (defun load-file (file) | |
255 | "Load the Lisp file named FILE." | |
256 | (interactive "fLoad file: ") | |
257 | (load (expand-file-name file) nil nil t)) | |
258 | ||
259 | (defun load-library (library) | |
260 | "Load the library named LIBRARY. | |
261 | This is an interface to the function `load'." | |
262 | (interactive "sLoad library: ") | |
263 | (load library)) | |
5d68c2c2 RS |
264 | |
265 | ;; OTHER is the other file to be compared. | |
266 | (defun file-local-copy (file) | |
267 | "Copy the file FILE into a temporary file on this machine. | |
268 | Returns the name of the local copy, or nil, if FILE is directly | |
269 | accessible." | |
5dbfdacd | 270 | (let ((handler (find-file-name-handler file))) |
5d68c2c2 RS |
271 | (if handler |
272 | (funcall handler 'file-local-copy file) | |
273 | nil))) | |
f3e23606 RS |
274 | |
275 | (defun file-truename (filename) | |
276 | "Return the truename of FILENAME, which should be absolute. | |
277 | The truename of a file name is found by chasing symbolic links | |
278 | both at the level of the file and at the level of the directories | |
279 | containing it, until no links are left at any level." | |
6d1df4b2 JB |
280 | (if (string= filename "~") |
281 | (setq filename (expand-file-name filename))) | |
5dbfdacd | 282 | (let ((handler (find-file-name-handler filename))) |
6f176f94 RS |
283 | ;; For file name that has a special handler, call handler. |
284 | ;; This is so that ange-ftp can save time by doing a no-op. | |
285 | (if handler | |
286 | (funcall handler 'file-truename filename) | |
287 | (let ((dir (file-name-directory filename)) | |
288 | target dirfile) | |
289 | ;; Get the truename of the directory. | |
290 | (setq dirfile (directory-file-name dir)) | |
291 | ;; If these are equal, we have the (or a) root directory. | |
292 | (or (string= dir dirfile) | |
293 | (setq dir (file-name-as-directory (file-truename dirfile)))) | |
294 | ;; Put it back on the file name. | |
295 | (setq filename (concat dir (file-name-nondirectory filename))) | |
296 | ;; Is the file name the name of a link? | |
297 | (setq target (file-symlink-p filename)) | |
298 | (if target | |
299 | ;; Yes => chase that link, then start all over | |
300 | ;; since the link may point to a directory name that uses links. | |
301 | (file-truename (expand-file-name target dir)) | |
302 | ;; No, we are done! | |
303 | filename))))) | |
5dbfdacd | 304 | |
b4da00e9 RM |
305 | \f |
306 | (defun switch-to-buffer-other-window (buffer) | |
307 | "Select buffer BUFFER in another window." | |
308 | (interactive "BSwitch to buffer in other window: ") | |
309 | (let ((pop-up-windows t)) | |
310 | (pop-to-buffer buffer t))) | |
311 | ||
f98955ea JB |
312 | (defun switch-to-buffer-other-frame (buffer) |
313 | "Switch to buffer BUFFER in another frame." | |
314 | (interactive "BSwitch to buffer in other frame: ") | |
315 | (let ((pop-up-frames t)) | |
2be5e5db | 316 | (pop-to-buffer buffer t))) |
5bbbceb1 | 317 | |
b4da00e9 RM |
318 | (defun find-file (filename) |
319 | "Edit file FILENAME. | |
320 | Switch to a buffer visiting file FILENAME, | |
321 | creating one if none already exists." | |
322 | (interactive "FFind file: ") | |
323 | (switch-to-buffer (find-file-noselect filename))) | |
324 | ||
325 | (defun find-file-other-window (filename) | |
326 | "Edit file FILENAME, in another window. | |
327 | May create a new window, or reuse an existing one. | |
328 | See the function `display-buffer'." | |
329 | (interactive "FFind file in other window: ") | |
330 | (switch-to-buffer-other-window (find-file-noselect filename))) | |
331 | ||
f98955ea JB |
332 | (defun find-file-other-frame (filename) |
333 | "Edit file FILENAME, in another frame. | |
334 | May create a new frame, or reuse an existing one. | |
5bbbceb1 | 335 | See the function `display-buffer'." |
f98955ea JB |
336 | (interactive "FFind file in other frame: ") |
337 | (switch-to-buffer-other-frame (find-file-noselect filename))) | |
5bbbceb1 | 338 | |
b4da00e9 RM |
339 | (defun find-file-read-only (filename) |
340 | "Edit file FILENAME but don't allow changes. | |
341 | Like \\[find-file] but marks buffer as read-only. | |
342 | Use \\[toggle-read-only] to permit editing." | |
343 | (interactive "fFind file read-only: ") | |
344 | (find-file filename) | |
345 | (setq buffer-read-only t)) | |
346 | ||
347 | (defun find-file-read-only-other-window (filename) | |
348 | "Edit file FILENAME in another window but don't allow changes. | |
349 | Like \\[find-file-other-window] but marks buffer as read-only. | |
350 | Use \\[toggle-read-only] to permit editing." | |
351 | (interactive "fFind file read-only other window: ") | |
352 | (find-file filename) | |
353 | (setq buffer-read-only t)) | |
354 | ||
f98955ea JB |
355 | (defun find-file-read-only-other-frame (filename) |
356 | "Edit file FILENAME in another frame but don't allow changes. | |
357 | Like \\[find-file-other-frame] but marks buffer as read-only. | |
5bbbceb1 | 358 | Use \\[toggle-read-only] to permit editing." |
f98955ea JB |
359 | (interactive "fFind file read-only other frame: ") |
360 | (find-file-other-frame filename) | |
5bbbceb1 JB |
361 | (setq buffer-read-only t)) |
362 | ||
b4da00e9 RM |
363 | (defun find-alternate-file (filename) |
364 | "Find file FILENAME, select its buffer, kill previous buffer. | |
365 | If the current buffer now contains an empty file that you just visited | |
366 | \(presumably by mistake), use this command to visit the file you really want." | |
367 | (interactive | |
368 | (let ((file buffer-file-name) | |
369 | (file-name nil) | |
370 | (file-dir nil)) | |
371 | (and file | |
372 | (setq file-name (file-name-nondirectory file) | |
373 | file-dir (file-name-directory file))) | |
a61f59b4 JA |
374 | (list (read-file-name |
375 | "Find alternate file: " file-dir nil nil file-name)))) | |
b4da00e9 RM |
376 | (and (buffer-modified-p) |
377 | ;; (not buffer-read-only) | |
378 | (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " | |
379 | (buffer-name)))) | |
380 | (error "Aborted")) | |
381 | (let ((obuf (current-buffer)) | |
382 | (ofile buffer-file-name) | |
383 | (oname (buffer-name))) | |
384 | (rename-buffer " **lose**") | |
385 | (setq buffer-file-name nil) | |
386 | (unwind-protect | |
387 | (progn | |
388 | (unlock-buffer) | |
389 | (find-file filename)) | |
390 | (cond ((eq obuf (current-buffer)) | |
391 | (setq buffer-file-name ofile) | |
392 | (lock-buffer) | |
393 | (rename-buffer oname)))) | |
394 | (or (eq (current-buffer) obuf) | |
395 | (kill-buffer obuf)))) | |
396 | ||
397 | (defun create-file-buffer (filename) | |
398 | "Create a suitably named buffer for visiting FILENAME, and return it. | |
399 | FILENAME (sans directory) is used unchanged if that name is free; | |
400 | otherwise a string <2> or <3> or ... is appended to get an unused name." | |
401 | (let ((lastname (file-name-nondirectory filename))) | |
402 | (if (string= lastname "") | |
403 | (setq lastname filename)) | |
404 | (generate-new-buffer lastname))) | |
405 | ||
5bbbceb1 JB |
406 | (defun generate-new-buffer (name) |
407 | "Create and return a buffer with a name based on NAME. | |
29165787 | 408 | Choose the buffer's name using `generate-new-buffer-name'." |
5bbbceb1 JB |
409 | (get-buffer-create (generate-new-buffer-name name))) |
410 | ||
e373f201 JB |
411 | (defconst automount-dir-prefix "^/tmp_mnt/" |
412 | "Regexp to match the automounter prefix in a directory name.") | |
413 | ||
ffb3a4db RS |
414 | (defvar abbreviated-home-dir nil |
415 | "The the user's homedir abbreviated according to `directory-abbrev-list'.") | |
416 | ||
5bbbceb1 | 417 | (defun abbreviate-file-name (filename) |
29165787 | 418 | "Return a version of FILENAME shortened using `directory-abbrev-alist'. |
5bbbceb1 | 419 | This also substitutes \"~\" for the user's home directory. |
29165787 | 420 | Type \\[describe-variable] directory-abbrev-alist RET for more information." |
e373f201 JB |
421 | ;; Get rid of the prefixes added by the automounter. |
422 | (if (and (string-match automount-dir-prefix filename) | |
423 | (file-exists-p (file-name-directory | |
424 | (substring filename (1- (match-end 0)))))) | |
425 | (setq filename (substring filename (1- (match-end 0))))) | |
5bbbceb1 | 426 | (let ((tail directory-abbrev-alist)) |
ffb3a4db RS |
427 | ;; If any elt of directory-abbrev-alist matches this name, |
428 | ;; abbreviate accordingly. | |
5bbbceb1 JB |
429 | (while tail |
430 | (if (string-match (car (car tail)) filename) | |
431 | (setq filename | |
432 | (concat (cdr (car tail)) (substring filename (match-end 0))))) | |
433 | (setq tail (cdr tail))) | |
ffb3a4db RS |
434 | ;; Compute and save the abbreviated homedir name. |
435 | ;; We defer computing this until the first time it's needed, to | |
436 | ;; give time for directory-abbrev-alist to be set properly. | |
437 | (or abbreviated-home-dir | |
438 | (setq abbreviated-home-dir | |
439 | (let ((abbreviated-home-dir "$foo")) | |
440 | (concat "^" (abbreviate-file-name (expand-file-name "~")))))) | |
441 | ;; If FILENAME starts with the abbreviated homedir, | |
442 | ;; make it start with `~' instead. | |
443 | (if (string-match abbreviated-home-dir filename) | |
5bbbceb1 JB |
444 | (setq filename |
445 | (concat "~" (substring filename (match-end 0))))) | |
446 | filename)) | |
447 | ||
b4da00e9 RM |
448 | (defun find-file-noselect (filename &optional nowarn) |
449 | "Read file FILENAME into a buffer and return the buffer. | |
450 | If a buffer exists visiting FILENAME, return that one, but | |
451 | verify that the file has not changed since visited or saved. | |
452 | The buffer is not selected, just returned to the caller." | |
e373f201 JB |
453 | (setq filename |
454 | (abbreviate-file-name | |
455 | (expand-file-name filename))) | |
b4da00e9 RM |
456 | (if (file-directory-p filename) |
457 | (if find-file-run-dired | |
458 | (dired-noselect filename) | |
459 | (error "%s is a directory." filename)) | |
f3e23606 RS |
460 | (let* ((buf (get-file-buffer filename)) |
461 | (truename (abbreviate-file-name (file-truename filename))) | |
462 | (number (nthcdr 10 (file-attributes truename))) | |
463 | ;; Find any buffer for a file which has same truename. | |
464 | (same-truename | |
465 | (or buf ; Shortcut | |
466 | (let (found | |
467 | (list (buffer-list))) | |
468 | (while (and (not found) list) | |
469 | (save-excursion | |
470 | (set-buffer (car list)) | |
471 | (if (string= buffer-file-truename truename) | |
472 | (setq found (car list)))) | |
473 | (setq list (cdr list))) | |
474 | found))) | |
475 | (same-number | |
476 | (or buf ; Shortcut | |
477 | (and number | |
478 | (let (found | |
479 | (list (buffer-list))) | |
480 | (while (and (not found) list) | |
481 | (save-excursion | |
482 | (set-buffer (car list)) | |
483 | (if (equal buffer-file-number number) | |
484 | (setq found (car list)))) | |
485 | (setq list (cdr list))) | |
486 | found)))) | |
487 | error) | |
488 | ;; Let user know if there is a buffer with the same truename. | |
489 | (if (and (not buf) same-truename (not nowarn)) | |
490 | (message "%s and %s are the same file (%s)" | |
491 | filename (buffer-file-name same-truename) | |
492 | truename) | |
493 | (if (and (not buf) same-number (not nowarn)) | |
494 | (message "%s and %s are the same file" | |
495 | filename (buffer-file-name same-number)))) | |
496 | ||
497 | ;; Optionally also find that buffer. | |
498 | (if (or find-file-existing-other-name find-file-visit-truename) | |
499 | (setq buf (or same-truename same-number))) | |
b4da00e9 RM |
500 | (if buf |
501 | (or nowarn | |
502 | (verify-visited-file-modtime buf) | |
503 | (cond ((not (file-exists-p filename)) | |
504 | (error "File %s no longer exists!" filename)) | |
505 | ((yes-or-no-p | |
506 | (format | |
507 | (if (buffer-modified-p buf) | |
508 | "File %s changed on disk. Discard your edits? " | |
509 | "File %s changed on disk. Read the new version? ") | |
510 | (file-name-nondirectory filename))) | |
511 | (save-excursion | |
512 | (set-buffer buf) | |
513 | (revert-buffer t t))))) | |
514 | (save-excursion | |
f3e23606 RS |
515 | ;;; The truename stuff makes this obsolete. |
516 | ;;; (let* ((link-name (car (file-attributes filename))) | |
517 | ;;; (linked-buf (and (stringp link-name) | |
518 | ;;; (get-file-buffer link-name)))) | |
519 | ;;; (if (bufferp linked-buf) | |
520 | ;;; (message "Symbolic link to file in buffer %s" | |
521 | ;;; (buffer-name linked-buf)))) | |
b4da00e9 RM |
522 | (setq buf (create-file-buffer filename)) |
523 | (set-buffer buf) | |
524 | (erase-buffer) | |
525 | (condition-case () | |
526 | (insert-file-contents filename t) | |
527 | (file-error | |
528 | (setq error t) | |
529 | ;; Run find-file-not-found-hooks until one returns non-nil. | |
530 | (let ((hooks find-file-not-found-hooks)) | |
531 | (while (and hooks | |
532 | (not (funcall (car hooks)))) | |
533 | (setq hooks (cdr hooks)))))) | |
f3e23606 RS |
534 | ;; Find the file's truename, and maybe use that as visited name. |
535 | (setq buffer-file-truename (abbreviate-file-name truename)) | |
536 | (setq buffer-file-number number) | |
537 | (if find-file-visit-truename (setq filename buffer-file-truename)) | |
b4da00e9 RM |
538 | ;; Set buffer's default directory to that of the file. |
539 | (setq default-directory (file-name-directory filename)) | |
540 | ;; Turn off backup files for certain file names. Since | |
541 | ;; this is a permanent local, the major mode won't eliminate it. | |
542 | (and (not (funcall backup-enable-predicate buffer-file-name)) | |
543 | (progn | |
544 | (make-local-variable 'backup-inhibited) | |
545 | (setq backup-inhibited t))) | |
546 | (after-find-file error (not nowarn)))) | |
547 | buf))) | |
548 | \f | |
8cfb9d46 | 549 | (defun after-find-file (&optional error warn noauto) |
b4da00e9 RM |
550 | "Called after finding a file and by the default revert function. |
551 | Sets buffer mode, parses local variables. | |
8cfb9d46 | 552 | Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an |
b4da00e9 RM |
553 | error in reading the file. WARN non-nil means warn if there |
554 | exists an auto-save file more recent than the visited file. | |
8cfb9d46 | 555 | NOAUTO means don't mess with auto-save mode. |
b4da00e9 RM |
556 | Finishes by calling the functions in `find-file-hooks'." |
557 | (setq buffer-read-only (not (file-writable-p buffer-file-name))) | |
558 | (if noninteractive | |
559 | nil | |
560 | (let* (not-serious | |
561 | (msg | |
562 | (cond ((and error (file-attributes buffer-file-name)) | |
563 | (setq buffer-read-only t) | |
564 | "File exists, but is read-protected.") | |
565 | ((not buffer-read-only) | |
566 | (if (and warn | |
567 | (file-newer-than-file-p (make-auto-save-file-name) | |
568 | buffer-file-name)) | |
569 | "Auto save file is newer; consider M-x recover-file" | |
570 | (setq not-serious t) | |
571 | (if error "(New file)" nil))) | |
572 | ((not error) | |
573 | (setq not-serious t) | |
574 | "Note: file is write protected") | |
575 | ((file-attributes (directory-file-name default-directory)) | |
576 | "File not found and directory write-protected") | |
4e43240a RS |
577 | ((file-exists-p (file-name-directory buffer-file-name)) |
578 | (setq buffer-read-only nil)) | |
b4da00e9 | 579 | (t |
5bbbceb1 | 580 | (setq buffer-read-only nil) |
4e43240a RS |
581 | (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name)))) |
582 | "Use M-x make-dir RET RET to create the directory" | |
583 | "Use C-u M-x make-dir RET RET to create directory and its parents"))))) | |
b4da00e9 RM |
584 | (if msg |
585 | (progn | |
586 | (message msg) | |
587 | (or not-serious (sit-for 1 nil t))))) | |
8cfb9d46 | 588 | (if (and auto-save-default (not noauto)) |
b4da00e9 RM |
589 | (auto-save-mode t))) |
590 | (normal-mode t) | |
591 | (mapcar 'funcall find-file-hooks)) | |
592 | ||
593 | (defun normal-mode (&optional find-file) | |
594 | "Choose the major mode for this buffer automatically. | |
595 | Also sets up any specified local variables of the file. | |
596 | Uses the visited file name, the -*- line, and the local variables spec. | |
597 | ||
598 | This function is called automatically from `find-file'. In that case, | |
599 | we may set up specified local variables depending on the value of | |
600 | `enable-local-variables': if it is t, we do; if it is nil, we don't; | |
601 | otherwise, we query. `enable-local-variables' is ignored if you | |
602 | run `normal-mode' explicitly." | |
603 | (interactive) | |
604 | (or find-file (funcall (or default-major-mode 'fundamental-mode))) | |
605 | (condition-case err | |
606 | (set-auto-mode) | |
607 | (error (message "File mode specification error: %s" | |
608 | (prin1-to-string err)))) | |
609 | (condition-case err | |
7b3f3dc2 JB |
610 | (let ((enable-local-variables (or (not find-file) |
611 | enable-local-variables))) | |
612 | (hack-local-variables)) | |
b4da00e9 RM |
613 | (error (message "File local-variables error: %s" |
614 | (prin1-to-string err))))) | |
615 | ||
7b3f3dc2 JB |
616 | (defvar auto-mode-alist (mapcar 'purecopy |
617 | '(("\\.text\\'" . text-mode) | |
618 | ("\\.c\\'" . c-mode) | |
619 | ("\\.h\\'" . c-mode) | |
620 | ("\\.tex\\'" . TeX-mode) | |
621 | ("\\.ltx\\'" . LaTeX-mode) | |
622 | ("\\.el\\'" . emacs-lisp-mode) | |
623 | ("\\.mm\\'" . nroff-mode) | |
624 | ("\\.me\\'" . nroff-mode) | |
7b3f3dc2 JB |
625 | ("\\.scm\\'" . scheme-mode) |
626 | ("\\.l\\'" . lisp-mode) | |
627 | ("\\.lisp\\'" . lisp-mode) | |
628 | ("\\.f\\'" . fortran-mode) | |
629 | ("\\.for\\'" . fortran-mode) | |
630 | ("\\.mss\\'" . scribe-mode) | |
631 | ("\\.pl\\'" . prolog-mode) | |
632 | ("\\.cc\\'" . c++-mode) | |
633 | ("\\.C\\'" . c++-mode) | |
634 | ;;; Less common extensions come here | |
635 | ;;; so more common ones above are found faster. | |
636 | ("\\.s\\'" . asm-mode) | |
637 | ("ChangeLog\\'" . change-log-mode) | |
7adc58e5 | 638 | ("ChangeLog.[0-9]+\\'" . change-log-mode) |
7b3f3dc2 | 639 | ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode) |
7adc58e5 RS |
640 | ;; The following should come after the ChangeLog pattern |
641 | ;; for the sake of ChangeLog.1, etc. | |
642 | ("\\.[12345678]\\'" . nroff-mode) | |
7b3f3dc2 JB |
643 | ("\\.TeX\\'" . TeX-mode) |
644 | ("\\.sty\\'" . LaTeX-mode) | |
645 | ("\\.bbl\\'" . LaTeX-mode) | |
646 | ("\\.bib\\'" . bibtex-mode) | |
647 | ("\\.article\\'" . text-mode) | |
648 | ("\\.letter\\'" . text-mode) | |
649 | ("\\.texinfo\\'" . texinfo-mode) | |
fd5285f3 | 650 | ("\\.texi\\'" . texinfo-mode) |
7b3f3dc2 JB |
651 | ("\\.lsp\\'" . lisp-mode) |
652 | ("\\.awk\\'" . awk-mode) | |
653 | ("\\.prolog\\'" . prolog-mode) | |
654 | ;; Mailer puts message to be edited in | |
655 | ;; /tmp/Re.... or Message | |
656 | ("^/tmp/Re" . text-mode) | |
657 | ("/Message[0-9]*\\'" . text-mode) | |
658 | ;; some news reader is reported to use this | |
659 | ("^/tmp/fol/" . text-mode) | |
660 | ("\\.y\\'" . c-mode) | |
661 | ("\\.oak\\'" . scheme-mode) | |
662 | ("\\.scm.[0-9]*\\'" . scheme-mode) | |
663 | ;; .emacs following a directory delimiter | |
664 | ;; in either Unix or VMS syntax. | |
665 | ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode) | |
666 | ("\\.ml\\'" . lisp-mode))) | |
667 | "\ | |
668 | Alist of filename patterns vs corresponding major mode functions. | |
669 | Each element looks like (REGEXP . FUNCTION). | |
670 | Visiting a file whose name matches REGEXP causes FUNCTION to be called.") | |
671 | ||
b4da00e9 RM |
672 | (defun set-auto-mode () |
673 | "Select major mode appropriate for current buffer. | |
7b3f3dc2 JB |
674 | This checks for a -*- mode tag in the buffer's text, or |
675 | compares the filename against the entries in auto-mode-alist. It does | |
676 | not check for the \"mode:\" local variable in the Local Variables | |
677 | section of the file; for that, use `hack-local-variables'. | |
678 | ||
f3e23606 | 679 | If `enable-local-variables' is nil, this function does not check for a |
7b3f3dc2 | 680 | -*- mode tag." |
b4da00e9 RM |
681 | ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- |
682 | (let (beg end mode) | |
683 | (save-excursion | |
684 | (goto-char (point-min)) | |
685 | (skip-chars-forward " \t\n") | |
7b3f3dc2 JB |
686 | (if (and enable-local-variables |
687 | (search-forward "-*-" (save-excursion (end-of-line) (point)) t) | |
b4da00e9 RM |
688 | (progn |
689 | (skip-chars-forward " \t") | |
690 | (setq beg (point)) | |
7b3f3dc2 JB |
691 | (search-forward "-*-" |
692 | (save-excursion (end-of-line) (point)) | |
693 | t)) | |
b4da00e9 RM |
694 | (progn |
695 | (forward-char -3) | |
696 | (skip-chars-backward " \t") | |
697 | (setq end (point)) | |
698 | (goto-char beg) | |
699 | (if (search-forward ":" end t) | |
700 | (progn | |
701 | (goto-char beg) | |
702 | (if (let ((case-fold-search t)) | |
703 | (search-forward "mode:" end t)) | |
704 | (progn | |
705 | (skip-chars-forward " \t") | |
706 | (setq beg (point)) | |
707 | (if (search-forward ";" end t) | |
708 | (forward-char -1) | |
709 | (goto-char end)) | |
710 | (skip-chars-backward " \t") | |
711 | (setq mode (buffer-substring beg (point)))))) | |
712 | (setq mode (buffer-substring beg end))))) | |
713 | (setq mode (intern (concat (downcase mode) "-mode"))) | |
714 | (let ((alist auto-mode-alist) | |
715 | (name buffer-file-name)) | |
716 | (let ((case-fold-search (eq system-type 'vax-vms))) | |
717 | ;; Remove backup-suffixes from file name. | |
718 | (setq name (file-name-sans-versions name)) | |
719 | ;; Find first matching alist entry. | |
720 | (while (and (not mode) alist) | |
721 | (if (string-match (car (car alist)) name) | |
722 | (setq mode (cdr (car alist)))) | |
723 | (setq alist (cdr alist))))))) | |
724 | (if mode (funcall mode)))) | |
725 | ||
f3e23606 RS |
726 | (defun hack-local-variables-prop-line () |
727 | ;; Set local variables specified in the -*- line. | |
728 | ;; Returns t if mode was set. | |
729 | (save-excursion | |
730 | (goto-char (point-min)) | |
731 | (skip-chars-forward " \t\n\r") | |
732 | (let ((result '()) | |
733 | (end (save-excursion (end-of-line) (point))) | |
734 | mode-p) | |
735 | ;; Parse the -*- line into the `result' alist. | |
736 | (cond ((not (search-forward "-*-" end t)) | |
737 | ;; doesn't have one. | |
738 | nil) | |
739 | ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)") | |
740 | ;; Simple form: "-*- MODENAME -*-". | |
741 | (setq result | |
742 | (list (cons 'mode | |
743 | (intern (buffer-substring | |
744 | (match-beginning 1) | |
745 | (match-end 1))))))) | |
746 | (t | |
747 | ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' | |
748 | ;; (last ";" is optional). | |
749 | (save-excursion | |
750 | (if (search-forward "-*-" end t) | |
751 | (setq end (- (point) 3)) | |
752 | (error "-*- not terminated before end of line"))) | |
753 | (while (< (point) end) | |
754 | (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*") | |
755 | (error "malformed -*- line")) | |
756 | (goto-char (match-end 0)) | |
757 | (let ((key (intern (downcase (buffer-substring | |
758 | (match-beginning 1) | |
759 | (match-end 1))))) | |
760 | (val (save-restriction | |
761 | (narrow-to-region (point) end) | |
762 | (read (current-buffer))))) | |
763 | (setq result (cons (cons key val) result)) | |
764 | (skip-chars-forward " \t;"))) | |
765 | (setq result (nreverse result)))) | |
766 | ||
767 | ;; Mode is magic. | |
768 | (let (mode) | |
769 | (while (setq mode (assq 'mode result)) | |
770 | (setq mode-p t result (delq mode result)) | |
771 | (funcall (intern (concat (downcase (symbol-name (cdr mode))) | |
772 | "-mode"))))) | |
773 | ||
774 | (if (and result | |
775 | (or (eq enable-local-variables t) | |
776 | (and enable-local-variables | |
777 | (save-window-excursion | |
778 | (switch-to-buffer (current-buffer)) | |
779 | (y-or-n-p (format "Set local variables as specified in -*- line of %s? " | |
780 | (file-name-nondirectory buffer-file-name))))))) | |
781 | (while result | |
782 | (let ((key (car (car result))) | |
783 | (val (cdr (car result)))) | |
784 | ;; 'mode has already been removed from this list. | |
785 | (hack-one-local-variable key val)) | |
786 | (setq result (cdr result)))) | |
787 | mode-p))) | |
788 | ||
7b3f3dc2 | 789 | (defun hack-local-variables () |
5792c834 | 790 | "Parse and put into effect this buffer's local variables spec." |
f3e23606 | 791 | (hack-local-variables-prop-line) |
b4da00e9 RM |
792 | ;; Look for "Local variables:" line in last page. |
793 | (save-excursion | |
794 | (goto-char (point-max)) | |
795 | (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) | |
796 | (if (let ((case-fold-search t)) | |
797 | (and (search-forward "Local Variables:" nil t) | |
7b3f3dc2 | 798 | (or (eq enable-local-variables t) |
b4da00e9 RM |
799 | (and enable-local-variables |
800 | (save-window-excursion | |
801 | (switch-to-buffer (current-buffer)) | |
802 | (save-excursion | |
803 | (beginning-of-line) | |
804 | (set-window-start (selected-window) (point))) | |
805 | (y-or-n-p (format "Set local variables as specified at end of %s? " | |
806 | (file-name-nondirectory buffer-file-name)))))))) | |
807 | (let ((continue t) | |
2bba782c RS |
808 | prefix prefixlen suffix beg |
809 | (enable-local-eval enable-local-eval)) | |
b4da00e9 RM |
810 | ;; The prefix is what comes before "local variables:" in its line. |
811 | ;; The suffix is what comes after "local variables:" in its line. | |
812 | (skip-chars-forward " \t") | |
813 | (or (eolp) | |
814 | (setq suffix (buffer-substring (point) | |
815 | (progn (end-of-line) (point))))) | |
816 | (goto-char (match-beginning 0)) | |
817 | (or (bolp) | |
818 | (setq prefix | |
819 | (buffer-substring (point) | |
820 | (progn (beginning-of-line) (point))))) | |
7b3f3dc2 | 821 | |
b4da00e9 RM |
822 | (if prefix (setq prefixlen (length prefix) |
823 | prefix (regexp-quote prefix))) | |
824 | (if suffix (setq suffix (concat (regexp-quote suffix) "$"))) | |
825 | (while continue | |
826 | ;; Look at next local variable spec. | |
827 | (if selective-display (re-search-forward "[\n\C-m]") | |
828 | (forward-line 1)) | |
829 | ;; Skip the prefix, if any. | |
830 | (if prefix | |
831 | (if (looking-at prefix) | |
832 | (forward-char prefixlen) | |
833 | (error "Local variables entry is missing the prefix"))) | |
834 | ;; Find the variable name; strip whitespace. | |
835 | (skip-chars-forward " \t") | |
836 | (setq beg (point)) | |
837 | (skip-chars-forward "^:\n") | |
838 | (if (eolp) (error "Missing colon in local variables entry")) | |
839 | (skip-chars-backward " \t") | |
840 | (let* ((str (buffer-substring beg (point))) | |
841 | (var (read str)) | |
842 | val) | |
843 | ;; Setting variable named "end" means end of list. | |
844 | (if (string-equal (downcase str) "end") | |
845 | (setq continue nil) | |
846 | ;; Otherwise read the variable value. | |
847 | (skip-chars-forward "^:") | |
848 | (forward-char 1) | |
849 | (setq val (read (current-buffer))) | |
850 | (skip-chars-backward "\n") | |
851 | (skip-chars-forward " \t") | |
852 | (or (if suffix (looking-at suffix) (eolp)) | |
853 | (error "Local variables entry is terminated incorrectly")) | |
854 | ;; Set the variable. "Variables" mode and eval are funny. | |
f3e23606 RS |
855 | (hack-one-local-variable var val)))))))) |
856 | ||
857 | (defconst ignored-local-variables | |
858 | '(enable-local-eval) | |
859 | "Variables to be ignored in a file's local variable spec.") | |
860 | ||
861 | ;; "Set" one variable in a local variables spec. | |
862 | ;; A few variable names are treated specially. | |
863 | (defun hack-one-local-variable (var val) | |
864 | (cond ((eq var 'mode) | |
865 | (funcall (intern (concat (downcase (symbol-name val)) | |
866 | "-mode")))) | |
867 | ((memq var ignored-local-variables) | |
868 | nil) | |
869 | ;; "Setting" eval means either eval it or do nothing. | |
870 | ((eq var 'eval) | |
871 | (if (and (not (string= (user-login-name) "root")) | |
872 | (or (eq enable-local-eval t) | |
873 | (and enable-local-eval | |
874 | (save-window-excursion | |
875 | (switch-to-buffer (current-buffer)) | |
876 | (save-excursion | |
877 | (beginning-of-line) | |
878 | (set-window-start (selected-window) (point))) | |
879 | (setq enable-local-eval | |
880 | (y-or-n-p (format "Process `eval' local variable in file %s? " | |
881 | (file-name-nondirectory buffer-file-name)))))))) | |
882 | (save-excursion (eval val)) | |
883 | (message "Ignoring `eval:' in file's local variables"))) | |
884 | ;; Ordinary variable, really set it. | |
885 | (t (make-local-variable var) | |
886 | (set var val)))) | |
887 | ||
b4da00e9 RM |
888 | \f |
889 | (defun set-visited-file-name (filename) | |
890 | "Change name of file visited in current buffer to FILENAME. | |
891 | The next time the buffer is saved it will go in the newly specified file. | |
892 | nil or empty string as argument means make buffer not be visiting any file. | |
893 | Remember to delete the initial contents of the minibuffer | |
894 | if you wish to pass an empty string as the argument." | |
895 | (interactive "FSet visited file name: ") | |
896 | (if filename | |
897 | (setq filename | |
898 | (if (string-equal filename "") | |
899 | nil | |
900 | (expand-file-name filename)))) | |
901 | (or (equal filename buffer-file-name) | |
902 | (null filename) | |
903 | (progn | |
904 | (lock-buffer filename) | |
905 | (unlock-buffer))) | |
906 | (setq buffer-file-name filename) | |
907 | (if filename ; make buffer name reflect filename. | |
5bbbceb1 | 908 | (let ((new-name (file-name-nondirectory buffer-file-name))) |
b4da00e9 RM |
909 | (if (string= new-name "") |
910 | (error "Empty file name")) | |
911 | (if (eq system-type 'vax-vms) | |
912 | (setq new-name (downcase new-name))) | |
913 | (setq default-directory (file-name-directory buffer-file-name)) | |
5bbbceb1 | 914 | (rename-buffer new-name t))) |
b4da00e9 RM |
915 | (setq buffer-backed-up nil) |
916 | (clear-visited-file-modtime) | |
f3e23606 RS |
917 | (if filename |
918 | (progn | |
919 | (setq buffer-file-truename | |
920 | (abbreviate-file-name (file-truename buffer-file-name))) | |
921 | (if find-file-visit-truename | |
922 | (setq buffer-file-name buffer-file-truename)) | |
923 | (setq buffer-file-number (nth 10 (file-attributes buffer-file-name)))) | |
924 | (setq buffer-file-truename nil buffer-file-number nil)) | |
b4da00e9 RM |
925 | ;; write-file-hooks is normally used for things like ftp-find-file |
926 | ;; that visit things that are not local files as if they were files. | |
927 | ;; Changing to visit an ordinary local file instead should flush the hook. | |
928 | (kill-local-variable 'write-file-hooks) | |
c9dca4e0 | 929 | (kill-local-variable 'local-write-file-hooks) |
b4da00e9 RM |
930 | (kill-local-variable 'revert-buffer-function) |
931 | (kill-local-variable 'backup-inhibited) | |
932 | ;; Turn off backup files for certain file names. | |
933 | ;; Since this is a permanent local, the major mode won't eliminate it. | |
934 | (and (not (funcall backup-enable-predicate buffer-file-name)) | |
935 | (progn | |
936 | (make-local-variable 'backup-inhibited) | |
937 | (setq backup-inhibited t))) | |
938 | ;; If auto-save was not already on, turn it on if appropriate. | |
939 | (if (not buffer-auto-save-file-name) | |
6861ad22 RS |
940 | (auto-save-mode (and buffer-file-name auto-save-default)) |
941 | ;; If auto save is on, start using a new name. | |
942 | ;; We deliberately don't rename or delete the old auto save | |
943 | ;; for the old visited file name. This is because perhaps | |
944 | ;; the user wants to save the new state and then compare with the | |
945 | ;; previous state from the auto save file. | |
946 | (setq buffer-auto-save-file-name | |
947 | (make-auto-save-file-name))) | |
b4da00e9 RM |
948 | (if buffer-file-name |
949 | (set-buffer-modified-p t))) | |
950 | ||
951 | (defun write-file (filename) | |
952 | "Write current buffer into file FILENAME. | |
41f48cb1 RS |
953 | Makes buffer visit that file, and marks it not modified. |
954 | If the buffer is already visiting a file, you can specify | |
955 | a directory name as FILENAME, to write a file of the same | |
956 | old name in that directory." | |
b4da00e9 RM |
957 | ;; (interactive "FWrite file: ") |
958 | (interactive | |
959 | (list (if buffer-file-name | |
960 | (read-file-name "Write file: " | |
961 | nil nil nil nil) | |
962 | (read-file-name "Write file: " | |
963 | (cdr (assq 'default-directory | |
964 | (buffer-local-variables))) | |
965 | nil nil (buffer-name))))) | |
966 | (or (null filename) (string-equal filename "") | |
41f48cb1 RS |
967 | (progn |
968 | ;; If arg is just a directory, | |
969 | ;; use same file name, but in that directory. | |
970 | (if (and (file-directory-p filename) buffer-file-name) | |
971 | (setq filename (concat (file-name-as-directory filename) | |
972 | (file-name-nondirectory buffer-file-name)))) | |
973 | (set-visited-file-name filename))) | |
b4da00e9 RM |
974 | (set-buffer-modified-p t) |
975 | (save-buffer)) | |
976 | \f | |
977 | (defun backup-buffer () | |
978 | "Make a backup of the disk file visited by the current buffer, if appropriate. | |
979 | This is normally done before saving the buffer the first time. | |
980 | If the value is non-nil, it is the result of `file-modes' on the original | |
981 | file; this means that the caller, after saving the buffer, should change | |
982 | the modes of the new file to agree with the old modes." | |
983 | (if (and make-backup-files (not backup-inhibited) | |
984 | (not buffer-backed-up) | |
985 | (file-exists-p buffer-file-name) | |
986 | (memq (aref (elt (file-attributes buffer-file-name) 8) 0) | |
987 | '(?- ?l))) | |
988 | (let ((real-file-name buffer-file-name) | |
989 | backup-info backupname targets setmodes) | |
990 | ;; If specified name is a symbolic link, chase it to the target. | |
991 | ;; Thus we make the backups in the directory where the real file is. | |
992 | (while (let ((tem (file-symlink-p real-file-name))) | |
993 | (if tem | |
994 | (setq real-file-name | |
995 | (expand-file-name tem | |
996 | (file-name-directory real-file-name)))) | |
997 | tem)) | |
998 | (setq backup-info (find-backup-file-name real-file-name) | |
999 | backupname (car backup-info) | |
1000 | targets (cdr backup-info)) | |
1001 | ;;; (if (file-directory-p buffer-file-name) | |
1002 | ;;; (error "Cannot save buffer in directory %s" buffer-file-name)) | |
1003 | (condition-case () | |
1004 | (let ((delete-old-versions | |
1005 | ;; If have old versions to maybe delete, | |
1006 | ;; ask the user to confirm now, before doing anything. | |
1007 | ;; But don't actually delete til later. | |
1008 | (and targets | |
6d74b528 | 1009 | (or (eq trim-versions-without-asking t) (eq trim-versions-without-asking nil)) |
b4da00e9 RM |
1010 | (or trim-versions-without-asking |
1011 | (y-or-n-p (format "Delete excess backup versions of %s? " | |
1012 | real-file-name)))))) | |
1013 | ;; Actually write the back up file. | |
1014 | (condition-case () | |
1015 | (if (or file-precious-flag | |
1016 | ; (file-symlink-p buffer-file-name) | |
1017 | backup-by-copying | |
1018 | (and backup-by-copying-when-linked | |
1019 | (> (file-nlinks real-file-name) 1)) | |
1020 | (and backup-by-copying-when-mismatch | |
1021 | (let ((attr (file-attributes real-file-name))) | |
1022 | (or (nth 9 attr) | |
1023 | (/= (nth 2 attr) (user-uid)))))) | |
65864ae0 RS |
1024 | (condition-case () |
1025 | (copy-file real-file-name backupname t t) | |
1026 | (file-error | |
1027 | ;; If copying fails because file BACKUPNAME | |
1028 | ;; is not writable, delete that file and try again. | |
1029 | (if (and (file-exists-p backupname) | |
1030 | (not (file-writable-p backupname))) | |
1031 | (delete-file backupname)) | |
1032 | (copy-file real-file-name backupname t t))) | |
1033 | ;; rename-file should delete old backup. | |
b4da00e9 RM |
1034 | (rename-file real-file-name backupname t) |
1035 | (setq setmodes (file-modes backupname))) | |
1036 | (file-error | |
1037 | ;; If trouble writing the backup, write it in ~. | |
1038 | (setq backupname (expand-file-name "~/%backup%~")) | |
1039 | (message "Cannot write backup file; backing up in ~/%%backup%%~") | |
1040 | (sleep-for 1) | |
65864ae0 RS |
1041 | (condition-case () |
1042 | (copy-file real-file-name backupname t t) | |
1043 | (file-error | |
1044 | ;; If copying fails because file BACKUPNAME | |
1045 | ;; is not writable, delete that file and try again. | |
1046 | (if (and (file-exists-p backupname) | |
1047 | (not (file-writable-p backupname))) | |
1048 | (delete-file backupname)) | |
1049 | (copy-file real-file-name backupname t t))))) | |
b4da00e9 RM |
1050 | (setq buffer-backed-up t) |
1051 | ;; Now delete the old versions, if desired. | |
1052 | (if delete-old-versions | |
1053 | (while targets | |
1054 | (condition-case () | |
1055 | (delete-file (car targets)) | |
1056 | (file-error nil)) | |
1057 | (setq targets (cdr targets)))) | |
1058 | setmodes) | |
1059 | (file-error nil))))) | |
1060 | ||
c3554e95 | 1061 | (defun file-name-sans-versions (name &optional keep-backup-version) |
b4da00e9 RM |
1062 | "Return FILENAME sans backup versions or strings. |
1063 | This is a separate procedure so your site-init or startup file can | |
c3554e95 RS |
1064 | redefine it. |
1065 | If the optional argument KEEP-BACKUP-VERSION is non-nil, | |
1066 | we do not remove backup version numbers, only true file version numbers." | |
5dbfdacd | 1067 | (let ((handler (find-file-name-handler name))) |
c3554e95 RS |
1068 | (if handler |
1069 | (funcall handler 'file-name-sans-versions name keep-backup-version) | |
1070 | (substring name 0 | |
1071 | (if (eq system-type 'vax-vms) | |
1072 | ;; VMS version number is (a) semicolon, optional | |
1073 | ;; sign, zero or more digits or (b) period, option | |
1074 | ;; sign, zero or more digits, provided this is the | |
1075 | ;; second period encountered outside of the | |
1076 | ;; device/directory part of the file name. | |
1077 | (or (string-match ";[---+]?[0-9]*\\'" name) | |
1078 | (if (string-match "\\.[^]>:]*\\(\\.[---+]?[0-9]*\\)\\'" | |
1079 | name) | |
1080 | (match-beginning 1)) | |
1081 | (length name)) | |
1082 | (if keep-backup-version | |
1083 | (length name) | |
1084 | (or (string-match "\\.~[0-9]+~\\'" name) | |
1085 | (string-match "~\\'" name) | |
1086 | (length name)))))))) | |
b4da00e9 RM |
1087 | |
1088 | (defun make-backup-file-name (file) | |
1089 | "Create the non-numeric backup file name for FILE. | |
1090 | This is a separate function so you can redefine it for customization." | |
1091 | (concat file "~")) | |
1092 | ||
1093 | (defun backup-file-name-p (file) | |
1094 | "Return non-nil if FILE is a backup file name (numeric or not). | |
1095 | This is a separate function so you can redefine it for customization. | |
1096 | You may need to redefine `file-name-sans-versions' as well." | |
1097 | (string-match "~$" file)) | |
1098 | ||
2d051399 RS |
1099 | ;; This is used in various files. |
1100 | ;; The usage of bv-length is not very clean, | |
1101 | ;; but I can't see a good alternative, | |
1102 | ;; so as of now I am leaving it alone. | |
1103 | (defun backup-extract-version (fn) | |
1104 | "Given the name of a numeric backup file, return the backup number. | |
1105 | Uses the free variable `bv-length', whose value should be | |
1106 | the index in the name where the version number begins." | |
1107 | (if (and (string-match "[0-9]+~$" fn bv-length) | |
1108 | (= (match-beginning 0) bv-length)) | |
1109 | (string-to-int (substring fn bv-length -1)) | |
1110 | 0)) | |
1111 | ||
b4da00e9 RM |
1112 | ;; I believe there is no need to alter this behavior for VMS; |
1113 | ;; since backup files are not made on VMS, it should not get called. | |
1114 | (defun find-backup-file-name (fn) | |
1115 | "Find a file name for a backup file, and suggestions for deletions. | |
1116 | Value is a list whose car is the name for the backup file | |
1117 | and whose cdr is a list of old versions to consider deleting now." | |
1118 | (if (eq version-control 'never) | |
1119 | (list (make-backup-file-name fn)) | |
1120 | (let* ((base-versions (concat (file-name-nondirectory fn) ".~")) | |
1121 | (bv-length (length base-versions)) | |
1122 | (possibilities (file-name-all-completions | |
1123 | base-versions | |
1124 | (file-name-directory fn))) | |
79058860 | 1125 | (versions (sort (mapcar |
2d051399 | 1126 | (function backup-extract-version) |
79058860 | 1127 | possibilities) |
b4da00e9 | 1128 | '<)) |
5bbbceb1 | 1129 | (high-water-mark (apply 'max 0 versions)) |
b4da00e9 RM |
1130 | (deserve-versions-p |
1131 | (or version-control | |
1132 | (> high-water-mark 0))) | |
1133 | (number-to-delete (- (length versions) | |
1134 | kept-old-versions kept-new-versions -1))) | |
1135 | (if (not deserve-versions-p) | |
1136 | (list (make-backup-file-name fn)) | |
1137 | (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~") | |
1138 | (if (> number-to-delete 0) | |
1139 | (mapcar (function (lambda (n) | |
1140 | (concat fn ".~" (int-to-string n) "~"))) | |
1141 | (let ((v (nthcdr kept-old-versions versions))) | |
1142 | (rplacd (nthcdr (1- number-to-delete) v) ()) | |
1143 | v)))))))) | |
1144 | ||
b4da00e9 RM |
1145 | (defun file-nlinks (filename) |
1146 | "Return number of names file FILENAME has." | |
1147 | (car (cdr (file-attributes filename)))) | |
6c636af9 | 1148 | |
46fa3073 RS |
1149 | (defun file-relative-name-1 (directory) |
1150 | (cond ((string= directory "/") | |
1151 | filename) | |
1152 | ((string-match (concat "^" (regexp-quote directory)) | |
1153 | filename) | |
1154 | (substring filename (match-end 0))) | |
1155 | (t | |
1156 | (file-relative-name-1 | |
1157 | (file-name-directory (substring directory 0 -1)))))) | |
1158 | ||
6c636af9 RM |
1159 | (defun file-relative-name (filename &optional directory) |
1160 | "Convert FILENAME to be relative to DIRECTORY (default: default-directory)." | |
1161 | (setq filename (expand-file-name filename) | |
ea912aa6 RM |
1162 | directory (file-name-as-directory (if directory |
1163 | (expand-file-name directory) | |
6c636af9 | 1164 | default-directory))) |
46fa3073 | 1165 | (file-relative-name-1 directory)) |
b4da00e9 RM |
1166 | \f |
1167 | (defun save-buffer (&optional args) | |
1168 | "Save current buffer in visited file if modified. Versions described below. | |
1169 | By default, makes the previous version into a backup file | |
1170 | if previously requested or if this is the first save. | |
1171 | With 1 or 3 \\[universal-argument]'s, marks this version | |
1172 | to become a backup when the next save is done. | |
1173 | With 2 or 3 \\[universal-argument]'s, | |
1174 | unconditionally makes the previous version into a backup file. | |
1175 | With argument of 0, never makes the previous version into a backup file. | |
1176 | ||
1177 | If a file's name is FOO, the names of its numbered backup versions are | |
1178 | FOO.~i~ for various integers i. A non-numbered backup file is called FOO~. | |
1179 | Numeric backups (rather than FOO~) will be made if value of | |
1180 | `version-control' is not the atom `never' and either there are already | |
1181 | numeric versions of the file being backed up, or `version-control' is | |
1182 | non-nil. | |
1183 | We don't want excessive versions piling up, so there are variables | |
1184 | `kept-old-versions', which tells Emacs how many oldest versions to keep, | |
1185 | and `kept-new-versions', which tells how many newest versions to keep. | |
1186 | Defaults are 2 old versions and 2 new. | |
1187 | `dired-kept-versions' controls dired's clean-directory (.) command. | |
1188 | If `trim-versions-without-asking' is nil, system will query user | |
1189 | before trimming versions. Otherwise it does it silently." | |
1190 | (interactive "p") | |
1191 | (let ((modp (buffer-modified-p)) | |
1192 | (large (> (buffer-size) 50000)) | |
1193 | (make-backup-files (and make-backup-files (not (eq args 0))))) | |
1194 | (and modp (memq args '(16 64)) (setq buffer-backed-up nil)) | |
1195 | (if (and modp large) (message "Saving file %s..." (buffer-file-name))) | |
1196 | (basic-save-buffer) | |
1197 | (and modp (memq args '(4 64)) (setq buffer-backed-up nil)))) | |
1198 | ||
1199 | (defun delete-auto-save-file-if-necessary (&optional force) | |
1200 | "Delete auto-save file for current buffer if `delete-auto-save-files' is t. | |
1201 | Normally delete only if the file was written by this Emacs since | |
1202 | the last real save, but optional arg FORCE non-nil means delete anyway." | |
1203 | (and buffer-auto-save-file-name delete-auto-save-files | |
1204 | (not (string= buffer-file-name buffer-auto-save-file-name)) | |
1205 | (or force (recent-auto-save-p)) | |
1206 | (progn | |
1207 | (condition-case () | |
1208 | (delete-file buffer-auto-save-file-name) | |
1209 | (file-error nil)) | |
1210 | (set-buffer-auto-saved)))) | |
1211 | ||
1212 | (defun basic-save-buffer () | |
1213 | "Save the current buffer in its visited file, if it has been modified." | |
1214 | (interactive) | |
1215 | (if (buffer-modified-p) | |
1216 | (let ((recent-save (recent-auto-save-p)) | |
1217 | setmodes tempsetmodes) | |
1218 | ;; On VMS, rename file and buffer to get rid of version number. | |
1219 | (if (and (eq system-type 'vax-vms) | |
1220 | (not (string= buffer-file-name | |
1221 | (file-name-sans-versions buffer-file-name)))) | |
1222 | (let (buffer-new-name) | |
1223 | ;; Strip VMS version number before save. | |
1224 | (setq buffer-file-name | |
1225 | (file-name-sans-versions buffer-file-name)) | |
1226 | ;; Construct a (unique) buffer name to correspond. | |
1227 | (let ((buf (create-file-buffer (downcase buffer-file-name)))) | |
1228 | (setq buffer-new-name (buffer-name buf)) | |
1229 | (kill-buffer buf)) | |
1230 | (rename-buffer buffer-new-name))) | |
1231 | ;; If buffer has no file name, ask user for one. | |
1232 | (or buffer-file-name | |
1233 | (progn | |
1234 | (setq buffer-file-name | |
1235 | (expand-file-name (read-file-name "File to save in: ") nil) | |
1236 | default-directory (file-name-directory buffer-file-name)) | |
1237 | (auto-save-mode auto-save-default))) | |
1238 | (or (verify-visited-file-modtime (current-buffer)) | |
1239 | (not (file-exists-p buffer-file-name)) | |
1240 | (yes-or-no-p | |
1241 | (format "%s has changed since visited or saved. Save anyway? " | |
1242 | (file-name-nondirectory buffer-file-name))) | |
1243 | (error "Save not confirmed")) | |
1244 | (save-restriction | |
1245 | (widen) | |
1246 | (and (> (point-max) 1) | |
1247 | (/= (char-after (1- (point-max))) ?\n) | |
1248 | (or (eq require-final-newline t) | |
1249 | (and require-final-newline | |
1250 | (y-or-n-p | |
1251 | (format "Buffer %s does not end in newline. Add one? " | |
1252 | (buffer-name))))) | |
1253 | (save-excursion | |
1254 | (goto-char (point-max)) | |
1255 | (insert ?\n))) | |
c9dca4e0 RS |
1256 | (let ((hooks (append write-contents-hooks local-write-file-hooks |
1257 | write-file-hooks)) | |
b4da00e9 RM |
1258 | (done nil)) |
1259 | (while (and hooks | |
1260 | (not (setq done (funcall (car hooks))))) | |
1261 | (setq hooks (cdr hooks))) | |
1262 | ;; If a hook returned t, file is already "written". | |
1263 | (cond ((not done) | |
1264 | (if (not (file-writable-p buffer-file-name)) | |
1265 | (let ((dir (file-name-directory buffer-file-name))) | |
1266 | (if (not (file-directory-p dir)) | |
1267 | (error "%s is not a directory" dir) | |
1268 | (if (not (file-exists-p buffer-file-name)) | |
1269 | (error "Directory %s write-protected" dir) | |
1270 | (if (yes-or-no-p | |
1271 | (format "File %s is write-protected; try to save anyway? " | |
1272 | (file-name-nondirectory | |
1273 | buffer-file-name))) | |
1274 | (setq tempsetmodes t) | |
1275 | (error "Attempt to save to a file which you aren't allowed to write")))))) | |
1276 | (or buffer-backed-up | |
1277 | (setq setmodes (backup-buffer))) | |
1278 | (if file-precious-flag | |
f3e23606 RS |
1279 | ;; If file is precious, write temp name, then rename it. |
1280 | (let ((dir (file-name-directory buffer-file-name)) | |
1281 | (realname buffer-file-name) | |
1282 | tempname temp nogood i succeed) | |
1283 | (setq i 0) | |
1284 | (setq nogood t) | |
1285 | ;; Find the temporary name to write under. | |
1286 | (while nogood | |
1287 | (setq tempname (format "%s#tmp#%d" dir i)) | |
1288 | (setq nogood (file-exists-p tempname)) | |
1289 | (setq i (1+ i))) | |
b4da00e9 RM |
1290 | (unwind-protect |
1291 | (progn (clear-visited-file-modtime) | |
1292 | (write-region (point-min) (point-max) | |
f3e23606 RS |
1293 | tempname nil realname) |
1294 | (setq succeed t)) | |
1295 | ;; If writing the temp file fails, | |
1296 | ;; delete the temp file. | |
1297 | (or succeed (delete-file tempname))) | |
b411d947 JB |
1298 | ;; Since we have created an entirely new file |
1299 | ;; and renamed it, make sure it gets the | |
1300 | ;; right permission bits set. | |
1301 | (setq setmodes (file-modes buffer-file-name)) | |
f3e23606 RS |
1302 | ;; We succeeded in writing the temp file, |
1303 | ;; so rename it. | |
1304 | (rename-file tempname buffer-file-name t)) | |
b4da00e9 RM |
1305 | ;; If file not writable, see if we can make it writable |
1306 | ;; temporarily while we write it. | |
1307 | ;; But no need to do so if we have just backed it up | |
1308 | ;; (setmodes is set) because that says we're superseding. | |
1309 | (cond ((and tempsetmodes (not setmodes)) | |
1310 | ;; Change the mode back, after writing. | |
1311 | (setq setmodes (file-modes buffer-file-name)) | |
1312 | (set-file-modes buffer-file-name 511))) | |
1313 | (write-region (point-min) (point-max) | |
1314 | buffer-file-name nil t))))) | |
f3e23606 | 1315 | (setq buffer-file-number (nth 10 (file-attributes buffer-file-name))) |
b4da00e9 RM |
1316 | (if setmodes |
1317 | (condition-case () | |
f3e23606 | 1318 | (set-file-modes buffer-file-name setmodes) |
b4da00e9 RM |
1319 | (error nil)))) |
1320 | ;; If the auto-save file was recent before this command, | |
1321 | ;; delete it now. | |
1322 | (delete-auto-save-file-if-necessary recent-save) | |
1323 | (run-hooks 'after-save-hooks)) | |
1324 | (message "(No changes need to be saved)"))) | |
1325 | ||
b4da00e9 RM |
1326 | (defun save-some-buffers (&optional arg exiting) |
1327 | "Save some modified file-visiting buffers. Asks user about each one. | |
5bbbceb1 JB |
1328 | Optional argument (the prefix) non-nil means save all with no questions. |
1329 | Optional second argument EXITING means ask about certain non-file buffers | |
1330 | as well as about file buffers." | |
b4da00e9 | 1331 | (interactive "P") |
907482b9 | 1332 | (save-window-excursion |
b5e86cb3 RM |
1333 | (if (zerop (map-y-or-n-p |
1334 | (function | |
1335 | (lambda (buffer) | |
1336 | (and (buffer-modified-p buffer) | |
1337 | (or | |
1338 | (buffer-file-name buffer) | |
1339 | (and exiting | |
1340 | (progn | |
1341 | (set-buffer buffer) | |
1342 | (and buffer-offer-save (> (buffer-size) 0))))) | |
1343 | (if arg | |
1344 | t | |
1345 | (if (buffer-file-name buffer) | |
1346 | (format "Save file %s? " | |
1347 | (buffer-file-name buffer)) | |
1348 | (format "Save buffer %s? " | |
1349 | (buffer-name buffer))))))) | |
1350 | (function | |
1351 | (lambda (buffer) | |
e065a56e JB |
1352 | (set-buffer buffer) |
1353 | (save-buffer))) | |
b5e86cb3 | 1354 | (buffer-list) |
907482b9 | 1355 | '("buffer" "buffers" "save") |
df01170b RS |
1356 | (list (list ?\C-r (lambda (buf) |
1357 | (view-buffer buf) | |
1586b965 RS |
1358 | (setq view-exit-action |
1359 | '(lambda (ignore) | |
1360 | (exit-recursive-edit))) | |
df01170b RS |
1361 | (recursive-edit) |
1362 | ;; Return nil to ask about BUF again. | |
1363 | nil) | |
907482b9 RM |
1364 | "display the current buffer")) |
1365 | )) | |
b5e86cb3 | 1366 | (message "(No files need saving)")))) |
b4da00e9 RM |
1367 | \f |
1368 | (defun not-modified (&optional arg) | |
1369 | "Mark current buffer as unmodified, not needing to be saved. | |
1370 | With prefix arg, mark buffer as modified, so \\[save-buffer] will save." | |
1371 | (interactive "P") | |
1372 | (message (if arg "Modification-flag set" | |
1373 | "Modification-flag cleared")) | |
1374 | (set-buffer-modified-p arg)) | |
1375 | ||
1376 | (defun toggle-read-only (&optional arg) | |
1377 | "Change whether this buffer is visiting its file read-only. | |
1378 | With arg, set read-only iff arg is positive." | |
1379 | (interactive "P") | |
1380 | (setq buffer-read-only | |
1381 | (if (null arg) | |
1382 | (not buffer-read-only) | |
1383 | (> (prefix-numeric-value arg) 0))) | |
1384 | ;; Force mode-line redisplay | |
1385 | (set-buffer-modified-p (buffer-modified-p))) | |
1386 | ||
1387 | (defun insert-file (filename) | |
1388 | "Insert contents of file FILENAME into buffer after point. | |
1389 | Set mark after the inserted text. | |
1390 | ||
1391 | This function is meant for the user to run interactively. | |
1392 | Don't call it from programs! Use `insert-file-contents' instead. | |
1393 | \(Its calling sequence is different; see its documentation)." | |
1394 | (interactive "fInsert file: ") | |
1395 | (let ((tem (insert-file-contents filename))) | |
1396 | (push-mark (+ (point) (car (cdr tem)))))) | |
1397 | ||
1398 | (defun append-to-file (start end filename) | |
1399 | "Append the contents of the region to the end of file FILENAME. | |
1400 | When called from a function, expects three arguments, | |
1401 | START, END and FILENAME. START and END are buffer positions | |
1402 | saying what text to write." | |
1403 | (interactive "r\nFAppend to file: ") | |
1404 | (write-region start end filename t)) | |
1405 | ||
1406 | (defun file-newest-backup (filename) | |
1407 | "Return most recent backup file for FILENAME or nil if no backups exist." | |
1408 | (let* ((filename (expand-file-name filename)) | |
1409 | (file (file-name-nondirectory filename)) | |
1410 | (dir (file-name-directory filename)) | |
1411 | (comp (file-name-all-completions file dir)) | |
1412 | newest) | |
1413 | (while comp | |
1414 | (setq file (concat dir (car comp)) | |
1415 | comp (cdr comp)) | |
1416 | (if (and (backup-file-name-p file) | |
1417 | (or (null newest) (file-newer-than-file-p file newest))) | |
1418 | (setq newest file))) | |
1419 | newest)) | |
1420 | ||
1421 | (defun rename-uniquely () | |
1422 | "Rename current buffer to a similar name not already taken. | |
1423 | This function is useful for creating multiple shell process buffers | |
1424 | or multiple mail buffers, etc." | |
1425 | (interactive) | |
1426 | (let* ((new-buf (generate-new-buffer (buffer-name))) | |
1427 | (name (buffer-name new-buf))) | |
1428 | (kill-buffer new-buf) | |
1429 | (rename-buffer name) | |
1430 | (set-buffer-modified-p (buffer-modified-p)))) ; force mode line update | |
5bbbceb1 | 1431 | |
4e43240a RS |
1432 | (defun make-directory (dir &optional parents) |
1433 | "Create the directory DIR and any nonexistent parent dirs." | |
1434 | (interactive "FMake directory: \nP") | |
5dbfdacd | 1435 | (let ((handler (find-file-name-handler dir))) |
4e43240a RS |
1436 | (if handler |
1437 | (funcall handler 'make-directory dir parents) | |
1438 | (if (not parents) | |
1439 | (make-directory-internal dir) | |
1440 | (let ((dir (directory-file-name (expand-file-name dir))) | |
1441 | create-list) | |
1442 | (while (not (file-exists-p dir)) | |
1443 | (setq create-list (cons dir create-list) | |
1444 | dir (directory-file-name (file-name-directory dir)))) | |
1445 | (while create-list | |
1446 | (make-directory-internal (car create-list)) | |
1447 | (setq create-list (cdr create-list)))))))) | |
b4da00e9 RM |
1448 | \f |
1449 | (put 'revert-buffer-function 'permanent-local t) | |
1450 | (defvar revert-buffer-function nil | |
1451 | "Function to use to revert this buffer, or nil to do the default.") | |
1452 | ||
1453 | (put 'revert-buffer-insert-file-contents-function 'permanent-local t) | |
1454 | (defvar revert-buffer-insert-file-contents-function nil | |
1455 | "Function to use to insert contents when reverting this buffer. | |
1456 | Gets two args, first the nominal file name to use, | |
1457 | and second, t if reading the auto-save file.") | |
1458 | ||
1ab31687 | 1459 | (defun revert-buffer (&optional ignore-auto noconfirm) |
b4da00e9 RM |
1460 | "Replace the buffer text with the text of the visited file on disk. |
1461 | This undoes all changes since the file was visited or saved. | |
8c0e7b73 JB |
1462 | With a prefix argument, offer to revert from latest auto-save file, if |
1463 | that is more recent than the visited file. | |
1ab31687 JB |
1464 | |
1465 | When called from lisp, the first argument is IGNORE-AUTO; only offer | |
1466 | to revert from the auto-save file when this is nil. Note that the | |
1467 | sense of this argument is the reverse of the prefix argument, for the | |
1468 | sake of backward compatibility. IGNORE-AUTO is optional, defaulting | |
1469 | to nil. | |
1470 | ||
1471 | Optional second argument NOCONFIRM means don't ask for confirmation at | |
1472 | all. | |
b4da00e9 | 1473 | |
8c0e7b73 JB |
1474 | If the value of `revert-buffer-function' is non-nil, it is called to |
1475 | do the work." | |
1ab31687 JB |
1476 | ;; I admit it's odd to reverse the sense of the prefix argument, but |
1477 | ;; there is a lot of code out there which assumes that the first | |
1478 | ;; argument should be t to avoid consulting the auto-save file, and | |
1479 | ;; there's no straightforward way to encourage authors to notice a | |
1480 | ;; reversal of the argument sense. So I'm just changing the user | |
1481 | ;; interface, but leaving the programmatic interface the same. | |
1482 | (interactive (list (not prefix-arg))) | |
b4da00e9 | 1483 | (if revert-buffer-function |
1ab31687 | 1484 | (funcall revert-buffer-function ignore-auto noconfirm) |
b4da00e9 | 1485 | (let* ((opoint (point)) |
1ab31687 JB |
1486 | (auto-save-p (and (not ignore-auto) |
1487 | (recent-auto-save-p) | |
b4da00e9 RM |
1488 | buffer-auto-save-file-name |
1489 | (file-readable-p buffer-auto-save-file-name) | |
1490 | (y-or-n-p | |
1491 | "Buffer has been auto-saved recently. Revert from auto-save file? "))) | |
1492 | (file-name (if auto-save-p | |
1493 | buffer-auto-save-file-name | |
1494 | buffer-file-name))) | |
1495 | (cond ((null file-name) | |
1496 | (error "Buffer does not seem to be associated with any file")) | |
1497 | ((or noconfirm | |
1498 | (yes-or-no-p (format "Revert buffer from file %s? " | |
1499 | file-name))) | |
1500 | ;; If file was backed up but has changed since, | |
1501 | ;; we shd make another backup. | |
1502 | (and (not auto-save-p) | |
5bbbceb1 | 1503 | (not (verify-visited-file-modtime (current-buffer))) |
b4da00e9 RM |
1504 | (setq buffer-backed-up nil)) |
1505 | ;; Get rid of all undo records for this buffer. | |
1506 | (or (eq buffer-undo-list t) | |
1507 | (setq buffer-undo-list nil)) | |
1508 | (let ((buffer-read-only nil) | |
1509 | ;; Don't make undo records for the reversion. | |
1510 | (buffer-undo-list t)) | |
1511 | (if revert-buffer-insert-file-contents-function | |
1512 | (funcall revert-buffer-insert-file-contents-function | |
1513 | file-name auto-save-p) | |
1514 | (if (not (file-exists-p file-name)) | |
1515 | (error "File %s no longer exists!" file-name)) | |
1516 | ;; Bind buffer-file-name to nil | |
1517 | ;; so that we don't try to lock the file. | |
1518 | (let ((buffer-file-name nil)) | |
1519 | (or auto-save-p | |
1520 | (unlock-buffer)) | |
1521 | (erase-buffer)) | |
1522 | (insert-file-contents file-name (not auto-save-p)))) | |
1523 | (goto-char (min opoint (point-max))) | |
8cfb9d46 | 1524 | (after-find-file nil nil t) |
b4da00e9 RM |
1525 | t))))) |
1526 | ||
1527 | (defun recover-file (file) | |
1528 | "Visit file FILE, but get contents from its last auto-save file." | |
1529 | (interactive | |
1530 | (let ((prompt-file buffer-file-name) | |
1531 | (file-name nil) | |
1532 | (file-dir nil)) | |
1533 | (and prompt-file | |
1534 | (setq file-name (file-name-nondirectory prompt-file) | |
1535 | file-dir (file-name-directory prompt-file))) | |
1536 | (list (read-file-name "Recover file: " | |
1537 | file-dir nil nil file-name)))) | |
1538 | (setq file (expand-file-name file)) | |
1539 | (if (auto-save-file-name-p file) (error "%s is an auto-save file" file)) | |
1540 | (let ((file-name (let ((buffer-file-name file)) | |
1541 | (make-auto-save-file-name)))) | |
1542 | (cond ((not (file-newer-than-file-p file-name file)) | |
1543 | (error "Auto-save file %s not current" file-name)) | |
1544 | ((save-window-excursion | |
1545 | (if (not (eq system-type 'vax-vms)) | |
1546 | (with-output-to-temp-buffer "*Directory*" | |
1547 | (buffer-disable-undo standard-output) | |
1548 | (call-process "ls" nil standard-output nil | |
2dc2b736 RS |
1549 | (if (file-symlink-p file) "-lL" "-l") |
1550 | file file-name))) | |
b4da00e9 RM |
1551 | (yes-or-no-p (format "Recover auto save file %s? " file-name))) |
1552 | (switch-to-buffer (find-file-noselect file t)) | |
1553 | (let ((buffer-read-only nil)) | |
1554 | (erase-buffer) | |
1555 | (insert-file-contents file-name nil)) | |
8cfb9d46 | 1556 | (after-find-file nil nil t)) |
b4da00e9 RM |
1557 | (t (error "Recover-file cancelled."))))) |
1558 | ||
1559 | (defun kill-some-buffers () | |
1560 | "For each buffer, ask whether to kill it." | |
1561 | (interactive) | |
1562 | (let ((list (buffer-list))) | |
1563 | (while list | |
1564 | (let* ((buffer (car list)) | |
1565 | (name (buffer-name buffer))) | |
1566 | (and (not (string-equal name "")) | |
1567 | (/= (aref name 0) ? ) | |
1568 | (yes-or-no-p | |
1569 | (format "Buffer %s %s. Kill? " | |
1570 | name | |
1571 | (if (buffer-modified-p buffer) | |
1572 | "HAS BEEN EDITED" "is unmodified"))) | |
1573 | (kill-buffer buffer))) | |
1574 | (setq list (cdr list))))) | |
1575 | \f | |
1576 | (defun auto-save-mode (arg) | |
1577 | "Toggle auto-saving of contents of current buffer. | |
f3e23606 | 1578 | With prefix argument ARG, turn auto-saving on if positive, else off." |
b4da00e9 RM |
1579 | (interactive "P") |
1580 | (setq buffer-auto-save-file-name | |
1581 | (and (if (null arg) | |
1582 | (not buffer-auto-save-file-name) | |
1583 | (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))) | |
1584 | (if (and buffer-file-name auto-save-visited-file-name | |
1585 | (not buffer-read-only)) | |
1586 | buffer-file-name | |
1587 | (make-auto-save-file-name)))) | |
1588 | (if (interactive-p) | |
1589 | (message "Auto-save %s (in this buffer)" | |
1590 | (if buffer-auto-save-file-name "on" "off"))) | |
1591 | buffer-auto-save-file-name) | |
1592 | ||
1593 | (defun rename-auto-save-file () | |
1594 | "Adjust current buffer's auto save file name for current conditions. | |
1595 | Also rename any existing auto save file, if it was made in this session." | |
1596 | (let ((osave buffer-auto-save-file-name)) | |
1597 | (setq buffer-auto-save-file-name | |
1598 | (make-auto-save-file-name)) | |
1599 | (if (and osave buffer-auto-save-file-name | |
1600 | (not (string= buffer-auto-save-file-name buffer-file-name)) | |
1601 | (not (string= buffer-auto-save-file-name osave)) | |
1602 | (file-exists-p osave) | |
1603 | (recent-auto-save-p)) | |
1604 | (rename-file osave buffer-auto-save-file-name t)))) | |
1605 | ||
1606 | (defun make-auto-save-file-name () | |
1607 | "Return file name to use for auto-saves of current buffer. | |
1608 | Does not consider `auto-save-visited-file-name' as that variable is checked | |
1609 | before calling this function. You can redefine this for customization. | |
1610 | See also `auto-save-file-name-p'." | |
1611 | (if buffer-file-name | |
1612 | (concat (file-name-directory buffer-file-name) | |
1613 | "#" | |
1614 | (file-name-nondirectory buffer-file-name) | |
1615 | "#") | |
1616 | ;; For non-file bfr, use bfr name and Emacs pid. | |
1617 | (expand-file-name (format "#%s#%s#" (buffer-name) (make-temp-name ""))))) | |
1618 | ||
1619 | (defun auto-save-file-name-p (filename) | |
1620 | "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'. | |
1621 | FILENAME should lack slashes. You can redefine this for customization." | |
1622 | (string-match "^#.*#$" filename)) | |
1623 | \f | |
1624 | (defconst list-directory-brief-switches | |
1625 | (if (eq system-type 'vax-vms) "" "-CF") | |
1626 | "*Switches for list-directory to pass to `ls' for brief listing,") | |
1627 | ||
1628 | (defconst list-directory-verbose-switches | |
1629 | (if (eq system-type 'vax-vms) | |
1630 | "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)" | |
1631 | "-l") | |
1632 | "*Switches for list-directory to pass to `ls' for verbose listing,") | |
1633 | ||
1634 | (defun list-directory (dirname &optional verbose) | |
1635 | "Display a list of files in or matching DIRNAME, a la `ls'. | |
1636 | DIRNAME is globbed by the shell if necessary. | |
1637 | Prefix arg (second arg if noninteractive) means supply -l switch to `ls'. | |
1638 | Actions controlled by variables `list-directory-brief-switches' | |
1639 | and `list-directory-verbose-switches'." | |
1640 | (interactive (let ((pfx current-prefix-arg)) | |
1641 | (list (read-file-name (if pfx "List directory (verbose): " | |
1642 | "List directory (brief): ") | |
1643 | nil default-directory nil) | |
1644 | pfx))) | |
1645 | (let ((switches (if verbose list-directory-verbose-switches | |
1646 | list-directory-brief-switches))) | |
1647 | (or dirname (setq dirname default-directory)) | |
1648 | (setq dirname (expand-file-name dirname)) | |
1649 | (with-output-to-temp-buffer "*Directory*" | |
1650 | (buffer-disable-undo standard-output) | |
1651 | (princ "Directory ") | |
1652 | (princ dirname) | |
1653 | (terpri) | |
c3554e95 RS |
1654 | (save-excursion |
1655 | (set-buffer "*Directory*") | |
1656 | (let ((wildcard (not (file-directory-p dirname)))) | |
1657 | (insert-directory dirname switches wildcard (not wildcard))))))) | |
1658 | ||
1659 | (defvar insert-directory-program "ls" | |
1660 | "Absolute or relative name of the `ls' program used by `insert-directory'.") | |
1661 | ||
1662 | ;; insert-directory | |
1663 | ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and | |
1664 | ;; FULL-DIRECTORY-P is nil. | |
1665 | ;; The single line of output must display FILE's name as it was | |
1666 | ;; given, namely, an absolute path name. | |
1667 | ;; - must insert exactly one line for each file if WILDCARD or | |
1668 | ;; FULL-DIRECTORY-P is t, plus one optional "total" line | |
1669 | ;; before the file lines, plus optional text after the file lines. | |
1670 | ;; Lines are delimited by "\n", so filenames containing "\n" are not | |
1671 | ;; allowed. | |
1672 | ;; File lines should display the basename. | |
1673 | ;; - must be consistent with | |
1674 | ;; - functions dired-move-to-filename, (these two define what a file line is) | |
1675 | ;; dired-move-to-end-of-filename, | |
1676 | ;; dired-between-files, (shortcut for (not (dired-move-to-filename))) | |
1677 | ;; dired-insert-headerline | |
1678 | ;; dired-after-subdir-garbage (defines what a "total" line is) | |
1679 | ;; - variable dired-subdir-regexp | |
1680 | (defun insert-directory (file switches &optional wildcard full-directory-p) | |
1681 | "Insert directory listing for of FILE, formatted according to SWITCHES. | |
1682 | Leaves point after the inserted text. | |
1683 | Optional third arg WILDCARD means treat FILE as shell wildcard. | |
1684 | Optional fourth arg FULL-DIRECTORY-P means file is a directory and | |
1685 | switches do not contain `d', so that a full listing is expected. | |
1686 | ||
1687 | This works by running a directory listing program | |
406e12d9 | 1688 | whose name is in the variable `insert-directory-program'. |
c3554e95 | 1689 | If WILDCARD, it also runs the shell specified by `shell-file-name'." |
5dbfdacd | 1690 | (let ((handler (find-file-name-handler file))) |
c3554e95 RS |
1691 | (if handler |
1692 | (funcall handler 'insert-directory file switches | |
1693 | wildcard full-directory-p) | |
b4da00e9 | 1694 | (if (eq system-type 'vax-vms) |
c3554e95 RS |
1695 | (vms-read-directory file switches (current-buffer)) |
1696 | (if wildcard | |
1697 | (let ((default-directory (file-name-directory file))) | |
1698 | (call-process shell-file-name nil t nil | |
1699 | "-c" (concat insert-directory-program | |
1700 | " -d " switches " " | |
1701 | (file-name-nondirectory file)))) | |
632c2579 JB |
1702 | ;; Chase links till we reach a non-link. |
1703 | ;; This used to be commented out, but Barry Margolin says: | |
1704 | ;; SunOS 4.1.3 (and SV and POSIX?) lists the link | |
1705 | ;; if we give a link to a directory - yuck! | |
1706 | (let (symlink) | |
1707 | (while (setq symlink (file-symlink-p file)) | |
1708 | (setq file symlink))) | |
c3554e95 | 1709 | (call-process insert-directory-program nil t nil switches file)))))) |
b4da00e9 RM |
1710 | |
1711 | (defun save-buffers-kill-emacs (&optional arg) | |
1712 | "Offer to save each buffer, then kill this Emacs process. | |
1713 | With prefix arg, silently save all file-visiting buffers, then kill." | |
1714 | (interactive "P") | |
1715 | (save-some-buffers arg t) | |
1716 | (and (or (not (memq t (mapcar (function | |
1717 | (lambda (buf) (and (buffer-file-name buf) | |
1718 | (buffer-modified-p buf)))) | |
1719 | (buffer-list)))) | |
1720 | (yes-or-no-p "Modified buffers exist; exit anyway? ")) | |
1721 | (or (not (fboundp 'process-list)) | |
1722 | ;; process-list is not defined on VMS. | |
1723 | (let ((processes (process-list)) | |
1724 | active) | |
1725 | (while processes | |
528415e7 | 1726 | (and (memq (process-status (car processes)) '(run stop open)) |
b4da00e9 RM |
1727 | (let ((val (process-kill-without-query (car processes)))) |
1728 | (process-kill-without-query (car processes) val) | |
1729 | val) | |
1730 | (setq active t)) | |
1731 | (setq processes (cdr processes))) | |
1732 | (or (not active) | |
1733 | (yes-or-no-p "Active processes exist; kill them and exit anyway? ")))) | |
1734 | (kill-emacs))) | |
1735 | \f | |
1736 | (define-key ctl-x-map "\C-f" 'find-file) | |
1737 | (define-key ctl-x-map "\C-q" 'toggle-read-only) | |
1738 | (define-key ctl-x-map "\C-r" 'find-file-read-only) | |
1739 | (define-key ctl-x-map "\C-v" 'find-alternate-file) | |
1740 | (define-key ctl-x-map "\C-s" 'save-buffer) | |
1741 | (define-key ctl-x-map "s" 'save-some-buffers) | |
1742 | (define-key ctl-x-map "\C-w" 'write-file) | |
1743 | (define-key ctl-x-map "i" 'insert-file) | |
1744 | (define-key esc-map "~" 'not-modified) | |
1745 | (define-key ctl-x-map "\C-d" 'list-directory) | |
1746 | (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs) | |
1747 | ||
1748 | (define-key ctl-x-4-map "f" 'find-file-other-window) | |
1749 | (define-key ctl-x-4-map "r" 'find-file-read-only-other-window) | |
1750 | (define-key ctl-x-4-map "\C-f" 'find-file-other-window) | |
1751 | (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window) | |
924f0a24 | 1752 | (define-key ctl-x-4-map "\C-o" 'display-buffer) |
5bbbceb1 | 1753 | |
f98955ea JB |
1754 | (define-key ctl-x-5-map "b" 'switch-to-buffer-other-frame) |
1755 | (define-key ctl-x-5-map "f" 'find-file-other-frame) | |
1756 | (define-key ctl-x-5-map "\C-f" 'find-file-other-frame) | |
1757 | (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame) | |
c0274f38 ER |
1758 | |
1759 | ;;; files.el ends here |