如果之前保存了桌面,则从* scratch *切换到上次查看的文件

时间:2013-07-10 01:16:27

标签: emacs

考虑到我的*scratch*缓冲区也是现有文件,是否有人可以帮我切换到桌面上最后查看的文件保存?考虑到我的*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) 缓冲区总是胜过桌面保存的初始缓冲区选择。

emacs-startup-hook

编辑:基于@juanleon提供的答案,我决定让大自然继续运行并使用after-init-hook处理它(在{{1}之后加载}})。暂存缓冲区想要胜过desktop.el,所以让它然后埋葬它。为了处理desktop.el没有保存最后查看文件的情况(即因为它们在退出前全部关闭),我埋葬*Messages*缓冲区(if它有焦点)结果是自定义*scratch*缓冲区再次成为焦点。要从*scratch*保存功能中删除自定义desktop.el文件,我将[*]scratch[*]添加到desktop-files-not-to-save列表中。我使用auto-save-buffers-enhanced自动保存我的自定义*scratch*文件(设置为每次修改后发生1秒),该实用程序允许我用(setq auto-save-buffers-enhanced-exclude-regexps . . .)排除用户定义的文件类型:{{3 }注意:如果使用最新版本的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)

{{1}}

1 个答案:

答案 0 :(得分:2)

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

因此,find-file将在所有内容结束时运行,find-file恰好“unbury”文件的缓冲区(如果已加载)。如果您不希望它接管初始缓冲区选择,也可以在(bury-buffer)的末尾添加kill-default-scratch,也不是初始缓冲区选择。

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