Go to the first, previous, next, last section, table of contents.


Reading Files Info Buffers

Before a file can be edited it must be read into a buffer, this buffer can then be modified and later saved over the original contents of the file. Note that editing a buffer makes no changes to the contents of the file on disk; the buffer will have to be written back to the file on the disk first. See section Writing Buffers.

Function: open-file file-name
This function returns a buffer containing the contents of the file called file-name.

If an existing buffer contains the file called file-name that buffer is returned. Otherwise a new buffer is created and the file read into it.

When the file has successfully been read into the new buffer any local variables defined at the end of the file are processed (see section File Variables) and the function init-mode is used to try to install a major mode for the new buffer. See section Installing Modes.

If file may not be written to the buffer is marked to be read-only.

Note that the hook, read-file-hook, can be used to read the contents of the file into the buffer if necessary. See the documentation of this hook for more details.

Hook: read-file-hook
This hook is called by the open-file function when it wants to read a file into a buffer. If the hook returns a non-nil value open-file assumes that one member of the hook was successful in reading the file, otherwise the file will be read verbatim into the buffer.

The hook is called with two arguments: the name of the file and the buffer to read it into respectively.

If any members of the hook decide to read the file they're responsible for setting the buffer-file-name component of the buffer and the buffer's buffer-file-modtime variables to suitable values.

See the `gzip.jl' file in the Lisp library directory for an example of how this hook can be used (in this case to automatically decompress gzip'ed files).

Function: read-buffer file-or-name &optional buffer
Replaces all text contained by the buffer by the contents of the file file-or-name. This can be either a Lisp file object, in which case bytes will be read until the end of the file is reached, or the name of a file to read.

The following commands are used to read a file into a buffer then display that buffer in the current buffer.

Command: find-file file-name
Display a buffer containing the file file-name in the current window.

When called interactively file-name will be prompted for.

Command: find-alternate-file file-name
Replace the current buffer with one displaying the file file-name. What actually happens is that the current buffer is killed and a new one created.

When called interactively this function will prompt for its argument.

Command: find-file-read-only file-name
Display a buffer containing file-name in the current window. The buffer will be read-only.

This will prompt for its argument when called interactively.

There is also a command to insert the contents of a file into a buffer.

Command: insert-file file-name &optional buffer
This command inserts the contents of the file file-name into the buffer buffer (or the current buffer).

The hook insert-file-hook is called with file-name as an argument to try and insert the file (into the current buffer at the current position). If this hook returns nil (i.e. none of the functions in the hook inserted the file) it will be inserted normally.

If called interactively, file-name will be prompted for.

Hook: insert-file-hook
Hook used to insert a file (given as the hook's argument) into the current buffer at the current cursor position.

Command: revert-buffer &optional buffer
Reloads the contents of the buffer from the file it was originally loaded from; if any unsaved modifications will be lost the user is asked for confirmation.


Go to the first, previous, next, last section, table of contents.