(py-no-outdent-re): new constant

(py-indent-line, py-electric-colon): watch for compound statements one
line after another.
This commit is contained in:
Barry Warsaw 1995-03-14 20:53:08 +00:00
parent b5e0ecbd33
commit 4f009fb092
1 changed files with 39 additions and 18 deletions

View File

@ -310,13 +310,25 @@ Currently-active file is at the head of the list.")
(defconst py-outdent-re (defconst py-outdent-re
(concat "\\(" (mapconcat 'identity (concat "\\(" (mapconcat 'identity
'("else:" '("else:"
"except\\s +.*:" "except\\(\\s +.*\\)?:"
"finally:" "finally:"
"elif\\s +.*:") "elif\\s +.*:")
"\\|") "\\|")
"\\)") "\\)")
"Regexp matching clauses to be outdented one level.") "Regexp matching clauses to be outdented one level.")
(defconst py-no-outdent-re
(concat "\\(" (mapconcat 'identity
'("try\\s +.*:"
"except\\(\\s +.*\\)?:"
"while\\s +.*:"
"for\\s +.*:"
"if\\s +.*:"
"elif\\s +.*:")
"\\|")
"\\)")
"Regexp matching lines to not outdent after.")
;;;###autoload ;;;###autoload
(defun python-mode () (defun python-mode ()
@ -397,20 +409,27 @@ In certain cases the line is outdented appropriately. If a numeric
argument is provided, that many colons are inserted non-electrically." argument is provided, that many colons are inserted non-electrically."
(interactive "P") (interactive "P")
(self-insert-command (prefix-numeric-value arg)) (self-insert-command (prefix-numeric-value arg))
(let (this-indent) (save-excursion
(if (and (not arg) (let ((here (point))
(save-excursion (outdent 0)
(back-to-indentation) (indent (py-compute-indentation)))
(looking-at py-outdent-re)) (if (and (not arg)
(= (setq this-indent (py-compute-indentation)) (progn
(save-excursion (back-to-indentation)
(forward-line -1) (looking-at py-outdent-re))
(py-compute-indentation))) (prog2
) (backward-to-indentation 1)
(save-excursion (not (looking-at py-no-outdent-re))
(beginning-of-line) (goto-char here))
(delete-horizontal-space) (= indent (progn
(indent-to (- this-indent py-indent-offset))) (forward-line -1)
(py-compute-indentation)))
)
(setq outdent py-indent-offset))
(goto-char here)
(beginning-of-line)
(delete-horizontal-space)
(indent-to (- indent outdent))
))) )))
@ -630,10 +649,12 @@ needed so that only a single column position is deleted."
(let* ((ci (current-indentation)) (let* ((ci (current-indentation))
(move-to-indentation-p (<= (current-column) ci)) (move-to-indentation-p (<= (current-column) ci))
(need (py-compute-indentation))) (need (py-compute-indentation)))
;; watch for outdents ;; see if we need to outdent
(if (save-excursion (if (save-excursion
(back-to-indentation) (and (progn (back-to-indentation)
(looking-at py-outdent-re)) (looking-at py-outdent-re))
(progn (backward-to-indentation 1)
(not (looking-at py-no-outdent-re)))))
(setq need (- need py-indent-offset))) (setq need (- need py-indent-offset)))
(if (/= ci need) (if (/= ci need)
(save-excursion (save-excursion