【问题标题】:emacs `python-shell-send-defun` skips first line in bufferemacs `python-shell-send-defun` 跳过缓冲区中的第一行
【发布时间】:2018-03-14 20:08:28
【问题描述】:

在内置 python 模式下,C-M-x,又名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 主要模式中的相应命令在光标之前发送整个多行 defun 时正常工作)。这里发生了什么?我在 Mac OS 中的 emacs 25 gui 版本和 Ubuntu 中的 emacs 24 中都对此进行了测试。两者都显示相同的错误。

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

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

【问题讨论】:

    标签: python emacs


    【解决方案1】:

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

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

    【讨论】:

    • 我已经向 emacs 邮件列表提交了错误报告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多