emacs 23 python.el auto-indent style - 可以配置吗? [英] emacs 23 python.el auto-indent style -- can this be configured?

查看:194
本文介绍了emacs 23 python.el auto-indent style - 可以配置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  x = a_function_with_dict_parameter({
'test':'一个值',
'second':'另一个值',
})
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':'这是一个值',
'second':'另一个值',
})
a_function_with_multiline_parameters(on ='First',line = 'line',
now_on ='Second',next_line ='Line',next ='Third',finally ='Line')

似乎我想让自动缩进执行的逻辑是:



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



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



有人知道我可以如何修改emacs自动缩进以实现我想要的风格?

解决方案

b

 (require'python)

;缩进
(defadvice python-calculate-indentation(around outdent-closing-brackets)
处理以一个结束括号开头的行,并缩进它们,以便
他们与包含相应开始括号。
(save-excursion
(行首)
(let((syntax(syntax-ppss) eq'string(syntax-ppss-context syntax)))
(python-continuation-line-p)
(cadr语法)
$ b(查找\\s)))
(progn
(forward-char 1)
(ignore-errors(backward-sexp))
(set-ad-return-value(current-indentation)))
ad-do-it)))

(ad-activate'python-calculate-indentation)

现在,一个简单的python像这样:

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

成为...

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


I have been using emacs 23 (python.el) for just over a month now and I'm unhappy with the default auto-indentation settings.

Currently, my Python files are auto-indented as follows:

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')

I would prefer if I could set the auto-indentation settings so the same code could easily be formatted:

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')

It seems that the logic for how I'd like the auto-indentation to perform would be:

If the last character (non-comment/whitespace) of the previous line is a :, increase the indent-level by 1. Else, use the same indentation level.

But using that logic, TAB would need to actually increase the indent-level of the current line. (Currently, TAB only moves the line to the auto-indent level)

Does anyone know how I can modify emacs auto-indentation to achieve my desired style?

解决方案

You can try this peace of code :

(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)

Now, a simple python dict like this :

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

becomes...

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

这篇关于emacs 23 python.el auto-indent style - 可以配置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆