diff options
author | Barry Warsaw <barry@python.org> | 1998-03-19 22:48:02 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 1998-03-19 22:48:02 (GMT) |
commit | e908b6ba57d12fc72f49eed21a3ce4c4b3ec9533 (patch) | |
tree | 396d6b4632a8831e745b5176369b3c469ee426db | |
parent | b6c1f1f92779dec4f906b439882f9575de3b0595 (diff) | |
download | cpython-e908b6ba57d12fc72f49eed21a3ce4c4b3ec9533.zip cpython-e908b6ba57d12fc72f49eed21a3ce4c4b3ec9533.tar.gz cpython-e908b6ba57d12fc72f49eed21a3ce4c4b3ec9533.tar.bz2 |
(py-in-literal, py-fast-in-literal): New functions (mostly) stolen
from CC Mode.
(py-guess-indent-offset): Teach it about colons in `literals'
(e.g. comments and strings). Don't false hit colons in literals; keep
searching for a real block introducing line.
-rw-r--r-- | Misc/python-mode.el | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/Misc/python-mode.el b/Misc/python-mode.el index 29286e4..45eadfe 100644 --- a/Misc/python-mode.el +++ b/Misc/python-mode.el @@ -518,6 +518,29 @@ Currently-active file is at the head of the list.") ) )) +(defun py-in-literal (&optional lim) + ;; Determine if point is in a Python literal, defined as a comment + ;; or string. This is the version used for non-XEmacs, which has a + ;; nicer interface. + ;; + ;; WARNING: Watch out for infinite recursion. + (let* ((lim (or lim (c-point 'bod))) + (state (parse-partial-sexp lim (point)))) + (cond + ((nth 3 state) 'string) + ((nth 4 state) 'comment) + (t nil)))) + +;; XEmacs has a built-in function that should make this much quicker. +;; In this case, lim is ignored +(defun py-fast-in-literal (&optional lim) + ;; don't have to worry about context == 'block-comment + (buffer-syntactic-context)) + +(if (fboundp 'buffer-syntactic-context) + (defalias 'c-in-literal 'c-fast-in-literal)) + + ;; Major mode boilerplate @@ -1652,41 +1675,39 @@ it's tried again going backward." (interactive "P") ; raw prefix arg (let (new-value (start (point)) - restart + (restart (point)) (found nil) colon-indent) (py-goto-initial-line) (while (not (or found (eobp))) - (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) - (progn - (setq restart (point)) - (py-goto-initial-line) - (if (py-statement-opens-block-p) - (setq found t) - (goto-char restart))))) - (if found - () + (when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) + (not (py-in-literal restart))) + (setq restart (point)) + (py-goto-initial-line) + (if (py-statement-opens-block-p) + (setq found t) + (goto-char restart)))) + (unless found (goto-char start) (py-goto-initial-line) (while (not (or found (bobp))) - (setq found - (and - (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) - (or (py-goto-initial-line) t) ; always true -- side effect - (py-statement-opens-block-p))))) + (setq found (and + (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) + (or (py-goto-initial-line) t) ; always true -- side effect + (py-statement-opens-block-p))))) (setq colon-indent (current-indentation) found (and found (zerop (py-next-statement 1))) new-value (- (current-indentation) colon-indent)) (goto-char start) - (if found - (progn - (funcall (if global 'kill-local-variable 'make-local-variable) - 'py-indent-offset) - (setq py-indent-offset new-value) - (message "%s value of py-indent-offset set to %d" - (if global "Global" "Local") - py-indent-offset)) - (error "Sorry, couldn't guess a value for py-indent-offset")))) + (if (not found) + (error "Sorry, couldn't guess a value for py-indent-offset") + (funcall (if global 'kill-local-variable 'make-local-variable) + 'py-indent-offset) + (setq py-indent-offset new-value) + (message "%s value of py-indent-offset set to %d" + (if global "Global" "Local") + py-indent-offset)) + )) (defun py-shift-region (start end count) (save-excursion |