Skip to main content
 首页 » 编程设计

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

2024年10月25日10lyj

如何计算来自 emacs 缓冲区的一行 Python 代码并将结果写入光标处的缓冲区?我想在 haskell-mode 中做一些类似的事情,你可以在其中键入 C-u C-c C-t 并且你光标处的任何类型定义都会插入到你的光标处。

所以,如果我选择并发送了一个区域,例如

1 + 2

从 .py 缓冲区到我将看到的 Python 劣质进程

3 1 + 2

在我的 .py 缓冲区中,即。 3 后跟一个换行符被插入点。

请您参考如下方法:

要在点插入 shell 命令的结果,我们有 C-u M-!(shell-command),因此您可以插入 python 命令的结果python -c "打印 1 + 2"。

要使用当前行需要一点口齿不清。这是一个非常简单的例子。

(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