aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorKenichi Handa2010-11-25 12:56:41 +0900
committerKenichi Handa2010-11-25 12:56:41 +0900
commite957f9ae90f3cab1584c06877cbff075d52a6a9a (patch)
treed19aea6f4c6a3e369604ab8bc97be6d419073b0c /lisp/progmodes/python.el
parentb84ae584330c940010bc543fd925eddeb13fd9e2 (diff)
parent33aeea0eb66921329fde41e14cfda2565c6bad6d (diff)
downloademacs-e957f9ae90f3cab1584c06877cbff075d52a6a9a.tar.gz
emacs-e957f9ae90f3cab1584c06877cbff075d52a6a9a.zip
merge emacs-23
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el164
1 files changed, 108 insertions, 56 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index ed2a3236be1..6fdaa126b5b 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -170,18 +170,9 @@
170 ;; Make outer chars of matching triple-quote sequences into generic 170 ;; Make outer chars of matching triple-quote sequences into generic
171 ;; string delimiters. Fixme: Is there a better way? 171 ;; string delimiters. Fixme: Is there a better way?
172 ;; First avoid a sequence preceded by an odd number of backslashes. 172 ;; First avoid a sequence preceded by an odd number of backslashes.
173 `((,(rx (not (any ?\\)) 173 `((,(concat "\\(?:\\([RUru]\\)[Rr]?\\|^\\|[^\\]\\(?:\\\\.\\)*\\)" ;Prefix.
174 ?\\ (* (and ?\\ ?\\)) 174 "\\(?:\\('\\)'\\('\\)\\|\\(?2:\"\\)\"\\(?3:\"\\)\\)")
175 (group (syntax string-quote)) 175 (1 (python-quote-syntax 1) nil lax)
176 (backref 1)
177 (group (backref 1)))
178 (2 ,(string-to-syntax "\""))) ; dummy
179 (,(rx (group (optional (any "uUrR"))) ; prefix gets syntax property
180 (optional (any "rR")) ; possible second prefix
181 (group (syntax string-quote)) ; maybe gets property
182 (backref 2) ; per first quote
183 (group (backref 2))) ; maybe gets property
184 (1 (python-quote-syntax 1))
185 (2 (python-quote-syntax 2)) 176 (2 (python-quote-syntax 2))
186 (3 (python-quote-syntax 3))) 177 (3 (python-quote-syntax 3)))
187 ;; This doesn't really help. 178 ;; This doesn't really help.
@@ -219,9 +210,9 @@ Used for syntactic keywords. N is the match number (1, 2 or 3)."
219 (eval-when-compile (string-to-syntax "|")))))) 210 (eval-when-compile (string-to-syntax "|"))))))
220 ;; Consider property for initial char, accounting for prefixes. 211 ;; Consider property for initial char, accounting for prefixes.
221 ((or (and (= n 2) ; leading quote (not prefix) 212 ((or (and (= n 2) ; leading quote (not prefix)
222 (= (match-beginning 1) (match-end 1))) ; prefix is null 213 (not (match-end 1))) ; prefix is null
223 (and (= n 1) ; prefix 214 (and (= n 1) ; prefix
224 (/= (match-beginning 1) (match-end 1)))) ; non-empty 215 (match-end 1))) ; non-empty
225 (let ((font-lock-syntactic-keywords nil)) 216 (let ((font-lock-syntactic-keywords nil))
226 (unless (eq 'string (syntax-ppss-context (syntax-ppss))) 217 (unless (eq 'string (syntax-ppss-context (syntax-ppss)))
227 (eval-when-compile (string-to-syntax "|"))))) 218 (eval-when-compile (string-to-syntax "|")))))
@@ -579,6 +570,33 @@ having to restart the program."
579 "Queue of Python temp files awaiting execution. 570 "Queue of Python temp files awaiting execution.
580Currently-active file is at the head of the list.") 571Currently-active file is at the head of the list.")
581 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
582(defvar python-pdbtrack-is-tracking-p nil) 600(defvar python-pdbtrack-is-tracking-p nil)
583 601
584(defconst python-pdbtrack-stack-entry-regexp 602(defconst python-pdbtrack-stack-entry-regexp
@@ -1311,13 +1329,9 @@ See `python-check-command' for the default."
1311 1329
1312;;;; Inferior mode stuff (following cmuscheme). 1330;;;; Inferior mode stuff (following cmuscheme).
1313 1331
1314;; Fixme: Make sure we can work with IPython.
1315
1316(defcustom python-python-command "python" 1332(defcustom python-python-command "python"
1317 "Shell command to run Python interpreter. 1333 "Shell command to run Python interpreter.
1318Any arguments can't contain whitespace. 1334Any arguments can't contain whitespace."
1319Note that IPython may not work properly; it must at least be used
1320with the `-cl' flag, i.e. use `ipython -cl'."
1321 :group 'python 1335 :group 'python
1322 :type 'string) 1336 :type 'string)
1323 1337
@@ -1395,6 +1409,23 @@ local value.")
1395;; Autoloaded. 1409;; Autoloaded.
1396(declare-function compilation-shell-minor-mode "compile" (&optional arg)) 1410(declare-function compilation-shell-minor-mode "compile" (&optional arg))
1397 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
1398;; 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
1399;; 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;
1400;; syntax?; font-locking, e.g. for triple-quoted strings? 1431;; syntax?; font-locking, e.g. for triple-quoted strings?
@@ -1417,14 +1448,12 @@ For running multiple processes in multiple buffers, see `run-python' and
1417 1448
1418\\{inferior-python-mode-map}" 1449\\{inferior-python-mode-map}"
1419 :group 'python 1450 :group 'python
1451 (require 'ansi-color) ; for ipython
1420 (setq mode-line-process '(":%s")) 1452 (setq mode-line-process '(":%s"))
1421 (set (make-local-variable 'comint-input-filter) 'python-input-filter) 1453 (set (make-local-variable 'comint-input-filter) 'python-input-filter)
1422 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter 1454 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter
1423 nil t) 1455 nil t)
1424 ;; Still required by `comint-redirect-send-command', for instance 1456 (python--set-prompt-regexp)
1425 ;; (and we need to match things like `>>> ... >>> '):
1426 (set (make-local-variable 'comint-prompt-regexp)
1427 (rx line-start (1+ (and (or (repeat 3 (any ">.")) "(Pdb)") " "))))
1428 (set (make-local-variable 'compilation-error-regexp-alist) 1457 (set (make-local-variable 'compilation-error-regexp-alist)
1429 python-compilation-regexp-alist) 1458 python-compilation-regexp-alist)
1430 (compilation-shell-minor-mode 1)) 1459 (compilation-shell-minor-mode 1))
@@ -1435,6 +1464,16 @@ Default ignores all inputs of 0, 1, or 2 non-blank characters."
1435 :type 'regexp 1464 :type 'regexp
1436 :group 'python) 1465 :group 'python)
1437 1466
1467(defcustom python-remove-cwd-from-path t
1468 "Whether to allow loading of Python modules from the current directory.
1469If this is non-nil, Emacs removes '' from sys.path when starting
1470an inferior Python process. This is the default, for security
1471reasons, as it is easy for the Python process to be started
1472without the user's realization (e.g. to perform completion)."
1473 :type 'boolean
1474 :group 'python
1475 :version "23.3")
1476
1438(defun python-input-filter (str) 1477(defun python-input-filter (str)
1439 "`comint-input-filter' function for inferior Python. 1478 "`comint-input-filter' function for inferior Python.
1440Don't save anything for STR matching `inferior-python-filter-regexp'." 1479Don't save anything for STR matching `inferior-python-filter-regexp'."
@@ -1521,34 +1560,39 @@ Don't save anything for STR matching `inferior-python-filter-regexp'."
1521 cmd))) 1560 cmd)))
1522 (unless (shell-command-to-string cmd) 1561 (unless (shell-command-to-string cmd)
1523 (error "Can't run Python command `%s'" cmd)) 1562 (error "Can't run Python command `%s'" cmd))
1524 (let* ((res (shell-command-to-string (concat cmd " --version")))) 1563 (let* ((res (shell-command-to-string
1525 (string-match "Python \\([0-9]\\)\\.\\([0-9]\\)" res) 1564 (concat cmd
1526 (unless (and (equal "2" (match-string 1 res)) 1565 " -c \"from sys import version_info;\
1527 (match-beginning 2) 1566print version_info >= (2, 2) and version_info < (3, 0)\""))))
1528 (>= (string-to-number (match-string 2 res)) 2)) 1567 (unless (string-match "True" res)
1529 (error "Only Python versions >= 2.2 and < 3.0 supported"))) 1568 (error "Only Python versions >= 2.2 and < 3.0 are supported")))
1530 (setq python-version-checked t))) 1569 (setq python-version-checked t)))
1531 1570
1532;;;###autoload 1571;;;###autoload
1533(defun run-python (&optional cmd noshow new) 1572(defun run-python (&optional cmd noshow new)
1534 "Run an inferior Python process, input and output via buffer *Python*. 1573 "Run an inferior Python process, input and output via buffer *Python*.
1535CMD is the Python command to run. NOSHOW non-nil means don't show the 1574CMD is the Python command to run. NOSHOW non-nil means don't
1536buffer automatically. 1575show the buffer automatically.
1537 1576
1538Normally, if there is a process already running in `python-buffer', 1577Interactively, a prefix arg means to prompt for the initial
1539switch to that buffer. Interactively, a prefix arg allows you to edit 1578Python command line (default is `python-command').
1540the initial command line (default is `python-command'); `-i' etc. args 1579
1541will be added to this as appropriate. A new process is started if: 1580A new process is started if one isn't running attached to
1542one isn't running attached to `python-buffer', or interactively the 1581`python-buffer', or if called from Lisp with non-nil arg NEW.
1543default `python-command', or argument NEW is non-nil. See also the 1582Otherwise, if a process is already running in `python-buffer',
1544documentation for `python-buffer'. 1583switch to that buffer.
1545 1584
1546Runs the hook `inferior-python-mode-hook' \(after the 1585This command runs the hook `inferior-python-mode-hook' after
1547`comint-mode-hook' is run). \(Type \\[describe-mode] in the process 1586running `comint-mode-hook'. Type \\[describe-mode] in the
1548buffer for a list of commands.)" 1587process buffer for a list of commands.
1588
1589By default, Emacs inhibits the loading of Python modules from the
1590current working directory, for security reasons. To disable this
1591behavior, change `python-remove-cwd-from-path' to nil."
1549 (interactive (if current-prefix-arg 1592 (interactive (if current-prefix-arg
1550 (list (read-string "Run Python: " python-command) nil t) 1593 (list (read-string "Run Python: " python-command) nil t)
1551 (list python-command))) 1594 (list python-command)))
1595 (require 'ansi-color) ; for ipython
1552 (unless cmd (setq cmd python-command)) 1596 (unless cmd (setq cmd python-command))
1553 (python-check-version cmd) 1597 (python-check-version cmd)
1554 (setq python-command cmd) 1598 (setq python-command cmd)
@@ -1558,16 +1602,19 @@ buffer for a list of commands.)"
1558 (when (or new (not (comint-check-proc python-buffer))) 1602 (when (or new (not (comint-check-proc python-buffer)))
1559 (with-current-buffer 1603 (with-current-buffer
1560 (let* ((cmdlist 1604 (let* ((cmdlist
1561 (append (python-args-to-list cmd) 1605 (append (python-args-to-list cmd) '("-i")
1562 '("-i" "-c" "import sys; sys.path.remove('')"))) 1606 (if python-remove-cwd-from-path
1607 '("-c" "import sys; sys.path.remove('')"))))
1563 (path (getenv "PYTHONPATH")) 1608 (path (getenv "PYTHONPATH"))
1564 (process-environment ; to import emacs.py 1609 (process-environment ; to import emacs.py
1565 (cons (concat "PYTHONPATH=" 1610 (cons (concat "PYTHONPATH="
1566 (if path (concat path path-separator)) 1611 (if path (concat path path-separator))
1567 data-directory) 1612 data-directory)
1568 process-environment)) 1613 process-environment))
1569 ;; Suppress use of pager for help output: 1614 ;; If we use a pipe, unicode characters are not printed
1570 (process-connection-type nil)) 1615 ;; correctly (Bug#5794) and IPython does not work at
1616 ;; all (Bug#5390).
1617 (process-connection-type t))
1571 (apply 'make-comint-in-buffer "Python" 1618 (apply 'make-comint-in-buffer "Python"
1572 (generate-new-buffer "*Python*") 1619 (generate-new-buffer "*Python*")
1573 (car cmdlist) nil (cdr cmdlist))) 1620 (car cmdlist) nil (cdr cmdlist)))
@@ -1623,7 +1670,12 @@ buffer for a list of commands.)"
1623 ;; non-ASCII. 1670 ;; non-ASCII.
1624 (interactive "r") 1671 (interactive "r")
1625 (let* ((f (make-temp-file "py")) 1672 (let* ((f (make-temp-file "py"))
1626 (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)))
1627 (orig-start (copy-marker start))) 1679 (orig-start (copy-marker start)))
1628 (when (save-excursion 1680 (when (save-excursion
1629 (goto-char start) 1681 (goto-char start)
@@ -1823,7 +1875,9 @@ If there isn't, it's probably not appropriate to send input to return Eldoc
1823information 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."
1824 (with-current-buffer (process-buffer (or proc (python-proc))) 1876 (with-current-buffer (process-buffer (or proc (python-proc)))
1825 (save-excursion 1877 (save-excursion
1826 (save-match-data (re-search-backward ">>> \\=" nil t))))) 1878 (save-match-data
1879 (re-search-backward (concat python--prompt-regexp " *\\=")
1880 nil t)))))
1827 1881
1828;; Fixme: Is there anything reasonable we can do with random methods? 1882;; Fixme: Is there anything reasonable we can do with random methods?
1829;; (Currently only works with functions.) 1883;; (Currently only works with functions.)
@@ -2539,9 +2593,7 @@ Runs `jython-mode-hook' after `python-mode-hook'."
2539 "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.
2540This function is appropriate for `comint-output-filter-functions'." 2594This function is appropriate for `comint-output-filter-functions'."
2541 ;; TBD: this should probably use split-string 2595 ;; TBD: this should probably use split-string
2542 (when (and (or (string-equal string ">>> ") 2596 (when (and (string-match python--prompt-regexp string)
2543 (and (>= (length string) 5)
2544 (string-equal (substring string -5) "\n>>> ")))
2545 python-file-queue) 2597 python-file-queue)
2546 (condition-case nil 2598 (condition-case nil
2547 (delete-file (car python-file-queue)) 2599 (delete-file (car python-file-queue))
@@ -2753,6 +2805,7 @@ comint believe the user typed this string so that
2753 (funcall (process-filter proc) proc msg)) 2805 (funcall (process-filter proc) proc msg))
2754 (set-buffer curbuf)) 2806 (set-buffer curbuf))
2755 (process-send-string proc cmd))) 2807 (process-send-string proc cmd)))
2808
2756;;;###autoload 2809;;;###autoload
2757(defun python-shell (&optional argprompt) 2810(defun python-shell (&optional argprompt)
2758 "Start an interactive Python interpreter in another window. 2811 "Start an interactive Python interpreter in another window.
@@ -2767,7 +2820,7 @@ command is used to switch to an existing process, only when a new
2767process is started. If you use this, you will probably want to ensure 2820process is started. If you use this, you will probably want to ensure
2768that the current arguments are retained (they will be included in the 2821that the current arguments are retained (they will be included in the
2769prompt). This argument is ignored when this function is called 2822prompt). This argument is ignored when this function is called
2770programmatically, or when running in Emacs 19.34 or older. 2823programmatically.
2771 2824
2772Note: You can toggle between using the CPython interpreter and the 2825Note: You can toggle between using the CPython interpreter and the
2773JPython interpreter by hitting \\[python-toggle-shells]. This toggles 2826JPython interpreter by hitting \\[python-toggle-shells]. This toggles
@@ -2792,6 +2845,7 @@ interaction between undo and process filters; the same problem exists in
2792non-Python process buffers using the default (Emacs-supplied) process 2845non-Python process buffers using the default (Emacs-supplied) process
2793filter." 2846filter."
2794 (interactive "P") 2847 (interactive "P")
2848 (require 'ansi-color) ; For ipython
2795 ;; Set the default shell if not already set 2849 ;; Set the default shell if not already set
2796 (when (null python-which-shell) 2850 (when (null python-which-shell)
2797 (python-toggle-shells python-default-interpreter)) 2851 (python-toggle-shells python-default-interpreter))
@@ -2808,10 +2862,9 @@ filter."
2808 )))) 2862 ))))
2809 (switch-to-buffer-other-window 2863 (switch-to-buffer-other-window
2810 (apply 'make-comint python-which-bufname python-which-shell nil args)) 2864 (apply 'make-comint python-which-bufname python-which-shell nil args))
2811 (make-local-variable 'comint-prompt-regexp)
2812 (set-process-sentinel (get-buffer-process (current-buffer)) 2865 (set-process-sentinel (get-buffer-process (current-buffer))
2813 'python-sentinel) 2866 'python-sentinel)
2814 (setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) ") 2867 (python--set-prompt-regexp)
2815 (add-hook 'comint-output-filter-functions 2868 (add-hook 'comint-output-filter-functions
2816 'python-comint-output-filter-function nil t) 2869 'python-comint-output-filter-function nil t)
2817 ;; pdbtrack 2870 ;; pdbtrack
@@ -2844,5 +2897,4 @@ filter."
2844(provide 'python) 2897(provide 'python)
2845(provide 'python-21) 2898(provide 'python-21)
2846 2899
2847;; arch-tag: 6fce1d99-a704-4de9-ba19-c6e4912b0554
2848;;; python.el ends here 2900;;; python.el ends here