2

考虑到我的*scratch*缓冲区也是现有文件,有人可以帮我在打开 Emacs 时从桌面保存切换到上次查看的文件吗?

就初始缓冲区选择而言,*scratch*缓冲区始终胜过桌面保存。

(setq initial-scratch-message nil)
;; (setq initial-buffer-choice "~/.0.data/.0.emacs/*scratch*")

(defun kill-default-scratch ()
    "Avoid having a buffer named `*scratch*<2>` when Emacs loads
    my preferred *scratch* file from a specified location -- however,
    yield to the last viewed file if it exists from desktop-save."
    (kill-buffer "*scratch*")
    (find-file "~/.0.data/.0.emacs/*scratch*") )

(add-hook 'after-init-hook 'kill-default-scratch)

(require 'saveplace)
(setq-default save-place t)
(setq save-place-file "~/.0.data/.0.emacs/.saved-places")

(desktop-save-mode 1)
(setq desktop-dirname           "~/.0.data/.0.emacs/"
    desktop-base-file-name      ".emacs.desktop"
    desktop-base-lock-name      ".lock"
    desktop-path                (list desktop-dirname)
    desktop-save                t
    desktop-files-not-to-save   "[*]bbdb[*]\\|[*]BBDB[*]\\|[*]TODO[*]" ;; "^$"  reload tramp paths
    desktop-load-locked-desktop nil )

(setq desktop-buffers-not-to-save
        (concat "\\("
                "^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
                "\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble"
                "\\)$"))
    (add-to-list 'desktop-modes-not-to-save 'dired-mode)
    (add-to-list 'desktop-modes-not-to-save 'Info-mode)
    (add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
    (add-to-list 'desktop-modes-not-to-save 'fundamental-mode)

编辑:根据@juanleon 提供的答案,我决定让大自然顺其自然并使用emacs-startup-hook(在 之后加载after-init-hook)来处理它。临时缓冲区想要胜过desktop.el,所以让它然后掩埋它。为了处理没有最后查看的文件保存的情况desktop.el(即,因为它们在退出之前都已关闭),我将*Messages*缓冲区(if它有焦点)埋起来,结果是自定义*scratch*缓冲区再次获得焦点。为了从保存功能中删除自定义*scratch*文件,我添加到. 我用来自动保存我的自定义desktop.el[*]scratch[*]desktop-files-not-to-saveauto-save-buffers-enhanced*scratch*文件(设置为在每次修改后 1 秒发生)并且该实用程序允许我排除用户定义的文件类型(setq auto-save-buffers-enhanced-exclude-regexps . . .)https ://github.com/kentaro/auto-save-buffers-enhanced/blob/master/auto-save- buffers-enhanced.el [注意:如果使用最新版本的 Emacs ,则flet需要将其中 的引用更改为。]cl-flet

(setq initial-scratch-message nil)
(setq initial-buffer-choice t)

(require 'auto-save-buffers-enhanced)
(auto-save-buffers-enhanced t)
(setq auto-save-buffers-enhanced-save-scratch-buffer-to-file-p 1)
(setq auto-save-buffers-enhanced-exclude-regexps '("^not-save-file" "\\.ignore$" "\\.txt" "[*]TODO[*]" "\\.yasnippet" "\\.tex" "\\user_pref" "\\.org_archive" "\\.org" "\\.ppet"))

(require 'saveplace)
(setq-default save-place t)
(setq save-place-file "~/.0.data/.0.emacs/.saved-places")

(desktop-save-mode 1) ;; uses the after-init-hook
(setq desktop-dirname           "~/.0.data/.0.emacs/"
    desktop-base-file-name      ".emacs.desktop"
    desktop-base-lock-name      ".lock"
    desktop-path                (list desktop-dirname)
    desktop-save                t
    desktop-files-not-to-save   "[*]scratch[*]\\|[*]bbdb[*]\\|[*]BBDB[*]\\|[*]TODO[*]" ;; "^$"  reload tramp paths
    desktop-load-locked-desktop nil )
(setq desktop-buffers-not-to-save
        (concat "\\("
                "^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
                "\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble"
                "\\)$"))
    (add-to-list 'desktop-modes-not-to-save 'dired-mode)
    (add-to-list 'desktop-modes-not-to-save 'Info-mode)
    (add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
    (add-to-list 'desktop-modes-not-to-save 'fundamental-mode)

(defun custom-scratch-setup ()
"Avoid having a buffer named `*scratch*<2>` when Emacs loads
my preferred *scratch* file from a specified location -- however,
yield to the last viewed file if it exists from desktop-save."
    (kill-buffer "*scratch*")
    (message "The default `*scratch*` buffer has been killed.")
    (find-file "~/.0.data/.0.emacs/*scratch*")
    (message "Finished loading the custom `*scratch*` file.")
    (bury-buffer)
    (message "The buffer *scratch* has been buried.")
    (if (eq (current-buffer) 
            (get-buffer "*Messages*") )
        (progn    (bury-buffer)
        (message "The buffer *Messages* has been buried."))) )
(add-hook 'emacs-startup-hook 'custom-scratch-setup)
4

1 回答 1

2

我的猜测是after-init-hook在桌面完成加载它的东西之后运行(桌面使用那个钩子)

因此,find-file将在一切结束时运行,并且find-file如果已经加载,则恰好“取消”文件的缓冲区。如果您不希望它接管初始缓冲区选择,也不是初始缓冲区选择,您可以(bury-buffer)在末尾添加 a 。kill-default-scratch

另一种选择是交换钩子after-init-hook的运行顺序。您可以通过使用“需要”desktop.el 和/或使用 arg APPEND 来做到这一点add-hook。如果您的功能在桌面功能之前运行,(desktop-read)将优先于您的find-file

于 2013-07-10T07:42:06.923 回答