2012-01-13 111 views

回答

12

auto-save-mode實際適用於非文件緩衝區。它只是默認不啓用 - 通常發生在(after-find-file)

所以:的Mxauto-save-modeRET

默認情況下自動保存的文件將被寫入緩衝區的default-directory(或/var/tmp~/,這取決於寫權限;看到vbuffer-auto-save-file-nameRET)這可能是一個有點尷尬弄清楚崩潰後,所以設置爲標準的東西可能是一個好主意。

下將確保這些自動保存的文件被寫入到你的主目錄(或M-Xcustomize-variableRETmy-non-file-buffer-auto-save-dirRET),如果auto-save-mode交互式的調用。這將有希望避免與具有非文件緩衝區的auto-save-mode的任何其他用途相沖突(例如代碼提及郵件模式)。

(defcustom my-non-file-buffer-auto-save-dir (expand-file-name "~/") 
    "Directory in which to store auto-save files for non-file buffers, 
when `auto-save-mode' is invoked manually.") 

(defadvice auto-save-mode (around use-my-non-file-buffer-auto-save-dir) 
    "Use a standard location for auto-save files for non-file buffers" 
    (if (and (not buffer-file-name) 
      (called-interactively-p 'any)) 
     (let ((default-directory my-non-file-buffer-auto-save-dir)) 
     ad-do-it) 
    ad-do-it)) 
(ad-activate 'auto-save-mode) 
+0

偉大的信息。自動保存模式有點神祕,因爲它非常不顯眼(這是件好事)。我使用make-auto-save-file-name來控制緩衝區的保存位置,而不是defadvice。 – 2012-01-13 22:15:40

5

phils' answer清除了我的東西,但我最終使用了一種不同的方法。爲了文檔的目的,我將它作爲單獨的答案發布。這是我的自動保存節:

;; Put autosave files (ie #foo#) in one place 
(defvar autosave-dir (concat "~/.emacs.d/autosave.1")) 
(defvar autosave-dir-nonfile (concat "~/.emacs.d/autosave.nonfile")) 
(make-directory autosave-dir t) 
(make-directory autosave-dir-nonfile t) 
(defun auto-save-file-name-p (filename) (string-match "^#.*#$" (file-name-nondirectory filename))) 
(defun make-auto-save-file-name() 
    (if buffer-file-name (concat autosave-dir "/" "#" (file-name-nondirectory buffer-file-name) "#") 
    (expand-file-name (concat autosave-dir-nonfile "/" "#%" 
           (replace-regexp-in-string "[*]\\|/" "" (buffer-name)) "#")))) 

在此上下文中爲非訪問文件緩衝區創建單獨的目錄是可選的;他們也可以進入中央位置(在這種情況下,autosave-dir)。還要注意,如果臨時緩衝區名稱是「* foo/bar *」(帶有星號和/或斜槓),我必須進行一些基本的文件名清理。

最後,可以自動打開,使用一些特定的模式中臨時緩衝區自動保存像

(add-hook 'org2blog/wp-mode-hook '(lambda() (auto-save-mode t))) 
相關問題