1 ;;; python.el --- Python's flying circus support for Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 2003-2018 Free Software Foundation, Inc.
5 ;; Author: Fabián E. Gallina <fgallina@gnu.org>
6 ;; URL: https://github.com/fgallina/python.el
8 ;; Package-Requires: ((emacs "24.1") (cl-lib "1.0"))
9 ;; Maintainer: emacs-devel@gnu.org
11 ;; Keywords: languages
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published
17 ;; by the Free Software Foundation, either version 3 of the License,
18 ;; or (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
30 ;; Major mode for editing Python files with some fontification and
31 ;; indentation bits extracted from original Dave Love's python.el
32 ;; found in GNU/Emacs.
34 ;; Implements Syntax highlighting, Indentation, Movement, Shell
35 ;; interaction, Shell completion, Shell virtualenv support, Shell
36 ;; package support, Shell syntax highlighting, Pdb tracking, Symbol
37 ;; completion, Skeletons, FFAP, Code Check, Eldoc, Imenu.
39 ;; Syntax highlighting: Fontification of code is provided and supports
40 ;; python's triple quoted strings properly.
42 ;; Indentation: Automatic indentation with indentation cycling is
43 ;; provided, it allows you to navigate different available levels of
44 ;; indentation by hitting <tab> several times. Also electric-indent-mode
45 ;; is supported such that when inserting a colon the current line is
46 ;; dedented automatically if needed.
48 ;; Movement: `beginning-of-defun' and `end-of-defun' functions are
49 ;; properly implemented. There are also specialized
50 ;; `forward-sentence' and `backward-sentence' replacements called
51 ;; `python-nav-forward-block', `python-nav-backward-block'
52 ;; respectively which navigate between beginning of blocks of code.
53 ;; Extra functions `python-nav-forward-statement',
54 ;; `python-nav-backward-statement',
55 ;; `python-nav-beginning-of-statement', `python-nav-end-of-statement',
56 ;; `python-nav-beginning-of-block', `python-nav-end-of-block' and
57 ;; `python-nav-if-name-main' are included but no bound to any key. At
58 ;; last but not least the specialized `python-nav-forward-sexp' allows
59 ;; easy navigation between code blocks. If you prefer `cc-mode'-like
60 ;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
61 ;; enough, You can do that using the `python-mode-hook':
63 ;; (add-hook 'python-mode-hook
64 ;; (lambda () (setq forward-sexp-function nil)))
66 ;; Shell interaction: is provided and allows opening Python shells
67 ;; inside Emacs and executing any block of code of your current buffer
68 ;; in that inferior Python process.
70 ;; Besides that only the standard CPython (2.x and 3.x) shell and
71 ;; IPython are officially supported out of the box, the interaction
72 ;; should support any other readline based Python shells as well
73 ;; (e.g. Jython and PyPy have been reported to work). You can change
74 ;; your default interpreter and commandline arguments by setting the
75 ;; `python-shell-interpreter' and `python-shell-interpreter-args'
76 ;; variables. This example enables IPython globally:
78 ;; (setq python-shell-interpreter "ipython"
79 ;; python-shell-interpreter-args "-i")
81 ;; Using the "console" subcommand to start IPython in server-client
82 ;; mode is known to fail intermittently due a bug on IPython itself
83 ;; (see URL `https://debbugs.gnu.org/cgi/bugreport.cgi?bug=18052#27').
84 ;; There seems to be a race condition in the IPython server (A.K.A
85 ;; kernel) when code is sent while it is still initializing, sometimes
86 ;; causing the shell to get stalled. With that said, if an IPython
87 ;; kernel is already running, "console --existing" seems to work fine.
89 ;; Running IPython on Windows needs more tweaking. The way you should
90 ;; set `python-shell-interpreter' and `python-shell-interpreter-args'
91 ;; is as follows (of course you need to modify the paths according to
94 ;; (setq python-shell-interpreter "C:/Python27/python.exe"
95 ;; python-shell-interpreter-args
96 ;; "-i C:/Python27/Scripts/ipython-script.py")
98 ;; Missing or delayed output used to happen due to differences between
99 ;; Operating Systems' pipe buffering (e.g. CPython 3.3.4 in Windows 7.
100 ;; See URL `https://debbugs.gnu.org/cgi/bugreport.cgi?bug=17304'). To
101 ;; avoid this, the `python-shell-unbuffered' defaults to non-nil and
102 ;; controls whether `python-shell-calculate-process-environment'
103 ;; should set the "PYTHONUNBUFFERED" environment variable on startup:
104 ;; See URL `https://docs.python.org/3/using/cmdline.html#cmdoption-u'.
106 ;; The interaction relies upon having prompts for input (e.g. ">>> "
107 ;; and "... " in standard Python shell) and output (e.g. "Out[1]: " in
108 ;; IPython) detected properly. Failing that Emacs may hang but, in
109 ;; the case that happens, you can recover with \\[keyboard-quit]. To
110 ;; avoid this issue, a two-step prompt autodetection mechanism is
111 ;; provided: the first step is manual and consists of a collection of
112 ;; regular expressions matching common prompts for Python shells
113 ;; stored in `python-shell-prompt-input-regexps' and
114 ;; `python-shell-prompt-output-regexps', and dir-local friendly vars
115 ;; `python-shell-prompt-regexp', `python-shell-prompt-block-regexp',
116 ;; `python-shell-prompt-output-regexp' which are appended to the
117 ;; former automatically when a shell spawns; the second step is
118 ;; automatic and depends on the `python-shell-prompt-detect' helper
119 ;; function. See its docstring for details on global variables that
120 ;; modify its behavior.
122 ;; Shell completion: hitting tab will try to complete the current
123 ;; word. The two built-in mechanisms depend on Python's readline
124 ;; module: the "native" completion is tried first and is activated
125 ;; when `python-shell-completion-native-enable' is non-nil, the
126 ;; current `python-shell-interpreter' is not a member of the
127 ;; `python-shell-completion-native-disabled-interpreters' variable and
128 ;; `python-shell-completion-native-setup' succeeds; the "fallback" or
129 ;; "legacy" mechanism works by executing Python code in the background
130 ;; and enables auto-completion for shells that do not support
131 ;; receiving escape sequences (with some limitations, i.e. completion
132 ;; in blocks does not work). The code executed for the "fallback"
133 ;; completion can be found in `python-shell-completion-setup-code' and
134 ;; `python-shell-completion-string-code' variables. Their default
135 ;; values enable completion for both CPython and IPython, and probably
136 ;; any readline based shell (it's known to work with PyPy). If your
137 ;; Python installation lacks readline (like CPython for Windows),
138 ;; installing pyreadline (URL `http://ipython.org/pyreadline.html')
139 ;; should suffice. To troubleshoot why you are not getting any
140 ;; completions, you can try the following in your Python shell:
142 ;; >>> import readline, rlcompleter
144 ;; If you see an error, then you need to either install pyreadline or
145 ;; setup custom code that avoids that dependency.
147 ;; Shell virtualenv support: The shell also contains support for
148 ;; virtualenvs and other special environment modifications thanks to
149 ;; `python-shell-process-environment' and `python-shell-exec-path'.
150 ;; These two variables allows you to modify execution paths and
151 ;; environment variables to make easy for you to setup virtualenv rules
152 ;; or behavior modifications when running shells. Here is an example
153 ;; of how to make shell processes to be run using the /path/to/env/
156 ;; (setq python-shell-process-environment
158 ;; (format "PATH=%s" (mapconcat
161 ;; (cons (getenv "PATH")
162 ;; '("/path/to/env/bin/")))
164 ;; "VIRTUAL_ENV=/path/to/env/"))
165 ;; (python-shell-exec-path . ("/path/to/env/bin/"))
167 ;; Since the above is cumbersome and can be programmatically
168 ;; calculated, the variable `python-shell-virtualenv-root' is
169 ;; provided. When this variable is set with the path of the
170 ;; virtualenv to use, `process-environment' and `exec-path' get proper
171 ;; values in order to run shells inside the specified virtualenv. So
172 ;; the following will achieve the same as the previous example:
174 ;; (setq python-shell-virtualenv-root "/path/to/env/")
176 ;; Also the `python-shell-extra-pythonpaths' variable have been
177 ;; introduced as simple way of adding paths to the PYTHONPATH without
178 ;; affecting existing values.
180 ;; Shell package support: you can enable a package in the current
181 ;; shell so that relative imports work properly using the
182 ;; `python-shell-package-enable' command.
184 ;; Shell remote support: remote Python shells are started with the
185 ;; correct environment for files opened remotely through tramp, also
186 ;; respecting dir-local variables provided `enable-remote-dir-locals'
187 ;; is non-nil. The logic for this is transparently handled by the
188 ;; `python-shell-with-environment' macro.
190 ;; Shell syntax highlighting: when enabled current input in shell is
191 ;; highlighted. The variable `python-shell-font-lock-enable' controls
192 ;; activation of this feature globally when shells are started.
193 ;; Activation/deactivation can be also controlled on the fly via the
194 ;; `python-shell-font-lock-toggle' command.
196 ;; Pdb tracking: when you execute a block of code that contains some
197 ;; call to pdb (or ipdb) it will prompt the block of code and will
198 ;; follow the execution of pdb marking the current line with an arrow.
200 ;; Symbol completion: you can complete the symbol at point. It uses
201 ;; the shell completion in background so you should run
202 ;; `python-shell-send-buffer' from time to time to get better results.
204 ;; Skeletons: skeletons are provided for simple inserting of things like class,
205 ;; def, for, import, if, try, and while. These skeletons are
206 ;; integrated with abbrev. If you have `abbrev-mode' activated and
207 ;; `python-skeleton-autoinsert' is set to t, then whenever you type
208 ;; the name of any of those defined and hit SPC, they will be
209 ;; automatically expanded. As an alternative you can use the defined
210 ;; skeleton commands: `python-skeleton-<foo>'.
212 ;; FFAP: You can find the filename for a given module when using ffap
213 ;; out of the box. This feature needs an inferior python shell
216 ;; Code check: Check the current file for errors with `python-check'
217 ;; using the program defined in `python-check-command'.
219 ;; Eldoc: returns documentation for object at point by using the
220 ;; inferior python subprocess to inspect its documentation. As you
221 ;; might guessed you should run `python-shell-send-buffer' from time
222 ;; to time to get better results too.
224 ;; Imenu: There are two index building functions to be used as
225 ;; `imenu-create-index-function': `python-imenu-create-index' (the
226 ;; default one, builds the alist in form of a tree) and
227 ;; `python-imenu-create-flat-index'. See also
228 ;; `python-imenu-format-item-label-function',
229 ;; `python-imenu-format-parent-item-label-function',
230 ;; `python-imenu-format-parent-item-jump-label-function' variables for
231 ;; changing the way labels are formatted in the tree version.
233 ;; If you used python-mode.el you may miss auto-indentation when
234 ;; inserting newlines. To achieve the same behavior you have two
237 ;; 1) Enable the minor-mode `electric-indent-mode' (enabled by
238 ;; default) and use RET. If this mode is disabled use
239 ;; `newline-and-indent', bound to C-j.
241 ;; 2) Add the following hook in your .emacs:
243 ;; (add-hook 'python-mode-hook
245 ;; (define-key python-mode-map "\C-m" 'newline-and-indent)))
247 ;; I'd recommend the first one since you'll get the same behavior for
248 ;; all modes out-of-the-box.
252 ;; Add this to your .emacs:
254 ;; (add-to-list 'load-path "/folder/containing/file")
261 (require 'ansi-color
)
267 ;; Avoid compiler warnings
268 (defvar view-return-to-alist
)
269 (defvar compilation-error-regexp-alist
)
270 (defvar outline-heading-end-regexp
)
272 (autoload 'comint-mode
"comint")
273 (autoload 'help-function-arglist
"help-fns")
276 (add-to-list 'auto-mode-alist
(cons (purecopy "\\.py[iw]?\\'") 'python-mode
))
278 (add-to-list 'interpreter-mode-alist
(cons (purecopy "python[0-9.]*") 'python-mode
))
281 "Python Language's flying circus support for Emacs."
284 :link
'(emacs-commentary-link "python"))
291 (unless (fboundp 'prog-first-column
)
292 (defun prog-first-column ()
294 (unless (fboundp 'file-local-name
)
295 (defun file-local-name (file)
296 "Return the local name component of FILE.
297 It returns a file name which can be used directly as argument of
298 `process-file', `start-file-process', or `shell-command'."
299 (or (file-remote-p file
'localname
) file
))))
301 ;; In Emacs 24.3 and earlier, `define-derived-mode' does not define
302 ;; the hook variable, it only puts documentation on the symbol.
303 (defvar inferior-python-mode-hook
)
308 (defvar python-mode-map
309 (let ((map (make-sparse-keymap)))
311 (define-key map
[remap backward-sentence
] 'python-nav-backward-block
)
312 (define-key map
[remap forward-sentence
] 'python-nav-forward-block
)
313 (define-key map
[remap backward-up-list
] 'python-nav-backward-up-list
)
314 (define-key map
[remap mark-defun
] 'python-mark-defun
)
315 (define-key map
"\C-c\C-j" 'imenu
)
317 (define-key map
"\177" 'python-indent-dedent-line-backspace
)
318 (define-key map
(kbd "<backtab>") 'python-indent-dedent-line
)
319 (define-key map
"\C-c<" 'python-indent-shift-left
)
320 (define-key map
"\C-c>" 'python-indent-shift-right
)
322 (define-key map
"\C-c\C-tc" 'python-skeleton-class
)
323 (define-key map
"\C-c\C-td" 'python-skeleton-def
)
324 (define-key map
"\C-c\C-tf" 'python-skeleton-for
)
325 (define-key map
"\C-c\C-ti" 'python-skeleton-if
)
326 (define-key map
"\C-c\C-tm" 'python-skeleton-import
)
327 (define-key map
"\C-c\C-tt" 'python-skeleton-try
)
328 (define-key map
"\C-c\C-tw" 'python-skeleton-while
)
330 (define-key map
"\C-c\C-p" 'run-python
)
331 (define-key map
"\C-c\C-s" 'python-shell-send-string
)
332 (define-key map
"\C-c\C-r" 'python-shell-send-region
)
333 (define-key map
"\C-\M-x" 'python-shell-send-defun
)
334 (define-key map
"\C-c\C-c" 'python-shell-send-buffer
)
335 (define-key map
"\C-c\C-l" 'python-shell-send-file
)
336 (define-key map
"\C-c\C-z" 'python-shell-switch-to-shell
)
337 ;; Some util commands
338 (define-key map
"\C-c\C-v" 'python-check
)
339 (define-key map
"\C-c\C-f" 'python-eldoc-at-point
)
340 (define-key map
"\C-c\C-d" 'python-describe-at-point
)
342 (substitute-key-definition 'complete-symbol
'completion-at-point
344 (easy-menu-define python-menu map
"Python Mode menu"
346 :help
"Python-specific Features"
347 ["Shift region left" python-indent-shift-left
:active mark-active
348 :help
"Shift region left by a single indentation step"]
349 ["Shift region right" python-indent-shift-right
:active mark-active
350 :help
"Shift region right by a single indentation step"]
352 ["Start of def/class" beginning-of-defun
353 :help
"Go to start of outermost definition around point"]
354 ["End of def/class" end-of-defun
355 :help
"Go to end of definition around point"]
356 ["Mark def/class" mark-defun
357 :help
"Mark outermost definition around point"]
358 ["Jump to def/class" imenu
359 :help
"Jump to a class or function definition"]
363 ["Start interpreter" run-python
364 :help
"Run inferior Python process in a separate buffer"]
365 ["Switch to shell" python-shell-switch-to-shell
366 :help
"Switch to running inferior Python process"]
367 ["Eval string" python-shell-send-string
368 :help
"Eval string in inferior Python session"]
369 ["Eval buffer" python-shell-send-buffer
370 :help
"Eval buffer in inferior Python session"]
371 ["Eval region" python-shell-send-region
372 :help
"Eval region in inferior Python session"]
373 ["Eval defun" python-shell-send-defun
374 :help
"Eval defun in inferior Python session"]
375 ["Eval file" python-shell-send-file
376 :help
"Eval file in inferior Python session"]
377 ["Debugger" pdb
:help
"Run pdb under GUD"]
379 ["Check file" python-check
380 :help
"Check file for errors"]
381 ["Help on symbol" python-eldoc-at-point
382 :help
"Get help on symbol at point"]
383 ["Complete symbol" completion-at-point
384 :help
"Complete symbol before point"]))
386 "Keymap for `python-mode'.")
389 ;;; Python specialized rx
392 (defconst python-rx-constituents
393 `((block-start .
,(rx symbol-start
394 (or "def" "class" "if" "elif" "else" "try"
395 "except" "finally" "for" "while" "with"
396 ;; Python 3.5+ PEP492
397 (and "async" (+ space
)
398 (or "def" "for" "with")))
400 (dedenter .
,(rx symbol-start
401 (or "elif" "else" "except" "finally")
403 (block-ender .
,(rx symbol-start
405 "break" "continue" "pass" "raise" "return")
407 (decorator .
,(rx line-start
(* space
) ?
@ (any letter ?_
)
409 (defun .
,(rx symbol-start
411 ;; Python 3.5+ PEP492
412 (and "async" (+ space
) "def"))
414 (if-name-main .
,(rx line-start
"if" (+ space
) "__name__"
415 (+ space
) "==" (+ space
)
416 (any ?
' ?
\") "__main__" (any ?
' ?
\")
418 (symbol-name .
,(rx (any letter ?_
) (* (any word ?_
))))
419 (open-paren .
,(rx (or "{" "[" "(")))
420 (close-paren .
,(rx (or "}" "]" ")")))
421 (simple-operator .
,(rx (any ?
+ ?- ?
/ ?
& ?^ ?~ ?| ?
* ?
< ?
> ?
= ?%
)))
422 ;; FIXME: rx should support (not simple-operator).
423 (not-simple-operator .
,(rx
425 (any ?
+ ?- ?
/ ?
& ?^ ?~ ?| ?
* ?
< ?
> ?
= ?%
))))
426 ;; FIXME: Use regexp-opt.
427 (operator .
,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
428 "=" "%" "**" "//" "<<" ">>" "<=" "!="
429 "==" ">=" "is" "not")))
430 ;; FIXME: Use regexp-opt.
431 (assignment-operator .
,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**="
432 ">>=" "<<=" "&=" "^=" "|=")))
433 (string-delimiter .
,(rx (and
434 ;; Match even number of backslashes.
435 (or (not (any ?
\\ ?
\' ?
\")) point
436 ;; Quotes might be preceded by an escaped quote.
437 (and (or (not (any ?
\\)) point
) ?
\\
438 (* ?
\\ ?
\\) (any ?
\' ?
\")))
440 ;; Match single or triple quotes of any kind.
441 (group (or "\"" "\"\"\"" "'" "'''")))))
442 (coding-cookie .
,(rx line-start ?
# (* space
)
444 ;; # coding=<encoding name>
445 (: "coding" (or ?
: ?
=) (* space
) (group-n 1 (+ (or word ?-
))))
446 ;; # -*- coding: <encoding name> -*-
447 (: "-*-" (* space
) "coding:" (* space
)
448 (group-n 1 (+ (or word ?-
))) (* space
) "-*-")
449 ;; # vim: set fileencoding=<encoding name> :
450 (: "vim:" (* space
) "set" (+ space
)
451 "fileencoding" (* space
) ?
= (* space
)
452 (group-n 1 (+ (or word ?-
))) (* space
) ":")))))
453 "Additional Python specific sexps for `python-rx'")
455 (defmacro python-rx
(&rest regexps
)
456 "Python mode specialized rx macro.
457 This variant of `rx' supports common Python named REGEXPS."
458 (let ((rx-constituents (append python-rx-constituents rx-constituents
)))
459 (cond ((null regexps
)
462 (rx-to-string `(and ,@regexps
) t
))
464 (rx-to-string (car regexps
) t
))))))
467 ;;; Font-lock and syntax
470 (defun python-syntax--context-compiler-macro (form type
&optional syntax-ppss
)
473 `(let ((ppss (or ,syntax-ppss
(syntax-ppss))))
474 (and (nth 4 ppss
) (nth 8 ppss
))))
476 `(let ((ppss (or ,syntax-ppss
(syntax-ppss))))
477 (and (nth 3 ppss
) (nth 8 ppss
))))
479 `(nth 1 (or ,syntax-ppss
(syntax-ppss))))
482 (defun python-syntax-context (type &optional syntax-ppss
)
483 "Return non-nil if point is on TYPE using SYNTAX-PPSS.
484 TYPE can be `comment', `string' or `paren'. It returns the start
485 character address of the specified TYPE."
486 (declare (compiler-macro python-syntax--context-compiler-macro
))
487 (let ((ppss (or syntax-ppss
(syntax-ppss))))
489 (`comment
(and (nth 4 ppss
) (nth 8 ppss
)))
490 (`string
(and (nth 3 ppss
) (nth 8 ppss
)))
491 (`paren
(nth 1 ppss
))
494 (defun python-syntax-context-type (&optional syntax-ppss
)
495 "Return the context type using SYNTAX-PPSS.
496 The type returned can be `comment', `string' or `paren'."
497 (let ((ppss (or syntax-ppss
(syntax-ppss))))
499 ((nth 8 ppss
) (if (nth 4 ppss
) 'comment
'string
))
500 ((nth 1 ppss
) 'paren
))))
502 (defsubst python-syntax-comment-or-string-p
(&optional ppss
)
503 "Return non-nil if PPSS is inside comment or string."
504 (nth 8 (or ppss
(syntax-ppss))))
506 (defsubst python-syntax-closing-paren-p
()
507 "Return non-nil if char after point is a closing paren."
508 (eql (syntax-class (syntax-after (point)))
509 (syntax-class (string-to-syntax ")"))))
511 (define-obsolete-function-alias
512 'python-info-ppss-context
#'python-syntax-context
"24.3")
514 (define-obsolete-function-alias
515 'python-info-ppss-context-type
#'python-syntax-context-type
"24.3")
517 (define-obsolete-function-alias
518 'python-info-ppss-comment-or-string-p
519 #'python-syntax-comment-or-string-p
"24.3")
521 (defun python-font-lock-syntactic-face-function (state)
522 "Return syntactic face given STATE."
524 (if (python-info-docstring-p state
)
526 font-lock-string-face
)
527 font-lock-comment-face
))
529 (defvar python-font-lock-keywords
533 "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with"
534 "assert" "else" "if" "pass" "yield" "break" "except" "import" "class"
535 "in" "raise" "continue" "finally" "is" "return" "def" "for" "lambda"
540 ;; False, None, and True are listed as keywords on the Python 3
541 ;; documentation, but since they also qualify as constants they are
542 ;; fontified like that in order to keep font-lock consistent between
545 ;; Python 3.5+ PEP492
546 (and "async" (+ space
) (or "def" "for" "with"))
552 (,(rx symbol-start
"def" (1+ space
) (group (1+ (or word ?_
))))
553 (1 font-lock-function-name-face
))
555 (,(rx symbol-start
"class" (1+ space
) (group (1+ (or word ?_
))))
556 (1 font-lock-type-face
))
560 "Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
561 ;; copyright, license, credits, quit and exit are added by the site
562 ;; module and they are not intended to be used in programs
563 "copyright" "credits" "exit" "license" "quit")
564 symbol-end
) . font-lock-constant-face
)
566 (,(rx line-start
(* (any " \t")) (group "@" (1+ (or word ?_
))
567 (0+ "." (1+ (or word ?_
)))))
568 (1 font-lock-type-face
))
569 ;; Builtin Exceptions
573 "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
574 "BufferError" "BytesWarning" "DeprecationWarning" "EOFError"
575 "EnvironmentError" "Exception" "FloatingPointError" "FutureWarning"
576 "GeneratorExit" "IOError" "ImportError" "ImportWarning"
577 "IndentationError" "IndexError" "KeyError" "KeyboardInterrupt"
578 "LookupError" "MemoryError" "NameError" "NotImplementedError"
579 "OSError" "OverflowError" "PendingDeprecationWarning"
580 "ReferenceError" "RuntimeError" "RuntimeWarning" "StopIteration"
581 "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit" "TabError"
582 "TypeError" "UnboundLocalError" "UnicodeDecodeError"
583 "UnicodeEncodeError" "UnicodeError" "UnicodeTranslateError"
584 "UnicodeWarning" "UserWarning" "ValueError" "Warning"
589 "BlockingIOError" "BrokenPipeError" "ChildProcessError"
590 "ConnectionAbortedError" "ConnectionError" "ConnectionRefusedError"
591 "ConnectionResetError" "FileExistsError" "FileNotFoundError"
592 "InterruptedError" "IsADirectoryError" "NotADirectoryError"
593 "PermissionError" "ProcessLookupError" "RecursionError"
594 "ResourceWarning" "StopAsyncIteration" "TimeoutError"
596 "VMSError" "WindowsError"
598 symbol-end
) . font-lock-type-face
)
602 "abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
603 "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
604 "eval" "filter" "float" "format" "frozenset" "getattr" "globals"
605 "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
606 "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
607 "min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
608 "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
609 "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
612 "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
613 "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
616 "ascii" "bytearray" "bytes" "exec"
618 "__all__" "__doc__" "__name__" "__package__")
619 symbol-end
) . font-lock-builtin-face
)
621 ;; support for a = b = c = 5
623 (let ((re (python-rx (group (+ (any word ?. ?_
)))
624 (? ?\
[ (+ (not (any ?\
]))) ?\
]) (* space
)
625 assignment-operator
))
627 (while (and (setq res
(re-search-forward re limit t
))
628 (or (python-syntax-context 'paren
)
629 (equal (char-after (point)) ?
=))))
631 (1 font-lock-variable-name-face nil nil
))
632 ;; support for a, b, c = (1, 2, 3)
634 (let ((re (python-rx (group (+ (any word ?. ?_
))) (* space
)
635 (* ?
, (* space
) (+ (any word ?. ?_
)) (* space
))
636 ?
, (* space
) (+ (any word ?. ?_
)) (* space
)
637 assignment-operator
))
639 (while (and (setq res
(re-search-forward re limit t
))
640 (goto-char (match-end 1))
641 (python-syntax-context 'paren
)))
643 (1 font-lock-variable-name-face nil nil
))))
645 (defconst python-syntax-propertize-function
646 (syntax-propertize-rules
647 ((python-rx string-delimiter
)
648 (0 (ignore (python-syntax-stringify))))))
650 (define-obsolete-variable-alias 'python--prettify-symbols-alist
651 'python-prettify-symbols-alist
"26.1")
653 (defvar python-prettify-symbols-alist
657 "Value for `prettify-symbols-alist' in `python-mode'.")
659 (defsubst python-syntax-count-quotes
(quote-char &optional point limit
)
660 "Count number of quotes around point (max is 3).
661 QUOTE-CHAR is the quote char to count. Optional argument POINT is
662 the point where scan starts (defaults to current point), and LIMIT
663 is used to limit the scan."
666 (or (not limit
) (< (+ point i
) limit
))
667 (eq (char-after (+ point i
)) quote-char
))
671 (defun python-syntax-stringify ()
672 "Put `syntax-table' property correctly on single/triple quotes."
673 (let* ((num-quotes (length (match-string-no-properties 1)))
675 (backward-char num-quotes
)
677 (forward-char num-quotes
)))
678 (string-start (and (not (nth 4 ppss
)) (nth 8 ppss
)))
679 (quote-starting-pos (- (point) num-quotes
))
680 (quote-ending-pos (point))
683 (python-syntax-count-quotes
684 (char-before) string-start quote-starting-pos
))))
685 (cond ((and string-start
(= num-closing-quotes
0))
686 ;; This set of quotes doesn't match the string starting
690 ;; This set of quotes delimit the start of a string.
691 (put-text-property quote-starting-pos
(1+ quote-starting-pos
)
692 'syntax-table
(string-to-syntax "|")))
693 ((= num-quotes num-closing-quotes
)
694 ;; This set of quotes delimit the end of a string.
695 (put-text-property (1- quote-ending-pos
) quote-ending-pos
696 'syntax-table
(string-to-syntax "|")))
697 ((> num-quotes num-closing-quotes
)
698 ;; This may only happen whenever a triple quote is closing
699 ;; a single quoted string. Add string delimiter syntax to
701 (put-text-property quote-starting-pos quote-ending-pos
702 'syntax-table
(string-to-syntax "|"))))))
704 (defvar python-mode-syntax-table
705 (let ((table (make-syntax-table)))
706 ;; Give punctuation syntax to ASCII that normally has symbol
707 ;; syntax or has word syntax and isn't a letter.
708 (let ((symbol (string-to-syntax "_"))
709 (sst (standard-syntax-table)))
712 (if (equal symbol
(aref sst i
))
713 (modify-syntax-entry i
"." table
)))))
714 (modify-syntax-entry ?$
"." table
)
715 (modify-syntax-entry ?%
"." table
)
717 (modify-syntax-entry ?
# "<" table
)
718 (modify-syntax-entry ?
\n ">" table
)
719 (modify-syntax-entry ?
' "\"" table
)
720 (modify-syntax-entry ?
` "$" table
)
722 "Syntax table for Python files.")
724 (defvar python-dotty-syntax-table
725 (let ((table (make-syntax-table python-mode-syntax-table
)))
726 (modify-syntax-entry ?.
"w" table
)
727 (modify-syntax-entry ?_
"w" table
)
729 "Dotty syntax table for Python files.
730 It makes underscores and dots word constituent chars.")
735 (define-obsolete-variable-alias
736 'python-indent
'python-indent-offset
"24.3")
738 (defcustom python-indent-offset
4
739 "Default indentation offset for Python."
744 (define-obsolete-variable-alias
745 'python-guess-indent
'python-indent-guess-indent-offset
"24.3")
747 (defcustom python-indent-guess-indent-offset t
748 "Non-nil tells Python mode to guess `python-indent-offset' value."
753 (defcustom python-indent-guess-indent-offset-verbose t
754 "Non-nil means to emit a warning when indentation guessing fails."
760 (defcustom python-indent-trigger-commands
761 '(indent-for-tab-command yas-expand yas
/expand
)
762 "Commands that might trigger a `python-indent-line' call."
763 :type
'(repeat symbol
)
766 (defcustom python-indent-def-block-scale
2
767 "Multiplier applied to indentation inside multi-line def blocks."
772 (defvar python-indent-current-level
0
773 "Deprecated var available for compatibility.")
775 (defvar python-indent-levels
'(0)
776 "Deprecated var available for compatibility.")
778 (make-obsolete-variable
779 'python-indent-current-level
780 "The indentation API changed to avoid global state.
781 The function `python-indent-calculate-levels' does not use it
782 anymore. If you were defadvising it and or depended on this
783 variable for indentation customizations, refactor your code to
784 work on `python-indent-calculate-indentation' instead."
787 (make-obsolete-variable
788 'python-indent-levels
789 "The indentation API changed to avoid global state.
790 The function `python-indent-calculate-levels' does not use it
791 anymore. If you were defadvising it and or depended on this
792 variable for indentation customizations, refactor your code to
793 work on `python-indent-calculate-indentation' instead."
796 (defun python-indent-guess-indent-offset ()
797 "Guess and set `python-indent-offset' for the current buffer."
802 (goto-char (point-min))
804 (while (and (not block-end
)
806 (python-rx line-start block-start
) nil t
))
808 (not (python-syntax-context-type))
810 (goto-char (line-end-position))
811 (python-util-forward-comment -
1)
812 (if (equal (char-before) ?
:)
815 (when (python-info-block-continuation-line-p)
816 (while (and (python-info-continuation-line-p)
819 (python-util-forward-comment -
1)
820 (when (equal (char-before) ?
:)
822 (setq block-end
(point-marker))))
825 (goto-char block-end
)
826 (python-util-forward-comment)
827 (current-indentation))))
828 (if (and indentation
(not (zerop indentation
)))
829 (set (make-local-variable 'python-indent-offset
) indentation
)
830 (when python-indent-guess-indent-offset-verbose
831 (message "Can't guess python-indent-offset, using defaults: %s"
832 python-indent-offset
))))))))
834 (defun python-indent-context ()
835 "Get information about the current indentation context.
836 Context is returned in a cons with the form (STATUS . START).
838 STATUS can be one of the following:
844 - Point is after a comment line.
845 - START is the position of the \"#\" character.
847 - Point is inside string.
848 - START is the position of the first quote that starts it.
850 - No possible indentation case matches.
851 - START is always zero.
854 - Fallback case when point is inside paren.
855 - START is the first non space char position *after* the open paren.
856 :inside-paren-at-closing-nested-paren
857 - Point is on a line that contains a nested paren closer.
858 - START is the position of the open paren it closes.
859 :inside-paren-at-closing-paren
860 - Point is on a line that contains a paren closer.
861 - START is the position of the open paren.
862 :inside-paren-newline-start
863 - Point is inside a paren with items starting in their own line.
864 - START is the position of the open paren.
865 :inside-paren-newline-start-from-block
866 - Point is inside a paren with items starting in their own line
868 - START is the position of the open paren.
871 - Fallback case when point is after backslash.
872 - START is the char after the position of the backslash.
873 :after-backslash-assignment-continuation
874 - Point is after a backslashed assignment.
875 - START is the char after the position of the backslash.
876 :after-backslash-block-continuation
877 - Point is after a backslashed block continuation.
878 - START is the char after the position of the backslash.
879 :after-backslash-dotted-continuation
880 - Point is after a backslashed dotted continuation. Previous
881 line must contain a dot to align with.
882 - START is the char after the position of the backslash.
883 :after-backslash-first-line
884 - First line following a backslashed continuation.
885 - START is the char after the position of the backslash.
888 - Point is after a line containing a block ender.
889 - START is the position where the ender starts.
891 - Point is after a line starting a block.
892 - START is the position where the block starts.
894 - Point is after a simple line.
895 - START is the position where the previous line starts.
896 :at-dedenter-block-start
897 - Point is on a line starting a dedenter block.
898 - START is the position where the dedenter block starts."
899 (let ((ppss (save-excursion
903 ;; Beginning of buffer.
904 ((= (line-number-at-pos) 1)
907 ((let ((start (python-syntax-context 'string ppss
)))
909 (cons (if (python-info-docstring-p)
911 :inside-string
) start
))))
913 ((let* ((start (python-syntax-context 'paren ppss
))
920 (= (line-number-at-pos)
922 (python-util-forward-comment)
923 (line-number-at-pos))))))))
926 ;; Current line only holds the closing paren.
928 (skip-syntax-forward " ")
929 (when (and (python-syntax-closing-paren-p)
932 (not (python-syntax-context 'paren
))))
933 (cons :inside-paren-at-closing-paren start
))))
934 ;; Current line only holds a closing paren for nested.
936 (back-to-indentation)
937 (python-syntax-closing-paren-p))
938 (cons :inside-paren-at-closing-nested-paren start
))
939 ;; This line starts from an opening block in its own line.
945 (back-to-indentation)
946 (looking-at (python-rx block-start
))))
948 :inside-paren-newline-start-from-block start
))))
950 (cons :inside-paren-newline-start start
))
952 (t (cons :inside-paren
954 (goto-char (1+ start
))
955 (skip-syntax-forward "(" 1)
956 (skip-syntax-forward " ")
959 ((let ((start (when (not (python-syntax-comment-or-string-p ppss
))
960 (python-info-line-ends-backslash-p
961 (1- (line-number-at-pos))))))
964 ;; Continuation of dotted expression.
966 (back-to-indentation)
967 (when (eq (char-after) ?\.
)
968 ;; Move point back until it's not inside a paren.
972 (python-syntax-context 'paren
))))
973 (goto-char (line-end-position))
974 (while (and (search-backward
975 "." (line-beginning-position) t
)
976 (python-syntax-context-type)))
977 ;; Ensure previous statement has dot to align with.
978 (when (and (eq (char-after) ?\.
)
979 (not (python-syntax-context-type)))
980 (cons :after-backslash-dotted-continuation
(point))))))
981 ;; Continuation of block definition.
982 ((let ((block-continuation-start
983 (python-info-block-continuation-line-p)))
984 (when block-continuation-start
986 (goto-char block-continuation-start
)
988 (python-rx block-start
(* space
))
989 (line-end-position) t
)
990 (cons :after-backslash-block-continuation
(point))))))
991 ;; Continuation of assignment.
992 ((let ((assignment-continuation-start
993 (python-info-assignment-continuation-line-p)))
994 (when assignment-continuation-start
996 (goto-char assignment-continuation-start
)
997 (cons :after-backslash-assignment-continuation
(point))))))
998 ;; First line after backslash continuation start.
1001 (when (or (= (line-number-at-pos) 1)
1002 (not (python-info-beginning-of-backslash
1003 (1- (line-number-at-pos)))))
1004 (cons :after-backslash-first-line start
))))
1006 (t (cons :after-backslash start
))))))
1007 ;; After beginning of block.
1008 ((let ((start (save-excursion
1009 (back-to-indentation)
1010 (python-util-forward-comment -
1)
1011 (when (equal (char-before) ?
:)
1012 (python-nav-beginning-of-block)))))
1014 (cons :after-block-start start
))))
1015 ;; At dedenter statement.
1016 ((let ((start (python-info-dedenter-statement-p)))
1018 (cons :at-dedenter-block-start start
))))
1019 ;; After normal line, comment or ender (default case).
1021 (back-to-indentation)
1022 (skip-chars-backward " \t\n")
1025 (python-nav-beginning-of-statement)
1027 (cond ((python-info-current-line-comment-p)
1030 (goto-char (line-end-position))
1031 (python-util-forward-comment -
1)
1032 (python-nav-beginning-of-statement)
1033 (looking-at (python-rx block-ender
)))
1038 (defun python-indent--calculate-indentation ()
1039 "Internal implementation of `python-indent-calculate-indentation'.
1040 May return an integer for the maximum possible indentation at
1041 current context or a list of integers. The latter case is only
1042 happening for :at-dedenter-block-start context since the
1043 possibilities can be narrowed to specific indentation points."
1045 (pcase (python-indent-context)
1046 (`(:no-indent .
,_
) (prog-first-column)) ; usually 0
1051 :inside-paren-at-closing-paren
1052 :inside-paren-at-closing-nested-paren
) .
,start
)
1053 ;; Copy previous indentation.
1055 (current-indentation))
1056 (`(:inside-docstring .
,start
)
1057 (let* ((line-indentation (current-indentation))
1060 (current-indentation))))
1061 (max line-indentation base-indent
)))
1062 (`(,(or :after-block-start
1063 :after-backslash-first-line
1064 :after-backslash-assignment-continuation
1065 :inside-paren-newline-start
) .
,start
)
1066 ;; Add one indentation level.
1068 (+ (current-indentation) python-indent-offset
))
1069 (`(,(or :inside-paren
1070 :after-backslash-block-continuation
1071 :after-backslash-dotted-continuation
) .
,start
)
1072 ;; Use the column given by the context.
1075 (`(:after-block-end .
,start
)
1076 ;; Subtract one indentation level.
1078 (- (current-indentation) python-indent-offset
))
1079 (`(:at-dedenter-block-start .
,_
)
1080 ;; List all possible indentation levels from opening blocks.
1081 (let ((opening-block-start-points
1082 (python-info-dedenter-opening-block-positions)))
1083 (if (not opening-block-start-points
)
1084 (prog-first-column) ; if not found default to first column
1085 (mapcar (lambda (pos)
1088 (current-indentation)))
1089 opening-block-start-points
))))
1090 (`(,(or :inside-paren-newline-start-from-block
) .
,start
)
1092 (+ (current-indentation)
1093 (* python-indent-offset python-indent-def-block-scale
))))))
1095 (defun python-indent--calculate-levels (indentation)
1096 "Calculate levels list given INDENTATION.
1097 Argument INDENTATION can either be an integer or a list of
1098 integers. Levels are returned in ascending order, and in the
1099 case INDENTATION is a list, this order is enforced."
1100 (if (listp indentation
)
1101 (sort (copy-sequence indentation
) #'<)
1102 (nconc (number-sequence (prog-first-column) (1- indentation
)
1103 python-indent-offset
)
1104 (list indentation
))))
1106 (defun python-indent--previous-level (levels indentation
)
1107 "Return previous level from LEVELS relative to INDENTATION."
1108 (let* ((levels (sort (copy-sequence levels
) #'>))
1109 (default (car levels
)))
1111 (dolist (level levels
)
1112 (when (funcall #'< level indentation
)
1113 (throw 'return level
)))
1116 (defun python-indent-calculate-indentation (&optional previous
)
1117 "Calculate indentation.
1118 Get indentation of PREVIOUS level when argument is non-nil.
1119 Return the max level of the cycle when indentation reaches the
1121 (let* ((indentation (python-indent--calculate-indentation))
1122 (levels (python-indent--calculate-levels indentation
)))
1124 (python-indent--previous-level levels
(current-indentation))
1126 (apply #'max levels
)
1127 (prog-first-column)))))
1129 (defun python-indent-line (&optional previous
)
1130 "Internal implementation of `python-indent-line-function'.
1131 Use the PREVIOUS level when argument is non-nil, otherwise indent
1132 to the maximum available level. When indentation is the minimum
1133 possible and PREVIOUS is non-nil, cycle back to the maximum
1135 (let ((follow-indentation-p
1136 ;; Check if point is within indentation.
1137 (and (<= (line-beginning-position) (point))
1138 (>= (+ (line-beginning-position)
1139 (current-indentation))
1143 (python-indent-calculate-indentation previous
))
1144 (python-info-dedenter-opening-block-message))
1145 (when follow-indentation-p
1146 (back-to-indentation))))
1148 (defun python-indent-calculate-levels ()
1149 "Return possible indentation levels."
1150 (python-indent--calculate-levels
1151 (python-indent--calculate-indentation)))
1153 (defun python-indent-line-function ()
1154 "`indent-line-function' for Python mode.
1155 When the variable `last-command' is equal to one of the symbols
1156 inside `python-indent-trigger-commands' it cycles possible
1157 indentation levels from right to left."
1159 (and (memq this-command python-indent-trigger-commands
)
1160 (eq last-command this-command
))))
1162 (defun python-indent-dedent-line ()
1163 "De-indent current line."
1165 (when (and (not (bolp))
1166 (not (python-syntax-comment-or-string-p))
1167 (= (current-indentation) (current-column)))
1168 (python-indent-line t
)
1171 (defun python-indent-dedent-line-backspace (arg)
1172 "De-indent current line.
1173 Argument ARG is passed to `backward-delete-char-untabify' when
1174 point is not in between the indentation."
1176 (unless (python-indent-dedent-line)
1177 (backward-delete-char-untabify arg
)))
1179 (put 'python-indent-dedent-line-backspace
'delete-selection
'supersede
)
1181 (defun python-indent-region (start end
)
1182 "Indent a Python region automagically.
1184 Called from a program, START and END specify the region to indent."
1185 (let ((deactivate-mark nil
))
1188 (setq end
(point-marker))
1190 (or (bolp) (forward-line 1))
1191 (while (< (point) end
)
1192 (or (and (bolp) (eolp))
1194 ;; Skip if previous line is empty or a comment.
1196 (let ((line-is-comment-p
1197 (python-info-current-line-comment-p)))
1200 (or (and (python-info-current-line-comment-p)
1201 ;; Unless this line is a comment too.
1202 (not line-is-comment-p
))
1203 (python-info-current-line-empty-p)))))
1204 ;; Don't mess with strings, unless it's the
1205 ;; enclosing set of quotes or a docstring.
1206 (or (not (python-syntax-context 'string
))
1210 (current-indentation)
1211 (python-syntax-count-quotes (char-after) (point))))
1212 (string-to-syntax "|"))
1213 (python-info-docstring-p))
1214 ;; Skip if current line is a block start, a
1215 ;; dedenter or block ender.
1217 (back-to-indentation)
1220 (or block-start dedenter block-ender
))))))
1221 (python-indent-line)))
1223 (move-marker end nil
))))
1225 (defun python-indent-shift-left (start end
&optional count
)
1226 "Shift lines contained in region START END by COUNT columns to the left.
1227 COUNT defaults to `python-indent-offset'. If region isn't
1228 active, the current line is shifted. The shifted region includes
1229 the lines in which START and END lie. An error is signaled if
1230 any lines in the region are indented less than COUNT columns."
1233 (list (region-beginning) (region-end) current-prefix-arg
)
1234 (list (line-beginning-position) (line-end-position) current-prefix-arg
)))
1236 (setq count
(prefix-numeric-value count
))
1237 (setq count python-indent-offset
))
1239 (let ((deactivate-mark nil
))
1242 (while (< (point) end
)
1243 (if (and (< (current-indentation) count
)
1244 (not (looking-at "[ \t]*$")))
1245 (user-error "Can't shift all lines enough"))
1247 (indent-rigidly start end
(- count
))))))
1249 (defun python-indent-shift-right (start end
&optional count
)
1250 "Shift lines contained in region START END by COUNT columns to the right.
1251 COUNT defaults to `python-indent-offset'. If region isn't
1252 active, the current line is shifted. The shifted region includes
1253 the lines in which START and END lie."
1256 (list (region-beginning) (region-end) current-prefix-arg
)
1257 (list (line-beginning-position) (line-end-position) current-prefix-arg
)))
1258 (let ((deactivate-mark nil
))
1259 (setq count
(if count
(prefix-numeric-value count
)
1260 python-indent-offset
))
1261 (indent-rigidly start end count
)))
1263 (defun python-indent-post-self-insert-function ()
1264 "Adjust indentation after insertion of some characters.
1265 This function is intended to be added to `post-self-insert-hook.'
1266 If a line renders a paren alone, after adding a char before it,
1267 the line will be re-indented automatically if needed."
1268 (when (and electric-indent-mode
1269 (eq (char-before) last-command-event
)
1270 (not (python-syntax-context 'string
))
1273 (not (python-syntax-context 'string
(syntax-ppss)))))
1275 ;; Electric indent inside parens
1278 (let ((paren-start (python-syntax-context 'paren
)))
1279 ;; Check that point is inside parens.
1282 ;; Filter the case where input is happening in the same
1283 ;; line where the open paren is.
1284 (= (line-number-at-pos)
1285 (line-number-at-pos paren-start
)))))
1286 ;; When content has been added before the closing paren or a
1287 ;; comma has been inserted, it's ok to do the trick.
1289 (memq (char-after) '(?\
) ?\
] ?\
}))
1290 (eq (char-before) ?
,)))
1292 (goto-char (line-beginning-position))
1293 (let ((indentation (python-indent-calculate-indentation)))
1294 (when (and (numberp indentation
) (< (current-indentation) indentation
))
1295 (indent-line-to indentation
)))))
1297 ((and (eq ?
: last-command-event
)
1298 (memq ?
: electric-indent-chars
)
1299 (not current-prefix-arg
)
1300 ;; Trigger electric colon only at end of line
1302 ;; Avoid re-indenting on extra colon
1303 (not (equal ?
: (char-before (1- (point)))))
1304 (not (python-syntax-comment-or-string-p)))
1305 ;; Just re-indent dedenters
1306 (let ((dedenter-pos (python-info-dedenter-statement-p))
1307 (current-pos (point)))
1310 (goto-char dedenter-pos
)
1311 (python-indent-line)
1312 (unless (= (line-number-at-pos dedenter-pos
)
1313 (line-number-at-pos current-pos
))
1314 ;; Reindent region if this is a multiline statement
1315 (python-indent-region dedenter-pos current-pos
)))))))))
1320 (defun python-mark-defun (&optional allow-extend
)
1321 "Put mark at end of this defun, point at beginning.
1322 The defun marked is the one that contains point or follows point.
1324 Interactively (or with ALLOW-EXTEND non-nil), if this command is
1325 repeated or (in Transient Mark mode) if the mark is active, it
1326 marks the next defun after the ones already marked."
1328 (when (python-info-looking-at-beginning-of-defun)
1330 (mark-defun allow-extend))
1335 (defvar python-nav-beginning-of-defun-regexp
1336 (python-rx line-start
(* space
) defun
(+ space
) (group symbol-name
))
1337 "Regexp matching class or function definition.
1338 The name of the defun should be grouped so it can be retrieved
1339 via `match-string'.")
1341 (defun python-nav--beginning-of-defun (&optional arg
)
1342 "Internal implementation of `python-nav-beginning-of-defun'.
1343 With positive ARG search backwards, else search forwards."
1344 (when (or (null arg
) (= arg
0)) (setq arg
1))
1345 (let* ((re-search-fn (if (> arg
0)
1346 #'re-search-backward
1347 #'re-search-forward
))
1348 (line-beg-pos (line-beginning-position))
1349 (line-content-start (+ line-beg-pos
(current-indentation)))
1350 (pos (point-marker))
1355 (not (python-info-looking-at-beginning-of-defun))
1356 (python-nav-backward-block)))
1357 (or (and (python-info-looking-at-beginning-of-defun)
1358 (+ (current-indentation) python-indent-offset
))
1362 (when (and (< arg
0)
1363 (python-info-looking-at-beginning-of-defun))
1365 (while (and (funcall re-search-fn
1366 python-nav-beginning-of-defun-regexp nil t
)
1367 (or (python-syntax-context-type)
1368 ;; Handle nested defuns when moving
1369 ;; backwards by checking indentation.
1371 (not (= (current-indentation) 0))
1372 (>= (current-indentation) beg-indentation
)))))
1373 (and (python-info-looking-at-beginning-of-defun)
1374 (or (not (= (line-number-at-pos pos
)
1375 (line-number-at-pos)))
1376 (and (>= (point) line-beg-pos
)
1377 (<= (point) line-content-start
)
1378 (> pos line-content-start
)))))))
1380 (or (beginning-of-line 1) t
)
1381 (and (goto-char pos
) nil
))))
1383 (defun python-nav-beginning-of-defun (&optional arg
)
1384 "Move point to `beginning-of-defun'.
1385 With positive ARG search backwards else search forward.
1386 ARG nil or 0 defaults to 1. When searching backwards,
1387 nested defuns are handled with care depending on current
1388 point position. Return non-nil if point is moved to
1389 `beginning-of-defun'."
1390 (when (or (null arg
) (= arg
0)) (setq arg
1))
1392 (while (and (not (= arg
0))
1393 (let ((keep-searching-p
1394 (python-nav--beginning-of-defun arg)))
1395 (when (and keep-searching-p
(null found
))
1398 (setq arg
(if (> arg
0) (1- arg
) (1+ arg
))))
1401 (defun python-nav-end-of-defun ()
1402 "Move point to the end of def or class.
1403 Returns nil if point is not in a def or class."
1405 (let ((beg-defun-indent)
1407 (when (or (python-info-looking-at-beginning-of-defun)
1408 (python-nav-beginning-of-defun 1)
1409 (python-nav-beginning-of-defun -1))
1410 (setq beg-defun-indent
(current-indentation))
1412 (python-nav-end-of-statement)
1413 (python-util-forward-comment 1)
1414 (and (> (current-indentation) beg-defun-indent
)
1416 (python-util-forward-comment -
1)
1418 ;; Ensure point moves forward.
1419 (and (> beg-pos
(point)) (goto-char beg-pos
)))))
1421 (defun python-nav--syntactically (fn poscompfn
&optional contextfn
)
1422 "Move point using FN avoiding places with specific context.
1423 FN must take no arguments. POSCOMPFN is a two arguments function
1424 used to compare current and previous point after it is moved
1425 using FN, this is normally a less-than or greater-than
1426 comparison. Optional argument CONTEXTFN defaults to
1427 `python-syntax-context-type' and is used for checking current
1428 point context, it must return a non-nil value if this point must
1430 (let ((contextfn (or contextfn
'python-syntax-context-type
))
1431 (start-pos (point-marker))
1436 (and (funcall fn
) (point-marker)))
1437 (context (funcall contextfn
)))
1438 (cond ((and (not context
) newpos
1439 (or (and (not prev-pos
) newpos
)
1440 (and prev-pos newpos
1441 (funcall poscompfn newpos prev-pos
))))
1442 (throw 'found
(point-marker)))
1443 ((and newpos context
)
1444 (setq prev-pos
(point)))
1445 (t (when (not newpos
) (goto-char start-pos
))
1446 (throw 'found nil
))))))))
1448 (defun python-nav--forward-defun (arg)
1449 "Internal implementation of python-nav-{backward,forward}-defun.
1450 Uses ARG to define which function to call, and how many times
1453 (while (and (> arg
0)
1455 (python-nav--syntactically
1458 python-nav-beginning-of-defun-regexp nil t
))
1460 (setq arg
(1- arg
)))
1461 (while (and (< arg
0)
1463 (python-nav--syntactically
1466 python-nav-beginning-of-defun-regexp nil t
))
1468 (setq arg
(1+ arg
)))
1471 (defun python-nav-backward-defun (&optional arg
)
1472 "Navigate to closer defun backward ARG times.
1473 Unlikely `python-nav-beginning-of-defun' this doesn't care about
1474 nested definitions."
1476 (python-nav--forward-defun (- (or arg
1))))
1478 (defun python-nav-forward-defun (&optional arg
)
1479 "Navigate to closer defun forward ARG times.
1480 Unlikely `python-nav-beginning-of-defun' this doesn't care about
1481 nested definitions."
1483 (python-nav--forward-defun (or arg
1)))
1485 (defun python-nav-beginning-of-statement ()
1486 "Move to start of current statement."
1489 (let* ((ppss (syntax-ppss))
1492 (python-syntax-context 'paren ppss
)
1493 (python-syntax-context 'string ppss
))))
1496 (goto-char context-point
)
1497 (python-nav-beginning-of-statement))
1500 (python-info-line-ends-backslash-p))
1502 (python-nav-beginning-of-statement))))
1503 (back-to-indentation)
1506 (defun python-nav-end-of-statement (&optional noend
)
1507 "Move to end of current statement.
1508 Optional argument NOEND is internal and makes the logic to not
1509 jump to the end of line when moving forward searching for the end
1512 (let (string-start bs-pos
(last-string-end 0))
1513 (while (and (or noend
(goto-char (line-end-position)))
1515 (cond ((setq string-start
(python-syntax-context 'string
))
1516 ;; The assertion can only fail if syntax table
1517 ;; text properties and the `syntax-ppss' cache
1518 ;; are somehow out of whack. This has been
1519 ;; observed when using `syntax-ppss' during
1521 (cl-assert (>= string-start last-string-end
)
1524 Overlapping strings detected (start=%d, last-end=%d)")
1525 (goto-char string-start
)
1526 (if (python-syntax-context 'paren
)
1527 ;; Ended up inside a paren, roll again.
1528 (python-nav-end-of-statement t
)
1529 ;; This is not inside a paren, move to the
1530 ;; end of this string.
1531 (goto-char (+ (point)
1532 (python-syntax-count-quotes
1533 (char-after (point)) (point))))
1534 (setq last-string-end
1535 (or (re-search-forward
1536 (rx (syntax string-delimiter
)) nil t
)
1537 (goto-char (point-max))))))
1538 ((python-syntax-context 'paren
)
1539 ;; The statement won't end before we've escaped
1540 ;; at least one level of parenthesis.
1542 (goto-char (scan-lists (point) 1 -
1))
1543 (scan-error (goto-char (nth 3 err
)))))
1544 ((setq bs-pos
(python-info-line-ends-backslash-p))
1546 (forward-line 1))))))
1549 (defun python-nav-backward-statement (&optional arg
)
1550 "Move backward to previous statement.
1551 With ARG, repeat. See `python-nav-forward-statement'."
1553 (or arg
(setq arg
1))
1554 (python-nav-forward-statement (- arg
)))
1556 (defun python-nav-forward-statement (&optional arg
)
1557 "Move forward to next statement.
1558 With ARG, repeat. With negative argument, move ARG times
1559 backward to previous statement."
1561 (or arg
(setq arg
1))
1563 (python-nav-end-of-statement)
1564 (python-util-forward-comment)
1565 (python-nav-beginning-of-statement)
1566 (setq arg
(1- arg
)))
1568 (python-nav-beginning-of-statement)
1569 (python-util-forward-comment -
1)
1570 (python-nav-beginning-of-statement)
1571 (setq arg
(1+ arg
))))
1573 (defun python-nav-beginning-of-block ()
1574 "Move to start of current block."
1576 (let ((starting-pos (point)))
1578 (python-nav-beginning-of-statement)
1579 (looking-at (python-rx block-start
)))
1581 ;; Go to first line beginning a statement
1582 (while (and (not (bobp))
1583 (or (and (python-nav-beginning-of-statement) nil
)
1584 (python-info-current-line-comment-p)
1585 (python-info-current-line-empty-p)))
1587 (let ((block-matching-indent
1588 (- (current-indentation) python-indent-offset
)))
1590 (and (python-nav-backward-block)
1591 (> (current-indentation) block-matching-indent
)))
1592 (if (and (looking-at (python-rx block-start
))
1593 (= (current-indentation) block-matching-indent
))
1595 (and (goto-char starting-pos
) nil
))))))
1597 (defun python-nav-end-of-block ()
1598 "Move to end of current block."
1600 (when (python-nav-beginning-of-block)
1601 (let ((block-indentation (current-indentation)))
1602 (python-nav-end-of-statement)
1603 (while (and (forward-line 1)
1605 (or (and (> (current-indentation) block-indentation
)
1606 (or (python-nav-end-of-statement) t
))
1607 (python-info-current-line-comment-p)
1608 (python-info-current-line-empty-p))))
1609 (python-util-forward-comment -
1)
1612 (defun python-nav-backward-block (&optional arg
)
1613 "Move backward to previous block of code.
1614 With ARG, repeat. See `python-nav-forward-block'."
1616 (or arg
(setq arg
1))
1617 (python-nav-forward-block (- arg
)))
1619 (defun python-nav-forward-block (&optional arg
)
1620 "Move forward to next block of code.
1621 With ARG, repeat. With negative argument, move ARG times
1622 backward to previous block."
1624 (or arg
(setq arg
1))
1625 (let ((block-start-regexp
1626 (python-rx line-start
(* whitespace
) block-start
))
1627 (starting-pos (point)))
1629 (python-nav-end-of-statement)
1631 (re-search-forward block-start-regexp nil t
)
1632 (python-syntax-context-type)))
1633 (setq arg
(1- arg
)))
1635 (python-nav-beginning-of-statement)
1637 (re-search-backward block-start-regexp nil t
)
1638 (python-syntax-context-type)))
1639 (setq arg
(1+ arg
)))
1640 (python-nav-beginning-of-statement)
1641 (if (not (looking-at (python-rx block-start
)))
1642 (and (goto-char starting-pos
) nil
)
1643 (and (not (= (point) starting-pos
)) (point-marker)))))
1645 (defun python-nav--lisp-forward-sexp (&optional arg
)
1646 "Standard version `forward-sexp'.
1647 It ignores completely the value of `forward-sexp-function' by
1648 setting it to nil before calling `forward-sexp'. With positive
1649 ARG move forward only one sexp, else move backwards."
1650 (let ((forward-sexp-function)
1651 (arg (if (or (not arg
) (> arg
0)) 1 -
1)))
1652 (forward-sexp arg
)))
1654 (defun python-nav--lisp-forward-sexp-safe (&optional arg
)
1655 "Safe version of standard `forward-sexp'.
1656 When at end of sexp (i.e. looking at an opening/closing paren)
1657 skips it instead of throwing an error. With positive ARG move
1658 forward only one sexp, else move backwards."
1659 (let* ((arg (if (or (not arg
) (> arg
0)) 1 -
1))
1661 (if (> arg
0) (python-rx close-paren
) (python-rx open-paren
)))
1663 (if (> arg
0) #'re-search-forward
#'re-search-backward
)))
1665 (python-nav--lisp-forward-sexp arg
)
1667 (while (and (funcall search-fn paren-regexp nil t
)
1668 (python-syntax-context 'paren
)))))))
1670 (defun python-nav--forward-sexp (&optional dir safe skip-parens-p
)
1671 "Move to forward sexp.
1672 With positive optional argument DIR direction move forward, else
1673 backwards. When optional argument SAFE is non-nil do not throw
1674 errors when at end of sexp, skip it instead. With optional
1675 argument SKIP-PARENS-P force sexp motion to ignore parenthesized
1676 expressions when looking at them in either direction."
1677 (setq dir
(or dir
1))
1679 (let* ((forward-p (if (> dir
0)
1680 (and (setq dir
1) t
)
1681 (and (setq dir -
1) nil
)))
1682 (context-type (python-syntax-context-type)))
1684 ((memq context-type
'(string comment
))
1685 ;; Inside of a string, get out of it.
1686 (let ((forward-sexp-function))
1687 (forward-sexp dir
)))
1688 ((and (not skip-parens-p
)
1689 (or (eq context-type
'paren
)
1691 (eq (syntax-class (syntax-after (point)))
1692 (car (string-to-syntax "(")))
1693 (eq (syntax-class (syntax-after (1- (point))))
1694 (car (string-to-syntax ")"))))))
1695 ;; Inside a paren or looking at it, lisp knows what to do.
1697 (python-nav--lisp-forward-sexp-safe dir
)
1698 (python-nav--lisp-forward-sexp dir
)))
1700 ;; This part handles the lispy feel of
1701 ;; `python-nav-forward-sexp'. Knowing everything about the
1702 ;; current context and the context of the next sexp tries to
1703 ;; follow the lisp sexp motion commands in a symmetric manner.
1706 ((python-info-beginning-of-block-p) 'block-start
)
1707 ((python-info-end-of-block-p) 'block-end
)
1708 ((python-info-beginning-of-statement-p) 'statement-start
)
1709 ((python-info-end-of-statement-p) 'statement-end
)))
1713 (python-nav--lisp-forward-sexp-safe dir
)
1714 (python-nav--lisp-forward-sexp dir
))
1718 (goto-char next-sexp-pos
)
1720 ((python-info-beginning-of-block-p) 'block-start
)
1721 ((python-info-end-of-block-p) 'block-end
)
1722 ((python-info-beginning-of-statement-p) 'statement-start
)
1723 ((python-info-end-of-statement-p) 'statement-end
)
1724 ((python-info-statement-starts-block-p) 'starts-block
)
1725 ((python-info-statement-ends-block-p) 'ends-block
)))))
1727 (cond ((and (not (eobp))
1728 (python-info-current-line-empty-p))
1729 (python-util-forward-comment dir
)
1730 (python-nav--forward-sexp dir safe skip-parens-p
))
1731 ((eq context
'block-start
)
1732 (python-nav-end-of-block))
1733 ((eq context
'statement-start
)
1734 (python-nav-end-of-statement))
1735 ((and (memq context
'(statement-end block-end
))
1736 (eq next-sexp-context
'ends-block
))
1737 (goto-char next-sexp-pos
)
1738 (python-nav-end-of-block))
1739 ((and (memq context
'(statement-end block-end
))
1740 (eq next-sexp-context
'starts-block
))
1741 (goto-char next-sexp-pos
)
1742 (python-nav-end-of-block))
1743 ((memq context
'(statement-end block-end
))
1744 (goto-char next-sexp-pos
)
1745 (python-nav-end-of-statement))
1746 (t (goto-char next-sexp-pos
)))
1747 (cond ((and (not (bobp))
1748 (python-info-current-line-empty-p))
1749 (python-util-forward-comment dir
)
1750 (python-nav--forward-sexp dir safe skip-parens-p
))
1751 ((eq context
'block-end
)
1752 (python-nav-beginning-of-block))
1753 ((eq context
'statement-end
)
1754 (python-nav-beginning-of-statement))
1755 ((and (memq context
'(statement-start block-start
))
1756 (eq next-sexp-context
'starts-block
))
1757 (goto-char next-sexp-pos
)
1758 (python-nav-beginning-of-block))
1759 ((and (memq context
'(statement-start block-start
))
1760 (eq next-sexp-context
'ends-block
))
1761 (goto-char next-sexp-pos
)
1762 (python-nav-beginning-of-block))
1763 ((memq context
'(statement-start block-start
))
1764 (goto-char next-sexp-pos
)
1765 (python-nav-beginning-of-statement))
1766 (t (goto-char next-sexp-pos
))))))))))
1768 (defun python-nav-forward-sexp (&optional arg safe skip-parens-p
)
1769 "Move forward across expressions.
1770 With ARG, do it that many times. Negative arg -N means move
1771 backward N times. When optional argument SAFE is non-nil do not
1772 throw errors when at end of sexp, skip it instead. With optional
1773 argument SKIP-PARENS-P force sexp motion to ignore parenthesized
1774 expressions when looking at them in either direction (forced to t
1775 in interactive calls)."
1777 (or arg
(setq arg
1))
1778 ;; Do not follow parens on interactive calls. This hack to detect
1779 ;; if the function was called interactively copes with the way
1780 ;; `forward-sexp' works by calling `forward-sexp-function', losing
1781 ;; interactive detection by checking `current-prefix-arg'. The
1782 ;; reason to make this distinction is that lisp functions like
1783 ;; `blink-matching-open' get confused causing issues like the one in
1784 ;; Bug#16191. With this approach the user gets a symmetric behavior
1785 ;; when working interactively while called functions expecting
1786 ;; paren-based sexp motion work just fine.
1790 (memq real-this-command
1792 #'forward-sexp
#'backward-sexp
1793 #'python-nav-forward-sexp
#'python-nav-backward-sexp
1794 #'python-nav-forward-sexp-safe
#'python-nav-backward-sexp
))))
1796 (python-nav--forward-sexp 1 safe skip-parens-p
)
1797 (setq arg
(1- arg
)))
1799 (python-nav--forward-sexp -
1 safe skip-parens-p
)
1800 (setq arg
(1+ arg
))))
1802 (defun python-nav-backward-sexp (&optional arg safe skip-parens-p
)
1803 "Move backward across expressions.
1804 With ARG, do it that many times. Negative arg -N means move
1805 forward N times. When optional argument SAFE is non-nil do not
1806 throw errors when at end of sexp, skip it instead. With optional
1807 argument SKIP-PARENS-P force sexp motion to ignore parenthesized
1808 expressions when looking at them in either direction (forced to t
1809 in interactive calls)."
1811 (or arg
(setq arg
1))
1812 (python-nav-forward-sexp (- arg
) safe skip-parens-p
))
1814 (defun python-nav-forward-sexp-safe (&optional arg skip-parens-p
)
1815 "Move forward safely across expressions.
1816 With ARG, do it that many times. Negative arg -N means move
1817 backward N times. With optional argument SKIP-PARENS-P force
1818 sexp motion to ignore parenthesized expressions when looking at
1819 them in either direction (forced to t in interactive calls)."
1821 (python-nav-forward-sexp arg t skip-parens-p
))
1823 (defun python-nav-backward-sexp-safe (&optional arg skip-parens-p
)
1824 "Move backward safely across expressions.
1825 With ARG, do it that many times. Negative arg -N means move
1826 forward N times. With optional argument SKIP-PARENS-P force sexp
1827 motion to ignore parenthesized expressions when looking at them in
1828 either direction (forced to t in interactive calls)."
1830 (python-nav-backward-sexp arg t skip-parens-p
))
1832 (defun python-nav--up-list (&optional dir
)
1833 "Internal implementation of `python-nav-up-list'.
1834 DIR is always 1 or -1 and comes sanitized from
1835 `python-nav-up-list' calls."
1836 (let ((context (python-syntax-context-type))
1837 (forward-p (> dir
0)))
1839 ((memq context
'(string comment
)))
1840 ((eq context
'paren
)
1841 (let ((forward-sexp-function))
1843 ((and forward-p
(python-info-end-of-block-p))
1844 (let ((parent-end-pos
1846 (let ((indentation (and
1847 (python-nav-beginning-of-block)
1848 (current-indentation))))
1849 (while (and indentation
1851 (>= (current-indentation) indentation
)
1852 (python-nav-backward-block)))
1853 (python-nav-end-of-block)))))
1854 (and (> (or parent-end-pos
(point)) (point))
1855 (goto-char parent-end-pos
))))
1856 (forward-p (python-nav-end-of-block))
1857 ((and (not forward-p
)
1858 (> (current-indentation) 0)
1859 (python-info-beginning-of-block-p))
1860 (let ((prev-block-pos
1862 (let ((indentation (current-indentation)))
1863 (while (and (python-nav-backward-block)
1864 (>= (current-indentation) indentation
))))
1866 (and (> (point) prev-block-pos
)
1867 (goto-char prev-block-pos
))))
1868 ((not forward-p
) (python-nav-beginning-of-block)))))
1870 (defun python-nav-up-list (&optional arg
)
1871 "Move forward out of one level of parentheses (or blocks).
1872 With ARG, do this that many times.
1873 A negative argument means move backward but still to a less deep spot.
1874 This command assumes point is not in a string or comment."
1876 (or arg
(setq arg
1))
1878 (python-nav--up-list 1)
1879 (setq arg
(1- arg
)))
1881 (python-nav--up-list -
1)
1882 (setq arg
(1+ arg
))))
1884 (defun python-nav-backward-up-list (&optional arg
)
1885 "Move backward out of one level of parentheses (or blocks).
1886 With ARG, do this that many times.
1887 A negative argument means move forward but still to a less deep spot.
1888 This command assumes point is not in a string or comment."
1890 (or arg
(setq arg
1))
1891 (python-nav-up-list (- arg
)))
1893 (defun python-nav-if-name-main ()
1894 "Move point at the beginning the __main__ block.
1895 When \"if __name__ == \\='__main__\\=':\" is found returns its
1896 position, else returns nil."
1898 (let ((point (point))
1899 (found (catch 'found
1900 (goto-char (point-min))
1901 (while (re-search-forward
1902 (python-rx line-start
1904 "__name__" (+ space
)
1906 (group-n 1 (or ?
\" ?
\'))
1907 "__main__" (backref 1) (* space
) ":")
1909 (when (not (python-syntax-context-type))
1911 (throw 'found t
))))))
1914 (ignore (goto-char point
)))))
1917 ;;; Shell integration
1919 (defcustom python-shell-buffer-name
"Python"
1920 "Default buffer name for Python interpreter."
1925 (defcustom python-shell-interpreter
"python"
1926 "Default Python interpreter for shell."
1930 (defcustom python-shell-internal-buffer-name
"Python Internal"
1931 "Default buffer name for the Internal Python interpreter."
1936 (defcustom python-shell-interpreter-args
"-i"
1937 "Default arguments for the Python interpreter."
1941 (defcustom python-shell-interpreter-interactive-arg
"-i"
1942 "Interpreter argument to force it to run interactively."
1946 (defcustom python-shell-prompt-detect-enabled t
1947 "Non-nil enables autodetection of interpreter prompts."
1952 (defcustom python-shell-prompt-detect-failure-warning t
1953 "Non-nil enables warnings when detection of prompts fail."
1958 (defcustom python-shell-prompt-input-regexps
1959 '(">>> " "\\.\\.\\. " ; Python
1960 "In \\[[0-9]+\\]: " ; IPython
1961 " \\.\\.\\.: " ; IPython
1962 ;; Using ipdb outside IPython may fail to cleanup and leave static
1963 ;; IPython prompts activated, this adds some safeguard for that.
1964 "In : " "\\.\\.\\.: ")
1965 "List of regular expressions matching input prompts."
1966 :type
'(repeat string
)
1969 (defcustom python-shell-prompt-output-regexps
1971 "Out\\[[0-9]+\\]: " ; IPython
1972 "Out :") ; ipdb safeguard
1973 "List of regular expressions matching output prompts."
1974 :type
'(repeat string
)
1977 (defcustom python-shell-prompt-regexp
">>> "
1978 "Regular expression matching top level input prompt of Python shell.
1979 It should not contain a caret (^) at the beginning."
1982 (defcustom python-shell-prompt-block-regexp
"\\.\\.\\. "
1983 "Regular expression matching block input prompt of Python shell.
1984 It should not contain a caret (^) at the beginning."
1987 (defcustom python-shell-prompt-output-regexp
""
1988 "Regular expression matching output prompt of Python shell.
1989 It should not contain a caret (^) at the beginning."
1992 (defcustom python-shell-prompt-pdb-regexp
"[(<]*[Ii]?[Pp]db[>)]+ "
1993 "Regular expression matching pdb input prompt of Python shell.
1994 It should not contain a caret (^) at the beginning."
1997 (define-obsolete-variable-alias
1998 'python-shell-enable-font-lock
'python-shell-font-lock-enable
"25.1")
2000 (defcustom python-shell-font-lock-enable t
2001 "Should syntax highlighting be enabled in the Python shell buffer?
2002 Restart the Python shell after changing this variable for it to take effect."
2007 (defcustom python-shell-unbuffered t
2008 "Should shell output be unbuffered?.
2009 When non-nil, this may prevent delayed and missing output in the
2010 Python shell. See commentary for details."
2015 (defcustom python-shell-process-environment nil
2016 "List of overridden environment variables for subprocesses to inherit.
2017 Each element should be a string of the form ENVVARNAME=VALUE.
2018 When this variable is non-nil, values are exported into the
2019 process environment before starting it. Any variables already
2020 present in the current environment are superseded by variables
2022 :type
'(repeat string
)
2025 (defcustom python-shell-extra-pythonpaths nil
2026 "List of extra pythonpaths for Python shell.
2027 When this variable is non-nil, values added at the beginning of
2028 the PYTHONPATH before starting processes. Any values present
2029 here that already exists in PYTHONPATH are moved to the beginning
2030 of the list so that they are prioritized when looking for
2032 :type
'(repeat string
)
2035 (defcustom python-shell-exec-path nil
2036 "List of paths for searching executables.
2037 When this variable is non-nil, values added at the beginning of
2038 the PATH before starting processes. Any values present here that
2039 already exists in PATH are moved to the beginning of the list so
2040 that they are prioritized when looking for executables."
2041 :type
'(repeat string
)
2044 (defcustom python-shell-remote-exec-path nil
2045 "List of paths to be ensured remotely for searching executables.
2046 When this variable is non-nil, values are exported into remote
2047 hosts PATH before starting processes. Values defined in
2048 `python-shell-exec-path' will take precedence to paths defined
2049 here. Normally you wont use this variable directly unless you
2050 plan to ensure a particular set of paths to all Python shell
2051 executed through tramp connections."
2053 :type
'(repeat string
)
2056 (define-obsolete-variable-alias
2057 'python-shell-virtualenv-path
'python-shell-virtualenv-root
"25.1")
2059 (defcustom python-shell-virtualenv-root nil
2060 "Path to virtualenv root.
2061 This variable, when set to a string, makes the environment to be
2062 modified such that shells are started within the specified
2064 :type
'(choice (const nil
) string
)
2067 (defcustom python-shell-setup-codes nil
2068 "List of code run by `python-shell-send-setup-codes'."
2069 :type
'(repeat symbol
)
2072 (defcustom python-shell-compilation-regexp-alist
2073 `((,(rx line-start
(1+ (any " \t")) "File \""
2074 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
2075 "\", line " (group (1+ digit
)))
2077 (,(rx " in file " (group (1+ not-newline
)) " on line "
2080 (,(rx line-start
"> " (group (1+ (not (any "(\"<"))))
2081 "(" (group (1+ digit
)) ")" (1+ (not (any "("))) "()")
2083 "`compilation-error-regexp-alist' for inferior Python."
2084 :type
'(alist string
)
2087 (defmacro python-shell--add-to-path-with-priority
(pathvar paths
)
2088 "Modify PATHVAR and ensure PATHS are added only once at beginning."
2089 `(dolist (path (reverse ,paths
))
2090 (cl-delete path
,pathvar
:test
#'string
=)
2091 (cl-pushnew path
,pathvar
:test
#'string
=)))
2093 (defun python-shell-calculate-pythonpath ()
2094 "Calculate the PYTHONPATH using `python-shell-extra-pythonpaths'."
2097 (or (getenv "PYTHONPATH") "") path-separator
'omit
)))
2098 (python-shell--add-to-path-with-priority
2099 pythonpath python-shell-extra-pythonpaths
)
2100 (mapconcat 'identity pythonpath path-separator
)))
2102 (defun python-shell-calculate-process-environment ()
2103 "Calculate `process-environment' or `tramp-remote-process-environment'.
2104 Prepends `python-shell-process-environment', sets extra
2105 pythonpaths from `python-shell-extra-pythonpaths' and sets a few
2106 virtualenv related vars. If `default-directory' points to a
2107 remote host, the returned value is intended for
2108 `tramp-remote-process-environment'."
2109 (let* ((remote-p (file-remote-p default-directory
))
2110 (process-environment (if remote-p
2111 tramp-remote-process-environment
2112 process-environment
))
2113 (virtualenv (when python-shell-virtualenv-root
2114 (directory-file-name python-shell-virtualenv-root
))))
2115 (dolist (env python-shell-process-environment
)
2116 (pcase-let ((`(,key
,value
) (split-string env
"=")))
2117 (setenv key value
)))
2118 (when python-shell-unbuffered
2119 (setenv "PYTHONUNBUFFERED" "1"))
2120 (when python-shell-extra-pythonpaths
2121 (setenv "PYTHONPATH" (python-shell-calculate-pythonpath)))
2122 (if (not virtualenv
)
2124 (setenv "PYTHONHOME" nil
)
2125 (setenv "VIRTUAL_ENV" virtualenv
))
2126 process-environment
))
2128 (defun python-shell-calculate-exec-path ()
2129 "Calculate `exec-path'.
2130 Prepends `python-shell-exec-path' and adds the binary directory
2131 for virtualenv if `python-shell-virtualenv-root' is set - this
2132 will use the python interpreter from inside the virtualenv when
2133 starting the shell. If `default-directory' points to a remote host,
2134 the returned value appends `python-shell-remote-exec-path' instead
2136 (let ((new-path (copy-sequence
2137 (if (file-remote-p default-directory
)
2138 python-shell-remote-exec-path
2141 ;; Windows and POSIX systems use different venv directory structures
2142 (virtualenv-bin-dir (if (eq system-type
'windows-nt
) "Scripts" "bin")))
2143 (python-shell--add-to-path-with-priority
2144 new-path python-shell-exec-path
)
2145 (if (not python-shell-virtualenv-root
)
2147 (python-shell--add-to-path-with-priority
2149 (list (expand-file-name virtualenv-bin-dir python-shell-virtualenv-root
)))
2152 (defun python-shell-tramp-refresh-remote-path (vec paths
)
2153 "Update VEC's remote-path giving PATHS priority."
2154 (let ((remote-path (tramp-get-connection-property vec
"remote-path" nil
)))
2156 (python-shell--add-to-path-with-priority remote-path paths
)
2157 (tramp-set-connection-property vec
"remote-path" remote-path
)
2158 (tramp-set-remote-path vec
))))
2160 (defun python-shell-tramp-refresh-process-environment (vec env
)
2161 "Update VEC's process environment with ENV."
2162 ;; Stolen from `tramp-open-connection-setup-interactive-shell'.
2163 (let ((env (append (when (fboundp 'tramp-get-remote-locale
)
2164 ;; Emacs<24.4 compat.
2165 (list (tramp-get-remote-locale vec
)))
2166 (copy-sequence env
)))
2167 (tramp-end-of-heredoc
2168 (if (boundp 'tramp-end-of-heredoc
)
2169 tramp-end-of-heredoc
2170 (md5 tramp-end-of-output
)))
2173 (setq item
(split-string (car env
) "=" 'omit
))
2174 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
2175 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
2176 (push (format "%s %s" (car item
) (cdr item
)) vars
)
2177 (push (car item
) unset
))
2178 (setq env
(cdr env
)))
2182 (format "while read var val; do export $var=$val; done <<'%s'\n%s\n%s"
2183 tramp-end-of-heredoc
2184 (mapconcat 'identity vars
"\n")
2185 tramp-end-of-heredoc
)
2189 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
))))
2191 (defmacro python-shell-with-environment
(&rest body
)
2192 "Modify shell environment during execution of BODY.
2193 Temporarily sets `process-environment' and `exec-path' during
2194 execution of body. If `default-directory' points to a remote
2195 machine then modifies `tramp-remote-process-environment' and
2196 `python-shell-remote-exec-path' instead."
2197 (declare (indent 0) (debug (body)))
2198 (let ((vec (make-symbol "vec")))
2201 (when (file-remote-p default-directory
)
2203 (tramp-dissect-file-name default-directory
'noexpand
))))
2204 (process-environment
2207 (python-shell-calculate-process-environment)))
2211 (python-shell-calculate-exec-path)))
2212 (tramp-remote-process-environment
2214 (python-shell-calculate-process-environment)
2215 tramp-remote-process-environment
)))
2216 (when (tramp-get-connection-process ,vec
)
2217 ;; For already existing connections, the new exec path must
2218 ;; be re-set, otherwise it won't take effect. One example
2219 ;; of such case is when remote dir-locals are read and
2220 ;; *then* subprocesses are triggered within the same
2222 (python-shell-tramp-refresh-remote-path
2223 ,vec
(python-shell-calculate-exec-path))
2224 ;; The `tramp-remote-process-environment' variable is only
2225 ;; effective when the started process is an interactive
2226 ;; shell, otherwise (like in the case of processes started
2227 ;; with `process-file') the environment is not changed.
2228 ;; This makes environment modifications effective
2230 (python-shell-tramp-refresh-process-environment
2231 ,vec tramp-remote-process-environment
))
2232 ,(macroexp-progn body
)))))
2234 (defvar python-shell--prompt-calculated-input-regexp nil
2235 "Calculated input prompt regexp for inferior python shell.
2236 Do not set this variable directly, instead use
2237 `python-shell-prompt-set-calculated-regexps'.")
2239 (defvar python-shell--block-prompt nil
2240 "Input block prompt for inferior python shell.
2241 Do not set this variable directly, instead use
2242 `python-shell-prompt-set-calculated-regexps'.")
2244 (defvar python-shell--prompt-calculated-output-regexp nil
2245 "Calculated output prompt regexp for inferior python shell.
2246 Do not set this variable directly, instead use
2247 `python-shell-set-prompt-regexp'.")
2249 (defun python-shell-prompt-detect ()
2250 "Detect prompts for the current `python-shell-interpreter'.
2251 When prompts can be retrieved successfully from the
2252 `python-shell-interpreter' run with
2253 `python-shell-interpreter-interactive-arg', returns a list of
2254 three elements, where the first two are input prompts and the
2255 last one is an output prompt. When no prompts can be detected
2256 and `python-shell-prompt-detect-failure-warning' is non-nil,
2257 shows a warning with instructions to avoid hangs and returns nil.
2258 When `python-shell-prompt-detect-enabled' is nil avoids any
2259 detection and just returns nil."
2260 (when python-shell-prompt-detect-enabled
2261 (python-shell-with-environment
2262 (let* ((code (concat
2264 "ps = [getattr(sys, 'ps%s' % i, '') for i in range(1,4)]\n"
2265 ;; JSON is built manually for compatibility
2266 "ps_json = '\\n[\"%s\", \"%s\", \"%s\"]\\n' % tuple(ps)\n"
2269 (interpreter python-shell-interpreter
)
2270 (interpreter-arg python-shell-interpreter-interactive-arg
)
2273 ;; TODO: improve error handling by using
2274 ;; `condition-case' and displaying the error message to
2275 ;; the user in the no-prompts warning.
2278 ;; Python 2.x on Windows does not handle
2279 ;; carriage returns in unbuffered mode.
2280 (let ((inhibit-eol-conversion (getenv "PYTHONUNBUFFERED")))
2281 (python-shell--save-temp-file code
))))
2282 ;; Use `process-file' as it is remote-host friendly.
2290 (delete-file code-file
)))
2294 (dolist (line (split-string output
"\n" t
))
2296 ;; Check if current line is a valid JSON array
2297 (and (string= (substring line
0 2) "[\"")
2299 ;; Return prompts as a list, not vector
2300 (append (json-read-from-string line
) nil
)))))
2301 ;; The list must contain 3 strings, where the first
2302 ;; is the input prompt, the second is the block
2303 ;; prompt and the last one is the output prompt. The
2304 ;; input prompt is the only one that can't be empty.
2305 (when (and (= (length res
) 3)
2306 (cl-every #'stringp res
)
2307 (not (string= (car res
) "")))
2308 (throw 'prompts res
))))
2310 (when (and (not prompts
)
2311 python-shell-prompt-detect-failure-warning
)
2313 '(python python-shell-prompt-regexp
)
2316 "Python shell prompts cannot be detected.\n"
2317 "If your emacs session hangs when starting python shells\n"
2318 "recover with `keyboard-quit' and then try fixing the\n"
2319 "interactive flag for your interpreter by adjusting the\n"
2320 "`python-shell-interpreter-interactive-arg' or add regexps\n"
2321 "matching shell prompts in the directory-local friendly vars:\n"
2322 " + `python-shell-prompt-regexp'\n"
2323 " + `python-shell-prompt-block-regexp'\n"
2324 " + `python-shell-prompt-output-regexp'\n"
2325 "Or alternatively in:\n"
2326 " + `python-shell-prompt-input-regexps'\n"
2327 " + `python-shell-prompt-output-regexps'")))
2330 (defun python-shell-prompt-validate-regexps ()
2331 "Validate all user provided regexps for prompts.
2332 Signals `user-error' if any of these vars contain invalid
2333 regexps: `python-shell-prompt-regexp',
2334 `python-shell-prompt-block-regexp',
2335 `python-shell-prompt-pdb-regexp',
2336 `python-shell-prompt-output-regexp',
2337 `python-shell-prompt-input-regexps',
2338 `python-shell-prompt-output-regexps'."
2339 (dolist (symbol (list 'python-shell-prompt-input-regexps
2340 'python-shell-prompt-output-regexps
2341 'python-shell-prompt-regexp
2342 'python-shell-prompt-block-regexp
2343 'python-shell-prompt-pdb-regexp
2344 'python-shell-prompt-output-regexp
))
2345 (dolist (regexp (let ((regexps (symbol-value symbol
)))
2349 (when (not (python-util-valid-regexp-p regexp
))
2350 (user-error "Invalid regexp %s in `%s'"
2353 (defun python-shell-prompt-set-calculated-regexps ()
2354 "Detect and set input and output prompt regexps.
2355 Build and set the values for `python-shell-input-prompt-regexp'
2356 and `python-shell-output-prompt-regexp' using the values from
2357 `python-shell-prompt-regexp', `python-shell-prompt-block-regexp',
2358 `python-shell-prompt-pdb-regexp',
2359 `python-shell-prompt-output-regexp',
2360 `python-shell-prompt-input-regexps',
2361 `python-shell-prompt-output-regexps' and detected prompts from
2362 `python-shell-prompt-detect'."
2363 (when (not (and python-shell--prompt-calculated-input-regexp
2364 python-shell--prompt-calculated-output-regexp
))
2365 (let* ((detected-prompts (python-shell-prompt-detect))
2367 (output-prompts nil
)
2371 (mapconcat #'identity
2374 (let ((length-a (length a
))
2375 (length-b (length b
)))
2376 (if (= length-a length-b
)
2378 (> (length a
) (length b
))))))
2381 ;; Validate ALL regexps
2382 (python-shell-prompt-validate-regexps)
2383 ;; Collect all user defined input prompts
2384 (dolist (prompt (append python-shell-prompt-input-regexps
2385 (list python-shell-prompt-regexp
2386 python-shell-prompt-block-regexp
2387 python-shell-prompt-pdb-regexp
)))
2388 (cl-pushnew prompt input-prompts
:test
#'string
=))
2389 ;; Collect all user defined output prompts
2390 (dolist (prompt (cons python-shell-prompt-output-regexp
2391 python-shell-prompt-output-regexps
))
2392 (cl-pushnew prompt output-prompts
:test
#'string
=))
2393 ;; Collect detected prompts if any
2394 (when detected-prompts
2395 (dolist (prompt (butlast detected-prompts
))
2396 (setq prompt
(regexp-quote prompt
))
2397 (cl-pushnew prompt input-prompts
:test
#'string
=))
2398 (setq python-shell--block-prompt
(nth 1 detected-prompts
))
2399 (cl-pushnew (regexp-quote
2400 (car (last detected-prompts
)))
2401 output-prompts
:test
#'string
=))
2402 ;; Set input and output prompt regexps from collected prompts
2403 (setq python-shell--prompt-calculated-input-regexp
2404 (funcall build-regexp input-prompts
)
2405 python-shell--prompt-calculated-output-regexp
2406 (funcall build-regexp output-prompts
)))))
2408 (defun python-shell-get-process-name (dedicated)
2409 "Calculate the appropriate process name for inferior Python process.
2410 If DEDICATED is t returns a string with the form
2411 `python-shell-buffer-name'[`buffer-name'] else returns the value
2412 of `python-shell-buffer-name'."
2414 (format "%s[%s]" python-shell-buffer-name
(buffer-name))
2415 python-shell-buffer-name
))
2417 (defun python-shell-internal-get-process-name ()
2418 "Calculate the appropriate process name for Internal Python process.
2419 The name is calculated from `python-shell-global-buffer-name' and
2421 (format "%s[%s]" python-shell-internal-buffer-name
(buffer-name)))
2423 (defun python-shell-calculate-command ()
2424 "Calculate the string used to execute the inferior Python process."
2426 ;; `python-shell-make-comint' expects to be able to
2427 ;; `split-string-and-unquote' the result of this function.
2428 (combine-and-quote-strings (list python-shell-interpreter
))
2429 python-shell-interpreter-args
))
2431 (define-obsolete-function-alias
2432 'python-shell-parse-command
2433 #'python-shell-calculate-command
"25.1")
2435 (defvar python-shell--package-depth
10)
2437 (defun python-shell-package-enable (directory package
)
2438 "Add DIRECTORY parent to $PYTHONPATH and enable PACKAGE."
2440 (let* ((dir (expand-file-name
2441 (read-directory-name
2443 (file-name-directory
2444 (or (buffer-file-name) default-directory
)))))
2445 (name (completing-read
2447 (python-util-list-packages
2448 dir python-shell--package-depth
))))
2450 (python-shell-send-string
2453 "import os.path;import sys;"
2454 "sys.path.append(os.path.dirname(os.path.dirname('''%s''')));"
2455 "__package__ = '''%s''';"
2457 directory package package
)
2458 (python-shell-get-process)))
2460 (defun python-shell-accept-process-output (process &optional timeout regexp
)
2461 "Accept PROCESS output with TIMEOUT until REGEXP is found.
2462 Optional argument TIMEOUT is the timeout argument to
2463 `accept-process-output' calls. Optional argument REGEXP
2464 overrides the regexp to match the end of output, defaults to
2465 `comint-prompt-regexp'. Returns non-nil when output was
2468 This utility is useful in situations where the output may be
2469 received in chunks, since `accept-process-output' gives no
2470 guarantees they will be grabbed in a single call. An example use
2471 case for this would be the CPython shell start-up, where the
2472 banner and the initial prompt are received separately."
2473 (let ((regexp (or regexp comint-prompt-regexp
)))
2476 (when (not (accept-process-output process timeout
))
2479 regexp
(car (python-util-comint-last-prompt)))
2480 (throw 'found t
))))))
2482 (defun python-shell-comint-end-of-output-p (output)
2483 "Return non-nil if OUTPUT ends with input prompt."
2485 ;; XXX: It seems on macOS an extra carriage return is attached
2486 ;; at the end of output, this handles that too.
2489 ;; Remove initial caret from calculated regexp
2490 (replace-regexp-in-string
2491 (rx string-start ?^
) ""
2492 python-shell--prompt-calculated-input-regexp
)
2496 (define-obsolete-function-alias
2497 'python-comint-output-filter-function
2498 'ansi-color-filter-apply
2501 (defun python-comint-postoutput-scroll-to-bottom (output)
2502 "Faster version of `comint-postoutput-scroll-to-bottom'.
2503 Avoids `recenter' calls until OUTPUT is completely sent."
2504 (when (and (not (string= "" output
))
2505 (python-shell-comint-end-of-output-p
2506 (ansi-color-filter-apply output
)))
2507 (comint-postoutput-scroll-to-bottom output
))
2510 (defvar python-shell--parent-buffer nil
)
2512 (defmacro python-shell-with-shell-buffer
(&rest body
)
2513 "Execute the forms in BODY with the shell buffer temporarily current.
2514 Signals an error if no shell buffer is available for current buffer."
2515 (declare (indent 0) (debug t
))
2516 (let ((shell-process (make-symbol "shell-process")))
2517 `(let ((,shell-process
(python-shell-get-process-or-error)))
2518 (with-current-buffer (process-buffer ,shell-process
)
2521 (defvar python-shell--font-lock-buffer nil
)
2523 (defun python-shell-font-lock-get-or-create-buffer ()
2524 "Get or create a font-lock buffer for current inferior process."
2525 (python-shell-with-shell-buffer
2526 (if python-shell--font-lock-buffer
2527 python-shell--font-lock-buffer
2529 (process-name (get-buffer-process (current-buffer)))))
2530 (generate-new-buffer
2531 (format " *%s-font-lock*" process-name
))))))
2533 (defun python-shell-font-lock-kill-buffer ()
2534 "Kill the font-lock buffer safely."
2535 (when (and python-shell--font-lock-buffer
2536 (buffer-live-p python-shell--font-lock-buffer
))
2537 (kill-buffer python-shell--font-lock-buffer
)
2538 (when (derived-mode-p 'inferior-python-mode
)
2539 (setq python-shell--font-lock-buffer nil
))))
2541 (defmacro python-shell-font-lock-with-font-lock-buffer
(&rest body
)
2542 "Execute the forms in BODY in the font-lock buffer.
2543 The value returned is the value of the last form in BODY. See
2544 also `with-current-buffer'."
2545 (declare (indent 0) (debug t
))
2546 `(python-shell-with-shell-buffer
2547 (save-current-buffer
2548 (when (not (and python-shell--font-lock-buffer
2549 (get-buffer python-shell--font-lock-buffer
)))
2550 (setq python-shell--font-lock-buffer
2551 (python-shell-font-lock-get-or-create-buffer)))
2552 (set-buffer python-shell--font-lock-buffer
)
2553 (when (not font-lock-mode
)
2555 (set (make-local-variable 'delay-mode-hooks
) t
)
2556 (let ((python-indent-guess-indent-offset nil
))
2557 (when (not (derived-mode-p 'python-mode
))
2561 (defun python-shell-font-lock-cleanup-buffer ()
2562 "Cleanup the font-lock buffer.
2563 Provided as a command because this might be handy if something
2564 goes wrong and syntax highlighting in the shell gets messed up."
2566 (python-shell-with-shell-buffer
2567 (python-shell-font-lock-with-font-lock-buffer
2570 (defun python-shell-font-lock-comint-output-filter-function (output)
2571 "Clean up the font-lock buffer after any OUTPUT."
2572 (if (and (not (string= "" output
))
2573 ;; Is end of output and is not just a prompt.
2575 (python-shell-comint-end-of-output-p
2576 (ansi-color-filter-apply output
))
2578 ;; If output is other than an input prompt then "real" output has
2579 ;; been received and the font-lock buffer must be cleaned up.
2580 (python-shell-font-lock-cleanup-buffer)
2581 ;; Otherwise just add a newline.
2582 (python-shell-font-lock-with-font-lock-buffer
2583 (goto-char (point-max))
2587 (defun python-shell-font-lock-post-command-hook ()
2588 "Fontifies current line in shell buffer."
2589 (let ((prompt-end (cdr (python-util-comint-last-prompt))))
2590 (when (and prompt-end
(> (point) prompt-end
)
2591 (process-live-p (get-buffer-process (current-buffer))))
2592 (let* ((input (buffer-substring-no-properties
2593 prompt-end
(point-max)))
2594 (deactivate-mark nil
)
2595 (start-pos prompt-end
)
2596 (buffer-undo-list t
)
2597 (font-lock-buffer-pos nil
)
2599 (python-shell-font-lock-with-font-lock-buffer
2600 (delete-region (line-beginning-position)
2602 (setq font-lock-buffer-pos
(point))
2604 ;; Ensure buffer is fontified, keeping it
2605 ;; compatible with Emacs < 24.4.
2606 (if (fboundp 'font-lock-ensure
)
2607 (funcall 'font-lock-ensure
)
2608 (font-lock-default-fontify-buffer))
2609 (buffer-substring font-lock-buffer-pos
2611 (replacement-length (length replacement
))
2613 ;; Inject text properties to get input fontified.
2614 (while (not (= i replacement-length
))
2615 (let* ((plist (text-properties-at i replacement
))
2616 (next-change (or (next-property-change i replacement
)
2617 replacement-length
))
2618 (plist (let ((face (plist-get plist
'face
)))
2621 ;; Replace FACE text properties with
2622 ;; FONT-LOCK-FACE so input is fontified.
2623 (plist-put plist
'face nil
)
2624 (plist-put plist
'font-lock-face face
)))))
2625 (set-text-properties
2626 (+ start-pos i
) (+ start-pos next-change
) plist
)
2627 (setq i next-change
)))))))
2629 (defun python-shell-font-lock-turn-on (&optional msg
)
2630 "Turn on shell font-lock.
2631 With argument MSG show activation message."
2633 (python-shell-with-shell-buffer
2634 (python-shell-font-lock-kill-buffer)
2635 (set (make-local-variable 'python-shell--font-lock-buffer
) nil
)
2636 (add-hook 'post-command-hook
2637 #'python-shell-font-lock-post-command-hook nil
'local
)
2638 (add-hook 'kill-buffer-hook
2639 #'python-shell-font-lock-kill-buffer nil
'local
)
2640 (add-hook 'comint-output-filter-functions
2641 #'python-shell-font-lock-comint-output-filter-function
2644 (message "Shell font-lock is enabled"))))
2646 (defun python-shell-font-lock-turn-off (&optional msg
)
2647 "Turn off shell font-lock.
2648 With argument MSG show deactivation message."
2650 (python-shell-with-shell-buffer
2651 (python-shell-font-lock-kill-buffer)
2652 (when (python-util-comint-last-prompt)
2653 ;; Cleanup current fontification
2654 (remove-text-properties
2655 (cdr (python-util-comint-last-prompt))
2657 '(face nil font-lock-face nil
)))
2658 (set (make-local-variable 'python-shell--font-lock-buffer
) nil
)
2659 (remove-hook 'post-command-hook
2660 #'python-shell-font-lock-post-command-hook
'local
)
2661 (remove-hook 'kill-buffer-hook
2662 #'python-shell-font-lock-kill-buffer
'local
)
2663 (remove-hook 'comint-output-filter-functions
2664 #'python-shell-font-lock-comint-output-filter-function
2667 (message "Shell font-lock is disabled"))))
2669 (defun python-shell-font-lock-toggle (&optional msg
)
2670 "Toggle font-lock for shell.
2671 With argument MSG show activation/deactivation message."
2673 (python-shell-with-shell-buffer
2674 (set (make-local-variable 'python-shell-font-lock-enable
)
2675 (not python-shell-font-lock-enable
))
2676 (if python-shell-font-lock-enable
2677 (python-shell-font-lock-turn-on msg
)
2678 (python-shell-font-lock-turn-off msg
))
2679 python-shell-font-lock-enable
))
2681 (defvar python-shell--first-prompt-received-output-buffer nil
)
2682 (defvar python-shell--first-prompt-received nil
)
2684 (defcustom python-shell-first-prompt-hook nil
2685 "Hook run upon first (non-pdb) shell prompt detection.
2686 This is the place for shell setup functions that need to wait for
2687 output. Since the first prompt is ensured, this helps the
2688 current process to not hang while waiting. This is useful to
2689 safely attach setup code for long-running processes that
2690 eventually provide a shell."
2695 (defun python-shell-comint-watch-for-first-prompt-output-filter (output)
2696 "Run `python-shell-first-prompt-hook' when first prompt is found in OUTPUT."
2697 (when (not python-shell--first-prompt-received
)
2698 (set (make-local-variable 'python-shell--first-prompt-received-output-buffer
)
2699 (concat python-shell--first-prompt-received-output-buffer
2700 (ansi-color-filter-apply output
)))
2701 (when (python-shell-comint-end-of-output-p
2702 python-shell--first-prompt-received-output-buffer
)
2704 (concat python-shell-prompt-pdb-regexp
(rx eos
))
2705 (or python-shell--first-prompt-received-output-buffer
""))
2706 ;; Skip pdb prompts and reset the buffer.
2707 (setq python-shell--first-prompt-received-output-buffer nil
)
2708 (set (make-local-variable 'python-shell--first-prompt-received
) t
)
2709 (setq python-shell--first-prompt-received-output-buffer nil
)
2710 (with-current-buffer (current-buffer)
2711 (let ((inhibit-quit nil
))
2712 (run-hooks 'python-shell-first-prompt-hook
))))))
2715 ;; Used to hold user interactive overrides to
2716 ;; `python-shell-interpreter' and `python-shell-interpreter-args' that
2717 ;; will be made buffer-local by `inferior-python-mode':
2718 (defvar python-shell--interpreter
)
2719 (defvar python-shell--interpreter-args
)
2721 (define-derived-mode inferior-python-mode comint-mode
"Inferior Python"
2722 "Major mode for Python inferior process.
2723 Runs a Python interpreter as a subprocess of Emacs, with Python
2724 I/O through an Emacs buffer. Variables `python-shell-interpreter'
2725 and `python-shell-interpreter-args' control which Python
2726 interpreter is run. Variables
2727 `python-shell-prompt-regexp',
2728 `python-shell-prompt-output-regexp',
2729 `python-shell-prompt-block-regexp',
2730 `python-shell-font-lock-enable',
2731 `python-shell-completion-setup-code',
2732 `python-shell-completion-string-code',
2733 `python-eldoc-setup-code', `python-eldoc-string-code',
2734 `python-ffap-setup-code' and `python-ffap-string-code' can
2735 customize this mode for different Python interpreters.
2737 This mode resets `comint-output-filter-functions' locally, so you
2738 may want to re-add custom functions to it using the
2739 `inferior-python-mode-hook'.
2741 You can also add additional setup code to be run at
2742 initialization of the interpreter via `python-shell-setup-codes'
2745 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
2746 (when python-shell--parent-buffer
2747 (python-util-clone-local-variables python-shell--parent-buffer
))
2748 (set (make-local-variable 'indent-tabs-mode
) nil
)
2749 ;; Users can interactively override default values for
2750 ;; `python-shell-interpreter' and `python-shell-interpreter-args'
2751 ;; when calling `run-python'. This ensures values let-bound in
2752 ;; `python-shell-make-comint' are locally set if needed.
2753 (set (make-local-variable 'python-shell-interpreter
)
2754 (or python-shell--interpreter python-shell-interpreter
))
2755 (set (make-local-variable 'python-shell-interpreter-args
)
2756 (or python-shell--interpreter-args python-shell-interpreter-args
))
2757 (set (make-local-variable 'python-shell--prompt-calculated-input-regexp
) nil
)
2758 (set (make-local-variable 'python-shell--block-prompt
) nil
)
2759 (set (make-local-variable 'python-shell--prompt-calculated-output-regexp
) nil
)
2760 (python-shell-prompt-set-calculated-regexps)
2761 (setq comint-prompt-regexp python-shell--prompt-calculated-input-regexp
)
2762 (set (make-local-variable 'comint-prompt-read-only
) t
)
2763 (setq mode-line-process
'(":%s"))
2764 (set (make-local-variable 'comint-output-filter-functions
)
2765 '(ansi-color-process-output
2766 python-shell-comint-watch-for-first-prompt-output-filter
2767 python-pdbtrack-comint-output-filter-function
2768 python-comint-postoutput-scroll-to-bottom
2769 comint-watch-for-password-prompt
))
2770 (set (make-local-variable 'compilation-error-regexp-alist
)
2771 python-shell-compilation-regexp-alist
)
2772 (add-hook 'completion-at-point-functions
2773 #'python-shell-completion-at-point nil
'local
)
2774 (define-key inferior-python-mode-map
"\t"
2775 'python-shell-completion-complete-or-indent
)
2776 (make-local-variable 'python-pdbtrack-buffers-to-kill
)
2777 (make-local-variable 'python-pdbtrack-tracked-buffer
)
2778 (make-local-variable 'python-shell-internal-last-output
)
2779 (when python-shell-font-lock-enable
2780 (python-shell-font-lock-turn-on))
2781 (compilation-shell-minor-mode 1))
2783 (defun python-shell-make-comint (cmd proc-name
&optional show internal
)
2784 "Create a Python shell comint buffer.
2785 CMD is the Python command to be executed and PROC-NAME is the
2786 process name the comint buffer will get. After the comint buffer
2787 is created the `inferior-python-mode' is activated. When
2788 optional argument SHOW is non-nil the buffer is shown. When
2789 optional argument INTERNAL is non-nil this process is run on a
2790 buffer with a name that starts with a space, following the Emacs
2791 convention for temporary/internal buffers, and also makes sure
2792 the user is not queried for confirmation when the process is
2795 (python-shell-with-environment
2796 (let* ((proc-buffer-name
2797 (format (if (not internal
) "*%s*" " *%s*") proc-name
)))
2798 (when (not (comint-check-proc proc-buffer-name
))
2799 (let* ((cmdlist (split-string-and-unquote cmd
))
2800 (interpreter (car cmdlist
))
2801 (args (cdr cmdlist
))
2802 (buffer (apply #'make-comint-in-buffer proc-name proc-buffer-name
2803 interpreter nil args
))
2804 (python-shell--parent-buffer (current-buffer))
2805 (process (get-buffer-process buffer
))
2806 ;; Users can override the interpreter and args
2807 ;; interactively when calling `run-python', let-binding
2808 ;; these allows having the new right values in all
2809 ;; setup code that is done in `inferior-python-mode',
2810 ;; which is important, especially for prompt detection.
2811 (python-shell--interpreter interpreter
)
2812 (python-shell--interpreter-args
2813 (mapconcat #'identity args
" ")))
2814 (with-current-buffer buffer
2815 (inferior-python-mode))
2816 (when show
(display-buffer buffer
))
2817 (and internal
(set-process-query-on-exit-flag process nil
))))
2818 proc-buffer-name
))))
2821 (defun run-python (&optional cmd dedicated show
)
2822 "Run an inferior Python process.
2824 Argument CMD defaults to `python-shell-calculate-command' return
2825 value. When called interactively with `prefix-arg', it allows
2826 the user to edit such value and choose whether the interpreter
2827 should be DEDICATED for the current buffer. When numeric prefix
2828 arg is other than 0 or 4 do not SHOW.
2830 For a given buffer and same values of DEDICATED, if a process is
2831 already running for it, it will do nothing. This means that if
2832 the current buffer is using a global process, the user is still
2833 able to switch it to use a dedicated one.
2835 Runs the hook `inferior-python-mode-hook' after
2836 `comint-mode-hook' is run. (Type \\[describe-mode] in the
2837 process buffer for a list of commands.)"
2839 (if current-prefix-arg
2841 (read-shell-command "Run Python: " (python-shell-calculate-command))
2842 (y-or-n-p "Make dedicated process? ")
2843 (= (prefix-numeric-value current-prefix-arg
) 4))
2844 (list (python-shell-calculate-command) nil t
)))
2846 (python-shell-make-comint
2847 (or cmd
(python-shell-calculate-command))
2848 (python-shell-get-process-name dedicated
) show
)))
2849 (pop-to-buffer buffer
)
2850 (get-buffer-process buffer
)))
2852 (defun run-python-internal ()
2853 "Run an inferior Internal Python process.
2854 Input and output via buffer named after
2855 `python-shell-internal-buffer-name' and what
2856 `python-shell-internal-get-process-name' returns.
2858 This new kind of shell is intended to be used for generic
2859 communication related to defined configurations; the main
2860 difference with global or dedicated shells is that these ones are
2861 attached to a configuration, not a buffer. This means that can
2862 be used for example to retrieve the sys.path and other stuff,
2863 without messing with user shells. Note that
2864 `python-shell-font-lock-enable' and `inferior-python-mode-hook'
2865 are set to nil for these shells, so setup codes are not sent at
2867 (let ((python-shell-font-lock-enable nil
)
2868 (inferior-python-mode-hook nil
))
2870 (python-shell-make-comint
2871 (python-shell-calculate-command)
2872 (python-shell-internal-get-process-name) nil t
))))
2874 (defun python-shell-get-buffer ()
2875 "Return inferior Python buffer for current buffer.
2876 If current buffer is in `inferior-python-mode', return it."
2877 (if (derived-mode-p 'inferior-python-mode
)
2879 (let* ((dedicated-proc-name (python-shell-get-process-name t
))
2880 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name
))
2881 (global-proc-name (python-shell-get-process-name nil
))
2882 (global-proc-buffer-name (format "*%s*" global-proc-name
))
2883 (dedicated-running (comint-check-proc dedicated-proc-buffer-name
))
2884 (global-running (comint-check-proc global-proc-buffer-name
)))
2885 ;; Always prefer dedicated
2886 (or (and dedicated-running dedicated-proc-buffer-name
)
2887 (and global-running global-proc-buffer-name
)))))
2889 (defun python-shell-get-process ()
2890 "Return inferior Python process for current buffer."
2891 (get-buffer-process (python-shell-get-buffer)))
2893 (defun python-shell-get-process-or-error (&optional interactivep
)
2894 "Return inferior Python process for current buffer or signal error.
2895 When argument INTERACTIVEP is non-nil, use `user-error' instead
2896 of `error' with a user-friendly message."
2897 (or (python-shell-get-process)
2900 "Start a Python process first with `M-x run-python' or `%s'."
2904 #'run-python overriding-local-map t
)))
2906 "No inferior Python process running."))))
2908 (defun python-shell-get-or-create-process (&optional cmd dedicated show
)
2909 "Get or create an inferior Python process for current buffer and return it.
2910 Arguments CMD, DEDICATED and SHOW are those of `run-python' and
2911 are used to start the shell. If those arguments are not
2912 provided, `run-python' is called interactively and the user will
2913 be asked for their values."
2914 (let ((shell-process (python-shell-get-process)))
2915 (when (not shell-process
)
2917 ;; XXX: Refactor code such that calling `run-python'
2918 ;; interactively is not needed anymore.
2919 (call-interactively 'run-python
)
2920 (run-python cmd dedicated show
)))
2921 (or shell-process
(python-shell-get-process))))
2924 #'python-shell-get-or-create-process
2925 "Instead call `python-shell-get-process' and create one if returns nil."
2928 (define-obsolete-variable-alias
2929 'python-buffer
'python-shell-internal-buffer
"24.3")
2931 (defvar python-shell-internal-buffer nil
2932 "Current internal shell buffer for the current buffer.
2933 This is really not necessary at all for the code to work but it's
2934 there for compatibility with CEDET.")
2936 (define-obsolete-variable-alias
2937 'python-preoutput-result
'python-shell-internal-last-output
"24.3")
2939 (defvar python-shell-internal-last-output nil
2940 "Last output captured by the internal shell.
2941 This is really not necessary at all for the code to work but it's
2942 there for compatibility with CEDET.")
2944 (defun python-shell-internal-get-or-create-process ()
2945 "Get or create an inferior Internal Python process."
2946 (let ((proc-name (python-shell-internal-get-process-name)))
2947 (if (process-live-p proc-name
)
2948 (get-process proc-name
)
2949 (run-python-internal))))
2951 (define-obsolete-function-alias
2952 'python-proc
'python-shell-internal-get-or-create-process
"24.3")
2954 (defun python-shell--save-temp-file (string)
2955 (let* ((temporary-file-directory
2956 (if (file-remote-p default-directory
)
2957 (concat (file-remote-p default-directory
) "/tmp")
2958 temporary-file-directory
))
2959 (temp-file-name (make-temp-file "py"))
2960 (coding-system-for-write (python-info-encoding)))
2961 (with-temp-file temp-file-name
2963 (delete-trailing-whitespace))
2966 (defun python-shell-send-string (string &optional process msg
)
2967 "Send STRING to inferior Python PROCESS.
2968 When optional argument MSG is non-nil, forces display of a
2969 user-friendly message if there's no process running; defaults to
2970 t when called interactively."
2972 (list (read-string "Python command: ") nil t
))
2973 (let ((process (or process
(python-shell-get-process-or-error msg
))))
2974 (if (string-match ".\n+." string
) ;Multiline.
2975 (let* ((temp-file-name (python-shell--save-temp-file string
))
2976 (file-name (or (buffer-file-name) temp-file-name
)))
2977 (python-shell-send-file file-name process temp-file-name t
))
2978 (comint-send-string process string
)
2979 (when (or (not (string-match "\n\\'" string
))
2980 (string-match "\n[ \t].*\n?\\'" string
))
2981 (comint-send-string process
"\n")))))
2983 (defvar python-shell-output-filter-in-progress nil
)
2984 (defvar python-shell-output-filter-buffer nil
)
2986 (defun python-shell-output-filter (string)
2987 "Filter used in `python-shell-send-string-no-output' to grab output.
2988 STRING is the output received to this point from the process.
2989 This filter saves received output from the process in
2990 `python-shell-output-filter-buffer' and stops receiving it after
2991 detecting a prompt at the end of the buffer."
2993 string
(ansi-color-filter-apply string
)
2994 python-shell-output-filter-buffer
2995 (concat python-shell-output-filter-buffer string
))
2996 (when (python-shell-comint-end-of-output-p
2997 python-shell-output-filter-buffer
)
2998 ;; Output ends when `python-shell-output-filter-buffer' contains
2999 ;; the prompt attached at the end of it.
3000 (setq python-shell-output-filter-in-progress nil
3001 python-shell-output-filter-buffer
3002 (substring python-shell-output-filter-buffer
3003 0 (match-beginning 0)))
3005 python-shell--prompt-calculated-output-regexp
3006 python-shell-output-filter-buffer
)
3007 ;; Some shells, like IPython might append a prompt before the
3008 ;; output, clean that.
3009 (setq python-shell-output-filter-buffer
3010 (substring python-shell-output-filter-buffer
(match-end 0)))))
3013 (defun python-shell-send-string-no-output (string &optional process
)
3014 "Send STRING to PROCESS and inhibit output.
3016 (let ((process (or process
(python-shell-get-process-or-error)))
3017 (comint-preoutput-filter-functions
3018 '(python-shell-output-filter))
3019 (python-shell-output-filter-in-progress t
)
3023 (python-shell-send-string string process
)
3024 (while python-shell-output-filter-in-progress
3025 ;; `python-shell-output-filter' takes care of setting
3026 ;; `python-shell-output-filter-in-progress' to NIL after it
3027 ;; detects end of output.
3028 (accept-process-output process
))
3030 python-shell-output-filter-buffer
3031 (setq python-shell-output-filter-buffer nil
)))
3032 (with-current-buffer (process-buffer process
)
3033 (comint-interrupt-subjob)))))
3035 (defun python-shell-internal-send-string (string)
3036 "Send STRING to the Internal Python interpreter.
3037 Returns the output. See `python-shell-send-string-no-output'."
3038 ;; XXX Remove `python-shell-internal-last-output' once CEDET is
3039 ;; updated to support this new mode.
3040 (setq python-shell-internal-last-output
3041 (python-shell-send-string-no-output
3042 ;; Makes this function compatible with the old
3043 ;; python-send-receive. (At least for CEDET).
3044 (replace-regexp-in-string "_emacs_out +" "" string
)
3045 (python-shell-internal-get-or-create-process))))
3047 (define-obsolete-function-alias
3048 'python-send-receive
'python-shell-internal-send-string
"24.3")
3050 (define-obsolete-function-alias
3051 'python-send-string
'python-shell-internal-send-string
"24.3")
3053 (defun python-shell-buffer-substring (start end
&optional nomain
)
3054 "Send buffer substring from START to END formatted for shell.
3055 This is a wrapper over `buffer-substring' that takes care of
3056 different transformations for the code sent to be evaluated in
3058 1. When optional argument NOMAIN is non-nil everything under an
3059 \"if __name__ == \\='__main__\\='\" block will be removed.
3060 2. When a subregion of the buffer is sent, it takes care of
3061 appending extra empty lines so tracebacks are correct.
3062 3. When the region sent is a substring of the current buffer, a
3063 coding cookie is added.
3064 4. Wraps indented regions under an \"if True:\" block so the
3065 interpreter evaluates them correctly."
3066 (let* ((start (save-excursion
3067 ;; Normalize start to the line beginning position.
3069 (line-beginning-position)))
3070 (substring (buffer-substring-no-properties start end
))
3071 (starts-at-point-min-p (save-restriction
3073 (= (point-min) start
)))
3074 (encoding (python-info-encoding))
3075 (toplevel-p (zerop (save-excursion
3077 (python-util-forward-comment 1)
3078 (current-indentation))))
3079 (fillstr (when (not starts-at-point-min-p
)
3081 (format "# -*- coding: %s -*-\n" encoding
)
3083 ;; Subtract 2 because of the coding cookie.
3084 (- (line-number-at-pos start
) 2) ?
\n)))))
3090 (goto-char (point-min))
3091 (when (not toplevel-p
)
3093 (delete-region (point) (line-end-position)))
3095 (let* ((if-name-main-start-end
3098 (when (python-nav-if-name-main)
3100 (progn (python-nav-forward-sexp-safe)
3101 ;; Include ending newline
3104 ;; Oh destructuring bind, how I miss you.
3105 (if-name-main-start (car if-name-main-start-end
))
3106 (if-name-main-end (cdr if-name-main-start-end
))
3107 (fillstr (make-string
3108 (- (line-number-at-pos if-name-main-end
)
3109 (line-number-at-pos if-name-main-start
)) ?
\n)))
3110 (when if-name-main-start-end
3111 (goto-char if-name-main-start
)
3112 (delete-region if-name-main-start if-name-main-end
)
3114 ;; Ensure there's only one coding cookie in the generated string.
3115 (goto-char (point-min))
3116 (when (looking-at-p (python-rx coding-cookie
))
3118 (when (looking-at-p (python-rx coding-cookie
))
3120 (line-beginning-position) (line-end-position))))
3121 (buffer-substring-no-properties (point-min) (point-max)))))
3123 (defun python-shell-send-region (start end
&optional send-main msg
)
3124 "Send the region delimited by START and END to inferior Python process.
3125 When optional argument SEND-MAIN is non-nil, allow execution of
3126 code inside blocks delimited by \"if __name__== \\='__main__\\=':\".
3127 When called interactively SEND-MAIN defaults to nil, unless it's
3128 called with prefix argument. When optional argument MSG is
3129 non-nil, forces display of a user-friendly message if there's no
3130 process running; defaults to t when called interactively."
3132 (list (region-beginning) (region-end) current-prefix-arg t
))
3133 (let* ((string (python-shell-buffer-substring start end
(not send-main
)))
3134 (process (python-shell-get-process-or-error msg
))
3135 (original-string (buffer-substring-no-properties start end
))
3136 (_ (string-match "\\`\n*\\(.*\\)" original-string
)))
3137 (message "Sent: %s..." (match-string 1 original-string
))
3138 (python-shell-send-string string process
)))
3140 (defun python-shell-send-buffer (&optional send-main msg
)
3141 "Send the entire buffer to inferior Python process.
3142 When optional argument SEND-MAIN is non-nil, allow execution of
3143 code inside blocks delimited by \"if __name__== \\='__main__\\=':\".
3144 When called interactively SEND-MAIN defaults to nil, unless it's
3145 called with prefix argument. When optional argument MSG is
3146 non-nil, forces display of a user-friendly message if there's no
3147 process running; defaults to t when called interactively."
3148 (interactive (list current-prefix-arg t
))
3151 (python-shell-send-region (point-min) (point-max) send-main msg
)))
3153 (defun python-shell-send-defun (&optional arg msg
)
3154 "Send the current defun to inferior Python process.
3155 When argument ARG is non-nil do not include decorators. When
3156 optional argument MSG is non-nil, forces display of a
3157 user-friendly message if there's no process running; defaults to
3158 t when called interactively."
3159 (interactive (list current-prefix-arg t
))
3161 (python-shell-send-region
3164 (while (and (or (python-nav-beginning-of-defun)
3165 (beginning-of-line 1))
3166 (> (current-indentation) 0)))
3169 (eq (forward-line -
1) 0)
3170 (if (looking-at (python-rx decorator
))
3176 (or (python-nav-end-of-defun)
3182 (defun python-shell-send-file (file-name &optional process temp-file-name
3184 "Send FILE-NAME to inferior Python PROCESS.
3185 If TEMP-FILE-NAME is passed then that file is used for processing
3186 instead, while internally the shell will continue to use
3187 FILE-NAME. If TEMP-FILE-NAME and DELETE are non-nil, then
3188 TEMP-FILE-NAME is deleted after evaluation is performed. When
3189 optional argument MSG is non-nil, forces display of a
3190 user-friendly message if there's no process running; defaults to
3191 t when called interactively."
3194 (read-file-name "File to send: ") ; file-name
3196 nil
; temp-file-name
3199 (let* ((process (or process
(python-shell-get-process-or-error msg
)))
3200 (encoding (with-temp-buffer
3201 (insert-file-contents
3202 (or temp-file-name file-name
))
3203 (python-info-encoding)))
3204 (file-name (file-local-name (expand-file-name file-name
)))
3205 (temp-file-name (when temp-file-name
3206 (file-local-name (expand-file-name
3208 (python-shell-send-string
3211 "import codecs, os;"
3212 "__pyfile = codecs.open('''%s''', encoding='''%s''');"
3213 "__code = __pyfile.read().encode('''%s''');"
3215 (when (and delete temp-file-name
)
3216 (format "os.remove('''%s''');" temp-file-name
))
3217 "exec(compile(__code, '''%s''', 'exec'));")
3218 (or temp-file-name file-name
) encoding encoding file-name
)
3221 (defun python-shell-switch-to-shell (&optional msg
)
3222 "Switch to inferior Python process buffer.
3223 When optional argument MSG is non-nil, forces display of a
3224 user-friendly message if there's no process running; defaults to
3225 t when called interactively."
3228 (process-buffer (python-shell-get-process-or-error msg
)) nil t
))
3230 (defun python-shell-send-setup-code ()
3231 "Send all setup code for shell.
3232 This function takes the list of setup code to send from the
3233 `python-shell-setup-codes' list."
3234 (when python-shell-setup-codes
3235 (let ((process (python-shell-get-process))
3239 (cond ((stringp elt
) elt
)
3240 ((symbolp elt
) (symbol-value elt
))
3242 python-shell-setup-codes
3243 "\n\nprint ('python.el: sent setup code')"))))
3244 (python-shell-send-string code process
)
3245 (python-shell-accept-process-output process
))))
3247 (add-hook 'python-shell-first-prompt-hook
3248 #'python-shell-send-setup-code
)
3251 ;;; Shell completion
3253 (defcustom python-shell-completion-setup-code
3255 def __PYTHON_EL_get_completions(text):
3266 import builtins as __builtin__
3267 builtins = dir(__builtin__)
3269 is_ipython = ('__IPYTHON__' in builtins or
3270 '__IPYTHON__active' in builtins)
3271 splits = text.split()
3272 is_module = splits and splits[0] in ('from', 'import')
3274 if is_ipython and is_module:
3275 from IPython.core.completerlib import module_completion
3276 completions = module_completion(text.strip())
3277 elif is_ipython and '__IP' in builtins:
3278 completions = __IP.complete(text)
3279 elif is_ipython and 'get_ipython' in builtins:
3280 completions = get_ipython().Completer.all_completions(text)
3282 # Try to reuse current completer.
3283 completer = readline.get_completer()
3285 # importing rlcompleter sets the completer, use it as a
3286 # last resort to avoid breaking customizations.
3288 completer = readline.get_completer()
3289 if getattr(completer, 'PYTHON_EL_WRAPPED', False):
3290 completer.print_mode = False
3293 completion = completer(text, i)
3297 completions.append(completion)
3301 if getattr(completer, 'PYTHON_EL_WRAPPED', False):
3302 completer.print_mode = True
3304 "Code used to setup completion in inferior Python processes."
3308 (define-obsolete-variable-alias
3309 'python-shell-completion-module-string-code
3310 'python-shell-completion-string-code
3312 "Completion string code must also autocomplete modules.")
3314 (define-obsolete-variable-alias
3315 'python-shell-completion-pdb-string-code
3316 'python-shell-completion-string-code
3318 "Completion string code must work for (i)pdb.")
3320 (defcustom python-shell-completion-string-code
3321 "';'.join(__PYTHON_EL_get_completions('''%s'''))"
3322 "Python code used to get a string of completions separated by semicolons.
3323 The string passed to the function is the current python name or
3324 the full statement in the case of imports."
3328 (defcustom python-shell-completion-native-disabled-interpreters
3329 ;; PyPy's readline cannot handle some escape sequences yet. Native
3330 ;; completion was found to be non-functional for IPython (see
3331 ;; Bug#25067). Native completion doesn't work on w32 (Bug#28580).
3332 (if (eq system-type
'windows-nt
) '("")
3333 '("pypy" "ipython"))
3334 "List of disabled interpreters.
3335 When a match is found, native completion is disabled."
3337 :type
'(repeat string
))
3339 (defcustom python-shell-completion-native-enable t
3340 "Enable readline based native completion."
3344 (defcustom python-shell-completion-native-output-timeout
5.0
3345 "Time in seconds to wait for completion output before giving up."
3349 (defcustom python-shell-completion-native-try-output-timeout
1.0
3350 "Time in seconds to wait for *trying* native completion output."
3354 (defvar python-shell-completion-native-redirect-buffer
3355 " *Python completions redirect*"
3356 "Buffer to be used to redirect output of readline commands.")
3358 (defun python-shell-completion-native-interpreter-disabled-p ()
3359 "Return non-nil if interpreter has native completion disabled."
3360 (when python-shell-completion-native-disabled-interpreters
3362 (regexp-opt python-shell-completion-native-disabled-interpreters
)
3363 (file-name-nondirectory python-shell-interpreter
))))
3365 (defun python-shell-completion-native-try ()
3366 "Return non-nil if can trigger native completion."
3367 (let ((python-shell-completion-native-enable t
)
3368 (python-shell-completion-native-output-timeout
3369 python-shell-completion-native-try-output-timeout
))
3370 (python-shell-completion-native-get-completions
3371 (get-buffer-process (current-buffer))
3374 (defun python-shell-completion-native-setup ()
3375 "Try to setup native completion, return non-nil on success."
3376 (let ((process (python-shell-get-process)))
3377 (with-current-buffer (process-buffer process
)
3378 (python-shell-send-string "
3379 def __PYTHON_EL_native_completion_setup():
3387 import builtins as __builtin__
3389 builtins = dir(__builtin__)
3390 is_ipython = ('__IPYTHON__' in builtins or
3391 '__IPYTHON__active' in builtins)
3393 class __PYTHON_EL_Completer:
3394 '''Completer wrapper that prints candidates to stdout.
3396 It wraps an existing completer function and changes its behavior so
3397 that the user input is unchanged and real candidates are printed to
3400 Returned candidates are '0__dummy_completion__' and
3401 '1__dummy_completion__' in that order ('0__dummy_completion__' is
3402 returned repeatedly until all possible candidates are consumed).
3404 The real candidates are printed to stdout so that they can be
3405 easily retrieved through comint output redirect trickery.
3408 PYTHON_EL_WRAPPED = True
3410 def __init__(self, completer):
3411 self.completer = completer
3412 self.last_completion = None
3413 self.print_mode = True
3415 def __call__(self, text, state):
3417 # Set the first dummy completion.
3418 self.last_completion = None
3419 completion = '0__dummy_completion__'
3421 completion = self.completer(text, state - 1)
3424 if self.last_completion != '1__dummy_completion__':
3425 # When no more completions are available, returning a
3426 # dummy with non-sharing prefix allow ensuring output
3427 # while preventing changes to current input.
3428 # Coincidentally it's also the end of output.
3429 completion = '1__dummy_completion__'
3430 elif completion.endswith('('):
3431 # Remove parens on callables as it breaks completion on
3432 # arguments (e.g. str(Ari<tab>)).
3433 completion = completion[:-1]
3434 self.last_completion = completion
3437 '0__dummy_completion__', '1__dummy_completion__'):
3440 # For every non-dummy completion, return a repeated dummy
3441 # one and print the real candidate so it can be retrieved
3442 # by comint output filters.
3445 return '0__dummy_completion__'
3451 completer = readline.get_completer()
3454 # Used as last resort to avoid breaking customizations.
3456 completer = readline.get_completer()
3458 if completer and not getattr(completer, 'PYTHON_EL_WRAPPED', False):
3459 # Wrap the existing completer function only once.
3460 new_completer = __PYTHON_EL_Completer(completer)
3462 readline.set_completer(new_completer)
3464 # Try both initializations to cope with all IPython versions.
3465 # This works fine for IPython 3.x but not for earlier:
3466 readline.set_completer(new_completer)
3467 # IPython<3 hacks readline such that `readline.set_completer`
3468 # won't work. This workaround injects the new completer
3469 # function into the existing instance directly:
3470 instance = getattr(completer, 'im_self', completer.__self__)
3471 instance.rlcomplete = new_completer
3473 if readline.__doc__ and 'libedit' in readline.__doc__:
3474 raise Exception('''libedit based readline is known not to work,
3475 see etc/PROBLEMS under \"In Inferior Python mode, input is echoed\".''')
3476 readline.parse_and_bind('bind ^I rl_complete')
3478 readline.parse_and_bind('tab: complete')
3479 # Require just one tab to send output.
3480 readline.parse_and_bind('set show-all-if-ambiguous on')
3482 print ('python.el: native completion setup loaded')
3485 print ('python.el: native completion setup failed, %s: %s'
3486 % sys.exc_info()[:2])
3488 __PYTHON_EL_native_completion_setup()" process
)
3490 (python-shell-accept-process-output
3491 process python-shell-completion-native-try-output-timeout
)
3494 (regexp-quote "python.el: native completion setup loaded") nil t
1)))
3495 (python-shell-completion-native-try)))))
3497 (defun python-shell-completion-native-turn-off (&optional msg
)
3498 "Turn off shell native completions.
3499 With argument MSG show deactivation message."
3501 (python-shell-with-shell-buffer
3502 (set (make-local-variable 'python-shell-completion-native-enable
) nil
)
3504 (message "Shell native completion is disabled, using fallback"))))
3506 (defun python-shell-completion-native-turn-on (&optional msg
)
3507 "Turn on shell native completions.
3508 With argument MSG show deactivation message."
3510 (python-shell-with-shell-buffer
3511 (set (make-local-variable 'python-shell-completion-native-enable
) t
)
3512 (python-shell-completion-native-turn-on-maybe msg
)))
3514 (defun python-shell-completion-native-turn-on-maybe (&optional msg
)
3515 "Turn on native completions if enabled and available.
3516 With argument MSG show activation/deactivation message."
3518 (python-shell-with-shell-buffer
3519 (when python-shell-completion-native-enable
3521 ((python-shell-completion-native-interpreter-disabled-p)
3522 (python-shell-completion-native-turn-off msg
))
3523 ((python-shell-completion-native-setup)
3525 (message "Shell native completion is enabled.")))
3527 '(python python-shell-completion-native-turn-on-maybe
)
3530 "Your `python-shell-interpreter' doesn't seem to "
3531 "support readline, yet `python-shell-completion-native-enable' "
3532 (format "was t and %S is not part of the "
3533 (file-name-nondirectory python-shell-interpreter
))
3534 "`python-shell-completion-native-disabled-interpreters' "
3535 "list. Native completions have been disabled locally. "))
3536 (python-shell-completion-native-turn-off msg
))))))
3538 (defun python-shell-completion-native-turn-on-maybe-with-msg ()
3539 "Like `python-shell-completion-native-turn-on-maybe' but force messages."
3540 (python-shell-completion-native-turn-on-maybe t
))
3542 (add-hook 'python-shell-first-prompt-hook
3543 #'python-shell-completion-native-turn-on-maybe-with-msg
)
3545 (defun python-shell-completion-native-toggle (&optional msg
)
3546 "Toggle shell native completion.
3547 With argument MSG show activation/deactivation message."
3549 (python-shell-with-shell-buffer
3550 (if python-shell-completion-native-enable
3551 (python-shell-completion-native-turn-off msg
)
3552 (python-shell-completion-native-turn-on msg
))
3553 python-shell-completion-native-enable
))
3555 (defun python-shell-completion-native-get-completions (process import input
)
3556 "Get completions using native readline for PROCESS.
3557 When IMPORT is non-nil takes precedence over INPUT for
3559 (with-current-buffer (process-buffer process
)
3560 (let* ((input (or import input
))
3561 (original-filter-fn (process-filter process
))
3562 (redirect-buffer (get-buffer-create
3563 python-shell-completion-native-redirect-buffer
))
3565 (new-input (concat input trigger
))
3568 (+ (- (point-max) (comint-bol)) (length new-input
))))
3569 (delete-line-command (make-string input-length ?
\b))
3570 (input-to-send (concat new-input delete-line-command
)))
3571 ;; Ensure restoring the process filter, even if the user quits
3572 ;; or there's some other error.
3574 (with-current-buffer redirect-buffer
3575 ;; Cleanup the redirect buffer
3577 ;; Mimic `comint-redirect-send-command', unfortunately it
3578 ;; can't be used here because it expects a newline in the
3579 ;; command and that's exactly what we are trying to avoid.
3580 (let ((comint-redirect-echo-input nil
)
3581 (comint-redirect-completed nil
)
3582 (comint-redirect-perform-sanity-check nil
)
3583 (comint-redirect-insert-matching-regexp t
)
3584 (comint-redirect-finished-regexp
3585 "1__dummy_completion__[[:space:]]*\n")
3586 (comint-redirect-output-buffer redirect-buffer
))
3587 ;; Compatibility with Emacs 24.x. Comint changed and
3588 ;; now `comint-redirect-filter' gets 3 args. This
3589 ;; checks which version of `comint-redirect-filter' is
3590 ;; in use based on its args and uses `apply-partially'
3591 ;; to make it up for the 3 args case.
3593 (help-function-arglist 'comint-redirect-filter
)) 3)
3595 process
(apply-partially
3596 #'comint-redirect-filter original-filter-fn
))
3597 (set-process-filter process
#'comint-redirect-filter
))
3598 (process-send-string process input-to-send
)
3599 ;; Grab output until our dummy completion used as
3600 ;; output end marker is found.
3601 (when (python-shell-accept-process-output
3602 process python-shell-completion-native-output-timeout
3603 comint-redirect-finished-regexp
)
3604 (re-search-backward "0__dummy_completion__" nil t
)
3605 (cl-remove-duplicates
3607 (buffer-substring-no-properties
3608 (line-beginning-position) (point-min))
3609 "[ \f\t\n\r\v()]+" t
)
3611 (set-process-filter process original-filter-fn
)))))
3613 (defun python-shell-completion-get-completions (process import input
)
3614 "Do completion at point using PROCESS for IMPORT or INPUT.
3615 When IMPORT is non-nil takes precedence over INPUT for
3617 (setq input
(or import input
))
3618 (with-current-buffer (process-buffer process
)
3620 (python-util-strip-string
3621 (python-shell-send-string-no-output
3623 (concat python-shell-completion-setup-code
3624 "\nprint (" python-shell-completion-string-code
")")
3626 (when (> (length completions
) 2)
3627 (split-string completions
3628 "^'\\|^\"\\|;\\|'$\\|\"$" t
)))))
3630 (defun python-shell-completion-at-point (&optional process
)
3631 "Function for `completion-at-point-functions' in `inferior-python-mode'.
3632 Optional argument PROCESS forces completions to be retrieved
3633 using that one instead of current buffer's process."
3634 (setq process
(or process
(get-buffer-process (current-buffer))))
3635 (let* ((line-start (if (derived-mode-p 'inferior-python-mode
)
3636 ;; Working on a shell buffer: use prompt end.
3637 (cdr (python-util-comint-last-prompt))
3638 (line-beginning-position)))
3640 (when (string-match-p
3641 (rx (* space
) word-start
(or "from" "import") word-end space
)
3642 (buffer-substring-no-properties line-start
(point)))
3643 (buffer-substring-no-properties line-start
(point))))
3646 (if (not (re-search-backward
3648 (or whitespace open-paren close-paren string-delimiter
))
3652 (forward-char (length (match-string-no-properties 0)))
3656 (with-current-buffer (process-buffer process
)
3657 (python-util-comint-last-prompt)))
3659 (with-current-buffer (process-buffer process
)
3660 (when prompt-boundaries
3661 (buffer-substring-no-properties
3662 (car prompt-boundaries
) (cdr prompt-boundaries
)))))
3664 (with-current-buffer (process-buffer process
)
3665 (cond ((or (null prompt
)
3666 (< (point) (cdr prompt-boundaries
)))
3668 ((or (not python-shell-completion-native-enable
)
3669 ;; Even if native completion is enabled, for
3670 ;; pdb interaction always use the fallback
3671 ;; mechanism since the completer is changed.
3672 ;; Also, since pdb interaction is single-line
3673 ;; based, this is enough.
3674 (string-match-p python-shell-prompt-pdb-regexp prompt
))
3675 (if (or (equal python-shell--block-prompt prompt
)
3677 python-shell-prompt-block-regexp prompt
))
3678 ;; The non-native completion mechanism sends
3679 ;; newlines to the interpreter, so we can't use
3680 ;; it during a multiline statement (Bug#28051).
3682 #'python-shell-completion-get-completions
))
3683 (t #'python-shell-completion-native-get-completions
)))))
3685 (completion-table-dynamic
3688 process import-statement
)))))
3690 (define-obsolete-function-alias
3691 'python-shell-completion-complete-at-point
3692 'python-shell-completion-at-point
3695 (defun python-shell-completion-complete-or-indent ()
3696 "Complete or indent depending on the context.
3697 If content before pointer is all whitespace, indent.
3698 If not try to complete."
3700 (if (string-match "^[[:space:]]*$"
3701 (buffer-substring (comint-line-beginning-position)
3703 (indent-for-tab-command)
3704 (completion-at-point)))
3707 ;;; PDB Track integration
3709 (defcustom python-pdbtrack-activate t
3710 "Non-nil makes Python shell enable pdbtracking."
3715 (defcustom python-pdbtrack-stacktrace-info-regexp
3716 "> \\([^\"(<]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()"
3717 "Regular expression matching stacktrace information.
3718 Used to extract the current line and module being inspected."
3723 (defvar python-pdbtrack-tracked-buffer nil
3724 "Variable containing the value of the current tracked buffer.
3725 Never set this variable directly, use
3726 `python-pdbtrack-set-tracked-buffer' instead.")
3728 (defvar python-pdbtrack-buffers-to-kill nil
3729 "List of buffers to be deleted after tracking finishes.")
3731 (defun python-pdbtrack-set-tracked-buffer (file-name)
3732 "Set the buffer for FILE-NAME as the tracked buffer.
3733 Internally it uses the `python-pdbtrack-tracked-buffer' variable.
3734 Returns the tracked buffer."
3735 (let* ((file-name-prospect (concat (file-remote-p default-directory
)
3737 (file-buffer (get-file-buffer file-name-prospect
)))
3739 (setq python-pdbtrack-tracked-buffer file-buffer
)
3741 ((file-exists-p file-name-prospect
)
3742 (setq file-buffer
(find-file-noselect file-name-prospect
)))
3743 ((and (not (equal file-name file-name-prospect
))
3744 (file-exists-p file-name
))
3745 ;; Fallback to a locally available copy of the file.
3746 (setq file-buffer
(find-file-noselect file-name-prospect
))))
3747 (when (not (member file-buffer python-pdbtrack-buffers-to-kill
))
3748 (add-to-list 'python-pdbtrack-buffers-to-kill file-buffer
)))
3751 (defun python-pdbtrack-comint-output-filter-function (output)
3752 "Move overlay arrow to current pdb line in tracked buffer.
3753 Argument OUTPUT is a string with the output from the comint process."
3754 (when (and python-pdbtrack-activate
(not (string= output
"")))
3755 (let* ((full-output (ansi-color-filter-apply
3756 (buffer-substring comint-last-input-end
(point-max))))
3760 (insert full-output
)
3761 ;; When the debugger encounters a pdb.set_trace()
3762 ;; command, it prints a single stack frame. Sometimes
3763 ;; it prints a bit of extra information about the
3764 ;; arguments of the present function. When ipdb
3765 ;; encounters an exception, it prints the _entire_ stack
3766 ;; trace. To handle all of these cases, we want to find
3767 ;; the _last_ stack frame printed in the most recent
3768 ;; batch of output, then jump to the corresponding
3769 ;; file/line number.
3770 (goto-char (point-max))
3771 (when (re-search-backward python-pdbtrack-stacktrace-info-regexp nil t
)
3772 (setq line-number
(string-to-number
3773 (match-string-no-properties 2)))
3774 (match-string-no-properties 1)))))
3775 (if (and file-name line-number
)
3776 (let* ((tracked-buffer
3777 (python-pdbtrack-set-tracked-buffer file-name
))
3778 (shell-buffer (current-buffer))
3779 (tracked-buffer-window (get-buffer-window tracked-buffer
))
3780 (tracked-buffer-line-pos))
3781 (with-current-buffer tracked-buffer
3782 (set (make-local-variable 'overlay-arrow-string
) "=>")
3783 (set (make-local-variable 'overlay-arrow-position
) (make-marker))
3784 (setq tracked-buffer-line-pos
(progn
3785 (goto-char (point-min))
3786 (forward-line (1- line-number
))
3788 (when tracked-buffer-window
3790 tracked-buffer-window tracked-buffer-line-pos
))
3791 (set-marker overlay-arrow-position tracked-buffer-line-pos
))
3792 (pop-to-buffer tracked-buffer
)
3793 (switch-to-buffer-other-window shell-buffer
))
3794 (when python-pdbtrack-tracked-buffer
3795 (with-current-buffer python-pdbtrack-tracked-buffer
3796 (set-marker overlay-arrow-position nil
))
3797 (mapc #'(lambda (buffer)
3798 (ignore-errors (kill-buffer buffer
)))
3799 python-pdbtrack-buffers-to-kill
)
3800 (setq python-pdbtrack-tracked-buffer nil
3801 python-pdbtrack-buffers-to-kill nil
)))))
3805 ;;; Symbol completion
3807 (defun python-completion-at-point ()
3808 "Function for `completion-at-point-functions' in `python-mode'.
3809 For this to work as best as possible you should call
3810 `python-shell-send-buffer' from time to time so context in
3811 inferior Python process is updated properly."
3812 (let ((process (python-shell-get-process)))
3814 (python-shell-completion-at-point process
))))
3816 (define-obsolete-function-alias
3817 'python-completion-complete-at-point
3818 'python-completion-at-point
3824 (defcustom python-fill-comment-function
'python-fill-comment
3825 "Function to fill comments.
3826 This is the function used by `python-fill-paragraph' to
3831 (defcustom python-fill-string-function
'python-fill-string
3832 "Function to fill strings.
3833 This is the function used by `python-fill-paragraph' to
3838 (defcustom python-fill-decorator-function
'python-fill-decorator
3839 "Function to fill decorators.
3840 This is the function used by `python-fill-paragraph' to
3845 (defcustom python-fill-paren-function
'python-fill-paren
3846 "Function to fill parens.
3847 This is the function used by `python-fill-paragraph' to
3852 (defcustom python-fill-docstring-style
'pep-257
3853 "Style used to fill docstrings.
3854 This affects `python-fill-string' behavior with regards to
3855 triple quotes positioning.
3857 Possible values are `django', `onetwo', `pep-257', `pep-257-nn',
3858 `symmetric', and nil. A value of nil won't care about quotes
3859 position and will treat docstrings a normal string, any other
3860 value may result in one of the following docstring styles:
3865 Process foo, return bar.
3869 Process foo, return bar.
3871 If processing fails throw ProcessingError.
3876 \"\"\"Process foo, return bar.\"\"\"
3879 Process foo, return bar.
3881 If processing fails throw ProcessingError.
3887 \"\"\"Process foo, return bar.\"\"\"
3889 \"\"\"Process foo, return bar.
3891 If processing fails throw ProcessingError.
3897 \"\"\"Process foo, return bar.\"\"\"
3899 \"\"\"Process foo, return bar.
3901 If processing fails throw ProcessingError.
3906 \"\"\"Process foo, return bar.\"\"\"
3909 Process foo, return bar.
3911 If processing fails throw ProcessingError.
3914 (const :tag
"Don't format docstrings" nil
)
3915 (const :tag
"Django's coding standards style." django
)
3916 (const :tag
"One newline and start and Two at end style." onetwo
)
3917 (const :tag
"PEP-257 with 2 newlines at end of string." pep-257
)
3918 (const :tag
"PEP-257 with 1 newline at end of string." pep-257-nn
)
3919 (const :tag
"Symmetric style." symmetric
))
3922 (memq val
'(django onetwo pep-257 pep-257-nn symmetric nil
))))
3924 (defun python-fill-paragraph (&optional justify
)
3925 "`fill-paragraph-function' handling multi-line strings and possibly comments.
3926 If any of the current line is in or at the end of a multi-line string,
3927 fill the string or the paragraph of it that point is in, preserving
3928 the string's indentation.
3929 Optional argument JUSTIFY defines if the paragraph should be justified."
3934 ((python-syntax-context 'comment
)
3935 (funcall python-fill-comment-function justify
))
3936 ;; Strings/Docstrings
3937 ((save-excursion (or (python-syntax-context 'string
)
3938 (equal (string-to-syntax "|")
3939 (syntax-after (point)))))
3940 (funcall python-fill-string-function justify
))
3942 ((equal (char-after (save-excursion
3943 (python-nav-beginning-of-statement))) ?
@)
3944 (funcall python-fill-decorator-function justify
))
3946 ((or (python-syntax-context 'paren
)
3947 (looking-at (python-rx open-paren
))
3949 (skip-syntax-forward "^(" (line-end-position))
3950 (looking-at (python-rx open-paren
))))
3951 (funcall python-fill-paren-function justify
))
3954 (defun python-fill-comment (&optional justify
)
3955 "Comment fill function for `python-fill-paragraph'.
3956 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3957 (fill-comment-paragraph justify
))
3959 (defun python-fill-string (&optional justify
)
3960 "String fill function for `python-fill-paragraph'.
3961 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3962 (let* ((str-start-pos
3965 (or (python-syntax-context 'string
)
3966 (and (equal (string-to-syntax "|")
3967 (syntax-after (point)))
3969 (num-quotes (python-syntax-count-quotes
3970 (char-after str-start-pos
) str-start-pos
))
3973 (goto-char (+ str-start-pos num-quotes
))
3974 (or (re-search-forward (rx (syntax string-delimiter
)) nil t
)
3975 (goto-char (point-max)))
3978 ;; Docstring styles may vary for oneliners and multi-liners.
3979 (> (count-matches "\n" str-start-pos str-end-pos
) 0))
3981 (pcase python-fill-docstring-style
3982 ;; delimiters-style is a cons cell with the form
3983 ;; (START-NEWLINES . END-NEWLINES). When any of the sexps
3984 ;; is NIL means to not add any newlines for start or end
3985 ;; of docstring. See `python-fill-docstring-style' for a
3986 ;; graphic idea of each style.
3987 (`django
(cons 1 1))
3988 (`onetwo
(and multi-line-p
(cons 1 2)))
3989 (`pep-257
(and multi-line-p
(cons nil
2)))
3990 (`pep-257-nn
(and multi-line-p
(cons nil
1)))
3991 (`symmetric
(and multi-line-p
(cons 1 1)))))
3992 (fill-paragraph-function))
3994 (narrow-to-region str-start-pos str-end-pos
)
3995 (fill-paragraph justify
))
3997 (when (and (python-info-docstring-p) python-fill-docstring-style
)
3998 ;; Add the number of newlines indicated by the selected style
3999 ;; at the start of the docstring.
4000 (goto-char (+ str-start-pos num-quotes
))
4001 (delete-region (point) (progn
4002 (skip-syntax-forward "> ")
4004 (and (car delimiters-style
)
4005 (or (newline (car delimiters-style
)) t
)
4006 ;; Indent only if a newline is added.
4007 (indent-according-to-mode))
4008 ;; Add the number of newlines indicated by the selected style
4009 ;; at the end of the docstring.
4010 (goto-char (if (not (= str-end-pos
(point-max)))
4011 (- str-end-pos num-quotes
)
4013 (delete-region (point) (progn
4014 (skip-syntax-backward "> ")
4016 (and (cdr delimiters-style
)
4017 ;; Add newlines only if string ends.
4018 (not (= str-end-pos
(point-max)))
4019 (or (newline (cdr delimiters-style
)) t
)
4020 ;; Again indent only if a newline is added.
4021 (indent-according-to-mode))))) t
)
4023 (defun python-fill-decorator (&optional _justify
)
4024 "Decorator fill function for `python-fill-paragraph'.
4025 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
4028 (defun python-fill-paren (&optional justify
)
4029 "Paren fill function for `python-fill-paragraph'.
4030 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
4032 (narrow-to-region (progn
4033 (while (python-syntax-context 'paren
)
4034 (goto-char (1- (point))))
4035 (line-beginning-position))
4037 (when (not (python-syntax-context 'paren
))
4039 (when (not (python-syntax-context 'paren
))
4040 (skip-syntax-backward "^)")))
4041 (while (and (python-syntax-context 'paren
)
4043 (goto-char (1+ (point))))
4045 (let ((paragraph-start "\f\\|[ \t]*$")
4046 (paragraph-separate ",")
4047 (fill-paragraph-function))
4048 (goto-char (point-min))
4049 (fill-paragraph justify
))
4052 (python-indent-line)
4053 (goto-char (line-end-position))))
4059 (define-obsolete-variable-alias
4060 'python-use-skeletons
'python-skeleton-autoinsert
"24.3")
4062 (defcustom python-skeleton-autoinsert nil
4063 "Non-nil means template skeletons will be automagically inserted.
4064 This happens when pressing \"if<SPACE>\", for example, to prompt for
4070 (defvar python-skeleton-available
'()
4071 "Internal list of available skeletons.")
4073 (define-abbrev-table 'python-mode-skeleton-abbrev-table
()
4074 "Abbrev table for Python mode skeletons."
4076 ;; Allow / inside abbrevs.
4077 :regexp
"\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
4078 ;; Only expand in code.
4079 :enable-function
(lambda ()
4081 (not (python-syntax-comment-or-string-p))
4082 python-skeleton-autoinsert
)))
4084 (defmacro python-skeleton-define
(name doc
&rest skel
)
4085 "Define a `python-mode' skeleton using NAME DOC and SKEL.
4086 The skeleton will be bound to python-skeleton-NAME and will
4087 be added to `python-mode-skeleton-abbrev-table'."
4088 (declare (indent 2))
4089 (let* ((name (symbol-name name
))
4090 (function-name (intern (concat "python-skeleton-" name
))))
4092 (define-abbrev python-mode-skeleton-abbrev-table
4093 ,name
"" ',function-name
:system t
)
4094 (setq python-skeleton-available
4095 (cons ',function-name python-skeleton-available
))
4096 (define-skeleton ,function-name
4098 (format "Insert %s statement." name
))
4101 (define-abbrev-table 'python-mode-abbrev-table
()
4102 "Abbrev table for Python mode."
4103 :parents
(list python-mode-skeleton-abbrev-table
))
4105 (defmacro python-define-auxiliary-skeleton
(name &optional doc
&rest skel
)
4106 "Define a `python-mode' auxiliary skeleton using NAME DOC and SKEL.
4107 The skeleton will be bound to python-skeleton-NAME."
4108 (declare (indent 2))
4109 (let* ((name (symbol-name name
))
4110 (function-name (intern (concat "python-skeleton--" name
)))
4111 (msg (funcall (if (fboundp 'format-message
) #'format-message
#'format
)
4112 "Add `%s' clause? " name
)))
4115 `(< ,(format "%s:" name
) \n \n
4117 `(define-skeleton ,function-name
4119 (format "Auxiliary skeleton for %s statement." name
))
4121 (unless (y-or-n-p ,msg
)
4125 (python-define-auxiliary-skeleton else
)
4127 (python-define-auxiliary-skeleton except
)
4129 (python-define-auxiliary-skeleton finally
)
4131 (python-skeleton-define if nil
4135 ("other condition, %s: "
4139 '(python-skeleton--else) | ^
)
4141 (python-skeleton-define while nil
4145 '(python-skeleton--else) | ^
)
4147 (python-skeleton-define for nil
4151 '(python-skeleton--else) | ^
)
4153 (python-skeleton-define import nil
4154 "Import from module: "
4155 "from " str
& " " | -
5
4157 ("Identifier: " str
", ") -
2 \n _
)
4159 (python-skeleton-define try nil
4165 "except " str
":" \n
4168 '(python-skeleton--except)
4169 '(python-skeleton--else)
4170 '(python-skeleton--finally) | ^
)
4172 (python-skeleton-define def nil
4174 "def " str
"(" ("Parameter, %s: "
4175 (unless (equal ?\
( (char-before)) ", ")
4177 "\"\"\"" -
"\"\"\"" \n
4180 (python-skeleton-define class nil
4182 "class " str
"(" ("Inheritance, %s: "
4183 (unless (equal ?\
( (char-before)) ", ")
4187 "\"\"\"" -
"\"\"\"" \n
4190 (defun python-skeleton-add-menu-items ()
4191 "Add menu items to Python->Skeletons menu."
4192 (let ((skeletons (sort python-skeleton-available
'string
<)))
4193 (dolist (skeleton skeletons
)
4195 nil
'("Python" "Skeletons")
4197 "Insert %s" (nth 2 (split-string (symbol-name skeleton
) "-")))
4202 (defcustom python-ffap-setup-code
4204 def __FFAP_get_module_path(objstr):
4208 # NameError exceptions are delayed until this point.
4210 module = inspect.getmodule(obj)
4211 filename = module.__file__
4212 ext = os.path.splitext(filename)[1]
4213 if ext in ('.pyc', '.pyo'):
4214 # Point to the source file.
4215 filename = filename[:-1]
4216 if os.path.exists(filename):
4221 "Python code to get a module path."
4225 (defcustom python-ffap-string-code
4226 "__FFAP_get_module_path('''%s''')"
4227 "Python code used to get a string with the path of a module."
4231 (defun python-ffap-module-path (module)
4232 "Function for `ffap-alist' to return path for MODULE."
4234 (and (derived-mode-p 'inferior-python-mode
)
4235 (get-buffer-process (current-buffer)))
4236 (python-shell-get-process))))
4240 (python-shell-send-string-no-output
4242 python-ffap-setup-code
4243 "\nprint (" (format python-ffap-string-code module
) ")")
4245 (unless (zerop (length module-file
))
4246 (python-util-strip-string module-file
))))))
4250 (eval-after-load "ffap"
4252 (push '(python-mode . python-ffap-module-path
) ffap-alist
)
4253 (push '(inferior-python-mode . python-ffap-module-path
) ffap-alist
)))
4258 (defcustom python-check-command
4259 (or (executable-find "pyflakes")
4260 (executable-find "epylint")
4261 "install pyflakes, pylint or something else")
4262 "Command used to check a Python file."
4266 (defcustom python-check-buffer-name
4267 "*Python check: %s*"
4268 "Buffer name used for check commands."
4272 (defvar python-check-custom-command nil
4274 ;; XXX: Avoid `defvar-local' for compat with Emacs<24.3
4275 (make-variable-buffer-local 'python-check-custom-command
)
4277 (defun python-check (command)
4278 "Check a Python file (default current buffer's file).
4279 Runs COMMAND, a shell command, as if by `compile'.
4280 See `python-check-command' for the default."
4282 (list (read-string "Check command: "
4283 (or python-check-custom-command
4284 (concat python-check-command
" "
4285 (shell-quote-argument
4287 (let ((name (buffer-file-name)))
4289 (file-name-nondirectory name
)))
4291 (setq python-check-custom-command command
)
4292 (save-some-buffers (not compilation-ask-about-save
) nil
)
4293 (python-shell-with-environment
4294 (compilation-start command nil
4296 (format python-check-buffer-name command
)))))
4301 (defcustom python-eldoc-setup-code
4302 "def __PYDOC_get_help(obj):
4306 str_type = basestring
4307 argspec_function = inspect.getargspec
4310 argspec_function = inspect.getfullargspec
4311 if isinstance(obj, str_type):
4312 obj = eval(obj, globals())
4313 doc = inspect.getdoc(obj)
4314 if not doc and callable(obj):
4316 if inspect.isclass(obj) and hasattr(obj, '__init__'):
4317 target = obj.__init__
4323 args = inspect.formatargspec(*argspec_function(target))
4325 doc = '{objtype} {name}{args}'.format(
4326 objtype=objtype, name=name, args=args
4329 doc = doc.splitlines()[0]
4333 "Python code to setup documentation retrieval."
4337 (defcustom python-eldoc-string-code
4338 "__PYDOC_get_help('''%s''')"
4339 "Python code used to get a string with the documentation of an object."
4343 (defun python-eldoc--get-symbol-at-point ()
4344 "Get the current symbol for eldoc.
4345 Returns the current symbol handling point within arguments."
4347 (let ((start (python-syntax-context 'paren
)))
4352 (memq (char-syntax (char-after)) '(?\ ?-
)))
4353 ;; Try to adjust to closest symbol if not in one.
4354 (python-util-forward-comment -
1)))
4355 (python-info-current-symbol t
)))
4357 (defun python-eldoc--get-doc-at-point (&optional force-input force-process
)
4358 "Internal implementation to get documentation at point.
4359 If not FORCE-INPUT is passed then what `python-eldoc--get-symbol-at-point'
4360 returns will be used. If not FORCE-PROCESS is passed what
4361 `python-shell-get-process' returns is used."
4362 (let ((process (or force-process
(python-shell-get-process))))
4364 (let* ((input (or force-input
4365 (python-eldoc--get-symbol-at-point)))
4368 ;; Prevent resizing the echo area when iPython is
4369 ;; enabled. Bug#18794.
4370 (python-util-strip-string
4371 (python-shell-send-string-no-output
4373 python-eldoc-setup-code
4374 "\nprint(" (format python-eldoc-string-code input
) ")")
4376 (unless (zerop (length docstring
))
4379 (defvar-local python-eldoc-get-doc t
4380 "Non-nil means eldoc should fetch the documentation
4381 automatically. Set to nil by `python-eldoc-function' if
4382 `python-eldoc-function-timeout-permanent' is non-nil and
4383 `python-eldoc-function' times out.")
4385 (defcustom python-eldoc-function-timeout
1
4386 "Timeout for `python-eldoc-function' in seconds."
4391 (defcustom python-eldoc-function-timeout-permanent t
4392 "Non-nil means that when `python-eldoc-function' times out
4393 `python-eldoc-get-doc' will be set to nil"
4398 (defun python-eldoc-function ()
4399 "`eldoc-documentation-function' for Python.
4400 For this to work as best as possible you should call
4401 `python-shell-send-buffer' from time to time so context in
4402 inferior Python process is updated properly.
4404 If `python-eldoc-function-timeout' seconds elapse before this
4405 function returns then if
4406 `python-eldoc-function-timeout-permanent' is non-nil
4407 `python-eldoc-get-doc' will be set to nil and eldoc will no
4408 longer return the documentation at the point automatically.
4410 Set `python-eldoc-get-doc' to t to reenable eldoc documentation
4412 (when python-eldoc-get-doc
4413 (with-timeout (python-eldoc-function-timeout
4414 (if python-eldoc-function-timeout-permanent
4416 (message "Eldoc echo-area display muted in this buffer, see `python-eldoc-function'")
4417 (setq python-eldoc-get-doc nil
))
4418 (message "`python-eldoc-function' timed out, see `python-eldoc-function-timeout'")))
4419 (python-eldoc--get-doc-at-point))))
4421 (defun python-eldoc-at-point (symbol)
4422 "Get help on SYMBOL using `help'.
4423 Interactively, prompt for symbol."
4425 (let ((symbol (python-eldoc--get-symbol-at-point))
4426 (enable-recursive-minibuffers t
))
4427 (list (read-string (if symbol
4428 (format "Describe symbol (default %s): " symbol
)
4429 "Describe symbol: ")
4431 (message (python-eldoc--get-doc-at-point symbol
)))
4433 (defun python-describe-at-point (symbol process
)
4434 (interactive (list (python-info-current-symbol)
4435 (python-shell-get-process)))
4436 (comint-send-string process
(concat "help('" symbol
"')\n")))
4441 (defun python-hideshow-forward-sexp-function (arg)
4442 "Python specific `forward-sexp' function for `hs-minor-mode'.
4443 Argument ARG is ignored."
4444 arg
; Shut up, byte compiler.
4445 (python-nav-end-of-defun)
4446 (unless (python-info-current-line-empty-p)
4452 (defvar python-imenu-format-item-label-function
4453 'python-imenu-format-item-label
4454 "Imenu function used to format an item label.
4455 It must be a function with two arguments: TYPE and NAME.")
4457 (defvar python-imenu-format-parent-item-label-function
4458 'python-imenu-format-parent-item-label
4459 "Imenu function used to format a parent item label.
4460 It must be a function with two arguments: TYPE and NAME.")
4462 (defvar python-imenu-format-parent-item-jump-label-function
4463 'python-imenu-format-parent-item-jump-label
4464 "Imenu function used to format a parent jump item label.
4465 It must be a function with two arguments: TYPE and NAME.")
4467 (defun python-imenu-format-item-label (type name
)
4468 "Return Imenu label for single node using TYPE and NAME."
4469 (format "%s (%s)" name type
))
4471 (defun python-imenu-format-parent-item-label (type name
)
4472 "Return Imenu label for parent node using TYPE and NAME."
4473 (format "%s..." (python-imenu-format-item-label type name
)))
4475 (defun python-imenu-format-parent-item-jump-label (type _name
)
4476 "Return Imenu label for parent node jump using TYPE and NAME."
4477 (if (string= type
"class")
4478 "*class definition*"
4479 "*function definition*"))
4481 (defun python-imenu--get-defun-type-name ()
4482 "Return defun type and name at current position."
4483 (when (looking-at python-nav-beginning-of-defun-regexp
)
4484 (let ((split (split-string (match-string-no-properties 0))))
4485 (if (= (length split
) 2)
4487 (list (concat (car split
) " " (cadr split
))
4488 (car (last split
)))))))
4490 (defun python-imenu--put-parent (type name pos tree
)
4491 "Add the parent with TYPE, NAME and POS to TREE."
4493 (funcall python-imenu-format-item-label-function type name
))
4495 (funcall python-imenu-format-parent-item-jump-label-function type name
)))
4498 (cons label
(cons (cons jump-label pos
) tree
)))))
4500 (defun python-imenu--build-tree (&optional min-indent prev-indent tree
)
4501 "Recursively build the tree of nested definitions of a node.
4502 Arguments MIN-INDENT, PREV-INDENT and TREE are internal and should
4503 not be passed explicitly unless you know what you are doing."
4504 (setq min-indent
(or min-indent
0)
4505 prev-indent
(or prev-indent python-indent-offset
))
4506 (let* ((pos (python-nav-backward-defun))
4507 (defun-type-name (and pos
(python-imenu--get-defun-type-name)))
4508 (type (car defun-type-name
))
4509 (name (cadr defun-type-name
))
4511 (funcall python-imenu-format-item-label-function type name
)))
4512 (indent (current-indentation))
4513 (children-indent-limit (+ python-indent-offset min-indent
)))
4515 ;; Nothing found, probably near to bobp.
4517 ((<= indent min-indent
)
4518 ;; The current indentation points that this is a parent
4519 ;; node, add it to the tree and stop recursing.
4520 (python-imenu--put-parent type name pos tree
))
4522 (python-imenu--build-tree
4525 (if (<= indent children-indent-limit
)
4526 ;; This lies within the children indent offset range,
4527 ;; so it's a normal child of its parent (i.e., not
4528 ;; a child of a child).
4529 (cons (cons label pos
) tree
)
4530 ;; Oh no, a child of a child?! Fear not, we
4531 ;; know how to roll. We recursively parse these by
4532 ;; swapping prev-indent and min-indent plus adding this
4533 ;; newly found item to a fresh subtree. This works, I
4536 (python-imenu--build-tree
4537 prev-indent indent
(list (cons label pos
)))
4540 (defun python-imenu-create-index ()
4541 "Return tree Imenu alist for the current Python buffer.
4542 Change `python-imenu-format-item-label-function',
4543 `python-imenu-format-parent-item-label-function',
4544 `python-imenu-format-parent-item-jump-label-function' to
4545 customize how labels are formatted."
4546 (goto-char (point-max))
4549 (while (setq tree
(python-imenu--build-tree))
4550 (setq index
(cons tree index
)))
4553 (defun python-imenu-create-flat-index (&optional alist prefix
)
4554 "Return flat outline of the current Python buffer for Imenu.
4555 Optional argument ALIST is the tree to be flattened; when nil
4556 `python-imenu-build-index' is used with
4557 `python-imenu-format-parent-item-jump-label-function'
4558 `python-imenu-format-parent-item-label-function'
4559 `python-imenu-format-item-label-function' set to
4560 (lambda (type name) name)
4561 Optional argument PREFIX is used in recursive calls and should
4562 not be passed explicitly.
4569 (\"decorator\" . 173)
4572 (\"wrapped_f\" . 393))))
4578 (\"decorator\" . 173)
4579 (\"decorator.wrap\" . 353)
4580 (\"decorator.wrapped_f\" . 393))"
4581 ;; Inspired by imenu--flatten-index-alist removed in revno 21853.
4586 (let ((name (if prefix
4587 (concat prefix
"." (car item
))
4590 (cond ((or (numberp pos
) (markerp pos
))
4591 (list (cons name pos
)))
4594 (cons name
(cdar pos
))
4595 (python-imenu-create-flat-index (cddr item
) name
))))))
4597 (let* ((fn (lambda (_type name
) name
))
4598 (python-imenu-format-item-label-function fn
)
4599 (python-imenu-format-parent-item-label-function fn
)
4600 (python-imenu-format-parent-item-jump-label-function fn
))
4601 (python-imenu-create-index))))))
4606 (defun python-info-current-defun (&optional include-type
)
4607 "Return name of surrounding function with Python compatible dotty syntax.
4608 Optional argument INCLUDE-TYPE indicates to include the type of the defun.
4609 This function can be used as the value of `add-log-current-defun-function'
4610 since it returns nil if point is not inside a defun."
4616 (starting-indentation (current-indentation))
4617 (starting-pos (point))
4622 (while (python-nav-beginning-of-defun 1)
4623 (when (save-match-data
4625 (or (not last-indent
)
4626 (< (current-indentation) last-indent
))
4630 ;; If this is the first run, we may add
4631 ;; the current defun at point.
4632 (setq first-run nil
)
4633 (goto-char starting-pos
)
4634 (python-nav-beginning-of-statement)
4635 (beginning-of-line 1)
4637 python-nav-beginning-of-defun-regexp
)))
4641 (+ (current-indentation)
4642 python-indent-offset
)))
4643 (if (< starting-indentation min-indent
)
4644 ;; If the starting indentation is not
4645 ;; within the min defun indent make the
4648 ;; Else go to the end of defun and add
4649 ;; up the current indentation to the
4651 (python-nav-end-of-defun)
4653 (if (>= (current-indentation) min-indent
)
4654 (1+ (current-indentation))
4656 (save-match-data (setq last-indent
(current-indentation)))
4657 (if (or (not include-type
) type
)
4658 (setq names
(cons (match-string-no-properties 1) names
))
4659 (let ((match (split-string (match-string-no-properties 0))))
4660 (setq type
(car match
))
4661 (setq names
(cons (cadr match
) names
)))))
4662 ;; Stop searching ASAP.
4663 (and (= (current-indentation) 0) (throw 'exit t
))))
4665 (concat (and type
(format "%s " type
))
4666 (mapconcat 'identity names
".")))))))
4668 (defun python-info-current-symbol (&optional replace-self
)
4669 "Return current symbol using dotty syntax.
4670 With optional argument REPLACE-SELF convert \"self\" to current
4673 (and (not (python-syntax-comment-or-string-p))
4674 (with-syntax-table python-dotty-syntax-table
4675 (let ((sym (symbol-at-point)))
4677 (substring-no-properties (symbol-name sym
))))))))
4679 (if (not replace-self
)
4681 (let ((current-defun (python-info-current-defun)))
4682 (if (not current-defun
)
4684 (replace-regexp-in-string
4685 (python-rx line-start word-start
"self" word-end ?.
)
4687 (mapconcat 'identity
4688 (butlast (split-string current-defun
"\\."))
4692 (defun python-info-statement-starts-block-p ()
4693 "Return non-nil if current statement opens a block."
4695 (python-nav-beginning-of-statement)
4696 (looking-at (python-rx block-start
))))
4698 (defun python-info-statement-ends-block-p ()
4699 "Return non-nil if point is at end of block."
4700 (let ((end-of-block-pos (save-excursion
4701 (python-nav-end-of-block)))
4702 (end-of-statement-pos (save-excursion
4703 (python-nav-end-of-statement))))
4704 (and end-of-block-pos end-of-statement-pos
4705 (= end-of-block-pos end-of-statement-pos
))))
4707 (defun python-info-beginning-of-statement-p ()
4708 "Return non-nil if point is at beginning of statement."
4709 (= (point) (save-excursion
4710 (python-nav-beginning-of-statement)
4713 (defun python-info-end-of-statement-p ()
4714 "Return non-nil if point is at end of statement."
4715 (= (point) (save-excursion
4716 (python-nav-end-of-statement)
4719 (defun python-info-beginning-of-block-p ()
4720 "Return non-nil if point is at beginning of block."
4721 (and (python-info-beginning-of-statement-p)
4722 (python-info-statement-starts-block-p)))
4724 (defun python-info-end-of-block-p ()
4725 "Return non-nil if point is at end of block."
4726 (and (python-info-end-of-statement-p)
4727 (python-info-statement-ends-block-p)))
4729 (define-obsolete-function-alias
4730 'python-info-closing-block
4731 'python-info-dedenter-opening-block-position
"24.4")
4733 (defun python-info-dedenter-opening-block-position ()
4734 "Return the point of the closest block the current line closes.
4735 Returns nil if point is not on a dedenter statement or no opening
4736 block can be detected. The latter case meaning current file is
4737 likely an invalid python file."
4738 (let ((positions (python-info-dedenter-opening-block-positions))
4739 (indentation (current-indentation))
4741 (while (and (not position
)
4744 (goto-char (car positions
))
4745 (if (<= (current-indentation) indentation
)
4746 (setq position
(car positions
))
4747 (setq positions
(cdr positions
)))))
4750 (defun python-info-dedenter-opening-block-positions ()
4751 "Return points of blocks the current line may close sorted by closer.
4752 Returns nil if point is not on a dedenter statement or no opening
4753 block can be detected. The latter case meaning current file is
4754 likely an invalid python file."
4756 (let ((dedenter-pos (python-info-dedenter-statement-p)))
4758 (goto-char dedenter-pos
)
4759 (let* ((cur-line (line-beginning-position))
4760 (pairs '(("elif" "elif" "if")
4761 ("else" "if" "elif" "except" "for" "while")
4762 ("except" "except" "try")
4763 ("finally" "else" "except" "try")))
4764 (dedenter (match-string-no-properties 0))
4765 (possible-opening-blocks (cdr (assoc-string dedenter pairs
)))
4766 (collected-indentations)
4769 (while (python-nav--syntactically
4771 (re-search-backward (python-rx block-start
) nil t
))
4773 (let ((indentation (current-indentation)))
4774 (when (and (not (memq indentation collected-indentations
))
4775 (or (not collected-indentations
)
4776 (< indentation
(apply #'min collected-indentations
)))
4777 ;; There must be no line with indentation
4778 ;; smaller than `indentation' (except for
4779 ;; blank lines) between the found opening
4780 ;; block and the current line, otherwise it
4781 ;; is not an opening block.
4784 (let ((no-back-indent t
))
4786 (while (and (< (point) cur-line
)
4787 (setq no-back-indent
4788 (or (> (current-indentation) indentation
)
4789 (python-info-current-line-empty-p))))
4792 (setq collected-indentations
4793 (cons indentation collected-indentations
))
4794 (when (member (match-string-no-properties 0)
4795 possible-opening-blocks
)
4796 (setq opening-blocks
(cons (point) opening-blocks
))))
4797 (when (zerop indentation
)
4798 (throw 'exit nil
)))))
4800 (nreverse opening-blocks
))))))
4802 (define-obsolete-function-alias
4803 'python-info-closing-block-message
4804 'python-info-dedenter-opening-block-message
"24.4")
4806 (defun python-info-dedenter-opening-block-message ()
4807 "Message the first line of the block the current statement closes."
4808 (let ((point (python-info-dedenter-opening-block-position)))
4810 (message "Closes %s" (save-excursion
4813 (point) (line-end-position)))))))
4815 (defun python-info-dedenter-statement-p ()
4816 "Return point if current statement is a dedenter.
4817 Sets `match-data' to the keyword that starts the dedenter
4820 (python-nav-beginning-of-statement)
4821 (when (and (not (python-syntax-context-type))
4822 (looking-at (python-rx dedenter
)))
4825 (defun python-info-line-ends-backslash-p (&optional line-number
)
4826 "Return non-nil if current line ends with backslash.
4827 With optional argument LINE-NUMBER, check that line instead."
4830 (python-util-goto-line line-number
))
4831 (while (and (not (eobp))
4832 (goto-char (line-end-position))
4833 (python-syntax-context 'paren
)
4834 (not (equal (char-before (point)) ?
\\)))
4836 (when (equal (char-before) ?
\\)
4839 (defun python-info-beginning-of-backslash (&optional line-number
)
4840 "Return the point where the backslashed line starts.
4841 Optional argument LINE-NUMBER forces the line number to check against."
4844 (python-util-goto-line line-number
))
4845 (when (python-info-line-ends-backslash-p)
4846 (while (save-excursion
4847 (goto-char (line-beginning-position))
4848 (python-syntax-context 'paren
))
4850 (back-to-indentation)
4853 (defun python-info-continuation-line-p ()
4854 "Check if current line is continuation of another.
4855 When current line is continuation of another return the point
4856 where the continued line ends."
4858 (let* ((context-type (progn
4859 (back-to-indentation)
4860 (python-syntax-context-type)))
4861 (line-start (line-number-at-pos))
4862 (context-start (when context-type
4863 (python-syntax-context context-type
))))
4864 (cond ((equal context-type
'paren
)
4865 ;; Lines inside a paren are always a continuation line
4866 ;; (except the first one).
4867 (python-util-forward-comment -
1)
4869 ((member context-type
'(string comment
))
4870 ;; move forward an roll again
4871 (goto-char context-start
)
4872 (python-util-forward-comment)
4873 (python-info-continuation-line-p))
4875 ;; Not within a paren, string or comment, the only way
4876 ;; we are dealing with a continuation line is that
4877 ;; previous line contains a backslash, and this can
4878 ;; only be the previous line from current
4879 (back-to-indentation)
4880 (python-util-forward-comment -
1)
4881 (when (and (equal (1- line-start
) (line-number-at-pos))
4882 (python-info-line-ends-backslash-p))
4883 (point-marker)))))))
4885 (defun python-info-block-continuation-line-p ()
4886 "Return non-nil if current line is a continuation of a block."
4888 (when (python-info-continuation-line-p)
4890 (back-to-indentation)
4891 (when (looking-at (python-rx block-start
))
4894 (defun python-info-assignment-statement-p (&optional current-line-only
)
4895 "Check if current line is an assignment.
4896 With argument CURRENT-LINE-ONLY is non-nil, don't follow any
4897 continuations, just check the if current line is an assignment."
4900 (if current-line-only
4901 (back-to-indentation)
4902 (python-nav-beginning-of-statement))
4904 (re-search-forward (python-rx not-simple-operator
4906 (group not-simple-operator
))
4907 (line-end-position) t
)
4910 ;; The assignment operator should not be inside a string.
4911 (backward-char (length (match-string-no-properties 1)))
4912 (setq found
(not (python-syntax-context-type)))))
4914 (skip-syntax-forward " ")
4917 ;; TODO: rename to clarify this is only for the first continuation
4918 ;; line or remove it and move its body to `python-indent-context'.
4919 (defun python-info-assignment-continuation-line-p ()
4920 "Check if current line is the first continuation of an assignment.
4921 When current line is continuation of another with an assignment
4922 return the point of the first non-blank character after the
4925 (when (python-info-continuation-line-p)
4927 (python-info-assignment-statement-p t
))))
4929 (defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss
)
4930 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS."
4931 (and (not (python-syntax-context-type (or syntax-ppss
(syntax-ppss))))
4933 (beginning-of-line 1)
4934 (looking-at python-nav-beginning-of-defun-regexp
))))
4936 (defun python-info-current-line-comment-p ()
4937 "Return non-nil if current line is a comment line."
4939 (or (char-after (+ (line-beginning-position) (current-indentation))) ?_
)
4942 (defun python-info-current-line-empty-p ()
4943 "Return non-nil if current line is empty, ignoring whitespace."
4945 (beginning-of-line 1)
4947 (python-rx line-start
(* whitespace
)
4948 (group (* not-newline
))
4949 (* whitespace
) line-end
))
4950 (string-equal "" (match-string-no-properties 1))))
4952 (defun python-info-docstring-p (&optional syntax-ppss
)
4953 "Return non-nil if point is in a docstring.
4954 When optional argument SYNTAX-PPSS is given, use that instead of
4955 point's current `syntax-ppss'."
4956 ;;; https://www.python.org/dev/peps/pep-0257/#what-is-a-docstring
4958 (when (and syntax-ppss
(python-syntax-context 'string syntax-ppss
))
4959 (goto-char (nth 8 syntax-ppss
)))
4960 (python-nav-beginning-of-statement)
4962 (indentation (current-indentation))
4963 (backward-sexp-point)
4964 (re (concat "[uU]?[rR]?"
4965 (python-rx string-delimiter
))))
4967 (not (python-info-assignment-statement-p))
4969 ;; Allow up to two consecutive docstrings only.
4972 (let (last-backward-sexp-point)
4973 (while (save-excursion
4974 (python-nav-backward-sexp)
4975 (setq backward-sexp-point
(point))
4976 (and (= indentation
(current-indentation))
4977 ;; Make sure we're always moving point.
4978 ;; If we get stuck in the same position
4979 ;; on consecutive loop iterations,
4981 (prog1 (not (eql last-backward-sexp-point
4982 backward-sexp-point
))
4983 (setq last-backward-sexp-point
4984 backward-sexp-point
))
4986 (concat "[uU]?[rR]?"
4987 (python-rx string-delimiter
)))))
4988 ;; Previous sexp was a string, restore point.
4989 (goto-char backward-sexp-point
)
4992 (python-util-forward-comment -
1)
4993 (python-nav-beginning-of-statement)
4995 ((python-info-assignment-statement-p) t
)
4996 ((python-info-looking-at-beginning-of-defun))
4999 (defun python-info-encoding-from-cookie ()
5000 "Detect current buffer's encoding from its coding cookie.
5001 Returns the encoding as a symbol."
5002 (let ((first-two-lines
5006 (goto-char (point-min))
5008 (buffer-substring-no-properties
5011 (when (string-match (python-rx coding-cookie
) first-two-lines
)
5012 (intern (match-string-no-properties 1 first-two-lines
)))))
5014 (defun python-info-encoding ()
5015 "Return encoding for file.
5016 Try `python-info-encoding-from-cookie', if none is found then
5018 ;; If no encoding is defined, then it's safe to use UTF-8: Python 2
5019 ;; uses ASCII as default while Python 3 uses UTF-8. This means that
5020 ;; in the worst case scenario python.el will make things work for
5021 ;; Python 2 files with unicode data and no encoding defined.
5022 (or (python-info-encoding-from-cookie)
5026 ;;; Utility functions
5028 (defun python-util-goto-line (line-number)
5029 "Move point to LINE-NUMBER."
5030 (goto-char (point-min))
5031 (forward-line (1- line-number
)))
5033 ;; Stolen from org-mode
5034 (defun python-util-clone-local-variables (from-buffer &optional regexp
)
5035 "Clone local variables from FROM-BUFFER.
5036 Optional argument REGEXP selects variables to clone and defaults
5040 (and (symbolp (car pair
))
5041 (string-match (or regexp
"^python-")
5042 (symbol-name (car pair
)))
5043 (set (make-local-variable (car pair
))
5045 (buffer-local-variables from-buffer
)))
5047 (defvar comint-last-prompt-overlay
) ; Shut up, byte compiler.
5049 (defun python-util-comint-last-prompt ()
5050 "Return comint last prompt overlay start and end.
5051 This is for compatibility with Emacs < 24.4."
5052 (cond ((bound-and-true-p comint-last-prompt-overlay
)
5053 (cons (overlay-start comint-last-prompt-overlay
)
5054 (overlay-end comint-last-prompt-overlay
)))
5055 ((bound-and-true-p comint-last-prompt
)
5059 (defun python-util-forward-comment (&optional direction
)
5060 "Python mode specific version of `forward-comment'.
5061 Optional argument DIRECTION defines the direction to move to."
5062 (let ((comment-start (python-syntax-context 'comment
))
5063 (factor (if (< (or direction
0) 0)
5067 (goto-char comment-start
))
5068 (forward-comment factor
)))
5070 (defun python-util-list-directories (directory &optional predicate max-depth
)
5071 "List DIRECTORY subdirs, filtered by PREDICATE and limited by MAX-DEPTH.
5072 Argument PREDICATE defaults to `identity' and must be a function
5073 that takes one argument (a full path) and returns non-nil for
5074 allowed files. When optional argument MAX-DEPTH is non-nil, stop
5075 searching when depth is reached, else don't limit."
5076 (let* ((dir (expand-file-name directory
))
5077 (dir-length (length dir
))
5078 (predicate (or predicate
#'identity
))
5079 (to-scan (list dir
))
5082 (let ((current-dir (car to-scan
)))
5083 (when (funcall predicate current-dir
)
5084 (setq tally
(cons current-dir tally
)))
5085 (setq to-scan
(append (cdr to-scan
)
5086 (python-util-list-files
5087 current-dir
#'file-directory-p
)
5089 (when (and max-depth
5091 (length (split-string
5092 (substring current-dir dir-length
)
5094 (setq to-scan nil
))))
5097 (defun python-util-list-files (dir &optional predicate
)
5098 "List files in DIR, filtering with PREDICATE.
5099 Argument PREDICATE defaults to `identity' and must be a function
5100 that takes one argument (a full path) and returns non-nil for
5102 (let ((dir-name (file-name-as-directory dir
)))
5104 (mapcar (lambda (file-name)
5105 (let ((full-file-name (expand-file-name file-name dir-name
)))
5107 (not (member file-name
'("." "..")))
5108 (funcall (or predicate
#'identity
) full-file-name
))
5109 (list full-file-name
))))
5110 (directory-files dir-name
)))))
5112 (defun python-util-list-packages (dir &optional max-depth
)
5113 "List packages in DIR, limited by MAX-DEPTH.
5114 When optional argument MAX-DEPTH is non-nil, stop searching when
5115 depth is reached, else don't limit."
5116 (let* ((dir (expand-file-name dir
))
5117 (parent-dir (file-name-directory
5118 (directory-file-name
5119 (file-name-directory
5120 (file-name-as-directory dir
)))))
5121 (subpath-length (length parent-dir
)))
5124 (replace-regexp-in-string
5125 (rx (or ?
\\ ?
/)) "." (substring file-name subpath-length
)))
5126 (python-util-list-directories
5127 (directory-file-name dir
)
5129 (file-exists-p (expand-file-name "__init__.py" dir
)))
5132 (defun python-util-popn (lst n
)
5133 "Return LST first N elements.
5134 N should be an integer, when negative its opposite is used.
5135 When N is bigger than the length of LST, the list is
5137 (let* ((n (min (abs n
)))
5143 (setq acc
(cons (car lst
) acc
)
5148 (defun python-util-strip-string (string)
5149 "Strip STRING whitespace and newlines from end and beginning."
5150 (replace-regexp-in-string
5151 (rx (or (: string-start
(* (any whitespace ?
\r ?
\n)))
5152 (: (* (any whitespace ?
\r ?
\n)) string-end
)))
5156 (defun python-util-valid-regexp-p (regexp)
5157 "Return non-nil if REGEXP is valid."
5158 (ignore-errors (string-match regexp
"") t
))
5161 ;;; Flymake integration
5163 (defgroup python-flymake nil
5164 "Integration between Python and Flymake."
5166 :link
'(custom-group-link :tag
"Flymake" flymake
)
5169 (defcustom python-flymake-command
'("pyflakes")
5170 "The external tool that will be used to perform the syntax check.
5171 This is a non empty list of strings, the checker tool possibly followed by
5172 required arguments. Once launched it will receive the Python source to be
5173 checked as its standard input.
5174 To use `flake8' you would set this to (\"flake8\" \"-\")."
5176 :group
'python-flymake
5177 :type
'(repeat string
))
5179 ;; The default regexp accomodates for older pyflakes, which did not
5180 ;; report the column number, and at the same time it's compatible with
5181 ;; flake8 output, although it may be redefined to explicitly match the
5183 (defcustom python-flymake-command-output-pattern
5185 "^\\(?:<?stdin>?\\):\\(?1:[0-9]+\\):\\(?:\\(?2:[0-9]+\\):\\)? \\(?3:.*\\)$"
5187 "Specify how to parse the output of `python-flymake-command'.
5188 The value has the form (REGEXP LINE COLUMN TYPE MESSAGE): if
5189 REGEXP matches, the LINE'th subexpression gives the line number,
5190 the COLUMN'th subexpression gives the column number on that line,
5191 the TYPE'th subexpression gives the type of the message and the
5192 MESSAGE'th gives the message text itself.
5194 If COLUMN or TYPE are nil or that index didn't match, that
5195 information is not present on the matched line and a default will
5198 :group
'python-flymake
5200 (integer :tag
"Line's index")
5202 (const :tag
"No column" nil
)
5203 (integer :tag
"Column's index"))
5205 (const :tag
"No type" nil
)
5206 (integer :tag
"Type's index"))
5207 (integer :tag
"Message's index")))
5209 (defcustom python-flymake-msg-alist
5210 '(("\\(^redefinition\\|.*unused.*\\|used$\\)" .
:warning
))
5211 "Alist used to associate messages to their types.
5212 Each element should be a cons-cell (REGEXP . TYPE), where TYPE
5213 should be a diagnostic type symbol like `:error', `:warning' or
5214 `:note'. For example, when using `flake8' a possible
5215 configuration could be:
5217 ((\"\\(^redefinition\\|.*unused.*\\|used$\\)\" . :warning)
5218 (\"^E999\" . :error)
5219 (\"^[EW][0-9]+\" . :note))
5221 By default messages are considered errors."
5223 :group
'python-flymake
5224 :type
`(alist :key-type
(regexp)
5225 :value-type
(symbol)))
5227 (defvar-local python--flymake-proc nil
)
5229 (defun python--flymake-parse-output (source proc report-fn
)
5230 "Collect diagnostics parsing checker tool's output line by line."
5231 (let ((rx (nth 0 python-flymake-command-output-pattern
))
5232 (lineidx (nth 1 python-flymake-command-output-pattern
))
5233 (colidx (nth 2 python-flymake-command-output-pattern
))
5234 (typeidx (nth 3 python-flymake-command-output-pattern
))
5235 (msgidx (nth 4 python-flymake-command-output-pattern
)))
5236 (with-current-buffer (process-buffer proc
)
5237 (goto-char (point-min))
5239 while
(search-forward-regexp rx nil t
)
5240 for msg
= (match-string msgidx
)
5241 for
(beg . end
) = (flymake-diag-region
5244 (match-string lineidx
))
5246 (match-string colidx
)
5248 (match-string colidx
))))
5249 for type
= (or (and typeidx
5250 (match-string typeidx
)
5252 (match-string typeidx
)
5253 python-flymake-msg-alist
5256 python-flymake-msg-alist
5259 collect
(flymake-make-diagnostic
5260 source beg end type msg
)
5262 finally
(funcall report-fn diags
)))))
5264 (defun python-flymake (report-fn &rest _args
)
5265 "Flymake backend for Python.
5266 This backend uses `python-flymake-command' (which see) to launch a process
5267 that is passed the current buffer's content via stdin.
5268 REPORT-FN is Flymake's callback function."
5269 (unless (executable-find (car python-flymake-command
))
5270 (error "Cannot find a suitable checker"))
5272 (when (process-live-p python--flymake-proc
)
5273 (kill-process python--flymake-proc
))
5275 (let ((source (current-buffer)))
5278 (setq python--flymake-proc
5280 :name
"python-flymake"
5282 :connection-type
'pipe
5283 :buffer
(generate-new-buffer " *python-flymake*")
5284 :command python-flymake-command
5286 (lambda (proc _event
)
5287 (when (eq 'exit
(process-status proc
))
5289 (when (with-current-buffer source
5290 (eq proc python--flymake-proc
))
5291 (python--flymake-parse-output source proc report-fn
))
5292 (kill-buffer (process-buffer proc
)))))))
5293 (process-send-region python--flymake-proc
(point-min) (point-max))
5294 (process-send-eof python--flymake-proc
))))
5297 (defun python-electric-pair-string-delimiter ()
5298 (when (and electric-pair-mode
5299 (memq last-command-event
'(?
\" ?
\'))
5301 (while (eq (char-before (- (point) count
)) last-command-event
)
5304 (eq (char-after) last-command-event
))
5305 (save-excursion (insert (make-string 2 last-command-event
)))))
5307 (defvar electric-indent-inhibit
)
5308 (defvar prettify-symbols-alist
)
5311 (define-derived-mode python-mode prog-mode
"Python"
5312 "Major mode for editing Python files.
5314 \\{python-mode-map}"
5315 (set (make-local-variable 'tab-width
) 8)
5316 (set (make-local-variable 'indent-tabs-mode
) nil
)
5318 (set (make-local-variable 'comment-start
) "# ")
5319 (set (make-local-variable 'comment-start-skip
) "#+\\s-*")
5321 (set (make-local-variable 'parse-sexp-lookup-properties
) t
)
5322 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
5324 (set (make-local-variable 'forward-sexp-function
)
5325 'python-nav-forward-sexp
)
5327 (set (make-local-variable 'font-lock-defaults
)
5328 '(python-font-lock-keywords
5330 (font-lock-syntactic-face-function
5331 . python-font-lock-syntactic-face-function
)))
5333 (set (make-local-variable 'syntax-propertize-function
)
5334 python-syntax-propertize-function
)
5336 (set (make-local-variable 'indent-line-function
)
5337 #'python-indent-line-function
)
5338 (set (make-local-variable 'indent-region-function
) #'python-indent-region
)
5339 ;; Because indentation is not redundant, we cannot safely reindent code.
5340 (set (make-local-variable 'electric-indent-inhibit
) t
)
5341 (set (make-local-variable 'electric-indent-chars
)
5342 (cons ?
: electric-indent-chars
))
5344 ;; Add """ ... """ pairing to electric-pair-mode.
5345 (add-hook 'post-self-insert-hook
5346 #'python-electric-pair-string-delimiter
'append t
)
5348 (set (make-local-variable 'paragraph-start
) "\\s-*$")
5349 (set (make-local-variable 'fill-paragraph-function
)
5350 #'python-fill-paragraph
)
5352 (set (make-local-variable 'beginning-of-defun-function
)
5353 #'python-nav-beginning-of-defun
)
5354 (set (make-local-variable 'end-of-defun-function
)
5355 #'python-nav-end-of-defun
)
5357 (add-hook 'completion-at-point-functions
5358 #'python-completion-at-point nil
'local
)
5360 (add-hook 'post-self-insert-hook
5361 #'python-indent-post-self-insert-function
'append
'local
)
5363 (set (make-local-variable 'imenu-create-index-function
)
5364 #'python-imenu-create-index
)
5366 (set (make-local-variable 'add-log-current-defun-function
)
5367 #'python-info-current-defun
)
5369 (add-hook 'which-func-functions
#'python-info-current-defun
nil t
)
5371 (set (make-local-variable 'skeleton-further-elements
)
5373 (< '(backward-delete-char-untabify (min python-indent-offset
5375 (^
'(- (1+ (current-indentation))))))
5377 (if (null eldoc-documentation-function
)
5379 (set (make-local-variable 'eldoc-documentation-function
)
5380 #'python-eldoc-function
)
5381 (add-function :before-until
(local 'eldoc-documentation-function
)
5382 #'python-eldoc-function
))
5385 'hs-special-modes-alist
5387 "\\s-*\\_<\\(?:def\\|class\\)\\_>"
5388 ;; Use the empty string as end regexp so it doesn't default to
5389 ;; "\\s)". This way parens at end of defun are properly hidden.
5392 python-hideshow-forward-sexp-function
5395 (set (make-local-variable 'outline-regexp
)
5396 (python-rx (* space
) block-start
))
5397 (set (make-local-variable 'outline-heading-end-regexp
) ":[^\n]*\n")
5398 (set (make-local-variable 'outline-level
)
5400 "`outline-level' function for Python mode."
5401 (1+ (/ (current-indentation) python-indent-offset
))))
5403 (set (make-local-variable 'prettify-symbols-alist
)
5404 python-prettify-symbols-alist
)
5406 (python-skeleton-add-menu-items)
5408 (make-local-variable 'python-shell-internal-buffer
)
5410 (when python-indent-guess-indent-offset
5411 (python-indent-guess-indent-offset))
5413 (add-hook 'flymake-diagnostic-functions
#'python-flymake nil t
))
5419 ;; indent-tabs-mode: nil
5422 ;;; python.el ends here