diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 569 |
1 files changed, 343 insertions, 226 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 5f919bf495f..436442da2d0 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | ;;; python.el --- Python's flying circus support for Emacs | 1 | ;;; python.el --- Python's flying circus support for Emacs -*- lexical-binding: t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2003-2013 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2003-2014 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; Author: Fabián E. Gallina <fabian@anue.biz> | 5 | ;; Author: Fabián E. Gallina <fabian@anue.biz> |
| 6 | ;; URL: https://github.com/fgallina/python.el | 6 | ;; URL: https://github.com/fgallina/python.el |
| 7 | ;; Version: 0.24.2 | 7 | ;; Version: 0.24.2 |
| 8 | ;; Maintainer: FSF | 8 | ;; Maintainer: emacs-devel@gnu.org |
| 9 | ;; Created: Jul 2010 | 9 | ;; Created: Jul 2010 |
| 10 | ;; Keywords: languages | 10 | ;; Keywords: languages |
| 11 | 11 | ||
| @@ -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 |
| @@ -180,7 +180,7 @@ | |||
| 180 | ;; Imenu: There are two index building functions to be used as | 180 | ;; Imenu: There are two index building functions to be used as |
| 181 | ;; `imenu-create-index-function': `python-imenu-create-index' (the | 181 | ;; `imenu-create-index-function': `python-imenu-create-index' (the |
| 182 | ;; default one, builds the alist in form of a tree) and | 182 | ;; default one, builds the alist in form of a tree) and |
| 183 | ;; `python-imenu-create-flat-index'. See also | 183 | ;; `python-imenu-create-flat-index'. See also |
| 184 | ;; `python-imenu-format-item-label-function', | 184 | ;; `python-imenu-format-item-label-function', |
| 185 | ;; `python-imenu-format-parent-item-label-function', | 185 | ;; `python-imenu-format-parent-item-label-function', |
| 186 | ;; `python-imenu-format-parent-item-jump-label-function' variables for | 186 | ;; `python-imenu-format-parent-item-jump-label-function' variables for |
| @@ -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) |
| @@ -358,7 +357,7 @@ | |||
| 358 | 357 | ||
| 359 | (defmacro python-rx (&rest regexps) | 358 | (defmacro python-rx (&rest regexps) |
| 360 | "Python mode specialized rx macro. | 359 | "Python mode specialized rx macro. |
| 361 | This variant of `rx' supports common python named REGEXPS." | 360 | This variant of `rx' supports common Python named REGEXPS." |
| 362 | (let ((rx-constituents (append python-rx-constituents rx-constituents))) | 361 | (let ((rx-constituents (append python-rx-constituents rx-constituents))) |
| 363 | (cond ((null regexps) | 362 | (cond ((null regexps) |
| 364 | (error "No regexp")) | 363 | (error "No regexp")) |
| @@ -529,7 +528,7 @@ The type returned can be `comment', `string' or `paren'." | |||
| 529 | (defsubst python-syntax-count-quotes (quote-char &optional point limit) | 528 | (defsubst python-syntax-count-quotes (quote-char &optional point limit) |
| 530 | "Count number of quotes around point (max is 3). | 529 | "Count number of quotes around point (max is 3). |
| 531 | QUOTE-CHAR is the quote char to count. Optional argument POINT is | 530 | QUOTE-CHAR is the quote char to count. Optional argument POINT is |
| 532 | the point where scan starts (defaults to current point) and LIMIT | 531 | the point where scan starts (defaults to current point), and LIMIT |
| 533 | is used to limit the scan." | 532 | is used to limit the scan." |
| 534 | (let ((i 0)) | 533 | (let ((i 0)) |
| 535 | (while (and (< i 3) | 534 | (while (and (< i 3) |
| @@ -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))))))) |
| @@ -684,9 +683,11 @@ AFTER-LINE.") | |||
| 684 | (defun python-indent-context () | 683 | (defun python-indent-context () |
| 685 | "Get information on indentation context. | 684 | "Get information on indentation context. |
| 686 | Context information is returned with a cons with the form: | 685 | Context information is returned with a cons with the form: |
| 687 | \(STATUS . START) | 686 | (STATUS . START) |
| 688 | 687 | ||
| 689 | Where status can be any of the following symbols: | 688 | Where status can be any of the following symbols: |
| 689 | |||
| 690 | * after-comment: When current line might continue a comment block | ||
| 690 | * inside-paren: If point in between (), {} or [] | 691 | * inside-paren: If point in between (), {} or [] |
| 691 | * inside-string: If point is inside a string | 692 | * inside-string: If point is inside a string |
| 692 | * after-backslash: Previous line ends in a backslash | 693 | * after-backslash: Previous line ends in a backslash |
| @@ -705,6 +706,17 @@ START is the buffer position where the sexp starts." | |||
| 705 | (goto-char (line-beginning-position)) | 706 | (goto-char (line-beginning-position)) |
| 706 | (bobp)) | 707 | (bobp)) |
| 707 | 'no-indent) | 708 | 'no-indent) |
| 709 | ;; Comment continuation | ||
| 710 | ((save-excursion | ||
| 711 | (when (and | ||
| 712 | (or | ||
| 713 | (python-info-current-line-comment-p) | ||
| 714 | (python-info-current-line-empty-p)) | ||
| 715 | (progn | ||
| 716 | (forward-comment -1) | ||
| 717 | (python-info-current-line-comment-p))) | ||
| 718 | (setq start (point)) | ||
| 719 | 'after-comment))) | ||
| 708 | ;; Inside string | 720 | ;; Inside string |
| 709 | ((setq start (python-syntax-context 'string ppss)) | 721 | ((setq start (python-syntax-context 'string ppss)) |
| 710 | 'inside-string) | 722 | 'inside-string) |
| @@ -756,6 +768,9 @@ START is the buffer position where the sexp starts." | |||
| 756 | (save-excursion | 768 | (save-excursion |
| 757 | (pcase context-status | 769 | (pcase context-status |
| 758 | (`no-indent 0) | 770 | (`no-indent 0) |
| 771 | (`after-comment | ||
| 772 | (goto-char context-start) | ||
| 773 | (current-indentation)) | ||
| 759 | ;; When point is after beginning of block just add one level | 774 | ;; When point is after beginning of block just add one level |
| 760 | ;; of indentation relative to the context-start | 775 | ;; of indentation relative to the context-start |
| 761 | (`after-beginning-of-block | 776 | (`after-beginning-of-block |
| @@ -765,19 +780,31 @@ START is the buffer position where the sexp starts." | |||
| 765 | ;; indentation, in the case current line starts with a | 780 | ;; indentation, in the case current line starts with a |
| 766 | ;; `python-indent-dedenters' de-indent one level. | 781 | ;; `python-indent-dedenters' de-indent one level. |
| 767 | (`after-line | 782 | (`after-line |
| 768 | (- | 783 | (let* ((pair (save-excursion |
| 769 | (save-excursion | 784 | (goto-char context-start) |
| 770 | (goto-char context-start) | 785 | (cons |
| 771 | (current-indentation)) | 786 | (current-indentation) |
| 772 | (if (or (save-excursion | 787 | (python-info-beginning-of-block-p)))) |
| 773 | (back-to-indentation) | 788 | (context-indentation (car pair)) |
| 774 | (looking-at (regexp-opt python-indent-dedenters))) | 789 | (after-block-start-p (cdr pair)) |
| 775 | (save-excursion | 790 | (adjustment |
| 776 | (python-util-forward-comment -1) | 791 | (if (or (save-excursion |
| 777 | (python-nav-beginning-of-statement) | 792 | (back-to-indentation) |
| 778 | (member (current-word) python-indent-block-enders))) | 793 | (and |
| 779 | python-indent-offset | 794 | ;; De-indent only when dedenters are not |
| 780 | 0))) | 795 | ;; next to a block start. This allows |
| 796 | ;; one-liner constructs such as: | ||
| 797 | ;; if condition: print "yay" | ||
| 798 | ;; else: print "wry" | ||
| 799 | (not after-block-start-p) | ||
| 800 | (looking-at (regexp-opt python-indent-dedenters)))) | ||
| 801 | (save-excursion | ||
| 802 | (python-util-forward-comment -1) | ||
| 803 | (python-nav-beginning-of-statement) | ||
| 804 | (looking-at (regexp-opt python-indent-block-enders)))) | ||
| 805 | python-indent-offset | ||
| 806 | 0))) | ||
| 807 | (- context-indentation adjustment))) | ||
| 781 | ;; When inside of a string, do nothing. just use the current | 808 | ;; When inside of a string, do nothing. just use the current |
| 782 | ;; indentation. XXX: perhaps it would be a good idea to | 809 | ;; indentation. XXX: perhaps it would be a good idea to |
| 783 | ;; invoke standard text indentation here | 810 | ;; invoke standard text indentation here |
| @@ -936,11 +963,11 @@ variable `python-indent-current-level'. | |||
| 936 | 963 | ||
| 937 | When the variable `last-command' is not equal to one of the | 964 | When the variable `last-command' is not equal to one of the |
| 938 | symbols inside `python-indent-trigger-commands' and FORCE-TOGGLE | 965 | symbols inside `python-indent-trigger-commands' and FORCE-TOGGLE |
| 939 | is nil it calculates possible indentation levels and saves it in | 966 | is nil it calculates possible indentation levels and saves them |
| 940 | the variable `python-indent-levels'. Afterwards it sets the | 967 | in the variable `python-indent-levels'. Afterwards it sets the |
| 941 | variable `python-indent-current-level' correctly so offset is | 968 | variable `python-indent-current-level' correctly so offset is |
| 942 | equal to (`nth' `python-indent-current-level' | 969 | equal to |
| 943 | `python-indent-levels')" | 970 | (nth python-indent-current-level python-indent-levels)" |
| 944 | (or | 971 | (or |
| 945 | (and (or (and (memq this-command python-indent-trigger-commands) | 972 | (and (or (and (memq this-command python-indent-trigger-commands) |
| 946 | (eq last-command this-command)) | 973 | (eq last-command this-command)) |
| @@ -983,14 +1010,14 @@ See `python-indent-line' for details." | |||
| 983 | (defun python-indent-dedent-line-backspace (arg) | 1010 | (defun python-indent-dedent-line-backspace (arg) |
| 984 | "De-indent current line. | 1011 | "De-indent current line. |
| 985 | Argument ARG is passed to `backward-delete-char-untabify' when | 1012 | Argument ARG is passed to `backward-delete-char-untabify' when |
| 986 | point is not in between the indentation." | 1013 | point is not in between the indentation." |
| 987 | (interactive "*p") | 1014 | (interactive "*p") |
| 988 | (when (not (python-indent-dedent-line)) | 1015 | (when (not (python-indent-dedent-line)) |
| 989 | (backward-delete-char-untabify arg))) | 1016 | (backward-delete-char-untabify arg))) |
| 990 | (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede) | 1017 | (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede) |
| 991 | 1018 | ||
| 992 | (defun python-indent-region (start end) | 1019 | (defun python-indent-region (start end) |
| 993 | "Indent a python region automagically. | 1020 | "Indent a Python region automagically. |
| 994 | 1021 | ||
| 995 | Called from a program, START and END specify the region to indent." | 1022 | Called from a program, START and END specify the region to indent." |
| 996 | (let ((deactivate-mark nil)) | 1023 | (let ((deactivate-mark nil)) |
| @@ -1049,7 +1076,7 @@ any lines in the region are indented less than COUNT columns." | |||
| 1049 | (add-to-list 'debug-ignored-errors "^Can't shift all lines enough") | 1076 | (add-to-list 'debug-ignored-errors "^Can't shift all lines enough") |
| 1050 | 1077 | ||
| 1051 | (defun python-indent-shift-right (start end &optional count) | 1078 | (defun python-indent-shift-right (start end &optional count) |
| 1052 | "Shift lines contained in region START END by COUNT columns to the left. | 1079 | "Shift lines contained in region START END by COUNT columns to the right. |
| 1053 | COUNT defaults to `python-indent-offset'. If region isn't | 1080 | COUNT defaults to `python-indent-offset'. If region isn't |
| 1054 | active, the current line is shifted. The shifted region includes | 1081 | active, the current line is shifted. The shifted region includes |
| 1055 | the lines in which START and END lie." | 1082 | the lines in which START and END lie." |
| @@ -1058,48 +1085,42 @@ the lines in which START and END lie." | |||
| 1058 | (list (region-beginning) (region-end) current-prefix-arg) | 1085 | (list (region-beginning) (region-end) current-prefix-arg) |
| 1059 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | 1086 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) |
| 1060 | (let ((deactivate-mark nil)) | 1087 | (let ((deactivate-mark nil)) |
| 1061 | (if count | 1088 | (setq count (if count (prefix-numeric-value count) |
| 1062 | (setq count (prefix-numeric-value count)) | 1089 | python-indent-offset)) |
| 1063 | (setq count python-indent-offset)) | ||
| 1064 | (indent-rigidly start end count))) | 1090 | (indent-rigidly start end count))) |
| 1065 | 1091 | ||
| 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 () | 1092 | (defun python-indent-post-self-insert-function () |
| 1087 | "Adjust closing paren line indentation after a char is added. | 1093 | "Adjust indentation after insertion of some characters. |
| 1088 | This function is intended to be added to the | 1094 | This function is intended to be added to `post-self-insert-hook.' |
| 1089 | `post-self-insert-hook.' If a line renders a paren alone, after | 1095 | If a line renders a paren alone, after adding a char before it, |
| 1090 | adding a char before it, the line will be re-indented | 1096 | the line will be re-indented automatically if needed." |
| 1091 | automatically if needed." | 1097 | (when (and electric-indent-mode |
| 1092 | (when (and (eq (char-before) last-command-event) | 1098 | (eq (char-before) last-command-event)) |
| 1093 | (not (bolp)) | 1099 | (cond |
| 1094 | (memq (char-after) '(?\) ?\] ?\}))) | 1100 | ((and (not (bolp)) |
| 1095 | (save-excursion | 1101 | (memq (char-after) '(?\) ?\] ?\}))) |
| 1096 | (goto-char (line-beginning-position)) | 1102 | (save-excursion |
| 1097 | ;; If after going to the beginning of line the point | 1103 | (goto-char (line-beginning-position)) |
| 1098 | ;; is still inside a paren it's ok to do the trick | 1104 | ;; If after going to the beginning of line the point |
| 1099 | (when (python-syntax-context 'paren) | 1105 | ;; is still inside a paren it's ok to do the trick |
| 1100 | (let ((indentation (python-indent-calculate-indentation))) | 1106 | (when (python-syntax-context 'paren) |
| 1101 | (when (< (current-indentation) indentation) | 1107 | (let ((indentation (python-indent-calculate-indentation))) |
| 1102 | (indent-line-to indentation))))))) | 1108 | (when (< (current-indentation) indentation) |
| 1109 | (indent-line-to indentation)))))) | ||
| 1110 | ((and (eq ?: last-command-event) | ||
| 1111 | (memq ?: electric-indent-chars) | ||
| 1112 | (not current-prefix-arg) | ||
| 1113 | (eolp) | ||
| 1114 | (not (equal ?: (char-before (1- (point))))) | ||
| 1115 | (not (python-syntax-comment-or-string-p))) | ||
| 1116 | (let ((indentation (current-indentation)) | ||
| 1117 | (calculated-indentation (python-indent-calculate-indentation))) | ||
| 1118 | (python-info-closing-block-message) | ||
| 1119 | (when (> indentation calculated-indentation) | ||
| 1120 | (save-excursion | ||
| 1121 | (indent-line-to calculated-indentation) | ||
| 1122 | (when (not (python-info-closing-block-message)) | ||
| 1123 | (indent-line-to indentation))))))))) | ||
| 1103 | 1124 | ||
| 1104 | 1125 | ||
| 1105 | ;;; Navigation | 1126 | ;;; Navigation |
| @@ -1154,20 +1175,20 @@ With positive ARG search backwards, else search forwards." | |||
| 1154 | 1175 | ||
| 1155 | (defun python-nav-beginning-of-defun (&optional arg) | 1176 | (defun python-nav-beginning-of-defun (&optional arg) |
| 1156 | "Move point to `beginning-of-defun'. | 1177 | "Move point to `beginning-of-defun'. |
| 1157 | With positive ARG search backwards else search forward. When ARG | 1178 | With positive ARG search backwards else search forward. |
| 1158 | is nil or 0 defaults to 1. When searching backwards nested | 1179 | ARG nil or 0 defaults to 1. When searching backwards, |
| 1159 | defuns are handled with care depending on current point | 1180 | nested defuns are handled with care depending on current |
| 1160 | position. Return non-nil if point is moved to | 1181 | point position. Return non-nil if point is moved to |
| 1161 | `beginning-of-defun'." | 1182 | `beginning-of-defun'." |
| 1162 | (when (or (null arg) (= arg 0)) (setq arg 1)) | 1183 | (when (or (null arg) (= arg 0)) (setq arg 1)) |
| 1163 | (let ((found)) | 1184 | (let ((found)) |
| 1164 | (cond ((and (eq this-command 'mark-defun) | 1185 | (while (and (not (= arg 0)) |
| 1165 | (python-info-looking-at-beginning-of-defun))) | 1186 | (let ((keep-searching-p |
| 1166 | (t | 1187 | (python-nav--beginning-of-defun arg))) |
| 1167 | (dotimes (i (if (> arg 0) arg (- arg))) | 1188 | (when (and keep-searching-p (null found)) |
| 1168 | (when (and (python-nav--beginning-of-defun arg) | 1189 | (setq found t)) |
| 1169 | (not found)) | 1190 | keep-searching-p)) |
| 1170 | (setq found t))))) | 1191 | (setq arg (if (> arg 0) (1- arg) (1+ arg)))) |
| 1171 | found)) | 1192 | found)) |
| 1172 | 1193 | ||
| 1173 | (defun python-nav-end-of-defun () | 1194 | (defun python-nav-end-of-defun () |
| @@ -1257,15 +1278,21 @@ nested definitions." | |||
| 1257 | (defun python-nav-beginning-of-statement () | 1278 | (defun python-nav-beginning-of-statement () |
| 1258 | "Move to start of current statement." | 1279 | "Move to start of current statement." |
| 1259 | (interactive "^") | 1280 | (interactive "^") |
| 1260 | (while (and (or (back-to-indentation) t) | 1281 | (back-to-indentation) |
| 1261 | (not (bobp)) | 1282 | (let* ((ppss (syntax-ppss)) |
| 1262 | (when (or | 1283 | (context-point |
| 1263 | (save-excursion | 1284 | (or |
| 1264 | (forward-line -1) | 1285 | (python-syntax-context 'paren ppss) |
| 1265 | (python-info-line-ends-backslash-p)) | 1286 | (python-syntax-context 'string ppss)))) |
| 1266 | (python-syntax-context 'string) | 1287 | (cond ((bobp)) |
| 1267 | (python-syntax-context 'paren)) | 1288 | (context-point |
| 1268 | (forward-line -1)))) | 1289 | (goto-char context-point) |
| 1290 | (python-nav-beginning-of-statement)) | ||
| 1291 | ((save-excursion | ||
| 1292 | (forward-line -1) | ||
| 1293 | (python-info-line-ends-backslash-p)) | ||
| 1294 | (forward-line -1) | ||
| 1295 | (python-nav-beginning-of-statement)))) | ||
| 1269 | (point-marker)) | 1296 | (point-marker)) |
| 1270 | 1297 | ||
| 1271 | (defun python-nav-end-of-statement (&optional noend) | 1298 | (defun python-nav-end-of-statement (&optional noend) |
| @@ -1396,25 +1423,36 @@ backward to previous block." | |||
| 1396 | (and (goto-char starting-pos) nil) | 1423 | (and (goto-char starting-pos) nil) |
| 1397 | (and (not (= (point) starting-pos)) (point-marker))))) | 1424 | (and (not (= (point) starting-pos)) (point-marker))))) |
| 1398 | 1425 | ||
| 1399 | (defun python-nav-lisp-forward-sexp-safe (&optional arg) | 1426 | (defun python-nav--lisp-forward-sexp (&optional arg) |
| 1400 | "Safe version of standard `forward-sexp'. | 1427 | "Standard version `forward-sexp'. |
| 1401 | When ARG > 0 move forward, else if ARG is < 0." | 1428 | It ignores completely the value of `forward-sexp-function' by |
| 1402 | (or arg (setq arg 1)) | 1429 | setting it to nil before calling `forward-sexp'. With positive |
| 1430 | ARG move forward only one sexp, else move backwards." | ||
| 1403 | (let ((forward-sexp-function) | 1431 | (let ((forward-sexp-function) |
| 1404 | (paren-regexp | 1432 | (arg (if (or (not arg) (> arg 0)) 1 -1))) |
| 1405 | (if (> arg 0) (python-rx close-paren) (python-rx open-paren))) | 1433 | (forward-sexp arg))) |
| 1406 | (search-fn | 1434 | |
| 1407 | (if (> arg 0) #'re-search-forward #'re-search-backward))) | 1435 | (defun python-nav--lisp-forward-sexp-safe (&optional arg) |
| 1436 | "Safe version of standard `forward-sexp'. | ||
| 1437 | When at end of sexp (i.e. looking at a opening/closing paren) | ||
| 1438 | skips it instead of throwing an error. With positive ARG move | ||
| 1439 | forward only one sexp, else move backwards." | ||
| 1440 | (let* ((arg (if (or (not arg) (> arg 0)) 1 -1)) | ||
| 1441 | (paren-regexp | ||
| 1442 | (if (> arg 0) (python-rx close-paren) (python-rx open-paren))) | ||
| 1443 | (search-fn | ||
| 1444 | (if (> arg 0) #'re-search-forward #'re-search-backward))) | ||
| 1408 | (condition-case nil | 1445 | (condition-case nil |
| 1409 | (forward-sexp arg) | 1446 | (python-nav--lisp-forward-sexp arg) |
| 1410 | (error | 1447 | (error |
| 1411 | (while (and (funcall search-fn paren-regexp nil t) | 1448 | (while (and (funcall search-fn paren-regexp nil t) |
| 1412 | (python-syntax-context 'paren))))))) | 1449 | (python-syntax-context 'paren))))))) |
| 1413 | 1450 | ||
| 1414 | (defun python-nav--forward-sexp (&optional dir) | 1451 | (defun python-nav--forward-sexp (&optional dir safe) |
| 1415 | "Move to forward sexp. | 1452 | "Move to forward sexp. |
| 1416 | With positive Optional argument DIR direction move forward, else | 1453 | With positive optional argument DIR direction move forward, else |
| 1417 | backwards." | 1454 | backwards. When optional argument SAFE is non-nil do not throw |
| 1455 | errors when at end of sexp, skip it instead." | ||
| 1418 | (setq dir (or dir 1)) | 1456 | (setq dir (or dir 1)) |
| 1419 | (unless (= dir 0) | 1457 | (unless (= dir 0) |
| 1420 | (let* ((forward-p (if (> dir 0) | 1458 | (let* ((forward-p (if (> dir 0) |
| @@ -1432,7 +1470,9 @@ backwards." | |||
| 1432 | (eq (syntax-class (syntax-after (1- (point)))) | 1470 | (eq (syntax-class (syntax-after (1- (point)))) |
| 1433 | (car (string-to-syntax ")"))))) | 1471 | (car (string-to-syntax ")"))))) |
| 1434 | ;; Inside a paren or looking at it, lisp knows what to do. | 1472 | ;; Inside a paren or looking at it, lisp knows what to do. |
| 1435 | (python-nav-lisp-forward-sexp-safe dir)) | 1473 | (if safe |
| 1474 | (python-nav--lisp-forward-sexp-safe dir) | ||
| 1475 | (python-nav--lisp-forward-sexp dir))) | ||
| 1436 | (t | 1476 | (t |
| 1437 | ;; This part handles the lispy feel of | 1477 | ;; This part handles the lispy feel of |
| 1438 | ;; `python-nav-forward-sexp'. Knowing everything about the | 1478 | ;; `python-nav-forward-sexp'. Knowing everything about the |
| @@ -1446,7 +1486,9 @@ backwards." | |||
| 1446 | ((python-info-end-of-statement-p) 'statement-end))) | 1486 | ((python-info-end-of-statement-p) 'statement-end))) |
| 1447 | (next-sexp-pos | 1487 | (next-sexp-pos |
| 1448 | (save-excursion | 1488 | (save-excursion |
| 1449 | (python-nav-lisp-forward-sexp-safe dir) | 1489 | (if safe |
| 1490 | (python-nav--lisp-forward-sexp-safe dir) | ||
| 1491 | (python-nav--lisp-forward-sexp dir)) | ||
| 1450 | (point))) | 1492 | (point))) |
| 1451 | (next-sexp-context | 1493 | (next-sexp-context |
| 1452 | (save-excursion | 1494 | (save-excursion |
| @@ -1500,23 +1542,48 @@ backwards." | |||
| 1500 | (python-nav-beginning-of-statement)) | 1542 | (python-nav-beginning-of-statement)) |
| 1501 | (t (goto-char next-sexp-pos)))))))))) | 1543 | (t (goto-char next-sexp-pos)))))))))) |
| 1502 | 1544 | ||
| 1503 | (defun python-nav--backward-sexp () | ||
| 1504 | "Move to backward sexp." | ||
| 1505 | (python-nav--forward-sexp -1)) | ||
| 1506 | |||
| 1507 | (defun python-nav-forward-sexp (&optional arg) | 1545 | (defun python-nav-forward-sexp (&optional arg) |
| 1508 | "Move forward across one block of code. | 1546 | "Move forward across expressions. |
| 1509 | With ARG, do it that many times. Negative arg -N means | 1547 | With ARG, do it that many times. Negative arg -N means move |
| 1510 | move backward N times." | 1548 | backward N times." |
| 1549 | (interactive "^p") | ||
| 1550 | (or arg (setq arg 1)) | ||
| 1551 | (while (> arg 0) | ||
| 1552 | (python-nav--forward-sexp 1) | ||
| 1553 | (setq arg (1- arg))) | ||
| 1554 | (while (< arg 0) | ||
| 1555 | (python-nav--forward-sexp -1) | ||
| 1556 | (setq arg (1+ arg)))) | ||
| 1557 | |||
| 1558 | (defun python-nav-backward-sexp (&optional arg) | ||
| 1559 | "Move backward across expressions. | ||
| 1560 | With ARG, do it that many times. Negative arg -N means move | ||
| 1561 | forward N times." | ||
| 1562 | (interactive "^p") | ||
| 1563 | (or arg (setq arg 1)) | ||
| 1564 | (python-nav-forward-sexp (- arg))) | ||
| 1565 | |||
| 1566 | (defun python-nav-forward-sexp-safe (&optional arg) | ||
| 1567 | "Move forward safely across expressions. | ||
| 1568 | With ARG, do it that many times. Negative arg -N means move | ||
| 1569 | backward N times." | ||
| 1511 | (interactive "^p") | 1570 | (interactive "^p") |
| 1512 | (or arg (setq arg 1)) | 1571 | (or arg (setq arg 1)) |
| 1513 | (while (> arg 0) | 1572 | (while (> arg 0) |
| 1514 | (python-nav--forward-sexp) | 1573 | (python-nav--forward-sexp 1 t) |
| 1515 | (setq arg (1- arg))) | 1574 | (setq arg (1- arg))) |
| 1516 | (while (< arg 0) | 1575 | (while (< arg 0) |
| 1517 | (python-nav--backward-sexp) | 1576 | (python-nav--forward-sexp -1 t) |
| 1518 | (setq arg (1+ arg)))) | 1577 | (setq arg (1+ arg)))) |
| 1519 | 1578 | ||
| 1579 | (defun python-nav-backward-sexp-safe (&optional arg) | ||
| 1580 | "Move backward safely across expressions. | ||
| 1581 | With ARG, do it that many times. Negative arg -N means move | ||
| 1582 | forward N times." | ||
| 1583 | (interactive "^p") | ||
| 1584 | (or arg (setq arg 1)) | ||
| 1585 | (python-nav-forward-sexp-safe (- arg))) | ||
| 1586 | |||
| 1520 | (defun python-nav--up-list (&optional dir) | 1587 | (defun python-nav--up-list (&optional dir) |
| 1521 | "Internal implementation of `python-nav-up-list'. | 1588 | "Internal implementation of `python-nav-up-list'. |
| 1522 | DIR is always 1 or -1 and comes sanitized from | 1589 | DIR is always 1 or -1 and comes sanitized from |
| @@ -1572,7 +1639,7 @@ This command assumes point is not in a string or comment." | |||
| 1572 | (defun python-nav-backward-up-list (&optional arg) | 1639 | (defun python-nav-backward-up-list (&optional arg) |
| 1573 | "Move backward out of one level of parentheses (or blocks). | 1640 | "Move backward out of one level of parentheses (or blocks). |
| 1574 | With ARG, do this that many times. | 1641 | With ARG, do this that many times. |
| 1575 | A negative argument means move backward but still to a less deep spot. | 1642 | A negative argument means move forward but still to a less deep spot. |
| 1576 | This command assumes point is not in a string or comment." | 1643 | This command assumes point is not in a string or comment." |
| 1577 | (interactive "^p") | 1644 | (interactive "^p") |
| 1578 | (or arg (setq arg 1)) | 1645 | (or arg (setq arg 1)) |
| @@ -1627,36 +1694,36 @@ position, else returns nil." | |||
| 1627 | :group 'python) | 1694 | :group 'python) |
| 1628 | 1695 | ||
| 1629 | (defcustom python-shell-prompt-regexp ">>> " | 1696 | (defcustom python-shell-prompt-regexp ">>> " |
| 1630 | "Regular Expression matching top\-level input prompt of python shell. | 1697 | "Regular expression matching top-level input prompt of Python shell. |
| 1631 | It should not contain a caret (^) at the beginning." | 1698 | It should not contain a caret (^) at the beginning." |
| 1632 | :type 'string | 1699 | :type 'string |
| 1633 | :group 'python | 1700 | :group 'python |
| 1634 | :safe 'stringp) | 1701 | :safe 'stringp) |
| 1635 | 1702 | ||
| 1636 | (defcustom python-shell-prompt-block-regexp "[.][.][.] " | 1703 | (defcustom python-shell-prompt-block-regexp "[.][.][.] " |
| 1637 | "Regular Expression matching block input prompt of python shell. | 1704 | "Regular expression matching block input prompt of Python shell. |
| 1638 | It should not contain a caret (^) at the beginning." | 1705 | It should not contain a caret (^) at the beginning." |
| 1639 | :type 'string | 1706 | :type 'string |
| 1640 | :group 'python | 1707 | :group 'python |
| 1641 | :safe 'stringp) | 1708 | :safe 'stringp) |
| 1642 | 1709 | ||
| 1643 | (defcustom python-shell-prompt-output-regexp "" | 1710 | (defcustom python-shell-prompt-output-regexp "" |
| 1644 | "Regular Expression matching output prompt of python shell. | 1711 | "Regular expression matching output prompt of Python shell. |
| 1645 | It should not contain a caret (^) at the beginning." | 1712 | It should not contain a caret (^) at the beginning." |
| 1646 | :type 'string | 1713 | :type 'string |
| 1647 | :group 'python | 1714 | :group 'python |
| 1648 | :safe 'stringp) | 1715 | :safe 'stringp) |
| 1649 | 1716 | ||
| 1650 | (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ " | 1717 | (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ " |
| 1651 | "Regular Expression matching pdb input prompt of python shell. | 1718 | "Regular expression matching pdb input prompt of Python shell. |
| 1652 | It should not contain a caret (^) at the beginning." | 1719 | It should not contain a caret (^) at the beginning." |
| 1653 | :type 'string | 1720 | :type 'string |
| 1654 | :group 'python | 1721 | :group 'python |
| 1655 | :safe 'stringp) | 1722 | :safe 'stringp) |
| 1656 | 1723 | ||
| 1657 | (defcustom python-shell-enable-font-lock t | 1724 | (defcustom python-shell-enable-font-lock t |
| 1658 | "Should syntax highlighting be enabled in the python shell buffer? | 1725 | "Should syntax highlighting be enabled in the Python shell buffer? |
| 1659 | Restart the python shell after changing this variable for it to take effect." | 1726 | Restart the Python shell after changing this variable for it to take effect." |
| 1660 | :type 'boolean | 1727 | :type 'boolean |
| 1661 | :group 'python | 1728 | :group 'python |
| 1662 | :safe 'booleanp) | 1729 | :safe 'booleanp) |
| @@ -1822,9 +1889,9 @@ It makes parens and quotes be treated as punctuation chars.") | |||
| 1822 | (define-derived-mode inferior-python-mode comint-mode "Inferior Python" | 1889 | (define-derived-mode inferior-python-mode comint-mode "Inferior Python" |
| 1823 | "Major mode for Python inferior process. | 1890 | "Major mode for Python inferior process. |
| 1824 | Runs a Python interpreter as a subprocess of Emacs, with Python | 1891 | Runs a Python interpreter as a subprocess of Emacs, with Python |
| 1825 | I/O through an Emacs buffer. Variables | 1892 | I/O through an Emacs buffer. Variables `python-shell-interpreter' |
| 1826 | `python-shell-interpreter' and `python-shell-interpreter-args' | 1893 | and `python-shell-interpreter-args' control which Python |
| 1827 | controls which Python interpreter is run. Variables | 1894 | interpreter is run. Variables |
| 1828 | `python-shell-prompt-regexp', | 1895 | `python-shell-prompt-regexp', |
| 1829 | `python-shell-prompt-output-regexp', | 1896 | `python-shell-prompt-output-regexp', |
| 1830 | `python-shell-prompt-block-regexp', | 1897 | `python-shell-prompt-block-regexp', |
| @@ -1892,8 +1959,8 @@ variable. | |||
| 1892 | (compilation-shell-minor-mode 1)) | 1959 | (compilation-shell-minor-mode 1)) |
| 1893 | 1960 | ||
| 1894 | (defun python-shell-make-comint (cmd proc-name &optional pop internal) | 1961 | (defun python-shell-make-comint (cmd proc-name &optional pop internal) |
| 1895 | "Create a python shell comint buffer. | 1962 | "Create a Python shell comint buffer. |
| 1896 | CMD is the python command to be executed and PROC-NAME is the | 1963 | CMD is the Python command to be executed and PROC-NAME is the |
| 1897 | process name the comint buffer will get. After the comint buffer | 1964 | process name the comint buffer will get. After the comint buffer |
| 1898 | is created the `inferior-python-mode' is activated. When | 1965 | is created the `inferior-python-mode' is activated. When |
| 1899 | optional argument POP is non-nil the buffer is shown. When | 1966 | optional argument POP is non-nil the buffer is shown. When |
| @@ -1932,8 +1999,8 @@ command used to call the interpreter and define DEDICATED, so a | |||
| 1932 | dedicated process for the current buffer is open. When numeric | 1999 | dedicated process for the current buffer is open. When numeric |
| 1933 | prefix arg is other than 0 or 4 do not SHOW. | 2000 | prefix arg is other than 0 or 4 do not SHOW. |
| 1934 | 2001 | ||
| 1935 | Runs the hook `inferior-python-mode-hook' (after the | 2002 | Runs the hook `inferior-python-mode-hook' after |
| 1936 | `comint-mode-hook' is run). \(Type \\[describe-mode] in the | 2003 | `comint-mode-hook' is run. (Type \\[describe-mode] in the |
| 1937 | process buffer for a list of commands.)" | 2004 | process buffer for a list of commands.)" |
| 1938 | (interactive | 2005 | (interactive |
| 1939 | (if current-prefix-arg | 2006 | (if current-prefix-arg |
| @@ -1953,7 +2020,7 @@ Input and output via buffer named after | |||
| 1953 | `python-shell-internal-get-process-name' returns. | 2020 | `python-shell-internal-get-process-name' returns. |
| 1954 | 2021 | ||
| 1955 | This new kind of shell is intended to be used for generic | 2022 | This new kind of shell is intended to be used for generic |
| 1956 | communication related to defined configurations, the main | 2023 | communication related to defined configurations; the main |
| 1957 | difference with global or dedicated shells is that these ones are | 2024 | difference with global or dedicated shells is that these ones are |
| 1958 | attached to a configuration, not a buffer. This means that can | 2025 | attached to a configuration, not a buffer. This means that can |
| 1959 | be used for example to retrieve the sys.path and other stuff, | 2026 | be used for example to retrieve the sys.path and other stuff, |
| @@ -1968,8 +2035,8 @@ startup." | |||
| 1968 | (python-shell-parse-command) | 2035 | (python-shell-parse-command) |
| 1969 | (python-shell-internal-get-process-name) nil t)))) | 2036 | (python-shell-internal-get-process-name) nil t)))) |
| 1970 | 2037 | ||
| 1971 | (defun python-shell-get-process () | 2038 | (defun python-shell-get-buffer () |
| 1972 | "Get inferior Python process for current buffer and return it." | 2039 | "Return inferior Python buffer for current buffer." |
| 1973 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) | 2040 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) |
| 1974 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) | 2041 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) |
| 1975 | (global-proc-name (python-shell-get-process-name nil)) | 2042 | (global-proc-name (python-shell-get-process-name nil)) |
| @@ -1977,8 +2044,12 @@ startup." | |||
| 1977 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | 2044 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) |
| 1978 | (global-running (comint-check-proc global-proc-buffer-name))) | 2045 | (global-running (comint-check-proc global-proc-buffer-name))) |
| 1979 | ;; Always prefer dedicated | 2046 | ;; Always prefer dedicated |
| 1980 | (get-buffer-process (or (and dedicated-running dedicated-proc-buffer-name) | 2047 | (or (and dedicated-running dedicated-proc-buffer-name) |
| 1981 | (and global-running global-proc-buffer-name))))) | 2048 | (and global-running global-proc-buffer-name)))) |
| 2049 | |||
| 2050 | (defun python-shell-get-process () | ||
| 2051 | "Return inferior Python process for current buffer." | ||
| 2052 | (get-buffer-process (python-shell-get-buffer))) | ||
| 1982 | 2053 | ||
| 1983 | (defun python-shell-get-or-create-process () | 2054 | (defun python-shell-get-or-create-process () |
| 1984 | "Get or create an inferior Python process for current buffer and return it." | 2055 | "Get or create an inferior Python process for current buffer and return it." |
| @@ -1987,8 +2058,7 @@ startup." | |||
| 1987 | (global-proc-name (python-shell-get-process-name nil)) | 2058 | (global-proc-name (python-shell-get-process-name nil)) |
| 1988 | (global-proc-buffer-name (format "*%s*" global-proc-name)) | 2059 | (global-proc-buffer-name (format "*%s*" global-proc-name)) |
| 1989 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | 2060 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) |
| 1990 | (global-running (comint-check-proc global-proc-buffer-name)) | 2061 | (global-running (comint-check-proc global-proc-buffer-name))) |
| 1991 | (current-prefix-arg 16)) | ||
| 1992 | (when (and (not dedicated-running) (not global-running)) | 2062 | (when (and (not dedicated-running) (not global-running)) |
| 1993 | (if (call-interactively 'run-python) | 2063 | (if (call-interactively 'run-python) |
| 1994 | (setq dedicated-running t) | 2064 | (setq dedicated-running t) |
| @@ -2032,27 +2102,29 @@ there for compatibility with CEDET.") | |||
| 2032 | (define-obsolete-variable-alias | 2102 | (define-obsolete-variable-alias |
| 2033 | 'python-preoutput-result 'python-shell-internal-last-output "24.3") | 2103 | 'python-preoutput-result 'python-shell-internal-last-output "24.3") |
| 2034 | 2104 | ||
| 2035 | (defun python-shell-send-string (string &optional process msg) | 2105 | (defun python-shell--save-temp-file (string) |
| 2036 | "Send STRING to inferior Python PROCESS. | 2106 | (let* ((temporary-file-directory |
| 2037 | When MSG is non-nil messages the first line of STRING." | 2107 | (if (file-remote-p default-directory) |
| 2108 | (concat (file-remote-p default-directory) "/tmp") | ||
| 2109 | temporary-file-directory)) | ||
| 2110 | (temp-file-name (make-temp-file "py")) | ||
| 2111 | (coding-system-for-write 'utf-8)) | ||
| 2112 | (with-temp-file temp-file-name | ||
| 2113 | (insert "# -*- coding: utf-8 -*-\n") ;Not needed for Python-3. | ||
| 2114 | (insert string) | ||
| 2115 | (delete-trailing-whitespace)) | ||
| 2116 | temp-file-name)) | ||
| 2117 | |||
| 2118 | (defun python-shell-send-string (string &optional process) | ||
| 2119 | "Send STRING to inferior Python PROCESS." | ||
| 2038 | (interactive "sPython command: ") | 2120 | (interactive "sPython command: ") |
| 2039 | (let ((process (or process (python-shell-get-or-create-process))) | 2121 | (let ((process (or process (python-shell-get-or-create-process)))) |
| 2040 | (lines (split-string string "\n" t))) | 2122 | (if (string-match ".\n+." string) ;Multiline. |
| 2041 | (and msg (message "Sent: %s..." (nth 0 lines))) | 2123 | (let* ((temp-file-name (python-shell--save-temp-file string))) |
| 2042 | (if (> (length lines) 1) | 2124 | (python-shell-send-file temp-file-name process temp-file-name t)) |
| 2043 | (let* ((temporary-file-directory | ||
| 2044 | (if (file-remote-p default-directory) | ||
| 2045 | (concat (file-remote-p default-directory) "/tmp") | ||
| 2046 | temporary-file-directory)) | ||
| 2047 | (temp-file-name (make-temp-file "py")) | ||
| 2048 | (file-name (or (buffer-file-name) temp-file-name))) | ||
| 2049 | (with-temp-file temp-file-name | ||
| 2050 | (insert string) | ||
| 2051 | (delete-trailing-whitespace)) | ||
| 2052 | (python-shell-send-file file-name process temp-file-name)) | ||
| 2053 | (comint-send-string process string) | 2125 | (comint-send-string process string) |
| 2054 | (when (or (not (string-match "\n$" string)) | 2126 | (when (or (not (string-match "\n\\'" string)) |
| 2055 | (string-match "\n[ \t].*\n?$" string)) | 2127 | (string-match "\n[ \t].*\n?\\'" string)) |
| 2056 | (comint-send-string process "\n"))))) | 2128 | (comint-send-string process "\n"))))) |
| 2057 | 2129 | ||
| 2058 | (defvar python-shell-output-filter-in-progress nil) | 2130 | (defvar python-shell-output-filter-in-progress nil) |
| @@ -2091,10 +2163,9 @@ detecting a prompt at the end of the buffer." | |||
| 2091 | (substring python-shell-output-filter-buffer (match-end 0))))) | 2163 | (substring python-shell-output-filter-buffer (match-end 0))))) |
| 2092 | "") | 2164 | "") |
| 2093 | 2165 | ||
| 2094 | (defun python-shell-send-string-no-output (string &optional process msg) | 2166 | (defun python-shell-send-string-no-output (string &optional process) |
| 2095 | "Send STRING to PROCESS and inhibit output. | 2167 | "Send STRING to PROCESS and inhibit output. |
| 2096 | When MSG is non-nil messages the first line of STRING. Return | 2168 | Return the output." |
| 2097 | the output." | ||
| 2098 | (let ((process (or process (python-shell-get-or-create-process))) | 2169 | (let ((process (or process (python-shell-get-or-create-process))) |
| 2099 | (comint-preoutput-filter-functions | 2170 | (comint-preoutput-filter-functions |
| 2100 | '(python-shell-output-filter)) | 2171 | '(python-shell-output-filter)) |
| @@ -2102,7 +2173,7 @@ the output." | |||
| 2102 | (inhibit-quit t)) | 2173 | (inhibit-quit t)) |
| 2103 | (or | 2174 | (or |
| 2104 | (with-local-quit | 2175 | (with-local-quit |
| 2105 | (python-shell-send-string string process msg) | 2176 | (python-shell-send-string string process) |
| 2106 | (while python-shell-output-filter-in-progress | 2177 | (while python-shell-output-filter-in-progress |
| 2107 | ;; `python-shell-output-filter' takes care of setting | 2178 | ;; `python-shell-output-filter' takes care of setting |
| 2108 | ;; `python-shell-output-filter-in-progress' to NIL after it | 2179 | ;; `python-shell-output-filter-in-progress' to NIL after it |
| @@ -2124,7 +2195,7 @@ Returns the output. See `python-shell-send-string-no-output'." | |||
| 2124 | ;; Makes this function compatible with the old | 2195 | ;; Makes this function compatible with the old |
| 2125 | ;; python-send-receive. (At least for CEDET). | 2196 | ;; python-send-receive. (At least for CEDET). |
| 2126 | (replace-regexp-in-string "_emacs_out +" "" string) | 2197 | (replace-regexp-in-string "_emacs_out +" "" string) |
| 2127 | (python-shell-internal-get-or-create-process) nil))) | 2198 | (python-shell-internal-get-or-create-process)))) |
| 2128 | 2199 | ||
| 2129 | (define-obsolete-function-alias | 2200 | (define-obsolete-function-alias |
| 2130 | 'python-send-receive 'python-shell-internal-send-string "24.3") | 2201 | 'python-send-receive 'python-shell-internal-send-string "24.3") |
| @@ -2132,19 +2203,26 @@ Returns the output. See `python-shell-send-string-no-output'." | |||
| 2132 | (define-obsolete-function-alias | 2203 | (define-obsolete-function-alias |
| 2133 | 'python-send-string 'python-shell-internal-send-string "24.3") | 2204 | 'python-send-string 'python-shell-internal-send-string "24.3") |
| 2134 | 2205 | ||
| 2206 | (defvar python--use-fake-loc nil | ||
| 2207 | "If non-nil, use `compilation-fake-loc' to trace errors back to the buffer. | ||
| 2208 | If nil, regions of text are prepended by the corresponding number of empty | ||
| 2209 | lines and Python is told to output error messages referring to the whole | ||
| 2210 | source file.") | ||
| 2211 | |||
| 2135 | (defun python-shell-buffer-substring (start end &optional nomain) | 2212 | (defun python-shell-buffer-substring (start end &optional nomain) |
| 2136 | "Send buffer substring from START to END formatted for shell. | 2213 | "Send buffer substring from START to END formatted for shell. |
| 2137 | This is a wrapper over `buffer-substring' that takes care of | 2214 | This is a wrapper over `buffer-substring' that takes care of |
| 2138 | different transformations for the code sent to be evaluated in | 2215 | different transformations for the code sent to be evaluated in |
| 2139 | the python shell: | 2216 | the python shell: |
| 2140 | 1. When Optional Argument NOMAIN is non-nil everything under an | 2217 | 1. When optional argument NOMAIN is non-nil everything under an |
| 2141 | \"if __name__ == '__main__'\" block will be removed. | 2218 | \"if __name__ == '__main__'\" block will be removed. |
| 2142 | 2. When a subregion of the buffer is sent, it takes care of | 2219 | 2. When a subregion of the buffer is sent, it takes care of |
| 2143 | appending extra empty lines so tracebacks are correct. | 2220 | appending extra empty lines so tracebacks are correct. |
| 2144 | 3. Wraps indented regions under an \"if True:\" block so the | 2221 | 3. Wraps indented regions under an \"if True:\" block so the |
| 2145 | interpreter evaluates them correctly." | 2222 | interpreter evaluates them correctly." |
| 2146 | (let ((substring (buffer-substring-no-properties start end)) | 2223 | (let ((substring (buffer-substring-no-properties start end)) |
| 2147 | (fillstr (make-string (1- (line-number-at-pos start)) ?\n)) | 2224 | (fillstr (unless python--use-fake-loc |
| 2225 | (make-string (1- (line-number-at-pos start)) ?\n))) | ||
| 2148 | (toplevel-block-p (save-excursion | 2226 | (toplevel-block-p (save-excursion |
| 2149 | (goto-char start) | 2227 | (goto-char start) |
| 2150 | (or (zerop (line-number-at-pos start)) | 2228 | (or (zerop (line-number-at-pos start)) |
| @@ -2153,9 +2231,14 @@ the python shell: | |||
| 2153 | (zerop (current-indentation))))))) | 2231 | (zerop (current-indentation))))))) |
| 2154 | (with-temp-buffer | 2232 | (with-temp-buffer |
| 2155 | (python-mode) | 2233 | (python-mode) |
| 2156 | (insert fillstr) | 2234 | (if fillstr (insert fillstr)) |
| 2157 | (insert substring) | 2235 | (insert substring) |
| 2158 | (goto-char (point-min)) | 2236 | (goto-char (point-min)) |
| 2237 | (unless python--use-fake-loc | ||
| 2238 | ;; python-shell--save-temp-file adds an extra coding line, which would | ||
| 2239 | ;; throw off the line-counts, so let's try to compensate here. | ||
| 2240 | (if (looking-at "[ \t]*[#\n]") | ||
| 2241 | (delete-region (point) (line-beginning-position 2)))) | ||
| 2159 | (when (not toplevel-block-p) | 2242 | (when (not toplevel-block-p) |
| 2160 | (insert "if True:") | 2243 | (insert "if True:") |
| 2161 | (delete-region (point) (line-end-position))) | 2244 | (delete-region (point) (line-end-position))) |
| @@ -2165,7 +2248,7 @@ the python shell: | |||
| 2165 | (save-excursion | 2248 | (save-excursion |
| 2166 | (when (python-nav-if-name-main) | 2249 | (when (python-nav-if-name-main) |
| 2167 | (cons (point) | 2250 | (cons (point) |
| 2168 | (progn (python-nav-forward-sexp) | 2251 | (progn (python-nav-forward-sexp-safe) |
| 2169 | (point))))))) | 2252 | (point))))))) |
| 2170 | ;; Oh destructuring bind, how I miss you. | 2253 | ;; Oh destructuring bind, how I miss you. |
| 2171 | (if-name-main-start (car if-name-main-start-end)) | 2254 | (if-name-main-start (car if-name-main-start-end)) |
| @@ -2179,22 +2262,35 @@ the python shell: | |||
| 2179 | (line-number-at-pos if-name-main-start)) ?\n))))) | 2262 | (line-number-at-pos if-name-main-start)) ?\n))))) |
| 2180 | (buffer-substring-no-properties (point-min) (point-max))))) | 2263 | (buffer-substring-no-properties (point-min) (point-max))))) |
| 2181 | 2264 | ||
| 2182 | (defun python-shell-send-region (start end) | 2265 | (declare-function compilation-fake-loc "compile" |
| 2266 | (marker file &optional line col)) | ||
| 2267 | |||
| 2268 | (defun python-shell-send-region (start end &optional nomain) | ||
| 2183 | "Send the region delimited by START and END to inferior Python process." | 2269 | "Send the region delimited by START and END to inferior Python process." |
| 2184 | (interactive "r") | 2270 | (interactive "r") |
| 2185 | (python-shell-send-string | 2271 | (let* ((python--use-fake-loc |
| 2186 | (python-shell-buffer-substring start end) nil t)) | 2272 | (or python--use-fake-loc (not buffer-file-name))) |
| 2273 | (string (python-shell-buffer-substring start end nomain)) | ||
| 2274 | (process (python-shell-get-or-create-process)) | ||
| 2275 | (_ (string-match "\\`\n*\\(.*\\)" string))) | ||
| 2276 | (message "Sent: %s..." (match-string 1 string)) | ||
| 2277 | (let* ((temp-file-name (python-shell--save-temp-file string)) | ||
| 2278 | (file-name (or (buffer-file-name) temp-file-name))) | ||
| 2279 | (python-shell-send-file file-name process temp-file-name t) | ||
| 2280 | (unless python--use-fake-loc | ||
| 2281 | (with-current-buffer (process-buffer process) | ||
| 2282 | (compilation-fake-loc (copy-marker start) temp-file-name | ||
| 2283 | 2)) ;; Not 1, because of the added coding line. | ||
| 2284 | )))) | ||
| 2187 | 2285 | ||
| 2188 | (defun python-shell-send-buffer (&optional arg) | 2286 | (defun python-shell-send-buffer (&optional arg) |
| 2189 | "Send the entire buffer to inferior Python process. | 2287 | "Send the entire buffer to inferior Python process. |
| 2190 | With prefix ARG allow execution of code inside blocks delimited | 2288 | With prefix ARG allow execution of code inside blocks delimited |
| 2191 | by \"if __name__== '__main__':\"" | 2289 | by \"if __name__== '__main__':\"." |
| 2192 | (interactive "P") | 2290 | (interactive "P") |
| 2193 | (save-restriction | 2291 | (save-restriction |
| 2194 | (widen) | 2292 | (widen) |
| 2195 | (python-shell-send-string | 2293 | (python-shell-send-region (point-min) (point-max) (not arg)))) |
| 2196 | (python-shell-buffer-substring | ||
| 2197 | (point-min) (point-max) (not arg))))) | ||
| 2198 | 2294 | ||
| 2199 | (defun python-shell-send-defun (arg) | 2295 | (defun python-shell-send-defun (arg) |
| 2200 | "Send the current defun to inferior Python process. | 2296 | "Send the current defun to inferior Python process. |
| @@ -2217,11 +2313,12 @@ When argument ARG is non-nil do not include decorators." | |||
| 2217 | (end-of-line 1)) | 2313 | (end-of-line 1)) |
| 2218 | (point-marker))))) | 2314 | (point-marker))))) |
| 2219 | 2315 | ||
| 2220 | (defun python-shell-send-file (file-name &optional process temp-file-name) | 2316 | (defun python-shell-send-file (file-name &optional process temp-file-name |
| 2317 | delete) | ||
| 2221 | "Send FILE-NAME to inferior Python PROCESS. | 2318 | "Send FILE-NAME to inferior Python PROCESS. |
| 2222 | If TEMP-FILE-NAME is passed then that file is used for processing | 2319 | If TEMP-FILE-NAME is passed then that file is used for processing |
| 2223 | instead, while internally the shell will continue to use | 2320 | instead, while internally the shell will continue to use FILE-NAME. |
| 2224 | FILE-NAME." | 2321 | If DELETE is non-nil, delete the file afterwards." |
| 2225 | (interactive "fFile to send: ") | 2322 | (interactive "fFile to send: ") |
| 2226 | (let* ((process (or process (python-shell-get-or-create-process))) | 2323 | (let* ((process (or process (python-shell-get-or-create-process))) |
| 2227 | (temp-file-name (when temp-file-name | 2324 | (temp-file-name (when temp-file-name |
| @@ -2239,8 +2336,11 @@ FILE-NAME." | |||
| 2239 | (format | 2336 | (format |
| 2240 | (concat "__pyfile = open('''%s''');" | 2337 | (concat "__pyfile = open('''%s''');" |
| 2241 | "exec(compile(__pyfile.read(), '''%s''', 'exec'));" | 2338 | "exec(compile(__pyfile.read(), '''%s''', 'exec'));" |
| 2242 | "__pyfile.close()") | 2339 | "__pyfile.close()%s") |
| 2243 | (or temp-file-name file-name) file-name) | 2340 | (or temp-file-name file-name) file-name |
| 2341 | (if delete (format "; import os; os.remove('''%s''')" | ||
| 2342 | (or temp-file-name file-name)) | ||
| 2343 | "")) | ||
| 2244 | process))) | 2344 | process))) |
| 2245 | 2345 | ||
| 2246 | (defun python-shell-switch-to-shell () | 2346 | (defun python-shell-switch-to-shell () |
| @@ -2318,8 +2418,7 @@ and use the following as the value of this variable: | |||
| 2318 | 2418 | ||
| 2319 | (defun python-shell-completion-get-completions (process line input) | 2419 | (defun python-shell-completion-get-completions (process line input) |
| 2320 | "Do completion at point for PROCESS. | 2420 | "Do completion at point for PROCESS. |
| 2321 | LINE is used to detect the context on how to complete given | 2421 | LINE is used to detect the context on how to complete given INPUT." |
| 2322 | INPUT." | ||
| 2323 | (let* ((prompt | 2422 | (let* ((prompt |
| 2324 | ;; Get last prompt of the inferior process buffer (this | 2423 | ;; Get last prompt of the inferior process buffer (this |
| 2325 | ;; intentionally avoids using `comint-last-prompt' because | 2424 | ;; intentionally avoids using `comint-last-prompt' because |
| @@ -2405,8 +2504,8 @@ using that one instead of current buffer's process." | |||
| 2405 | 2504 | ||
| 2406 | (defun python-shell-completion-complete-or-indent () | 2505 | (defun python-shell-completion-complete-or-indent () |
| 2407 | "Complete or indent depending on the context. | 2506 | "Complete or indent depending on the context. |
| 2408 | If content before pointer is all whitespace indent. If not try | 2507 | If content before pointer is all whitespace, indent. |
| 2409 | to complete." | 2508 | If not try to complete." |
| 2410 | (interactive) | 2509 | (interactive) |
| 2411 | (if (string-match "^[[:space:]]*$" | 2510 | (if (string-match "^[[:space:]]*$" |
| 2412 | (buffer-substring (comint-line-beginning-position) | 2511 | (buffer-substring (comint-line-beginning-position) |
| @@ -2418,14 +2517,14 @@ to complete." | |||
| 2418 | ;;; PDB Track integration | 2517 | ;;; PDB Track integration |
| 2419 | 2518 | ||
| 2420 | (defcustom python-pdbtrack-activate t | 2519 | (defcustom python-pdbtrack-activate t |
| 2421 | "Non-nil makes python shell enable pdbtracking." | 2520 | "Non-nil makes Python shell enable pdbtracking." |
| 2422 | :type 'boolean | 2521 | :type 'boolean |
| 2423 | :group 'python | 2522 | :group 'python |
| 2424 | :safe 'booleanp) | 2523 | :safe 'booleanp) |
| 2425 | 2524 | ||
| 2426 | (defcustom python-pdbtrack-stacktrace-info-regexp | 2525 | (defcustom python-pdbtrack-stacktrace-info-regexp |
| 2427 | "^> \\([^\"(<]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()" | 2526 | "> \\([^\"(<]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()" |
| 2428 | "Regular Expression matching stacktrace information. | 2527 | "Regular expression matching stacktrace information. |
| 2429 | Used to extract the current line and module being inspected." | 2528 | Used to extract the current line and module being inspected." |
| 2430 | :type 'string | 2529 | :type 'string |
| 2431 | :group 'python | 2530 | :group 'python |
| @@ -2443,7 +2542,9 @@ Never set this variable directly, use | |||
| 2443 | "Set the buffer for FILE-NAME as the tracked buffer. | 2542 | "Set the buffer for FILE-NAME as the tracked buffer. |
| 2444 | Internally it uses the `python-pdbtrack-tracked-buffer' variable. | 2543 | Internally it uses the `python-pdbtrack-tracked-buffer' variable. |
| 2445 | Returns the tracked buffer." | 2544 | Returns the tracked buffer." |
| 2446 | (let ((file-buffer (get-file-buffer file-name))) | 2545 | (let ((file-buffer (get-file-buffer |
| 2546 | (concat (file-remote-p default-directory) | ||
| 2547 | file-name)))) | ||
| 2447 | (if file-buffer | 2548 | (if file-buffer |
| 2448 | (setq python-pdbtrack-tracked-buffer file-buffer) | 2549 | (setq python-pdbtrack-tracked-buffer file-buffer) |
| 2449 | (setq file-buffer (find-file-noselect file-name)) | 2550 | (setq file-buffer (find-file-noselect file-name)) |
| @@ -2509,9 +2610,9 @@ Argument OUTPUT is a string with the output from the comint process." | |||
| 2509 | 2610 | ||
| 2510 | (defun python-completion-complete-at-point () | 2611 | (defun python-completion-complete-at-point () |
| 2511 | "Complete current symbol at point. | 2612 | "Complete current symbol at point. |
| 2512 | For this to work the best as possible you should call | 2613 | For this to work as best as possible you should call |
| 2513 | `python-shell-send-buffer' from time to time so context in | 2614 | `python-shell-send-buffer' from time to time so context in |
| 2514 | inferior python process is updated properly." | 2615 | inferior Python process is updated properly." |
| 2515 | (let ((process (python-shell-get-process))) | 2616 | (let ((process (python-shell-get-process))) |
| 2516 | (if (not process) | 2617 | (if (not process) |
| 2517 | (error "Completion needs an inferior Python process running") | 2618 | (error "Completion needs an inferior Python process running") |
| @@ -2556,12 +2657,12 @@ fill parens." | |||
| 2556 | This affects `python-fill-string' behavior with regards to | 2657 | This affects `python-fill-string' behavior with regards to |
| 2557 | triple quotes positioning. | 2658 | triple quotes positioning. |
| 2558 | 2659 | ||
| 2559 | Possible values are DJANGO, ONETWO, PEP-257, PEP-257-NN, | 2660 | Possible values are `django', `onetwo', `pep-257', `pep-257-nn', |
| 2560 | SYMMETRIC, and NIL. A value of NIL won't care about quotes | 2661 | `symmetric', and nil. A value of nil won't care about quotes |
| 2561 | position and will treat docstrings a normal string, any other | 2662 | position and will treat docstrings a normal string, any other |
| 2562 | value may result in one of the following docstring styles: | 2663 | value may result in one of the following docstring styles: |
| 2563 | 2664 | ||
| 2564 | DJANGO: | 2665 | `django': |
| 2565 | 2666 | ||
| 2566 | \"\"\" | 2667 | \"\"\" |
| 2567 | Process foo, return bar. | 2668 | Process foo, return bar. |
| @@ -2573,7 +2674,7 @@ DJANGO: | |||
| 2573 | If processing fails throw ProcessingError. | 2674 | If processing fails throw ProcessingError. |
| 2574 | \"\"\" | 2675 | \"\"\" |
| 2575 | 2676 | ||
| 2576 | ONETWO: | 2677 | `onetwo': |
| 2577 | 2678 | ||
| 2578 | \"\"\"Process foo, return bar.\"\"\" | 2679 | \"\"\"Process foo, return bar.\"\"\" |
| 2579 | 2680 | ||
| @@ -2584,7 +2685,7 @@ ONETWO: | |||
| 2584 | 2685 | ||
| 2585 | \"\"\" | 2686 | \"\"\" |
| 2586 | 2687 | ||
| 2587 | PEP-257: | 2688 | `pep-257': |
| 2588 | 2689 | ||
| 2589 | \"\"\"Process foo, return bar.\"\"\" | 2690 | \"\"\"Process foo, return bar.\"\"\" |
| 2590 | 2691 | ||
| @@ -2594,7 +2695,7 @@ PEP-257: | |||
| 2594 | 2695 | ||
| 2595 | \"\"\" | 2696 | \"\"\" |
| 2596 | 2697 | ||
| 2597 | PEP-257-NN: | 2698 | `pep-257-nn': |
| 2598 | 2699 | ||
| 2599 | \"\"\"Process foo, return bar.\"\"\" | 2700 | \"\"\"Process foo, return bar.\"\"\" |
| 2600 | 2701 | ||
| @@ -2603,7 +2704,7 @@ PEP-257-NN: | |||
| 2603 | If processing fails throw ProcessingError. | 2704 | If processing fails throw ProcessingError. |
| 2604 | \"\"\" | 2705 | \"\"\" |
| 2605 | 2706 | ||
| 2606 | SYMMETRIC: | 2707 | `symmetric': |
| 2607 | 2708 | ||
| 2608 | \"\"\"Process foo, return bar.\"\"\" | 2709 | \"\"\"Process foo, return bar.\"\"\" |
| 2609 | 2710 | ||
| @@ -2960,8 +3061,8 @@ The skeleton will be bound to python-skeleton-NAME." | |||
| 2960 | 3061 | ||
| 2961 | (defun python-check (command) | 3062 | (defun python-check (command) |
| 2962 | "Check a Python file (default current buffer's file). | 3063 | "Check a Python file (default current buffer's file). |
| 2963 | Runs COMMAND, a shell command, as if by `compile'. See | 3064 | Runs COMMAND, a shell command, as if by `compile'. |
| 2964 | `python-check-command' for the default." | 3065 | See `python-check-command' for the default." |
| 2965 | (interactive | 3066 | (interactive |
| 2966 | (list (read-string "Check command: " | 3067 | (list (read-string "Check command: " |
| 2967 | (or python-check-custom-command | 3068 | (or python-check-custom-command |
| @@ -3026,10 +3127,9 @@ Runs COMMAND, a shell command, as if by `compile'. See | |||
| 3026 | 3127 | ||
| 3027 | (defun python-eldoc--get-doc-at-point (&optional force-input force-process) | 3128 | (defun python-eldoc--get-doc-at-point (&optional force-input force-process) |
| 3028 | "Internal implementation to get documentation at point. | 3129 | "Internal implementation to get documentation at point. |
| 3029 | If not FORCE-INPUT is passed then what | 3130 | If not FORCE-INPUT is passed then what `python-info-current-symbol' |
| 3030 | `python-info-current-symbol' returns will be used. If not | 3131 | returns will be used. If not FORCE-PROCESS is passed what |
| 3031 | FORCE-PROCESS is passed what `python-shell-get-process' returns | 3132 | `python-shell-get-process' returns is used." |
| 3032 | is used." | ||
| 3033 | (let ((process (or force-process (python-shell-get-process)))) | 3133 | (let ((process (or force-process (python-shell-get-process)))) |
| 3034 | (if (not process) | 3134 | (if (not process) |
| 3035 | (error "Eldoc needs an inferior Python process running") | 3135 | (error "Eldoc needs an inferior Python process running") |
| @@ -3042,9 +3142,9 @@ is used." | |||
| 3042 | 3142 | ||
| 3043 | (defun python-eldoc-function () | 3143 | (defun python-eldoc-function () |
| 3044 | "`eldoc-documentation-function' for Python. | 3144 | "`eldoc-documentation-function' for Python. |
| 3045 | For this to work the best as possible you should call | 3145 | For this to work as best as possible you should call |
| 3046 | `python-shell-send-buffer' from time to time so context in | 3146 | `python-shell-send-buffer' from time to time so context in |
| 3047 | inferior python process is updated properly." | 3147 | inferior Python process is updated properly." |
| 3048 | (python-eldoc--get-doc-at-point)) | 3148 | (python-eldoc--get-doc-at-point)) |
| 3049 | 3149 | ||
| 3050 | (defun python-eldoc-at-point (symbol) | 3150 | (defun python-eldoc-at-point (symbol) |
| @@ -3081,15 +3181,15 @@ It must be a function with two arguments: TYPE and NAME.") | |||
| 3081 | It must be a function with two arguments: TYPE and NAME.") | 3181 | It must be a function with two arguments: TYPE and NAME.") |
| 3082 | 3182 | ||
| 3083 | (defun python-imenu-format-item-label (type name) | 3183 | (defun python-imenu-format-item-label (type name) |
| 3084 | "Return imenu label for single node using TYPE and NAME." | 3184 | "Return Imenu label for single node using TYPE and NAME." |
| 3085 | (format "%s (%s)" name type)) | 3185 | (format "%s (%s)" name type)) |
| 3086 | 3186 | ||
| 3087 | (defun python-imenu-format-parent-item-label (type name) | 3187 | (defun python-imenu-format-parent-item-label (type name) |
| 3088 | "Return imenu label for parent node using TYPE and NAME." | 3188 | "Return Imenu label for parent node using TYPE and NAME." |
| 3089 | (format "%s..." (python-imenu-format-item-label type name))) | 3189 | (format "%s..." (python-imenu-format-item-label type name))) |
| 3090 | 3190 | ||
| 3091 | (defun python-imenu-format-parent-item-jump-label (type _name) | 3191 | (defun python-imenu-format-parent-item-jump-label (type _name) |
| 3092 | "Return imenu label for parent node jump using TYPE and NAME." | 3192 | "Return Imenu label for parent node jump using TYPE and NAME." |
| 3093 | (if (string= type "class") | 3193 | (if (string= type "class") |
| 3094 | "*class definition*" | 3194 | "*class definition*" |
| 3095 | "*function definition*")) | 3195 | "*function definition*")) |
| @@ -3106,7 +3206,7 @@ It must be a function with two arguments: TYPE and NAME.") | |||
| 3106 | 3206 | ||
| 3107 | (defun python-imenu--build-tree (&optional min-indent prev-indent tree) | 3207 | (defun python-imenu--build-tree (&optional min-indent prev-indent tree) |
| 3108 | "Recursively build the tree of nested definitions of a node. | 3208 | "Recursively build the tree of nested definitions of a node. |
| 3109 | Arguments MIN-INDENT PREV-INDENT and TREE are internal and should | 3209 | Arguments MIN-INDENT, PREV-INDENT and TREE are internal and should |
| 3110 | not be passed explicitly unless you know what you are doing." | 3210 | not be passed explicitly unless you know what you are doing." |
| 3111 | (setq min-indent (or min-indent 0) | 3211 | (setq min-indent (or min-indent 0) |
| 3112 | prev-indent (or prev-indent python-indent-offset)) | 3212 | prev-indent (or prev-indent python-indent-offset)) |
| @@ -3147,7 +3247,7 @@ not be passed explicitly unless you know what you are doing." | |||
| 3147 | tree))))))) | 3247 | tree))))))) |
| 3148 | 3248 | ||
| 3149 | (defun python-imenu-create-index () | 3249 | (defun python-imenu-create-index () |
| 3150 | "Return tree Imenu alist for the current python buffer. | 3250 | "Return tree Imenu alist for the current Python buffer. |
| 3151 | Change `python-imenu-format-item-label-function', | 3251 | Change `python-imenu-format-item-label-function', |
| 3152 | `python-imenu-format-parent-item-label-function', | 3252 | `python-imenu-format-parent-item-label-function', |
| 3153 | `python-imenu-format-parent-item-jump-label-function' to | 3253 | `python-imenu-format-parent-item-jump-label-function' to |
| @@ -3160,18 +3260,19 @@ customize how labels are formatted." | |||
| 3160 | index)) | 3260 | index)) |
| 3161 | 3261 | ||
| 3162 | (defun python-imenu-create-flat-index (&optional alist prefix) | 3262 | (defun python-imenu-create-flat-index (&optional alist prefix) |
| 3163 | "Return flat outline of the current python buffer for Imenu. | 3263 | "Return flat outline of the current Python buffer for Imenu. |
| 3164 | Optional Argument ALIST is the tree to be flattened, when nil | 3264 | Optional argument ALIST is the tree to be flattened; when nil |
| 3165 | `python-imenu-build-index' is used with | 3265 | `python-imenu-build-index' is used with |
| 3166 | `python-imenu-format-parent-item-jump-label-function' | 3266 | `python-imenu-format-parent-item-jump-label-function' |
| 3167 | `python-imenu-format-parent-item-label-function' | 3267 | `python-imenu-format-parent-item-label-function' |
| 3168 | `python-imenu-format-item-label-function' set to (lambda (type | 3268 | `python-imenu-format-item-label-function' set to |
| 3169 | name) name). Optional Argument PREFIX is used in recursive calls | 3269 | (lambda (type name) name) |
| 3170 | and should not be passed explicitly. | 3270 | Optional argument PREFIX is used in recursive calls and should |
| 3271 | not be passed explicitly. | ||
| 3171 | 3272 | ||
| 3172 | Converts this: | 3273 | Converts this: |
| 3173 | 3274 | ||
| 3174 | \((\"Foo\" . 103) | 3275 | ((\"Foo\" . 103) |
| 3175 | (\"Bar\" . 138) | 3276 | (\"Bar\" . 138) |
| 3176 | (\"decorator\" | 3277 | (\"decorator\" |
| 3177 | (\"decorator\" . 173) | 3278 | (\"decorator\" . 173) |
| @@ -3181,7 +3282,7 @@ Converts this: | |||
| 3181 | 3282 | ||
| 3182 | To this: | 3283 | To this: |
| 3183 | 3284 | ||
| 3184 | \((\"Foo\" . 103) | 3285 | ((\"Foo\" . 103) |
| 3185 | (\"Bar\" . 138) | 3286 | (\"Bar\" . 138) |
| 3186 | (\"decorator\" . 173) | 3287 | (\"decorator\" . 173) |
| 3187 | (\"decorator.wrap\" . 353) | 3288 | (\"decorator.wrap\" . 353) |
| @@ -3214,9 +3315,8 @@ To this: | |||
| 3214 | (defun python-info-current-defun (&optional include-type) | 3315 | (defun python-info-current-defun (&optional include-type) |
| 3215 | "Return name of surrounding function with Python compatible dotty syntax. | 3316 | "Return name of surrounding function with Python compatible dotty syntax. |
| 3216 | Optional argument INCLUDE-TYPE indicates to include the type of the defun. | 3317 | Optional argument INCLUDE-TYPE indicates to include the type of the defun. |
| 3217 | This function is compatible to be used as | 3318 | This function can be used as the value of `add-log-current-defun-function' |
| 3218 | `add-log-current-defun-function' since it returns nil if point is | 3319 | since it returns nil if point is not inside a defun." |
| 3219 | not inside a defun." | ||
| 3220 | (save-restriction | 3320 | (save-restriction |
| 3221 | (widen) | 3321 | (widen) |
| 3222 | (save-excursion | 3322 | (save-excursion |
| @@ -3479,13 +3579,13 @@ operator." | |||
| 3479 | (looking-at python-nav-beginning-of-defun-regexp)))) | 3579 | (looking-at python-nav-beginning-of-defun-regexp)))) |
| 3480 | 3580 | ||
| 3481 | (defun python-info-current-line-comment-p () | 3581 | (defun python-info-current-line-comment-p () |
| 3482 | "Check if current line is a comment line." | 3582 | "Return non-nil if current line is a comment line." |
| 3483 | (char-equal | 3583 | (char-equal |
| 3484 | (or (char-after (+ (line-beginning-position) (current-indentation))) ?_) | 3584 | (or (char-after (+ (line-beginning-position) (current-indentation))) ?_) |
| 3485 | ?#)) | 3585 | ?#)) |
| 3486 | 3586 | ||
| 3487 | (defun python-info-current-line-empty-p () | 3587 | (defun python-info-current-line-empty-p () |
| 3488 | "Check if current line is empty, ignoring whitespace." | 3588 | "Return non-nil if current line is empty, ignoring whitespace." |
| 3489 | (save-excursion | 3589 | (save-excursion |
| 3490 | (beginning-of-line 1) | 3590 | (beginning-of-line 1) |
| 3491 | (looking-at | 3591 | (looking-at |
| @@ -3529,9 +3629,9 @@ Optional argument DIRECTION defines the direction to move to." | |||
| 3529 | 3629 | ||
| 3530 | (defun python-util-popn (lst n) | 3630 | (defun python-util-popn (lst n) |
| 3531 | "Return LST first N elements. | 3631 | "Return LST first N elements. |
| 3532 | N should be an integer, when it's a natural negative number its | 3632 | N should be an integer, when negative its opposite is used. |
| 3533 | opposite is used. When N is bigger than the length of LST, the | 3633 | When N is bigger than the length of LST, the list is |
| 3534 | list is returned as is." | 3634 | returned as is." |
| 3535 | (let* ((n (min (abs n))) | 3635 | (let* ((n (min (abs n))) |
| 3536 | (len (length lst)) | 3636 | (len (length lst)) |
| 3537 | (acc)) | 3637 | (acc)) |
| @@ -3544,13 +3644,23 @@ list is returned as is." | |||
| 3544 | (reverse acc)))) | 3644 | (reverse acc)))) |
| 3545 | 3645 | ||
| 3546 | 3646 | ||
| 3647 | (defun python-electric-pair-string-delimiter () | ||
| 3648 | (when (and electric-pair-mode | ||
| 3649 | (memq last-command-event '(?\" ?\')) | ||
| 3650 | (let ((count 0)) | ||
| 3651 | (while (eq (char-before (- (point) count)) last-command-event) | ||
| 3652 | (cl-incf count)) | ||
| 3653 | (= count 3)) | ||
| 3654 | (eq (char-after) last-command-event)) | ||
| 3655 | (save-excursion (insert (make-string 2 last-command-event))))) | ||
| 3656 | |||
| 3657 | (defvar electric-indent-inhibit) | ||
| 3658 | |||
| 3547 | ;;;###autoload | 3659 | ;;;###autoload |
| 3548 | (define-derived-mode python-mode prog-mode "Python" | 3660 | (define-derived-mode python-mode prog-mode "Python" |
| 3549 | "Major mode for editing Python files. | 3661 | "Major mode for editing Python files. |
| 3550 | 3662 | ||
| 3551 | \\{python-mode-map} | 3663 | \\{python-mode-map}" |
| 3552 | Entry to this mode calls the value of `python-mode-hook' | ||
| 3553 | if that value is non-nil." | ||
| 3554 | (set (make-local-variable 'tab-width) 8) | 3664 | (set (make-local-variable 'tab-width) 8) |
| 3555 | (set (make-local-variable 'indent-tabs-mode) nil) | 3665 | (set (make-local-variable 'indent-tabs-mode) nil) |
| 3556 | 3666 | ||
| @@ -3572,10 +3682,17 @@ if that value is non-nil." | |||
| 3572 | (set (make-local-variable 'indent-line-function) | 3682 | (set (make-local-variable 'indent-line-function) |
| 3573 | #'python-indent-line-function) | 3683 | #'python-indent-line-function) |
| 3574 | (set (make-local-variable 'indent-region-function) #'python-indent-region) | 3684 | (set (make-local-variable 'indent-region-function) #'python-indent-region) |
| 3685 | ;; Because indentation is not redundant, we cannot safely reindent code. | ||
| 3686 | (setq-local electric-indent-inhibit t) | ||
| 3687 | (setq-local electric-indent-chars (cons ?: electric-indent-chars)) | ||
| 3688 | |||
| 3689 | ;; Add """ ... """ pairing to electric-pair-mode. | ||
| 3690 | (add-hook 'post-self-insert-hook | ||
| 3691 | #'python-electric-pair-string-delimiter 'append t) | ||
| 3575 | 3692 | ||
| 3576 | (set (make-local-variable 'paragraph-start) "\\s-*$") | 3693 | (set (make-local-variable 'paragraph-start) "\\s-*$") |
| 3577 | (set (make-local-variable 'fill-paragraph-function) | 3694 | (set (make-local-variable 'fill-paragraph-function) |
| 3578 | 'python-fill-paragraph) | 3695 | #'python-fill-paragraph) |
| 3579 | 3696 | ||
| 3580 | (set (make-local-variable 'beginning-of-defun-function) | 3697 | (set (make-local-variable 'beginning-of-defun-function) |
| 3581 | #'python-nav-beginning-of-defun) | 3698 | #'python-nav-beginning-of-defun) |
| @@ -3583,10 +3700,10 @@ if that value is non-nil." | |||
| 3583 | #'python-nav-end-of-defun) | 3700 | #'python-nav-end-of-defun) |
| 3584 | 3701 | ||
| 3585 | (add-hook 'completion-at-point-functions | 3702 | (add-hook 'completion-at-point-functions |
| 3586 | 'python-completion-complete-at-point nil 'local) | 3703 | #'python-completion-complete-at-point nil 'local) |
| 3587 | 3704 | ||
| 3588 | (add-hook 'post-self-insert-hook | 3705 | (add-hook 'post-self-insert-hook |
| 3589 | 'python-indent-post-self-insert-function nil 'local) | 3706 | #'python-indent-post-self-insert-function 'append 'local) |
| 3590 | 3707 | ||
| 3591 | (set (make-local-variable 'imenu-create-index-function) | 3708 | (set (make-local-variable 'imenu-create-index-function) |
| 3592 | #'python-imenu-create-index) | 3709 | #'python-imenu-create-index) |