简体   繁体   English

emacs-仅在主要模式下设置快捷键?

[英]emacs - set shortcut key only in major mode?

I would like to override Cl and use it to do Mx erase-buffer followed by simulating hitting RET , only when I am in m-shell-mode . 我想覆盖Cl并使用它做Mx erase-buffer然后仅当我处于m-shell-mode时,模拟RET击中。 Cl should be its default, recenter-top-bottom , otherwise. Cl应该是默认值,否则为recenter-top-bottom How do I do so? 我该怎么做?

Not sure what m-shell-mode is, but if it's a well-defined major mode , then the following should do the trick: 不知道什么是m-shell-mode ,但是如果它是定义明确的主模式 ,那么以下方法可以解决问题:

(require 'm-shell-mode)
(define-key m-shell-mode-map (kbd "C-l") 'erase-buffer)

Might I suggest an alternative binding, which has the same visual effect, but keeps the buffer contents around (which can be handy). 可能我建议使用替代绑定,该绑定具有相同的视觉效果,但保留缓冲区内容(可能很方便)。

(defun shell-clear-command (&optional a)
  "\"clear\" the screen"
  (interactive "P")
  (recenter (or a 0)))
(define-key m-shell-mode-map (kbd "C-l") 'shell-clear-command)

If m-shell-mode is based on comint-mode , which is true of many modes that provide a shell to interact with another process, then you can pass the return keypress to matlab with the function comint-send-input . 如果m-shell-mode是基于comint-mode ,这是许多模式,提供一个外壳来与另一个进程交互的真实,那么你可以通过return按键与功能的MATLAB的comint-send-input In that case the following code should do what you want: 在这种情况下,以下代码应按您的要求执行:

(defun clear-and-return () 
  "Erases the buffer, and then passes a return to the buffer process.
Assumes the buffer is attached to a comint process."
  (interactive)
  (erase-buffer) 
  (comint-send-input))

(defun my-m-shell-mode-hook ()
  (local-set-key (kbd "C-l") 'clear-and-return))

(add-hook 'm-shell-mode-hook 'my-m-shell-mode-hook)

The first defun makes a function that does what you want. 第一个defun会执行您想要的功能。 The second is a hook function that will bind Cl to that function for the buffer that is active when the function is called. 第二个是钩子函数,该钩子函数会将Cl绑定到该函数,以使该函数在调用该函数时处于活动状态。 The add-hook tells emacs to run the second function whenever you start m-shell-mode . 每当您启动m-shell-mode时, add-hook告诉emacs运行第二个功能。 You can add further m-shell-mode customizations inside the body of my-m-shell-mode , and Emacs will run all of them each time you start the mode. 您可以在my-m-shell-mode主体中添加其他m-shell-mode定制,并且每次启动模式时Emacs都会运行所有定制。

If m-shell-mode is not based on comint-mode , you need to find out what happens when you press return . 如果m-shell-mode不是基于comint-mode ,则需要找出在按return时会发生什么。 From a buffer that is running the mode, type Ch k RET to find the function bound to the return key. 从运行该模式的缓冲区中,键入Ch k RET以查找绑定到返回键的函数。 Use that function instead of comint-send-input in the code above. 使用该函数代替上面的代码中的comint-send-input

您可以将以下代码添加到m-shell-mode挂钩:

(local-set-key (kbd "C-l") 'erase-buffer)

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

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