diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 591 |
1 files changed, 433 insertions, 158 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 35e24e14e1c..c46c5d68019 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -69,7 +69,7 @@ | |||
| 69 | ;; Besides that only the standard CPython (2.x and 3.x) shell and | 69 | ;; Besides that only the standard CPython (2.x and 3.x) shell and |
| 70 | ;; IPython are officially supported out of the box, the interaction | 70 | ;; IPython are officially supported out of the box, the interaction |
| 71 | ;; should support any other readline based Python shells as well | 71 | ;; should support any other readline based Python shells as well |
| 72 | ;; (e.g. Jython and Pypy have been reported to work). You can change | 72 | ;; (e.g. Jython and PyPy have been reported to work). You can change |
| 73 | ;; your default interpreter and commandline arguments by setting the | 73 | ;; your default interpreter and commandline arguments by setting the |
| 74 | ;; `python-shell-interpreter' and `python-shell-interpreter-args' | 74 | ;; `python-shell-interpreter' and `python-shell-interpreter-args' |
| 75 | ;; variables. This example enables IPython globally: | 75 | ;; variables. This example enables IPython globally: |
| @@ -119,18 +119,24 @@ | |||
| 119 | ;; modify its behavior. | 119 | ;; modify its behavior. |
| 120 | 120 | ||
| 121 | ;; Shell completion: hitting tab will try to complete the current | 121 | ;; Shell completion: hitting tab will try to complete the current |
| 122 | ;; word. Shell completion is implemented in such way that if you | 122 | ;; word. The two built-in mechanisms depend on Python's readline |
| 123 | ;; change the `python-shell-interpreter' it should be possible to | 123 | ;; module: the "native" completion is tried first and is activated |
| 124 | ;; integrate custom logic to calculate completions. To achieve this | 124 | ;; when `python-shell-completion-native-enable' is non-nil, the |
| 125 | ;; you just need to set `python-shell-completion-setup-code' and | 125 | ;; current `python-shell-interpreter' is not a member of the |
| 126 | ;; `python-shell-completion-string-code'. The default provided code, | 126 | ;; `python-shell-completion-native-disabled-interpreters' variable and |
| 127 | ;; enables autocompletion for both CPython and IPython (and ideally | 127 | ;; `python-shell-completion-native-setup' succeeds; the "fallback" or |
| 128 | ;; any readline based Python shell). This code depends on the | 128 | ;; "legacy" mechanism works by executing Python code in the background |
| 129 | ;; readline module, so if you are using some Operating System that | 129 | ;; and enables auto-completion for shells that do not support |
| 130 | ;; bundles Python without it (like Windows), installing pyreadline | 130 | ;; receiving escape sequences (with some limitations, i.e. completion |
| 131 | ;; from URL `http://ipython.scipy.org/moin/PyReadline/Intro' should | 131 | ;; in blocks does not work). The code executed for the "fallback" |
| 132 | ;; suffice. To troubleshoot why you are not getting any completions | 132 | ;; completion can be found in `python-shell-completion-setup-code' and |
| 133 | ;; you can try the following in your Python shell: | 133 | ;; `python-shell-completion-string-code' variables. Their default |
| 134 | ;; values enable completion for both CPython and IPython, and probably | ||
| 135 | ;; any readline based shell (it's known to work with PyPy). If your | ||
| 136 | ;; Python installation lacks readline (like CPython for Windows), | ||
| 137 | ;; installing pyreadline (URL `http://ipython.org/pyreadline.html') | ||
| 138 | ;; should suffice. To troubleshoot why you are not getting any | ||
| 139 | ;; completions, you can try the following in your Python shell: | ||
| 134 | 140 | ||
| 135 | ;; >>> import readline, rlcompleter | 141 | ;; >>> import readline, rlcompleter |
| 136 | 142 | ||
| @@ -256,6 +262,7 @@ | |||
| 256 | (defvar outline-heading-end-regexp) | 262 | (defvar outline-heading-end-regexp) |
| 257 | 263 | ||
| 258 | (autoload 'comint-mode "comint") | 264 | (autoload 'comint-mode "comint") |
| 265 | (autoload 'help-function-arglist "help-fns") | ||
| 259 | 266 | ||
| 260 | ;;;###autoload | 267 | ;;;###autoload |
| 261 | (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode)) | 268 | (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode)) |
| @@ -395,7 +402,18 @@ | |||
| 395 | (* ?\\ ?\\) (any ?\' ?\"))) | 402 | (* ?\\ ?\\) (any ?\' ?\"))) |
| 396 | (* ?\\ ?\\) | 403 | (* ?\\ ?\\) |
| 397 | ;; Match single or triple quotes of any kind. | 404 | ;; Match single or triple quotes of any kind. |
| 398 | (group (or "\"" "\"\"\"" "'" "'''")))))) | 405 | (group (or "\"" "\"\"\"" "'" "'''"))))) |
| 406 | (coding-cookie . ,(rx line-start ?# (* space) | ||
| 407 | (or | ||
| 408 | ;; # coding=<encoding name> | ||
| 409 | (: "coding" (or ?: ?=) (* space) (group-n 1 (+ (or word ?-)))) | ||
| 410 | ;; # -*- coding: <encoding name> -*- | ||
| 411 | (: "-*-" (* space) "coding:" (* space) | ||
| 412 | (group-n 1 (+ (or word ?-))) (* space) "-*-") | ||
| 413 | ;; # vim: set fileencoding=<encoding name> : | ||
| 414 | (: "vim:" (* space) "set" (+ space) | ||
| 415 | "fileencoding" (* space) ?= (* space) | ||
| 416 | (group-n 1 (+ (or word ?-))) (* space) ":"))))) | ||
| 399 | "Additional Python specific sexps for `python-rx'") | 417 | "Additional Python specific sexps for `python-rx'") |
| 400 | 418 | ||
| 401 | (defmacro python-rx (&rest regexps) | 419 | (defmacro python-rx (&rest regexps) |
| @@ -1206,12 +1224,18 @@ the line will be re-indented automatically if needed." | |||
| 1206 | (eolp) | 1224 | (eolp) |
| 1207 | ;; Avoid re-indenting on extra colon | 1225 | ;; Avoid re-indenting on extra colon |
| 1208 | (not (equal ?: (char-before (1- (point))))) | 1226 | (not (equal ?: (char-before (1- (point))))) |
| 1209 | (not (python-syntax-comment-or-string-p)) | 1227 | (not (python-syntax-comment-or-string-p))) |
| 1210 | ;; Never re-indent at beginning of defun | 1228 | ;; Just re-indent dedenters |
| 1211 | (not (save-excursion | 1229 | (let ((dedenter-pos (python-info-dedenter-statement-p)) |
| 1212 | (python-nav-beginning-of-statement) | 1230 | (current-pos (point))) |
| 1213 | (python-info-looking-at-beginning-of-defun)))) | 1231 | (when dedenter-pos |
| 1214 | (python-indent-line))))) | 1232 | (save-excursion |
| 1233 | (goto-char dedenter-pos) | ||
| 1234 | (python-indent-line) | ||
| 1235 | (unless (= (line-number-at-pos dedenter-pos) | ||
| 1236 | (line-number-at-pos current-pos)) | ||
| 1237 | ;; Reindent region if this is a multiline statement | ||
| 1238 | (python-indent-region dedenter-pos current-pos))))))))) | ||
| 1215 | 1239 | ||
| 1216 | 1240 | ||
| 1217 | ;;; Navigation | 1241 | ;;; Navigation |
| @@ -2087,36 +2111,18 @@ and `python-shell-output-prompt-regexp' using the values from | |||
| 2087 | 2111 | ||
| 2088 | (defun python-shell-get-process-name (dedicated) | 2112 | (defun python-shell-get-process-name (dedicated) |
| 2089 | "Calculate the appropriate process name for inferior Python process. | 2113 | "Calculate the appropriate process name for inferior Python process. |
| 2090 | If DEDICATED is t and the variable `buffer-file-name' is non-nil | 2114 | If DEDICATED is t returns a string with the form |
| 2091 | returns a string with the form | 2115 | `python-shell-buffer-name'[`buffer-name'] else returns the value |
| 2092 | `python-shell-buffer-name'[variable `buffer-file-name'] else | 2116 | of `python-shell-buffer-name'." |
| 2093 | returns the value of `python-shell-buffer-name'." | 2117 | (if dedicated |
| 2094 | (let ((process-name | 2118 | (format "%s[%s]" python-shell-buffer-name (buffer-name)) |
| 2095 | (if (and dedicated | 2119 | python-shell-buffer-name)) |
| 2096 | buffer-file-name) | ||
| 2097 | (format "%s[%s]" python-shell-buffer-name buffer-file-name) | ||
| 2098 | (format "%s" python-shell-buffer-name)))) | ||
| 2099 | process-name)) | ||
| 2100 | 2120 | ||
| 2101 | (defun python-shell-internal-get-process-name () | 2121 | (defun python-shell-internal-get-process-name () |
| 2102 | "Calculate the appropriate process name for Internal Python process. | 2122 | "Calculate the appropriate process name for Internal Python process. |
| 2103 | The name is calculated from `python-shell-global-buffer-name' and | 2123 | The name is calculated from `python-shell-global-buffer-name' and |
| 2104 | a hash of all relevant global shell settings in order to ensure | 2124 | the `buffer-name'." |
| 2105 | uniqueness for different types of configurations." | 2125 | (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))) |
| 2106 | (format "%s [%s]" | ||
| 2107 | python-shell-internal-buffer-name | ||
| 2108 | (md5 | ||
| 2109 | (concat | ||
| 2110 | python-shell-interpreter | ||
| 2111 | python-shell-interpreter-args | ||
| 2112 | python-shell--prompt-calculated-input-regexp | ||
| 2113 | python-shell--prompt-calculated-output-regexp | ||
| 2114 | (mapconcat #'symbol-value python-shell-setup-codes "") | ||
| 2115 | (mapconcat #'identity python-shell-process-environment "") | ||
| 2116 | (mapconcat #'identity python-shell-extra-pythonpaths "") | ||
| 2117 | (mapconcat #'identity python-shell-exec-path "") | ||
| 2118 | (or python-shell-virtualenv-root "") | ||
| 2119 | (mapconcat #'identity python-shell-exec-path ""))))) | ||
| 2120 | 2126 | ||
| 2121 | (defun python-shell-calculate-command () | 2127 | (defun python-shell-calculate-command () |
| 2122 | "Calculate the string used to execute the inferior Python process." | 2128 | "Calculate the string used to execute the inferior Python process." |
| @@ -2256,11 +2262,9 @@ Avoids `recenter' calls until OUTPUT is completely sent." | |||
| 2256 | "Execute the forms in BODY with the shell buffer temporarily current. | 2262 | "Execute the forms in BODY with the shell buffer temporarily current. |
| 2257 | Signals an error if no shell buffer is available for current buffer." | 2263 | Signals an error if no shell buffer is available for current buffer." |
| 2258 | (declare (indent 0) (debug t)) | 2264 | (declare (indent 0) (debug t)) |
| 2259 | (let ((shell-buffer (make-symbol "shell-buffer"))) | 2265 | (let ((shell-process (make-symbol "shell-process"))) |
| 2260 | `(let ((,shell-buffer (python-shell-get-buffer))) | 2266 | `(let ((,shell-process (python-shell-get-process-or-error))) |
| 2261 | (when (not ,shell-buffer) | 2267 | (with-current-buffer (process-buffer ,shell-process) |
| 2262 | (error "No inferior Python buffer available.")) | ||
| 2263 | (with-current-buffer ,shell-buffer | ||
| 2264 | ,@body)))) | 2268 | ,@body)))) |
| 2265 | 2269 | ||
| 2266 | (defvar python-shell--font-lock-buffer nil) | 2270 | (defvar python-shell--font-lock-buffer nil) |
| @@ -2472,12 +2476,12 @@ variable. | |||
| 2472 | (python-shell-accept-process-output | 2476 | (python-shell-accept-process-output |
| 2473 | (get-buffer-process (current-buffer)))) | 2477 | (get-buffer-process (current-buffer)))) |
| 2474 | 2478 | ||
| 2475 | (defun python-shell-make-comint (cmd proc-name &optional pop internal) | 2479 | (defun python-shell-make-comint (cmd proc-name &optional show internal) |
| 2476 | "Create a Python shell comint buffer. | 2480 | "Create a Python shell comint buffer. |
| 2477 | CMD is the Python command to be executed and PROC-NAME is the | 2481 | CMD is the Python command to be executed and PROC-NAME is the |
| 2478 | process name the comint buffer will get. After the comint buffer | 2482 | process name the comint buffer will get. After the comint buffer |
| 2479 | is created the `inferior-python-mode' is activated. When | 2483 | is created the `inferior-python-mode' is activated. When |
| 2480 | optional argument POP is non-nil the buffer is shown. When | 2484 | optional argument SHOW is non-nil the buffer is shown. When |
| 2481 | optional argument INTERNAL is non-nil this process is run on a | 2485 | optional argument INTERNAL is non-nil this process is run on a |
| 2482 | buffer with a name that starts with a space, following the Emacs | 2486 | buffer with a name that starts with a space, following the Emacs |
| 2483 | convention for temporary/internal buffers, and also makes sure | 2487 | convention for temporary/internal buffers, and also makes sure |
| @@ -2506,16 +2510,13 @@ killed." | |||
| 2506 | (mapconcat #'identity args " "))) | 2510 | (mapconcat #'identity args " "))) |
| 2507 | (with-current-buffer buffer | 2511 | (with-current-buffer buffer |
| 2508 | (inferior-python-mode)) | 2512 | (inferior-python-mode)) |
| 2509 | (and pop (pop-to-buffer buffer t)) | 2513 | (when show (display-buffer buffer)) |
| 2510 | (and internal (set-process-query-on-exit-flag process nil)))) | 2514 | (and internal (set-process-query-on-exit-flag process nil)))) |
| 2511 | proc-buffer-name))) | 2515 | proc-buffer-name))) |
| 2512 | 2516 | ||
| 2513 | ;;;###autoload | 2517 | ;;;###autoload |
| 2514 | (defun run-python (&optional cmd dedicated show) | 2518 | (defun run-python (&optional cmd dedicated show) |
| 2515 | "Run an inferior Python process. | 2519 | "Run an inferior Python process. |
| 2516 | Input and output via buffer named after | ||
| 2517 | `python-shell-buffer-name'. If there is a process already | ||
| 2518 | running in that buffer, just switch to it. | ||
| 2519 | 2520 | ||
| 2520 | Argument CMD defaults to `python-shell-calculate-command' return | 2521 | Argument CMD defaults to `python-shell-calculate-command' return |
| 2521 | value. When called interactively with `prefix-arg', it allows | 2522 | value. When called interactively with `prefix-arg', it allows |
| @@ -2523,6 +2524,11 @@ the user to edit such value and choose whether the interpreter | |||
| 2523 | should be DEDICATED for the current buffer. When numeric prefix | 2524 | should be DEDICATED for the current buffer. When numeric prefix |
| 2524 | arg is other than 0 or 4 do not SHOW. | 2525 | arg is other than 0 or 4 do not SHOW. |
| 2525 | 2526 | ||
| 2527 | For a given buffer and same values of DEDICATED, if a process is | ||
| 2528 | already running for it, it will do nothing. This means that if | ||
| 2529 | the current buffer is using a global process, the user is still | ||
| 2530 | able to switch it to use a dedicated one. | ||
| 2531 | |||
| 2526 | Runs the hook `inferior-python-mode-hook' after | 2532 | Runs the hook `inferior-python-mode-hook' after |
| 2527 | `comint-mode-hook' is run. (Type \\[describe-mode] in the | 2533 | `comint-mode-hook' is run. (Type \\[describe-mode] in the |
| 2528 | process buffer for a list of commands.)" | 2534 | process buffer for a list of commands.)" |
| @@ -2533,10 +2539,10 @@ process buffer for a list of commands.)" | |||
| 2533 | (y-or-n-p "Make dedicated process? ") | 2539 | (y-or-n-p "Make dedicated process? ") |
| 2534 | (= (prefix-numeric-value current-prefix-arg) 4)) | 2540 | (= (prefix-numeric-value current-prefix-arg) 4)) |
| 2535 | (list (python-shell-calculate-command) nil t))) | 2541 | (list (python-shell-calculate-command) nil t))) |
| 2536 | (python-shell-make-comint | 2542 | (get-buffer-process |
| 2537 | (or cmd (python-shell-calculate-command)) | 2543 | (python-shell-make-comint |
| 2538 | (python-shell-get-process-name dedicated) show) | 2544 | (or cmd (python-shell-calculate-command)) |
| 2539 | dedicated) | 2545 | (python-shell-get-process-name dedicated) show))) |
| 2540 | 2546 | ||
| 2541 | (defun run-python-internal () | 2547 | (defun run-python-internal () |
| 2542 | "Run an inferior Internal Python process. | 2548 | "Run an inferior Internal Python process. |
| @@ -2579,6 +2585,21 @@ If current buffer is in `inferior-python-mode', return it." | |||
| 2579 | "Return inferior Python process for current buffer." | 2585 | "Return inferior Python process for current buffer." |
| 2580 | (get-buffer-process (python-shell-get-buffer))) | 2586 | (get-buffer-process (python-shell-get-buffer))) |
| 2581 | 2587 | ||
| 2588 | (defun python-shell-get-process-or-error (&optional interactivep) | ||
| 2589 | "Return inferior Python process for current buffer or signal error. | ||
| 2590 | When argument INTERACTIVEP is non-nil, use `user-error' instead | ||
| 2591 | of `error' with a user-friendly message." | ||
| 2592 | (or (python-shell-get-process) | ||
| 2593 | (if interactivep | ||
| 2594 | (user-error | ||
| 2595 | "Start a Python process first with `M-x run-python' or `%s'." | ||
| 2596 | ;; Get the binding. | ||
| 2597 | (key-description | ||
| 2598 | (where-is-internal | ||
| 2599 | #'run-python overriding-local-map t))) | ||
| 2600 | (error | ||
| 2601 | "No inferior Python process running.")))) | ||
| 2602 | |||
| 2582 | (defun python-shell-get-or-create-process (&optional cmd dedicated show) | 2603 | (defun python-shell-get-or-create-process (&optional cmd dedicated show) |
| 2583 | "Get or create an inferior Python process for current buffer and return it. | 2604 | "Get or create an inferior Python process for current buffer and return it. |
| 2584 | Arguments CMD, DEDICATED and SHOW are those of `run-python' and | 2605 | Arguments CMD, DEDICATED and SHOW are those of `run-python' and |
| @@ -2594,6 +2615,11 @@ be asked for their values." | |||
| 2594 | (run-python cmd dedicated show))) | 2615 | (run-python cmd dedicated show))) |
| 2595 | (or shell-process (python-shell-get-process)))) | 2616 | (or shell-process (python-shell-get-process)))) |
| 2596 | 2617 | ||
| 2618 | (make-obsolete | ||
| 2619 | #'python-shell-get-or-create-process | ||
| 2620 | "Instead call `python-shell-get-process' and create one if returns nil." | ||
| 2621 | "25.1") | ||
| 2622 | |||
| 2597 | (defvar python-shell-internal-buffer nil | 2623 | (defvar python-shell-internal-buffer nil |
| 2598 | "Current internal shell buffer for the current buffer. | 2624 | "Current internal shell buffer for the current buffer. |
| 2599 | This is really not necessary at all for the code to work but it's | 2625 | This is really not necessary at all for the code to work but it's |
| @@ -2606,12 +2632,10 @@ there for compatibility with CEDET.") | |||
| 2606 | 2632 | ||
| 2607 | (defun python-shell-internal-get-or-create-process () | 2633 | (defun python-shell-internal-get-or-create-process () |
| 2608 | "Get or create an inferior Internal Python process." | 2634 | "Get or create an inferior Internal Python process." |
| 2609 | (let* ((proc-name (python-shell-internal-get-process-name)) | 2635 | (let ((proc-name (python-shell-internal-get-process-name))) |
| 2610 | (proc-buffer-name (format " *%s*" proc-name))) | 2636 | (if (process-live-p proc-name) |
| 2611 | (when (not (process-live-p proc-name)) | 2637 | (get-process proc-name) |
| 2612 | (run-python-internal) | 2638 | (run-python-internal)))) |
| 2613 | (setq python-shell-internal-buffer proc-buffer-name)) | ||
| 2614 | (get-buffer-process proc-buffer-name))) | ||
| 2615 | 2639 | ||
| 2616 | (define-obsolete-function-alias | 2640 | (define-obsolete-function-alias |
| 2617 | 'python-proc 'python-shell-internal-get-or-create-process "24.3") | 2641 | 'python-proc 'python-shell-internal-get-or-create-process "24.3") |
| @@ -2628,20 +2652,24 @@ there for compatibility with CEDET.") | |||
| 2628 | (concat (file-remote-p default-directory) "/tmp") | 2652 | (concat (file-remote-p default-directory) "/tmp") |
| 2629 | temporary-file-directory)) | 2653 | temporary-file-directory)) |
| 2630 | (temp-file-name (make-temp-file "py")) | 2654 | (temp-file-name (make-temp-file "py")) |
| 2631 | (coding-system-for-write 'utf-8)) | 2655 | (coding-system-for-write (python-info-encoding))) |
| 2632 | (with-temp-file temp-file-name | 2656 | (with-temp-file temp-file-name |
| 2633 | (insert "# -*- coding: utf-8 -*-\n") ;Not needed for Python-3. | ||
| 2634 | (insert string) | 2657 | (insert string) |
| 2635 | (delete-trailing-whitespace)) | 2658 | (delete-trailing-whitespace)) |
| 2636 | temp-file-name)) | 2659 | temp-file-name)) |
| 2637 | 2660 | ||
| 2638 | (defun python-shell-send-string (string &optional process) | 2661 | (defun python-shell-send-string (string &optional process msg) |
| 2639 | "Send STRING to inferior Python PROCESS." | 2662 | "Send STRING to inferior Python PROCESS. |
| 2640 | (interactive "sPython command: ") | 2663 | When optional argument MSG is non-nil, forces display of a |
| 2641 | (let ((process (or process (python-shell-get-or-create-process)))) | 2664 | user-friendly message if there's no process running; defaults to |
| 2665 | t when called interactively." | ||
| 2666 | (interactive | ||
| 2667 | (list (read-string "Python command: ") nil t)) | ||
| 2668 | (let ((process (or process (python-shell-get-process-or-error msg)))) | ||
| 2642 | (if (string-match ".\n+." string) ;Multiline. | 2669 | (if (string-match ".\n+." string) ;Multiline. |
| 2643 | (let* ((temp-file-name (python-shell--save-temp-file string))) | 2670 | (let* ((temp-file-name (python-shell--save-temp-file string)) |
| 2644 | (python-shell-send-file temp-file-name process temp-file-name t)) | 2671 | (file-name (or (buffer-file-name) temp-file-name))) |
| 2672 | (python-shell-send-file file-name process temp-file-name t)) | ||
| 2645 | (comint-send-string process string) | 2673 | (comint-send-string process string) |
| 2646 | (when (or (not (string-match "\n\\'" string)) | 2674 | (when (or (not (string-match "\n\\'" string)) |
| 2647 | (string-match "\n[ \t].*\n?\\'" string)) | 2675 | (string-match "\n[ \t].*\n?\\'" string)) |
| @@ -2680,7 +2708,7 @@ detecting a prompt at the end of the buffer." | |||
| 2680 | (defun python-shell-send-string-no-output (string &optional process) | 2708 | (defun python-shell-send-string-no-output (string &optional process) |
| 2681 | "Send STRING to PROCESS and inhibit output. | 2709 | "Send STRING to PROCESS and inhibit output. |
| 2682 | Return the output." | 2710 | Return the output." |
| 2683 | (let ((process (or process (python-shell-get-or-create-process))) | 2711 | (let ((process (or process (python-shell-get-process-or-error))) |
| 2684 | (comint-preoutput-filter-functions | 2712 | (comint-preoutput-filter-functions |
| 2685 | '(python-shell-output-filter)) | 2713 | '(python-shell-output-filter)) |
| 2686 | (python-shell-output-filter-in-progress t) | 2714 | (python-shell-output-filter-in-progress t) |
| @@ -2717,12 +2745,6 @@ Returns the output. See `python-shell-send-string-no-output'." | |||
| 2717 | (define-obsolete-function-alias | 2745 | (define-obsolete-function-alias |
| 2718 | 'python-send-string 'python-shell-internal-send-string "24.3") | 2746 | 'python-send-string 'python-shell-internal-send-string "24.3") |
| 2719 | 2747 | ||
| 2720 | (defvar python--use-fake-loc nil | ||
| 2721 | "If non-nil, use `compilation-fake-loc' to trace errors back to the buffer. | ||
| 2722 | If nil, regions of text are prepended by the corresponding number of empty | ||
| 2723 | lines and Python is told to output error messages referring to the whole | ||
| 2724 | source file.") | ||
| 2725 | |||
| 2726 | (defun python-shell-buffer-substring (start end &optional nomain) | 2748 | (defun python-shell-buffer-substring (start end &optional nomain) |
| 2727 | "Send buffer substring from START to END formatted for shell. | 2749 | "Send buffer substring from START to END formatted for shell. |
| 2728 | This is a wrapper over `buffer-substring' that takes care of | 2750 | This is a wrapper over `buffer-substring' that takes care of |
| @@ -2732,27 +2754,32 @@ the python shell: | |||
| 2732 | \"if __name__ == '__main__'\" block will be removed. | 2754 | \"if __name__ == '__main__'\" block will be removed. |
| 2733 | 2. When a subregion of the buffer is sent, it takes care of | 2755 | 2. When a subregion of the buffer is sent, it takes care of |
| 2734 | appending extra empty lines so tracebacks are correct. | 2756 | appending extra empty lines so tracebacks are correct. |
| 2735 | 3. Wraps indented regions under an \"if True:\" block so the | 2757 | 3. When the region sent is a substring of the current buffer, a |
| 2758 | coding cookie is added. | ||
| 2759 | 4. Wraps indented regions under an \"if True:\" block so the | ||
| 2736 | interpreter evaluates them correctly." | 2760 | interpreter evaluates them correctly." |
| 2737 | (let ((substring (buffer-substring-no-properties start end)) | 2761 | (let* ((substring (buffer-substring-no-properties start end)) |
| 2738 | (fillstr (unless python--use-fake-loc | 2762 | (starts-at-point-min-p (save-restriction |
| 2739 | (make-string (1- (line-number-at-pos start)) ?\n))) | 2763 | (widen) |
| 2740 | (toplevel-block-p (save-excursion | 2764 | (= (point-min) start))) |
| 2741 | (goto-char start) | 2765 | (encoding (python-info-encoding)) |
| 2742 | (or (zerop (line-number-at-pos start)) | 2766 | (fillstr (when (not starts-at-point-min-p) |
| 2743 | (progn | 2767 | (concat |
| 2744 | (python-util-forward-comment 1) | 2768 | (format "# -*- coding: %s -*-\n" encoding) |
| 2745 | (zerop (current-indentation))))))) | 2769 | (make-string |
| 2770 | ;; Substract 2 because of the coding cookie. | ||
| 2771 | (- (line-number-at-pos start) 2) ?\n)))) | ||
| 2772 | (toplevel-block-p (save-excursion | ||
| 2773 | (goto-char start) | ||
| 2774 | (or (zerop (line-number-at-pos start)) | ||
| 2775 | (progn | ||
| 2776 | (python-util-forward-comment 1) | ||
| 2777 | (zerop (current-indentation))))))) | ||
| 2746 | (with-temp-buffer | 2778 | (with-temp-buffer |
| 2747 | (python-mode) | 2779 | (python-mode) |
| 2748 | (if fillstr (insert fillstr)) | 2780 | (if fillstr (insert fillstr)) |
| 2749 | (insert substring) | 2781 | (insert substring) |
| 2750 | (goto-char (point-min)) | 2782 | (goto-char (point-min)) |
| 2751 | (unless python--use-fake-loc | ||
| 2752 | ;; python-shell--save-temp-file adds an extra coding line, which would | ||
| 2753 | ;; throw off the line-counts, so let's try to compensate here. | ||
| 2754 | (if (looking-at "[ \t]*[#\n]") | ||
| 2755 | (delete-region (point) (line-beginning-position 2)))) | ||
| 2756 | (when (not toplevel-block-p) | 2783 | (when (not toplevel-block-p) |
| 2757 | (insert "if True:") | 2784 | (insert "if True:") |
| 2758 | (delete-region (point) (line-end-position))) | 2785 | (delete-region (point) (line-end-position))) |
| @@ -2763,53 +2790,65 @@ the python shell: | |||
| 2763 | (when (python-nav-if-name-main) | 2790 | (when (python-nav-if-name-main) |
| 2764 | (cons (point) | 2791 | (cons (point) |
| 2765 | (progn (python-nav-forward-sexp-safe) | 2792 | (progn (python-nav-forward-sexp-safe) |
| 2793 | ;; Include ending newline | ||
| 2794 | (forward-line 1) | ||
| 2766 | (point))))))) | 2795 | (point))))))) |
| 2767 | ;; Oh destructuring bind, how I miss you. | 2796 | ;; Oh destructuring bind, how I miss you. |
| 2768 | (if-name-main-start (car if-name-main-start-end)) | 2797 | (if-name-main-start (car if-name-main-start-end)) |
| 2769 | (if-name-main-end (cdr if-name-main-start-end))) | 2798 | (if-name-main-end (cdr if-name-main-start-end)) |
| 2799 | (fillstr (make-string | ||
| 2800 | (- (line-number-at-pos if-name-main-end) | ||
| 2801 | (line-number-at-pos if-name-main-start)) ?\n))) | ||
| 2770 | (when if-name-main-start-end | 2802 | (when if-name-main-start-end |
| 2771 | (goto-char if-name-main-start) | 2803 | (goto-char if-name-main-start) |
| 2772 | (delete-region if-name-main-start if-name-main-end) | 2804 | (delete-region if-name-main-start if-name-main-end) |
| 2773 | (insert | 2805 | (insert fillstr)))) |
| 2774 | (make-string | 2806 | ;; Ensure there's only one coding cookie in the generated string. |
| 2775 | (- (line-number-at-pos if-name-main-end) | 2807 | (goto-char (point-min)) |
| 2776 | (line-number-at-pos if-name-main-start)) ?\n))))) | 2808 | (when (looking-at-p (python-rx coding-cookie)) |
| 2809 | (forward-line 1) | ||
| 2810 | (when (looking-at-p (python-rx coding-cookie)) | ||
| 2811 | (delete-region | ||
| 2812 | (line-beginning-position) (line-end-position)))) | ||
| 2777 | (buffer-substring-no-properties (point-min) (point-max))))) | 2813 | (buffer-substring-no-properties (point-min) (point-max))))) |
| 2778 | 2814 | ||
| 2779 | (declare-function compilation-fake-loc "compile" | 2815 | (defun python-shell-send-region (start end &optional send-main msg) |
| 2780 | (marker file &optional line col)) | 2816 | "Send the region delimited by START and END to inferior Python process. |
| 2781 | 2817 | When optional argument SEND-MAIN is non-nil, allow execution of | |
| 2782 | (defun python-shell-send-region (start end &optional nomain) | 2818 | code inside blocks delimited by \"if __name__== '__main__':\". |
| 2783 | "Send the region delimited by START and END to inferior Python process." | 2819 | When called interactively SEND-MAIN defaults to nil, unless it's |
| 2784 | (interactive "r") | 2820 | called with prefix argument. When optional argument MSG is |
| 2785 | (let* ((python--use-fake-loc | 2821 | non-nil, forces display of a user-friendly message if there's no |
| 2786 | (or python--use-fake-loc (not buffer-file-name))) | 2822 | process running; defaults to t when called interactively." |
| 2787 | (string (python-shell-buffer-substring start end nomain)) | 2823 | (interactive |
| 2788 | (process (python-shell-get-or-create-process)) | 2824 | (list (region-beginning) (region-end) current-prefix-arg t)) |
| 2789 | (_ (string-match "\\`\n*\\(.*\\)" string))) | 2825 | (let* ((string (python-shell-buffer-substring start end (not send-main))) |
| 2790 | (message "Sent: %s..." (match-string 1 string)) | 2826 | (process (python-shell-get-process-or-error msg)) |
| 2791 | (let* ((temp-file-name (python-shell--save-temp-file string)) | 2827 | (original-string (buffer-substring-no-properties start end)) |
| 2792 | (file-name (or (buffer-file-name) temp-file-name))) | 2828 | (_ (string-match "\\`\n*\\(.*\\)" original-string))) |
| 2793 | (python-shell-send-file file-name process temp-file-name t) | 2829 | (message "Sent: %s..." (match-string 1 original-string)) |
| 2794 | (unless python--use-fake-loc | 2830 | (python-shell-send-string string process))) |
| 2795 | (with-current-buffer (process-buffer process) | 2831 | |
| 2796 | (compilation-fake-loc (copy-marker start) temp-file-name | 2832 | (defun python-shell-send-buffer (&optional send-main msg) |
| 2797 | 2)) ;; Not 1, because of the added coding line. | ||
| 2798 | )))) | ||
| 2799 | |||
| 2800 | (defun python-shell-send-buffer (&optional arg) | ||
| 2801 | "Send the entire buffer to inferior Python process. | 2833 | "Send the entire buffer to inferior Python process. |
| 2802 | With prefix ARG allow execution of code inside blocks delimited | 2834 | When optional argument SEND-MAIN is non-nil, allow execution of |
| 2803 | by \"if __name__== '__main__':\"." | 2835 | code inside blocks delimited by \"if __name__== '__main__':\". |
| 2804 | (interactive "P") | 2836 | When called interactively SEND-MAIN defaults to nil, unless it's |
| 2837 | called with prefix argument. When optional argument MSG is | ||
| 2838 | non-nil, forces display of a user-friendly message if there's no | ||
| 2839 | process running; defaults to t when called interactively." | ||
| 2840 | (interactive (list current-prefix-arg t)) | ||
| 2805 | (save-restriction | 2841 | (save-restriction |
| 2806 | (widen) | 2842 | (widen) |
| 2807 | (python-shell-send-region (point-min) (point-max) (not arg)))) | 2843 | (python-shell-send-region (point-min) (point-max) send-main msg))) |
| 2808 | 2844 | ||
| 2809 | (defun python-shell-send-defun (arg) | 2845 | (defun python-shell-send-defun (&optional arg msg) |
| 2810 | "Send the current defun to inferior Python process. | 2846 | "Send the current defun to inferior Python process. |
| 2811 | When argument ARG is non-nil do not include decorators." | 2847 | When argument ARG is non-nil do not include decorators. When |
| 2812 | (interactive "P") | 2848 | optional argument MSG is non-nil, forces display of a |
| 2849 | user-friendly message if there's no process running; defaults to | ||
| 2850 | t when called interactively." | ||
| 2851 | (interactive (list current-prefix-arg t)) | ||
| 2813 | (save-excursion | 2852 | (save-excursion |
| 2814 | (python-shell-send-region | 2853 | (python-shell-send-region |
| 2815 | (progn | 2854 | (progn |
| @@ -2825,42 +2864,60 @@ When argument ARG is non-nil do not include decorators." | |||
| 2825 | (progn | 2864 | (progn |
| 2826 | (or (python-nav-end-of-defun) | 2865 | (or (python-nav-end-of-defun) |
| 2827 | (end-of-line 1)) | 2866 | (end-of-line 1)) |
| 2828 | (point-marker))))) | 2867 | (point-marker)) |
| 2868 | nil ;; noop | ||
| 2869 | msg))) | ||
| 2829 | 2870 | ||
| 2830 | (defun python-shell-send-file (file-name &optional process temp-file-name | 2871 | (defun python-shell-send-file (file-name &optional process temp-file-name |
| 2831 | delete) | 2872 | delete msg) |
| 2832 | "Send FILE-NAME to inferior Python PROCESS. | 2873 | "Send FILE-NAME to inferior Python PROCESS. |
| 2833 | If TEMP-FILE-NAME is passed then that file is used for processing | 2874 | If TEMP-FILE-NAME is passed then that file is used for processing |
| 2834 | instead, while internally the shell will continue to use FILE-NAME. | 2875 | instead, while internally the shell will continue to use |
| 2835 | If DELETE is non-nil, delete the file afterwards." | 2876 | FILE-NAME. If TEMP-FILE-NAME and DELETE are non-nil, then |
| 2836 | (interactive "fFile to send: ") | 2877 | TEMP-FILE-NAME is deleted after evaluation is performed. When |
| 2837 | (let* ((process (or process (python-shell-get-or-create-process))) | 2878 | optional argument MSG is non-nil, forces display of a |
| 2879 | user-friendly message if there's no process running; defaults to | ||
| 2880 | t when called interactively." | ||
| 2881 | (interactive | ||
| 2882 | (list | ||
| 2883 | (read-file-name "File to send: ") ; file-name | ||
| 2884 | nil ; process | ||
| 2885 | nil ; temp-file-name | ||
| 2886 | nil ; delete | ||
| 2887 | t)) ; msg | ||
| 2888 | (let* ((process (or process (python-shell-get-process-or-error msg))) | ||
| 2889 | (encoding (with-temp-buffer | ||
| 2890 | (insert-file-contents | ||
| 2891 | (or temp-file-name file-name)) | ||
| 2892 | (python-info-encoding))) | ||
| 2893 | (file-name (expand-file-name | ||
| 2894 | (or (file-remote-p file-name 'localname) | ||
| 2895 | file-name))) | ||
| 2838 | (temp-file-name (when temp-file-name | 2896 | (temp-file-name (when temp-file-name |
| 2839 | (expand-file-name | 2897 | (expand-file-name |
| 2840 | (or (file-remote-p temp-file-name 'localname) | 2898 | (or (file-remote-p temp-file-name 'localname) |
| 2841 | temp-file-name)))) | 2899 | temp-file-name))))) |
| 2842 | (file-name (or (when file-name | ||
| 2843 | (expand-file-name | ||
| 2844 | (or (file-remote-p file-name 'localname) | ||
| 2845 | file-name))) | ||
| 2846 | temp-file-name))) | ||
| 2847 | (when (not file-name) | ||
| 2848 | (error "If FILE-NAME is nil then TEMP-FILE-NAME must be non-nil")) | ||
| 2849 | (python-shell-send-string | 2900 | (python-shell-send-string |
| 2850 | (format | 2901 | (format |
| 2851 | (concat "__pyfile = open('''%s''');" | 2902 | (concat |
| 2852 | "exec(compile(__pyfile.read(), '''%s''', 'exec'));" | 2903 | "import codecs, os;" |
| 2853 | "__pyfile.close()%s") | 2904 | "__pyfile = codecs.open('''%s''', encoding='''%s''');" |
| 2854 | (or temp-file-name file-name) file-name | 2905 | "__code = __pyfile.read().encode('''%s''');" |
| 2855 | (if delete (format "; import os; os.remove('''%s''')" | 2906 | "__pyfile.close();" |
| 2856 | (or temp-file-name file-name)) | 2907 | (when (and delete temp-file-name) |
| 2857 | "")) | 2908 | (format "os.remove('''%s''');" temp-file-name)) |
| 2909 | "exec(compile(__code, '''%s''', 'exec'));") | ||
| 2910 | (or temp-file-name file-name) encoding encoding file-name) | ||
| 2858 | process))) | 2911 | process))) |
| 2859 | 2912 | ||
| 2860 | (defun python-shell-switch-to-shell () | 2913 | (defun python-shell-switch-to-shell (&optional msg) |
| 2861 | "Switch to inferior Python process buffer." | 2914 | "Switch to inferior Python process buffer. |
| 2862 | (interactive) | 2915 | When optional argument MSG is non-nil, forces display of a |
| 2863 | (process-buffer (python-shell-get-or-create-process)) t) | 2916 | user-friendly message if there's no process running; defaults to |
| 2917 | t when called interactively." | ||
| 2918 | (interactive "p") | ||
| 2919 | (pop-to-buffer | ||
| 2920 | (process-buffer (python-shell-get-process-or-error msg)) nil t)) | ||
| 2864 | 2921 | ||
| 2865 | (defun python-shell-send-setup-code () | 2922 | (defun python-shell-send-setup-code () |
| 2866 | "Send all setup code for shell. | 2923 | "Send all setup code for shell. |
| @@ -2947,6 +3004,194 @@ the full statement in the case of imports." | |||
| 2947 | "25.1" | 3004 | "25.1" |
| 2948 | "Completion string code must work for (i)pdb.") | 3005 | "Completion string code must work for (i)pdb.") |
| 2949 | 3006 | ||
| 3007 | (defcustom python-shell-completion-native-disabled-interpreters | ||
| 3008 | ;; PyPy's readline cannot handle some escape sequences yet. | ||
| 3009 | (list "pypy") | ||
| 3010 | "List of disabled interpreters. | ||
| 3011 | When a match is found, native completion is disabled." | ||
| 3012 | :type '(repeat string)) | ||
| 3013 | |||
| 3014 | (defcustom python-shell-completion-native-enable t | ||
| 3015 | "Enable readline based native completion." | ||
| 3016 | :type 'boolean) | ||
| 3017 | |||
| 3018 | (defcustom python-shell-completion-native-output-timeout 0.01 | ||
| 3019 | "Time in seconds to wait for completion output before giving up." | ||
| 3020 | :type 'float) | ||
| 3021 | |||
| 3022 | (defvar python-shell-completion-native-redirect-buffer | ||
| 3023 | " *Python completions redirect*" | ||
| 3024 | "Buffer to be used to redirect output of readline commands.") | ||
| 3025 | |||
| 3026 | (defun python-shell-completion-native-interpreter-disabled-p () | ||
| 3027 | "Return non-nil if interpreter has native completion disabled." | ||
| 3028 | (when python-shell-completion-native-disabled-interpreters | ||
| 3029 | (string-match | ||
| 3030 | (regexp-opt python-shell-completion-native-disabled-interpreters) | ||
| 3031 | (file-name-nondirectory python-shell-interpreter)))) | ||
| 3032 | |||
| 3033 | (defun python-shell-completion-native-try () | ||
| 3034 | "Return non-nil if can trigger native completion." | ||
| 3035 | (let ((python-shell-completion-native-enable t)) | ||
| 3036 | (python-shell-completion-native-get-completions | ||
| 3037 | (get-buffer-process (current-buffer)) | ||
| 3038 | nil "int"))) | ||
| 3039 | |||
| 3040 | (defun python-shell-completion-native-setup () | ||
| 3041 | "Try to setup native completion, return non-nil on success." | ||
| 3042 | (let ((process (python-shell-get-process))) | ||
| 3043 | (python-shell-send-string | ||
| 3044 | (funcall | ||
| 3045 | 'mapconcat | ||
| 3046 | #'identity | ||
| 3047 | (list | ||
| 3048 | "try:" | ||
| 3049 | " import readline, rlcompleter" | ||
| 3050 | ;; Remove parens on callables as it breaks completion on | ||
| 3051 | ;; arguments (e.g. str(Ari<tab>)). | ||
| 3052 | " class Completer(rlcompleter.Completer):" | ||
| 3053 | " def _callable_postfix(self, val, word):" | ||
| 3054 | " return word" | ||
| 3055 | " readline.set_completer(Completer().complete)" | ||
| 3056 | " if readline.__doc__ and 'libedit' in readline.__doc__:" | ||
| 3057 | " readline.parse_and_bind('bind ^I rl_complete')" | ||
| 3058 | " else:" | ||
| 3059 | " readline.parse_and_bind('tab: complete')" | ||
| 3060 | " print ('python.el: readline is available')" | ||
| 3061 | "except:" | ||
| 3062 | " print ('python.el: readline not available')") | ||
| 3063 | "\n") | ||
| 3064 | process) | ||
| 3065 | (python-shell-accept-process-output process) | ||
| 3066 | (when (save-excursion | ||
| 3067 | (re-search-backward | ||
| 3068 | (regexp-quote "python.el: readline is available") nil t 1)) | ||
| 3069 | (python-shell-completion-native-try)))) | ||
| 3070 | |||
| 3071 | (defun python-shell-completion-native-turn-off (&optional msg) | ||
| 3072 | "Turn off shell native completions. | ||
| 3073 | With argument MSG show deactivation message." | ||
| 3074 | (interactive "p") | ||
| 3075 | (python-shell-with-shell-buffer | ||
| 3076 | (set (make-local-variable 'python-shell-completion-native-enable) nil) | ||
| 3077 | (when msg | ||
| 3078 | (message "Shell native completion is disabled, using fallback")))) | ||
| 3079 | |||
| 3080 | (defun python-shell-completion-native-turn-on (&optional msg) | ||
| 3081 | "Turn on shell native completions. | ||
| 3082 | With argument MSG show deactivation message." | ||
| 3083 | (interactive "p") | ||
| 3084 | (python-shell-with-shell-buffer | ||
| 3085 | (set (make-local-variable 'python-shell-completion-native-enable) t) | ||
| 3086 | (python-shell-completion-native-turn-on-maybe msg))) | ||
| 3087 | |||
| 3088 | (defun python-shell-completion-native-turn-on-maybe (&optional msg) | ||
| 3089 | "Turn on native completions if enabled and available. | ||
| 3090 | With argument MSG show activation/deactivation message." | ||
| 3091 | (interactive "p") | ||
| 3092 | (python-shell-with-shell-buffer | ||
| 3093 | (when python-shell-completion-native-enable | ||
| 3094 | (cond | ||
| 3095 | ((python-shell-completion-native-interpreter-disabled-p) | ||
| 3096 | (python-shell-completion-native-turn-off msg)) | ||
| 3097 | ((python-shell-completion-native-setup) | ||
| 3098 | (when msg | ||
| 3099 | (message "Shell native completion is enabled."))) | ||
| 3100 | (t (lwarn | ||
| 3101 | '(python python-shell-completion-native-turn-on-maybe) | ||
| 3102 | :warning | ||
| 3103 | (concat | ||
| 3104 | "Your `python-shell-interpreter' doesn't seem to " | ||
| 3105 | "support readline, yet `python-shell-completion-native' " | ||
| 3106 | (format "was `t' and %S is not part of the " | ||
| 3107 | (file-name-nondirectory python-shell-interpreter)) | ||
| 3108 | "`python-shell-completion-native-disabled-interpreters' " | ||
| 3109 | "list. Native completions have been disabled locally. ")) | ||
| 3110 | (python-shell-completion-native-turn-off msg)))))) | ||
| 3111 | |||
| 3112 | (defun python-shell-completion-native-turn-on-maybe-with-msg () | ||
| 3113 | "Like `python-shell-completion-native-turn-on-maybe' but force messages." | ||
| 3114 | (python-shell-completion-native-turn-on-maybe t)) | ||
| 3115 | |||
| 3116 | (add-hook 'inferior-python-mode-hook | ||
| 3117 | #'python-shell-completion-native-turn-on-maybe-with-msg) | ||
| 3118 | |||
| 3119 | (defun python-shell-completion-native-toggle (&optional msg) | ||
| 3120 | "Toggle shell native completion. | ||
| 3121 | With argument MSG show activation/deactivation message." | ||
| 3122 | (interactive "p") | ||
| 3123 | (python-shell-with-shell-buffer | ||
| 3124 | (if python-shell-completion-native-enable | ||
| 3125 | (python-shell-completion-native-turn-off msg) | ||
| 3126 | (python-shell-completion-native-turn-on msg)) | ||
| 3127 | python-shell-completion-native-enable)) | ||
| 3128 | |||
| 3129 | (defun python-shell-completion-native-get-completions (process import input) | ||
| 3130 | "Get completions using native readline for PROCESS. | ||
| 3131 | When IMPORT is non-nil takes precedence over INPUT for | ||
| 3132 | completion." | ||
| 3133 | (when (and python-shell-completion-native-enable | ||
| 3134 | (python-util-comint-last-prompt) | ||
| 3135 | (>= (point) (cdr (python-util-comint-last-prompt)))) | ||
| 3136 | (let* ((input (or import input)) | ||
| 3137 | (original-filter-fn (process-filter process)) | ||
| 3138 | (redirect-buffer (get-buffer-create | ||
| 3139 | python-shell-completion-native-redirect-buffer)) | ||
| 3140 | (separators (python-rx | ||
| 3141 | (or whitespace open-paren close-paren))) | ||
| 3142 | (trigger "\t\t\t") | ||
| 3143 | (new-input (concat input trigger)) | ||
| 3144 | (input-length | ||
| 3145 | (save-excursion | ||
| 3146 | (+ (- (point-max) (comint-bol)) (length new-input)))) | ||
| 3147 | (delete-line-command (make-string input-length ?\b)) | ||
| 3148 | (input-to-send (concat new-input delete-line-command))) | ||
| 3149 | ;; Ensure restoring the process filter, even if the user quits | ||
| 3150 | ;; or there's some other error. | ||
| 3151 | (unwind-protect | ||
| 3152 | (with-current-buffer redirect-buffer | ||
| 3153 | ;; Cleanup the redirect buffer | ||
| 3154 | (delete-region (point-min) (point-max)) | ||
| 3155 | ;; Mimic `comint-redirect-send-command', unfortunately it | ||
| 3156 | ;; can't be used here because it expects a newline in the | ||
| 3157 | ;; command and that's exactly what we are trying to avoid. | ||
| 3158 | (let ((comint-redirect-echo-input nil) | ||
| 3159 | (comint-redirect-verbose nil) | ||
| 3160 | (comint-redirect-perform-sanity-check nil) | ||
| 3161 | (comint-redirect-insert-matching-regexp nil) | ||
| 3162 | ;; Feed it some regex that will never match. | ||
| 3163 | (comint-redirect-finished-regexp "^\\'$") | ||
| 3164 | (comint-redirect-output-buffer redirect-buffer)) | ||
| 3165 | ;; Compatibility with Emacs 24.x. Comint changed and | ||
| 3166 | ;; now `comint-redirect-filter' gets 3 args. This | ||
| 3167 | ;; checks which version of `comint-redirect-filter' is | ||
| 3168 | ;; in use based on its args and uses `apply-partially' | ||
| 3169 | ;; to make it up for the 3 args case. | ||
| 3170 | (if (= (length | ||
| 3171 | (help-function-arglist 'comint-redirect-filter)) 3) | ||
| 3172 | (set-process-filter | ||
| 3173 | process (apply-partially | ||
| 3174 | #'comint-redirect-filter original-filter-fn)) | ||
| 3175 | (set-process-filter process #'comint-redirect-filter)) | ||
| 3176 | (process-send-string process input-to-send) | ||
| 3177 | (accept-process-output | ||
| 3178 | process | ||
| 3179 | python-shell-completion-native-output-timeout) | ||
| 3180 | ;; XXX: can't use `python-shell-accept-process-output' | ||
| 3181 | ;; here because there are no guarantees on how output | ||
| 3182 | ;; ends. The workaround here is to call | ||
| 3183 | ;; `accept-process-output' until we don't find anything | ||
| 3184 | ;; else to accept. | ||
| 3185 | (while (accept-process-output | ||
| 3186 | process | ||
| 3187 | python-shell-completion-native-output-timeout)) | ||
| 3188 | (cl-remove-duplicates | ||
| 3189 | (split-string | ||
| 3190 | (buffer-substring-no-properties | ||
| 3191 | (point-min) (point-max)) | ||
| 3192 | separators t)))) | ||
| 3193 | (set-process-filter process original-filter-fn))))) | ||
| 3194 | |||
| 2950 | (defun python-shell-completion-get-completions (process import input) | 3195 | (defun python-shell-completion-get-completions (process import input) |
| 2951 | "Do completion at point using PROCESS for IMPORT or INPUT. | 3196 | "Do completion at point using PROCESS for IMPORT or INPUT. |
| 2952 | When IMPORT is non-nil takes precedence over INPUT for | 3197 | When IMPORT is non-nil takes precedence over INPUT for |
| @@ -3004,11 +3249,15 @@ using that one instead of current buffer's process." | |||
| 3004 | last-prompt-end | 3249 | last-prompt-end |
| 3005 | (forward-char (length (match-string-no-properties 0))) | 3250 | (forward-char (length (match-string-no-properties 0))) |
| 3006 | (point)))) | 3251 | (point)))) |
| 3007 | (end (point))) | 3252 | (end (point)) |
| 3253 | (completion-fn | ||
| 3254 | (if python-shell-completion-native-enable | ||
| 3255 | #'python-shell-completion-native-get-completions | ||
| 3256 | #'python-shell-completion-get-completions))) | ||
| 3008 | (list start end | 3257 | (list start end |
| 3009 | (completion-table-dynamic | 3258 | (completion-table-dynamic |
| 3010 | (apply-partially | 3259 | (apply-partially |
| 3011 | #'python-shell-completion-get-completions | 3260 | completion-fn |
| 3012 | process import-statement))))) | 3261 | process import-statement))))) |
| 3013 | 3262 | ||
| 3014 | (define-obsolete-function-alias | 3263 | (define-obsolete-function-alias |
| @@ -4155,6 +4404,32 @@ operator." | |||
| 4155 | (* whitespace) line-end)) | 4404 | (* whitespace) line-end)) |
| 4156 | (string-equal "" (match-string-no-properties 1)))) | 4405 | (string-equal "" (match-string-no-properties 1)))) |
| 4157 | 4406 | ||
| 4407 | (defun python-info-encoding-from-cookie () | ||
| 4408 | "Detect current buffer's encoding from its coding cookie. | ||
| 4409 | Returns the enconding as a symbol." | ||
| 4410 | (let ((first-two-lines | ||
| 4411 | (save-excursion | ||
| 4412 | (save-restriction | ||
| 4413 | (widen) | ||
| 4414 | (goto-char (point-min)) | ||
| 4415 | (forward-line 2) | ||
| 4416 | (buffer-substring-no-properties | ||
| 4417 | (point) | ||
| 4418 | (point-min)))))) | ||
| 4419 | (when (string-match (python-rx coding-cookie) first-two-lines) | ||
| 4420 | (intern (match-string-no-properties 1 first-two-lines))))) | ||
| 4421 | |||
| 4422 | (defun python-info-encoding () | ||
| 4423 | "Return encoding for file. | ||
| 4424 | Try `python-info-encoding-from-cookie', if none is found then | ||
| 4425 | default to utf-8." | ||
| 4426 | ;; If no enconding is defined, then it's safe to use UTF-8: Python 2 | ||
| 4427 | ;; uses ASCII as default while Python 3 uses UTF-8. This means that | ||
| 4428 | ;; in the worst case escenario python.el will make things work for | ||
| 4429 | ;; Python 2 files with unicode data and no encoding defined. | ||
| 4430 | (or (python-info-encoding-from-cookie) | ||
| 4431 | 'utf-8)) | ||
| 4432 | |||
| 4158 | 4433 | ||
| 4159 | ;;; Utility functions | 4434 | ;;; Utility functions |
| 4160 | 4435 | ||