summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>1995-03-14 20:53:08 (GMT)
committerBarry Warsaw <barry@python.org>1995-03-14 20:53:08 (GMT)
commit4f009fb092aceb7bb9d85319b541e4e22e54977f (patch)
tree7642334ac634aaade00bedc6699ac9955f318b09
parentb5e0ecbd3315e9035f91764e45e64f98bb033282 (diff)
downloadcpython-4f009fb092aceb7bb9d85319b541e4e22e54977f.zip
cpython-4f009fb092aceb7bb9d85319b541e4e22e54977f.tar.gz
cpython-4f009fb092aceb7bb9d85319b541e4e22e54977f.tar.bz2
(py-no-outdent-re): new constant
(py-indent-line, py-electric-colon): watch for compound statements one line after another.
-rw-r--r--Misc/python-mode.el57
1 files changed, 39 insertions, 18 deletions
diff --git a/Misc/python-mode.el b/Misc/python-mode.el
index 217a655..0d5df7b 100644
--- a/Misc/python-mode.el
+++ b/Misc/python-mode.el
@@ -310,13 +310,25 @@ Currently-active file is at the head of the list.")
(defconst py-outdent-re
(concat "\\(" (mapconcat 'identity
'("else:"
- "except\\s +.*:"
+ "except\\(\\s +.*\\)?:"
"finally:"
"elif\\s +.*:")
"\\|")
"\\)")
"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
(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."
(interactive "P")
(self-insert-command (prefix-numeric-value arg))
- (let (this-indent)
- (if (and (not arg)
- (save-excursion
- (back-to-indentation)
- (looking-at py-outdent-re))
- (= (setq this-indent (py-compute-indentation))
- (save-excursion
- (forward-line -1)
- (py-compute-indentation)))
- )
- (save-excursion
- (beginning-of-line)
- (delete-horizontal-space)
- (indent-to (- this-indent py-indent-offset)))
+ (save-excursion
+ (let ((here (point))
+ (outdent 0)
+ (indent (py-compute-indentation)))
+ (if (and (not arg)
+ (progn
+ (back-to-indentation)
+ (looking-at py-outdent-re))
+ (prog2
+ (backward-to-indentation 1)
+ (not (looking-at py-no-outdent-re))
+ (goto-char here))
+ (= indent (progn
+ (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))
(move-to-indentation-p (<= (current-column) ci))
(need (py-compute-indentation)))
- ;; watch for outdents
+ ;; see if we need to outdent
(if (save-excursion
- (back-to-indentation)
- (looking-at py-outdent-re))
+ (and (progn (back-to-indentation)
+ (looking-at py-outdent-re))
+ (progn (backward-to-indentation 1)
+ (not (looking-at py-no-outdent-re)))))
(setq need (- need py-indent-offset)))
(if (/= ci need)
(save-excursion