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