【问题标题】:asynchronously evaluating whole python or perl buffer异步评估整个 python 或 perl 缓冲区
【发布时间】:2014-10-25 05:13:14
【问题描述】:

我已经编写了一些附加到我的 F5 和 F6 键的函数,以便在按下时,这些函数会评估 perl 或 python 的缓冲区内容。

;; F5 = perlevaluatie
(defun perl-eval ()
  "Run whole buffer as Perl code"
  (interactive)
  (shell-command-on-region (point-min) (point-max) "perl") ; feeds the region to perl on STDIN
)
(global-set-key (kbd "<f5>") 'perl-eval)

;; F6 = pythonevaluatie
(defun python-eval ()
  "Run whole buffer as Python code"
  (interactive)
  (shell-command-on-region (point-min) (point-max) "python")
)
(global-set-key (kbd "<f6>") 'python-eval)

但是,当我将这些函数用于长时间运行的脚本时,emacs 会挂起。与 shell-command 函数一样,附加 & 并没有帮助。有人知道使shell-command-on-region 异步的方法吗?

提前致谢,

【问题讨论】:

  • 你看quickrun了吗?我发现在 emacs 上测试我的脚本非常方便。
  • 您不能在 emacs 24.3.1 中异步使用 shell-command-on-region。在那里,shell 仅使用同步的命令call-process-region 调用。要拥有异步命令,您需要start-process 和观察启动进程执行的哨兵。一个有趣的问题是:如果在脚本运行期间修改了输入区域会发生什么?

标签: python perl emacs elisp


【解决方案1】:

我为此使用compileM-x compileperl &lt;file-name&gt;。第一次运行后,您可以使用 recompile 重新运行它。

键入文件名的时间稍长,但您会获得很多好东西,而且它当然是异步的。

编辑:第一次自动使用编译然后重新编译的奖励辅助功能:

(defun my-recompile ()
  "Recompile if possible, otherwise compile current buffer."
  (interactive)
  ;; If recompile exists do it, else compile
  (if (fboundp 'recompile) (recompile)
    (compile "make -k")))

并在适当的钩子中绑定类似的东西

(local-set-key (kbd "<f5>") 'my-recompile)
(local-set-key (kbd "<C-f5>") 'compile)

【讨论】:

  • 我不明白? Perl 或 python 是脚本语言,那么为什么要编译它们呢?不能在不将缓冲区保存到文件的情况下运行它们吗?
  • @MartenBE compile 可能是一个误导性的名称:它可用于运行任何 shell 命令。你必须保存它,但我从来没有发现这是一个问题(因为你可以设置compile 自动保存缓冲区)。
猜你喜欢
  • 1970-01-01
  • 2011-12-11
  • 2014-09-07
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2015-12-04
相关资源
最近更新 更多