aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorLiu Hui2024-02-26 18:46:36 +0800
committerBasil L. Contovounesios2024-03-14 15:09:56 +0100
commita7057745f5ef903a2655c6d9e7813168e361baf7 (patch)
tree96c9ebebdd1a3f8c2ffcc2405c3f2961fedadb18 /lisp/progmodes/python.el
parentc94d680f6eb46a47549633c7076fe32660b3cd42 (diff)
downloademacs-a7057745f5ef903a2655c6d9e7813168e361baf7.tar.gz
emacs-a7057745f5ef903a2655c6d9e7813168e361baf7.zip
Detect the readline support for Python shell completion
* lisp/progmodes/python.el (python-shell-comint-watch-for-first-prompt-output-filter): Detect the readline support. (python-shell-readline-completer-delims): Update docstring. (python-shell-completion-native-setup): Move the readline detection code to ... (python-shell-readline-detect): ... new function. (python-shell-completion-native-turn-on-maybe): Skip if Python has no readline support. (python-shell-completion-at-point): Respect the delimiter of readline completer in non-native completion. * test/lisp/progmodes/python-tests.el (python-shell-completion-at-point-1) (python-shell-completion-at-point-native-1) (python-completion-at-point-1, python-completion-at-point-2) (python-completion-at-point-pdb-1) (python-completion-at-point-while-running-1) (python-completion-at-point-native-1) (python-completion-at-point-native-2) (python-completion-at-point-native-with-ffap-1) (python-completion-at-point-native-with-eldoc-1): Skip tests if Python has no readline support. (python-shell-completion-at-point-jedi-completer): Add test for non-native Python shell completion. (bug#68559)
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el29
1 files changed, 22 insertions, 7 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 1016655cb62..8279617b6e7 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3601,6 +3601,7 @@ The coding cookie regexp is specified in PEP 263.")
3601 (python-shell-send-string-no-output python-shell-eval-file-setup-code)) 3601 (python-shell-send-string-no-output python-shell-eval-file-setup-code))
3602 (with-current-buffer (current-buffer) 3602 (with-current-buffer (current-buffer)
3603 (let ((inhibit-quit nil)) 3603 (let ((inhibit-quit nil))
3604 (python-shell-readline-detect)
3604 (run-hooks 'python-shell-first-prompt-hook)))))) 3605 (run-hooks 'python-shell-first-prompt-hook))))))
3605 output) 3606 output)
3606 3607
@@ -4361,7 +4362,23 @@ When a match is found, native completion is disabled."
4361 4362
4362(defvar python-shell-readline-completer-delims nil 4363(defvar python-shell-readline-completer-delims nil
4363 "Word delimiters used by the readline completer. 4364 "Word delimiters used by the readline completer.
4364It is automatically set by Python shell.") 4365It is automatically set by Python shell. An empty string means no
4366characters are considered delimiters and the readline completion
4367considers the entire line of input. A value of nil means the Python
4368shell has no readline support.")
4369
4370(defun python-shell-readline-detect ()
4371 "Detect the readline support for Python shell completion."
4372 (let* ((process (python-shell-get-process))
4373 (output (python-shell-send-string-no-output "
4374try:
4375 import readline
4376 print(readline.get_completer_delims())
4377except:
4378 print('No readline support')" process)))
4379 (setq-local python-shell-readline-completer-delims
4380 (unless (string-search "No readline support" output)
4381 (string-trim-right output)))))
4365 4382
4366(defvar python-shell-completion-native-redirect-buffer 4383(defvar python-shell-completion-native-redirect-buffer
4367 " *Python completions redirect*" 4384 " *Python completions redirect*"
@@ -4501,10 +4518,6 @@ def __PYTHON_EL_native_completion_setup():
4501__PYTHON_EL_native_completion_setup()" process))) 4518__PYTHON_EL_native_completion_setup()" process)))
4502 (when (string-match-p "python\\.el: native completion setup loaded" 4519 (when (string-match-p "python\\.el: native completion setup loaded"
4503 output) 4520 output)
4504 (setq-local python-shell-readline-completer-delims
4505 (string-trim-right
4506 (python-shell-send-string-no-output
4507 "import readline; print(readline.get_completer_delims())")))
4508 (python-shell-completion-native-try)))) 4521 (python-shell-completion-native-try))))
4509 4522
4510(defun python-shell-completion-native-turn-off (&optional msg) 4523(defun python-shell-completion-native-turn-off (&optional msg)
@@ -4533,7 +4546,8 @@ With argument MSG show activation/deactivation message."
4533 (cond 4546 (cond
4534 ((python-shell-completion-native-interpreter-disabled-p) 4547 ((python-shell-completion-native-interpreter-disabled-p)
4535 (python-shell-completion-native-turn-off msg)) 4548 (python-shell-completion-native-turn-off msg))
4536 ((python-shell-completion-native-setup) 4549 ((and python-shell-readline-completer-delims
4550 (python-shell-completion-native-setup))
4537 (when msg 4551 (when msg
4538 (message "Shell native completion is enabled."))) 4552 (message "Shell native completion is enabled.")))
4539 (t 4553 (t
@@ -4705,7 +4719,8 @@ using that one instead of current buffer's process."
4705 (with-current-buffer (process-buffer process) 4719 (with-current-buffer (process-buffer process)
4706 (if python-shell-completion-native-enable 4720 (if python-shell-completion-native-enable
4707 (string= python-shell-readline-completer-delims "") 4721 (string= python-shell-readline-completer-delims "")
4708 (string-match-p "ipython[23]?\\'" python-shell-interpreter))))) 4722 (or (string-match-p "ipython[23]?\\'" python-shell-interpreter)
4723 (equal python-shell-readline-completer-delims ""))))))
4709 (start 4724 (start
4710 (if (< (point) line-start) 4725 (if (< (point) line-start)
4711 (point) 4726 (point)