Emacs:在缓冲区评估期间将参数传递给劣质的Python shell [英] Emacs: pass arguments to inferior Python shell during buffer evaluation
问题描述
这似乎并不容易;由 python.el
模块管理的劣质进程旨在持续存在 python-send-buffer
(和朋友)。我找到的一个解决方案是编写自己的功能,可以从下面的过程中以程序方式设置 sys.argv
:
< pre $($($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (current-buffer))
(with-temp-buffer
(insertimport sys; sys.argv ='''args'''.split()\\\
)
(insert-buffer-substring source-buffer)
(python-send-buffer))))
在您的 * scratch *
缓冲区中执行此功能,并将其保存在您的 .emacs
文件,然后,如果你想,绑定到一个方便的键序列。 Cc Ca
似乎不被 python-mode
使用,所以也许:
(全局设置键\Cc\Ca'python-send-buffer-with-my-args)
该命令将提示您使用参数,然后将源缓冲区复制到临时缓冲区,并使用代码片段将 sys.argv
设置为您提供的参数列表,最后将调用 python-send-buffer
。 p>
上面的代码将天真地分割你在空格上键入的字符串,所以如果你需要提供有空格的参数,你需要一个更复杂的算法。 / p>
recently I started using Emacs as a Python IDE, and it not quite intuitive... The problem I am struggling with right now is how to pass command line arguments to the inferior python shell when the buffer is evaluated with C-c C-c. Thanks for help.
This doesn't appear to be easily possible; the inferior process managed by the python.el
module is designed to persist across many invocations of python-send-buffer
(and friends). One solution I've found is to write your own function that sets sys.argv
programmatically from within the inferior process:
(defun python-send-buffer-with-my-args (args)
(interactive "sPython arguments: ")
(let ((source-buffer (current-buffer)))
(with-temp-buffer
(insert "import sys; sys.argv = '''" args "'''.split()\n")
(insert-buffer-substring source-buffer)
(python-send-buffer))))
Execute this function in your *scratch*
buffer and/or save it in your .emacs
file, then, if you want, bind it to a convenient key sequence. C-c C-a
doesn't seem to be used by python-mode
, so perhaps:
(global-set-key "\C-c\C-a" 'python-send-buffer-with-my-args)
The command will prompt you for arguments to use, then copy your source buffer into a temporary buffer, prepending it with a code snippet that sets sys.argv
to the list of arguments you supplied, and finally will call python-send-buffer
.
The above code will just naively split the string you type on whitespace, so if you need to supply arguments that have whitespace in them, you'll need a more sophisticated algorithm.
这篇关于Emacs:在缓冲区评估期间将参数传递给劣质的Python shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!