diff options
| -rw-r--r-- | lisp/ChangeLog | 1 | ||||
| -rw-r--r-- | lisp/progmodes/python.el | 35 |
2 files changed, 30 insertions, 6 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index b48ff2f5030..f99a4e8b1fb 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | * progmodes/python.el (python-shell-completion-get-completions): | 3 | * progmodes/python.el (python-shell-completion-get-completions): |
| 4 | Drop use of deleted `comint-last-prompt-overlay'. | 4 | Drop use of deleted `comint-last-prompt-overlay'. |
| 5 | (python-nav-if-name-main): New command. | ||
| 5 | 6 | ||
| 6 | 2013-09-01 Glenn Morris <rgm@gnu.org> | 7 | 2013-09-01 Glenn Morris <rgm@gnu.org> |
| 7 | 8 | ||
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index b8e2f4c8de9..af55854bbc4 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -52,12 +52,12 @@ | |||
| 52 | ;; Extra functions `python-nav-forward-statement', | 52 | ;; Extra functions `python-nav-forward-statement', |
| 53 | ;; `python-nav-backward-statement', | 53 | ;; `python-nav-backward-statement', |
| 54 | ;; `python-nav-beginning-of-statement', `python-nav-end-of-statement', | 54 | ;; `python-nav-beginning-of-statement', `python-nav-end-of-statement', |
| 55 | ;; `python-nav-beginning-of-block' and `python-nav-end-of-block' are | 55 | ;; `python-nav-beginning-of-block', `python-nav-end-of-block' and |
| 56 | ;; included but no bound to any key. At last but not least the | 56 | ;; `python-nav-if-name-main' are included but no bound to any key. At |
| 57 | ;; specialized `python-nav-forward-sexp' allows easy navigation | 57 | ;; last but not least the specialized `python-nav-forward-sexp' allows |
| 58 | ;; between code blocks. If you prefer `cc-mode'-like `forward-sexp' | 58 | ;; easy navigation between code blocks. If you prefer `cc-mode'-like |
| 59 | ;; movement, setting `forward-sexp-function' to nil is enough, You can | 59 | ;; `forward-sexp' movement, setting `forward-sexp-function' to nil is |
| 60 | ;; do that using the `python-mode-hook': | 60 | ;; enough, You can do that using the `python-mode-hook': |
| 61 | 61 | ||
| 62 | ;; (add-hook 'python-mode-hook | 62 | ;; (add-hook 'python-mode-hook |
| 63 | ;; (lambda () (setq forward-sexp-function nil))) | 63 | ;; (lambda () (setq forward-sexp-function nil))) |
| @@ -1583,6 +1583,29 @@ This command assumes point is not in a string or comment." | |||
| 1583 | (or arg (setq arg 1)) | 1583 | (or arg (setq arg 1)) |
| 1584 | (python-nav-up-list (- arg))) | 1584 | (python-nav-up-list (- arg))) |
| 1585 | 1585 | ||
| 1586 | (defun python-nav-if-name-main () | ||
| 1587 | "Move point at the beginning the __main__ block. | ||
| 1588 | When \"if __name__ == '__main__':\" is found returns its | ||
| 1589 | position, else returns nil." | ||
| 1590 | (interactive) | ||
| 1591 | (let ((point (point)) | ||
| 1592 | (found (catch 'found | ||
| 1593 | (goto-char (point-min)) | ||
| 1594 | (while (re-search-forward | ||
| 1595 | (python-rx line-start | ||
| 1596 | "if" (+ space) | ||
| 1597 | "__name__" (+ space) | ||
| 1598 | "==" (+ space) | ||
| 1599 | (group-n 1 (or ?\" ?\')) | ||
| 1600 | "__main__" (backref 1) (* space) ":") | ||
| 1601 | nil t) | ||
| 1602 | (when (not (python-syntax-context-type)) | ||
| 1603 | (beginning-of-line) | ||
| 1604 | (throw 'found t)))))) | ||
| 1605 | (if found | ||
| 1606 | (point) | ||
| 1607 | (ignore (goto-char point))))) | ||
| 1608 | |||
| 1586 | 1609 | ||
| 1587 | ;;; Shell integration | 1610 | ;;; Shell integration |
| 1588 | 1611 | ||