简体   繁体   English

如何使用python-mode在Emacs中缩进代码?

[英]How to indent code in Emacs with python-mode?

I am running Emacs with python-mode.el for coding in Python. 我使用python-mode.el运行Emacs以便在Python中进行编码。 I hope to learn how to make a region of code indented well automatically. 我希望学习如何使代码区域自动缩进。

Following code is not indented well. 以下代码没有缩进。

while match != None:

        if match.group(1):
            titles.append(match.group(1))

        if match.group(2):
            if match.group(2) != '':
                pns.append(int(match.group(2)))
            else:
                pns.append('')
        else:
            pns.append('')

        if match.group(3):
            closings.append(len(''.join(match.group(3).split())))
        else:
            closings.append(0)

    match = pat.search(match.group(4))

If I select the region, and hit Mx indent-region , it becomes totally wrong: 如果我选择区域,并点击Mx indent-region ,则完全错误:

while match != None:

    if match.group(1):
        titles.append(match.group(1))

        if match.group(2):
            if match.group(2) != '':
                pns.append(int(match.group(2)))
            else:
                pns.append('')
        else:
            pns.append('')

            if match.group(3):
                closings.append(len(''.join(match.group(3).split())))
            else:
                closings.append(0)

                match = pat.search(match.group(4))

The ideal should be: 理想应该是:

while match != None:

    if match.group(1):
        titles.append(match.group(1))

    if match.group(2):
        if match.group(2) != '':
            pns.append(int(match.group(2)))
        else:
            pns.append('')
    else:
        pns.append('')

    if match.group(3):
        closings.append(len(''.join(match.group(3).split())))
    else:
        closings.append(0)

    match = pat.search(match.group(4))
  1. Why does Mx indent-region incorrectly understand the indent relation between lines of code? 为什么Mx indent-region错误地理解了代码行之间的缩进关系? Is it because my code is ambiguous? 是因为我的代码含糊不清吗?
  2. What should I do then? 那我该怎么办?

Thanks. 谢谢。

The problem is that emacs has no way of knowing where you want an if-block to end. 问题是emacs无法知道你想要if块的结束位置。 Both your desired code and the code indent-region produces are valid python. 您所需的代码和代码缩进区域生成都是有效的python。 In C-like languages this isn't a problem due to the braces deciding the length of blocks. 在类C语言中,由于大括号决定了块的长度,因此这不是问题。 For python, since emacs can't know for sure it assumes each line of code is still part of the previous block. 对于python,因为emacs无法确定它假设每行代码仍然是前一个块的一部分。

You might want to look at python-indent-left (bound to "Cc <") and python-indent-right ("Cc >"). 您可能希望查看python-indent-left(绑定到“Cc <”)和python-indent-right(“Cc>”)。 To fix your example, you'd highlight everything apart from the first line, and run python-indent-left. 要修复你的例子,你要突出显示除第一行之外的所有内容,然后运行python-indent-left。

As said, in Python you can't auto-indent larger sections reliably. 如上所述,在Python中,您无法可靠地自动缩进较大的部分。

However, there is a way to speed up doing it line by line. 但是,有一种方法可以加快逐行进行。 This is in use here: 这在这里使用:

(defun indent-and-forward ()
  "Indent current line and go forward one line. "
  (interactive "*")
  (if (empty-line-p)
      (fixup-whitespace)
      (indent-according-to-mode))
  (if (eobp)
      (newline-and-indent)
    (forward-line 1))
  (back-to-indentation))

BTW it should work with other modes too, not just Python. 顺便说一句,它也应该与其他模式一起使用,而不仅仅是Python。 Keys here are 这里的钥匙是

(global-set-key [(super i)] 'indent-and-forward)

This keys pressed, you may travel large parts - just keep an eye it still does what you want. 按下这个键,你可以旅行大部分 - 只要留意它仍然做你想要的。 If not - use TAB-key for just this line and proceed with the next one. 如果不是 - 只使用TAB键作为此行并继续下一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM