aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el57
1 files changed, 51 insertions, 6 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index e1347754c4a..d3ffc2db2c9 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1524,6 +1524,10 @@ marks the next defun after the ones already marked."
1524The name of the defun should be grouped so it can be retrieved 1524The name of the defun should be grouped so it can be retrieved
1525via `match-string'.") 1525via `match-string'.")
1526 1526
1527(defvar python-nav-beginning-of-block-regexp
1528 (python-rx line-start (* space) block-start)
1529 "Regexp matching block start.")
1530
1527(defun python-nav--beginning-of-defun (&optional arg) 1531(defun python-nav--beginning-of-defun (&optional arg)
1528 "Internal implementation of `python-nav-beginning-of-defun'. 1532 "Internal implementation of `python-nav-beginning-of-defun'.
1529With positive ARG search backwards, else search forwards." 1533With positive ARG search backwards, else search forwards."
@@ -4916,9 +4920,37 @@ Interactively, prompt for symbol."
4916(defun python-hideshow-forward-sexp-function (_arg) 4920(defun python-hideshow-forward-sexp-function (_arg)
4917 "Python specific `forward-sexp' function for `hs-minor-mode'. 4921 "Python specific `forward-sexp' function for `hs-minor-mode'.
4918Argument ARG is ignored." 4922Argument ARG is ignored."
4919 (python-nav-end-of-defun) 4923 (python-nav-end-of-block))
4920 (unless (python-info-current-line-empty-p) 4924
4921 (backward-char))) 4925(defun python-hideshow-find-next-block (regexp maxp comments)
4926 "Python specific `hs-find-next-block' function for `hs-minor-mode'.
4927Call `python-nav-forward-block' to find next block and check if
4928block-start ends within MAXP. If COMMENTS is not nil, comments
4929are also searched. REGEXP is passed to `looking-at' to set
4930`match-data'."
4931 (let* ((next-block (save-excursion
4932 (or (and
4933 (python-info-looking-at-beginning-of-block)
4934 (re-search-forward
4935 (python-rx block-start) maxp t))
4936 (and (python-nav-forward-block)
4937 (< (point) maxp)
4938 (re-search-forward
4939 (python-rx block-start) maxp t))
4940 (1+ maxp))))
4941 (next-comment
4942 (or (when comments
4943 (save-excursion
4944 (cl-loop while (re-search-forward "#" maxp t)
4945 if (python-syntax-context 'comment)
4946 return (point))))
4947 (1+ maxp)))
4948 (next-block-or-comment (min next-block next-comment)))
4949 (when (<= next-block-or-comment maxp)
4950 (goto-char next-block-or-comment)
4951 (save-excursion
4952 (beginning-of-line)
4953 (looking-at regexp)))))
4922 4954
4923 4955
4924;;; Imenu 4956;;; Imenu
@@ -5415,6 +5447,16 @@ instead of the current physical line."
5415 (beginning-of-line 1) 5447 (beginning-of-line 1)
5416 (looking-at python-nav-beginning-of-defun-regexp)))) 5448 (looking-at python-nav-beginning-of-defun-regexp))))
5417 5449
5450(defun python-info-looking-at-beginning-of-block ()
5451 "Check if point is at the beginning of block."
5452 (let ((pos (point)))
5453 (save-excursion
5454 (python-nav-beginning-of-statement)
5455 (beginning-of-line)
5456 (and
5457 (<= (point) pos (+ (point) (current-indentation)))
5458 (looking-at python-nav-beginning-of-block-regexp)))))
5459
5418(defun python-info-current-line-comment-p () 5460(defun python-info-current-line-comment-p ()
5419 "Return non-nil if current line is a comment line." 5461 "Return non-nil if current line is a comment line."
5420 (char-equal 5462 (char-equal
@@ -5870,14 +5912,17 @@ REPORT-FN is Flymake's callback function."
5870 5912
5871 (add-to-list 5913 (add-to-list
5872 'hs-special-modes-alist 5914 'hs-special-modes-alist
5873 '(python-mode 5915 `(python-mode
5874 "\\s-*\\_<\\(?:def\\|class\\)\\_>" 5916 ,python-nav-beginning-of-block-regexp
5875 ;; Use the empty string as end regexp so it doesn't default to 5917 ;; Use the empty string as end regexp so it doesn't default to
5876 ;; "\\s)". This way parens at end of defun are properly hidden. 5918 ;; "\\s)". This way parens at end of defun are properly hidden.
5877 "" 5919 ""
5878 "#" 5920 "#"
5879 python-hideshow-forward-sexp-function 5921 python-hideshow-forward-sexp-function
5880 nil)) 5922 nil
5923 python-nav-beginning-of-block
5924 python-hideshow-find-next-block
5925 python-info-looking-at-beginning-of-block))
5881 5926
5882 (setq-local outline-regexp (python-rx (* space) block-start)) 5927 (setq-local outline-regexp (python-rx (* space) block-start))
5883 (setq-local outline-level 5928 (setq-local outline-level