Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; files.el --- file input and output commands for Emacs |
2 | ||
18cf9502 | 3 | ;; Copyright (C) 1985, 86, 87, 92, 93, 94 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 | ||
65151a1b RS |
42 | Do not use `~' in the TO strings. |
43 | They should be ordinary absolute directory names. | |
44 | ||
b4da00e9 RM |
45 | Use this feature when you have directories which you normally refer to |
46 | via absolute symbolic links. Make TO the name of the link, and FROM | |
47 | the name it is linked to.") | |
48 | ||
49 | ;;; Turn off backup files on VMS since it has version numbers. | |
50 | (defconst make-backup-files (not (eq system-type 'vax-vms)) | |
4ea836c1 | 51 | "*Non-nil means make a backup of a file the first time it is saved. |
b4da00e9 RM |
52 | This can be done by renaming the file or by copying. |
53 | ||
54 | Renaming means that Emacs renames the existing file so that it is a | |
55 | backup file, then writes the buffer into a new file. Any other names | |
56 | that the old file had will now refer to the backup file. The new file | |
57 | is owned by you and its group is defaulted. | |
58 | ||
59 | Copying means that Emacs copies the existing file into the backup | |
60 | file, then writes the buffer on top of the existing file. Any other | |
61 | names that the old file had will now refer to the new (edited) file. | |
62 | The file's owner and group are unchanged. | |
63 | ||
64 | The choice of renaming or copying is controlled by the variables | |
65 | `backup-by-copying', `backup-by-copying-when-linked' and | |
f862241d | 66 | `backup-by-copying-when-mismatch'. See also `backup-inhibited'.") |
b4da00e9 RM |
67 | |
68 | ;; Do this so that local variables based on the file name | |
69 | ;; are not overridden by the major mode. | |
70 | (defvar backup-inhibited nil | |
f862241d RS |
71 | "Non-nil means don't make a backup, regardless of the other parameters. |
72 | This variable is intended for use by making it local to a buffer. | |
73 | But it is local only if you make it local.") | |
b4da00e9 RM |
74 | (put 'backup-inhibited 'permanent-local t) |
75 | ||
76 | (defconst backup-by-copying nil | |
77 | "*Non-nil means always use copying to create backup files. | |
78 | See documentation of variable `make-backup-files'.") | |
79 | ||
80 | (defconst backup-by-copying-when-linked nil | |
81 | "*Non-nil means use copying to create backups for files with multiple names. | |
82 | This causes the alternate names to refer to the latest version as edited. | |
83 | This variable is relevant only if `backup-by-copying' is nil.") | |
84 | ||
85 | (defconst backup-by-copying-when-mismatch nil | |
86 | "*Non-nil means create backups by copying if this preserves owner or group. | |
87 | Renaming may still be used (subject to control of other variables) | |
88 | when it would not result in changing the owner or group of the file; | |
89 | that is, for files which are owned by you and whose group matches | |
90 | the default for a new file created there by you. | |
91 | This variable is relevant only if `backup-by-copying' is nil.") | |
92 | ||
93 | (defvar backup-enable-predicate | |
94 | '(lambda (name) | |
95 | (or (< (length name) 5) | |
96 | (not (string-equal "/tmp/" (substring name 0 5))))) | |
97 | "Predicate that looks at a file name and decides whether to make backups. | |
98 | Called with an absolute file name as argument, it returns t to enable backup.") | |
99 | ||
100 | (defconst buffer-offer-save nil | |
101 | "*Non-nil in a buffer means offer to save the buffer on exit | |
102 | even if the buffer is not visiting a file. | |
103 | Automatically local in all buffers.") | |
104 | (make-variable-buffer-local 'buffer-offer-save) | |
105 | ||
f3e23606 RS |
106 | (defconst find-file-existing-other-name nil |
107 | "*Non-nil means find a file under alternative names, in existing buffers. | |
108 | This means if any existing buffer is visiting the file you want | |
109 | under another name, you get the existing buffer instead of a new buffer.") | |
110 | ||
111 | (defconst find-file-visit-truename nil | |
112 | "*Non-nil means visit a file under its truename. | |
113 | The truename of a file is found by chasing all links | |
114 | both at the file level and at the levels of the containing directories.") | |
115 | ||
116 | (defvar buffer-file-truename nil | |
7d806fcd KH |
117 | "The abbreviated truename of the file visited in the current buffer. |
118 | That is, (abbreviated-file-name (file-truename buffer-file-name)). | |
f3e23606 RS |
119 | This variable is automatically local in all buffers, when non-nil.") |
120 | (make-variable-buffer-local 'buffer-file-truename) | |
121 | (put 'buffer-file-truename 'permanent-local t) | |
122 | ||
123 | (defvar buffer-file-number nil | |
124 | "The device number and file number of the file visited in the current buffer. | |
125 | The value is a list of the form (FILENUM DEVNUM). | |
126 | This pair of numbers uniquely identifies the file. | |
127 | If the buffer is visiting a new file, the value is nil.") | |
128 | (make-variable-buffer-local 'buffer-file-number) | |
129 | (put 'buffer-file-number 'permanent-local t) | |
130 | ||
b4da00e9 RM |
131 | (defconst file-precious-flag nil |
132 | "*Non-nil means protect against I/O errors while saving files. | |
133 | Some modes set this non-nil in particular buffers.") | |
134 | ||
135 | (defvar version-control nil | |
136 | "*Control use of version numbers for backup files. | |
137 | t means make numeric backup versions unconditionally. | |
138 | nil means make them for files that have some already. | |
399f8dd6 | 139 | `never' means do not make them.") |
b4da00e9 RM |
140 | |
141 | (defvar dired-kept-versions 2 | |
142 | "*When cleaning directory, number of versions to keep.") | |
143 | ||
144 | (defvar trim-versions-without-asking nil | |
6d74b528 ER |
145 | "*If t, deletes excess backup versions silently. |
146 | If nil, asks confirmation. Any other value prevents any trimming.") | |
b4da00e9 RM |
147 | |
148 | (defvar kept-old-versions 2 | |
149 | "*Number of oldest versions to keep when a new numbered backup is made.") | |
150 | ||
151 | (defvar kept-new-versions 2 | |
152 | "*Number of newest versions to keep when a new numbered backup is made. | |
153 | Includes the new backup. Must be > 0") | |
154 | ||
155 | (defconst require-final-newline nil | |
156 | "*Value of t says silently ensure a file ends in a newline when it is saved. | |
157 | Non-nil but not t says ask user whether to add a newline when there isn't one. | |
158 | nil means don't add newlines.") | |
159 | ||
160 | (defconst auto-save-default t | |
161 | "*Non-nil says by default do auto-saving of every file-visiting buffer.") | |
162 | ||
163 | (defconst auto-save-visited-file-name nil | |
164 | "*Non-nil says auto-save a buffer in the file it is visiting, when practical. | |
165 | Normally auto-save files are written under other names.") | |
166 | ||
167 | (defconst save-abbrevs nil | |
168 | "*Non-nil means save word abbrevs too when files are saved. | |
169 | Loading an abbrev file sets this to t.") | |
170 | ||
171 | (defconst find-file-run-dired t | |
320b4233 | 172 | "*Non-nil says run dired if `find-file' is given the name of a directory.") |
b4da00e9 | 173 | |
92966e6f RS |
174 | ;;;It is not useful to make this a local variable. |
175 | ;;;(put 'find-file-not-found-hooks 'permanent-local t) | |
b4da00e9 RM |
176 | (defvar find-file-not-found-hooks nil |
177 | "List of functions to be called for `find-file' on nonexistent file. | |
178 | These functions are called as soon as the error is detected. | |
179 | `buffer-file-name' is already set up. | |
180 | The functions are called in the order given until one of them returns non-nil.") | |
181 | ||
92966e6f RS |
182 | ;;;It is not useful to make this a local variable. |
183 | ;;;(put 'find-file-hooks 'permanent-local t) | |
b4da00e9 RM |
184 | (defvar find-file-hooks nil |
185 | "List of functions to be called after a buffer is loaded from a file. | |
186 | The buffer's local variables (if any) will have been processed before the | |
187 | functions are called.") | |
188 | ||
b4da00e9 RM |
189 | (defvar write-file-hooks nil |
190 | "List of functions to be called before writing out a buffer to a file. | |
191 | If one of them returns non-nil, the file is considered already written | |
8c0e7b73 JB |
192 | and the rest are not called. |
193 | These hooks are considered to pertain to the visited file. | |
194 | So this list is cleared if you change the visited file name. | |
c9dca4e0 RS |
195 | See also `write-contents-hooks'. |
196 | Don't make this variable buffer-local; instead, use `local-write-file-hooks'.") | |
b19f1da4 BF |
197 | ;;; However, in case someone does make it local... |
198 | (put 'write-file-hooks 'permanent-local t) | |
c9dca4e0 | 199 | |
c9dca4e0 RS |
200 | (defvar local-write-file-hooks nil |
201 | "Just like `write-file-hooks', except intended for per-buffer use. | |
202 | The functions in this list are called before the ones in | |
203 | `write-file-hooks'.") | |
b19f1da4 BF |
204 | (make-variable-buffer-local 'local-write-file-hooks) |
205 | (put 'local-write-file-hooks 'permanent-local t) | |
8c0e7b73 JB |
206 | |
207 | (defvar write-contents-hooks nil | |
208 | "List of functions to be called before writing out a buffer to a file. | |
209 | If one of them returns non-nil, the file is considered already written | |
210 | and the rest are not called. | |
211 | These hooks are considered to pertain to the buffer's contents, | |
212 | not to the particular visited file; thus, `set-visited-file-name' does | |
213 | not clear this variable, but changing the major mode does clear it. | |
214 | See also `write-file-hooks'.") | |
b4da00e9 RM |
215 | |
216 | (defconst enable-local-variables t | |
217 | "*Control use of local-variables lists in files you visit. | |
218 | The value can be t, nil or something else. | |
219 | A value of t means local-variables lists are obeyed; | |
220 | nil means they are ignored; anything else means query. | |
221 | ||
222 | The command \\[normal-mode] always obeys local-variables lists | |
223 | and ignores this variable.") | |
224 | ||
2bba782c | 225 | (defconst enable-local-eval 'maybe |
d207b766 RS |
226 | "*Control processing of the \"variable\" `eval' in a file's local variables. |
227 | The value can be t, nil or something else. | |
228 | A value of t means obey `eval' variables; | |
229 | nil means ignore them; anything else means query. | |
230 | ||
231 | The command \\[normal-mode] always obeys local-variables lists | |
232 | and ignores this variable.") | |
b4da00e9 RM |
233 | |
234 | ;; Avoid losing in versions where CLASH_DETECTION is disabled. | |
235 | (or (fboundp 'lock-buffer) | |
231c4e10 | 236 | (defalias 'lock-buffer 'ignore)) |
b4da00e9 | 237 | (or (fboundp 'unlock-buffer) |
231c4e10 | 238 | (defalias 'unlock-buffer 'ignore)) |
93fe0a35 RS |
239 | |
240 | ;; This hook function provides support for ange-ftp host name | |
241 | ;; completion. It runs the usual ange-ftp hook, but only for | |
242 | ;; completion operations. Having this here avoids the need | |
243 | ;; to load ange-ftp when it's not really in use. | |
244 | (defun ange-ftp-completion-hook-function (op &rest args) | |
245 | (if (memq op '(file-name-completion file-name-all-completions)) | |
246 | (apply 'ange-ftp-hook-function op args) | |
57e81f57 RS |
247 | (let ((inhibit-file-name-handlers |
248 | (cons 'ange-ftp-completion-hook-function | |
249 | (and (eq inhibit-file-name-operation op) | |
250 | inhibit-file-name-handlers))) | |
251 | (inhibit-file-name-operation op)) | |
93fe0a35 | 252 | (apply op args)))) |
b4da00e9 RM |
253 | \f |
254 | (defun pwd () | |
255 | "Show the current default directory." | |
256 | (interactive nil) | |
257 | (message "Directory %s" default-directory)) | |
258 | ||
231c4e10 ER |
259 | (defvar cd-path nil |
260 | "Value of the CDPATH environment variable, as a list. | |
261 | Not actually set up until the first time you you use it.") | |
262 | ||
263 | (defun parse-colon-path (cd-path) | |
264 | "Explode a colon-separated list of paths into a string list." | |
265 | (and cd-path | |
266 | (let (cd-prefix cd-list (cd-start 0) cd-colon) | |
267 | (setq cd-path (concat cd-path ":")) | |
268 | (while (setq cd-colon (string-match ":" cd-path cd-start)) | |
269 | (setq cd-list | |
9daefb36 | 270 | (nconc cd-list |
c52e4104 RS |
271 | (list (if (= cd-start cd-colon) |
272 | nil | |
273 | (substitute-in-file-name | |
e33e80e4 RS |
274 | (file-name-as-directory |
275 | (substring cd-path cd-start cd-colon))))))) | |
231c4e10 ER |
276 | (setq cd-start (+ cd-colon 1))) |
277 | cd-list))) | |
278 | ||
279 | (defun cd-absolute (dir) | |
30c5ce9c | 280 | "Change current directory to given absolute file name DIR." |
b3c4423b | 281 | (setq dir (abbreviate-file-name (expand-file-name dir))) |
b4da00e9 RM |
282 | (if (not (eq system-type 'vax-vms)) |
283 | (setq dir (file-name-as-directory dir))) | |
284 | (if (not (file-directory-p dir)) | |
285 | (error "%s is not a directory" dir) | |
286 | (if (file-executable-p dir) | |
287 | (setq default-directory dir) | |
9daefb36 | 288 | (error "Cannot cd to %s: Permission denied" dir)))) |
b4da00e9 | 289 | |
231c4e10 ER |
290 | (defun cd (dir) |
291 | "Make DIR become the current buffer's default directory. | |
30c5ce9c RS |
292 | If your environment includes a `CDPATH' variable, try each one of that |
293 | colon-separated list of directories when resolving a relative directory name." | |
231c4e10 | 294 | (interactive "FChange default directory: ") |
30c5ce9c RS |
295 | (if (file-name-absolute-p dir) |
296 | (cd-absolute (expand-file-name dir)) | |
297 | (if (null cd-path) | |
298 | (let ((trypath (parse-colon-path (getenv "CDPATH")))) | |
299 | (setq cd-path (or trypath (list "./"))))) | |
300 | (if (not (catch 'found | |
301 | (mapcar | |
302 | (function (lambda (x) | |
303 | (let ((f (expand-file-name (concat x dir)))) | |
304 | (if (file-directory-p f) | |
305 | (progn | |
306 | (cd-absolute f) | |
307 | (throw 'found t)))))) | |
308 | cd-path) | |
309 | nil)) | |
310 | (error "No such directory found via CDPATH environment variable")))) | |
231c4e10 | 311 | |
b4da00e9 RM |
312 | (defun load-file (file) |
313 | "Load the Lisp file named FILE." | |
314 | (interactive "fLoad file: ") | |
315 | (load (expand-file-name file) nil nil t)) | |
316 | ||
317 | (defun load-library (library) | |
318 | "Load the library named LIBRARY. | |
319 | This is an interface to the function `load'." | |
320 | (interactive "sLoad library: ") | |
321 | (load library)) | |
5d68c2c2 | 322 | |
7c4b8f8c | 323 | (defun file-local-copy (file &optional buffer) |
5d68c2c2 RS |
324 | "Copy the file FILE into a temporary file on this machine. |
325 | Returns the name of the local copy, or nil, if FILE is directly | |
326 | accessible." | |
6eaebaa2 | 327 | (let ((handler (find-file-name-handler file 'file-local-copy))) |
5d68c2c2 RS |
328 | (if handler |
329 | (funcall handler 'file-local-copy file) | |
330 | nil))) | |
f3e23606 RS |
331 | |
332 | (defun file-truename (filename) | |
333 | "Return the truename of FILENAME, which should be absolute. | |
334 | The truename of a file name is found by chasing symbolic links | |
335 | both at the level of the file and at the level of the directories | |
336 | containing it, until no links are left at any level." | |
7a5a26a6 RS |
337 | (if (or (string= filename "~") |
338 | (and (string= (substring filename 0 1) "~") | |
339 | (string-match "~[^/]*" filename))) | |
1cc2fbeb RS |
340 | (progn |
341 | (setq filename (expand-file-name filename)) | |
342 | (if (string= filename "") | |
343 | (setq filename "/")))) | |
6eaebaa2 | 344 | (let ((handler (find-file-name-handler filename 'file-truename))) |
6f176f94 RS |
345 | ;; For file name that has a special handler, call handler. |
346 | ;; This is so that ange-ftp can save time by doing a no-op. | |
347 | (if handler | |
348 | (funcall handler 'file-truename filename) | |
349 | (let ((dir (file-name-directory filename)) | |
350 | target dirfile) | |
351 | ;; Get the truename of the directory. | |
352 | (setq dirfile (directory-file-name dir)) | |
353 | ;; If these are equal, we have the (or a) root directory. | |
354 | (or (string= dir dirfile) | |
355 | (setq dir (file-name-as-directory (file-truename dirfile)))) | |
c3bf10c2 RS |
356 | (if (equal ".." (file-name-nondirectory filename)) |
357 | (directory-file-name (file-name-directory (directory-file-name dir))) | |
358 | (if (equal "." (file-name-nondirectory filename)) | |
359 | (directory-file-name dir) | |
360 | ;; Put it back on the file name. | |
361 | (setq filename (concat dir (file-name-nondirectory filename))) | |
362 | ;; Is the file name the name of a link? | |
363 | (setq target (file-symlink-p filename)) | |
364 | (if target | |
365 | ;; Yes => chase that link, then start all over | |
366 | ;; since the link may point to a directory name that uses links. | |
367 | ;; We can't safely use expand-file-name here | |
368 | ;; since target might look like foo/../bar where foo | |
369 | ;; is itself a link. Instead, we handle . and .. above. | |
370 | (if (file-name-absolute-p target) | |
371 | (file-truename target) | |
372 | (file-truename (concat dir target))) | |
373 | ;; No, we are done! | |
374 | filename))))))) | |
5dbfdacd | 375 | |
5dadeb29 RS |
376 | (defun file-chase-links (filename) |
377 | "Chase links in FILENAME until a name that is not a link. | |
378 | Does not examine containing directories for links, | |
379 | unlike `file-truename'." | |
380 | (let (tem (count 100) (newname filename)) | |
381 | (while (setq tem (file-symlink-p newname)) | |
382 | (if (= count 0) | |
383 | (error "Apparent cycle of symbolic links for %s" filename)) | |
f01de0db KH |
384 | ;; In the context of a link, `//' doesn't mean what Emacs thinks. |
385 | (while (string-match "//+" tem) | |
386 | (setq tem (concat (substring tem 0 (1+ (match-beginning 0))) | |
387 | (substring tem (match-end 0))))) | |
cf65b429 RS |
388 | ;; Handle `..' by hand, since it needs to work in the |
389 | ;; target of any directory symlink. | |
390 | ;; This code is not quite complete; it does not handle | |
391 | ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose. | |
f01de0db | 392 | (while (string-match "\\`\\.\\./" tem) |
cf65b429 RS |
393 | (setq tem (substring tem 3)) |
394 | (setq newname (file-name-as-directory | |
395 | ;; Do the .. by hand. | |
396 | (directory-file-name | |
397 | (file-name-directory | |
398 | ;; Chase links in the default dir of the symlink. | |
399 | (file-chase-links | |
400 | (directory-file-name | |
401 | (file-name-directory newname)))))))) | |
5dadeb29 RS |
402 | (setq newname (expand-file-name tem (file-name-directory newname))) |
403 | (setq count (1- count))) | |
404 | newname)) | |
b4da00e9 RM |
405 | \f |
406 | (defun switch-to-buffer-other-window (buffer) | |
407 | "Select buffer BUFFER in another window." | |
408 | (interactive "BSwitch to buffer in other window: ") | |
409 | (let ((pop-up-windows t)) | |
410 | (pop-to-buffer buffer t))) | |
411 | ||
f98955ea JB |
412 | (defun switch-to-buffer-other-frame (buffer) |
413 | "Switch to buffer BUFFER in another frame." | |
414 | (interactive "BSwitch to buffer in other frame: ") | |
415 | (let ((pop-up-frames t)) | |
2be5e5db | 416 | (pop-to-buffer buffer t))) |
5bbbceb1 | 417 | |
b4da00e9 RM |
418 | (defun find-file (filename) |
419 | "Edit file FILENAME. | |
420 | Switch to a buffer visiting file FILENAME, | |
421 | creating one if none already exists." | |
422 | (interactive "FFind file: ") | |
423 | (switch-to-buffer (find-file-noselect filename))) | |
424 | ||
425 | (defun find-file-other-window (filename) | |
426 | "Edit file FILENAME, in another window. | |
427 | May create a new window, or reuse an existing one. | |
428 | See the function `display-buffer'." | |
429 | (interactive "FFind file in other window: ") | |
430 | (switch-to-buffer-other-window (find-file-noselect filename))) | |
431 | ||
f98955ea JB |
432 | (defun find-file-other-frame (filename) |
433 | "Edit file FILENAME, in another frame. | |
434 | May create a new frame, or reuse an existing one. | |
5bbbceb1 | 435 | See the function `display-buffer'." |
f98955ea JB |
436 | (interactive "FFind file in other frame: ") |
437 | (switch-to-buffer-other-frame (find-file-noselect filename))) | |
5bbbceb1 | 438 | |
b4da00e9 RM |
439 | (defun find-file-read-only (filename) |
440 | "Edit file FILENAME but don't allow changes. | |
441 | Like \\[find-file] but marks buffer as read-only. | |
442 | Use \\[toggle-read-only] to permit editing." | |
443 | (interactive "fFind file read-only: ") | |
444 | (find-file filename) | |
320b4233 RS |
445 | (setq buffer-read-only t) |
446 | (current-buffer)) | |
b4da00e9 RM |
447 | |
448 | (defun find-file-read-only-other-window (filename) | |
449 | "Edit file FILENAME in another window but don't allow changes. | |
450 | Like \\[find-file-other-window] but marks buffer as read-only. | |
451 | Use \\[toggle-read-only] to permit editing." | |
452 | (interactive "fFind file read-only other window: ") | |
9466a1f6 | 453 | (find-file-other-window filename) |
320b4233 RS |
454 | (setq buffer-read-only t) |
455 | (current-buffer)) | |
b4da00e9 | 456 | |
f98955ea JB |
457 | (defun find-file-read-only-other-frame (filename) |
458 | "Edit file FILENAME in another frame but don't allow changes. | |
459 | Like \\[find-file-other-frame] but marks buffer as read-only. | |
5bbbceb1 | 460 | Use \\[toggle-read-only] to permit editing." |
f98955ea JB |
461 | (interactive "fFind file read-only other frame: ") |
462 | (find-file-other-frame filename) | |
320b4233 RS |
463 | (setq buffer-read-only t) |
464 | (current-buffer)) | |
5bbbceb1 | 465 | |
b4da00e9 RM |
466 | (defun find-alternate-file (filename) |
467 | "Find file FILENAME, select its buffer, kill previous buffer. | |
468 | If the current buffer now contains an empty file that you just visited | |
469 | \(presumably by mistake), use this command to visit the file you really want." | |
470 | (interactive | |
471 | (let ((file buffer-file-name) | |
472 | (file-name nil) | |
473 | (file-dir nil)) | |
474 | (and file | |
475 | (setq file-name (file-name-nondirectory file) | |
476 | file-dir (file-name-directory file))) | |
a61f59b4 JA |
477 | (list (read-file-name |
478 | "Find alternate file: " file-dir nil nil file-name)))) | |
b4da00e9 RM |
479 | (and (buffer-modified-p) |
480 | ;; (not buffer-read-only) | |
481 | (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " | |
482 | (buffer-name)))) | |
483 | (error "Aborted")) | |
484 | (let ((obuf (current-buffer)) | |
485 | (ofile buffer-file-name) | |
8bb27285 RS |
486 | (onum buffer-file-number) |
487 | (otrue buffer-file-truename) | |
b4da00e9 RM |
488 | (oname (buffer-name))) |
489 | (rename-buffer " **lose**") | |
490 | (setq buffer-file-name nil) | |
8bb27285 RS |
491 | (setq buffer-file-number nil) |
492 | (setq buffer-file-truename nil) | |
b4da00e9 RM |
493 | (unwind-protect |
494 | (progn | |
495 | (unlock-buffer) | |
496 | (find-file filename)) | |
497 | (cond ((eq obuf (current-buffer)) | |
498 | (setq buffer-file-name ofile) | |
8bb27285 RS |
499 | (setq buffer-file-number onum) |
500 | (setq buffer-file-truename otrue) | |
b4da00e9 RM |
501 | (lock-buffer) |
502 | (rename-buffer oname)))) | |
503 | (or (eq (current-buffer) obuf) | |
504 | (kill-buffer obuf)))) | |
505 | ||
506 | (defun create-file-buffer (filename) | |
507 | "Create a suitably named buffer for visiting FILENAME, and return it. | |
508 | FILENAME (sans directory) is used unchanged if that name is free; | |
509 | otherwise a string <2> or <3> or ... is appended to get an unused name." | |
510 | (let ((lastname (file-name-nondirectory filename))) | |
511 | (if (string= lastname "") | |
512 | (setq lastname filename)) | |
513 | (generate-new-buffer lastname))) | |
514 | ||
5bbbceb1 JB |
515 | (defun generate-new-buffer (name) |
516 | "Create and return a buffer with a name based on NAME. | |
29165787 | 517 | Choose the buffer's name using `generate-new-buffer-name'." |
5bbbceb1 JB |
518 | (get-buffer-create (generate-new-buffer-name name))) |
519 | ||
e373f201 JB |
520 | (defconst automount-dir-prefix "^/tmp_mnt/" |
521 | "Regexp to match the automounter prefix in a directory name.") | |
522 | ||
ffb3a4db | 523 | (defvar abbreviated-home-dir nil |
292297c6 | 524 | "The user's homedir abbreviated according to `directory-abbrev-list'.") |
ffb3a4db | 525 | |
5bbbceb1 | 526 | (defun abbreviate-file-name (filename) |
29165787 | 527 | "Return a version of FILENAME shortened using `directory-abbrev-alist'. |
5bbbceb1 | 528 | This also substitutes \"~\" for the user's home directory. |
29165787 | 529 | Type \\[describe-variable] directory-abbrev-alist RET for more information." |
e373f201 JB |
530 | ;; Get rid of the prefixes added by the automounter. |
531 | (if (and (string-match automount-dir-prefix filename) | |
532 | (file-exists-p (file-name-directory | |
533 | (substring filename (1- (match-end 0)))))) | |
534 | (setq filename (substring filename (1- (match-end 0))))) | |
5bbbceb1 | 535 | (let ((tail directory-abbrev-alist)) |
ffb3a4db RS |
536 | ;; If any elt of directory-abbrev-alist matches this name, |
537 | ;; abbreviate accordingly. | |
5bbbceb1 JB |
538 | (while tail |
539 | (if (string-match (car (car tail)) filename) | |
540 | (setq filename | |
541 | (concat (cdr (car tail)) (substring filename (match-end 0))))) | |
542 | (setq tail (cdr tail))) | |
ffb3a4db RS |
543 | ;; Compute and save the abbreviated homedir name. |
544 | ;; We defer computing this until the first time it's needed, to | |
545 | ;; give time for directory-abbrev-alist to be set properly. | |
e98dda89 RS |
546 | ;; We include a slash at the end, to avoid spurious matches |
547 | ;; such as `/usr/foobar' when the home dir is `/usr/foo'. | |
ffb3a4db RS |
548 | (or abbreviated-home-dir |
549 | (setq abbreviated-home-dir | |
550 | (let ((abbreviated-home-dir "$foo")) | |
3a8a836d RS |
551 | (concat "^" (abbreviate-file-name (expand-file-name "~")) |
552 | "\\(/\\|$\\)")))) | |
553 | ||
ffb3a4db RS |
554 | ;; If FILENAME starts with the abbreviated homedir, |
555 | ;; make it start with `~' instead. | |
ca33ccb5 RS |
556 | (if (and (string-match abbreviated-home-dir filename) |
557 | ;; If the home dir is just /, don't change it. | |
558 | (not (and (= (match-end 0) 1) | |
288201bd RS |
559 | (= (aref filename 0) ?/))) |
560 | (not (and (eq system-type 'ms-dos) | |
561 | (save-match-data | |
562 | (string-match "^[a-zA-Z]:/$" filename))))) | |
5bbbceb1 | 563 | (setq filename |
28dbf501 | 564 | (concat "~" |
3a8a836d | 565 | (substring filename (match-beginning 1) (match-end 1)) |
28dbf501 | 566 | (substring filename (match-end 0))))) |
5bbbceb1 JB |
567 | filename)) |
568 | ||
1770543d RS |
569 | (defvar find-file-not-true-dirname-list nil |
570 | "*List of logical names for which visiting shouldn't save the true dirname. | |
571 | On VMS, when you visit a file using a logical name that searches a path, | |
572 | you may or may not want the visited file name to record the specific | |
573 | directory where the file was found. If you *do not* want that, add the logical | |
574 | name to this list as a string.") | |
575 | ||
138c44f6 KH |
576 | (defun find-buffer-visiting (filename) |
577 | "Return the buffer visiting file FILENAME (a string). | |
578 | This is like `get-file-buffer', except that it checks for any buffer | |
579 | visiting the same file, possibly under a different name. | |
580 | If there is no such live buffer, return nil." | |
581 | (let ((buf (get-file-buffer filename)) | |
582 | (truename (abbreviate-file-name (file-truename filename)))) | |
583 | (or buf | |
584 | (let ((list (buffer-list)) found) | |
585 | (while (and (not found) list) | |
586 | (save-excursion | |
587 | (set-buffer (car list)) | |
588 | (if (and buffer-file-name | |
589 | (string= buffer-file-truename truename)) | |
590 | (setq found (car list)))) | |
591 | (setq list (cdr list))) | |
592 | found) | |
0f933cf6 | 593 | (let ((number (nthcdr 10 (file-attributes truename))) |
138c44f6 | 594 | (list (buffer-list)) found) |
181c830f RS |
595 | (and number |
596 | (while (and (not found) list) | |
597 | (save-excursion | |
598 | (set-buffer (car list)) | |
d49ab5a0 RS |
599 | (if (and buffer-file-name |
600 | (equal buffer-file-number number) | |
181c830f RS |
601 | ;; Verify this buffer's file number |
602 | ;; still belongs to its file. | |
603 | (file-exists-p buffer-file-name) | |
604 | (equal (nthcdr 10 (file-attributes buffer-file-name)) | |
605 | number)) | |
606 | (setq found (car list)))) | |
607 | (setq list (cdr list)))) | |
138c44f6 KH |
608 | found)))) |
609 | ||
b4da00e9 RM |
610 | (defun find-file-noselect (filename &optional nowarn) |
611 | "Read file FILENAME into a buffer and return the buffer. | |
612 | If a buffer exists visiting FILENAME, return that one, but | |
613 | verify that the file has not changed since visited or saved. | |
614 | The buffer is not selected, just returned to the caller." | |
e373f201 JB |
615 | (setq filename |
616 | (abbreviate-file-name | |
617 | (expand-file-name filename))) | |
b4da00e9 RM |
618 | (if (file-directory-p filename) |
619 | (if find-file-run-dired | |
620 | (dired-noselect filename) | |
621 | (error "%s is a directory." filename)) | |
f3e23606 RS |
622 | (let* ((buf (get-file-buffer filename)) |
623 | (truename (abbreviate-file-name (file-truename filename))) | |
624 | (number (nthcdr 10 (file-attributes truename))) | |
625 | ;; Find any buffer for a file which has same truename. | |
138c44f6 | 626 | (other (and (not buf) (find-buffer-visiting filename))) |
f3e23606 RS |
627 | error) |
628 | ;; Let user know if there is a buffer with the same truename. | |
138c44f6 KH |
629 | (if other |
630 | (progn | |
327bebe8 RS |
631 | (or nowarn |
632 | (string-equal filename (buffer-file-name other)) | |
633 | (message "%s and %s are the same file" | |
634 | filename (buffer-file-name other))) | |
138c44f6 KH |
635 | ;; Optionally also find that buffer. |
636 | (if (or find-file-existing-other-name find-file-visit-truename) | |
637 | (setq buf other)))) | |
b4da00e9 RM |
638 | (if buf |
639 | (or nowarn | |
640 | (verify-visited-file-modtime buf) | |
641 | (cond ((not (file-exists-p filename)) | |
642 | (error "File %s no longer exists!" filename)) | |
643 | ((yes-or-no-p | |
644 | (format | |
645 | (if (buffer-modified-p buf) | |
646 | "File %s changed on disk. Discard your edits? " | |
647 | "File %s changed on disk. Read the new version? ") | |
648 | (file-name-nondirectory filename))) | |
649 | (save-excursion | |
650 | (set-buffer buf) | |
651 | (revert-buffer t t))))) | |
652 | (save-excursion | |
f3e23606 RS |
653 | ;;; The truename stuff makes this obsolete. |
654 | ;;; (let* ((link-name (car (file-attributes filename))) | |
655 | ;;; (linked-buf (and (stringp link-name) | |
656 | ;;; (get-file-buffer link-name)))) | |
657 | ;;; (if (bufferp linked-buf) | |
658 | ;;; (message "Symbolic link to file in buffer %s" | |
659 | ;;; (buffer-name linked-buf)))) | |
b4da00e9 RM |
660 | (setq buf (create-file-buffer filename)) |
661 | (set-buffer buf) | |
662 | (erase-buffer) | |
663 | (condition-case () | |
664 | (insert-file-contents filename t) | |
665 | (file-error | |
666 | (setq error t) | |
667 | ;; Run find-file-not-found-hooks until one returns non-nil. | |
668 | (let ((hooks find-file-not-found-hooks)) | |
669 | (while (and hooks | |
455d9d28 RS |
670 | (not (and (funcall (car hooks)) |
671 | ;; If a hook succeeded, clear error. | |
672 | (progn (setq error nil) | |
673 | ;; Also exit the loop. | |
674 | t)))) | |
b4da00e9 | 675 | (setq hooks (cdr hooks)))))) |
f3e23606 | 676 | ;; Find the file's truename, and maybe use that as visited name. |
138c44f6 | 677 | (setq buffer-file-truename truename) |
1770543d RS |
678 | (setq buffer-file-number number) |
679 | ;; On VMS, we may want to remember which directory in a search list | |
680 | ;; the file was found in. | |
681 | (and (eq system-type 'vax-vms) | |
682 | (let (logical) | |
683 | (if (string-match ":" (file-name-directory filename)) | |
684 | (setq logical (substring (file-name-directory filename) | |
685 | 0 (match-beginning 0)))) | |
686 | (not (member logical find-file-not-true-dirname-list))) | |
687 | (setq buffer-file-name buffer-file-truename)) | |
1cc2fbeb | 688 | (if find-file-visit-truename |
27d0420c RS |
689 | (setq buffer-file-name |
690 | (setq filename | |
691 | (expand-file-name buffer-file-truename)))) | |
b4da00e9 RM |
692 | ;; Set buffer's default directory to that of the file. |
693 | (setq default-directory (file-name-directory filename)) | |
694 | ;; Turn off backup files for certain file names. Since | |
695 | ;; this is a permanent local, the major mode won't eliminate it. | |
696 | (and (not (funcall backup-enable-predicate buffer-file-name)) | |
697 | (progn | |
698 | (make-local-variable 'backup-inhibited) | |
699 | (setq backup-inhibited t))) | |
700 | (after-find-file error (not nowarn)))) | |
701 | buf))) | |
702 | \f | |
8cfb9d46 | 703 | (defun after-find-file (&optional error warn noauto) |
b4da00e9 RM |
704 | "Called after finding a file and by the default revert function. |
705 | Sets buffer mode, parses local variables. | |
8cfb9d46 | 706 | Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an |
b4da00e9 RM |
707 | error in reading the file. WARN non-nil means warn if there |
708 | exists an auto-save file more recent than the visited file. | |
8cfb9d46 | 709 | NOAUTO means don't mess with auto-save mode. |
b4da00e9 RM |
710 | Finishes by calling the functions in `find-file-hooks'." |
711 | (setq buffer-read-only (not (file-writable-p buffer-file-name))) | |
712 | (if noninteractive | |
713 | nil | |
714 | (let* (not-serious | |
715 | (msg | |
716 | (cond ((and error (file-attributes buffer-file-name)) | |
717 | (setq buffer-read-only t) | |
ff78d520 | 718 | "File exists, but cannot be read.") |
b4da00e9 RM |
719 | ((not buffer-read-only) |
720 | (if (and warn | |
721 | (file-newer-than-file-p (make-auto-save-file-name) | |
722 | buffer-file-name)) | |
723 | "Auto save file is newer; consider M-x recover-file" | |
724 | (setq not-serious t) | |
725 | (if error "(New file)" nil))) | |
726 | ((not error) | |
727 | (setq not-serious t) | |
728 | "Note: file is write protected") | |
729 | ((file-attributes (directory-file-name default-directory)) | |
730 | "File not found and directory write-protected") | |
4e43240a RS |
731 | ((file-exists-p (file-name-directory buffer-file-name)) |
732 | (setq buffer-read-only nil)) | |
b4da00e9 | 733 | (t |
5bbbceb1 | 734 | (setq buffer-read-only nil) |
4e43240a RS |
735 | (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name)))) |
736 | "Use M-x make-dir RET RET to create the directory" | |
737 | "Use C-u M-x make-dir RET RET to create directory and its parents"))))) | |
b4da00e9 RM |
738 | (if msg |
739 | (progn | |
740 | (message msg) | |
741 | (or not-serious (sit-for 1 nil t))))) | |
8cfb9d46 | 742 | (if (and auto-save-default (not noauto)) |
b4da00e9 RM |
743 | (auto-save-mode t))) |
744 | (normal-mode t) | |
745 | (mapcar 'funcall find-file-hooks)) | |
746 | ||
747 | (defun normal-mode (&optional find-file) | |
748 | "Choose the major mode for this buffer automatically. | |
749 | Also sets up any specified local variables of the file. | |
750 | Uses the visited file name, the -*- line, and the local variables spec. | |
751 | ||
752 | This function is called automatically from `find-file'. In that case, | |
753 | we may set up specified local variables depending on the value of | |
754 | `enable-local-variables': if it is t, we do; if it is nil, we don't; | |
755 | otherwise, we query. `enable-local-variables' is ignored if you | |
756 | run `normal-mode' explicitly." | |
757 | (interactive) | |
758 | (or find-file (funcall (or default-major-mode 'fundamental-mode))) | |
759 | (condition-case err | |
760 | (set-auto-mode) | |
761 | (error (message "File mode specification error: %s" | |
762 | (prin1-to-string err)))) | |
763 | (condition-case err | |
7b3f3dc2 JB |
764 | (let ((enable-local-variables (or (not find-file) |
765 | enable-local-variables))) | |
766 | (hack-local-variables)) | |
b4da00e9 RM |
767 | (error (message "File local-variables error: %s" |
768 | (prin1-to-string err))))) | |
769 | ||
7b3f3dc2 JB |
770 | (defvar auto-mode-alist (mapcar 'purecopy |
771 | '(("\\.text\\'" . text-mode) | |
772 | ("\\.c\\'" . c-mode) | |
773 | ("\\.h\\'" . c-mode) | |
774 | ("\\.tex\\'" . TeX-mode) | |
775 | ("\\.ltx\\'" . LaTeX-mode) | |
776 | ("\\.el\\'" . emacs-lisp-mode) | |
777 | ("\\.mm\\'" . nroff-mode) | |
778 | ("\\.me\\'" . nroff-mode) | |
c80d2575 ER |
779 | ("\\.ms\\'" . nroff-mode) |
780 | ("\\.man\\'" . nroff-mode) | |
7b3f3dc2 JB |
781 | ("\\.scm\\'" . scheme-mode) |
782 | ("\\.l\\'" . lisp-mode) | |
783 | ("\\.lisp\\'" . lisp-mode) | |
784 | ("\\.f\\'" . fortran-mode) | |
785 | ("\\.for\\'" . fortran-mode) | |
e70991d4 RS |
786 | ("\\.p\\'" . pascal-mode) |
787 | ("\\.pas\\'" . pascal-mode) | |
7b3f3dc2 JB |
788 | ("\\.mss\\'" . scribe-mode) |
789 | ("\\.pl\\'" . prolog-mode) | |
790 | ("\\.cc\\'" . c++-mode) | |
2b845a8c | 791 | ("\\.hh\\'" . c++-mode) |
7b3f3dc2 | 792 | ("\\.C\\'" . c++-mode) |
2b845a8c | 793 | ("\\.H\\'" . c++-mode) |
a3666e47 RS |
794 | ;;; ("\\.mk\\'" . makefile-mode) |
795 | ;;; ("[Mm]akefile" . makefile-mode) | |
7b3f3dc2 JB |
796 | ;;; Less common extensions come here |
797 | ;;; so more common ones above are found faster. | |
7ca1d903 RS |
798 | ("\\.texinfo\\'" . texinfo-mode) |
799 | ("\\.texi\\'" . texinfo-mode) | |
7b3f3dc2 JB |
800 | ("\\.s\\'" . asm-mode) |
801 | ("ChangeLog\\'" . change-log-mode) | |
bb157910 | 802 | ("change.log\\'" . change-log-mode) |
9bb004f6 | 803 | ("changelo\\'" . change-log-mode) |
7adc58e5 | 804 | ("ChangeLog.[0-9]+\\'" . change-log-mode) |
7b3f3dc2 | 805 | ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode) |
7adc58e5 RS |
806 | ;; The following should come after the ChangeLog pattern |
807 | ;; for the sake of ChangeLog.1, etc. | |
808 | ("\\.[12345678]\\'" . nroff-mode) | |
7b3f3dc2 JB |
809 | ("\\.TeX\\'" . TeX-mode) |
810 | ("\\.sty\\'" . LaTeX-mode) | |
811 | ("\\.bbl\\'" . LaTeX-mode) | |
812 | ("\\.bib\\'" . bibtex-mode) | |
813 | ("\\.article\\'" . text-mode) | |
814 | ("\\.letter\\'" . text-mode) | |
7ca1d903 | 815 | ("\\.tcl\\'" . tcl-mode) |
7b3f3dc2 JB |
816 | ("\\.lsp\\'" . lisp-mode) |
817 | ("\\.awk\\'" . awk-mode) | |
818 | ("\\.prolog\\'" . prolog-mode) | |
c80d2575 | 819 | ("\\.tar\\'" . tar-mode) |
7b3f3dc2 JB |
820 | ;; Mailer puts message to be edited in |
821 | ;; /tmp/Re.... or Message | |
822 | ("^/tmp/Re" . text-mode) | |
823 | ("/Message[0-9]*\\'" . text-mode) | |
824 | ;; some news reader is reported to use this | |
825 | ("^/tmp/fol/" . text-mode) | |
826 | ("\\.y\\'" . c-mode) | |
c80d2575 | 827 | ("\\.lex\\'" . c-mode) |
7b3f3dc2 JB |
828 | ("\\.oak\\'" . scheme-mode) |
829 | ("\\.scm.[0-9]*\\'" . scheme-mode) | |
278584fb RS |
830 | ("\\.sgm\\'" . sgml-mode) |
831 | ("\\.sgml\\'" . sgml-mode) | |
832 | ("\\.dtd\\'" . sgml-mode) | |
7b3f3dc2 JB |
833 | ;; .emacs following a directory delimiter |
834 | ;; in either Unix or VMS syntax. | |
835 | ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode) | |
bb157910 RS |
836 | ;; _emacs following a directory delimiter |
837 | ;; in MsDos syntax | |
838 | ("[:/]_emacs\\'" . emacs-lisp-mode) | |
7b3f3dc2 JB |
839 | ("\\.ml\\'" . lisp-mode))) |
840 | "\ | |
841 | Alist of filename patterns vs corresponding major mode functions. | |
81860e46 RS |
842 | Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION). |
843 | Visiting a file whose name matches REGEXP causes FUNCTION to be called. | |
844 | If the element has the form (REGEXP FUNCTION), then after calling | |
845 | FUNCTION, we delete the suffix that matched REGEXP and search the list | |
846 | again for another match.") | |
7b3f3dc2 | 847 | |
c907d156 RS |
848 | (defconst interpreter-mode-alist |
849 | '(("perl" . perl-mode) | |
850 | ("scope" . tcl-mode) | |
851 | ("wish" . tcl-mode) | |
852 | ("shell" . tcl-mode) | |
853 | ("form" . tcl-mode) | |
8db739de NF |
854 | ("tcl" . tcl-mode) |
855 | ("awk" . awk-mode) | |
856 | ("gawk" . awk-mode) | |
857 | ("scm" . scheme-mode)) | |
c907d156 RS |
858 | "Alist mapping interpreter names to major modes. |
859 | This alist applies to files whose first line starts with `#!'. | |
860 | Each element looks like (INTERPRETER . MODE). | |
861 | The car of each element is compared with | |
862 | the name of the interpreter specified in the first line. | |
863 | If it matches, mode MODE is selected.") | |
864 | ||
45fb3bb8 RS |
865 | (defconst inhibit-first-line-modes-regexps '("\\.tar$") |
866 | "List of regexps; if one matches a file name, don't look for `-*-'.") | |
a0c9f21b | 867 | |
bb157910 RS |
868 | (defvar user-init-file |
869 | "" ; set by command-line | |
870 | "File name including directory of user's initialization file.") | |
871 | ||
b4da00e9 RM |
872 | (defun set-auto-mode () |
873 | "Select major mode appropriate for current buffer. | |
7b3f3dc2 | 874 | This checks for a -*- mode tag in the buffer's text, or |
81860e46 | 875 | compares the filename against the entries in `auto-mode-alist'. It does |
7b3f3dc2 JB |
876 | not check for the \"mode:\" local variable in the Local Variables |
877 | section of the file; for that, use `hack-local-variables'. | |
878 | ||
f3e23606 | 879 | If `enable-local-variables' is nil, this function does not check for a |
7b3f3dc2 | 880 | -*- mode tag." |
b4da00e9 | 881 | ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- |
9fa7bfe5 | 882 | (let (beg end done) |
b4da00e9 RM |
883 | (save-excursion |
884 | (goto-char (point-min)) | |
885 | (skip-chars-forward " \t\n") | |
9fa7bfe5 RS |
886 | (and enable-local-variables |
887 | ;; Don't look for -*- if this file name matches any | |
45fb3bb8 RS |
888 | ;; of the regexps in inhibit-first-line-modes-regexps. |
889 | (let ((temp inhibit-first-line-modes-regexps)) | |
9fa7bfe5 RS |
890 | (while (and temp |
891 | (not (string-match (car temp) | |
892 | buffer-file-name))) | |
893 | (setq temp (cdr temp))) | |
894 | (not temp)) | |
895 | (search-forward "-*-" (save-excursion | |
896 | ;; If the file begins with "#!" | |
897 | ;; (exec interpreter magic), look | |
898 | ;; for mode frobs in the first two | |
899 | ;; lines. You cannot necessarily | |
900 | ;; put them in the first line of | |
901 | ;; such a file without screwing up | |
902 | ;; the interpreter invocation. | |
903 | (end-of-line (and (looking-at "^#!") 2)) | |
904 | (point)) t) | |
905 | (progn | |
906 | (skip-chars-forward " \t") | |
907 | (setq beg (point)) | |
908 | (search-forward "-*-" | |
909 | (save-excursion (end-of-line) (point)) | |
910 | t)) | |
911 | (progn | |
912 | (forward-char -3) | |
913 | (skip-chars-backward " \t") | |
914 | (setq end (point)) | |
915 | (goto-char beg) | |
916 | (if (save-excursion (search-forward ":" end t)) | |
917 | ;; Find all specifications for the `mode:' variable | |
57a0155c | 918 | ;; and execute them left to right. |
9fa7bfe5 RS |
919 | (while (let ((case-fold-search t)) |
920 | (search-forward "mode:" end t)) | |
921 | (skip-chars-forward " \t") | |
922 | (setq beg (point)) | |
923 | (if (search-forward ";" end t) | |
924 | (forward-char -1) | |
925 | (goto-char end)) | |
926 | (skip-chars-backward " \t") | |
927 | (funcall (intern (concat (downcase (buffer-substring beg (point))) "-mode")))) | |
928 | ;; Simple -*-MODE-*- case. | |
929 | (funcall (intern (concat (downcase (buffer-substring beg end)) "-mode")))) | |
930 | (setq done t))) | |
931 | ;; If we didn't find a mode from a -*- line, try using the file name. | |
932 | (if (and (not done) buffer-file-name) | |
81860e46 RS |
933 | (let ((name buffer-file-name) |
934 | (case-fold-search (eq system-type 'vax-vms)) | |
935 | (keep-going t)) | |
936 | ;; Remove backup-suffixes from file name. | |
937 | (setq name (file-name-sans-versions name)) | |
938 | (while keep-going | |
939 | (setq keep-going nil) | |
940 | (let ((alist auto-mode-alist) | |
941 | (mode nil)) | |
942 | ;; Find first matching alist entry. | |
943 | (while (and (not mode) alist) | |
944 | (if (string-match (car (car alist)) name) | |
945 | (if (and (consp (cdr (car alist))) | |
946 | (nth 2 (car alist))) | |
947 | (progn | |
948 | (setq mode (car (cdr (car alist))) | |
949 | name (substring name 0 (match-beginning 0)) | |
950 | keep-going t)) | |
951 | (setq mode (cdr (car alist)) | |
952 | keep-going nil))) | |
953 | (setq alist (cdr alist))) | |
c907d156 RS |
954 | (if mode |
955 | (funcall mode) | |
956 | ;; If we can't deduce a mode from the file name, | |
957 | ;; look for an interpreter specified in the first line. | |
958 | (let ((interpreter | |
959 | (save-excursion | |
960 | (goto-char (point-min)) | |
961 | (if (looking-at "#! *") | |
962 | (progn | |
963 | (goto-char (match-end 0)) | |
964 | (buffer-substring (point) | |
965 | (progn (end-of-line) (point)))) | |
966 | ""))) | |
967 | elt) | |
968 | ;; Map interpreter name to a mode. | |
969 | (setq elt (assoc (file-name-nondirectory interpreter) | |
970 | interpreter-mode-alist)) | |
971 | (if elt | |
972 | (funcall (cdr elt)))))))))))) | |
b4da00e9 | 973 | |
f3e23606 RS |
974 | (defun hack-local-variables-prop-line () |
975 | ;; Set local variables specified in the -*- line. | |
9fa7bfe5 RS |
976 | ;; Ignore any specification for `mode:'; |
977 | ;; set-auto-mode should already have handled that. | |
f3e23606 RS |
978 | (save-excursion |
979 | (goto-char (point-min)) | |
13a66180 KH |
980 | (let ((result nil) |
981 | (end (save-excursion (end-of-line (and (looking-at "^#!") 2)) (point)))) | |
f3e23606 RS |
982 | ;; Parse the -*- line into the `result' alist. |
983 | (cond ((not (search-forward "-*-" end t)) | |
984 | ;; doesn't have one. | |
985 | nil) | |
986 | ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)") | |
13a66180 KH |
987 | ;; Simple form: "-*- MODENAME -*-". Already handled. |
988 | nil) | |
f3e23606 RS |
989 | (t |
990 | ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' | |
991 | ;; (last ";" is optional). | |
992 | (save-excursion | |
993 | (if (search-forward "-*-" end t) | |
994 | (setq end (- (point) 3)) | |
995 | (error "-*- not terminated before end of line"))) | |
996 | (while (< (point) end) | |
997 | (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*") | |
998 | (error "malformed -*- line")) | |
999 | (goto-char (match-end 0)) | |
1000 | (let ((key (intern (downcase (buffer-substring | |
1001 | (match-beginning 1) | |
1002 | (match-end 1))))) | |
1003 | (val (save-restriction | |
1004 | (narrow-to-region (point) end) | |
1005 | (read (current-buffer))))) | |
13a66180 KH |
1006 | (or (eq key 'mode) |
1007 | (setq result (cons (cons key val) result))) | |
f3e23606 RS |
1008 | (skip-chars-forward " \t;"))) |
1009 | (setq result (nreverse result)))) | |
f3e23606 RS |
1010 | |
1011 | (if (and result | |
1012 | (or (eq enable-local-variables t) | |
1013 | (and enable-local-variables | |
1014 | (save-window-excursion | |
6f931919 RS |
1015 | (condition-case nil |
1016 | (switch-to-buffer (current-buffer)) | |
1017 | (error | |
1018 | ;; If we fail to switch in the selected window, | |
1019 | ;; it is probably a minibuffer. | |
1020 | ;; So try another window. | |
1021 | (condition-case nil | |
1022 | (switch-to-buffer-other-window (current-buffer)) | |
1023 | (error | |
1024 | (switch-to-buffer-other-frame (current-buffer)))))) | |
f3e23606 RS |
1025 | (y-or-n-p (format "Set local variables as specified in -*- line of %s? " |
1026 | (file-name-nondirectory buffer-file-name))))))) | |
1027 | (while result | |
13a66180 | 1028 | (hack-one-local-variable (car (car result)) (cdr (car result))) |
9fa7bfe5 | 1029 | (setq result (cdr result))))))) |
f3e23606 | 1030 | |
7b3f3dc2 | 1031 | (defun hack-local-variables () |
5792c834 | 1032 | "Parse and put into effect this buffer's local variables spec." |
f3e23606 | 1033 | (hack-local-variables-prop-line) |
b4da00e9 RM |
1034 | ;; Look for "Local variables:" line in last page. |
1035 | (save-excursion | |
1036 | (goto-char (point-max)) | |
1037 | (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) | |
1038 | (if (let ((case-fold-search t)) | |
1039 | (and (search-forward "Local Variables:" nil t) | |
7b3f3dc2 | 1040 | (or (eq enable-local-variables t) |
b4da00e9 RM |
1041 | (and enable-local-variables |
1042 | (save-window-excursion | |
1043 | (switch-to-buffer (current-buffer)) | |
1044 | (save-excursion | |
1045 | (beginning-of-line) | |
1046 | (set-window-start (selected-window) (point))) | |
1047 | (y-or-n-p (format "Set local variables as specified at end of %s? " | |
51de78c1 RS |
1048 | (if buffer-file-name |
1049 | (file-name-nondirectory | |
1050 | buffer-file-name) | |
1051 | (concat "buffer " | |
1052 | (buffer-name)))))))))) | |
b4da00e9 | 1053 | (let ((continue t) |
2bba782c RS |
1054 | prefix prefixlen suffix beg |
1055 | (enable-local-eval enable-local-eval)) | |
b4da00e9 RM |
1056 | ;; The prefix is what comes before "local variables:" in its line. |
1057 | ;; The suffix is what comes after "local variables:" in its line. | |
1058 | (skip-chars-forward " \t") | |
1059 | (or (eolp) | |
1060 | (setq suffix (buffer-substring (point) | |
1061 | (progn (end-of-line) (point))))) | |
1062 | (goto-char (match-beginning 0)) | |
1063 | (or (bolp) | |
1064 | (setq prefix | |
1065 | (buffer-substring (point) | |
1066 | (progn (beginning-of-line) (point))))) | |
7b3f3dc2 | 1067 | |
b4da00e9 RM |
1068 | (if prefix (setq prefixlen (length prefix) |
1069 | prefix (regexp-quote prefix))) | |
1070 | (if suffix (setq suffix (concat (regexp-quote suffix) "$"))) | |
1071 | (while continue | |
1072 | ;; Look at next local variable spec. | |
1073 | (if selective-display (re-search-forward "[\n\C-m]") | |
1074 | (forward-line 1)) | |
1075 | ;; Skip the prefix, if any. | |
1076 | (if prefix | |
1077 | (if (looking-at prefix) | |
1078 | (forward-char prefixlen) | |
1079 | (error "Local variables entry is missing the prefix"))) | |
1080 | ;; Find the variable name; strip whitespace. | |
1081 | (skip-chars-forward " \t") | |
1082 | (setq beg (point)) | |
1083 | (skip-chars-forward "^:\n") | |
1084 | (if (eolp) (error "Missing colon in local variables entry")) | |
1085 | (skip-chars-backward " \t") | |
1086 | (let* ((str (buffer-substring beg (point))) | |
1087 | (var (read str)) | |
1088 | val) | |
1089 | ;; Setting variable named "end" means end of list. | |
1090 | (if (string-equal (downcase str) "end") | |
1091 | (setq continue nil) | |
1092 | ;; Otherwise read the variable value. | |
1093 | (skip-chars-forward "^:") | |
1094 | (forward-char 1) | |
1095 | (setq val (read (current-buffer))) | |
1096 | (skip-chars-backward "\n") | |
1097 | (skip-chars-forward " \t") | |
1098 | (or (if suffix (looking-at suffix) (eolp)) | |
1099 | (error "Local variables entry is terminated incorrectly")) | |
1100 | ;; Set the variable. "Variables" mode and eval are funny. | |
f3e23606 RS |
1101 | (hack-one-local-variable var val)))))))) |
1102 | ||
1103 | (defconst ignored-local-variables | |
1104 | '(enable-local-eval) | |
1105 | "Variables to be ignored in a file's local variable spec.") | |
1106 | ||
be0218a8 RS |
1107 | ;; Get confirmation before setting these variables as locals in a file. |
1108 | (put 'eval 'risky-local-variable t) | |
1109 | (put 'file-name-handler-alist 'risky-local-variable t) | |
1110 | (put 'minor-mode-map-alist 'risky-local-variable t) | |
1111 | (put 'after-load-alist 'risky-local-variable t) | |
1112 | ||
d0bd3513 RS |
1113 | (defun hack-one-local-variable-quotep (exp) |
1114 | (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp)))) | |
1115 | ||
f3e23606 RS |
1116 | ;; "Set" one variable in a local variables spec. |
1117 | ;; A few variable names are treated specially. | |
1118 | (defun hack-one-local-variable (var val) | |
1119 | (cond ((eq var 'mode) | |
1120 | (funcall (intern (concat (downcase (symbol-name val)) | |
1121 | "-mode")))) | |
1122 | ((memq var ignored-local-variables) | |
1123 | nil) | |
1124 | ;; "Setting" eval means either eval it or do nothing. | |
7d3221d7 | 1125 | ;; Likewise for setting hook variables. |
be0218a8 | 1126 | ((or (get var 'risky-local-variable) |
37fd9666 RS |
1127 | (string-match "-hooks?$\\|-functions?$\\|-forms?$" |
1128 | (symbol-name var))) | |
d0bd3513 RS |
1129 | ;; Permit evaling a put of a harmless property |
1130 | ;; if the args do nothing tricky. | |
1131 | (if (or (and (eq var 'eval) | |
1132 | (consp val) | |
1133 | (eq (car val) 'put) | |
1134 | (hack-one-local-variable-quotep (nth 1 val)) | |
1135 | (hack-one-local-variable-quotep (nth 2 val)) | |
1136 | ;; Only allow safe values of lisp-indent-hook; | |
1137 | ;; not functions. | |
1138 | (or (numberp (nth 3 val)) | |
dc417415 | 1139 | (equal (nth 3 val) ''defun)) |
d0bd3513 RS |
1140 | (memq (nth 1 (nth 2 val)) |
1141 | '(lisp-indent-hook))) | |
1142 | ;; Permit eval if not root and user says ok. | |
1143 | (and (not (string= (user-login-name) "root")) | |
1144 | (or (eq enable-local-eval t) | |
1145 | (and enable-local-eval | |
1146 | (save-window-excursion | |
1147 | (switch-to-buffer (current-buffer)) | |
1148 | (save-excursion | |
1149 | (beginning-of-line) | |
1150 | (set-window-start (selected-window) (point))) | |
1151 | (setq enable-local-eval | |
1152 | (y-or-n-p (format "Process `eval' or hook local variables in file %s? " | |
1153 | (file-name-nondirectory buffer-file-name))))))))) | |
7d3221d7 RS |
1154 | (if (eq var 'eval) |
1155 | (save-excursion (eval val)) | |
1156 | (make-local-variable var) | |
1157 | (set var val)) | |
f3e23606 RS |
1158 | (message "Ignoring `eval:' in file's local variables"))) |
1159 | ;; Ordinary variable, really set it. | |
1160 | (t (make-local-variable var) | |
1161 | (set var val)))) | |
1162 | ||
b4da00e9 RM |
1163 | \f |
1164 | (defun set-visited-file-name (filename) | |
1165 | "Change name of file visited in current buffer to FILENAME. | |
1166 | The next time the buffer is saved it will go in the newly specified file. | |
1167 | nil or empty string as argument means make buffer not be visiting any file. | |
1168 | Remember to delete the initial contents of the minibuffer | |
1169 | if you wish to pass an empty string as the argument." | |
1170 | (interactive "FSet visited file name: ") | |
1171 | (if filename | |
1172 | (setq filename | |
1173 | (if (string-equal filename "") | |
1174 | nil | |
1175 | (expand-file-name filename)))) | |
1176 | (or (equal filename buffer-file-name) | |
b4da00e9 | 1177 | (progn |
6a8f71e7 | 1178 | (and filename (lock-buffer filename)) |
b4da00e9 RM |
1179 | (unlock-buffer))) |
1180 | (setq buffer-file-name filename) | |
1181 | (if filename ; make buffer name reflect filename. | |
5bbbceb1 | 1182 | (let ((new-name (file-name-nondirectory buffer-file-name))) |
b4da00e9 RM |
1183 | (if (string= new-name "") |
1184 | (error "Empty file name")) | |
1185 | (if (eq system-type 'vax-vms) | |
1186 | (setq new-name (downcase new-name))) | |
1187 | (setq default-directory (file-name-directory buffer-file-name)) | |
6f931919 RS |
1188 | (or (string= new-name (buffer-name)) |
1189 | (rename-buffer new-name t)))) | |
b4da00e9 RM |
1190 | (setq buffer-backed-up nil) |
1191 | (clear-visited-file-modtime) | |
f3e23606 RS |
1192 | (if filename |
1193 | (progn | |
1194 | (setq buffer-file-truename | |
1195 | (abbreviate-file-name (file-truename buffer-file-name))) | |
1196 | (if find-file-visit-truename | |
1197 | (setq buffer-file-name buffer-file-truename)) | |
1198 | (setq buffer-file-number (nth 10 (file-attributes buffer-file-name)))) | |
1199 | (setq buffer-file-truename nil buffer-file-number nil)) | |
b4da00e9 RM |
1200 | ;; write-file-hooks is normally used for things like ftp-find-file |
1201 | ;; that visit things that are not local files as if they were files. | |
1202 | ;; Changing to visit an ordinary local file instead should flush the hook. | |
1203 | (kill-local-variable 'write-file-hooks) | |
c9dca4e0 | 1204 | (kill-local-variable 'local-write-file-hooks) |
b4da00e9 RM |
1205 | (kill-local-variable 'revert-buffer-function) |
1206 | (kill-local-variable 'backup-inhibited) | |
ee81c959 RS |
1207 | ;; If buffer was read-only because of version control, |
1208 | ;; that reason is gone now, so make it writable. | |
1209 | (if vc-mode | |
1210 | (setq buffer-read-only nil)) | |
1211 | (kill-local-variable 'vc-mode) | |
b4da00e9 RM |
1212 | ;; Turn off backup files for certain file names. |
1213 | ;; Since this is a permanent local, the major mode won't eliminate it. | |
1214 | (and (not (funcall backup-enable-predicate buffer-file-name)) | |
1215 | (progn | |
1216 | (make-local-variable 'backup-inhibited) | |
1217 | (setq backup-inhibited t))) | |
c77a81cf RS |
1218 | (let ((oauto buffer-auto-save-file-name)) |
1219 | ;; If auto-save was not already on, turn it on if appropriate. | |
1220 | (if (not buffer-auto-save-file-name) | |
1221 | (and buffer-file-name auto-save-default | |
1222 | (auto-save-mode t)) | |
1223 | ;; If auto save is on, start using a new name. | |
1224 | ;; We deliberately don't rename or delete the old auto save | |
1225 | ;; for the old visited file name. This is because perhaps | |
1226 | ;; the user wants to save the new state and then compare with the | |
1227 | ;; previous state from the auto save file. | |
1228 | (setq buffer-auto-save-file-name | |
1229 | (make-auto-save-file-name))) | |
1230 | ;; Rename the old auto save file if any. | |
1231 | (and oauto buffer-auto-save-file-name | |
e6f0e76c | 1232 | (file-exists-p oauto) |
c77a81cf | 1233 | (rename-file oauto buffer-auto-save-file-name t))) |
b4da00e9 RM |
1234 | (if buffer-file-name |
1235 | (set-buffer-modified-p t))) | |
1236 | ||
1237 | (defun write-file (filename) | |
1238 | "Write current buffer into file FILENAME. | |
41f48cb1 RS |
1239 | Makes buffer visit that file, and marks it not modified. |
1240 | If the buffer is already visiting a file, you can specify | |
1241 | a directory name as FILENAME, to write a file of the same | |
1242 | old name in that directory." | |
b4da00e9 RM |
1243 | ;; (interactive "FWrite file: ") |
1244 | (interactive | |
1245 | (list (if buffer-file-name | |
1246 | (read-file-name "Write file: " | |
1247 | nil nil nil nil) | |
1248 | (read-file-name "Write file: " | |
1249 | (cdr (assq 'default-directory | |
1250 | (buffer-local-variables))) | |
1251 | nil nil (buffer-name))))) | |
1252 | (or (null filename) (string-equal filename "") | |
41f48cb1 RS |
1253 | (progn |
1254 | ;; If arg is just a directory, | |
1255 | ;; use same file name, but in that directory. | |
1256 | (if (and (file-directory-p filename) buffer-file-name) | |
1257 | (setq filename (concat (file-name-as-directory filename) | |
1258 | (file-name-nondirectory buffer-file-name)))) | |
1259 | (set-visited-file-name filename))) | |
b4da00e9 RM |
1260 | (set-buffer-modified-p t) |
1261 | (save-buffer)) | |
1262 | \f | |
1263 | (defun backup-buffer () | |
1264 | "Make a backup of the disk file visited by the current buffer, if appropriate. | |
1265 | This is normally done before saving the buffer the first time. | |
1266 | If the value is non-nil, it is the result of `file-modes' on the original | |
1267 | file; this means that the caller, after saving the buffer, should change | |
1268 | the modes of the new file to agree with the old modes." | |
1269 | (if (and make-backup-files (not backup-inhibited) | |
1270 | (not buffer-backed-up) | |
1271 | (file-exists-p buffer-file-name) | |
1272 | (memq (aref (elt (file-attributes buffer-file-name) 8) 0) | |
1273 | '(?- ?l))) | |
1274 | (let ((real-file-name buffer-file-name) | |
1275 | backup-info backupname targets setmodes) | |
1276 | ;; If specified name is a symbolic link, chase it to the target. | |
1277 | ;; Thus we make the backups in the directory where the real file is. | |
5dadeb29 | 1278 | (setq real-file-name (file-chase-links real-file-name)) |
b4da00e9 RM |
1279 | (setq backup-info (find-backup-file-name real-file-name) |
1280 | backupname (car backup-info) | |
1281 | targets (cdr backup-info)) | |
1282 | ;;; (if (file-directory-p buffer-file-name) | |
1283 | ;;; (error "Cannot save buffer in directory %s" buffer-file-name)) | |
1284 | (condition-case () | |
1285 | (let ((delete-old-versions | |
1286 | ;; If have old versions to maybe delete, | |
1287 | ;; ask the user to confirm now, before doing anything. | |
1288 | ;; But don't actually delete til later. | |
1289 | (and targets | |
6d74b528 | 1290 | (or (eq trim-versions-without-asking t) (eq trim-versions-without-asking nil)) |
b4da00e9 RM |
1291 | (or trim-versions-without-asking |
1292 | (y-or-n-p (format "Delete excess backup versions of %s? " | |
1293 | real-file-name)))))) | |
1294 | ;; Actually write the back up file. | |
1295 | (condition-case () | |
1296 | (if (or file-precious-flag | |
1297 | ; (file-symlink-p buffer-file-name) | |
1298 | backup-by-copying | |
1299 | (and backup-by-copying-when-linked | |
1300 | (> (file-nlinks real-file-name) 1)) | |
1301 | (and backup-by-copying-when-mismatch | |
1302 | (let ((attr (file-attributes real-file-name))) | |
1303 | (or (nth 9 attr) | |
1304 | (/= (nth 2 attr) (user-uid)))))) | |
65864ae0 RS |
1305 | (condition-case () |
1306 | (copy-file real-file-name backupname t t) | |
1307 | (file-error | |
1308 | ;; If copying fails because file BACKUPNAME | |
1309 | ;; is not writable, delete that file and try again. | |
1310 | (if (and (file-exists-p backupname) | |
1311 | (not (file-writable-p backupname))) | |
1312 | (delete-file backupname)) | |
1313 | (copy-file real-file-name backupname t t))) | |
1314 | ;; rename-file should delete old backup. | |
b4da00e9 RM |
1315 | (rename-file real-file-name backupname t) |
1316 | (setq setmodes (file-modes backupname))) | |
1317 | (file-error | |
1318 | ;; If trouble writing the backup, write it in ~. | |
1319 | (setq backupname (expand-file-name "~/%backup%~")) | |
1320 | (message "Cannot write backup file; backing up in ~/%%backup%%~") | |
1321 | (sleep-for 1) | |
65864ae0 RS |
1322 | (condition-case () |
1323 | (copy-file real-file-name backupname t t) | |
1324 | (file-error | |
1325 | ;; If copying fails because file BACKUPNAME | |
1326 | ;; is not writable, delete that file and try again. | |
1327 | (if (and (file-exists-p backupname) | |
1328 | (not (file-writable-p backupname))) | |
1329 | (delete-file backupname)) | |
1330 | (copy-file real-file-name backupname t t))))) | |
b4da00e9 RM |
1331 | (setq buffer-backed-up t) |
1332 | ;; Now delete the old versions, if desired. | |
1333 | (if delete-old-versions | |
1334 | (while targets | |
1335 | (condition-case () | |
1336 | (delete-file (car targets)) | |
1337 | (file-error nil)) | |
1338 | (setq targets (cdr targets)))) | |
1339 | setmodes) | |
1340 | (file-error nil))))) | |
1341 | ||
c3554e95 | 1342 | (defun file-name-sans-versions (name &optional keep-backup-version) |
b4da00e9 RM |
1343 | "Return FILENAME sans backup versions or strings. |
1344 | This is a separate procedure so your site-init or startup file can | |
c3554e95 RS |
1345 | redefine it. |
1346 | If the optional argument KEEP-BACKUP-VERSION is non-nil, | |
1347 | we do not remove backup version numbers, only true file version numbers." | |
6eaebaa2 | 1348 | (let ((handler (find-file-name-handler name 'file-name-sans-versions))) |
c3554e95 RS |
1349 | (if handler |
1350 | (funcall handler 'file-name-sans-versions name keep-backup-version) | |
1351 | (substring name 0 | |
1352 | (if (eq system-type 'vax-vms) | |
1353 | ;; VMS version number is (a) semicolon, optional | |
1354 | ;; sign, zero or more digits or (b) period, option | |
1355 | ;; sign, zero or more digits, provided this is the | |
1356 | ;; second period encountered outside of the | |
1357 | ;; device/directory part of the file name. | |
26add1bf KH |
1358 | (or (string-match ";[-+]?[0-9]*\\'" name) |
1359 | (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'" | |
c3554e95 RS |
1360 | name) |
1361 | (match-beginning 1)) | |
1362 | (length name)) | |
1363 | (if keep-backup-version | |
1364 | (length name) | |
1365 | (or (string-match "\\.~[0-9]+~\\'" name) | |
1366 | (string-match "~\\'" name) | |
1367 | (length name)))))))) | |
b4da00e9 RM |
1368 | |
1369 | (defun make-backup-file-name (file) | |
1370 | "Create the non-numeric backup file name for FILE. | |
1371 | This is a separate function so you can redefine it for customization." | |
bb157910 RS |
1372 | (if (eq system-type 'ms-dos) |
1373 | (let ((fn (file-name-nondirectory file))) | |
1374 | (concat (file-name-directory file) | |
1375 | (if (string-match "\\([^.]*\\)\\(\\..*\\)?" fn) | |
1376 | (substring fn 0 (match-end 1))) | |
1377 | ".bak")) | |
1378 | (concat file "~"))) | |
b4da00e9 RM |
1379 | |
1380 | (defun backup-file-name-p (file) | |
1381 | "Return non-nil if FILE is a backup file name (numeric or not). | |
1382 | This is a separate function so you can redefine it for customization. | |
1383 | You may need to redefine `file-name-sans-versions' as well." | |
bb157910 RS |
1384 | (if (eq system-type 'ms-dos) |
1385 | (string-match "\\.bak$" file) | |
1386 | (string-match "~$" file))) | |
b4da00e9 | 1387 | |
2d051399 RS |
1388 | ;; This is used in various files. |
1389 | ;; The usage of bv-length is not very clean, | |
1390 | ;; but I can't see a good alternative, | |
1391 | ;; so as of now I am leaving it alone. | |
1392 | (defun backup-extract-version (fn) | |
1393 | "Given the name of a numeric backup file, return the backup number. | |
1394 | Uses the free variable `bv-length', whose value should be | |
1395 | the index in the name where the version number begins." | |
1396 | (if (and (string-match "[0-9]+~$" fn bv-length) | |
1397 | (= (match-beginning 0) bv-length)) | |
1398 | (string-to-int (substring fn bv-length -1)) | |
1399 | 0)) | |
1400 | ||
b4da00e9 RM |
1401 | ;; I believe there is no need to alter this behavior for VMS; |
1402 | ;; since backup files are not made on VMS, it should not get called. | |
1403 | (defun find-backup-file-name (fn) | |
1404 | "Find a file name for a backup file, and suggestions for deletions. | |
1405 | Value is a list whose car is the name for the backup file | |
1406 | and whose cdr is a list of old versions to consider deleting now." | |
1407 | (if (eq version-control 'never) | |
1408 | (list (make-backup-file-name fn)) | |
1409 | (let* ((base-versions (concat (file-name-nondirectory fn) ".~")) | |
1410 | (bv-length (length base-versions)) | |
7a5a26a6 RS |
1411 | possibilities |
1412 | (versions nil) | |
1413 | (high-water-mark 0) | |
1414 | (deserve-versions-p nil) | |
1415 | (number-to-delete 0)) | |
1416 | (condition-case () | |
1417 | (setq possibilities (file-name-all-completions | |
1418 | base-versions | |
1419 | (file-name-directory fn)) | |
1420 | versions (sort (mapcar | |
1421 | (function backup-extract-version) | |
1422 | possibilities) | |
1423 | '<) | |
1424 | high-water-mark (apply 'max 0 versions) | |
1425 | deserve-versions-p (or version-control | |
1426 | (> high-water-mark 0)) | |
1427 | number-to-delete (- (length versions) | |
1428 | kept-old-versions kept-new-versions -1)) | |
1429 | (file-error | |
1430 | (setq possibilities nil))) | |
b4da00e9 RM |
1431 | (if (not deserve-versions-p) |
1432 | (list (make-backup-file-name fn)) | |
1433 | (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~") | |
9115e938 NF |
1434 | (if (and (> number-to-delete 0) |
1435 | ;; Delete nothing if there is overflow | |
1436 | ;; in the number of versions to keep. | |
1437 | (>= (+ kept-new-versions kept-old-versions -1) 0)) | |
b4da00e9 RM |
1438 | (mapcar (function (lambda (n) |
1439 | (concat fn ".~" (int-to-string n) "~"))) | |
1440 | (let ((v (nthcdr kept-old-versions versions))) | |
1441 | (rplacd (nthcdr (1- number-to-delete) v) ()) | |
1442 | v)))))))) | |
1443 | ||
b4da00e9 RM |
1444 | (defun file-nlinks (filename) |
1445 | "Return number of names file FILENAME has." | |
1446 | (car (cdr (file-attributes filename)))) | |
6c636af9 RM |
1447 | |
1448 | (defun file-relative-name (filename &optional directory) | |
1449 | "Convert FILENAME to be relative to DIRECTORY (default: default-directory)." | |
1450 | (setq filename (expand-file-name filename) | |
77fb6aed RS |
1451 | directory (file-name-as-directory (expand-file-name |
1452 | (or directory default-directory)))) | |
b1fa544f KH |
1453 | (let ((ancestor "")) |
1454 | (while (not (string-match (concat "^" (regexp-quote directory)) filename)) | |
1455 | (setq directory (file-name-directory (substring directory 0 -1)) | |
1456 | ancestor (concat "../" ancestor))) | |
1457 | (concat ancestor (substring filename (match-end 0))))) | |
b4da00e9 RM |
1458 | \f |
1459 | (defun save-buffer (&optional args) | |
1460 | "Save current buffer in visited file if modified. Versions described below. | |
1461 | By default, makes the previous version into a backup file | |
1462 | if previously requested or if this is the first save. | |
1463 | With 1 or 3 \\[universal-argument]'s, marks this version | |
1464 | to become a backup when the next save is done. | |
1465 | With 2 or 3 \\[universal-argument]'s, | |
1466 | unconditionally makes the previous version into a backup file. | |
1467 | With argument of 0, never makes the previous version into a backup file. | |
1468 | ||
1469 | If a file's name is FOO, the names of its numbered backup versions are | |
1470 | FOO.~i~ for various integers i. A non-numbered backup file is called FOO~. | |
1471 | Numeric backups (rather than FOO~) will be made if value of | |
1472 | `version-control' is not the atom `never' and either there are already | |
1473 | numeric versions of the file being backed up, or `version-control' is | |
1474 | non-nil. | |
1475 | We don't want excessive versions piling up, so there are variables | |
1476 | `kept-old-versions', which tells Emacs how many oldest versions to keep, | |
1477 | and `kept-new-versions', which tells how many newest versions to keep. | |
1478 | Defaults are 2 old versions and 2 new. | |
1479 | `dired-kept-versions' controls dired's clean-directory (.) command. | |
1480 | If `trim-versions-without-asking' is nil, system will query user | |
1481 | before trimming versions. Otherwise it does it silently." | |
1482 | (interactive "p") | |
1483 | (let ((modp (buffer-modified-p)) | |
1484 | (large (> (buffer-size) 50000)) | |
1485 | (make-backup-files (and make-backup-files (not (eq args 0))))) | |
1486 | (and modp (memq args '(16 64)) (setq buffer-backed-up nil)) | |
1487 | (if (and modp large) (message "Saving file %s..." (buffer-file-name))) | |
1488 | (basic-save-buffer) | |
1489 | (and modp (memq args '(4 64)) (setq buffer-backed-up nil)))) | |
1490 | ||
1491 | (defun delete-auto-save-file-if-necessary (&optional force) | |
1492 | "Delete auto-save file for current buffer if `delete-auto-save-files' is t. | |
1493 | Normally delete only if the file was written by this Emacs since | |
1494 | the last real save, but optional arg FORCE non-nil means delete anyway." | |
1495 | (and buffer-auto-save-file-name delete-auto-save-files | |
1496 | (not (string= buffer-file-name buffer-auto-save-file-name)) | |
1497 | (or force (recent-auto-save-p)) | |
1498 | (progn | |
1499 | (condition-case () | |
1500 | (delete-file buffer-auto-save-file-name) | |
1501 | (file-error nil)) | |
1502 | (set-buffer-auto-saved)))) | |
1503 | ||
1504 | (defun basic-save-buffer () | |
1505 | "Save the current buffer in its visited file, if it has been modified." | |
1506 | (interactive) | |
1507 | (if (buffer-modified-p) | |
1508 | (let ((recent-save (recent-auto-save-p)) | |
1509 | setmodes tempsetmodes) | |
1510 | ;; On VMS, rename file and buffer to get rid of version number. | |
1511 | (if (and (eq system-type 'vax-vms) | |
1512 | (not (string= buffer-file-name | |
1513 | (file-name-sans-versions buffer-file-name)))) | |
1514 | (let (buffer-new-name) | |
1515 | ;; Strip VMS version number before save. | |
1516 | (setq buffer-file-name | |
1517 | (file-name-sans-versions buffer-file-name)) | |
1518 | ;; Construct a (unique) buffer name to correspond. | |
1519 | (let ((buf (create-file-buffer (downcase buffer-file-name)))) | |
1520 | (setq buffer-new-name (buffer-name buf)) | |
1521 | (kill-buffer buf)) | |
1522 | (rename-buffer buffer-new-name))) | |
1523 | ;; If buffer has no file name, ask user for one. | |
1524 | (or buffer-file-name | |
2943f983 RS |
1525 | (set-visited-file-name |
1526 | (expand-file-name (read-file-name "File to save in: ") nil))) | |
b4da00e9 RM |
1527 | (or (verify-visited-file-modtime (current-buffer)) |
1528 | (not (file-exists-p buffer-file-name)) | |
1529 | (yes-or-no-p | |
1530 | (format "%s has changed since visited or saved. Save anyway? " | |
1531 | (file-name-nondirectory buffer-file-name))) | |
1532 | (error "Save not confirmed")) | |
1533 | (save-restriction | |
1534 | (widen) | |
1535 | (and (> (point-max) 1) | |
1536 | (/= (char-after (1- (point-max))) ?\n) | |
425c7aa8 KH |
1537 | (not (and (eq selective-display t) |
1538 | (= (char-after (1- (point-max))) ?\r))) | |
b4da00e9 RM |
1539 | (or (eq require-final-newline t) |
1540 | (and require-final-newline | |
1541 | (y-or-n-p | |
1542 | (format "Buffer %s does not end in newline. Add one? " | |
1543 | (buffer-name))))) | |
1544 | (save-excursion | |
1545 | (goto-char (point-max)) | |
1546 | (insert ?\n))) | |
c9dca4e0 RS |
1547 | (let ((hooks (append write-contents-hooks local-write-file-hooks |
1548 | write-file-hooks)) | |
b4da00e9 RM |
1549 | (done nil)) |
1550 | (while (and hooks | |
1551 | (not (setq done (funcall (car hooks))))) | |
1552 | (setq hooks (cdr hooks))) | |
1553 | ;; If a hook returned t, file is already "written". | |
1554 | (cond ((not done) | |
87d26afc | 1555 | (setq setmodes (basic-save-buffer-1))))) |
f3e23606 | 1556 | (setq buffer-file-number (nth 10 (file-attributes buffer-file-name))) |
b4da00e9 RM |
1557 | (if setmodes |
1558 | (condition-case () | |
f3e23606 | 1559 | (set-file-modes buffer-file-name setmodes) |
b4da00e9 RM |
1560 | (error nil)))) |
1561 | ;; If the auto-save file was recent before this command, | |
1562 | ;; delete it now. | |
1563 | (delete-auto-save-file-if-necessary recent-save) | |
1bf3d475 | 1564 | (run-hooks 'after-save-hook)) |
b4da00e9 RM |
1565 | (message "(No changes need to be saved)"))) |
1566 | ||
87d26afc RS |
1567 | ;; This does the "real job" of writing a buffer into its visited file |
1568 | ;; and making a backup file. This is what is normally done | |
1569 | ;; but inhibited if one of write-file-hooks returns non-nil. | |
1570 | ;; It returns a value to store in setmodes. | |
1571 | (defun basic-save-buffer-1 () | |
1572 | (let (tempsetmodes setmodes) | |
1573 | (if (not (file-writable-p buffer-file-name)) | |
1574 | (let ((dir (file-name-directory buffer-file-name))) | |
1575 | (if (not (file-directory-p dir)) | |
1576 | (error "%s is not a directory" dir) | |
1577 | (if (not (file-exists-p buffer-file-name)) | |
1578 | (error "Directory %s write-protected" dir) | |
1579 | (if (yes-or-no-p | |
1580 | (format "File %s is write-protected; try to save anyway? " | |
1581 | (file-name-nondirectory | |
1582 | buffer-file-name))) | |
1583 | (setq tempsetmodes t) | |
1584 | (error "Attempt to save to a file which you aren't allowed to write")))))) | |
1585 | (or buffer-backed-up | |
1586 | (setq setmodes (backup-buffer))) | |
1587 | (if file-precious-flag | |
1588 | ;; If file is precious, write temp name, then rename it. | |
1589 | (let ((dir (file-name-directory buffer-file-name)) | |
1590 | (realname buffer-file-name) | |
1591 | tempname temp nogood i succeed) | |
1592 | (setq i 0) | |
1593 | (setq nogood t) | |
1594 | ;; Find the temporary name to write under. | |
1595 | (while nogood | |
1596 | (setq tempname (format "%s#tmp#%d" dir i)) | |
1597 | (setq nogood (file-exists-p tempname)) | |
1598 | (setq i (1+ i))) | |
1599 | (unwind-protect | |
1600 | (progn (clear-visited-file-modtime) | |
1601 | (write-region (point-min) (point-max) | |
1602 | tempname nil realname) | |
1603 | (setq succeed t)) | |
1604 | ;; If writing the temp file fails, | |
1605 | ;; delete the temp file. | |
1606 | (or succeed (delete-file tempname))) | |
1607 | ;; Since we have created an entirely new file | |
1608 | ;; and renamed it, make sure it gets the | |
1609 | ;; right permission bits set. | |
1610 | (setq setmodes (file-modes buffer-file-name)) | |
1611 | ;; We succeeded in writing the temp file, | |
1612 | ;; so rename it. | |
1613 | (rename-file tempname buffer-file-name t)) | |
1614 | ;; If file not writable, see if we can make it writable | |
1615 | ;; temporarily while we write it. | |
1616 | ;; But no need to do so if we have just backed it up | |
1617 | ;; (setmodes is set) because that says we're superseding. | |
1618 | (cond ((and tempsetmodes (not setmodes)) | |
1619 | ;; Change the mode back, after writing. | |
1620 | (setq setmodes (file-modes buffer-file-name)) | |
1621 | (set-file-modes buffer-file-name 511))) | |
1622 | (write-region (point-min) (point-max) | |
1623 | buffer-file-name nil t)) | |
1624 | setmodes)) | |
1625 | ||
b4da00e9 RM |
1626 | (defun save-some-buffers (&optional arg exiting) |
1627 | "Save some modified file-visiting buffers. Asks user about each one. | |
5bbbceb1 JB |
1628 | Optional argument (the prefix) non-nil means save all with no questions. |
1629 | Optional second argument EXITING means ask about certain non-file buffers | |
1630 | as well as about file buffers." | |
b4da00e9 | 1631 | (interactive "P") |
907482b9 | 1632 | (save-window-excursion |
5625c2fc RS |
1633 | (let ((files-done |
1634 | (map-y-or-n-p | |
1635 | (function | |
1636 | (lambda (buffer) | |
1637 | (and (buffer-modified-p buffer) | |
1638 | (or | |
1639 | (buffer-file-name buffer) | |
1640 | (and exiting | |
1641 | (progn | |
1642 | (set-buffer buffer) | |
1643 | (and buffer-offer-save (> (buffer-size) 0))))) | |
1644 | (if arg | |
1645 | t | |
1646 | (if (buffer-file-name buffer) | |
1647 | (format "Save file %s? " | |
1648 | (buffer-file-name buffer)) | |
1649 | (format "Save buffer %s? " | |
1650 | (buffer-name buffer))))))) | |
1651 | (function | |
1652 | (lambda (buffer) | |
1653 | (set-buffer buffer) | |
1654 | (save-buffer))) | |
1655 | (buffer-list) | |
1656 | '("buffer" "buffers" "save") | |
1657 | (list (list ?\C-r (lambda (buf) | |
1658 | (view-buffer buf) | |
1659 | (setq view-exit-action | |
1660 | '(lambda (ignore) | |
1661 | (exit-recursive-edit))) | |
1662 | (recursive-edit) | |
1663 | ;; Return nil to ask about BUF again. | |
1664 | nil) | |
1665 | "display the current buffer")))) | |
1666 | (abbrevs-done | |
1667 | (and save-abbrevs abbrevs-changed | |
1668 | (progn | |
1669 | (if (or arg | |
1670 | (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name))) | |
1671 | (write-abbrev-file nil)) | |
1672 | ;; Don't keep bothering user if he says no. | |
1673 | (setq abbrevs-changed nil) | |
1674 | t)))) | |
1675 | (or (> files-done 0) abbrevs-done | |
1676 | (message "(No files need saving)"))))) | |
b4da00e9 RM |
1677 | \f |
1678 | (defun not-modified (&optional arg) | |
1679 | "Mark current buffer as unmodified, not needing to be saved. | |
1680 | With prefix arg, mark buffer as modified, so \\[save-buffer] will save." | |
1681 | (interactive "P") | |
1682 | (message (if arg "Modification-flag set" | |
1683 | "Modification-flag cleared")) | |
1684 | (set-buffer-modified-p arg)) | |
1685 | ||
1686 | (defun toggle-read-only (&optional arg) | |
1687 | "Change whether this buffer is visiting its file read-only. | |
1688 | With arg, set read-only iff arg is positive." | |
1689 | (interactive "P") | |
1690 | (setq buffer-read-only | |
1691 | (if (null arg) | |
1692 | (not buffer-read-only) | |
1693 | (> (prefix-numeric-value arg) 0))) | |
1694 | ;; Force mode-line redisplay | |
1695 | (set-buffer-modified-p (buffer-modified-p))) | |
1696 | ||
1697 | (defun insert-file (filename) | |
1698 | "Insert contents of file FILENAME into buffer after point. | |
1699 | Set mark after the inserted text. | |
1700 | ||
1701 | This function is meant for the user to run interactively. | |
1702 | Don't call it from programs! Use `insert-file-contents' instead. | |
1703 | \(Its calling sequence is different; see its documentation)." | |
6f931919 | 1704 | (interactive "*fInsert file: ") |
3be6243a RS |
1705 | (if (file-directory-p filename) |
1706 | (signal 'file-error (list "Opening input file" "file is a directory" | |
1707 | filename))) | |
b4da00e9 RM |
1708 | (let ((tem (insert-file-contents filename))) |
1709 | (push-mark (+ (point) (car (cdr tem)))))) | |
1710 | ||
1711 | (defun append-to-file (start end filename) | |
1712 | "Append the contents of the region to the end of file FILENAME. | |
1713 | When called from a function, expects three arguments, | |
1714 | START, END and FILENAME. START and END are buffer positions | |
1715 | saying what text to write." | |
1716 | (interactive "r\nFAppend to file: ") | |
1717 | (write-region start end filename t)) | |
1718 | ||
1719 | (defun file-newest-backup (filename) | |
1720 | "Return most recent backup file for FILENAME or nil if no backups exist." | |
1721 | (let* ((filename (expand-file-name filename)) | |
1722 | (file (file-name-nondirectory filename)) | |
1723 | (dir (file-name-directory filename)) | |
1724 | (comp (file-name-all-completions file dir)) | |
1725 | newest) | |
1726 | (while comp | |
1727 | (setq file (concat dir (car comp)) | |
1728 | comp (cdr comp)) | |
1729 | (if (and (backup-file-name-p file) | |
1730 | (or (null newest) (file-newer-than-file-p file newest))) | |
1731 | (setq newest file))) | |
1732 | newest)) | |
1733 | ||
1734 | (defun rename-uniquely () | |
1735 | "Rename current buffer to a similar name not already taken. | |
1736 | This function is useful for creating multiple shell process buffers | |
1737 | or multiple mail buffers, etc." | |
1738 | (interactive) | |
1739 | (let* ((new-buf (generate-new-buffer (buffer-name))) | |
1740 | (name (buffer-name new-buf))) | |
1741 | (kill-buffer new-buf) | |
1742 | (rename-buffer name) | |
1743 | (set-buffer-modified-p (buffer-modified-p)))) ; force mode line update | |
5bbbceb1 | 1744 | |
4e43240a | 1745 | (defun make-directory (dir &optional parents) |
5ce8bb89 RS |
1746 | "Create the directory DIR and any nonexistent parent dirs. |
1747 | Interactively, the default choice of directory to create | |
1748 | is the current default directory for file names. | |
1749 | That is useful when you have visited a file in a nonexistint directory. | |
1750 | ||
1751 | Noninteractively, the second (optional) argument PARENTS says whether | |
1752 | to create parent directories if they don't exist." | |
1753 | (interactive | |
1754 | (list (read-file-name "Make directory: " default-directory default-directory | |
1755 | nil nil) | |
1756 | t)) | |
6eaebaa2 | 1757 | (let ((handler (find-file-name-handler dir 'make-directory))) |
4e43240a RS |
1758 | (if handler |
1759 | (funcall handler 'make-directory dir parents) | |
1760 | (if (not parents) | |
1761 | (make-directory-internal dir) | |
1762 | (let ((dir (directory-file-name (expand-file-name dir))) | |
1763 | create-list) | |
1764 | (while (not (file-exists-p dir)) | |
1765 | (setq create-list (cons dir create-list) | |
1766 | dir (directory-file-name (file-name-directory dir)))) | |
1767 | (while create-list | |
1768 | (make-directory-internal (car create-list)) | |
1769 | (setq create-list (cdr create-list)))))))) | |
b4da00e9 RM |
1770 | \f |
1771 | (put 'revert-buffer-function 'permanent-local t) | |
1772 | (defvar revert-buffer-function nil | |
1773 | "Function to use to revert this buffer, or nil to do the default.") | |
1774 | ||
1775 | (put 'revert-buffer-insert-file-contents-function 'permanent-local t) | |
1776 | (defvar revert-buffer-insert-file-contents-function nil | |
1777 | "Function to use to insert contents when reverting this buffer. | |
1778 | Gets two args, first the nominal file name to use, | |
1779 | and second, t if reading the auto-save file.") | |
1780 | ||
1ab31687 | 1781 | (defun revert-buffer (&optional ignore-auto noconfirm) |
b4da00e9 RM |
1782 | "Replace the buffer text with the text of the visited file on disk. |
1783 | This undoes all changes since the file was visited or saved. | |
8c0e7b73 JB |
1784 | With a prefix argument, offer to revert from latest auto-save file, if |
1785 | that is more recent than the visited file. | |
1ab31687 | 1786 | |
fc51b251 | 1787 | When called from lisp, The first argument is IGNORE-AUTO; only offer |
1ab31687 JB |
1788 | to revert from the auto-save file when this is nil. Note that the |
1789 | sense of this argument is the reverse of the prefix argument, for the | |
1790 | sake of backward compatibility. IGNORE-AUTO is optional, defaulting | |
1791 | to nil. | |
1792 | ||
1793 | Optional second argument NOCONFIRM means don't ask for confirmation at | |
1794 | all. | |
b4da00e9 | 1795 | |
8c0e7b73 | 1796 | If the value of `revert-buffer-function' is non-nil, it is called to |
fb6208a6 RS |
1797 | do the work. |
1798 | ||
4fbf62ee KH |
1799 | The default revert function runs the hook `before-revert-hook' at the |
1800 | beginning and `after-revert-hook' at the end." | |
1ab31687 JB |
1801 | ;; I admit it's odd to reverse the sense of the prefix argument, but |
1802 | ;; there is a lot of code out there which assumes that the first | |
1803 | ;; argument should be t to avoid consulting the auto-save file, and | |
1804 | ;; there's no straightforward way to encourage authors to notice a | |
1805 | ;; reversal of the argument sense. So I'm just changing the user | |
1806 | ;; interface, but leaving the programmatic interface the same. | |
1807 | (interactive (list (not prefix-arg))) | |
b4da00e9 | 1808 | (if revert-buffer-function |
1ab31687 | 1809 | (funcall revert-buffer-function ignore-auto noconfirm) |
b4da00e9 | 1810 | (let* ((opoint (point)) |
1ab31687 JB |
1811 | (auto-save-p (and (not ignore-auto) |
1812 | (recent-auto-save-p) | |
b4da00e9 RM |
1813 | buffer-auto-save-file-name |
1814 | (file-readable-p buffer-auto-save-file-name) | |
1815 | (y-or-n-p | |
1816 | "Buffer has been auto-saved recently. Revert from auto-save file? "))) | |
1817 | (file-name (if auto-save-p | |
1818 | buffer-auto-save-file-name | |
1819 | buffer-file-name))) | |
1820 | (cond ((null file-name) | |
1821 | (error "Buffer does not seem to be associated with any file")) | |
1822 | ((or noconfirm | |
1823 | (yes-or-no-p (format "Revert buffer from file %s? " | |
1824 | file-name))) | |
4fbf62ee | 1825 | (run-hooks 'before-revert-hook) |
b4da00e9 RM |
1826 | ;; If file was backed up but has changed since, |
1827 | ;; we shd make another backup. | |
1828 | (and (not auto-save-p) | |
5bbbceb1 | 1829 | (not (verify-visited-file-modtime (current-buffer))) |
b4da00e9 RM |
1830 | (setq buffer-backed-up nil)) |
1831 | ;; Get rid of all undo records for this buffer. | |
1832 | (or (eq buffer-undo-list t) | |
1833 | (setq buffer-undo-list nil)) | |
1834 | (let ((buffer-read-only nil) | |
1835 | ;; Don't make undo records for the reversion. | |
1836 | (buffer-undo-list t)) | |
1837 | (if revert-buffer-insert-file-contents-function | |
1838 | (funcall revert-buffer-insert-file-contents-function | |
1839 | file-name auto-save-p) | |
1840 | (if (not (file-exists-p file-name)) | |
1841 | (error "File %s no longer exists!" file-name)) | |
1842 | ;; Bind buffer-file-name to nil | |
1843 | ;; so that we don't try to lock the file. | |
1844 | (let ((buffer-file-name nil)) | |
1845 | (or auto-save-p | |
f9456b0a | 1846 | (unlock-buffer))) |
55fbbe8e | 1847 | (widen) |
f9456b0a RS |
1848 | (insert-file-contents file-name (not auto-save-p) |
1849 | nil nil t))) | |
b4da00e9 | 1850 | (goto-char (min opoint (point-max))) |
8cfb9d46 | 1851 | (after-find-file nil nil t) |
4fbf62ee KH |
1852 | (run-hooks 'after-revert-hook) |
1853 | t))))) | |
b4da00e9 RM |
1854 | |
1855 | (defun recover-file (file) | |
1856 | "Visit file FILE, but get contents from its last auto-save file." | |
1857 | (interactive | |
1858 | (let ((prompt-file buffer-file-name) | |
1859 | (file-name nil) | |
1860 | (file-dir nil)) | |
1861 | (and prompt-file | |
1862 | (setq file-name (file-name-nondirectory prompt-file) | |
1863 | file-dir (file-name-directory prompt-file))) | |
1864 | (list (read-file-name "Recover file: " | |
1865 | file-dir nil nil file-name)))) | |
1866 | (setq file (expand-file-name file)) | |
f7da6740 RS |
1867 | (if (auto-save-file-name-p (file-name-nondirectory file)) |
1868 | (error "%s is an auto-save file" file)) | |
b4da00e9 RM |
1869 | (let ((file-name (let ((buffer-file-name file)) |
1870 | (make-auto-save-file-name)))) | |
1871 | (cond ((not (file-newer-than-file-p file-name file)) | |
1872 | (error "Auto-save file %s not current" file-name)) | |
1873 | ((save-window-excursion | |
1874 | (if (not (eq system-type 'vax-vms)) | |
1875 | (with-output-to-temp-buffer "*Directory*" | |
1876 | (buffer-disable-undo standard-output) | |
1877 | (call-process "ls" nil standard-output nil | |
2dc2b736 RS |
1878 | (if (file-symlink-p file) "-lL" "-l") |
1879 | file file-name))) | |
b4da00e9 RM |
1880 | (yes-or-no-p (format "Recover auto save file %s? " file-name))) |
1881 | (switch-to-buffer (find-file-noselect file t)) | |
1882 | (let ((buffer-read-only nil)) | |
1883 | (erase-buffer) | |
1884 | (insert-file-contents file-name nil)) | |
8cfb9d46 | 1885 | (after-find-file nil nil t)) |
b4da00e9 RM |
1886 | (t (error "Recover-file cancelled."))))) |
1887 | ||
1888 | (defun kill-some-buffers () | |
1889 | "For each buffer, ask whether to kill it." | |
1890 | (interactive) | |
1891 | (let ((list (buffer-list))) | |
1892 | (while list | |
1893 | (let* ((buffer (car list)) | |
1894 | (name (buffer-name buffer))) | |
1895 | (and (not (string-equal name "")) | |
1896 | (/= (aref name 0) ? ) | |
1897 | (yes-or-no-p | |
1898 | (format "Buffer %s %s. Kill? " | |
1899 | name | |
1900 | (if (buffer-modified-p buffer) | |
1901 | "HAS BEEN EDITED" "is unmodified"))) | |
1902 | (kill-buffer buffer))) | |
1903 | (setq list (cdr list))))) | |
1904 | \f | |
1905 | (defun auto-save-mode (arg) | |
1906 | "Toggle auto-saving of contents of current buffer. | |
f3e23606 | 1907 | With prefix argument ARG, turn auto-saving on if positive, else off." |
b4da00e9 RM |
1908 | (interactive "P") |
1909 | (setq buffer-auto-save-file-name | |
1910 | (and (if (null arg) | |
1911 | (not buffer-auto-save-file-name) | |
1912 | (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))) | |
1913 | (if (and buffer-file-name auto-save-visited-file-name | |
1914 | (not buffer-read-only)) | |
1915 | buffer-file-name | |
1916 | (make-auto-save-file-name)))) | |
0b7f1ef2 RS |
1917 | ;; If -1 was stored here, to temporarily turn off saving, |
1918 | ;; turn it back on. | |
1919 | (and (< buffer-saved-size 0) | |
1920 | (setq buffer-saved-size 0)) | |
b4da00e9 RM |
1921 | (if (interactive-p) |
1922 | (message "Auto-save %s (in this buffer)" | |
1923 | (if buffer-auto-save-file-name "on" "off"))) | |
1924 | buffer-auto-save-file-name) | |
1925 | ||
1926 | (defun rename-auto-save-file () | |
1927 | "Adjust current buffer's auto save file name for current conditions. | |
1928 | Also rename any existing auto save file, if it was made in this session." | |
1929 | (let ((osave buffer-auto-save-file-name)) | |
1930 | (setq buffer-auto-save-file-name | |
1931 | (make-auto-save-file-name)) | |
1932 | (if (and osave buffer-auto-save-file-name | |
1933 | (not (string= buffer-auto-save-file-name buffer-file-name)) | |
1934 | (not (string= buffer-auto-save-file-name osave)) | |
1935 | (file-exists-p osave) | |
1936 | (recent-auto-save-p)) | |
1937 | (rename-file osave buffer-auto-save-file-name t)))) | |
1938 | ||
1939 | (defun make-auto-save-file-name () | |
1940 | "Return file name to use for auto-saves of current buffer. | |
1941 | Does not consider `auto-save-visited-file-name' as that variable is checked | |
1942 | before calling this function. You can redefine this for customization. | |
1943 | See also `auto-save-file-name-p'." | |
1944 | (if buffer-file-name | |
1945 | (concat (file-name-directory buffer-file-name) | |
1946 | "#" | |
1947 | (file-name-nondirectory buffer-file-name) | |
1948 | "#") | |
1949 | ;; For non-file bfr, use bfr name and Emacs pid. | |
1950 | (expand-file-name (format "#%s#%s#" (buffer-name) (make-temp-name ""))))) | |
1951 | ||
1952 | (defun auto-save-file-name-p (filename) | |
1953 | "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'. | |
1954 | FILENAME should lack slashes. You can redefine this for customization." | |
1955 | (string-match "^#.*#$" filename)) | |
1956 | \f | |
1957 | (defconst list-directory-brief-switches | |
1958 | (if (eq system-type 'vax-vms) "" "-CF") | |
1959 | "*Switches for list-directory to pass to `ls' for brief listing,") | |
1960 | ||
1961 | (defconst list-directory-verbose-switches | |
1962 | (if (eq system-type 'vax-vms) | |
1963 | "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)" | |
1964 | "-l") | |
1965 | "*Switches for list-directory to pass to `ls' for verbose listing,") | |
1966 | ||
1967 | (defun list-directory (dirname &optional verbose) | |
1968 | "Display a list of files in or matching DIRNAME, a la `ls'. | |
1969 | DIRNAME is globbed by the shell if necessary. | |
1970 | Prefix arg (second arg if noninteractive) means supply -l switch to `ls'. | |
1971 | Actions controlled by variables `list-directory-brief-switches' | |
1972 | and `list-directory-verbose-switches'." | |
1973 | (interactive (let ((pfx current-prefix-arg)) | |
1974 | (list (read-file-name (if pfx "List directory (verbose): " | |
1975 | "List directory (brief): ") | |
1976 | nil default-directory nil) | |
1977 | pfx))) | |
1978 | (let ((switches (if verbose list-directory-verbose-switches | |
1979 | list-directory-brief-switches))) | |
1980 | (or dirname (setq dirname default-directory)) | |
1981 | (setq dirname (expand-file-name dirname)) | |
1982 | (with-output-to-temp-buffer "*Directory*" | |
1983 | (buffer-disable-undo standard-output) | |
1984 | (princ "Directory ") | |
1985 | (princ dirname) | |
1986 | (terpri) | |
c3554e95 RS |
1987 | (save-excursion |
1988 | (set-buffer "*Directory*") | |
1989 | (let ((wildcard (not (file-directory-p dirname)))) | |
1990 | (insert-directory dirname switches wildcard (not wildcard))))))) | |
1991 | ||
1992 | (defvar insert-directory-program "ls" | |
1993 | "Absolute or relative name of the `ls' program used by `insert-directory'.") | |
1994 | ||
1995 | ;; insert-directory | |
1996 | ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and | |
1997 | ;; FULL-DIRECTORY-P is nil. | |
1998 | ;; The single line of output must display FILE's name as it was | |
1999 | ;; given, namely, an absolute path name. | |
2000 | ;; - must insert exactly one line for each file if WILDCARD or | |
2001 | ;; FULL-DIRECTORY-P is t, plus one optional "total" line | |
2002 | ;; before the file lines, plus optional text after the file lines. | |
2003 | ;; Lines are delimited by "\n", so filenames containing "\n" are not | |
2004 | ;; allowed. | |
2005 | ;; File lines should display the basename. | |
2006 | ;; - must be consistent with | |
2007 | ;; - functions dired-move-to-filename, (these two define what a file line is) | |
2008 | ;; dired-move-to-end-of-filename, | |
2009 | ;; dired-between-files, (shortcut for (not (dired-move-to-filename))) | |
2010 | ;; dired-insert-headerline | |
2011 | ;; dired-after-subdir-garbage (defines what a "total" line is) | |
2012 | ;; - variable dired-subdir-regexp | |
2013 | (defun insert-directory (file switches &optional wildcard full-directory-p) | |
a18b7c81 | 2014 | "Insert directory listing for FILE, formatted according to SWITCHES. |
c3554e95 RS |
2015 | Leaves point after the inserted text. |
2016 | Optional third arg WILDCARD means treat FILE as shell wildcard. | |
2017 | Optional fourth arg FULL-DIRECTORY-P means file is a directory and | |
2018 | switches do not contain `d', so that a full listing is expected. | |
2019 | ||
2020 | This works by running a directory listing program | |
406e12d9 | 2021 | whose name is in the variable `insert-directory-program'. |
c3554e95 | 2022 | If WILDCARD, it also runs the shell specified by `shell-file-name'." |
6eaebaa2 | 2023 | (let ((handler (find-file-name-handler file 'insert-directory))) |
c3554e95 RS |
2024 | (if handler |
2025 | (funcall handler 'insert-directory file switches | |
2026 | wildcard full-directory-p) | |
b4da00e9 | 2027 | (if (eq system-type 'vax-vms) |
c3554e95 RS |
2028 | (vms-read-directory file switches (current-buffer)) |
2029 | (if wildcard | |
a9fa0bd5 RS |
2030 | ;; Run ls in the directory of the file pattern we asked for. |
2031 | (let ((default-directory | |
2032 | (if (file-name-absolute-p file) | |
2033 | (file-name-directory file) | |
6e2dc7cb RS |
2034 | (file-name-directory (expand-file-name file)))) |
2035 | (pattern (file-name-nondirectory file)) | |
2036 | (beg 0)) | |
2037 | ;; Quote some characters that have special meanings in shells; | |
2038 | ;; but don't quote the wildcards--we want them to be special. | |
2039 | ;; We also currently don't quote the quoting characters | |
2040 | ;; in case people want to use them explicitly to quote | |
2041 | ;; wildcard characters. | |
2042 | (while (string-match "[ \t\n;<>&|{}()#$]" pattern beg) | |
2043 | (setq pattern | |
2044 | (concat (substring pattern 0 (match-beginning 0)) | |
2045 | "\\" | |
2046 | (substring pattern (match-beginning 0))) | |
2047 | beg (1+ (match-end 0)))) | |
c3554e95 RS |
2048 | (call-process shell-file-name nil t nil |
2049 | "-c" (concat insert-directory-program | |
2050 | " -d " switches " " | |
6e2dc7cb | 2051 | pattern))) |
a18b7c81 JB |
2052 | ;; SunOS 4.1.3, SVr4 and others need the "." to list the |
2053 | ;; directory if FILE is a symbolic link. | |
2054 | (call-process insert-directory-program nil t nil switches | |
2055 | (if full-directory-p | |
2056 | (concat (file-name-as-directory file) ".") | |
2057 | file))))))) | |
b4da00e9 | 2058 | |
88902b35 RS |
2059 | (defvar kill-emacs-query-functions nil |
2060 | "Functions to call with no arguments to query about killing Emacs.") | |
2061 | ||
b4da00e9 RM |
2062 | (defun save-buffers-kill-emacs (&optional arg) |
2063 | "Offer to save each buffer, then kill this Emacs process. | |
2064 | With prefix arg, silently save all file-visiting buffers, then kill." | |
2065 | (interactive "P") | |
2066 | (save-some-buffers arg t) | |
2067 | (and (or (not (memq t (mapcar (function | |
2068 | (lambda (buf) (and (buffer-file-name buf) | |
2069 | (buffer-modified-p buf)))) | |
2070 | (buffer-list)))) | |
2071 | (yes-or-no-p "Modified buffers exist; exit anyway? ")) | |
2072 | (or (not (fboundp 'process-list)) | |
2073 | ;; process-list is not defined on VMS. | |
2074 | (let ((processes (process-list)) | |
2075 | active) | |
2076 | (while processes | |
528415e7 | 2077 | (and (memq (process-status (car processes)) '(run stop open)) |
b4da00e9 RM |
2078 | (let ((val (process-kill-without-query (car processes)))) |
2079 | (process-kill-without-query (car processes) val) | |
2080 | val) | |
2081 | (setq active t)) | |
2082 | (setq processes (cdr processes))) | |
2083 | (or (not active) | |
2084 | (yes-or-no-p "Active processes exist; kill them and exit anyway? ")))) | |
88902b35 RS |
2085 | ;; Query the user for other things, perhaps. |
2086 | (let ((functions kill-emacs-query-functions) | |
2087 | (yes t)) | |
2088 | (while (and functions yes) | |
2089 | (setq yes (and yes (funcall (car functions)))) | |
2090 | (setq functions (cdr functions))) | |
2091 | yes) | |
b4da00e9 RM |
2092 | (kill-emacs))) |
2093 | \f | |
2094 | (define-key ctl-x-map "\C-f" 'find-file) | |
2095 | (define-key ctl-x-map "\C-q" 'toggle-read-only) | |
2096 | (define-key ctl-x-map "\C-r" 'find-file-read-only) | |
2097 | (define-key ctl-x-map "\C-v" 'find-alternate-file) | |
2098 | (define-key ctl-x-map "\C-s" 'save-buffer) | |
2099 | (define-key ctl-x-map "s" 'save-some-buffers) | |
2100 | (define-key ctl-x-map "\C-w" 'write-file) | |
2101 | (define-key ctl-x-map "i" 'insert-file) | |
2102 | (define-key esc-map "~" 'not-modified) | |
2103 | (define-key ctl-x-map "\C-d" 'list-directory) | |
2104 | (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs) | |
2105 | ||
2106 | (define-key ctl-x-4-map "f" 'find-file-other-window) | |
2107 | (define-key ctl-x-4-map "r" 'find-file-read-only-other-window) | |
2108 | (define-key ctl-x-4-map "\C-f" 'find-file-other-window) | |
2109 | (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window) | |
924f0a24 | 2110 | (define-key ctl-x-4-map "\C-o" 'display-buffer) |
5bbbceb1 | 2111 | |
f98955ea JB |
2112 | (define-key ctl-x-5-map "b" 'switch-to-buffer-other-frame) |
2113 | (define-key ctl-x-5-map "f" 'find-file-other-frame) | |
2114 | (define-key ctl-x-5-map "\C-f" 'find-file-other-frame) | |
2115 | (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame) | |
c0274f38 ER |
2116 | |
2117 | ;;; files.el ends here |