【问题标题】:Changing Python indentation amount in Emacs在 Emacs 中更改 Python 缩进量
【发布时间】:2016-10-02 19:15:15
【问题描述】:

因为我有

(setq python-indent-offset 4)

在我的 .emacs 中,当我开始使用诸如

之类的代码时
def fn():
  foo = bar()
  if bar1 == bar2:
    if blah1 == blah2:
      return yay

和mark-whole-buffer (C-x h) 然后是缩进区域(C-M-\),我期望得到:

def fn():
    foo = bar()
    if bar1 == bar2:
        if blah1 == blah2:
            return yay

但我得到了:

def fn():
    foo = bar()
  if bar1 == bar2:
    if blah1 == blah2:
      return yay

但是 indent-region 可以很好地与花括号分隔的语言一起使用。有没有办法让它与 Python 等基于缩进的语言一起工作?

请注意:

  1. 我是 awareReindent,但我想留在 Emacs。
  2. 我对 string-rectangle (C-x r t) and python-indent-shift-right/left (C-c >) 很熟悉,但这些及其变体都不能满足我的要求。

【问题讨论】:

  • 问题似乎是在python-indent-region 中,检查该行是否在块开始。如果它在块开始(例如 if 语句中的 if),它将跳过该行并且不更改缩进。你应该把它作为一个错误提出来。

标签: python emacs


【解决方案1】:

我不确定这是否是一个完美的解决方案,因此我不想将其作为 emacs-devel 的补丁推送,但由于问题出在 python-indent-region 中(我在评论中留下了关于此的注释)。我们可以尝试重新定义该函数:

(defun python-indent-region (start end)
  "Indent a Python region automagically.

Called from a program, START and END specify the region to indent."
  (let ((deactivate-mark nil))
    (save-excursion
      (goto-char end)
      (setq end (point-marker))
      (goto-char start)
      (or (bolp) (forward-line 1))
      (while (< (point) end)
        (or (and (bolp) (eolp))
            (when (and
                   ;; Skip if previous line is empty or a comment.
                   (save-excursion
                     (let ((line-is-comment-p
                            (python-info-current-line-comment-p)))
                       (forward-line -1)
                       (not
                        (or (and (python-info-current-line-comment-p)
                                 ;; Unless this line is a comment too.
                                 (not line-is-comment-p))
                            (python-info-current-line-empty-p)))))
                   ;; Don't mess with strings, unless it's the
                   ;; enclosing set of quotes or a docstring.
                   (or (not (python-syntax-context 'string))
                       (eq
                        (syntax-after
                         (+ (1- (point))
                            (current-indentation)
                            (python-syntax-count-quotes (char-after) (point))))
                        (string-to-syntax "|"))
                       (python-info-docstring-p)))
              (python-indent-line)))
        (forward-line 1))
      (move-marker end nil))))

我更改的部分只是删除检查以查看该行是否是块 ender、denter 或块启动,并在所有这些情况下运行 (python-indent-line)

我已经在这个示例和其他示例中对此进行了测试,它们似乎可以正常工作。如果它不起作用,我很想知道!

【讨论】:

  • Hmm.. 在 eval-region 之后,重新启动 Emacs,并使用另一个(出血边缘)版本的 Emacs,在 M-x 之后无法识别函数名称 python-indent-region。调查。我也试过改这个名字。
  • 它不应该通过 m-x 可见,它应该由 C-M-\ 调用,尽管当您使用该命令时
【解决方案2】:

或者,您可以从 emacs 中调用 python 包并让它替换缓冲区。

(defun my-py-reindent ()
         "Reindent the current file with pypi's reindent."
         (interactive)
           (shell-command-to-string (format "reindent %s" (buffer-file-name)))
           (revert-buffer t t t))

很简单! (使用的文档:http://wikemacs.org/wiki/Emacs_Lisp_Cheat_Sheet#File

【讨论】:

    猜你喜欢
    • 2012-09-30
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多