aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorChong Yidong2010-11-21 12:04:43 -0500
committerChong Yidong2010-11-21 12:04:43 -0500
commitae3da38afc6269294faf1e6204128b5be2f40c3f (patch)
tree46f19e82c04d455aca103cb1b4a37a5c690ef36f /lisp/progmodes/python.el
parent1d851570a67adec22da23bdc7499bb489992d578 (diff)
downloademacs-ae3da38afc6269294faf1e6204128b5be2f40c3f.tar.gz
emacs-ae3da38afc6269294faf1e6204128b5be2f40c3f.zip
Backport fixes for Bug#5390 and Bug#5694 from trunk.
* progmodes/python.el: Add Ipython support (Bug#5390). (python-shell-prompt-alist) (python-shell-continuation-prompt-alist): New options. (python--set-prompt-regexp): New function. (inferior-python-mode, run-python, python-shell): Require ansi-color. Use python--set-prompt-regexp to set the comint prompt based on the Python interpreter. (python--prompt-regexp): New var. (python-check-comint-prompt) (python-comint-output-filter-function): Use it. (run-python): Use a pipe (Bug#5694).
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el95
1 files changed, 71 insertions, 24 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 653ef3150e5..6fdaa126b5b 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -570,6 +570,33 @@ having to restart the program."
570 "Queue of Python temp files awaiting execution. 570 "Queue of Python temp files awaiting execution.
571Currently-active file is at the head of the list.") 571Currently-active file is at the head of the list.")
572 572
573(defcustom python-shell-prompt-alist
574 '(("ipython" . "^In \\[[0-9]+\\]: *")
575 (t . "^>>> "))
576 "Alist of Python input prompts.
577Each element has the form (PROGRAM . REGEXP), where PROGRAM is
578the value of `python-python-command' for the python process and
579REGEXP is a regular expression matching the Python prompt.
580PROGRAM can also be t, which specifies the default when no other
581element matches `python-python-command'."
582 :type 'string
583 :group 'python
584 :version "24.1")
585
586(defcustom python-shell-continuation-prompt-alist
587 '(("ipython" . "^ [.][.][.]+: *")
588 (t . "^[.][.][.] "))
589 "Alist of Python continued-line prompts.
590Each element has the form (PROGRAM . REGEXP), where PROGRAM is
591the value of `python-python-command' for the python process and
592REGEXP is a regular expression matching the Python prompt for
593continued lines.
594PROGRAM can also be t, which specifies the default when no other
595element matches `python-python-command'."
596 :type 'string
597 :group 'python
598 :version "24.1")
599
573(defvar python-pdbtrack-is-tracking-p nil) 600(defvar python-pdbtrack-is-tracking-p nil)
574 601
575(defconst python-pdbtrack-stack-entry-regexp 602(defconst python-pdbtrack-stack-entry-regexp
@@ -1302,13 +1329,9 @@ See `python-check-command' for the default."
1302 1329
1303;;;; Inferior mode stuff (following cmuscheme). 1330;;;; Inferior mode stuff (following cmuscheme).
1304 1331
1305;; Fixme: Make sure we can work with IPython.
1306
1307(defcustom python-python-command "python" 1332(defcustom python-python-command "python"
1308 "Shell command to run Python interpreter. 1333 "Shell command to run Python interpreter.
1309Any arguments can't contain whitespace. 1334Any arguments can't contain whitespace."
1310Note that IPython may not work properly; it must at least be used
1311with the `-cl' flag, i.e. use `ipython -cl'."
1312 :group 'python 1335 :group 'python
1313 :type 'string) 1336 :type 'string)
1314 1337
@@ -1386,6 +1409,23 @@ local value.")
1386;; Autoloaded. 1409;; Autoloaded.
1387(declare-function compilation-shell-minor-mode "compile" (&optional arg)) 1410(declare-function compilation-shell-minor-mode "compile" (&optional arg))
1388 1411
1412(defvar python--prompt-regexp nil)
1413
1414(defun python--set-prompt-regexp ()
1415 (let ((prompt (cdr-safe (or (assoc python-python-command
1416 python-shell-prompt-alist)
1417 (assq t python-shell-prompt-alist))))
1418 (cprompt (cdr-safe (or (assoc python-python-command
1419 python-shell-continuation-prompt-alist)
1420 (assq t python-shell-continuation-prompt-alist)))))
1421 (set (make-local-variable 'comint-prompt-regexp)
1422 (concat "\\("
1423 (mapconcat 'identity
1424 (delq nil (list prompt cprompt "^([Pp]db) "))
1425 "\\|")
1426 "\\)"))
1427 (set (make-local-variable 'python--prompt-regexp) prompt)))
1428
1389;; Fixme: This should inherit some stuff from `python-mode', but I'm 1429;; Fixme: This should inherit some stuff from `python-mode', but I'm
1390;; not sure how much: at least some keybindings, like C-c C-f; 1430;; not sure how much: at least some keybindings, like C-c C-f;
1391;; syntax?; font-locking, e.g. for triple-quoted strings? 1431;; syntax?; font-locking, e.g. for triple-quoted strings?
@@ -1408,14 +1448,12 @@ For running multiple processes in multiple buffers, see `run-python' and
1408 1448
1409\\{inferior-python-mode-map}" 1449\\{inferior-python-mode-map}"
1410 :group 'python 1450 :group 'python
1451 (require 'ansi-color) ; for ipython
1411 (setq mode-line-process '(":%s")) 1452 (setq mode-line-process '(":%s"))
1412 (set (make-local-variable 'comint-input-filter) 'python-input-filter) 1453 (set (make-local-variable 'comint-input-filter) 'python-input-filter)
1413 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter 1454 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter
1414 nil t) 1455 nil t)
1415 ;; Still required by `comint-redirect-send-command', for instance 1456 (python--set-prompt-regexp)
1416 ;; (and we need to match things like `>>> ... >>> '):
1417 (set (make-local-variable 'comint-prompt-regexp)
1418 (rx line-start (1+ (and (or (repeat 3 (any ">.")) "(Pdb)") " "))))
1419 (set (make-local-variable 'compilation-error-regexp-alist) 1457 (set (make-local-variable 'compilation-error-regexp-alist)
1420 python-compilation-regexp-alist) 1458 python-compilation-regexp-alist)
1421 (compilation-shell-minor-mode 1)) 1459 (compilation-shell-minor-mode 1))
@@ -1522,12 +1560,12 @@ Don't save anything for STR matching `inferior-python-filter-regexp'."
1522 cmd))) 1560 cmd)))
1523 (unless (shell-command-to-string cmd) 1561 (unless (shell-command-to-string cmd)
1524 (error "Can't run Python command `%s'" cmd)) 1562 (error "Can't run Python command `%s'" cmd))
1525 (let* ((res (shell-command-to-string (concat cmd " --version")))) 1563 (let* ((res (shell-command-to-string
1526 (string-match "Python \\([0-9]\\)\\.\\([0-9]\\)" res) 1564 (concat cmd
1527 (unless (and (equal "2" (match-string 1 res)) 1565 " -c \"from sys import version_info;\
1528 (match-beginning 2) 1566print version_info >= (2, 2) and version_info < (3, 0)\""))))
1529 (>= (string-to-number (match-string 2 res)) 2)) 1567 (unless (string-match "True" res)
1530 (error "Only Python versions >= 2.2 and < 3.0 supported"))) 1568 (error "Only Python versions >= 2.2 and < 3.0 are supported")))
1531 (setq python-version-checked t))) 1569 (setq python-version-checked t)))
1532 1570
1533;;;###autoload 1571;;;###autoload
@@ -1554,6 +1592,7 @@ behavior, change `python-remove-cwd-from-path' to nil."
1554 (interactive (if current-prefix-arg 1592 (interactive (if current-prefix-arg
1555 (list (read-string "Run Python: " python-command) nil t) 1593 (list (read-string "Run Python: " python-command) nil t)
1556 (list python-command))) 1594 (list python-command)))
1595 (require 'ansi-color) ; for ipython
1557 (unless cmd (setq cmd python-command)) 1596 (unless cmd (setq cmd python-command))
1558 (python-check-version cmd) 1597 (python-check-version cmd)
1559 (setq python-command cmd) 1598 (setq python-command cmd)
@@ -1572,8 +1611,10 @@ behavior, change `python-remove-cwd-from-path' to nil."
1572 (if path (concat path path-separator)) 1611 (if path (concat path path-separator))
1573 data-directory) 1612 data-directory)
1574 process-environment)) 1613 process-environment))
1575 ;; Suppress use of pager for help output: 1614 ;; If we use a pipe, unicode characters are not printed
1576 (process-connection-type nil)) 1615 ;; correctly (Bug#5794) and IPython does not work at
1616 ;; all (Bug#5390).
1617 (process-connection-type t))
1577 (apply 'make-comint-in-buffer "Python" 1618 (apply 'make-comint-in-buffer "Python"
1578 (generate-new-buffer "*Python*") 1619 (generate-new-buffer "*Python*")
1579 (car cmdlist) nil (cdr cmdlist))) 1620 (car cmdlist) nil (cdr cmdlist)))
@@ -1629,7 +1670,12 @@ behavior, change `python-remove-cwd-from-path' to nil."
1629 ;; non-ASCII. 1670 ;; non-ASCII.
1630 (interactive "r") 1671 (interactive "r")
1631 (let* ((f (make-temp-file "py")) 1672 (let* ((f (make-temp-file "py"))
1632 (command (format "emacs.eexecfile(%S)" f)) 1673 (command
1674 ;; IPython puts the FakeModule module into __main__ so
1675 ;; emacs.eexecfile becomes useless.
1676 (if (string-match "^ipython" python-command)
1677 (format "execfile %S" f)
1678 (format "emacs.eexecfile(%S)" f)))
1633 (orig-start (copy-marker start))) 1679 (orig-start (copy-marker start)))
1634 (when (save-excursion 1680 (when (save-excursion
1635 (goto-char start) 1681 (goto-char start)
@@ -1829,7 +1875,9 @@ If there isn't, it's probably not appropriate to send input to return Eldoc
1829information etc. If PROC is non-nil, check the buffer for that process." 1875information etc. If PROC is non-nil, check the buffer for that process."
1830 (with-current-buffer (process-buffer (or proc (python-proc))) 1876 (with-current-buffer (process-buffer (or proc (python-proc)))
1831 (save-excursion 1877 (save-excursion
1832 (save-match-data (re-search-backward ">>> \\=" nil t))))) 1878 (save-match-data
1879 (re-search-backward (concat python--prompt-regexp " *\\=")
1880 nil t)))))
1833 1881
1834;; Fixme: Is there anything reasonable we can do with random methods? 1882;; Fixme: Is there anything reasonable we can do with random methods?
1835;; (Currently only works with functions.) 1883;; (Currently only works with functions.)
@@ -2545,9 +2593,7 @@ Runs `jython-mode-hook' after `python-mode-hook'."
2545 "Watch output for Python prompt and exec next file waiting in queue. 2593 "Watch output for Python prompt and exec next file waiting in queue.
2546This function is appropriate for `comint-output-filter-functions'." 2594This function is appropriate for `comint-output-filter-functions'."
2547 ;; TBD: this should probably use split-string 2595 ;; TBD: this should probably use split-string
2548 (when (and (or (string-equal string ">>> ") 2596 (when (and (string-match python--prompt-regexp string)
2549 (and (>= (length string) 5)
2550 (string-equal (substring string -5) "\n>>> ")))
2551 python-file-queue) 2597 python-file-queue)
2552 (condition-case nil 2598 (condition-case nil
2553 (delete-file (car python-file-queue)) 2599 (delete-file (car python-file-queue))
@@ -2759,6 +2805,7 @@ comint believe the user typed this string so that
2759 (funcall (process-filter proc) proc msg)) 2805 (funcall (process-filter proc) proc msg))
2760 (set-buffer curbuf)) 2806 (set-buffer curbuf))
2761 (process-send-string proc cmd))) 2807 (process-send-string proc cmd)))
2808
2762;;;###autoload 2809;;;###autoload
2763(defun python-shell (&optional argprompt) 2810(defun python-shell (&optional argprompt)
2764 "Start an interactive Python interpreter in another window. 2811 "Start an interactive Python interpreter in another window.
@@ -2798,6 +2845,7 @@ interaction between undo and process filters; the same problem exists in
2798non-Python process buffers using the default (Emacs-supplied) process 2845non-Python process buffers using the default (Emacs-supplied) process
2799filter." 2846filter."
2800 (interactive "P") 2847 (interactive "P")
2848 (require 'ansi-color) ; For ipython
2801 ;; Set the default shell if not already set 2849 ;; Set the default shell if not already set
2802 (when (null python-which-shell) 2850 (when (null python-which-shell)
2803 (python-toggle-shells python-default-interpreter)) 2851 (python-toggle-shells python-default-interpreter))
@@ -2814,10 +2862,9 @@ filter."
2814 )))) 2862 ))))
2815 (switch-to-buffer-other-window 2863 (switch-to-buffer-other-window
2816 (apply 'make-comint python-which-bufname python-which-shell nil args)) 2864 (apply 'make-comint python-which-bufname python-which-shell nil args))
2817 (make-local-variable 'comint-prompt-regexp)
2818 (set-process-sentinel (get-buffer-process (current-buffer)) 2865 (set-process-sentinel (get-buffer-process (current-buffer))
2819 'python-sentinel) 2866 'python-sentinel)
2820 (setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) ") 2867 (python--set-prompt-regexp)
2821 (add-hook 'comint-output-filter-functions 2868 (add-hook 'comint-output-filter-functions
2822 'python-comint-output-filter-function nil t) 2869 'python-comint-output-filter-function nil t)
2823 ;; pdbtrack 2870 ;; pdbtrack