Re: python-mode.el

Sjoerd Mullender (Sjoerd.Mullender@cwi.nl)
Thu, 14 Apr 1994 12:31:12 +0200

On Wed, Apr 13 1994 Donald Beaudry wrote:

> I just made a small change to python-mode.el to change the way that
> continuation lines are handled. By replacing the existing
> py-compute-indentation function with the one that follows,
> continuation lines will be indented to just inside of the paren that
> contains the expression (just like in c-mode). If you are not nested
> inside of parens, indentation is computed as before. The variable
> py-nested-indent (also defined below) can be used to enable and
> disable this behavior.

I made a small fix to this. The problem was that if the open
parenthesis is on a line with tabs the indentation of the following
line was not correct. Replace with the following version.
BTW, Guido changed the official python-mode.el to incorporate this
feature.

(defvar py-nested-indent t
"*If non-nil, indent nested continuation lines to inside the opening paren")

(defun py-compute-indentation ()
(save-excursion
(beginning-of-line)
(cond
;; are we on a continuation line?
( (py-continuation-line-p)
(let ((nest (and py-nested-indent (py-nesting-level))))
(if nest
(save-excursion
(goto-char nest)
(1+ (current-column)))
(forward-line -1)
(if (py-continuation-line-p) ; on at least 3rd line in block
(current-indentation) ; so just continue the pattern
;; else on 2nd line in block, so indent more
(+ (current-indentation) py-indent-offset
py-continuation-offset)))))
;; not on a continuation line

;; if at start of restriction, or on a non-indenting comment line,
;; assume they intended whatever's there
( (or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
(current-indentation) )

;; else indentation based on that of the statement that precedes
;; us; use the first line of that statement to establish the base,
;; in case the user forced a non-std indentation for the
;; continuation lines (if any)
( t
;; skip back over blank & non-indenting comment lines
;; note: will skip a blank or non-indenting comment line that
;; happens to be a continuation line too
(re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
nil 'move)
(py-goto-initial-line)
(if (py-statement-opens-block-p)
(+ (current-indentation) py-indent-offset)
(current-indentation))))))

Sjoerd Mullender, CWI, P.O. Box 94079, 1090 GB Amsterdam, The Netherlands
E-Mail: Sjoerd.Mullender@cwi.nl; Phone: +31 20 592 4127; Fax: +31 20 592 4199
URL: <http://www.cwi.nl/cwi/people/Sjoerd.Mullender.html>