如何实现另存为并且保留原文件的功能?


      (write-file (concat "~/Documents/" (format-time-string "%m-%d") "-" name ".gist" ))))

用write-file会把原来的buffer换成新的buffer,而我只是想备份原来的buffer,还是要继续编辑的。

怎么实现这个备份功能?

有时候要备份一下 Message / Backtrace / 各种 Reports

我猜你要的是write-region

write-region is an interactive built-in function in ‘C source code’.

(write-region START END FILENAME &optional APPEND VISIT LOCKNAME MUSTBENEW)

Write current region into specified file. When called from a program, requires three arguments: START, END and FILENAME. START and END are normally buffer positions specifying the part of the buffer to write. If START is nil, that means to use the entire buffer contents; END is ignored. If START is a string, then output that string to the file instead of any buffer contents; END is ignored.

Optional fourth argument APPEND if non-nil means append to existing file contents (if any). If it is a number, seek to that offset in the file before writing. Optional fifth argument VISIT, if t or a string, means set the last-save-file-modtime of buffer to this file’s modtime and mark buffer not modified. If VISIT is a string, it is a second file name; the output goes to FILENAME, but the buffer is marked as visiting VISIT. VISIT is also the file name to lock and unlock for clash detection. If VISIT is neither t nor nil nor a string, or if Emacs is in batch mode, do not display the “Wrote file” message. The optional sixth arg LOCKNAME, if non-nil, specifies the name to use for locking and unlocking, overriding FILENAME and VISIT. The optional seventh arg MUSTBENEW, if non-nil, insists on a check for an existing file with the same name. If MUSTBENEW is ‘excl’, that means to get an error if the file already exists; never overwrite. If MUSTBENEW is neither nil nor ‘excl’, that means ask for confirmation before overwriting, but do go ahead and overwrite the file if the user confirms.

This does code conversion according to the value of ‘coding-system-for-write’, ‘buffer-file-coding-system’, or ‘file-coding-system-alist’, and sets the variable ‘last-coding-system-used’ to the coding system actually used.

This calls ‘write-region-annotate-functions’ at the start, and ‘write-region-post-annotation-function’ at the end.

1 个赞