在emacs缓冲区中写入评估python命令的结果

时间:2014-10-26 05:31:58

标签: python emacs

如何从emacs缓冲区评估一行Python代码并将结果写入我光标处的缓冲区?我想在haskell-mode中执行类似C-u C-c C-t的操作,您可以在其中键入1 + 2,并在光标处插入光标处的任何类型定义。

所以,如果我选择并发送了一个像

这样的地区

.py

3 1 + 2缓冲区到Python下级进程,我会看到

.py

在我的3缓冲区中,即。后面插入了{{1}}后跟换行符。

2 个答案:

答案 0 :(得分:1)

org-mode,即org-babel提供类似的东西,似乎值得测试。

python-mode.el的当前主干

https://launchpad.net/python-mode

提供选项py-store-result-p

t时,结果可以在杀戮戒指的汽车中访问 - 下一个插入它。

您可以定义一个读取(car kill-ring)以插入的命令。

此外,功能请求应该有一些机会。

答案 1 :(得分:1)

要在C-u M-!shell-command)的点处插入shell命令的结果,因此您可以使用python -c "print 1 + 2"插入python命令的结果。

要获取当前行需要一些lisp。这是一个非常简单的例子。

(defun my-py-eval-insert-current-line ()
     "Eval the current line and insert its result."
     (interactive)    
     (setq cmd (read-from-minibuffer "Python cmd: " (format "python -c 'print %s'" (current-line))))
     (setq res (shell-command-to-string cmd))
     (insert res)
     )

(defun current-line ()
"returns the current line."
;; http://ergoemacs.org/emacs/elisp_all_about_lines.html
     (let (p1 p2 myLine)
       (setq p1 (line-beginning-position) )
       (setq p2 (line-end-position) )
       (setq myLine (buffer-substring-no-properties p1 p2))
       ))

我更愿意看看pymacs是否有严重的事情。

ps:elisp函数:http://wikemacs.org/wiki/Emacs_Lisp_Cheat_Sheet