📄 files.el
字号:
;; File input and output commands for Emacs;; Copyright (C) 1985, 1986, 1987, 1990 Free Software Foundation, Inc.;; This file is part of GNU Emacs.;; GNU Emacs is free software; you can redistribute it and/or modify;; it under the terms of the GNU General Public License as published by;; the Free Software Foundation; either version 1, or (at your option);; any later version.;; GNU Emacs is distributed in the hope that it will be useful,;; but WITHOUT ANY WARRANTY; without even the implied warranty of;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the;; GNU General Public License for more details.;; You should have received a copy of the GNU General Public License;; along with GNU Emacs; see the file COPYING. If not, write to;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.(defconst delete-auto-save-files t "*Non-nil means delete a buffer's auto-save filewhen the buffer is saved for real.");(make-variable-buffer-local 'buffer-backed-up);(defvar buffer-backed-up nil; "Non-nil if this buffer's file has been backed up.;Backing up is done before the first time the file is saved.");;; Turn off backup files on VMS since it has version numbers.(defconst make-backup-files (not (eq system-type 'vax-vms)) "*Create a backup of each file when it is saved for the first time.This can be done by renaming the file or by copying.Renaming means that Emacs renames the existing file so that it is abackup file, then writes the buffer into a new file. Any other namesthat the old file had will now refer to the backup file.The new file is owned by you and its group is defaulted.Copying means that Emacs copies the existing file into the backup file,then writes the buffer on top of the existing file. Any other namesthat the old file had will now refer to the new (edited) file.The file's owner and group are unchanged.The choice of renaming or copying is controlled by the variablesbackup-by-copying, backup-by-copying-when-linked andbackup-by-copying-when-mismatch.")(defconst backup-by-copying nil "*Non-nil means always use copying to create backup files.See documentation of variable make-backup-files.")(defconst backup-by-copying-when-linked nil "*Non-nil means use copying to create backups for files with multiple names.This causes the alternate names to refer to the latest version as edited.This variable is relevant only if backup-by-copying is nil.")(defconst backup-by-copying-when-mismatch nil "*Non-nil means create backups by copying if this preserves owner or group.Renaming may still be used (subject to control of other variables)when it would not result in changing the owner or group of the file;that is, for files which are owned by you and whose group matchesthe default for a new file created there by you.This variable is relevant only if backup-by-copying is nil.")(defconst buffer-offer-save nil "*Non-nil in a buffer means offer to save the buffer on exiteven if the buffer is not visiting a file. Automatically local inall buffers.")(make-variable-buffer-local 'buffer-offer-save)(defconst file-precious-flag nil "*Non-nil means protect against I/O errors while saving files.Some modes set this non-nil in particular buffers.")(defvar version-control nil "*Control use of version numbers for backup files.t means make numeric backup versions unconditionally.nil means make them for files that have some already.never means do not make them.")(defvar dired-kept-versions 2 "*When cleaning directory, number of versions to keep.")(defvar trim-versions-without-asking nil "*If true, deletes excess backup versions silently.Otherwise asks confirmation.")(defvar kept-old-versions 2 "*Number of oldest versions to keep when a new numbered backup is made.")(defvar kept-new-versions 2 "*Number of newest versions to keep when a new numbered backup is made.Includes the new backup. Must be > 0")(defconst require-final-newline nil "*t says silently put a newline at the end whenever a file is saved.Non-nil but not t says ask user whether to add a newline in each such case.nil means don't add newlines.")(defconst auto-save-default t "*t says by default do auto-saving of every file-visiting buffer.")(defconst auto-save-visited-file-name nil "*t says auto-save a buffer in the file it is visiting, when practical.Normally auto-save files are written under other names.")(defconst save-abbrevs nil "*Non-nil means save word abbrevs too when files are saved.Loading an abbrev file sets this to t.")(defconst find-file-run-dired t "*Non-nil says run dired if find-file is given the name of a directory.")(defvar find-file-not-found-hooks nil "List of functions to be called for find-file on nonexistent file.These functions are called as soon as the error is detected.buffer-file-name is already set up.The functions are called in the order given,until one of them returns non-nil.")(defvar find-file-hooks nil "List of functions to be called after a buffer is loaded from a file.The buffer's local variables (if any) will have been processed before thefunctions are called.")(defvar write-file-hooks nil "List of functions to be called before writing out a buffer to a file.If one of them returns non-nil, the file is considered already writtenand the rest are not called.")(defconst inhibit-local-variables nil "*Non-nil means query before obeying a file's local-variables list.This applies when the local-variables list is scanned automaticallyafter you find a file. If you explicitly request such a scan with\\[normal-mode], there is no query, regardless of this variable.");; Avoid losing in versions where CLASH_DETECTION is disabled.(or (fboundp 'lock-buffer) (fset 'lock-buffer 'ignore))(or (fboundp 'unlock-buffer) (fset 'unlock-buffer 'ignore))(defun pwd () "Show the current default directory." (interactive nil) (message "Directory %s" default-directory))(defun cd (dir) "Make DIR become the current buffer's default directory." (interactive "DChange default directory: ") (setq dir (expand-file-name dir)) (if (not (eq system-type 'vax-vms)) (setq dir (file-name-as-directory dir))) (if (not (file-directory-p dir)) (error "%s is not a directory" dir) (setq default-directory dir)) (pwd))(defun load-file (file) "Load the file FILE of Lisp code." (interactive "fLoad file: ") (load (expand-file-name file) nil nil t))(defun load-library (library) "Load the library named LIBRARY.This is an interface to the function `load'." (interactive "sLoad library: ") (load library))(defun switch-to-buffer-other-window (buffer) "Select buffer BUFFER in another window." (interactive "BSwitch to buffer in other window: ") (let ((pop-up-windows t)) (pop-to-buffer buffer t)))(defun find-file (filename) "Edit file FILENAME.Switch to a buffer visiting file FILENAME,creating one if none already exists." (interactive "FFind file: ") (switch-to-buffer (find-file-noselect filename)))(defun find-file-other-window (filename) "Edit file FILENAME, in another window.May create a new window, or reuse an existing one;see the function display-buffer." (interactive "FFind file in other window: ") (switch-to-buffer-other-window (find-file-noselect filename)))(defun find-file-read-only (filename) "Edit file FILENAME but don't save without confirmation.Like find-file but marks buffer as read-only." (interactive "fFind file read-only: ") (find-file filename) (setq buffer-read-only t))(defun find-alternate-file (filename) "Find file FILENAME, select its buffer, kill previous buffer.If the current buffer now contains an empty file that you just visited\(presumably by mistake), use this command to visit the file you really want." (interactive "FFind alternate file: ") (and (buffer-modified-p);;; (not buffer-read-only) (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " (buffer-name)))) (error "Aborted")) (let ((obuf (current-buffer)) (ofile buffer-file-name) (oname (buffer-name))) (rename-buffer " **lose**") (setq buffer-file-name nil) (unwind-protect (progn (unlock-buffer) (find-file filename)) (cond ((eq obuf (current-buffer)) (setq buffer-file-name ofile) (lock-buffer) (rename-buffer oname)))) (kill-buffer obuf)))(defun create-file-buffer (filename) "Create a suitably named buffer for visiting FILENAME, and return it.FILENAME (sans directory) is used unchanged if that name is free;otherwise a string <2> or <3> or ... is appended to get an unused name." (let ((lastname (file-name-nondirectory filename))) (if (string= lastname "") (setq lastname filename)) (generate-new-buffer lastname)))(defun find-file-noselect (filename &optional nowarn) "Read file FILENAME into a buffer and return the buffer.If a buffer exists visiting FILENAME, return that one,but verify that the file has not changed since visited or saved.The buffer is not selected, just returned to the caller." (setq filename (expand-file-name filename)) ;; Get rid of the prefixes added by the automounter. (if (string-match "^/tmp_mnt/" filename) (setq filename (substring filename (1- (match-end 0))))) (if (file-directory-p filename) (if find-file-run-dired (dired-noselect filename) (error "%s is a directory." filename)) (let ((buf (get-file-buffer filename)) error) (if buf (or nowarn (verify-visited-file-modtime buf) (cond ((not (file-exists-p filename)) (error "File %s no longer exists!" filename)) ((yes-or-no-p (if (buffer-modified-p buf) "File has changed since last visited or saved. Flush your changes? " "File has changed since last visited or saved. Read from disk? ")) (save-excursion (set-buffer buf) (revert-buffer t t))))) (save-excursion (setq buf (create-file-buffer filename)) (set-buffer buf) (erase-buffer) (condition-case () (insert-file-contents filename t) (file-error (setq error t) ;; Run find-file-not-found-hooks until one returns non-nil. (let ((hooks find-file-not-found-hooks)) (while (and hooks (not (funcall (car hooks)))) (setq hooks (cdr hooks)))))) (setq default-directory (file-name-directory filename)) (after-find-file error (not nowarn)))) buf)))(defun after-find-file (&optional error warn) "Called after finding a file and by the default revert function.Sets buffer mode, parses local variables.Optional args ERROR and WARN: ERROR non-nil means there was anerror in reading the file. WARN non-nil means warn if thereexists an auto-save file more recent than the visited file.Finishes by calling the functions in find-file-hooks." (setq buffer-read-only (not (file-writable-p buffer-file-name))) (if noninteractive nil (let* (not-serious (msg (cond ((not buffer-read-only) (if (and warn (file-newer-than-file-p (make-auto-save-file-name) buffer-file-name)) "Auto save file is newer; consider M-x recover-file" (setq not-serious t) (if error "(New file)" nil))) ((not error) (setq not-serious t) "File is write protected") ((file-attributes buffer-file-name) "File exists, but is read-protected.") ((file-attributes (directory-file-name default-directory)) "File not found and directory write-protected") (t "File not found and directory doesn't exist")))) (if msg (progn (message msg) (or not-serious (sit-for 1 t))))) (if auto-save-default (auto-save-mode t))) (normal-mode t) (mapcar 'funcall find-file-hooks))(defun normal-mode (&optional find-file) "Choose the major mode for this buffer automatically.Also sets up any specified local variables of the file.Uses the visited file name, the -*- line, and the local variables spec.This function is called automatically from `find-file'. In that case,if `inhibit-local-variables' is non-`nil' we require confirmation beforeprocessing a local variables spec. If you run `normal-mode' explicitly,confirmation is never required." (interactive) (or find-file (funcall (or default-major-mode 'fundamental-mode))) (condition-case err (set-auto-mode) (error (message "File mode specification error: %s" (prin1-to-string err)))) (condition-case err (hack-local-variables (not find-file)) (error (message "File local-variables error: %s" (prin1-to-string err)))));(defvar auto-mode-alist ...) now in loaddefs.el(defun set-auto-mode () "Select major mode appropriate for current buffer.May base decision on visited file name (See variable auto-mode-list)or on buffer contents (-*- line or local variables spec), but does not lookfor the \"mode:\" local variable. For that, use hack-local-variables." ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- (let (beg end mode) (save-excursion (goto-char (point-min)) (skip-chars-forward " \t\n") (if (and (search-forward "-*-" (save-excursion (end-of-line) (point)) t) (progn (skip-chars-forward " \t") (setq beg (point)) (search-forward "-*-" (save-excursion (end-of-line) (point)) t)) (progn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -