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

时间:2018-03-14 20:08:29

标签: python emacs

内置python模式中C-M-x,a.k.a。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-x python-shell-send-defun

  6. 我们得到:

        return x+1
    SyntaxError: 'return' outside function
    

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

    这是一个错误,还是我误解了python-shell-send-defun实际上做了什么?

    更新:通过一些进一步的测试,似乎只有在第一行开始的函数才会出现此错误。如果我们在def f(x):上方插入一个空行,C-M-x就可以正常工作。

1 个答案:

答案 0 :(得分:0)

是的,这看起来像一个bug,应该报告为一个。应该重写该函数以进行如下检查

(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)))