aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorEli Zaretskii2012-11-13 16:17:18 +0200
committerEli Zaretskii2012-11-13 16:17:18 +0200
commit3c4ca7155293ffc2d04708007131bcbc882d8913 (patch)
tree61787be8cd43b6fb3d5159852fbd186eea404de7 /lisp/progmodes/python.el
parent5ade42a5114255c43117065494b96d480c1e1588 (diff)
parentc708524567662c8911c5ab2695acc7bda0383705 (diff)
downloademacs-3c4ca7155293ffc2d04708007131bcbc882d8913.tar.gz
emacs-3c4ca7155293ffc2d04708007131bcbc882d8913.zip
Merge from trunk.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el166
1 files changed, 87 insertions, 79 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index ff805d64024..949b0252bf1 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1074,12 +1074,9 @@ automatically if needed."
1074The name of the defun should be grouped so it can be retrieved 1074The name of the defun should be grouped so it can be retrieved
1075via `match-string'.") 1075via `match-string'.")
1076 1076
1077(defun python-nav-beginning-of-defun (&optional arg) 1077(defun python-nav--beginning-of-defun (&optional arg)
1078 "Move point to `beginning-of-defun'. 1078 "Internal implementation of `python-nav-beginning-of-defun'.
1079With positive ARG move search backwards. With negative do the 1079With positive ARG search backwards, else search forwards."
1080same but forward. When ARG is nil or 0 defaults to 1. This is
1081the main part of `python-beginning-of-defun-function'. Return
1082non-nil if point is moved to `beginning-of-defun'."
1083 (when (or (null arg) (= arg 0)) (setq arg 1)) 1080 (when (or (null arg) (= arg 0)) (setq arg 1))
1084 (let* ((re-search-fn (if (> arg 0) 1081 (let* ((re-search-fn (if (> arg 0)
1085 #'re-search-backward 1082 #'re-search-backward
@@ -1087,6 +1084,15 @@ non-nil if point is moved to `beginning-of-defun'."
1087 (line-beg-pos (line-beginning-position)) 1084 (line-beg-pos (line-beginning-position))
1088 (line-content-start (+ line-beg-pos (current-indentation))) 1085 (line-content-start (+ line-beg-pos (current-indentation)))
1089 (pos (point-marker)) 1086 (pos (point-marker))
1087 (beg-indentation
1088 (and (> arg 0)
1089 (save-excursion
1090 (and (python-info-current-line-empty-p)
1091 (python-util-forward-comment -1))
1092 (python-nav-beginning-of-statement)
1093 (if (python-info-looking-at-beginning-of-defun)
1094 (+ (current-indentation) python-indent-offset)
1095 (current-indentation)))))
1090 (found 1096 (found
1091 (progn 1097 (progn
1092 (when (and (< arg 0) 1098 (when (and (< arg 0)
@@ -1094,7 +1100,12 @@ non-nil if point is moved to `beginning-of-defun'."
1094 (end-of-line 1)) 1100 (end-of-line 1))
1095 (while (and (funcall re-search-fn 1101 (while (and (funcall re-search-fn
1096 python-nav-beginning-of-defun-regexp nil t) 1102 python-nav-beginning-of-defun-regexp nil t)
1097 (python-syntax-context-type))) 1103 (or (python-syntax-context-type)
1104 ;; Handle nested defuns when moving
1105 ;; backwards by checking indentation.
1106 (and (> arg 0)
1107 (not (= (current-indentation) 0))
1108 (>= (current-indentation) beg-indentation)))))
1098 (and (python-info-looking-at-beginning-of-defun) 1109 (and (python-info-looking-at-beginning-of-defun)
1099 (or (not (= (line-number-at-pos pos) 1110 (or (not (= (line-number-at-pos pos)
1100 (line-number-at-pos))) 1111 (line-number-at-pos)))
@@ -1105,55 +1116,43 @@ non-nil if point is moved to `beginning-of-defun'."
1105 (or (beginning-of-line 1) t) 1116 (or (beginning-of-line 1) t)
1106 (and (goto-char pos) nil)))) 1117 (and (goto-char pos) nil))))
1107 1118
1108(defun python-beginning-of-defun-function (&optional arg) 1119(defun python-nav-beginning-of-defun (&optional arg)
1109 "Move point to the beginning of def or class. 1120 "Move point to `beginning-of-defun'.
1110With positive ARG move that number of functions backwards. With 1121With positive ARG search backwards else search forward. When ARG
1111negative do the same but forward. When ARG is nil or 0 defaults 1122is nil or 0 defaults to 1. When searching backwards nested
1112to 1. Return non-nil if point is moved to `beginning-of-defun'." 1123defuns are handled with care depending on current point
1124position. Return non-nil if point is moved to
1125`beginning-of-defun'."
1113 (when (or (null arg) (= arg 0)) (setq arg 1)) 1126 (when (or (null arg) (= arg 0)) (setq arg 1))
1114 (let ((found)) 1127 (let ((found))
1115 (cond ((and (eq this-command 'mark-defun) 1128 (cond ((and (eq this-command 'mark-defun)
1116 (python-info-looking-at-beginning-of-defun))) 1129 (python-info-looking-at-beginning-of-defun)))
1117 (t 1130 (t
1118 (dotimes (i (if (> arg 0) arg (- arg))) 1131 (dotimes (i (if (> arg 0) arg (- arg)))
1119 (when (and (python-nav-beginning-of-defun arg) 1132 (when (and (python-nav--beginning-of-defun arg)
1120 (not found)) 1133 (not found))
1121 (setq found t))))) 1134 (setq found t)))))
1122 found)) 1135 found))
1123 1136
1124(defun python-end-of-defun-function () 1137(defun python-nav-end-of-defun ()
1125 "Move point to the end of def or class. 1138 "Move point to the end of def or class.
1126Returns nil if point is not in a def or class." 1139Returns nil if point is not in a def or class."
1127 (interactive) 1140 (interactive)
1128 (let ((beg-defun-indent)) 1141 (let ((beg-defun-indent)
1142 (beg-pos (point)))
1129 (when (or (python-info-looking-at-beginning-of-defun) 1143 (when (or (python-info-looking-at-beginning-of-defun)
1130 (python-beginning-of-defun-function 1) 1144 (python-nav-beginning-of-defun 1)
1131 (python-beginning-of-defun-function -1)) 1145 (python-nav-beginning-of-defun -1))
1132 (setq beg-defun-indent (current-indentation)) 1146 (setq beg-defun-indent (current-indentation))
1147 (while (progn
1148 (python-nav-end-of-statement)
1149 (python-util-forward-comment 1)
1150 (and (> (current-indentation) beg-defun-indent)
1151 (not (eobp)))))
1152 (python-util-forward-comment -1)
1133 (forward-line 1) 1153 (forward-line 1)
1134 ;; Go as forward as possible 1154 ;; Ensure point moves forward.
1135 (while (and (or 1155 (and (> beg-pos (point)) (goto-char beg-pos)))))
1136 (python-nav-beginning-of-defun -1)
1137 (and (goto-char (point-max)) nil))
1138 (> (current-indentation) beg-defun-indent)))
1139 (beginning-of-line 1)
1140 ;; Go as backwards as possible
1141 (while (and (forward-line -1)
1142 (not (bobp))
1143 (or (not (current-word))
1144 (equal (char-after (+ (point) (current-indentation))) ?#)
1145 (<= (current-indentation) beg-defun-indent)
1146 (looking-at (python-rx decorator))
1147 (python-syntax-context-type))))
1148 (forward-line 1)
1149 ;; If point falls inside a paren or string context the point is
1150 ;; forwarded at the end of it (or end of buffer if its not closed)
1151 (let ((context-type (python-syntax-context-type)))
1152 (when (memq context-type '(paren string))
1153 ;; Slow but safe.
1154 (while (and (not (eobp))
1155 (python-syntax-context-type))
1156 (forward-line 1)))))))
1157 1156
1158(defun python-nav-beginning-of-statement () 1157(defun python-nav-beginning-of-statement ()
1159 "Move to start of current statement." 1158 "Move to start of current statement."
@@ -1733,17 +1732,24 @@ variable.
1733 (set (make-local-variable 'font-lock-defaults) 1732 (set (make-local-variable 'font-lock-defaults)
1734 '(python-font-lock-keywords nil nil nil nil)) 1733 '(python-font-lock-keywords nil nil nil nil))
1735 (set (make-local-variable 'syntax-propertize-function) 1734 (set (make-local-variable 'syntax-propertize-function)
1736 (syntax-propertize-rules 1735 (eval
1737 (comint-prompt-regexp 1736 ;; XXX: Unfortunately eval is needed here to make use of the
1738 (0 (ignore 1737 ;; dynamic value of `comint-prompt-regexp'.
1739 (put-text-property 1738 `(syntax-propertize-rules
1740 comint-last-input-start end 'syntax-table 1739 (,comint-prompt-regexp
1741 python-shell-output-syntax-table) 1740 (0 (ignore
1742 (font-lock-unfontify-region comint-last-input-start end)))) 1741 (put-text-property
1743 ((python-rx string-delimiter) 1742 comint-last-input-start end 'syntax-table
1744 (0 (ignore 1743 python-shell-output-syntax-table)
1745 (and (not (eq (get-text-property start 'field) 'output)) 1744 ;; XXX: This might look weird, but it is the easiest
1746 (python-syntax-stringify)))))))) 1745 ;; way to ensure font lock gets cleaned up before the
1746 ;; current prompt, which is needed for unclosed
1747 ;; strings to not mess up with current input.
1748 (font-lock-unfontify-region comint-last-input-start end))))
1749 (,(python-rx string-delimiter)
1750 (0 (ignore
1751 (and (not (eq (get-text-property start 'field) 'output))
1752 (python-syntax-stringify)))))))))
1747 (compilation-shell-minor-mode 1)) 1753 (compilation-shell-minor-mode 1))
1748 1754
1749(defun python-shell-make-comint (cmd proc-name &optional pop internal) 1755(defun python-shell-make-comint (cmd proc-name &optional pop internal)
@@ -2015,7 +2021,7 @@ When argument ARG is non-nil do not include decorators."
2015 (python-shell-send-region 2021 (python-shell-send-region
2016 (progn 2022 (progn
2017 (end-of-line 1) 2023 (end-of-line 1)
2018 (while (and (or (python-beginning-of-defun-function) 2024 (while (and (or (python-nav-beginning-of-defun)
2019 (beginning-of-line 1)) 2025 (beginning-of-line 1))
2020 (> (current-indentation) 0))) 2026 (> (current-indentation) 0)))
2021 (when (not arg) 2027 (when (not arg)
@@ -2024,7 +2030,7 @@ When argument ARG is non-nil do not include decorators."
2024 (forward-line 1)) 2030 (forward-line 1))
2025 (point-marker)) 2031 (point-marker))
2026 (progn 2032 (progn
2027 (or (python-end-of-defun-function) 2033 (or (python-nav-end-of-defun)
2028 (end-of-line 1)) 2034 (end-of-line 1))
2029 (point-marker))))) 2035 (point-marker)))))
2030 2036
@@ -2872,38 +2878,40 @@ Optional argument INCLUDE-TYPE indicates to include the type of the defun.
2872This function is compatible to be used as 2878This function is compatible to be used as
2873`add-log-current-defun-function' since it returns nil if point is 2879`add-log-current-defun-function' since it returns nil if point is
2874not inside a defun." 2880not inside a defun."
2875 (let ((names '())
2876 (starting-indentation)
2877 (starting-point)
2878 (first-run t))
2879 (save-restriction 2881 (save-restriction
2880 (widen) 2882 (widen)
2881 (save-excursion 2883 (save-excursion
2882 (setq starting-point (point-marker))
2883 (setq starting-indentation (save-excursion
2884 (python-nav-beginning-of-statement)
2885 (current-indentation)))
2886 (end-of-line 1) 2884 (end-of-line 1)
2887 (while (python-beginning-of-defun-function 1) 2885 (let ((names)
2888 (when (or (< (current-indentation) starting-indentation) 2886 (starting-indentation
2889 (and first-run 2887 (save-excursion
2890 (< 2888 (and
2891 starting-point 2889 (python-nav-beginning-of-defun 1)
2892 (save-excursion 2890 ;; This extra number is just for checking code
2893 (python-end-of-defun-function) 2891 ;; against indentation to work well on first run.
2894 (point-marker))))) 2892 (+ (current-indentation) 4))))
2895 (setq first-run nil) 2893 (starting-point (point)))
2896 (setq starting-indentation (current-indentation)) 2894 ;; Check point is inside a defun.
2897 (looking-at python-nav-beginning-of-defun-regexp) 2895 (when (and starting-indentation
2898 (setq names (cons 2896 (< starting-point
2897 (save-excursion
2898 (python-nav-end-of-defun)
2899 (point))))
2900 (catch 'exit
2901 (while (python-nav-beginning-of-defun 1)
2902 (when (< (current-indentation) starting-indentation)
2903 (setq starting-indentation (current-indentation))
2904 (setq names
2905 (cons
2899 (if (not include-type) 2906 (if (not include-type)
2900 (match-string-no-properties 1) 2907 (match-string-no-properties 1)
2901 (mapconcat 'identity 2908 (mapconcat 'identity
2902 (split-string 2909 (split-string
2903 (match-string-no-properties 0)) " ")) 2910 (match-string-no-properties 0)) " "))
2904 names)))))) 2911 names)))
2905 (when names 2912 (and (= (current-indentation) 0) (throw 'exit t)))))
2906 (mapconcat (lambda (string) string) names ".")))) 2913 (and names
2914 (mapconcat (lambda (string) string) names "."))))))
2907 2915
2908(defun python-info-current-symbol (&optional replace-self) 2916(defun python-info-current-symbol (&optional replace-self)
2909 "Return current symbol using dotty syntax. 2917 "Return current symbol using dotty syntax.
@@ -3193,9 +3201,9 @@ if that value is non-nil."
3193 'python-fill-paragraph) 3201 'python-fill-paragraph)
3194 3202
3195 (set (make-local-variable 'beginning-of-defun-function) 3203 (set (make-local-variable 'beginning-of-defun-function)
3196 #'python-beginning-of-defun-function) 3204 #'python-nav-beginning-of-defun)
3197 (set (make-local-variable 'end-of-defun-function) 3205 (set (make-local-variable 'end-of-defun-function)
3198 #'python-end-of-defun-function) 3206 #'python-nav-end-of-defun)
3199 3207
3200 (add-hook 'completion-at-point-functions 3208 (add-hook 'completion-at-point-functions
3201 'python-completion-complete-at-point nil 'local) 3209 'python-completion-complete-at-point nil 'local)
@@ -3223,7 +3231,7 @@ if that value is non-nil."
3223 (add-to-list 'hs-special-modes-alist 3231 (add-to-list 'hs-special-modes-alist
3224 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#" 3232 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
3225 ,(lambda (arg) 3233 ,(lambda (arg)
3226 (python-end-of-defun-function)) nil)) 3234 (python-nav-end-of-defun)) nil))
3227 3235
3228 (set (make-local-variable 'mode-require-final-newline) t) 3236 (set (make-local-variable 'mode-require-final-newline) t)
3229 3237