Subject: Re: bug#7329: [Patch] Enable completion in inferior-python-mode

From: Андрей Парамонов<[email-masked]>
Date: Fri, 19 Nov 2010 23:33:37 +0300

Updated version of the patch attached. It does the following:

1) Enables completion in inferior Python mode.

2) Enables describe-symbol in inferior Python mode.

3) Checks for interpreter more intelligently.

Also it works with 23.2.

Andrey Paramonov
--- /home/pent/python.el.orig	2010-11-12 21:55:47.000000000 +0300
+++ /home/pent/python.el	2010-11-19 23:20:14.000000000 +0300
@@ -1365,13 +1365,9 @@
     ;; This will inherit from comint-mode-map.
     (define-key map "\C-c\C-l" 'python-load-file)
     (define-key map "\C-c\C-v" 'python-check)
-    ;; Note that we _can_ still use these commands which send to the
-    ;; Python process even at the prompt iff we have a normal prompt,
-    ;; i.e. '>>> ' and not '... '.  See the comment before
-    ;; python-send-region.  Fixme: uncomment these if we address that.
-
-    ;; (define-key map [(meta ?\t)] 'python-complete-symbol)
-    ;; (define-key map "\C-c\C-f" 'python-describe-symbol)
+    (substitute-key-definition 'complete-symbol 'completion-at-point
+			       map global-map)
+    (define-key map "\C-c\C-f" 'python-describe-symbol)
     map))
 
 (defvar inferior-python-mode-syntax-table
@@ -1412,6 +1408,8 @@
   (set (make-local-variable 'comint-input-filter) 'python-input-filter)
   (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter
 	    nil t)
+  (add-hook 'completion-at-point-functions
+	    'python-completion-at-point nil 'local)
   ;; Still required by `comint-redirect-send-command', for instance
   ;; (and we need to match things like `>>> ... >>> '):
   (set (make-local-variable 'comint-prompt-regexp)
@@ -1771,50 +1769,51 @@
 			    (format "Describe symbol (default %s): " symbol)
 			  "Describe symbol: ")
 			nil nil symbol))))
-  (if (equal symbol "") (error "No symbol"))
-  ;; Ensure we have a suitable help buffer.
-  ;; Fixme: Maybe process `Related help topics' a la help xrefs and
-  ;; allow C-c C-f in help buffer.
-  (let ((temp-buffer-show-hook		; avoid xref stuff
-	 (lambda ()
-	   (toggle-read-only 1)
-	   (setq view-return-to-alist
-		 (list (cons (selected-window) help-return-method))))))
-    (with-output-to-temp-buffer (help-buffer)
-      (with-current-buffer standard-output
- 	;; Fixme: Is this actually useful?
-	(help-setup-xref (list 'python-describe-symbol symbol)
-			 (called-interactively-p 'interactive))
-	(set (make-local-variable 'comint-redirect-subvert-readonly) t)
-	(help-print-return-message))))
-  (comint-redirect-send-command-to-process (format "emacs.ehelp(%S, %s)"
-						   symbol python-imports)
-   "*Help*" (python-proc) nil nil))
-
-(add-to-list 'debug-ignored-errors "^No symbol")
+  (when (python-check-comint-prompt)
+    (if (equal symbol "") (error "No symbol"))
+    ;; Ensure we have a suitable help buffer.
+    ;; Fixme: Maybe process `Related help topics' a la help xrefs and
+    ;; allow C-c C-f in help buffer.
+    (let ((temp-buffer-show-hook		; avoid xref stuff
+	   (lambda ()
+	     (toggle-read-only 1)
+	     (setq view-return-to-alist
+		   (list (cons (selected-window) help-return-method))))))
+      (with-output-to-temp-buffer (help-buffer)
+	(with-current-buffer standard-output
+	  ;; Fixme: Is this actually useful?
+	  (help-setup-xref (list 'python-describe-symbol symbol)
+			   (called-interactively-p 'interactive))
+	  (set (make-local-variable 'comint-redirect-subvert-readonly) t)
+	  (help-print-return-message))))
+    (comint-redirect-send-command-to-process 
+     (format "emacs.ehelp(%S, %s)" symbol python-imports) 
+     "*Help*" (python-proc) nil nil))
+  (add-to-list 'debug-ignored-errors "^No symbol"))
 
 (defun python-send-receive (string)
   "Send STRING to inferior Python (if any) and return result.
 The result is what follows `_emacs_out' in the output.
 This is a no-op if `python-check-comint-prompt' returns nil."
-  (python-send-string string)
-  (let ((proc (python-proc)))
-    (with-current-buffer (process-buffer proc)
-      (when (python-check-comint-prompt proc)
-	(set (make-local-variable 'python-preoutput-result) nil)
-	(while (progn
-		 (accept-process-output proc 5)
-		 (null python-preoutput-result)))
-	(prog1 python-preoutput-result
-	  (kill-local-variable 'python-preoutput-result))))))
+  (when (python-check-comint-prompt)
+    (python-send-string string)
+    (with-current-buffer (process-buffer (python-proc))
+      (set (make-local-variable 'python-preoutput-result) nil)
+      (while (progn
+	       (accept-process-output (python-proc) 5)
+	       (null python-preoutput-result)))
+      (prog1 python-preoutput-result
+	(kill-local-variable 'python-preoutput-result)))))
 
 (defun python-check-comint-prompt (&optional proc)
   "Return non-nil if and only if there's a normal prompt in the inferior buffer.
 If there isn't, it's probably not appropriate to send input to return Eldoc
 information etc.  If PROC is non-nil, check the buffer for that process."
-  (with-current-buffer (process-buffer (or proc (python-proc)))
-    (save-excursion
-      (save-match-data (re-search-backward ">>> \\=" nil t)))))
+  (let ((process (or proc (python-proc))))
+    (with-current-buffer (process-buffer process)
+      (save-excursion
+	(goto-char (process-mark process))
+	(save-match-data (re-search-backward "^>>> \\=" nil t))))))
 
 ;; Fixme:  Is there anything reasonable we can do with random methods?
 ;; (Currently only works with functions.)