From 7daa4ff121ad8da6e609b959d5c95796e5d3a9eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabi=C3=A1n=20Ezequiel=20Gallina?= Date: Mon, 28 Jul 2014 01:32:28 -0300 Subject: [PATCH] Parse completion input in a iPython friendly way. * lisp/progmodes/python.el (python-shell-completion-at-point): Rename from python-shell-completion-complete-at-point. (inferior-python-mode): Use it. (python-completion-at-point): Rename from python-completion-complete-at-point. Parse input up to first backward occurrence of whitespace, open-paren, close-paren or string delimiter. (python-mode): Use it. Fixes: debbugs:18084 --- lisp/ChangeLog | 13 ++++++++++ lisp/progmodes/python.el | 53 ++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 0dee47e5287..e00e58c20b5 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,16 @@ +2014-07-28 Fabián Ezequiel Gallina + + Parse completion input in a iPython friendly way. (Bug#18084) + * progmodes/python.el + (python-shell-completion-at-point): Rename from + python-shell-completion-complete-at-point. + (inferior-python-mode): Use it. + (python-completion-at-point): Rename from + python-completion-complete-at-point. Parse input up to first + backward occurrence of whitespace, open-paren, close-paren or + string delimiter. + (python-mode): Use it. + 2014-07-28 Fabián Ezequiel Gallina Prevent Python process shell buffer to pop twice. diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 5ec2b865a46..7d882837abc 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -2369,9 +2369,9 @@ variable. (define-key inferior-python-mode-map [remap complete-symbol] 'completion-at-point) (add-hook 'completion-at-point-functions - 'python-shell-completion-complete-at-point nil 'local) + 'python-shell-completion-at-point nil 'local) (add-to-list (make-local-variable 'comint-dynamic-complete-functions) - 'python-shell-completion-complete-at-point) + 'python-shell-completion-at-point) (define-key inferior-python-mode-map "\t" 'python-shell-completion-complete-or-indent) (make-local-variable 'python-pdbtrack-buffers-to-kill) @@ -2896,32 +2896,21 @@ LINE is used to detect the context on how to complete given INPUT." (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t))))))) -(defun python-shell-completion-complete-at-point (&optional process) - "Perform completion at point in inferior Python. +(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 using that one instead of current buffer's process." (setq process (or process (get-buffer-process (current-buffer)))) (let* ((start (save-excursion - (with-syntax-table python-dotty-syntax-table - (let* ((paren-depth (car (syntax-ppss))) - (syntax-string "w_") - (syntax-list (string-to-syntax syntax-string))) - ;; Stop scanning for the beginning of the completion - ;; subject after the char before point matches a - ;; delimiter - (while (member - (car (syntax-after (1- (point)))) syntax-list) - (skip-syntax-backward syntax-string) - (when (or (equal (char-before) ?\)) - (equal (char-before) ?\")) - (forward-char -1)) - (while (or - ;; honor initial paren depth - (> (car (syntax-ppss)) paren-depth) - (python-syntax-context 'string)) - (forward-char -1))) - (point))))) + (if (not (re-search-backward + (python-rx + (or whitespace open-paren close-paren string-delimiter)) + (cdr (python-util-comint-last-prompt)) + t 1)) + (cdr (python-util-comint-last-prompt)) + (forward-char (length (match-string-no-properties 0))) + (point)))) (end (point))) (list start end (completion-table-dynamic @@ -2930,6 +2919,11 @@ using that one instead of current buffer's process." process (buffer-substring-no-properties (line-beginning-position) end)))))) +(define-obsolete-function-alias + 'python-shell-completion-complete-at-point + 'python-shell-completion-at-point + "24.5") + (defun python-shell-completion-complete-or-indent () "Complete or indent depending on the context. If content before pointer is all whitespace, indent. @@ -3036,14 +3030,19 @@ Argument OUTPUT is a string with the output from the comint process." ;;; Symbol completion -(defun python-completion-complete-at-point () - "Complete current symbol at point. +(defun python-completion-at-point () + "Function for `completion-at-point-functions' in `python-mode'. For this to work as best as possible you should call `python-shell-send-buffer' from time to time so context in inferior Python process is updated properly." (let ((process (python-shell-get-process))) (when process - (python-shell-completion-complete-at-point process)))) + (python-shell-completion-at-point process)))) + +(define-obsolete-function-alias + 'python-completion-complete-at-point + 'python-completion-at-point + "24.5") ;;; Fill paragraph @@ -4268,7 +4267,7 @@ Arguments START and END narrow the buffer region to work on." #'python-nav-end-of-defun) (add-hook 'completion-at-point-functions - #'python-completion-complete-at-point nil 'local) + #'python-completion-at-point nil 'local) (add-hook 'post-self-insert-hook #'python-indent-post-self-insert-function 'append 'local) -- 2.39.2