Emacs python mode equivalent to c-tab-always-indent

G

G.A.

Does anyone have a modification to python-mode.el that implements something
along the lines of c-tab-always-indent? Currently, it a) gets the
indentation wrong inside of """ module strings; and b) reindents the line
if I try to use tabs inside subsequent lines of the """ comment string.

I can live with the first problem but not the second. With c/c++ my
solution is to set c-tab-always-indent to nil, which changes the behavior
so that it only reindents the line if the cursor is in the leading
whitespace, and otherwise it inserts a real tab (though a virtual tab is
fine, too).

Thanks,

Gary
 
G

G.A.

On Mon, 10 Nov 2003 00:41:35 GMT, G.A.

Rather than waiting around for an answer, I went ahead and made my
modifications to python-mode.el. While I've tweaked emacs lisp stuff in
the past, I've never been confidant about it, given the complexity of many
elisp packages. So now the question is whether they're good enough to put
back into the official version, and if so, how would I do that?

For what it's worth, I'm including the code below. It's relatively small,
a definition for py-tab-always-indent and a few lines changed in
py-indent-line.

Gary

(defcustom py-tab-always-indent t
"*Non-nil means TAB in Python mode should always reindent the current
line, regardless of where in the line point is when the TAB command is
used."
:type 'boolean
:group 'python)

(defun py-indent-line (&optional arg)
"Fix the indentation of the current line according to Python rules.
With \\[universal-argument] (programmatically, the optional argument
ARG non-nil), ignore dedenting rules for block closing statements
(e.g. return, raise, break, continue, pass)

This function is normally bound to `indent-line-function' so
\\[indent-for-tab-command] will call it."
(interactive "P")
(let* ((ci (current-indentation))
(move-to-indentation-p (<= (current-column) ci))
(need (py-compute-indentation (not arg))))
;; see if we need to dedent
(if (py-outdent-p)
(setq need (- need py-indent-offset)))
(if (or py-tab-always-indent
move-to-indentation-p)
(progn (if (/= ci need)
(save-excursion
(beginning-of-line)
(delete-horizontal-space)
(indent-to need)))
(if move-to-indentation-p (back-to-indentation)))
(insert-tab))))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top