comparison lisp/files.el @ 45976:071129f77fae

Rename (and mark as obsolete) find-file-hooks to find-file-hook, find-file-not-found-hooks to find-file-not-found-functions, write-file-hooks to write-file-functions, write-contents-hooks to write-contents-functions. Mark local-write-file-hooks as obsolete. (locate-file-completion): Don't cons uselessly. Use test-completion. (basic-save-buffer): Use (point-min) rather than 1. (basic-save-buffer-2): Obey the `setmodes' returned by backup-buffer.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sun, 23 Jun 2002 21:16:38 +0000
parents 00b587e45a1a
children 56af4e9d2ac3
comparison
equal deleted inserted replaced
45975:7ea38f55545b 45976:071129f77fae
342 :type '(hook :options (cvs-dired-noselect dired-noselect)) 342 :type '(hook :options (cvs-dired-noselect dired-noselect))
343 :group 'find-file) 343 :group 'find-file)
344 344
345 ;;;It is not useful to make this a local variable. 345 ;;;It is not useful to make this a local variable.
346 ;;;(put 'find-file-not-found-hooks 'permanent-local t) 346 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
347 (defvar find-file-not-found-hooks nil 347 (defvar find-file-not-found-functions nil
348 "List of functions to be called for `find-file' on nonexistent file. 348 "List of functions to be called for `find-file' on nonexistent file.
349 These functions are called as soon as the error is detected. 349 These functions are called as soon as the error is detected.
350 Variable `buffer-file-name' is already set up. 350 Variable `buffer-file-name' is already set up.
351 The functions are called in the order given until one of them returns non-nil.") 351 The functions are called in the order given until one of them returns non-nil.")
352 (defvaralias 'find-file-not-found-hooks 'find-file-not-found-functions)
353 (make-obsolete-variable
354 'find-file-not-found-hooks 'find-file-not-found-functions "21.4")
352 355
353 ;;;It is not useful to make this a local variable. 356 ;;;It is not useful to make this a local variable.
354 ;;;(put 'find-file-hooks 'permanent-local t) 357 ;;;(put 'find-file-hooks 'permanent-local t)
355 (defvar find-file-hooks nil 358 (defvar find-file-hook nil
356 "List of functions to be called after a buffer is loaded from a file. 359 "List of functions to be called after a buffer is loaded from a file.
357 The buffer's local variables (if any) will have been processed before the 360 The buffer's local variables (if any) will have been processed before the
358 functions are called.") 361 functions are called.")
359 362 (defvaralias 'find-file-hooks 'find-file-hook)
360 (defvar write-file-hooks nil 363 (make-obsolete-variable 'find-file-hooks 'find-file-hook "21.4")
364
365 (defvar write-file-functions nil
361 "List of functions to be called before writing out a buffer to a file. 366 "List of functions to be called before writing out a buffer to a file.
362 If one of them returns non-nil, the file is considered already written 367 If one of them returns non-nil, the file is considered already written
363 and the rest are not called. 368 and the rest are not called.
364 These hooks are considered to pertain to the visited file. 369 These hooks are considered to pertain to the visited file.
365 So any buffer-local binding of `write-file-hooks' is 370 So any buffer-local binding of this variable is discarded if you change
366 discarded if you change the visited file name with \\[set-visited-file-name]. 371 the visited file name with \\[set-visited-file-name], but not when you
367 372 change the major mode.
368 Don't make this variable buffer-local; instead, use `local-write-file-hooks'. 373
369 See also `write-contents-hooks'.") 374 See also `write-contents-functions'.")
370 ;;; However, in case someone does make it local... 375 (put 'write-file-functions 'permanent-local t)
371 (put 'write-file-hooks 'permanent-local t) 376 (defvaralias 'write-file-hooks 'write-file-functions)
372 377 (make-obsolete-variable 'write-file-hooks 'write-file-functions "21.4")
373 (defvar local-write-file-hooks nil 378
374 "Just like `write-file-hooks', except intended for per-buffer use. 379 (defvar local-write-file-hooks nil)
375 The functions in this list are called before the ones in
376 `write-file-hooks'.
377
378 This variable is meant to be used for hooks that have to do with a
379 particular visited file. Therefore, it is a permanent local, so that
380 changing the major mode does not clear it. However, calling
381 `set-visited-file-name' does clear it.")
382 (make-variable-buffer-local 'local-write-file-hooks) 380 (make-variable-buffer-local 'local-write-file-hooks)
383 (put 'local-write-file-hooks 'permanent-local t) 381 (put 'local-write-file-hooks 'permanent-local t)
384 382 (make-obsolete-variable 'local-write-file-hooks 'write-file-functions "21.4")
385 (defvar write-contents-hooks nil 383
384 (defvar write-contents-functions nil
386 "List of functions to be called before writing out a buffer to a file. 385 "List of functions to be called before writing out a buffer to a file.
387 If one of them returns non-nil, the file is considered already written 386 If one of them returns non-nil, the file is considered already written
388 and the rest are not called. 387 and the rest are not called.
389 388
390 This variable is meant to be used for hooks that pertain to the 389 This variable is meant to be used for hooks that pertain to the
391 buffer's contents, not to the particular visited file; thus, 390 buffer's contents, not to the particular visited file; thus,
392 `set-visited-file-name' does not clear this variable; but changing the 391 `set-visited-file-name' does not clear this variable; but changing the
393 major mode does clear it. 392 major mode does clear it.
394 393
395 This variable automatically becomes buffer-local whenever it is set. 394 See also `write-file-functions'.")
396 If you use `add-hook' to add elements to the list, use nil for the 395 (make-variable-buffer-local 'write-contents-functions)
397 LOCAL argument. 396 (defvaralias 'write-contents-hooks 'write-contents-functions)
398 397 (make-obsolete-variable 'write-contents-hooks 'write-contents-functions "21.4")
399 See also `write-file-hooks'.")
400 (make-variable-buffer-local 'write-contents-hooks)
401 398
402 (defcustom enable-local-variables t 399 (defcustom enable-local-variables t
403 "*Control use of local variables in files you visit. 400 "*Control use of local variables in files you visit.
404 The value can be t, nil or something else. 401 The value can be t, nil or something else.
405 A value of t means file local variables specifications are obeyed; 402 A value of t means file local variables specifications are obeyed;
600 (file-name-nondirectory string) dir)) 597 (file-name-nondirectory string) dir))
601 (push (if string-dir (concat string-dir file) file) names) 598 (push (if string-dir (concat string-dir file) file) names)
602 (when (string-match suffix file) 599 (when (string-match suffix file)
603 (setq file (substring file 0 (match-beginning 0))) 600 (setq file (substring file 0 (match-beginning 0)))
604 (push (if string-dir (concat string-dir file) file) names))))) 601 (push (if string-dir (concat string-dir file) file) names)))))
605 (if action 602 (cond
606 (all-completions string (mapcar 'list names)) 603 ((eq action t) (all-completions string names))
607 (try-completion string (mapcar 'list names)))))) 604 ((null action) (try-completion string names))
605 (t (test-completion string names))))))
608 606
609 (defun load-library (library) 607 (defun load-library (library)
610 "Load the library named LIBRARY. 608 "Load the library named LIBRARY.
611 This is an interface to the function `load'." 609 This is an interface to the function `load'."
612 (interactive 610 (interactive
1254 (not (file-readable-p filename))) 1252 (not (file-readable-p filename)))
1255 (kill-buffer buf) 1253 (kill-buffer buf)
1256 (signal 'file-error (list "File is not readable" 1254 (signal 'file-error (list "File is not readable"
1257 filename))) 1255 filename)))
1258 ;; Run find-file-not-found-hooks until one returns non-nil. 1256 ;; Run find-file-not-found-hooks until one returns non-nil.
1259 (or (run-hook-with-args-until-success 'find-file-not-found-hooks) 1257 (or (run-hook-with-args-until-success 'find-file-not-found-functions)
1260 ;; If they fail too, set error. 1258 ;; If they fail too, set error.
1261 (setq error t))))) 1259 (setq error t)))))
1262 ;; Record the file's truename, and maybe use that as visited name. 1260 ;; Record the file's truename, and maybe use that as visited name.
1263 (if (equal filename buffer-file-name) 1261 (if (equal filename buffer-file-name)
1264 (setq buffer-file-truename truename) 1262 (setq buffer-file-truename truename)
1297 1295
1298 (defun insert-file-contents-literally (filename &optional visit beg end replace) 1296 (defun insert-file-contents-literally (filename &optional visit beg end replace)
1299 "Like `insert-file-contents', but only reads in the file literally. 1297 "Like `insert-file-contents', but only reads in the file literally.
1300 A buffer may be modified in several ways after reading into the buffer, 1298 A buffer may be modified in several ways after reading into the buffer,
1301 to Emacs features such as format decoding, character code 1299 to Emacs features such as format decoding, character code
1302 conversion, `find-file-hooks', automatic uncompression, etc. 1300 conversion, `find-file-hook', automatic uncompression, etc.
1303 1301
1304 This function ensures that none of these modifications will take place." 1302 This function ensures that none of these modifications will take place."
1305 (let ((format-alist nil) 1303 (let ((format-alist nil)
1306 (after-insert-file-functions nil) 1304 (after-insert-file-functions nil)
1307 (coding-system-for-read 'no-conversion) 1305 (coding-system-for-read 'no-conversion)
1370 exists an auto-save file more recent than the visited file. 1368 exists an auto-save file more recent than the visited file.
1371 NOAUTO means don't mess with auto-save mode. 1369 NOAUTO means don't mess with auto-save mode.
1372 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil 1370 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
1373 means this call was from `revert-buffer'. 1371 means this call was from `revert-buffer'.
1374 Fifth arg NOMODES non-nil means don't alter the file's modes. 1372 Fifth arg NOMODES non-nil means don't alter the file's modes.
1375 Finishes by calling the functions in `find-file-hooks' 1373 Finishes by calling the functions in `find-file-hook'
1376 unless NOMODES is non-nil." 1374 unless NOMODES is non-nil."
1377 (setq buffer-read-only (not (file-writable-p buffer-file-name))) 1375 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
1378 (if noninteractive 1376 (if noninteractive
1379 nil 1377 nil
1380 (let* (not-serious 1378 (let* (not-serious
1424 (normal-mode t) 1422 (normal-mode t)
1425 (when (and buffer-read-only 1423 (when (and buffer-read-only
1426 view-read-only 1424 view-read-only
1427 (not (eq (get major-mode 'mode-class) 'special))) 1425 (not (eq (get major-mode 'mode-class) 'special)))
1428 (view-mode-enter)) 1426 (view-mode-enter))
1429 (run-hooks 'find-file-hooks))) 1427 (run-hooks 'find-file-hook)))
1430 1428
1431 (defun normal-mode (&optional find-file) 1429 (defun normal-mode (&optional find-file)
1432 "Choose the major mode for this buffer automatically. 1430 "Choose the major mode for this buffer automatically.
1433 Also sets up any specified local variables of the file. 1431 Also sets up any specified local variables of the file.
1434 Uses the visited file name, the -*- line, and the local variables spec. 1432 Uses the visited file name, the -*- line, and the local variables spec.
2167 (setq buffer-file-name buffer-file-truename)))) 2165 (setq buffer-file-name buffer-file-truename))))
2168 (setq buffer-file-number 2166 (setq buffer-file-number
2169 (if filename 2167 (if filename
2170 (nthcdr 10 (file-attributes buffer-file-name)) 2168 (nthcdr 10 (file-attributes buffer-file-name))
2171 nil))) 2169 nil)))
2172 ;; write-file-hooks is normally used for things like ftp-find-file 2170 ;; write-file-functions is normally used for things like ftp-find-file
2173 ;; that visit things that are not local files as if they were files. 2171 ;; that visit things that are not local files as if they were files.
2174 ;; Changing to visit an ordinary local file instead should flush the hook. 2172 ;; Changing to visit an ordinary local file instead should flush the hook.
2175 (kill-local-variable 'write-file-hooks) 2173 (kill-local-variable 'write-file-functions)
2176 (kill-local-variable 'local-write-file-hooks) 2174 (kill-local-variable 'local-write-file-hooks)
2177 (kill-local-variable 'revert-buffer-function) 2175 (kill-local-variable 'revert-buffer-function)
2178 (kill-local-variable 'backup-inhibited) 2176 (kill-local-variable 'backup-inhibited)
2179 ;; If buffer was read-only because of version control, 2177 ;; If buffer was read-only because of version control,
2180 ;; that reason is gone now, so make it writable. 2178 ;; that reason is gone now, so make it writable.
2744 (make-variable-buffer-local 'save-buffer-coding-system) 2742 (make-variable-buffer-local 'save-buffer-coding-system)
2745 (put 'save-buffer-coding-system 'permanent-local t) 2743 (put 'save-buffer-coding-system 'permanent-local t)
2746 2744
2747 (defun basic-save-buffer () 2745 (defun basic-save-buffer ()
2748 "Save the current buffer in its visited file, if it has been modified. 2746 "Save the current buffer in its visited file, if it has been modified.
2749 The hooks `write-contents-hooks', `local-write-file-hooks' and 2747 The hooks `write-contents-functions' and `write-file-functions' get a chance
2750 `write-file-hooks' get a chance to do the job of saving; if they do not, 2748 to do the job of saving; if they do not, then the buffer is saved in
2751 then the buffer is saved in the visited file file in the usual way. 2749 the visited file file in the usual way.
2752 After saving the buffer, this function runs `after-save-hook'." 2750 After saving the buffer, this function runs `after-save-hook'."
2753 (interactive) 2751 (interactive)
2754 (save-current-buffer 2752 (save-current-buffer
2755 ;; In an indirect buffer, save its base buffer instead. 2753 ;; In an indirect buffer, save its base buffer instead.
2756 (if (buffer-base-buffer) 2754 (if (buffer-base-buffer)
2788 (file-name-nondirectory buffer-file-name))) 2786 (file-name-nondirectory buffer-file-name)))
2789 (error "Save not confirmed")) 2787 (error "Save not confirmed"))
2790 (save-restriction 2788 (save-restriction
2791 (widen) 2789 (widen)
2792 (save-excursion 2790 (save-excursion
2793 (and (> (point-max) 1) 2791 (and (> (point-max) (point-min))
2794 (not find-file-literally) 2792 (not find-file-literally)
2795 (/= (char-after (1- (point-max))) ?\n) 2793 (/= (char-after (1- (point-max))) ?\n)
2796 (not (and (eq selective-display t) 2794 (not (and (eq selective-display t)
2797 (= (char-after (1- (point-max))) ?\r))) 2795 (= (char-after (1- (point-max))) ?\r)))
2798 (or (eq require-final-newline t) 2796 (or (eq require-final-newline t)
2803 (save-excursion 2801 (save-excursion
2804 (goto-char (point-max)) 2802 (goto-char (point-max))
2805 (insert ?\n)))) 2803 (insert ?\n))))
2806 ;; Support VC version backups. 2804 ;; Support VC version backups.
2807 (vc-before-save) 2805 (vc-before-save)
2808 (or (run-hook-with-args-until-success 'write-contents-hooks) 2806 (or (run-hook-with-args-until-success 'write-contents-functions)
2809 (run-hook-with-args-until-success 'local-write-file-hooks) 2807 (run-hook-with-args-until-success 'local-write-file-hooks)
2810 (run-hook-with-args-until-success 'write-file-hooks) 2808 (run-hook-with-args-until-success 'write-file-functions)
2811 ;; If a hook returned t, file is already "written". 2809 ;; If a hook returned t, file is already "written".
2812 ;; Otherwise, write it the usual way now. 2810 ;; Otherwise, write it the usual way now.
2813 (setq setmodes (basic-save-buffer-1))) 2811 (setq setmodes (basic-save-buffer-1)))
2814 ;; Now we have saved the current buffer. Let's make sure 2812 ;; Now we have saved the current buffer. Let's make sure
2815 ;; that buffer-file-coding-system is fixed to what 2813 ;; that buffer-file-coding-system is fixed to what
2831 (run-hooks 'after-save-hook)) 2829 (run-hooks 'after-save-hook))
2832 (message "(No changes need to be saved)")))) 2830 (message "(No changes need to be saved)"))))
2833 2831
2834 ;; This does the "real job" of writing a buffer into its visited file 2832 ;; This does the "real job" of writing a buffer into its visited file
2835 ;; and making a backup file. This is what is normally done 2833 ;; and making a backup file. This is what is normally done
2836 ;; but inhibited if one of write-file-hooks returns non-nil. 2834 ;; but inhibited if one of write-file-functions returns non-nil.
2837 ;; It returns a value to store in setmodes. 2835 ;; It returns a value to store in setmodes.
2838 (defun basic-save-buffer-1 () 2836 (defun basic-save-buffer-1 ()
2839 (if save-buffer-coding-system 2837 (if save-buffer-coding-system
2840 (let ((coding-system-for-write save-buffer-coding-system)) 2838 (let ((coding-system-for-write save-buffer-coding-system))
2841 (basic-save-buffer-2)) 2839 (basic-save-buffer-2))
2897 (file-error nil)) 2895 (file-error nil))
2898 (set-visited-file-modtime old-modtime)))) 2896 (set-visited-file-modtime old-modtime))))
2899 ;; Since we have created an entirely new file 2897 ;; Since we have created an entirely new file
2900 ;; and renamed it, make sure it gets the 2898 ;; and renamed it, make sure it gets the
2901 ;; right permission bits set. 2899 ;; right permission bits set.
2902 (setq setmodes (file-modes buffer-file-name)) 2900 (setq setmodes (or setmodes (file-modes buffer-file-name)))
2903 ;; We succeeded in writing the temp file, 2901 ;; We succeeded in writing the temp file,
2904 ;; so rename it. 2902 ;; so rename it.
2905 (rename-file tempname buffer-file-name t)) 2903 (rename-file tempname buffer-file-name t))
2906 ;; If file not writable, see if we can make it writable 2904 ;; If file not writable, see if we can make it writable
2907 ;; temporarily while we write it. 2905 ;; temporarily while we write it.