【问题标题】:Emacs 24.3 python: Can't guess python-indent-offset, using defaults 4Emacs 24.3 python:无法猜测 python-indent-offset,使用默认值 4
【发布时间】:2013-09-17 16:52:47
【问题描述】:

任何了解 Lisp 的人都可以帮忙解决这个警告吗?

我升级到 Emacs 24.3,每当我使用 Emacs 创建 Python 文件时,都会收到此警告消息。在python.el 中搜索并找到以下产生警告的代码部分:

(let ((indentation (when block-end
                     (goto-char block-end)
                     (python-util-forward-comment)
                     (current-indentation))))
  (if indentation
      (set (make-local-variable 'python-indent-offset) indentation)
    (message "Can't guess python-indent-offset, using defaults: %s"
             python-indent-offset)))

这是我的.emacs 设置:

(setq-default c-basic-offset   4
              tab-width        4
              indent-tabs-mode nil)

(add-hook 'c-mode-common-hook
          (lambda ()
            (c-set-offset 'arglist-intro '+)
            (c-set-offset 'arglist-close 0)))

(add-hook 'python-mode-hook
          (lambda ()
            (c-set-offset 'arglist-intro '+)
            (c-set-offset 'arglist-close 0)))

【问题讨论】:

  • 不要引用你的 lambda,它充其量是没用的。

标签: python python-2.7 emacs elisp indentation


【解决方案1】:

除了前面的答案和消除警告之外,此过程还处理问题并提供两个示例说明警告为何有用。

使用C-x C-b 获取当前打开的缓冲区列表。用 Python 过滤那些 扩展名,在它们周围添加引号,并在此命令中替换它们:

(dolist (f '(
    "/path/to/file1"
    "/path/to/file2"
    ))
  (message (concat "Opening file: " f))
  (find-file (expand-file-name f)))

执行命令(选择区域,然后选择M-x evaluate-region)。然后*Messages* 看起来像这样:

打开文件:/path/to/file1 打开文件:/path/to/file2 无法猜测 python-indent-offset,使用默认值:4 有关 GNU Emacs 和 GNU 系统的信息,请键入 C-h C-a。

我有一些用于测试但忘记的文件,例如:

from time import sleep

sleep(10000)

多亏了这条消息,我才意识到这一点,我删除了文件。

在另一种情况下,我有一个只有全局变量的有用文件,所以我添加了这个 行:

# -*- mode: python; python-indent-offset: 4 -*-

SOME_GLOBAL_VAR = 0

虽然这确实设置了缓冲区局部变量(通过更改为 6 进行检查,然后 使用C-h v python-indent-offset) 检查值,它只在 警告信息,而不是之前。我决定容忍这个消息。

【讨论】:

    【解决方案2】:

    如果您只想使警告静音,同时让 emacs 仍然按照 juanleon 的回答解释的那样猜测偏移量,您可以关闭 python-indent-guess-indent-offset-verbose 变量。

    (setq python-indent-guess-indent-offset t)  
    (setq python-indent-guess-indent-offset-verbose nil)
    

    每个 this comprehensive answer 在 emacs SE 上。

    【讨论】:

      【解决方案3】:

      当您打开一个 python 文件时,emacs 会根据该文件样式猜测缩进偏移量(要缩进的空格数)。当您创建文件(您描述的情况)时,emacs 无法猜测(文件为空),因此它使用您的默认值 (4) 并通知用户。

      换句话说:tt是一个无害的警告;如果您发现这是一个错误,请直接报告。

      如果您不喜欢 emacs 猜测偏移量,请将变量 python-indent-guess-indent-offset 自定义为 nil,然后 emacs 将始终使用您的默认值(在 python 中非常不安全,其中缩进有意义,您可能正在编辑由其他默认值的人)。

      【讨论】:

      • 你说得对,这很不安全。但是,有时将整个文件缩进更改为默认缩进非常有用。你如何以安全的方式做到这一点?
      • @esilva,我会使用 autopep8 (pypi.python.org/pypi/autopep8) 或者 reindent (pypi.python.org/pypi/Reindent/0.1.0),只要我的默认值符合 PEP8
      • 如果您不喜欢 emacs 告诉您它猜测偏移量,请将变量 python-indent-guess-indent-offset-verbose 自定义为 nil。
      • 当文件为空时可以说是一个错误 - 新文件的默认值是正确的,几乎按照定义。围绕此代码添加对 (< (point-min) (point-max)) 的测试相当容易 - 我建议您 (@bohani) 尝试一下,然后提交补丁!
      【解决方案4】:

      查看python.el,而不是python.elc。如果你没有python.el,那么用谷歌搜索它(至少看看它——你不需要它来使用 Emacs)。 *.elc 是一个字节编译的文件,人类几乎无法理解。

      python.el 中的源代码将告诉您 python-indent-guess-indent-offset 的作用,以及为什么您会看到所看到的结果。

      【讨论】:

      • (let ((indentation (当block-end (goto-char block-end) (python-util-forward-comment) (current-indentation)))) (if indentation (set (make -local-variable 'python-indent-offset) 缩进)(消息“无法猜测 python-indent-offset,使用默认值:%s”python-indent-offset)))))))
      • 从 python.el 得到了上面的快照,你能看一下吗?我不太懂lisp
      • 请参阅@juanleon 的回答,它很好地解释了事情。代码查找当前缩进作为指导,但如果文件为空则没有指导,因此它使用默认值 4。此消息只是提供信息;这不是一个警告。
      猜你喜欢
      • 2013-03-29
      • 2014-03-02
      • 2022-06-18
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多