Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; files.el --- file input and output commands for Emacs |
2 | ||
9596811a RS |
3 | ;; Copyright (C) 1985, 86, 87, 92, 93, |
4 | ;; 94, 95, 1996 Free Software Foundation, Inc. | |
b4da00e9 | 5 | |
3a801d0c ER |
6 | ;; Maintainer: FSF |
7 | ||
b4da00e9 RM |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
b4da00e9 RM |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
693f800d RS |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
b4da00e9 | 24 | |
e41b2db1 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; Defines most of Emacs's file- and directory-handling functions, | |
28 | ;; including basic file visiting, backup generation, link handling, | |
29 | ;; ITS-id version control, load- and write-hook handling, and the like. | |
30 | ||
e5167999 ER |
31 | ;;; Code: |
32 | ||
d7fa5aa2 | 33 | (defvar delete-auto-save-files t |
d1739e52 | 34 | "*Non-nil means delete auto-save file when a buffer is saved or killed.") |
b4da00e9 | 35 | |
d7fa5aa2 | 36 | (defvar directory-abbrev-alist |
b4da00e9 RM |
37 | nil |
38 | "*Alist of abbreviations for file directories. | |
39 | A list of elements of the form (FROM . TO), each meaning to replace | |
40 | FROM with TO when it appears in a directory name. This replacement is | |
41 | done when setting up the default directory of a newly visited file. | |
42 | *Every* FROM string should start with `^'. | |
43 | ||
65151a1b RS |
44 | Do not use `~' in the TO strings. |
45 | They should be ordinary absolute directory names. | |
46 | ||
b4da00e9 RM |
47 | Use this feature when you have directories which you normally refer to |
48 | via absolute symbolic links. Make TO the name of the link, and FROM | |
49 | the name it is linked to.") | |
50 | ||
51 | ;;; Turn off backup files on VMS since it has version numbers. | |
d7fa5aa2 | 52 | (defvar make-backup-files (not (eq system-type 'vax-vms)) |
4ea836c1 | 53 | "*Non-nil means make a backup of a file the first time it is saved. |
b4da00e9 RM |
54 | This can be done by renaming the file or by copying. |
55 | ||
56 | Renaming means that Emacs renames the existing file so that it is a | |
57 | backup file, then writes the buffer into a new file. Any other names | |
58 | that the old file had will now refer to the backup file. The new file | |
59 | is owned by you and its group is defaulted. | |
60 | ||
61 | Copying means that Emacs copies the existing file into the backup | |
62 | file, then writes the buffer on top of the existing file. Any other | |
63 | names that the old file had will now refer to the new (edited) file. | |
64 | The file's owner and group are unchanged. | |
65 | ||
66 | The choice of renaming or copying is controlled by the variables | |
67 | `backup-by-copying', `backup-by-copying-when-linked' and | |
f862241d | 68 | `backup-by-copying-when-mismatch'. See also `backup-inhibited'.") |
b4da00e9 RM |
69 | |
70 | ;; Do this so that local variables based on the file name | |
71 | ;; are not overridden by the major mode. | |
72 | (defvar backup-inhibited nil | |
f862241d RS |
73 | "Non-nil means don't make a backup, regardless of the other parameters. |
74 | This variable is intended for use by making it local to a buffer. | |
75 | But it is local only if you make it local.") | |
b4da00e9 RM |
76 | (put 'backup-inhibited 'permanent-local t) |
77 | ||
d7fa5aa2 | 78 | (defvar backup-by-copying nil |
b4da00e9 RM |
79 | "*Non-nil means always use copying to create backup files. |
80 | See documentation of variable `make-backup-files'.") | |
81 | ||
d7fa5aa2 | 82 | (defvar backup-by-copying-when-linked nil |
b4da00e9 RM |
83 | "*Non-nil means use copying to create backups for files with multiple names. |
84 | This causes the alternate names to refer to the latest version as edited. | |
85 | This variable is relevant only if `backup-by-copying' is nil.") | |
86 | ||
d7fa5aa2 | 87 | (defvar backup-by-copying-when-mismatch nil |
b4da00e9 RM |
88 | "*Non-nil means create backups by copying if this preserves owner or group. |
89 | Renaming may still be used (subject to control of other variables) | |
90 | when it would not result in changing the owner or group of the file; | |
91 | that is, for files which are owned by you and whose group matches | |
92 | the default for a new file created there by you. | |
93 | This variable is relevant only if `backup-by-copying' is nil.") | |
94 | ||
95 | (defvar backup-enable-predicate | |
96 | '(lambda (name) | |
97 | (or (< (length name) 5) | |
98 | (not (string-equal "/tmp/" (substring name 0 5))))) | |
99 | "Predicate that looks at a file name and decides whether to make backups. | |
100 | Called with an absolute file name as argument, it returns t to enable backup.") | |
101 | ||
d7fa5aa2 | 102 | (defvar buffer-offer-save nil |
b4da00e9 RM |
103 | "*Non-nil in a buffer means offer to save the buffer on exit |
104 | even if the buffer is not visiting a file. | |
105 | Automatically local in all buffers.") | |
106 | (make-variable-buffer-local 'buffer-offer-save) | |
107 | ||
d7fa5aa2 | 108 | (defvar find-file-existing-other-name t |
f3e23606 RS |
109 | "*Non-nil means find a file under alternative names, in existing buffers. |
110 | This means if any existing buffer is visiting the file you want | |
111 | under another name, you get the existing buffer instead of a new buffer.") | |
112 | ||
d7fa5aa2 | 113 | (defvar find-file-visit-truename nil |
f3e23606 RS |
114 | "*Non-nil means visit a file under its truename. |
115 | The truename of a file is found by chasing all links | |
116 | both at the file level and at the levels of the containing directories.") | |
117 | ||
d7fa5aa2 RS |
118 | (defvar find-file-revert-without-query |
119 | nil | |
ebeb898f RS |
120 | "*Specify which files should be reverted without query. |
121 | The value is a list of regular expressions. | |
122 | If the file name matches one of these regular expressions, | |
123 | then `find-file' reverts the file without querying | |
124 | if the file has changed on disk and you have not edited the buffer.") | |
125 | ||
f3e23606 RS |
126 | (defvar buffer-file-number nil |
127 | "The device number and file number of the file visited in the current buffer. | |
128 | The value is a list of the form (FILENUM DEVNUM). | |
129 | This pair of numbers uniquely identifies the file. | |
130 | If the buffer is visiting a new file, the value is nil.") | |
131 | (make-variable-buffer-local 'buffer-file-number) | |
132 | (put 'buffer-file-number 'permanent-local t) | |
133 | ||
de88363f RS |
134 | (defvar buffer-file-numbers-unique (not (memq system-type '(windows-nt))) |
135 | "Non-nil means that buffer-file-number uniquely identifies files.") | |
136 | ||
d7fa5aa2 | 137 | (defvar file-precious-flag nil |
b4da00e9 | 138 | "*Non-nil means protect against I/O errors while saving files. |
560f4415 | 139 | Some modes set this non-nil in particular buffers. |
4b7271c1 KH |
140 | |
141 | This feature works by writing the new contents into a temporary file | |
142 | and then renaming the temporary file to replace the original. | |
143 | In this way, any I/O error in writing leaves the original untouched, | |
144 | and there is never any instant where the file is nonexistent. | |
145 | ||
146 | Note that this feature forces backups to be made by copying. | |
560f4415 KH |
147 | Yet, at the same time, saving a precious file |
148 | breaks any hard links between it and other files.") | |
b4da00e9 RM |
149 | |
150 | (defvar version-control nil | |
151 | "*Control use of version numbers for backup files. | |
152 | t means make numeric backup versions unconditionally. | |
153 | nil means make them for files that have some already. | |
399f8dd6 | 154 | `never' means do not make them.") |
b4da00e9 RM |
155 | |
156 | (defvar dired-kept-versions 2 | |
157 | "*When cleaning directory, number of versions to keep.") | |
158 | ||
de7d5e1b RS |
159 | (defvar delete-old-versions nil |
160 | "*If t, delete excess backup versions silently. | |
161 | If nil, ask confirmation. Any other value prevents any trimming.") | |
b4da00e9 RM |
162 | |
163 | (defvar kept-old-versions 2 | |
164 | "*Number of oldest versions to keep when a new numbered backup is made.") | |
165 | ||
166 | (defvar kept-new-versions 2 | |
167 | "*Number of newest versions to keep when a new numbered backup is made. | |
168 | Includes the new backup. Must be > 0") | |
169 | ||
d7fa5aa2 | 170 | (defvar require-final-newline nil |
b4da00e9 RM |
171 | "*Value of t says silently ensure a file ends in a newline when it is saved. |
172 | Non-nil but not t says ask user whether to add a newline when there isn't one. | |
173 | nil means don't add newlines.") | |
174 | ||
d7fa5aa2 | 175 | (defvar auto-save-default t |
b4da00e9 RM |
176 | "*Non-nil says by default do auto-saving of every file-visiting buffer.") |
177 | ||
d7fa5aa2 | 178 | (defvar auto-save-visited-file-name nil |
b4da00e9 RM |
179 | "*Non-nil says auto-save a buffer in the file it is visiting, when practical. |
180 | Normally auto-save files are written under other names.") | |
181 | ||
d7fa5aa2 | 182 | (defvar save-abbrevs nil |
b4da00e9 RM |
183 | "*Non-nil means save word abbrevs too when files are saved. |
184 | Loading an abbrev file sets this to t.") | |
185 | ||
d7fa5aa2 | 186 | (defvar find-file-run-dired t |
320b4233 | 187 | "*Non-nil says run dired if `find-file' is given the name of a directory.") |
b4da00e9 | 188 | |
92966e6f RS |
189 | ;;;It is not useful to make this a local variable. |
190 | ;;;(put 'find-file-not-found-hooks 'permanent-local t) | |
b4da00e9 RM |
191 | (defvar find-file-not-found-hooks nil |
192 | "List of functions to be called for `find-file' on nonexistent file. | |
193 | These functions are called as soon as the error is detected. | |
194 | `buffer-file-name' is already set up. | |
195 | The functions are called in the order given until one of them returns non-nil.") | |
196 | ||
92966e6f RS |
197 | ;;;It is not useful to make this a local variable. |
198 | ;;;(put 'find-file-hooks 'permanent-local t) | |
b4da00e9 RM |
199 | (defvar find-file-hooks nil |
200 | "List of functions to be called after a buffer is loaded from a file. | |
201 | The buffer's local variables (if any) will have been processed before the | |
202 | functions are called.") | |
203 | ||
b4da00e9 RM |
204 | (defvar write-file-hooks nil |
205 | "List of functions to be called before writing out a buffer to a file. | |
206 | If one of them returns non-nil, the file is considered already written | |
8c0e7b73 JB |
207 | and the rest are not called. |
208 | These hooks are considered to pertain to the visited file. | |
209 | So this list is cleared if you change the visited file name. | |
f82966e4 KH |
210 | |
211 | Don't make this variable buffer-local; instead, use `local-write-file-hooks'. | |
212 | See also `write-contents-hooks'.") | |
b19f1da4 BF |
213 | ;;; However, in case someone does make it local... |
214 | (put 'write-file-hooks 'permanent-local t) | |
c9dca4e0 | 215 | |
c9dca4e0 RS |
216 | (defvar local-write-file-hooks nil |
217 | "Just like `write-file-hooks', except intended for per-buffer use. | |
218 | The functions in this list are called before the ones in | |
f82966e4 KH |
219 | `write-file-hooks'. |
220 | ||
221 | This variable is meant to be used for hooks that have to do with a | |
222 | particular visited file. Therefore, it is a permanent local, so that | |
223 | changing the major mode does not clear it. However, calling | |
224 | `set-visited-file-name' does clear it.") | |
b19f1da4 BF |
225 | (make-variable-buffer-local 'local-write-file-hooks) |
226 | (put 'local-write-file-hooks 'permanent-local t) | |
8c0e7b73 JB |
227 | |
228 | (defvar write-contents-hooks nil | |
229 | "List of functions to be called before writing out a buffer to a file. | |
230 | If one of them returns non-nil, the file is considered already written | |
231 | and the rest are not called. | |
f82966e4 KH |
232 | |
233 | This variable is meant to be used for hooks that pertain to the | |
234 | buffer's contents, not to the particular visited file; thus, | |
235 | `set-visited-file-name' does not clear this variable; but changing the | |
693f800d RS |
236 | major mode does clear it. |
237 | ||
238 | This variable automatically becomes buffer-local whenever it is set. | |
659ebcc6 | 239 | If you use `add-hook' to add elements to the list, use nil for the |
693f800d | 240 | LOCAL argument. |
f82966e4 | 241 | |
8c0e7b73 | 242 | See also `write-file-hooks'.") |
f82966e4 | 243 | (make-variable-buffer-local 'write-contents-hooks) |
b4da00e9 | 244 | |
d7fa5aa2 | 245 | (defvar enable-local-variables t |
93a2702d | 246 | "*Control use of local variables in files you visit. |
b4da00e9 | 247 | The value can be t, nil or something else. |
93a2702d | 248 | A value of t means file local variables specifications are obeyed; |
b4da00e9 RM |
249 | nil means they are ignored; anything else means query. |
250 | ||
93a2702d RS |
251 | The command \\[normal-mode] always obeys file local variable |
252 | specifications and ignores this variable.") | |
b4da00e9 | 253 | |
d7fa5aa2 | 254 | (defvar enable-local-eval 'maybe |
d207b766 RS |
255 | "*Control processing of the \"variable\" `eval' in a file's local variables. |
256 | The value can be t, nil or something else. | |
257 | A value of t means obey `eval' variables; | |
258 | nil means ignore them; anything else means query. | |
259 | ||
260 | The command \\[normal-mode] always obeys local-variables lists | |
261 | and ignores this variable.") | |
b4da00e9 RM |
262 | |
263 | ;; Avoid losing in versions where CLASH_DETECTION is disabled. | |
264 | (or (fboundp 'lock-buffer) | |
231c4e10 | 265 | (defalias 'lock-buffer 'ignore)) |
b4da00e9 | 266 | (or (fboundp 'unlock-buffer) |
231c4e10 | 267 | (defalias 'unlock-buffer 'ignore)) |
a7305f6e RS |
268 | (or (fboundp 'file-locked-p) |
269 | (defalias 'file-locked-p 'ignore)) | |
93fe0a35 RS |
270 | |
271 | ;; This hook function provides support for ange-ftp host name | |
272 | ;; completion. It runs the usual ange-ftp hook, but only for | |
273 | ;; completion operations. Having this here avoids the need | |
274 | ;; to load ange-ftp when it's not really in use. | |
275 | (defun ange-ftp-completion-hook-function (op &rest args) | |
276 | (if (memq op '(file-name-completion file-name-all-completions)) | |
277 | (apply 'ange-ftp-hook-function op args) | |
57e81f57 RS |
278 | (let ((inhibit-file-name-handlers |
279 | (cons 'ange-ftp-completion-hook-function | |
280 | (and (eq inhibit-file-name-operation op) | |
281 | inhibit-file-name-handlers))) | |
282 | (inhibit-file-name-operation op)) | |
93fe0a35 | 283 | (apply op args)))) |
567c1ca9 RS |
284 | |
285 | (defun convert-standard-filename (filename) | |
286 | "Convert a standard file's name to something suitable for the current OS. | |
287 | This function's standard definition is trivial; it just returns the argument. | |
288 | However, on some systems, the function is redefined | |
289 | with a definition that really does change some file names." | |
290 | filename) | |
b4da00e9 RM |
291 | \f |
292 | (defun pwd () | |
293 | "Show the current default directory." | |
294 | (interactive nil) | |
295 | (message "Directory %s" default-directory)) | |
296 | ||
231c4e10 ER |
297 | (defvar cd-path nil |
298 | "Value of the CDPATH environment variable, as a list. | |
299 | Not actually set up until the first time you you use it.") | |
300 | ||
306faa42 RS |
301 | (defvar path-separator ":" |
302 | "Character used to separate concatenated paths.") | |
303 | ||
231c4e10 ER |
304 | (defun parse-colon-path (cd-path) |
305 | "Explode a colon-separated list of paths into a string list." | |
306 | (and cd-path | |
307 | (let (cd-prefix cd-list (cd-start 0) cd-colon) | |
306faa42 RS |
308 | (setq cd-path (concat cd-path path-separator)) |
309 | (while (setq cd-colon (string-match path-separator cd-path cd-start)) | |
231c4e10 | 310 | (setq cd-list |
9daefb36 | 311 | (nconc cd-list |
c52e4104 RS |
312 | (list (if (= cd-start cd-colon) |
313 | nil | |
314 | (substitute-in-file-name | |
e33e80e4 RS |
315 | (file-name-as-directory |
316 | (substring cd-path cd-start cd-colon))))))) | |
231c4e10 ER |
317 | (setq cd-start (+ cd-colon 1))) |
318 | cd-list))) | |
319 | ||
320 | (defun cd-absolute (dir) | |
30c5ce9c | 321 | "Change current directory to given absolute file name DIR." |
f4a0f59b RS |
322 | ;; Put the name into directory syntax now, |
323 | ;; because otherwise expand-file-name may give some bad results. | |
b4da00e9 RM |
324 | (if (not (eq system-type 'vax-vms)) |
325 | (setq dir (file-name-as-directory dir))) | |
f4a0f59b | 326 | (setq dir (abbreviate-file-name (expand-file-name dir))) |
b4da00e9 RM |
327 | (if (not (file-directory-p dir)) |
328 | (error "%s is not a directory" dir) | |
329 | (if (file-executable-p dir) | |
330 | (setq default-directory dir) | |
9daefb36 | 331 | (error "Cannot cd to %s: Permission denied" dir)))) |
b4da00e9 | 332 | |
231c4e10 ER |
333 | (defun cd (dir) |
334 | "Make DIR become the current buffer's default directory. | |
30c5ce9c RS |
335 | If your environment includes a `CDPATH' variable, try each one of that |
336 | colon-separated list of directories when resolving a relative directory name." | |
dac4ea74 RS |
337 | (interactive |
338 | (list (read-file-name "Change default directory: " | |
2121cd9c RM |
339 | default-directory default-directory |
340 | (and (member cd-path '(nil ("./"))) | |
341 | (null (getenv "CDPATH")))))) | |
30c5ce9c RS |
342 | (if (file-name-absolute-p dir) |
343 | (cd-absolute (expand-file-name dir)) | |
344 | (if (null cd-path) | |
345 | (let ((trypath (parse-colon-path (getenv "CDPATH")))) | |
346 | (setq cd-path (or trypath (list "./"))))) | |
347 | (if (not (catch 'found | |
348 | (mapcar | |
349 | (function (lambda (x) | |
350 | (let ((f (expand-file-name (concat x dir)))) | |
351 | (if (file-directory-p f) | |
352 | (progn | |
353 | (cd-absolute f) | |
354 | (throw 'found t)))))) | |
355 | cd-path) | |
356 | nil)) | |
357 | (error "No such directory found via CDPATH environment variable")))) | |
231c4e10 | 358 | |
b4da00e9 RM |
359 | (defun load-file (file) |
360 | "Load the Lisp file named FILE." | |
361 | (interactive "fLoad file: ") | |
362 | (load (expand-file-name file) nil nil t)) | |
363 | ||
364 | (defun load-library (library) | |
365 | "Load the library named LIBRARY. | |
366 | This is an interface to the function `load'." | |
367 | (interactive "sLoad library: ") | |
368 | (load library)) | |
5d68c2c2 | 369 | |
7c4b8f8c | 370 | (defun file-local-copy (file &optional buffer) |
5d68c2c2 RS |
371 | "Copy the file FILE into a temporary file on this machine. |
372 | Returns the name of the local copy, or nil, if FILE is directly | |
373 | accessible." | |
6eaebaa2 | 374 | (let ((handler (find-file-name-handler file 'file-local-copy))) |
5d68c2c2 RS |
375 | (if handler |
376 | (funcall handler 'file-local-copy file) | |
377 | nil))) | |
f3e23606 | 378 | |
05ef1cda | 379 | (defun file-truename (filename &optional counter prev-dirs) |
f3e23606 RS |
380 | "Return the truename of FILENAME, which should be absolute. |
381 | The truename of a file name is found by chasing symbolic links | |
382 | both at the level of the file and at the level of the directories | |
05ef1cda RS |
383 | containing it, until no links are left at any level. |
384 | ||
385 | The arguments COUNTER and PREV-DIRS are used only in recursive calls. | |
386 | Do not specify them in other calls." | |
387 | ;; COUNTER can be a cons cell whose car is the count of how many more links | |
388 | ;; to chase before getting an error. | |
389 | ;; PREV-DIRS can be a cons cell whose car is an alist | |
390 | ;; of truenames we've just recently computed. | |
cc37a58c | 391 | |
cc37a58c SM |
392 | ;; The last test looks dubious, maybe `+' is meant here? --simon. |
393 | (if (or (string= filename "") (string= filename "~") | |
7a5a26a6 RS |
394 | (and (string= (substring filename 0 1) "~") |
395 | (string-match "~[^/]*" filename))) | |
1cc2fbeb RS |
396 | (progn |
397 | (setq filename (expand-file-name filename)) | |
398 | (if (string= filename "") | |
399 | (setq filename "/")))) | |
05ef1cda | 400 | (or counter (setq counter (list 100))) |
b505828b RS |
401 | (let (done |
402 | ;; For speed, remove the ange-ftp completion handler from the list. | |
403 | ;; We know it's not needed here. | |
404 | ;; For even more speed, do this only on the outermost call. | |
405 | (file-name-handler-alist | |
406 | (if prev-dirs file-name-handler-alist | |
407 | (let ((tem (copy-sequence file-name-handler-alist))) | |
408 | (delq (rassq 'ange-ftp-completion-hook-function tem) tem))))) | |
409 | (or prev-dirs (setq prev-dirs (list nil))) | |
05ef1cda RS |
410 | ;; If this file directly leads to a link, process that iteratively |
411 | ;; so that we don't use lots of stack. | |
412 | (while (not done) | |
413 | (setcar counter (1- (car counter))) | |
414 | (if (< (car counter) 0) | |
415 | (error "Apparent cycle of symbolic links for %s" filename)) | |
416 | (let ((handler (find-file-name-handler filename 'file-truename))) | |
417 | ;; For file name that has a special handler, call handler. | |
418 | ;; This is so that ange-ftp can save time by doing a no-op. | |
419 | (if handler | |
420 | (setq filename (funcall handler 'file-truename filename) | |
421 | done t) | |
fb145562 | 422 | (let ((dir (or (file-name-directory filename) default-directory)) |
05ef1cda RS |
423 | target dirfile) |
424 | ;; Get the truename of the directory. | |
425 | (setq dirfile (directory-file-name dir)) | |
426 | ;; If these are equal, we have the (or a) root directory. | |
427 | (or (string= dir dirfile) | |
428 | ;; If this is the same dir we last got the truename for, | |
429 | ;; save time--don't recalculate. | |
430 | (if (assoc dir (car prev-dirs)) | |
431 | (setq dir (cdr (assoc dir (car prev-dirs)))) | |
432 | (let ((old dir) | |
433 | (new (file-name-as-directory (file-truename dirfile counter prev-dirs)))) | |
434 | (setcar prev-dirs (cons (cons old new) (car prev-dirs))) | |
435 | (setq dir new)))) | |
436 | (if (equal ".." (file-name-nondirectory filename)) | |
437 | (setq filename | |
438 | (directory-file-name (file-name-directory (directory-file-name dir))) | |
439 | done t) | |
440 | (if (equal "." (file-name-nondirectory filename)) | |
441 | (setq filename (directory-file-name dir) | |
442 | done t) | |
443 | ;; Put it back on the file name. | |
444 | (setq filename (concat dir (file-name-nondirectory filename))) | |
445 | ;; Is the file name the name of a link? | |
446 | (setq target (file-symlink-p filename)) | |
447 | (if target | |
448 | ;; Yes => chase that link, then start all over | |
449 | ;; since the link may point to a directory name that uses links. | |
450 | ;; We can't safely use expand-file-name here | |
451 | ;; since target might look like foo/../bar where foo | |
452 | ;; is itself a link. Instead, we handle . and .. above. | |
453 | (setq filename | |
454 | (if (file-name-absolute-p target) | |
455 | target | |
456 | (concat dir target)) | |
457 | done nil) | |
458 | ;; No, we are done! | |
459 | (setq done t)))))))) | |
460 | filename)) | |
5dbfdacd | 461 | |
5dadeb29 RS |
462 | (defun file-chase-links (filename) |
463 | "Chase links in FILENAME until a name that is not a link. | |
464 | Does not examine containing directories for links, | |
465 | unlike `file-truename'." | |
466 | (let (tem (count 100) (newname filename)) | |
467 | (while (setq tem (file-symlink-p newname)) | |
468 | (if (= count 0) | |
469 | (error "Apparent cycle of symbolic links for %s" filename)) | |
f01de0db KH |
470 | ;; In the context of a link, `//' doesn't mean what Emacs thinks. |
471 | (while (string-match "//+" tem) | |
472 | (setq tem (concat (substring tem 0 (1+ (match-beginning 0))) | |
473 | (substring tem (match-end 0))))) | |
cf65b429 RS |
474 | ;; Handle `..' by hand, since it needs to work in the |
475 | ;; target of any directory symlink. | |
476 | ;; This code is not quite complete; it does not handle | |
477 | ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose. | |
f01de0db | 478 | (while (string-match "\\`\\.\\./" tem) |
cf65b429 RS |
479 | (setq tem (substring tem 3)) |
480 | (setq newname (file-name-as-directory | |
481 | ;; Do the .. by hand. | |
482 | (directory-file-name | |
483 | (file-name-directory | |
484 | ;; Chase links in the default dir of the symlink. | |
485 | (file-chase-links | |
486 | (directory-file-name | |
487 | (file-name-directory newname)))))))) | |
5dadeb29 RS |
488 | (setq newname (expand-file-name tem (file-name-directory newname))) |
489 | (setq count (1- count))) | |
490 | newname)) | |
b4da00e9 | 491 | \f |
467ff692 RS |
492 | (defun switch-to-buffer-other-window (buffer &optional norecord) |
493 | "Select buffer BUFFER in another window. | |
494 | Optional second arg NORECORD non-nil means | |
495 | do not put this buffer at the front of the list of recently selected ones." | |
b4da00e9 RM |
496 | (interactive "BSwitch to buffer in other window: ") |
497 | (let ((pop-up-windows t)) | |
467ff692 | 498 | (pop-to-buffer buffer t norecord))) |
b4da00e9 | 499 | |
467ff692 RS |
500 | (defun switch-to-buffer-other-frame (buffer &optional norecord) |
501 | "Switch to buffer BUFFER in another frame. | |
502 | Optional second arg NORECORD non-nil means | |
503 | do not put this buffer at the front of the list of recently selected ones." | |
f98955ea JB |
504 | (interactive "BSwitch to buffer in other frame: ") |
505 | (let ((pop-up-frames t)) | |
467ff692 | 506 | (pop-to-buffer buffer t norecord) |
336b7f41 | 507 | (raise-frame (window-frame (selected-window))))) |
5bbbceb1 | 508 | |
82d0954a | 509 | (defun find-file (filename &optional coding-system) |
b4da00e9 RM |
510 | "Edit file FILENAME. |
511 | Switch to a buffer visiting file FILENAME, | |
82d0954a KH |
512 | creating one if none already exists. |
513 | A prefix argument enables user to specify the coding-system interactively." | |
514 | (interactive "FFind file: \nZCoding-system: ") | |
515 | (if coding-system | |
516 | (let ((coding-system-for-read coding-system)) | |
517 | (switch-to-buffer (find-file-noselect filename))) | |
518 | (switch-to-buffer (find-file-noselect filename)))) | |
519 | ||
520 | (defun find-file-other-window (filename &optional coding-system) | |
b4da00e9 RM |
521 | "Edit file FILENAME, in another window. |
522 | May create a new window, or reuse an existing one. | |
82d0954a | 523 | A prefix argument enables user to specify the coding-system interactively. |
b4da00e9 | 524 | See the function `display-buffer'." |
82d0954a KH |
525 | (interactive "FFind file in other window: \nZCoding-system: ") |
526 | (if coding-system | |
527 | (let ((coding-system-for-read coding-system)) | |
528 | (switch-to-buffer-other-window (find-file-noselect filename))) | |
529 | (switch-to-buffer-other-window (find-file-noselect filename)))) | |
b4da00e9 | 530 | |
82d0954a | 531 | (defun find-file-other-frame (filename &optional coding-system) |
f98955ea JB |
532 | "Edit file FILENAME, in another frame. |
533 | May create a new frame, or reuse an existing one. | |
82d0954a | 534 | A prefix argument enables user to specify the coding-system interactively. |
5bbbceb1 | 535 | See the function `display-buffer'." |
82d0954a KH |
536 | (interactive "FFind file in other frame: \nZCoding-system: ") |
537 | (if coding-system | |
538 | (let ((coding-system-for-read coding-system)) | |
539 | (switch-to-buffer-other-frame (find-file-noselect filename))) | |
540 | (switch-to-buffer-other-frame (find-file-noselect filename)))) | |
5bbbceb1 | 541 | |
82d0954a | 542 | (defun find-file-read-only (filename &optional coding-system) |
b4da00e9 RM |
543 | "Edit file FILENAME but don't allow changes. |
544 | Like \\[find-file] but marks buffer as read-only. | |
82d0954a | 545 | A prefix argument enables user to specify the coding-system interactively. |
b4da00e9 | 546 | Use \\[toggle-read-only] to permit editing." |
82d0954a KH |
547 | (interactive "fFind file read-only: \nZCoding-system: ") |
548 | (find-file filename coding-system) | |
320b4233 RS |
549 | (setq buffer-read-only t) |
550 | (current-buffer)) | |
b4da00e9 | 551 | |
82d0954a | 552 | (defun find-file-read-only-other-window (filename &optional coding-system) |
b4da00e9 RM |
553 | "Edit file FILENAME in another window but don't allow changes. |
554 | Like \\[find-file-other-window] but marks buffer as read-only. | |
82d0954a | 555 | A prefix argument enables user to specify the coding-system interactively. |
b4da00e9 | 556 | Use \\[toggle-read-only] to permit editing." |
82d0954a KH |
557 | (interactive "fFind file read-only other window: \nZCoding-system: ") |
558 | (find-file-other-window filename coding-system) | |
320b4233 RS |
559 | (setq buffer-read-only t) |
560 | (current-buffer)) | |
b4da00e9 | 561 | |
82d0954a | 562 | (defun find-file-read-only-other-frame (filename &optional coding-system) |
f98955ea JB |
563 | "Edit file FILENAME in another frame but don't allow changes. |
564 | Like \\[find-file-other-frame] but marks buffer as read-only. | |
82d0954a | 565 | A prefix argument enables user to specify the coding-system interactively. |
5bbbceb1 | 566 | Use \\[toggle-read-only] to permit editing." |
82d0954a KH |
567 | (interactive "fFind file read-only other frame: \nZCoding-system: ") |
568 | (find-file-other-frame filename coding-system) | |
320b4233 RS |
569 | (setq buffer-read-only t) |
570 | (current-buffer)) | |
5bbbceb1 | 571 | |
82d0954a | 572 | (defun find-alternate-file-other-window (filename &optional coding-system) |
60eaf370 | 573 | "Find file FILENAME as a replacement for the file in the next window. |
82d0954a KH |
574 | This command does not select that window. |
575 | A prefix argument enables user to specify the coding-system interactively." | |
60eaf370 RS |
576 | (interactive |
577 | (save-selected-window | |
578 | (other-window 1) | |
579 | (let ((file buffer-file-name) | |
580 | (file-name nil) | |
581 | (file-dir nil)) | |
582 | (and file | |
583 | (setq file-name (file-name-nondirectory file) | |
584 | file-dir (file-name-directory file))) | |
585 | (list (read-file-name | |
82d0954a KH |
586 | "Find alternate file: " file-dir nil nil file-name) |
587 | (if current-prefix-arg | |
588 | (read-coding-system "Coding-system: ")))))) | |
60eaf370 | 589 | (if (one-window-p) |
82d0954a | 590 | (find-file-other-window filename coding-system) |
60eaf370 RS |
591 | (save-selected-window |
592 | (other-window 1) | |
82d0954a | 593 | (find-alternate-file filename coding-system)))) |
60eaf370 | 594 | |
82d0954a | 595 | (defun find-alternate-file (filename &optional coding-system) |
b4da00e9 RM |
596 | "Find file FILENAME, select its buffer, kill previous buffer. |
597 | If the current buffer now contains an empty file that you just visited | |
82d0954a KH |
598 | \(presumably by mistake), use this command to visit the file you really want. |
599 | A prefix argument enables user to specify the coding-system interactively." | |
b4da00e9 RM |
600 | (interactive |
601 | (let ((file buffer-file-name) | |
602 | (file-name nil) | |
603 | (file-dir nil)) | |
604 | (and file | |
605 | (setq file-name (file-name-nondirectory file) | |
606 | file-dir (file-name-directory file))) | |
a61f59b4 | 607 | (list (read-file-name |
82d0954a KH |
608 | "Find alternate file: " file-dir nil nil file-name) |
609 | (if current-prefix-arg | |
610 | (read-coding-system "Coding-system: "))))) | |
2aa8cc2d | 611 | (and (buffer-modified-p) (buffer-file-name) |
b4da00e9 RM |
612 | ;; (not buffer-read-only) |
613 | (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " | |
614 | (buffer-name)))) | |
615 | (error "Aborted")) | |
616 | (let ((obuf (current-buffer)) | |
617 | (ofile buffer-file-name) | |
8bb27285 RS |
618 | (onum buffer-file-number) |
619 | (otrue buffer-file-truename) | |
b4da00e9 | 620 | (oname (buffer-name))) |
baf9b8c4 RS |
621 | (if (get-buffer " **lose**") |
622 | (kill-buffer " **lose**")) | |
b4da00e9 | 623 | (rename-buffer " **lose**") |
b4da00e9 RM |
624 | (unwind-protect |
625 | (progn | |
626 | (unlock-buffer) | |
a4ad4d96 RS |
627 | (setq buffer-file-name nil) |
628 | (setq buffer-file-number nil) | |
629 | (setq buffer-file-truename nil) | |
82d0954a | 630 | (find-file filename coding-system)) |
b4da00e9 RM |
631 | (cond ((eq obuf (current-buffer)) |
632 | (setq buffer-file-name ofile) | |
8bb27285 RS |
633 | (setq buffer-file-number onum) |
634 | (setq buffer-file-truename otrue) | |
b4da00e9 RM |
635 | (lock-buffer) |
636 | (rename-buffer oname)))) | |
637 | (or (eq (current-buffer) obuf) | |
638 | (kill-buffer obuf)))) | |
639 | ||
640 | (defun create-file-buffer (filename) | |
641 | "Create a suitably named buffer for visiting FILENAME, and return it. | |
642 | FILENAME (sans directory) is used unchanged if that name is free; | |
643 | otherwise a string <2> or <3> or ... is appended to get an unused name." | |
644 | (let ((lastname (file-name-nondirectory filename))) | |
645 | (if (string= lastname "") | |
646 | (setq lastname filename)) | |
647 | (generate-new-buffer lastname))) | |
648 | ||
5bbbceb1 JB |
649 | (defun generate-new-buffer (name) |
650 | "Create and return a buffer with a name based on NAME. | |
29165787 | 651 | Choose the buffer's name using `generate-new-buffer-name'." |
5bbbceb1 JB |
652 | (get-buffer-create (generate-new-buffer-name name))) |
653 | ||
d7fa5aa2 | 654 | (defvar automount-dir-prefix "^/tmp_mnt/" |
e373f201 JB |
655 | "Regexp to match the automounter prefix in a directory name.") |
656 | ||
ffb3a4db | 657 | (defvar abbreviated-home-dir nil |
292297c6 | 658 | "The user's homedir abbreviated according to `directory-abbrev-list'.") |
ffb3a4db | 659 | |
5bbbceb1 | 660 | (defun abbreviate-file-name (filename) |
29165787 | 661 | "Return a version of FILENAME shortened using `directory-abbrev-alist'. |
5bbbceb1 | 662 | This also substitutes \"~\" for the user's home directory. |
29165787 | 663 | Type \\[describe-variable] directory-abbrev-alist RET for more information." |
e373f201 | 664 | ;; Get rid of the prefixes added by the automounter. |
a8f34fc0 RS |
665 | (if (and automount-dir-prefix |
666 | (string-match automount-dir-prefix filename) | |
e373f201 JB |
667 | (file-exists-p (file-name-directory |
668 | (substring filename (1- (match-end 0)))))) | |
669 | (setq filename (substring filename (1- (match-end 0))))) | |
5bbbceb1 | 670 | (let ((tail directory-abbrev-alist)) |
ffb3a4db RS |
671 | ;; If any elt of directory-abbrev-alist matches this name, |
672 | ;; abbreviate accordingly. | |
5bbbceb1 JB |
673 | (while tail |
674 | (if (string-match (car (car tail)) filename) | |
675 | (setq filename | |
676 | (concat (cdr (car tail)) (substring filename (match-end 0))))) | |
677 | (setq tail (cdr tail))) | |
ffb3a4db RS |
678 | ;; Compute and save the abbreviated homedir name. |
679 | ;; We defer computing this until the first time it's needed, to | |
680 | ;; give time for directory-abbrev-alist to be set properly. | |
e98dda89 RS |
681 | ;; We include a slash at the end, to avoid spurious matches |
682 | ;; such as `/usr/foobar' when the home dir is `/usr/foo'. | |
ffb3a4db RS |
683 | (or abbreviated-home-dir |
684 | (setq abbreviated-home-dir | |
685 | (let ((abbreviated-home-dir "$foo")) | |
3a8a836d RS |
686 | (concat "^" (abbreviate-file-name (expand-file-name "~")) |
687 | "\\(/\\|$\\)")))) | |
76d5492b | 688 | |
ffb3a4db RS |
689 | ;; If FILENAME starts with the abbreviated homedir, |
690 | ;; make it start with `~' instead. | |
ca33ccb5 RS |
691 | (if (and (string-match abbreviated-home-dir filename) |
692 | ;; If the home dir is just /, don't change it. | |
693 | (not (and (= (match-end 0) 1) | |
288201bd | 694 | (= (aref filename 0) ?/))) |
567c1ca9 RS |
695 | ;; MS-DOS root directories can come with a drive letter; |
696 | ;; Novell Netware allows drive letters beyond `Z:'. | |
76d5492b | 697 | (not (and (or (eq system-type 'ms-dos) |
c32d49e8 | 698 | (eq system-type 'windows-nt)) |
288201bd | 699 | (save-match-data |
567c1ca9 | 700 | (string-match "^[a-zA-`]:/$" filename))))) |
5bbbceb1 | 701 | (setq filename |
28dbf501 | 702 | (concat "~" |
3a8a836d | 703 | (substring filename (match-beginning 1) (match-end 1)) |
28dbf501 | 704 | (substring filename (match-end 0))))) |
5bbbceb1 JB |
705 | filename)) |
706 | ||
1770543d RS |
707 | (defvar find-file-not-true-dirname-list nil |
708 | "*List of logical names for which visiting shouldn't save the true dirname. | |
709 | On VMS, when you visit a file using a logical name that searches a path, | |
710 | you may or may not want the visited file name to record the specific | |
711 | directory where the file was found. If you *do not* want that, add the logical | |
712 | name to this list as a string.") | |
713 | ||
138c44f6 KH |
714 | (defun find-buffer-visiting (filename) |
715 | "Return the buffer visiting file FILENAME (a string). | |
716 | This is like `get-file-buffer', except that it checks for any buffer | |
717 | visiting the same file, possibly under a different name. | |
718 | If there is no such live buffer, return nil." | |
719 | (let ((buf (get-file-buffer filename)) | |
720 | (truename (abbreviate-file-name (file-truename filename)))) | |
721 | (or buf | |
722 | (let ((list (buffer-list)) found) | |
723 | (while (and (not found) list) | |
724 | (save-excursion | |
725 | (set-buffer (car list)) | |
726 | (if (and buffer-file-name | |
727 | (string= buffer-file-truename truename)) | |
728 | (setq found (car list)))) | |
729 | (setq list (cdr list))) | |
730 | found) | |
0f933cf6 | 731 | (let ((number (nthcdr 10 (file-attributes truename))) |
138c44f6 | 732 | (list (buffer-list)) found) |
de88363f RS |
733 | (and buffer-file-numbers-unique |
734 | number | |
181c830f RS |
735 | (while (and (not found) list) |
736 | (save-excursion | |
737 | (set-buffer (car list)) | |
d49ab5a0 RS |
738 | (if (and buffer-file-name |
739 | (equal buffer-file-number number) | |
181c830f RS |
740 | ;; Verify this buffer's file number |
741 | ;; still belongs to its file. | |
742 | (file-exists-p buffer-file-name) | |
743 | (equal (nthcdr 10 (file-attributes buffer-file-name)) | |
744 | number)) | |
745 | (setq found (car list)))) | |
746 | (setq list (cdr list)))) | |
138c44f6 KH |
747 | found)))) |
748 | ||
40dfe94d RS |
749 | (defun insert-file-contents-literally (filename &optional visit beg end replace) |
750 | "Like `insert-file-contents', q.v., but only reads in the file. | |
751 | A buffer may be modified in several ways after reading into the buffer due | |
752 | to advanced Emacs features, such as file-name-handlers, format decoding, | |
753 | find-file-hooks, etc. | |
6e6f5d9e RS |
754 | This function ensures that none of these modifications will take place. |
755 | ||
756 | This function does not work for remote files, because it turns off | |
757 | file name handlers and remote file access uses a file name handler." | |
40dfe94d RS |
758 | (let ((file-name-handler-alist nil) |
759 | (format-alist nil) | |
760 | (after-insert-file-functions nil) | |
76d5492b | 761 | (find-buffer-file-type-function |
40dfe94d RS |
762 | (if (fboundp 'find-buffer-file-type) |
763 | (symbol-function 'find-buffer-file-type) | |
764 | nil))) | |
765 | (unwind-protect | |
766 | (progn | |
767 | (fset 'find-buffer-file-type (lambda (filename) t)) | |
768 | (insert-file-contents filename visit beg end replace)) | |
769 | (if find-buffer-file-type-function | |
770 | (fset 'find-buffer-file-type find-buffer-file-type-function) | |
771 | (fmakunbound 'find-buffer-file-type))))) | |
772 | ||
773 | (defun find-file-noselect (filename &optional nowarn rawfile) | |
b4da00e9 RM |
774 | "Read file FILENAME into a buffer and return the buffer. |
775 | If a buffer exists visiting FILENAME, return that one, but | |
776 | verify that the file has not changed since visited or saved. | |
82d0954a KH |
777 | The buffer is not selected, just returned to the caller. |
778 | Optional first arg NOWARN non-nil means suppress any warning messages. | |
779 | Optional second arg RAWFILE non-nil means the file is read literally" | |
e373f201 JB |
780 | (setq filename |
781 | (abbreviate-file-name | |
782 | (expand-file-name filename))) | |
b4da00e9 RM |
783 | (if (file-directory-p filename) |
784 | (if find-file-run-dired | |
4147a3cc KH |
785 | (dired-noselect (if find-file-visit-truename |
786 | (abbreviate-file-name (file-truename filename)) | |
787 | filename)) | |
ba369f54 | 788 | (error "%s is a directory" filename)) |
f3e23606 RS |
789 | (let* ((buf (get-file-buffer filename)) |
790 | (truename (abbreviate-file-name (file-truename filename))) | |
791 | (number (nthcdr 10 (file-attributes truename))) | |
792 | ;; Find any buffer for a file which has same truename. | |
138c44f6 | 793 | (other (and (not buf) (find-buffer-visiting filename))) |
f3e23606 RS |
794 | error) |
795 | ;; Let user know if there is a buffer with the same truename. | |
138c44f6 KH |
796 | (if other |
797 | (progn | |
327bebe8 RS |
798 | (or nowarn |
799 | (string-equal filename (buffer-file-name other)) | |
800 | (message "%s and %s are the same file" | |
801 | filename (buffer-file-name other))) | |
138c44f6 KH |
802 | ;; Optionally also find that buffer. |
803 | (if (or find-file-existing-other-name find-file-visit-truename) | |
804 | (setq buf other)))) | |
b4da00e9 RM |
805 | (if buf |
806 | (or nowarn | |
807 | (verify-visited-file-modtime buf) | |
808 | (cond ((not (file-exists-p filename)) | |
809 | (error "File %s no longer exists!" filename)) | |
ebeb898f RS |
810 | ;; Certain files should be reverted automatically |
811 | ;; if they have changed on disk and not in the buffer. | |
812 | ((and (not (buffer-modified-p buf)) | |
813 | (let ((tail find-file-revert-without-query) | |
814 | (found nil)) | |
815 | (while tail | |
816 | (if (string-match (car tail) filename) | |
817 | (setq found t)) | |
818 | (setq tail (cdr tail))) | |
819 | found)) | |
820 | (with-current-buffer buf | |
821 | (message "Reverting file %s..." filename) | |
822 | (revert-buffer t t) | |
823 | (message "Reverting file %s...done" filename))) | |
b4da00e9 | 824 | ((yes-or-no-p |
b7d19f4a KH |
825 | (if (string= (file-name-nondirectory filename) |
826 | (buffer-name buf)) | |
827 | (format | |
828 | (if (buffer-modified-p buf) | |
829 | "File %s changed on disk. Discard your edits? " | |
830 | "File %s changed on disk. Reread from disk? ") | |
831 | (file-name-nondirectory filename)) | |
832 | (format | |
833 | (if (buffer-modified-p buf) | |
834 | "File %s changed on disk. Discard your edits in %s? " | |
835 | "File %s changed on disk. Reread from disk into %s? ") | |
836 | (file-name-nondirectory filename) | |
837 | (buffer-name buf)))) | |
ebeb898f | 838 | (with-current-buffer buf |
b4da00e9 RM |
839 | (revert-buffer t t))))) |
840 | (save-excursion | |
f3e23606 RS |
841 | ;;; The truename stuff makes this obsolete. |
842 | ;;; (let* ((link-name (car (file-attributes filename))) | |
843 | ;;; (linked-buf (and (stringp link-name) | |
844 | ;;; (get-file-buffer link-name)))) | |
845 | ;;; (if (bufferp linked-buf) | |
846 | ;;; (message "Symbolic link to file in buffer %s" | |
847 | ;;; (buffer-name linked-buf)))) | |
b4da00e9 | 848 | (setq buf (create-file-buffer filename)) |
a2e12c0c | 849 | (set-buffer-major-mode buf) |
b4da00e9 RM |
850 | (set-buffer buf) |
851 | (erase-buffer) | |
40dfe94d RS |
852 | (if rawfile |
853 | (condition-case () | |
854 | (insert-file-contents-literally filename t) | |
855 | (file-error | |
856 | ;; Unconditionally set error | |
857 | (setq error t))) | |
858 | (condition-case () | |
859 | (insert-file-contents filename t) | |
860 | (file-error | |
861 | ;; Run find-file-not-found-hooks until one returns non-nil. | |
862 | (or (run-hook-with-args-until-success 'find-file-not-found-hooks) | |
863 | ;; If they fail too, set error. | |
864 | (setq error t))))) | |
f3e23606 | 865 | ;; Find the file's truename, and maybe use that as visited name. |
138c44f6 | 866 | (setq buffer-file-truename truename) |
1770543d RS |
867 | (setq buffer-file-number number) |
868 | ;; On VMS, we may want to remember which directory in a search list | |
869 | ;; the file was found in. | |
870 | (and (eq system-type 'vax-vms) | |
871 | (let (logical) | |
872 | (if (string-match ":" (file-name-directory filename)) | |
873 | (setq logical (substring (file-name-directory filename) | |
874 | 0 (match-beginning 0)))) | |
875 | (not (member logical find-file-not-true-dirname-list))) | |
876 | (setq buffer-file-name buffer-file-truename)) | |
1cc2fbeb | 877 | (if find-file-visit-truename |
27d0420c RS |
878 | (setq buffer-file-name |
879 | (setq filename | |
880 | (expand-file-name buffer-file-truename)))) | |
b4da00e9 RM |
881 | ;; Set buffer's default directory to that of the file. |
882 | (setq default-directory (file-name-directory filename)) | |
883 | ;; Turn off backup files for certain file names. Since | |
884 | ;; this is a permanent local, the major mode won't eliminate it. | |
885 | (and (not (funcall backup-enable-predicate buffer-file-name)) | |
886 | (progn | |
887 | (make-local-variable 'backup-inhibited) | |
888 | (setq backup-inhibited t))) | |
40dfe94d RS |
889 | (if rawfile |
890 | nil | |
fa660969 KH |
891 | (after-find-file error (not nowarn)) |
892 | (setq buf (current-buffer))))) | |
b4da00e9 RM |
893 | buf))) |
894 | \f | |
f7d786d0 RS |
895 | (defvar after-find-file-from-revert-buffer nil) |
896 | ||
e0ab8879 | 897 | (defun after-find-file (&optional error warn noauto |
9a30563f RS |
898 | after-find-file-from-revert-buffer |
899 | nomodes) | |
b4da00e9 RM |
900 | "Called after finding a file and by the default revert function. |
901 | Sets buffer mode, parses local variables. | |
8cfb9d46 | 902 | Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an |
b4da00e9 RM |
903 | error in reading the file. WARN non-nil means warn if there |
904 | exists an auto-save file more recent than the visited file. | |
8cfb9d46 | 905 | NOAUTO means don't mess with auto-save mode. |
e0ab8879 RS |
906 | Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil |
907 | means this call was from `revert-buffer'. | |
9a30563f RS |
908 | Fifth arg NOMODES non-nil means don't alter the file's modes. |
909 | Finishes by calling the functions in `find-file-hooks' | |
910 | unless NOMODES is non-nil." | |
b4da00e9 RM |
911 | (setq buffer-read-only (not (file-writable-p buffer-file-name))) |
912 | (if noninteractive | |
913 | nil | |
914 | (let* (not-serious | |
915 | (msg | |
916 | (cond ((and error (file-attributes buffer-file-name)) | |
917 | (setq buffer-read-only t) | |
ff78d520 | 918 | "File exists, but cannot be read.") |
b4da00e9 RM |
919 | ((not buffer-read-only) |
920 | (if (and warn | |
921 | (file-newer-than-file-p (make-auto-save-file-name) | |
922 | buffer-file-name)) | |
6a6a0b8b RS |
923 | (format "%s has auto save data; consider M-x recover-file" |
924 | (file-name-nondirectory buffer-file-name)) | |
b4da00e9 RM |
925 | (setq not-serious t) |
926 | (if error "(New file)" nil))) | |
927 | ((not error) | |
928 | (setq not-serious t) | |
929 | "Note: file is write protected") | |
930 | ((file-attributes (directory-file-name default-directory)) | |
931 | "File not found and directory write-protected") | |
4e43240a RS |
932 | ((file-exists-p (file-name-directory buffer-file-name)) |
933 | (setq buffer-read-only nil)) | |
b4da00e9 | 934 | (t |
5bbbceb1 | 935 | (setq buffer-read-only nil) |
4e43240a RS |
936 | (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name)))) |
937 | "Use M-x make-dir RET RET to create the directory" | |
938 | "Use C-u M-x make-dir RET RET to create directory and its parents"))))) | |
b4da00e9 RM |
939 | (if msg |
940 | (progn | |
941 | (message msg) | |
942 | (or not-serious (sit-for 1 nil t))))) | |
8cfb9d46 | 943 | (if (and auto-save-default (not noauto)) |
b4da00e9 | 944 | (auto-save-mode t))) |
9a30563f RS |
945 | (if nomodes |
946 | nil | |
947 | (normal-mode t) | |
948 | (run-hooks 'find-file-hooks))) | |
b4da00e9 RM |
949 | |
950 | (defun normal-mode (&optional find-file) | |
951 | "Choose the major mode for this buffer automatically. | |
952 | Also sets up any specified local variables of the file. | |
953 | Uses the visited file name, the -*- line, and the local variables spec. | |
954 | ||
955 | This function is called automatically from `find-file'. In that case, | |
956 | we may set up specified local variables depending on the value of | |
957 | `enable-local-variables': if it is t, we do; if it is nil, we don't; | |
958 | otherwise, we query. `enable-local-variables' is ignored if you | |
959 | run `normal-mode' explicitly." | |
960 | (interactive) | |
961 | (or find-file (funcall (or default-major-mode 'fundamental-mode))) | |
962 | (condition-case err | |
963 | (set-auto-mode) | |
964 | (error (message "File mode specification error: %s" | |
965 | (prin1-to-string err)))) | |
966 | (condition-case err | |
7b3f3dc2 JB |
967 | (let ((enable-local-variables (or (not find-file) |
968 | enable-local-variables))) | |
969 | (hack-local-variables)) | |
b4da00e9 RM |
970 | (error (message "File local-variables error: %s" |
971 | (prin1-to-string err))))) | |
972 | ||
f76e0cd0 | 973 | (defvar auto-mode-alist |
567c1ca9 | 974 | '(("\\.te?xt\\'" . text-mode) |
f76e0cd0 RS |
975 | ("\\.c\\'" . c-mode) |
976 | ("\\.h\\'" . c-mode) | |
977 | ("\\.tex\\'" . tex-mode) | |
978 | ("\\.ltx\\'" . latex-mode) | |
979 | ("\\.el\\'" . emacs-lisp-mode) | |
980 | ("\\.mm\\'" . nroff-mode) | |
981 | ("\\.me\\'" . nroff-mode) | |
982 | ("\\.ms\\'" . nroff-mode) | |
983 | ("\\.man\\'" . nroff-mode) | |
984 | ("\\.scm\\'" . scheme-mode) | |
985 | ("\\.l\\'" . lisp-mode) | |
986 | ("\\.lisp\\'" . lisp-mode) | |
987 | ("\\.f\\'" . fortran-mode) | |
0254679b | 988 | ("\\.F\\'" . fortran-mode) |
f76e0cd0 RS |
989 | ("\\.for\\'" . fortran-mode) |
990 | ("\\.p\\'" . pascal-mode) | |
991 | ("\\.pas\\'" . pascal-mode) | |
992 | ("\\.mss\\'" . scribe-mode) | |
993 | ("\\.ad[abs]\\'" . ada-mode) | |
994 | ("\\.icn\\'" . icon-mode) | |
995 | ("\\.pl\\'" . perl-mode) | |
35394129 | 996 | ("\\.pm\\'" . perl-mode) |
f76e0cd0 RS |
997 | ("\\.cc\\'" . c++-mode) |
998 | ("\\.hh\\'" . c++-mode) | |
bfc6cac9 | 999 | ("\\.hpp\\'" . c++-mode) |
f76e0cd0 RS |
1000 | ("\\.C\\'" . c++-mode) |
1001 | ("\\.H\\'" . c++-mode) | |
1002 | ("\\.cpp\\'" . c++-mode) | |
1003 | ("\\.cxx\\'" . c++-mode) | |
1004 | ("\\.hxx\\'" . c++-mode) | |
1005 | ("\\.c\\+\\+\\'" . c++-mode) | |
1006 | ("\\.h\\+\\+\\'" . c++-mode) | |
1fbd6d0f | 1007 | ("\\.m\\'" . objc-mode) |
95b53225 | 1008 | ("\\.java\\'" . java-mode) |
1fbd6d0f | 1009 | ("\\.sim\\'" . simula-mode) |
f76e0cd0 | 1010 | ("\\.mk\\'" . makefile-mode) |
5da565f8 | 1011 | ("\\(M\\|m\\|GNUm\\)akefile\\(.in\\)?\\'" . makefile-mode) |
7b3f3dc2 JB |
1012 | ;;; Less common extensions come here |
1013 | ;;; so more common ones above are found faster. | |
f76e0cd0 | 1014 | ("\\.texinfo\\'" . texinfo-mode) |
567c1ca9 | 1015 | ("\\.te?xi\\'" . texinfo-mode) |
f76e0cd0 | 1016 | ("\\.s\\'" . asm-mode) |
600a5f71 KH |
1017 | ("\\.S\\'" . asm-mode) |
1018 | ("\\.asm\\'" . asm-mode) | |
f76e0cd0 RS |
1019 | ("ChangeLog\\'" . change-log-mode) |
1020 | ("change.log\\'" . change-log-mode) | |
1021 | ("changelo\\'" . change-log-mode) | |
1022 | ("ChangeLog.[0-9]+\\'" . change-log-mode) | |
6e6f5d9e RS |
1023 | ;; for MSDOS and MS-Windows (which are case-insensitive) |
1024 | ("changelog\\'" . change-log-mode) | |
1025 | ("changelog.[0-9]+\\'" . change-log-mode) | |
f76e0cd0 RS |
1026 | ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode) |
1027 | ("\\.scm\\.[0-9]*\\'" . scheme-mode) | |
7458cc35 | 1028 | ("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode) |
d1194c85 RS |
1029 | ("/\\.\\(bash_profile\\|z?login\\|bash_login\\|z?logout\\)\\'" . sh-mode) |
1030 | ("/\\.\\(bash_logout\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode) | |
1031 | ("/\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode) | |
c1fe251a KH |
1032 | ;;; The following should come after the ChangeLog pattern |
1033 | ;;; for the sake of ChangeLog.1, etc. | |
1034 | ;;; and after the .scm.[0-9] pattern too. | |
f76e0cd0 RS |
1035 | ("\\.[12345678]\\'" . nroff-mode) |
1036 | ("\\.TeX\\'" . tex-mode) | |
1037 | ("\\.sty\\'" . latex-mode) | |
1038 | ("\\.cls\\'" . latex-mode) ;LaTeX 2e class | |
1039 | ("\\.bbl\\'" . latex-mode) | |
1040 | ("\\.bib\\'" . bibtex-mode) | |
1041 | ("\\.article\\'" . text-mode) | |
1042 | ("\\.letter\\'" . text-mode) | |
1043 | ("\\.tcl\\'" . tcl-mode) | |
e1ec01c2 | 1044 | ("\\.exp\\'" . tcl-mode) |
c87c4cda KH |
1045 | ("\\.itcl\\'" . tcl-mode) |
1046 | ("\\.itk\\'" . tcl-mode) | |
f76e0cd0 RS |
1047 | ("\\.f90\\'" . f90-mode) |
1048 | ("\\.lsp\\'" . lisp-mode) | |
1049 | ("\\.awk\\'" . awk-mode) | |
1050 | ("\\.prolog\\'" . prolog-mode) | |
1051 | ("\\.tar\\'" . tar-mode) | |
1052 | ("\\.\\(arc\\|zip\\|lzh\\|zoo\\)\\'" . archive-mode) | |
12a8a239 | 1053 | ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\)\\'" . archive-mode) |
f76e0cd0 RS |
1054 | ;; Mailer puts message to be edited in |
1055 | ;; /tmp/Re.... or Message | |
628b6035 | 1056 | ("\\`/tmp/Re" . text-mode) |
f76e0cd0 RS |
1057 | ("/Message[0-9]*\\'" . text-mode) |
1058 | ("/drafts/[0-9]+\\'" . mh-letter-mode) | |
1059 | ;; some news reader is reported to use this | |
628b6035 | 1060 | ("\\`/tmp/fol/" . text-mode) |
f76e0cd0 RS |
1061 | ("\\.y\\'" . c-mode) |
1062 | ("\\.lex\\'" . c-mode) | |
1063 | ("\\.oak\\'" . scheme-mode) | |
693f800d | 1064 | ("\\.sgml?\\'" . sgml-mode) |
f76e0cd0 | 1065 | ("\\.dtd\\'" . sgml-mode) |
693f800d | 1066 | ("\\.s?html?\\'" . html-mode) |
f76e0cd0 RS |
1067 | ;; .emacs following a directory delimiter |
1068 | ;; in either Unix or VMS syntax. | |
1069 | ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode) | |
1070 | ;; _emacs following a directory delimiter | |
1071 | ;; in MsDos syntax | |
1072 | ("[:/]_emacs\\'" . emacs-lisp-mode) | |
1073 | ("\\.ml\\'" . lisp-mode)) | |
7b3f3dc2 JB |
1074 | "\ |
1075 | Alist of filename patterns vs corresponding major mode functions. | |
116987ba RS |
1076 | Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL). |
1077 | \(NON-NIL stands for anything that is not nil; the value does not matter.) | |
1078 | Visiting a file whose name matches REGEXP specifies FUNCTION as the | |
1079 | mode function to use. FUNCTION will be called, unless it is nil. | |
1080 | ||
1081 | If the element has the form (REGEXP FUNCTION NON-NIL), then after | |
1082 | calling FUNCTION (if it's not nil), we delete the suffix that matched | |
1083 | REGEXP and search the list again for another match.") | |
7b3f3dc2 | 1084 | |
d7fa5aa2 | 1085 | (defvar interpreter-mode-alist |
c907d156 | 1086 | '(("perl" . perl-mode) |
e049945b | 1087 | ("perl5" . perl-mode) |
c907d156 | 1088 | ("wish" . tcl-mode) |
e3998da1 | 1089 | ("wishx" . tcl-mode) |
8db739de | 1090 | ("tcl" . tcl-mode) |
e3998da1 | 1091 | ("tclsh" . tcl-mode) |
8db739de | 1092 | ("awk" . awk-mode) |
dfeadd84 | 1093 | ("mawk" . awk-mode) |
d4f567a1 | 1094 | ("nawk" . awk-mode) |
8db739de | 1095 | ("gawk" . awk-mode) |
d1194c85 RS |
1096 | ("scm" . scheme-mode) |
1097 | ("ash" . sh-mode) | |
1098 | ("bash" . sh-mode) | |
1099 | ("csh" . sh-mode) | |
1100 | ("dtksh" . sh-mode) | |
1101 | ("es" . sh-mode) | |
1102 | ("itcsh" . sh-mode) | |
1103 | ("jsh" . sh-mode) | |
1104 | ("ksh" . sh-mode) | |
1105 | ("oash" . sh-mode) | |
1106 | ("pdksh" . sh-mode) | |
1107 | ("rc" . sh-mode) | |
1108 | ("sh" . sh-mode) | |
1109 | ("sh5" . sh-mode) | |
1110 | ("tcsh" . sh-mode) | |
1111 | ("wksh" . sh-mode) | |
1112 | ("wsh" . sh-mode) | |
1113 | ("zsh" . sh-mode) | |
1114 | ("tail" . text-mode) | |
1115 | ("more" . text-mode) | |
1116 | ("less" . text-mode) | |
1117 | ("pg" . text-mode)) | |
c907d156 RS |
1118 | "Alist mapping interpreter names to major modes. |
1119 | This alist applies to files whose first line starts with `#!'. | |
1120 | Each element looks like (INTERPRETER . MODE). | |
1121 | The car of each element is compared with | |
1122 | the name of the interpreter specified in the first line. | |
1123 | If it matches, mode MODE is selected.") | |
1124 | ||
d7fa5aa2 | 1125 | (defvar inhibit-first-line-modes-regexps '("\\.tar\\'" "\\.tgz\\'") |
45fb3bb8 | 1126 | "List of regexps; if one matches a file name, don't look for `-*-'.") |
a0c9f21b | 1127 | |
d7fa5aa2 | 1128 | (defvar inhibit-first-line-modes-suffixes nil |
b20ff6d0 RS |
1129 | "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'. |
1130 | When checking `inhibit-first-line-modes-regexps', we first discard | |
1131 | from the end of the file name anything that matches one of these regexps.") | |
1132 | ||
bb157910 RS |
1133 | (defvar user-init-file |
1134 | "" ; set by command-line | |
1135 | "File name including directory of user's initialization file.") | |
1136 | ||
b4da00e9 RM |
1137 | (defun set-auto-mode () |
1138 | "Select major mode appropriate for current buffer. | |
e3998da1 RS |
1139 | This checks for a -*- mode tag in the buffer's text, |
1140 | compares the filename against the entries in `auto-mode-alist', | |
1141 | or checks the interpreter that runs this file against | |
1142 | `interpreter-mode-alist'. | |
1143 | ||
1144 | It does not check for the `mode:' local variable in the | |
1145 | Local Variables section of the file; for that, use `hack-local-variables'. | |
7b3f3dc2 | 1146 | |
f3e23606 | 1147 | If `enable-local-variables' is nil, this function does not check for a |
7b3f3dc2 | 1148 | -*- mode tag." |
b4da00e9 | 1149 | ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- |
d0fc03ef | 1150 | (let (beg end done modes) |
b4da00e9 RM |
1151 | (save-excursion |
1152 | (goto-char (point-min)) | |
1153 | (skip-chars-forward " \t\n") | |
9fa7bfe5 RS |
1154 | (and enable-local-variables |
1155 | ;; Don't look for -*- if this file name matches any | |
45fb3bb8 | 1156 | ;; of the regexps in inhibit-first-line-modes-regexps. |
cae111fa | 1157 | (let ((temp inhibit-first-line-modes-regexps) |
4bc3d240 RS |
1158 | (name (if buffer-file-name |
1159 | (file-name-sans-versions buffer-file-name) | |
1160 | (buffer-name)))) | |
38832b4c KH |
1161 | (while (let ((sufs inhibit-first-line-modes-suffixes)) |
1162 | (while (and sufs (not (string-match (car sufs) name))) | |
1163 | (setq sufs (cdr sufs))) | |
1164 | sufs) | |
1165 | (setq name (substring name 0 (match-beginning 0)))) | |
9fa7bfe5 | 1166 | (while (and temp |
b20ff6d0 | 1167 | (not (string-match (car temp) name))) |
9fa7bfe5 RS |
1168 | (setq temp (cdr temp))) |
1169 | (not temp)) | |
1170 | (search-forward "-*-" (save-excursion | |
1171 | ;; If the file begins with "#!" | |
1172 | ;; (exec interpreter magic), look | |
1173 | ;; for mode frobs in the first two | |
1174 | ;; lines. You cannot necessarily | |
1175 | ;; put them in the first line of | |
1176 | ;; such a file without screwing up | |
1177 | ;; the interpreter invocation. | |
1178 | (end-of-line (and (looking-at "^#!") 2)) | |
1179 | (point)) t) | |
1180 | (progn | |
1181 | (skip-chars-forward " \t") | |
1182 | (setq beg (point)) | |
1183 | (search-forward "-*-" | |
1184 | (save-excursion (end-of-line) (point)) | |
1185 | t)) | |
1186 | (progn | |
1187 | (forward-char -3) | |
1188 | (skip-chars-backward " \t") | |
1189 | (setq end (point)) | |
1190 | (goto-char beg) | |
1191 | (if (save-excursion (search-forward ":" end t)) | |
1192 | ;; Find all specifications for the `mode:' variable | |
57a0155c | 1193 | ;; and execute them left to right. |
9fa7bfe5 | 1194 | (while (let ((case-fold-search t)) |
6054fcc6 KH |
1195 | (or (and (looking-at "mode:") |
1196 | (goto-char (match-end 0))) | |
1197 | (re-search-forward "[ \t;]mode:" end t))) | |
9fa7bfe5 RS |
1198 | (skip-chars-forward " \t") |
1199 | (setq beg (point)) | |
1200 | (if (search-forward ";" end t) | |
1201 | (forward-char -1) | |
1202 | (goto-char end)) | |
1203 | (skip-chars-backward " \t") | |
d0fc03ef RS |
1204 | (setq modes (cons (intern (concat (downcase (buffer-substring beg (point))) "-mode")) |
1205 | modes))) | |
9fa7bfe5 | 1206 | ;; Simple -*-MODE-*- case. |
d0fc03ef RS |
1207 | (setq modes (cons (intern (concat (downcase (buffer-substring beg end)) |
1208 | "-mode")) | |
1209 | modes)))))) | |
1210 | ;; If we found modes to use, invoke them now, | |
1211 | ;; outside the save-excursion. | |
1212 | (if modes | |
668d9a3e | 1213 | (progn (mapcar 'funcall (nreverse modes)) |
d0fc03ef RS |
1214 | (setq done t))) |
1215 | ;; If we didn't find a mode from a -*- line, try using the file name. | |
1216 | (if (and (not done) buffer-file-name) | |
1217 | (let ((name buffer-file-name) | |
1218 | (keep-going t)) | |
1219 | ;; Remove backup-suffixes from file name. | |
1220 | (setq name (file-name-sans-versions name)) | |
1221 | (while keep-going | |
1222 | (setq keep-going nil) | |
1223 | (let ((alist auto-mode-alist) | |
1224 | (mode nil)) | |
1225 | ;; Find first matching alist entry. | |
76d5492b | 1226 | (let ((case-fold-search |
d0fc03ef RS |
1227 | (memq system-type '(vax-vms windows-nt)))) |
1228 | (while (and (not mode) alist) | |
1229 | (if (string-match (car (car alist)) name) | |
1230 | (if (and (consp (cdr (car alist))) | |
1231 | (nth 2 (car alist))) | |
1232 | (progn | |
1233 | (setq mode (car (cdr (car alist))) | |
1234 | name (substring name 0 (match-beginning 0)) | |
1235 | keep-going t)) | |
1236 | (setq mode (cdr (car alist)) | |
1237 | keep-going nil))) | |
1238 | (setq alist (cdr alist)))) | |
1239 | (if mode | |
1240 | (funcall mode) | |
1241 | ;; If we can't deduce a mode from the file name, | |
1242 | ;; look for an interpreter specified in the first line. | |
1540e4e9 KH |
1243 | ;; As a special case, allow for things like "#!/bin/env perl", |
1244 | ;; which finds the interpreter anywhere in $PATH. | |
d0fc03ef RS |
1245 | (let ((interpreter |
1246 | (save-excursion | |
1247 | (goto-char (point-min)) | |
dfeadd84 | 1248 | (if (looking-at "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)") |
1540e4e9 KH |
1249 | (buffer-substring (match-beginning 2) |
1250 | (match-end 2)) | |
d0fc03ef RS |
1251 | ""))) |
1252 | elt) | |
1253 | ;; Map interpreter name to a mode. | |
1254 | (setq elt (assoc (file-name-nondirectory interpreter) | |
1255 | interpreter-mode-alist)) | |
1256 | (if elt | |
1257 | (funcall (cdr elt))))))))))) | |
b4da00e9 | 1258 | |
f3e23606 RS |
1259 | (defun hack-local-variables-prop-line () |
1260 | ;; Set local variables specified in the -*- line. | |
9fa7bfe5 RS |
1261 | ;; Ignore any specification for `mode:'; |
1262 | ;; set-auto-mode should already have handled that. | |
f3e23606 RS |
1263 | (save-excursion |
1264 | (goto-char (point-min)) | |
13a66180 KH |
1265 | (let ((result nil) |
1266 | (end (save-excursion (end-of-line (and (looking-at "^#!") 2)) (point)))) | |
f3e23606 RS |
1267 | ;; Parse the -*- line into the `result' alist. |
1268 | (cond ((not (search-forward "-*-" end t)) | |
1269 | ;; doesn't have one. | |
1270 | nil) | |
1271 | ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)") | |
13a66180 KH |
1272 | ;; Simple form: "-*- MODENAME -*-". Already handled. |
1273 | nil) | |
f3e23606 RS |
1274 | (t |
1275 | ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' | |
1276 | ;; (last ";" is optional). | |
1277 | (save-excursion | |
1278 | (if (search-forward "-*-" end t) | |
1279 | (setq end (- (point) 3)) | |
1280 | (error "-*- not terminated before end of line"))) | |
1281 | (while (< (point) end) | |
1282 | (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*") | |
1283 | (error "malformed -*- line")) | |
1284 | (goto-char (match-end 0)) | |
1c26a6f3 KH |
1285 | ;; There used to be a downcase here, |
1286 | ;; but the manual didn't say so, | |
1287 | ;; and people want to set var names that aren't all lc. | |
1288 | (let ((key (intern (buffer-substring | |
1289 | (match-beginning 1) | |
1290 | (match-end 1)))) | |
f3e23606 RS |
1291 | (val (save-restriction |
1292 | (narrow-to-region (point) end) | |
1293 | (read (current-buffer))))) | |
93a2702d RS |
1294 | ;; It is traditional to ignore |
1295 | ;; case when checking for `mode' in set-auto-mode, | |
1296 | ;; so we must do that here as well. | |
1297 | ;; That is inconsistent, but we're stuck with it. | |
1298 | (or (equal (downcase (symbol-name key)) "mode") | |
13a66180 | 1299 | (setq result (cons (cons key val) result))) |
f3e23606 RS |
1300 | (skip-chars-forward " \t;"))) |
1301 | (setq result (nreverse result)))) | |
76d5492b | 1302 | |
f3e23606 RS |
1303 | (if (and result |
1304 | (or (eq enable-local-variables t) | |
1305 | (and enable-local-variables | |
1306 | (save-window-excursion | |
6f931919 RS |
1307 | (condition-case nil |
1308 | (switch-to-buffer (current-buffer)) | |
1309 | (error | |
1310 | ;; If we fail to switch in the selected window, | |
1311 | ;; it is probably a minibuffer. | |
1312 | ;; So try another window. | |
1313 | (condition-case nil | |
1314 | (switch-to-buffer-other-window (current-buffer)) | |
1315 | (error | |
1316 | (switch-to-buffer-other-frame (current-buffer)))))) | |
f3e23606 RS |
1317 | (y-or-n-p (format "Set local variables as specified in -*- line of %s? " |
1318 | (file-name-nondirectory buffer-file-name))))))) | |
2837b9df RS |
1319 | (let ((enable-local-eval enable-local-eval)) |
1320 | (while result | |
1321 | (hack-one-local-variable (car (car result)) (cdr (car result))) | |
1322 | (setq result (cdr result)))))))) | |
f3e23606 | 1323 | |
1d517019 RS |
1324 | (defvar hack-local-variables-hook nil |
1325 | "Normal hook run after processing a file's local variables specs. | |
1326 | Major modes can use this to examine user-specified local variables | |
1327 | in order to initialize other data structure based on them.") | |
1328 | ||
7b3f3dc2 | 1329 | (defun hack-local-variables () |
5792c834 | 1330 | "Parse and put into effect this buffer's local variables spec." |
f3e23606 | 1331 | (hack-local-variables-prop-line) |
b4da00e9 RM |
1332 | ;; Look for "Local variables:" line in last page. |
1333 | (save-excursion | |
1334 | (goto-char (point-max)) | |
1335 | (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) | |
1336 | (if (let ((case-fold-search t)) | |
1337 | (and (search-forward "Local Variables:" nil t) | |
7b3f3dc2 | 1338 | (or (eq enable-local-variables t) |
b4da00e9 RM |
1339 | (and enable-local-variables |
1340 | (save-window-excursion | |
1341 | (switch-to-buffer (current-buffer)) | |
1342 | (save-excursion | |
1343 | (beginning-of-line) | |
1344 | (set-window-start (selected-window) (point))) | |
1345 | (y-or-n-p (format "Set local variables as specified at end of %s? " | |
51de78c1 | 1346 | (if buffer-file-name |
76d5492b | 1347 | (file-name-nondirectory |
51de78c1 RS |
1348 | buffer-file-name) |
1349 | (concat "buffer " | |
1350 | (buffer-name)))))))))) | |
b4da00e9 | 1351 | (let ((continue t) |
2bba782c RS |
1352 | prefix prefixlen suffix beg |
1353 | (enable-local-eval enable-local-eval)) | |
b4da00e9 RM |
1354 | ;; The prefix is what comes before "local variables:" in its line. |
1355 | ;; The suffix is what comes after "local variables:" in its line. | |
1356 | (skip-chars-forward " \t") | |
1357 | (or (eolp) | |
1358 | (setq suffix (buffer-substring (point) | |
1359 | (progn (end-of-line) (point))))) | |
1360 | (goto-char (match-beginning 0)) | |
1361 | (or (bolp) | |
1362 | (setq prefix | |
1363 | (buffer-substring (point) | |
1364 | (progn (beginning-of-line) (point))))) | |
7b3f3dc2 | 1365 | |
b4da00e9 RM |
1366 | (if prefix (setq prefixlen (length prefix) |
1367 | prefix (regexp-quote prefix))) | |
1368 | (if suffix (setq suffix (concat (regexp-quote suffix) "$"))) | |
1369 | (while continue | |
1370 | ;; Look at next local variable spec. | |
1371 | (if selective-display (re-search-forward "[\n\C-m]") | |
1372 | (forward-line 1)) | |
1373 | ;; Skip the prefix, if any. | |
1374 | (if prefix | |
1375 | (if (looking-at prefix) | |
1376 | (forward-char prefixlen) | |
1377 | (error "Local variables entry is missing the prefix"))) | |
1378 | ;; Find the variable name; strip whitespace. | |
1379 | (skip-chars-forward " \t") | |
1380 | (setq beg (point)) | |
1381 | (skip-chars-forward "^:\n") | |
1382 | (if (eolp) (error "Missing colon in local variables entry")) | |
1383 | (skip-chars-backward " \t") | |
1384 | (let* ((str (buffer-substring beg (point))) | |
1385 | (var (read str)) | |
1386 | val) | |
1387 | ;; Setting variable named "end" means end of list. | |
1388 | (if (string-equal (downcase str) "end") | |
1389 | (setq continue nil) | |
1390 | ;; Otherwise read the variable value. | |
1391 | (skip-chars-forward "^:") | |
1392 | (forward-char 1) | |
1393 | (setq val (read (current-buffer))) | |
1394 | (skip-chars-backward "\n") | |
1395 | (skip-chars-forward " \t") | |
1396 | (or (if suffix (looking-at suffix) (eolp)) | |
1397 | (error "Local variables entry is terminated incorrectly")) | |
1398 | ;; Set the variable. "Variables" mode and eval are funny. | |
1d517019 RS |
1399 | (hack-one-local-variable var val))))))) |
1400 | (run-hooks 'hack-local-variables-hook)) | |
f3e23606 | 1401 | |
d7fa5aa2 | 1402 | (defvar ignored-local-variables |
f3e23606 RS |
1403 | '(enable-local-eval) |
1404 | "Variables to be ignored in a file's local variable spec.") | |
1405 | ||
be0218a8 | 1406 | ;; Get confirmation before setting these variables as locals in a file. |
05ef1cda | 1407 | (put 'debugger 'risky-local-variable t) |
f6fa3ee3 | 1408 | (put 'enable-local-eval 'risky-local-variable t) |
fce47eea | 1409 | (put 'ignored-local-variables 'risky-local-variable t) |
be0218a8 RS |
1410 | (put 'eval 'risky-local-variable t) |
1411 | (put 'file-name-handler-alist 'risky-local-variable t) | |
1412 | (put 'minor-mode-map-alist 'risky-local-variable t) | |
1413 | (put 'after-load-alist 'risky-local-variable t) | |
f6fa3ee3 RS |
1414 | (put 'buffer-file-name 'risky-local-variable t) |
1415 | (put 'buffer-auto-save-file-name 'risky-local-variable t) | |
1416 | (put 'buffer-file-truename 'risky-local-variable t) | |
b0ffcc0d RS |
1417 | (put 'exec-path 'risky-local-variable t) |
1418 | (put 'load-path 'risky-local-variable t) | |
1419 | (put 'exec-directory 'risky-local-variable t) | |
1420 | (put 'process-environment 'risky-local-variable t) | |
eba1c87a RS |
1421 | (put 'dabbrev-case-fold-search 'risky-local-variable t) |
1422 | (put 'dabbrev-case-replace 'risky-local-variable t) | |
9afa8bf7 RS |
1423 | ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode. |
1424 | (put 'outline-level 'risky-local-variable t) | |
d98b741d | 1425 | (put 'rmail-output-file-alist 'risky-local-variable t) |
f6fa3ee3 | 1426 | |
6672c42b RS |
1427 | ;; This one is safe because the user gets to check it before it is used. |
1428 | (put 'compile-command 'safe-local-variable t) | |
1429 | ||
d0bd3513 RS |
1430 | (defun hack-one-local-variable-quotep (exp) |
1431 | (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp)))) | |
1432 | ||
f3e23606 RS |
1433 | ;; "Set" one variable in a local variables spec. |
1434 | ;; A few variable names are treated specially. | |
1435 | (defun hack-one-local-variable (var val) | |
1436 | (cond ((eq var 'mode) | |
1437 | (funcall (intern (concat (downcase (symbol-name val)) | |
1438 | "-mode")))) | |
1439 | ((memq var ignored-local-variables) | |
1440 | nil) | |
1441 | ;; "Setting" eval means either eval it or do nothing. | |
7d3221d7 | 1442 | ;; Likewise for setting hook variables. |
be0218a8 | 1443 | ((or (get var 'risky-local-variable) |
6672c42b RS |
1444 | (and |
1445 | (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$" | |
1446 | (symbol-name var)) | |
1447 | (not (get var 'safe-local-variable)))) | |
0225ae5e | 1448 | ;; Permit evalling a put of a harmless property. |
d0bd3513 RS |
1449 | ;; if the args do nothing tricky. |
1450 | (if (or (and (eq var 'eval) | |
1451 | (consp val) | |
1452 | (eq (car val) 'put) | |
1453 | (hack-one-local-variable-quotep (nth 1 val)) | |
1454 | (hack-one-local-variable-quotep (nth 2 val)) | |
1455 | ;; Only allow safe values of lisp-indent-hook; | |
1456 | ;; not functions. | |
1457 | (or (numberp (nth 3 val)) | |
dc417415 | 1458 | (equal (nth 3 val) ''defun)) |
d0bd3513 RS |
1459 | (memq (nth 1 (nth 2 val)) |
1460 | '(lisp-indent-hook))) | |
1461 | ;; Permit eval if not root and user says ok. | |
c7e62660 | 1462 | (and (not (zerop (user-uid))) |
d0bd3513 RS |
1463 | (or (eq enable-local-eval t) |
1464 | (and enable-local-eval | |
1465 | (save-window-excursion | |
1466 | (switch-to-buffer (current-buffer)) | |
1467 | (save-excursion | |
1468 | (beginning-of-line) | |
1469 | (set-window-start (selected-window) (point))) | |
1470 | (setq enable-local-eval | |
1471 | (y-or-n-p (format "Process `eval' or hook local variables in file %s? " | |
1472 | (file-name-nondirectory buffer-file-name))))))))) | |
7d3221d7 RS |
1473 | (if (eq var 'eval) |
1474 | (save-excursion (eval val)) | |
1475 | (make-local-variable var) | |
1476 | (set var val)) | |
f3e23606 RS |
1477 | (message "Ignoring `eval:' in file's local variables"))) |
1478 | ;; Ordinary variable, really set it. | |
1479 | (t (make-local-variable var) | |
1480 | (set var val)))) | |
1481 | ||
b4da00e9 | 1482 | \f |
f36012a6 | 1483 | (defun set-visited-file-name (filename &optional no-query along-with-file) |
b4da00e9 RM |
1484 | "Change name of file visited in current buffer to FILENAME. |
1485 | The next time the buffer is saved it will go in the newly specified file. | |
1486 | nil or empty string as argument means make buffer not be visiting any file. | |
1487 | Remember to delete the initial contents of the minibuffer | |
6a6b62f8 RS |
1488 | if you wish to pass an empty string as the argument. |
1489 | ||
1490 | The optional second argument NO-QUERY, if non-nil, inhibits asking for | |
f36012a6 RS |
1491 | confirmation in the case where another buffer is already visiting FILENAME. |
1492 | ||
1493 | The optional third argument ALONG-WITH-FILE, if non-nil, means that | |
1494 | the old visited file has been renamed to the new name FILENAME." | |
b4da00e9 | 1495 | (interactive "FSet visited file name: ") |
c11a94fe RS |
1496 | (if (buffer-base-buffer) |
1497 | (error "An indirect buffer cannot visit a file")) | |
a522e5bf RS |
1498 | (let (truename) |
1499 | (if filename | |
1500 | (setq filename | |
1501 | (if (string-equal filename "") | |
1502 | nil | |
1503 | (expand-file-name filename)))) | |
1504 | (if filename | |
1505 | (progn | |
1506 | (setq truename (file-truename filename)) | |
1507 | (if find-file-visit-truename | |
a522e5bf | 1508 | (setq filename truename)))) |
11e314fa | 1509 | (let ((buffer (and filename (find-buffer-visiting filename)))) |
7b89d38e | 1510 | (and buffer (not (eq buffer (current-buffer))) |
6a6b62f8 | 1511 | (not no-query) |
7b89d38e RS |
1512 | (not (y-or-n-p (message "A buffer is visiting %s; proceed? " |
1513 | filename))) | |
1514 | (error "Aborted"))) | |
a522e5bf RS |
1515 | (or (equal filename buffer-file-name) |
1516 | (progn | |
1517 | (and filename (lock-buffer filename)) | |
1518 | (unlock-buffer))) | |
1519 | (setq buffer-file-name filename) | |
1520 | (if filename ; make buffer name reflect filename. | |
1521 | (let ((new-name (file-name-nondirectory buffer-file-name))) | |
1522 | (if (string= new-name "") | |
1523 | (error "Empty file name")) | |
1524 | (if (eq system-type 'vax-vms) | |
1525 | (setq new-name (downcase new-name))) | |
1526 | (setq default-directory (file-name-directory buffer-file-name)) | |
1527 | (or (string= new-name (buffer-name)) | |
1528 | (rename-buffer new-name t)))) | |
1529 | (setq buffer-backed-up nil) | |
f36012a6 RS |
1530 | (or along-with-file |
1531 | (clear-visited-file-modtime)) | |
8ccdc29e | 1532 | ;; Abbreviate the file names of the buffer. |
4826e97f | 1533 | (if truename |
8ccdc29e RS |
1534 | (progn |
1535 | (setq buffer-file-truename (abbreviate-file-name truename)) | |
1536 | (if find-file-visit-truename | |
1537 | (setq buffer-file-name buffer-file-truename)))) | |
a522e5bf RS |
1538 | (setq buffer-file-number |
1539 | (if filename | |
2a47b4f5 | 1540 | (nthcdr 10 (file-attributes buffer-file-name)) |
a522e5bf | 1541 | nil))) |
b4da00e9 RM |
1542 | ;; write-file-hooks is normally used for things like ftp-find-file |
1543 | ;; that visit things that are not local files as if they were files. | |
1544 | ;; Changing to visit an ordinary local file instead should flush the hook. | |
1545 | (kill-local-variable 'write-file-hooks) | |
c9dca4e0 | 1546 | (kill-local-variable 'local-write-file-hooks) |
b4da00e9 RM |
1547 | (kill-local-variable 'revert-buffer-function) |
1548 | (kill-local-variable 'backup-inhibited) | |
ee81c959 RS |
1549 | ;; If buffer was read-only because of version control, |
1550 | ;; that reason is gone now, so make it writable. | |
1551 | (if vc-mode | |
1552 | (setq buffer-read-only nil)) | |
1553 | (kill-local-variable 'vc-mode) | |
b4da00e9 RM |
1554 | ;; Turn off backup files for certain file names. |
1555 | ;; Since this is a permanent local, the major mode won't eliminate it. | |
1556 | (and (not (funcall backup-enable-predicate buffer-file-name)) | |
1557 | (progn | |
1558 | (make-local-variable 'backup-inhibited) | |
1559 | (setq backup-inhibited t))) | |
c77a81cf RS |
1560 | (let ((oauto buffer-auto-save-file-name)) |
1561 | ;; If auto-save was not already on, turn it on if appropriate. | |
1562 | (if (not buffer-auto-save-file-name) | |
1563 | (and buffer-file-name auto-save-default | |
1564 | (auto-save-mode t)) | |
1565 | ;; If auto save is on, start using a new name. | |
1566 | ;; We deliberately don't rename or delete the old auto save | |
1567 | ;; for the old visited file name. This is because perhaps | |
1568 | ;; the user wants to save the new state and then compare with the | |
1569 | ;; previous state from the auto save file. | |
1570 | (setq buffer-auto-save-file-name | |
1571 | (make-auto-save-file-name))) | |
1572 | ;; Rename the old auto save file if any. | |
1573 | (and oauto buffer-auto-save-file-name | |
e6f0e76c | 1574 | (file-exists-p oauto) |
c77a81cf | 1575 | (rename-file oauto buffer-auto-save-file-name t))) |
f36012a6 RS |
1576 | (and buffer-file-name |
1577 | (not along-with-file) | |
1578 | (set-buffer-modified-p t))) | |
b4da00e9 | 1579 | |
82d0954a | 1580 | (defun write-file (filename &optional confirm coding-system) |
b4da00e9 | 1581 | "Write current buffer into file FILENAME. |
41f48cb1 RS |
1582 | Makes buffer visit that file, and marks it not modified. |
1583 | If the buffer is already visiting a file, you can specify | |
1584 | a directory name as FILENAME, to write a file of the same | |
c2fb8488 | 1585 | old name in that directory. |
7458cc35 | 1586 | |
c2fb8488 | 1587 | If optional second arg CONFIRM is non-nil, |
7458cc35 | 1588 | ask for confirmation for overwriting an existing file. |
82d0954a KH |
1589 | Interactively, confirmation is required unless you supply a prefix argument. |
1590 | ||
1591 | A prefix argument also enables user to interactively specify a | |
1592 | coding-system for encoding the file." | |
b4da00e9 RM |
1593 | ;; (interactive "FWrite file: ") |
1594 | (interactive | |
1595 | (list (if buffer-file-name | |
1596 | (read-file-name "Write file: " | |
1597 | nil nil nil nil) | |
1598 | (read-file-name "Write file: " | |
1599 | (cdr (assq 'default-directory | |
1600 | (buffer-local-variables))) | |
c2fb8488 | 1601 | nil nil (buffer-name))) |
82d0954a KH |
1602 | (not current-prefix-arg) |
1603 | (if current-prefix-arg | |
1604 | (read-coding-system "Coding-system: ")) | |
1605 | )) | |
b4da00e9 | 1606 | (or (null filename) (string-equal filename "") |
41f48cb1 RS |
1607 | (progn |
1608 | ;; If arg is just a directory, | |
1609 | ;; use same file name, but in that directory. | |
1610 | (if (and (file-directory-p filename) buffer-file-name) | |
1611 | (setq filename (concat (file-name-as-directory filename) | |
1612 | (file-name-nondirectory buffer-file-name)))) | |
c2fb8488 RS |
1613 | (and confirm |
1614 | (file-exists-p filename) | |
1615 | (or (y-or-n-p (format "File `%s' exists; overwrite? " filename)) | |
1616 | (error "Canceled"))) | |
5f65549e | 1617 | (set-visited-file-name filename (not confirm)))) |
b4da00e9 | 1618 | (set-buffer-modified-p t) |
82d0954a KH |
1619 | (if coding-system |
1620 | (let ((coding-system-for-write coding-system)) | |
1621 | ;; It is convenient to change buffer-file-coding-system to the | |
1622 | ;; specified one. | |
1623 | (set-buffer-file-coding-system coding-system) | |
1624 | (save-buffer)) | |
1625 | (save-buffer))) | |
b4da00e9 RM |
1626 | \f |
1627 | (defun backup-buffer () | |
1628 | "Make a backup of the disk file visited by the current buffer, if appropriate. | |
1629 | This is normally done before saving the buffer the first time. | |
1630 | If the value is non-nil, it is the result of `file-modes' on the original | |
1631 | file; this means that the caller, after saving the buffer, should change | |
27ab6944 KH |
1632 | the modes of the new file to agree with the old modes. |
1633 | ||
1634 | A backup may be done by renaming or by copying; see documentation of | |
1635 | variable `make-backup-files'. If it's done by renaming, then the file is | |
1636 | no longer accessible under its old name." | |
b4da00e9 RM |
1637 | (if (and make-backup-files (not backup-inhibited) |
1638 | (not buffer-backed-up) | |
1639 | (file-exists-p buffer-file-name) | |
1640 | (memq (aref (elt (file-attributes buffer-file-name) 8) 0) | |
1641 | '(?- ?l))) | |
1642 | (let ((real-file-name buffer-file-name) | |
1643 | backup-info backupname targets setmodes) | |
1644 | ;; If specified name is a symbolic link, chase it to the target. | |
1645 | ;; Thus we make the backups in the directory where the real file is. | |
5dadeb29 | 1646 | (setq real-file-name (file-chase-links real-file-name)) |
b4da00e9 RM |
1647 | (setq backup-info (find-backup-file-name real-file-name) |
1648 | backupname (car backup-info) | |
1649 | targets (cdr backup-info)) | |
1650 | ;;; (if (file-directory-p buffer-file-name) | |
1651 | ;;; (error "Cannot save buffer in directory %s" buffer-file-name)) | |
eb650569 RS |
1652 | (if backup-info |
1653 | (condition-case () | |
1654 | (let ((delete-old-versions | |
1655 | ;; If have old versions to maybe delete, | |
1656 | ;; ask the user to confirm now, before doing anything. | |
1657 | ;; But don't actually delete til later. | |
1658 | (and targets | |
1659 | (or (eq delete-old-versions t) (eq delete-old-versions nil)) | |
1660 | (or delete-old-versions | |
1661 | (y-or-n-p (format "Delete excess backup versions of %s? " | |
1662 | real-file-name)))))) | |
1663 | ;; Actually write the back up file. | |
1664 | (condition-case () | |
1665 | (if (or file-precious-flag | |
1666 | ; (file-symlink-p buffer-file-name) | |
1667 | backup-by-copying | |
1668 | (and backup-by-copying-when-linked | |
1669 | (> (file-nlinks real-file-name) 1)) | |
1670 | (and backup-by-copying-when-mismatch | |
1671 | (let ((attr (file-attributes real-file-name))) | |
1672 | (or (nth 9 attr) | |
1673 | (not (file-ownership-preserved-p real-file-name)))))) | |
1674 | (condition-case () | |
1675 | (copy-file real-file-name backupname t t) | |
1676 | (file-error | |
1677 | ;; If copying fails because file BACKUPNAME | |
1678 | ;; is not writable, delete that file and try again. | |
1679 | (if (and (file-exists-p backupname) | |
1680 | (not (file-writable-p backupname))) | |
1681 | (delete-file backupname)) | |
1682 | (copy-file real-file-name backupname t t))) | |
1683 | ;; rename-file should delete old backup. | |
1684 | (rename-file real-file-name backupname t) | |
1685 | (setq setmodes (file-modes backupname))) | |
1686 | (file-error | |
1687 | ;; If trouble writing the backup, write it in ~. | |
567c1ca9 RS |
1688 | (setq backupname (expand-file-name |
1689 | (convert-standard-filename | |
1690 | "~/%backup%~"))) | |
1691 | (message "Cannot write backup file; backing up in %s" | |
1692 | (file-name-nondirectory backupname)) | |
eb650569 RS |
1693 | (sleep-for 1) |
1694 | (condition-case () | |
1695 | (copy-file real-file-name backupname t t) | |
1696 | (file-error | |
1697 | ;; If copying fails because file BACKUPNAME | |
1698 | ;; is not writable, delete that file and try again. | |
1699 | (if (and (file-exists-p backupname) | |
1700 | (not (file-writable-p backupname))) | |
1701 | (delete-file backupname)) | |
1702 | (copy-file real-file-name backupname t t))))) | |
1703 | (setq buffer-backed-up t) | |
1704 | ;; Now delete the old versions, if desired. | |
1705 | (if delete-old-versions | |
1706 | (while targets | |
1707 | (condition-case () | |
1708 | (delete-file (car targets)) | |
1709 | (file-error nil)) | |
1710 | (setq targets (cdr targets)))) | |
1711 | setmodes) | |
1712 | (file-error nil)))))) | |
b4da00e9 | 1713 | |
c3554e95 | 1714 | (defun file-name-sans-versions (name &optional keep-backup-version) |
b4da00e9 RM |
1715 | "Return FILENAME sans backup versions or strings. |
1716 | This is a separate procedure so your site-init or startup file can | |
c3554e95 RS |
1717 | redefine it. |
1718 | If the optional argument KEEP-BACKUP-VERSION is non-nil, | |
1719 | we do not remove backup version numbers, only true file version numbers." | |
6eaebaa2 | 1720 | (let ((handler (find-file-name-handler name 'file-name-sans-versions))) |
c3554e95 RS |
1721 | (if handler |
1722 | (funcall handler 'file-name-sans-versions name keep-backup-version) | |
1723 | (substring name 0 | |
1724 | (if (eq system-type 'vax-vms) | |
1725 | ;; VMS version number is (a) semicolon, optional | |
1726 | ;; sign, zero or more digits or (b) period, option | |
1727 | ;; sign, zero or more digits, provided this is the | |
1728 | ;; second period encountered outside of the | |
1729 | ;; device/directory part of the file name. | |
26add1bf KH |
1730 | (or (string-match ";[-+]?[0-9]*\\'" name) |
1731 | (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'" | |
c3554e95 RS |
1732 | name) |
1733 | (match-beginning 1)) | |
1734 | (length name)) | |
1735 | (if keep-backup-version | |
1736 | (length name) | |
dbb12f2c | 1737 | (or (string-match "\\.~[0-9.]+~\\'" name) |
c3554e95 RS |
1738 | (string-match "~\\'" name) |
1739 | (length name)))))))) | |
b4da00e9 | 1740 | |
cb0cd911 RS |
1741 | (defun file-ownership-preserved-p (file) |
1742 | "Returns t if deleting FILE and rewriting it would preserve the owner." | |
1743 | (let ((handler (find-file-name-handler file 'file-ownership-preserved-p))) | |
1744 | (if handler | |
1745 | (funcall handler 'file-ownership-preserved-p file) | |
ec5533be | 1746 | (let ((attributes (file-attributes file))) |
306faa42 RS |
1747 | ;; Return t if the file doesn't exist, since it's true that no |
1748 | ;; information would be lost by an (attempted) delete and create. | |
1749 | (or (null attributes) | |
1750 | (= (nth 2 attributes) (user-uid))))))) | |
cb0cd911 | 1751 | |
20b5d24c RS |
1752 | (defun file-name-sans-extension (filename) |
1753 | "Return FILENAME sans final \"extension\". | |
1754 | The extension, in a file name, is the part that follows the last `.'." | |
1755 | (save-match-data | |
1756 | (let ((file (file-name-sans-versions (file-name-nondirectory filename))) | |
1757 | directory) | |
1758 | (if (string-match "\\.[^.]*\\'" file) | |
1759 | (if (setq directory (file-name-directory filename)) | |
1760 | (expand-file-name (substring file 0 (match-beginning 0)) | |
1761 | directory) | |
1762 | (substring file 0 (match-beginning 0))) | |
1763 | filename)))) | |
1764 | ||
93a2702d RS |
1765 | (defun file-name-extension (filename &optional period) |
1766 | "Return FILENAME's final \"extension\". | |
1767 | The extension, in a file name, is the part that follows the last `.'. | |
1768 | Return nil for extensionless file names such as `foo'. | |
1769 | Return the empty string for file names such as `foo.'. | |
1770 | ||
1771 | If PERIOD is non-nil, then the returned value includes the period | |
1772 | that delimits the extension, and if FILENAME has no extension, | |
1773 | the value is \"\"." | |
1774 | (save-match-data | |
1775 | (let ((file (file-name-sans-versions (file-name-nondirectory filename)))) | |
1776 | (if (string-match "\\.[^.]*\\'" file) | |
1777 | (substring file (+ (match-beginning 0) (if period 0 1))) | |
1778 | (if period | |
1779 | ""))))) | |
1780 | ||
b4da00e9 RM |
1781 | (defun make-backup-file-name (file) |
1782 | "Create the non-numeric backup file name for FILE. | |
1783 | This is a separate function so you can redefine it for customization." | |
28ee503c KH |
1784 | (if (and (eq system-type 'ms-dos) |
1785 | (not (msdos-long-file-names))) | |
bb157910 RS |
1786 | (let ((fn (file-name-nondirectory file))) |
1787 | (concat (file-name-directory file) | |
066327ae KH |
1788 | (or |
1789 | (and (string-match "\\`[^.]+\\'" fn) | |
1790 | (concat (match-string 0 fn) ".~")) | |
1791 | (and (string-match "\\`[^.]+\\.\\(..?\\)?" fn) | |
1792 | (concat (match-string 0 fn) "~"))))) | |
bb157910 | 1793 | (concat file "~"))) |
b4da00e9 RM |
1794 | |
1795 | (defun backup-file-name-p (file) | |
1796 | "Return non-nil if FILE is a backup file name (numeric or not). | |
1797 | This is a separate function so you can redefine it for customization. | |
1798 | You may need to redefine `file-name-sans-versions' as well." | |
066327ae | 1799 | (string-match "~\\'" file)) |
b4da00e9 | 1800 | |
2d051399 RS |
1801 | ;; This is used in various files. |
1802 | ;; The usage of bv-length is not very clean, | |
1803 | ;; but I can't see a good alternative, | |
1804 | ;; so as of now I am leaving it alone. | |
1805 | (defun backup-extract-version (fn) | |
1806 | "Given the name of a numeric backup file, return the backup number. | |
1807 | Uses the free variable `bv-length', whose value should be | |
1808 | the index in the name where the version number begins." | |
1809 | (if (and (string-match "[0-9]+~$" fn bv-length) | |
1810 | (= (match-beginning 0) bv-length)) | |
1811 | (string-to-int (substring fn bv-length -1)) | |
1812 | 0)) | |
1813 | ||
b4da00e9 RM |
1814 | ;; I believe there is no need to alter this behavior for VMS; |
1815 | ;; since backup files are not made on VMS, it should not get called. | |
1816 | (defun find-backup-file-name (fn) | |
1817 | "Find a file name for a backup file, and suggestions for deletions. | |
1818 | Value is a list whose car is the name for the backup file | |
eb650569 RS |
1819 | and whose cdr is a list of old versions to consider deleting now. |
1820 | If the value is nil, don't make a backup." | |
1821 | (let ((handler (find-file-name-handler fn 'find-backup-file-name))) | |
1822 | ;; Run a handler for this function so that ange-ftp can refuse to do it. | |
1823 | (if handler | |
1824 | (funcall handler 'find-backup-file-name fn) | |
1825 | (if (eq version-control 'never) | |
b4da00e9 | 1826 | (list (make-backup-file-name fn)) |
eb650569 RS |
1827 | (let* ((base-versions (concat (file-name-nondirectory fn) ".~")) |
1828 | (bv-length (length base-versions)) | |
1829 | possibilities | |
1830 | (versions nil) | |
1831 | (high-water-mark 0) | |
1832 | (deserve-versions-p nil) | |
1833 | (number-to-delete 0)) | |
1834 | (condition-case () | |
1835 | (setq possibilities (file-name-all-completions | |
1836 | base-versions | |
1837 | (file-name-directory fn)) | |
1838 | versions (sort (mapcar | |
1839 | (function backup-extract-version) | |
1840 | possibilities) | |
1841 | '<) | |
1842 | high-water-mark (apply 'max 0 versions) | |
1843 | deserve-versions-p (or version-control | |
1844 | (> high-water-mark 0)) | |
1845 | number-to-delete (- (length versions) | |
1846 | kept-old-versions kept-new-versions -1)) | |
1847 | (file-error | |
1848 | (setq possibilities nil))) | |
1849 | (if (not deserve-versions-p) | |
1850 | (list (make-backup-file-name fn)) | |
1851 | (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~") | |
1852 | (if (and (> number-to-delete 0) | |
1853 | ;; Delete nothing if there is overflow | |
1854 | ;; in the number of versions to keep. | |
1855 | (>= (+ kept-new-versions kept-old-versions -1) 0)) | |
1856 | (mapcar (function (lambda (n) | |
1857 | (concat fn ".~" (int-to-string n) "~"))) | |
1858 | (let ((v (nthcdr kept-old-versions versions))) | |
1859 | (rplacd (nthcdr (1- number-to-delete) v) ()) | |
1860 | v)))))))))) | |
b4da00e9 | 1861 | |
b4da00e9 RM |
1862 | (defun file-nlinks (filename) |
1863 | "Return number of names file FILENAME has." | |
1864 | (car (cdr (file-attributes filename)))) | |
6c636af9 RM |
1865 | |
1866 | (defun file-relative-name (filename &optional directory) | |
1867 | "Convert FILENAME to be relative to DIRECTORY (default: default-directory)." | |
96c188b0 EN |
1868 | (save-match-data |
1869 | (setq filename (expand-file-name filename) | |
1870 | directory (file-name-as-directory | |
1871 | (expand-file-name (or directory default-directory)))) | |
1872 | (let ((ancestor "")) | |
1873 | (while (not (string-match (concat "^" (regexp-quote directory)) filename)) | |
1874 | (setq directory (file-name-directory (substring directory 0 -1)) | |
1875 | ancestor (concat "../" ancestor))) | |
1876 | (concat ancestor (substring filename (match-end 0)))))) | |
b4da00e9 RM |
1877 | \f |
1878 | (defun save-buffer (&optional args) | |
1879 | "Save current buffer in visited file if modified. Versions described below. | |
1880 | By default, makes the previous version into a backup file | |
1881 | if previously requested or if this is the first save. | |
ac9650be | 1882 | With 1 \\[universal-argument], marks this version |
b4da00e9 | 1883 | to become a backup when the next save is done. |
ac9650be | 1884 | With 2 \\[universal-argument]'s, |
b4da00e9 | 1885 | unconditionally makes the previous version into a backup file. |
ac9650be RS |
1886 | With 3 \\[universal-argument]'s, marks this version |
1887 | to become a backup when the next save is done, | |
1888 | and unconditionally makes the previous version into a backup file. | |
1889 | ||
b4da00e9 RM |
1890 | With argument of 0, never makes the previous version into a backup file. |
1891 | ||
1892 | If a file's name is FOO, the names of its numbered backup versions are | |
1893 | FOO.~i~ for various integers i. A non-numbered backup file is called FOO~. | |
1894 | Numeric backups (rather than FOO~) will be made if value of | |
1895 | `version-control' is not the atom `never' and either there are already | |
1896 | numeric versions of the file being backed up, or `version-control' is | |
1897 | non-nil. | |
1898 | We don't want excessive versions piling up, so there are variables | |
1899 | `kept-old-versions', which tells Emacs how many oldest versions to keep, | |
1900 | and `kept-new-versions', which tells how many newest versions to keep. | |
1901 | Defaults are 2 old versions and 2 new. | |
1902 | `dired-kept-versions' controls dired's clean-directory (.) command. | |
de7d5e1b | 1903 | If `delete-old-versions' is nil, system will query user |
b4da00e9 RM |
1904 | before trimming versions. Otherwise it does it silently." |
1905 | (interactive "p") | |
1906 | (let ((modp (buffer-modified-p)) | |
1907 | (large (> (buffer-size) 50000)) | |
b5a8e0fc RS |
1908 | (make-backup-files (or (and make-backup-files (not (eq args 0))) |
1909 | (memq args '(16 64))))) | |
b4da00e9 RM |
1910 | (and modp (memq args '(16 64)) (setq buffer-backed-up nil)) |
1911 | (if (and modp large) (message "Saving file %s..." (buffer-file-name))) | |
1912 | (basic-save-buffer) | |
1913 | (and modp (memq args '(4 64)) (setq buffer-backed-up nil)))) | |
1914 | ||
1915 | (defun delete-auto-save-file-if-necessary (&optional force) | |
1916 | "Delete auto-save file for current buffer if `delete-auto-save-files' is t. | |
1917 | Normally delete only if the file was written by this Emacs since | |
1918 | the last real save, but optional arg FORCE non-nil means delete anyway." | |
1919 | (and buffer-auto-save-file-name delete-auto-save-files | |
1920 | (not (string= buffer-file-name buffer-auto-save-file-name)) | |
1921 | (or force (recent-auto-save-p)) | |
1922 | (progn | |
1923 | (condition-case () | |
1924 | (delete-file buffer-auto-save-file-name) | |
1925 | (file-error nil)) | |
1926 | (set-buffer-auto-saved)))) | |
1927 | ||
1cc852cc RS |
1928 | (defvar after-save-hook nil |
1929 | "Normal hook that is run after a buffer is saved to its file.") | |
1930 | ||
b4da00e9 | 1931 | (defun basic-save-buffer () |
1cc852cc RS |
1932 | "Save the current buffer in its visited file, if it has been modified. |
1933 | After saving the buffer, run `after-save-hook'." | |
b4da00e9 | 1934 | (interactive) |
c11a94fe RS |
1935 | (save-excursion |
1936 | ;; In an indirect buffer, save its base buffer instead. | |
1937 | (if (buffer-base-buffer) | |
1938 | (set-buffer (buffer-base-buffer))) | |
1939 | (if (buffer-modified-p) | |
1940 | (let ((recent-save (recent-auto-save-p)) | |
1941 | setmodes tempsetmodes) | |
1942 | ;; On VMS, rename file and buffer to get rid of version number. | |
1943 | (if (and (eq system-type 'vax-vms) | |
1944 | (not (string= buffer-file-name | |
1945 | (file-name-sans-versions buffer-file-name)))) | |
1946 | (let (buffer-new-name) | |
1947 | ;; Strip VMS version number before save. | |
1948 | (setq buffer-file-name | |
1949 | (file-name-sans-versions buffer-file-name)) | |
1950 | ;; Construct a (unique) buffer name to correspond. | |
1951 | (let ((buf (create-file-buffer (downcase buffer-file-name)))) | |
1952 | (setq buffer-new-name (buffer-name buf)) | |
1953 | (kill-buffer buf)) | |
1954 | (rename-buffer buffer-new-name))) | |
1955 | ;; If buffer has no file name, ask user for one. | |
1956 | (or buffer-file-name | |
182891ef RS |
1957 | (let ((filename |
1958 | (expand-file-name | |
1959 | (read-file-name "File to save in: ") nil))) | |
1960 | (and (file-exists-p filename) | |
1961 | (or (y-or-n-p (format "File `%s' exists; overwrite? " | |
1962 | filename)) | |
1963 | (error "Canceled"))) | |
1964 | (set-visited-file-name filename))) | |
c11a94fe RS |
1965 | (or (verify-visited-file-modtime (current-buffer)) |
1966 | (not (file-exists-p buffer-file-name)) | |
1967 | (yes-or-no-p | |
1968 | (format "%s has changed since visited or saved. Save anyway? " | |
1969 | (file-name-nondirectory buffer-file-name))) | |
1970 | (error "Save not confirmed")) | |
1971 | (save-restriction | |
1972 | (widen) | |
1973 | (and (> (point-max) 1) | |
1974 | (/= (char-after (1- (point-max))) ?\n) | |
1975 | (not (and (eq selective-display t) | |
1976 | (= (char-after (1- (point-max))) ?\r))) | |
1977 | (or (eq require-final-newline t) | |
1978 | (and require-final-newline | |
1979 | (y-or-n-p | |
1980 | (format "Buffer %s does not end in newline. Add one? " | |
1981 | (buffer-name))))) | |
1982 | (save-excursion | |
1983 | (goto-char (point-max)) | |
1984 | (insert ?\n))) | |
1985 | (or (run-hook-with-args-until-success 'write-contents-hooks) | |
1986 | (run-hook-with-args-until-success 'local-write-file-hooks) | |
1987 | (run-hook-with-args-until-success 'write-file-hooks) | |
1988 | ;; If a hook returned t, file is already "written". | |
1989 | ;; Otherwise, write it the usual way now. | |
1990 | (setq setmodes (basic-save-buffer-1))) | |
2a47b4f5 RS |
1991 | (setq buffer-file-number |
1992 | (nthcdr 10 (file-attributes buffer-file-name))) | |
c11a94fe RS |
1993 | (if setmodes |
1994 | (condition-case () | |
1995 | (set-file-modes buffer-file-name setmodes) | |
1996 | (error nil)))) | |
1997 | ;; If the auto-save file was recent before this command, | |
1998 | ;; delete it now. | |
1999 | (delete-auto-save-file-if-necessary recent-save) | |
49530862 RS |
2000 | ;; Support VC `implicit' locking. |
2001 | (vc-after-save) | |
c11a94fe RS |
2002 | (run-hooks 'after-save-hook)) |
2003 | (message "(No changes need to be saved)")))) | |
b4da00e9 | 2004 | |
87d26afc RS |
2005 | ;; This does the "real job" of writing a buffer into its visited file |
2006 | ;; and making a backup file. This is what is normally done | |
2007 | ;; but inhibited if one of write-file-hooks returns non-nil. | |
2008 | ;; It returns a value to store in setmodes. | |
2009 | (defun basic-save-buffer-1 () | |
2010 | (let (tempsetmodes setmodes) | |
2011 | (if (not (file-writable-p buffer-file-name)) | |
2012 | (let ((dir (file-name-directory buffer-file-name))) | |
2013 | (if (not (file-directory-p dir)) | |
2014 | (error "%s is not a directory" dir) | |
2015 | (if (not (file-exists-p buffer-file-name)) | |
2016 | (error "Directory %s write-protected" dir) | |
2017 | (if (yes-or-no-p | |
2018 | (format "File %s is write-protected; try to save anyway? " | |
2019 | (file-name-nondirectory | |
2020 | buffer-file-name))) | |
2021 | (setq tempsetmodes t) | |
2022 | (error "Attempt to save to a file which you aren't allowed to write")))))) | |
2023 | (or buffer-backed-up | |
2024 | (setq setmodes (backup-buffer))) | |
76d5492b | 2025 | (let ((dir (file-name-directory buffer-file-name))) |
f4a0f59b RS |
2026 | (if (and file-precious-flag |
2027 | (file-writable-p dir)) | |
2028 | ;; If file is precious, write temp name, then rename it. | |
2029 | ;; This requires write access to the containing dir, | |
2030 | ;; which is why we don't try it if we don't have that access. | |
2031 | (let ((realname buffer-file-name) | |
6782610c RS |
2032 | tempname temp nogood i succeed |
2033 | (old-modtime (visited-file-modtime))) | |
f4a0f59b RS |
2034 | (setq i 0) |
2035 | (setq nogood t) | |
2036 | ;; Find the temporary name to write under. | |
2037 | (while nogood | |
567c1ca9 | 2038 | (setq tempname (format |
28ee503c KH |
2039 | (if (and (eq system-type 'ms-dos) |
2040 | (not (msdos-long-file-names))) | |
567c1ca9 RS |
2041 | "%s#%d.tm#" ; MSDOS limits files to 8+3 |
2042 | "%s#tmp#%d") | |
2043 | dir i)) | |
f4a0f59b RS |
2044 | (setq nogood (file-exists-p tempname)) |
2045 | (setq i (1+ i))) | |
2046 | (unwind-protect | |
2047 | (progn (clear-visited-file-modtime) | |
2048 | (write-region (point-min) (point-max) | |
89095962 RS |
2049 | tempname nil realname |
2050 | buffer-file-truename) | |
f4a0f59b RS |
2051 | (setq succeed t)) |
2052 | ;; If writing the temp file fails, | |
2053 | ;; delete the temp file. | |
76d5492b | 2054 | (or succeed |
6782610c RS |
2055 | (progn |
2056 | (delete-file tempname) | |
2057 | (set-visited-file-modtime old-modtime)))) | |
f4a0f59b RS |
2058 | ;; Since we have created an entirely new file |
2059 | ;; and renamed it, make sure it gets the | |
2060 | ;; right permission bits set. | |
2061 | (setq setmodes (file-modes buffer-file-name)) | |
2062 | ;; We succeeded in writing the temp file, | |
2063 | ;; so rename it. | |
2064 | (rename-file tempname buffer-file-name t)) | |
2065 | ;; If file not writable, see if we can make it writable | |
2066 | ;; temporarily while we write it. | |
2067 | ;; But no need to do so if we have just backed it up | |
2068 | ;; (setmodes is set) because that says we're superseding. | |
2069 | (cond ((and tempsetmodes (not setmodes)) | |
2070 | ;; Change the mode back, after writing. | |
2071 | (setq setmodes (file-modes buffer-file-name)) | |
2072 | (set-file-modes buffer-file-name 511))) | |
2073 | (write-region (point-min) (point-max) | |
89095962 | 2074 | buffer-file-name nil t buffer-file-truename))) |
87d26afc RS |
2075 | setmodes)) |
2076 | ||
b4da00e9 RM |
2077 | (defun save-some-buffers (&optional arg exiting) |
2078 | "Save some modified file-visiting buffers. Asks user about each one. | |
5bbbceb1 JB |
2079 | Optional argument (the prefix) non-nil means save all with no questions. |
2080 | Optional second argument EXITING means ask about certain non-file buffers | |
2081 | as well as about file buffers." | |
b4da00e9 | 2082 | (interactive "P") |
907482b9 | 2083 | (save-window-excursion |
76d5492b RM |
2084 | (let* ((queried nil) |
2085 | (files-done | |
2086 | (map-y-or-n-p | |
2087 | (function | |
2088 | (lambda (buffer) | |
2089 | (and (buffer-modified-p buffer) | |
2090 | (not (buffer-base-buffer buffer)) | |
2091 | (or | |
2092 | (buffer-file-name buffer) | |
2093 | (and exiting | |
2094 | (progn | |
2095 | (set-buffer buffer) | |
2096 | (and buffer-offer-save (> (buffer-size) 0))))) | |
2097 | (if arg | |
2098 | t | |
2099 | (setq queried t) | |
2100 | (if (buffer-file-name buffer) | |
2101 | (format "Save file %s? " | |
2102 | (buffer-file-name buffer)) | |
2103 | (format "Save buffer %s? " | |
2104 | (buffer-name buffer))))))) | |
2105 | (function | |
2106 | (lambda (buffer) | |
2107 | (set-buffer buffer) | |
2108 | (save-buffer))) | |
2109 | (buffer-list) | |
2110 | '("buffer" "buffers" "save") | |
2111 | (list (list ?\C-r (lambda (buf) | |
2112 | (view-buffer buf) | |
2113 | (setq view-exit-action | |
2114 | '(lambda (ignore) | |
2115 | (exit-recursive-edit))) | |
2116 | (recursive-edit) | |
2117 | ;; Return nil to ask about BUF again. | |
2118 | nil) | |
2119 | "display the current buffer")))) | |
2120 | (abbrevs-done | |
2121 | (and save-abbrevs abbrevs-changed | |
2122 | (progn | |
2123 | (if (or arg | |
2124 | (y-or-n-p (format "Save abbrevs in %s? " | |
2125 | abbrev-file-name))) | |
2126 | (write-abbrev-file nil)) | |
2127 | ;; Don't keep bothering user if he says no. | |
2128 | (setq abbrevs-changed nil) | |
2129 | t)))) | |
2130 | (or queried (> files-done 0) abbrevs-done | |
5625c2fc | 2131 | (message "(No files need saving)"))))) |
b4da00e9 RM |
2132 | \f |
2133 | (defun not-modified (&optional arg) | |
2134 | "Mark current buffer as unmodified, not needing to be saved. | |
a641f9a1 RM |
2135 | With prefix arg, mark buffer as modified, so \\[save-buffer] will save. |
2136 | ||
2137 | It is not a good idea to use this function in Lisp programs, because it | |
2138 | prints a message in the minibuffer. Instead, use `set-buffer-modified-p'." | |
b4da00e9 RM |
2139 | (interactive "P") |
2140 | (message (if arg "Modification-flag set" | |
2141 | "Modification-flag cleared")) | |
2142 | (set-buffer-modified-p arg)) | |
2143 | ||
2144 | (defun toggle-read-only (&optional arg) | |
2145 | "Change whether this buffer is visiting its file read-only. | |
2146 | With arg, set read-only iff arg is positive." | |
2147 | (interactive "P") | |
2148 | (setq buffer-read-only | |
2149 | (if (null arg) | |
2150 | (not buffer-read-only) | |
2151 | (> (prefix-numeric-value arg) 0))) | |
3941fe2c | 2152 | (force-mode-line-update)) |
b4da00e9 | 2153 | |
82d0954a | 2154 | (defun insert-file (filename &optional coding-system) |
b4da00e9 RM |
2155 | "Insert contents of file FILENAME into buffer after point. |
2156 | Set mark after the inserted text. | |
82d0954a | 2157 | A prefix argument enables user to specify the coding-system interactively. |
b4da00e9 RM |
2158 | |
2159 | This function is meant for the user to run interactively. | |
2160 | Don't call it from programs! Use `insert-file-contents' instead. | |
2161 | \(Its calling sequence is different; see its documentation)." | |
82d0954a | 2162 | (interactive "*fInsert file: \nZCoding-system: ") |
3be6243a RS |
2163 | (if (file-directory-p filename) |
2164 | (signal 'file-error (list "Opening input file" "file is a directory" | |
2165 | filename))) | |
82d0954a KH |
2166 | (let ((tem |
2167 | (if coding-system | |
2168 | (let ((coding-system-for-read coding-system)) | |
2169 | (insert-file-contents filename)) | |
2170 | (insert-file-contents filename)))) | |
b4da00e9 RM |
2171 | (push-mark (+ (point) (car (cdr tem)))))) |
2172 | ||
82d0954a | 2173 | (defun append-to-file (start end filename &optional coding-system) |
b4da00e9 RM |
2174 | "Append the contents of the region to the end of file FILENAME. |
2175 | When called from a function, expects three arguments, | |
2176 | START, END and FILENAME. START and END are buffer positions | |
82d0954a KH |
2177 | saying what text to write. |
2178 | A prefix argument enables user to specify the coding-system interactively." | |
2179 | (interactive "r\nFAppend to file: \nZCoding-system: ") | |
2180 | (if coding-system | |
2181 | (let ((coding-system-for-write coding-system)) | |
2182 | (write-region start end filename t)) | |
2183 | (write-region start end filename t))) | |
b4da00e9 RM |
2184 | |
2185 | (defun file-newest-backup (filename) | |
2186 | "Return most recent backup file for FILENAME or nil if no backups exist." | |
2187 | (let* ((filename (expand-file-name filename)) | |
2188 | (file (file-name-nondirectory filename)) | |
2189 | (dir (file-name-directory filename)) | |
2190 | (comp (file-name-all-completions file dir)) | |
cf7e94a0 RS |
2191 | (newest nil) |
2192 | tem) | |
b4da00e9 | 2193 | (while comp |
cf7e94a0 | 2194 | (setq tem (car comp) |
b4da00e9 | 2195 | comp (cdr comp)) |
cf7e94a0 RS |
2196 | (cond ((and (backup-file-name-p tem) |
2197 | (string= (file-name-sans-versions tem) file)) | |
2198 | (setq tem (concat dir tem)) | |
2199 | (if (or (null newest) | |
2200 | (file-newer-than-file-p tem newest)) | |
2201 | (setq newest tem))))) | |
b4da00e9 RM |
2202 | newest)) |
2203 | ||
2204 | (defun rename-uniquely () | |
2205 | "Rename current buffer to a similar name not already taken. | |
2206 | This function is useful for creating multiple shell process buffers | |
2207 | or multiple mail buffers, etc." | |
2208 | (interactive) | |
40eb8038 | 2209 | (save-match-data |
ed34f320 RS |
2210 | (let* ((base-name (if (and (string-match "<[0-9]+>\\'" (buffer-name)) |
2211 | (not (and buffer-file-name | |
2212 | (string= (buffer-name) | |
2213 | (file-name-nondirectory | |
2214 | buffer-file-name))))) | |
2215 | ;; If the existing buffer name has a <NNN>, | |
2216 | ;; which isn't part of the file name (if any), | |
2217 | ;; then get rid of that. | |
40eb8038 RS |
2218 | (substring (buffer-name) 0 (match-beginning 0)) |
2219 | (buffer-name))) | |
2220 | (new-buf (generate-new-buffer base-name)) | |
2221 | (name (buffer-name new-buf))) | |
2222 | (kill-buffer new-buf) | |
2223 | (rename-buffer name) | |
3941fe2c | 2224 | (force-mode-line-update)))) |
5bbbceb1 | 2225 | |
4e43240a | 2226 | (defun make-directory (dir &optional parents) |
5ce8bb89 RS |
2227 | "Create the directory DIR and any nonexistent parent dirs. |
2228 | Interactively, the default choice of directory to create | |
2229 | is the current default directory for file names. | |
0225ae5e | 2230 | That is useful when you have visited a file in a nonexistent directory. |
5ce8bb89 RS |
2231 | |
2232 | Noninteractively, the second (optional) argument PARENTS says whether | |
2233 | to create parent directories if they don't exist." | |
2234 | (interactive | |
2235 | (list (read-file-name "Make directory: " default-directory default-directory | |
2236 | nil nil) | |
2237 | t)) | |
6eaebaa2 | 2238 | (let ((handler (find-file-name-handler dir 'make-directory))) |
4e43240a RS |
2239 | (if handler |
2240 | (funcall handler 'make-directory dir parents) | |
2241 | (if (not parents) | |
2242 | (make-directory-internal dir) | |
2243 | (let ((dir (directory-file-name (expand-file-name dir))) | |
2244 | create-list) | |
2245 | (while (not (file-exists-p dir)) | |
76d5492b | 2246 | (setq create-list (cons dir create-list) |
4e43240a RS |
2247 | dir (directory-file-name (file-name-directory dir)))) |
2248 | (while create-list | |
2249 | (make-directory-internal (car create-list)) | |
2250 | (setq create-list (cdr create-list)))))))) | |
b4da00e9 RM |
2251 | \f |
2252 | (put 'revert-buffer-function 'permanent-local t) | |
2253 | (defvar revert-buffer-function nil | |
0973d78b RS |
2254 | "Function to use to revert this buffer, or nil to do the default. |
2255 | The function receives two arguments IGNORE-AUTO and NOCONFIRM, | |
2256 | which are the arguments that `revert-buffer' received.") | |
b4da00e9 RM |
2257 | |
2258 | (put 'revert-buffer-insert-file-contents-function 'permanent-local t) | |
2259 | (defvar revert-buffer-insert-file-contents-function nil | |
2260 | "Function to use to insert contents when reverting this buffer. | |
2261 | Gets two args, first the nominal file name to use, | |
2262 | and second, t if reading the auto-save file.") | |
2263 | ||
5f76e7d4 KH |
2264 | (defvar before-revert-hook nil |
2265 | "Normal hook for `revert-buffer' to run before reverting. | |
2266 | If `revert-buffer-function' is used to override the normal revert | |
2267 | mechanism, this hook is not used.") | |
2268 | ||
2269 | (defvar after-revert-hook nil | |
2270 | "Normal hook for `revert-buffer' to run after reverting. | |
2271 | Note that the hook value that it runs is the value that was in effect | |
2272 | before reverting; that makes a difference if you have buffer-local | |
2273 | hook functions. | |
2274 | ||
2275 | If `revert-buffer-function' is used to override the normal revert | |
2276 | mechanism, this hook is not used.") | |
2277 | ||
9a30563f | 2278 | (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes) |
7e7c9c4e | 2279 | "Replace current buffer text with the text of the visited file on disk. |
b4da00e9 | 2280 | This undoes all changes since the file was visited or saved. |
8c0e7b73 JB |
2281 | With a prefix argument, offer to revert from latest auto-save file, if |
2282 | that is more recent than the visited file. | |
1ab31687 | 2283 | |
7e7c9c4e RS |
2284 | This command also works for special buffers that contain text which |
2285 | doesn't come from a file, but reflects some other data base instead: | |
2286 | for example, Dired buffers and buffer-list buffers. In these cases, | |
2287 | it reconstructs the buffer contents from the appropriate data base. | |
2288 | ||
65ee6096 | 2289 | When called from Lisp, the first argument is IGNORE-AUTO; only offer |
1ab31687 JB |
2290 | to revert from the auto-save file when this is nil. Note that the |
2291 | sense of this argument is the reverse of the prefix argument, for the | |
2292 | sake of backward compatibility. IGNORE-AUTO is optional, defaulting | |
2293 | to nil. | |
2294 | ||
2295 | Optional second argument NOCONFIRM means don't ask for confirmation at | |
2296 | all. | |
b4da00e9 | 2297 | |
5b2b26d5 RS |
2298 | Optional third argument PRESERVE-MODES non-nil means don't alter |
2299 | the files modes. Normally we reinitialize them using `normal-mode'. | |
2300 | ||
8c0e7b73 | 2301 | If the value of `revert-buffer-function' is non-nil, it is called to |
7e7c9c4e RS |
2302 | do all the work for this command. Otherwise, the hooks |
2303 | `before-revert-hook' and `after-revert-hook' are run at the beginning | |
2304 | and the end, and if `revert-buffer-insert-file-contents-function' is | |
2305 | non-nil, it is called instead of rereading visited file contents." | |
fb6208a6 | 2306 | |
1ab31687 JB |
2307 | ;; I admit it's odd to reverse the sense of the prefix argument, but |
2308 | ;; there is a lot of code out there which assumes that the first | |
2309 | ;; argument should be t to avoid consulting the auto-save file, and | |
2310 | ;; there's no straightforward way to encourage authors to notice a | |
2311 | ;; reversal of the argument sense. So I'm just changing the user | |
2312 | ;; interface, but leaving the programmatic interface the same. | |
e0867e99 | 2313 | (interactive (list (not current-prefix-arg))) |
b4da00e9 | 2314 | (if revert-buffer-function |
1ab31687 | 2315 | (funcall revert-buffer-function ignore-auto noconfirm) |
b4da00e9 | 2316 | (let* ((opoint (point)) |
1ab31687 JB |
2317 | (auto-save-p (and (not ignore-auto) |
2318 | (recent-auto-save-p) | |
b4da00e9 RM |
2319 | buffer-auto-save-file-name |
2320 | (file-readable-p buffer-auto-save-file-name) | |
2321 | (y-or-n-p | |
2322 | "Buffer has been auto-saved recently. Revert from auto-save file? "))) | |
2323 | (file-name (if auto-save-p | |
2324 | buffer-auto-save-file-name | |
2325 | buffer-file-name))) | |
2326 | (cond ((null file-name) | |
2327 | (error "Buffer does not seem to be associated with any file")) | |
2328 | ((or noconfirm | |
2329 | (yes-or-no-p (format "Revert buffer from file %s? " | |
2330 | file-name))) | |
4fbf62ee | 2331 | (run-hooks 'before-revert-hook) |
b4da00e9 RM |
2332 | ;; If file was backed up but has changed since, |
2333 | ;; we shd make another backup. | |
2334 | (and (not auto-save-p) | |
5bbbceb1 | 2335 | (not (verify-visited-file-modtime (current-buffer))) |
b4da00e9 RM |
2336 | (setq buffer-backed-up nil)) |
2337 | ;; Get rid of all undo records for this buffer. | |
2338 | (or (eq buffer-undo-list t) | |
2339 | (setq buffer-undo-list nil)) | |
ba5c8c93 KH |
2340 | ;; Effectively copy the after-revert-hook status, |
2341 | ;; since after-find-file will clobber it. | |
2342 | (let ((global-hook (default-value 'after-revert-hook)) | |
2343 | (local-hook-p (local-variable-p 'after-revert-hook)) | |
2344 | (local-hook (and (local-variable-p 'after-revert-hook) | |
2345 | after-revert-hook))) | |
2346 | (let (buffer-read-only | |
2347 | ;; Don't make undo records for the reversion. | |
2348 | (buffer-undo-list t)) | |
2349 | (if revert-buffer-insert-file-contents-function | |
2350 | (funcall revert-buffer-insert-file-contents-function | |
2351 | file-name auto-save-p) | |
2352 | (if (not (file-exists-p file-name)) | |
2353 | (error "File %s no longer exists!" file-name)) | |
2354 | ;; Bind buffer-file-name to nil | |
2355 | ;; so that we don't try to lock the file. | |
2356 | (let ((buffer-file-name nil)) | |
2357 | (or auto-save-p | |
2358 | (unlock-buffer))) | |
2359 | (widen) | |
2360 | (insert-file-contents file-name (not auto-save-p) | |
2361 | nil nil t))) | |
2362 | (goto-char (min opoint (point-max))) | |
2363 | ;; Recompute the truename in case changes in symlinks | |
2364 | ;; have changed the truename. | |
2365 | (setq buffer-file-truename | |
2366 | (abbreviate-file-name (file-truename buffer-file-name))) | |
9a30563f | 2367 | (after-find-file nil nil t t preserve-modes) |
ba5c8c93 KH |
2368 | ;; Run after-revert-hook as it was before we reverted. |
2369 | (setq-default revert-buffer-internal-hook global-hook) | |
2370 | (if local-hook-p | |
2371 | (progn | |
2372 | (make-local-variable 'revert-buffer-internal-hook) | |
2373 | (setq revert-buffer-internal-hook local-hook)) | |
2374 | (kill-local-variable 'revert-buffer-internal-hook)) | |
2375 | (run-hooks 'revert-buffer-internal-hook)) | |
4fbf62ee | 2376 | t))))) |
b4da00e9 RM |
2377 | |
2378 | (defun recover-file (file) | |
2379 | "Visit file FILE, but get contents from its last auto-save file." | |
10f7c7fc RS |
2380 | ;; Actually putting the file name in the minibuffer should be used |
2381 | ;; only rarely. | |
2382 | ;; Not just because users often use the default. | |
e1dadc17 | 2383 | (interactive "FRecover file: ") |
b4da00e9 | 2384 | (setq file (expand-file-name file)) |
f7da6740 RS |
2385 | (if (auto-save-file-name-p (file-name-nondirectory file)) |
2386 | (error "%s is an auto-save file" file)) | |
b4da00e9 RM |
2387 | (let ((file-name (let ((buffer-file-name file)) |
2388 | (make-auto-save-file-name)))) | |
945e1965 RS |
2389 | (cond ((if (file-exists-p file) |
2390 | (not (file-newer-than-file-p file-name file)) | |
2391 | (not (file-exists-p file-name))) | |
b4da00e9 RM |
2392 | (error "Auto-save file %s not current" file-name)) |
2393 | ((save-window-excursion | |
2394 | (if (not (eq system-type 'vax-vms)) | |
2395 | (with-output-to-temp-buffer "*Directory*" | |
2396 | (buffer-disable-undo standard-output) | |
2397 | (call-process "ls" nil standard-output nil | |
2dc2b736 RS |
2398 | (if (file-symlink-p file) "-lL" "-l") |
2399 | file file-name))) | |
b4da00e9 RM |
2400 | (yes-or-no-p (format "Recover auto save file %s? " file-name))) |
2401 | (switch-to-buffer (find-file-noselect file t)) | |
82d0954a KH |
2402 | (let ((buffer-read-only nil) |
2403 | ;; Auto-saved file shoule be read without any code conversion. | |
2404 | (coding-system-for-read 'no-conversion)) | |
b4da00e9 RM |
2405 | (erase-buffer) |
2406 | (insert-file-contents file-name nil)) | |
8cfb9d46 | 2407 | (after-find-file nil nil t)) |
b4da00e9 RM |
2408 | (t (error "Recover-file cancelled."))))) |
2409 | ||
6598027d | 2410 | (defun recover-session () |
9aee5392 RS |
2411 | "Recover auto save files from a previous Emacs session. |
2412 | This command first displays a Dired buffer showing you the | |
2413 | previous sessions that you could recover from. | |
2414 | To choose one, move point to the proper line and then type C-c C-c. | |
2415 | Then you'll be asked about a number of files to recover." | |
2416 | (interactive) | |
363a5030 RS |
2417 | (if (null auto-save-list-file-prefix) |
2418 | (error "You set `auto-save-list-file-prefix' to disable making session files")) | |
6f4983e6 | 2419 | (let ((ls-lisp-support-shell-wildcards t)) |
7b3478a5 RS |
2420 | (dired (concat auto-save-list-file-prefix "*") |
2421 | (concat dired-listing-switches "t"))) | |
9aee5392 RS |
2422 | (goto-char (point-min)) |
2423 | (or (looking-at "Move to the session you want to recover,") | |
2424 | (let ((inhibit-read-only t)) | |
033ef863 RS |
2425 | (insert "Move to the session you want to recover,\n" |
2426 | "then type C-c C-c to select it.\n\n" | |
2427 | "You can also delete some of these files;\n" | |
2428 | "type d on a line to mark that file for deletion.\n\n"))) | |
9aee5392 | 2429 | (use-local-map (nconc (make-sparse-keymap) (current-local-map))) |
80280bb7 | 2430 | (define-key (current-local-map) "\C-c\C-c" 'recover-session-finish)) |
9aee5392 | 2431 | |
80280bb7 | 2432 | (defun recover-session-finish () |
9aee5392 RS |
2433 | "Choose one saved session to recover auto-save files from. |
2434 | This command is used in the special Dired buffer created by | |
80280bb7 | 2435 | \\[recover-session]." |
9aee5392 RS |
2436 | (interactive) |
2437 | ;; Get the name of the session file to recover from. | |
2438 | (let ((file (dired-get-filename)) | |
953a03b2 | 2439 | files |
9aee5392 | 2440 | (buffer (get-buffer-create " *recover*"))) |
033ef863 | 2441 | (dired-do-flagged-delete t) |
9aee5392 RS |
2442 | (unwind-protect |
2443 | (save-excursion | |
2444 | ;; Read in the auto-save-list file. | |
2445 | (set-buffer buffer) | |
2446 | (erase-buffer) | |
2447 | (insert-file-contents file) | |
953a03b2 RS |
2448 | ;; Loop thru the text of that file |
2449 | ;; and get out the names of the files to recover. | |
2450 | (while (not (eobp)) | |
2451 | (let (thisfile autofile) | |
2452 | (if (eolp) | |
2453 | ;; This is a pair of lines for a non-file-visiting buffer. | |
2454 | ;; Get the auto-save file name and manufacture | |
2455 | ;; a "visited file name" from that. | |
2456 | (progn | |
2457 | (forward-line 1) | |
2458 | (setq autofile | |
2459 | (buffer-substring-no-properties | |
2460 | (point) | |
2461 | (save-excursion | |
2462 | (end-of-line) | |
2463 | (point)))) | |
2464 | (setq thisfile | |
2465 | (expand-file-name | |
2466 | (substring | |
2467 | (file-name-nondirectory autofile) | |
2468 | 1 -1) | |
2469 | (file-name-directory autofile))) | |
2470 | (forward-line 1)) | |
2471 | ;; This pair of lines is a file-visiting | |
2472 | ;; buffer. Use the visited file name. | |
2473 | (progn | |
2474 | (setq thisfile | |
2475 | (buffer-substring-no-properties | |
2476 | (point) (progn (end-of-line) (point)))) | |
2477 | (forward-line 1) | |
2478 | (setq autofile | |
2479 | (buffer-substring-no-properties | |
2480 | (point) (progn (end-of-line) (point)))) | |
2481 | (forward-line 1))) | |
2482 | ;; Ignore a file if its auto-save file does not exist now. | |
2483 | (if (file-exists-p autofile) | |
2484 | (setq files (cons thisfile files))))) | |
2485 | (setq files (nreverse files)) | |
945e1965 RS |
2486 | ;; The file contains a pair of line for each auto-saved buffer. |
2487 | ;; The first line of the pair contains the visited file name | |
2488 | ;; or is empty if the buffer was not visiting a file. | |
2489 | ;; The second line is the auto-save file name. | |
953a03b2 RS |
2490 | (if files |
2491 | (map-y-or-n-p "Recover %s? " | |
2492 | (lambda (file) | |
2493 | (condition-case nil | |
2494 | (save-excursion (recover-file file)) | |
76d5492b | 2495 | (error |
953a03b2 RS |
2496 | "Failed to recover `%s'" file))) |
2497 | files | |
2498 | '("file" "files" "recover")) | |
2499 | (message "No files can be recovered from this session now"))) | |
9aee5392 RS |
2500 | (kill-buffer buffer)))) |
2501 | ||
73ba610a RS |
2502 | (defun kill-some-buffers (&optional list) |
2503 | "For each buffer in LIST, ask whether to kill it. | |
2504 | LIST defaults to all existing live buffers." | |
b4da00e9 | 2505 | (interactive) |
73ba610a RS |
2506 | (if (null list) |
2507 | (setq list (buffer-list))) | |
2508 | (while list | |
2509 | (let* ((buffer (car list)) | |
2510 | (name (buffer-name buffer))) | |
2511 | (and (not (string-equal name "")) | |
2512 | (/= (aref name 0) ? ) | |
2513 | (yes-or-no-p | |
2514 | (format "Buffer %s %s. Kill? " | |
2515 | name | |
2516 | (if (buffer-modified-p buffer) | |
2517 | "HAS BEEN EDITED" "is unmodified"))) | |
2518 | (kill-buffer buffer))) | |
2519 | (setq list (cdr list)))) | |
b4da00e9 RM |
2520 | \f |
2521 | (defun auto-save-mode (arg) | |
2522 | "Toggle auto-saving of contents of current buffer. | |
f3e23606 | 2523 | With prefix argument ARG, turn auto-saving on if positive, else off." |
b4da00e9 RM |
2524 | (interactive "P") |
2525 | (setq buffer-auto-save-file-name | |
2526 | (and (if (null arg) | |
74b9c2af RS |
2527 | (or (not buffer-auto-save-file-name) |
2528 | ;; If autosave is off because buffer has shrunk, | |
2529 | ;; then toggling should turn it on. | |
2530 | (< buffer-saved-size 0)) | |
b4da00e9 RM |
2531 | (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))) |
2532 | (if (and buffer-file-name auto-save-visited-file-name | |
2533 | (not buffer-read-only)) | |
2534 | buffer-file-name | |
2535 | (make-auto-save-file-name)))) | |
0b7f1ef2 RS |
2536 | ;; If -1 was stored here, to temporarily turn off saving, |
2537 | ;; turn it back on. | |
2538 | (and (< buffer-saved-size 0) | |
2539 | (setq buffer-saved-size 0)) | |
b4da00e9 RM |
2540 | (if (interactive-p) |
2541 | (message "Auto-save %s (in this buffer)" | |
2542 | (if buffer-auto-save-file-name "on" "off"))) | |
2543 | buffer-auto-save-file-name) | |
2544 | ||
2545 | (defun rename-auto-save-file () | |
2546 | "Adjust current buffer's auto save file name for current conditions. | |
2547 | Also rename any existing auto save file, if it was made in this session." | |
2548 | (let ((osave buffer-auto-save-file-name)) | |
2549 | (setq buffer-auto-save-file-name | |
2550 | (make-auto-save-file-name)) | |
2551 | (if (and osave buffer-auto-save-file-name | |
2552 | (not (string= buffer-auto-save-file-name buffer-file-name)) | |
2553 | (not (string= buffer-auto-save-file-name osave)) | |
2554 | (file-exists-p osave) | |
2555 | (recent-auto-save-p)) | |
2556 | (rename-file osave buffer-auto-save-file-name t)))) | |
2557 | ||
2558 | (defun make-auto-save-file-name () | |
2559 | "Return file name to use for auto-saves of current buffer. | |
2560 | Does not consider `auto-save-visited-file-name' as that variable is checked | |
2561 | before calling this function. You can redefine this for customization. | |
2562 | See also `auto-save-file-name-p'." | |
2563 | (if buffer-file-name | |
28ee503c KH |
2564 | (if (and (eq system-type 'ms-dos) |
2565 | (not (msdos-long-file-names))) | |
4503dacb RS |
2566 | (let ((fn (file-name-nondirectory buffer-file-name))) |
2567 | (string-match "\\`\\([^.]+\\)\\(\\.\\(..?\\)?.?\\|\\)\\'" fn) | |
2568 | (concat (file-name-directory buffer-file-name) | |
2569 | "#" (match-string 1 fn) | |
2570 | "." (match-string 3 fn) "#")) | |
2571 | (concat (file-name-directory buffer-file-name) | |
2572 | "#" | |
2573 | (file-name-nondirectory buffer-file-name) | |
2574 | "#")) | |
0a1763b4 RS |
2575 | |
2576 | ;; Deal with buffers that don't have any associated files. (Mail | |
2577 | ;; mode tends to create a good number of these.) | |
2578 | ||
7d483e8c | 2579 | (let ((buffer-name (buffer-name)) |
0a1763b4 | 2580 | (limit 0)) |
c3348e10 RS |
2581 | ;; Eliminate all slashes and backslashes by |
2582 | ;; replacing them with sequences that start with %. | |
2583 | ;; Quote % also, to keep distinct names distinct. | |
2584 | (while (string-match "[/\\%]" buffer-name limit) | |
2585 | (let* ((character (aref buffer-name (match-beginning 0))) | |
2586 | (replacement | |
2587 | (cond ((eq character ?%) "%%") | |
2588 | ((eq character ?/) "%+") | |
2589 | ((eq character ?\\) "%-")))) | |
2590 | (setq buffer-name (replace-match replacement t t buffer-name)) | |
2591 | (setq limit (1+ (match-end 0))))) | |
a8abaf83 RS |
2592 | ;; Generate the file name. |
2593 | (expand-file-name | |
2594 | (format "#%s#%s#" buffer-name (make-temp-name "")) | |
2595 | ;; Try a few alternative directories, to get one we can write it. | |
2596 | (cond | |
2597 | ((file-writable-p default-directory) default-directory) | |
2598 | ((file-writable-p "/var/tmp/") "/var/tmp/") | |
2599 | ("~/")))))) | |
b4da00e9 RM |
2600 | |
2601 | (defun auto-save-file-name-p (filename) | |
2602 | "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'. | |
2603 | FILENAME should lack slashes. You can redefine this for customization." | |
2604 | (string-match "^#.*#$" filename)) | |
2605 | \f | |
6f4983e6 RS |
2606 | (defun wildcard-to-regexp (wildcard) |
2607 | "Given a shell file name pattern WILDCARD, return an equivalent regexp. | |
2608 | The generated regexp will match a filename iff the filename | |
2609 | matches that wildcard according to shell rules. Only wildcards known | |
2610 | by `sh' are supported." | |
2611 | (let* ((i (string-match "[[.*+\\^$?]" wildcard)) | |
2612 | ;; Copy the initial run of non-special characters. | |
2613 | (result (substring wildcard 0 i)) | |
2614 | (len (length wildcard))) | |
2615 | ;; If no special characters, we're almost done. | |
2616 | (if i | |
2617 | (while (< i len) | |
2618 | (let ((ch (aref wildcard i)) | |
2619 | j) | |
2620 | (setq | |
2621 | result | |
2622 | (concat result | |
2623 | (cond | |
7e7c9c4e RS |
2624 | ((and (eq ch ?\[) |
2625 | (< (1+ i) len) | |
2626 | (eq (aref wildcard (1+ i)) ?\])) | |
2627 | "\\[") | |
6f4983e6 RS |
2628 | ((eq ch ?\[) ; [...] maps to regexp char class |
2629 | (progn | |
2630 | (setq i (1+ i)) | |
2631 | (concat | |
2632 | (cond | |
2633 | ((eq (aref wildcard i) ?!) ; [!...] -> [^...] | |
2634 | (progn | |
2635 | (setq i (1+ i)) | |
2636 | (if (eq (aref wildcard i) ?\]) | |
2637 | (progn | |
2638 | (setq i (1+ i)) | |
2639 | "[^]") | |
2640 | "[^"))) | |
2641 | ((eq (aref wildcard i) ?^) | |
2642 | ;; Found "[^". Insert a `\0' character | |
2643 | ;; (which cannot happen in a filename) | |
2644 | ;; into the character class, so that `^' | |
2645 | ;; is not the first character after `[', | |
2646 | ;; and thus non-special in a regexp. | |
2647 | (progn | |
2648 | (setq i (1+ i)) | |
2649 | "[\000^")) | |
2650 | ((eq (aref wildcard i) ?\]) | |
2651 | ;; I don't think `]' can appear in a | |
2652 | ;; character class in a wildcard, but | |
2653 | ;; let's be general here. | |
2654 | (progn | |
2655 | (setq i (1+ i)) | |
2656 | "[]")) | |
2657 | (t "[")) | |
2658 | (prog1 ; copy everything upto next `]'. | |
2659 | (substring wildcard | |
2660 | i | |
2661 | (setq j (string-match | |
2662 | "]" wildcard i))) | |
2663 | (setq i (if j (1- j) (1- len))))))) | |
2664 | ((eq ch ?.) "\\.") | |
2665 | ((eq ch ?*) "[^\000]*") | |
2666 | ((eq ch ?+) "\\+") | |
2667 | ((eq ch ?^) "\\^") | |
2668 | ((eq ch ?$) "\\$") | |
2669 | ((eq ch ?\\) "\\\\") ; probably cannot happen... | |
2670 | ((eq ch ??) "[^\000]") | |
2671 | (t (char-to-string ch))))) | |
2672 | (setq i (1+ i))))) | |
2673 | ;; Shell wildcards should match the entire filename, | |
2674 | ;; not its part. Make the regexp say so. | |
2675 | (concat "\\`" result "\\'"))) | |
2676 | \f | |
d7fa5aa2 | 2677 | (defvar list-directory-brief-switches |
b4da00e9 RM |
2678 | (if (eq system-type 'vax-vms) "" "-CF") |
2679 | "*Switches for list-directory to pass to `ls' for brief listing,") | |
2680 | ||
d7fa5aa2 | 2681 | (defvar list-directory-verbose-switches |
b4da00e9 RM |
2682 | (if (eq system-type 'vax-vms) |
2683 | "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)" | |
2684 | "-l") | |
2685 | "*Switches for list-directory to pass to `ls' for verbose listing,") | |
2686 | ||
2687 | (defun list-directory (dirname &optional verbose) | |
2688 | "Display a list of files in or matching DIRNAME, a la `ls'. | |
2689 | DIRNAME is globbed by the shell if necessary. | |
2690 | Prefix arg (second arg if noninteractive) means supply -l switch to `ls'. | |
2691 | Actions controlled by variables `list-directory-brief-switches' | |
2692 | and `list-directory-verbose-switches'." | |
2693 | (interactive (let ((pfx current-prefix-arg)) | |
2694 | (list (read-file-name (if pfx "List directory (verbose): " | |
2695 | "List directory (brief): ") | |
2696 | nil default-directory nil) | |
2697 | pfx))) | |
2698 | (let ((switches (if verbose list-directory-verbose-switches | |
2699 | list-directory-brief-switches))) | |
2700 | (or dirname (setq dirname default-directory)) | |
2701 | (setq dirname (expand-file-name dirname)) | |
2702 | (with-output-to-temp-buffer "*Directory*" | |
2703 | (buffer-disable-undo standard-output) | |
2704 | (princ "Directory ") | |
2705 | (princ dirname) | |
2706 | (terpri) | |
c3554e95 RS |
2707 | (save-excursion |
2708 | (set-buffer "*Directory*") | |
5a8a160e RS |
2709 | (setq default-directory |
2710 | (if (file-directory-p dirname) | |
2711 | (file-name-as-directory dirname) | |
2712 | (file-name-directory dirname))) | |
c3554e95 RS |
2713 | (let ((wildcard (not (file-directory-p dirname)))) |
2714 | (insert-directory dirname switches wildcard (not wildcard))))))) | |
2715 | ||
2716 | (defvar insert-directory-program "ls" | |
2717 | "Absolute or relative name of the `ls' program used by `insert-directory'.") | |
2718 | ||
2719 | ;; insert-directory | |
2720 | ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and | |
2721 | ;; FULL-DIRECTORY-P is nil. | |
2722 | ;; The single line of output must display FILE's name as it was | |
2723 | ;; given, namely, an absolute path name. | |
2724 | ;; - must insert exactly one line for each file if WILDCARD or | |
2725 | ;; FULL-DIRECTORY-P is t, plus one optional "total" line | |
2726 | ;; before the file lines, plus optional text after the file lines. | |
2727 | ;; Lines are delimited by "\n", so filenames containing "\n" are not | |
2728 | ;; allowed. | |
2729 | ;; File lines should display the basename. | |
2730 | ;; - must be consistent with | |
2731 | ;; - functions dired-move-to-filename, (these two define what a file line is) | |
2732 | ;; dired-move-to-end-of-filename, | |
2733 | ;; dired-between-files, (shortcut for (not (dired-move-to-filename))) | |
2734 | ;; dired-insert-headerline | |
2735 | ;; dired-after-subdir-garbage (defines what a "total" line is) | |
2736 | ;; - variable dired-subdir-regexp | |
2737 | (defun insert-directory (file switches &optional wildcard full-directory-p) | |
a18b7c81 | 2738 | "Insert directory listing for FILE, formatted according to SWITCHES. |
c3554e95 | 2739 | Leaves point after the inserted text. |
727d277d | 2740 | SWITCHES may be a string of options, or a list of strings. |
c3554e95 RS |
2741 | Optional third arg WILDCARD means treat FILE as shell wildcard. |
2742 | Optional fourth arg FULL-DIRECTORY-P means file is a directory and | |
2743 | switches do not contain `d', so that a full listing is expected. | |
2744 | ||
2745 | This works by running a directory listing program | |
406e12d9 | 2746 | whose name is in the variable `insert-directory-program'. |
c3554e95 | 2747 | If WILDCARD, it also runs the shell specified by `shell-file-name'." |
c870ab8e RS |
2748 | ;; We need the directory in order to find the right handler. |
2749 | (let ((handler (find-file-name-handler (expand-file-name file) | |
2750 | 'insert-directory))) | |
c3554e95 RS |
2751 | (if handler |
2752 | (funcall handler 'insert-directory file switches | |
2753 | wildcard full-directory-p) | |
b4da00e9 | 2754 | (if (eq system-type 'vax-vms) |
c3554e95 | 2755 | (vms-read-directory file switches (current-buffer)) |
b7fa904c RS |
2756 | (or (= 0 |
2757 | (if wildcard | |
2758 | ;; Run ls in the directory of the file pattern we asked for. | |
2759 | (let ((default-directory | |
2760 | (if (file-name-absolute-p file) | |
2761 | (file-name-directory file) | |
2762 | (file-name-directory (expand-file-name file)))) | |
2763 | (pattern (file-name-nondirectory file)) | |
2764 | (beg 0)) | |
2765 | ;; Quote some characters that have special meanings in shells; | |
2766 | ;; but don't quote the wildcards--we want them to be special. | |
2767 | ;; We also currently don't quote the quoting characters | |
2768 | ;; in case people want to use them explicitly to quote | |
2769 | ;; wildcard characters. | |
2770 | (while (string-match "[ \t\n;<>&|()#$]" pattern beg) | |
2771 | (setq pattern | |
2772 | (concat (substring pattern 0 (match-beginning 0)) | |
2773 | "\\" | |
2774 | (substring pattern (match-beginning 0))) | |
2775 | beg (1+ (match-end 0)))) | |
2776 | (call-process shell-file-name nil t nil | |
2777 | "-c" (concat "\\" ;; Disregard shell aliases! | |
2778 | insert-directory-program | |
2779 | " -d " | |
2780 | (if (stringp switches) | |
2781 | switches | |
2782 | (mapconcat 'identity switches " ")) | |
a8bf610e | 2783 | " -- " |
b7fa904c RS |
2784 | pattern))) |
2785 | ;; SunOS 4.1.3, SVr4 and others need the "." to list the | |
2786 | ;; directory if FILE is a symbolic link. | |
2787 | (apply 'call-process | |
2788 | insert-directory-program nil t nil | |
2789 | (let (list) | |
2790 | (if (listp switches) | |
2791 | (setq list switches) | |
2792 | (if (not (equal switches "")) | |
2793 | (progn | |
2794 | ;; Split the switches at any spaces | |
2795 | ;; so we can pass separate options as separate args. | |
2796 | (while (string-match " " switches) | |
2797 | (setq list (cons (substring switches 0 (match-beginning 0)) | |
2798 | list) | |
2799 | switches (substring switches (match-end 0)))) | |
2800 | (setq list (nreverse (cons switches list)))))) | |
2801 | (append list | |
a8bf610e RS |
2802 | ;; Avoid lossage if FILE starts with `-'. |
2803 | '("--") | |
b7fa904c RS |
2804 | (list |
2805 | (if full-directory-p | |
2806 | (concat (file-name-as-directory file) ".") | |
2807 | file))))))) | |
2808 | ;; We get here if ls failed. | |
2809 | ;; Access the file to get a suitable error. | |
2810 | (access-file file "Reading directory")))))) | |
b4da00e9 | 2811 | |
88902b35 | 2812 | (defvar kill-emacs-query-functions nil |
65d5c6de | 2813 | "Functions to call with no arguments to query about killing Emacs. |
78c793d1 RS |
2814 | If any of these functions returns nil, killing Emacs is cancelled. |
2815 | `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions, | |
2816 | but `kill-emacs', the low level primitive, does not. | |
2817 | See also `kill-emacs-hook'.") | |
88902b35 | 2818 | |
b4da00e9 RM |
2819 | (defun save-buffers-kill-emacs (&optional arg) |
2820 | "Offer to save each buffer, then kill this Emacs process. | |
2821 | With prefix arg, silently save all file-visiting buffers, then kill." | |
2822 | (interactive "P") | |
2823 | (save-some-buffers arg t) | |
2824 | (and (or (not (memq t (mapcar (function | |
2825 | (lambda (buf) (and (buffer-file-name buf) | |
2826 | (buffer-modified-p buf)))) | |
2827 | (buffer-list)))) | |
2828 | (yes-or-no-p "Modified buffers exist; exit anyway? ")) | |
2829 | (or (not (fboundp 'process-list)) | |
2830 | ;; process-list is not defined on VMS. | |
2831 | (let ((processes (process-list)) | |
2832 | active) | |
2833 | (while processes | |
528415e7 | 2834 | (and (memq (process-status (car processes)) '(run stop open)) |
b4da00e9 RM |
2835 | (let ((val (process-kill-without-query (car processes)))) |
2836 | (process-kill-without-query (car processes) val) | |
2837 | val) | |
2838 | (setq active t)) | |
2839 | (setq processes (cdr processes))) | |
2840 | (or (not active) | |
2841 | (yes-or-no-p "Active processes exist; kill them and exit anyway? ")))) | |
88902b35 | 2842 | ;; Query the user for other things, perhaps. |
fb15c113 | 2843 | (run-hook-with-args-until-failure 'kill-emacs-query-functions) |
b4da00e9 RM |
2844 | (kill-emacs))) |
2845 | \f | |
47afc068 RS |
2846 | ;; We use /: as a prefix to "quote" a file name |
2847 | ;; so that magic file name handlers will not apply to it. | |
2848 | ||
2849 | (setq file-name-handler-alist | |
2850 | (cons '("\\`/:" . file-name-non-special) | |
2851 | file-name-handler-alist)) | |
2852 | ||
2853 | ;; We depend on being the last handler on the list, | |
2854 | ;; so that anything else which does need handling | |
2855 | ;; has been handled already. | |
2856 | ;; So it is safe for us to inhibit *all* magic file name handlers. | |
2857 | ||
2858 | (defun file-name-non-special (operation &rest arguments) | |
2859 | (let ((file-name-handler-alist nil) | |
2860 | ;; Get a list of the indices of the args which are file names. | |
2861 | (file-arg-indices | |
2862 | (cdr (or (assq operation | |
2863 | ;; The first four are special because they | |
2864 | ;; return a file name. We want to include the /: | |
2865 | ;; in the return value. | |
2866 | ;; So just avoid stripping it in the first place. | |
2867 | '((expand-file-name . nil) | |
2868 | (file-name-directory . nil) | |
2869 | (file-name-as-directory . nil) | |
2870 | (directory-file-name . nil) | |
2871 | (rename-file 0 1) | |
2872 | (copy-file 0 1) | |
2873 | (make-symbolic-link 0 1) | |
2874 | (add-name-to-file 0 1))) | |
2875 | ;; For all other operations, treat the first argument only | |
2876 | ;; as the file name. | |
2877 | '(nil 0)))) | |
2878 | ;; Copy ARGUMENTS so we can replace elements in it. | |
2879 | (arguments (copy-sequence arguments))) | |
2880 | ;; Strip off the /: from the file names that have this handler. | |
2881 | (save-match-data | |
2882 | (while file-arg-indices | |
2883 | (and (nth (car file-arg-indices) arguments) | |
2884 | (string-match "\\`/:" (nth (car file-arg-indices) arguments)) | |
2885 | (setcar (nthcdr (car file-arg-indices) arguments) | |
2886 | (substring (nth (car file-arg-indices) arguments) 2))) | |
2887 | (setq file-arg-indices (cdr file-arg-indices)))) | |
2888 | (apply operation arguments))) | |
2889 | \f | |
b4da00e9 RM |
2890 | (define-key ctl-x-map "\C-f" 'find-file) |
2891 | (define-key ctl-x-map "\C-q" 'toggle-read-only) | |
2892 | (define-key ctl-x-map "\C-r" 'find-file-read-only) | |
2893 | (define-key ctl-x-map "\C-v" 'find-alternate-file) | |
2894 | (define-key ctl-x-map "\C-s" 'save-buffer) | |
2895 | (define-key ctl-x-map "s" 'save-some-buffers) | |
2896 | (define-key ctl-x-map "\C-w" 'write-file) | |
2897 | (define-key ctl-x-map "i" 'insert-file) | |
2898 | (define-key esc-map "~" 'not-modified) | |
2899 | (define-key ctl-x-map "\C-d" 'list-directory) | |
2900 | (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs) | |
2901 | ||
2902 | (define-key ctl-x-4-map "f" 'find-file-other-window) | |
2903 | (define-key ctl-x-4-map "r" 'find-file-read-only-other-window) | |
2904 | (define-key ctl-x-4-map "\C-f" 'find-file-other-window) | |
2905 | (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window) | |
924f0a24 | 2906 | (define-key ctl-x-4-map "\C-o" 'display-buffer) |
5bbbceb1 | 2907 | |
f98955ea JB |
2908 | (define-key ctl-x-5-map "b" 'switch-to-buffer-other-frame) |
2909 | (define-key ctl-x-5-map "f" 'find-file-other-frame) | |
2910 | (define-key ctl-x-5-map "\C-f" 'find-file-other-frame) | |
2911 | (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame) | |
c0274f38 ER |
2912 | |
2913 | ;;; files.el ends here |