2011-02-23 39 views
13

我一直在使用emacs 23(python.el)一个多月,我对默认的自动缩进设置感到不满。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') 

看起来,我想如何执行自动缩进的逻辑是:

如果上一行的最后一个字符(非注释/空白)为a,则增加t他缩进1级。 否则,使用相同的缩进级别。

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

有谁知道我如何修改emacs自动缩进以实现我想要的样式?

回答

1

尝试使用最后的fgallina的python.el版本。它包含许多其他improvements

我使用这个版本,TAB有你想要的行为,但我对python.el做了一些修改,所以我不能确定你会得到相同的行为。如果是这样,请告诉我。

+0

不幸的是,这并不像我所描述的那样工作。自动缩进之后,不断按下“TAB”会使该线滚动通过负值(朝向边距)缩进级别。此外,如果我手动操作缩进级别,后续行不会保持在同一级别。好消息是我可以查看python.el源代码,看看我能否弄清楚事情是如何工作的,并且可能会自己弄清楚如何修改它。 – brildum 2011-02-24 15:01:36

-1

的Mx定制组RET 蟒蛇RET

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字典是这样的:

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

成为.. 。

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

这个问题可以更新emacs 24吗?函数'python-calculate-indentation'不再存在 - 最接近的替换似乎是'python-indent-calculate-indentation',但是这个函数的操作语义似乎已经改变了。 – barik 2013-08-02 17:10:59

相关问题