简体   繁体   English

Emacs自动保存在交换机缓冲区上

[英]Emacs auto-save on switch buffer

Call me lame, but I'm tired of my subconscious Cx Cs nervous twitch. 叫我跛脚,但我厌倦了我潜意识的Cx Cs紧张的抽搐。 I am switching buffers often enough and I think I would like to save a certain buffer as soon as I switch to another. 我经常切换缓冲区,我想我一旦切换到另一个就想保存一个缓冲区。 I have not had the time yet to learn Emacs-Lisp basics. 我还没来得及学习Emacs-Lisp的基础知识。

Any hints on how to do this, or better solutions? 有关如何做到这一点或更好的解决方案的任何提示?

(On a related note, I found an autosave workaround that can save the current buffer as soon as you are idle for a given amount of time.) (在相关的说明中,我发现了一种自动保存解决方法 ,可以在空闲一段时间后立即保存当前缓冲区。)

To expand on Seth 's answer , I'd do this: 为了扩展赛斯答案 ,我会这样做:

(defadvice switch-to-buffer (before save-buffer-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-window (before other-window-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-frame (before other-frame-now activate)
  (when buffer-file-name (save-buffer)))

The check for buffer-file-name avoids saving buffers w/out files. 检查buffer-file-name可以避免保存缓冲区w / out文件。 You need to figure out all the entry points you use for switching buffers that you care about (I'd also advise other-window ). 您需要找出用于切换您关心的缓冲区的所有入口点(我还建议other-window )。

I'm kind of new to emacs lisp myself but this works in my testing: 我自己也是emacs lisp的新手,但这在我的测试中有效:

(defadvice switch-to-buffer (before save-buffer-now)
  (save-buffer))

(ad-activate 'switch-to-buffer)

It's kind of annoying though because it's called after EVERY buffer (like scratch ). 这有点令人讨厌,因为它是在每个缓冲区(如临时 )之后调用的。 So, consider this answer a hint. 所以,请考虑这个答案。

When you want to disable it, you'll need to call: 如果要禁用它,则需要调用:

(ad-disable-advice 'switch-to-buffer 'before 'save-buffer-now)
(ad-activate 'switch-to-buffer)

A couple of ideas. 一些想法。

First, if you find yourself invoking a command like save with a sufficiently high frequency, you might consider a shorter key binding for the command. 首先,如果您发现自己调用了具有足够高频率的保存命令,则可以考虑使用较短的命令键绑定。 For example, I also found myself having the same "twitch," so now I use f2 instead of Cx Cs for saving edits. 例如,我也发现自己有相同的“抽搐”,所以现在我使用f2而不是Cx Cs来保存编辑。

The function that I bind to f2 saves every unsaved buffer unconditionally. 我绑定到f2的函数无条件地保存每个未保存的缓冲区。 You might find it useful: 您可能会发现它很有用:

(defun force-save-all ()
    "Unconditionally saves all unsaved buffers."
    (interactive)
    (save-some-buffers t))

(global-set-key [f2] 'force-save-all)

Now, on to the main issue. 现在,关于主要问题。 You could try something like this (notice that force-save-all is called): 你可以试试这样的东西(注意force-save-all被调用):

(defun my-switch-to-buffer (buffer)
    (interactive (list (read-buffer "Switch to buffer: " (cadr buffer-name-history) nil)))
    (force-save-all)
    (switch-to-buffer buffer))

(global-set-key "\C-xb" 'my-switch-to-buffer)

Of course, you could also bind the switch buffer functionality to another key, like a function key, so that it's a one press operation. 当然,您也可以将交换缓冲区功能绑定到另一个键,如功能键,这样就可以进行一次按键操作。

I thought that @seth had a great idea about using advice, but I noticed that the ELisp manual suggests that advice not be used for key bindings . 我认为@seth对使用建议有很好的想法,但我注意到ELisp手册表明建议不能用于键绑定 I'm not quite sure why this is the case, but that's what the manual suggests FYI. 我不太确定为什么会出现这种情况,但这就是手册建议FYI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM