Mercurial > emacs
comparison lisp/progmodes/python.el @ 72459:2657308de2f6
Update to Dave Love's latest version.
(python-font-lock-keywords, python-mode): Don't use
font-lock-syntax-table, but match symbol elements explicitly instead.
(python-mode-map): Add help, and a few more key bindings.
(python-skip-comments/blanks): Move out of comments as well.
(python-continuation-line-p): Behave better with unbalanced parens.
(python-blank-line-p): New fun.
(python-open-block-statement-p): Don't use a heuristic.
(python-outdent-p): Better handle blocks-in-the-same-line.
(python-calculate-indentation): Misc improvements.
(python-comment-indent): Remove.
(python-block-pairs): New var.
(python-first-word): New fun.
(python-indentation-levels): Handle more common cases.
(python-indent-line-1): Add `leave' argument.
(python-indent-region): New fun.
(python-skip-out): New fun.
(python-beginning-of-statement, python-end-of-statement): Use it.
(python-next-statement): Return correct count even at eob.
(python-end-of-block): Fix paren-typo.
(python-imenu-create-index): Add module variables.
(run-python): Add `new' arg. Check we're at a prompt before returning.
(python-send-command): Move to end of buffer. Wait for prompt to return.
(python-set-proc): New fun.
(python-imports): New var.
(python-describe-symbol): Use it. Adjust to new interface of `ehelp'.
(python-eldoc-function): Try to move out of arg list.
(python-outline-level): Offset by 1.
(python-find-imports): New fun.
(python-symbol-completions): Use python-imports.
(python-module-path, ffap-alist): Add support for ffap.
(python-skeletons, python-mode-abbrev-table, def-python-skeleton)
(pythin-insert-*, python-default-template, python-expand-template):
Add templates/skeletons.
(python-setup-brm): Support for Bicycle Repair Man.
(python-abbrev-syntax-table): New var.
(python-abbrev-pc-hook, python-pea-hook): New funs.
| author | Stefan Monnier <monnier@iro.umontreal.ca> |
|---|---|
| date | Sun, 20 Aug 2006 17:55:16 +0000 |
| parents | 7a59f2600206 |
| children | 2fee33a062da |
comparison
equal
deleted
inserted
replaced
| 72458:18443633d75d | 72459:2657308de2f6 |
|---|---|
| 7 ;; Created: Nov 2003 | 7 ;; Created: Nov 2003 |
| 8 ;; Keywords: languages | 8 ;; Keywords: languages |
| 9 | 9 |
| 10 ;; This file is part of GNU Emacs. | 10 ;; This file is part of GNU Emacs. |
| 11 | 11 |
| 12 ;; This file is free software; you can redistribute it and/or modify | 12 ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 13 ;; it under the terms of the GNU General Public License as published by | 13 ;; it under the terms of the GNU General Public License as published by |
| 14 ;; the Free Software Foundation; either version 2, or (at your option) | 14 ;; the Free Software Foundation; either version 2, or (at your option) |
| 15 ;; any later version. | 15 ;; any later version. |
| 16 | 16 |
| 17 ;; This file is distributed in the hope that it will be useful, | 17 ;; GNU Emacs is distributed in the hope that it will be useful, |
| 18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | 18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 ;; GNU General Public License for more details. | 20 ;; GNU General Public License for more details. |
| 21 | 21 |
| 22 ;; You should have received a copy of the GNU General Public License | 22 ;; You should have received a copy of the GNU General Public License |
| 23 ;; along with GNU Emacs; see the file COPYING. If not, write to | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 24 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 25 ;; Boston, MA 02110-1301, USA. | 25 ;; Boston, MA 02110-1301, USA. |
| 26 | 26 |
| 27 ;;; Commentary: | 27 ;;; Commentary: |
| 28 | 28 |
| 29 ;; Major mode for editing Python, with support for inferior processes. | 29 ;; Major mode for editing Python, with support for inferior processes. |
| 30 | 30 |
| 31 ;; There is another Python mode, python-mode.el, used by XEmacs and | 31 ;; There is another Python mode, python-mode.el, used by XEmacs and |
| 32 ;; maintained with Python. That isn't covered by an FSF copyright | 32 ;; maintained with Python. That isn't covered by an FSF copyright |
| 33 ;; assignment, unlike this code, and seems not to be well-maintained | 33 ;; assignment, unlike this code, and seems not to be well-maintained |
| 34 ;; for Emacs (though I've submitted fixes). This mode is rather | 34 ;; for Emacs (though I've submitted fixes). This mode is rather |
| 35 ;; simpler and is, perhaps, better in other ways. In particular, | 35 ;; simpler and is better in other ways. In particular, using the |
| 36 ;; using the syntax functions with text properties maintained by | 36 ;; syntax functions with text properties maintained by font-lock makes |
| 37 ;; font-lock should make it more correct with arbitrary string and | 37 ;; it more correct with arbitrary string and comment contents. |
| 38 ;; comment contents. | |
| 39 | 38 |
| 40 ;; This doesn't implement all the facilities of python-mode.el. Some | 39 ;; This doesn't implement all the facilities of python-mode.el. Some |
| 41 ;; just need doing, e.g. catching exceptions in the inferior Python | 40 ;; just need doing, e.g. catching exceptions in the inferior Python |
| 42 ;; buffer (but see M-x pdb for debugging). [Actually, the use of | 41 ;; buffer (but see M-x pdb for debugging). [Actually, the use of |
| 43 ;; `compilation-minor-mode' now is probably enough for that.] Others | 42 ;; `compilation-shell-minor-mode' now is probably enough for that.] |
| 44 ;; don't seem appropriate. For instance, `forward-into-nomenclature' | 43 ;; Others don't seem appropriate. For instance, |
| 45 ;; should be done separately, since it's not specific to Python, and | 44 ;; `forward-into-nomenclature' should be done separately, since it's |
| 46 ;; I've installed a minor mode to do the job properly in Emacs 22. | 45 ;; not specific to Python, and I've installed a minor mode to do the |
| 46 ;; job properly in Emacs 23. [CC mode 5.31 contains an incompatible | |
| 47 ;; feature, `c-subword-mode' which is intended to have a similar | |
| 48 ;; effect, but actually only affects word-oriented keybindings.] | |
| 49 | |
| 47 ;; Other things seem more natural or canonical here, e.g. the | 50 ;; Other things seem more natural or canonical here, e.g. the |
| 48 ;; {beginning,end}-of-defun implementation dealing with nested | 51 ;; {beginning,end}-of-defun implementation dealing with nested |
| 49 ;; definitions, and the inferior mode following `cmuscheme'. The | 52 ;; definitions, and the inferior mode following `cmuscheme'. (The |
| 50 ;; inferior mode can find the source of errors from | 53 ;; inferior mode can find the source of errors from |
| 51 ;; `python-send-region' & al via `compilation-minor-mode'. Successive | 54 ;; `python-send-region' & al via `compilation-shell-minor-mode'.) |
| 52 ;; TABs cycle between possible indentations for the line. There is | 55 ;; There is (limited) symbol completion using lookup in Python and |
| 53 ;; symbol completion using lookup in Python. | 56 ;; Eldoc support also using the inferior process. Successive TABs |
| 54 | 57 ;; cycle between possible indentations for the line. |
| 55 ;; Even where it has similar facilities, this is incompatible with | 58 |
| 56 ;; python-mode.el in various respects. For instance, various key | 59 ;; Even where it has similar facilities, this mode is incompatible |
| 57 ;; bindings are changed to obey Emacs conventions, and things like | 60 ;; with python-mode.el in some respects. For instance, various key |
| 58 ;; marking blocks and `beginning-of-defun' behave differently. | 61 ;; bindings are changed to obey Emacs conventions. |
| 59 | 62 |
| 60 ;; TODO: See various Fixmes below. | 63 ;; TODO: See various Fixmes below. |
| 61 | 64 |
| 62 ;;; Code: | 65 ;;; Code: |
| 63 | 66 |
| 64 ;; It's messy to autoload the relevant comint functions so that comint | |
| 65 ;; is only required when inferior Python is used. | |
| 66 (require 'comint) | |
| 67 (eval-when-compile | 67 (eval-when-compile |
| 68 (require 'compile) | 68 (require 'compile) |
| 69 (autoload 'info-lookup-maybe-add-help "info-look")) | 69 (require 'comint)) |
| 70 | |
| 71 (autoload 'comint-mode "comint") | |
| 70 | 72 |
| 71 (defgroup python nil | 73 (defgroup python nil |
| 72 "Silly walks in the Python language." | 74 "Silly walks in the Python language." |
| 73 :group 'languages | 75 :group 'languages |
| 74 :version "22.1" | 76 :version "22.1" |
| 82 (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode)) | 84 (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode)) |
| 83 | 85 |
| 84 ;;;; Font lock | 86 ;;;; Font lock |
| 85 | 87 |
| 86 (defvar python-font-lock-keywords | 88 (defvar python-font-lock-keywords |
| 87 `(,(rx (and word-start | 89 `(,(rx symbol-start |
| 88 ;; From v 2.3 reference. | 90 ;; From v 2.4 reference. |
| 89 ;; def and class dealt with separately below | 91 ;; def and class dealt with separately below |
| 90 (or "and" "assert" "break" "continue" "del" "elif" "else" | 92 (or "and" "assert" "break" "continue" "del" "elif" "else" |
| 91 "except" "exec" "finally" "for" "from" "global" "if" | 93 "except" "exec" "finally" "for" "from" "global" "if" |
| 92 "import" "in" "is" "lambda" "not" "or" "pass" "print" | 94 "import" "in" "is" "lambda" "not" "or" "pass" "print" |
| 93 "raise" "return" "try" "while" "yield" | 95 "raise" "return" "try" "while" "yield" |
| 94 ;; Future keywords | 96 ;; Future keywords |
| 95 "as" "None") | 97 "as" "None") |
| 96 word-end)) | 98 symbol-end) |
| 97 (,(rx (and word-start (group "class") (1+ space) (group (1+ word)))) | 99 ;; Definitions |
| 98 (1 font-lock-keyword-face) (2 font-lock-type-face)) | 100 (,(rx symbol-start (group "class") (1+ space) (group (1+ (or word ?_)))) |
| 99 (,(rx (and word-start (group "def") (1+ space) (group (1+ word)))) | 101 (1 font-lock-keyword-face) (2 font-lock-type-face)) |
| 100 (1 font-lock-keyword-face) (2 font-lock-function-name-face)))) | 102 (,(rx symbol-start (group "def") (1+ space) (group (1+ (or word ?_)))) |
| 103 (1 font-lock-keyword-face) (2 font-lock-function-name-face)) | |
| 104 ;; Top-level assignments are worth highlighting. | |
| 105 (,(rx line-start (group (1+ (or word ?_))) (0+ space) "=") | |
| 106 (1 font-lock-variable-name-face)) | |
| 107 (,(rx "@" (1+ (or word ?_))) ; decorators | |
| 108 (0 font-lock-preprocessor-face)))) | |
| 101 | 109 |
| 102 (defconst python-font-lock-syntactic-keywords | 110 (defconst python-font-lock-syntactic-keywords |
| 103 ;; Make outer chars of matching triple-quote sequences into generic | 111 ;; Make outer chars of matching triple-quote sequences into generic |
| 104 ;; string delimiters. Fixme: Is there a better way? | 112 ;; string delimiters. Fixme: Is there a better way? |
| 105 `((,(rx (and (or line-start buffer-start (not (syntax escape))) ; avoid escaped | 113 `((,(rx (or line-start buffer-start |
| 106 ; leading quote | 114 (not (syntax escape))) ; avoid escaped leading quote |
| 107 (group (optional (any "uUrR"))) ; prefix gets syntax property | 115 (group (optional (any "uUrR"))) ; prefix gets syntax property |
| 108 (optional (any "rR")) ; possible second prefix | 116 (optional (any "rR")) ; possible second prefix |
| 109 (group (syntax string-quote)) ; maybe gets property | 117 (group (syntax string-quote)) ; maybe gets property |
| 110 (backref 2) ; per first quote | 118 (backref 2) ; per first quote |
| 111 (group (backref 2)))) ; maybe gets property | 119 (group (backref 2))) ; maybe gets property |
| 112 (1 (python-quote-syntax 1)) | 120 (1 (python-quote-syntax 1)) |
| 113 (2 (python-quote-syntax 2)) | 121 (2 (python-quote-syntax 2)) |
| 114 (3 (python-quote-syntax 3))) | 122 (3 (python-quote-syntax 3))) |
| 115 ;; This doesn't really help. | 123 ;; This doesn't really help. |
| 116 ;;; (,(rx (and ?\\ (group ?\n))) (1 " ")) | 124 ;;; (,(rx (and ?\\ (group ?\n))) (1 " ")) |
| 130 ;; Test cases: | 138 ;; Test cases: |
| 131 ;; ur"""ar""" x='"' # """ | 139 ;; ur"""ar""" x='"' # """ |
| 132 ;; x = ''' """ ' a | 140 ;; x = ''' """ ' a |
| 133 ;; ''' | 141 ;; ''' |
| 134 ;; x '"""' x """ \"""" x | 142 ;; x '"""' x """ \"""" x |
| 143 ;; Fixme: """""" goes wrong (due to syntax-ppss not getting the string | |
| 144 ;; fence context). | |
| 135 (save-excursion | 145 (save-excursion |
| 136 (goto-char (match-beginning 0)) | 146 (goto-char (match-beginning 0)) |
| 137 (cond | 147 (cond |
| 138 ;; Consider property for the last char if in a fenced string. | 148 ;; Consider property for the last char if in a fenced string. |
| 139 ((= n 3) | 149 ((= n 3) |
| 140 (let ((syntax (syntax-ppss))) | 150 (let ((syntax (syntax-ppss))) |
| 141 (when (eq t (nth 3 syntax)) ; after unclosed fence | 151 (when (eq t (nth 3 syntax)) ; after unclosed fence |
| 142 (goto-char (nth 8 syntax)) ; fence position | 152 (goto-char (nth 8 syntax)) ; fence position |
| 143 ;; Skip any prefix. | 153 (skip-chars-forward "uUrR") ; skip any prefix |
| 144 (if (memq (char-after) '(?u ?U ?R ?r)) | |
| 145 (skip-chars-forward "uUrR")) | |
| 146 ;; Is it a matching sequence? | 154 ;; Is it a matching sequence? |
| 147 (if (eq (char-after) (char-after (match-beginning 2))) | 155 (if (eq (char-after) (char-after (match-beginning 2))) |
| 148 (eval-when-compile (string-to-syntax "|")))))) | 156 (eval-when-compile (string-to-syntax "|")))))) |
| 149 ;; Consider property for initial char, accounting for prefixes. | 157 ;; Consider property for initial char, accounting for prefixes. |
| 150 ((or (and (= n 2) ; not prefix | 158 ((or (and (= n 2) ; leading quote (not prefix) |
| 151 (= (match-beginning 1) (match-end 1))) ; prefix is null | 159 (= (match-beginning 1) (match-end 1))) ; prefix is null |
| 152 (and (= n 1) ; prefix | 160 (and (= n 1) ; prefix |
| 153 (/= (match-beginning 1) (match-end 1)))) ; non-empty | 161 (/= (match-beginning 1) (match-end 1)))) ; non-empty |
| 154 (unless (eq 'string (syntax-ppss-context (syntax-ppss))) | 162 (unless (eq 'string (syntax-ppss-context (syntax-ppss))) |
| 155 (eval-when-compile (string-to-syntax "|")))) | 163 (eval-when-compile (string-to-syntax "|")))) |
| 156 ;; Otherwise (we're in a non-matching string) the property is | 164 ;; Otherwise (we're in a non-matching string) the property is |
| 157 ;; nil, which is OK. | 165 ;; nil, which is OK. |
| 158 ))) | 166 ))) |
| 159 | 167 |
| 160 ;; This isn't currently in `font-lock-defaults' as probably not worth | 168 ;; This isn't currently in `font-lock-defaults' as probably not worth |
| 202 (define-key map "\C-c\C-z" 'python-switch-to-python) | 210 (define-key map "\C-c\C-z" 'python-switch-to-python) |
| 203 (define-key map "\C-c\C-m" 'python-load-file) | 211 (define-key map "\C-c\C-m" 'python-load-file) |
| 204 (define-key map "\C-c\C-l" 'python-load-file) ; a la cmuscheme | 212 (define-key map "\C-c\C-l" 'python-load-file) ; a la cmuscheme |
| 205 (substitute-key-definition 'complete-symbol 'python-complete-symbol | 213 (substitute-key-definition 'complete-symbol 'python-complete-symbol |
| 206 map global-map) | 214 map global-map) |
| 207 ;; Fixme: Add :help to menu. | 215 (define-key map "\C-c\C-i" 'python-find-imports) |
| 216 (define-key map "\C-c\C-t" 'python-expand-template) | |
| 208 (easy-menu-define python-menu map "Python Mode menu" | 217 (easy-menu-define python-menu map "Python Mode menu" |
| 209 '("Python" | 218 `("Python" |
| 210 ["Shift region left" python-shift-left :active mark-active] | 219 :help "Python-specific Features" |
| 211 ["Shift region right" python-shift-right :active mark-active] | 220 ["Shift region left" python-shift-left :active mark-active |
| 221 :help "Shift by a single indentation step"] | |
| 222 ["Shift region right" python-shift-right :active mark-active | |
| 223 :help "Shift by a single indentation step"] | |
| 212 "-" | 224 "-" |
| 213 ["Mark block" python-mark-block] | 225 ["Mark block" python-mark-block |
| 226 :help "Mark innermost block around point"] | |
| 214 ["Mark def/class" mark-defun | 227 ["Mark def/class" mark-defun |
| 215 :help "Mark innermost definition around point"] | 228 :help "Mark innermost definition around point"] |
| 216 "-" | 229 "-" |
| 217 ["Start of block" python-beginning-of-block] | 230 ["Start of block" python-beginning-of-block |
| 218 ["End of block" python-end-of-block] | 231 :help "Go to start of innermost definition around point"] |
| 232 ["End of block" python-end-of-block | |
| 233 :help "Go to end of innermost definition around point"] | |
| 219 ["Start of def/class" beginning-of-defun | 234 ["Start of def/class" beginning-of-defun |
| 220 :help "Go to start of innermost definition around point"] | 235 :help "Go to start of innermost definition around point"] |
| 221 ["End of def/class" end-of-defun | 236 ["End of def/class" end-of-defun |
| 222 :help "Go to end of innermost definition around point"] | 237 :help "Go to end of innermost definition around point"] |
| 238 "-" | |
| 239 ("Templates..." | |
| 240 :help "Expand templates for compound statements" | |
| 241 :filter (lambda (&rest junk) | |
| 242 (mapcar (lambda (elt) | |
| 243 (vector (car elt) (cdr elt) t)) | |
| 244 python-skeletons))) ; defined later | |
| 223 "-" | 245 "-" |
| 224 ["Start interpreter" run-python | 246 ["Start interpreter" run-python |
| 225 :help "Run `inferior' Python in separate buffer"] | 247 :help "Run `inferior' Python in separate buffer"] |
| 226 ["Import/reload file" python-load-file | 248 ["Import/reload file" python-load-file |
| 227 :help "Load into inferior Python session"] | 249 :help "Load into inferior Python session"] |
| 231 :help "Evaluate region en bloc in inferior Python session"] | 253 :help "Evaluate region en bloc in inferior Python session"] |
| 232 ["Eval def/class" python-send-defun | 254 ["Eval def/class" python-send-defun |
| 233 :help "Evaluate current definition in inferior Python session"] | 255 :help "Evaluate current definition in inferior Python session"] |
| 234 ["Switch to interpreter" python-switch-to-python | 256 ["Switch to interpreter" python-switch-to-python |
| 235 :help "Switch to inferior Python buffer"] | 257 :help "Switch to inferior Python buffer"] |
| 258 ["Set default process" python-set-proc | |
| 259 :help "Make buffer's inferior process the default" | |
| 260 :active (buffer-live-p python-buffer)] | |
| 236 ["Check file" python-check :help "Run pychecker"] | 261 ["Check file" python-check :help "Run pychecker"] |
| 237 ["Debugger" pdb :help "Run pdb under GUD"] | 262 ["Debugger" pdb :help "Run pdb under GUD"] |
| 238 "-" | 263 "-" |
| 239 ["Help on symbol" python-describe-symbol | 264 ["Help on symbol" python-describe-symbol |
| 240 :help "Use pydoc on symbol at point"])) | 265 :help "Use pydoc on symbol at point"] |
| 266 ["Complete symbol" python-complete-symbol | |
| 267 :help "Complete (qualified) symbol before point"] | |
| 268 ["Update imports" python-find-imports | |
| 269 :help "Update list of top-level imports for completion"])) | |
| 241 map)) | 270 map)) |
| 271 ;; Fixme: add toolbar stuff for useful things like symbol help, send | |
| 272 ;; region, at least. (Shouldn't be specific to Python, obviously.) | |
| 273 ;; eric has items including: (un)indent, (un)comment, restart script, | |
| 274 ;; run script, debug script; also things for profiling, unit testing. | |
| 242 | 275 |
| 243 (defvar python-mode-syntax-table | 276 (defvar python-mode-syntax-table |
| 244 (let ((table (make-syntax-table))) | 277 (let ((table (make-syntax-table))) |
| 245 ;; Give punctuation syntax to ASCII that normally has symbol | 278 ;; Give punctuation syntax to ASCII that normally has symbol |
| 246 ;; syntax or has word syntax and isn't a letter. | 279 ;; syntax or has word syntax and isn't a letter. |
| 261 | 294 |
| 262 ;;;; Utility stuff | 295 ;;;; Utility stuff |
| 263 | 296 |
| 264 (defsubst python-in-string/comment () | 297 (defsubst python-in-string/comment () |
| 265 "Return non-nil if point is in a Python literal (a comment or string)." | 298 "Return non-nil if point is in a Python literal (a comment or string)." |
| 266 (syntax-ppss-context (syntax-ppss))) | 299 ;; We don't need to save the match data. |
| 300 (nth 8 (syntax-ppss))) | |
| 267 | 301 |
| 268 (defconst python-space-backslash-table | 302 (defconst python-space-backslash-table |
| 269 (let ((table (copy-syntax-table python-mode-syntax-table))) | 303 (let ((table (copy-syntax-table python-mode-syntax-table))) |
| 270 (modify-syntax-entry ?\\ " " table) | 304 (modify-syntax-entry ?\\ " " table) |
| 271 table) | 305 table) |
| 272 "`python-mode-syntax-table' with backslash given whitespace syntax.") | 306 "`python-mode-syntax-table' with backslash given whitespace syntax.") |
| 273 | 307 |
| 274 (defun python-skip-comments/blanks (&optional backward) | 308 (defun python-skip-comments/blanks (&optional backward) |
| 275 "Skip comments and blank lines. | 309 "Skip comments and blank lines. |
| 276 BACKWARD non-nil means go backwards, otherwise go forwards. Backslash is | 310 BACKWARD non-nil means go backwards, otherwise go forwards. |
| 277 treated as whitespace so that continued blank lines are skipped. | 311 Backslash is treated as whitespace so that continued blank lines |
| 278 Doesn't move out of comments -- should be outside or at end of line." | 312 are skipped. Doesn't move out of comments -- should be outside |
| 279 (with-syntax-table python-space-backslash-table | 313 or at end of line." |
| 280 (forward-comment (if backward | 314 (let ((arg (if backward |
| 281 most-negative-fixnum | 315 ;; If we're in a comment (including on the trailing |
| 282 most-positive-fixnum)))) | 316 ;; newline), forward-comment doesn't move backwards out |
| 317 ;; of it. Don't set the syntax table round this bit! | |
| 318 (let ((syntax (syntax-ppss))) | |
| 319 (if (nth 4 syntax) | |
| 320 (goto-char (nth 8 syntax))) | |
| 321 (- (point-max))) | |
| 322 (point-max)))) | |
| 323 (with-syntax-table python-space-backslash-table | |
| 324 (forward-comment arg)))) | |
| 283 | 325 |
| 284 (defun python-backslash-continuation-line-p () | 326 (defun python-backslash-continuation-line-p () |
| 285 "Non-nil if preceding line ends with backslash that is not in a comment." | 327 "Non-nil if preceding line ends with backslash that is not in a comment." |
| 286 (and (eq ?\\ (char-before (line-end-position 0))) | 328 (and (eq ?\\ (char-before (line-end-position 0))) |
| 287 (not (syntax-ppss-context (syntax-ppss))))) | 329 (not (syntax-ppss-context (syntax-ppss))))) |
| 288 | 330 |
| 289 (defun python-continuation-line-p () | 331 (defun python-continuation-line-p () |
| 290 "Return non-nil if current line continues a previous one. | 332 "Return non-nil if current line continues a previous one. |
| 291 The criteria are that the previous line ends in a backslash outside | 333 The criteria are that the previous line ends in a backslash outside |
| 292 comments and strings, or that the bracket/paren nesting depth is nonzero." | 334 comments and strings, or that point is within brackets/parens." |
| 293 (or (and (eq ?\\ (char-before (line-end-position 0))) | 335 (or (python-backslash-continuation-line-p) |
| 294 (not (syntax-ppss-context (syntax-ppss)))) | 336 (let ((depth (syntax-ppss-depth |
| 295 (< 0 (syntax-ppss-depth | 337 (save-excursion ; syntax-ppss with arg changes point |
| 296 (save-excursion ; syntax-ppss with arg changes point | 338 (syntax-ppss (line-beginning-position)))))) |
| 297 (syntax-ppss (line-beginning-position))))))) | 339 (or (> depth 0) |
| 340 (if (< depth 0) ; Unbalanced brackets -- act locally | |
| 341 (save-excursion | |
| 342 (condition-case () | |
| 343 (progn (backward-up-list) t) ; actually within brackets | |
| 344 (error nil)))))))) | |
| 298 | 345 |
| 299 (defun python-comment-line-p () | 346 (defun python-comment-line-p () |
| 300 "Return non-nil iff current line has only a comment." | 347 "Return non-nil iff current line has only a comment." |
| 301 (save-excursion | 348 (save-excursion |
| 302 (end-of-line) | 349 (end-of-line) |
| 303 (when (eq 'comment (syntax-ppss-context (syntax-ppss))) | 350 (when (eq 'comment (syntax-ppss-context (syntax-ppss))) |
| 304 (back-to-indentation) | 351 (back-to-indentation) |
| 305 (looking-at (rx (or (syntax comment-start) line-end)))))) | 352 (looking-at (rx (or (syntax comment-start) line-end)))))) |
| 306 | 353 |
| 354 (defun python-blank-line-p () | |
| 355 "Return non-nil iff current line is blank." | |
| 356 (save-excursion | |
| 357 (beginning-of-line) | |
| 358 (looking-at "\\s-*$"))) | |
| 359 | |
| 307 (defun python-beginning-of-string () | 360 (defun python-beginning-of-string () |
| 308 "Go to beginning of string around point. | 361 "Go to beginning of string around point. |
| 309 Do nothing if not in string." | 362 Do nothing if not in string." |
| 310 (let ((state (syntax-ppss))) | 363 (let ((state (syntax-ppss))) |
| 311 (when (eq 'string (syntax-ppss-context state)) | 364 (when (eq 'string (syntax-ppss-context state)) |
| 314 (defun python-open-block-statement-p (&optional bos) | 367 (defun python-open-block-statement-p (&optional bos) |
| 315 "Return non-nil if statement at point opens a block. | 368 "Return non-nil if statement at point opens a block. |
| 316 BOS non-nil means point is known to be at beginning of statement." | 369 BOS non-nil means point is known to be at beginning of statement." |
| 317 (save-excursion | 370 (save-excursion |
| 318 (unless bos (python-beginning-of-statement)) | 371 (unless bos (python-beginning-of-statement)) |
| 319 (and (not (python-comment-line-p)) | 372 (looking-at (rx (and (or "if" "else" "elif" "while" "for" "def" |
| 320 (re-search-forward (rx (and ?: (0+ space) | 373 "class" "try" "except" "finally") |
| 321 (optional (and (syntax comment-start) | 374 symbol-end))))) |
| 322 (0+ not-newline))) | |
| 323 line-end)) | |
| 324 (save-excursion (python-end-of-statement)) | |
| 325 t) | |
| 326 (not (progn (goto-char (match-beginning 0)) | |
| 327 (python-in-string/comment)))))) | |
| 328 | 375 |
| 329 (defun python-close-block-statement-p (&optional bos) | 376 (defun python-close-block-statement-p (&optional bos) |
| 330 "Return non-nil if current line is a statement closing a block. | 377 "Return non-nil if current line is a statement closing a block. |
| 331 BOS non-nil means point is at beginning of statement. | 378 BOS non-nil means point is at beginning of statement. |
| 332 The criteria are that the line isn't a comment or in string and starts with | 379 The criteria are that the line isn't a comment or in string and |
| 333 keyword `raise', `break', `continue' or `pass'." | 380 starts with keyword `raise', `break', `continue' or `pass'." |
| 334 (save-excursion | 381 (save-excursion |
| 335 (unless bos (python-beginning-of-statement)) | 382 (unless bos (python-beginning-of-statement)) |
| 336 (back-to-indentation) | 383 (back-to-indentation) |
| 337 (looking-at (rx (and (or "return" "raise" "break" "continue" "pass") | 384 (looking-at (rx (or "return" "raise" "break" "continue" "pass") |
| 338 symbol-end))))) | 385 symbol-end)))) |
| 339 | 386 |
| 340 (defun python-outdent-p () | 387 (defun python-outdent-p () |
| 341 "Return non-nil if current line should outdent a level." | 388 "Return non-nil if current line should outdent a level." |
| 342 (save-excursion | 389 (save-excursion |
| 343 (back-to-indentation) | 390 (back-to-indentation) |
| 344 (and (looking-at (rx (and (or (and (or "else" "finally") symbol-end) | 391 (and (looking-at (rx (and (or "else" "finally" "except" "elif") |
| 345 (and (or "except" "elif") symbol-end | 392 symbol-end))) |
| 346 (1+ (not (any ?:))))) | 393 (not (python-in-string/comment)) |
| 347 (optional space) ":" (optional space) | |
| 348 (or (syntax comment-start) line-end)))) | |
| 349 (progn (end-of-line) | |
| 350 (not (python-in-string/comment))) | |
| 351 ;; Ensure there's a previous statement and move to it. | 394 ;; Ensure there's a previous statement and move to it. |
| 352 (zerop (python-previous-statement)) | 395 (zerop (python-previous-statement)) |
| 353 (not (python-close-block-statement-p t)) | 396 (not (python-close-block-statement-p t)) |
| 354 ;; Fixme: check this | 397 ;; Fixme: check this |
| 355 (not (looking-at (rx (and (or (and (or "if" "elif" "except" | 398 (not (python-open-block-statement-p))))) |
| 356 "for" "while") | |
| 357 symbol-end (1+ (not (any ?:)))) | |
| 358 (and "try" symbol-end)) | |
| 359 (optional space) ":" (optional space) | |
| 360 (or (syntax comment-start) line-end))))) | |
| 361 (progn (end-of-line) | |
| 362 (not (python-in-string/comment)))))) | |
| 363 | 399 |
| 364 ;;;; Indentation. | 400 ;;;; Indentation. |
| 365 | 401 |
| 366 (defcustom python-indent 4 | 402 (defcustom python-indent 4 |
| 367 "*Number of columns for a unit of indentation in Python mode. | 403 "*Number of columns for a unit of indentation in Python mode. |
| 375 :group 'python) | 411 :group 'python) |
| 376 | 412 |
| 377 (defcustom python-indent-string-contents t | 413 (defcustom python-indent-string-contents t |
| 378 "*Non-nil means indent contents of multi-line strings together. | 414 "*Non-nil means indent contents of multi-line strings together. |
| 379 This means indent them the same as the preceding non-blank line. | 415 This means indent them the same as the preceding non-blank line. |
| 380 Otherwise indent them to column zero." | 416 Otherwise preserve their indentation. |
| 417 | |
| 418 This only applies to `doc' strings, i.e. those that form statements; | |
| 419 the indentation is preserved in others." | |
| 381 :type '(choice (const :tag "Align with preceding" t) | 420 :type '(choice (const :tag "Align with preceding" t) |
| 382 (const :tag "Indent to column 0" nil)) | 421 (const :tag "Preserve indentation" nil)) |
| 383 :group 'python) | 422 :group 'python) |
| 384 | 423 |
| 385 (defcustom python-honour-comment-indentation nil | 424 (defcustom python-honour-comment-indentation nil |
| 386 "Non-nil means indent relative to preceding comment line. | 425 "Non-nil means indent relative to preceding comment line. |
| 387 Only do this for comments where the leading comment character is followed | 426 Only do this for comments where the leading comment character is |
| 388 by space. This doesn't apply to comment lines, which are always indented | 427 followed by space. This doesn't apply to comment lines, which |
| 389 in lines with preceding comments." | 428 are always indented in lines with preceding comments." |
| 390 :type 'boolean | 429 :type 'boolean |
| 391 :group 'python) | 430 :group 'python) |
| 392 | 431 |
| 393 (defcustom python-continuation-offset 4 | 432 (defcustom python-continuation-offset 4 |
| 394 "*Number of columns of additional indentation for continuation lines. | 433 "*Number of columns of additional indentation for continuation lines. |
| 395 Continuation lines follow a backslash-terminated line starting a statement." | 434 Continuation lines follow a backslash-terminated line starting a |
| 435 statement." | |
| 396 :group 'python | 436 :group 'python |
| 397 :type 'integer) | 437 :type 'integer) |
| 398 | 438 |
| 399 (defun python-guess-indent () | 439 (defun python-guess-indent () |
| 400 "Guess step for indentation of current buffer. | 440 "Guess step for indentation of current buffer. |
| 404 (save-restriction | 444 (save-restriction |
| 405 (widen) | 445 (widen) |
| 406 (goto-char (point-min)) | 446 (goto-char (point-min)) |
| 407 (let (done indent) | 447 (let (done indent) |
| 408 (while (and (not done) (not (eobp))) | 448 (while (and (not done) (not (eobp))) |
| 409 (when (and (re-search-forward (rx (and ?: (0+ space) | 449 (when (and (re-search-forward (rx ?: (0+ space) |
| 410 (or (syntax comment-start) | 450 (or (syntax comment-start) |
| 411 line-end))) | 451 line-end)) |
| 412 nil 'move) | 452 nil 'move) |
| 413 (python-open-block-statement-p)) | 453 (python-open-block-statement-p)) |
| 414 (save-excursion | 454 (save-excursion |
| 415 (python-beginning-of-statement) | 455 (python-beginning-of-statement) |
| 416 (let ((initial (current-indentation))) | 456 (let ((initial (current-indentation))) |
| 423 (set (make-local-variable 'python-indent) indent) | 463 (set (make-local-variable 'python-indent) indent) |
| 424 (unless (= tab-width python-indent) | 464 (unless (= tab-width python-indent) |
| 425 (setq indent-tabs-mode nil))) | 465 (setq indent-tabs-mode nil))) |
| 426 indent))))) | 466 indent))))) |
| 427 | 467 |
| 468 ;; Alist of possible indentations and start of statement they would | |
| 469 ;; close. Used in indentation cycling (below). | |
| 470 (defvar python-indent-list nil | |
| 471 "Internal use.") | |
| 472 ;; Length of the above | |
| 473 (defvar python-indent-list-length nil | |
| 474 "Internal use.") | |
| 475 ;; Current index into the alist. | |
| 476 (defvar python-indent-index nil | |
| 477 "Internal use.") | |
| 478 | |
| 428 (defun python-calculate-indentation () | 479 (defun python-calculate-indentation () |
| 429 "Calculate Python indentation for line at point." | 480 "Calculate Python indentation for line at point." |
| 481 (setq python-indent-list nil | |
| 482 python-indent-list-length 1) | |
| 430 (save-excursion | 483 (save-excursion |
| 431 (beginning-of-line) | 484 (beginning-of-line) |
| 432 (let ((syntax (syntax-ppss)) | 485 (let ((syntax (syntax-ppss)) |
| 433 start) | 486 start) |
| 434 (cond | 487 (cond |
| 435 ((eq 'string (syntax-ppss-context syntax)) ; multi-line string | 488 ((eq 'string (syntax-ppss-context syntax)) ; multi-line string |
| 436 (if (not python-indent-string-contents) | 489 (if (not python-indent-string-contents) |
| 437 0 | 490 (current-indentation) |
| 438 (save-excursion | 491 ;; Only respect `python-indent-string-contents' in doc |
| 492 ;; strings (defined as those which form statements). | |
| 493 (if (not (save-excursion | |
| 494 (python-beginning-of-statement) | |
| 495 (looking-at (rx (or (syntax string-delimiter) | |
| 496 (syntax string-quote)))))) | |
| 497 (current-indentation) | |
| 439 ;; Find indentation of preceding non-blank line within string. | 498 ;; Find indentation of preceding non-blank line within string. |
| 440 (setq start (nth 8 syntax)) | 499 (setq start (nth 8 syntax)) |
| 441 (forward-line -1) | 500 (forward-line -1) |
| 442 (while (and (< start (point)) (looking-at "\\s-*$")) | 501 (while (and (< start (point)) (looking-at "\\s-*$")) |
| 443 (forward-line -1)) | 502 (forward-line -1)) |
| 444 (current-indentation)))) | 503 (current-indentation)))) |
| 445 ((python-continuation-line-p) | 504 ((python-continuation-line-p) ; after backslash, or bracketed |
| 446 (let ((point (point)) | 505 (let ((point (point)) |
| 447 (open-start (cadr syntax))) | 506 (open-start (cadr syntax)) |
| 507 (backslash (python-backslash-continuation-line-p)) | |
| 508 (colon (eq ?: (char-before (1- (line-beginning-position)))))) | |
| 448 (if open-start | 509 (if open-start |
| 449 ;; Inside bracketed expression. | 510 ;; Inside bracketed expression. |
| 450 (progn | 511 (progn |
| 451 (goto-char (1+ open-start)) | 512 (goto-char (1+ open-start)) |
| 452 ;; Look for first item in list (preceding point) and | 513 ;; Look for first item in list (preceding point) and |
| 456 (condition-case () | 517 (condition-case () |
| 457 (progn (forward-sexp) | 518 (progn (forward-sexp) |
| 458 (backward-sexp) | 519 (backward-sexp) |
| 459 (< (point) point)) | 520 (< (point) point)) |
| 460 (error nil)))) | 521 (error nil)))) |
| 461 (current-column) | 522 ;; Extra level if we're backslash-continued or |
| 523 ;; following a key. | |
| 524 (if (or backslash colon) | |
| 525 (+ python-indent (current-column)) | |
| 526 (current-column)) | |
| 462 ;; Otherwise indent relative to statement start, one | 527 ;; Otherwise indent relative to statement start, one |
| 463 ;; level per bracketing level. | 528 ;; level per bracketing level. |
| 464 (goto-char (1+ open-start)) | 529 (goto-char (1+ open-start)) |
| 465 (python-beginning-of-statement) | 530 (python-beginning-of-statement) |
| 466 (+ (current-indentation) (* (car syntax) python-indent)))) | 531 (+ (current-indentation) (* (car syntax) python-indent)))) |
| 470 ;; We're past first continuation line. Align with | 535 ;; We're past first continuation line. Align with |
| 471 ;; previous line. | 536 ;; previous line. |
| 472 (current-indentation) | 537 (current-indentation) |
| 473 ;; First continuation line. Indent one step, with an | 538 ;; First continuation line. Indent one step, with an |
| 474 ;; extra one if statement opens a block. | 539 ;; extra one if statement opens a block. |
| 475 (save-excursion | 540 (python-beginning-of-statement) |
| 476 (python-beginning-of-statement) | 541 (+ (current-indentation) python-continuation-offset |
| 477 (+ (current-indentation) python-continuation-offset | 542 (if (python-open-block-statement-p t) |
| 478 (if (python-open-block-statement-p t) | 543 python-indent |
| 479 python-indent | 544 0)))))) |
| 480 0))))))) | |
| 481 ((bobp) 0) | 545 ((bobp) 0) |
| 482 ;; Fixme: Like python-mode.el; not convinced by this. | 546 ;; Fixme: Like python-mode.el; not convinced by this. |
| 483 ((looking-at (rx (and (0+ space) (syntax comment-start) | 547 ((looking-at (rx (0+ space) (syntax comment-start) |
| 484 (not (any " \t\n"))))) ; non-indentable comment | 548 (not (any " \t\n")))) ; non-indentable comment |
| 485 (current-indentation)) | 549 (current-indentation)) |
| 486 (t (let ((point (point))) | 550 (t (let ((point (point))) |
| 487 (if python-honour-comment-indentation | 551 (if python-honour-comment-indentation |
| 488 ;; Back over whitespace, newlines, non-indentable comments. | 552 ;; Back over whitespace, newlines, non-indentable comments. |
| 489 (catch 'done | 553 (catch 'done |
| 496 ;; trailing comment | 560 ;; trailing comment |
| 497 ((/= (current-column) (current-indentation)) | 561 ((/= (current-column) (current-indentation)) |
| 498 (python-beginning-of-statement) | 562 (python-beginning-of-statement) |
| 499 t) | 563 t) |
| 500 ;; indentable comment like python-mode.el | 564 ;; indentable comment like python-mode.el |
| 501 ((and (looking-at (rx (and (syntax comment-start) | 565 ((and (looking-at (rx (syntax comment-start) |
| 502 (or space line-end)))) | 566 (or space line-end))) |
| 503 (/= 0 (current-column))))) | 567 (/= 0 (current-column))))) |
| 504 (throw 'done t)))) | 568 (throw 'done t))))) |
| 505 ;; Else back over all comments. | 569 (python-indentation-levels) |
| 506 (python-skip-comments/blanks t) | 570 ;; Prefer to indent comments with an immediately-following |
| 507 (python-beginning-of-statement)) | 571 ;; statement, e.g. |
| 508 ;; don't lose on bogus outdent | 572 ;; ... |
| 509 (max 0 (+ (current-indentation) | 573 ;; # ... |
| 510 (or (cond ((python-open-block-statement-p t) | 574 ;; def ... |
| 511 python-indent) | 575 (when (and (> python-indent-list-length 1) |
| 512 ((python-close-block-statement-p t) | 576 (python-comment-line-p)) |
| 513 (- python-indent))) | 577 (forward-line) |
| 514 (progn (goto-char point) | 578 (unless (python-comment-line-p) |
| 515 (if (python-outdent-p) | 579 (let ((elt (assq (current-indentation) python-indent-list))) |
| 516 (- python-indent))) | 580 (setq python-indent-list |
| 517 0))))))))) | 581 (nconc (delete elt python-indent-list) |
| 518 | 582 (list elt)))))) |
| 519 (defun python-comment-indent () | 583 (caar (last python-indent-list)))))))) |
| 520 "`comment-indent-function' for Python." | |
| 521 ;; If previous non-blank line was a comment, use its indentation. | |
| 522 ;; FIXME: This seems unnecessary since the default code delegates to | |
| 523 ;; indent-according-to-mode. --Stef | |
| 524 (unless (bobp) | |
| 525 (save-excursion | |
| 526 (forward-comment -1) | |
| 527 (if (eq ?# (char-after)) (current-column))))) | |
| 528 | 584 |
| 529 ;;;; Cycling through the possible indentations with successive TABs. | 585 ;;;; Cycling through the possible indentations with successive TABs. |
| 530 | 586 |
| 531 ;; These don't need to be buffer-local since they're only relevant | 587 ;; These don't need to be buffer-local since they're only relevant |
| 532 ;; during a cycle. | 588 ;; during a cycle. |
| 533 | 589 |
| 534 ;; Alist of possible indentations and start of statement they would close. | |
| 535 (defvar python-indent-list nil | |
| 536 "Internal use.") | |
| 537 ;; Length of the above | |
| 538 (defvar python-indent-list-length nil | |
| 539 "Internal use.") | |
| 540 ;; Current index into the alist. | |
| 541 (defvar python-indent-index nil | |
| 542 "Internal use.") | |
| 543 | |
| 544 (defun python-initial-text () | 590 (defun python-initial-text () |
| 545 "Text of line following indentation and ignoring any trailing comment." | 591 "Text of line following indentation and ignoring any trailing comment." |
| 546 (buffer-substring (+ (line-beginning-position) (current-indentation)) | 592 (save-excursion |
| 547 (save-excursion | 593 (buffer-substring (progn |
| 548 (end-of-line) | 594 (back-to-indentation) |
| 549 (forward-comment -1) | 595 (point)) |
| 550 (point)))) | 596 (progn |
| 597 (end-of-line) | |
| 598 (forward-comment -1) | |
| 599 (point))))) | |
| 600 | |
| 601 (defconst python-block-pairs | |
| 602 '(("else" "if" "elif" "while" "for" "try" "except") | |
| 603 ("elif" "if" "elif") | |
| 604 ("except" "try" "except") | |
| 605 ("finally" "try")) | |
| 606 "Alist of keyword matches. | |
| 607 The car of an element is a keyword introducing a statement which | |
| 608 can close a block opened by a keyword in the cdr.") | |
| 609 | |
| 610 (defun python-first-word () | |
| 611 "Return first word (actually symbol) on the line." | |
| 612 (save-excursion | |
| 613 (back-to-indentation) | |
| 614 (current-word t))) | |
| 551 | 615 |
| 552 (defun python-indentation-levels () | 616 (defun python-indentation-levels () |
| 553 "Return a list of possible indentations for this line. | 617 "Return a list of possible indentations for this line. |
| 618 It is assumed not to be a continuation line or in a multi-line string. | |
| 554 Includes the default indentation and those which would close all | 619 Includes the default indentation and those which would close all |
| 555 enclosing blocks. Assumes the line has already been indented per | 620 enclosing blocks. Elements of the list are actually pairs: |
| 556 `python-indent-line'. Elements of the list are actually pairs: | |
| 557 \(INDENTATION . TEXT), where TEXT is the initial text of the | 621 \(INDENTATION . TEXT), where TEXT is the initial text of the |
| 558 corresponding block opening (or nil)." | 622 corresponding block opening (or nil)." |
| 559 (save-excursion | 623 (save-excursion |
| 560 (let ((levels (list (cons (current-indentation) | 624 (let ((initial "") |
| 561 (save-excursion | 625 levels indent) |
| 562 (if (python-beginning-of-block) | 626 ;; Only one possibility immediately following a block open |
| 563 (python-initial-text))))))) | 627 ;; statement, assuming it doesn't have a `suite' on the same line. |
| 564 ;; Only one possibility if we immediately follow a block open or | 628 (cond |
| 565 ;; are in a continuation line. | 629 ((save-excursion (and (python-previous-statement) |
| 566 (unless (or (python-continuation-line-p) | 630 (python-open-block-statement-p t) |
| 567 (save-excursion (and (python-previous-statement) | 631 (setq indent (current-indentation)) |
| 568 (python-open-block-statement-p t)))) | 632 ;; Check we don't have something like: |
| 569 (while (python-beginning-of-block) | 633 ;; if ...: ... |
| 570 (push (cons (current-indentation) (python-initial-text)) | 634 (if (progn (python-end-of-statement) |
| 571 levels))) | 635 (python-skip-comments/blanks t) |
| 572 levels))) | 636 (eq ?: (char-before))) |
| 637 (setq indent (+ python-indent indent))))) | |
| 638 (push (cons indent initial) levels)) | |
| 639 ;; Only one possibility for comment line immediately following | |
| 640 ;; another. | |
| 641 ((save-excursion | |
| 642 (when (python-comment-line-p) | |
| 643 (forward-line -1) | |
| 644 (if (python-comment-line-p) | |
| 645 (push (cons (current-indentation) initial) levels))))) | |
| 646 ;; Fixme: Maybe have a case here which indents (only) first | |
| 647 ;; line after a lambda. | |
| 648 (t | |
| 649 (let ((start (car (assoc (python-first-word) python-block-pairs)))) | |
| 650 (python-previous-statement) | |
| 651 ;; Is this a valid indentation for the line of interest? | |
| 652 (unless (or (if start ; potentially only outdentable | |
| 653 ;; Check for things like: | |
| 654 ;; if ...: ... | |
| 655 ;; else ...: | |
| 656 ;; where the second line need not be outdented. | |
| 657 (not (member (python-first-word) | |
| 658 (cdr (assoc start | |
| 659 python-block-pairs))))) | |
| 660 ;; Not sensible to indent to the same level as | |
| 661 ;; previous `return' &c. | |
| 662 (python-close-block-statement-p)) | |
| 663 (push (cons (current-indentation) (python-initial-text)) | |
| 664 levels)) | |
| 665 (while (python-beginning-of-block) | |
| 666 (when (or (not start) | |
| 667 (member (python-first-word) | |
| 668 (cdr (assoc start python-block-pairs)))) | |
| 669 (push (cons (current-indentation) (python-initial-text)) | |
| 670 levels)))))) | |
| 671 (prog1 (or levels (setq levels '((0 . "")))) | |
| 672 (setq python-indent-list levels | |
| 673 python-indent-list-length (length python-indent-list)))))) | |
| 573 | 674 |
| 574 ;; This is basically what `python-indent-line' would be if we didn't | 675 ;; This is basically what `python-indent-line' would be if we didn't |
| 575 ;; do the cycling. | 676 ;; do the cycling. |
| 576 (defun python-indent-line-1 () | 677 (defun python-indent-line-1 (&optional leave) |
| 577 "Subroutine of `python-indent-line'." | 678 "Subroutine of `python-indent-line'. |
| 679 Does non-repeated indentation. LEAVE non-nil means leave | |
| 680 indentation if it is valid, i.e. one of the positions returned by | |
| 681 `python-calculate-indentation'." | |
| 578 (let ((target (python-calculate-indentation)) | 682 (let ((target (python-calculate-indentation)) |
| 579 (pos (- (point-max) (point)))) | 683 (pos (- (point-max) (point)))) |
| 580 (if (= target (current-indentation)) | 684 (if (or (= target (current-indentation)) |
| 685 ;; Maybe keep a valid indentation. | |
| 686 (and leave python-indent-list | |
| 687 (assq (current-indentation) python-indent-list))) | |
| 581 (if (< (current-column) (current-indentation)) | 688 (if (< (current-column) (current-indentation)) |
| 582 (back-to-indentation)) | 689 (back-to-indentation)) |
| 583 (beginning-of-line) | 690 (beginning-of-line) |
| 584 (delete-horizontal-space) | 691 (delete-horizontal-space) |
| 585 (indent-to target) | 692 (indent-to target) |
| 587 (goto-char (- (point-max) pos)))))) | 694 (goto-char (- (point-max) pos)))))) |
| 588 | 695 |
| 589 (defun python-indent-line () | 696 (defun python-indent-line () |
| 590 "Indent current line as Python code. | 697 "Indent current line as Python code. |
| 591 When invoked via `indent-for-tab-command', cycle through possible | 698 When invoked via `indent-for-tab-command', cycle through possible |
| 592 indentations for current line. The cycle is broken by a command different | 699 indentations for current line. The cycle is broken by a command |
| 593 from `indent-for-tab-command', i.e. successive TABs do the cycling." | 700 different from `indent-for-tab-command', i.e. successive TABs do |
| 701 the cycling." | |
| 594 (interactive) | 702 (interactive) |
| 595 ;; Don't do extra work if invoked via `indent-region', for instance. | 703 (if (and (eq this-command 'indent-for-tab-command) |
| 596 (if (not (eq this-command 'indent-for-tab-command)) | 704 (eq last-command this-command)) |
| 597 (python-indent-line-1) | 705 (if (= 1 python-indent-list-length) |
| 598 (if (eq last-command this-command) | 706 (message "Sole indentation") |
| 599 (if (= 1 python-indent-list-length) | 707 (progn (setq python-indent-index |
| 600 (message "Sole indentation") | 708 (% (1+ python-indent-index) python-indent-list-length)) |
| 601 (progn (setq python-indent-index (% (1+ python-indent-index) | 709 (beginning-of-line) |
| 602 python-indent-list-length)) | 710 (delete-horizontal-space) |
| 603 (beginning-of-line) | 711 (indent-to (car (nth python-indent-index python-indent-list))) |
| 604 (delete-horizontal-space) | 712 (if (python-block-end-p) |
| 605 (indent-to (car (nth python-indent-index python-indent-list))) | 713 (let ((text (cdr (nth python-indent-index |
| 606 (if (python-block-end-p) | 714 python-indent-list)))) |
| 607 (let ((text (cdr (nth python-indent-index | 715 (if text |
| 608 python-indent-list)))) | 716 (message "Closes: %s" text)))))) |
| 609 (if text | 717 (python-indent-line-1) |
| 610 (message "Closes: %s" text)))))) | 718 (setq python-indent-index (1- python-indent-list-length)))) |
| 611 (python-indent-line-1) | 719 |
| 612 (setq python-indent-list (python-indentation-levels) | 720 (defun python-indent-region (start end) |
| 613 python-indent-list-length (length python-indent-list) | 721 "`indent-region-function' for Python. |
| 614 python-indent-index (1- python-indent-list-length))))) | 722 Leaves validly-indented lines alone, i.e. doesn't indent to |
| 723 another valid position." | |
| 724 (save-excursion | |
| 725 (goto-char end) | |
| 726 (setq end (point-marker)) | |
| 727 (goto-char start) | |
| 728 (or (bolp) (forward-line 1)) | |
| 729 (while (< (point) end) | |
| 730 (or (and (bolp) (eolp)) | |
| 731 (python-indent-line-1 t)) | |
| 732 (forward-line 1)) | |
| 733 (move-marker end nil))) | |
| 615 | 734 |
| 616 (defun python-block-end-p () | 735 (defun python-block-end-p () |
| 617 "Non-nil if this is a line in a statement closing a block, | 736 "Non-nil if this is a line in a statement closing a block, |
| 618 or a blank line indented to where it would close a block." | 737 or a blank line indented to where it would close a block." |
| 619 (and (not (python-comment-line-p)) | 738 (and (not (python-comment-line-p)) |
| 620 (or (python-close-block-statement-p t) | 739 (or (python-close-block-statement-p t) |
| 621 (< (current-indentation) | 740 (< (current-indentation) |
| 622 (save-excursion | 741 (save-excursion |
| 623 (python-previous-statement) | 742 (python-previous-statement) |
| 624 (current-indentation)))))) | 743 (current-indentation)))))) |
| 625 | |
| 626 ;; Fixme: Define an indent-region-function. It should probably leave | |
| 627 ;; lines alone if the indentation is already at one of the allowed | |
| 628 ;; levels. Otherwise, M-C-\ typically keeps indenting more deeply | |
| 629 ;; down a function. | |
| 630 | 744 |
| 631 ;;;; Movement. | 745 ;;;; Movement. |
| 746 | |
| 747 ;; Fixme: Define {for,back}ward-sexp-function? Maybe skip units like | |
| 748 ;; block, statement, depending on context. | |
| 632 | 749 |
| 633 (defun python-beginning-of-defun () | 750 (defun python-beginning-of-defun () |
| 634 "`beginning-of-defun-function' for Python. | 751 "`beginning-of-defun-function' for Python. |
| 635 Finds beginning of innermost nested class or method definition. | 752 Finds beginning of innermost nested class or method definition. |
| 636 Returns the name of the definition found at the end, or nil if reached | 753 Returns the name of the definition found at the end, or nil if |
| 637 start of buffer." | 754 reached start of buffer." |
| 638 (let ((ci (current-indentation)) | 755 (let ((ci (current-indentation)) |
| 639 (def-re (rx (and line-start (0+ space) (or "def" "class") | 756 (def-re (rx line-start (0+ space) (or "def" "class") (1+ space) |
| 640 (1+ space) | 757 (group (1+ (or word (syntax symbol)))))) |
| 641 (group (1+ (or word (syntax symbol))))))) | |
| 642 found lep def-line) | 758 found lep def-line) |
| 643 (if (python-comment-line-p) | 759 (if (python-comment-line-p) |
| 644 (setq ci most-positive-fixnum)) | 760 (setq ci most-positive-fixnum)) |
| 645 (while (and (not (bobp)) (not found)) | 761 (while (and (not (bobp)) (not found)) |
| 646 ;; Treat bol at beginning of function as outside function so | 762 ;; Treat bol at beginning of function as outside function so |
| 650 (setq lep (line-end-position)) | 766 (setq lep (line-end-position)) |
| 651 (if (and (re-search-backward def-re nil 'move) | 767 (if (and (re-search-backward def-re nil 'move) |
| 652 ;; Must be less indented or matching top level, or | 768 ;; Must be less indented or matching top level, or |
| 653 ;; equally indented if we started on a definition line. | 769 ;; equally indented if we started on a definition line. |
| 654 (let ((in (current-indentation))) | 770 (let ((in (current-indentation))) |
| 655 (or (and (zerop ci) (zerop in)) | 771 (or (and (zerop ci) (zerop in)) |
| 656 (= lep (line-end-position)) ; on initial line | 772 (= lep (line-end-position)) ; on initial line |
| 657 (and def-line (= in ci)) | 773 ;; Not sure why it was like this -- fails in case of |
| 658 (< in ci))) | 774 ;; last internal function followed by first |
| 775 ;; non-def statement of the main body. | |
| 776 ;; (and def-line (= in ci)) | |
| 777 (= in ci) | |
| 778 (< in ci))) | |
| 659 (not (python-in-string/comment))) | 779 (not (python-in-string/comment))) |
| 660 (setq found t))))) | 780 (setq found t))))) |
| 661 | 781 |
| 662 (defun python-end-of-defun () | 782 (defun python-end-of-defun () |
| 663 "`end-of-defun-function' for Python. | 783 "`end-of-defun-function' for Python. |
| 664 Finds end of innermost nested class or method definition." | 784 Finds end of innermost nested class or method definition." |
| 665 (let ((orig (point)) | 785 (let ((orig (point)) |
| 666 (pattern (rx (and line-start (0+ space) (or "def" "class") space)))) | 786 (pattern (rx line-start (0+ space) (or "def" "class") space))) |
| 667 ;; Go to start of current block and check whether it's at top | 787 ;; Go to start of current block and check whether it's at top |
| 668 ;; level. If it is, and not a block start, look forward for | 788 ;; level. If it is, and not a block start, look forward for |
| 669 ;; definition statement. | 789 ;; definition statement. |
| 670 (when (python-comment-line-p) | 790 (when (python-comment-line-p) |
| 671 (end-of-line) | 791 (end-of-line) |
| 690 ;; We're at a definition statement (or end-of-buffer). | 810 ;; We're at a definition statement (or end-of-buffer). |
| 691 (unless (eobp) | 811 (unless (eobp) |
| 692 (python-end-of-block) | 812 (python-end-of-block) |
| 693 ;; Count trailing space in defun (but not trailing comments). | 813 ;; Count trailing space in defun (but not trailing comments). |
| 694 (skip-syntax-forward " >") | 814 (skip-syntax-forward " >") |
| 695 (beginning-of-line)) | 815 (unless (eobp) ; e.g. missing final newline |
| 696 ;; Catch pathological case like this, where the beginning-of-defun | 816 (beginning-of-line))) |
| 817 ;; Catch pathological cases like this, where the beginning-of-defun | |
| 697 ;; skips to a definition we're not in: | 818 ;; skips to a definition we're not in: |
| 698 ;; if ...: | 819 ;; if ...: |
| 699 ;; ... | 820 ;; ... |
| 700 ;; else: | 821 ;; else: |
| 701 ;; ... # point here | 822 ;; ... # point here |
| 704 (if (< (point) orig) | 825 (if (< (point) orig) |
| 705 (goto-char (point-max))))) | 826 (goto-char (point-max))))) |
| 706 | 827 |
| 707 (defun python-beginning-of-statement () | 828 (defun python-beginning-of-statement () |
| 708 "Go to start of current statement. | 829 "Go to start of current statement. |
| 709 Accounts for continuation lines, multi-line strings, and multi-line bracketed | 830 Accounts for continuation lines, multi-line strings, and |
| 710 expressions." | 831 multi-line bracketed expressions." |
| 711 (beginning-of-line) | 832 (beginning-of-line) |
| 712 (python-beginning-of-string) | 833 (python-beginning-of-string) |
| 713 (catch 'foo | 834 (while (python-continuation-line-p) |
| 714 (while (python-continuation-line-p) | 835 (beginning-of-line) |
| 715 (beginning-of-line) | 836 (if (python-backslash-continuation-line-p) |
| 716 (if (python-backslash-continuation-line-p) | 837 (progn |
| 838 (forward-line -1) | |
| 717 (while (python-backslash-continuation-line-p) | 839 (while (python-backslash-continuation-line-p) |
| 718 (forward-line -1)) | 840 (forward-line -1))) |
| 719 (python-beginning-of-string) | 841 (python-beginning-of-string) |
| 720 ;; Skip forward out of nested brackets. | 842 (python-skip-out))) |
| 721 (condition-case () ; beware invalid syntax | |
| 722 (let ((depth (syntax-ppss-depth (syntax-ppss)))) | |
| 723 ;; Beware negative depths. | |
| 724 (if (> depth 0) (backward-up-list depth)) | |
| 725 t) | |
| 726 (error (throw 'foo nil)))))) | |
| 727 (back-to-indentation)) | 843 (back-to-indentation)) |
| 844 | |
| 845 (defun python-skip-out (&optional forward syntax) | |
| 846 "Skip out of any nested brackets. | |
| 847 Skip forward if FORWARD is non-nil, else backward. | |
| 848 If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point. | |
| 849 Return non-nil iff skipping was done." | |
| 850 (let ((depth (syntax-ppss-depth (or syntax (syntax-ppss)))) | |
| 851 (forward (if forward -1 1))) | |
| 852 (unless (zerop depth) | |
| 853 (if (> depth 0) | |
| 854 ;; Skip forward out of nested brackets. | |
| 855 (condition-case () ; beware invalid syntax | |
| 856 (progn (backward-up-list (* forward depth)) t) | |
| 857 (error nil)) | |
| 858 ;; Invalid syntax (too many closed brackets). | |
| 859 ;; Skip out of as many as possible. | |
| 860 (let (done) | |
| 861 (while (condition-case () | |
| 862 (progn (backward-up-list forward) | |
| 863 (setq done t)) | |
| 864 (error nil))) | |
| 865 done))))) | |
| 728 | 866 |
| 729 (defun python-end-of-statement () | 867 (defun python-end-of-statement () |
| 730 "Go to the end of the current statement and return point. | 868 "Go to the end of the current statement and return point. |
| 731 Usually this is the start of the next line, but if this is a | 869 Usually this is the start of the next line, but if this is a |
| 732 multi-line statement we need to skip over the continuation lines. | 870 multi-line statement we need to skip over the continuation lines. |
| 743 ;; Go to start of string and skip it. | 881 ;; Go to start of string and skip it. |
| 744 (goto-char (nth 8 s)) | 882 (goto-char (nth 8 s)) |
| 745 (condition-case () ; beware invalid syntax | 883 (condition-case () ; beware invalid syntax |
| 746 (progn (forward-sexp) t) | 884 (progn (forward-sexp) t) |
| 747 (error (end-of-line)))) | 885 (error (end-of-line)))) |
| 748 ((> (syntax-ppss-depth s) 0) | 886 ((python-skip-out t s)))) |
| 749 ;; Skip forward out of nested brackets. | |
| 750 (condition-case () ; beware invalid syntax | |
| 751 (progn (backward-up-list | |
| 752 (- (syntax-ppss-depth s))) | |
| 753 t) | |
| 754 (error (end-of-line)))))) | |
| 755 (end-of-line)) | 887 (end-of-line)) |
| 756 (unless comment | 888 (unless comment |
| 757 (eq ?\\ (char-before)))) ; Line continued? | 889 (eq ?\\ (char-before)))) ; Line continued? |
| 758 (end-of-line 2)) ; Try next line. | 890 (end-of-line 2)) ; Try next line. |
| 759 (point)) | 891 (point)) |
| 783 (python-previous-statement (- count)) | 915 (python-previous-statement (- count)) |
| 784 (beginning-of-line) | 916 (beginning-of-line) |
| 785 (while (and (> count 0) (not (eobp))) | 917 (while (and (> count 0) (not (eobp))) |
| 786 (python-end-of-statement) | 918 (python-end-of-statement) |
| 787 (python-skip-comments/blanks) | 919 (python-skip-comments/blanks) |
| 788 (setq count (1- count))) | 920 (unless (eobp) |
| 921 (setq count (1- count)))) | |
| 789 count)) | 922 count)) |
| 790 | 923 |
| 791 (defun python-beginning-of-block (&optional arg) | 924 (defun python-beginning-of-block (&optional arg) |
| 792 "Go to start of current block. | 925 "Go to start of current block. |
| 793 With numeric arg, do it that many times. If ARG is negative, call | 926 With numeric arg, do it that many times. If ARG is negative, call |
| 800 (cond | 933 (cond |
| 801 ((zerop arg)) | 934 ((zerop arg)) |
| 802 ((< arg 0) (python-end-of-block (- arg))) | 935 ((< arg 0) (python-end-of-block (- arg))) |
| 803 (t | 936 (t |
| 804 (let ((point (point))) | 937 (let ((point (point))) |
| 805 (if (python-comment-line-p) | 938 (if (or (python-comment-line-p) |
| 939 (python-blank-line-p)) | |
| 806 (python-skip-comments/blanks t)) | 940 (python-skip-comments/blanks t)) |
| 807 (python-beginning-of-statement) | 941 (python-beginning-of-statement) |
| 808 (let ((ci (current-indentation))) | 942 (let ((ci (current-indentation))) |
| 809 (if (zerop ci) | 943 (if (zerop ci) |
| 810 (not (goto-char point)) ; return nil | 944 (not (goto-char point)) ; return nil |
| 828 (not (goto-char point))) ; Failed -- return nil | 962 (not (goto-char point))) ; Failed -- return nil |
| 829 (python-beginning-of-block (1- arg))))))))) | 963 (python-beginning-of-block (1- arg))))))))) |
| 830 | 964 |
| 831 (defun python-end-of-block (&optional arg) | 965 (defun python-end-of-block (&optional arg) |
| 832 "Go to end of current block. | 966 "Go to end of current block. |
| 833 With numeric arg, do it that many times. If ARG is negative, call | 967 With numeric arg, do it that many times. If ARG is negative, |
| 834 `python-beginning-of-block' instead. | 968 call `python-beginning-of-block' instead. |
| 835 If current statement is in column zero and doesn't open a block, don't | 969 If current statement is in column zero and doesn't open a block, |
| 836 move and return nil. Otherwise return t." | 970 don't move and return nil. Otherwise return t." |
| 837 (interactive "p") | 971 (interactive "p") |
| 838 (unless arg (setq arg 1)) | 972 (unless arg (setq arg 1)) |
| 839 (if (< arg 0) | 973 (if (< arg 0) |
| 840 (python-beginning-of-block (- arg))) | 974 (python-beginning-of-block (- arg)) |
| 841 (while (and (> arg 0) | 975 (while (and (> arg 0) |
| 842 (let* ((point (point)) | 976 (let* ((point (point)) |
| 843 (_ (if (python-comment-line-p) | 977 (_ (if (python-comment-line-p) |
| 844 (python-skip-comments/blanks t))) | 978 (python-skip-comments/blanks t))) |
| 845 (ci (current-indentation)) | 979 (ci (current-indentation)) |
| 846 (open (python-open-block-statement-p))) | 980 (open (python-open-block-statement-p))) |
| 847 (if (and (zerop ci) (not open)) | 981 (if (and (zerop ci) (not open)) |
| 848 (not (goto-char point)) | 982 (not (goto-char point)) |
| 849 (catch 'done | 983 (catch 'done |
| 850 (while (zerop (python-next-statement)) | 984 (while (zerop (python-next-statement)) |
| 851 (when (or (and open (<= (current-indentation) ci)) | 985 (when (or (and open (<= (current-indentation) ci)) |
| 852 (< (current-indentation) ci)) | 986 (< (current-indentation) ci)) |
| 853 (python-skip-comments/blanks t) | 987 (python-skip-comments/blanks t) |
| 854 (beginning-of-line 2) | 988 (beginning-of-line 2) |
| 855 (throw 'done t))) | 989 (throw 'done t))))))) |
| 856 (not (goto-char point)))))) | 990 (setq arg (1- arg))) |
| 857 (setq arg (1- arg))) | 991 (zerop arg))) |
| 858 (zerop arg)) | |
| 859 | 992 |
| 860 ;;;; Imenu. | 993 ;;;; Imenu. |
| 861 | 994 |
| 862 (defvar python-recursing) | 995 (defvar python-recursing) |
| 863 (defun python-imenu-create-index () | 996 (defun python-imenu-create-index () |
| 866 Makes nested Imenu menus from nested `class' and `def' statements. | 999 Makes nested Imenu menus from nested `class' and `def' statements. |
| 867 The nested menus are headed by an item referencing the outer | 1000 The nested menus are headed by an item referencing the outer |
| 868 definition; it has a space prepended to the name so that it sorts | 1001 definition; it has a space prepended to the name so that it sorts |
| 869 first with `imenu--sort-by-name' (though, unfortunately, sub-menus | 1002 first with `imenu--sort-by-name' (though, unfortunately, sub-menus |
| 870 precede it)." | 1003 precede it)." |
| 871 (unless (boundp 'python-recursing) ; dynamically bound below | 1004 (unless (boundp 'python-recursing) ; dynamically bound below |
| 872 (goto-char (point-min))) ; normal call from Imenu | 1005 ;; Normal call from Imenu. |
| 873 (let (index-alist ; accumulated value to return | 1006 (goto-char (point-min)) |
| 874 name) | 1007 ;; Without this, we can get an infloop if the buffer isn't all |
| 1008 ;; fontified. I guess this is really a bug in syntax.el. OTOH, | |
| 1009 ;; _with_ this, imenu doesn't immediately work; I can't figure out | |
| 1010 ;; what's going on, but it must be something to do with timers in | |
| 1011 ;; font-lock. | |
| 1012 ;; This can't be right, especially not when jit-lock is not used. --Stef | |
| 1013 ;; (unless (get-text-property (1- (point-max)) 'fontified) | |
| 1014 ;; (font-lock-fontify-region (point-min) (point-max))) | |
| 1015 ) | |
| 1016 (let (index-alist) ; accumulated value to return | |
| 875 (while (re-search-forward | 1017 (while (re-search-forward |
| 876 (rx (and line-start (0+ space) ; leading space | 1018 (rx line-start (0+ space) ; leading space |
| 877 (or (group "def") (group "class")) ; type | 1019 (or (group "def") (group "class")) ; type |
| 878 (1+ space) (group (1+ (or word ?_))))) ; name | 1020 (1+ space) (group (1+ (or word ?_)))) ; name |
| 879 nil t) | 1021 nil t) |
| 880 (unless (python-in-string/comment) | 1022 (unless (python-in-string/comment) |
| 881 (let ((pos (match-beginning 0)) | 1023 (let ((pos (match-beginning 0)) |
| 882 (name (match-string-no-properties 3))) | 1024 (name (match-string-no-properties 3))) |
| 883 (if (match-beginning 2) ; def or class? | 1025 (if (match-beginning 2) ; def or class? |
| 888 (sublist (python-imenu-create-index))) | 1030 (sublist (python-imenu-create-index))) |
| 889 (if sublist | 1031 (if sublist |
| 890 (progn (push (cons (concat " " name) pos) sublist) | 1032 (progn (push (cons (concat " " name) pos) sublist) |
| 891 (push (cons name sublist) index-alist)) | 1033 (push (cons name sublist) index-alist)) |
| 892 (push (cons name pos) index-alist))))))) | 1034 (push (cons name pos) index-alist))))))) |
| 893 (nreverse index-alist))) | 1035 (unless (boundp 'python-recursing) |
| 1036 ;; Look for module variables. | |
| 1037 (let (vars) | |
| 1038 (goto-char (point-min)) | |
| 1039 (while (re-search-forward | |
| 1040 (rx line-start (group (1+ (or word ?_))) (0+ space) "=") | |
| 1041 nil t) | |
| 1042 (unless (python-in-string/comment) | |
| 1043 (push (cons (match-string 1) (match-beginning 1)) | |
| 1044 vars))) | |
| 1045 (setq index-alist (nreverse index-alist)) | |
| 1046 (if vars | |
| 1047 (push (cons "Module variables" | |
| 1048 (nreverse vars)) | |
| 1049 index-alist)))) | |
| 1050 index-alist)) | |
| 894 | 1051 |
| 895 ;;;; `Electric' commands. | 1052 ;;;; `Electric' commands. |
| 896 | 1053 |
| 897 (defun python-electric-colon (arg) | 1054 (defun python-electric-colon (arg) |
| 898 "Insert a colon and maybe outdent the line if it is a statement like `else'. | 1055 "Insert a colon and maybe outdent the line if it is a statement like `else'. |
| 908 (python-indent-line))) ; OK, do it | 1065 (python-indent-line))) ; OK, do it |
| 909 (put 'python-electric-colon 'delete-selection t) | 1066 (put 'python-electric-colon 'delete-selection t) |
| 910 | 1067 |
| 911 (defun python-backspace (arg) | 1068 (defun python-backspace (arg) |
| 912 "Maybe delete a level of indentation on the current line. | 1069 "Maybe delete a level of indentation on the current line. |
| 913 If not at the end of line's indentation, or on a comment line, just call | 1070 Do so if point is at the end of the line's indentation. |
| 914 `backward-delete-char-untabify'. With ARG, repeat that many times." | 1071 Otherwise just call `backward-delete-char-untabify'. |
| 1072 Repeat ARG times." | |
| 915 (interactive "*p") | 1073 (interactive "*p") |
| 916 (if (or (/= (current-indentation) (current-column)) | 1074 (if (or (/= (current-indentation) (current-column)) |
| 917 (bolp) | 1075 (bolp) |
| 918 (python-continuation-line-p)) | 1076 (python-continuation-line-p)) |
| 919 (backward-delete-char-untabify arg) | 1077 (backward-delete-char-untabify arg) |
| 920 (let ((indent 0)) | 1078 ;; Look for the largest valid indentation which is smaller than |
| 921 (save-excursion | 1079 ;; the current indentation. |
| 922 (while (and (> arg 0) (python-beginning-of-block)) | 1080 (let ((indent 0) |
| 923 (setq arg (1- arg))) | 1081 (ci (current-indentation)) |
| 924 (when (zerop arg) | 1082 (indents (python-indentation-levels)) |
| 925 (setq indent (current-indentation)) | 1083 initial) |
| 926 (message "Closes %s" (python-initial-text)))) | 1084 (dolist (x indents) |
| 1085 (if (< (car x) ci) | |
| 1086 (setq indent (max indent (car x))))) | |
| 1087 (setq initial (cdr (assq indent indents))) | |
| 1088 (if (> (length initial) 0) | |
| 1089 (message "Closes %s" initial)) | |
| 927 (delete-horizontal-space) | 1090 (delete-horizontal-space) |
| 928 (indent-to indent)))) | 1091 (indent-to indent)))) |
| 929 (put 'python-backspace 'delete-selection 'supersede) | 1092 (put 'python-backspace 'delete-selection 'supersede) |
| 930 | 1093 |
| 931 ;;;; pychecker | 1094 ;;;; pychecker |
| 963 ;; Fixme: Make sure we can work with IPython. | 1126 ;; Fixme: Make sure we can work with IPython. |
| 964 | 1127 |
| 965 (defcustom python-python-command "python" | 1128 (defcustom python-python-command "python" |
| 966 "*Shell command to run Python interpreter. | 1129 "*Shell command to run Python interpreter. |
| 967 Any arguments can't contain whitespace. | 1130 Any arguments can't contain whitespace. |
| 968 Note that IPython may not work properly; it must at least be used with the | 1131 Note that IPython may not work properly; it must at least be used |
| 969 `-cl' flag, i.e. use `ipython -cl'." | 1132 with the `-cl' flag, i.e. use `ipython -cl'." |
| 970 :group 'python | 1133 :group 'python |
| 971 :type 'string) | 1134 :type 'string) |
| 972 | 1135 |
| 973 (defcustom python-jython-command "jython" | 1136 (defcustom python-jython-command "jython" |
| 974 "*Shell command to run Jython interpreter. | 1137 "*Shell command to run Jython interpreter. |
| 976 :group 'python | 1139 :group 'python |
| 977 :type 'string) | 1140 :type 'string) |
| 978 | 1141 |
| 979 (defvar python-command python-python-command | 1142 (defvar python-command python-python-command |
| 980 "Actual command used to run Python. | 1143 "Actual command used to run Python. |
| 981 May be `python-python-command' or `python-jython-command'. | 1144 May be `python-python-command' or `python-jython-command', possibly |
| 982 Additional arguments are added when the command is used by `run-python' | 1145 modified by the user. Additional arguments are added when the command |
| 983 et al.") | 1146 is used by `run-python' et al.") |
| 984 | 1147 |
| 985 (defvar python-buffer nil | 1148 (defvar python-buffer nil |
| 986 "The current python process buffer." | 1149 "*The current python process buffer. |
| 987 ;; Fixme: a single process is currently assumed, so that this doc | 1150 |
| 988 ;; is misleading. | 1151 Commands that send text from source buffers to Python processes have |
| 989 | 1152 to choose a process to send to. This is determined by buffer-local |
| 990 ;; "*The current python process buffer. | 1153 value of `python-buffer'. If its value in the current buffer, |
| 991 ;; To run multiple Python processes, start the first with \\[run-python]. | 1154 i.e. both any local value and the default one, is nil, `run-python' |
| 992 ;; It will be in a buffer named *Python*. Rename that with | 1155 and commands that send to the Python process will start a new process. |
| 993 ;; \\[rename-buffer]. Now start a new process with \\[run-python]. It | 1156 |
| 994 ;; will be in a new buffer, named *Python*. Switch between the different | 1157 Whenever \\[run-python] starts a new process, it resets the default |
| 995 ;; process buffers with \\[switch-to-buffer]. | 1158 value of `python-buffer' to be the new process's buffer and sets the |
| 996 | 1159 buffer-local value similarly if the current buffer is in Python mode |
| 997 ;; Commands that send text from source buffers to Python processes have | 1160 or Inferior Python mode, so that source buffer stays associated with a |
| 998 ;; to choose a process to send to. This is determined by global variable | 1161 specific sub-process. |
| 999 ;; `python-buffer'. Suppose you have three inferior Pythons running: | 1162 |
| 1000 ;; Buffer Process | 1163 Use \\[python-set-proc] to set the default value from a buffer with a |
| 1001 ;; foo python | 1164 local value.") |
| 1002 ;; bar python<2> | 1165 (make-variable-buffer-local 'python-buffer) |
| 1003 ;; *Python* python<3> | |
| 1004 ;; If you do a \\[python-send-region-and-go] command on some Python source | |
| 1005 ;; code, what process does it go to? | |
| 1006 | |
| 1007 ;; - In a process buffer (foo, bar, or *Python*), send it to that process. | |
| 1008 ;; - In some other buffer (e.g. a source file), send it to the process | |
| 1009 ;; attached to `python-buffer'. | |
| 1010 ;; Process selection is done by function `python-proc'. | |
| 1011 | |
| 1012 ;; Whenever \\[run-python] starts a new process, it resets `python-buffer' | |
| 1013 ;; to be the new process's buffer. If you only run one process, this will | |
| 1014 ;; do the right thing. If you run multiple processes, you can change | |
| 1015 ;; `python-buffer' to another process buffer with \\[set-variable]." | |
| 1016 ) | |
| 1017 | 1166 |
| 1018 (defconst python-compilation-regexp-alist | 1167 (defconst python-compilation-regexp-alist |
| 1019 ;; FIXME: maybe these should move to compilation-error-regexp-alist-alist. | 1168 ;; FIXME: maybe these should move to compilation-error-regexp-alist-alist. |
| 1020 `((,(rx (and line-start (1+ (any " \t")) "File \"" | 1169 ;; The first already is (for CAML), but the second isn't. Anyhow, |
| 1021 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c | 1170 ;; these are specific to the inferior buffer. -- fx |
| 1022 "\", line " (group (1+ digit)))) | 1171 `((,(rx line-start (1+ (any " \t")) "File \"" |
| 1172 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c | |
| 1173 "\", line " (group (1+ digit))) | |
| 1023 1 2) | 1174 1 2) |
| 1024 (,(rx (and " in file " (group (1+ not-newline)) " on line " | 1175 (,(rx " in file " (group (1+ not-newline)) " on line " |
| 1025 (group (1+ digit)))) | 1176 (group (1+ digit))) |
| 1026 1 2)) | 1177 1 2)) |
| 1027 "`compilation-error-regexp-alist' for inferior Python.") | 1178 "`compilation-error-regexp-alist' for inferior Python.") |
| 1028 | 1179 |
| 1029 (defvar inferior-python-mode-map | 1180 (defvar inferior-python-mode-map |
| 1030 (let ((map (make-sparse-keymap))) | 1181 (let ((map (make-sparse-keymap))) |
| 1038 | 1189 |
| 1039 ;; (define-key map [(meta ?\t)] 'python-complete-symbol) | 1190 ;; (define-key map [(meta ?\t)] 'python-complete-symbol) |
| 1040 ;; (define-key map "\C-c\C-f" 'python-describe-symbol) | 1191 ;; (define-key map "\C-c\C-f" 'python-describe-symbol) |
| 1041 map)) | 1192 map)) |
| 1042 | 1193 |
| 1043 ;; Fixme: This should inherit some stuff from python-mode, but I'm not | 1194 ;; Fixme: This should inherit some stuff from `python-mode', but I'm |
| 1044 ;; sure how much: at least some keybindings, like C-c C-f; syntax?; | 1195 ;; not sure how much: at least some keybindings, like C-c C-f; |
| 1045 ;; font-locking, e.g. for triple-quoted strings? | 1196 ;; syntax?; font-locking, e.g. for triple-quoted strings? |
| 1046 (define-derived-mode inferior-python-mode comint-mode "Inferior Python" | 1197 (define-derived-mode inferior-python-mode comint-mode "Inferior Python" |
| 1047 "Major mode for interacting with an inferior Python process. | 1198 "Major mode for interacting with an inferior Python process. |
| 1048 A Python process can be started with \\[run-python]. | 1199 A Python process can be started with \\[run-python]. |
| 1049 | 1200 |
| 1050 Hooks `comint-mode-hook' and `inferior-python-mode-hook' are run in | 1201 Hooks `comint-mode-hook' and `inferior-python-mode-hook' are run in |
| 1051 that order. | 1202 that order. |
| 1052 | 1203 |
| 1053 You can send text to the inferior Python process from other buffers containing | 1204 You can send text to the inferior Python process from other buffers |
| 1054 Python source. | 1205 containing Python source. |
| 1055 * `python-switch-to-python' switches the current buffer to the Python | 1206 * \\[python-switch-to-python] switches the current buffer to the Python |
| 1056 process buffer. | 1207 process buffer. |
| 1057 * `python-send-region' sends the current region to the Python process. | 1208 * \\[python-send-region] sends the current region to the Python process. |
| 1058 * `python-send-region-and-go' switches to the Python process buffer | 1209 * \\[python-send-region-and-go] switches to the Python process buffer |
| 1059 after sending the text. | 1210 after sending the text. |
| 1060 For running multiple processes in multiple buffers, see `python-buffer'. | 1211 For running multiple processes in multiple buffers, see `run-python' and |
| 1212 `python-buffer'. | |
| 1061 | 1213 |
| 1062 \\{inferior-python-mode-map}" | 1214 \\{inferior-python-mode-map}" |
| 1063 :group 'python | 1215 :group 'python |
| 1064 (set-syntax-table python-mode-syntax-table) | 1216 (set-syntax-table python-mode-syntax-table) |
| 1065 (setq mode-line-process '(":%s")) | 1217 (setq mode-line-process '(":%s")) |
| 1067 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter | 1219 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter |
| 1068 nil t) | 1220 nil t) |
| 1069 ;; Still required by `comint-redirect-send-command', for instance | 1221 ;; Still required by `comint-redirect-send-command', for instance |
| 1070 ;; (and we need to match things like `>>> ... >>> '): | 1222 ;; (and we need to match things like `>>> ... >>> '): |
| 1071 (set (make-local-variable 'comint-prompt-regexp) | 1223 (set (make-local-variable 'comint-prompt-regexp) |
| 1072 (rx (and line-start (1+ (and (repeat 3 (any ">.")) ?\s))))) | 1224 (rx line-start (1+ (and (repeat 3 (any ">.")) " ")))) |
| 1073 (set (make-local-variable 'compilation-error-regexp-alist) | 1225 (set (make-local-variable 'compilation-error-regexp-alist) |
| 1074 python-compilation-regexp-alist) | 1226 python-compilation-regexp-alist) |
| 1075 (compilation-shell-minor-mode 1)) | 1227 (compilation-shell-minor-mode 1)) |
| 1076 | 1228 |
| 1077 (defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'" | 1229 (defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'" |
| 1110 (defun python-preoutput-filter (s) | 1262 (defun python-preoutput-filter (s) |
| 1111 "`comint-preoutput-filter-functions' function: ignore prompts not at bol." | 1263 "`comint-preoutput-filter-functions' function: ignore prompts not at bol." |
| 1112 (when python-preoutput-leftover | 1264 (when python-preoutput-leftover |
| 1113 (setq s (concat python-preoutput-leftover s)) | 1265 (setq s (concat python-preoutput-leftover s)) |
| 1114 (setq python-preoutput-leftover nil)) | 1266 (setq python-preoutput-leftover nil)) |
| 1115 (cond ((and (string-match (rx (and string-start (repeat 3 (any ".>")) | 1267 (cond ((and (string-match (rx string-start (repeat 3 (any ".>")) |
| 1116 " " string-end)) | 1268 " " string-end) |
| 1117 s) | 1269 s) |
| 1118 (/= (let ((inhibit-field-text-motion t)) | 1270 (/= (let ((inhibit-field-text-motion t)) |
| 1119 (line-beginning-position)) | 1271 (line-beginning-position)) |
| 1120 (point))) | 1272 (point))) |
| 1273 ;; The need for this seems to be system-dependent: | |
| 1274 ;; What is this all about, exactly? --Stef | |
| 1275 ;; (if (and (eq ?. (aref s 0))) | |
| 1276 ;; (accept-process-output (get-buffer-process (current-buffer)) 1)) | |
| 1121 "") | 1277 "") |
| 1122 ((string= s "_emacs_ok\n") | 1278 ((string= s "_emacs_ok\n") |
| 1123 (when python-preoutput-continuation | 1279 (when python-preoutput-continuation |
| 1124 (funcall python-preoutput-continuation) | 1280 (funcall python-preoutput-continuation) |
| 1125 (setq python-preoutput-continuation nil)) | 1281 (setq python-preoutput-continuation nil)) |
| 1134 (eq t (compare-strings s nil end "_emacs_out " nil end)))) | 1290 (eq t (compare-strings s nil end "_emacs_out " nil end)))) |
| 1135 (setq python-preoutput-leftover s) | 1291 (setq python-preoutput-leftover s) |
| 1136 "") | 1292 "") |
| 1137 (t s))) | 1293 (t s))) |
| 1138 | 1294 |
| 1295 (autoload 'comint-check-proc "comint") | |
| 1296 | |
| 1139 ;;;###autoload | 1297 ;;;###autoload |
| 1140 (defun run-python (&optional cmd noshow) | 1298 (defun run-python (&optional cmd noshow new) |
| 1141 "Run an inferior Python process, input and output via buffer *Python*. | 1299 "Run an inferior Python process, input and output via buffer *Python*. |
| 1142 CMD is the Python command to run. NOSHOW non-nil means don't show the | 1300 CMD is the Python command to run. NOSHOW non-nil means don't show the |
| 1143 buffer automatically. | 1301 buffer automatically. |
| 1144 If there is a process already running in `*Python*', switch to | 1302 |
| 1145 that buffer. Interactively, a prefix arg allows you to edit the initial | 1303 Normally, if there is a process already running in `python-buffer', |
| 1146 command line (default is `python-command'); `-i' etc. args will be added | 1304 switch to that buffer. Interactively, a prefix arg allows you to edit |
| 1147 to this as appropriate. Runs the hook `inferior-python-mode-hook' | 1305 the initial command line (default is `python-command'); `-i' etc. args |
| 1148 \(after the `comint-mode-hook' is run). | 1306 will be added to this as appropriate. A new process is started if: |
| 1149 \(Type \\[describe-mode] in the process buffer for a list of commands.)" | 1307 one isn't running attached to `python-buffer', or interactively the |
| 1150 (interactive (list (if current-prefix-arg | 1308 default `python-command', or argument NEW is non-nil. See also the |
| 1151 (read-string "Run Python: " python-command) | 1309 documentation for `python-buffer'. |
| 1152 python-command))) | 1310 |
| 1311 Runs the hook `inferior-python-mode-hook' \(after the | |
| 1312 `comint-mode-hook' is run). \(Type \\[describe-mode] in the process | |
| 1313 buffer for a list of commands.)" | |
| 1314 (interactive (if current-prefix-arg | |
| 1315 (list (read-string "Run Python: " python-command) nil t) | |
| 1316 (list python-command))) | |
| 1153 (unless cmd (setq cmd python-python-command)) | 1317 (unless cmd (setq cmd python-python-command)) |
| 1154 (setq python-command cmd) | 1318 (setq python-command cmd) |
| 1155 ;; Fixme: Consider making `python-buffer' buffer-local as a buffer | 1319 ;; Fixme: Consider making `python-buffer' buffer-local as a buffer |
| 1156 ;; (not a name) in Python buffers from which `run-python' &c is | 1320 ;; (not a name) in Python buffers from which `run-python' &c is |
| 1157 ;; invoked. Would support multiple processes better. | 1321 ;; invoked. Would support multiple processes better. |
| 1158 (unless (comint-check-proc python-buffer) | 1322 (when (or new (not (comint-check-proc python-buffer))) |
| 1159 (let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) | 1323 (save-current-buffer |
| 1160 (path (getenv "PYTHONPATH")) | 1324 (let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) |
| 1161 (process-environment ; to import emacs.py | 1325 (path (getenv "PYTHONPATH")) |
| 1162 (cons (concat "PYTHONPATH=" data-directory | 1326 (process-environment ; to import emacs.py |
| 1163 (if path (concat ":" path))) | 1327 (cons (concat "PYTHONPATH=" data-directory |
| 1164 process-environment))) | 1328 (if path (concat ":" path))) |
| 1165 (set-buffer (apply 'make-comint "Python" (car cmdlist) nil | 1329 process-environment))) |
| 1166 (cdr cmdlist))) | 1330 (set-buffer (apply 'make-comint-in-buffer "Python" |
| 1167 (setq python-buffer (buffer-name))) | 1331 (generate-new-buffer "*Python*") |
| 1168 (inferior-python-mode) | 1332 (car cmdlist) nil (cdr cmdlist))) |
| 1169 ;; Load function defintions we need. | 1333 (setq-default python-buffer (current-buffer)) |
| 1170 ;; Before the preoutput function was used, this was done via -c in | 1334 (setq python-buffer (current-buffer))) |
| 1171 ;; cmdlist, but that loses the banner and doesn't run the startup | 1335 (accept-process-output (get-buffer-process python-buffer) 5) |
| 1172 ;; file. The code might be inline here, but there's enough that it | 1336 (inferior-python-mode))) |
| 1173 ;; seems worth putting in a separate file, and it's probably cleaner | 1337 (if (eq 'python-mode major-mode) |
| 1174 ;; to put it in a module. | 1338 (setq python-buffer (default-value 'python-buffer))) ; buffer-local |
| 1175 (python-send-string "import emacs")) | 1339 ;; Load function definitions we need. |
| 1176 (unless noshow (pop-to-buffer python-buffer))) | 1340 ;; Before the preoutput function was used, this was done via -c in |
| 1341 ;; cmdlist, but that loses the banner and doesn't run the startup | |
| 1342 ;; file. The code might be inline here, but there's enough that it | |
| 1343 ;; seems worth putting in a separate file, and it's probably cleaner | |
| 1344 ;; to put it in a module. | |
| 1345 (python-send-string "import emacs") | |
| 1346 ;; Ensure we're at a prompt before doing anything else. | |
| 1347 (python-send-receive "print '_emacs_out ()'") | |
| 1348 ;; Without this, help output goes into the inferior python buffer if | |
| 1349 ;; the process isn't already running. | |
| 1350 (sit-for 1 t) ;Should we use accept-process-output instead? --Stef | |
| 1351 (unless noshow (pop-to-buffer python-buffer t))) | |
| 1177 | 1352 |
| 1178 ;; Fixme: We typically lose if the inferior isn't in the normal REPL, | 1353 ;; Fixme: We typically lose if the inferior isn't in the normal REPL, |
| 1179 ;; e.g. prompt is `help> '. Probably raise an error if the form of | 1354 ;; e.g. prompt is `help> '. Probably raise an error if the form of |
| 1180 ;; the prompt is unexpected; actually, it needs to be `>>> ', not | 1355 ;; the prompt is unexpected. Actually, it needs to be `>>> ', not |
| 1181 ;; `... ', i.e. we're not inputting a block &c. However, this may not | 1356 ;; `... ', i.e. we're not inputting a block &c. However, this may not |
| 1182 ;; be the place to do it, e.g. we might actually want to send commands | 1357 ;; be the place to check it, e.g. we might actually want to send |
| 1183 ;; having set up such a state. | 1358 ;; commands having set up such a state. |
| 1184 | 1359 |
| 1185 (defun python-send-command (command) | 1360 (defun python-send-command (command) |
| 1186 "Like `python-send-string' but resets `compilation-minor-mode'." | 1361 "Like `python-send-string' but resets `compilation-shell-minor-mode'." |
| 1187 (goto-char (point-max)) | |
| 1188 (let ((end (marker-position (process-mark (python-proc))))) | 1362 (let ((end (marker-position (process-mark (python-proc))))) |
| 1363 (with-current-buffer python-buffer (goto-char (point-max))) | |
| 1189 (compilation-forget-errors) | 1364 (compilation-forget-errors) |
| 1190 (python-send-string command) | 1365 (python-send-string command) |
| 1191 (set-marker compilation-parsing-end end) | 1366 ;; Must wait until this has completed before re-setting variables below. |
| 1192 (setq compilation-last-buffer (current-buffer)))) | 1367 (python-send-receive "print '_emacs_out ()'") |
| 1368 (with-current-buffer python-buffer | |
| 1369 (set-marker compilation-parsing-end end) | |
| 1370 (setq compilation-last-buffer (current-buffer))))) | |
| 1193 | 1371 |
| 1194 (defun python-send-region (start end) | 1372 (defun python-send-region (start end) |
| 1195 "Send the region to the inferior Python process." | 1373 "Send the region to the inferior Python process." |
| 1196 ;; The region is evaluated from a temporary file. This avoids | 1374 ;; The region is evaluated from a temporary file. This avoids |
| 1197 ;; problems with blank lines, which have different semantics | 1375 ;; problems with blank lines, which have different semantics |
| 1200 ;; function to remove the temporary file when it has been evaluated | 1378 ;; function to remove the temporary file when it has been evaluated |
| 1201 ;; (though we could probably do it in Lisp with a Comint output | 1379 ;; (though we could probably do it in Lisp with a Comint output |
| 1202 ;; filter). This function also catches exceptions and truncates | 1380 ;; filter). This function also catches exceptions and truncates |
| 1203 ;; tracebacks not to mention the frame of the function itself. | 1381 ;; tracebacks not to mention the frame of the function itself. |
| 1204 ;; | 1382 ;; |
| 1205 ;; The compilation-minor-mode parsing takes care of relating the | 1383 ;; The `compilation-shell-minor-mode' parsing takes care of relating |
| 1206 ;; reference to the temporary file to the source. | 1384 ;; the reference to the temporary file to the source. |
| 1207 ;; | 1385 ;; |
| 1208 ;; Fixme: Write a `coding' header to the temp file if the region is | 1386 ;; Fixme: Write a `coding' header to the temp file if the region is |
| 1209 ;; non-ASCII. | 1387 ;; non-ASCII. |
| 1210 (interactive "r") | 1388 (interactive "r") |
| 1211 (let* ((f (make-temp-file "py")) | 1389 (let* ((f (make-temp-file "py")) |
| 1218 (goto-char orig-start) | 1396 (goto-char orig-start) |
| 1219 ;; Wrong if we had indented code at buffer start. | 1397 ;; Wrong if we had indented code at buffer start. |
| 1220 (set-marker orig-start (line-beginning-position 0))) | 1398 (set-marker orig-start (line-beginning-position 0))) |
| 1221 (write-region "if True:\n" nil f nil 'nomsg)) | 1399 (write-region "if True:\n" nil f nil 'nomsg)) |
| 1222 (write-region start end f t 'nomsg) | 1400 (write-region start end f t 'nomsg) |
| 1223 (with-current-buffer (process-buffer (python-proc)) ;Runs python if needed. | 1401 (python-send-command command) |
| 1224 (python-send-command command) | 1402 (with-current-buffer (process-buffer (python-proc)) |
| 1225 ;; Tell compile.el to redirect error locations in file `f' to | 1403 ;; Tell compile.el to redirect error locations in file `f' to |
| 1226 ;; positions past marker `orig-start'. It has to be done *after* | 1404 ;; positions past marker `orig-start'. It has to be done *after* |
| 1227 ;; python-send-command's call to compilation-forget-errors. | 1405 ;; `python-send-command''s call to `compilation-forget-errors'. |
| 1228 (compilation-fake-loc orig-start f)))) | 1406 (compilation-fake-loc orig-start f)))) |
| 1229 | 1407 |
| 1230 (defun python-send-string (string) | 1408 (defun python-send-string (string) |
| 1231 "Evaluate STRING in inferior Python process." | 1409 "Evaluate STRING in inferior Python process." |
| 1232 (interactive "sPython command: ") | 1410 (interactive "sPython command: ") |
| 1245 (interactive) | 1423 (interactive) |
| 1246 (save-excursion (python-send-region (progn (beginning-of-defun) (point)) | 1424 (save-excursion (python-send-region (progn (beginning-of-defun) (point)) |
| 1247 (progn (end-of-defun) (point))))) | 1425 (progn (end-of-defun) (point))))) |
| 1248 | 1426 |
| 1249 (defun python-switch-to-python (eob-p) | 1427 (defun python-switch-to-python (eob-p) |
| 1250 "Switch to the Python process buffer. | 1428 "Switch to the Python process buffer, maybe starting new process. |
| 1251 With prefix arg, position cursor at end of buffer." | 1429 With prefix arg, position cursor at end of buffer." |
| 1252 (interactive "P") | 1430 (interactive "P") |
| 1253 (pop-to-buffer (process-buffer (python-proc))) ;Runs python if needed. | 1431 (pop-to-buffer (process-buffer (python-proc)) t) ;Runs python if needed. |
| 1254 (when eob-p | 1432 (when eob-p |
| 1255 (push-mark) | 1433 (push-mark) |
| 1256 (goto-char (point-max)))) | 1434 (goto-char (point-max)))) |
| 1257 | 1435 |
| 1258 (defun python-send-region-and-go (start end) | 1436 (defun python-send-region-and-go (start end) |
| 1262 (python-send-region start end) | 1440 (python-send-region start end) |
| 1263 (python-switch-to-python t)) | 1441 (python-switch-to-python t)) |
| 1264 | 1442 |
| 1265 (defcustom python-source-modes '(python-mode jython-mode) | 1443 (defcustom python-source-modes '(python-mode jython-mode) |
| 1266 "*Used to determine if a buffer contains Python source code. | 1444 "*Used to determine if a buffer contains Python source code. |
| 1267 If it's loaded into a buffer that is in one of these major modes, it's | 1445 If a file is loaded into a buffer that is in one of these major modes, |
| 1268 considered a Python source file by `python-load-file'. | 1446 it is considered Python source by `python-load-file', which uses the |
| 1269 Used by these commands to determine defaults." | 1447 value to determine defaults." |
| 1270 :type '(repeat function) | 1448 :type '(repeat function) |
| 1271 :group 'python) | 1449 :group 'python) |
| 1272 | 1450 |
| 1273 (defvar python-prev-dir/file nil | 1451 (defvar python-prev-dir/file nil |
| 1274 "Caches (directory . file) pair used in the last `python-load-file' command. | 1452 "Caches (directory . file) pair used in the last `python-load-file' command. |
| 1275 Used for determining the default in the next one.") | 1453 Used for determining the default in the next one.") |
| 1454 | |
| 1455 (autoload 'comint-get-source "comint") | |
| 1276 | 1456 |
| 1277 (defun python-load-file (file-name) | 1457 (defun python-load-file (file-name) |
| 1278 "Load a Python file FILE-NAME into the inferior Python process. | 1458 "Load a Python file FILE-NAME into the inferior Python process. |
| 1279 If the file has extension `.py' import or reload it as a module. | 1459 If the file has extension `.py' import or reload it as a module. |
| 1280 Treating it as a module keeps the global namespace clean, provides | 1460 Treating it as a module keeps the global namespace clean, provides |
| 1295 (format "emacs.eimport(%S,%S)" | 1475 (format "emacs.eimport(%S,%S)" |
| 1296 module (file-name-directory file-name))) | 1476 module (file-name-directory file-name))) |
| 1297 (format "execfile(%S)" file-name))) | 1477 (format "execfile(%S)" file-name))) |
| 1298 (message "%s loaded" file-name))) | 1478 (message "%s loaded" file-name))) |
| 1299 | 1479 |
| 1300 ;; Fixme: If we need to start the process, wait until we've got the OK | |
| 1301 ;; from the startup. | |
| 1302 (defun python-proc () | 1480 (defun python-proc () |
| 1303 "Return the current Python process. | 1481 "Return the current Python process. |
| 1304 See variable `python-buffer'. Starts a new process if necessary." | 1482 See variable `python-buffer'. Starts a new process if necessary." |
| 1305 (or (if python-buffer | 1483 ;; Fixme: Maybe should look for another active process if there |
| 1306 (get-buffer-process (if (eq major-mode 'inferior-python-mode) | 1484 ;; isn't one for `python-buffer'. |
| 1485 (unless (comint-check-proc python-buffer) | |
| 1486 (run-python nil t)) | |
| 1487 (get-buffer-process (or (if (eq major-mode 'inferior-python-mode) | |
| 1307 (current-buffer) | 1488 (current-buffer) |
| 1308 python-buffer))) | 1489 python-buffer)))) |
| 1309 (progn (run-python nil t) | 1490 |
| 1310 (python-proc)))) | 1491 (defun python-set-proc () |
| 1492 "Set the default value of `python-buffer' to correspond to this buffer. | |
| 1493 If the current buffer has a local value of `python-buffer', set the | |
| 1494 default (global) value to that. The associated Python process is | |
| 1495 the one that gets input from \\[python-send-region] et al when used | |
| 1496 in a buffer that doesn't have a local value of `python-buffer'." | |
| 1497 (interactive) | |
| 1498 (if (local-variable-p 'python-buffer) | |
| 1499 (setq-default python-buffer python-buffer) | |
| 1500 (error "No local value of `python-buffer'"))) | |
| 1311 | 1501 |
| 1312 ;;;; Context-sensitive help. | 1502 ;;;; Context-sensitive help. |
| 1313 | 1503 |
| 1314 (defconst python-dotty-syntax-table | 1504 (defconst python-dotty-syntax-table |
| 1315 (let ((table (make-syntax-table))) | 1505 (let ((table (make-syntax-table))) |
| 1320 Otherwise inherits from `python-mode-syntax-table'.") | 1510 Otherwise inherits from `python-mode-syntax-table'.") |
| 1321 | 1511 |
| 1322 (defvar view-return-to-alist) | 1512 (defvar view-return-to-alist) |
| 1323 (eval-when-compile (autoload 'help-buffer "help-fns")) | 1513 (eval-when-compile (autoload 'help-buffer "help-fns")) |
| 1324 | 1514 |
| 1515 (defvar python-imports) ; forward declaration | |
| 1516 | |
| 1325 ;; Fixme: Should this actually be used instead of info-look, i.e. be | 1517 ;; Fixme: Should this actually be used instead of info-look, i.e. be |
| 1326 ;; bound to C-h S? Can we use other pydoc stuff before python 2.2? | 1518 ;; bound to C-h S? [Probably not, since info-look may work in cases |
| 1519 ;; where this doesn't.] | |
| 1327 (defun python-describe-symbol (symbol) | 1520 (defun python-describe-symbol (symbol) |
| 1328 "Get help on SYMBOL using `help'. | 1521 "Get help on SYMBOL using `help'. |
| 1329 Interactively, prompt for symbol. | 1522 Interactively, prompt for symbol. |
| 1330 | 1523 |
| 1331 Symbol may be anything recognized by the interpreter's `help' command -- | 1524 Symbol may be anything recognized by the interpreter's `help' |
| 1332 e.g. `CALLS' -- not just variables in scope. | 1525 command -- e.g. `CALLS' -- not just variables in scope in the |
| 1333 This only works for Python version 2.2 or newer since earlier interpreters | 1526 interpreter. This only works for Python version 2.2 or newer |
| 1334 don't support `help'." | 1527 since earlier interpreters don't support `help'. |
| 1528 | |
| 1529 In some cases where this doesn't find documentation, \\[info-lookup-symbol] | |
| 1530 will." | |
| 1335 ;; Note that we do this in the inferior process, not a separate one, to | 1531 ;; Note that we do this in the inferior process, not a separate one, to |
| 1336 ;; ensure the environment is appropriate. | 1532 ;; ensure the environment is appropriate. |
| 1337 (interactive | 1533 (interactive |
| 1338 (let ((symbol (with-syntax-table python-dotty-syntax-table | 1534 (let ((symbol (with-syntax-table python-dotty-syntax-table |
| 1339 (current-word))) | 1535 (current-word))) |
| 1341 (list (read-string (if symbol | 1537 (list (read-string (if symbol |
| 1342 (format "Describe symbol (default %s): " symbol) | 1538 (format "Describe symbol (default %s): " symbol) |
| 1343 "Describe symbol: ") | 1539 "Describe symbol: ") |
| 1344 nil nil symbol)))) | 1540 nil nil symbol)))) |
| 1345 (if (equal symbol "") (error "No symbol")) | 1541 (if (equal symbol "") (error "No symbol")) |
| 1346 (let* ((func `(lambda () | 1542 ;; Ensure we have a suitable help buffer. |
| 1347 (comint-redirect-send-command | 1543 ;; Fixme: Maybe process `Related help topics' a la help xrefs and |
| 1348 (format "emacs.ehelp(%S, globals(), locals())\n" ,symbol) | 1544 ;; allow C-c C-f in help buffer. |
| 1349 "*Help*" nil)))) | 1545 (let ((temp-buffer-show-hook ; avoid xref stuff |
| 1350 ;; Ensure we have a suitable help buffer. | 1546 (lambda () |
| 1351 ;; Fixme: Maybe process `Related help topics' a la help xrefs and | 1547 (toggle-read-only 1) |
| 1352 ;; allow C-c C-f in help buffer. | 1548 (setq view-return-to-alist |
| 1353 (let ((temp-buffer-show-hook ; avoid xref stuff | 1549 (list (cons (selected-window) help-return-method)))))) |
| 1354 (lambda () | 1550 (with-output-to-temp-buffer (help-buffer) |
| 1355 (toggle-read-only 1) | 1551 (with-current-buffer standard-output |
| 1356 (setq view-return-to-alist | 1552 ;; Fixme: Is this actually useful? |
| 1357 (list (cons (selected-window) help-return-method)))))) | 1553 (help-setup-xref (list 'python-describe-symbol symbol) (interactive-p)) |
| 1358 (help-setup-xref (list 'python-describe-symbol symbol) (interactive-p)) | 1554 (set (make-local-variable 'comint-redirect-subvert-readonly) t) |
| 1359 (with-output-to-temp-buffer (help-buffer) | 1555 (print-help-return-message)))) |
| 1360 (with-current-buffer standard-output | 1556 (comint-redirect-send-command-to-process (format "emacs.ehelp(%S, %s)" |
| 1361 (set (make-local-variable 'comint-redirect-subvert-readonly) t) | 1557 symbol python-imports) |
| 1362 (print-help-return-message)))) | 1558 "*Help*" (python-proc) nil nil)) |
| 1363 (if (and python-buffer (get-buffer python-buffer)) | |
| 1364 (with-current-buffer python-buffer | |
| 1365 (funcall func)) | |
| 1366 (setq python-preoutput-continuation func) | |
| 1367 (run-python nil t)))) | |
| 1368 | 1559 |
| 1369 (add-to-list 'debug-ignored-errors "^No symbol") | 1560 (add-to-list 'debug-ignored-errors "^No symbol") |
| 1370 | 1561 |
| 1371 (defun python-send-receive (string) | 1562 (defun python-send-receive (string) |
| 1372 "Send STRING to inferior Python (if any) and return result. | 1563 "Send STRING to inferior Python (if any) and return result. |
| 1377 (while (progn | 1568 (while (progn |
| 1378 (accept-process-output proc 5) | 1569 (accept-process-output proc 5) |
| 1379 python-preoutput-leftover)) | 1570 python-preoutput-leftover)) |
| 1380 python-preoutput-result)) | 1571 python-preoutput-result)) |
| 1381 | 1572 |
| 1382 ;; Fixme: try to make it work with point in the arglist. Also, is | 1573 ;; Fixme: Is there anything reasonable we can do with random methods? |
| 1383 ;; there anything reasonable we can do with random methods? | |
| 1384 ;; (Currently only works with functions.) | 1574 ;; (Currently only works with functions.) |
| 1385 (defun python-eldoc-function () | 1575 (defun python-eldoc-function () |
| 1386 "`eldoc-print-current-symbol-info' for Python. | 1576 "`eldoc-print-current-symbol-info' for Python. |
| 1387 Only works when point is in a function name, not its arglist, for instance. | 1577 Only works when point is in a function name, not its arg list, for |
| 1388 Assumes an inferior Python is running." | 1578 instance. Assumes an inferior Python is running." |
| 1389 (let ((symbol (with-syntax-table python-dotty-syntax-table | 1579 (let ((symbol (with-syntax-table python-dotty-syntax-table |
| 1390 (current-word)))) | 1580 (current-word)))) |
| 1391 (when symbol | 1581 ;; First try the symbol we're on. |
| 1392 (python-send-receive (format "emacs.eargs(%S)" symbol))))) | 1582 (or (and symbol |
| 1583 (python-send-receive (format "emacs.eargs(%S, %s)" | |
| 1584 symbol python-imports))) | |
| 1585 ;; Try moving to symbol before enclosing parens. | |
| 1586 (let ((s (syntax-ppss))) | |
| 1587 (unless (zerop (car s)) | |
| 1588 (when (eq ?\( (char-after (nth 1 s))) | |
| 1589 (save-excursion | |
| 1590 (goto-char (nth 1 s)) | |
| 1591 (skip-syntax-backward "-") | |
| 1592 (let ((point (point))) | |
| 1593 (skip-chars-backward "a-zA-Z._") | |
| 1594 (if (< (point) point) | |
| 1595 (python-send-receive | |
| 1596 (format "emacs.eargs(%S, %s)" | |
| 1597 (buffer-substring-no-properties (point) point) | |
| 1598 python-imports))))))))))) | |
| 1393 | 1599 |
| 1394 ;;;; Info-look functionality. | 1600 ;;;; Info-look functionality. |
| 1395 | 1601 |
| 1396 (defun python-after-info-look () | 1602 (defun python-after-info-look () |
| 1397 "Set up info-look for Python. | 1603 "Set up info-look for Python. |
| 1441 ("(python-lib)Class-Exception-Object Index" nil "") | 1647 ("(python-lib)Class-Exception-Object Index" nil "") |
| 1442 ("(python-lib)Function-Method-Variable Index" nil "") | 1648 ("(python-lib)Function-Method-Variable Index" nil "") |
| 1443 ("(python-lib)Miscellaneous Index" nil "")))))) | 1649 ("(python-lib)Miscellaneous Index" nil "")))))) |
| 1444 (eval-after-load "info-look" '(python-after-info-look)) | 1650 (eval-after-load "info-look" '(python-after-info-look)) |
| 1445 | 1651 |
| 1446 ;;;; Miscellancy. | 1652 ;;;; Miscellany. |
| 1447 | 1653 |
| 1448 (defcustom python-jython-packages '("java" "javax" "org" "com") | 1654 (defcustom python-jython-packages '("java" "javax" "org" "com") |
| 1449 "Packages implying `jython-mode'. | 1655 "Packages implying `jython-mode'. |
| 1450 If these are imported near the beginning of the buffer, `python-mode' | 1656 If these are imported near the beginning of the buffer, `python-mode' |
| 1451 actually punts to `jython-mode'." | 1657 actually punts to `jython-mode'." |
| 1471 interpreter) | 1677 interpreter) |
| 1472 interpreter-mode-alist)))) | 1678 interpreter-mode-alist)))) |
| 1473 (jython-mode) | 1679 (jython-mode) |
| 1474 (if (catch 'done | 1680 (if (catch 'done |
| 1475 (while (re-search-forward | 1681 (while (re-search-forward |
| 1476 (rx (and line-start (or "import" "from") (1+ space) | 1682 (rx line-start (or "import" "from") (1+ space) |
| 1477 (group (1+ (not (any " \t\n.")))))) | 1683 (group (1+ (not (any " \t\n."))))) |
| 1478 (+ (point-min) 10000) ; Probably not worth customizing. | 1684 (+ (point-min) 10000) ; Probably not worth customizing. |
| 1479 t) | 1685 t) |
| 1480 (if (member (match-string 1) python-jython-packages) | 1686 (if (member (match-string 1) python-jython-packages) |
| 1481 (throw 'done t)))) | 1687 (throw 'done t)))) |
| 1482 (jython-mode))))))) | 1688 (jython-mode))))))) |
| 1560 | 1766 |
| 1561 (defun python-outline-level () | 1767 (defun python-outline-level () |
| 1562 "`outline-level' function for Python mode. | 1768 "`outline-level' function for Python mode. |
| 1563 The level is the number of `python-indent' steps of indentation | 1769 The level is the number of `python-indent' steps of indentation |
| 1564 of current line." | 1770 of current line." |
| 1565 (/ (current-indentation) python-indent)) | 1771 (1+ (/ (current-indentation) python-indent))) |
| 1566 | 1772 |
| 1567 ;; Fixme: Consider top-level assignments, imports, &c. | 1773 ;; Fixme: Consider top-level assignments, imports, &c. |
| 1568 (defun python-current-defun () | 1774 (defun python-current-defun () |
| 1569 "`add-log-current-defun-function' for Python." | 1775 "`add-log-current-defun-function' for Python." |
| 1570 (save-excursion | 1776 (save-excursion |
| 1575 (while (or start (> (current-indentation) 0)) | 1781 (while (or start (> (current-indentation) 0)) |
| 1576 (setq start nil) | 1782 (setq start nil) |
| 1577 (python-beginning-of-block) | 1783 (python-beginning-of-block) |
| 1578 (end-of-line) | 1784 (end-of-line) |
| 1579 (beginning-of-defun) | 1785 (beginning-of-defun) |
| 1580 (if (looking-at (rx (and (0+ space) (or "def" "class") (1+ space) | 1786 (if (looking-at (rx (0+ space) (or "def" "class") (1+ space) |
| 1581 (group (1+ (or word (syntax symbol)))) | 1787 (group (1+ (or word (syntax symbol)))))) |
| 1582 ;; Greediness makes this unnecessary? --Stef | |
| 1583 symbol-end))) | |
| 1584 (push (match-string 1) accum))) | 1788 (push (match-string 1) accum))) |
| 1585 (if accum (mapconcat 'identity accum "."))))) | 1789 (if accum (mapconcat 'identity accum "."))))) |
| 1586 | 1790 |
| 1587 (defun python-mark-block () | 1791 (defun python-mark-block () |
| 1588 "Mark the block around point. | 1792 "Mark the block around point. |
| 1591 (push-mark) | 1795 (push-mark) |
| 1592 (python-beginning-of-block) | 1796 (python-beginning-of-block) |
| 1593 (push-mark (point) nil t) | 1797 (push-mark (point) nil t) |
| 1594 (python-end-of-block) | 1798 (python-end-of-block) |
| 1595 (exchange-point-and-mark)) | 1799 (exchange-point-and-mark)) |
| 1800 | |
| 1801 ;; Fixme: Provide a find-function-like command to find source of a | |
| 1802 ;; definition (separate from BicycleRepairMan). Complicated by | |
| 1803 ;; finding the right qualified name. | |
| 1596 | 1804 |
| 1597 ;;;; Completion. | 1805 ;;;; Completion. |
| 1598 | 1806 |
| 1807 (defvar python-imports nil | |
| 1808 "String of top-level import statements updated by `python-find-imports'.") | |
| 1809 (make-variable-buffer-local 'python-imports) | |
| 1810 | |
| 1811 ;; Fixme: Should font-lock try to run this when it deals with an import? | |
| 1812 ;; Maybe not a good idea if it gets run multiple times when the | |
| 1813 ;; statement is being edited, and is more likely to end up with | |
| 1814 ;; something syntactically incorrect. | |
| 1815 ;; However, what we should do is to trundle up the block tree from point | |
| 1816 ;; to extract imports that appear to be in scope, and add those. | |
| 1817 (defun python-find-imports () | |
| 1818 "Find top-level imports, updating `python-imports'." | |
| 1819 (interactive) | |
| 1820 (save-excursion | |
| 1821 (let (lines) | |
| 1822 (goto-char (point-min)) | |
| 1823 (while (re-search-forward "^import\\>\\|^from\\>" nil t) | |
| 1824 (unless (syntax-ppss-context (syntax-ppss)) | |
| 1825 (push (buffer-substring (line-beginning-position) | |
| 1826 (line-beginning-position 2)) | |
| 1827 lines))) | |
| 1828 (setq python-imports | |
| 1829 (if lines | |
| 1830 (apply #'concat | |
| 1831 ;; This is probably best left out since you're unlikely to need the | |
| 1832 ;; doc for a function in the buffer and the import will lose if the | |
| 1833 ;; Python sub-process' working directory isn't the same as the | |
| 1834 ;; buffer's. | |
| 1835 ;; (if buffer-file-name | |
| 1836 ;; (concat | |
| 1837 ;; "import " | |
| 1838 ;; (file-name-sans-extension | |
| 1839 ;; (file-name-nondirectory buffer-file-name)))) | |
| 1840 (nreverse lines)) | |
| 1841 "None")) | |
| 1842 (when lines | |
| 1843 (set-text-properties 0 (length python-imports) nil python-imports) | |
| 1844 ;; The output ends up in the wrong place if the string we | |
| 1845 ;; send contains newlines (from the imports). | |
| 1846 (setq python-imports | |
| 1847 (replace-regexp-in-string "\n" "\\n" | |
| 1848 (format "%S" python-imports) t t)))))) | |
| 1849 | |
| 1850 ;; Fixme: This fails the first time if the sub-process isn't already | |
| 1851 ;; running. Presumably a timing issue with i/o to the process. | |
| 1599 (defun python-symbol-completions (symbol) | 1852 (defun python-symbol-completions (symbol) |
| 1600 "Return a list of completions of the string SYMBOL from Python process. | 1853 "Return a list of completions of the string SYMBOL from Python process. |
| 1601 The list is sorted." | 1854 The list is sorted. |
| 1855 Uses `python-imports' to load modules against which to complete." | |
| 1602 (when symbol | 1856 (when symbol |
| 1603 (let ((completions | 1857 (let ((completions |
| 1604 (condition-case () | 1858 (condition-case () |
| 1605 (car (read-from-string (python-send-receive | 1859 (car (read-from-string |
| 1606 (format "emacs.complete(%S)" symbol)))) | 1860 (python-send-receive |
| 1861 (format "emacs.complete(%S,%s)" symbol python-imports)))) | |
| 1607 (error nil)))) | 1862 (error nil)))) |
| 1608 (sort | 1863 (sort |
| 1609 ;; We can get duplicates from the above -- don't know why. | 1864 ;; We can get duplicates from the above -- don't know why. |
| 1610 (delete-dups completions) | 1865 (delete-dups completions) |
| 1611 #'string<)))) | 1866 #'string<)))) |
| 1613 (defun python-partial-symbol () | 1868 (defun python-partial-symbol () |
| 1614 "Return the partial symbol before point (for completion)." | 1869 "Return the partial symbol before point (for completion)." |
| 1615 (let ((end (point)) | 1870 (let ((end (point)) |
| 1616 (start (save-excursion | 1871 (start (save-excursion |
| 1617 (and (re-search-backward | 1872 (and (re-search-backward |
| 1618 (rx (and (or buffer-start (regexp "[^[:alnum:]._]")) | 1873 (rx (or buffer-start (regexp "[^[:alnum:]._]")) |
| 1619 (group (1+ (regexp "[[:alnum:]._]"))) | 1874 (group (1+ (regexp "[[:alnum:]._]"))) point) |
| 1620 point)) | |
| 1621 nil t) | 1875 nil t) |
| 1622 (match-beginning 1))))) | 1876 (match-beginning 1))))) |
| 1623 (if start (buffer-substring-no-properties start end)))) | 1877 (if start (buffer-substring-no-properties start end)))) |
| 1624 | 1878 |
| 1625 ;; Fixme: We should have an abstraction of this sort of thing in the | |
| 1626 ;; core. | |
| 1627 (defun python-complete-symbol () | 1879 (defun python-complete-symbol () |
| 1628 "Perform completion on the Python symbol preceding point. | 1880 "Perform completion on the Python symbol preceding point. |
| 1629 Repeating the command scrolls the completion window." | 1881 Repeating the command scrolls the completion window." |
| 1630 (interactive) | 1882 (interactive) |
| 1631 (let ((window (get-buffer-window "*Completions*"))) | 1883 (let ((window (get-buffer-window "*Completions*"))) |
| 1656 (message "Making completion list...") | 1908 (message "Making completion list...") |
| 1657 (with-output-to-temp-buffer "*Completions*" | 1909 (with-output-to-temp-buffer "*Completions*" |
| 1658 (display-completion-list completions symbol)) | 1910 (display-completion-list completions symbol)) |
| 1659 (message "Making completion list...%s" "done")))))))) | 1911 (message "Making completion list...%s" "done")))))))) |
| 1660 | 1912 |
| 1661 (eval-when-compile (require 'hippie-exp)) | |
| 1662 | |
| 1663 (defun python-try-complete (old) | 1913 (defun python-try-complete (old) |
| 1664 "Completion function for Python for use with `hippie-expand'." | 1914 "Completion function for Python for use with `hippie-expand'." |
| 1665 (when (eq major-mode 'python-mode) ; though we only add it locally | 1915 (when (eq major-mode 'python-mode) ; though we only add it locally |
| 1666 (unless old | 1916 (unless old |
| 1667 (let ((symbol (python-partial-symbol))) | 1917 (let ((symbol (python-partial-symbol))) |
| 1678 (he-substitute-string (pop he-expand-list)) | 1928 (he-substitute-string (pop he-expand-list)) |
| 1679 t) | 1929 t) |
| 1680 (if old (he-reset-string)) | 1930 (if old (he-reset-string)) |
| 1681 nil))) | 1931 nil))) |
| 1682 | 1932 |
| 1933 ;;;; FFAP support | |
| 1934 | |
| 1935 (defun python-module-path (module) | |
| 1936 "Function for `ffap-alist' to return path to MODULE." | |
| 1937 (python-send-receive (format "emacs.modpath (%S)" module))) | |
| 1938 | |
| 1939 (eval-after-load "ffap" | |
| 1940 '(push '(python-mode . python-module-path) ffap-alist)) | |
| 1941 | |
| 1942 ;;;; Skeletons | |
| 1943 | |
| 1944 (defvar python-skeletons nil | |
| 1945 "Alist of named skeletons for Python mode. | |
| 1946 Elements are of the form (NAME . EXPANDER-FUNCTION).") | |
| 1947 | |
| 1948 (defvar python-mode-abbrev-table nil | |
| 1949 "Abbrev table for Python mode. | |
| 1950 The default contents correspond to the elements of `python-skeletons'.") | |
| 1951 (define-abbrev-table 'python-mode-abbrev-table ()) | |
| 1952 | |
| 1953 (eval-when-compile | |
| 1954 ;; Define a user-level skeleton and add it to `python-skeletons' and | |
| 1955 ;; the abbrev table. | |
| 1956 (defmacro def-python-skeleton (name &rest elements) | |
| 1957 (let* ((name (symbol-name name)) | |
| 1958 (function (intern (concat "python-insert-" name)))) | |
| 1959 `(progn | |
| 1960 (add-to-list 'python-skeletons ',(cons name function)) | |
| 1961 (define-abbrev python-mode-abbrev-table ,name "" ',function nil t) | |
| 1962 (define-skeleton ,function | |
| 1963 ,(format "Insert Python \"%s\" template." name) | |
| 1964 ,@elements))))) | |
| 1965 (put 'def-python-skeleton 'lisp-indent-function 2) | |
| 1966 | |
| 1967 ;; From `skeleton-further-elements': | |
| 1968 ;; `<': outdent a level; | |
| 1969 ;; `^': delete indentation on current line and also previous newline. | |
| 1970 ;; Not quote like `delete-indentation'. Assumes point is at | |
| 1971 ;; beginning of indentation. | |
| 1972 | |
| 1973 (def-python-skeleton if | |
| 1974 "Condition: " | |
| 1975 "if " str ":" \n | |
| 1976 > _ \n | |
| 1977 ("other condition, %s: " | |
| 1978 < ; Avoid wrong indentation after block opening. | |
| 1979 "elif " str ":" \n | |
| 1980 > _ \n nil) | |
| 1981 (python-else) | ^) | |
| 1982 | |
| 1983 (define-skeleton python-else | |
| 1984 "Auxiliary skeleton." | |
| 1985 nil | |
| 1986 (unless (eq ?y (read-char "Add `else' clause? (y for yes or RET for no) ")) | |
| 1987 (signal 'quit t)) | |
| 1988 < "else:" \n | |
| 1989 > _ \n) | |
| 1990 | |
| 1991 (def-python-skeleton while | |
| 1992 "Condition: " | |
| 1993 "while " str ":" \n | |
| 1994 > _ \n | |
| 1995 (python-else) | ^) | |
| 1996 | |
| 1997 (def-python-skeleton for | |
| 1998 "Target, %s: " | |
| 1999 "for " str " in " (skeleton-read "Expression, %s: ") ":" \n | |
| 2000 > _ \n | |
| 2001 (python-else) | ^) | |
| 2002 | |
| 2003 (def-python-skeleton try/except | |
| 2004 nil | |
| 2005 "try:" \n | |
| 2006 > _ \n | |
| 2007 ("Exception, %s: " | |
| 2008 < "except " str (python-target) ":" \n | |
| 2009 > _ \n nil) | |
| 2010 < "except:" \n | |
| 2011 > _ \n | |
| 2012 (python-else) | ^) | |
| 2013 | |
| 2014 (define-skeleton python-target | |
| 2015 "Auxiliary skeleton." | |
| 2016 "Target, %s: " ", " str | -2) | |
| 2017 | |
| 2018 (def-python-skeleton try/finally | |
| 2019 nil | |
| 2020 "try:" \n | |
| 2021 > _ \n | |
| 2022 < "finally:" \n | |
| 2023 > _ \n) | |
| 2024 | |
| 2025 (def-python-skeleton def | |
| 2026 "Name: " | |
| 2027 "def " str " (" ("Parameter, %s: " (unless (equal ?\( (char-before)) ", ") | |
| 2028 str) "):" \n | |
| 2029 "\"\"\"" @ " \"\"\"" \n ; Fixme: syntaxification wrong for """""" | |
| 2030 > _ \n) | |
| 2031 | |
| 2032 (def-python-skeleton class | |
| 2033 "Name: " | |
| 2034 "class " str " (" ("Inheritance, %s: " | |
| 2035 (unless (equal ?\( (char-before)) ", ") | |
| 2036 str) | |
| 2037 & ")" | -2 ; close list or remove opening | |
| 2038 ":" \n | |
| 2039 "\"\"\"" @ " \"\"\"" \n | |
| 2040 > _ \n) | |
| 2041 | |
| 2042 (defvar python-default-template "if" | |
| 2043 "Default template to expand by `python-insert-template'. | |
| 2044 Updated on each expansion.") | |
| 2045 | |
| 2046 (defun python-expand-template (name) | |
| 2047 "Expand template named NAME. | |
| 2048 Interactively, prompt for the name with completion." | |
| 2049 (interactive | |
| 2050 (list (completing-read (format "Template to expand (default %s): " | |
| 2051 python-default-template) | |
| 2052 python-skeletons nil t))) | |
| 2053 (if (equal "" name) | |
| 2054 (setq name python-default-template) | |
| 2055 (setq python-default-template name)) | |
| 2056 (let ((func (cdr (assoc name python-skeletons)))) | |
| 2057 (if func | |
| 2058 (funcall func) | |
| 2059 (error "Undefined template: %s" name)))) | |
| 2060 | |
| 2061 ;;;; Bicycle Repair Man support | |
| 2062 | |
| 2063 (autoload 'pymacs-load "pymacs" nil t) | |
| 2064 (autoload 'brm-init "bikemacs") | |
| 2065 | |
| 2066 ;; I'm not sure how useful BRM really is, and it's certainly dangerous | |
| 2067 ;; the way it modifies files outside Emacs... Also note that the | |
| 2068 ;; current BRM loses with tabs used for indentation -- I submitted a | |
| 2069 ;; fix <URL:http://www.loveshack.ukfsn.org/emacs/bikeemacs.py.diff>. | |
| 2070 (defun python-setup-brm () | |
| 2071 "Set up Bicycle Repair Man refactoring tool (if available). | |
| 2072 | |
| 2073 Note that the `refactoring' features change files independently of | |
| 2074 Emacs and may modify and save the contents of the current buffer | |
| 2075 without confirmation." | |
| 2076 (interactive) | |
| 2077 (condition-case data | |
| 2078 (unless (fboundp 'brm-rename) | |
| 2079 (pymacs-load "bikeemacs" "brm-") ; first line of normal recipe | |
| 2080 (let ((py-mode-map (make-sparse-keymap)) ; it assumes this | |
| 2081 (features (cons 'python-mode features)) ; and requires this | |
| 2082 menu) | |
| 2083 (brm-init) ; second line of normal recipe | |
| 2084 (remove-hook 'python-mode-hook ; undo this from `brm-init' | |
| 2085 '(lambda () (easy-menu-add brm-menu))) | |
| 2086 (easy-menu-define | |
| 2087 python-brm-menu python-mode-map | |
| 2088 "Bicycle Repair Man" | |
| 2089 '("BicycleRepairMan" | |
| 2090 :help "Interface to navigation and refactoring tool" | |
| 2091 "Queries" | |
| 2092 ["Find References" brm-find-references | |
| 2093 :help "Find references to name at point in compilation buffer"] | |
| 2094 ["Find Definition" brm-find-definition | |
| 2095 :help "Find definition of name at point"] | |
| 2096 "-" | |
| 2097 "Refactoring" | |
| 2098 ["Rename" brm-rename | |
| 2099 :help "Replace name at point with a new name everywhere"] | |
| 2100 ["Extract Method" brm-extract-method | |
| 2101 :active (and mark-active (not buffer-read-only)) | |
| 2102 :help "Replace statements in region with a method"] | |
| 2103 ["Extract Local Variable" brm-extract-local-variable | |
| 2104 :active (and mark-active (not buffer-read-only)) | |
| 2105 :help "Replace expression in region with an assignment"] | |
| 2106 ["Inline Local Variable" brm-inline-local-variable | |
| 2107 :help | |
| 2108 "Substitute uses of variable at point with its definition"] | |
| 2109 ;; Fixme: Should check for anything to revert. | |
| 2110 ["Undo Last Refactoring" brm-undo :help ""])))) | |
| 2111 (error (error "Bicyclerepairman setup failed: %s" data)))) | |
| 2112 | |
| 1683 ;;;; Modes. | 2113 ;;;; Modes. |
| 1684 | 2114 |
| 1685 (defvar outline-heading-end-regexp) | 2115 (defvar outline-heading-end-regexp) |
| 1686 (defvar eldoc-documentation-function) | 2116 (defvar eldoc-documentation-function) |
| 2117 | |
| 2118 ;; Stuff to allow expanding abbrevs with non-word constituents. | |
| 2119 (defun python-abbrev-pc-hook () | |
| 2120 "Set the syntax table before possibly expanding abbrevs." | |
| 2121 (remove-hook 'post-command-hook 'python-abbrev-pc-hook t) | |
| 2122 (set-syntax-table python-mode-syntax-table)) | |
| 2123 | |
| 2124 (defvar python-abbrev-syntax-table | |
| 2125 (copy-syntax-table python-mode-syntax-table) | |
| 2126 "Syntax table used when expanding abbrevs.") | |
| 2127 | |
| 2128 (defun python-pea-hook () | |
| 2129 "Reset the syntax table after possibly expanding abbrevs." | |
| 2130 (set-syntax-table python-abbrev-syntax-table) | |
| 2131 (add-hook 'post-command-hook 'python-abbrev-pc-hook nil t)) | |
| 2132 (modify-syntax-entry ?/ "w" python-abbrev-syntax-table) | |
| 1687 | 2133 |
| 1688 ;;;###autoload | 2134 ;;;###autoload |
| 1689 (define-derived-mode python-mode fundamental-mode "Python" | 2135 (define-derived-mode python-mode fundamental-mode "Python" |
| 1690 "Major mode for editing Python files. | 2136 "Major mode for editing Python files. |
| 1691 Turns on Font Lock mode unconditionally since it is required for correct | 2137 Font Lock mode is currently required for correct parsing of the source. |
| 1692 parsing of the source. | |
| 1693 See also `jython-mode', which is actually invoked if the buffer appears to | 2138 See also `jython-mode', which is actually invoked if the buffer appears to |
| 1694 contain Jython code. See also `run-python' and associated Python mode | 2139 contain Jython code. See also `run-python' and associated Python mode |
| 1695 commands for running Python under Emacs. | 2140 commands for running Python under Emacs. |
| 1696 | 2141 |
| 1697 The Emacs commands which work with `defun's, e.g. \\[beginning-of-defun], deal | 2142 The Emacs commands which work with `defun's, e.g. \\[beginning-of-defun], deal |
| 1701 the end of definitions at that level, when they move up a level. | 2146 the end of definitions at that level, when they move up a level. |
| 1702 \\<python-mode-map> | 2147 \\<python-mode-map> |
| 1703 Colon is electric: it outdents the line if appropriate, e.g. for | 2148 Colon is electric: it outdents the line if appropriate, e.g. for |
| 1704 an else statement. \\[python-backspace] at the beginning of an indented statement | 2149 an else statement. \\[python-backspace] at the beginning of an indented statement |
| 1705 deletes a level of indentation to close the current block; otherwise it | 2150 deletes a level of indentation to close the current block; otherwise it |
| 1706 deletes a charcter backward. TAB indents the current line relative to | 2151 deletes a character backward. TAB indents the current line relative to |
| 1707 the preceding code. Successive TABs, with no intervening command, cycle | 2152 the preceding code. Successive TABs, with no intervening command, cycle |
| 1708 through the possibilities for indentation on the basis of enclosing blocks. | 2153 through the possibilities for indentation on the basis of enclosing blocks. |
| 1709 | 2154 |
| 1710 \\[fill-paragraph] fills comments and multiline strings appropriately, but has no | 2155 \\[fill-paragraph] fills comments and multi-line strings appropriately, but has no |
| 1711 effect outside them. | 2156 effect outside them. |
| 1712 | 2157 |
| 1713 Supports Eldoc mode (only for functions, using a Python process), | 2158 Supports Eldoc mode (only for functions, using a Python process), |
| 1714 Info-Look and Imenu. In Outline minor mode, `class' and `def' | 2159 Info-Look and Imenu. In Outline minor mode, `class' and `def' |
| 1715 lines count as headers. | 2160 lines count as headers. Symbol completion is available in the |
| 2161 same way as in the Python shell using the `rlcompleter' module | |
| 2162 and this is added to the Hippie Expand functions locally if | |
| 2163 Hippie Expand mode is turned on. Completion of symbols of the | |
| 2164 form x.y only works if the components are literal | |
| 2165 module/attribute names, not variables. An abbrev table is set up | |
| 2166 with skeleton expansions for compound statement templates. | |
| 1716 | 2167 |
| 1717 \\{python-mode-map}" | 2168 \\{python-mode-map}" |
| 1718 :group 'python | 2169 :group 'python |
| 1719 (set (make-local-variable 'font-lock-defaults) | 2170 (set (make-local-variable 'font-lock-defaults) |
| 1720 '(python-font-lock-keywords nil nil ((?_ . "w")) nil | 2171 '(python-font-lock-keywords nil nil nil nil |
| 1721 (font-lock-syntactic-keywords | 2172 (font-lock-syntactic-keywords |
| 1722 . python-font-lock-syntactic-keywords) | 2173 . python-font-lock-syntactic-keywords) |
| 1723 ;; This probably isn't worth it. | 2174 ;; This probably isn't worth it. |
| 1724 ;; (font-lock-syntactic-face-function | 2175 ;; (font-lock-syntactic-face-function |
| 1725 ;; . python-font-lock-syntactic-face-function) | 2176 ;; . python-font-lock-syntactic-face-function) |
| 1726 )) | 2177 )) |
| 1727 (set (make-local-variable 'parse-sexp-lookup-properties) t) | 2178 (set (make-local-variable 'parse-sexp-lookup-properties) t) |
| 1728 (set (make-local-variable 'comment-start) "# ") | 2179 (set (make-local-variable 'comment-start) "# ") |
| 1729 (set (make-local-variable 'comment-indent-function) #'python-comment-indent) | |
| 1730 (set (make-local-variable 'indent-line-function) #'python-indent-line) | 2180 (set (make-local-variable 'indent-line-function) #'python-indent-line) |
| 2181 (set (make-local-variable 'indent-region-function) #'python-indent-region) | |
| 1731 (set (make-local-variable 'paragraph-start) "\\s-*$") | 2182 (set (make-local-variable 'paragraph-start) "\\s-*$") |
| 1732 (set (make-local-variable 'fill-paragraph-function) 'python-fill-paragraph) | 2183 (set (make-local-variable 'fill-paragraph-function) 'python-fill-paragraph) |
| 1733 (set (make-local-variable 'require-final-newline) mode-require-final-newline) | 2184 (set (make-local-variable 'require-final-newline) mode-require-final-newline) |
| 1734 (set (make-local-variable 'add-log-current-defun-function) | 2185 (set (make-local-variable 'add-log-current-defun-function) |
| 1735 #'python-current-defun) | 2186 #'python-current-defun) |
| 1736 ;; Fixme: Generalize to do all blocks? | 2187 (set (make-local-variable 'outline-regexp) |
| 1737 (set (make-local-variable 'outline-regexp) "\\s-*\\(def\\|class\\)\\>") | 2188 (rx (* space) (or "class" "def" "elif" "else" "except" "finally" |
| 2189 "for" "if" "try" "while") | |
| 2190 symbol-end)) | |
| 1738 (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n") | 2191 (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n") |
| 1739 (set (make-local-variable 'outline-level) #'python-outline-level) | 2192 (set (make-local-variable 'outline-level) #'python-outline-level) |
| 1740 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil) | 2193 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil) |
| 1741 (make-local-variable 'python-saved-check-command) | 2194 (make-local-variable 'python-saved-check-command) |
| 1742 (set (make-local-variable 'beginning-of-defun-function) | 2195 (set (make-local-variable 'beginning-of-defun-function) |
| 1744 (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun) | 2197 (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun) |
| 1745 (setq imenu-create-index-function #'python-imenu-create-index) | 2198 (setq imenu-create-index-function #'python-imenu-create-index) |
| 1746 (set (make-local-variable 'eldoc-documentation-function) | 2199 (set (make-local-variable 'eldoc-documentation-function) |
| 1747 #'python-eldoc-function) | 2200 #'python-eldoc-function) |
| 1748 (add-hook 'eldoc-mode-hook | 2201 (add-hook 'eldoc-mode-hook |
| 1749 '(lambda () (run-python nil t)) nil t) ; need it running | 2202 (lambda () (run-python nil t)) ; need it running |
| 1750 (unless (assoc 'python-mode hs-special-modes-alist) | 2203 nil t) |
| 1751 (setq | 2204 ;; Fixme: should be in hideshow. This seems to be of limited use |
| 1752 hs-special-modes-alist | 2205 ;; since it isn't (can't be) indentation-based. Also hide-level |
| 1753 (cons (list | 2206 ;; doesn't seem to work properly. |
| 1754 'python-mode "^\\s-*def\\>" nil "#" | 2207 (add-to-list 'hs-special-modes-alist |
| 1755 (lambda (arg)(python-end-of-defun)(skip-chars-backward " \t\n")) | 2208 `(python-mode "^\\s-*def\\>" nil "#" |
| 1756 nil) | 2209 ,(lambda (arg) |
| 1757 hs-special-modes-alist))) | 2210 (python-end-of-defun) |
| 2211 (skip-chars-backward " \t\n")) | |
| 2212 nil)) | |
| 2213 (set (make-local-variable 'skeleton-further-elements) | |
| 2214 '((< '(backward-delete-char-untabify (min python-indent | |
| 2215 (current-column)))) | |
| 2216 (^ '(- (1+ (current-indentation)))))) | |
| 2217 (add-hook 'pre-abbrev-expand-hook 'python-pea-hook nil t) | |
| 1758 (if (featurep 'hippie-exp) | 2218 (if (featurep 'hippie-exp) |
| 1759 (set (make-local-variable 'hippie-expand-try-functions-list) | 2219 (set (make-local-variable 'hippie-expand-try-functions-list) |
| 1760 (cons 'python-try-complete hippie-expand-try-functions-list))) | 2220 (cons 'python-try-complete hippie-expand-try-functions-list))) |
| 1761 (when python-guess-indent (python-guess-indent)) | 2221 (when python-guess-indent (python-guess-indent)) |
| 1762 (set (make-local-variable 'python-command) python-python-command) | 2222 (set (make-local-variable 'python-command) python-python-command) |
| 2223 (python-find-imports) | |
| 1763 (unless (boundp 'python-mode-running) ; kill the recursion from jython-mode | 2224 (unless (boundp 'python-mode-running) ; kill the recursion from jython-mode |
| 1764 (let ((python-mode-running t)) | 2225 (let ((python-mode-running t)) |
| 1765 (python-maybe-jython)))) | 2226 (python-maybe-jython)))) |
| 1766 | 2227 |
| 1767 (custom-add-option 'python-mode-hook 'imenu-add-menubar-index) | 2228 (custom-add-option 'python-mode-hook 'imenu-add-menubar-index) |
| 1768 (custom-add-option 'python-mode-hook | 2229 (custom-add-option 'python-mode-hook |
| 1769 '(lambda () | 2230 '(lambda () |
| 1770 "Turn on Indent Tabs mode." | 2231 "Turn off Indent Tabs mode." |
| 1771 (set (make-local-variable 'indent-tabs-mode) t))) | 2232 (set (make-local-variable 'indent-tabs-mode) nil))) |
| 1772 (custom-add-option 'python-mode-hook 'turn-on-eldoc-mode) | 2233 (custom-add-option 'python-mode-hook 'turn-on-eldoc-mode) |
| 2234 (custom-add-option 'python-mode-hook 'abbrev-mode) | |
| 2235 (custom-add-option 'python-mode-hook 'python-setup-brm) | |
| 1773 | 2236 |
| 1774 ;;;###autoload | 2237 ;;;###autoload |
| 1775 (define-derived-mode jython-mode python-mode "Jython" | 2238 (define-derived-mode jython-mode python-mode "Jython" |
| 1776 "Major mode for editing Jython files. | 2239 "Major mode for editing Jython files. |
| 1777 Like `python-mode', but sets up parameters for Jython subprocesses. | 2240 Like `python-mode', but sets up parameters for Jython subprocesses. |
| 1778 Runs `jython-mode-hook' after `python-mode-hook'." | 2241 Runs `jython-mode-hook' after `python-mode-hook'." |
| 1779 :group 'python | 2242 :group 'python |
| 1780 (set (make-local-variable 'python-command) python-jython-command)) | 2243 (set (make-local-variable 'python-command) python-jython-command)) |
| 1781 | 2244 |
| 1782 (provide 'python) | 2245 (provide 'python) |
| 2246 (provide 'python-21) | |
| 1783 ;; arch-tag: 6fce1d99-a704-4de9-ba19-c6e4912b0554 | 2247 ;; arch-tag: 6fce1d99-a704-4de9-ba19-c6e4912b0554 |
| 1784 ;;; python.el ends here | 2248 ;;; python.el ends here |
