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