如何在 Emacs 中失去焦点时自动保存文件

发布于 2024-07-30 07:55:10 字数 43 浏览 5 评论 0原文

是否可以配置Emacs,以便在emacs窗口丢失时保存所有文件 重点?

Is it possible to configure Emacs, so that it saves all files when the emacs window loses
focus?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝礼 2024-08-06 07:55:11

我向 Gnu Emacs 24.4 添加了焦点钩子。

它们被称为focus-in-hookfocus-out-hook

您可以添加

(defun save-all ()
  (interactive)
  (save-some-buffers t))

(add-hook 'focus-out-hook 'save-all)

到 .emacs 文件中,它应该在失去焦点时保存所有文件。

I added focus hooks to Gnu Emacs 24.4.

They are called focus-in-hook and focus-out-hook.

You can add

(defun save-all ()
  (interactive)
  (save-some-buffers t))

(add-hook 'focus-out-hook 'save-all)

to your .emacs file and it should save all files on loss of focus.

花伊自在美 2024-08-06 07:55:11

我使用这个,只有当 emacs 在 X 下运行时它才会工作(就像在 ubuntu 中一样)。

(when
   (and (featurep 'x) window-system)
 (defvar on-blur--saved-window-id 0 "Last known focused window.")
 (defvar on-blur--timer nil "Timer refreshing known focused window.")
 (defun on-blur--refresh ()
   "Runs on-blur-hook if emacs has lost focus."
   (let* ((active-window (x-window-property
                          "_NET_ACTIVE_WINDOW" nil "WINDOW" 0 nil t))
          (active-window-id (if (numberp active-window)
                                active-window
                              (string-to-number
                               (format "%x00%x"
                                       (car active-window)
                                       (cdr active-window)) 16)))
          (emacs-window-id (string-to-number
                            (frame-parameter nil 'outer-window-id))))
     (when (and
            (= emacs-window-id on-blur--saved-window-id)
            (not (= active-window-id on-blur--saved-window-id)))
       (run-hooks 'on-blur-hook))
     (setq on-blur--saved-window-id active-window-id)
     (run-with-timer 1 nil 'on-blur--refresh)))
 (add-hook 'on-blur-hook #'(lambda () (save-some-buffers t)))
 (on-blur--refresh))

I use this, it will only work if emacs is running under X (like it probably would in something like ubuntu).

(when
   (and (featurep 'x) window-system)
 (defvar on-blur--saved-window-id 0 "Last known focused window.")
 (defvar on-blur--timer nil "Timer refreshing known focused window.")
 (defun on-blur--refresh ()
   "Runs on-blur-hook if emacs has lost focus."
   (let* ((active-window (x-window-property
                          "_NET_ACTIVE_WINDOW" nil "WINDOW" 0 nil t))
          (active-window-id (if (numberp active-window)
                                active-window
                              (string-to-number
                               (format "%x00%x"
                                       (car active-window)
                                       (cdr active-window)) 16)))
          (emacs-window-id (string-to-number
                            (frame-parameter nil 'outer-window-id))))
     (when (and
            (= emacs-window-id on-blur--saved-window-id)
            (not (= active-window-id on-blur--saved-window-id)))
       (run-hooks 'on-blur-hook))
     (setq on-blur--saved-window-id active-window-id)
     (run-with-timer 1 nil 'on-blur--refresh)))
 (add-hook 'on-blur-hook #'(lambda () (save-some-buffers t)))
 (on-blur--refresh))
始终不够爱げ你 2024-08-06 07:55:11

不确定这是否是您想要的。

(defun dld-deselect-frame-hook ()
  (save-some-buffers 1))

(add-hook 'deselect-frame-hook 'dld-deselect-frame-hook)

来自: http://www.dribin.org/dave /blog/archives/2003/09/10/emacs/

编辑:它似乎只在 XEmacs 中工作

Not sure if this is what you want.

(defun dld-deselect-frame-hook ()
  (save-some-buffers 1))

(add-hook 'deselect-frame-hook 'dld-deselect-frame-hook)

From: http://www.dribin.org/dave/blog/archives/2003/09/10/emacs/

EDIT: It only seems to work in XEmacs

两人的回忆 2024-08-06 07:55:11

[...]我正在谈论的功能来自
文士。 当
编辑 html 等,你不需要
必须再按 Cx Cs 了,你
只需更改窗口并检查您的
浏览器。

在这种情况下,不要切换到浏览器应用程序,而是命令 Emacs 加载浏览器应用程序(Cc CvMx browser-url-of-buffer)。 使用此方法,您可以编写自己的函数来保存缓冲区,然后启动浏览器,例如:

(defun my-browse-url-of-buffer ()
  "Save current buffer and view its content in browser."
  (interactive)
  (save-buffer)
  (browse-url-of-buffer))

并将其挂钩到方便的绑定。

或者,您仍然可以使用 html-autoview-mode,每次保存缓冲区时,都会自动将文件加载到您喜欢的浏览器中。

[…] the feature I am talking about is from
Scribes. It is very convient when
editing html and the like, you don't
have to press C-x C-s anymore, you
just change the window and check your
browser.

In that case, instead of switching to the browser application, order Emacs to load the browser application (C-c C-v or M-x browse-url-of-buffer). With this method, you can write your own function that saves the buffer and then brings the browser up, like:

(defun my-browse-url-of-buffer ()
  "Save current buffer and view its content in browser."
  (interactive)
  (save-buffer)
  (browse-url-of-buffer))

And hook it to a convenient binding.

Or you can still use the html-autoview-mode that each time you saves the buffer, automatically loads the file into your favorite browser.

夏有森光若流苏 2024-08-06 07:55:11

您可以使用“自动保存间隔”来保存您输入的每 n 个字符。 我的设置为 100。那么大约每 2-3 行代码吧?

自动保存间隔是一个变量
在“C 源代码”中定义。 它的价值
是 100

文档:
*自动保存之间的输入事件数。 零表示禁用
由于字符数自动保存
已输入。

您可以自定义此变量。

这并没有回答你原来的问题; 这只是实现类似目标的一种方法。

You can use `auto-save-interval' to save every n characters you type. Mine is set to 100. So about every 2-3 lines of code, maybe?

auto-save-interval is a variable
defined in `C source code'. Its value
is 100

Documentation:
*Number of input events between auto-saves. Zero means disable
autosaving due to number of characters
typed.

You can customize this variable.

This doesn't answer your original question; it's just a way to achieve something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文