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