comparison lisp/progmodes/python.el @ 91041:bdb3fe0ba9fa

Merge from emacs--devo--0 Patches applied: * emacs--devo--0 (patch 866-879) - Merge multi-tty branch - Update from CVS - Merge from emacs--rel--22 Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-257
author Miles Bader <miles@gnu.org>
date Thu, 11 Oct 2007 16:22:07 +0000
parents 424b655804ca 5039706521c9
children 4bc33ffdda1a
comparison
equal deleted inserted replaced
91040:14c4a6aac623 91041:bdb3fe0ba9fa
1 ;;; python.el --- silly walks for Python 1 ;;; python.el --- silly walks for Python -*- coding: iso-8859-1 -*-
2 2
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. 3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 4
5 ;; Author: Dave Love <fx@gnu.org> 5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: FSF 6 ;; Maintainer: FSF
87 87
88 ;;;; Font lock 88 ;;;; Font lock
89 89
90 (defvar python-font-lock-keywords 90 (defvar python-font-lock-keywords
91 `(,(rx symbol-start 91 `(,(rx symbol-start
92 ;; From v 2.4 reference. 92 ;; From v 2.5 reference, § keywords.
93 ;; def and class dealt with separately below 93 ;; def and class dealt with separately below
94 (or "and" "assert" "break" "continue" "del" "elif" "else" 94 (or "and" "as" "assert" "break" "continue" "del" "elif" "else"
95 "except" "exec" "finally" "for" "from" "global" "if" 95 "except" "exec" "finally" "for" "from" "global" "if"
96 "import" "in" "is" "lambda" "not" "or" "pass" "print" 96 "import" "in" "is" "lambda" "not" "or" "pass" "print"
97 "raise" "return" "try" "while" "yield" 97 "raise" "return" "try" "while" "with" "yield"
98 ;; Future keywords
99 "as" "None" "with"
100 ;; Not real keywords, but close enough to be fontified as such 98 ;; Not real keywords, but close enough to be fontified as such
101 "self" "True" "False") 99 "self" "True" "False")
102 symbol-end) 100 symbol-end)
101 (,(rx symbol-start "None" symbol-end) ; See § Keywords in 2.5 manual.
102 . font-lock-constant-face)
103 ;; Definitions 103 ;; Definitions
104 (,(rx symbol-start (group "class") (1+ space) (group (1+ (or word ?_)))) 104 (,(rx symbol-start (group "class") (1+ space) (group (1+ (or word ?_))))
105 (1 font-lock-keyword-face) (2 font-lock-type-face)) 105 (1 font-lock-keyword-face) (2 font-lock-type-face))
106 (,(rx symbol-start (group "def") (1+ space) (group (1+ (or word ?_)))) 106 (,(rx symbol-start (group "def") (1+ space) (group (1+ (or word ?_))))
107 (1 font-lock-keyword-face) (2 font-lock-function-name-face)) 107 (1 font-lock-keyword-face) (2 font-lock-function-name-face))
149 (save-excursion 149 (save-excursion
150 (goto-char (match-beginning 0)) 150 (goto-char (match-beginning 0))
151 (cond 151 (cond
152 ;; Consider property for the last char if in a fenced string. 152 ;; Consider property for the last char if in a fenced string.
153 ((= n 3) 153 ((= n 3)
154 (let ((syntax (syntax-ppss))) 154 (let* ((font-lock-syntactic-keywords nil)
155 (syntax (syntax-ppss)))
155 (when (eq t (nth 3 syntax)) ; after unclosed fence 156 (when (eq t (nth 3 syntax)) ; after unclosed fence
156 (goto-char (nth 8 syntax)) ; fence position 157 (goto-char (nth 8 syntax)) ; fence position
157 (skip-chars-forward "uUrR") ; skip any prefix 158 (skip-chars-forward "uUrR") ; skip any prefix
158 ;; Is it a matching sequence? 159 ;; Is it a matching sequence?
159 (if (eq (char-after) (char-after (match-beginning 2))) 160 (if (eq (char-after) (char-after (match-beginning 2)))
161 ;; Consider property for initial char, accounting for prefixes. 162 ;; Consider property for initial char, accounting for prefixes.
162 ((or (and (= n 2) ; leading quote (not prefix) 163 ((or (and (= n 2) ; leading quote (not prefix)
163 (= (match-beginning 1) (match-end 1))) ; prefix is null 164 (= (match-beginning 1) (match-end 1))) ; prefix is null
164 (and (= n 1) ; prefix 165 (and (= n 1) ; prefix
165 (/= (match-beginning 1) (match-end 1)))) ; non-empty 166 (/= (match-beginning 1) (match-end 1)))) ; non-empty
166 (unless (nth 3 (syntax-ppss)) 167 (let ((font-lock-syntactic-keywords nil))
167 (eval-when-compile (string-to-syntax "|")))) 168 (unless (nth 3 (syntax-ppss))
169 (eval-when-compile (string-to-syntax "|")))))
168 ;; Otherwise (we're in a non-matching string) the property is 170 ;; Otherwise (we're in a non-matching string) the property is
169 ;; nil, which is OK. 171 ;; nil, which is OK.
170 ))) 172 )))
171 173
172 ;; This isn't currently in `font-lock-defaults' as probably not worth 174 ;; This isn't currently in `font-lock-defaults' as probably not worth
346 (condition-case () 348 (condition-case ()
347 (progn (backward-up-list) t) ; actually within brackets 349 (progn (backward-up-list) t) ; actually within brackets
348 (error nil)))))))) 350 (error nil))))))))
349 351
350 (defun python-comment-line-p () 352 (defun python-comment-line-p ()
351 "Return non-nil if current line has only a comment." 353 "Return non-nil iff current line has only a comment."
352 (save-excursion 354 (save-excursion
353 (end-of-line) 355 (end-of-line)
354 (when (eq 'comment (syntax-ppss-context (syntax-ppss))) 356 (when (eq 'comment (syntax-ppss-context (syntax-ppss)))
355 (back-to-indentation) 357 (back-to-indentation)
356 (looking-at (rx (or (syntax comment-start) line-end)))))) 358 (looking-at (rx (or (syntax comment-start) line-end))))))
357 359
358 (defun python-blank-line-p () 360 (defun python-blank-line-p ()
359 "Return non-nil if current line is blank." 361 "Return non-nil iff current line is blank."
360 (save-excursion 362 (save-excursion
361 (beginning-of-line) 363 (beginning-of-line)
362 (looking-at "\\s-*$"))) 364 (looking-at "\\s-*$")))
363 365
364 (defun python-beginning-of-string () 366 (defun python-beginning-of-string ()
848 850
849 (defun python-skip-out (&optional forward syntax) 851 (defun python-skip-out (&optional forward syntax)
850 "Skip out of any nested brackets. 852 "Skip out of any nested brackets.
851 Skip forward if FORWARD is non-nil, else backward. 853 Skip forward if FORWARD is non-nil, else backward.
852 If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point. 854 If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point.
853 Return non-nil if skipping was done." 855 Return non-nil iff skipping was done."
854 (let ((depth (syntax-ppss-depth (or syntax (syntax-ppss)))) 856 (let ((depth (syntax-ppss-depth (or syntax (syntax-ppss))))
855 (forward (if forward -1 1))) 857 (forward (if forward -1 1)))
856 (unless (zerop depth) 858 (unless (zerop depth)
857 (if (> depth 0) 859 (if (> depth 0)
858 ;; Skip forward out of nested brackets. 860 ;; Skip forward out of nested brackets.
1081 (python-indent-line))) ; OK, do it 1083 (python-indent-line))) ; OK, do it
1082 (put 'python-electric-colon 'delete-selection t) 1084 (put 'python-electric-colon 'delete-selection t)
1083 1085
1084 (defun python-backspace (arg) 1086 (defun python-backspace (arg)
1085 "Maybe delete a level of indentation on the current line. 1087 "Maybe delete a level of indentation on the current line.
1086 Do so if point is at the end of the line's indentation. 1088 Do so if point is at the end of the line's indentation outside
1089 strings and comments.
1087 Otherwise just call `backward-delete-char-untabify'. 1090 Otherwise just call `backward-delete-char-untabify'.
1088 Repeat ARG times." 1091 Repeat ARG times."
1089 (interactive "*p") 1092 (interactive "*p")
1090 (if (or (/= (current-indentation) (current-column)) 1093 (if (or (/= (current-indentation) (current-column))
1091 (bolp) 1094 (bolp)
1092 (python-continuation-line-p)) 1095 (python-continuation-line-p)
1096 (python-in-string/comment))
1093 (backward-delete-char-untabify arg) 1097 (backward-delete-char-untabify arg)
1094 ;; Look for the largest valid indentation which is smaller than 1098 ;; Look for the largest valid indentation which is smaller than
1095 ;; the current indentation. 1099 ;; the current indentation.
1096 (let ((indent 0) 1100 (let ((indent 0)
1097 (ci (current-indentation)) 1101 (ci (current-indentation))
1188 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c 1192 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
1189 "\", line " (group (1+ digit))) 1193 "\", line " (group (1+ digit)))
1190 1 2) 1194 1 2)
1191 (,(rx " in file " (group (1+ not-newline)) " on line " 1195 (,(rx " in file " (group (1+ not-newline)) " on line "
1192 (group (1+ digit))) 1196 (group (1+ digit)))
1197 1 2)
1198 ;; pdb stack trace
1199 (,(rx line-start "> " (group (1+ (not (any "(\"<"))))
1200 "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()")
1193 1 2)) 1201 1 2))
1194 "`compilation-error-regexp-alist' for inferior Python.") 1202 "`compilation-error-regexp-alist' for inferior Python.")
1195 1203
1196 (defvar inferior-python-mode-map 1204 (defvar inferior-python-mode-map
1197 (let ((map (make-sparse-keymap))) 1205 (let ((map (make-sparse-keymap)))
1198 ;; This will inherit from comint-mode-map. 1206 ;; This will inherit from comint-mode-map.
1199 (define-key map "\C-c\C-l" 'python-load-file) 1207 (define-key map "\C-c\C-l" 'python-load-file)
1200 (define-key map "\C-c\C-v" 'python-check) 1208 (define-key map "\C-c\C-v" 'python-check)
1201 ;; Note that we _can_ still use these commands which send to the 1209 ;; Note that we _can_ still use these commands which send to the
1202 ;; Python process even at the prompt provided we have a normal prompt, 1210 ;; Python process even at the prompt iff we have a normal prompt,
1203 ;; i.e. '>>> ' and not '... '. See the comment before 1211 ;; i.e. '>>> ' and not '... '. See the comment before
1204 ;; python-send-region. Fixme: uncomment these if we address that. 1212 ;; python-send-region. Fixme: uncomment these if we address that.
1205 1213
1206 ;; (define-key map [(meta ?\t)] 'python-complete-symbol) 1214 ;; (define-key map [(meta ?\t)] 'python-complete-symbol)
1207 ;; (define-key map "\C-c\C-f" 'python-describe-symbol) 1215 ;; (define-key map "\C-c\C-f" 'python-describe-symbol)
1243 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter 1251 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter
1244 nil t) 1252 nil t)
1245 ;; Still required by `comint-redirect-send-command', for instance 1253 ;; Still required by `comint-redirect-send-command', for instance
1246 ;; (and we need to match things like `>>> ... >>> '): 1254 ;; (and we need to match things like `>>> ... >>> '):
1247 (set (make-local-variable 'comint-prompt-regexp) 1255 (set (make-local-variable 'comint-prompt-regexp)
1248 (rx line-start (1+ (and (repeat 3 (any ">.")) " ")))) 1256 (rx line-start (1+ (and (or (repeat 3 (any ">.")) "(Pdb)") " "))))
1249 (set (make-local-variable 'compilation-error-regexp-alist) 1257 (set (make-local-variable 'compilation-error-regexp-alist)
1250 python-compilation-regexp-alist) 1258 python-compilation-regexp-alist)
1251 (compilation-shell-minor-mode 1)) 1259 (compilation-shell-minor-mode 1))
1252 1260
1253 (defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'" 1261 (defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'"
1373 ;; cmdlist, but that loses the banner and doesn't run the startup 1381 ;; cmdlist, but that loses the banner and doesn't run the startup
1374 ;; file. The code might be inline here, but there's enough that it 1382 ;; file. The code might be inline here, but there's enough that it
1375 ;; seems worth putting in a separate file, and it's probably cleaner 1383 ;; seems worth putting in a separate file, and it's probably cleaner
1376 ;; to put it in a module. 1384 ;; to put it in a module.
1377 ;; Ensure we're at a prompt before doing anything else. 1385 ;; Ensure we're at a prompt before doing anything else.
1378 (python-send-receive "import emacs; print '_emacs_out ()'"))) 1386 (python-send-string "import emacs")))
1379 (if (derived-mode-p 'python-mode) 1387 (if (derived-mode-p 'python-mode)
1380 (setq python-buffer (default-value 'python-buffer))) ; buffer-local 1388 (setq python-buffer (default-value 'python-buffer))) ; buffer-local
1381 ;; Without this, help output goes into the inferior python buffer if 1389 ;; Without this, help output goes into the inferior python buffer if
1382 ;; the process isn't already running. 1390 ;; the process isn't already running.
1383 (sit-for 1 t) ;Should we use accept-process-output instead? --Stef 1391 (sit-for 1 t) ;Should we use accept-process-output instead? --Stef
1618 (kill-local-variable 'python-preoutput-result))))) 1626 (kill-local-variable 'python-preoutput-result)))))
1619 1627
1620 ;; Fixme: Is there anything reasonable we can do with random methods? 1628 ;; Fixme: Is there anything reasonable we can do with random methods?
1621 ;; (Currently only works with functions.) 1629 ;; (Currently only works with functions.)
1622 (defun python-eldoc-function () 1630 (defun python-eldoc-function ()
1623 "`eldoc-print-current-symbol-info' for Python. 1631 "`eldoc-documentation-function' for Python.
1624 Only works when point is in a function name, not its arg list, for 1632 Only works when point is in a function name, not its arg list, for
1625 instance. Assumes an inferior Python is running." 1633 instance. Assumes an inferior Python is running."
1626 (let ((symbol (with-syntax-table python-dotty-syntax-table 1634 (let ((symbol (with-syntax-table python-dotty-syntax-table
1627 (current-word)))) 1635 (current-word))))
1628 ;; This is run from timers, so inhibit-quit tends to be set. 1636 ;; This is run from timers, so inhibit-quit tends to be set.
1735 (if (member (match-string 1) python-jython-packages) 1743 (if (member (match-string 1) python-jython-packages)
1736 (throw 'done t)))) 1744 (throw 'done t))))
1737 (jython-mode))))))) 1745 (jython-mode)))))))
1738 1746
1739 (defun python-fill-paragraph (&optional justify) 1747 (defun python-fill-paragraph (&optional justify)
1740 "`fill-paragraph-function' handling comments and multi-line strings. 1748 "`fill-paragraph-function' handling multi-line strings and possibly comments.
1741 If any of the current line is a comment, fill the comment or the 1749 If any of the current line is in or at the end of a multi-line string,
1742 paragraph of it that point is in, preserving the comment's 1750 fill the string or the paragraph of it that point is in, preserving
1743 indentation and initial comment characters. Similarly if the end 1751 the strings's indentation."
1744 of the current line is in or at the end of a multi-line string.
1745 Otherwise, do nothing."
1746 (interactive "P") 1752 (interactive "P")
1747 (or (fill-comment-paragraph justify) 1753 (or (fill-comment-paragraph justify)
1748 ;; The `paragraph-start' and `paragraph-separate' variables
1749 ;; don't allow us to delimit the last paragraph in a multi-line
1750 ;; string properly, so narrow to the string and then fill around
1751 ;; (the end of) the current line.
1752 (save-excursion 1754 (save-excursion
1753 (end-of-line) 1755 (end-of-line)
1754 (let* ((syntax (syntax-ppss)) 1756 (let* ((syntax (syntax-ppss))
1755 (orig (point)) 1757 (orig (point))
1756 (start (nth 8 syntax)) 1758 start end)
1757 end) 1759 (cond ((nth 4 syntax) ; comment. fixme: loses with trailing one
1758 (cond ((eq t (nth 3 syntax)) ; in fenced string 1760 (let (fill-paragraph-function)
1759 (goto-char (nth 8 syntax)) ; string start 1761 (fill-paragraph justify)))
1762 ;; The `paragraph-start' and `paragraph-separate'
1763 ;; variables don't allow us to delimit the last
1764 ;; paragraph in a multi-line string properly, so narrow
1765 ;; to the string and then fill around (the end of) the
1766 ;; current line.
1767 ((eq t (nth 3 syntax)) ; in fenced string
1768 (goto-char (nth 8 syntax)) ; string start
1769 (setq start (line-beginning-position))
1760 (setq end (condition-case () ; for unbalanced quotes 1770 (setq end (condition-case () ; for unbalanced quotes
1761 (progn (forward-sexp) (point)) 1771 (progn (forward-sexp)
1772 (- (point) 3))
1762 (error (point-max))))) 1773 (error (point-max)))))
1763 ((re-search-backward "\\s|\\s-*\\=" nil t) ; end of fenced 1774 ((re-search-backward "\\s|\\s-*\\=" nil t) ; end of fenced string
1764 ; string
1765 (forward-char) 1775 (forward-char)
1766 (setq end (point)) 1776 (setq end (point))
1767 (condition-case () 1777 (condition-case ()
1768 (progn (backward-sexp) 1778 (progn (backward-sexp)
1769 (setq start (point))) 1779 (setq start (line-beginning-position)))
1770 (error (setq end nil))))) 1780 (error nil))))
1771 (when end 1781 (when end
1772 (save-restriction 1782 (save-restriction
1773 (narrow-to-region start end) 1783 (narrow-to-region start end)
1774 (goto-char orig) 1784 (goto-char orig)
1775 (let ((paragraph-separate 1785 ;; Avoid losing leading and trailing newlines in doc
1776 ;; Make sure that fenced-string delimiters that stand 1786 ;; strings written like:
1777 ;; on their own line stay there. 1787 ;; """
1778 (concat "[ \t]*['\"]+[ \t]*$\\|" paragraph-separate))) 1788 ;; ...
1779 (fill-paragraph justify)))))) 1789 ;; """
1780 t)) 1790 (let* ((paragraph-separate
1791 (concat ".*\\s|\"\"$" ; newline after opening quotes
1792 "\\|\\(?:" paragraph-separate "\\)"))
1793 (paragraph-start
1794 (concat ".*\\s|\"\"[ \t]*[^ \t].*" ; not newline after
1795 ; opening quotes
1796 "\\|\\(?:" paragraph-separate "\\)"))
1797 (fill-paragraph-function))
1798 (fill-paragraph justify))))))) t)
1781 1799
1782 (defun python-shift-left (start end &optional count) 1800 (defun python-shift-left (start end &optional count)
1783 "Shift lines in region COUNT (the prefix arg) columns to the left. 1801 "Shift lines in region COUNT (the prefix arg) columns to the left.
1784 COUNT defaults to `python-indent'. If region isn't active, just shift 1802 COUNT defaults to `python-indent'. If region isn't active, just shift
1785 current line. The region shifted includes the lines in which START and 1803 current line. The region shifted includes the lines in which START and
1884 (save-excursion 1902 (save-excursion
1885 (let (lines) 1903 (let (lines)
1886 (goto-char (point-min)) 1904 (goto-char (point-min))
1887 (while (re-search-forward "^import\\>\\|^from\\>" nil t) 1905 (while (re-search-forward "^import\\>\\|^from\\>" nil t)
1888 (unless (syntax-ppss-context (syntax-ppss)) 1906 (unless (syntax-ppss-context (syntax-ppss))
1889 (push (buffer-substring (line-beginning-position) 1907 (let ((start (line-beginning-position)))
1890 (line-beginning-position 2)) 1908 ;; Skip over continued lines.
1891 lines))) 1909 (while (and (eq ?\\ (char-before (line-end-position)))
1910 (= 0 (forward-line 1))))
1911 (push (buffer-substring start (line-beginning-position 2))
1912 lines))))
1892 (setq python-imports 1913 (setq python-imports
1893 (if lines 1914 (if lines
1894 (apply #'concat 1915 (apply #'concat
1895 ;; This is probably best left out since you're unlikely to need the 1916 ;; This is probably best left out since you're unlikely to need the
1896 ;; doc for a function in the buffer and the import will lose if the 1917 ;; doc for a function in the buffer and the import will lose if the
2278 nil t) 2299 nil t)
2279 ;; Fixme: should be in hideshow. This seems to be of limited use 2300 ;; Fixme: should be in hideshow. This seems to be of limited use
2280 ;; since it isn't (can't be) indentation-based. Also hide-level 2301 ;; since it isn't (can't be) indentation-based. Also hide-level
2281 ;; doesn't seem to work properly. 2302 ;; doesn't seem to work properly.
2282 (add-to-list 'hs-special-modes-alist 2303 (add-to-list 'hs-special-modes-alist
2283 `(python-mode "^\\s-*def\\>" nil "#" 2304 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
2284 ,(lambda (arg) 2305 ,(lambda (arg)
2285 (python-end-of-defun) 2306 (python-end-of-defun)
2286 (skip-chars-backward " \t\n")) 2307 (skip-chars-backward " \t\n"))
2287 nil)) 2308 nil))
2288 (set (make-local-variable 'skeleton-further-elements) 2309 (set (make-local-variable 'skeleton-further-elements)