(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.
This commit is contained in:
Barry Warsaw 1998-03-19 22:48:02 +00:00
parent b6c1f1f927
commit e908b6ba57
1 changed files with 45 additions and 24 deletions

View File

@ -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 ;; Major mode boilerplate
@ -1652,41 +1675,39 @@ it's tried again going backward."
(interactive "P") ; raw prefix arg (interactive "P") ; raw prefix arg
(let (new-value (let (new-value
(start (point)) (start (point))
restart (restart (point))
(found nil) (found nil)
colon-indent) colon-indent)
(py-goto-initial-line) (py-goto-initial-line)
(while (not (or found (eobp))) (while (not (or found (eobp)))
(if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) (when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
(progn (not (py-in-literal restart)))
(setq restart (point)) (setq restart (point))
(py-goto-initial-line) (py-goto-initial-line)
(if (py-statement-opens-block-p) (if (py-statement-opens-block-p)
(setq found t) (setq found t)
(goto-char restart))))) (goto-char restart))))
(if found (unless found
()
(goto-char start) (goto-char start)
(py-goto-initial-line) (py-goto-initial-line)
(while (not (or found (bobp))) (while (not (or found (bobp)))
(setq found (setq found (and
(and (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
(re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) (or (py-goto-initial-line) t) ; always true -- side effect
(or (py-goto-initial-line) t) ; always true -- side effect (py-statement-opens-block-p)))))
(py-statement-opens-block-p)))))
(setq colon-indent (current-indentation) (setq colon-indent (current-indentation)
found (and found (zerop (py-next-statement 1))) found (and found (zerop (py-next-statement 1)))
new-value (- (current-indentation) colon-indent)) new-value (- (current-indentation) colon-indent))
(goto-char start) (goto-char start)
(if found (if (not found)
(progn (error "Sorry, couldn't guess a value for py-indent-offset")
(funcall (if global 'kill-local-variable 'make-local-variable) (funcall (if global 'kill-local-variable 'make-local-variable)
'py-indent-offset) 'py-indent-offset)
(setq py-indent-offset new-value) (setq py-indent-offset new-value)
(message "%s value of py-indent-offset set to %d" (message "%s value of py-indent-offset set to %d"
(if global "Global" "Local") (if global "Global" "Local")
py-indent-offset)) py-indent-offset))
(error "Sorry, couldn't guess a value for py-indent-offset")))) ))
(defun py-shift-region (start end count) (defun py-shift-region (start end count)
(save-excursion (save-excursion