emacs 'python-shell-send-defun' 跳过缓冲区中的第一行



C-M-x中似乎有一个相当严重的错误,也就是 在内置的 python 模式下python-shell-send-defun

  1. 启动 Emacs
  2. 创建一个新文件,我们称之为test.py
  3. C-c C-prun-python
  4. test.py缓冲区中,键入一些简单的函数,例如

    def f(x):
    return x+1
    
  5. C-M-xpython-shell-send-defun

我们得到:

return x+1
SyntaxError: 'return' outside function

所以这里发生的事情是python-shell-send-defun只发送一行,而不是我们期望的两行(例如,emacs lisp major模式下的相应命令在光标之前发送整个多行defun时可以正常工作)。这是怎么回事?我在Mac OS的emacs 25 gui版本和Ubuntu的emacs 24中都对此进行了测试。两者都表现出相同的错误。

这是一个错误,还是我误解了python-shell-send-defun实际上的作用?

更新:通过一些进一步的测试,此错误似乎只发生在从第一行开始的函数上。如果我们在def f(x):正上方插入一个空行,C-M-x完美地工作。

是的,这看起来像一个错误,应该报告为一个错误。该函数可能应该重写为进行检查,如下所示

(defun python-shell-send-defun (&optional arg msg)
"Send the current defun to inferior Python process.
When argument ARG is non-nil do not include decorators.  When
optional argument MSG is non-nil, forces display of a
user-friendly message if there's no process running; defaults to
t when called interactively."
(interactive (list current-prefix-arg t))
(save-excursion
(python-shell-send-region
(progn
(end-of-line 1)
(while (and (or (python-nav-beginning-of-defun)
(beginning-of-line 1))
(> (current-indentation) 0)))
(when (not arg)
(while (and (forward-line -1)
(looking-at (python-rx decorator))))
;; add a check here to see if we are on the first line
(and (not (bobp)) (forward-line 1)))
(point-marker))
(progn
(or (python-nav-end-of-defun)
(end-of-line 1))
(point-marker))
nil  ;; noop
msg)))

最新更新