aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorMiles Bader2007-10-09 08:52:57 +0000
committerMiles Bader2007-10-09 08:52:57 +0000
commit1af74d06e5bdafad9d629d2ed729c5d743cfaf0f (patch)
tree0360965ec0ad2736ffee01cce4f040c6b0a99133 /lisp/progmodes/python.el
parenteceb3266a1f66a0034954aa82efbb20a5be959f8 (diff)
parent4b70e299ef66906fd285198003c72a1439d1f252 (diff)
downloademacs-1af74d06e5bdafad9d629d2ed729c5d743cfaf0f.tar.gz
emacs-1af74d06e5bdafad9d629d2ed729c5d743cfaf0f.zip
Merge from emacs--rel--22
Revision: emacs@sv.gnu.org/emacs--devo--0--patch-875
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el111
1 files changed, 66 insertions, 45 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 46d78c8793f..f1d6d02020b 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1,4 +1,4 @@
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
@@ -89,17 +89,17 @@
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))
@@ -151,7 +151,8 @@ Used for syntactic keywords. N is the match number (1, 2 or 3)."
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
@@ -163,8 +164,9 @@ Used for syntactic keywords. N is the match number (1, 2 or 3)."
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 )))
@@ -348,7 +350,7 @@ comments and strings, or that point is within brackets/parens."
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)))
@@ -356,7 +358,7 @@ comments and strings, or that point is within brackets/parens."
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-*$")))
@@ -850,7 +852,7 @@ multi-line bracketed expressions."
850 "Skip out of any nested brackets. 852 "Skip out of any nested brackets.
851Skip forward if FORWARD is non-nil, else backward. 853Skip forward if FORWARD is non-nil, else backward.
852If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point. 854If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point.
853Return non-nil if skipping was done." 855Return 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)
@@ -1083,13 +1085,15 @@ just insert a single colon."
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.
1086Do so if point is at the end of the line's indentation. 1088Do so if point is at the end of the line's indentation outside
1089strings and comments.
1087Otherwise just call `backward-delete-char-untabify'. 1090Otherwise just call `backward-delete-char-untabify'.
1088Repeat ARG times." 1091Repeat 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.
@@ -1190,6 +1194,10 @@ local value.")
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
@@ -1199,7 +1207,7 @@ local value.")
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
@@ -1245,7 +1253,7 @@ For running multiple processes in multiple buffers, see `run-python' and
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))
@@ -1737,47 +1745,57 @@ The criterion is either a match for `jython-mode' via
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.
1741If any of the current line is a comment, fill the comment or the 1749If any of the current line is in or at the end of a multi-line string,
1742paragraph of it that point is in, preserving the comment's 1750fill the string or the paragraph of it that point is in, preserving
1743indentation and initial comment characters. Similarly if the end 1751the strings's indentation."
1744of the current line is in or at the end of a multi-line string.
1745Otherwise, 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.
@@ -1886,9 +1904,12 @@ Uses `python-beginning-of-block', `python-end-of-block'."
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
@@ -2280,7 +2301,7 @@ with skeleton expansions for compound statement templates.
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"))