diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 4f222b4cf5..3239722981 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -3841,6 +3841,8 @@ python-shell-completion-get-completions (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t))))) +(defvar-local python-shell--capf-cache nil) + (defun python-shell-completion-at-point (&optional process) "Function for `completion-at-point-functions' in `inferior-python-mode'. Optional argument PROCESS forces completions to be retrieved @@ -3895,11 +3897,13 @@ python-shell-completion-at-point #'ignore #'python-shell-completion-get-completions)) (t #'python-shell-completion-native-get-completions))))) - (list start end - (completion-table-dynamic - (apply-partially - completion-fn - process import-statement))))) + (let ((re (or (car python-shell--capf-cache) regexp-unmatchable)) + (prefix (buffer-substring-no-properties start end))) + (unless (string-match re prefix) + (setq python-shell--capf-cache + (cons (concat "\\`" (regexp-quote prefix) "\\(?:\\sw\\|\\s_\\)*\\'") + (funcall completion-fn process import-statement prefix))))) + (list start end (cdr python-shell--capf-cache)))) (define-obsolete-function-alias 'python-shell-completion-complete-at-point