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