comparison lisp/files.el @ 17004:cd1d930a2c98

(find-file, find-file-other-window, find-file-read-only, find-file-read-only-other-window, find-file-read-only-other-frame, find-alternate-file-other-window, find-alternate-file): With a prefix argument, enable users to specify a coding system interactively. (find-file-noselect): Describe optional arguments NOWARN and RAWFILE in the doc-string. (write-file, insert-file, append-to-file): Accept a prefix argument to enable users to specify a coding system interactively. (revert-buffer): Read an auto-saved file without any code conversion.
author Karl Heuer <kwzh@gnu.org>
date Thu, 20 Feb 1997 05:44:24 +0000
parents c7683cba14c7
children 67515c200d09
comparison
equal deleted inserted replaced
17003:4ec627bf114e 17004:cd1d930a2c98
504 (interactive "BSwitch to buffer in other frame: ") 504 (interactive "BSwitch to buffer in other frame: ")
505 (let ((pop-up-frames t)) 505 (let ((pop-up-frames t))
506 (pop-to-buffer buffer t norecord) 506 (pop-to-buffer buffer t norecord)
507 (raise-frame (window-frame (selected-window))))) 507 (raise-frame (window-frame (selected-window)))))
508 508
509 (defun find-file (filename) 509 (defun find-file (filename &optional coding-system)
510 "Edit file FILENAME. 510 "Edit file FILENAME.
511 Switch to a buffer visiting file FILENAME, 511 Switch to a buffer visiting file FILENAME,
512 creating one if none already exists." 512 creating one if none already exists.
513 (interactive "FFind file: ") 513 A prefix argument enables user to specify the coding-system interactively."
514 (switch-to-buffer (find-file-noselect filename))) 514 (interactive "FFind file: \nZCoding-system: ")
515 515 (if coding-system
516 (defun find-file-other-window (filename) 516 (let ((coding-system-for-read coding-system))
517 (switch-to-buffer (find-file-noselect filename)))
518 (switch-to-buffer (find-file-noselect filename))))
519
520 (defun find-file-other-window (filename &optional coding-system)
517 "Edit file FILENAME, in another window. 521 "Edit file FILENAME, in another window.
518 May create a new window, or reuse an existing one. 522 May create a new window, or reuse an existing one.
523 A prefix argument enables user to specify the coding-system interactively.
519 See the function `display-buffer'." 524 See the function `display-buffer'."
520 (interactive "FFind file in other window: ") 525 (interactive "FFind file in other window: \nZCoding-system: ")
521 (switch-to-buffer-other-window (find-file-noselect filename))) 526 (if coding-system
522 527 (let ((coding-system-for-read coding-system))
523 (defun find-file-other-frame (filename) 528 (switch-to-buffer-other-window (find-file-noselect filename)))
529 (switch-to-buffer-other-window (find-file-noselect filename))))
530
531 (defun find-file-other-frame (filename &optional coding-system)
524 "Edit file FILENAME, in another frame. 532 "Edit file FILENAME, in another frame.
525 May create a new frame, or reuse an existing one. 533 May create a new frame, or reuse an existing one.
534 A prefix argument enables user to specify the coding-system interactively.
526 See the function `display-buffer'." 535 See the function `display-buffer'."
527 (interactive "FFind file in other frame: ") 536 (interactive "FFind file in other frame: \nZCoding-system: ")
528 (switch-to-buffer-other-frame (find-file-noselect filename))) 537 (if coding-system
529 538 (let ((coding-system-for-read coding-system))
530 (defun find-file-read-only (filename) 539 (switch-to-buffer-other-frame (find-file-noselect filename)))
540 (switch-to-buffer-other-frame (find-file-noselect filename))))
541
542 (defun find-file-read-only (filename &optional coding-system)
531 "Edit file FILENAME but don't allow changes. 543 "Edit file FILENAME but don't allow changes.
532 Like \\[find-file] but marks buffer as read-only. 544 Like \\[find-file] but marks buffer as read-only.
545 A prefix argument enables user to specify the coding-system interactively.
533 Use \\[toggle-read-only] to permit editing." 546 Use \\[toggle-read-only] to permit editing."
534 (interactive "fFind file read-only: ") 547 (interactive "fFind file read-only: \nZCoding-system: ")
535 (find-file filename) 548 (find-file filename coding-system)
536 (setq buffer-read-only t) 549 (setq buffer-read-only t)
537 (current-buffer)) 550 (current-buffer))
538 551
539 (defun find-file-read-only-other-window (filename) 552 (defun find-file-read-only-other-window (filename &optional coding-system)
540 "Edit file FILENAME in another window but don't allow changes. 553 "Edit file FILENAME in another window but don't allow changes.
541 Like \\[find-file-other-window] but marks buffer as read-only. 554 Like \\[find-file-other-window] but marks buffer as read-only.
555 A prefix argument enables user to specify the coding-system interactively.
542 Use \\[toggle-read-only] to permit editing." 556 Use \\[toggle-read-only] to permit editing."
543 (interactive "fFind file read-only other window: ") 557 (interactive "fFind file read-only other window: \nZCoding-system: ")
544 (find-file-other-window filename) 558 (find-file-other-window filename coding-system)
545 (setq buffer-read-only t) 559 (setq buffer-read-only t)
546 (current-buffer)) 560 (current-buffer))
547 561
548 (defun find-file-read-only-other-frame (filename) 562 (defun find-file-read-only-other-frame (filename &optional coding-system)
549 "Edit file FILENAME in another frame but don't allow changes. 563 "Edit file FILENAME in another frame but don't allow changes.
550 Like \\[find-file-other-frame] but marks buffer as read-only. 564 Like \\[find-file-other-frame] but marks buffer as read-only.
565 A prefix argument enables user to specify the coding-system interactively.
551 Use \\[toggle-read-only] to permit editing." 566 Use \\[toggle-read-only] to permit editing."
552 (interactive "fFind file read-only other frame: ") 567 (interactive "fFind file read-only other frame: \nZCoding-system: ")
553 (find-file-other-frame filename) 568 (find-file-other-frame filename coding-system)
554 (setq buffer-read-only t) 569 (setq buffer-read-only t)
555 (current-buffer)) 570 (current-buffer))
556 571
557 (defun find-alternate-file-other-window (filename) 572 (defun find-alternate-file-other-window (filename &optional coding-system)
558 "Find file FILENAME as a replacement for the file in the next window. 573 "Find file FILENAME as a replacement for the file in the next window.
559 This command does not select that window." 574 This command does not select that window.
575 A prefix argument enables user to specify the coding-system interactively."
560 (interactive 576 (interactive
561 (save-selected-window 577 (save-selected-window
562 (other-window 1) 578 (other-window 1)
563 (let ((file buffer-file-name) 579 (let ((file buffer-file-name)
564 (file-name nil) 580 (file-name nil)
565 (file-dir nil)) 581 (file-dir nil))
566 (and file 582 (and file
567 (setq file-name (file-name-nondirectory file) 583 (setq file-name (file-name-nondirectory file)
568 file-dir (file-name-directory file))) 584 file-dir (file-name-directory file)))
569 (list (read-file-name 585 (list (read-file-name
570 "Find alternate file: " file-dir nil nil file-name))))) 586 "Find alternate file: " file-dir nil nil file-name)
587 (if current-prefix-arg
588 (read-coding-system "Coding-system: "))))))
571 (if (one-window-p) 589 (if (one-window-p)
572 (find-file-other-window filename) 590 (find-file-other-window filename coding-system)
573 (save-selected-window 591 (save-selected-window
574 (other-window 1) 592 (other-window 1)
575 (find-alternate-file filename)))) 593 (find-alternate-file filename coding-system))))
576 594
577 (defun find-alternate-file (filename) 595 (defun find-alternate-file (filename &optional coding-system)
578 "Find file FILENAME, select its buffer, kill previous buffer. 596 "Find file FILENAME, select its buffer, kill previous buffer.
579 If the current buffer now contains an empty file that you just visited 597 If the current buffer now contains an empty file that you just visited
580 \(presumably by mistake), use this command to visit the file you really want." 598 \(presumably by mistake), use this command to visit the file you really want.
599 A prefix argument enables user to specify the coding-system interactively."
581 (interactive 600 (interactive
582 (let ((file buffer-file-name) 601 (let ((file buffer-file-name)
583 (file-name nil) 602 (file-name nil)
584 (file-dir nil)) 603 (file-dir nil))
585 (and file 604 (and file
586 (setq file-name (file-name-nondirectory file) 605 (setq file-name (file-name-nondirectory file)
587 file-dir (file-name-directory file))) 606 file-dir (file-name-directory file)))
588 (list (read-file-name 607 (list (read-file-name
589 "Find alternate file: " file-dir nil nil file-name)))) 608 "Find alternate file: " file-dir nil nil file-name)
609 (if current-prefix-arg
610 (read-coding-system "Coding-system: ")))))
590 (and (buffer-modified-p) (buffer-file-name) 611 (and (buffer-modified-p) (buffer-file-name)
591 ;; (not buffer-read-only) 612 ;; (not buffer-read-only)
592 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " 613 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
593 (buffer-name)))) 614 (buffer-name))))
594 (error "Aborted")) 615 (error "Aborted"))
604 (progn 625 (progn
605 (unlock-buffer) 626 (unlock-buffer)
606 (setq buffer-file-name nil) 627 (setq buffer-file-name nil)
607 (setq buffer-file-number nil) 628 (setq buffer-file-number nil)
608 (setq buffer-file-truename nil) 629 (setq buffer-file-truename nil)
609 (find-file filename)) 630 (find-file filename coding-system))
610 (cond ((eq obuf (current-buffer)) 631 (cond ((eq obuf (current-buffer))
611 (setq buffer-file-name ofile) 632 (setq buffer-file-name ofile)
612 (setq buffer-file-number onum) 633 (setq buffer-file-number onum)
613 (setq buffer-file-truename otrue) 634 (setq buffer-file-truename otrue)
614 (lock-buffer) 635 (lock-buffer)
751 772
752 (defun find-file-noselect (filename &optional nowarn rawfile) 773 (defun find-file-noselect (filename &optional nowarn rawfile)
753 "Read file FILENAME into a buffer and return the buffer. 774 "Read file FILENAME into a buffer and return the buffer.
754 If a buffer exists visiting FILENAME, return that one, but 775 If a buffer exists visiting FILENAME, return that one, but
755 verify that the file has not changed since visited or saved. 776 verify that the file has not changed since visited or saved.
756 The buffer is not selected, just returned to the caller." 777 The buffer is not selected, just returned to the caller.
778 Optional first arg NOWARN non-nil means suppress any warning messages.
779 Optional second arg RAWFILE non-nil means the file is read literally"
757 (setq filename 780 (setq filename
758 (abbreviate-file-name 781 (abbreviate-file-name
759 (expand-file-name filename))) 782 (expand-file-name filename)))
760 (if (file-directory-p filename) 783 (if (file-directory-p filename)
761 (if find-file-run-dired 784 (if find-file-run-dired
1552 (rename-file oauto buffer-auto-save-file-name t))) 1575 (rename-file oauto buffer-auto-save-file-name t)))
1553 (and buffer-file-name 1576 (and buffer-file-name
1554 (not along-with-file) 1577 (not along-with-file)
1555 (set-buffer-modified-p t))) 1578 (set-buffer-modified-p t)))
1556 1579
1557 (defun write-file (filename &optional confirm) 1580 (defun write-file (filename &optional confirm coding-system)
1558 "Write current buffer into file FILENAME. 1581 "Write current buffer into file FILENAME.
1559 Makes buffer visit that file, and marks it not modified. 1582 Makes buffer visit that file, and marks it not modified.
1560 If the buffer is already visiting a file, you can specify 1583 If the buffer is already visiting a file, you can specify
1561 a directory name as FILENAME, to write a file of the same 1584 a directory name as FILENAME, to write a file of the same
1562 old name in that directory. 1585 old name in that directory.
1563 1586
1564 If optional second arg CONFIRM is non-nil, 1587 If optional second arg CONFIRM is non-nil,
1565 ask for confirmation for overwriting an existing file. 1588 ask for confirmation for overwriting an existing file.
1566 Interactively, confirmation is required unless you supply a prefix argument." 1589 Interactively, confirmation is required unless you supply a prefix argument.
1590
1591 A prefix argument also enables user to interactively specify a
1592 coding-system for encoding the file."
1567 ;; (interactive "FWrite file: ") 1593 ;; (interactive "FWrite file: ")
1568 (interactive 1594 (interactive
1569 (list (if buffer-file-name 1595 (list (if buffer-file-name
1570 (read-file-name "Write file: " 1596 (read-file-name "Write file: "
1571 nil nil nil nil) 1597 nil nil nil nil)
1572 (read-file-name "Write file: " 1598 (read-file-name "Write file: "
1573 (cdr (assq 'default-directory 1599 (cdr (assq 'default-directory
1574 (buffer-local-variables))) 1600 (buffer-local-variables)))
1575 nil nil (buffer-name))) 1601 nil nil (buffer-name)))
1576 (not current-prefix-arg))) 1602 (not current-prefix-arg)
1603 (if current-prefix-arg
1604 (read-coding-system "Coding-system: "))
1605 ))
1577 (or (null filename) (string-equal filename "") 1606 (or (null filename) (string-equal filename "")
1578 (progn 1607 (progn
1579 ;; If arg is just a directory, 1608 ;; If arg is just a directory,
1580 ;; use same file name, but in that directory. 1609 ;; use same file name, but in that directory.
1581 (if (and (file-directory-p filename) buffer-file-name) 1610 (if (and (file-directory-p filename) buffer-file-name)
1585 (file-exists-p filename) 1614 (file-exists-p filename)
1586 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename)) 1615 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
1587 (error "Canceled"))) 1616 (error "Canceled")))
1588 (set-visited-file-name filename (not confirm)))) 1617 (set-visited-file-name filename (not confirm))))
1589 (set-buffer-modified-p t) 1618 (set-buffer-modified-p t)
1590 (save-buffer)) 1619 (if coding-system
1620 (let ((coding-system-for-write coding-system))
1621 ;; It is convenient to change buffer-file-coding-system to the
1622 ;; specified one.
1623 (set-buffer-file-coding-system coding-system)
1624 (save-buffer))
1625 (save-buffer)))
1591 1626
1592 (defun backup-buffer () 1627 (defun backup-buffer ()
1593 "Make a backup of the disk file visited by the current buffer, if appropriate. 1628 "Make a backup of the disk file visited by the current buffer, if appropriate.
1594 This is normally done before saving the buffer the first time. 1629 This is normally done before saving the buffer the first time.
1595 If the value is non-nil, it is the result of `file-modes' on the original 1630 If the value is non-nil, it is the result of `file-modes' on the original
2114 (if (null arg) 2149 (if (null arg)
2115 (not buffer-read-only) 2150 (not buffer-read-only)
2116 (> (prefix-numeric-value arg) 0))) 2151 (> (prefix-numeric-value arg) 0)))
2117 (force-mode-line-update)) 2152 (force-mode-line-update))
2118 2153
2119 (defun insert-file (filename) 2154 (defun insert-file (filename &optional coding-system)
2120 "Insert contents of file FILENAME into buffer after point. 2155 "Insert contents of file FILENAME into buffer after point.
2121 Set mark after the inserted text. 2156 Set mark after the inserted text.
2157 A prefix argument enables user to specify the coding-system interactively.
2122 2158
2123 This function is meant for the user to run interactively. 2159 This function is meant for the user to run interactively.
2124 Don't call it from programs! Use `insert-file-contents' instead. 2160 Don't call it from programs! Use `insert-file-contents' instead.
2125 \(Its calling sequence is different; see its documentation)." 2161 \(Its calling sequence is different; see its documentation)."
2126 (interactive "*fInsert file: ") 2162 (interactive "*fInsert file: \nZCoding-system: ")
2127 (if (file-directory-p filename) 2163 (if (file-directory-p filename)
2128 (signal 'file-error (list "Opening input file" "file is a directory" 2164 (signal 'file-error (list "Opening input file" "file is a directory"
2129 filename))) 2165 filename)))
2130 (let ((tem (insert-file-contents filename))) 2166 (let ((tem
2167 (if coding-system
2168 (let ((coding-system-for-read coding-system))
2169 (insert-file-contents filename))
2170 (insert-file-contents filename))))
2131 (push-mark (+ (point) (car (cdr tem)))))) 2171 (push-mark (+ (point) (car (cdr tem))))))
2132 2172
2133 (defun append-to-file (start end filename) 2173 (defun append-to-file (start end filename &optional coding-system)
2134 "Append the contents of the region to the end of file FILENAME. 2174 "Append the contents of the region to the end of file FILENAME.
2135 When called from a function, expects three arguments, 2175 When called from a function, expects three arguments,
2136 START, END and FILENAME. START and END are buffer positions 2176 START, END and FILENAME. START and END are buffer positions
2137 saying what text to write." 2177 saying what text to write.
2138 (interactive "r\nFAppend to file: ") 2178 A prefix argument enables user to specify the coding-system interactively."
2139 (write-region start end filename t)) 2179 (interactive "r\nFAppend to file: \nZCoding-system: ")
2180 (if coding-system
2181 (let ((coding-system-for-write coding-system))
2182 (write-region start end filename t))
2183 (write-region start end filename t)))
2140 2184
2141 (defun file-newest-backup (filename) 2185 (defun file-newest-backup (filename)
2142 "Return most recent backup file for FILENAME or nil if no backups exist." 2186 "Return most recent backup file for FILENAME or nil if no backups exist."
2143 (let* ((filename (expand-file-name filename)) 2187 (let* ((filename (expand-file-name filename))
2144 (file (file-name-nondirectory filename)) 2188 (file (file-name-nondirectory filename))
2353 (call-process "ls" nil standard-output nil 2397 (call-process "ls" nil standard-output nil
2354 (if (file-symlink-p file) "-lL" "-l") 2398 (if (file-symlink-p file) "-lL" "-l")
2355 file file-name))) 2399 file file-name)))
2356 (yes-or-no-p (format "Recover auto save file %s? " file-name))) 2400 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
2357 (switch-to-buffer (find-file-noselect file t)) 2401 (switch-to-buffer (find-file-noselect file t))
2358 (let ((buffer-read-only nil)) 2402 (let ((buffer-read-only nil)
2403 ;; Auto-saved file shoule be read without any code conversion.
2404 (coding-system-for-read 'no-conversion))
2359 (erase-buffer) 2405 (erase-buffer)
2360 (insert-file-contents file-name nil)) 2406 (insert-file-contents file-name nil))
2361 (after-find-file nil nil t)) 2407 (after-find-file nil nil t))
2362 (t (error "Recover-file cancelled."))))) 2408 (t (error "Recover-file cancelled.")))))
2363 2409