【问题标题】:elisp: call command on current fileelisp:在当前文件上调用命令
【发布时间】:2011-01-15 00:31:36
【问题描述】:

我想在 emacs 中设置一个键来对缓冲区中的文件执行 shell 命令,并在不提示的情况下恢复缓冲区。 shell命令为:p4 edit 'currentfilename.ext'

(global-set-key [\C-E] (funcall 'revert-buffer 1 1 1)) 
;; my attempt above to call revert-buffer with a non-nil 
;; argument (ignoring the shell command for now) -- get an init error:
;; Error in init file: error: "Buffer does not seem to be associated with any file"

对 elisp 来说是全新的。来自emacs manual,这里是revert-buffer的定义:

Command: revert-buffer &optional ignore-auto noconfirm preserve-modes

谢谢!

【问题讨论】:

  • 你想恢复缓冲区是因为它已经被编辑过,还是因为 shell 命令破坏了缓冲区的内容?
  • 因为它将缓冲区更改为写入而不是只读,并将其添加到版本控制中。

标签: emacs elisp


【解决方案1】:

您看到的实际错误是因为您错误地指定了全局设置键,即函数调用。你想要的是:

(global-set-key (kbd "C-S-e") '(lambda () (revert-buffer t t t)))

你有 funcall 实际评估你的 .emacs 何时加载,这是导致错误的原因。

然后,要获得全部内容,您可以创建如下命令:

(defun call-something-on-current-buffers-file ()
  "run a command on the current file and revert the buffer"
  (interactive)
  (shell-command 
   (format "/home/tjackson/bin/dummy.sh %s" 
       (shell-quote-argument (buffer-file-name))))
  (revert-buffer t t t))
(global-set-key (kbd "C-S-e") 'call-something-on-current-buffers-file)

显然自定义命令,并根据需要添加错误检查。

【讨论】:

  • 你需要以某种方式引用 shell 的文件名......comint-quote-filename 看起来 就像它完成了这项工作,但不幸的是,它只有在你已经在一个外壳缓冲区。就我所知。
  • @Zack comint-quote-filename 通话已添加。
  • 感谢您的回复。我得到:Symbol's function definition is void: comint-quote-filename。我用shell-quote-argument 替换了它,效果很好。旁注:emacs 手册指出:“要避免的键:[Ctrl]+[Shift]+[letter]。在文本终端中,它无法区分这种组合的移位和非移位版本。如果您总是在 GUI 中使用 emacs,则效果很好环境。”于是我把它改成了,它就认出了钥匙。
  • @Jasie 我知道必须有一个内置的引用,我已经更新了答案以使用shell-quote-argument。 (要修复 void 函数定义错误,您可以添加(require 'comint)),但shell-quote-argument 更好。
  • @TreyJackson 这很好,如果我需要在可以通过按 'q' 关闭的窗口中弹出输出,我需要做什么?
【解决方案2】:

也许使用次要模式“auto-revert-mode”是一种选择。 只需在当前缓冲区上启用它:

M-x "auto-revert-mode"

并始终确保在执行外部命令之前保存缓冲区。

【讨论】:

  • 这很好,但是默认的自动恢复模式时间是 5 秒(太慢了)。然后,如果我将其更改为 1 秒,它会每秒检查每个缓冲区,这是不必要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多