Next: , Previous: , Up: Files   [Contents][Index]


35.4 Writing to Files

You can write the contents of a buffer, or part of a buffer, directly to a file on disk using the append-to-file and write-region functions. Don’t use these functions to write to files that are being visited; that could cause confusion in the mechanisms for visiting.

Command: append-to-file start end filename

This function appends the contents of the region delimited by start and end in the current buffer to the end of file filename. If that file does not exist, it is created. If that file exists it is overwritten. This function returns nil.

An error is signaled if filename specifies a nonwritable file, or a nonexistent file in a directory where files cannot be created.

Command: write-region start end filename &optional append visit

This function writes the region delimited by start and end in the current buffer into the file specified by filename.

If start is a string, then write-region writes or appends that string, rather than text from the buffer.

If append is non-nil, then the specified text is appended to the existing file contents (if any).

If visit is t, then SXEmacs establishes an association between the buffer and the file: the buffer is then visiting that file. It also sets the last file modification time for the current buffer to filename’s modtime, and marks the buffer as not modified. This feature is used by save-buffer, but you probably should not use it yourself.

If visit is a string, it specifies the file name to visit. This way, you can write the data to one file (filename) while recording the buffer as visiting another file (visit). The argument visit is used in the echo area message and also for file locking; visit is stored in buffer-file-name. This feature is used to implement file-precious-flag; don’t use it yourself unless you really know what you’re doing.

The function write-region converts the data which it writes to the appropriate file formats specified by buffer-file-format. See Format Conversion. It also calls the functions in the list write-region-annotate-functions; see Saving Properties.

Normally, write-region displays a message ‘Wrote file filename’ in the echo area. If visit is neither t nor nil nor a string, then this message is inhibited. This feature is useful for programs that use files for internal purposes, files that the user does not need to know about.


Next: , Previous: , Up: Files   [Contents][Index]