Function: python-shell-completion-native-get-completions

python-shell-completion-native-get-completions is a byte-compiled function defined in python.el.gz.

Signature

(python-shell-completion-native-get-completions PROCESS INPUT)

Documentation

Get completions of INPUT using native readline for PROCESS.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defun python-shell-completion-native-get-completions (process input)
  "Get completions of INPUT using native readline for PROCESS."
  (with-current-buffer (process-buffer process)
    (let* ((original-filter-fn (process-filter process))
           (redirect-buffer (get-buffer-create
                             python-shell-completion-native-redirect-buffer))
           (sep (if (string= python-shell-readline-completer-delims "")
                    "[\n\r]+" "[ \f\t\n\r\v()]+"))
           (trigger "\t")
           (new-input (concat input trigger))
           (input-length
            (save-excursion
              (+ (- (point-max) (comint-bol)) (length new-input))))
           (delete-line-command (make-string input-length ?\b))
           (input-to-send (concat new-input delete-line-command)))
      ;; Ensure restoring the process filter, even if the user quits
      ;; or there's some other error.
      (unwind-protect
          (with-current-buffer redirect-buffer
            ;; Cleanup the redirect buffer
            (erase-buffer)
            ;; Mimic `comint-redirect-send-command', unfortunately it
            ;; can't be used here because it expects a newline in the
            ;; command and that's exactly what we are trying to avoid.
            (let ((comint-redirect-echo-input nil)
                  (comint-redirect-completed nil)
                  (comint-redirect-perform-sanity-check nil)
                  (comint-redirect-insert-matching-regexp t)
                  (comint-redirect-finished-regexp
                   "1__dummy_completion__.*\n")
                  (comint-redirect-output-buffer redirect-buffer))
              ;; Compatibility with Emacs 24.x.  Comint changed and
              ;; now `comint-redirect-filter' gets 3 args.  This
              ;; checks which version of `comint-redirect-filter' is
              ;; in use based on its args and uses `apply-partially'
              ;; to make it up for the 3 args case.
              (if (= (length
                      (help-function-arglist 'comint-redirect-filter))
                     3)
                  (set-process-filter
                   process (apply-partially
                            #'comint-redirect-filter original-filter-fn))
                (set-process-filter process #'comint-redirect-filter))
              (process-send-string process input-to-send)
              ;; Grab output until our dummy completion used as
              ;; output end marker is found.
              (when (python-shell-accept-process-output
                     process python-shell-completion-native-output-timeout
                     comint-redirect-finished-regexp)
                (re-search-backward "0__dummy_completion__" nil t)
                (let ((str (buffer-substring-no-properties
                            (line-beginning-position) (point-min))))
                  ;; The readline completer is allowed to return a list
                  ;; of (text start end type signature) as a JSON
                  ;; string.  See the return value for IPython in
                  ;; `python-shell-completion-setup-code'.
                  (if (string= "[" (substring str 0 1))
                      (condition-case nil
                          (python--parse-json-array str)
                        (t (cl-remove-duplicates (split-string str sep t)
                                                 :test #'string=)))
                    (cl-remove-duplicates (split-string str sep t)
                                          :test #'string=))))))
        (set-process-filter process original-filter-fn)))))