在 Emacs 中,键盘转义退出不破坏其他窗口的最佳方法是什么?

发布于 2024-07-13 14:24:27 字数 1450 浏览 7 评论 0原文

编辑:我知道有键盘退出(通常绑定到 Cg); 但我更感兴趣的是如何处理 Emacs 附带的编辑功能(就像本例一样)。 当我想稍微改变一些内置函数时,我时常会遇到这种情况。

在 emacs 中,当你按 M-ESC ESC(或 ESC 三次)时,你可以摆脱很多情况,例如瞬态标记等。但我习惯性地按退出键(实际上我将其重新映射为单击转义键)超出了我的预期,这最终会杀死我的 Windows 配置,这非常烦人。 函数 Keyboard-Escape-quit 在 simple.el 中定义:

(defun keyboard-escape-quit ()
  "Exit the current \"mode\" (in a generalized sense of the word).
This command can exit an interactive command such as `query-replace',
can clear out a prefix argument or a region,
can get out of the minibuffer or other recursive edit,
cancel the use of the current buffer (for special-purpose buffers),
or go back to just one window (by deleting all but the selected window)."
  (interactive)
  (cond ((eq last-command 'mode-exited) nil)
    ((> (minibuffer-depth) 0)
     (abort-recursive-edit))
    (current-prefix-arg
     nil)
    ((and transient-mark-mode mark-active)
     (deactivate-mark))
    ((> (recursion-depth) 0)
     (exit-recursive-edit))
    (buffer-quit-function
     (funcall buffer-quit-function))
    ((not (one-window-p t))
     (delete-other-windows))
    ((string-match "^ \\*" (buffer-name (current-buffer)))
     (bury-buffer))))

我可以看到我不想要这些行:

    ((not (one-window-p t))
     (delete-other-windows))

但是修改此函数的最佳方法是什么? 我只能看到两种方法:1)修改 simple.el 2)将此函数复制到我的 .emacs 文件并在那里进行修改。 这两种方式都不是很好。 理想情况下,我希望看到一些关于默认通知的内容,但我不知道在这种情况下如何做到这一点。

EDIT: I understand there is keyboard-quit (which is normally bound to C-g); but I'm more interested to know about how one deals with editing functions that come with Emacs (like in this case). I run into this kind of situations from time to time when I want to change just a little bit of some build-in functions.

In emacs, when you hit M-ESC ESC (or ESC three times), you can get out of a lots of situations like transient-mark, etc. But I habitually hit the escape key (I actually remap this to a single hit of the escape key) more than I intended, and that ends up killing my windows configuration, which is quite annoying. The function keyboard-escape-quit is defined in simple.el:

(defun keyboard-escape-quit ()
  "Exit the current \"mode\" (in a generalized sense of the word).
This command can exit an interactive command such as `query-replace',
can clear out a prefix argument or a region,
can get out of the minibuffer or other recursive edit,
cancel the use of the current buffer (for special-purpose buffers),
or go back to just one window (by deleting all but the selected window)."
  (interactive)
  (cond ((eq last-command 'mode-exited) nil)
    ((> (minibuffer-depth) 0)
     (abort-recursive-edit))
    (current-prefix-arg
     nil)
    ((and transient-mark-mode mark-active)
     (deactivate-mark))
    ((> (recursion-depth) 0)
     (exit-recursive-edit))
    (buffer-quit-function
     (funcall buffer-quit-function))
    ((not (one-window-p t))
     (delete-other-windows))
    ((string-match "^ \\*" (buffer-name (current-buffer)))
     (bury-buffer))))

And I can see that I don't want the lines:

    ((not (one-window-p t))
     (delete-other-windows))

But what is the best way to modify this function? I can see only two ways: 1) modify simple.el 2) copy this function to my .emacs file and do the modifications there. Both ways are not really good; ideally I would like to see something on the line of defadvice, but I can't see how I can do it in this case.

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

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

发布评论

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

评论(6

无言温柔 2024-07-20 14:24:27

您可以使用 around 建议并重新定义有问题的函数来执行您想要的操作(即 one-window-p 应该始终返回 t):

(defadvice keyboard-escape-quit (around my-keyboard-escape-quit activate)
  (let (orig-one-window-p)
    (fset 'orig-one-window-p (symbol-function 'one-window-p))
    (fset 'one-window-p (lambda (&optional nomini all-frames) t))
    (unwind-protect
        ad-do-it
      (fset 'one-window-p (symbol-function 'orig-one-window-p)))))

这种行为类似于 (let ...) 但必须更复杂,因为您需要在有限范围内覆盖函数而不是变量。

You could use around advice and redefine the offending function to do what you want (i.e. one-window-p should always return t):

(defadvice keyboard-escape-quit (around my-keyboard-escape-quit activate)
  (let (orig-one-window-p)
    (fset 'orig-one-window-p (symbol-function 'one-window-p))
    (fset 'one-window-p (lambda (&optional nomini all-frames) t))
    (unwind-protect
        ad-do-it
      (fset 'one-window-p (symbol-function 'orig-one-window-p)))))

This kind of acts like a (let ...) but has to be more complicated because you need to override a function for a limited scope instead of a variable.

苏大泽ㄣ 2024-07-20 14:24:27

我通常发现“键盘退出”(Cg)可以帮助摆脱所有这些情况。

但是,如果您确实想要此函数的变体,我认为复制到 .emacs 文件(并重命名,我通常使用 bp 前缀)并在那里进行编辑可能是最好的选择。

编辑,响应编辑:一般来说,每当我想要 emacs 函数的编辑版本时,我要么自己编写,要么将其复制到我的 .emacs,将其重命名为 bp-whotever,然后进行适当的编辑。

这样做的缺点是我的 .emacs 太大了,而且对于不再使用的古老函数来说可能会非常糟糕......优点是每当我需要编写新的东西时,我都有大量的示例代码可以查看...

I usually find that 'keyboard-quit (C-g) works to get out of all of those situations.

However, if you really want to have a variant of this function, I think that copying to your .emacs file (and renaming, I usually usa a prefix of bp) and making the edits there is probably the best option.

EDIT, in response to edit: In general, whenever I want an edited version of an emacs function, I either write it myself, or copy it to my .emacs, rename it bp-whotever and then do appropriate edits.

The downside of this is that my .emacs is HUGE, and probably extra-crufty with ancient functions that are nolonger used... the upside is that whenever I need to write something new, I've got tons of sample code to look at...

波浪屿的海角声 2024-07-20 14:24:27

这是另一个更简单的建议,它利用了 keyboard-escape-quit 在关闭窗口之前调用 buffer-quit-function 的事实:

(defadvice keyboard-escape-quit
  (around keyboard-escape-quit-dont-close-windows activate)
  (let ((buffer-quit-function (lambda () ())))
    ad-do-it))

适用于 Emacs 25.1。 (我最初使用了 @scottfrazer 的建议,但在 25.1 中不太满意。还没有费心调试。)

Here's another, simpler piece of advice that takes advantage of the fact that keyboard-escape-quit calls buffer-quit-function before closing windows:

(defadvice keyboard-escape-quit
  (around keyboard-escape-quit-dont-close-windows activate)
  (let ((buffer-quit-function (lambda () ())))
    ad-do-it))

Works with Emacs 25.1. (I originally used @scottfrazer's advice, but it's unhappy in 25.1. Haven't bothered debugging yet.)

灵芸 2024-07-20 14:24:27

默认情况下,按一次 Escape 键可充当 Meta 前缀键; 也就是说,涉及 Meta 键的键绑定。

按三次 Escape 键将运行键盘退出退出,这类似于键盘退出,但更多的是“按我的意思做”行为。

此代码可能对您的用例有所帮助。 您可以在 Emacs 初始化文件中使用它:

;;; esc always quits
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
(global-set-key [escape] 'keyboard-quit)

A single press of the Escape key, by default, acts as a Meta prefix key; that is, a keybinding which involves the Meta key.

Triple-pressing the Escape key will run keyboard-escape-quit, which is like keyboard-quit but with more of a "do what I mean" behaviour.

This code may help with your use case. You can use this in your Emacs init file:

;;; esc always quits
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
(global-set-key [escape] 'keyboard-quit)
圈圈圆圆圈圈 2024-07-20 14:24:27

我更感兴趣的是如何处理 Emacs 附带的编辑功能(就像本例一样)。 当我想稍微改变一些内置函数时,我时常会遇到这种情况。

这正是我创建库 el-patch 的目的。 您可以将其放入您的初始化文件中:

(el-patch-defun keyboard-escape-quit ()
  "Exit the current \"mode\" (in a generalized sense of the word).
This command can exit an interactive command such as `query-replace',
can clear out a prefix argument or a region,
can get out of the minibuffer or other recursive edit,
cancel the use of the current buffer (for special-purpose buffers),
or go back to just one window (by deleting all but the selected window)."
  (interactive)
  (cond ((eq last-command 'mode-exited) nil)
    ((> (minibuffer-depth) 0)
     (abort-recursive-edit))
    (current-prefix-arg
     nil)
    ((and transient-mark-mode mark-active)
     (deactivate-mark))
    ((> (recursion-depth) 0)
     (exit-recursive-edit))
    (buffer-quit-function
     (funcall buffer-quit-function))
    (el-patch-remove
      ((not (one-window-p t))
       (delete-other-windows)))
    ((string-match "^ \\*" (buffer-name (current-buffer)))
     (bury-buffer))))

I'm more interested to know about how one deals with editing functions that come with Emacs (like in this case). I run into this kind of situations from time to time when I want to change just a little bit of some build-in functions.

This is exactly the purpose for which I created the library el-patch. You would put this in your init-file:

(el-patch-defun keyboard-escape-quit ()
  "Exit the current \"mode\" (in a generalized sense of the word).
This command can exit an interactive command such as `query-replace',
can clear out a prefix argument or a region,
can get out of the minibuffer or other recursive edit,
cancel the use of the current buffer (for special-purpose buffers),
or go back to just one window (by deleting all but the selected window)."
  (interactive)
  (cond ((eq last-command 'mode-exited) nil)
    ((> (minibuffer-depth) 0)
     (abort-recursive-edit))
    (current-prefix-arg
     nil)
    ((and transient-mark-mode mark-active)
     (deactivate-mark))
    ((> (recursion-depth) 0)
     (exit-recursive-edit))
    (buffer-quit-function
     (funcall buffer-quit-function))
    (el-patch-remove
      ((not (one-window-p t))
       (delete-other-windows)))
    ((string-match "^ \\*" (buffer-name (current-buffer)))
     (bury-buffer))))
昵称有卵用 2024-07-20 14:24:27

这是使用 cl-lib 而不是 cl 的新方法,该方法现已弃用:

;; Make it so keyboard-escape-quit doesn't delete-other-windows
  (defadvice keyboard-escape-quit
      (around keyboard-escape-quit-dont-delete-other-windows activate)
    (cl-letf (((symbol-function 'delete-other-windows)
               (lambda () nil)))
      ad-do-it))

您需要确保在此之前您已调用:

(require 'cl-lib)

Here's a new way using cl-lib instead of cl which is now deprecated:

;; Make it so keyboard-escape-quit doesn't delete-other-windows
  (defadvice keyboard-escape-quit
      (around keyboard-escape-quit-dont-delete-other-windows activate)
    (cl-letf (((symbol-function 'delete-other-windows)
               (lambda () nil)))
      ad-do-it))

You'll need to make sure prior to it you have called:

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