diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 113 |
1 files changed, 83 insertions, 30 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 4e0f326e2d4..849951a633a 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -93,7 +93,7 @@ | |||
| 93 | 93 | ||
| 94 | (defvar python-font-lock-keywords | 94 | (defvar python-font-lock-keywords |
| 95 | `(,(rx symbol-start | 95 | `(,(rx symbol-start |
| 96 | ;; From v 2.5 reference, § keywords. | 96 | ;; From v 2.7 reference, § keywords. |
| 97 | ;; def and class dealt with separately below | 97 | ;; def and class dealt with separately below |
| 98 | (or "and" "as" "assert" "break" "continue" "del" "elif" "else" | 98 | (or "and" "as" "assert" "break" "continue" "del" "elif" "else" |
| 99 | "except" "exec" "finally" "for" "from" "global" "if" | 99 | "except" "exec" "finally" "for" "from" "global" "if" |
| @@ -102,7 +102,7 @@ | |||
| 102 | ;; Not real keywords, but close enough to be fontified as such | 102 | ;; Not real keywords, but close enough to be fontified as such |
| 103 | "self" "True" "False") | 103 | "self" "True" "False") |
| 104 | symbol-end) | 104 | symbol-end) |
| 105 | (,(rx symbol-start "None" symbol-end) ; see § Keywords in 2.5 manual | 105 | (,(rx symbol-start "None" symbol-end) ; see § Keywords in 2.7 manual |
| 106 | . font-lock-constant-face) | 106 | . font-lock-constant-face) |
| 107 | ;; Definitions | 107 | ;; Definitions |
| 108 | (,(rx symbol-start (group "class") (1+ space) (group (1+ (or word ?_)))) | 108 | (,(rx symbol-start (group "class") (1+ space) (group (1+ (or word ?_)))) |
| @@ -117,7 +117,7 @@ | |||
| 117 | (0+ "." (1+ (or word ?_))))) | 117 | (0+ "." (1+ (or word ?_))))) |
| 118 | (1 font-lock-type-face)) | 118 | (1 font-lock-type-face)) |
| 119 | ;; Built-ins. (The next three blocks are from | 119 | ;; Built-ins. (The next three blocks are from |
| 120 | ;; `__builtin__.__dict__.keys()' in Python 2.5.1.) These patterns | 120 | ;; `__builtin__.__dict__.keys()' in Python 2.7) These patterns |
| 121 | ;; are debateable, but they at least help to spot possible | 121 | ;; are debateable, but they at least help to spot possible |
| 122 | ;; shadowing of builtins. | 122 | ;; shadowing of builtins. |
| 123 | (,(rx symbol-start (or | 123 | (,(rx symbol-start (or |
| @@ -135,7 +135,9 @@ | |||
| 135 | "SystemExit" "TabError" "TypeError" "UnboundLocalError" | 135 | "SystemExit" "TabError" "TypeError" "UnboundLocalError" |
| 136 | "UnicodeDecodeError" "UnicodeEncodeError" "UnicodeError" | 136 | "UnicodeDecodeError" "UnicodeEncodeError" "UnicodeError" |
| 137 | "UnicodeTranslateError" "UnicodeWarning" "UserWarning" | 137 | "UnicodeTranslateError" "UnicodeWarning" "UserWarning" |
| 138 | "ValueError" "Warning" "ZeroDivisionError") symbol-end) | 138 | "ValueError" "Warning" "ZeroDivisionError" |
| 139 | ;; Python 2.7 | ||
| 140 | "BufferError" "BytesWarning" "WindowsError") symbol-end) | ||
| 139 | . font-lock-type-face) | 141 | . font-lock-type-face) |
| 140 | (,(rx (or line-start (not (any ". \t"))) (* (any " \t")) symbol-start | 142 | (,(rx (or line-start (not (any ". \t"))) (* (any " \t")) symbol-start |
| 141 | (group (or | 143 | (group (or |
| @@ -152,12 +154,16 @@ | |||
| 152 | "range" "raw_input" "reduce" "reload" "repr" "reversed" | 154 | "range" "raw_input" "reduce" "reload" "repr" "reversed" |
| 153 | "round" "set" "setattr" "slice" "sorted" "staticmethod" | 155 | "round" "set" "setattr" "slice" "sorted" "staticmethod" |
| 154 | "str" "sum" "super" "tuple" "type" "unichr" "unicode" "vars" | 156 | "str" "sum" "super" "tuple" "type" "unichr" "unicode" "vars" |
| 155 | "xrange" "zip")) symbol-end) | 157 | "xrange" "zip" |
| 158 | ;; Python 2.7. | ||
| 159 | "bin" "bytearray" "bytes" "format" "memoryview" "next" "print" | ||
| 160 | )) symbol-end) | ||
| 156 | (1 font-lock-builtin-face)) | 161 | (1 font-lock-builtin-face)) |
| 157 | (,(rx symbol-start (or | 162 | (,(rx symbol-start (or |
| 158 | ;; other built-ins | 163 | ;; other built-ins |
| 159 | "True" "False" "None" "Ellipsis" | 164 | "True" "False" "None" "Ellipsis" |
| 160 | "_" "__debug__" "__doc__" "__import__" "__name__") symbol-end) | 165 | "_" "__debug__" "__doc__" "__import__" "__name__" "__package__") |
| 166 | symbol-end) | ||
| 161 | . font-lock-builtin-face))) | 167 | . font-lock-builtin-face))) |
| 162 | 168 | ||
| 163 | (defconst python-font-lock-syntactic-keywords | 169 | (defconst python-font-lock-syntactic-keywords |
| @@ -573,6 +579,33 @@ having to restart the program." | |||
| 573 | "Queue of Python temp files awaiting execution. | 579 | "Queue of Python temp files awaiting execution. |
| 574 | Currently-active file is at the head of the list.") | 580 | Currently-active file is at the head of the list.") |
| 575 | 581 | ||
| 582 | (defcustom python-shell-prompt-alist | ||
| 583 | '(("ipython" . "^In \\[[0-9]+\\]: *") | ||
| 584 | (t . "^>>> ")) | ||
| 585 | "Alist of Python input prompts. | ||
| 586 | Each element has the form (PROGRAM . REGEXP), where PROGRAM is | ||
| 587 | the value of `python-python-command' for the python process and | ||
| 588 | REGEXP is a regular expression matching the Python prompt. | ||
| 589 | PROGRAM can also be t, which specifies the default when no other | ||
| 590 | element matches `python-python-command'." | ||
| 591 | :type 'string | ||
| 592 | :group 'python | ||
| 593 | :version "24.1") | ||
| 594 | |||
| 595 | (defcustom python-shell-continuation-prompt-alist | ||
| 596 | '(("ipython" . "^ [.][.][.]+: *") | ||
| 597 | (t . "^[.][.][.] ")) | ||
| 598 | "Alist of Python continued-line prompts. | ||
| 599 | Each element has the form (PROGRAM . REGEXP), where PROGRAM is | ||
| 600 | the value of `python-python-command' for the python process and | ||
| 601 | REGEXP is a regular expression matching the Python prompt for | ||
| 602 | continued lines. | ||
| 603 | PROGRAM can also be t, which specifies the default when no other | ||
| 604 | element matches `python-python-command'." | ||
| 605 | :type 'string | ||
| 606 | :group 'python | ||
| 607 | :version "24.1") | ||
| 608 | |||
| 576 | (defvar python-pdbtrack-is-tracking-p nil) | 609 | (defvar python-pdbtrack-is-tracking-p nil) |
| 577 | 610 | ||
| 578 | (defconst python-pdbtrack-stack-entry-regexp | 611 | (defconst python-pdbtrack-stack-entry-regexp |
| @@ -1305,13 +1338,9 @@ See `python-check-command' for the default." | |||
| 1305 | 1338 | ||
| 1306 | ;;;; Inferior mode stuff (following cmuscheme). | 1339 | ;;;; Inferior mode stuff (following cmuscheme). |
| 1307 | 1340 | ||
| 1308 | ;; Fixme: Make sure we can work with IPython. | ||
| 1309 | |||
| 1310 | (defcustom python-python-command "python" | 1341 | (defcustom python-python-command "python" |
| 1311 | "Shell command to run Python interpreter. | 1342 | "Shell command to run Python interpreter. |
| 1312 | Any arguments can't contain whitespace. | 1343 | Any arguments can't contain whitespace." |
| 1313 | Note that IPython may not work properly; it must at least be used | ||
| 1314 | with the `-cl' flag, i.e. use `ipython -cl'." | ||
| 1315 | :group 'python | 1344 | :group 'python |
| 1316 | :type 'string) | 1345 | :type 'string) |
| 1317 | 1346 | ||
| @@ -1389,6 +1418,23 @@ local value.") | |||
| 1389 | ;; Autoloaded. | 1418 | ;; Autoloaded. |
| 1390 | (declare-function compilation-shell-minor-mode "compile" (&optional arg)) | 1419 | (declare-function compilation-shell-minor-mode "compile" (&optional arg)) |
| 1391 | 1420 | ||
| 1421 | (defvar python--prompt-regexp nil) | ||
| 1422 | |||
| 1423 | (defun python--set-prompt-regexp () | ||
| 1424 | (let ((prompt (cdr-safe (or (assoc python-python-command | ||
| 1425 | python-shell-prompt-alist) | ||
| 1426 | (assq t python-shell-prompt-alist)))) | ||
| 1427 | (cprompt (cdr-safe (or (assoc python-python-command | ||
| 1428 | python-shell-continuation-prompt-alist) | ||
| 1429 | (assq t python-shell-continuation-prompt-alist))))) | ||
| 1430 | (set (make-local-variable 'comint-prompt-regexp) | ||
| 1431 | (concat "\\(" | ||
| 1432 | (mapconcat 'identity | ||
| 1433 | (delq nil (list prompt cprompt "^([Pp]db) ")) | ||
| 1434 | "\\|") | ||
| 1435 | "\\)")) | ||
| 1436 | (set (make-local-variable 'python--prompt-regexp) prompt))) | ||
| 1437 | |||
| 1392 | ;; Fixme: This should inherit some stuff from `python-mode', but I'm | 1438 | ;; Fixme: This should inherit some stuff from `python-mode', but I'm |
| 1393 | ;; not sure how much: at least some keybindings, like C-c C-f; | 1439 | ;; not sure how much: at least some keybindings, like C-c C-f; |
| 1394 | ;; syntax?; font-locking, e.g. for triple-quoted strings? | 1440 | ;; syntax?; font-locking, e.g. for triple-quoted strings? |
| @@ -1411,14 +1457,12 @@ For running multiple processes in multiple buffers, see `run-python' and | |||
| 1411 | 1457 | ||
| 1412 | \\{inferior-python-mode-map}" | 1458 | \\{inferior-python-mode-map}" |
| 1413 | :group 'python | 1459 | :group 'python |
| 1460 | (require 'ansi-color) ; for ipython | ||
| 1414 | (setq mode-line-process '(":%s")) | 1461 | (setq mode-line-process '(":%s")) |
| 1415 | (set (make-local-variable 'comint-input-filter) 'python-input-filter) | 1462 | (set (make-local-variable 'comint-input-filter) 'python-input-filter) |
| 1416 | (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter | 1463 | (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter |
| 1417 | nil t) | 1464 | nil t) |
| 1418 | ;; Still required by `comint-redirect-send-command', for instance | 1465 | (python--set-prompt-regexp) |
| 1419 | ;; (and we need to match things like `>>> ... >>> '): | ||
| 1420 | (set (make-local-variable 'comint-prompt-regexp) | ||
| 1421 | (rx line-start (1+ (and (or (repeat 3 (any ">.")) "(Pdb)") " ")))) | ||
| 1422 | (set (make-local-variable 'compilation-error-regexp-alist) | 1466 | (set (make-local-variable 'compilation-error-regexp-alist) |
| 1423 | python-compilation-regexp-alist) | 1467 | python-compilation-regexp-alist) |
| 1424 | (compilation-shell-minor-mode 1)) | 1468 | (compilation-shell-minor-mode 1)) |
| @@ -1515,12 +1559,12 @@ Don't save anything for STR matching `inferior-python-filter-regexp'." | |||
| 1515 | cmd))) | 1559 | cmd))) |
| 1516 | (unless (shell-command-to-string cmd) | 1560 | (unless (shell-command-to-string cmd) |
| 1517 | (error "Can't run Python command `%s'" cmd)) | 1561 | (error "Can't run Python command `%s'" cmd)) |
| 1518 | (let* ((res (shell-command-to-string (concat cmd " --version")))) | 1562 | (let* ((res (shell-command-to-string |
| 1519 | (string-match "Python \\([0-9]\\)\\.\\([0-9]\\)" res) | 1563 | (concat cmd |
| 1520 | (unless (and (equal "2" (match-string 1 res)) | 1564 | " -c \"from sys import version_info;\ |
| 1521 | (match-beginning 2) | 1565 | print version_info >= (2, 2) and version_info < (3, 0)\"")))) |
| 1522 | (>= (string-to-number (match-string 2 res)) 2)) | 1566 | (unless (string-match "True" res) |
| 1523 | (error "Only Python versions >= 2.2 and < 3.0 supported"))) | 1567 | (error "Only Python versions >= 2.2 and < 3.0 are supported"))) |
| 1524 | (setq python-version-checked t))) | 1568 | (setq python-version-checked t))) |
| 1525 | 1569 | ||
| 1526 | ;;;###autoload | 1570 | ;;;###autoload |
| @@ -1543,6 +1587,7 @@ buffer for a list of commands.)" | |||
| 1543 | (interactive (if current-prefix-arg | 1587 | (interactive (if current-prefix-arg |
| 1544 | (list (read-string "Run Python: " python-command) nil t) | 1588 | (list (read-string "Run Python: " python-command) nil t) |
| 1545 | (list python-command))) | 1589 | (list python-command))) |
| 1590 | (require 'ansi-color) ; for ipython | ||
| 1546 | (unless cmd (setq cmd python-command)) | 1591 | (unless cmd (setq cmd python-command)) |
| 1547 | (python-check-version cmd) | 1592 | (python-check-version cmd) |
| 1548 | (setq python-command cmd) | 1593 | (setq python-command cmd) |
| @@ -1560,8 +1605,10 @@ buffer for a list of commands.)" | |||
| 1560 | (if path (concat path path-separator)) | 1605 | (if path (concat path path-separator)) |
| 1561 | data-directory) | 1606 | data-directory) |
| 1562 | process-environment)) | 1607 | process-environment)) |
| 1563 | ;; Suppress use of pager for help output: | 1608 | ;; If we use a pipe, unicode characters are not printed |
| 1564 | (process-connection-type nil)) | 1609 | ;; correctly (Bug#5794) and IPython does not work at |
| 1610 | ;; all (Bug#5390). | ||
| 1611 | (process-connection-type t)) | ||
| 1565 | (apply 'make-comint-in-buffer "Python" | 1612 | (apply 'make-comint-in-buffer "Python" |
| 1566 | (generate-new-buffer "*Python*") | 1613 | (generate-new-buffer "*Python*") |
| 1567 | (car cmdlist) nil (cdr cmdlist))) | 1614 | (car cmdlist) nil (cdr cmdlist))) |
| @@ -1617,7 +1664,12 @@ buffer for a list of commands.)" | |||
| 1617 | ;; non-ASCII. | 1664 | ;; non-ASCII. |
| 1618 | (interactive "r") | 1665 | (interactive "r") |
| 1619 | (let* ((f (make-temp-file "py")) | 1666 | (let* ((f (make-temp-file "py")) |
| 1620 | (command (format "emacs.eexecfile(%S)" f)) | 1667 | (command |
| 1668 | ;; IPython puts the FakeModule module into __main__ so | ||
| 1669 | ;; emacs.eexecfile becomes useless. | ||
| 1670 | (if (string-match "^ipython" python-command) | ||
| 1671 | (format "execfile %S" f) | ||
| 1672 | (format "emacs.eexecfile(%S)" f))) | ||
| 1621 | (orig-start (copy-marker start))) | 1673 | (orig-start (copy-marker start))) |
| 1622 | (when (save-excursion | 1674 | (when (save-excursion |
| 1623 | (goto-char start) | 1675 | (goto-char start) |
| @@ -1817,7 +1869,9 @@ If there isn't, it's probably not appropriate to send input to return Eldoc | |||
| 1817 | information etc. If PROC is non-nil, check the buffer for that process." | 1869 | information etc. If PROC is non-nil, check the buffer for that process." |
| 1818 | (with-current-buffer (process-buffer (or proc (python-proc))) | 1870 | (with-current-buffer (process-buffer (or proc (python-proc))) |
| 1819 | (save-excursion | 1871 | (save-excursion |
| 1820 | (save-match-data (re-search-backward ">>> \\=" nil t))))) | 1872 | (save-match-data |
| 1873 | (re-search-backward (concat python--prompt-regexp " *\\=") | ||
| 1874 | nil t))))) | ||
| 1821 | 1875 | ||
| 1822 | ;; Fixme: Is there anything reasonable we can do with random methods? | 1876 | ;; Fixme: Is there anything reasonable we can do with random methods? |
| 1823 | ;; (Currently only works with functions.) | 1877 | ;; (Currently only works with functions.) |
| @@ -2533,9 +2587,7 @@ Runs `jython-mode-hook' after `python-mode-hook'." | |||
| 2533 | "Watch output for Python prompt and exec next file waiting in queue. | 2587 | "Watch output for Python prompt and exec next file waiting in queue. |
| 2534 | This function is appropriate for `comint-output-filter-functions'." | 2588 | This function is appropriate for `comint-output-filter-functions'." |
| 2535 | ;; TBD: this should probably use split-string | 2589 | ;; TBD: this should probably use split-string |
| 2536 | (when (and (or (string-equal string ">>> ") | 2590 | (when (and (string-match python--prompt-regexp string) |
| 2537 | (and (>= (length string) 5) | ||
| 2538 | (string-equal (substring string -5) "\n>>> "))) | ||
| 2539 | python-file-queue) | 2591 | python-file-queue) |
| 2540 | (condition-case nil | 2592 | (condition-case nil |
| 2541 | (delete-file (car python-file-queue)) | 2593 | (delete-file (car python-file-queue)) |
| @@ -2747,6 +2799,7 @@ comint believe the user typed this string so that | |||
| 2747 | (funcall (process-filter proc) proc msg)) | 2799 | (funcall (process-filter proc) proc msg)) |
| 2748 | (set-buffer curbuf)) | 2800 | (set-buffer curbuf)) |
| 2749 | (process-send-string proc cmd))) | 2801 | (process-send-string proc cmd))) |
| 2802 | |||
| 2750 | ;;;###autoload | 2803 | ;;;###autoload |
| 2751 | (defun python-shell (&optional argprompt) | 2804 | (defun python-shell (&optional argprompt) |
| 2752 | "Start an interactive Python interpreter in another window. | 2805 | "Start an interactive Python interpreter in another window. |
| @@ -2786,6 +2839,7 @@ interaction between undo and process filters; the same problem exists in | |||
| 2786 | non-Python process buffers using the default (Emacs-supplied) process | 2839 | non-Python process buffers using the default (Emacs-supplied) process |
| 2787 | filter." | 2840 | filter." |
| 2788 | (interactive "P") | 2841 | (interactive "P") |
| 2842 | (require 'ansi-color) ; For ipython | ||
| 2789 | ;; Set the default shell if not already set | 2843 | ;; Set the default shell if not already set |
| 2790 | (when (null python-which-shell) | 2844 | (when (null python-which-shell) |
| 2791 | (python-toggle-shells python-default-interpreter)) | 2845 | (python-toggle-shells python-default-interpreter)) |
| @@ -2802,10 +2856,9 @@ filter." | |||
| 2802 | )))) | 2856 | )))) |
| 2803 | (switch-to-buffer-other-window | 2857 | (switch-to-buffer-other-window |
| 2804 | (apply 'make-comint python-which-bufname python-which-shell nil args)) | 2858 | (apply 'make-comint python-which-bufname python-which-shell nil args)) |
| 2805 | (make-local-variable 'comint-prompt-regexp) | ||
| 2806 | (set-process-sentinel (get-buffer-process (current-buffer)) | 2859 | (set-process-sentinel (get-buffer-process (current-buffer)) |
| 2807 | 'python-sentinel) | 2860 | 'python-sentinel) |
| 2808 | (setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) ") | 2861 | (python--set-prompt-regexp) |
| 2809 | (add-hook 'comint-output-filter-functions | 2862 | (add-hook 'comint-output-filter-functions |
| 2810 | 'python-comint-output-filter-function nil t) | 2863 | 'python-comint-output-filter-function nil t) |
| 2811 | ;; pdbtrack | 2864 | ;; pdbtrack |