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