]> code.delx.au - gnu-emacs/blob - lisp/progmodes/python.el
Bump python.el version
[gnu-emacs] / lisp / progmodes / python.el
1 ;;; python.el --- Python's flying circus support for Emacs -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2003-2015 Free Software Foundation, Inc.
4
5 ;; Author: Fabián E. Gallina <fabian@anue.biz>
6 ;; URL: https://github.com/fgallina/python.el
7 ;; Version: 0.24.5
8 ;; Maintainer: emacs-devel@gnu.org
9 ;; Created: Jul 2010
10 ;; Keywords: languages
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published
16 ;; by the Free Software Foundation, either version 3 of the License,
17 ;; or (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; Major mode for editing Python files with some fontification and
30 ;; indentation bits extracted from original Dave Love's python.el
31 ;; found in GNU/Emacs.
32
33 ;; Implements Syntax highlighting, Indentation, Movement, Shell
34 ;; interaction, Shell completion, Shell virtualenv support, Pdb
35 ;; tracking, Symbol completion, Skeletons, FFAP, Code Check, Eldoc,
36 ;; Imenu.
37
38 ;; Syntax highlighting: Fontification of code is provided and supports
39 ;; python's triple quoted strings properly.
40
41 ;; Indentation: Automatic indentation with indentation cycling is
42 ;; provided, it allows you to navigate different available levels of
43 ;; indentation by hitting <tab> several times. Also electric-indent-mode
44 ;; is supported such that when inserting a colon the current line is
45 ;; dedented automatically if needed.
46
47 ;; Movement: `beginning-of-defun' and `end-of-defun' functions are
48 ;; properly implemented. There are also specialized
49 ;; `forward-sentence' and `backward-sentence' replacements called
50 ;; `python-nav-forward-block', `python-nav-backward-block'
51 ;; respectively which navigate between beginning of blocks of code.
52 ;; Extra functions `python-nav-forward-statement',
53 ;; `python-nav-backward-statement',
54 ;; `python-nav-beginning-of-statement', `python-nav-end-of-statement',
55 ;; `python-nav-beginning-of-block', `python-nav-end-of-block' and
56 ;; `python-nav-if-name-main' are included but no bound to any key. At
57 ;; last but not least the specialized `python-nav-forward-sexp' allows
58 ;; easy navigation between code blocks. If you prefer `cc-mode'-like
59 ;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
60 ;; enough, You can do that using the `python-mode-hook':
61
62 ;; (add-hook 'python-mode-hook
63 ;; (lambda () (setq forward-sexp-function nil)))
64
65 ;; Shell interaction: is provided and allows opening Python shells
66 ;; inside Emacs and executing any block of code of your current buffer
67 ;; in that inferior Python process.
68
69 ;; Besides that only the standard CPython (2.x and 3.x) shell and
70 ;; IPython are officially supported out of the box, the interaction
71 ;; should support any other readline based Python shells as well
72 ;; (e.g. Jython and Pypy have been reported to work). You can change
73 ;; your default interpreter and commandline arguments by setting the
74 ;; `python-shell-interpreter' and `python-shell-interpreter-args'
75 ;; variables. This example enables IPython globally:
76
77 ;; (setq python-shell-interpreter "ipython"
78 ;; python-shell-interpreter-args "-i")
79
80 ;; Using the "console" subcommand to start IPython in server-client
81 ;; mode is known to fail intermittently due a bug on IPython itself
82 ;; (see URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18052#27').
83 ;; There seems to be a race condition in the IPython server (A.K.A
84 ;; kernel) when code is sent while it is still initializing, sometimes
85 ;; causing the shell to get stalled. With that said, if an IPython
86 ;; kernel is already running, "console --existing" seems to work fine.
87
88 ;; Running IPython on Windows needs more tweaking. The way you should
89 ;; set `python-shell-interpreter' and `python-shell-interpreter-args'
90 ;; is as follows (of course you need to modify the paths according to
91 ;; your system):
92
93 ;; (setq python-shell-interpreter "C:\\Python27\\python.exe"
94 ;; python-shell-interpreter-args
95 ;; "-i C:\\Python27\\Scripts\\ipython-script.py")
96
97 ;; Missing or delayed output used to happen due to differences between
98 ;; Operating Systems' pipe buffering (e.g. CPython 3.3.4 in Windows 7.
99 ;; See URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17304'). To
100 ;; avoid this, the `python-shell-unbuffered' defaults to non-nil and
101 ;; controls whether `python-shell-calculate-process-environment'
102 ;; should set the "PYTHONUNBUFFERED" environment variable on startup:
103 ;; See URL `https://docs.python.org/3/using/cmdline.html#cmdoption-u'.
104
105 ;; The interaction relies upon having prompts for input (e.g. ">>> "
106 ;; and "... " in standard Python shell) and output (e.g. "Out[1]: " in
107 ;; IPython) detected properly. Failing that Emacs may hang but, in
108 ;; the case that happens, you can recover with \\[keyboard-quit]. To
109 ;; avoid this issue, a two-step prompt autodetection mechanism is
110 ;; provided: the first step is manual and consists of a collection of
111 ;; regular expressions matching common prompts for Python shells
112 ;; stored in `python-shell-prompt-input-regexps' and
113 ;; `python-shell-prompt-output-regexps', and dir-local friendly vars
114 ;; `python-shell-prompt-regexp', `python-shell-prompt-block-regexp',
115 ;; `python-shell-prompt-output-regexp' which are appended to the
116 ;; former automatically when a shell spawns; the second step is
117 ;; automatic and depends on the `python-shell-prompt-detect' helper
118 ;; function. See its docstring for details on global variables that
119 ;; modify its behavior.
120
121 ;; Shell completion: hitting tab will try to complete the current
122 ;; word. Shell completion is implemented in such way that if you
123 ;; change the `python-shell-interpreter' it should be possible to
124 ;; integrate custom logic to calculate completions. To achieve this
125 ;; you just need to set `python-shell-completion-setup-code' and
126 ;; `python-shell-completion-string-code'. The default provided code,
127 ;; enables autocompletion for both CPython and IPython (and ideally
128 ;; any readline based Python shell). This code depends on the
129 ;; readline module, so if you are using some Operating System that
130 ;; bundles Python without it (like Windows), installing pyreadline
131 ;; from URL `http://ipython.scipy.org/moin/PyReadline/Intro' should
132 ;; suffice. To troubleshoot why you are not getting any completions
133 ;; you can try the following in your Python shell:
134
135 ;; >>> import readline, rlcompleter
136
137 ;; If you see an error, then you need to either install pyreadline or
138 ;; setup custom code that avoids that dependency.
139
140 ;; Shell virtualenv support: The shell also contains support for
141 ;; virtualenvs and other special environment modifications thanks to
142 ;; `python-shell-process-environment' and `python-shell-exec-path'.
143 ;; These two variables allows you to modify execution paths and
144 ;; environment variables to make easy for you to setup virtualenv rules
145 ;; or behavior modifications when running shells. Here is an example
146 ;; of how to make shell processes to be run using the /path/to/env/
147 ;; virtualenv:
148
149 ;; (setq python-shell-process-environment
150 ;; (list
151 ;; (format "PATH=%s" (mapconcat
152 ;; 'identity
153 ;; (reverse
154 ;; (cons (getenv "PATH")
155 ;; '("/path/to/env/bin/")))
156 ;; ":"))
157 ;; "VIRTUAL_ENV=/path/to/env/"))
158 ;; (python-shell-exec-path . ("/path/to/env/bin/"))
159
160 ;; Since the above is cumbersome and can be programmatically
161 ;; calculated, the variable `python-shell-virtualenv-path' is
162 ;; provided. When this variable is set with the path of the
163 ;; virtualenv to use, `process-environment' and `exec-path' get proper
164 ;; values in order to run shells inside the specified virtualenv. So
165 ;; the following will achieve the same as the previous example:
166
167 ;; (setq python-shell-virtualenv-path "/path/to/env/")
168
169 ;; Also the `python-shell-extra-pythonpaths' variable have been
170 ;; introduced as simple way of adding paths to the PYTHONPATH without
171 ;; affecting existing values.
172
173 ;; Pdb tracking: when you execute a block of code that contains some
174 ;; call to pdb (or ipdb) it will prompt the block of code and will
175 ;; follow the execution of pdb marking the current line with an arrow.
176
177 ;; Symbol completion: you can complete the symbol at point. It uses
178 ;; the shell completion in background so you should run
179 ;; `python-shell-send-buffer' from time to time to get better results.
180
181 ;; Skeletons: 6 skeletons are provided for simple inserting of class,
182 ;; def, for, if, try and while. These skeletons are integrated with
183 ;; abbrev. If you have `abbrev-mode' activated and
184 ;; `python-skeleton-autoinsert' is set to t, then whenever you type
185 ;; the name of any of those defined and hit SPC, they will be
186 ;; automatically expanded. As an alternative you can use the defined
187 ;; skeleton commands: `python-skeleton-class', `python-skeleton-def'
188 ;; `python-skeleton-for', `python-skeleton-if', `python-skeleton-try'
189 ;; and `python-skeleton-while'.
190
191 ;; FFAP: You can find the filename for a given module when using ffap
192 ;; out of the box. This feature needs an inferior python shell
193 ;; running.
194
195 ;; Code check: Check the current file for errors with `python-check'
196 ;; using the program defined in `python-check-command'.
197
198 ;; Eldoc: returns documentation for object at point by using the
199 ;; inferior python subprocess to inspect its documentation. As you
200 ;; might guessed you should run `python-shell-send-buffer' from time
201 ;; to time to get better results too.
202
203 ;; Imenu: There are two index building functions to be used as
204 ;; `imenu-create-index-function': `python-imenu-create-index' (the
205 ;; default one, builds the alist in form of a tree) and
206 ;; `python-imenu-create-flat-index'. See also
207 ;; `python-imenu-format-item-label-function',
208 ;; `python-imenu-format-parent-item-label-function',
209 ;; `python-imenu-format-parent-item-jump-label-function' variables for
210 ;; changing the way labels are formatted in the tree version.
211
212 ;; If you used python-mode.el you may miss auto-indentation when
213 ;; inserting newlines. To achieve the same behavior you have two
214 ;; options:
215
216 ;; 1) Enable the minor-mode `electric-indent-mode' (enabled by
217 ;; default) and use RET. If this mode is disabled use
218 ;; `newline-and-indent', bound to C-j.
219
220 ;; 2) Add the following hook in your .emacs:
221
222 ;; (add-hook 'python-mode-hook
223 ;; #'(lambda ()
224 ;; (define-key python-mode-map "\C-m" 'newline-and-indent)))
225
226 ;; I'd recommend the first one since you'll get the same behavior for
227 ;; all modes out-of-the-box.
228
229 ;;; Installation:
230
231 ;; Add this to your .emacs:
232
233 ;; (add-to-list 'load-path "/folder/containing/file")
234 ;; (require 'python)
235
236 ;;; TODO:
237
238 ;;; Code:
239
240 (require 'ansi-color)
241 (require 'cl-lib)
242 (require 'comint)
243 (require 'json)
244
245 ;; Avoid compiler warnings
246 (defvar view-return-to-alist)
247 (defvar compilation-error-regexp-alist)
248 (defvar outline-heading-end-regexp)
249
250 (autoload 'comint-mode "comint")
251
252 ;;;###autoload
253 (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode))
254 ;;;###autoload
255 (add-to-list 'interpreter-mode-alist (cons (purecopy "python[0-9.]*") 'python-mode))
256
257 (defgroup python nil
258 "Python Language's flying circus support for Emacs."
259 :group 'languages
260 :version "24.3"
261 :link '(emacs-commentary-link "python"))
262
263 \f
264 ;;; Bindings
265
266 (defvar python-mode-map
267 (let ((map (make-sparse-keymap)))
268 ;; Movement
269 (define-key map [remap backward-sentence] 'python-nav-backward-block)
270 (define-key map [remap forward-sentence] 'python-nav-forward-block)
271 (define-key map [remap backward-up-list] 'python-nav-backward-up-list)
272 (define-key map "\C-c\C-j" 'imenu)
273 ;; Indent specific
274 (define-key map "\177" 'python-indent-dedent-line-backspace)
275 (define-key map (kbd "<backtab>") 'python-indent-dedent-line)
276 (define-key map "\C-c<" 'python-indent-shift-left)
277 (define-key map "\C-c>" 'python-indent-shift-right)
278 ;; Skeletons
279 (define-key map "\C-c\C-tc" 'python-skeleton-class)
280 (define-key map "\C-c\C-td" 'python-skeleton-def)
281 (define-key map "\C-c\C-tf" 'python-skeleton-for)
282 (define-key map "\C-c\C-ti" 'python-skeleton-if)
283 (define-key map "\C-c\C-tt" 'python-skeleton-try)
284 (define-key map "\C-c\C-tw" 'python-skeleton-while)
285 ;; Shell interaction
286 (define-key map "\C-c\C-p" 'run-python)
287 (define-key map "\C-c\C-s" 'python-shell-send-string)
288 (define-key map "\C-c\C-r" 'python-shell-send-region)
289 (define-key map "\C-\M-x" 'python-shell-send-defun)
290 (define-key map "\C-c\C-c" 'python-shell-send-buffer)
291 (define-key map "\C-c\C-l" 'python-shell-send-file)
292 (define-key map "\C-c\C-z" 'python-shell-switch-to-shell)
293 ;; Some util commands
294 (define-key map "\C-c\C-v" 'python-check)
295 (define-key map "\C-c\C-f" 'python-eldoc-at-point)
296 ;; Utilities
297 (substitute-key-definition 'complete-symbol 'completion-at-point
298 map global-map)
299 (easy-menu-define python-menu map "Python Mode menu"
300 `("Python"
301 :help "Python-specific Features"
302 ["Shift region left" python-indent-shift-left :active mark-active
303 :help "Shift region left by a single indentation step"]
304 ["Shift region right" python-indent-shift-right :active mark-active
305 :help "Shift region right by a single indentation step"]
306 "-"
307 ["Start of def/class" beginning-of-defun
308 :help "Go to start of outermost definition around point"]
309 ["End of def/class" end-of-defun
310 :help "Go to end of definition around point"]
311 ["Mark def/class" mark-defun
312 :help "Mark outermost definition around point"]
313 ["Jump to def/class" imenu
314 :help "Jump to a class or function definition"]
315 "--"
316 ("Skeletons")
317 "---"
318 ["Start interpreter" run-python
319 :help "Run inferior Python process in a separate buffer"]
320 ["Switch to shell" python-shell-switch-to-shell
321 :help "Switch to running inferior Python process"]
322 ["Eval string" python-shell-send-string
323 :help "Eval string in inferior Python session"]
324 ["Eval buffer" python-shell-send-buffer
325 :help "Eval buffer in inferior Python session"]
326 ["Eval region" python-shell-send-region
327 :help "Eval region in inferior Python session"]
328 ["Eval defun" python-shell-send-defun
329 :help "Eval defun in inferior Python session"]
330 ["Eval file" python-shell-send-file
331 :help "Eval file in inferior Python session"]
332 ["Debugger" pdb :help "Run pdb under GUD"]
333 "----"
334 ["Check file" python-check
335 :help "Check file for errors"]
336 ["Help on symbol" python-eldoc-at-point
337 :help "Get help on symbol at point"]
338 ["Complete symbol" completion-at-point
339 :help "Complete symbol before point"]))
340 map)
341 "Keymap for `python-mode'.")
342
343 \f
344 ;;; Python specialized rx
345
346 (eval-when-compile
347 (defconst python-rx-constituents
348 `((block-start . ,(rx symbol-start
349 (or "def" "class" "if" "elif" "else" "try"
350 "except" "finally" "for" "while" "with")
351 symbol-end))
352 (dedenter . ,(rx symbol-start
353 (or "elif" "else" "except" "finally")
354 symbol-end))
355 (block-ender . ,(rx symbol-start
356 (or
357 "break" "continue" "pass" "raise" "return")
358 symbol-end))
359 (decorator . ,(rx line-start (* space) ?@ (any letter ?_)
360 (* (any word ?_))))
361 (defun . ,(rx symbol-start (or "def" "class") symbol-end))
362 (if-name-main . ,(rx line-start "if" (+ space) "__name__"
363 (+ space) "==" (+ space)
364 (any ?' ?\") "__main__" (any ?' ?\")
365 (* space) ?:))
366 (symbol-name . ,(rx (any letter ?_) (* (any word ?_))))
367 (open-paren . ,(rx (or "{" "[" "(")))
368 (close-paren . ,(rx (or "}" "]" ")")))
369 (simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))
370 ;; FIXME: rx should support (not simple-operator).
371 (not-simple-operator . ,(rx
372 (not
373 (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))))
374 ;; FIXME: Use regexp-opt.
375 (operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
376 "=" "%" "**" "//" "<<" ">>" "<=" "!="
377 "==" ">=" "is" "not")))
378 ;; FIXME: Use regexp-opt.
379 (assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**="
380 ">>=" "<<=" "&=" "^=" "|=")))
381 (string-delimiter . ,(rx (and
382 ;; Match even number of backslashes.
383 (or (not (any ?\\ ?\' ?\")) point
384 ;; Quotes might be preceded by a escaped quote.
385 (and (or (not (any ?\\)) point) ?\\
386 (* ?\\ ?\\) (any ?\' ?\")))
387 (* ?\\ ?\\)
388 ;; Match single or triple quotes of any kind.
389 (group (or "\"" "\"\"\"" "'" "'''")))))
390 (coding-cookie . ,(rx line-start ?# (* space)
391 (or
392 ;; # coding=<encoding name>
393 (: "coding" (or ?: ?=) (* space) (group-n 1 (+ (or word ?-))))
394 ;; # -*- coding: <encoding name> -*-
395 (: "-*-" (* space) "coding:" (* space)
396 (group-n 1 (+ (or word ?-))) (* space) "-*-")
397 ;; # vim: set fileencoding=<encoding name> :
398 (: "vim:" (* space) "set" (+ space)
399 "fileencoding" (* space) ?= (* space)
400 (group-n 1 (+ (or word ?-))) (* space) ":")))))
401 "Additional Python specific sexps for `python-rx'")
402
403 (defmacro python-rx (&rest regexps)
404 "Python mode specialized rx macro.
405 This variant of `rx' supports common Python named REGEXPS."
406 (let ((rx-constituents (append python-rx-constituents rx-constituents)))
407 (cond ((null regexps)
408 (error "No regexp"))
409 ((cdr regexps)
410 (rx-to-string `(and ,@regexps) t))
411 (t
412 (rx-to-string (car regexps) t))))))
413
414 \f
415 ;;; Font-lock and syntax
416
417 (eval-when-compile
418 (defun python-syntax--context-compiler-macro (form type &optional syntax-ppss)
419 (pcase type
420 (`'comment
421 `(let ((ppss (or ,syntax-ppss (syntax-ppss))))
422 (and (nth 4 ppss) (nth 8 ppss))))
423 (`'string
424 `(let ((ppss (or ,syntax-ppss (syntax-ppss))))
425 (and (nth 3 ppss) (nth 8 ppss))))
426 (`'paren
427 `(nth 1 (or ,syntax-ppss (syntax-ppss))))
428 (_ form))))
429
430 (defun python-syntax-context (type &optional syntax-ppss)
431 "Return non-nil if point is on TYPE using SYNTAX-PPSS.
432 TYPE can be `comment', `string' or `paren'. It returns the start
433 character address of the specified TYPE."
434 (declare (compiler-macro python-syntax--context-compiler-macro))
435 (let ((ppss (or syntax-ppss (syntax-ppss))))
436 (pcase type
437 (`comment (and (nth 4 ppss) (nth 8 ppss)))
438 (`string (and (nth 3 ppss) (nth 8 ppss)))
439 (`paren (nth 1 ppss))
440 (_ nil))))
441
442 (defun python-syntax-context-type (&optional syntax-ppss)
443 "Return the context type using SYNTAX-PPSS.
444 The type returned can be `comment', `string' or `paren'."
445 (let ((ppss (or syntax-ppss (syntax-ppss))))
446 (cond
447 ((nth 8 ppss) (if (nth 4 ppss) 'comment 'string))
448 ((nth 1 ppss) 'paren))))
449
450 (defsubst python-syntax-comment-or-string-p (&optional ppss)
451 "Return non-nil if PPSS is inside 'comment or 'string."
452 (nth 8 (or ppss (syntax-ppss))))
453
454 (defsubst python-syntax-closing-paren-p ()
455 "Return non-nil if char after point is a closing paren."
456 (= (syntax-class (syntax-after (point)))
457 (syntax-class (string-to-syntax ")"))))
458
459 (define-obsolete-function-alias
460 'python-info-ppss-context #'python-syntax-context "24.3")
461
462 (define-obsolete-function-alias
463 'python-info-ppss-context-type #'python-syntax-context-type "24.3")
464
465 (define-obsolete-function-alias
466 'python-info-ppss-comment-or-string-p
467 #'python-syntax-comment-or-string-p "24.3")
468
469 (defvar python-font-lock-keywords
470 ;; Keywords
471 `(,(rx symbol-start
472 (or
473 "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with"
474 "assert" "else" "if" "pass" "yield" "break" "except" "import" "class"
475 "in" "raise" "continue" "finally" "is" "return" "def" "for" "lambda"
476 "try"
477 ;; Python 2:
478 "print" "exec"
479 ;; Python 3:
480 ;; False, None, and True are listed as keywords on the Python 3
481 ;; documentation, but since they also qualify as constants they are
482 ;; fontified like that in order to keep font-lock consistent between
483 ;; Python versions.
484 "nonlocal"
485 ;; Extra:
486 "self")
487 symbol-end)
488 ;; functions
489 (,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_))))
490 (1 font-lock-function-name-face))
491 ;; classes
492 (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_))))
493 (1 font-lock-type-face))
494 ;; Constants
495 (,(rx symbol-start
496 (or
497 "Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
498 ;; copyright, license, credits, quit and exit are added by the site
499 ;; module and they are not intended to be used in programs
500 "copyright" "credits" "exit" "license" "quit")
501 symbol-end) . font-lock-constant-face)
502 ;; Decorators.
503 (,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_))
504 (0+ "." (1+ (or word ?_)))))
505 (1 font-lock-type-face))
506 ;; Builtin Exceptions
507 (,(rx symbol-start
508 (or
509 "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
510 "DeprecationWarning" "EOFError" "EnvironmentError" "Exception"
511 "FloatingPointError" "FutureWarning" "GeneratorExit" "IOError"
512 "ImportError" "ImportWarning" "IndexError" "KeyError"
513 "KeyboardInterrupt" "LookupError" "MemoryError" "NameError"
514 "NotImplementedError" "OSError" "OverflowError"
515 "PendingDeprecationWarning" "ReferenceError" "RuntimeError"
516 "RuntimeWarning" "StopIteration" "SyntaxError" "SyntaxWarning"
517 "SystemError" "SystemExit" "TypeError" "UnboundLocalError"
518 "UnicodeDecodeError" "UnicodeEncodeError" "UnicodeError"
519 "UnicodeTranslateError" "UnicodeWarning" "UserWarning" "VMSError"
520 "ValueError" "Warning" "WindowsError" "ZeroDivisionError"
521 ;; Python 2:
522 "StandardError"
523 ;; Python 3:
524 "BufferError" "BytesWarning" "IndentationError" "ResourceWarning"
525 "TabError")
526 symbol-end) . font-lock-type-face)
527 ;; Builtins
528 (,(rx symbol-start
529 (or
530 "abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
531 "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
532 "eval" "filter" "float" "format" "frozenset" "getattr" "globals"
533 "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
534 "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
535 "min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
536 "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
537 "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
538 "__import__"
539 ;; Python 2:
540 "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
541 "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
542 "intern"
543 ;; Python 3:
544 "ascii" "bytearray" "bytes" "exec"
545 ;; Extra:
546 "__all__" "__doc__" "__name__" "__package__")
547 symbol-end) . font-lock-builtin-face)
548 ;; assignments
549 ;; support for a = b = c = 5
550 (,(lambda (limit)
551 (let ((re (python-rx (group (+ (any word ?. ?_)))
552 (? ?\[ (+ (not (any ?\]))) ?\]) (* space)
553 assignment-operator))
554 (res nil))
555 (while (and (setq res (re-search-forward re limit t))
556 (or (python-syntax-context 'paren)
557 (equal (char-after (point-marker)) ?=))))
558 res))
559 (1 font-lock-variable-name-face nil nil))
560 ;; support for a, b, c = (1, 2, 3)
561 (,(lambda (limit)
562 (let ((re (python-rx (group (+ (any word ?. ?_))) (* space)
563 (* ?, (* space) (+ (any word ?. ?_)) (* space))
564 ?, (* space) (+ (any word ?. ?_)) (* space)
565 assignment-operator))
566 (res nil))
567 (while (and (setq res (re-search-forward re limit t))
568 (goto-char (match-end 1))
569 (python-syntax-context 'paren)))
570 res))
571 (1 font-lock-variable-name-face nil nil))))
572
573 (defconst python-syntax-propertize-function
574 (syntax-propertize-rules
575 ((python-rx string-delimiter)
576 (0 (ignore (python-syntax-stringify))))))
577
578 (defsubst python-syntax-count-quotes (quote-char &optional point limit)
579 "Count number of quotes around point (max is 3).
580 QUOTE-CHAR is the quote char to count. Optional argument POINT is
581 the point where scan starts (defaults to current point), and LIMIT
582 is used to limit the scan."
583 (let ((i 0))
584 (while (and (< i 3)
585 (or (not limit) (< (+ point i) limit))
586 (eq (char-after (+ point i)) quote-char))
587 (setq i (1+ i)))
588 i))
589
590 (defun python-syntax-stringify ()
591 "Put `syntax-table' property correctly on single/triple quotes."
592 (let* ((num-quotes (length (match-string-no-properties 1)))
593 (ppss (prog2
594 (backward-char num-quotes)
595 (syntax-ppss)
596 (forward-char num-quotes)))
597 (string-start (and (not (nth 4 ppss)) (nth 8 ppss)))
598 (quote-starting-pos (- (point) num-quotes))
599 (quote-ending-pos (point))
600 (num-closing-quotes
601 (and string-start
602 (python-syntax-count-quotes
603 (char-before) string-start quote-starting-pos))))
604 (cond ((and string-start (= num-closing-quotes 0))
605 ;; This set of quotes doesn't match the string starting
606 ;; kind. Do nothing.
607 nil)
608 ((not string-start)
609 ;; This set of quotes delimit the start of a string.
610 (put-text-property quote-starting-pos (1+ quote-starting-pos)
611 'syntax-table (string-to-syntax "|")))
612 ((= num-quotes num-closing-quotes)
613 ;; This set of quotes delimit the end of a string.
614 (put-text-property (1- quote-ending-pos) quote-ending-pos
615 'syntax-table (string-to-syntax "|")))
616 ((> num-quotes num-closing-quotes)
617 ;; This may only happen whenever a triple quote is closing
618 ;; a single quoted string. Add string delimiter syntax to
619 ;; all three quotes.
620 (put-text-property quote-starting-pos quote-ending-pos
621 'syntax-table (string-to-syntax "|"))))))
622
623 (defvar python-mode-syntax-table
624 (let ((table (make-syntax-table)))
625 ;; Give punctuation syntax to ASCII that normally has symbol
626 ;; syntax or has word syntax and isn't a letter.
627 (let ((symbol (string-to-syntax "_"))
628 (sst (standard-syntax-table)))
629 (dotimes (i 128)
630 (unless (= i ?_)
631 (if (equal symbol (aref sst i))
632 (modify-syntax-entry i "." table)))))
633 (modify-syntax-entry ?$ "." table)
634 (modify-syntax-entry ?% "." table)
635 ;; exceptions
636 (modify-syntax-entry ?# "<" table)
637 (modify-syntax-entry ?\n ">" table)
638 (modify-syntax-entry ?' "\"" table)
639 (modify-syntax-entry ?` "$" table)
640 table)
641 "Syntax table for Python files.")
642
643 (defvar python-dotty-syntax-table
644 (let ((table (make-syntax-table python-mode-syntax-table)))
645 (modify-syntax-entry ?. "w" table)
646 (modify-syntax-entry ?_ "w" table)
647 table)
648 "Dotty syntax table for Python files.
649 It makes underscores and dots word constituent chars.")
650
651 \f
652 ;;; Indentation
653
654 (defcustom python-indent-offset 4
655 "Default indentation offset for Python."
656 :group 'python
657 :type 'integer
658 :safe 'integerp)
659
660 (defcustom python-indent-guess-indent-offset t
661 "Non-nil tells Python mode to guess `python-indent-offset' value."
662 :type 'boolean
663 :group 'python
664 :safe 'booleanp)
665
666 (defcustom python-indent-trigger-commands
667 '(indent-for-tab-command yas-expand yas/expand)
668 "Commands that might trigger a `python-indent-line' call."
669 :type '(repeat symbol)
670 :group 'python)
671
672 (define-obsolete-variable-alias
673 'python-indent 'python-indent-offset "24.3")
674
675 (define-obsolete-variable-alias
676 'python-guess-indent 'python-indent-guess-indent-offset "24.3")
677
678 (defvar python-indent-current-level 0
679 "Deprecated var available for compatibility.")
680
681 (defvar python-indent-levels '(0)
682 "Deprecated var available for compatibility.")
683
684 (make-obsolete-variable
685 'python-indent-current-level
686 "The indentation API changed to avoid global state.
687 The function `python-indent-calculate-levels' does not use it
688 anymore. If you were defadvising it and or depended on this
689 variable for indentation customizations, refactor your code to
690 work on `python-indent-calculate-indentation' instead."
691 "24.5")
692
693 (make-obsolete-variable
694 'python-indent-levels
695 "The indentation API changed to avoid global state.
696 The function `python-indent-calculate-levels' does not use it
697 anymore. If you were defadvising it and or depended on this
698 variable for indentation customizations, refactor your code to
699 work on `python-indent-calculate-indentation' instead."
700 "24.5")
701
702 (defun python-indent-guess-indent-offset ()
703 "Guess and set `python-indent-offset' for the current buffer."
704 (interactive)
705 (save-excursion
706 (save-restriction
707 (widen)
708 (goto-char (point-min))
709 (let ((block-end))
710 (while (and (not block-end)
711 (re-search-forward
712 (python-rx line-start block-start) nil t))
713 (when (and
714 (not (python-syntax-context-type))
715 (progn
716 (goto-char (line-end-position))
717 (python-util-forward-comment -1)
718 (if (equal (char-before) ?:)
719 t
720 (forward-line 1)
721 (when (python-info-block-continuation-line-p)
722 (while (and (python-info-continuation-line-p)
723 (not (eobp)))
724 (forward-line 1))
725 (python-util-forward-comment -1)
726 (when (equal (char-before) ?:)
727 t)))))
728 (setq block-end (point-marker))))
729 (let ((indentation
730 (when block-end
731 (goto-char block-end)
732 (python-util-forward-comment)
733 (current-indentation))))
734 (if (and indentation (not (zerop indentation)))
735 (set (make-local-variable 'python-indent-offset) indentation)
736 (message "Can't guess python-indent-offset, using defaults: %s"
737 python-indent-offset)))))))
738
739 (defun python-indent-context ()
740 "Get information about the current indentation context.
741 Context is returned in a cons with the form (STATUS . START).
742
743 STATUS can be one of the following:
744
745 keyword
746 -------
747
748 :after-comment
749 - Point is after a comment line.
750 - START is the position of the \"#\" character.
751 :inside-string
752 - Point is inside string.
753 - START is the position of the first quote that starts it.
754 :no-indent
755 - No possible indentation case matches.
756 - START is always zero.
757
758 :inside-paren
759 - Fallback case when point is inside paren.
760 - START is the first non space char position *after* the open paren.
761 :inside-paren-at-closing-nested-paren
762 - Point is on a line that contains a nested paren closer.
763 - START is the position of the open paren it closes.
764 :inside-paren-at-closing-paren
765 - Point is on a line that contains a paren closer.
766 - START is the position of the open paren.
767 :inside-paren-newline-start
768 - Point is inside a paren with items starting in their own line.
769 - START is the position of the open paren.
770 :inside-paren-newline-start-from-block
771 - Point is inside a paren with items starting in their own line
772 from a block start.
773 - START is the position of the open paren.
774
775 :after-backslash
776 - Fallback case when point is after backslash.
777 - START is the char after the position of the backslash.
778 :after-backslash-assignment-continuation
779 - Point is after a backslashed assignment.
780 - START is the char after the position of the backslash.
781 :after-backslash-block-continuation
782 - Point is after a backslashed block continuation.
783 - START is the char after the position of the backslash.
784 :after-backslash-dotted-continuation
785 - Point is after a backslashed dotted continuation. Previous
786 line must contain a dot to align with.
787 - START is the char after the position of the backslash.
788 :after-backslash-first-line
789 - First line following a backslashed continuation.
790 - START is the char after the position of the backslash.
791
792 :after-block-end
793 - Point is after a line containing a block ender.
794 - START is the position where the ender starts.
795 :after-block-start
796 - Point is after a line starting a block.
797 - START is the position where the block starts.
798 :after-line
799 - Point is after a simple line.
800 - START is the position where the previous line starts.
801 :at-dedenter-block-start
802 - Point is on a line starting a dedenter block.
803 - START is the position where the dedenter block starts."
804 (save-restriction
805 (widen)
806 (let ((ppss (save-excursion
807 (beginning-of-line)
808 (syntax-ppss))))
809 (cond
810 ;; Beginning of buffer.
811 ((= (line-number-at-pos) 1)
812 (cons :no-indent 0))
813 ;; Inside a string.
814 ((let ((start (python-syntax-context 'string ppss)))
815 (when start
816 (cons :inside-string start))))
817 ;; Inside a paren.
818 ((let* ((start (python-syntax-context 'paren ppss))
819 (starts-in-newline
820 (when start
821 (save-excursion
822 (goto-char start)
823 (forward-char)
824 (not
825 (= (line-number-at-pos)
826 (progn
827 (python-util-forward-comment)
828 (line-number-at-pos))))))))
829 (when start
830 (cond
831 ;; Current line only holds the closing paren.
832 ((save-excursion
833 (skip-syntax-forward " ")
834 (when (and (python-syntax-closing-paren-p)
835 (progn
836 (forward-char 1)
837 (not (python-syntax-context 'paren))))
838 (cons :inside-paren-at-closing-paren start))))
839 ;; Current line only holds a closing paren for nested.
840 ((save-excursion
841 (back-to-indentation)
842 (python-syntax-closing-paren-p))
843 (cons :inside-paren-at-closing-nested-paren start))
844 ;; This line starts from a opening block in its own line.
845 ((save-excursion
846 (goto-char start)
847 (when (and
848 starts-in-newline
849 (save-excursion
850 (back-to-indentation)
851 (looking-at (python-rx block-start))))
852 (cons
853 :inside-paren-newline-start-from-block start))))
854 (starts-in-newline
855 (cons :inside-paren-newline-start start))
856 ;; General case.
857 (t (cons :inside-paren
858 (save-excursion
859 (goto-char (1+ start))
860 (skip-syntax-forward "(" 1)
861 (skip-syntax-forward " ")
862 (point))))))))
863 ;; After backslash.
864 ((let ((start (when (not (python-syntax-comment-or-string-p ppss))
865 (python-info-line-ends-backslash-p
866 (1- (line-number-at-pos))))))
867 (when start
868 (cond
869 ;; Continuation of dotted expression.
870 ((save-excursion
871 (back-to-indentation)
872 (when (eq (char-after) ?\.)
873 ;; Move point back until it's not inside a paren.
874 (while (prog2
875 (forward-line -1)
876 (and (not (bobp))
877 (python-syntax-context 'paren))))
878 (goto-char (line-end-position))
879 (while (and (search-backward
880 "." (line-beginning-position) t)
881 (python-syntax-context-type)))
882 ;; Ensure previous statement has dot to align with.
883 (when (and (eq (char-after) ?\.)
884 (not (python-syntax-context-type)))
885 (cons :after-backslash-dotted-continuation (point))))))
886 ;; Continuation of block definition.
887 ((let ((block-continuation-start
888 (python-info-block-continuation-line-p)))
889 (when block-continuation-start
890 (save-excursion
891 (goto-char block-continuation-start)
892 (re-search-forward
893 (python-rx block-start (* space))
894 (line-end-position) t)
895 (cons :after-backslash-block-continuation (point))))))
896 ;; Continuation of assignment.
897 ((let ((assignment-continuation-start
898 (python-info-assignment-continuation-line-p)))
899 (when assignment-continuation-start
900 (save-excursion
901 (goto-char assignment-continuation-start)
902 (cons :after-backslash-assignment-continuation (point))))))
903 ;; First line after backslash continuation start.
904 ((save-excursion
905 (goto-char start)
906 (when (or (= (line-number-at-pos) 1)
907 (not (python-info-beginning-of-backslash
908 (1- (line-number-at-pos)))))
909 (cons :after-backslash-first-line start))))
910 ;; General case.
911 (t (cons :after-backslash start))))))
912 ;; After beginning of block.
913 ((let ((start (save-excursion
914 (back-to-indentation)
915 (python-util-forward-comment -1)
916 (when (equal (char-before) ?:)
917 (python-nav-beginning-of-block)))))
918 (when start
919 (cons :after-block-start start))))
920 ;; At dedenter statement.
921 ((let ((start (python-info-dedenter-statement-p)))
922 (when start
923 (cons :at-dedenter-block-start start))))
924 ;; After normal line, comment or ender (default case).
925 ((save-excursion
926 (back-to-indentation)
927 (skip-chars-backward " \t\n")
928 (python-nav-beginning-of-statement)
929 (cons
930 (cond ((python-info-current-line-comment-p)
931 :after-comment)
932 ((save-excursion
933 (goto-char (line-end-position))
934 (python-util-forward-comment -1)
935 (python-nav-beginning-of-statement)
936 (looking-at (python-rx block-ender)))
937 :after-block-end)
938 (t :after-line))
939 (point))))))))
940
941 (defun python-indent--calculate-indentation ()
942 "Internal implementation of `python-indent-calculate-indentation'.
943 May return an integer for the maximum possible indentation at
944 current context or a list of integers. The latter case is only
945 happening for :at-dedenter-block-start context since the
946 possibilities can be narrowed to specific indentation points."
947 (save-restriction
948 (widen)
949 (save-excursion
950 (pcase (python-indent-context)
951 (`(:no-indent . ,_) 0)
952 (`(,(or :after-line
953 :after-comment
954 :inside-string
955 :after-backslash
956 :inside-paren-at-closing-paren
957 :inside-paren-at-closing-nested-paren) . ,start)
958 ;; Copy previous indentation.
959 (goto-char start)
960 (current-indentation))
961 (`(,(or :after-block-start
962 :after-backslash-first-line
963 :inside-paren-newline-start) . ,start)
964 ;; Add one indentation level.
965 (goto-char start)
966 (+ (current-indentation) python-indent-offset))
967 (`(,(or :inside-paren
968 :after-backslash-block-continuation
969 :after-backslash-assignment-continuation
970 :after-backslash-dotted-continuation) . ,start)
971 ;; Use the column given by the context.
972 (goto-char start)
973 (current-column))
974 (`(:after-block-end . ,start)
975 ;; Subtract one indentation level.
976 (goto-char start)
977 (- (current-indentation) python-indent-offset))
978 (`(:at-dedenter-block-start . ,_)
979 ;; List all possible indentation levels from opening blocks.
980 (let ((opening-block-start-points
981 (python-info-dedenter-opening-block-positions)))
982 (if (not opening-block-start-points)
983 0 ; if not found default to first column
984 (mapcar (lambda (pos)
985 (save-excursion
986 (goto-char pos)
987 (current-indentation)))
988 opening-block-start-points))))
989 (`(,(or :inside-paren-newline-start-from-block) . ,start)
990 ;; Add two indentation levels to make the suite stand out.
991 (goto-char start)
992 (+ (current-indentation) (* python-indent-offset 2)))))))
993
994 (defun python-indent--calculate-levels (indentation)
995 "Calculate levels list given INDENTATION.
996 Argument INDENTATION can either be an integer or a list of
997 integers. Levels are returned in ascending order, and in the
998 case INDENTATION is a list, this order is enforced."
999 (if (listp indentation)
1000 (sort (copy-sequence indentation) #'<)
1001 (let* ((remainder (% indentation python-indent-offset))
1002 (steps (/ (- indentation remainder) python-indent-offset))
1003 (levels (mapcar (lambda (step)
1004 (* python-indent-offset step))
1005 (number-sequence steps 0 -1))))
1006 (reverse
1007 (if (not (zerop remainder))
1008 (cons indentation levels)
1009 levels)))))
1010
1011 (defun python-indent--previous-level (levels indentation)
1012 "Return previous level from LEVELS relative to INDENTATION."
1013 (let* ((levels (sort (copy-sequence levels) #'>))
1014 (default (car levels)))
1015 (catch 'return
1016 (dolist (level levels)
1017 (when (funcall #'< level indentation)
1018 (throw 'return level)))
1019 default)))
1020
1021 (defun python-indent-calculate-indentation (&optional previous)
1022 "Calculate indentation.
1023 Get indentation of PREVIOUS level when argument is non-nil.
1024 Return the max level of the cycle when indentation reaches the
1025 minimum."
1026 (let* ((indentation (python-indent--calculate-indentation))
1027 (levels (python-indent--calculate-levels indentation)))
1028 (if previous
1029 (python-indent--previous-level levels (current-indentation))
1030 (apply #'max levels))))
1031
1032 (defun python-indent-line (&optional previous)
1033 "Internal implementation of `python-indent-line-function'.
1034 Use the PREVIOUS level when argument is non-nil, otherwise indent
1035 to the maximum available level. When indentation is the minimum
1036 possible and PREVIOUS is non-nil, cycle back to the maximum
1037 level."
1038 (let ((follow-indentation-p
1039 ;; Check if point is within indentation.
1040 (and (<= (line-beginning-position) (point))
1041 (>= (+ (line-beginning-position)
1042 (current-indentation))
1043 (point)))))
1044 (save-excursion
1045 (indent-line-to
1046 (python-indent-calculate-indentation previous))
1047 (python-info-dedenter-opening-block-message))
1048 (when follow-indentation-p
1049 (back-to-indentation))))
1050
1051 (defun python-indent-calculate-levels ()
1052 "Return possible indentation levels."
1053 (python-indent--calculate-levels
1054 (python-indent--calculate-indentation)))
1055
1056 (defun python-indent-line-function ()
1057 "`indent-line-function' for Python mode.
1058 When the variable `last-command' is equal to one of the symbols
1059 inside `python-indent-trigger-commands' it cycles possible
1060 indentation levels from right to left."
1061 (python-indent-line
1062 (and (memq this-command python-indent-trigger-commands)
1063 (eq last-command this-command))))
1064
1065 (defun python-indent-dedent-line ()
1066 "De-indent current line."
1067 (interactive "*")
1068 (when (and (not (bolp))
1069 (not (python-syntax-comment-or-string-p))
1070 (= (current-indentation) (current-column)))
1071 (python-indent-line t)
1072 t))
1073
1074 (defun python-indent-dedent-line-backspace (arg)
1075 "De-indent current line.
1076 Argument ARG is passed to `backward-delete-char-untabify' when
1077 point is not in between the indentation."
1078 (interactive "*p")
1079 (unless (python-indent-dedent-line)
1080 (backward-delete-char-untabify arg)))
1081
1082 (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)
1083
1084 (defun python-indent-region (start end)
1085 "Indent a Python region automagically.
1086
1087 Called from a program, START and END specify the region to indent."
1088 (let ((deactivate-mark nil))
1089 (save-excursion
1090 (goto-char end)
1091 (setq end (point-marker))
1092 (goto-char start)
1093 (or (bolp) (forward-line 1))
1094 (while (< (point) end)
1095 (or (and (bolp) (eolp))
1096 (when (and
1097 ;; Skip if previous line is empty or a comment.
1098 (save-excursion
1099 (let ((line-is-comment-p
1100 (python-info-current-line-comment-p)))
1101 (forward-line -1)
1102 (not
1103 (or (and (python-info-current-line-comment-p)
1104 ;; Unless this line is a comment too.
1105 (not line-is-comment-p))
1106 (python-info-current-line-empty-p)))))
1107 ;; Don't mess with strings, unless it's the
1108 ;; enclosing set of quotes.
1109 (or (not (python-syntax-context 'string))
1110 (eq
1111 (syntax-after
1112 (+ (1- (point))
1113 (current-indentation)
1114 (python-syntax-count-quotes (char-after) (point))))
1115 (string-to-syntax "|")))
1116 ;; Skip if current line is a block start, a
1117 ;; dedenter or block ender.
1118 (save-excursion
1119 (back-to-indentation)
1120 (not (looking-at
1121 (python-rx
1122 (or block-start dedenter block-ender))))))
1123 (python-indent-line)))
1124 (forward-line 1))
1125 (move-marker end nil))))
1126
1127 (defun python-indent-shift-left (start end &optional count)
1128 "Shift lines contained in region START END by COUNT columns to the left.
1129 COUNT defaults to `python-indent-offset'. If region isn't
1130 active, the current line is shifted. The shifted region includes
1131 the lines in which START and END lie. An error is signaled if
1132 any lines in the region are indented less than COUNT columns."
1133 (interactive
1134 (if mark-active
1135 (list (region-beginning) (region-end) current-prefix-arg)
1136 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
1137 (if count
1138 (setq count (prefix-numeric-value count))
1139 (setq count python-indent-offset))
1140 (when (> count 0)
1141 (let ((deactivate-mark nil))
1142 (save-excursion
1143 (goto-char start)
1144 (while (< (point) end)
1145 (if (and (< (current-indentation) count)
1146 (not (looking-at "[ \t]*$")))
1147 (error "Can't shift all lines enough"))
1148 (forward-line))
1149 (indent-rigidly start end (- count))))))
1150
1151 (add-to-list 'debug-ignored-errors "^Can't shift all lines enough")
1152
1153 (defun python-indent-shift-right (start end &optional count)
1154 "Shift lines contained in region START END by COUNT columns to the right.
1155 COUNT defaults to `python-indent-offset'. If region isn't
1156 active, the current line is shifted. The shifted region includes
1157 the lines in which START and END lie."
1158 (interactive
1159 (if mark-active
1160 (list (region-beginning) (region-end) current-prefix-arg)
1161 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
1162 (let ((deactivate-mark nil))
1163 (setq count (if count (prefix-numeric-value count)
1164 python-indent-offset))
1165 (indent-rigidly start end count)))
1166
1167 (defun python-indent-post-self-insert-function ()
1168 "Adjust indentation after insertion of some characters.
1169 This function is intended to be added to `post-self-insert-hook.'
1170 If a line renders a paren alone, after adding a char before it,
1171 the line will be re-indented automatically if needed."
1172 (when (and electric-indent-mode
1173 (eq (char-before) last-command-event))
1174 (cond
1175 ;; Electric indent inside parens
1176 ((and
1177 (not (bolp))
1178 (let ((paren-start (python-syntax-context 'paren)))
1179 ;; Check that point is inside parens.
1180 (when paren-start
1181 (not
1182 ;; Filter the case where input is happening in the same
1183 ;; line where the open paren is.
1184 (= (line-number-at-pos)
1185 (line-number-at-pos paren-start)))))
1186 ;; When content has been added before the closing paren or a
1187 ;; comma has been inserted, it's ok to do the trick.
1188 (or
1189 (memq (char-after) '(?\) ?\] ?\}))
1190 (eq (char-before) ?,)))
1191 (save-excursion
1192 (goto-char (line-beginning-position))
1193 (let ((indentation (python-indent-calculate-indentation)))
1194 (when (< (current-indentation) indentation)
1195 (indent-line-to indentation)))))
1196 ;; Electric colon
1197 ((and (eq ?: last-command-event)
1198 (memq ?: electric-indent-chars)
1199 (not current-prefix-arg)
1200 ;; Trigger electric colon only at end of line
1201 (eolp)
1202 ;; Avoid re-indenting on extra colon
1203 (not (equal ?: (char-before (1- (point)))))
1204 (not (python-syntax-comment-or-string-p)))
1205 ;; Just re-indent dedenters
1206 (let ((dedenter-pos (python-info-dedenter-statement-p))
1207 (current-pos (point)))
1208 (when dedenter-pos
1209 (save-excursion
1210 (goto-char dedenter-pos)
1211 (python-indent-line)
1212 (unless (= (line-number-at-pos dedenter-pos)
1213 (line-number-at-pos current-pos))
1214 ;; Reindent region if this is a multiline statement
1215 (python-indent-region dedenter-pos current-pos)))))))))
1216
1217 \f
1218 ;;; Navigation
1219
1220 (defvar python-nav-beginning-of-defun-regexp
1221 (python-rx line-start (* space) defun (+ space) (group symbol-name))
1222 "Regexp matching class or function definition.
1223 The name of the defun should be grouped so it can be retrieved
1224 via `match-string'.")
1225
1226 (defun python-nav--beginning-of-defun (&optional arg)
1227 "Internal implementation of `python-nav-beginning-of-defun'.
1228 With positive ARG search backwards, else search forwards."
1229 (when (or (null arg) (= arg 0)) (setq arg 1))
1230 (let* ((re-search-fn (if (> arg 0)
1231 #'re-search-backward
1232 #'re-search-forward))
1233 (line-beg-pos (line-beginning-position))
1234 (line-content-start (+ line-beg-pos (current-indentation)))
1235 (pos (point-marker))
1236 (beg-indentation
1237 (and (> arg 0)
1238 (save-excursion
1239 (while (and
1240 (not (python-info-looking-at-beginning-of-defun))
1241 (python-nav-backward-block)))
1242 (or (and (python-info-looking-at-beginning-of-defun)
1243 (+ (current-indentation) python-indent-offset))
1244 0))))
1245 (found
1246 (progn
1247 (when (and (< arg 0)
1248 (python-info-looking-at-beginning-of-defun))
1249 (end-of-line 1))
1250 (while (and (funcall re-search-fn
1251 python-nav-beginning-of-defun-regexp nil t)
1252 (or (python-syntax-context-type)
1253 ;; Handle nested defuns when moving
1254 ;; backwards by checking indentation.
1255 (and (> arg 0)
1256 (not (= (current-indentation) 0))
1257 (>= (current-indentation) beg-indentation)))))
1258 (and (python-info-looking-at-beginning-of-defun)
1259 (or (not (= (line-number-at-pos pos)
1260 (line-number-at-pos)))
1261 (and (>= (point) line-beg-pos)
1262 (<= (point) line-content-start)
1263 (> pos line-content-start)))))))
1264 (if found
1265 (or (beginning-of-line 1) t)
1266 (and (goto-char pos) nil))))
1267
1268 (defun python-nav-beginning-of-defun (&optional arg)
1269 "Move point to `beginning-of-defun'.
1270 With positive ARG search backwards else search forward.
1271 ARG nil or 0 defaults to 1. When searching backwards,
1272 nested defuns are handled with care depending on current
1273 point position. Return non-nil if point is moved to
1274 `beginning-of-defun'."
1275 (when (or (null arg) (= arg 0)) (setq arg 1))
1276 (let ((found))
1277 (while (and (not (= arg 0))
1278 (let ((keep-searching-p
1279 (python-nav--beginning-of-defun arg)))
1280 (when (and keep-searching-p (null found))
1281 (setq found t))
1282 keep-searching-p))
1283 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
1284 found))
1285
1286 (defun python-nav-end-of-defun ()
1287 "Move point to the end of def or class.
1288 Returns nil if point is not in a def or class."
1289 (interactive)
1290 (let ((beg-defun-indent)
1291 (beg-pos (point)))
1292 (when (or (python-info-looking-at-beginning-of-defun)
1293 (python-nav-beginning-of-defun 1)
1294 (python-nav-beginning-of-defun -1))
1295 (setq beg-defun-indent (current-indentation))
1296 (while (progn
1297 (python-nav-end-of-statement)
1298 (python-util-forward-comment 1)
1299 (and (> (current-indentation) beg-defun-indent)
1300 (not (eobp)))))
1301 (python-util-forward-comment -1)
1302 (forward-line 1)
1303 ;; Ensure point moves forward.
1304 (and (> beg-pos (point)) (goto-char beg-pos)))))
1305
1306 (defun python-nav--syntactically (fn poscompfn &optional contextfn)
1307 "Move point using FN avoiding places with specific context.
1308 FN must take no arguments. POSCOMPFN is a two arguments function
1309 used to compare current and previous point after it is moved
1310 using FN, this is normally a less-than or greater-than
1311 comparison. Optional argument CONTEXTFN defaults to
1312 `python-syntax-context-type' and is used for checking current
1313 point context, it must return a non-nil value if this point must
1314 be skipped."
1315 (let ((contextfn (or contextfn 'python-syntax-context-type))
1316 (start-pos (point-marker))
1317 (prev-pos))
1318 (catch 'found
1319 (while t
1320 (let* ((newpos
1321 (and (funcall fn) (point-marker)))
1322 (context (funcall contextfn)))
1323 (cond ((and (not context) newpos
1324 (or (and (not prev-pos) newpos)
1325 (and prev-pos newpos
1326 (funcall poscompfn newpos prev-pos))))
1327 (throw 'found (point-marker)))
1328 ((and newpos context)
1329 (setq prev-pos (point)))
1330 (t (when (not newpos) (goto-char start-pos))
1331 (throw 'found nil))))))))
1332
1333 (defun python-nav--forward-defun (arg)
1334 "Internal implementation of python-nav-{backward,forward}-defun.
1335 Uses ARG to define which function to call, and how many times
1336 repeat it."
1337 (let ((found))
1338 (while (and (> arg 0)
1339 (setq found
1340 (python-nav--syntactically
1341 (lambda ()
1342 (re-search-forward
1343 python-nav-beginning-of-defun-regexp nil t))
1344 '>)))
1345 (setq arg (1- arg)))
1346 (while (and (< arg 0)
1347 (setq found
1348 (python-nav--syntactically
1349 (lambda ()
1350 (re-search-backward
1351 python-nav-beginning-of-defun-regexp nil t))
1352 '<)))
1353 (setq arg (1+ arg)))
1354 found))
1355
1356 (defun python-nav-backward-defun (&optional arg)
1357 "Navigate to closer defun backward ARG times.
1358 Unlikely `python-nav-beginning-of-defun' this doesn't care about
1359 nested definitions."
1360 (interactive "^p")
1361 (python-nav--forward-defun (- (or arg 1))))
1362
1363 (defun python-nav-forward-defun (&optional arg)
1364 "Navigate to closer defun forward ARG times.
1365 Unlikely `python-nav-beginning-of-defun' this doesn't care about
1366 nested definitions."
1367 (interactive "^p")
1368 (python-nav--forward-defun (or arg 1)))
1369
1370 (defun python-nav-beginning-of-statement ()
1371 "Move to start of current statement."
1372 (interactive "^")
1373 (back-to-indentation)
1374 (let* ((ppss (syntax-ppss))
1375 (context-point
1376 (or
1377 (python-syntax-context 'paren ppss)
1378 (python-syntax-context 'string ppss))))
1379 (cond ((bobp))
1380 (context-point
1381 (goto-char context-point)
1382 (python-nav-beginning-of-statement))
1383 ((save-excursion
1384 (forward-line -1)
1385 (python-info-line-ends-backslash-p))
1386 (forward-line -1)
1387 (python-nav-beginning-of-statement))))
1388 (point-marker))
1389
1390 (defun python-nav-end-of-statement (&optional noend)
1391 "Move to end of current statement.
1392 Optional argument NOEND is internal and makes the logic to not
1393 jump to the end of line when moving forward searching for the end
1394 of the statement."
1395 (interactive "^")
1396 (let (string-start bs-pos)
1397 (while (and (or noend (goto-char (line-end-position)))
1398 (not (eobp))
1399 (cond ((setq string-start (python-syntax-context 'string))
1400 (goto-char string-start)
1401 (if (python-syntax-context 'paren)
1402 ;; Ended up inside a paren, roll again.
1403 (python-nav-end-of-statement t)
1404 ;; This is not inside a paren, move to the
1405 ;; end of this string.
1406 (goto-char (+ (point)
1407 (python-syntax-count-quotes
1408 (char-after (point)) (point))))
1409 (or (re-search-forward (rx (syntax string-delimiter)) nil t)
1410 (goto-char (point-max)))))
1411 ((python-syntax-context 'paren)
1412 ;; The statement won't end before we've escaped
1413 ;; at least one level of parenthesis.
1414 (condition-case err
1415 (goto-char (scan-lists (point) 1 -1))
1416 (scan-error (goto-char (nth 3 err)))))
1417 ((setq bs-pos (python-info-line-ends-backslash-p))
1418 (goto-char bs-pos)
1419 (forward-line 1))))))
1420 (point-marker))
1421
1422 (defun python-nav-backward-statement (&optional arg)
1423 "Move backward to previous statement.
1424 With ARG, repeat. See `python-nav-forward-statement'."
1425 (interactive "^p")
1426 (or arg (setq arg 1))
1427 (python-nav-forward-statement (- arg)))
1428
1429 (defun python-nav-forward-statement (&optional arg)
1430 "Move forward to next statement.
1431 With ARG, repeat. With negative argument, move ARG times
1432 backward to previous statement."
1433 (interactive "^p")
1434 (or arg (setq arg 1))
1435 (while (> arg 0)
1436 (python-nav-end-of-statement)
1437 (python-util-forward-comment)
1438 (python-nav-beginning-of-statement)
1439 (setq arg (1- arg)))
1440 (while (< arg 0)
1441 (python-nav-beginning-of-statement)
1442 (python-util-forward-comment -1)
1443 (python-nav-beginning-of-statement)
1444 (setq arg (1+ arg))))
1445
1446 (defun python-nav-beginning-of-block ()
1447 "Move to start of current block."
1448 (interactive "^")
1449 (let ((starting-pos (point)))
1450 (if (progn
1451 (python-nav-beginning-of-statement)
1452 (looking-at (python-rx block-start)))
1453 (point-marker)
1454 ;; Go to first line beginning a statement
1455 (while (and (not (bobp))
1456 (or (and (python-nav-beginning-of-statement) nil)
1457 (python-info-current-line-comment-p)
1458 (python-info-current-line-empty-p)))
1459 (forward-line -1))
1460 (let ((block-matching-indent
1461 (- (current-indentation) python-indent-offset)))
1462 (while
1463 (and (python-nav-backward-block)
1464 (> (current-indentation) block-matching-indent)))
1465 (if (and (looking-at (python-rx block-start))
1466 (= (current-indentation) block-matching-indent))
1467 (point-marker)
1468 (and (goto-char starting-pos) nil))))))
1469
1470 (defun python-nav-end-of-block ()
1471 "Move to end of current block."
1472 (interactive "^")
1473 (when (python-nav-beginning-of-block)
1474 (let ((block-indentation (current-indentation)))
1475 (python-nav-end-of-statement)
1476 (while (and (forward-line 1)
1477 (not (eobp))
1478 (or (and (> (current-indentation) block-indentation)
1479 (or (python-nav-end-of-statement) t))
1480 (python-info-current-line-comment-p)
1481 (python-info-current-line-empty-p))))
1482 (python-util-forward-comment -1)
1483 (point-marker))))
1484
1485 (defun python-nav-backward-block (&optional arg)
1486 "Move backward to previous block of code.
1487 With ARG, repeat. See `python-nav-forward-block'."
1488 (interactive "^p")
1489 (or arg (setq arg 1))
1490 (python-nav-forward-block (- arg)))
1491
1492 (defun python-nav-forward-block (&optional arg)
1493 "Move forward to next block of code.
1494 With ARG, repeat. With negative argument, move ARG times
1495 backward to previous block."
1496 (interactive "^p")
1497 (or arg (setq arg 1))
1498 (let ((block-start-regexp
1499 (python-rx line-start (* whitespace) block-start))
1500 (starting-pos (point)))
1501 (while (> arg 0)
1502 (python-nav-end-of-statement)
1503 (while (and
1504 (re-search-forward block-start-regexp nil t)
1505 (python-syntax-context-type)))
1506 (setq arg (1- arg)))
1507 (while (< arg 0)
1508 (python-nav-beginning-of-statement)
1509 (while (and
1510 (re-search-backward block-start-regexp nil t)
1511 (python-syntax-context-type)))
1512 (setq arg (1+ arg)))
1513 (python-nav-beginning-of-statement)
1514 (if (not (looking-at (python-rx block-start)))
1515 (and (goto-char starting-pos) nil)
1516 (and (not (= (point) starting-pos)) (point-marker)))))
1517
1518 (defun python-nav--lisp-forward-sexp (&optional arg)
1519 "Standard version `forward-sexp'.
1520 It ignores completely the value of `forward-sexp-function' by
1521 setting it to nil before calling `forward-sexp'. With positive
1522 ARG move forward only one sexp, else move backwards."
1523 (let ((forward-sexp-function)
1524 (arg (if (or (not arg) (> arg 0)) 1 -1)))
1525 (forward-sexp arg)))
1526
1527 (defun python-nav--lisp-forward-sexp-safe (&optional arg)
1528 "Safe version of standard `forward-sexp'.
1529 When at end of sexp (i.e. looking at a opening/closing paren)
1530 skips it instead of throwing an error. With positive ARG move
1531 forward only one sexp, else move backwards."
1532 (let* ((arg (if (or (not arg) (> arg 0)) 1 -1))
1533 (paren-regexp
1534 (if (> arg 0) (python-rx close-paren) (python-rx open-paren)))
1535 (search-fn
1536 (if (> arg 0) #'re-search-forward #'re-search-backward)))
1537 (condition-case nil
1538 (python-nav--lisp-forward-sexp arg)
1539 (error
1540 (while (and (funcall search-fn paren-regexp nil t)
1541 (python-syntax-context 'paren)))))))
1542
1543 (defun python-nav--forward-sexp (&optional dir safe)
1544 "Move to forward sexp.
1545 With positive optional argument DIR direction move forward, else
1546 backwards. When optional argument SAFE is non-nil do not throw
1547 errors when at end of sexp, skip it instead."
1548 (setq dir (or dir 1))
1549 (unless (= dir 0)
1550 (let* ((forward-p (if (> dir 0)
1551 (and (setq dir 1) t)
1552 (and (setq dir -1) nil)))
1553 (context-type (python-syntax-context-type)))
1554 (cond
1555 ((memq context-type '(string comment))
1556 ;; Inside of a string, get out of it.
1557 (let ((forward-sexp-function))
1558 (forward-sexp dir)))
1559 ((or (eq context-type 'paren)
1560 (and forward-p (looking-at (python-rx open-paren)))
1561 (and (not forward-p)
1562 (eq (syntax-class (syntax-after (1- (point))))
1563 (car (string-to-syntax ")")))))
1564 ;; Inside a paren or looking at it, lisp knows what to do.
1565 (if safe
1566 (python-nav--lisp-forward-sexp-safe dir)
1567 (python-nav--lisp-forward-sexp dir)))
1568 (t
1569 ;; This part handles the lispy feel of
1570 ;; `python-nav-forward-sexp'. Knowing everything about the
1571 ;; current context and the context of the next sexp tries to
1572 ;; follow the lisp sexp motion commands in a symmetric manner.
1573 (let* ((context
1574 (cond
1575 ((python-info-beginning-of-block-p) 'block-start)
1576 ((python-info-end-of-block-p) 'block-end)
1577 ((python-info-beginning-of-statement-p) 'statement-start)
1578 ((python-info-end-of-statement-p) 'statement-end)))
1579 (next-sexp-pos
1580 (save-excursion
1581 (if safe
1582 (python-nav--lisp-forward-sexp-safe dir)
1583 (python-nav--lisp-forward-sexp dir))
1584 (point)))
1585 (next-sexp-context
1586 (save-excursion
1587 (goto-char next-sexp-pos)
1588 (cond
1589 ((python-info-beginning-of-block-p) 'block-start)
1590 ((python-info-end-of-block-p) 'block-end)
1591 ((python-info-beginning-of-statement-p) 'statement-start)
1592 ((python-info-end-of-statement-p) 'statement-end)
1593 ((python-info-statement-starts-block-p) 'starts-block)
1594 ((python-info-statement-ends-block-p) 'ends-block)))))
1595 (if forward-p
1596 (cond ((and (not (eobp))
1597 (python-info-current-line-empty-p))
1598 (python-util-forward-comment dir)
1599 (python-nav--forward-sexp dir))
1600 ((eq context 'block-start)
1601 (python-nav-end-of-block))
1602 ((eq context 'statement-start)
1603 (python-nav-end-of-statement))
1604 ((and (memq context '(statement-end block-end))
1605 (eq next-sexp-context 'ends-block))
1606 (goto-char next-sexp-pos)
1607 (python-nav-end-of-block))
1608 ((and (memq context '(statement-end block-end))
1609 (eq next-sexp-context 'starts-block))
1610 (goto-char next-sexp-pos)
1611 (python-nav-end-of-block))
1612 ((memq context '(statement-end block-end))
1613 (goto-char next-sexp-pos)
1614 (python-nav-end-of-statement))
1615 (t (goto-char next-sexp-pos)))
1616 (cond ((and (not (bobp))
1617 (python-info-current-line-empty-p))
1618 (python-util-forward-comment dir)
1619 (python-nav--forward-sexp dir))
1620 ((eq context 'block-end)
1621 (python-nav-beginning-of-block))
1622 ((eq context 'statement-end)
1623 (python-nav-beginning-of-statement))
1624 ((and (memq context '(statement-start block-start))
1625 (eq next-sexp-context 'starts-block))
1626 (goto-char next-sexp-pos)
1627 (python-nav-beginning-of-block))
1628 ((and (memq context '(statement-start block-start))
1629 (eq next-sexp-context 'ends-block))
1630 (goto-char next-sexp-pos)
1631 (python-nav-beginning-of-block))
1632 ((memq context '(statement-start block-start))
1633 (goto-char next-sexp-pos)
1634 (python-nav-beginning-of-statement))
1635 (t (goto-char next-sexp-pos))))))))))
1636
1637 (defun python-nav-forward-sexp (&optional arg)
1638 "Move forward across expressions.
1639 With ARG, do it that many times. Negative arg -N means move
1640 backward N times."
1641 (interactive "^p")
1642 (or arg (setq arg 1))
1643 (while (> arg 0)
1644 (python-nav--forward-sexp 1)
1645 (setq arg (1- arg)))
1646 (while (< arg 0)
1647 (python-nav--forward-sexp -1)
1648 (setq arg (1+ arg))))
1649
1650 (defun python-nav-backward-sexp (&optional arg)
1651 "Move backward across expressions.
1652 With ARG, do it that many times. Negative arg -N means move
1653 forward N times."
1654 (interactive "^p")
1655 (or arg (setq arg 1))
1656 (python-nav-forward-sexp (- arg)))
1657
1658 (defun python-nav-forward-sexp-safe (&optional arg)
1659 "Move forward safely across expressions.
1660 With ARG, do it that many times. Negative arg -N means move
1661 backward N times."
1662 (interactive "^p")
1663 (or arg (setq arg 1))
1664 (while (> arg 0)
1665 (python-nav--forward-sexp 1 t)
1666 (setq arg (1- arg)))
1667 (while (< arg 0)
1668 (python-nav--forward-sexp -1 t)
1669 (setq arg (1+ arg))))
1670
1671 (defun python-nav-backward-sexp-safe (&optional arg)
1672 "Move backward safely across expressions.
1673 With ARG, do it that many times. Negative arg -N means move
1674 forward N times."
1675 (interactive "^p")
1676 (or arg (setq arg 1))
1677 (python-nav-forward-sexp-safe (- arg)))
1678
1679 (defun python-nav--up-list (&optional dir)
1680 "Internal implementation of `python-nav-up-list'.
1681 DIR is always 1 or -1 and comes sanitized from
1682 `python-nav-up-list' calls."
1683 (let ((context (python-syntax-context-type))
1684 (forward-p (> dir 0)))
1685 (cond
1686 ((memq context '(string comment)))
1687 ((eq context 'paren)
1688 (let ((forward-sexp-function))
1689 (up-list dir)))
1690 ((and forward-p (python-info-end-of-block-p))
1691 (let ((parent-end-pos
1692 (save-excursion
1693 (let ((indentation (and
1694 (python-nav-beginning-of-block)
1695 (current-indentation))))
1696 (while (and indentation
1697 (> indentation 0)
1698 (>= (current-indentation) indentation)
1699 (python-nav-backward-block)))
1700 (python-nav-end-of-block)))))
1701 (and (> (or parent-end-pos (point)) (point))
1702 (goto-char parent-end-pos))))
1703 (forward-p (python-nav-end-of-block))
1704 ((and (not forward-p)
1705 (> (current-indentation) 0)
1706 (python-info-beginning-of-block-p))
1707 (let ((prev-block-pos
1708 (save-excursion
1709 (let ((indentation (current-indentation)))
1710 (while (and (python-nav-backward-block)
1711 (>= (current-indentation) indentation))))
1712 (point))))
1713 (and (> (point) prev-block-pos)
1714 (goto-char prev-block-pos))))
1715 ((not forward-p) (python-nav-beginning-of-block)))))
1716
1717 (defun python-nav-up-list (&optional arg)
1718 "Move forward out of one level of parentheses (or blocks).
1719 With ARG, do this that many times.
1720 A negative argument means move backward but still to a less deep spot.
1721 This command assumes point is not in a string or comment."
1722 (interactive "^p")
1723 (or arg (setq arg 1))
1724 (while (> arg 0)
1725 (python-nav--up-list 1)
1726 (setq arg (1- arg)))
1727 (while (< arg 0)
1728 (python-nav--up-list -1)
1729 (setq arg (1+ arg))))
1730
1731 (defun python-nav-backward-up-list (&optional arg)
1732 "Move backward out of one level of parentheses (or blocks).
1733 With ARG, do this that many times.
1734 A negative argument means move forward but still to a less deep spot.
1735 This command assumes point is not in a string or comment."
1736 (interactive "^p")
1737 (or arg (setq arg 1))
1738 (python-nav-up-list (- arg)))
1739
1740 (defun python-nav-if-name-main ()
1741 "Move point at the beginning the __main__ block.
1742 When \"if __name__ == '__main__':\" is found returns its
1743 position, else returns nil."
1744 (interactive)
1745 (let ((point (point))
1746 (found (catch 'found
1747 (goto-char (point-min))
1748 (while (re-search-forward
1749 (python-rx line-start
1750 "if" (+ space)
1751 "__name__" (+ space)
1752 "==" (+ space)
1753 (group-n 1 (or ?\" ?\'))
1754 "__main__" (backref 1) (* space) ":")
1755 nil t)
1756 (when (not (python-syntax-context-type))
1757 (beginning-of-line)
1758 (throw 'found t))))))
1759 (if found
1760 (point)
1761 (ignore (goto-char point)))))
1762
1763 \f
1764 ;;; Shell integration
1765
1766 (defcustom python-shell-buffer-name "Python"
1767 "Default buffer name for Python interpreter."
1768 :type 'string
1769 :group 'python
1770 :safe 'stringp)
1771
1772 (defcustom python-shell-interpreter "python"
1773 "Default Python interpreter for shell."
1774 :type 'string
1775 :group 'python)
1776
1777 (defcustom python-shell-internal-buffer-name "Python Internal"
1778 "Default buffer name for the Internal Python interpreter."
1779 :type 'string
1780 :group 'python
1781 :safe 'stringp)
1782
1783 (defcustom python-shell-interpreter-args "-i"
1784 "Default arguments for the Python interpreter."
1785 :type 'string
1786 :group 'python)
1787
1788 (defcustom python-shell-interpreter-interactive-arg "-i"
1789 "Interpreter argument to force it to run interactively."
1790 :type 'string
1791 :version "24.4")
1792
1793 (defcustom python-shell-prompt-detect-enabled t
1794 "Non-nil enables autodetection of interpreter prompts."
1795 :type 'boolean
1796 :safe 'booleanp
1797 :version "24.4")
1798
1799 (defcustom python-shell-prompt-detect-failure-warning t
1800 "Non-nil enables warnings when detection of prompts fail."
1801 :type 'boolean
1802 :safe 'booleanp
1803 :version "24.4")
1804
1805 (defcustom python-shell-prompt-input-regexps
1806 '(">>> " "\\.\\.\\. " ; Python
1807 "In \\[[0-9]+\\]: " ; IPython
1808 ;; Using ipdb outside IPython may fail to cleanup and leave static
1809 ;; IPython prompts activated, this adds some safeguard for that.
1810 "In : " "\\.\\.\\.: ")
1811 "List of regular expressions matching input prompts."
1812 :type '(repeat string)
1813 :version "24.4")
1814
1815 (defcustom python-shell-prompt-output-regexps
1816 '("" ; Python
1817 "Out\\[[0-9]+\\]: " ; IPython
1818 "Out :") ; ipdb safeguard
1819 "List of regular expressions matching output prompts."
1820 :type '(repeat string)
1821 :version "24.4")
1822
1823 (defcustom python-shell-prompt-regexp ">>> "
1824 "Regular expression matching top level input prompt of Python shell.
1825 It should not contain a caret (^) at the beginning."
1826 :type 'string)
1827
1828 (defcustom python-shell-prompt-block-regexp "\\.\\.\\. "
1829 "Regular expression matching block input prompt of Python shell.
1830 It should not contain a caret (^) at the beginning."
1831 :type 'string)
1832
1833 (defcustom python-shell-prompt-output-regexp ""
1834 "Regular expression matching output prompt of Python shell.
1835 It should not contain a caret (^) at the beginning."
1836 :type 'string)
1837
1838 (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ "
1839 "Regular expression matching pdb input prompt of Python shell.
1840 It should not contain a caret (^) at the beginning."
1841 :type 'string)
1842
1843 (defcustom python-shell-enable-font-lock t
1844 "Should syntax highlighting be enabled in the Python shell buffer?
1845 Restart the Python shell after changing this variable for it to take effect."
1846 :type 'boolean
1847 :group 'python
1848 :safe 'booleanp)
1849
1850 (defcustom python-shell-unbuffered t
1851 "Should shell output be unbuffered?.
1852 When non-nil, this may prevent delayed and missing output in the
1853 Python shell. See commentary for details."
1854 :type 'boolean
1855 :group 'python
1856 :safe 'booleanp)
1857
1858 (defcustom python-shell-process-environment nil
1859 "List of environment variables for Python shell.
1860 This variable follows the same rules as `process-environment'
1861 since it merges with it before the process creation routines are
1862 called. When this variable is nil, the Python shell is run with
1863 the default `process-environment'."
1864 :type '(repeat string)
1865 :group 'python
1866 :safe 'listp)
1867
1868 (defcustom python-shell-extra-pythonpaths nil
1869 "List of extra pythonpaths for Python shell.
1870 The values of this variable are added to the existing value of
1871 PYTHONPATH in the `process-environment' variable."
1872 :type '(repeat string)
1873 :group 'python
1874 :safe 'listp)
1875
1876 (defcustom python-shell-exec-path nil
1877 "List of path to search for binaries.
1878 This variable follows the same rules as `exec-path' since it
1879 merges with it before the process creation routines are called.
1880 When this variable is nil, the Python shell is run with the
1881 default `exec-path'."
1882 :type '(repeat string)
1883 :group 'python
1884 :safe 'listp)
1885
1886 (defcustom python-shell-virtualenv-path nil
1887 "Path to virtualenv root.
1888 This variable, when set to a string, makes the values stored in
1889 `python-shell-process-environment' and `python-shell-exec-path'
1890 to be modified properly so shells are started with the specified
1891 virtualenv."
1892 :type '(choice (const nil) string)
1893 :group 'python
1894 :safe 'stringp)
1895
1896 (defcustom python-shell-setup-codes '(python-shell-completion-setup-code
1897 python-ffap-setup-code
1898 python-eldoc-setup-code)
1899 "List of code run by `python-shell-send-setup-codes'."
1900 :type '(repeat symbol)
1901 :group 'python
1902 :safe 'listp)
1903
1904 (defcustom python-shell-compilation-regexp-alist
1905 `((,(rx line-start (1+ (any " \t")) "File \""
1906 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
1907 "\", line " (group (1+ digit)))
1908 1 2)
1909 (,(rx " in file " (group (1+ not-newline)) " on line "
1910 (group (1+ digit)))
1911 1 2)
1912 (,(rx line-start "> " (group (1+ (not (any "(\"<"))))
1913 "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()")
1914 1 2))
1915 "`compilation-error-regexp-alist' for inferior Python."
1916 :type '(alist string)
1917 :group 'python)
1918
1919 (defvar python-shell--prompt-calculated-input-regexp nil
1920 "Calculated input prompt regexp for inferior python shell.
1921 Do not set this variable directly, instead use
1922 `python-shell-prompt-set-calculated-regexps'.")
1923
1924 (defvar python-shell--prompt-calculated-output-regexp nil
1925 "Calculated output prompt regexp for inferior python shell.
1926 Do not set this variable directly, instead use
1927 `python-shell-set-prompt-regexp'.")
1928
1929 (defun python-shell-prompt-detect ()
1930 "Detect prompts for the current `python-shell-interpreter'.
1931 When prompts can be retrieved successfully from the
1932 `python-shell-interpreter' run with
1933 `python-shell-interpreter-interactive-arg', returns a list of
1934 three elements, where the first two are input prompts and the
1935 last one is an output prompt. When no prompts can be detected
1936 and `python-shell-prompt-detect-failure-warning' is non-nil,
1937 shows a warning with instructions to avoid hangs and returns nil.
1938 When `python-shell-prompt-detect-enabled' is nil avoids any
1939 detection and just returns nil."
1940 (when python-shell-prompt-detect-enabled
1941 (let* ((process-environment (python-shell-calculate-process-environment))
1942 (exec-path (python-shell-calculate-exec-path))
1943 (code (concat
1944 "import sys\n"
1945 "ps = [getattr(sys, 'ps%s' % i, '') for i in range(1,4)]\n"
1946 ;; JSON is built manually for compatibility
1947 "ps_json = '\\n[\"%s\", \"%s\", \"%s\"]\\n' % tuple(ps)\n"
1948 "print (ps_json)\n"
1949 "sys.exit(0)\n"))
1950 (output
1951 (with-temp-buffer
1952 ;; TODO: improve error handling by using
1953 ;; `condition-case' and displaying the error message to
1954 ;; the user in the no-prompts warning.
1955 (ignore-errors
1956 (let ((code-file (python-shell--save-temp-file code)))
1957 ;; Use `process-file' as it is remote-host friendly.
1958 (process-file
1959 python-shell-interpreter
1960 code-file
1961 '(t nil)
1962 nil
1963 python-shell-interpreter-interactive-arg)
1964 ;; Try to cleanup
1965 (delete-file code-file)))
1966 (buffer-string)))
1967 (prompts
1968 (catch 'prompts
1969 (dolist (line (split-string output "\n" t))
1970 (let ((res
1971 ;; Check if current line is a valid JSON array
1972 (and (string= (substring line 0 2) "[\"")
1973 (ignore-errors
1974 ;; Return prompts as a list, not vector
1975 (append (json-read-from-string line) nil)))))
1976 ;; The list must contain 3 strings, where the first
1977 ;; is the input prompt, the second is the block
1978 ;; prompt and the last one is the output prompt. The
1979 ;; input prompt is the only one that can't be empty.
1980 (when (and (= (length res) 3)
1981 (cl-every #'stringp res)
1982 (not (string= (car res) "")))
1983 (throw 'prompts res))))
1984 nil)))
1985 (when (and (not prompts)
1986 python-shell-prompt-detect-failure-warning)
1987 (warn
1988 (concat
1989 "Python shell prompts cannot be detected.\n"
1990 "If your emacs session hangs when starting python shells\n"
1991 "recover with `keyboard-quit' and then try fixing the\n"
1992 "interactive flag for your interpreter by adjusting the\n"
1993 "`python-shell-interpreter-interactive-arg' or add regexps\n"
1994 "matching shell prompts in the directory-local friendly vars:\n"
1995 " + `python-shell-prompt-regexp'\n"
1996 " + `python-shell-prompt-block-regexp'\n"
1997 " + `python-shell-prompt-output-regexp'\n"
1998 "Or alternatively in:\n"
1999 " + `python-shell-prompt-input-regexps'\n"
2000 " + `python-shell-prompt-output-regexps'")))
2001 prompts)))
2002
2003 (defun python-shell-prompt-validate-regexps ()
2004 "Validate all user provided regexps for prompts.
2005 Signals `user-error' if any of these vars contain invalid
2006 regexps: `python-shell-prompt-regexp',
2007 `python-shell-prompt-block-regexp',
2008 `python-shell-prompt-pdb-regexp',
2009 `python-shell-prompt-output-regexp',
2010 `python-shell-prompt-input-regexps',
2011 `python-shell-prompt-output-regexps'."
2012 (dolist (symbol (list 'python-shell-prompt-input-regexps
2013 'python-shell-prompt-output-regexps
2014 'python-shell-prompt-regexp
2015 'python-shell-prompt-block-regexp
2016 'python-shell-prompt-pdb-regexp
2017 'python-shell-prompt-output-regexp))
2018 (dolist (regexp (let ((regexps (symbol-value symbol)))
2019 (if (listp regexps)
2020 regexps
2021 (list regexps))))
2022 (when (not (python-util-valid-regexp-p regexp))
2023 (user-error "Invalid regexp %s in `%s'"
2024 regexp symbol)))))
2025
2026 (defun python-shell-prompt-set-calculated-regexps ()
2027 "Detect and set input and output prompt regexps.
2028 Build and set the values for `python-shell-input-prompt-regexp'
2029 and `python-shell-output-prompt-regexp' using the values from
2030 `python-shell-prompt-regexp', `python-shell-prompt-block-regexp',
2031 `python-shell-prompt-pdb-regexp',
2032 `python-shell-prompt-output-regexp',
2033 `python-shell-prompt-input-regexps',
2034 `python-shell-prompt-output-regexps' and detected prompts from
2035 `python-shell-prompt-detect'."
2036 (when (not (and python-shell--prompt-calculated-input-regexp
2037 python-shell--prompt-calculated-output-regexp))
2038 (let* ((detected-prompts (python-shell-prompt-detect))
2039 (input-prompts nil)
2040 (output-prompts nil)
2041 (build-regexp
2042 (lambda (prompts)
2043 (concat "^\\("
2044 (mapconcat #'identity
2045 (sort prompts
2046 (lambda (a b)
2047 (let ((length-a (length a))
2048 (length-b (length b)))
2049 (if (= length-a length-b)
2050 (string< a b)
2051 (> (length a) (length b))))))
2052 "\\|")
2053 "\\)"))))
2054 ;; Validate ALL regexps
2055 (python-shell-prompt-validate-regexps)
2056 ;; Collect all user defined input prompts
2057 (dolist (prompt (append python-shell-prompt-input-regexps
2058 (list python-shell-prompt-regexp
2059 python-shell-prompt-block-regexp
2060 python-shell-prompt-pdb-regexp)))
2061 (cl-pushnew prompt input-prompts :test #'string=))
2062 ;; Collect all user defined output prompts
2063 (dolist (prompt (cons python-shell-prompt-output-regexp
2064 python-shell-prompt-output-regexps))
2065 (cl-pushnew prompt output-prompts :test #'string=))
2066 ;; Collect detected prompts if any
2067 (when detected-prompts
2068 (dolist (prompt (butlast detected-prompts))
2069 (setq prompt (regexp-quote prompt))
2070 (cl-pushnew prompt input-prompts :test #'string=))
2071 (cl-pushnew (regexp-quote
2072 (car (last detected-prompts)))
2073 output-prompts :test #'string=))
2074 ;; Set input and output prompt regexps from collected prompts
2075 (setq python-shell--prompt-calculated-input-regexp
2076 (funcall build-regexp input-prompts)
2077 python-shell--prompt-calculated-output-regexp
2078 (funcall build-regexp output-prompts)))))
2079
2080 (defun python-shell-get-process-name (dedicated)
2081 "Calculate the appropriate process name for inferior Python process.
2082 If DEDICATED is t and the variable `buffer-file-name' is non-nil
2083 returns a string with the form
2084 `python-shell-buffer-name'[variable `buffer-file-name'] else
2085 returns the value of `python-shell-buffer-name'."
2086 (let ((process-name
2087 (if (and dedicated
2088 buffer-file-name)
2089 (format "%s[%s]" python-shell-buffer-name buffer-file-name)
2090 (format "%s" python-shell-buffer-name))))
2091 process-name))
2092
2093 (defun python-shell-internal-get-process-name ()
2094 "Calculate the appropriate process name for Internal Python process.
2095 The name is calculated from `python-shell-global-buffer-name' and
2096 a hash of all relevant global shell settings in order to ensure
2097 uniqueness for different types of configurations."
2098 (format "%s [%s]"
2099 python-shell-internal-buffer-name
2100 (md5
2101 (concat
2102 python-shell-interpreter
2103 python-shell-interpreter-args
2104 python-shell--prompt-calculated-input-regexp
2105 python-shell--prompt-calculated-output-regexp
2106 (mapconcat #'symbol-value python-shell-setup-codes "")
2107 (mapconcat #'identity python-shell-process-environment "")
2108 (mapconcat #'identity python-shell-extra-pythonpaths "")
2109 (mapconcat #'identity python-shell-exec-path "")
2110 (or python-shell-virtualenv-path "")
2111 (mapconcat #'identity python-shell-exec-path "")))))
2112
2113 (defun python-shell-parse-command () ;FIXME: why name it "parse"?
2114 "Calculate the string used to execute the inferior Python process."
2115 ;; FIXME: process-environment doesn't seem to be used anywhere within
2116 ;; this let.
2117 (let ((process-environment (python-shell-calculate-process-environment))
2118 (exec-path (python-shell-calculate-exec-path)))
2119 (format "%s %s"
2120 ;; FIXME: Why executable-find?
2121 (shell-quote-argument
2122 (executable-find python-shell-interpreter))
2123 python-shell-interpreter-args)))
2124
2125 (defun python-shell-calculate-process-environment ()
2126 "Calculate process environment given `python-shell-virtualenv-path'."
2127 (let ((process-environment (append
2128 python-shell-process-environment
2129 process-environment nil))
2130 (virtualenv (if python-shell-virtualenv-path
2131 (directory-file-name python-shell-virtualenv-path)
2132 nil)))
2133 (when python-shell-unbuffered
2134 (setenv "PYTHONUNBUFFERED" "1"))
2135 (when python-shell-extra-pythonpaths
2136 (setenv "PYTHONPATH"
2137 (format "%s%s%s"
2138 (mapconcat 'identity
2139 python-shell-extra-pythonpaths
2140 path-separator)
2141 path-separator
2142 (or (getenv "PYTHONPATH") ""))))
2143 (if (not virtualenv)
2144 process-environment
2145 (setenv "PYTHONHOME" nil)
2146 (setenv "PATH" (format "%s/bin%s%s"
2147 virtualenv path-separator
2148 (or (getenv "PATH") "")))
2149 (setenv "VIRTUAL_ENV" virtualenv))
2150 process-environment))
2151
2152 (defun python-shell-calculate-exec-path ()
2153 "Calculate exec path given `python-shell-virtualenv-path'."
2154 (let ((path (append python-shell-exec-path
2155 exec-path nil))) ;FIXME: Why nil?
2156 (if (not python-shell-virtualenv-path)
2157 path
2158 (cons (expand-file-name "bin" python-shell-virtualenv-path)
2159 path))))
2160
2161 (defun python-comint-output-filter-function (output)
2162 "Hook run after content is put into comint buffer.
2163 OUTPUT is a string with the contents of the buffer."
2164 (ansi-color-filter-apply output))
2165
2166 (defvar python-shell--parent-buffer nil)
2167
2168 (defvar python-shell-output-syntax-table
2169 (let ((table (make-syntax-table python-dotty-syntax-table)))
2170 (modify-syntax-entry ?\' "." table)
2171 (modify-syntax-entry ?\" "." table)
2172 (modify-syntax-entry ?\( "." table)
2173 (modify-syntax-entry ?\[ "." table)
2174 (modify-syntax-entry ?\{ "." table)
2175 (modify-syntax-entry ?\) "." table)
2176 (modify-syntax-entry ?\] "." table)
2177 (modify-syntax-entry ?\} "." table)
2178 table)
2179 "Syntax table for shell output.
2180 It makes parens and quotes be treated as punctuation chars.")
2181
2182 (define-derived-mode inferior-python-mode comint-mode "Inferior Python"
2183 "Major mode for Python inferior process.
2184 Runs a Python interpreter as a subprocess of Emacs, with Python
2185 I/O through an Emacs buffer. Variables `python-shell-interpreter'
2186 and `python-shell-interpreter-args' control which Python
2187 interpreter is run. Variables
2188 `python-shell-prompt-regexp',
2189 `python-shell-prompt-output-regexp',
2190 `python-shell-prompt-block-regexp',
2191 `python-shell-enable-font-lock',
2192 `python-shell-completion-setup-code',
2193 `python-shell-completion-string-code',
2194 `python-eldoc-setup-code', `python-eldoc-string-code',
2195 `python-ffap-setup-code' and `python-ffap-string-code' can
2196 customize this mode for different Python interpreters.
2197
2198 You can also add additional setup code to be run at
2199 initialization of the interpreter via `python-shell-setup-codes'
2200 variable.
2201
2202 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
2203 (let ((interpreter python-shell-interpreter)
2204 (args python-shell-interpreter-args))
2205 (when python-shell--parent-buffer
2206 (python-util-clone-local-variables python-shell--parent-buffer))
2207 ;; Users can override default values for these vars when calling
2208 ;; `run-python'. This ensures new values let-bound in
2209 ;; `python-shell-make-comint' are locally set.
2210 (set (make-local-variable 'python-shell-interpreter) interpreter)
2211 (set (make-local-variable 'python-shell-interpreter-args) args))
2212 (set (make-local-variable 'python-shell--prompt-calculated-input-regexp) nil)
2213 (set (make-local-variable 'python-shell--prompt-calculated-output-regexp) nil)
2214 (python-shell-prompt-set-calculated-regexps)
2215 (setq comint-prompt-regexp python-shell--prompt-calculated-input-regexp)
2216 (setq mode-line-process '(":%s"))
2217 (make-local-variable 'comint-output-filter-functions)
2218 (add-hook 'comint-output-filter-functions
2219 'python-comint-output-filter-function)
2220 (add-hook 'comint-output-filter-functions
2221 'python-pdbtrack-comint-output-filter-function)
2222 (set (make-local-variable 'compilation-error-regexp-alist)
2223 python-shell-compilation-regexp-alist)
2224 (define-key inferior-python-mode-map [remap complete-symbol]
2225 'completion-at-point)
2226 (add-hook 'completion-at-point-functions
2227 #'python-shell-completion-complete-at-point nil 'local)
2228 (add-hook 'comint-dynamic-complete-functions ;FIXME: really?
2229 #'python-shell-completion-complete-at-point nil 'local)
2230 (define-key inferior-python-mode-map "\t"
2231 'python-shell-completion-complete-or-indent)
2232 (make-local-variable 'python-pdbtrack-buffers-to-kill)
2233 (make-local-variable 'python-pdbtrack-tracked-buffer)
2234 (make-local-variable 'python-shell-internal-last-output)
2235 (when python-shell-enable-font-lock
2236 (set-syntax-table python-mode-syntax-table)
2237 (set (make-local-variable 'font-lock-defaults)
2238 '(python-font-lock-keywords nil nil nil nil))
2239 (set (make-local-variable 'syntax-propertize-function)
2240 (eval
2241 ;; XXX: Unfortunately eval is needed here to make use of the
2242 ;; dynamic value of `comint-prompt-regexp'.
2243 `(syntax-propertize-rules
2244 (,comint-prompt-regexp
2245 (0 (ignore
2246 (put-text-property
2247 comint-last-input-start end 'syntax-table
2248 python-shell-output-syntax-table)
2249 ;; XXX: This might look weird, but it is the easiest
2250 ;; way to ensure font lock gets cleaned up before the
2251 ;; current prompt, which is needed for unclosed
2252 ;; strings to not mess up with current input.
2253 (font-lock-unfontify-region comint-last-input-start end))))
2254 (,(python-rx string-delimiter)
2255 (0 (ignore
2256 (and (not (eq (get-text-property start 'field) 'output))
2257 (python-syntax-stringify)))))))))
2258 (compilation-shell-minor-mode 1))
2259
2260 (defun python-shell-make-comint (cmd proc-name &optional pop internal)
2261 "Create a Python shell comint buffer.
2262 CMD is the Python command to be executed and PROC-NAME is the
2263 process name the comint buffer will get. After the comint buffer
2264 is created the `inferior-python-mode' is activated. When
2265 optional argument POP is non-nil the buffer is shown. When
2266 optional argument INTERNAL is non-nil this process is run on a
2267 buffer with a name that starts with a space, following the Emacs
2268 convention for temporary/internal buffers, and also makes sure
2269 the user is not queried for confirmation when the process is
2270 killed."
2271 (save-excursion
2272 (let* ((proc-buffer-name
2273 (format (if (not internal) "*%s*" " *%s*") proc-name))
2274 (process-environment (python-shell-calculate-process-environment))
2275 (exec-path (python-shell-calculate-exec-path)))
2276 (when (not (comint-check-proc proc-buffer-name))
2277 (let* ((cmdlist (split-string-and-unquote cmd))
2278 (interpreter (car cmdlist))
2279 (args (cdr cmdlist))
2280 (buffer (apply #'make-comint-in-buffer proc-name proc-buffer-name
2281 interpreter nil args))
2282 (python-shell--parent-buffer (current-buffer))
2283 (process (get-buffer-process buffer))
2284 ;; As the user may have overridden default values for
2285 ;; these vars on `run-python', let-binding them allows
2286 ;; to have the new right values in all setup code
2287 ;; that's is done in `inferior-python-mode', which is
2288 ;; important, especially for prompt detection.
2289 (python-shell-interpreter interpreter)
2290 (python-shell-interpreter-args
2291 (mapconcat #'identity args " ")))
2292 (with-current-buffer buffer
2293 (inferior-python-mode))
2294 (accept-process-output process)
2295 (and pop (pop-to-buffer buffer t))
2296 (and internal (set-process-query-on-exit-flag process nil))))
2297 proc-buffer-name)))
2298
2299 ;;;###autoload
2300 (defun run-python (cmd &optional dedicated show)
2301 "Run an inferior Python process.
2302 Input and output via buffer named after
2303 `python-shell-buffer-name'. If there is a process already
2304 running in that buffer, just switch to it.
2305
2306 With argument, allows you to define CMD so you can edit the
2307 command used to call the interpreter and define DEDICATED, so a
2308 dedicated process for the current buffer is open. When numeric
2309 prefix arg is other than 0 or 4 do not SHOW.
2310
2311 Runs the hook `inferior-python-mode-hook' after
2312 `comint-mode-hook' is run. (Type \\[describe-mode] in the
2313 process buffer for a list of commands.)"
2314 (interactive
2315 (if current-prefix-arg
2316 (list
2317 (read-string "Run Python: " (python-shell-parse-command))
2318 (y-or-n-p "Make dedicated process? ")
2319 (= (prefix-numeric-value current-prefix-arg) 4))
2320 (list (python-shell-parse-command) nil t)))
2321 (python-shell-make-comint
2322 cmd (python-shell-get-process-name dedicated) show)
2323 dedicated)
2324
2325 (defun run-python-internal ()
2326 "Run an inferior Internal Python process.
2327 Input and output via buffer named after
2328 `python-shell-internal-buffer-name' and what
2329 `python-shell-internal-get-process-name' returns.
2330
2331 This new kind of shell is intended to be used for generic
2332 communication related to defined configurations; the main
2333 difference with global or dedicated shells is that these ones are
2334 attached to a configuration, not a buffer. This means that can
2335 be used for example to retrieve the sys.path and other stuff,
2336 without messing with user shells. Note that
2337 `python-shell-enable-font-lock' and `inferior-python-mode-hook'
2338 are set to nil for these shells, so setup codes are not sent at
2339 startup."
2340 (let ((python-shell-enable-font-lock nil)
2341 (inferior-python-mode-hook nil))
2342 (get-buffer-process
2343 (python-shell-make-comint
2344 (python-shell-parse-command)
2345 (python-shell-internal-get-process-name) nil t))))
2346
2347 (defun python-shell-get-buffer ()
2348 "Return inferior Python buffer for current buffer."
2349 (let* ((dedicated-proc-name (python-shell-get-process-name t))
2350 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
2351 (global-proc-name (python-shell-get-process-name nil))
2352 (global-proc-buffer-name (format "*%s*" global-proc-name))
2353 (dedicated-running (comint-check-proc dedicated-proc-buffer-name))
2354 (global-running (comint-check-proc global-proc-buffer-name)))
2355 ;; Always prefer dedicated
2356 (or (and dedicated-running dedicated-proc-buffer-name)
2357 (and global-running global-proc-buffer-name))))
2358
2359 (defun python-shell-get-process ()
2360 "Return inferior Python process for current buffer."
2361 (get-buffer-process (python-shell-get-buffer)))
2362
2363 (defun python-shell-get-or-create-process (&optional cmd dedicated show)
2364 "Get or create an inferior Python process for current buffer and return it.
2365 Arguments CMD, DEDICATED and SHOW are those of `run-python' and
2366 are used to start the shell. If those arguments are not
2367 provided, `run-python' is called interactively and the user will
2368 be asked for their values."
2369 (let* ((dedicated-proc-name (python-shell-get-process-name t))
2370 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
2371 (global-proc-name (python-shell-get-process-name nil))
2372 (global-proc-buffer-name (format "*%s*" global-proc-name))
2373 (dedicated-running (comint-check-proc dedicated-proc-buffer-name))
2374 (global-running (comint-check-proc global-proc-buffer-name))
2375 (current-prefix-arg 16))
2376 (when (and (not dedicated-running) (not global-running))
2377 (if (if (not cmd)
2378 ;; XXX: Refactor code such that calling `run-python'
2379 ;; interactively is not needed anymore.
2380 (call-interactively 'run-python)
2381 (run-python cmd dedicated show))
2382 (setq dedicated-running t)
2383 (setq global-running t)))
2384 ;; Always prefer dedicated
2385 (get-buffer-process (if dedicated-running
2386 dedicated-proc-buffer-name
2387 global-proc-buffer-name))))
2388
2389 (defvar python-shell-internal-buffer nil
2390 "Current internal shell buffer for the current buffer.
2391 This is really not necessary at all for the code to work but it's
2392 there for compatibility with CEDET.")
2393
2394 (defvar python-shell-internal-last-output nil
2395 "Last output captured by the internal shell.
2396 This is really not necessary at all for the code to work but it's
2397 there for compatibility with CEDET.")
2398
2399 (defun python-shell-internal-get-or-create-process ()
2400 "Get or create an inferior Internal Python process."
2401 (let* ((proc-name (python-shell-internal-get-process-name))
2402 (proc-buffer-name (format " *%s*" proc-name)))
2403 (when (not (process-live-p proc-name))
2404 (run-python-internal)
2405 (setq python-shell-internal-buffer proc-buffer-name)
2406 ;; XXX: Why is this `sit-for' needed?
2407 ;; `python-shell-make-comint' calls `accept-process-output'
2408 ;; already but it is not helping to get proper output on
2409 ;; 'gnu/linux when the internal shell process is not running and
2410 ;; a call to `python-shell-internal-send-string' is issued.
2411 (sit-for 0.1 t))
2412 (get-buffer-process proc-buffer-name)))
2413
2414 (define-obsolete-function-alias
2415 'python-proc 'python-shell-internal-get-or-create-process "24.3")
2416
2417 (define-obsolete-variable-alias
2418 'python-buffer 'python-shell-internal-buffer "24.3")
2419
2420 (define-obsolete-variable-alias
2421 'python-preoutput-result 'python-shell-internal-last-output "24.3")
2422
2423 (defun python-shell--save-temp-file (string)
2424 (let* ((temporary-file-directory
2425 (if (file-remote-p default-directory)
2426 (concat (file-remote-p default-directory) "/tmp")
2427 temporary-file-directory))
2428 (temp-file-name (make-temp-file "py"))
2429 (coding-system-for-write (python-info-encoding)))
2430 (with-temp-file temp-file-name
2431 (insert string)
2432 (delete-trailing-whitespace))
2433 temp-file-name))
2434
2435 (defun python-shell-send-string (string &optional process)
2436 "Send STRING to inferior Python PROCESS."
2437 (interactive "sPython command: ")
2438 (let ((process (or process (python-shell-get-or-create-process))))
2439 (if (string-match ".\n+." string) ;Multiline.
2440 (let* ((temp-file-name (python-shell--save-temp-file string))
2441 (file-name (or (buffer-file-name) temp-file-name)))
2442 (python-shell-send-file file-name process temp-file-name t))
2443 (comint-send-string process string)
2444 (when (or (not (string-match "\n\\'" string))
2445 (string-match "\n[ \t].*\n?\\'" string))
2446 (comint-send-string process "\n")))))
2447
2448 (defvar python-shell-output-filter-in-progress nil)
2449 (defvar python-shell-output-filter-buffer nil)
2450
2451 (defun python-shell-output-filter (string)
2452 "Filter used in `python-shell-send-string-no-output' to grab output.
2453 STRING is the output received to this point from the process.
2454 This filter saves received output from the process in
2455 `python-shell-output-filter-buffer' and stops receiving it after
2456 detecting a prompt at the end of the buffer."
2457 (setq
2458 string (ansi-color-filter-apply string)
2459 python-shell-output-filter-buffer
2460 (concat python-shell-output-filter-buffer string))
2461 (when (string-match
2462 ;; XXX: It seems on OSX an extra carriage return is attached
2463 ;; at the end of output, this handles that too.
2464 (concat
2465 "\r?\n"
2466 ;; Remove initial caret from calculated regexp
2467 (replace-regexp-in-string
2468 (rx string-start ?^) ""
2469 python-shell--prompt-calculated-input-regexp)
2470 "$")
2471 python-shell-output-filter-buffer)
2472 ;; Output ends when `python-shell-output-filter-buffer' contains
2473 ;; the prompt attached at the end of it.
2474 (setq python-shell-output-filter-in-progress nil
2475 python-shell-output-filter-buffer
2476 (substring python-shell-output-filter-buffer
2477 0 (match-beginning 0)))
2478 (when (string-match
2479 python-shell--prompt-calculated-output-regexp
2480 python-shell-output-filter-buffer)
2481 ;; Some shells, like IPython might append a prompt before the
2482 ;; output, clean that.
2483 (setq python-shell-output-filter-buffer
2484 (substring python-shell-output-filter-buffer (match-end 0)))))
2485 "")
2486
2487 (defun python-shell-send-string-no-output (string &optional process)
2488 "Send STRING to PROCESS and inhibit output.
2489 Return the output."
2490 (let ((process (or process (python-shell-get-or-create-process)))
2491 (comint-preoutput-filter-functions
2492 '(python-shell-output-filter))
2493 (python-shell-output-filter-in-progress t)
2494 (inhibit-quit t))
2495 (or
2496 (with-local-quit
2497 (python-shell-send-string string process)
2498 (while python-shell-output-filter-in-progress
2499 ;; `python-shell-output-filter' takes care of setting
2500 ;; `python-shell-output-filter-in-progress' to NIL after it
2501 ;; detects end of output.
2502 (accept-process-output process))
2503 (prog1
2504 python-shell-output-filter-buffer
2505 (setq python-shell-output-filter-buffer nil)))
2506 (with-current-buffer (process-buffer process)
2507 (comint-interrupt-subjob)))))
2508
2509 (defun python-shell-internal-send-string (string)
2510 "Send STRING to the Internal Python interpreter.
2511 Returns the output. See `python-shell-send-string-no-output'."
2512 ;; XXX Remove `python-shell-internal-last-output' once CEDET is
2513 ;; updated to support this new mode.
2514 (setq python-shell-internal-last-output
2515 (python-shell-send-string-no-output
2516 ;; Makes this function compatible with the old
2517 ;; python-send-receive. (At least for CEDET).
2518 (replace-regexp-in-string "_emacs_out +" "" string)
2519 (python-shell-internal-get-or-create-process))))
2520
2521 (define-obsolete-function-alias
2522 'python-send-receive 'python-shell-internal-send-string "24.3")
2523
2524 (define-obsolete-function-alias
2525 'python-send-string 'python-shell-internal-send-string "24.3")
2526
2527 (defun python-shell-buffer-substring (start end &optional nomain)
2528 "Send buffer substring from START to END formatted for shell.
2529 This is a wrapper over `buffer-substring' that takes care of
2530 different transformations for the code sent to be evaluated in
2531 the python shell:
2532 1. When optional argument NOMAIN is non-nil everything under an
2533 \"if __name__ == '__main__'\" block will be removed.
2534 2. When a subregion of the buffer is sent, it takes care of
2535 appending extra empty lines so tracebacks are correct.
2536 3. When the region sent is a substring of the current buffer, a
2537 coding cookie is added.
2538 4. Wraps indented regions under an \"if True:\" block so the
2539 interpreter evaluates them correctly."
2540 (let* ((substring (buffer-substring-no-properties start end))
2541 (starts-at-point-min-p (save-restriction
2542 (widen)
2543 (= (point-min) start)))
2544 (encoding (python-info-encoding))
2545 (fillstr (when (not starts-at-point-min-p)
2546 (concat
2547 (format "# -*- coding: %s -*-\n" encoding)
2548 (make-string
2549 ;; Subtract 2 because of the coding cookie.
2550 (- (line-number-at-pos start) 2) ?\n))))
2551 (toplevel-block-p (save-excursion
2552 (goto-char start)
2553 (or (zerop (line-number-at-pos start))
2554 (progn
2555 (python-util-forward-comment 1)
2556 (zerop (current-indentation)))))))
2557 (with-temp-buffer
2558 (python-mode)
2559 (if fillstr (insert fillstr))
2560 (insert substring)
2561 (goto-char (point-min))
2562 (when (not toplevel-block-p)
2563 (insert "if True:")
2564 (delete-region (point) (line-end-position)))
2565 (when nomain
2566 (let* ((if-name-main-start-end
2567 (and nomain
2568 (save-excursion
2569 (when (python-nav-if-name-main)
2570 (cons (point)
2571 (progn (python-nav-forward-sexp-safe)
2572 ;; Include ending newline
2573 (forward-line 1)
2574 (point)))))))
2575 ;; Oh destructuring bind, how I miss you.
2576 (if-name-main-start (car if-name-main-start-end))
2577 (if-name-main-end (cdr if-name-main-start-end))
2578 (fillstr (make-string
2579 (- (line-number-at-pos if-name-main-end)
2580 (line-number-at-pos if-name-main-start)) ?\n)))
2581 (when if-name-main-start-end
2582 (goto-char if-name-main-start)
2583 (delete-region if-name-main-start if-name-main-end)
2584 (insert fillstr))))
2585 ;; Ensure there's only one coding cookie in the generated string.
2586 (goto-char (point-min))
2587 (when (looking-at-p (python-rx coding-cookie))
2588 (forward-line 1)
2589 (when (looking-at-p (python-rx coding-cookie))
2590 (delete-region
2591 (line-beginning-position) (line-end-position))))
2592 (buffer-substring-no-properties (point-min) (point-max)))))
2593
2594 (defun python-shell-send-region (start end &optional send-main)
2595 "Send the region delimited by START and END to inferior Python process.
2596 When optional argument SEND-MAIN is non-nil, allow execution of
2597 code inside blocks delimited by \"if __name__== '__main__':\".
2598 When called interactively SEND-MAIN defaults to nil, unless it's
2599 called with prefix argument."
2600 (interactive "r\nP")
2601 (let* ((string (python-shell-buffer-substring start end (not send-main)))
2602 (process (python-shell-get-or-create-process))
2603 (original-string (buffer-substring-no-properties start end))
2604 (_ (string-match "\\`\n*\\(.*\\)" original-string)))
2605 (message "Sent: %s..." (match-string 1 original-string))
2606 (python-shell-send-string string process)))
2607
2608 (defun python-shell-send-buffer (&optional send-main)
2609 "Send the entire buffer to inferior Python process.
2610 When optional argument SEND-MAIN is non-nil, allow execution of
2611 code inside blocks delimited by \"if __name__== '__main__':\".
2612 When called interactively SEND-MAIN defaults to nil, unless it's
2613 called with prefix argument."
2614 (interactive "P")
2615 (save-restriction
2616 (widen)
2617 (python-shell-send-region (point-min) (point-max) send-main)))
2618
2619 (defun python-shell-send-defun (arg)
2620 "Send the current defun to inferior Python process.
2621 When argument ARG is non-nil do not include decorators."
2622 (interactive "P")
2623 (save-excursion
2624 (python-shell-send-region
2625 (progn
2626 (end-of-line 1)
2627 (while (and (or (python-nav-beginning-of-defun)
2628 (beginning-of-line 1))
2629 (> (current-indentation) 0)))
2630 (when (not arg)
2631 (while (and (forward-line -1)
2632 (looking-at (python-rx decorator))))
2633 (forward-line 1))
2634 (point-marker))
2635 (progn
2636 (or (python-nav-end-of-defun)
2637 (end-of-line 1))
2638 (point-marker)))))
2639
2640 (defun python-shell-send-file (file-name &optional process temp-file-name
2641 delete)
2642 "Send FILE-NAME to inferior Python PROCESS.
2643 If TEMP-FILE-NAME is passed then that file is used for processing
2644 instead, while internally the shell will continue to use
2645 FILE-NAME. If TEMP-FILE-NAME and DELETE are non-nil, then
2646 TEMP-FILE-NAME is deleted after evaluation is performed."
2647 (interactive "fFile to send: ")
2648 (let* ((process (or process (python-shell-get-or-create-process)))
2649 (encoding (with-temp-buffer
2650 (insert-file-contents
2651 (or temp-file-name file-name))
2652 (python-info-encoding)))
2653 (file-name (expand-file-name
2654 (or (file-remote-p file-name 'localname)
2655 file-name)))
2656 (temp-file-name (when temp-file-name
2657 (expand-file-name
2658 (or (file-remote-p temp-file-name 'localname)
2659 temp-file-name)))))
2660 (python-shell-send-string
2661 (format
2662 (concat
2663 "import codecs, os;"
2664 "__pyfile = codecs.open('''%s''', encoding='''%s''');"
2665 "__code = __pyfile.read().encode('''%s''');"
2666 "__pyfile.close();"
2667 (when (and delete temp-file-name)
2668 (format "os.remove('''%s''');" temp-file-name))
2669 "exec(compile(__code, '''%s''', 'exec'));")
2670 (or temp-file-name file-name) encoding encoding file-name)
2671 process)))
2672
2673 (defun python-shell-switch-to-shell ()
2674 "Switch to inferior Python process buffer."
2675 (interactive)
2676 (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t))
2677
2678 (defun python-shell-send-setup-code ()
2679 "Send all setup code for shell.
2680 This function takes the list of setup code to send from the
2681 `python-shell-setup-codes' list."
2682 (let ((process (get-buffer-process (current-buffer))))
2683 (dolist (code python-shell-setup-codes)
2684 (when code
2685 (message "Sent %s" code)
2686 (python-shell-send-string
2687 (symbol-value code) process)))))
2688
2689 (add-hook 'inferior-python-mode-hook
2690 #'python-shell-send-setup-code)
2691
2692 \f
2693 ;;; Shell completion
2694
2695 (defcustom python-shell-completion-setup-code
2696 "try:
2697 import __builtin__
2698 except ImportError:
2699 # Python 3
2700 import builtins as __builtin__
2701 try:
2702 import readline, rlcompleter
2703 except:
2704 def __PYTHON_EL_get_completions(text):
2705 return []
2706 else:
2707 def __PYTHON_EL_get_completions(text):
2708 builtins = dir(__builtin__)
2709 completions = []
2710 try:
2711 splits = text.split()
2712 is_module = splits and splits[0] in ('from', 'import')
2713 is_ipython = ('__IPYTHON__' in builtins or
2714 '__IPYTHON__active' in builtins)
2715 if is_module:
2716 from IPython.core.completerlib import module_completion
2717 completions = module_completion(text.strip())
2718 elif is_ipython and '__IP' in builtins:
2719 completions = __IP.complete(text)
2720 elif is_ipython and 'get_ipython' in builtins:
2721 completions = get_ipython().Completer.all_completions(text)
2722 else:
2723 i = 0
2724 while True:
2725 res = readline.get_completer()(text, i)
2726 if not res:
2727 break
2728 i += 1
2729 completions.append(res)
2730 except:
2731 pass
2732 return completions"
2733 "Code used to setup completion in inferior Python processes."
2734 :type 'string
2735 :group 'python)
2736
2737 (defcustom python-shell-completion-string-code
2738 "';'.join(__PYTHON_EL_get_completions('''%s'''))\n"
2739 "Python code used to get a string of completions separated by semicolons.
2740 The string passed to the function is the current python name or
2741 the full statement in the case of imports."
2742 :type 'string
2743 :group 'python)
2744
2745 (define-obsolete-variable-alias
2746 'python-shell-completion-module-string-code
2747 'python-shell-completion-string-code
2748 "24.4"
2749 "Completion string code must also autocomplete modules.")
2750
2751 (defcustom python-shell-completion-pdb-string-code
2752 "';'.join(globals().keys() + locals().keys())"
2753 "Python code used to get completions separated by semicolons for [i]pdb."
2754 :type 'string
2755 :group 'python)
2756
2757 (defun python-shell-completion-get-completions (process line input)
2758 "Do completion at point for PROCESS.
2759 LINE is used to detect the context on how to complete given INPUT."
2760 (with-current-buffer (process-buffer process)
2761 (let* ((prompt
2762 ;; Get last prompt of the inferior process buffer (this
2763 ;; intentionally avoids using `comint-last-prompt' because
2764 ;; of incompatibilities with Emacs 24.x).
2765 (save-excursion
2766 (buffer-substring-no-properties
2767 (line-beginning-position) ;End of prompt.
2768 (re-search-backward "^"))))
2769 (completion-code
2770 ;; Check whether a prompt matches a pdb string, an import
2771 ;; statement or just the standard prompt and use the
2772 ;; correct python-shell-completion-*-code string
2773 (cond ((and (> (length python-shell-completion-pdb-string-code) 0)
2774 (string-match
2775 (concat "^" python-shell-prompt-pdb-regexp) prompt))
2776 python-shell-completion-pdb-string-code)
2777 ((string-match
2778 python-shell--prompt-calculated-input-regexp prompt)
2779 python-shell-completion-string-code)
2780 (t nil)))
2781 (input
2782 (if (string-match
2783 (python-rx line-start (* space) (or "from" "import") space)
2784 line)
2785 line
2786 input)))
2787 (and completion-code
2788 (> (length input) 0)
2789 (let ((completions
2790 (python-util-strip-string
2791 (python-shell-send-string-no-output
2792 (format completion-code input) process))))
2793 (and (> (length completions) 2)
2794 (split-string completions
2795 "^'\\|^\"\\|;\\|'$\\|\"$" t)))))))
2796
2797 (defun python-shell-completion-complete-at-point (&optional process)
2798 "Perform completion at point in inferior Python.
2799 Optional argument PROCESS forces completions to be retrieved
2800 using that one instead of current buffer's process."
2801 (setq process (or process (get-buffer-process (current-buffer))))
2802 (let* ((start
2803 (save-excursion
2804 (with-syntax-table python-dotty-syntax-table
2805 (let* ((paren-depth (car (syntax-ppss)))
2806 (syntax-string "w_")
2807 (syntax-list (string-to-syntax syntax-string)))
2808 ;; Stop scanning for the beginning of the completion
2809 ;; subject after the char before point matches a
2810 ;; delimiter
2811 (while (member
2812 (car (syntax-after (1- (point)))) syntax-list)
2813 (skip-syntax-backward syntax-string)
2814 (when (or (equal (char-before) ?\))
2815 (equal (char-before) ?\"))
2816 (forward-char -1))
2817 (while (or
2818 ;; honor initial paren depth
2819 (> (car (syntax-ppss)) paren-depth)
2820 (python-syntax-context 'string))
2821 (forward-char -1)))
2822 (point)))))
2823 (end (point)))
2824 (list start end
2825 (completion-table-dynamic
2826 (apply-partially
2827 #'python-shell-completion-get-completions
2828 process (buffer-substring-no-properties
2829 (line-beginning-position) end))))))
2830
2831 (defun python-shell-completion-complete-or-indent ()
2832 "Complete or indent depending on the context.
2833 If content before pointer is all whitespace, indent.
2834 If not try to complete."
2835 (interactive)
2836 (if (string-match "^[[:space:]]*$"
2837 (buffer-substring (comint-line-beginning-position)
2838 (point-marker)))
2839 (indent-for-tab-command)
2840 (completion-at-point)))
2841
2842 \f
2843 ;;; PDB Track integration
2844
2845 (defcustom python-pdbtrack-activate t
2846 "Non-nil makes Python shell enable pdbtracking."
2847 :type 'boolean
2848 :group 'python
2849 :safe 'booleanp)
2850
2851 (defcustom python-pdbtrack-stacktrace-info-regexp
2852 "> \\([^\"(<]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()"
2853 "Regular expression matching stacktrace information.
2854 Used to extract the current line and module being inspected."
2855 :type 'string
2856 :group 'python
2857 :safe 'stringp)
2858
2859 (defvar python-pdbtrack-tracked-buffer nil
2860 "Variable containing the value of the current tracked buffer.
2861 Never set this variable directly, use
2862 `python-pdbtrack-set-tracked-buffer' instead.")
2863
2864 (defvar python-pdbtrack-buffers-to-kill nil
2865 "List of buffers to be deleted after tracking finishes.")
2866
2867 (defun python-pdbtrack-set-tracked-buffer (file-name)
2868 "Set the buffer for FILE-NAME as the tracked buffer.
2869 Internally it uses the `python-pdbtrack-tracked-buffer' variable.
2870 Returns the tracked buffer."
2871 (let ((file-buffer (get-file-buffer
2872 (concat (file-remote-p default-directory)
2873 file-name))))
2874 (if file-buffer
2875 (setq python-pdbtrack-tracked-buffer file-buffer)
2876 (setq file-buffer (find-file-noselect file-name))
2877 (when (not (member file-buffer python-pdbtrack-buffers-to-kill))
2878 (add-to-list 'python-pdbtrack-buffers-to-kill file-buffer)))
2879 file-buffer))
2880
2881 (defun python-pdbtrack-comint-output-filter-function (output)
2882 "Move overlay arrow to current pdb line in tracked buffer.
2883 Argument OUTPUT is a string with the output from the comint process."
2884 (when (and python-pdbtrack-activate (not (string= output "")))
2885 (let* ((full-output (ansi-color-filter-apply
2886 (buffer-substring comint-last-input-end (point-max))))
2887 (line-number)
2888 (file-name
2889 (with-temp-buffer
2890 (insert full-output)
2891 ;; When the debugger encounters a pdb.set_trace()
2892 ;; command, it prints a single stack frame. Sometimes
2893 ;; it prints a bit of extra information about the
2894 ;; arguments of the present function. When ipdb
2895 ;; encounters an exception, it prints the _entire_ stack
2896 ;; trace. To handle all of these cases, we want to find
2897 ;; the _last_ stack frame printed in the most recent
2898 ;; batch of output, then jump to the corresponding
2899 ;; file/line number.
2900 (goto-char (point-max))
2901 (when (re-search-backward python-pdbtrack-stacktrace-info-regexp nil t)
2902 (setq line-number (string-to-number
2903 (match-string-no-properties 2)))
2904 (match-string-no-properties 1)))))
2905 (if (and file-name line-number)
2906 (let* ((tracked-buffer
2907 (python-pdbtrack-set-tracked-buffer file-name))
2908 (shell-buffer (current-buffer))
2909 (tracked-buffer-window (get-buffer-window tracked-buffer))
2910 (tracked-buffer-line-pos))
2911 (with-current-buffer tracked-buffer
2912 (set (make-local-variable 'overlay-arrow-string) "=>")
2913 (set (make-local-variable 'overlay-arrow-position) (make-marker))
2914 (setq tracked-buffer-line-pos (progn
2915 (goto-char (point-min))
2916 (forward-line (1- line-number))
2917 (point-marker)))
2918 (when tracked-buffer-window
2919 (set-window-point
2920 tracked-buffer-window tracked-buffer-line-pos))
2921 (set-marker overlay-arrow-position tracked-buffer-line-pos))
2922 (pop-to-buffer tracked-buffer)
2923 (switch-to-buffer-other-window shell-buffer))
2924 (when python-pdbtrack-tracked-buffer
2925 (with-current-buffer python-pdbtrack-tracked-buffer
2926 (set-marker overlay-arrow-position nil))
2927 (mapc #'(lambda (buffer)
2928 (ignore-errors (kill-buffer buffer)))
2929 python-pdbtrack-buffers-to-kill)
2930 (setq python-pdbtrack-tracked-buffer nil
2931 python-pdbtrack-buffers-to-kill nil)))))
2932 output)
2933
2934 \f
2935 ;;; Symbol completion
2936
2937 (defun python-completion-complete-at-point ()
2938 "Complete current symbol at point.
2939 For this to work as best as possible you should call
2940 `python-shell-send-buffer' from time to time so context in
2941 inferior Python process is updated properly."
2942 (let ((process (python-shell-get-process)))
2943 (if (not process)
2944 (error "Completion needs an inferior Python process running")
2945 (python-shell-completion-complete-at-point process))))
2946
2947 (add-to-list 'debug-ignored-errors
2948 "^Completion needs an inferior Python process running.")
2949
2950 \f
2951 ;;; Fill paragraph
2952
2953 (defcustom python-fill-comment-function 'python-fill-comment
2954 "Function to fill comments.
2955 This is the function used by `python-fill-paragraph' to
2956 fill comments."
2957 :type 'symbol
2958 :group 'python)
2959
2960 (defcustom python-fill-string-function 'python-fill-string
2961 "Function to fill strings.
2962 This is the function used by `python-fill-paragraph' to
2963 fill strings."
2964 :type 'symbol
2965 :group 'python)
2966
2967 (defcustom python-fill-decorator-function 'python-fill-decorator
2968 "Function to fill decorators.
2969 This is the function used by `python-fill-paragraph' to
2970 fill decorators."
2971 :type 'symbol
2972 :group 'python)
2973
2974 (defcustom python-fill-paren-function 'python-fill-paren
2975 "Function to fill parens.
2976 This is the function used by `python-fill-paragraph' to
2977 fill parens."
2978 :type 'symbol
2979 :group 'python)
2980
2981 (defcustom python-fill-docstring-style 'pep-257
2982 "Style used to fill docstrings.
2983 This affects `python-fill-string' behavior with regards to
2984 triple quotes positioning.
2985
2986 Possible values are `django', `onetwo', `pep-257', `pep-257-nn',
2987 `symmetric', and nil. A value of nil won't care about quotes
2988 position and will treat docstrings a normal string, any other
2989 value may result in one of the following docstring styles:
2990
2991 `django':
2992
2993 \"\"\"
2994 Process foo, return bar.
2995 \"\"\"
2996
2997 \"\"\"
2998 Process foo, return bar.
2999
3000 If processing fails throw ProcessingError.
3001 \"\"\"
3002
3003 `onetwo':
3004
3005 \"\"\"Process foo, return bar.\"\"\"
3006
3007 \"\"\"
3008 Process foo, return bar.
3009
3010 If processing fails throw ProcessingError.
3011
3012 \"\"\"
3013
3014 `pep-257':
3015
3016 \"\"\"Process foo, return bar.\"\"\"
3017
3018 \"\"\"Process foo, return bar.
3019
3020 If processing fails throw ProcessingError.
3021
3022 \"\"\"
3023
3024 `pep-257-nn':
3025
3026 \"\"\"Process foo, return bar.\"\"\"
3027
3028 \"\"\"Process foo, return bar.
3029
3030 If processing fails throw ProcessingError.
3031 \"\"\"
3032
3033 `symmetric':
3034
3035 \"\"\"Process foo, return bar.\"\"\"
3036
3037 \"\"\"
3038 Process foo, return bar.
3039
3040 If processing fails throw ProcessingError.
3041 \"\"\""
3042 :type '(choice
3043 (const :tag "Don't format docstrings" nil)
3044 (const :tag "Django's coding standards style." django)
3045 (const :tag "One newline and start and Two at end style." onetwo)
3046 (const :tag "PEP-257 with 2 newlines at end of string." pep-257)
3047 (const :tag "PEP-257 with 1 newline at end of string." pep-257-nn)
3048 (const :tag "Symmetric style." symmetric))
3049 :group 'python
3050 :safe (lambda (val)
3051 (memq val '(django onetwo pep-257 pep-257-nn symmetric nil))))
3052
3053 (defun python-fill-paragraph (&optional justify)
3054 "`fill-paragraph-function' handling multi-line strings and possibly comments.
3055 If any of the current line is in or at the end of a multi-line string,
3056 fill the string or the paragraph of it that point is in, preserving
3057 the string's indentation.
3058 Optional argument JUSTIFY defines if the paragraph should be justified."
3059 (interactive "P")
3060 (save-excursion
3061 (cond
3062 ;; Comments
3063 ((python-syntax-context 'comment)
3064 (funcall python-fill-comment-function justify))
3065 ;; Strings/Docstrings
3066 ((save-excursion (or (python-syntax-context 'string)
3067 (equal (string-to-syntax "|")
3068 (syntax-after (point)))))
3069 (funcall python-fill-string-function justify))
3070 ;; Decorators
3071 ((equal (char-after (save-excursion
3072 (python-nav-beginning-of-statement))) ?@)
3073 (funcall python-fill-decorator-function justify))
3074 ;; Parens
3075 ((or (python-syntax-context 'paren)
3076 (looking-at (python-rx open-paren))
3077 (save-excursion
3078 (skip-syntax-forward "^(" (line-end-position))
3079 (looking-at (python-rx open-paren))))
3080 (funcall python-fill-paren-function justify))
3081 (t t))))
3082
3083 (defun python-fill-comment (&optional justify)
3084 "Comment fill function for `python-fill-paragraph'.
3085 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3086 (fill-comment-paragraph justify))
3087
3088 (defun python-fill-string (&optional justify)
3089 "String fill function for `python-fill-paragraph'.
3090 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3091 (let* ((str-start-pos
3092 (set-marker
3093 (make-marker)
3094 (or (python-syntax-context 'string)
3095 (and (equal (string-to-syntax "|")
3096 (syntax-after (point)))
3097 (point)))))
3098 (num-quotes (python-syntax-count-quotes
3099 (char-after str-start-pos) str-start-pos))
3100 (str-end-pos
3101 (save-excursion
3102 (goto-char (+ str-start-pos num-quotes))
3103 (or (re-search-forward (rx (syntax string-delimiter)) nil t)
3104 (goto-char (point-max)))
3105 (point-marker)))
3106 (multi-line-p
3107 ;; Docstring styles may vary for oneliners and multi-liners.
3108 (> (count-matches "\n" str-start-pos str-end-pos) 0))
3109 (delimiters-style
3110 (pcase python-fill-docstring-style
3111 ;; delimiters-style is a cons cell with the form
3112 ;; (START-NEWLINES . END-NEWLINES). When any of the sexps
3113 ;; is NIL means to not add any newlines for start or end
3114 ;; of docstring. See `python-fill-docstring-style' for a
3115 ;; graphic idea of each style.
3116 (`django (cons 1 1))
3117 (`onetwo (and multi-line-p (cons 1 2)))
3118 (`pep-257 (and multi-line-p (cons nil 2)))
3119 (`pep-257-nn (and multi-line-p (cons nil 1)))
3120 (`symmetric (and multi-line-p (cons 1 1)))))
3121 (docstring-p (save-excursion
3122 ;; Consider docstrings those strings which
3123 ;; start on a line by themselves.
3124 (python-nav-beginning-of-statement)
3125 (and (= (point) str-start-pos))))
3126 (fill-paragraph-function))
3127 (save-restriction
3128 (narrow-to-region str-start-pos str-end-pos)
3129 (fill-paragraph justify))
3130 (save-excursion
3131 (when (and docstring-p python-fill-docstring-style)
3132 ;; Add the number of newlines indicated by the selected style
3133 ;; at the start of the docstring.
3134 (goto-char (+ str-start-pos num-quotes))
3135 (delete-region (point) (progn
3136 (skip-syntax-forward "> ")
3137 (point)))
3138 (and (car delimiters-style)
3139 (or (newline (car delimiters-style)) t)
3140 ;; Indent only if a newline is added.
3141 (indent-according-to-mode))
3142 ;; Add the number of newlines indicated by the selected style
3143 ;; at the end of the docstring.
3144 (goto-char (if (not (= str-end-pos (point-max)))
3145 (- str-end-pos num-quotes)
3146 str-end-pos))
3147 (delete-region (point) (progn
3148 (skip-syntax-backward "> ")
3149 (point)))
3150 (and (cdr delimiters-style)
3151 ;; Add newlines only if string ends.
3152 (not (= str-end-pos (point-max)))
3153 (or (newline (cdr delimiters-style)) t)
3154 ;; Again indent only if a newline is added.
3155 (indent-according-to-mode))))) t)
3156
3157 (defun python-fill-decorator (&optional _justify)
3158 "Decorator fill function for `python-fill-paragraph'.
3159 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3160 t)
3161
3162 (defun python-fill-paren (&optional justify)
3163 "Paren fill function for `python-fill-paragraph'.
3164 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3165 (save-restriction
3166 (narrow-to-region (progn
3167 (while (python-syntax-context 'paren)
3168 (goto-char (1- (point-marker))))
3169 (point-marker)
3170 (line-beginning-position))
3171 (progn
3172 (when (not (python-syntax-context 'paren))
3173 (end-of-line)
3174 (when (not (python-syntax-context 'paren))
3175 (skip-syntax-backward "^)")))
3176 (while (and (python-syntax-context 'paren)
3177 (not (eobp)))
3178 (goto-char (1+ (point-marker))))
3179 (point-marker)))
3180 (let ((paragraph-start "\f\\|[ \t]*$")
3181 (paragraph-separate ",")
3182 (fill-paragraph-function))
3183 (goto-char (point-min))
3184 (fill-paragraph justify))
3185 (while (not (eobp))
3186 (forward-line 1)
3187 (python-indent-line)
3188 (goto-char (line-end-position))))
3189 t)
3190
3191 \f
3192 ;;; Skeletons
3193
3194 (defcustom python-skeleton-autoinsert nil
3195 "Non-nil means template skeletons will be automagically inserted.
3196 This happens when pressing \"if<SPACE>\", for example, to prompt for
3197 the if condition."
3198 :type 'boolean
3199 :group 'python
3200 :safe 'booleanp)
3201
3202 (define-obsolete-variable-alias
3203 'python-use-skeletons 'python-skeleton-autoinsert "24.3")
3204
3205 (defvar python-skeleton-available '()
3206 "Internal list of available skeletons.")
3207
3208 (define-abbrev-table 'python-mode-skeleton-abbrev-table ()
3209 "Abbrev table for Python mode skeletons."
3210 :case-fixed t
3211 ;; Allow / inside abbrevs.
3212 :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
3213 ;; Only expand in code.
3214 :enable-function (lambda ()
3215 (and
3216 (not (python-syntax-comment-or-string-p))
3217 python-skeleton-autoinsert)))
3218
3219 (defmacro python-skeleton-define (name doc &rest skel)
3220 "Define a `python-mode' skeleton using NAME DOC and SKEL.
3221 The skeleton will be bound to python-skeleton-NAME and will
3222 be added to `python-mode-skeleton-abbrev-table'."
3223 (declare (indent 2))
3224 (let* ((name (symbol-name name))
3225 (function-name (intern (concat "python-skeleton-" name))))
3226 `(progn
3227 (define-abbrev python-mode-skeleton-abbrev-table
3228 ,name "" ',function-name :system t)
3229 (setq python-skeleton-available
3230 (cons ',function-name python-skeleton-available))
3231 (define-skeleton ,function-name
3232 ,(or doc
3233 (format "Insert %s statement." name))
3234 ,@skel))))
3235
3236 (define-abbrev-table 'python-mode-abbrev-table ()
3237 "Abbrev table for Python mode."
3238 :parents (list python-mode-skeleton-abbrev-table))
3239
3240 (defmacro python-define-auxiliary-skeleton (name doc &optional &rest skel)
3241 "Define a `python-mode' auxiliary skeleton using NAME DOC and SKEL.
3242 The skeleton will be bound to python-skeleton-NAME."
3243 (declare (indent 2))
3244 (let* ((name (symbol-name name))
3245 (function-name (intern (concat "python-skeleton--" name)))
3246 (msg (format
3247 "Add '%s' clause? " name)))
3248 (when (not skel)
3249 (setq skel
3250 `(< ,(format "%s:" name) \n \n
3251 > _ \n)))
3252 `(define-skeleton ,function-name
3253 ,(or doc
3254 (format "Auxiliary skeleton for %s statement." name))
3255 nil
3256 (unless (y-or-n-p ,msg)
3257 (signal 'quit t))
3258 ,@skel)))
3259
3260 (python-define-auxiliary-skeleton else nil)
3261
3262 (python-define-auxiliary-skeleton except nil)
3263
3264 (python-define-auxiliary-skeleton finally nil)
3265
3266 (python-skeleton-define if nil
3267 "Condition: "
3268 "if " str ":" \n
3269 _ \n
3270 ("other condition, %s: "
3271 <
3272 "elif " str ":" \n
3273 > _ \n nil)
3274 '(python-skeleton--else) | ^)
3275
3276 (python-skeleton-define while nil
3277 "Condition: "
3278 "while " str ":" \n
3279 > _ \n
3280 '(python-skeleton--else) | ^)
3281
3282 (python-skeleton-define for nil
3283 "Iteration spec: "
3284 "for " str ":" \n
3285 > _ \n
3286 '(python-skeleton--else) | ^)
3287
3288 (python-skeleton-define try nil
3289 nil
3290 "try:" \n
3291 > _ \n
3292 ("Exception, %s: "
3293 <
3294 "except " str ":" \n
3295 > _ \n nil)
3296 resume:
3297 '(python-skeleton--except)
3298 '(python-skeleton--else)
3299 '(python-skeleton--finally) | ^)
3300
3301 (python-skeleton-define def nil
3302 "Function name: "
3303 "def " str "(" ("Parameter, %s: "
3304 (unless (equal ?\( (char-before)) ", ")
3305 str) "):" \n
3306 "\"\"\"" - "\"\"\"" \n
3307 > _ \n)
3308
3309 (python-skeleton-define class nil
3310 "Class name: "
3311 "class " str "(" ("Inheritance, %s: "
3312 (unless (equal ?\( (char-before)) ", ")
3313 str)
3314 & ")" | -2
3315 ":" \n
3316 "\"\"\"" - "\"\"\"" \n
3317 > _ \n)
3318
3319 (defun python-skeleton-add-menu-items ()
3320 "Add menu items to Python->Skeletons menu."
3321 (let ((skeletons (sort python-skeleton-available 'string<)))
3322 (dolist (skeleton skeletons)
3323 (easy-menu-add-item
3324 nil '("Python" "Skeletons")
3325 `[,(format
3326 "Insert %s" (nth 2 (split-string (symbol-name skeleton) "-")))
3327 ,skeleton t]))))
3328 \f
3329 ;;; FFAP
3330
3331 (defcustom python-ffap-setup-code
3332 "def __FFAP_get_module_path(module):
3333 try:
3334 import os
3335 path = __import__(module).__file__
3336 if path[-4:] == '.pyc' and os.path.exists(path[0:-1]):
3337 path = path[:-1]
3338 return path
3339 except:
3340 return ''"
3341 "Python code to get a module path."
3342 :type 'string
3343 :group 'python)
3344
3345 (defcustom python-ffap-string-code
3346 "__FFAP_get_module_path('''%s''')\n"
3347 "Python code used to get a string with the path of a module."
3348 :type 'string
3349 :group 'python)
3350
3351 (defun python-ffap-module-path (module)
3352 "Function for `ffap-alist' to return path for MODULE."
3353 (let ((process (or
3354 (and (derived-mode-p 'inferior-python-mode)
3355 (get-buffer-process (current-buffer)))
3356 (python-shell-get-process))))
3357 (if (not process)
3358 nil
3359 (let ((module-file
3360 (python-shell-send-string-no-output
3361 (format python-ffap-string-code module) process)))
3362 (when module-file
3363 (substring-no-properties module-file 1 -1))))))
3364
3365 (defvar ffap-alist)
3366
3367 (eval-after-load "ffap"
3368 '(progn
3369 (push '(python-mode . python-ffap-module-path) ffap-alist)
3370 (push '(inferior-python-mode . python-ffap-module-path) ffap-alist)))
3371
3372 \f
3373 ;;; Code check
3374
3375 (defcustom python-check-command
3376 "pyflakes"
3377 "Command used to check a Python file."
3378 :type 'string
3379 :group 'python)
3380
3381 (defcustom python-check-buffer-name
3382 "*Python check: %s*"
3383 "Buffer name used for check commands."
3384 :type 'string
3385 :group 'python)
3386
3387 (defvar python-check-custom-command nil
3388 "Internal use.")
3389
3390 (defun python-check (command)
3391 "Check a Python file (default current buffer's file).
3392 Runs COMMAND, a shell command, as if by `compile'.
3393 See `python-check-command' for the default."
3394 (interactive
3395 (list (read-string "Check command: "
3396 (or python-check-custom-command
3397 (concat python-check-command " "
3398 (shell-quote-argument
3399 (or
3400 (let ((name (buffer-file-name)))
3401 (and name
3402 (file-name-nondirectory name)))
3403 "")))))))
3404 (setq python-check-custom-command command)
3405 (save-some-buffers (not compilation-ask-about-save) nil)
3406 (let ((process-environment (python-shell-calculate-process-environment))
3407 (exec-path (python-shell-calculate-exec-path)))
3408 (compilation-start command nil
3409 (lambda (_modename)
3410 (format python-check-buffer-name command)))))
3411
3412 \f
3413 ;;; Eldoc
3414
3415 (defcustom python-eldoc-setup-code
3416 "def __PYDOC_get_help(obj):
3417 try:
3418 import inspect
3419 if hasattr(obj, 'startswith'):
3420 obj = eval(obj, globals())
3421 doc = inspect.getdoc(obj)
3422 if not doc and callable(obj):
3423 target = None
3424 if inspect.isclass(obj) and hasattr(obj, '__init__'):
3425 target = obj.__init__
3426 objtype = 'class'
3427 else:
3428 target = obj
3429 objtype = 'def'
3430 if target:
3431 args = inspect.formatargspec(
3432 *inspect.getargspec(target)
3433 )
3434 name = obj.__name__
3435 doc = '{objtype} {name}{args}'.format(
3436 objtype=objtype, name=name, args=args
3437 )
3438 else:
3439 doc = doc.splitlines()[0]
3440 except:
3441 doc = ''
3442 try:
3443 exec('print doc')
3444 except SyntaxError:
3445 print(doc)"
3446 "Python code to setup documentation retrieval."
3447 :type 'string
3448 :group 'python)
3449
3450 (defcustom python-eldoc-string-code
3451 "__PYDOC_get_help('''%s''')\n"
3452 "Python code used to get a string with the documentation of an object."
3453 :type 'string
3454 :group 'python)
3455
3456 (defun python-eldoc--get-doc-at-point (&optional force-input force-process)
3457 "Internal implementation to get documentation at point.
3458 If not FORCE-INPUT is passed then what `python-info-current-symbol'
3459 returns will be used. If not FORCE-PROCESS is passed what
3460 `python-shell-get-process' returns is used."
3461 (let ((process (or force-process (python-shell-get-process))))
3462 (if (not process)
3463 (error "Eldoc needs an inferior Python process running")
3464 (let ((input (or force-input
3465 (python-info-current-symbol t))))
3466 (and input
3467 ;; Prevent resizing the echo area when iPython is
3468 ;; enabled. Bug#18794.
3469 (python-util-strip-string
3470 (python-shell-send-string-no-output
3471 (format python-eldoc-string-code input)
3472 process)))))))
3473
3474 (defun python-eldoc-function ()
3475 "`eldoc-documentation-function' for Python.
3476 For this to work as best as possible you should call
3477 `python-shell-send-buffer' from time to time so context in
3478 inferior Python process is updated properly."
3479 (python-eldoc--get-doc-at-point))
3480
3481 (defun python-eldoc-at-point (symbol)
3482 "Get help on SYMBOL using `help'.
3483 Interactively, prompt for symbol."
3484 (interactive
3485 (let ((symbol (python-info-current-symbol t))
3486 (enable-recursive-minibuffers t))
3487 (list (read-string (if symbol
3488 (format "Describe symbol (default %s): " symbol)
3489 "Describe symbol: ")
3490 nil nil symbol))))
3491 (message (python-eldoc--get-doc-at-point symbol)))
3492
3493 (add-to-list 'debug-ignored-errors
3494 "^Eldoc needs an inferior Python process running.")
3495
3496 \f
3497 ;;; Imenu
3498
3499 (defvar python-imenu-format-item-label-function
3500 'python-imenu-format-item-label
3501 "Imenu function used to format an item label.
3502 It must be a function with two arguments: TYPE and NAME.")
3503
3504 (defvar python-imenu-format-parent-item-label-function
3505 'python-imenu-format-parent-item-label
3506 "Imenu function used to format a parent item label.
3507 It must be a function with two arguments: TYPE and NAME.")
3508
3509 (defvar python-imenu-format-parent-item-jump-label-function
3510 'python-imenu-format-parent-item-jump-label
3511 "Imenu function used to format a parent jump item label.
3512 It must be a function with two arguments: TYPE and NAME.")
3513
3514 (defun python-imenu-format-item-label (type name)
3515 "Return Imenu label for single node using TYPE and NAME."
3516 (format "%s (%s)" name type))
3517
3518 (defun python-imenu-format-parent-item-label (type name)
3519 "Return Imenu label for parent node using TYPE and NAME."
3520 (format "%s..." (python-imenu-format-item-label type name)))
3521
3522 (defun python-imenu-format-parent-item-jump-label (type _name)
3523 "Return Imenu label for parent node jump using TYPE and NAME."
3524 (if (string= type "class")
3525 "*class definition*"
3526 "*function definition*"))
3527
3528 (defun python-imenu--put-parent (type name pos tree)
3529 "Add the parent with TYPE, NAME and POS to TREE."
3530 (let ((label
3531 (funcall python-imenu-format-item-label-function type name))
3532 (jump-label
3533 (funcall python-imenu-format-parent-item-jump-label-function type name)))
3534 (if (not tree)
3535 (cons label pos)
3536 (cons label (cons (cons jump-label pos) tree)))))
3537
3538 (defun python-imenu--build-tree (&optional min-indent prev-indent tree)
3539 "Recursively build the tree of nested definitions of a node.
3540 Arguments MIN-INDENT, PREV-INDENT and TREE are internal and should
3541 not be passed explicitly unless you know what you are doing."
3542 (setq min-indent (or min-indent 0)
3543 prev-indent (or prev-indent python-indent-offset))
3544 (let* ((pos (python-nav-backward-defun))
3545 (type)
3546 (name (when (and pos (looking-at python-nav-beginning-of-defun-regexp))
3547 (let ((split (split-string (match-string-no-properties 0))))
3548 (setq type (car split))
3549 (cadr split))))
3550 (label (when name
3551 (funcall python-imenu-format-item-label-function type name)))
3552 (indent (current-indentation))
3553 (children-indent-limit (+ python-indent-offset min-indent)))
3554 (cond ((not pos)
3555 ;; Nothing found, probably near to bobp.
3556 nil)
3557 ((<= indent min-indent)
3558 ;; The current indentation points that this is a parent
3559 ;; node, add it to the tree and stop recursing.
3560 (python-imenu--put-parent type name pos tree))
3561 (t
3562 (python-imenu--build-tree
3563 min-indent
3564 indent
3565 (if (<= indent children-indent-limit)
3566 ;; This lies within the children indent offset range,
3567 ;; so it's a normal child of its parent (i.e., not
3568 ;; a child of a child).
3569 (cons (cons label pos) tree)
3570 ;; Oh no, a child of a child?! Fear not, we
3571 ;; know how to roll. We recursively parse these by
3572 ;; swapping prev-indent and min-indent plus adding this
3573 ;; newly found item to a fresh subtree. This works, I
3574 ;; promise.
3575 (cons
3576 (python-imenu--build-tree
3577 prev-indent indent (list (cons label pos)))
3578 tree)))))))
3579
3580 (defun python-imenu-create-index ()
3581 "Return tree Imenu alist for the current Python buffer.
3582 Change `python-imenu-format-item-label-function',
3583 `python-imenu-format-parent-item-label-function',
3584 `python-imenu-format-parent-item-jump-label-function' to
3585 customize how labels are formatted."
3586 (goto-char (point-max))
3587 (let ((index)
3588 (tree))
3589 (while (setq tree (python-imenu--build-tree))
3590 (setq index (cons tree index)))
3591 index))
3592
3593 (defun python-imenu-create-flat-index (&optional alist prefix)
3594 "Return flat outline of the current Python buffer for Imenu.
3595 Optional argument ALIST is the tree to be flattened; when nil
3596 `python-imenu-build-index' is used with
3597 `python-imenu-format-parent-item-jump-label-function'
3598 `python-imenu-format-parent-item-label-function'
3599 `python-imenu-format-item-label-function' set to
3600 (lambda (type name) name)
3601 Optional argument PREFIX is used in recursive calls and should
3602 not be passed explicitly.
3603
3604 Converts this:
3605
3606 ((\"Foo\" . 103)
3607 (\"Bar\" . 138)
3608 (\"decorator\"
3609 (\"decorator\" . 173)
3610 (\"wrap\"
3611 (\"wrap\" . 353)
3612 (\"wrapped_f\" . 393))))
3613
3614 To this:
3615
3616 ((\"Foo\" . 103)
3617 (\"Bar\" . 138)
3618 (\"decorator\" . 173)
3619 (\"decorator.wrap\" . 353)
3620 (\"decorator.wrapped_f\" . 393))"
3621 ;; Inspired by imenu--flatten-index-alist removed in revno 21853.
3622 (apply
3623 'nconc
3624 (mapcar
3625 (lambda (item)
3626 (let ((name (if prefix
3627 (concat prefix "." (car item))
3628 (car item)))
3629 (pos (cdr item)))
3630 (cond ((or (numberp pos) (markerp pos))
3631 (list (cons name pos)))
3632 ((listp pos)
3633 (cons
3634 (cons name (cdar pos))
3635 (python-imenu-create-flat-index (cddr item) name))))))
3636 (or alist
3637 (let* ((fn (lambda (_type name) name))
3638 (python-imenu-format-item-label-function fn)
3639 (python-imenu-format-parent-item-label-function fn)
3640 (python-imenu-format-parent-item-jump-label-function fn))
3641 (python-imenu-create-index))))))
3642
3643 \f
3644 ;;; Misc helpers
3645
3646 (defun python-info-current-defun (&optional include-type)
3647 "Return name of surrounding function with Python compatible dotty syntax.
3648 Optional argument INCLUDE-TYPE indicates to include the type of the defun.
3649 This function can be used as the value of `add-log-current-defun-function'
3650 since it returns nil if point is not inside a defun."
3651 (save-restriction
3652 (widen)
3653 (save-excursion
3654 (end-of-line 1)
3655 (let ((names)
3656 (starting-indentation (current-indentation))
3657 (starting-pos (point))
3658 (first-run t)
3659 (last-indent)
3660 (type))
3661 (catch 'exit
3662 (while (python-nav-beginning-of-defun 1)
3663 (when (save-match-data
3664 (and
3665 (or (not last-indent)
3666 (< (current-indentation) last-indent))
3667 (or
3668 (and first-run
3669 (save-excursion
3670 ;; If this is the first run, we may add
3671 ;; the current defun at point.
3672 (setq first-run nil)
3673 (goto-char starting-pos)
3674 (python-nav-beginning-of-statement)
3675 (beginning-of-line 1)
3676 (looking-at-p
3677 python-nav-beginning-of-defun-regexp)))
3678 (< starting-pos
3679 (save-excursion
3680 (let ((min-indent
3681 (+ (current-indentation)
3682 python-indent-offset)))
3683 (if (< starting-indentation min-indent)
3684 ;; If the starting indentation is not
3685 ;; within the min defun indent make the
3686 ;; check fail.
3687 starting-pos
3688 ;; Else go to the end of defun and add
3689 ;; up the current indentation to the
3690 ;; ending position.
3691 (python-nav-end-of-defun)
3692 (+ (point)
3693 (if (>= (current-indentation) min-indent)
3694 (1+ (current-indentation))
3695 0)))))))))
3696 (save-match-data (setq last-indent (current-indentation)))
3697 (if (or (not include-type) type)
3698 (setq names (cons (match-string-no-properties 1) names))
3699 (let ((match (split-string (match-string-no-properties 0))))
3700 (setq type (car match))
3701 (setq names (cons (cadr match) names)))))
3702 ;; Stop searching ASAP.
3703 (and (= (current-indentation) 0) (throw 'exit t))))
3704 (and names
3705 (concat (and type (format "%s " type))
3706 (mapconcat 'identity names ".")))))))
3707
3708 (defun python-info-current-symbol (&optional replace-self)
3709 "Return current symbol using dotty syntax.
3710 With optional argument REPLACE-SELF convert \"self\" to current
3711 parent defun name."
3712 (let ((name
3713 (and (not (python-syntax-comment-or-string-p))
3714 (with-syntax-table python-dotty-syntax-table
3715 (let ((sym (symbol-at-point)))
3716 (and sym
3717 (substring-no-properties (symbol-name sym))))))))
3718 (when name
3719 (if (not replace-self)
3720 name
3721 (let ((current-defun (python-info-current-defun)))
3722 (if (not current-defun)
3723 name
3724 (replace-regexp-in-string
3725 (python-rx line-start word-start "self" word-end ?.)
3726 (concat
3727 (mapconcat 'identity
3728 (butlast (split-string current-defun "\\."))
3729 ".") ".")
3730 name)))))))
3731
3732 (defun python-info-statement-starts-block-p ()
3733 "Return non-nil if current statement opens a block."
3734 (save-excursion
3735 (python-nav-beginning-of-statement)
3736 (looking-at (python-rx block-start))))
3737
3738 (defun python-info-statement-ends-block-p ()
3739 "Return non-nil if point is at end of block."
3740 (let ((end-of-block-pos (save-excursion
3741 (python-nav-end-of-block)))
3742 (end-of-statement-pos (save-excursion
3743 (python-nav-end-of-statement))))
3744 (and end-of-block-pos end-of-statement-pos
3745 (= end-of-block-pos end-of-statement-pos))))
3746
3747 (defun python-info-beginning-of-statement-p ()
3748 "Return non-nil if point is at beginning of statement."
3749 (= (point) (save-excursion
3750 (python-nav-beginning-of-statement)
3751 (point))))
3752
3753 (defun python-info-end-of-statement-p ()
3754 "Return non-nil if point is at end of statement."
3755 (= (point) (save-excursion
3756 (python-nav-end-of-statement)
3757 (point))))
3758
3759 (defun python-info-beginning-of-block-p ()
3760 "Return non-nil if point is at beginning of block."
3761 (and (python-info-beginning-of-statement-p)
3762 (python-info-statement-starts-block-p)))
3763
3764 (defun python-info-end-of-block-p ()
3765 "Return non-nil if point is at end of block."
3766 (and (python-info-end-of-statement-p)
3767 (python-info-statement-ends-block-p)))
3768
3769 (define-obsolete-function-alias
3770 'python-info-closing-block
3771 'python-info-dedenter-opening-block-position "24.4")
3772
3773 (defun python-info-dedenter-opening-block-position ()
3774 "Return the point of the closest block the current line closes.
3775 Returns nil if point is not on a dedenter statement or no opening
3776 block can be detected. The latter case meaning current file is
3777 likely an invalid python file."
3778 (let ((positions (python-info-dedenter-opening-block-positions))
3779 (indentation (current-indentation))
3780 (position))
3781 (while (and (not position)
3782 positions)
3783 (save-excursion
3784 (goto-char (car positions))
3785 (if (<= (current-indentation) indentation)
3786 (setq position (car positions))
3787 (setq positions (cdr positions)))))
3788 position))
3789
3790 (defun python-info-dedenter-opening-block-positions ()
3791 "Return points of blocks the current line may close sorted by closer.
3792 Returns nil if point is not on a dedenter statement or no opening
3793 block can be detected. The latter case meaning current file is
3794 likely an invalid python file."
3795 (save-excursion
3796 (let ((dedenter-pos (python-info-dedenter-statement-p)))
3797 (when dedenter-pos
3798 (goto-char dedenter-pos)
3799 (let* ((pairs '(("elif" "elif" "if")
3800 ("else" "if" "elif" "except" "for" "while")
3801 ("except" "except" "try")
3802 ("finally" "else" "except" "try")))
3803 (dedenter (match-string-no-properties 0))
3804 (possible-opening-blocks (cdr (assoc-string dedenter pairs)))
3805 (collected-indentations)
3806 (opening-blocks))
3807 (catch 'exit
3808 (while (python-nav--syntactically
3809 (lambda ()
3810 (re-search-backward (python-rx block-start) nil t))
3811 #'<)
3812 (let ((indentation (current-indentation)))
3813 (when (and (not (memq indentation collected-indentations))
3814 (or (not collected-indentations)
3815 (< indentation (apply #'min collected-indentations))))
3816 (setq collected-indentations
3817 (cons indentation collected-indentations))
3818 (when (member (match-string-no-properties 0)
3819 possible-opening-blocks)
3820 (setq opening-blocks (cons (point) opening-blocks))))
3821 (when (zerop indentation)
3822 (throw 'exit nil)))))
3823 ;; sort by closer
3824 (nreverse opening-blocks))))))
3825
3826 (define-obsolete-function-alias
3827 'python-info-closing-block-message
3828 'python-info-dedenter-opening-block-message "24.4")
3829
3830 (defun python-info-dedenter-opening-block-message ()
3831 "Message the first line of the block the current statement closes."
3832 (let ((point (python-info-dedenter-opening-block-position)))
3833 (when point
3834 (save-restriction
3835 (widen)
3836 (message "Closes %s" (save-excursion
3837 (goto-char point)
3838 (buffer-substring
3839 (point) (line-end-position))))))))
3840
3841 (defun python-info-dedenter-statement-p ()
3842 "Return point if current statement is a dedenter.
3843 Sets `match-data' to the keyword that starts the dedenter
3844 statement."
3845 (save-excursion
3846 (python-nav-beginning-of-statement)
3847 (when (and (not (python-syntax-context-type))
3848 (looking-at (python-rx dedenter)))
3849 (point))))
3850
3851 (defun python-info-line-ends-backslash-p (&optional line-number)
3852 "Return non-nil if current line ends with backslash.
3853 With optional argument LINE-NUMBER, check that line instead."
3854 (save-excursion
3855 (save-restriction
3856 (widen)
3857 (when line-number
3858 (python-util-goto-line line-number))
3859 (while (and (not (eobp))
3860 (goto-char (line-end-position))
3861 (python-syntax-context 'paren)
3862 (not (equal (char-before (point)) ?\\)))
3863 (forward-line 1))
3864 (when (equal (char-before) ?\\)
3865 (point-marker)))))
3866
3867 (defun python-info-beginning-of-backslash (&optional line-number)
3868 "Return the point where the backslashed line start.
3869 Optional argument LINE-NUMBER forces the line number to check against."
3870 (save-excursion
3871 (save-restriction
3872 (widen)
3873 (when line-number
3874 (python-util-goto-line line-number))
3875 (when (python-info-line-ends-backslash-p)
3876 (while (save-excursion
3877 (goto-char (line-beginning-position))
3878 (python-syntax-context 'paren))
3879 (forward-line -1))
3880 (back-to-indentation)
3881 (point-marker)))))
3882
3883 (defun python-info-continuation-line-p ()
3884 "Check if current line is continuation of another.
3885 When current line is continuation of another return the point
3886 where the continued line ends."
3887 (save-excursion
3888 (save-restriction
3889 (widen)
3890 (let* ((context-type (progn
3891 (back-to-indentation)
3892 (python-syntax-context-type)))
3893 (line-start (line-number-at-pos))
3894 (context-start (when context-type
3895 (python-syntax-context context-type))))
3896 (cond ((equal context-type 'paren)
3897 ;; Lines inside a paren are always a continuation line
3898 ;; (except the first one).
3899 (python-util-forward-comment -1)
3900 (point-marker))
3901 ((member context-type '(string comment))
3902 ;; move forward an roll again
3903 (goto-char context-start)
3904 (python-util-forward-comment)
3905 (python-info-continuation-line-p))
3906 (t
3907 ;; Not within a paren, string or comment, the only way
3908 ;; we are dealing with a continuation line is that
3909 ;; previous line contains a backslash, and this can
3910 ;; only be the previous line from current
3911 (back-to-indentation)
3912 (python-util-forward-comment -1)
3913 (when (and (equal (1- line-start) (line-number-at-pos))
3914 (python-info-line-ends-backslash-p))
3915 (point-marker))))))))
3916
3917 (defun python-info-block-continuation-line-p ()
3918 "Return non-nil if current line is a continuation of a block."
3919 (save-excursion
3920 (when (python-info-continuation-line-p)
3921 (forward-line -1)
3922 (back-to-indentation)
3923 (when (looking-at (python-rx block-start))
3924 (point-marker)))))
3925
3926 (defun python-info-assignment-continuation-line-p ()
3927 "Check if current line is a continuation of an assignment.
3928 When current line is continuation of another with an assignment
3929 return the point of the first non-blank character after the
3930 operator."
3931 (save-excursion
3932 (when (python-info-continuation-line-p)
3933 (forward-line -1)
3934 (back-to-indentation)
3935 (when (and (not (looking-at (python-rx block-start)))
3936 (and (re-search-forward (python-rx not-simple-operator
3937 assignment-operator
3938 not-simple-operator)
3939 (line-end-position) t)
3940 (not (python-syntax-context-type))))
3941 (skip-syntax-forward "\s")
3942 (point-marker)))))
3943
3944 (defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss)
3945 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS."
3946 (and (not (python-syntax-context-type (or syntax-ppss (syntax-ppss))))
3947 (save-excursion
3948 (beginning-of-line 1)
3949 (looking-at python-nav-beginning-of-defun-regexp))))
3950
3951 (defun python-info-current-line-comment-p ()
3952 "Return non-nil if current line is a comment line."
3953 (char-equal
3954 (or (char-after (+ (line-beginning-position) (current-indentation))) ?_)
3955 ?#))
3956
3957 (defun python-info-current-line-empty-p ()
3958 "Return non-nil if current line is empty, ignoring whitespace."
3959 (save-excursion
3960 (beginning-of-line 1)
3961 (looking-at
3962 (python-rx line-start (* whitespace)
3963 (group (* not-newline))
3964 (* whitespace) line-end))
3965 (string-equal "" (match-string-no-properties 1))))
3966
3967 (defun python-info-encoding-from-cookie ()
3968 "Detect current buffer's encoding from its coding cookie.
3969 Returns the encoding as a symbol."
3970 (let ((first-two-lines
3971 (save-excursion
3972 (save-restriction
3973 (widen)
3974 (goto-char (point-min))
3975 (forward-line 2)
3976 (buffer-substring-no-properties
3977 (point)
3978 (point-min))))))
3979 (when (string-match (python-rx coding-cookie) first-two-lines)
3980 (intern (match-string-no-properties 1 first-two-lines)))))
3981
3982 (defun python-info-encoding ()
3983 "Return encoding for file.
3984 Try `python-info-encoding-from-cookie', if none is found then
3985 default to utf-8."
3986 ;; If no encoding is defined, then it's safe to use UTF-8: Python 2
3987 ;; uses ASCII as default while Python 3 uses UTF-8. This means that
3988 ;; in the worst case scenario python.el will make things work for
3989 ;; Python 2 files with unicode data and no encoding defined.
3990 (or (python-info-encoding-from-cookie)
3991 'utf-8))
3992
3993 \f
3994 ;;; Utility functions
3995
3996 (defun python-util-goto-line (line-number)
3997 "Move point to LINE-NUMBER."
3998 (goto-char (point-min))
3999 (forward-line (1- line-number)))
4000
4001 ;; Stolen from org-mode
4002 (defun python-util-clone-local-variables (from-buffer &optional regexp)
4003 "Clone local variables from FROM-BUFFER.
4004 Optional argument REGEXP selects variables to clone and defaults
4005 to \"^python-\"."
4006 (mapc
4007 (lambda (pair)
4008 (and (symbolp (car pair))
4009 (string-match (or regexp "^python-")
4010 (symbol-name (car pair)))
4011 (set (make-local-variable (car pair))
4012 (cdr pair))))
4013 (buffer-local-variables from-buffer)))
4014
4015 (defun python-util-forward-comment (&optional direction)
4016 "Python mode specific version of `forward-comment'.
4017 Optional argument DIRECTION defines the direction to move to."
4018 (let ((comment-start (python-syntax-context 'comment))
4019 (factor (if (< (or direction 0) 0)
4020 -99999
4021 99999)))
4022 (when comment-start
4023 (goto-char comment-start))
4024 (forward-comment factor)))
4025
4026 (defun python-util-popn (lst n)
4027 "Return LST first N elements.
4028 N should be an integer, when negative its opposite is used.
4029 When N is bigger than the length of LST, the list is
4030 returned as is."
4031 (let* ((n (min (abs n)))
4032 (len (length lst))
4033 (acc))
4034 (if (> n len)
4035 lst
4036 (while (< 0 n)
4037 (setq acc (cons (car lst) acc)
4038 lst (cdr lst)
4039 n (1- n)))
4040 (reverse acc))))
4041
4042 (defun python-util-strip-string (string)
4043 "Strip STRING whitespace and newlines from end and beginning."
4044 (replace-regexp-in-string
4045 (rx (or (: string-start (* (any whitespace ?\r ?\n)))
4046 (: (* (any whitespace ?\r ?\n)) string-end)))
4047 ""
4048 string))
4049
4050 (defun python-util-valid-regexp-p (regexp)
4051 "Return non-nil if REGEXP is valid."
4052 (ignore-errors (string-match regexp "") t))
4053
4054 \f
4055 (defun python-electric-pair-string-delimiter ()
4056 (when (and electric-pair-mode
4057 (memq last-command-event '(?\" ?\'))
4058 (let ((count 0))
4059 (while (eq (char-before (- (point) count)) last-command-event)
4060 (cl-incf count))
4061 (= count 3))
4062 (eq (char-after) last-command-event))
4063 (save-excursion (insert (make-string 2 last-command-event)))))
4064
4065 (defvar electric-indent-inhibit)
4066
4067 ;;;###autoload
4068 (define-derived-mode python-mode prog-mode "Python"
4069 "Major mode for editing Python files.
4070
4071 \\{python-mode-map}"
4072 (set (make-local-variable 'tab-width) 8)
4073 (set (make-local-variable 'indent-tabs-mode) nil)
4074
4075 (set (make-local-variable 'comment-start) "# ")
4076 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
4077
4078 (set (make-local-variable 'parse-sexp-lookup-properties) t)
4079 (set (make-local-variable 'parse-sexp-ignore-comments) t)
4080
4081 (set (make-local-variable 'forward-sexp-function)
4082 'python-nav-forward-sexp)
4083
4084 (set (make-local-variable 'font-lock-defaults)
4085 '(python-font-lock-keywords nil nil nil nil))
4086
4087 (set (make-local-variable 'syntax-propertize-function)
4088 python-syntax-propertize-function)
4089
4090 (set (make-local-variable 'indent-line-function)
4091 #'python-indent-line-function)
4092 (set (make-local-variable 'indent-region-function) #'python-indent-region)
4093 ;; Because indentation is not redundant, we cannot safely reindent code.
4094 (setq-local electric-indent-inhibit t)
4095 (setq-local electric-indent-chars (cons ?: electric-indent-chars))
4096
4097 ;; Add """ ... """ pairing to electric-pair-mode.
4098 (add-hook 'post-self-insert-hook
4099 #'python-electric-pair-string-delimiter 'append t)
4100
4101 (set (make-local-variable 'paragraph-start) "\\s-*$")
4102 (set (make-local-variable 'fill-paragraph-function)
4103 #'python-fill-paragraph)
4104
4105 (set (make-local-variable 'beginning-of-defun-function)
4106 #'python-nav-beginning-of-defun)
4107 (set (make-local-variable 'end-of-defun-function)
4108 #'python-nav-end-of-defun)
4109
4110 (add-hook 'completion-at-point-functions
4111 #'python-completion-complete-at-point nil 'local)
4112
4113 (add-hook 'post-self-insert-hook
4114 #'python-indent-post-self-insert-function 'append 'local)
4115
4116 (set (make-local-variable 'imenu-create-index-function)
4117 #'python-imenu-create-index)
4118
4119 (set (make-local-variable 'add-log-current-defun-function)
4120 #'python-info-current-defun)
4121
4122 (add-hook 'which-func-functions #'python-info-current-defun nil t)
4123
4124 (set (make-local-variable 'skeleton-further-elements)
4125 '((abbrev-mode nil)
4126 (< '(backward-delete-char-untabify (min python-indent-offset
4127 (current-column))))
4128 (^ '(- (1+ (current-indentation))))))
4129
4130 (set (make-local-variable 'eldoc-documentation-function)
4131 #'python-eldoc-function)
4132
4133 (add-to-list 'hs-special-modes-alist
4134 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
4135 ,(lambda (_arg)
4136 (python-nav-end-of-defun)) nil))
4137
4138 (set (make-local-variable 'outline-regexp)
4139 (python-rx (* space) block-start))
4140 (set (make-local-variable 'outline-heading-end-regexp) ":[^\n]*\n")
4141 (set (make-local-variable 'outline-level)
4142 #'(lambda ()
4143 "`outline-level' function for Python mode."
4144 (1+ (/ (current-indentation) python-indent-offset))))
4145
4146 (python-skeleton-add-menu-items)
4147
4148 (make-local-variable 'python-shell-internal-buffer)
4149
4150 (when python-indent-guess-indent-offset
4151 (python-indent-guess-indent-offset)))
4152
4153
4154 (provide 'python)
4155
4156 ;; Local Variables:
4157 ;; coding: utf-8
4158 ;; indent-tabs-mode: nil
4159 ;; End:
4160
4161 ;;; python.el ends here