diff options
| author | Fabián Ezequiel Gallina | 2012-05-17 00:02:52 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2012-05-17 00:02:52 -0300 |
| commit | 45c138ac0ff103d83e1fd5ba5893b70c4a6d316e (patch) | |
| tree | b094775142201ed4c09c0b266d63a9df708f0f49 /lisp | |
| parent | 3b3027dc0af9f75395d225a57c1c89300ed95b8f (diff) | |
| download | emacs-45c138ac0ff103d83e1fd5ba5893b70c4a6d316e.tar.gz emacs-45c138ac0ff103d83e1fd5ba5893b70c4a6d316e.zip | |
First commit.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/progmodes/python.el | 1640 |
1 files changed, 1640 insertions, 0 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index e69de29bb2d..b281c01b7d9 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -0,0 +1,1640 @@ | |||
| 1 | ;;; python.el -- Python's flying circus support for Emacs | ||
| 2 | |||
| 3 | ;; Copyright (C) 2010 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Fabián E. Gallina <fabian@anue.biz> | ||
| 6 | ;; Maintainer: FSF | ||
| 7 | ;; Created: Jul 2010 | ||
| 8 | ;; Keywords: languages | ||
| 9 | |||
| 10 | ;; This file is NOT part of GNU Emacs. | ||
| 11 | |||
| 12 | ;; python.el is free software: you can redistribute it and/or modify | ||
| 13 | ;; it under the terms of the GNU General Public License as published by | ||
| 14 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 15 | ;; (at your option) any later version. | ||
| 16 | |||
| 17 | ;; python.el is distributed in the hope that it will be useful, | ||
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 20 | ;; GNU General Public License for more details. | ||
| 21 | |||
| 22 | ;; You should have received a copy of the GNU General Public License | ||
| 23 | ;; along with python.el. If not, see <http://www.gnu.org/licenses/>. | ||
| 24 | |||
| 25 | ;;; Commentary: | ||
| 26 | |||
| 27 | ;; Major mode for editing Python files with some fontification and | ||
| 28 | ;; indentation bits extracted from original Dave Love's python.el | ||
| 29 | ;; found in GNU/Emacs. | ||
| 30 | |||
| 31 | ;; While it probably has less features than Dave Love's python.el and | ||
| 32 | ;; PSF's python-mode.el it provides the main stuff you'll need while | ||
| 33 | ;; keeping it simple :) | ||
| 34 | |||
| 35 | ;; Implements Syntax highlighting, Indentation, Movement, Shell | ||
| 36 | ;; interaction, Shell completion, Pdb tracking, Symbol completion, | ||
| 37 | ;; Eldoc. | ||
| 38 | |||
| 39 | ;; Syntax highlighting: Fontification of code is provided and supports | ||
| 40 | ;; python's triple quoted strings properly. | ||
| 41 | |||
| 42 | ;; Indentation: Automatic indentation with indentation cycling is | ||
| 43 | ;; provided, it allows you to navigate different available levels of | ||
| 44 | ;; indentation by hitting <tab> several times. | ||
| 45 | |||
| 46 | ;; Movement: `beginning-of-defun' and `end-of-defun' functions are | ||
| 47 | ;; properly implemented. A `beginning-of-innermost-defun' is defined | ||
| 48 | ;; to navigate nested defuns. | ||
| 49 | |||
| 50 | ;; Shell interaction: is provided and allows you easily execute any | ||
| 51 | ;; block of code of your current buffer in an inferior Python process. | ||
| 52 | |||
| 53 | ;; Shell completion: hitting tab will try to complete the current | ||
| 54 | ;; word. Shell completion is implemented in a manner that if you | ||
| 55 | ;; change the `python-shell-interpreter' to any other (for example | ||
| 56 | ;; IPython) it should be easy to integrate another way to calculate | ||
| 57 | ;; completions. You just need to especify your custom | ||
| 58 | ;; `python-shell-completion-setup-code' and | ||
| 59 | ;; `python-shell-completion-strings-code' | ||
| 60 | |||
| 61 | ;; Pdb tracking: when you execute a block of code that contains some | ||
| 62 | ;; call to pdb (or ipdb) it will prompt the block of code and will | ||
| 63 | ;; follow the execution of pdb marking the current line with an arrow. | ||
| 64 | |||
| 65 | ;; Symbol completion: you can complete the symbol at point. It uses | ||
| 66 | ;; the shell completion in background so you should run | ||
| 67 | ;; `python-shell-send-buffer' from time to time to get better results. | ||
| 68 | |||
| 69 | ;; Eldoc: returns documentation for object at point by using the | ||
| 70 | ;; inferior python subprocess to inspect its documentation. As you | ||
| 71 | ;; might guessed you should run `python-shell-send-buffer' from time | ||
| 72 | ;; to time to get better results too. | ||
| 73 | |||
| 74 | ;;; Installation: | ||
| 75 | |||
| 76 | ;; Add this to your .emacs: | ||
| 77 | |||
| 78 | ;; (add-to-list 'load-path "/folder/containing/file") | ||
| 79 | ;; (require 'python) | ||
| 80 | |||
| 81 | ;;; TODO: | ||
| 82 | |||
| 83 | ;; Ordered by priority: | ||
| 84 | |||
| 85 | ;; Better decorator support for beginning of defun | ||
| 86 | |||
| 87 | ;; Fix shell autocompletion when: obj.<tab> | ||
| 88 | |||
| 89 | ;; Remove garbage prompts left from calls to `comint-send-string' and | ||
| 90 | ;; other comint related cleanups. | ||
| 91 | |||
| 92 | ;; Review code and cleanup | ||
| 93 | |||
| 94 | ;;; Code: | ||
| 95 | |||
| 96 | (require 'comint) | ||
| 97 | (require 'ansi-color) | ||
| 98 | (require 'outline) | ||
| 99 | |||
| 100 | (eval-when-compile | ||
| 101 | (require 'cl)) | ||
| 102 | |||
| 103 | (autoload 'comint-mode "comint") | ||
| 104 | |||
| 105 | ;;;###autoload | ||
| 106 | (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode)) | ||
| 107 | ;;;###autoload | ||
| 108 | (add-to-list 'interpreter-mode-alist (cons (purecopy "python") 'python-mode)) | ||
| 109 | |||
| 110 | (defgroup python nil | ||
| 111 | "Python Language's flying circus support for Emacs." | ||
| 112 | :group 'languages | ||
| 113 | :version "23.2" | ||
| 114 | :link '(emacs-commentary-link "python")) | ||
| 115 | |||
| 116 | |||
| 117 | ;;; Bindings | ||
| 118 | |||
| 119 | (defvar python-mode-map | ||
| 120 | (let ((map (make-sparse-keymap))) | ||
| 121 | ;; Indent specific | ||
| 122 | (define-key map "\177" 'python-indent-dedent-line-backspace) | ||
| 123 | (define-key map (kbd "<backtab>") 'python-indent-dedent-line) | ||
| 124 | (define-key map "\C-c<" 'python-indent-shift-left) | ||
| 125 | (define-key map "\C-c>" 'python-indent-shift-right) | ||
| 126 | ;; Shell interaction | ||
| 127 | (define-key map "\C-c\C-s" 'python-shell-send-string) | ||
| 128 | (define-key map "\C-c\C-r" 'python-shell-send-region) | ||
| 129 | (define-key map "\C-\M-x" 'python-shell-send-defun) | ||
| 130 | (define-key map "\C-c\C-c" 'python-shell-send-buffer) | ||
| 131 | (define-key map "\C-c\C-l" 'python-shell-send-file) | ||
| 132 | (define-key map "\C-c\C-z" 'python-shell-switch-to-shell) | ||
| 133 | ;; Utilities | ||
| 134 | (substitute-key-definition 'complete-symbol 'completion-at-point | ||
| 135 | map global-map) | ||
| 136 | (easy-menu-define python-menu map "Python Mode menu" | ||
| 137 | `("Python" | ||
| 138 | :help "Python-specific Features" | ||
| 139 | ["Shift region left" python-indent-shift-left :active mark-active | ||
| 140 | :help "Shift region left by a single indentation step"] | ||
| 141 | ["Shift region right" python-indent-shift-right :active mark-active | ||
| 142 | :help "Shift region right by a single indentation step"] | ||
| 143 | "-" | ||
| 144 | ["Mark def/class" mark-defun | ||
| 145 | :help "Mark outermost definition around point"] | ||
| 146 | "-" | ||
| 147 | ["Start of def/class" beginning-of-defun | ||
| 148 | :help "Go to start of outermost definition around point"] | ||
| 149 | ["Start of def/class" python-beginning-of-innermost-defun | ||
| 150 | :help "Go to start of innermost definition around point"] | ||
| 151 | ["End of def/class" end-of-defun | ||
| 152 | :help "Go to end of definition around point"] | ||
| 153 | "-" | ||
| 154 | ["Start interpreter" run-python | ||
| 155 | :help "Run inferior Python process in a separate buffer"] | ||
| 156 | ["Switch to shell" python-shell-switch-to-shell | ||
| 157 | :help "Switch to running inferior Python process"] | ||
| 158 | ["Eval string" python-shell-send-string | ||
| 159 | :help "Eval string in inferior Python session"] | ||
| 160 | ["Eval buffer" python-shell-send-buffer | ||
| 161 | :help "Eval buffer in inferior Python session"] | ||
| 162 | ["Eval region" python-shell-send-region | ||
| 163 | :help "Eval region in inferior Python session"] | ||
| 164 | ["Eval defun" python-shell-send-defun | ||
| 165 | :help "Eval defun in inferior Python session"] | ||
| 166 | ["Eval file" python-shell-send-file | ||
| 167 | :help "Eval file in inferior Python session"] | ||
| 168 | ["Debugger" pdb :help "Run pdb under GUD"] | ||
| 169 | "-" | ||
| 170 | ["Complete symbol" completion-at-point | ||
| 171 | :help "Complete symbol before point"])) | ||
| 172 | map) | ||
| 173 | "Keymap for `python-mode'.") | ||
| 174 | |||
| 175 | |||
| 176 | ;;; Python specialized rx | ||
| 177 | |||
| 178 | (defconst python-rx-constituents | ||
| 179 | (list | ||
| 180 | `(block-start . ,(rx symbol-start | ||
| 181 | (or "def" "class" "if" "elif" "else" "try" | ||
| 182 | "except" "finally" "for" "while" "with") | ||
| 183 | symbol-end)) | ||
| 184 | `(defun . ,(rx symbol-start (or "def" "class") symbol-end)) | ||
| 185 | `(open-paren . ,(rx (or "{" "[" "("))) | ||
| 186 | `(close-paren . ,(rx (or "}" "]" ")"))) | ||
| 187 | `(simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))) | ||
| 188 | `(not-simple-operator . ,(rx (not (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))) | ||
| 189 | `(operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">" | ||
| 190 | "=" "%" "**" "//" "<<" ">>" "<=" "!=" | ||
| 191 | "==" ">=" "is" "not"))) | ||
| 192 | `(assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**=" | ||
| 193 | ">>=" "<<=" "&=" "^=" "|="))))) | ||
| 194 | |||
| 195 | (defmacro python-rx (&rest regexps) | ||
| 196 | "Python mode especialized rx macro which supports common python named regexps." | ||
| 197 | (let ((rx-constituents (append python-rx-constituents rx-constituents))) | ||
| 198 | (cond ((null regexps) | ||
| 199 | (error "No regexp")) | ||
| 200 | ((cdr regexps) | ||
| 201 | (rx-to-string `(and ,@regexps) t)) | ||
| 202 | (t | ||
| 203 | (rx-to-string (car regexps) t))))) | ||
| 204 | |||
| 205 | |||
| 206 | ;;; Font-lock and syntax | ||
| 207 | |||
| 208 | (defvar python-font-lock-keywords | ||
| 209 | ;; Keywords | ||
| 210 | `(,(rx symbol-start | ||
| 211 | (or "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with" | ||
| 212 | "assert" "else" "if" "pass" "yield" "break" "except" "import" | ||
| 213 | "print" "class" "exec" "in" "raise" "continue" "finally" "is" | ||
| 214 | "return" "def" "for" "lambda" "try" "self") | ||
| 215 | symbol-end) | ||
| 216 | ;; functions | ||
| 217 | (,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_)))) | ||
| 218 | (1 font-lock-function-name-face)) | ||
| 219 | ;; classes | ||
| 220 | (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_)))) | ||
| 221 | (1 font-lock-type-face)) | ||
| 222 | ;; Constants | ||
| 223 | (,(rx symbol-start (group "None" symbol-end)) | ||
| 224 | (1 font-lock-constant-face)) | ||
| 225 | ;; Decorators. | ||
| 226 | (,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_)) | ||
| 227 | (0+ "." (1+ (or word ?_))))) | ||
| 228 | (1 font-lock-type-face)) | ||
| 229 | ;; Builtin Exceptions | ||
| 230 | (,(rx symbol-start | ||
| 231 | (or "ArithmeticError" "AssertionError" "AttributeError" | ||
| 232 | "BaseException" "BufferError" "BytesWarning" "DeprecationWarning" | ||
| 233 | "EOFError" "EnvironmentError" "Exception" "FloatingPointError" | ||
| 234 | "FutureWarning" "GeneratorExit" "IOError" "ImportError" | ||
| 235 | "ImportWarning" "IndentationError" "IndexError" "KeyError" | ||
| 236 | "KeyboardInterrupt" "LookupError" "MemoryError" "NameError" | ||
| 237 | "NotImplemented" "NotImplementedError" "OSError" "OverflowError" | ||
| 238 | "PendingDeprecationWarning" "ReferenceError" "RuntimeError" | ||
| 239 | "RuntimeWarning" "StandardError" "StopIteration" "SyntaxError" | ||
| 240 | "SyntaxWarning" "SystemError" "SystemExit" "TabError" "TypeError" | ||
| 241 | "UnboundLocalError" "UnicodeDecodeError" "UnicodeEncodeError" | ||
| 242 | "UnicodeError" "UnicodeTranslateError" "UnicodeWarning" | ||
| 243 | "UserWarning" "ValueError" "Warning" "ZeroDivisionError") | ||
| 244 | symbol-end) . font-lock-type-face) | ||
| 245 | ;; Builtins | ||
| 246 | (,(rx (or line-start (not (any ". \t"))) (* (any " \t")) symbol-start | ||
| 247 | (group | ||
| 248 | (or "_" "__debug__" "__doc__" "__import__" "__name__" "__package__" | ||
| 249 | "abs" "all" "any" "apply" "basestring" "bin" "bool" "buffer" | ||
| 250 | "bytearray" "bytes" "callable" "chr" "classmethod" "cmp" "coerce" | ||
| 251 | "compile" "complex" "copyright" "credits" "delattr" "dict" "dir" | ||
| 252 | "divmod" "enumerate" "eval" "execfile" "exit" "file" "filter" | ||
| 253 | "float" "format" "frozenset" "getattr" "globals" "hasattr" "hash" | ||
| 254 | "help" "hex" "id" "input" "int" "intern" "isinstance" "issubclass" | ||
| 255 | "iter" "len" "license" "list" "locals" "long" "map" "max" "min" | ||
| 256 | "next" "object" "oct" "open" "ord" "pow" "print" "property" "quit" | ||
| 257 | "range" "raw_input" "reduce" "reload" "repr" "reversed" "round" | ||
| 258 | "set" "setattr" "slice" "sorted" "staticmethod" "str" "sum" | ||
| 259 | "super" "tuple" "type" "unichr" "unicode" "vars" "xrange" "zip" | ||
| 260 | "True" "False" "Ellipsis")) symbol-end) | ||
| 261 | (1 font-lock-builtin-face)) | ||
| 262 | ;; asignations | ||
| 263 | ;; support for a = b = c = 5 | ||
| 264 | (,(lambda (limit) | ||
| 265 | (let ((re (python-rx (group (+ (any word ?. ?_))) (* space) | ||
| 266 | assignment-operator))) | ||
| 267 | (when (re-search-forward re limit t) | ||
| 268 | (while (and (not (equal (nth 0 (syntax-ppss)) 0)) | ||
| 269 | (re-search-forward re limit t))) | ||
| 270 | (if (equal (nth 0 (syntax-ppss)) 0) | ||
| 271 | t | ||
| 272 | (set-match-data nil))))) | ||
| 273 | (1 font-lock-variable-name-face nil nil)) | ||
| 274 | ;; support for a, b, c = (1, 2, 3) | ||
| 275 | (,(lambda (limit) | ||
| 276 | (let ((re (python-rx (group (+ (any word ?. ?_))) (* space) | ||
| 277 | (* ?, (* space) (+ (any word ?. ?_)) (* space)) | ||
| 278 | ?, (* space) (+ (any word ?. ?_)) (* space) | ||
| 279 | assignment-operator))) | ||
| 280 | (when (and (re-search-forward re limit t) | ||
| 281 | (goto-char (nth 3 (match-data)))) | ||
| 282 | (while (and (not (equal (nth 0 (syntax-ppss)) 0)) | ||
| 283 | (re-search-forward re limit t)) | ||
| 284 | (goto-char (nth 3 (match-data)))) | ||
| 285 | (if (equal (nth 0 (syntax-ppss)) 0) | ||
| 286 | t | ||
| 287 | (set-match-data nil))))) | ||
| 288 | (1 font-lock-variable-name-face nil nil)))) | ||
| 289 | |||
| 290 | ;; Fixme: Is there a better way? | ||
| 291 | (defconst python-font-lock-syntactic-keywords | ||
| 292 | ;; First avoid a sequence preceded by an odd number of backslashes. | ||
| 293 | `((,(rx (not (any ?\\)) | ||
| 294 | ?\\ (* (and ?\\ ?\\)) | ||
| 295 | (group (syntax string-quote)) | ||
| 296 | (backref 1) | ||
| 297 | (group (backref 1))) | ||
| 298 | (2 ,(string-to-syntax "\""))) ; dummy | ||
| 299 | (,(rx (group (optional (any "uUrR"))) ; prefix gets syntax property | ||
| 300 | (optional (any "rR")) ; possible second prefix | ||
| 301 | (group (syntax string-quote)) ; maybe gets property | ||
| 302 | (backref 2) ; per first quote | ||
| 303 | (group (backref 2))) ; maybe gets property | ||
| 304 | (1 (python-quote-syntax 1)) | ||
| 305 | (2 (python-quote-syntax 2)) | ||
| 306 | (3 (python-quote-syntax 3)))) | ||
| 307 | "Make outer chars of triple-quote strings into generic string delimiters.") | ||
| 308 | |||
| 309 | (defun python-quote-syntax (n) | ||
| 310 | "Put `syntax-table' property correctly on triple quote. | ||
| 311 | Used for syntactic keywords. N is the match number (1, 2 or 3)." | ||
| 312 | ;; Given a triple quote, we have to check the context to know | ||
| 313 | ;; whether this is an opening or closing triple or whether it's | ||
| 314 | ;; quoted anyhow, and should be ignored. (For that we need to do | ||
| 315 | ;; the same job as `syntax-ppss' to be correct and it seems to be OK | ||
| 316 | ;; to use it here despite initial worries.) We also have to sort | ||
| 317 | ;; out a possible prefix -- well, we don't _have_ to, but I think it | ||
| 318 | ;; should be treated as part of the string. | ||
| 319 | |||
| 320 | ;; Test cases: | ||
| 321 | ;; ur"""ar""" x='"' # """ | ||
| 322 | ;; x = ''' """ ' a | ||
| 323 | ;; ''' | ||
| 324 | ;; x '"""' x """ \"""" x | ||
| 325 | (save-excursion | ||
| 326 | (goto-char (match-beginning 0)) | ||
| 327 | (cond | ||
| 328 | ;; Consider property for the last char if in a fenced string. | ||
| 329 | ((= n 3) | ||
| 330 | (let* ((font-lock-syntactic-keywords nil) | ||
| 331 | (syntax (syntax-ppss))) | ||
| 332 | (when (eq t (nth 3 syntax)) ; after unclosed fence | ||
| 333 | (goto-char (nth 8 syntax)) ; fence position | ||
| 334 | (skip-chars-forward "uUrR") ; skip any prefix | ||
| 335 | ;; Is it a matching sequence? | ||
| 336 | (if (eq (char-after) (char-after (match-beginning 2))) | ||
| 337 | (eval-when-compile (string-to-syntax "|")))))) | ||
| 338 | ;; Consider property for initial char, accounting for prefixes. | ||
| 339 | ((or (and (= n 2) ; leading quote (not prefix) | ||
| 340 | (= (match-beginning 1) (match-end 1))) ; prefix is null | ||
| 341 | (and (= n 1) ; prefix | ||
| 342 | (/= (match-beginning 1) (match-end 1)))) ; non-empty | ||
| 343 | (let ((font-lock-syntactic-keywords nil)) | ||
| 344 | (unless (eq 'string (syntax-ppss-context (syntax-ppss))) | ||
| 345 | (eval-when-compile (string-to-syntax "|"))))) | ||
| 346 | ;; Otherwise (we're in a non-matching string) the property is | ||
| 347 | ;; nil, which is OK. | ||
| 348 | ))) | ||
| 349 | |||
| 350 | (defvar python-mode-syntax-table | ||
| 351 | (let ((table (make-syntax-table))) | ||
| 352 | ;; Give punctuation syntax to ASCII that normally has symbol | ||
| 353 | ;; syntax or has word syntax and isn't a letter. | ||
| 354 | (let ((symbol (string-to-syntax "_")) | ||
| 355 | (sst (standard-syntax-table))) | ||
| 356 | (dotimes (i 128) | ||
| 357 | (unless (= i ?_) | ||
| 358 | (if (equal symbol (aref sst i)) | ||
| 359 | (modify-syntax-entry i "." table))))) | ||
| 360 | (modify-syntax-entry ?$ "." table) | ||
| 361 | (modify-syntax-entry ?% "." table) | ||
| 362 | ;; exceptions | ||
| 363 | (modify-syntax-entry ?# "<" table) | ||
| 364 | (modify-syntax-entry ?\n ">" table) | ||
| 365 | (modify-syntax-entry ?' "\"" table) | ||
| 366 | (modify-syntax-entry ?` "$" table) | ||
| 367 | table) | ||
| 368 | "Syntax table for Python files.") | ||
| 369 | |||
| 370 | (defvar python-dotty-syntax-table | ||
| 371 | (let ((table (make-syntax-table python-mode-syntax-table))) | ||
| 372 | (modify-syntax-entry ?. "w" table) | ||
| 373 | (modify-syntax-entry ?_ "w" table) | ||
| 374 | table) | ||
| 375 | "Dotty syntax table for Python files. | ||
| 376 | It makes underscores and dots word constituent chars.") | ||
| 377 | |||
| 378 | |||
| 379 | ;;; Indentation | ||
| 380 | |||
| 381 | (defcustom python-indent-offset 4 | ||
| 382 | "Default indentation offset for Python." | ||
| 383 | :group 'python | ||
| 384 | :type 'integer | ||
| 385 | :safe 'integerp) | ||
| 386 | |||
| 387 | (defcustom python-indent-guess-indent-offset t | ||
| 388 | "Non-nil tells Python mode to guess `python-indent-offset' value." | ||
| 389 | :type 'boolean | ||
| 390 | :group 'python) | ||
| 391 | |||
| 392 | (defvar python-indent-current-level 0 | ||
| 393 | "Current indentation level `python-indent-line-function' is using.") | ||
| 394 | |||
| 395 | (defvar python-indent-levels '(0) | ||
| 396 | "Levels of indentation available for `python-indent-line-function'.") | ||
| 397 | |||
| 398 | (defvar python-indent-dedenters '("else" "elif" "except" "finally") | ||
| 399 | "List of words that should be dedented. | ||
| 400 | These make `python-indent-calculate-indentation' subtract the value of | ||
| 401 | `python-indent-offset'.") | ||
| 402 | |||
| 403 | (defun python-indent-guess-indent-offset () | ||
| 404 | "Guess and set the value for `python-indent-offset' given the current buffer." | ||
| 405 | (let ((guessed-indentation (save-excursion | ||
| 406 | (goto-char (point-min)) | ||
| 407 | (re-search-forward ":\\s-*\n" nil t) | ||
| 408 | (while (and (not (eobp)) (forward-comment 1))) | ||
| 409 | (current-indentation)))) | ||
| 410 | (when (not (equal guessed-indentation 0)) | ||
| 411 | (setq python-indent-offset guessed-indentation)))) | ||
| 412 | |||
| 413 | (defun python-indent-context (&optional stop) | ||
| 414 | "Return information on indentation context. | ||
| 415 | Optional argument STOP serves to stop recursive calls. | ||
| 416 | |||
| 417 | Returns a cons with the form: | ||
| 418 | |||
| 419 | \(STATUS . START) | ||
| 420 | |||
| 421 | Where status can be any of the following symbols: | ||
| 422 | |||
| 423 | * inside-paren: If point in between (), {} or [] | ||
| 424 | * inside-string: If point is inside a string | ||
| 425 | * after-backslash: Previous line ends in a backslash | ||
| 426 | * after-beginning-of-block: Point is after beginning of block | ||
| 427 | * after-line: Point is after normal line | ||
| 428 | * no-indent: Point is at beginning of buffer or other special case | ||
| 429 | |||
| 430 | START is the buffer position where the sexp starts." | ||
| 431 | (save-restriction | ||
| 432 | (widen) | ||
| 433 | (let ((ppss (save-excursion (beginning-of-line) (syntax-ppss))) | ||
| 434 | (start)) | ||
| 435 | (cons | ||
| 436 | (cond | ||
| 437 | ;; Inside a paren | ||
| 438 | ((setq start (nth 1 ppss)) | ||
| 439 | 'inside-paren) | ||
| 440 | ;; Inside string | ||
| 441 | ((setq start (when (and (nth 3 ppss)) | ||
| 442 | (nth 8 ppss))) | ||
| 443 | 'inside-string) | ||
| 444 | ;; After backslash | ||
| 445 | ((setq start (when (not (syntax-ppss-context ppss)) | ||
| 446 | (let ((line-beg-pos (line-beginning-position))) | ||
| 447 | (when (eq ?\\ (char-before (1- line-beg-pos))) | ||
| 448 | (- line-beg-pos 2))))) | ||
| 449 | 'after-backslash) | ||
| 450 | ;; After beginning of block | ||
| 451 | ((setq start (save-excursion | ||
| 452 | (let ((block-regexp (python-rx block-start)) | ||
| 453 | (block-start-line-end ":[[:space:]]*$")) | ||
| 454 | (back-to-indentation) | ||
| 455 | (while (and (forward-comment -1) (not (bobp)))) | ||
| 456 | (back-to-indentation) | ||
| 457 | (when (or (python-info-continuation-line-p) | ||
| 458 | (and (not (looking-at block-regexp)) | ||
| 459 | (save-excursion | ||
| 460 | (re-search-forward | ||
| 461 | block-start-line-end | ||
| 462 | (line-end-position) t)))) | ||
| 463 | (while (and (forward-line -1) | ||
| 464 | (python-info-continuation-line-p) | ||
| 465 | (not (bobp)))) | ||
| 466 | (when (not (looking-at block-regexp)) | ||
| 467 | (forward-line 1))) | ||
| 468 | (back-to-indentation) | ||
| 469 | (when (and (looking-at block-regexp) | ||
| 470 | (or (re-search-forward | ||
| 471 | block-start-line-end | ||
| 472 | (line-end-position) t) | ||
| 473 | (python-info-continuation-line-p))) | ||
| 474 | (point-marker))))) | ||
| 475 | 'after-beginning-of-block) | ||
| 476 | ;; After normal line | ||
| 477 | ((setq start (save-excursion | ||
| 478 | (while (and (forward-comment -1) (not (bobp)))) | ||
| 479 | (while (and (not (back-to-indentation)) | ||
| 480 | (not (bobp)) | ||
| 481 | (if (> (nth 0 (syntax-ppss)) 0) | ||
| 482 | (forward-line -1) | ||
| 483 | (if (save-excursion | ||
| 484 | (forward-line -1) | ||
| 485 | (python-info-line-ends-backslash-p)) | ||
| 486 | (forward-line -1))))) | ||
| 487 | (point-marker))) | ||
| 488 | 'after-line) | ||
| 489 | ;; Do not indent | ||
| 490 | (t 'no-indent)) | ||
| 491 | start)))) | ||
| 492 | |||
| 493 | (defun python-indent-calculate-indentation () | ||
| 494 | "Calculate correct indentation offset for the current line." | ||
| 495 | (let* ((indentation-context (python-indent-context)) | ||
| 496 | (context-status (car indentation-context)) | ||
| 497 | (context-start (cdr indentation-context))) | ||
| 498 | (save-restriction | ||
| 499 | (widen) | ||
| 500 | (save-excursion | ||
| 501 | (case context-status | ||
| 502 | ('no-indent 0) | ||
| 503 | ('after-beginning-of-block | ||
| 504 | (goto-char context-start) | ||
| 505 | (+ (current-indentation) python-indent-offset)) | ||
| 506 | ('after-line | ||
| 507 | (- | ||
| 508 | (save-excursion | ||
| 509 | (goto-char context-start) | ||
| 510 | (current-indentation)) | ||
| 511 | (if (progn | ||
| 512 | (back-to-indentation) | ||
| 513 | (looking-at (regexp-opt python-indent-dedenters))) | ||
| 514 | python-indent-offset | ||
| 515 | 0))) | ||
| 516 | ('inside-string | ||
| 517 | (goto-char context-start) | ||
| 518 | (current-indentation)) | ||
| 519 | ('after-backslash | ||
| 520 | (let* ((block-continuation | ||
| 521 | (save-excursion | ||
| 522 | (forward-line -1) | ||
| 523 | (python-info-block-continuation-line-p))) | ||
| 524 | (assignment-continuation | ||
| 525 | (save-excursion | ||
| 526 | (forward-line -1) | ||
| 527 | (python-info-assignment-continuation-line-p))) | ||
| 528 | (indentation (cond (block-continuation | ||
| 529 | (goto-char block-continuation) | ||
| 530 | (re-search-forward | ||
| 531 | (python-rx block-start (* space)) | ||
| 532 | (line-end-position) t) | ||
| 533 | (current-column)) | ||
| 534 | (assignment-continuation | ||
| 535 | (goto-char assignment-continuation) | ||
| 536 | (re-search-forward | ||
| 537 | (python-rx simple-operator) | ||
| 538 | (line-end-position) t) | ||
| 539 | (forward-char 1) | ||
| 540 | (re-search-forward | ||
| 541 | (python-rx (* space)) | ||
| 542 | (line-end-position) t) | ||
| 543 | (current-column)) | ||
| 544 | (t | ||
| 545 | (goto-char context-start) | ||
| 546 | (current-indentation))))) | ||
| 547 | indentation)) | ||
| 548 | ('inside-paren | ||
| 549 | (- | ||
| 550 | (save-excursion | ||
| 551 | (goto-char context-start) | ||
| 552 | (forward-char) | ||
| 553 | (if (looking-at "[[:space:]]*$") | ||
| 554 | (+ (current-indentation) python-indent-offset) | ||
| 555 | (forward-comment 1) | ||
| 556 | (current-column))) | ||
| 557 | (if (progn | ||
| 558 | (back-to-indentation) | ||
| 559 | (looking-at (regexp-opt '(")" "]" "}")))) | ||
| 560 | python-indent-offset | ||
| 561 | 0)))))))) | ||
| 562 | |||
| 563 | (defun python-indent-calculate-levels () | ||
| 564 | "Calculate `python-indent-levels' and reset `python-indent-current-level'." | ||
| 565 | (let* ((indentation (python-indent-calculate-indentation)) | ||
| 566 | (remainder (% indentation python-indent-offset)) | ||
| 567 | (steps (/ (- indentation remainder) python-indent-offset))) | ||
| 568 | (setq python-indent-levels '()) | ||
| 569 | (setq python-indent-levels (cons 0 python-indent-levels)) | ||
| 570 | (dotimes (step steps) | ||
| 571 | (setq python-indent-levels | ||
| 572 | (cons (* python-indent-offset (1+ step)) python-indent-levels))) | ||
| 573 | (when (not (eq 0 remainder)) | ||
| 574 | (setq python-indent-levels | ||
| 575 | (cons (+ (* python-indent-offset steps) remainder) | ||
| 576 | python-indent-levels))) | ||
| 577 | (setq python-indent-levels (nreverse python-indent-levels)) | ||
| 578 | (setq python-indent-current-level (1- (length python-indent-levels))))) | ||
| 579 | |||
| 580 | (defun python-indent-toggle-levels () | ||
| 581 | "Toggle `python-indent-current-level' over `python-indent-levels'." | ||
| 582 | (setq python-indent-current-level (1- python-indent-current-level)) | ||
| 583 | (when (< python-indent-current-level 0) | ||
| 584 | (setq python-indent-current-level (1- (length python-indent-levels))))) | ||
| 585 | |||
| 586 | (defun python-indent-line (&optional force-toggle) | ||
| 587 | "Internal implementation of `python-indent-line-function'. | ||
| 588 | |||
| 589 | Uses the offset calculated in | ||
| 590 | `python-indent-calculate-indentation' and available levels | ||
| 591 | indicated by the variable `python-indent-levels'. | ||
| 592 | |||
| 593 | When the variable `last-command' is equal to | ||
| 594 | `indent-for-tab-command' or FORCE-TOGGLE is non-nil: | ||
| 595 | |||
| 596 | * Cycles levels indicated in the variable `python-indent-levels' | ||
| 597 | by setting the current level in the variable | ||
| 598 | `python-indent-current-level'. | ||
| 599 | |||
| 600 | When the variable `last-command' is not equal to | ||
| 601 | `indent-for-tab-command' and FORCE-TOGGLE is nil: | ||
| 602 | |||
| 603 | * calculates possible indentation levels and saves it in the | ||
| 604 | variable `python-indent-levels'. | ||
| 605 | |||
| 606 | * sets the variable `python-indent-current-level' correctly so | ||
| 607 | offset is equal to (`nth' `python-indent-current-level' | ||
| 608 | `python-indent-levels')" | ||
| 609 | (if (or (and (eq this-command 'indent-for-tab-command) | ||
| 610 | (eq last-command this-command)) | ||
| 611 | force-toggle) | ||
| 612 | (python-indent-toggle-levels) | ||
| 613 | (python-indent-calculate-levels)) | ||
| 614 | (beginning-of-line) | ||
| 615 | (delete-horizontal-space) | ||
| 616 | (indent-to (nth python-indent-current-level python-indent-levels)) | ||
| 617 | (save-restriction | ||
| 618 | (widen) | ||
| 619 | (let ((closing-block-point (python-info-closing-block))) | ||
| 620 | (when closing-block-point | ||
| 621 | (message "Closes %s" (buffer-substring | ||
| 622 | closing-block-point | ||
| 623 | (save-excursion | ||
| 624 | (goto-char closing-block-point) | ||
| 625 | (line-end-position)))))))) | ||
| 626 | |||
| 627 | (defun python-indent-line-function () | ||
| 628 | "`indent-line-function' for Python mode. | ||
| 629 | Internally just calls `python-indent-line'." | ||
| 630 | (python-indent-line)) | ||
| 631 | |||
| 632 | (defun python-indent-dedent-line () | ||
| 633 | "Dedent current line." | ||
| 634 | (interactive "*") | ||
| 635 | (when (and (not (syntax-ppss-context (syntax-ppss))) | ||
| 636 | (<= (point-marker) (save-excursion | ||
| 637 | (back-to-indentation) | ||
| 638 | (point-marker))) | ||
| 639 | (> (current-column) 0)) | ||
| 640 | (python-indent-line t) | ||
| 641 | t)) | ||
| 642 | |||
| 643 | (defun python-indent-dedent-line-backspace (arg) | ||
| 644 | "Dedent current line. | ||
| 645 | Argument ARG is passed to `backward-delete-char-untabify' when | ||
| 646 | point is not in between the indentation." | ||
| 647 | (interactive "*p") | ||
| 648 | (when (not (python-indent-dedent-line)) | ||
| 649 | (backward-delete-char-untabify arg))) | ||
| 650 | |||
| 651 | (defun python-indent-region (start end) | ||
| 652 | "Indent a python region automagically. | ||
| 653 | |||
| 654 | Called from a program, START and END specify the region to indent." | ||
| 655 | (save-excursion | ||
| 656 | (goto-char end) | ||
| 657 | (setq end (point-marker)) | ||
| 658 | (goto-char start) | ||
| 659 | (or (bolp) (forward-line 1)) | ||
| 660 | (while (< (point) end) | ||
| 661 | (or (and (bolp) (eolp)) | ||
| 662 | (let (word) | ||
| 663 | (forward-line -1) | ||
| 664 | (back-to-indentation) | ||
| 665 | (setq word (current-word)) | ||
| 666 | (forward-line 1) | ||
| 667 | (when word | ||
| 668 | (beginning-of-line) | ||
| 669 | (delete-horizontal-space) | ||
| 670 | (indent-to (python-indent-calculate-indentation))))) | ||
| 671 | (forward-line 1)) | ||
| 672 | (move-marker end nil))) | ||
| 673 | |||
| 674 | (defun python-indent-shift-left (start end &optional count) | ||
| 675 | "Shift lines contained in region START END by COUNT columns to the left. | ||
| 676 | |||
| 677 | COUNT defaults to `python-indent-offset'. | ||
| 678 | |||
| 679 | If region isn't active, the current line is shifted. | ||
| 680 | |||
| 681 | The shifted region includes the lines in which START and END lie. | ||
| 682 | |||
| 683 | An error is signaled if any lines in the region are indented less | ||
| 684 | than COUNT columns." | ||
| 685 | (interactive | ||
| 686 | (if mark-active | ||
| 687 | (list (region-beginning) (region-end) current-prefix-arg) | ||
| 688 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | ||
| 689 | (if count | ||
| 690 | (setq count (prefix-numeric-value count)) | ||
| 691 | (setq count python-indent-offset)) | ||
| 692 | (when (> count 0) | ||
| 693 | (save-excursion | ||
| 694 | (goto-char start) | ||
| 695 | (while (< (point) end) | ||
| 696 | (if (and (< (current-indentation) count) | ||
| 697 | (not (looking-at "[ \t]*$"))) | ||
| 698 | (error "Can't shift all lines enough")) | ||
| 699 | (forward-line)) | ||
| 700 | (indent-rigidly start end (- count))))) | ||
| 701 | |||
| 702 | (add-to-list 'debug-ignored-errors "^Can't shift all lines enough") | ||
| 703 | |||
| 704 | (defun python-indent-shift-right (start end &optional count) | ||
| 705 | "Shift lines contained in region START END by COUNT columns to the left. | ||
| 706 | |||
| 707 | COUNT defaults to `python-indent-offset'. | ||
| 708 | |||
| 709 | If region isn't active, the current line is shifted. | ||
| 710 | |||
| 711 | The shifted region includes the lines in which START and END | ||
| 712 | lie." | ||
| 713 | (interactive | ||
| 714 | (if mark-active | ||
| 715 | (list (region-beginning) (region-end) current-prefix-arg) | ||
| 716 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | ||
| 717 | (if count | ||
| 718 | (setq count (prefix-numeric-value count)) | ||
| 719 | (setq count python-indent-offset)) | ||
| 720 | (indent-rigidly start end count)) | ||
| 721 | |||
| 722 | |||
| 723 | ;;; Navigation | ||
| 724 | |||
| 725 | (defvar python-beginning-of-defun-regexp | ||
| 726 | "^\\(def\\|class\\)[[:space:]]+[[:word:]]+" | ||
| 727 | "Regular expresion matching beginning of outermost class or function.") | ||
| 728 | |||
| 729 | (defvar python-beginning-of-innermost-defun-regexp | ||
| 730 | "^[[:space:]]*\\(def\\|class\\)[[:space:]]+[[:word:]]+" | ||
| 731 | "Regular expresion matching beginning of innermost class or function.") | ||
| 732 | |||
| 733 | (defun python-beginning-of-defun (&optional innermost) | ||
| 734 | "Move point to the beginning of innermost/outermost def or class. | ||
| 735 | If INNERMOST is non-nil then move to the beginning of the | ||
| 736 | innermost definition." | ||
| 737 | (let ((starting-point (point-marker)) | ||
| 738 | (nonblank-line-indent) | ||
| 739 | (defun-indent) | ||
| 740 | (defun-point) | ||
| 741 | (regexp (if innermost | ||
| 742 | python-beginning-of-innermost-defun-regexp | ||
| 743 | python-beginning-of-defun-regexp))) | ||
| 744 | (back-to-indentation) | ||
| 745 | (if (and (not (looking-at "@")) | ||
| 746 | (not (looking-at regexp))) | ||
| 747 | (forward-comment -1) | ||
| 748 | (while (and (not (eobp)) | ||
| 749 | (forward-line 1) | ||
| 750 | (not (back-to-indentation)) | ||
| 751 | (looking-at "@")))) | ||
| 752 | (when (not (looking-at regexp)) | ||
| 753 | (re-search-backward regexp nil t)) | ||
| 754 | (setq nonblank-line-indent (+ (current-indentation) python-indent-offset)) | ||
| 755 | (setq defun-indent (current-indentation)) | ||
| 756 | (setq defun-point (point-marker)) | ||
| 757 | (if (> nonblank-line-indent defun-indent) | ||
| 758 | (progn | ||
| 759 | (goto-char defun-point) | ||
| 760 | (forward-line -1) | ||
| 761 | (while (and (looking-at "@") | ||
| 762 | (forward-line -1) | ||
| 763 | (not (bobp)) | ||
| 764 | (not (back-to-indentation)))) | ||
| 765 | (forward-line 1) | ||
| 766 | (point-marker)) | ||
| 767 | (if innermost | ||
| 768 | (python-beginning-of-defun) | ||
| 769 | (goto-char starting-point) | ||
| 770 | nil)))) | ||
| 771 | |||
| 772 | (defun python-beginning-of-defun-function () | ||
| 773 | "Move point to the beginning of outermost def or class. | ||
| 774 | Returns nil if point is not in a def or class." | ||
| 775 | (python-beginning-of-defun nil)) | ||
| 776 | |||
| 777 | (defun python-beginning-of-innermost-defun () | ||
| 778 | "Move point to the beginning of innermost def or class. | ||
| 779 | Returns nil if point is not in a def or class." | ||
| 780 | (interactive) | ||
| 781 | (python-beginning-of-defun t)) | ||
| 782 | |||
| 783 | (defun python-end-of-defun-function () | ||
| 784 | "Move point to the end of def or class. | ||
| 785 | Returns nil if point is not in a def or class." | ||
| 786 | (let ((starting-point (point-marker)) | ||
| 787 | (defun-regexp (python-rx defun)) | ||
| 788 | (beg-defun-indent)) | ||
| 789 | (back-to-indentation) | ||
| 790 | (if (looking-at "@") | ||
| 791 | (while (and (not (eobp)) | ||
| 792 | (forward-line 1) | ||
| 793 | (not (back-to-indentation)) | ||
| 794 | (looking-at "@"))) | ||
| 795 | (while (and (not (bobp)) | ||
| 796 | (not (progn (back-to-indentation) (current-word))) | ||
| 797 | (forward-line -1)))) | ||
| 798 | (when (or (not (equal (current-indentation) 0)) | ||
| 799 | (string-match defun-regexp (current-word))) | ||
| 800 | (setq beg-defun-indent (save-excursion | ||
| 801 | (or (looking-at defun-regexp) | ||
| 802 | (python-beginning-of-innermost-defun)) | ||
| 803 | (current-indentation))) | ||
| 804 | (while (and (forward-line 1) | ||
| 805 | (not (eobp)) | ||
| 806 | (or (not (current-word)) | ||
| 807 | (> (current-indentation) beg-defun-indent)))) | ||
| 808 | (while (and (forward-comment -1) | ||
| 809 | (not (bobp)))) | ||
| 810 | (forward-line 1) | ||
| 811 | (point-marker)))) | ||
| 812 | |||
| 813 | |||
| 814 | ;;; Shell integration | ||
| 815 | |||
| 816 | (defvar python-shell-buffer-name "Python" | ||
| 817 | "Default buffer name for Python interpreter.") | ||
| 818 | |||
| 819 | (defcustom python-shell-interpreter "python" | ||
| 820 | "Default Python interpreter for shell." | ||
| 821 | :group 'python | ||
| 822 | :type 'string | ||
| 823 | :safe 'stringp) | ||
| 824 | |||
| 825 | (defcustom python-shell-interpreter-args "-i" | ||
| 826 | "Default arguments for the Python interpreter." | ||
| 827 | :group 'python | ||
| 828 | :type 'string | ||
| 829 | :safe 'stringp) | ||
| 830 | |||
| 831 | (defcustom python-shell-prompt-regexp ">>> " | ||
| 832 | "Regex matching top\-level input prompt of python shell. | ||
| 833 | The regex should not contain a caret (^) at the beginning." | ||
| 834 | :type 'string | ||
| 835 | :group 'python | ||
| 836 | :safe 'stringp) | ||
| 837 | |||
| 838 | (defcustom python-shell-prompt-block-regexp "[.][.][.] " | ||
| 839 | "Regex matching block input prompt of python shell. | ||
| 840 | The regex should not contain a caret (^) at the beginning." | ||
| 841 | :type 'string | ||
| 842 | :group 'python | ||
| 843 | :safe 'stringp) | ||
| 844 | |||
| 845 | (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ " | ||
| 846 | "Regex matching pdb input prompt of python shell. | ||
| 847 | The regex should not contain a caret (^) at the beginning." | ||
| 848 | :type 'string | ||
| 849 | :group 'python | ||
| 850 | :safe 'stringp) | ||
| 851 | |||
| 852 | (defcustom python-shell-compilation-regexp-alist | ||
| 853 | `((,(rx line-start (1+ (any " \t")) "File \"" | ||
| 854 | (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c | ||
| 855 | "\", line " (group (1+ digit))) | ||
| 856 | 1 2) | ||
| 857 | (,(rx " in file " (group (1+ not-newline)) " on line " | ||
| 858 | (group (1+ digit))) | ||
| 859 | 1 2) | ||
| 860 | (,(rx line-start "> " (group (1+ (not (any "(\"<")))) | ||
| 861 | "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()") | ||
| 862 | 1 2)) | ||
| 863 | "`compilation-error-regexp-alist' for inferior Python." | ||
| 864 | :type '(alist string) | ||
| 865 | :group 'python) | ||
| 866 | |||
| 867 | (defun python-shell-get-process-name (dedicated) | ||
| 868 | "Calculate the appropiate process name for inferior Python process. | ||
| 869 | |||
| 870 | If DEDICATED is t and the variable `buffer-file-name' is non-nil | ||
| 871 | returns a string with the form | ||
| 872 | `python-shell-buffer-name'[variable `buffer-file-name'] else | ||
| 873 | returns the value of `python-shell-buffer-name'. | ||
| 874 | |||
| 875 | After calculating the process name add the buffer name for the | ||
| 876 | process in the `same-window-buffer-names' list" | ||
| 877 | (let ((process-name | ||
| 878 | (if (and dedicated | ||
| 879 | buffer-file-name) | ||
| 880 | (format "%s[%s]" python-shell-buffer-name buffer-file-name) | ||
| 881 | (format "%s" python-shell-buffer-name)))) | ||
| 882 | (add-to-list 'same-window-buffer-names (purecopy | ||
| 883 | (format "*%s*" process-name))) | ||
| 884 | process-name)) | ||
| 885 | |||
| 886 | (defun python-shell-parse-command () | ||
| 887 | "Calculates the string used to execute the inferior Python process." | ||
| 888 | (format "%s %s" python-shell-interpreter python-shell-interpreter-args)) | ||
| 889 | |||
| 890 | (defun python-comint-output-filter-function (output) | ||
| 891 | "Hook run after content is put into comint buffer. | ||
| 892 | OUTPUT is a string with the contents of the buffer." | ||
| 893 | (ansi-color-filter-apply output)) | ||
| 894 | |||
| 895 | (defvar inferior-python-mode-current-file nil | ||
| 896 | "Current file from which a region was sent.") | ||
| 897 | (make-variable-buffer-local 'inferior-python-mode-current-file) | ||
| 898 | |||
| 899 | (defvar inferior-python-mode-current-temp-file nil | ||
| 900 | "Current temp file sent to process.") | ||
| 901 | (make-variable-buffer-local 'inferior-python-mode-current-file) | ||
| 902 | |||
| 903 | (define-derived-mode inferior-python-mode comint-mode "Inferior Python" | ||
| 904 | "Major mode for Python inferior process." | ||
| 905 | (set-syntax-table python-mode-syntax-table) | ||
| 906 | (setq mode-line-process '(":%s")) | ||
| 907 | (setq comint-prompt-regexp (format "^\\(?:%s\\|%s\\|%s\\)" | ||
| 908 | python-shell-prompt-regexp | ||
| 909 | python-shell-prompt-block-regexp | ||
| 910 | python-shell-prompt-pdb-regexp)) | ||
| 911 | (make-local-variable 'comint-output-filter-functions) | ||
| 912 | (add-hook 'comint-output-filter-functions | ||
| 913 | 'python-comint-output-filter-function) | ||
| 914 | (add-hook 'comint-output-filter-functions | ||
| 915 | 'python-pdbtrack-comint-output-filter-function) | ||
| 916 | (set (make-local-variable 'compilation-error-regexp-alist) | ||
| 917 | python-shell-compilation-regexp-alist) | ||
| 918 | (compilation-shell-minor-mode 1)) | ||
| 919 | |||
| 920 | (defun run-python (dedicated cmd) | ||
| 921 | "Run an inferior Python process. | ||
| 922 | |||
| 923 | Input and output via buffer *\\[python-shell-buffer-name]*. | ||
| 924 | |||
| 925 | If there is a process already running in | ||
| 926 | *\\[python-shell-buffer-name]*, switch to that buffer. | ||
| 927 | |||
| 928 | With argument, allows you to: | ||
| 929 | |||
| 930 | * Define DEDICATED so a dedicated process for the current buffer | ||
| 931 | is open. | ||
| 932 | |||
| 933 | * Define CMD so you can edit the command used to call the | ||
| 934 | interpreter (default is value of `python-shell-interpreter' and | ||
| 935 | arguments defined in `python-shell-interpreter-args'). | ||
| 936 | |||
| 937 | Runs the hook `inferior-python-mode-hook' (after the | ||
| 938 | `comint-mode-hook' is run). | ||
| 939 | |||
| 940 | \(Type \\[describe-mode] in the process buffer for a list of | ||
| 941 | commands.)" | ||
| 942 | (interactive | ||
| 943 | (if current-prefix-arg | ||
| 944 | (list | ||
| 945 | (y-or-n-p "Make dedicated process? ") | ||
| 946 | (read-string "Run Python: " (python-shell-parse-command))) | ||
| 947 | (list nil (python-shell-parse-command)))) | ||
| 948 | (let* ((proc-name (python-shell-get-process-name dedicated)) | ||
| 949 | (proc-buffer-name (format "*%s*" proc-name))) | ||
| 950 | (when (not (comint-check-proc proc-buffer-name)) | ||
| 951 | (let ((cmdlist (split-string-and-unquote cmd))) | ||
| 952 | (set-buffer | ||
| 953 | (apply 'make-comint proc-name (car cmdlist) nil | ||
| 954 | (cdr cmdlist))) | ||
| 955 | (inferior-python-mode))) | ||
| 956 | (pop-to-buffer proc-buffer-name)) | ||
| 957 | dedicated) | ||
| 958 | |||
| 959 | (defun python-shell-get-process () | ||
| 960 | "Get inferior Python process for current buffer and return it." | ||
| 961 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) | ||
| 962 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) | ||
| 963 | (global-proc-name (python-shell-get-process-name nil)) | ||
| 964 | (global-proc-buffer-name (format "*%s*" global-proc-name)) | ||
| 965 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | ||
| 966 | (global-running (comint-check-proc global-proc-buffer-name))) | ||
| 967 | ;; Always prefer dedicated | ||
| 968 | (get-buffer-process (or (and dedicated-running dedicated-proc-buffer-name) | ||
| 969 | (and global-running global-proc-buffer-name))))) | ||
| 970 | |||
| 971 | (defun python-shell-get-or-create-process () | ||
| 972 | "Get or create an inferior Python process for current buffer and return it." | ||
| 973 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) | ||
| 974 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) | ||
| 975 | (global-proc-name (python-shell-get-process-name nil)) | ||
| 976 | (global-proc-buffer-name (format "*%s*" global-proc-name)) | ||
| 977 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | ||
| 978 | (global-running (comint-check-proc global-proc-buffer-name)) | ||
| 979 | (current-prefix-arg 4)) | ||
| 980 | (when (and (not dedicated-running) (not global-running)) | ||
| 981 | (if (call-interactively 'run-python) | ||
| 982 | (setq dedicated-running t) | ||
| 983 | (setq global-running t))) | ||
| 984 | ;; Always prefer dedicated | ||
| 985 | (get-buffer-process (if dedicated-running | ||
| 986 | dedicated-proc-buffer-name | ||
| 987 | global-proc-buffer-name)))) | ||
| 988 | |||
| 989 | (defun python-shell-send-string (string) | ||
| 990 | "Send STRING to inferior Python process." | ||
| 991 | (interactive "sPython command: ") | ||
| 992 | (let ((process (python-shell-get-or-create-process))) | ||
| 993 | (message (format "Sent: %s..." string)) | ||
| 994 | (comint-send-string process string) | ||
| 995 | (when (or (not (string-match "\n$" string)) | ||
| 996 | (string-match "\n[ \t].*\n?$" string)) | ||
| 997 | (comint-send-string process "\n")))) | ||
| 998 | |||
| 999 | (defun python-shell-send-region (start end) | ||
| 1000 | "Send the region delimited by START and END to inferior Python process." | ||
| 1001 | (interactive "r") | ||
| 1002 | (let* ((contents (buffer-substring start end)) | ||
| 1003 | (current-file (buffer-file-name)) | ||
| 1004 | (process (python-shell-get-or-create-process)) | ||
| 1005 | (temp-file (make-temp-file "py"))) | ||
| 1006 | (with-temp-file temp-file | ||
| 1007 | (insert contents) | ||
| 1008 | (delete-trailing-whitespace) | ||
| 1009 | (goto-char (point-min)) | ||
| 1010 | (message (format "Sent: %s..." | ||
| 1011 | (buffer-substring (point-min) | ||
| 1012 | (line-end-position))))) | ||
| 1013 | (with-current-buffer (process-buffer process) | ||
| 1014 | (setq inferior-python-mode-current-file current-file) | ||
| 1015 | (setq inferior-python-mode-current-temp-file temp-file)) | ||
| 1016 | (comint-send-string process (format "execfile(r'%s')\n" temp-file)))) | ||
| 1017 | |||
| 1018 | (defun python-shell-send-buffer () | ||
| 1019 | "Send the entire buffer to inferior Python process." | ||
| 1020 | (interactive) | ||
| 1021 | (save-restriction | ||
| 1022 | (widen) | ||
| 1023 | (python-shell-send-region (point-min) (point-max)))) | ||
| 1024 | |||
| 1025 | (defun python-shell-send-defun (arg) | ||
| 1026 | "Send the (inner|outer)most def or class to inferior Python process. | ||
| 1027 | When argument ARG is non-nil sends the innermost defun." | ||
| 1028 | (interactive "P") | ||
| 1029 | (save-excursion | ||
| 1030 | (python-shell-send-region (progn | ||
| 1031 | (or (if arg | ||
| 1032 | (python-beginning-of-innermost-defun) | ||
| 1033 | (python-beginning-of-defun-function)) | ||
| 1034 | (progn (beginning-of-line) (point-marker)))) | ||
| 1035 | (progn | ||
| 1036 | (or (python-end-of-defun-function) | ||
| 1037 | (progn (end-of-line) (point-marker))))))) | ||
| 1038 | |||
| 1039 | (defun python-shell-send-file (file-name) | ||
| 1040 | "Send FILE-NAME to inferior Python process." | ||
| 1041 | (interactive "fFile to send: ") | ||
| 1042 | (comint-send-string | ||
| 1043 | (python-shell-get-or-create-process) | ||
| 1044 | (format "execfile('%s')\n" (expand-file-name file-name)))) | ||
| 1045 | |||
| 1046 | (defun python-shell-switch-to-shell () | ||
| 1047 | "Switch to inferior Python process buffer." | ||
| 1048 | (interactive) | ||
| 1049 | (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t)) | ||
| 1050 | |||
| 1051 | |||
| 1052 | ;;; Shell completion | ||
| 1053 | |||
| 1054 | (defvar python-shell-completion-setup-code | ||
| 1055 | "try: | ||
| 1056 | import readline | ||
| 1057 | except ImportError: | ||
| 1058 | def __COMPLETER_all_completions(text): [] | ||
| 1059 | else: | ||
| 1060 | import rlcompleter | ||
| 1061 | readline.set_completer(rlcompleter.Completer().complete) | ||
| 1062 | def __COMPLETER_all_completions(text): | ||
| 1063 | import sys | ||
| 1064 | completions = [] | ||
| 1065 | try: | ||
| 1066 | i = 0 | ||
| 1067 | while True: | ||
| 1068 | res = readline.get_completer()(text, i) | ||
| 1069 | if not res: break | ||
| 1070 | i += 1 | ||
| 1071 | completions.append(res) | ||
| 1072 | except NameError: | ||
| 1073 | pass | ||
| 1074 | return completions" | ||
| 1075 | "Code used to setup completion in inferior Python processes.") | ||
| 1076 | |||
| 1077 | (defvar python-shell-completion-strings-code | ||
| 1078 | "';'.join(__COMPLETER_all_completions('''%s'''))\n" | ||
| 1079 | "Python code used to get a string of completions separated by semicolons.") | ||
| 1080 | |||
| 1081 | (defun python-shell-completion-setup () | ||
| 1082 | "Send `python-shell-completion-setup-code' to inferior Python process. | ||
| 1083 | Also binds <tab> to `python-shell-complete-or-indent' in the | ||
| 1084 | `inferior-python-mode-map' and adds | ||
| 1085 | `python-shell-completion-complete-at-point' to the | ||
| 1086 | `comint-dynamic-complete-functions' list. | ||
| 1087 | It is specially designed to be added to the | ||
| 1088 | `inferior-python-mode-hook'." | ||
| 1089 | (when python-shell-completion-setup-code | ||
| 1090 | (let ((temp-file (make-temp-file "py")) | ||
| 1091 | (process (get-buffer-process (current-buffer)))) | ||
| 1092 | (with-temp-file temp-file | ||
| 1093 | (insert python-shell-completion-setup-code) | ||
| 1094 | (delete-trailing-whitespace) | ||
| 1095 | (goto-char (point-min))) | ||
| 1096 | (comint-send-string process | ||
| 1097 | (format "execfile(r'%s')\n" temp-file)) | ||
| 1098 | (message (format "Completion setup code sent."))) | ||
| 1099 | (add-to-list (make-local-variable | ||
| 1100 | 'comint-dynamic-complete-functions) | ||
| 1101 | 'python-shell-completion-complete-at-point) | ||
| 1102 | (define-key inferior-python-mode-map (kbd "<tab>") | ||
| 1103 | 'python-shell-completion-complete-or-indent))) | ||
| 1104 | |||
| 1105 | (defun python-shell-completion-complete-at-point () | ||
| 1106 | "Perform completion at point in inferior Python process." | ||
| 1107 | (interactive) | ||
| 1108 | (when (and comint-last-prompt-overlay | ||
| 1109 | (> (point-marker) (overlay-end comint-last-prompt-overlay))) | ||
| 1110 | (let* ((process (get-buffer-process (current-buffer))) | ||
| 1111 | (input (comint-word (current-word))) | ||
| 1112 | (completions (when input | ||
| 1113 | (delete-region (point-marker) | ||
| 1114 | (progn | ||
| 1115 | (forward-char (- (length input))) | ||
| 1116 | (point-marker))) | ||
| 1117 | (process-send-string | ||
| 1118 | process | ||
| 1119 | (format | ||
| 1120 | python-shell-completion-strings-code input)) | ||
| 1121 | (accept-process-output process) | ||
| 1122 | (save-excursion | ||
| 1123 | (re-search-backward comint-prompt-regexp | ||
| 1124 | comint-last-input-end t) | ||
| 1125 | (split-string | ||
| 1126 | (buffer-substring-no-properties | ||
| 1127 | (point-marker) comint-last-input-end) | ||
| 1128 | ";\\|\"\\|'\\|(" t)))) | ||
| 1129 | (completion (when completions (try-completion input completions)))) | ||
| 1130 | (when completions | ||
| 1131 | (save-excursion | ||
| 1132 | (forward-line -1) | ||
| 1133 | (kill-line 1))) | ||
| 1134 | (cond ((eq completion t) | ||
| 1135 | (when input (insert input))) | ||
| 1136 | ((null completion) | ||
| 1137 | (when input (insert input)) | ||
| 1138 | (message "Can't find completion for \"%s\"" input) | ||
| 1139 | (ding)) | ||
| 1140 | ((not (string= input completion)) | ||
| 1141 | (insert completion)) | ||
| 1142 | (t | ||
| 1143 | (message "Making completion list...") | ||
| 1144 | (when input (insert input)) | ||
| 1145 | (with-output-to-temp-buffer "*Python Completions*" | ||
| 1146 | (display-completion-list | ||
| 1147 | (all-completions input completions)))))))) | ||
| 1148 | |||
| 1149 | |||
| 1150 | (defun python-shell-completion-complete-or-indent () | ||
| 1151 | "Complete or indent depending on the context. | ||
| 1152 | If content before pointer is all whitespace indent. If not try to | ||
| 1153 | complete." | ||
| 1154 | (interactive) | ||
| 1155 | (if (string-match "^[[:space:]]*$" | ||
| 1156 | (buffer-substring (comint-line-beginning-position) | ||
| 1157 | (point-marker))) | ||
| 1158 | (indent-for-tab-command) | ||
| 1159 | (comint-dynamic-complete))) | ||
| 1160 | |||
| 1161 | (add-hook 'inferior-python-mode-hook | ||
| 1162 | #'python-shell-completion-setup) | ||
| 1163 | |||
| 1164 | |||
| 1165 | ;;; PDB Track integration | ||
| 1166 | |||
| 1167 | (defvar python-pdbtrack-stacktrace-info-regexp | ||
| 1168 | "> %s(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()" | ||
| 1169 | "Regexp matching stacktrace information. | ||
| 1170 | It is used to extract the current line and module beign | ||
| 1171 | inspected. | ||
| 1172 | The regexp should not start with a caret (^) and can contain a | ||
| 1173 | string placeholder (\%s) which is replaced with the filename | ||
| 1174 | beign inspected (so other files in the debugging process are not | ||
| 1175 | opened)") | ||
| 1176 | |||
| 1177 | (defvar python-pdbtrack-tracking-buffers '() | ||
| 1178 | "Alist containing elements of form (#<buffer> . #<buffer>). | ||
| 1179 | The car of each element of the alist is the tracking buffer and | ||
| 1180 | the cdr is the tracked buffer.") | ||
| 1181 | |||
| 1182 | (defun python-pdbtrack-get-or-add-tracking-buffers () | ||
| 1183 | "Get/Add a tracked buffer for the current buffer. | ||
| 1184 | Internally it uses the `python-pdbtrack-tracking-buffers' alist. | ||
| 1185 | Returns a cons with the form: | ||
| 1186 | * (#<tracking buffer> . #< tracked buffer>)." | ||
| 1187 | (or | ||
| 1188 | (assq (current-buffer) python-pdbtrack-tracking-buffers) | ||
| 1189 | (let* ((file (with-current-buffer (current-buffer) | ||
| 1190 | (or inferior-python-mode-current-file | ||
| 1191 | inferior-python-mode-current-temp-file))) | ||
| 1192 | (tracking-buffers | ||
| 1193 | `(,(current-buffer) . | ||
| 1194 | ,(or (get-file-buffer file) | ||
| 1195 | (find-file-noselect file))))) | ||
| 1196 | (set-buffer (cdr tracking-buffers)) | ||
| 1197 | (python-mode) | ||
| 1198 | (set-buffer (car tracking-buffers)) | ||
| 1199 | (setq python-pdbtrack-tracking-buffers | ||
| 1200 | (cons tracking-buffers python-pdbtrack-tracking-buffers)) | ||
| 1201 | tracking-buffers))) | ||
| 1202 | |||
| 1203 | (defun python-pdbtrack-comint-output-filter-function (output) | ||
| 1204 | "Move overlay arrow to current pdb line in tracked buffer. | ||
| 1205 | Argument OUTPUT is a string with the output from the comint process." | ||
| 1206 | (when (not (string= output "")) | ||
| 1207 | (let ((full-output (ansi-color-filter-apply | ||
| 1208 | (buffer-substring comint-last-input-end | ||
| 1209 | (point-max))))) | ||
| 1210 | (if (string-match python-shell-prompt-pdb-regexp full-output) | ||
| 1211 | (let* ((tracking-buffers (python-pdbtrack-get-or-add-tracking-buffers)) | ||
| 1212 | (line-num | ||
| 1213 | (save-excursion | ||
| 1214 | (string-match | ||
| 1215 | (format python-pdbtrack-stacktrace-info-regexp | ||
| 1216 | (regexp-quote | ||
| 1217 | inferior-python-mode-current-temp-file)) | ||
| 1218 | full-output) | ||
| 1219 | (string-to-number (or (match-string-no-properties 1 full-output) "")))) | ||
| 1220 | (tracked-buffer-window (get-buffer-window (cdr tracking-buffers))) | ||
| 1221 | (tracked-buffer-line-pos)) | ||
| 1222 | (when line-num | ||
| 1223 | (with-current-buffer (cdr tracking-buffers) | ||
| 1224 | (set (make-local-variable 'overlay-arrow-string) "=>") | ||
| 1225 | (set (make-local-variable 'overlay-arrow-position) (make-marker)) | ||
| 1226 | (setq tracked-buffer-line-pos (progn | ||
| 1227 | (goto-char (point-min)) | ||
| 1228 | (forward-line (1- line-num)) | ||
| 1229 | (point-marker))) | ||
| 1230 | (when tracked-buffer-window | ||
| 1231 | (set-window-point tracked-buffer-window tracked-buffer-line-pos)) | ||
| 1232 | (set-marker overlay-arrow-position tracked-buffer-line-pos))) | ||
| 1233 | (pop-to-buffer (cdr tracking-buffers)) | ||
| 1234 | (switch-to-buffer-other-window (car tracking-buffers))) | ||
| 1235 | (let ((tracking-buffers (assq (current-buffer) | ||
| 1236 | python-pdbtrack-tracking-buffers))) | ||
| 1237 | (when tracking-buffers | ||
| 1238 | (if inferior-python-mode-current-file | ||
| 1239 | (with-current-buffer (cdr tracking-buffers) | ||
| 1240 | (set-marker overlay-arrow-position nil)) | ||
| 1241 | (kill-buffer (cdr tracking-buffers))) | ||
| 1242 | (setq python-pdbtrack-tracking-buffers | ||
| 1243 | (assq-delete-all (current-buffer) | ||
| 1244 | python-pdbtrack-tracking-buffers))))))) | ||
| 1245 | output) | ||
| 1246 | |||
| 1247 | |||
| 1248 | ;;; Symbol completion | ||
| 1249 | |||
| 1250 | (defun python-completion-complete-at-point () | ||
| 1251 | "Complete current symbol at point. | ||
| 1252 | For this to work the best as possible you should call | ||
| 1253 | `python-shell-send-buffer' from time to time so context in | ||
| 1254 | inferior python process is updated properly." | ||
| 1255 | (interactive) | ||
| 1256 | (let ((process (python-shell-get-process))) | ||
| 1257 | (if (not process) | ||
| 1258 | (error "Completion needs an inferior Python process running.") | ||
| 1259 | (let* ((input (when (comint-word (current-word)) | ||
| 1260 | (with-syntax-table python-dotty-syntax-table | ||
| 1261 | (buffer-substring (point-marker) | ||
| 1262 | (save-excursion | ||
| 1263 | (forward-word -1) | ||
| 1264 | (point-marker)))))) | ||
| 1265 | (completions (when input | ||
| 1266 | (delete-region (point-marker) | ||
| 1267 | (progn | ||
| 1268 | (forward-char (- (length input))) | ||
| 1269 | (point-marker))) | ||
| 1270 | (process-send-string | ||
| 1271 | process | ||
| 1272 | (format | ||
| 1273 | python-shell-completion-strings-code input)) | ||
| 1274 | (accept-process-output process) | ||
| 1275 | (with-current-buffer (process-buffer process) | ||
| 1276 | (save-excursion | ||
| 1277 | (re-search-backward comint-prompt-regexp | ||
| 1278 | comint-last-input-end t) | ||
| 1279 | (split-string | ||
| 1280 | (buffer-substring-no-properties | ||
| 1281 | (point-marker) comint-last-input-end) | ||
| 1282 | ";\\|\"\\|'\\|(" t))))) | ||
| 1283 | (completion (when completions | ||
| 1284 | (try-completion input completions)))) | ||
| 1285 | (with-current-buffer (process-buffer process) | ||
| 1286 | (save-excursion | ||
| 1287 | (forward-line -1) | ||
| 1288 | (kill-line 1))) | ||
| 1289 | (when completions | ||
| 1290 | (cond ((eq completion t) | ||
| 1291 | (insert input)) | ||
| 1292 | ((null completion) | ||
| 1293 | (insert input) | ||
| 1294 | (message "Can't find completion for \"%s\"" input) | ||
| 1295 | (ding)) | ||
| 1296 | ((not (string= input completion)) | ||
| 1297 | (insert completion)) | ||
| 1298 | (t | ||
| 1299 | (message "Making completion list...") | ||
| 1300 | (insert input) | ||
| 1301 | (with-output-to-temp-buffer "*Python Completions*" | ||
| 1302 | (display-completion-list | ||
| 1303 | (all-completions input completions)))))))))) | ||
| 1304 | |||
| 1305 | (add-to-list 'debug-ignored-errors "^Completion needs an inferior Python process running.") | ||
| 1306 | |||
| 1307 | |||
| 1308 | ;;; Fill paragraph | ||
| 1309 | |||
| 1310 | (defun python-fill-paragraph-function (&optional justify) | ||
| 1311 | "`fill-paragraph-function' handling multi-line strings and possibly comments. | ||
| 1312 | If any of the current line is in or at the end of a multi-line string, | ||
| 1313 | fill the string or the paragraph of it that point is in, preserving | ||
| 1314 | the string's indentation." | ||
| 1315 | (interactive "P") | ||
| 1316 | (save-excursion | ||
| 1317 | (back-to-indentation) | ||
| 1318 | (cond | ||
| 1319 | ;; Comments | ||
| 1320 | ((fill-comment-paragraph justify)) | ||
| 1321 | ;; Docstrings | ||
| 1322 | ((save-excursion (skip-chars-forward "\"'uUrR") | ||
| 1323 | (nth 3 (syntax-ppss))) | ||
| 1324 | (let ((marker (point-marker)) | ||
| 1325 | (string-start-marker | ||
| 1326 | (progn | ||
| 1327 | (skip-chars-forward "\"'uUrR") | ||
| 1328 | (goto-char (nth 8 (syntax-ppss))) | ||
| 1329 | (skip-chars-forward "\"'uUrR") | ||
| 1330 | (point-marker))) | ||
| 1331 | (reg-start (line-beginning-position)) | ||
| 1332 | (string-end-marker | ||
| 1333 | (progn | ||
| 1334 | (while (nth 3 (syntax-ppss)) (goto-char (1+ (point-marker)))) | ||
| 1335 | (skip-chars-backward "\"'") | ||
| 1336 | (point-marker))) | ||
| 1337 | (reg-end (line-end-position)) | ||
| 1338 | (fill-paragraph-function)) | ||
| 1339 | (save-restriction | ||
| 1340 | (narrow-to-region reg-start reg-end) | ||
| 1341 | (save-excursion | ||
| 1342 | (goto-char string-start-marker) | ||
| 1343 | (delete-region (point-marker) (progn | ||
| 1344 | (skip-syntax-forward "> ") | ||
| 1345 | (point-marker))) | ||
| 1346 | (goto-char string-end-marker) | ||
| 1347 | (delete-region (point-marker) (progn | ||
| 1348 | (skip-syntax-backward "> ") | ||
| 1349 | (point-marker))) | ||
| 1350 | (save-excursion | ||
| 1351 | (goto-char marker) | ||
| 1352 | (fill-paragraph justify)) | ||
| 1353 | ;; If there is a newline in the docstring lets put triple | ||
| 1354 | ;; quote in it's own line to follow pep 8 | ||
| 1355 | (when (save-excursion | ||
| 1356 | (re-search-backward "\n" string-start-marker t)) | ||
| 1357 | (newline) | ||
| 1358 | (newline-and-indent)) | ||
| 1359 | (fill-paragraph justify)))) t) | ||
| 1360 | ;; Decorators | ||
| 1361 | ((equal (char-after (save-excursion | ||
| 1362 | (back-to-indentation) | ||
| 1363 | (point-marker))) ?@) t) | ||
| 1364 | ;; Parens | ||
| 1365 | ((or (> (nth 0 (syntax-ppss)) 0) | ||
| 1366 | (looking-at (python-rx open-paren)) | ||
| 1367 | (save-excursion | ||
| 1368 | (skip-syntax-forward "^(" (line-end-position)) | ||
| 1369 | (looking-at (python-rx open-paren)))) | ||
| 1370 | (save-restriction | ||
| 1371 | (narrow-to-region (progn | ||
| 1372 | (while (> (nth 0 (syntax-ppss)) 0) | ||
| 1373 | (goto-char (1- (point-marker)))) | ||
| 1374 | (point-marker) | ||
| 1375 | (line-beginning-position)) | ||
| 1376 | (progn | ||
| 1377 | (when (not (> (nth 0 (syntax-ppss)) 0)) | ||
| 1378 | (end-of-line) | ||
| 1379 | (when (not (> (nth 0 (syntax-ppss)) 0)) | ||
| 1380 | (skip-syntax-backward "^)"))) | ||
| 1381 | (while (> (nth 0 (syntax-ppss)) 0) | ||
| 1382 | (goto-char (1+ (point-marker)))) | ||
| 1383 | (point-marker))) | ||
| 1384 | (let ((paragraph-start "\f\\|[ \t]*$") | ||
| 1385 | (paragraph-separate ",") | ||
| 1386 | (fill-paragraph-function)) | ||
| 1387 | (goto-char (point-min)) | ||
| 1388 | (fill-paragraph justify)) | ||
| 1389 | (while (not (eobp)) | ||
| 1390 | (forward-line 1) | ||
| 1391 | (python-indent-line) | ||
| 1392 | (goto-char (line-end-position)))) t) | ||
| 1393 | (t t)))) | ||
| 1394 | |||
| 1395 | |||
| 1396 | ;;; Eldoc | ||
| 1397 | |||
| 1398 | (defvar python-eldoc-setup-code | ||
| 1399 | "def __PYDOC_get_help(obj): | ||
| 1400 | try: | ||
| 1401 | import pydoc | ||
| 1402 | obj = eval(obj, globals()) | ||
| 1403 | return pydoc.getdoc(obj) | ||
| 1404 | except: | ||
| 1405 | return ''" | ||
| 1406 | "Python code to setup documentation retrieval.") | ||
| 1407 | |||
| 1408 | (defvar python-eldoc-string-code | ||
| 1409 | "print __PYDOC_get_help('''%s''')\n" | ||
| 1410 | "Python code used to get a string with the documentation of an object.") | ||
| 1411 | |||
| 1412 | (defun python-eldoc-setup () | ||
| 1413 | "Send `python-eldoc-setup-code' to inferior Python process. | ||
| 1414 | It is specially designed to be added to the | ||
| 1415 | `inferior-python-mode-hook'." | ||
| 1416 | (when python-eldoc-setup-code | ||
| 1417 | (let ((temp-file (make-temp-file "py"))) | ||
| 1418 | (with-temp-file temp-file | ||
| 1419 | (insert python-eldoc-setup-code) | ||
| 1420 | (delete-trailing-whitespace) | ||
| 1421 | (goto-char (point-min))) | ||
| 1422 | (comint-send-string (get-buffer-process (current-buffer)) | ||
| 1423 | (format "execfile(r'%s')\n" temp-file)) | ||
| 1424 | (message (format "Completion setup code sent."))))) | ||
| 1425 | |||
| 1426 | (defun python-eldoc-function () | ||
| 1427 | "`eldoc-documentation-function' for Python. | ||
| 1428 | For this to work the best as possible you should call | ||
| 1429 | `python-shell-send-buffer' from time to time so context in | ||
| 1430 | inferior python process is updated properly." | ||
| 1431 | (interactive) | ||
| 1432 | (let ((process (python-shell-get-process))) | ||
| 1433 | (if (not process) | ||
| 1434 | "Eldoc needs an inferior Python process running." | ||
| 1435 | (let* ((current-defun (python-info-current-defun)) | ||
| 1436 | (input (with-syntax-table python-dotty-syntax-table | ||
| 1437 | (if (not current-defun) | ||
| 1438 | (current-word) | ||
| 1439 | (concat current-defun "." (current-word))))) | ||
| 1440 | (ppss (syntax-ppss)) | ||
| 1441 | (help (when (and input | ||
| 1442 | (not (string= input (concat current-defun "."))) | ||
| 1443 | (eq nil (nth 3 ppss)) | ||
| 1444 | (eq nil (nth 4 ppss))) | ||
| 1445 | (when (string-match (concat | ||
| 1446 | (regexp-quote (concat current-defun ".")) | ||
| 1447 | "self\\.") input) | ||
| 1448 | (with-temp-buffer | ||
| 1449 | (insert input) | ||
| 1450 | (goto-char (point-min)) | ||
| 1451 | (forward-word) | ||
| 1452 | (forward-char) | ||
| 1453 | (delete-region (point-marker) (search-forward "self.")) | ||
| 1454 | (setq input (buffer-substring (point-min) (point-max))))) | ||
| 1455 | (process-send-string | ||
| 1456 | process (format python-eldoc-string-code input)) | ||
| 1457 | (accept-process-output process) | ||
| 1458 | (with-current-buffer (process-buffer process) | ||
| 1459 | (when comint-last-prompt-overlay | ||
| 1460 | (save-excursion | ||
| 1461 | (goto-char comint-last-input-end) | ||
| 1462 | (re-search-forward comint-prompt-regexp | ||
| 1463 | (line-end-position) t) | ||
| 1464 | (buffer-substring-no-properties | ||
| 1465 | (point-marker) | ||
| 1466 | (overlay-start comint-last-prompt-overlay)))))))) | ||
| 1467 | (with-current-buffer (process-buffer process) | ||
| 1468 | (when comint-last-prompt-overlay | ||
| 1469 | (delete-region comint-last-input-end | ||
| 1470 | (overlay-start comint-last-prompt-overlay)))) | ||
| 1471 | (when (and help | ||
| 1472 | (not (string= help "\n"))) | ||
| 1473 | help))))) | ||
| 1474 | |||
| 1475 | (add-hook 'inferior-python-mode-hook | ||
| 1476 | #'python-eldoc-setup) | ||
| 1477 | |||
| 1478 | |||
| 1479 | ;;; Misc helpers | ||
| 1480 | |||
| 1481 | (defun python-info-current-defun () | ||
| 1482 | "Return name of surrounding function with Python compatible dotty syntax. | ||
| 1483 | This function is compatible to be used as | ||
| 1484 | `add-log-current-defun-function' since it returns nil if point is | ||
| 1485 | not inside a defun." | ||
| 1486 | (let ((names '())) | ||
| 1487 | (save-restriction | ||
| 1488 | (widen) | ||
| 1489 | (save-excursion | ||
| 1490 | (beginning-of-line) | ||
| 1491 | (when (not (>= (current-indentation) python-indent-offset)) | ||
| 1492 | (while (and (not (eobp)) (forward-comment 1)))) | ||
| 1493 | (while (and (not (equal 0 (current-indentation))) | ||
| 1494 | (python-beginning-of-innermost-defun)) | ||
| 1495 | (back-to-indentation) | ||
| 1496 | (looking-at "\\(?:def\\|class\\) +\\([^(]+\\)[^:]+:\\s-*\n") | ||
| 1497 | (setq names (cons (match-string-no-properties 1) names))))) | ||
| 1498 | (when names | ||
| 1499 | (mapconcat (lambda (string) string) names ".")))) | ||
| 1500 | |||
| 1501 | (defun python-info-closing-block () | ||
| 1502 | "Return the point of the block that the current line closes." | ||
| 1503 | (let ((closing-word (save-excursion | ||
| 1504 | (back-to-indentation) | ||
| 1505 | (current-word))) | ||
| 1506 | (indentation (current-indentation))) | ||
| 1507 | (when (member closing-word python-indent-dedenters) | ||
| 1508 | (save-excursion | ||
| 1509 | (forward-line -1) | ||
| 1510 | (while (and (> (current-indentation) indentation) | ||
| 1511 | (not (bobp)) | ||
| 1512 | (not (back-to-indentation)) | ||
| 1513 | (forward-line -1))) | ||
| 1514 | (back-to-indentation) | ||
| 1515 | (cond | ||
| 1516 | ((not (equal indentation (current-indentation))) nil) | ||
| 1517 | ((string= closing-word "elif") | ||
| 1518 | (when (member (current-word) '("if" "elif")) | ||
| 1519 | (point-marker))) | ||
| 1520 | ((string= closing-word "else") | ||
| 1521 | (when (member (current-word) '("if" "elif" "except" "for" "while")) | ||
| 1522 | (point-marker))) | ||
| 1523 | ((string= closing-word "except") | ||
| 1524 | (when (member (current-word) '("try")) | ||
| 1525 | (point-marker))) | ||
| 1526 | ((string= closing-word "finally") | ||
| 1527 | (when (member (current-word) '("except" "else")) | ||
| 1528 | (point-marker)))))))) | ||
| 1529 | |||
| 1530 | (defun python-info-line-ends-backslash-p () | ||
| 1531 | "Return non-nil if current line ends with backslash." | ||
| 1532 | (string= (or (ignore-errors | ||
| 1533 | (buffer-substring | ||
| 1534 | (line-end-position) | ||
| 1535 | (- (line-end-position) 1))) "") "\\")) | ||
| 1536 | |||
| 1537 | (defun python-info-continuation-line-p () | ||
| 1538 | "Return non-nil if current line is continuation of another." | ||
| 1539 | (or (python-info-line-ends-backslash-p) | ||
| 1540 | (string-match ",[[:space:]]*$" (buffer-substring | ||
| 1541 | (line-beginning-position) | ||
| 1542 | (line-end-position))) | ||
| 1543 | (save-excursion | ||
| 1544 | (let ((innermost-paren (progn | ||
| 1545 | (goto-char (line-end-position)) | ||
| 1546 | (nth 1 (syntax-ppss))))) | ||
| 1547 | (when (and innermost-paren | ||
| 1548 | (and (<= (line-beginning-position) innermost-paren) | ||
| 1549 | (>= (line-end-position) innermost-paren))) | ||
| 1550 | (goto-char innermost-paren) | ||
| 1551 | (looking-at (python-rx open-paren (* space) line-end))))) | ||
| 1552 | (save-excursion | ||
| 1553 | (back-to-indentation) | ||
| 1554 | (nth 1 (syntax-ppss))))) | ||
| 1555 | |||
| 1556 | (defun python-info-block-continuation-line-p () | ||
| 1557 | "Return non-nil if current line is a continuation of a block." | ||
| 1558 | (save-excursion | ||
| 1559 | (while (and (not (bobp)) | ||
| 1560 | (python-info-continuation-line-p)) | ||
| 1561 | (forward-line -1)) | ||
| 1562 | (forward-line 1) | ||
| 1563 | (back-to-indentation) | ||
| 1564 | (when (looking-at (python-rx block-start)) | ||
| 1565 | (point-marker)))) | ||
| 1566 | |||
| 1567 | (defun python-info-assignment-continuation-line-p () | ||
| 1568 | "Return non-nil if current line is a continuation of an assignment." | ||
| 1569 | (save-excursion | ||
| 1570 | (while (and (not (bobp)) | ||
| 1571 | (python-info-continuation-line-p)) | ||
| 1572 | (forward-line -1)) | ||
| 1573 | (forward-line 1) | ||
| 1574 | (back-to-indentation) | ||
| 1575 | (when (and (not (looking-at (python-rx block-start))) | ||
| 1576 | (save-excursion | ||
| 1577 | (and (re-search-forward (python-rx not-simple-operator | ||
| 1578 | assignment-operator | ||
| 1579 | not-simple-operator) | ||
| 1580 | (line-end-position) t) | ||
| 1581 | (not (syntax-ppss-context (syntax-ppss)))))) | ||
| 1582 | (point-marker)))) | ||
| 1583 | |||
| 1584 | |||
| 1585 | ;;;###autoload | ||
| 1586 | (define-derived-mode python-mode fundamental-mode "Python" | ||
| 1587 | "A major mode for editing Python files." | ||
| 1588 | (set (make-local-variable 'tab-width) 8) | ||
| 1589 | (set (make-local-variable 'indent-tabs-mode) nil) | ||
| 1590 | |||
| 1591 | (set (make-local-variable 'comment-start) "# ") | ||
| 1592 | (set (make-local-variable 'comment-start-skip) "#+\\s-*") | ||
| 1593 | |||
| 1594 | (set (make-local-variable 'parse-sexp-lookup-properties) t) | ||
| 1595 | (set (make-local-variable 'parse-sexp-ignore-comments) t) | ||
| 1596 | |||
| 1597 | (set (make-local-variable 'font-lock-defaults) | ||
| 1598 | '(python-font-lock-keywords | ||
| 1599 | nil nil nil nil | ||
| 1600 | (font-lock-syntactic-keywords . python-font-lock-syntactic-keywords))) | ||
| 1601 | |||
| 1602 | (set (make-local-variable 'indent-line-function) #'python-indent-line-function) | ||
| 1603 | (set (make-local-variable 'indent-region-function) #'python-indent-region) | ||
| 1604 | |||
| 1605 | (set (make-local-variable 'paragraph-start) "\\s-*$") | ||
| 1606 | (set (make-local-variable 'fill-paragraph-function) 'python-fill-paragraph-function) | ||
| 1607 | |||
| 1608 | (set (make-local-variable 'beginning-of-defun-function) | ||
| 1609 | #'python-beginning-of-defun-function) | ||
| 1610 | (set (make-local-variable 'end-of-defun-function) | ||
| 1611 | #'python-end-of-defun-function) | ||
| 1612 | |||
| 1613 | (add-hook 'completion-at-point-functions | ||
| 1614 | 'python-completion-complete-at-point nil 'local) | ||
| 1615 | |||
| 1616 | (set (make-local-variable 'add-log-current-defun-function) | ||
| 1617 | #'python-info-current-defun) | ||
| 1618 | |||
| 1619 | (set (make-local-variable 'eldoc-documentation-function) | ||
| 1620 | #'python-eldoc-function) | ||
| 1621 | |||
| 1622 | (add-to-list 'hs-special-modes-alist | ||
| 1623 | `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#" | ||
| 1624 | ,(lambda (arg) | ||
| 1625 | (python-end-of-defun-function)) nil)) | ||
| 1626 | |||
| 1627 | (set (make-local-variable 'outline-regexp) | ||
| 1628 | (python-rx (* space) block-start)) | ||
| 1629 | (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n") | ||
| 1630 | (set (make-local-variable 'outline-level) | ||
| 1631 | #'(lambda () | ||
| 1632 | "`outline-level' function for Python mode." | ||
| 1633 | (1+ (/ (current-indentation) python-indent-offset)))) | ||
| 1634 | |||
| 1635 | (when python-indent-guess-indent-offset | ||
| 1636 | (python-indent-guess-indent-offset))) | ||
| 1637 | |||
| 1638 | |||
| 1639 | (provide 'python) | ||
| 1640 | ;;; python.el ends here | ||