首页 文章

emacs 23 python.el auto-indent style - 可以配置吗?

提问于
浏览
13

我一直在使用emacs 23(python.el)一个多月了,我对默认的自动缩进设置不满意 .

目前,我的Python文件是自动缩进的,如下所示:

x = a_function_with_dict_parameter({
                                   'test' : 'Here is a value',
                                   'second' : 'Another value',
                                   })
a_function_with_multiline_parameters(on='First', line='Line',
                                     now_on='Second', next_line='Line',
                                     next='Third', finally='Line')

我更喜欢如果我可以设置自动缩进设置,以便可以轻松格式化相同的代码:

x = a_function_with_dict_parameter({
    'test' : 'Here is a value',
    'second' : 'Another value',
})
a_function_with_multiline_parameters(on='First', line='Line',
    now_on='Second', next_line='Line', next='Third', finally='Line')

似乎我喜欢自动缩进的逻辑是:

如果前一行的最后一个字符(非注释/空白)是:,则将缩进级别增加1.否则,使用相同的缩进级别 .

但是使用该逻辑, TAB 将需要实际增加当前行的缩进级别 . (目前, TAB 仅将行移动到自动缩进级别)

有谁知道如何修改emacs auto-indentation以实现我想要的风格?

3 回答

  • 3

    你可以尝试这种代码安静:

    (require 'python)
    
    ; indentation
    (defadvice python-calculate-indentation (around outdent-closing-brackets)
      "Handle lines beginning with a closing bracket and indent them so that
      they line up with the line containing the corresponding opening bracket."
    (save-excursion
      (beginning-of-line)
      (let ((syntax (syntax-ppss)))
        (if (and (not (eq 'string (syntax-ppss-context syntax)))
                 (python-continuation-line-p)
                 (cadr syntax)
                 (skip-syntax-forward "-")
                 (looking-at "\\s)"))
            (progn
              (forward-char 1)
              (ignore-errors (backward-sexp))
              (setq ad-return-value (current-indentation)))
          ad-do-it))))
    
    (ad-activate 'python-calculate-indentation)
    

    现在,这是一个简单的python dict:

    a = {'foo': 'bar',
         'foobar': 'barfoo'
        }
    

    成为...

    a = {'foo': 'bar',
         'foobar': 'barfoo'
    }
    
  • 1

    试试最后一个fgallina的python.el版本 . 它包含许多其他improvements .

    我使用这个版本, TAB 有你想要的行为,但我对python.el做了几处修改,所以我可以't be sure that you' ll得到相同的行为 . 如果是这样,请告诉我 .

  • -1

    M-x customize-group RET python RET

相关问题