]> git.eshelyaron.com Git - emacs.git/commitdiff
Better shell setup using the new python-shell-send-setup-codes function.
authorFabián Ezequiel Gallina <fgallina@cuca>
Thu, 17 May 2012 03:03:09 +0000 (00:03 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Thu, 17 May 2012 03:03:09 +0000 (00:03 -0300)
At the moment of shell setup, all the pending output is accepted so
the prompt is always displayed correctly.

lisp/progmodes/python.el

index b745050f551cae2d14953562e8a52e99357e1a40..92a19309dc1f503fbebb0970e3b435a4372265bd 100644 (file)
@@ -963,14 +963,14 @@ Returns nil if point is not in a def or class."
 
 (defcustom python-shell-interpreter "python"
   "Default Python interpreter for shell."
-  :group 'python
   :type 'string
+  :group 'python
   :safe 'stringp)
 
 (defcustom python-shell-interpreter-args "-i"
   "Default arguments for the Python interpreter."
-  :group 'python
   :type 'string
+  :group 'python
   :safe 'stringp)
 
 (defcustom python-shell-prompt-regexp ">>> "
@@ -1001,6 +1001,18 @@ The regex should not contain a caret (^) at the beginning."
   :group 'python
   :safe 'stringp)
 
+(defcustom python-shell-setup-codes '(python-shell-completion-setup-code
+                                      python-ffap-setup-code
+                                      python-eldoc-setup-code)
+  "List of code run by `python-shell-send-setup-codes'.
+Each variable can be either a simple string with the code to
+execute or a cons with the form (CODE . DESCRIPTION), where CODE
+is a string with the code to execute and DESCRIPTION is the
+description of it."
+  :type '(repeat symbol)
+  :group 'python
+  :safe 'listp)
+
 (defcustom python-shell-compilation-regexp-alist
   `((,(rx line-start (1+ (any " \t")) "File \""
          (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
@@ -1256,6 +1268,24 @@ FILE-NAME."
   (interactive)
   (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t))
 
+(defun python-shell-send-setup-code ()
+  "Send all setup code for shell.
+This function takes the list of setup code to send from the
+`python-shell-setup-codes' list."
+  (let ((msg "Sent %s")
+        (process (get-buffer-process (current-buffer))))
+    (accept-process-output process 1)
+    (dolist (code python-shell-setup-codes)
+      (when code
+        (when (consp code)
+          (setq msg (cdr code)))
+        (message (format msg code))
+        (python-shell-send-string-no-output
+         (symbol-value code) process)))))
+
+(add-hook 'inferior-python-mode-hook
+          #'python-shell-send-setup-code)
+
 \f
 ;;; Shell completion
 
@@ -1286,16 +1316,6 @@ else:
   "';'.join(__COMPLETER_all_completions('''%s'''))\n"
   "Python code used to get a string of completions separated by semicolons.")
 
-(defun python-shell-completion-setup ()
-  "Send `python-shell-completion-setup-code' to inferior Python process.
-It is specially designed to be added to the
-`inferior-python-mode-hook'."
-  (when (> (length python-shell-completion-setup-code) 0)
-    (python-shell-send-string-no-output
-     python-shell-completion-setup-code
-     (get-buffer-process (current-buffer)))
-    (message "Completion setup code sent.")))
-
 (defun python-shell-completion--get-completions (input process)
   "Retrieve available completions for INPUT using PROCESS."
   (with-current-buffer (process-buffer process)
@@ -1349,9 +1369,6 @@ complete."
       (indent-for-tab-command)
     (comint-dynamic-complete)))
 
-(add-hook 'inferior-python-mode-hook
-          #'python-shell-completion-setup)
-
 \f
 ;;; PDB Track integration
 
@@ -1698,17 +1715,6 @@ The skeleton will be bound to python-skeleton-NAME."
   "__FFAP_get_module_path('''%s''')\n"
   "Python code used to get a string with the path of a module.")
 
-(defun python-ffap-setup ()
-  "Send `python-ffap-setup-code' to inferior Python process.
-It is specially designed to be added to the
-`inferior-python-mode-hook'."
-
-  (when (> (length python-ffap-setup-code) 0)
-    (python-shell-send-string-no-output
-     python-ffap-setup-code
-     (get-buffer-process (current-buffer)))
-    (message "FFAP setup code sent.")))
-
 (defun python-ffap-module-path (module)
   "Function for `ffap-alist' to return path for MODULE."
   (let ((process (or
@@ -1728,9 +1734,6 @@ It is specially designed to be added to the
      (push '(python-mode . python-ffap-module-path) ffap-alist)
      (push '(inferior-python-mode . python-ffap-module-path) ffap-alist)))
 
-(add-hook 'inferior-python-mode-hook
-          #'python-ffap-setup)
-
 \f
 ;;; Code check
 
@@ -1781,16 +1784,6 @@ Runs COMMAND, a shell command, as if by `compile'.  See
   "__PYDOC_get_help('''%s''')\n"
   "Python code used to get a string with the documentation of an object.")
 
-(defun python-eldoc-setup ()
-  "Send `python-eldoc-setup-code' to inferior Python process.
-It is specially designed to be added to the
-`inferior-python-mode-hook'."
-  (when (> (length python-eldoc-setup-code) 0)
-    (python-shell-send-string-no-output
-     python-eldoc-setup-code
-     (get-buffer-process (current-buffer)))
-    (message "Eldoc setup code sent.")))
-
 (defun python-eldoc--get-doc-at-point (&optional force-input force-process)
   "Internal implementation to get documentation at point.
 If not FORCE-INPUT is passed then what `current-word' returns
@@ -1862,9 +1855,6 @@ Interactively, prompt for symbol."
                (python-eldoc--get-doc-at-point symbol process))
               (help-print-return-message)))))))
 
-(add-hook 'inferior-python-mode-hook
-          #'python-eldoc-setup)
-
 \f
 ;;; Misc helpers