diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 97 |
1 files changed, 48 insertions, 49 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 7a90f0bb5ee..3e0708cd3c2 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -40,9 +40,9 @@ | |||
| 40 | 40 | ||
| 41 | ;; Indentation: Automatic indentation with indentation cycling is | 41 | ;; Indentation: Automatic indentation with indentation cycling is |
| 42 | ;; provided, it allows you to navigate different available levels of | 42 | ;; provided, it allows you to navigate different available levels of |
| 43 | ;; indentation by hitting <tab> several times. Also when inserting a | 43 | ;; indentation by hitting <tab> several times. Also electric-indent-mode |
| 44 | ;; colon the `python-indent-electric-colon' command is invoked and | 44 | ;; is supported such that when inserting a colon the current line is |
| 45 | ;; causes the current line to be dedented automatically if needed. | 45 | ;; dedented automatically if needed. |
| 46 | 46 | ||
| 47 | ;; Movement: `beginning-of-defun' and `end-of-defun' functions are | 47 | ;; Movement: `beginning-of-defun' and `end-of-defun' functions are |
| 48 | ;; properly implemented. There are also specialized | 48 | ;; properly implemented. There are also specialized |
| @@ -248,7 +248,6 @@ | |||
| 248 | (define-key map (kbd "<backtab>") 'python-indent-dedent-line) | 248 | (define-key map (kbd "<backtab>") 'python-indent-dedent-line) |
| 249 | (define-key map "\C-c<" 'python-indent-shift-left) | 249 | (define-key map "\C-c<" 'python-indent-shift-left) |
| 250 | (define-key map "\C-c>" 'python-indent-shift-right) | 250 | (define-key map "\C-c>" 'python-indent-shift-right) |
| 251 | (define-key map ":" 'python-indent-electric-colon) | ||
| 252 | ;; Skeletons | 251 | ;; Skeletons |
| 253 | (define-key map "\C-c\C-tc" 'python-skeleton-class) | 252 | (define-key map "\C-c\C-tc" 'python-skeleton-class) |
| 254 | (define-key map "\C-c\C-td" 'python-skeleton-def) | 253 | (define-key map "\C-c\C-td" 'python-skeleton-def) |
| @@ -676,7 +675,7 @@ AFTER-LINE.") | |||
| 676 | (goto-char block-end) | 675 | (goto-char block-end) |
| 677 | (python-util-forward-comment) | 676 | (python-util-forward-comment) |
| 678 | (current-indentation)))) | 677 | (current-indentation)))) |
| 679 | (if indentation | 678 | (if (and indentation (not (zerop indentation))) |
| 680 | (set (make-local-variable 'python-indent-offset) indentation) | 679 | (set (make-local-variable 'python-indent-offset) indentation) |
| 681 | (message "Can't guess python-indent-offset, using defaults: %s" | 680 | (message "Can't guess python-indent-offset, using defaults: %s" |
| 682 | python-indent-offset))))))) | 681 | python-indent-offset))))))) |
| @@ -1058,48 +1057,43 @@ the lines in which START and END lie." | |||
| 1058 | (list (region-beginning) (region-end) current-prefix-arg) | 1057 | (list (region-beginning) (region-end) current-prefix-arg) |
| 1059 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | 1058 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) |
| 1060 | (let ((deactivate-mark nil)) | 1059 | (let ((deactivate-mark nil)) |
| 1061 | (if count | 1060 | (setq count (if count (prefix-numeric-value count) |
| 1062 | (setq count (prefix-numeric-value count)) | 1061 | python-indent-offset)) |
| 1063 | (setq count python-indent-offset)) | ||
| 1064 | (indent-rigidly start end count))) | 1062 | (indent-rigidly start end count))) |
| 1065 | 1063 | ||
| 1066 | (defun python-indent-electric-colon (arg) | ||
| 1067 | "Insert a colon and maybe de-indent the current line. | ||
| 1068 | With numeric ARG, just insert that many colons. With | ||
| 1069 | \\[universal-argument], just insert a single colon." | ||
| 1070 | (interactive "*P") | ||
| 1071 | (self-insert-command (if (not (integerp arg)) 1 arg)) | ||
| 1072 | (when (and (not arg) | ||
| 1073 | (eolp) | ||
| 1074 | (not (equal ?: (char-after (- (point-marker) 2)))) | ||
| 1075 | (not (python-syntax-comment-or-string-p))) | ||
| 1076 | (let ((indentation (current-indentation)) | ||
| 1077 | (calculated-indentation (python-indent-calculate-indentation))) | ||
| 1078 | (python-info-closing-block-message) | ||
| 1079 | (when (> indentation calculated-indentation) | ||
| 1080 | (save-excursion | ||
| 1081 | (indent-line-to calculated-indentation) | ||
| 1082 | (when (not (python-info-closing-block-message)) | ||
| 1083 | (indent-line-to indentation))))))) | ||
| 1084 | (put 'python-indent-electric-colon 'delete-selection t) | ||
| 1085 | |||
| 1086 | (defun python-indent-post-self-insert-function () | 1064 | (defun python-indent-post-self-insert-function () |
| 1087 | "Adjust closing paren line indentation after a char is added. | 1065 | "Adjust indentation after insertion of some characters. |
| 1088 | This function is intended to be added to the | 1066 | This function is intended to be added to the |
| 1089 | `post-self-insert-hook.' If a line renders a paren alone, after | 1067 | `post-self-insert-hook.' If a line renders a paren alone, after |
| 1090 | adding a char before it, the line will be re-indented | 1068 | adding a char before it, the line will be re-indented |
| 1091 | automatically if needed." | 1069 | automatically if needed." |
| 1092 | (when (and (eq (char-before) last-command-event) | 1070 | (when (and electric-indent-mode |
| 1093 | (not (bolp)) | 1071 | (eq (char-before) last-command-event)) |
| 1094 | (memq (char-after) '(?\) ?\] ?\}))) | 1072 | (cond |
| 1095 | (save-excursion | 1073 | ((and (not (bolp)) |
| 1096 | (goto-char (line-beginning-position)) | 1074 | (memq (char-after) '(?\) ?\] ?\}))) |
| 1097 | ;; If after going to the beginning of line the point | 1075 | (save-excursion |
| 1098 | ;; is still inside a paren it's ok to do the trick | 1076 | (goto-char (line-beginning-position)) |
| 1099 | (when (python-syntax-context 'paren) | 1077 | ;; If after going to the beginning of line the point |
| 1100 | (let ((indentation (python-indent-calculate-indentation))) | 1078 | ;; is still inside a paren it's ok to do the trick |
| 1101 | (when (< (current-indentation) indentation) | 1079 | (when (python-syntax-context 'paren) |
| 1102 | (indent-line-to indentation))))))) | 1080 | (let ((indentation (python-indent-calculate-indentation))) |
| 1081 | (when (< (current-indentation) indentation) | ||
| 1082 | (indent-line-to indentation)))))) | ||
| 1083 | ((and (eq ?: last-command-event) | ||
| 1084 | (memq ?: electric-indent-chars) | ||
| 1085 | (not current-prefix-arg) | ||
| 1086 | (eolp) | ||
| 1087 | (not (equal ?: (char-before (1- (point))))) | ||
| 1088 | (not (python-syntax-comment-or-string-p))) | ||
| 1089 | (let ((indentation (current-indentation)) | ||
| 1090 | (calculated-indentation (python-indent-calculate-indentation))) | ||
| 1091 | (python-info-closing-block-message) | ||
| 1092 | (when (> indentation calculated-indentation) | ||
| 1093 | (save-excursion | ||
| 1094 | (indent-line-to calculated-indentation) | ||
| 1095 | (when (not (python-info-closing-block-message)) | ||
| 1096 | (indent-line-to indentation))))))))) | ||
| 1103 | 1097 | ||
| 1104 | 1098 | ||
| 1105 | ;;; Navigation | 1099 | ;;; Navigation |
| @@ -2056,7 +2050,7 @@ When MSG is non-nil messages the first line of STRING." | |||
| 2056 | (let ((process (or process (python-shell-get-or-create-process)))) | 2050 | (let ((process (or process (python-shell-get-or-create-process)))) |
| 2057 | (if (string-match ".\n+." string) ;Multiline. | 2051 | (if (string-match ".\n+." string) ;Multiline. |
| 2058 | (let* ((temp-file-name (python-shell--save-temp-file string))) | 2052 | (let* ((temp-file-name (python-shell--save-temp-file string))) |
| 2059 | (python-shell-send-file temp-file-name process temp-file-name)) | 2053 | (python-shell-send-file temp-file-name process temp-file-name t)) |
| 2060 | (comint-send-string process string) | 2054 | (comint-send-string process string) |
| 2061 | (when (or (not (string-match "\n\\'" string)) | 2055 | (when (or (not (string-match "\n\\'" string)) |
| 2062 | (string-match "\n[ \t].*\n?\\'" string)) | 2056 | (string-match "\n[ \t].*\n?\\'" string)) |
| @@ -2212,7 +2206,7 @@ the python shell: | |||
| 2212 | (message "Sent: %s..." (match-string 1 string)) | 2206 | (message "Sent: %s..." (match-string 1 string)) |
| 2213 | (let* ((temp-file-name (python-shell--save-temp-file string)) | 2207 | (let* ((temp-file-name (python-shell--save-temp-file string)) |
| 2214 | (file-name (or (buffer-file-name) temp-file-name))) | 2208 | (file-name (or (buffer-file-name) temp-file-name))) |
| 2215 | (python-shell-send-file file-name process temp-file-name) | 2209 | (python-shell-send-file file-name process temp-file-name t) |
| 2216 | (unless python--use-fake-loc | 2210 | (unless python--use-fake-loc |
| 2217 | (with-current-buffer (process-buffer process) | 2211 | (with-current-buffer (process-buffer process) |
| 2218 | (compilation-fake-loc (copy-marker start) temp-file-name | 2212 | (compilation-fake-loc (copy-marker start) temp-file-name |
| @@ -2249,11 +2243,12 @@ When argument ARG is non-nil do not include decorators." | |||
| 2249 | (end-of-line 1)) | 2243 | (end-of-line 1)) |
| 2250 | (point-marker))))) | 2244 | (point-marker))))) |
| 2251 | 2245 | ||
| 2252 | (defun python-shell-send-file (file-name &optional process temp-file-name) | 2246 | (defun python-shell-send-file (file-name &optional process temp-file-name |
| 2247 | delete) | ||
| 2253 | "Send FILE-NAME to inferior Python PROCESS. | 2248 | "Send FILE-NAME to inferior Python PROCESS. |
| 2254 | If TEMP-FILE-NAME is passed then that file is used for processing | 2249 | If TEMP-FILE-NAME is passed then that file is used for processing |
| 2255 | instead, while internally the shell will continue to use | 2250 | instead, while internally the shell will continue to use |
| 2256 | FILE-NAME." | 2251 | FILE-NAME. If DELETE is non-nil, delete the file afterwards." |
| 2257 | (interactive "fFile to send: ") | 2252 | (interactive "fFile to send: ") |
| 2258 | (let* ((process (or process (python-shell-get-or-create-process))) | 2253 | (let* ((process (or process (python-shell-get-or-create-process))) |
| 2259 | (temp-file-name (when temp-file-name | 2254 | (temp-file-name (when temp-file-name |
| @@ -2271,8 +2266,11 @@ FILE-NAME." | |||
| 2271 | (format | 2266 | (format |
| 2272 | (concat "__pyfile = open('''%s''');" | 2267 | (concat "__pyfile = open('''%s''');" |
| 2273 | "exec(compile(__pyfile.read(), '''%s''', 'exec'));" | 2268 | "exec(compile(__pyfile.read(), '''%s''', 'exec'));" |
| 2274 | "__pyfile.close()") | 2269 | "__pyfile.close()%s") |
| 2275 | (or temp-file-name file-name) file-name) | 2270 | (or temp-file-name file-name) file-name |
| 2271 | (if delete (format "; import os; os.remove('''%s''')" | ||
| 2272 | (or temp-file-name file-name)) | ||
| 2273 | "")) | ||
| 2276 | process))) | 2274 | process))) |
| 2277 | 2275 | ||
| 2278 | (defun python-shell-switch-to-shell () | 2276 | (defun python-shell-switch-to-shell () |
| @@ -3615,6 +3613,7 @@ list is returned as is." | |||
| 3615 | (set (make-local-variable 'indent-region-function) #'python-indent-region) | 3613 | (set (make-local-variable 'indent-region-function) #'python-indent-region) |
| 3616 | ;; Because indentation is not redundant, we cannot safely reindent code. | 3614 | ;; Because indentation is not redundant, we cannot safely reindent code. |
| 3617 | (setq-local electric-indent-inhibit t) | 3615 | (setq-local electric-indent-inhibit t) |
| 3616 | (setq-local electric-indent-chars (cons ?: electric-indent-chars)) | ||
| 3618 | 3617 | ||
| 3619 | ;; Add """ ... """ pairing to electric-pair-mode. | 3618 | ;; Add """ ... """ pairing to electric-pair-mode. |
| 3620 | (add-hook 'post-self-insert-hook | 3619 | (add-hook 'post-self-insert-hook |
| @@ -3622,7 +3621,7 @@ list is returned as is." | |||
| 3622 | 3621 | ||
| 3623 | (set (make-local-variable 'paragraph-start) "\\s-*$") | 3622 | (set (make-local-variable 'paragraph-start) "\\s-*$") |
| 3624 | (set (make-local-variable 'fill-paragraph-function) | 3623 | (set (make-local-variable 'fill-paragraph-function) |
| 3625 | 'python-fill-paragraph) | 3624 | #'python-fill-paragraph) |
| 3626 | 3625 | ||
| 3627 | (set (make-local-variable 'beginning-of-defun-function) | 3626 | (set (make-local-variable 'beginning-of-defun-function) |
| 3628 | #'python-nav-beginning-of-defun) | 3627 | #'python-nav-beginning-of-defun) |
| @@ -3630,10 +3629,10 @@ list is returned as is." | |||
| 3630 | #'python-nav-end-of-defun) | 3629 | #'python-nav-end-of-defun) |
| 3631 | 3630 | ||
| 3632 | (add-hook 'completion-at-point-functions | 3631 | (add-hook 'completion-at-point-functions |
| 3633 | 'python-completion-complete-at-point nil 'local) | 3632 | #'python-completion-complete-at-point nil 'local) |
| 3634 | 3633 | ||
| 3635 | (add-hook 'post-self-insert-hook | 3634 | (add-hook 'post-self-insert-hook |
| 3636 | 'python-indent-post-self-insert-function nil 'local) | 3635 | #'python-indent-post-self-insert-function 'append 'local) |
| 3637 | 3636 | ||
| 3638 | (set (make-local-variable 'imenu-create-index-function) | 3637 | (set (make-local-variable 'imenu-create-index-function) |
| 3639 | #'python-imenu-create-index) | 3638 | #'python-imenu-create-index) |