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

G.A. gafStopSpamData at ziplink.stopallspam.net
Tue Nov 11 13:55:54 EST 2003


On Mon, 10 Nov 2003 00:41:35 GMT, G.A.
<gafStopSpamData at ziplink.stopallspam.net> wrote:

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




More information about the Python-list mailing list