* progmodes/python.el (python-nav-if-name-main): New command.

parent cd16c5f1
......@@ -2,6 +2,7 @@
* progmodes/python.el (python-shell-completion-get-completions):
Drop use of deleted `comint-last-prompt-overlay'.
(python-nav-if-name-main): New command.
2013-09-01 Glenn Morris <rgm@gnu.org>
......
......@@ -52,12 +52,12 @@
;; Extra functions `python-nav-forward-statement',
;; `python-nav-backward-statement',
;; `python-nav-beginning-of-statement', `python-nav-end-of-statement',
;; `python-nav-beginning-of-block' and `python-nav-end-of-block' are
;; included but no bound to any key. At last but not least the
;; specialized `python-nav-forward-sexp' allows easy navigation
;; between code blocks. If you prefer `cc-mode'-like `forward-sexp'
;; movement, setting `forward-sexp-function' to nil is enough, You can
;; do that using the `python-mode-hook':
;; `python-nav-beginning-of-block', `python-nav-end-of-block' and
;; `python-nav-if-name-main' are included but no bound to any key. At
;; last but not least the specialized `python-nav-forward-sexp' allows
;; easy navigation between code blocks. If you prefer `cc-mode'-like
;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
;; enough, You can do that using the `python-mode-hook':
;; (add-hook 'python-mode-hook
;; (lambda () (setq forward-sexp-function nil)))
......@@ -1583,6 +1583,29 @@ This command assumes point is not in a string or comment."
(or arg (setq arg 1))
(python-nav-up-list (- arg)))
(defun python-nav-if-name-main ()
"Move point at the beginning the __main__ block.
When \"if __name__ == '__main__':\" is found returns its
position, else returns nil."
(interactive)
(let ((point (point))
(found (catch 'found
(goto-char (point-min))
(while (re-search-forward
(python-rx line-start
"if" (+ space)
"__name__" (+ space)
"==" (+ space)
(group-n 1 (or ?\" ?\'))
"__main__" (backref 1) (* space) ":")
nil t)
(when (not (python-syntax-context-type))
(beginning-of-line)
(throw 'found t))))))
(if found
(point)
(ignore (goto-char point)))))
;;; Shell integration
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment