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