aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2012-08-14 01:18:41 -0300
committerFabián Ezequiel Gallina2012-08-14 01:18:41 -0300
commit5beed58664d830277199c61ef385f7d4383f1f38 (patch)
tree119c4bbe52a93869870d00ad1abc99ba9d3fe524 /lisp/progmodes/python.el
parent76d0e68f8d6bbae0886443c98322e81eab0dc5aa (diff)
downloademacs-5beed58664d830277199c61ef385f7d4383f1f38.tar.gz
emacs-5beed58664d830277199c61ef385f7d4383f1f38.zip
Use `completion-table-dynamic' for completion functions.
* progmodes/python.el (python-shell-completion--do-completion-at-point) (python-shell-completion--get-completions): Remove functions. (python-shell-completion-complete-at-point): New function. (python-completion-complete-at-point): Use it.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el143
1 files changed, 72 insertions, 71 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index f695d747483..ad2286d4b2b 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2018,27 +2018,71 @@ and use the following as the value of this variable:
2018 :type 'string 2018 :type 'string
2019 :group 'python) 2019 :group 'python)
2020 2020
2021(defun python-shell-completion--get-completions (input process completion-code) 2021(defun python-shell-completion-get-completions (process line input)
2022 "Retrieve available completions for INPUT using PROCESS. 2022 "Do completion at point for PROCESS.
2023Argument COMPLETION-CODE is the python code used to get 2023LINE is used to detect the context on how to complete given
2024completions on the current context." 2024INPUT."
2025 (with-current-buffer (process-buffer process) 2025 (let* ((prompt
2026 (let ((completions (python-shell-send-string-no-output 2026 ;; Get the last prompt for the inferior process
2027 (format completion-code input) process))) 2027 ;; buffer. This is used for the completion code selection
2028 (when (> (length completions) 2) 2028 ;; heuristic.
2029 (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t))))) 2029 (with-current-buffer (process-buffer process)
2030 2030 (buffer-substring-no-properties
2031(defun python-shell-completion--do-completion-at-point (process) 2031 (overlay-start comint-last-prompt-overlay)
2032 "Do completion at point for PROCESS." 2032 (overlay-end comint-last-prompt-overlay))))
2033 (with-syntax-table python-dotty-syntax-table 2033 (completion-context
2034 (let* ((beg 2034 ;; Check whether a prompt matches a pdb string, an import
2035 (save-excursion 2035 ;; statement or just the standard prompt and use the
2036 ;; correct python-shell-completion-*-code string
2037 (cond ((and (> (length python-shell-completion-pdb-string-code) 0)
2038 (string-match
2039 (concat "^" python-shell-prompt-pdb-regexp) prompt))
2040 'pdb)
2041 ((and (>
2042 (length python-shell-completion-module-string-code) 0)
2043 (string-match
2044 (concat "^" python-shell-prompt-regexp) prompt)
2045 (string-match "^[ \t]*\\(from\\|import\\)[ \t]" line))
2046 'import)
2047 ((string-match
2048 (concat "^" python-shell-prompt-regexp) prompt)
2049 'default)
2050 (t nil)))
2051 (completion-code
2052 (case completion-context
2053 (pdb python-shell-completion-pdb-string-code)
2054 (import python-shell-completion-module-string-code)
2055 (default python-shell-completion-string-code)
2056 (t nil)))
2057 (input
2058 (if (eq completion-context 'import)
2059 (replace-regexp-in-string "^[ \t]+" "" line)
2060 input)))
2061 (and completion-code
2062 (> (length input) 0)
2063 (with-current-buffer (process-buffer process)
2064 (let ((completions (python-shell-send-string-no-output
2065 (format completion-code input) process)))
2066 (and (> (length completions) 2)
2067 (split-string completions
2068 "^'\\|^\"\\|;\\|'$\\|\"$" t)))))))
2069
2070(defun python-shell-completion-complete-at-point (&optional process)
2071 "Perform completion at point in inferior Python.
2072Optional argument PROCESS forces completions to be retrieved
2073using that one instead of current buffer's process."
2074 (setq process (or process (get-buffer-process (current-buffer))))
2075 (let* ((start
2076 (save-excursion
2077 (with-syntax-table python-dotty-syntax-table
2036 (let* ((paren-depth (car (syntax-ppss))) 2078 (let* ((paren-depth (car (syntax-ppss)))
2037 (syntax-string "w_") 2079 (syntax-string "w_")
2038 (syntax-list (string-to-syntax syntax-string))) 2080 (syntax-list (string-to-syntax syntax-string)))
2039 ;; Stop scanning for the beginning of the completion subject 2081 ;; Stop scanning for the beginning of the completion
2040 ;; after the char before point matches a delimiter 2082 ;; subject after the char before point matches a
2041 (while (member (car (syntax-after (1- (point)))) syntax-list) 2083 ;; delimiter
2084 (while (member
2085 (car (syntax-after (1- (point)))) syntax-list)
2042 (skip-syntax-backward syntax-string) 2086 (skip-syntax-backward syntax-string)
2043 (when (or (equal (char-before) ?\)) 2087 (when (or (equal (char-before) ?\))
2044 (equal (char-before) ?\")) 2088 (equal (char-before) ?\"))
@@ -2047,58 +2091,15 @@ completions on the current context."
2047 ;; honor initial paren depth 2091 ;; honor initial paren depth
2048 (> (car (syntax-ppss)) paren-depth) 2092 (> (car (syntax-ppss)) paren-depth)
2049 (python-syntax-context 'string)) 2093 (python-syntax-context 'string))
2050 (forward-char -1)))) 2094 (forward-char -1)))
2051 (point))) 2095 (point)))))
2052 (end (point)) 2096 (end (point)))
2053 (line (buffer-substring-no-properties (point-at-bol) end)) 2097 (list start end
2054 (input (buffer-substring-no-properties beg end)) 2098 (completion-table-dynamic
2055 ;; Get the last prompt for the inferior process buffer. This is 2099 (apply-partially
2056 ;; used for the completion code selection heuristic. 2100 #'python-shell-completion-get-completions
2057 (prompt 2101 process (buffer-substring-no-properties
2058 (with-current-buffer (process-buffer process) 2102 (line-beginning-position) end))))))
2059 (buffer-substring-no-properties
2060 (overlay-start comint-last-prompt-overlay)
2061 (overlay-end comint-last-prompt-overlay))))
2062 (completion-context
2063 ;; Check whether a prompt matches a pdb string, an import statement
2064 ;; or just the standard prompt and use the correct
2065 ;; python-shell-completion-*-code string
2066 (cond ((and (> (length python-shell-completion-pdb-string-code) 0)
2067 (string-match
2068 (concat "^" python-shell-prompt-pdb-regexp) prompt))
2069 'pdb)
2070 ((and (>
2071 (length python-shell-completion-module-string-code) 0)
2072 (string-match
2073 (concat "^" python-shell-prompt-regexp) prompt)
2074 (string-match "^[ \t]*\\(from\\|import\\)[ \t]" line))
2075 'import)
2076 ((string-match
2077 (concat "^" python-shell-prompt-regexp) prompt)
2078 'default)
2079 (t nil)))
2080 (completion-code
2081 (case completion-context
2082 ('pdb python-shell-completion-pdb-string-code)
2083 ('import python-shell-completion-module-string-code)
2084 ('default python-shell-completion-string-code)
2085 (t nil)))
2086 (input
2087 (if (eq completion-context 'import)
2088 (replace-regexp-in-string "^[ \t]+" "" line)
2089 input))
2090 (completions
2091 (and completion-code (> (length input) 0)
2092 (python-shell-completion--get-completions
2093 input process completion-code))))
2094 (list beg end completions))))
2095
2096(defun python-shell-completion-complete-at-point ()
2097 "Perform completion at point in inferior Python process."
2098 (and comint-last-prompt-overlay
2099 (> (point-marker) (overlay-end comint-last-prompt-overlay))
2100 (python-shell-completion--do-completion-at-point
2101 (get-buffer-process (current-buffer)))))
2102 2103
2103(defun python-shell-completion-complete-or-indent () 2104(defun python-shell-completion-complete-or-indent ()
2104 "Complete or indent depending on the context. 2105 "Complete or indent depending on the context.
@@ -2210,7 +2211,7 @@ inferior python process is updated properly."
2210 (let ((process (python-shell-get-process))) 2211 (let ((process (python-shell-get-process)))
2211 (if (not process) 2212 (if (not process)
2212 (error "Completion needs an inferior Python process running") 2213 (error "Completion needs an inferior Python process running")
2213 (python-shell-completion--do-completion-at-point process)))) 2214 (python-shell-completion-complete-at-point process))))
2214 2215
2215(add-to-list 'debug-ignored-errors 2216(add-to-list 'debug-ignored-errors
2216 "^Completion needs an inferior Python process running.") 2217 "^Completion needs an inferior Python process running.")