13

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?

3 Answers 3

3

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'
}
1
  • 1
    Could this question have an update for emacs 24? The function python-calculate-indentation no longer exists -- the closest replacement appears to be python-indent-calculate-indentation, but the semantics of how this function operates appears to have changed.
    – barik
    Aug 2, 2013 at 17:10
1

Try with the last fgallina's python.el version. It contains many other improvements.

I use this version and TAB has the behavior that you want, but I made several modifications to python.el, so I can't be sure that you'll get the same behavior. If so, Let me know.

1
  • Unfortunately this doesn't work quite as I had described. After auto-indenting, continually pressing TAB causes the line to scroll through negative (towards the margin) indentation levels. Additionally, if I manipulate an indentation level manually, subsequent lines do not stay on the same level. The good news is that I can review the python.el source and see if I can figure out how things work and potentially figure out how to modify it myself.
    – brildum
    Feb 24, 2011 at 15:01
-1

M-x customize-group RET python RET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.