]> code.delx.au - gnu-emacs/commitdiff
Complete module import lines in addition to variable names
authorDan Davison <dandavison7@gmail.com>
Thu, 17 May 2012 03:03:27 +0000 (00:03 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Thu, 17 May 2012 03:03:27 +0000 (00:03 -0300)
Available when using ipython v0.11. Completions on lines starting with
"from " or "import " are supplied by
IPython.core.completerlib.module_completion

ipython v0.11 configuration:

(setq python-shell-interpreter "ipython"
      python-shell-completion-setup-code
      "from IPython.core.completerlib import module_completion\n"
      python-shell-module-completion-string-code
      "';'.join(module_completion('''%s'''))\n"
      python-shell-completion-string-code
      "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")

lisp/progmodes/python.el

index 4fe5bd87462a271e10feddbfaa7a5c533ef80104..72df547d76007195290af2ebc354f49de34e074d 100644 (file)
@@ -1600,24 +1600,48 @@ else:
   :group 'python
   :safe 'stringp)
 
-(defun python-shell-completion--get-completions (input process)
+(defcustom python-shell-module-completion-string-code ""
+  "Python code used to get a string of completions separated by
+  semicolons on a module import line.
+
+For IPython v0.11, add the following line to
+`python-shell-completion-setup-code':
+
+from IPython.core.completerlib import module_completion
+
+and use the following as the value of this variable:
+
+';'.join(module_completion('''%s'''))\n"
+  :type 'string
+  :group 'python
+  :safe 'stringp)
+
+(defvar python-shell-import-line-regexp "^\\(from\\|import\\)[ \t]")
+
+(defun python-shell-completion--get-completions (input process completion-code)
   "Retrieve available completions for INPUT using PROCESS."
   (with-current-buffer (process-buffer process)
     (let ((completions (python-shell-send-string-no-output
-                        (format python-shell-completion-string-code input)
-                        process)))
+                        (format completion-code input) process)))
       (when (> (length completions) 2)
         (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t)))))
 
 (defun python-shell-completion--do-completion-at-point (process)
   "Do completion for INPUT using COMPLETIONS."
   (with-syntax-table python-dotty-syntax-table
-    (let* ((input (substring-no-properties
-                       (or (comint-word (current-word)) "") nil nil))
-        (completions (python-shell-completion--get-completions
-                             input process))
-        (completion (when completions
-                      (try-completion input completions))))
+    (let* ((line (substring-no-properties
+                 (buffer-substring (point-at-bol) (point)) nil nil))
+          (input (substring-no-properties
+                  (or (comint-word (current-word)) "") nil nil))
+          (completions
+           (if (and (> (length python-shell-module-completion-string-code) 0)
+                    (string-match python-shell-import-line-regexp line))
+               (python-shell-completion--get-completions
+                line process python-shell-module-completion-string-code)
+             (python-shell-completion--get-completions
+              input process python-shell-completion-string-code)))
+          (completion (when completions
+                        (try-completion input completions))))
       (cond ((eq completion t)
            t)
            ((null completion)