1 ;;; python.el --- Python's flying circus support for Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 2003-2014 Free Software Foundation, Inc.
5 ;; Author: Fabián E. Gallina <fabian@anue.biz>
6 ;; URL: https://github.com/fgallina/python.el
8 ;; Maintainer: emacs-devel@gnu.org
10 ;; Keywords: languages
12 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
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.
38 ;; Syntax highlighting: Fontification of code is provided and supports
39 ;; python's triple quoted strings properly.
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.
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':
62 ;; (add-hook 'python-mode-hook
63 ;; (lambda () (setq forward-sexp-function nil)))
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.
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:
77 ;; (setq python-shell-interpreter "ipython"
78 ;; python-shell-interpreter-args "-i")
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.
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
93 ;; (setq python-shell-interpreter "C:\\Python27\\python.exe"
94 ;; python-shell-interpreter-args
95 ;; "-i C:\\Python27\\Scripts\\ipython-script.py")
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'.
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.
121 ;; Shell completion: hitting tab will try to complete the current
122 ;; word. Shell completion is implemented in such way that if you
123 ;; change the `python-shell-interpreter' it should be possible to
124 ;; integrate custom logic to calculate completions. To achieve this
125 ;; you just need to set `python-shell-completion-setup-code' and
126 ;; `python-shell-completion-string-code'. The default provided code,
127 ;; enables autocompletion for both CPython and IPython (and ideally
128 ;; any readline based Python shell). This code depends on the
129 ;; readline module, so if you are using some Operating System that
130 ;; bundles Python without it (like Windows), installing pyreadline
131 ;; from URL `http://ipython.scipy.org/moin/PyReadline/Intro' should
132 ;; suffice. To troubleshoot why you are not getting any completions
133 ;; you can try the following in your Python shell:
135 ;; >>> import readline, rlcompleter
137 ;; If you see an error, then you need to either install pyreadline or
138 ;; setup custom code that avoids that dependency.
140 ;; Shell virtualenv support: The shell also contains support for
141 ;; virtualenvs and other special environment modifications thanks to
142 ;; `python-shell-process-environment' and `python-shell-exec-path'.
143 ;; These two variables allows you to modify execution paths and
144 ;; environment variables to make easy for you to setup virtualenv rules
145 ;; or behavior modifications when running shells. Here is an example
146 ;; of how to make shell processes to be run using the /path/to/env/
149 ;; (setq python-shell-process-environment
151 ;; (format "PATH=%s" (mapconcat
154 ;; (cons (getenv "PATH")
155 ;; '("/path/to/env/bin/")))
157 ;; "VIRTUAL_ENV=/path/to/env/"))
158 ;; (python-shell-exec-path . ("/path/to/env/bin/"))
160 ;; Since the above is cumbersome and can be programmatically
161 ;; calculated, the variable `python-shell-virtualenv-root' is
162 ;; provided. When this variable is set with the path of the
163 ;; virtualenv to use, `process-environment' and `exec-path' get proper
164 ;; values in order to run shells inside the specified virtualenv. So
165 ;; the following will achieve the same as the previous example:
167 ;; (setq python-shell-virtualenv-root "/path/to/env/")
169 ;; Also the `python-shell-extra-pythonpaths' variable have been
170 ;; introduced as simple way of adding paths to the PYTHONPATH without
171 ;; affecting existing values.
173 ;; Shell package support: you can enable a package in the current
174 ;; shell so that relative imports work properly using the
175 ;; `python-shell-package-enable' command.
177 ;; Shell syntax highlighting: when enabled current input in shell is
178 ;; highlighted. The variable `python-shell-font-lock-enable' controls
179 ;; activation of this feature globally when shells are started.
180 ;; Activation/deactivation can be also controlled on the fly via the
181 ;; `python-shell-font-lock-toggle' command.
183 ;; Pdb tracking: when you execute a block of code that contains some
184 ;; call to pdb (or ipdb) it will prompt the block of code and will
185 ;; follow the execution of pdb marking the current line with an arrow.
187 ;; Symbol completion: you can complete the symbol at point. It uses
188 ;; the shell completion in background so you should run
189 ;; `python-shell-send-buffer' from time to time to get better results.
191 ;; Skeletons: skeletons are provided for simple inserting of things like class,
192 ;; def, for, import, if, try, and while. These skeletons are
193 ;; integrated with abbrev. If you have `abbrev-mode' activated and
194 ;; `python-skeleton-autoinsert' is set to t, then whenever you type
195 ;; the name of any of those defined and hit SPC, they will be
196 ;; automatically expanded. As an alternative you can use the defined
197 ;; skeleton commands: `python-skeleton-<foo>'.
199 ;; FFAP: You can find the filename for a given module when using ffap
200 ;; out of the box. This feature needs an inferior python shell
203 ;; Code check: Check the current file for errors with `python-check'
204 ;; using the program defined in `python-check-command'.
206 ;; Eldoc: returns documentation for object at point by using the
207 ;; inferior python subprocess to inspect its documentation. As you
208 ;; might guessed you should run `python-shell-send-buffer' from time
209 ;; to time to get better results too.
211 ;; Imenu: There are two index building functions to be used as
212 ;; `imenu-create-index-function': `python-imenu-create-index' (the
213 ;; default one, builds the alist in form of a tree) and
214 ;; `python-imenu-create-flat-index'. See also
215 ;; `python-imenu-format-item-label-function',
216 ;; `python-imenu-format-parent-item-label-function',
217 ;; `python-imenu-format-parent-item-jump-label-function' variables for
218 ;; changing the way labels are formatted in the tree version.
220 ;; If you used python-mode.el you may miss auto-indentation when
221 ;; inserting newlines. To achieve the same behavior you have two
224 ;; 1) Enable the minor-mode `electric-indent-mode' (enabled by
225 ;; default) and use RET. If this mode is disabled use
226 ;; `newline-and-indent', bound to C-j.
228 ;; 2) Add the following hook in your .emacs:
230 ;; (add-hook 'python-mode-hook
232 ;; (define-key python-mode-map "\C-m" 'newline-and-indent)))
234 ;; I'd recommend the first one since you'll get the same behavior for
235 ;; all modes out-of-the-box.
239 ;; Add this to your .emacs:
241 ;; (add-to-list 'load-path "/folder/containing/file")
248 (require 'ansi-color)
253 ;; Avoid compiler warnings
254 (defvar view-return-to-alist)
255 (defvar compilation-error-regexp-alist)
256 (defvar outline-heading-end-regexp)
258 (autoload 'comint-mode "comint")
261 (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode))
263 (add-to-list 'interpreter-mode-alist (cons (purecopy "python[0-9.]*") 'python-mode))
266 "Python Language's flying circus support for Emacs."
269 :link '(emacs-commentary-link "python"))
274 (defvar python-mode-map
275 (let ((map (make-sparse-keymap)))
277 (define-key map [remap backward-sentence] 'python-nav-backward-block)
278 (define-key map [remap forward-sentence] 'python-nav-forward-block)
279 (define-key map [remap backward-up-list] 'python-nav-backward-up-list)
280 (define-key map "\C-c\C-j" 'imenu)
282 (define-key map "\177" 'python-indent-dedent-line-backspace)
283 (define-key map (kbd "<backtab>") 'python-indent-dedent-line)
284 (define-key map "\C-c<" 'python-indent-shift-left)
285 (define-key map "\C-c>" 'python-indent-shift-right)
287 (define-key map "\C-c\C-tc" 'python-skeleton-class)
288 (define-key map "\C-c\C-td" 'python-skeleton-def)
289 (define-key map "\C-c\C-tf" 'python-skeleton-for)
290 (define-key map "\C-c\C-ti" 'python-skeleton-if)
291 (define-key map "\C-c\C-tm" 'python-skeleton-import)
292 (define-key map "\C-c\C-tt" 'python-skeleton-try)
293 (define-key map "\C-c\C-tw" 'python-skeleton-while)
295 (define-key map "\C-c\C-p" 'run-python)
296 (define-key map "\C-c\C-s" 'python-shell-send-string)
297 (define-key map "\C-c\C-r" 'python-shell-send-region)
298 (define-key map "\C-\M-x" 'python-shell-send-defun)
299 (define-key map "\C-c\C-c" 'python-shell-send-buffer)
300 (define-key map "\C-c\C-l" 'python-shell-send-file)
301 (define-key map "\C-c\C-z" 'python-shell-switch-to-shell)
302 ;; Some util commands
303 (define-key map "\C-c\C-v" 'python-check)
304 (define-key map "\C-c\C-f" 'python-eldoc-at-point)
306 (substitute-key-definition 'complete-symbol 'completion-at-point
308 (easy-menu-define python-menu map "Python Mode menu"
310 :help "Python-specific Features"
311 ["Shift region left" python-indent-shift-left :active mark-active
312 :help "Shift region left by a single indentation step"]
313 ["Shift region right" python-indent-shift-right :active mark-active
314 :help "Shift region right by a single indentation step"]
316 ["Start of def/class" beginning-of-defun
317 :help "Go to start of outermost definition around point"]
318 ["End of def/class" end-of-defun
319 :help "Go to end of definition around point"]
320 ["Mark def/class" mark-defun
321 :help "Mark outermost definition around point"]
322 ["Jump to def/class" imenu
323 :help "Jump to a class or function definition"]
327 ["Start interpreter" run-python
328 :help "Run inferior Python process in a separate buffer"]
329 ["Switch to shell" python-shell-switch-to-shell
330 :help "Switch to running inferior Python process"]
331 ["Eval string" python-shell-send-string
332 :help "Eval string in inferior Python session"]
333 ["Eval buffer" python-shell-send-buffer
334 :help "Eval buffer in inferior Python session"]
335 ["Eval region" python-shell-send-region
336 :help "Eval region in inferior Python session"]
337 ["Eval defun" python-shell-send-defun
338 :help "Eval defun in inferior Python session"]
339 ["Eval file" python-shell-send-file
340 :help "Eval file in inferior Python session"]
341 ["Debugger" pdb :help "Run pdb under GUD"]
343 ["Check file" python-check
344 :help "Check file for errors"]
345 ["Help on symbol" python-eldoc-at-point
346 :help "Get help on symbol at point"]
347 ["Complete symbol" completion-at-point
348 :help "Complete symbol before point"]))
350 "Keymap for `python-mode'.")
353 ;;; Python specialized rx
356 (defconst python-rx-constituents
357 `((block-start . ,(rx symbol-start
358 (or "def" "class" "if" "elif" "else" "try"
359 "except" "finally" "for" "while" "with")
361 (dedenter . ,(rx symbol-start
362 (or "elif" "else" "except" "finally")
364 (block-ender . ,(rx symbol-start
366 "break" "continue" "pass" "raise" "return")
368 (decorator . ,(rx line-start (* space) ?@ (any letter ?_)
370 (defun . ,(rx symbol-start (or "def" "class") symbol-end))
371 (if-name-main . ,(rx line-start "if" (+ space) "__name__"
372 (+ space) "==" (+ space)
373 (any ?' ?\") "__main__" (any ?' ?\")
375 (symbol-name . ,(rx (any letter ?_) (* (any word ?_))))
376 (open-paren . ,(rx (or "{" "[" "(")))
377 (close-paren . ,(rx (or "}" "]" ")")))
378 (simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))
379 ;; FIXME: rx should support (not simple-operator).
380 (not-simple-operator . ,(rx
382 (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))))
383 ;; FIXME: Use regexp-opt.
384 (operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
385 "=" "%" "**" "//" "<<" ">>" "<=" "!="
386 "==" ">=" "is" "not")))
387 ;; FIXME: Use regexp-opt.
388 (assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**="
389 ">>=" "<<=" "&=" "^=" "|=")))
390 (string-delimiter . ,(rx (and
391 ;; Match even number of backslashes.
392 (or (not (any ?\\ ?\' ?\")) point
393 ;; Quotes might be preceded by a escaped quote.
394 (and (or (not (any ?\\)) point) ?\\
395 (* ?\\ ?\\) (any ?\' ?\")))
397 ;; Match single or triple quotes of any kind.
398 (group (or "\"" "\"\"\"" "'" "'''"))))))
399 "Additional Python specific sexps for `python-rx'")
401 (defmacro python-rx (&rest regexps)
402 "Python mode specialized rx macro.
403 This variant of `rx' supports common Python named REGEXPS."
404 (let ((rx-constituents (append python-rx-constituents rx-constituents)))
405 (cond ((null regexps)
408 (rx-to-string `(and ,@regexps) t))
410 (rx-to-string (car regexps) t))))))
413 ;;; Font-lock and syntax
416 (defun python-syntax--context-compiler-macro (form type &optional syntax-ppss)
419 `(let ((ppss (or ,syntax-ppss (syntax-ppss))))
420 (and (nth 4 ppss) (nth 8 ppss))))
422 `(let ((ppss (or ,syntax-ppss (syntax-ppss))))
423 (and (nth 3 ppss) (nth 8 ppss))))
425 `(nth 1 (or ,syntax-ppss (syntax-ppss))))
428 (defun python-syntax-context (type &optional syntax-ppss)
429 "Return non-nil if point is on TYPE using SYNTAX-PPSS.
430 TYPE can be `comment', `string' or `paren'. It returns the start
431 character address of the specified TYPE."
432 (declare (compiler-macro python-syntax--context-compiler-macro))
433 (let ((ppss (or syntax-ppss (syntax-ppss))))
435 (`comment (and (nth 4 ppss) (nth 8 ppss)))
436 (`string (and (nth 3 ppss) (nth 8 ppss)))
437 (`paren (nth 1 ppss))
440 (defun python-syntax-context-type (&optional syntax-ppss)
441 "Return the context type using SYNTAX-PPSS.
442 The type returned can be `comment', `string' or `paren'."
443 (let ((ppss (or syntax-ppss (syntax-ppss))))
445 ((nth 8 ppss) (if (nth 4 ppss) 'comment 'string))
446 ((nth 1 ppss) 'paren))))
448 (defsubst python-syntax-comment-or-string-p ()
449 "Return non-nil if point is inside 'comment or 'string."
450 (nth 8 (syntax-ppss)))
452 (define-obsolete-function-alias
453 'python-info-ppss-context #'python-syntax-context "24.3")
455 (define-obsolete-function-alias
456 'python-info-ppss-context-type #'python-syntax-context-type "24.3")
458 (define-obsolete-function-alias
459 'python-info-ppss-comment-or-string-p
460 #'python-syntax-comment-or-string-p "24.3")
462 (defvar python-font-lock-keywords
466 "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with"
467 "assert" "else" "if" "pass" "yield" "break" "except" "import" "class"
468 "in" "raise" "continue" "finally" "is" "return" "def" "for" "lambda"
473 ;; False, None, and True are listed as keywords on the Python 3
474 ;; documentation, but since they also qualify as constants they are
475 ;; fontified like that in order to keep font-lock consistent between
482 (,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_))))
483 (1 font-lock-function-name-face))
485 (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_))))
486 (1 font-lock-type-face))
490 "Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
491 ;; copyright, license, credits, quit and exit are added by the site
492 ;; module and they are not intended to be used in programs
493 "copyright" "credits" "exit" "license" "quit")
494 symbol-end) . font-lock-constant-face)
496 (,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_))
497 (0+ "." (1+ (or word ?_)))))
498 (1 font-lock-type-face))
499 ;; Builtin Exceptions
502 "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
503 "DeprecationWarning" "EOFError" "EnvironmentError" "Exception"
504 "FloatingPointError" "FutureWarning" "GeneratorExit" "IOError"
505 "ImportError" "ImportWarning" "IndexError" "KeyError"
506 "KeyboardInterrupt" "LookupError" "MemoryError" "NameError"
507 "NotImplementedError" "OSError" "OverflowError"
508 "PendingDeprecationWarning" "ReferenceError" "RuntimeError"
509 "RuntimeWarning" "StopIteration" "SyntaxError" "SyntaxWarning"
510 "SystemError" "SystemExit" "TypeError" "UnboundLocalError"
511 "UnicodeDecodeError" "UnicodeEncodeError" "UnicodeError"
512 "UnicodeTranslateError" "UnicodeWarning" "UserWarning" "VMSError"
513 "ValueError" "Warning" "WindowsError" "ZeroDivisionError"
517 "BufferError" "BytesWarning" "IndentationError" "ResourceWarning"
519 symbol-end) . font-lock-type-face)
523 "abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
524 "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
525 "eval" "filter" "float" "format" "frozenset" "getattr" "globals"
526 "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
527 "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
528 "min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
529 "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
530 "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
533 "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
534 "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
537 "ascii" "bytearray" "bytes" "exec"
539 "__all__" "__doc__" "__name__" "__package__")
540 symbol-end) . font-lock-builtin-face)
542 ;; support for a = b = c = 5
544 (let ((re (python-rx (group (+ (any word ?. ?_)))
545 (? ?\[ (+ (not (any ?\]))) ?\]) (* space)
546 assignment-operator))
548 (while (and (setq res (re-search-forward re limit t))
549 (or (python-syntax-context 'paren)
550 (equal (char-after (point)) ?=))))
552 (1 font-lock-variable-name-face nil nil))
553 ;; support for a, b, c = (1, 2, 3)
555 (let ((re (python-rx (group (+ (any word ?. ?_))) (* space)
556 (* ?, (* space) (+ (any word ?. ?_)) (* space))
557 ?, (* space) (+ (any word ?. ?_)) (* space)
558 assignment-operator))
560 (while (and (setq res (re-search-forward re limit t))
561 (goto-char (match-end 1))
562 (python-syntax-context 'paren)))
564 (1 font-lock-variable-name-face nil nil))))
566 (defconst python-syntax-propertize-function
567 (syntax-propertize-rules
568 ((python-rx string-delimiter)
569 (0 (ignore (python-syntax-stringify))))))
571 (defsubst python-syntax-count-quotes (quote-char &optional point limit)
572 "Count number of quotes around point (max is 3).
573 QUOTE-CHAR is the quote char to count. Optional argument POINT is
574 the point where scan starts (defaults to current point), and LIMIT
575 is used to limit the scan."
578 (or (not limit) (< (+ point i) limit))
579 (eq (char-after (+ point i)) quote-char))
583 (defun python-syntax-stringify ()
584 "Put `syntax-table' property correctly on single/triple quotes."
585 (let* ((num-quotes (length (match-string-no-properties 1)))
587 (backward-char num-quotes)
589 (forward-char num-quotes)))
590 (string-start (and (not (nth 4 ppss)) (nth 8 ppss)))
591 (quote-starting-pos (- (point) num-quotes))
592 (quote-ending-pos (point))
595 (python-syntax-count-quotes
596 (char-before) string-start quote-starting-pos))))
597 (cond ((and string-start (= num-closing-quotes 0))
598 ;; This set of quotes doesn't match the string starting
602 ;; This set of quotes delimit the start of a string.
603 (put-text-property quote-starting-pos (1+ quote-starting-pos)
604 'syntax-table (string-to-syntax "|")))
605 ((= num-quotes num-closing-quotes)
606 ;; This set of quotes delimit the end of a string.
607 (put-text-property (1- quote-ending-pos) quote-ending-pos
608 'syntax-table (string-to-syntax "|")))
609 ((> num-quotes num-closing-quotes)
610 ;; This may only happen whenever a triple quote is closing
611 ;; a single quoted string. Add string delimiter syntax to
613 (put-text-property quote-starting-pos quote-ending-pos
614 'syntax-table (string-to-syntax "|"))))))
616 (defvar python-mode-syntax-table
617 (let ((table (make-syntax-table)))
618 ;; Give punctuation syntax to ASCII that normally has symbol
619 ;; syntax or has word syntax and isn't a letter.
620 (let ((symbol (string-to-syntax "_"))
621 (sst (standard-syntax-table)))
624 (if (equal symbol (aref sst i))
625 (modify-syntax-entry i "." table)))))
626 (modify-syntax-entry ?$ "." table)
627 (modify-syntax-entry ?% "." table)
629 (modify-syntax-entry ?# "<" table)
630 (modify-syntax-entry ?\n ">" table)
631 (modify-syntax-entry ?' "\"" table)
632 (modify-syntax-entry ?` "$" table)
634 "Syntax table for Python files.")
636 (defvar python-dotty-syntax-table
637 (let ((table (make-syntax-table python-mode-syntax-table)))
638 (modify-syntax-entry ?. "w" table)
639 (modify-syntax-entry ?_ "w" table)
641 "Dotty syntax table for Python files.
642 It makes underscores and dots word constituent chars.")
647 (defcustom python-indent-offset 4
648 "Default indentation offset for Python."
653 (defcustom python-indent-guess-indent-offset t
654 "Non-nil tells Python mode to guess `python-indent-offset' value."
659 (defcustom python-indent-trigger-commands
660 '(indent-for-tab-command yas-expand yas/expand)
661 "Commands that might trigger a `python-indent-line' call."
662 :type '(repeat symbol)
665 (define-obsolete-variable-alias
666 'python-indent 'python-indent-offset "24.3")
668 (define-obsolete-variable-alias
669 'python-guess-indent 'python-indent-guess-indent-offset "24.3")
671 (defvar python-indent-current-level 0
672 "Current indentation level `python-indent-line-function' is using.")
674 (defvar python-indent-levels '(0)
675 "Levels of indentation available for `python-indent-line-function'.")
677 (defun python-indent-guess-indent-offset ()
678 "Guess and set `python-indent-offset' for the current buffer."
683 (goto-char (point-min))
685 (while (and (not block-end)
687 (python-rx line-start block-start) nil t))
689 (not (python-syntax-context-type))
691 (goto-char (line-end-position))
692 (python-util-forward-comment -1)
693 (if (equal (char-before) ?:)
696 (when (python-info-block-continuation-line-p)
697 (while (and (python-info-continuation-line-p)
700 (python-util-forward-comment -1)
701 (when (equal (char-before) ?:)
703 (setq block-end (point-marker))))
706 (goto-char block-end)
707 (python-util-forward-comment)
708 (current-indentation))))
709 (if (and indentation (not (zerop indentation)))
710 (set (make-local-variable 'python-indent-offset) indentation)
711 (message "Can't guess python-indent-offset, using defaults: %s"
712 python-indent-offset)))))))
714 (defun python-indent-context ()
715 "Get information on indentation context.
716 Context information is returned with a cons with the form:
719 Where status can be any of the following symbols:
721 * after-comment: When current line might continue a comment block
722 * inside-paren: If point in between (), {} or []
723 * inside-string: If point is inside a string
724 * after-backslash: Previous line ends in a backslash
725 * after-beginning-of-block: Point is after beginning of block
726 * after-line: Point is after normal line
727 * dedenter-statement: Point is on a dedenter statement.
728 * no-indent: Point is at beginning of buffer or other special case
729 START is the buffer position where the sexp starts."
732 (let ((ppss (save-excursion (beginning-of-line) (syntax-ppss)))
736 ;; Beginning of buffer
738 (goto-char (line-beginning-position))
741 ;; Comment continuation
745 (python-info-current-line-comment-p)
746 (python-info-current-line-empty-p))
749 (python-info-current-line-comment-p)))
753 ((setq start (python-syntax-context 'string ppss))
756 ((setq start (python-syntax-context 'paren ppss))
759 ((setq start (when (not (or (python-syntax-context 'string ppss)
760 (python-syntax-context 'comment ppss)))
761 (let ((line-beg-pos (line-number-at-pos)))
762 (python-info-line-ends-backslash-p
763 (1- line-beg-pos)))))
765 ;; After beginning of block
766 ((setq start (save-excursion
768 (back-to-indentation)
769 (python-util-forward-comment -1)
770 (equal (char-before) ?:))
771 ;; Move to the first block start that's not in within
772 ;; a string, comment or paren and that's not a
773 ;; continuation line.
774 (while (and (re-search-backward
775 (python-rx block-start) nil t)
777 (python-syntax-context-type)
778 (python-info-continuation-line-p))))
779 (when (looking-at (python-rx block-start))
781 'after-beginning-of-block)
782 ((when (setq start (python-info-dedenter-statement-p))
783 'dedenter-statement))
785 ((setq start (save-excursion
786 (back-to-indentation)
787 (skip-chars-backward (rx (or whitespace ?\n)))
788 (python-nav-beginning-of-statement)
795 (defun python-indent-calculate-indentation ()
796 "Calculate correct indentation offset for the current line."
797 (let* ((indentation-context (python-indent-context))
798 (context-status (car indentation-context))
799 (context-start (cdr indentation-context)))
803 (pcase context-status
806 (goto-char context-start)
807 (current-indentation))
808 ;; When point is after beginning of block just add one level
809 ;; of indentation relative to the context-start
810 (`after-beginning-of-block
811 (goto-char context-start)
812 (+ (current-indentation) python-indent-offset))
813 ;; When after a simple line just use previous line
816 (let* ((pair (save-excursion
817 (goto-char context-start)
819 (current-indentation)
820 (python-info-beginning-of-block-p))))
821 (context-indentation (car pair))
822 ;; TODO: Separate block enders into its own case.
825 (python-util-forward-comment -1)
826 (python-nav-beginning-of-statement)
827 (looking-at (python-rx block-ender)))
830 (- context-indentation adjustment)))
831 ;; When point is on a dedenter statement, search for the
832 ;; opening block that corresponds to it and use its
833 ;; indentation. If no opening block is found just remove
834 ;; indentation as this is an invalid python file.
836 (let ((block-start-point
837 (python-info-dedenter-opening-block-position)))
839 (if (not block-start-point)
841 (goto-char block-start-point)
842 (current-indentation)))))
843 ;; When inside of a string, do nothing. just use the current
844 ;; indentation. XXX: perhaps it would be a good idea to
845 ;; invoke standard text indentation here
847 (goto-char context-start)
848 (current-indentation))
849 ;; After backslash we have several possibilities.
852 ;; Check if current line is a dot continuation. For this
853 ;; the current line must start with a dot and previous
854 ;; line must contain a dot too.
856 (back-to-indentation)
857 (when (looking-at "\\.")
858 ;; If after moving one line back point is inside a paren it
859 ;; needs to move back until it's not anymore
863 (python-syntax-context 'paren))))
864 (goto-char (line-end-position))
865 (while (and (re-search-backward
866 "\\." (line-beginning-position) t)
867 (python-syntax-context-type)))
868 (if (and (looking-at "\\.")
869 (not (python-syntax-context-type)))
870 ;; The indentation is the same column of the
871 ;; first matching dot that's not inside a
872 ;; comment, a string or a paren
874 ;; No dot found on previous line, just add another
875 ;; indentation level.
876 (+ (current-indentation) python-indent-offset)))))
877 ;; Check if prev line is a block continuation
878 ((let ((block-continuation-start
879 (python-info-block-continuation-line-p)))
880 (when block-continuation-start
881 ;; If block-continuation-start is set jump to that
882 ;; marker and use first column after the block start
883 ;; as indentation value.
884 (goto-char block-continuation-start)
886 (python-rx block-start (* space))
887 (line-end-position) t)
889 ;; Check if current line is an assignment continuation
890 ((let ((assignment-continuation-start
891 (python-info-assignment-continuation-line-p)))
892 (when assignment-continuation-start
893 ;; If assignment-continuation is set jump to that
894 ;; marker and use first column after the assignment
895 ;; operator as indentation value.
896 (goto-char assignment-continuation-start)
900 (goto-char (python-info-beginning-of-backslash))
905 (or (python-info-beginning-of-backslash) (point)))
906 (python-info-line-ends-backslash-p)))
907 ;; The two previous lines ended in a backslash so we must
908 ;; respect previous line indentation.
909 (current-indentation)
910 ;; What happens here is that we are dealing with the second
911 ;; line of a backslash continuation, in that case we just going
912 ;; to add one indentation level.
913 (+ (current-indentation) python-indent-offset)))))
914 ;; When inside a paren there's a need to handle nesting
918 ;; If current line closes the outermost open paren use the
919 ;; current indentation of the context-start line.
921 (skip-syntax-forward "\s" (line-end-position))
922 (when (and (looking-at (regexp-opt '(")" "]" "}")))
925 (not (python-syntax-context 'paren))))
926 (goto-char context-start)
927 (current-indentation))))
928 ;; If open paren is contained on a line by itself add another
929 ;; indentation level, else look for the first word after the
930 ;; opening paren and use it's column position as indentation
932 ((let* ((content-starts-in-newline)
935 (if (setq content-starts-in-newline
937 (goto-char context-start)
941 (line-beginning-position)
943 (python-util-forward-comment))
945 (+ (current-indentation) python-indent-offset)
949 ;; If current line closes a nested open paren de-indent one
952 (back-to-indentation)
953 (looking-at (regexp-opt '(")" "]" "}"))))
954 (- indent python-indent-offset))
955 ;; If the line of the opening paren that wraps the current
956 ;; line starts a block add another level of indentation to
957 ;; follow new pep8 recommendation. See: http://ur1.ca/5rojx
959 (when (and content-starts-in-newline
961 (goto-char context-start)
962 (back-to-indentation)
963 (looking-at (python-rx block-start))))
964 (+ indent python-indent-offset))))
967 (defun python-indent-calculate-levels ()
968 "Calculate `python-indent-levels' and reset `python-indent-current-level'."
969 (if (or (python-info-continuation-line-p)
970 (not (python-info-dedenter-statement-p)))
971 ;; XXX: This asks for a refactor. Even if point is on a
972 ;; dedenter statement, it could be multiline and in that case
973 ;; the continuation lines should be indented with normal rules.
974 (let* ((indentation (python-indent-calculate-indentation))
975 (remainder (% indentation python-indent-offset))
976 (steps (/ (- indentation remainder) python-indent-offset)))
977 (setq python-indent-levels (list 0))
978 (dotimes (step steps)
979 (push (* python-indent-offset (1+ step)) python-indent-levels))
980 (when (not (eq 0 remainder))
981 (push (+ (* python-indent-offset steps) remainder) python-indent-levels)))
982 (setq python-indent-levels
984 (mapcar (lambda (pos)
987 (current-indentation)))
988 (python-info-dedenter-opening-block-positions))
990 (setq python-indent-current-level (1- (length python-indent-levels))
991 python-indent-levels (nreverse python-indent-levels)))
993 (defun python-indent-toggle-levels ()
994 "Toggle `python-indent-current-level' over `python-indent-levels'."
995 (setq python-indent-current-level (1- python-indent-current-level))
996 (when (< python-indent-current-level 0)
997 (setq python-indent-current-level (1- (length python-indent-levels)))))
999 (defun python-indent-line (&optional force-toggle)
1000 "Internal implementation of `python-indent-line-function'.
1001 Uses the offset calculated in
1002 `python-indent-calculate-indentation' and available levels
1003 indicated by the variable `python-indent-levels' to set the
1004 current indentation.
1006 When the variable `last-command' is equal to one of the symbols
1007 inside `python-indent-trigger-commands' or FORCE-TOGGLE is
1008 non-nil it cycles levels indicated in the variable
1009 `python-indent-levels' by setting the current level in the
1010 variable `python-indent-current-level'.
1012 When the variable `last-command' is not equal to one of the
1013 symbols inside `python-indent-trigger-commands' and FORCE-TOGGLE
1014 is nil it calculates possible indentation levels and saves them
1015 in the variable `python-indent-levels'. Afterwards it sets the
1016 variable `python-indent-current-level' correctly so offset is
1018 (nth python-indent-current-level python-indent-levels)"
1020 (and (or (and (memq this-command python-indent-trigger-commands)
1021 (eq last-command this-command))
1023 (not (equal python-indent-levels '(0)))
1024 (or (python-indent-toggle-levels) t))
1025 (python-indent-calculate-levels))
1026 (let* ((starting-pos (point-marker))
1027 (indent-ending-position
1028 (+ (line-beginning-position) (current-indentation)))
1029 (follow-indentation-p
1031 (and (<= (line-beginning-position) starting-pos)
1032 (>= indent-ending-position starting-pos))))
1033 (next-indent (nth python-indent-current-level python-indent-levels)))
1034 (unless (= next-indent (current-indentation))
1036 (delete-horizontal-space)
1037 (indent-to next-indent)
1038 (goto-char starting-pos))
1039 (and follow-indentation-p (back-to-indentation)))
1040 (python-info-dedenter-opening-block-message))
1042 (defun python-indent-line-function ()
1043 "`indent-line-function' for Python mode.
1044 See `python-indent-line' for details."
1045 (python-indent-line))
1047 (defun python-indent-dedent-line ()
1048 "De-indent current line."
1050 (when (and (not (python-syntax-comment-or-string-p))
1051 (<= (point) (save-excursion
1052 (back-to-indentation)
1054 (> (current-column) 0))
1055 (python-indent-line t)
1058 (defun python-indent-dedent-line-backspace (arg)
1059 "De-indent current line.
1060 Argument ARG is passed to `backward-delete-char-untabify' when
1061 point is not in between the indentation."
1063 (when (not (python-indent-dedent-line))
1064 (backward-delete-char-untabify arg)))
1065 (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)
1067 (defun python-indent-region (start end)
1068 "Indent a Python region automagically.
1070 Called from a program, START and END specify the region to indent."
1071 (let ((deactivate-mark nil))
1074 (setq end (point-marker))
1076 (or (bolp) (forward-line 1))
1077 (while (< (point) end)
1078 (or (and (bolp) (eolp))
1080 ;; Skip if previous line is empty or a comment.
1082 (let ((line-is-comment-p
1083 (python-info-current-line-comment-p)))
1086 (or (and (python-info-current-line-comment-p)
1087 ;; Unless this line is a comment too.
1088 (not line-is-comment-p))
1089 (python-info-current-line-empty-p)))))
1090 ;; Don't mess with strings, unless it's the
1091 ;; enclosing set of quotes.
1092 (or (not (python-syntax-context 'string))
1096 (current-indentation)
1097 (python-syntax-count-quotes (char-after) (point))))
1098 (string-to-syntax "|")))
1099 ;; Skip if current line is a block start, a
1100 ;; dedenter or block ender.
1102 (back-to-indentation)
1105 (or block-start dedenter block-ender))))))
1106 (python-indent-line)))
1108 (move-marker end nil))))
1110 (defun python-indent-shift-left (start end &optional count)
1111 "Shift lines contained in region START END by COUNT columns to the left.
1112 COUNT defaults to `python-indent-offset'. If region isn't
1113 active, the current line is shifted. The shifted region includes
1114 the lines in which START and END lie. An error is signaled if
1115 any lines in the region are indented less than COUNT columns."
1118 (list (region-beginning) (region-end) current-prefix-arg)
1119 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
1121 (setq count (prefix-numeric-value count))
1122 (setq count python-indent-offset))
1124 (let ((deactivate-mark nil))
1127 (while (< (point) end)
1128 (if (and (< (current-indentation) count)
1129 (not (looking-at "[ \t]*$")))
1130 (user-error "Can't shift all lines enough"))
1132 (indent-rigidly start end (- count))))))
1134 (defun python-indent-shift-right (start end &optional count)
1135 "Shift lines contained in region START END by COUNT columns to the right.
1136 COUNT defaults to `python-indent-offset'. If region isn't
1137 active, the current line is shifted. The shifted region includes
1138 the lines in which START and END lie."
1141 (list (region-beginning) (region-end) current-prefix-arg)
1142 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
1143 (let ((deactivate-mark nil))
1144 (setq count (if count (prefix-numeric-value count)
1145 python-indent-offset))
1146 (indent-rigidly start end count)))
1148 (defun python-indent-post-self-insert-function ()
1149 "Adjust indentation after insertion of some characters.
1150 This function is intended to be added to `post-self-insert-hook.'
1151 If a line renders a paren alone, after adding a char before it,
1152 the line will be re-indented automatically if needed."
1153 (when (and electric-indent-mode
1154 (eq (char-before) last-command-event))
1156 ;; Electric indent inside parens
1159 (let ((paren-start (python-syntax-context 'paren)))
1160 ;; Check that point is inside parens.
1163 ;; Filter the case where input is happening in the same
1164 ;; line where the open paren is.
1165 (= (line-number-at-pos)
1166 (line-number-at-pos paren-start)))))
1167 ;; When content has been added before the closing paren or a
1168 ;; comma has been inserted, it's ok to do the trick.
1170 (memq (char-after) '(?\) ?\] ?\}))
1171 (eq (char-before) ?,)))
1173 (goto-char (line-beginning-position))
1174 (let ((indentation (python-indent-calculate-indentation)))
1175 (when (< (current-indentation) indentation)
1176 (indent-line-to indentation)))))
1178 ((and (eq ?: last-command-event)
1179 (memq ?: electric-indent-chars)
1180 (not current-prefix-arg)
1181 ;; Trigger electric colon only at end of line
1183 ;; Avoid re-indenting on extra colon
1184 (not (equal ?: (char-before (1- (point)))))
1185 (not (python-syntax-comment-or-string-p))
1186 ;; Never re-indent at beginning of defun
1187 (not (save-excursion
1188 (python-nav-beginning-of-statement)
1189 (python-info-looking-at-beginning-of-defun))))
1190 (python-indent-line)))))
1195 (defvar python-nav-beginning-of-defun-regexp
1196 (python-rx line-start (* space) defun (+ space) (group symbol-name))
1197 "Regexp matching class or function definition.
1198 The name of the defun should be grouped so it can be retrieved
1199 via `match-string'.")
1201 (defun python-nav--beginning-of-defun (&optional arg)
1202 "Internal implementation of `python-nav-beginning-of-defun'.
1203 With positive ARG search backwards, else search forwards."
1204 (when (or (null arg) (= arg 0)) (setq arg 1))
1205 (let* ((re-search-fn (if (> arg 0)
1206 #'re-search-backward
1207 #'re-search-forward))
1208 (line-beg-pos (line-beginning-position))
1209 (line-content-start (+ line-beg-pos (current-indentation)))
1210 (pos (point-marker))
1215 (not (python-info-looking-at-beginning-of-defun))
1216 (python-nav-backward-block)))
1217 (or (and (python-info-looking-at-beginning-of-defun)
1218 (+ (current-indentation) python-indent-offset))
1222 (when (and (< arg 0)
1223 (python-info-looking-at-beginning-of-defun))
1225 (while (and (funcall re-search-fn
1226 python-nav-beginning-of-defun-regexp nil t)
1227 (or (python-syntax-context-type)
1228 ;; Handle nested defuns when moving
1229 ;; backwards by checking indentation.
1231 (not (= (current-indentation) 0))
1232 (>= (current-indentation) beg-indentation)))))
1233 (and (python-info-looking-at-beginning-of-defun)
1234 (or (not (= (line-number-at-pos pos)
1235 (line-number-at-pos)))
1236 (and (>= (point) line-beg-pos)
1237 (<= (point) line-content-start)
1238 (> pos line-content-start)))))))
1240 (or (beginning-of-line 1) t)
1241 (and (goto-char pos) nil))))
1243 (defun python-nav-beginning-of-defun (&optional arg)
1244 "Move point to `beginning-of-defun'.
1245 With positive ARG search backwards else search forward.
1246 ARG nil or 0 defaults to 1. When searching backwards,
1247 nested defuns are handled with care depending on current
1248 point position. Return non-nil if point is moved to
1249 `beginning-of-defun'."
1250 (when (or (null arg) (= arg 0)) (setq arg 1))
1252 (while (and (not (= arg 0))
1253 (let ((keep-searching-p
1254 (python-nav--beginning-of-defun arg)))
1255 (when (and keep-searching-p (null found))
1258 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
1261 (defun python-nav-end-of-defun ()
1262 "Move point to the end of def or class.
1263 Returns nil if point is not in a def or class."
1265 (let ((beg-defun-indent)
1267 (when (or (python-info-looking-at-beginning-of-defun)
1268 (python-nav-beginning-of-defun 1)
1269 (python-nav-beginning-of-defun -1))
1270 (setq beg-defun-indent (current-indentation))
1272 (python-nav-end-of-statement)
1273 (python-util-forward-comment 1)
1274 (and (> (current-indentation) beg-defun-indent)
1276 (python-util-forward-comment -1)
1278 ;; Ensure point moves forward.
1279 (and (> beg-pos (point)) (goto-char beg-pos)))))
1281 (defun python-nav--syntactically (fn poscompfn &optional contextfn)
1282 "Move point using FN avoiding places with specific context.
1283 FN must take no arguments. POSCOMPFN is a two arguments function
1284 used to compare current and previous point after it is moved
1285 using FN, this is normally a less-than or greater-than
1286 comparison. Optional argument CONTEXTFN defaults to
1287 `python-syntax-context-type' and is used for checking current
1288 point context, it must return a non-nil value if this point must
1290 (let ((contextfn (or contextfn 'python-syntax-context-type))
1291 (start-pos (point-marker))
1296 (and (funcall fn) (point-marker)))
1297 (context (funcall contextfn)))
1298 (cond ((and (not context) newpos
1299 (or (and (not prev-pos) newpos)
1300 (and prev-pos newpos
1301 (funcall poscompfn newpos prev-pos))))
1302 (throw 'found (point-marker)))
1303 ((and newpos context)
1304 (setq prev-pos (point)))
1305 (t (when (not newpos) (goto-char start-pos))
1306 (throw 'found nil))))))))
1308 (defun python-nav--forward-defun (arg)
1309 "Internal implementation of python-nav-{backward,forward}-defun.
1310 Uses ARG to define which function to call, and how many times
1313 (while (and (> arg 0)
1315 (python-nav--syntactically
1318 python-nav-beginning-of-defun-regexp nil t))
1320 (setq arg (1- arg)))
1321 (while (and (< arg 0)
1323 (python-nav--syntactically
1326 python-nav-beginning-of-defun-regexp nil t))
1328 (setq arg (1+ arg)))
1331 (defun python-nav-backward-defun (&optional arg)
1332 "Navigate to closer defun backward ARG times.
1333 Unlikely `python-nav-beginning-of-defun' this doesn't care about
1334 nested definitions."
1336 (python-nav--forward-defun (- (or arg 1))))
1338 (defun python-nav-forward-defun (&optional arg)
1339 "Navigate to closer defun forward ARG times.
1340 Unlikely `python-nav-beginning-of-defun' this doesn't care about
1341 nested definitions."
1343 (python-nav--forward-defun (or arg 1)))
1345 (defun python-nav-beginning-of-statement ()
1346 "Move to start of current statement."
1348 (back-to-indentation)
1349 (let* ((ppss (syntax-ppss))
1352 (python-syntax-context 'paren ppss)
1353 (python-syntax-context 'string ppss))))
1356 (goto-char context-point)
1357 (python-nav-beginning-of-statement))
1360 (python-info-line-ends-backslash-p))
1362 (python-nav-beginning-of-statement))))
1365 (defun python-nav-end-of-statement (&optional noend)
1366 "Move to end of current statement.
1367 Optional argument NOEND is internal and makes the logic to not
1368 jump to the end of line when moving forward searching for the end
1371 (let (string-start bs-pos)
1372 (while (and (or noend (goto-char (line-end-position)))
1374 (cond ((setq string-start (python-syntax-context 'string))
1375 (goto-char string-start)
1376 (if (python-syntax-context 'paren)
1377 ;; Ended up inside a paren, roll again.
1378 (python-nav-end-of-statement t)
1379 ;; This is not inside a paren, move to the
1380 ;; end of this string.
1381 (goto-char (+ (point)
1382 (python-syntax-count-quotes
1383 (char-after (point)) (point))))
1384 (or (re-search-forward (rx (syntax string-delimiter)) nil t)
1385 (goto-char (point-max)))))
1386 ((python-syntax-context 'paren)
1387 ;; The statement won't end before we've escaped
1388 ;; at least one level of parenthesis.
1390 (goto-char (scan-lists (point) 1 -1))
1391 (scan-error (goto-char (nth 3 err)))))
1392 ((setq bs-pos (python-info-line-ends-backslash-p))
1394 (forward-line 1))))))
1397 (defun python-nav-backward-statement (&optional arg)
1398 "Move backward to previous statement.
1399 With ARG, repeat. See `python-nav-forward-statement'."
1401 (or arg (setq arg 1))
1402 (python-nav-forward-statement (- arg)))
1404 (defun python-nav-forward-statement (&optional arg)
1405 "Move forward to next statement.
1406 With ARG, repeat. With negative argument, move ARG times
1407 backward to previous statement."
1409 (or arg (setq arg 1))
1411 (python-nav-end-of-statement)
1412 (python-util-forward-comment)
1413 (python-nav-beginning-of-statement)
1414 (setq arg (1- arg)))
1416 (python-nav-beginning-of-statement)
1417 (python-util-forward-comment -1)
1418 (python-nav-beginning-of-statement)
1419 (setq arg (1+ arg))))
1421 (defun python-nav-beginning-of-block ()
1422 "Move to start of current block."
1424 (let ((starting-pos (point)))
1426 (python-nav-beginning-of-statement)
1427 (looking-at (python-rx block-start)))
1429 ;; Go to first line beginning a statement
1430 (while (and (not (bobp))
1431 (or (and (python-nav-beginning-of-statement) nil)
1432 (python-info-current-line-comment-p)
1433 (python-info-current-line-empty-p)))
1435 (let ((block-matching-indent
1436 (- (current-indentation) python-indent-offset)))
1438 (and (python-nav-backward-block)
1439 (> (current-indentation) block-matching-indent)))
1440 (if (and (looking-at (python-rx block-start))
1441 (= (current-indentation) block-matching-indent))
1443 (and (goto-char starting-pos) nil))))))
1445 (defun python-nav-end-of-block ()
1446 "Move to end of current block."
1448 (when (python-nav-beginning-of-block)
1449 (let ((block-indentation (current-indentation)))
1450 (python-nav-end-of-statement)
1451 (while (and (forward-line 1)
1453 (or (and (> (current-indentation) block-indentation)
1454 (or (python-nav-end-of-statement) t))
1455 (python-info-current-line-comment-p)
1456 (python-info-current-line-empty-p))))
1457 (python-util-forward-comment -1)
1460 (defun python-nav-backward-block (&optional arg)
1461 "Move backward to previous block of code.
1462 With ARG, repeat. See `python-nav-forward-block'."
1464 (or arg (setq arg 1))
1465 (python-nav-forward-block (- arg)))
1467 (defun python-nav-forward-block (&optional arg)
1468 "Move forward to next block of code.
1469 With ARG, repeat. With negative argument, move ARG times
1470 backward to previous block."
1472 (or arg (setq arg 1))
1473 (let ((block-start-regexp
1474 (python-rx line-start (* whitespace) block-start))
1475 (starting-pos (point)))
1477 (python-nav-end-of-statement)
1479 (re-search-forward block-start-regexp nil t)
1480 (python-syntax-context-type)))
1481 (setq arg (1- arg)))
1483 (python-nav-beginning-of-statement)
1485 (re-search-backward block-start-regexp nil t)
1486 (python-syntax-context-type)))
1487 (setq arg (1+ arg)))
1488 (python-nav-beginning-of-statement)
1489 (if (not (looking-at (python-rx block-start)))
1490 (and (goto-char starting-pos) nil)
1491 (and (not (= (point) starting-pos)) (point-marker)))))
1493 (defun python-nav--lisp-forward-sexp (&optional arg)
1494 "Standard version `forward-sexp'.
1495 It ignores completely the value of `forward-sexp-function' by
1496 setting it to nil before calling `forward-sexp'. With positive
1497 ARG move forward only one sexp, else move backwards."
1498 (let ((forward-sexp-function)
1499 (arg (if (or (not arg) (> arg 0)) 1 -1)))
1500 (forward-sexp arg)))
1502 (defun python-nav--lisp-forward-sexp-safe (&optional arg)
1503 "Safe version of standard `forward-sexp'.
1504 When at end of sexp (i.e. looking at a opening/closing paren)
1505 skips it instead of throwing an error. With positive ARG move
1506 forward only one sexp, else move backwards."
1507 (let* ((arg (if (or (not arg) (> arg 0)) 1 -1))
1509 (if (> arg 0) (python-rx close-paren) (python-rx open-paren)))
1511 (if (> arg 0) #'re-search-forward #'re-search-backward)))
1513 (python-nav--lisp-forward-sexp arg)
1515 (while (and (funcall search-fn paren-regexp nil t)
1516 (python-syntax-context 'paren)))))))
1518 (defun python-nav--forward-sexp (&optional dir safe)
1519 "Move to forward sexp.
1520 With positive optional argument DIR direction move forward, else
1521 backwards. When optional argument SAFE is non-nil do not throw
1522 errors when at end of sexp, skip it instead."
1523 (setq dir (or dir 1))
1525 (let* ((forward-p (if (> dir 0)
1526 (and (setq dir 1) t)
1527 (and (setq dir -1) nil)))
1528 (context-type (python-syntax-context-type)))
1530 ((memq context-type '(string comment))
1531 ;; Inside of a string, get out of it.
1532 (let ((forward-sexp-function))
1533 (forward-sexp dir)))
1534 ((or (eq context-type 'paren)
1535 (and forward-p (looking-at (python-rx open-paren)))
1536 (and (not forward-p)
1537 (eq (syntax-class (syntax-after (1- (point))))
1538 (car (string-to-syntax ")")))))
1539 ;; Inside a paren or looking at it, lisp knows what to do.
1541 (python-nav--lisp-forward-sexp-safe dir)
1542 (python-nav--lisp-forward-sexp dir)))
1544 ;; This part handles the lispy feel of
1545 ;; `python-nav-forward-sexp'. Knowing everything about the
1546 ;; current context and the context of the next sexp tries to
1547 ;; follow the lisp sexp motion commands in a symmetric manner.
1550 ((python-info-beginning-of-block-p) 'block-start)
1551 ((python-info-end-of-block-p) 'block-end)
1552 ((python-info-beginning-of-statement-p) 'statement-start)
1553 ((python-info-end-of-statement-p) 'statement-end)))
1557 (python-nav--lisp-forward-sexp-safe dir)
1558 (python-nav--lisp-forward-sexp dir))
1562 (goto-char next-sexp-pos)
1564 ((python-info-beginning-of-block-p) 'block-start)
1565 ((python-info-end-of-block-p) 'block-end)
1566 ((python-info-beginning-of-statement-p) 'statement-start)
1567 ((python-info-end-of-statement-p) 'statement-end)
1568 ((python-info-statement-starts-block-p) 'starts-block)
1569 ((python-info-statement-ends-block-p) 'ends-block)))))
1571 (cond ((and (not (eobp))
1572 (python-info-current-line-empty-p))
1573 (python-util-forward-comment dir)
1574 (python-nav--forward-sexp dir))
1575 ((eq context 'block-start)
1576 (python-nav-end-of-block))
1577 ((eq context 'statement-start)
1578 (python-nav-end-of-statement))
1579 ((and (memq context '(statement-end block-end))
1580 (eq next-sexp-context 'ends-block))
1581 (goto-char next-sexp-pos)
1582 (python-nav-end-of-block))
1583 ((and (memq context '(statement-end block-end))
1584 (eq next-sexp-context 'starts-block))
1585 (goto-char next-sexp-pos)
1586 (python-nav-end-of-block))
1587 ((memq context '(statement-end block-end))
1588 (goto-char next-sexp-pos)
1589 (python-nav-end-of-statement))
1590 (t (goto-char next-sexp-pos)))
1591 (cond ((and (not (bobp))
1592 (python-info-current-line-empty-p))
1593 (python-util-forward-comment dir)
1594 (python-nav--forward-sexp dir))
1595 ((eq context 'block-end)
1596 (python-nav-beginning-of-block))
1597 ((eq context 'statement-end)
1598 (python-nav-beginning-of-statement))
1599 ((and (memq context '(statement-start block-start))
1600 (eq next-sexp-context 'starts-block))
1601 (goto-char next-sexp-pos)
1602 (python-nav-beginning-of-block))
1603 ((and (memq context '(statement-start block-start))
1604 (eq next-sexp-context 'ends-block))
1605 (goto-char next-sexp-pos)
1606 (python-nav-beginning-of-block))
1607 ((memq context '(statement-start block-start))
1608 (goto-char next-sexp-pos)
1609 (python-nav-beginning-of-statement))
1610 (t (goto-char next-sexp-pos))))))))))
1612 (defun python-nav-forward-sexp (&optional arg)
1613 "Move forward across expressions.
1614 With ARG, do it that many times. Negative arg -N means move
1617 (or arg (setq arg 1))
1619 (python-nav--forward-sexp 1)
1620 (setq arg (1- arg)))
1622 (python-nav--forward-sexp -1)
1623 (setq arg (1+ arg))))
1625 (defun python-nav-backward-sexp (&optional arg)
1626 "Move backward across expressions.
1627 With ARG, do it that many times. Negative arg -N means move
1630 (or arg (setq arg 1))
1631 (python-nav-forward-sexp (- arg)))
1633 (defun python-nav-forward-sexp-safe (&optional arg)
1634 "Move forward safely across expressions.
1635 With ARG, do it that many times. Negative arg -N means move
1638 (or arg (setq arg 1))
1640 (python-nav--forward-sexp 1 t)
1641 (setq arg (1- arg)))
1643 (python-nav--forward-sexp -1 t)
1644 (setq arg (1+ arg))))
1646 (defun python-nav-backward-sexp-safe (&optional arg)
1647 "Move backward safely across expressions.
1648 With ARG, do it that many times. Negative arg -N means move
1651 (or arg (setq arg 1))
1652 (python-nav-forward-sexp-safe (- arg)))
1654 (defun python-nav--up-list (&optional dir)
1655 "Internal implementation of `python-nav-up-list'.
1656 DIR is always 1 or -1 and comes sanitized from
1657 `python-nav-up-list' calls."
1658 (let ((context (python-syntax-context-type))
1659 (forward-p (> dir 0)))
1661 ((memq context '(string comment)))
1662 ((eq context 'paren)
1663 (let ((forward-sexp-function))
1665 ((and forward-p (python-info-end-of-block-p))
1666 (let ((parent-end-pos
1668 (let ((indentation (and
1669 (python-nav-beginning-of-block)
1670 (current-indentation))))
1671 (while (and indentation
1673 (>= (current-indentation) indentation)
1674 (python-nav-backward-block)))
1675 (python-nav-end-of-block)))))
1676 (and (> (or parent-end-pos (point)) (point))
1677 (goto-char parent-end-pos))))
1678 (forward-p (python-nav-end-of-block))
1679 ((and (not forward-p)
1680 (> (current-indentation) 0)
1681 (python-info-beginning-of-block-p))
1682 (let ((prev-block-pos
1684 (let ((indentation (current-indentation)))
1685 (while (and (python-nav-backward-block)
1686 (>= (current-indentation) indentation))))
1688 (and (> (point) prev-block-pos)
1689 (goto-char prev-block-pos))))
1690 ((not forward-p) (python-nav-beginning-of-block)))))
1692 (defun python-nav-up-list (&optional arg)
1693 "Move forward out of one level of parentheses (or blocks).
1694 With ARG, do this that many times.
1695 A negative argument means move backward but still to a less deep spot.
1696 This command assumes point is not in a string or comment."
1698 (or arg (setq arg 1))
1700 (python-nav--up-list 1)
1701 (setq arg (1- arg)))
1703 (python-nav--up-list -1)
1704 (setq arg (1+ arg))))
1706 (defun python-nav-backward-up-list (&optional arg)
1707 "Move backward out of one level of parentheses (or blocks).
1708 With ARG, do this that many times.
1709 A negative argument means move forward but still to a less deep spot.
1710 This command assumes point is not in a string or comment."
1712 (or arg (setq arg 1))
1713 (python-nav-up-list (- arg)))
1715 (defun python-nav-if-name-main ()
1716 "Move point at the beginning the __main__ block.
1717 When \"if __name__ == '__main__':\" is found returns its
1718 position, else returns nil."
1720 (let ((point (point))
1721 (found (catch 'found
1722 (goto-char (point-min))
1723 (while (re-search-forward
1724 (python-rx line-start
1726 "__name__" (+ space)
1728 (group-n 1 (or ?\" ?\'))
1729 "__main__" (backref 1) (* space) ":")
1731 (when (not (python-syntax-context-type))
1733 (throw 'found t))))))
1736 (ignore (goto-char point)))))
1739 ;;; Shell integration
1741 (defcustom python-shell-buffer-name "Python"
1742 "Default buffer name for Python interpreter."
1747 (defcustom python-shell-interpreter "python"
1748 "Default Python interpreter for shell."
1752 (defcustom python-shell-internal-buffer-name "Python Internal"
1753 "Default buffer name for the Internal Python interpreter."
1758 (defcustom python-shell-interpreter-args "-i"
1759 "Default arguments for the Python interpreter."
1763 (defcustom python-shell-interpreter-interactive-arg "-i"
1764 "Interpreter argument to force it to run interactively."
1768 (defcustom python-shell-prompt-detect-enabled t
1769 "Non-nil enables autodetection of interpreter prompts."
1774 (defcustom python-shell-prompt-detect-failure-warning t
1775 "Non-nil enables warnings when detection of prompts fail."
1780 (defcustom python-shell-prompt-input-regexps
1781 '(">>> " "\\.\\.\\. " ; Python
1782 "In \\[[0-9]+\\]: " ; IPython
1783 " \\.\\.\\.: " ; IPython
1784 ;; Using ipdb outside IPython may fail to cleanup and leave static
1785 ;; IPython prompts activated, this adds some safeguard for that.
1786 "In : " "\\.\\.\\.: ")
1787 "List of regular expressions matching input prompts."
1788 :type '(repeat string)
1791 (defcustom python-shell-prompt-output-regexps
1793 "Out\\[[0-9]+\\]: " ; IPython
1794 "Out :") ; ipdb safeguard
1795 "List of regular expressions matching output prompts."
1796 :type '(repeat string)
1799 (defcustom python-shell-prompt-regexp ">>> "
1800 "Regular expression matching top level input prompt of Python shell.
1801 It should not contain a caret (^) at the beginning."
1804 (defcustom python-shell-prompt-block-regexp "\\.\\.\\. "
1805 "Regular expression matching block input prompt of Python shell.
1806 It should not contain a caret (^) at the beginning."
1809 (defcustom python-shell-prompt-output-regexp ""
1810 "Regular expression matching output prompt of Python shell.
1811 It should not contain a caret (^) at the beginning."
1814 (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ "
1815 "Regular expression matching pdb input prompt of Python shell.
1816 It should not contain a caret (^) at the beginning."
1819 (define-obsolete-variable-alias
1820 'python-shell-enable-font-lock 'python-shell-font-lock-enable "25.1")
1822 (defcustom python-shell-font-lock-enable t
1823 "Should syntax highlighting be enabled in the Python shell buffer?
1824 Restart the Python shell after changing this variable for it to take effect."
1829 (defcustom python-shell-unbuffered t
1830 "Should shell output be unbuffered?.
1831 When non-nil, this may prevent delayed and missing output in the
1832 Python shell. See commentary for details."
1837 (defcustom python-shell-process-environment nil
1838 "List of environment variables for Python shell.
1839 This variable follows the same rules as `process-environment'
1840 since it merges with it before the process creation routines are
1841 called. When this variable is nil, the Python shell is run with
1842 the default `process-environment'."
1843 :type '(repeat string)
1847 (defcustom python-shell-extra-pythonpaths nil
1848 "List of extra pythonpaths for Python shell.
1849 The values of this variable are added to the existing value of
1850 PYTHONPATH in the `process-environment' variable."
1851 :type '(repeat string)
1855 (defcustom python-shell-exec-path nil
1856 "List of path to search for binaries.
1857 This variable follows the same rules as `exec-path' since it
1858 merges with it before the process creation routines are called.
1859 When this variable is nil, the Python shell is run with the
1860 default `exec-path'."
1861 :type '(repeat string)
1865 (defcustom python-shell-virtualenv-root nil
1866 "Path to virtualenv root.
1867 This variable, when set to a string, makes the values stored in
1868 `python-shell-process-environment' and `python-shell-exec-path'
1869 to be modified properly so shells are started with the specified
1871 :type '(choice (const nil) string)
1875 (define-obsolete-variable-alias
1876 'python-shell-virtualenv-path 'python-shell-virtualenv-root "25.1")
1878 (defcustom python-shell-setup-codes '(python-shell-completion-setup-code
1879 python-ffap-setup-code
1880 python-eldoc-setup-code)
1881 "List of code run by `python-shell-send-setup-codes'."
1882 :type '(repeat symbol)
1886 (defcustom python-shell-compilation-regexp-alist
1887 `((,(rx line-start (1+ (any " \t")) "File \""
1888 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
1889 "\", line " (group (1+ digit)))
1891 (,(rx " in file " (group (1+ not-newline)) " on line "
1894 (,(rx line-start "> " (group (1+ (not (any "(\"<"))))
1895 "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()")
1897 "`compilation-error-regexp-alist' for inferior Python."
1898 :type '(alist string)
1901 (defvar python-shell--prompt-calculated-input-regexp nil
1902 "Calculated input prompt regexp for inferior python shell.
1903 Do not set this variable directly, instead use
1904 `python-shell-prompt-set-calculated-regexps'.")
1906 (defvar python-shell--prompt-calculated-output-regexp nil
1907 "Calculated output prompt regexp for inferior python shell.
1908 Do not set this variable directly, instead use
1909 `python-shell-set-prompt-regexp'.")
1911 (defun python-shell-prompt-detect ()
1912 "Detect prompts for the current `python-shell-interpreter'.
1913 When prompts can be retrieved successfully from the
1914 `python-shell-interpreter' run with
1915 `python-shell-interpreter-interactive-arg', returns a list of
1916 three elements, where the first two are input prompts and the
1917 last one is an output prompt. When no prompts can be detected
1918 and `python-shell-prompt-detect-failure-warning' is non-nil,
1919 shows a warning with instructions to avoid hangs and returns nil.
1920 When `python-shell-prompt-detect-enabled' is nil avoids any
1921 detection and just returns nil."
1922 (when python-shell-prompt-detect-enabled
1923 (let* ((process-environment (python-shell-calculate-process-environment))
1924 (exec-path (python-shell-calculate-exec-path))
1927 "ps = [getattr(sys, 'ps%s' % i, '') for i in range(1,4)]\n"
1928 ;; JSON is built manually for compatibility
1929 "ps_json = '\\n[\"%s\", \"%s\", \"%s\"]\\n' % tuple(ps)\n"
1934 ;; TODO: improve error handling by using
1935 ;; `condition-case' and displaying the error message to
1936 ;; the user in the no-prompts warning.
1938 (let ((code-file (python-shell--save-temp-file code)))
1939 ;; Use `process-file' as it is remote-host friendly.
1941 python-shell-interpreter
1945 python-shell-interpreter-interactive-arg)
1947 (delete-file code-file)))
1951 (dolist (line (split-string output "\n" t))
1953 ;; Check if current line is a valid JSON array
1954 (and (string= (substring line 0 2) "[\"")
1956 ;; Return prompts as a list, not vector
1957 (append (json-read-from-string line) nil)))))
1958 ;; The list must contain 3 strings, where the first
1959 ;; is the input prompt, the second is the block
1960 ;; prompt and the last one is the output prompt. The
1961 ;; input prompt is the only one that can't be empty.
1962 (when (and (= (length res) 3)
1963 (cl-every #'stringp res)
1964 (not (string= (car res) "")))
1965 (throw 'prompts res))))
1967 (when (and (not prompts)
1968 python-shell-prompt-detect-failure-warning)
1970 '(python python-shell-prompt-regexp)
1973 "Python shell prompts cannot be detected.\n"
1974 "If your emacs session hangs when starting python shells\n"
1975 "recover with `keyboard-quit' and then try fixing the\n"
1976 "interactive flag for your interpreter by adjusting the\n"
1977 "`python-shell-interpreter-interactive-arg' or add regexps\n"
1978 "matching shell prompts in the directory-local friendly vars:\n"
1979 " + `python-shell-prompt-regexp'\n"
1980 " + `python-shell-prompt-block-regexp'\n"
1981 " + `python-shell-prompt-output-regexp'\n"
1982 "Or alternatively in:\n"
1983 " + `python-shell-prompt-input-regexps'\n"
1984 " + `python-shell-prompt-output-regexps'")))
1987 (defun python-shell-prompt-validate-regexps ()
1988 "Validate all user provided regexps for prompts.
1989 Signals `user-error' if any of these vars contain invalid
1990 regexps: `python-shell-prompt-regexp',
1991 `python-shell-prompt-block-regexp',
1992 `python-shell-prompt-pdb-regexp',
1993 `python-shell-prompt-output-regexp',
1994 `python-shell-prompt-input-regexps',
1995 `python-shell-prompt-output-regexps'."
1996 (dolist (symbol (list 'python-shell-prompt-input-regexps
1997 'python-shell-prompt-output-regexps
1998 'python-shell-prompt-regexp
1999 'python-shell-prompt-block-regexp
2000 'python-shell-prompt-pdb-regexp
2001 'python-shell-prompt-output-regexp))
2002 (dolist (regexp (let ((regexps (symbol-value symbol)))
2006 (when (not (python-util-valid-regexp-p regexp))
2007 (user-error "Invalid regexp %s in `%s'"
2010 (defun python-shell-prompt-set-calculated-regexps ()
2011 "Detect and set input and output prompt regexps.
2012 Build and set the values for `python-shell-input-prompt-regexp'
2013 and `python-shell-output-prompt-regexp' using the values from
2014 `python-shell-prompt-regexp', `python-shell-prompt-block-regexp',
2015 `python-shell-prompt-pdb-regexp',
2016 `python-shell-prompt-output-regexp',
2017 `python-shell-prompt-input-regexps',
2018 `python-shell-prompt-output-regexps' and detected prompts from
2019 `python-shell-prompt-detect'."
2020 (when (not (and python-shell--prompt-calculated-input-regexp
2021 python-shell--prompt-calculated-output-regexp))
2022 (let* ((detected-prompts (python-shell-prompt-detect))
2024 (output-prompts nil)
2028 (mapconcat #'identity
2031 (let ((length-a (length a))
2032 (length-b (length b)))
2033 (if (= length-a length-b)
2035 (> (length a) (length b))))))
2038 ;; Validate ALL regexps
2039 (python-shell-prompt-validate-regexps)
2040 ;; Collect all user defined input prompts
2041 (dolist (prompt (append python-shell-prompt-input-regexps
2042 (list python-shell-prompt-regexp
2043 python-shell-prompt-block-regexp
2044 python-shell-prompt-pdb-regexp)))
2045 (cl-pushnew prompt input-prompts :test #'string=))
2046 ;; Collect all user defined output prompts
2047 (dolist (prompt (cons python-shell-prompt-output-regexp
2048 python-shell-prompt-output-regexps))
2049 (cl-pushnew prompt output-prompts :test #'string=))
2050 ;; Collect detected prompts if any
2051 (when detected-prompts
2052 (dolist (prompt (butlast detected-prompts))
2053 (setq prompt (regexp-quote prompt))
2054 (cl-pushnew prompt input-prompts :test #'string=))
2055 (cl-pushnew (regexp-quote
2056 (car (last detected-prompts)))
2057 output-prompts :test #'string=))
2058 ;; Set input and output prompt regexps from collected prompts
2059 (setq python-shell--prompt-calculated-input-regexp
2060 (funcall build-regexp input-prompts)
2061 python-shell--prompt-calculated-output-regexp
2062 (funcall build-regexp output-prompts)))))
2064 (defun python-shell-get-process-name (dedicated)
2065 "Calculate the appropriate process name for inferior Python process.
2066 If DEDICATED is t and the variable `buffer-file-name' is non-nil
2067 returns a string with the form
2068 `python-shell-buffer-name'[variable `buffer-file-name'] else
2069 returns the value of `python-shell-buffer-name'."
2073 (format "%s[%s]" python-shell-buffer-name buffer-file-name)
2074 (format "%s" python-shell-buffer-name))))
2077 (defun python-shell-internal-get-process-name ()
2078 "Calculate the appropriate process name for Internal Python process.
2079 The name is calculated from `python-shell-global-buffer-name' and
2080 a hash of all relevant global shell settings in order to ensure
2081 uniqueness for different types of configurations."
2083 python-shell-internal-buffer-name
2086 python-shell-interpreter
2087 python-shell-interpreter-args
2088 python-shell--prompt-calculated-input-regexp
2089 python-shell--prompt-calculated-output-regexp
2090 (mapconcat #'symbol-value python-shell-setup-codes "")
2091 (mapconcat #'identity python-shell-process-environment "")
2092 (mapconcat #'identity python-shell-extra-pythonpaths "")
2093 (mapconcat #'identity python-shell-exec-path "")
2094 (or python-shell-virtualenv-root "")
2095 (mapconcat #'identity python-shell-exec-path "")))))
2097 (defun python-shell-calculate-command ()
2098 "Calculate the string used to execute the inferior Python process."
2099 (let ((exec-path (python-shell-calculate-exec-path)))
2100 ;; `exec-path' gets tweaked so that virtualenv's specific
2101 ;; `python-shell-interpreter' absolute path can be found by
2102 ;; `executable-find'.
2104 (executable-find python-shell-interpreter)
2105 python-shell-interpreter-args)))
2107 (define-obsolete-function-alias
2108 'python-shell-parse-command
2109 #'python-shell-calculate-command "25.1")
2111 (defun python-shell-calculate-pythonpath ()
2112 "Calculate the PYTHONPATH using `python-shell-extra-pythonpaths'."
2113 (let ((pythonpath (getenv "PYTHONPATH"))
2114 (extra (mapconcat 'identity
2115 python-shell-extra-pythonpaths
2118 (concat extra path-separator pythonpath)
2121 (defun python-shell-calculate-process-environment ()
2122 "Calculate process environment given `python-shell-virtualenv-root'."
2123 (let ((process-environment (append
2124 python-shell-process-environment
2125 process-environment nil))
2126 (virtualenv (if python-shell-virtualenv-root
2127 (directory-file-name python-shell-virtualenv-root)
2129 (when python-shell-unbuffered
2130 (setenv "PYTHONUNBUFFERED" "1"))
2131 (when python-shell-extra-pythonpaths
2132 (setenv "PYTHONPATH" (python-shell-calculate-pythonpath)))
2133 (if (not virtualenv)
2135 (setenv "PYTHONHOME" nil)
2136 (setenv "PATH" (format "%s/bin%s%s"
2137 virtualenv path-separator
2138 (or (getenv "PATH") "")))
2139 (setenv "VIRTUAL_ENV" virtualenv))
2140 process-environment))
2142 (defun python-shell-calculate-exec-path ()
2143 "Calculate exec path given `python-shell-virtualenv-root'."
2145 ;; Use nil as the tail so that the list is a full copy,
2146 ;; this is a paranoid safeguard for side-effects.
2147 python-shell-exec-path exec-path nil)))
2148 (if (not python-shell-virtualenv-root)
2150 (cons (expand-file-name "bin" python-shell-virtualenv-root)
2153 (defvar python-shell--package-depth 10)
2155 (defun python-shell-package-enable (directory package)
2156 "Add DIRECTORY parent to $PYTHONPATH and enable PACKAGE."
2158 (let* ((dir (expand-file-name
2159 (read-directory-name
2161 (file-name-directory
2162 (or (buffer-file-name) default-directory)))))
2163 (name (completing-read
2165 (python-util-list-packages
2166 dir python-shell--package-depth))))
2168 (python-shell-send-string
2171 "import os.path;import sys;"
2172 "sys.path.append(os.path.dirname(os.path.dirname('''%s''')));"
2173 "__package__ = '''%s''';"
2175 directory package package)
2176 (python-shell-get-process)))
2178 (defun python-shell-accept-process-output (process &optional timeout regexp)
2179 "Accept PROCESS output with TIMEOUT until REGEXP is found.
2180 Optional argument TIMEOUT is the timeout argument to
2181 `accept-process-output' calls. Optional argument REGEXP
2182 overrides the regexp to match the end of output, defaults to
2183 `comint-prompt-regexp.'. Returns non-nil when output was
2186 This utility is useful in situations where the output may be
2187 received in chunks, since `accept-process-output' gives no
2188 guarantees they will be grabbed in a single call. An example use
2189 case for this would be the CPython shell start-up, where the
2190 banner and the initial prompt are received separately."
2191 (let ((regexp (or regexp comint-prompt-regexp)))
2194 (when (not (accept-process-output process timeout))
2196 (when (looking-back regexp)
2197 (throw 'found t))))))
2199 (defun python-shell-comint-end-of-output-p (output)
2200 "Return non-nil if OUTPUT is ends with input prompt."
2202 ;; XXX: It seems on OSX an extra carriage return is attached
2203 ;; at the end of output, this handles that too.
2206 ;; Remove initial caret from calculated regexp
2207 (replace-regexp-in-string
2208 (rx string-start ?^) ""
2209 python-shell--prompt-calculated-input-regexp)
2213 (define-obsolete-function-alias
2214 'python-comint-output-filter-function
2215 'ansi-color-filter-apply
2218 (defun python-comint-postoutput-scroll-to-bottom (output)
2219 "Faster version of `comint-postoutput-scroll-to-bottom'.
2220 Avoids `recenter' calls until OUTPUT is completely sent."
2221 (when (and (not (string= "" output))
2222 (python-shell-comint-end-of-output-p
2223 (ansi-color-filter-apply output)))
2224 (comint-postoutput-scroll-to-bottom output))
2227 (defvar python-shell--parent-buffer nil)
2229 (defmacro python-shell-with-shell-buffer (&rest body)
2230 "Execute the forms in BODY with the shell buffer temporarily current.
2231 Signals an error if no shell buffer is available for current buffer."
2232 (declare (indent 0) (debug t))
2233 (let ((shell-buffer (make-symbol "shell-buffer")))
2234 `(let ((,shell-buffer (python-shell-get-buffer)))
2235 (when (not ,shell-buffer)
2236 (error "No inferior Python buffer available."))
2237 (with-current-buffer ,shell-buffer
2240 (defvar python-shell--font-lock-buffer nil)
2242 (defun python-shell-font-lock-get-or-create-buffer ()
2243 "Get or create a font-lock buffer for current inferior process."
2244 (python-shell-with-shell-buffer
2245 (if python-shell--font-lock-buffer
2246 python-shell--font-lock-buffer
2248 (process-name (get-buffer-process (current-buffer)))))
2249 (generate-new-buffer
2250 (format "*%s-font-lock*" process-name))))))
2252 (defun python-shell-font-lock-kill-buffer ()
2253 "Kill the font-lock buffer safely."
2254 (python-shell-with-shell-buffer
2255 (when (and python-shell--font-lock-buffer
2256 (buffer-live-p python-shell--font-lock-buffer))
2257 (kill-buffer python-shell--font-lock-buffer)
2258 (when (derived-mode-p 'inferior-python-mode)
2259 (setq python-shell--font-lock-buffer nil)))))
2261 (defmacro python-shell-font-lock-with-font-lock-buffer (&rest body)
2262 "Execute the forms in BODY in the font-lock buffer.
2263 The value returned is the value of the last form in BODY. See
2264 also `with-current-buffer'."
2265 (declare (indent 0) (debug t))
2266 `(python-shell-with-shell-buffer
2267 (save-current-buffer
2268 (when (not (and python-shell--font-lock-buffer
2269 (get-buffer python-shell--font-lock-buffer)))
2270 (setq python-shell--font-lock-buffer
2271 (python-shell-font-lock-get-or-create-buffer)))
2272 (set-buffer python-shell--font-lock-buffer)
2273 (set (make-local-variable 'delay-mode-hooks) t)
2274 (let ((python-indent-guess-indent-offset nil))
2275 (when (not (derived-mode-p 'python-mode))
2279 (defun python-shell-font-lock-cleanup-buffer ()
2280 "Cleanup the font-lock buffer.
2281 Provided as a command because this might be handy if something
2282 goes wrong and syntax highlighting in the shell gets messed up."
2284 (python-shell-with-shell-buffer
2285 (python-shell-font-lock-with-font-lock-buffer
2286 (delete-region (point-min) (point-max)))))
2288 (defun python-shell-font-lock-comint-output-filter-function (output)
2289 "Clean up the font-lock buffer after any OUTPUT."
2290 (when (and (not (string= "" output))
2291 ;; Is end of output and is not just a prompt.
2293 (python-shell-comint-end-of-output-p
2294 (ansi-color-filter-apply output))
2296 ;; If output is other than an input prompt then "real" output has
2297 ;; been received and the font-lock buffer must be cleaned up.
2298 (python-shell-font-lock-cleanup-buffer))
2301 (defun python-shell-font-lock-post-command-hook ()
2302 "Fontifies current line in shell buffer."
2303 (if (eq this-command 'comint-send-input)
2304 ;; Add a newline when user sends input as this may be a block.
2305 (python-shell-font-lock-with-font-lock-buffer
2306 (goto-char (line-end-position))
2308 (when (and (python-util-comint-last-prompt)
2309 (> (point) (cdr (python-util-comint-last-prompt))))
2310 (let ((input (buffer-substring-no-properties
2311 (cdr (python-util-comint-last-prompt)) (point-max)))
2312 (old-input (python-shell-font-lock-with-font-lock-buffer
2313 (buffer-substring-no-properties
2314 (line-beginning-position) (point-max))))
2315 (current-point (point))
2316 (buffer-undo-list t))
2317 ;; When input hasn't changed, do nothing.
2318 (when (not (string= input old-input))
2319 (delete-region (cdr (python-util-comint-last-prompt)) (point-max))
2321 (python-shell-font-lock-with-font-lock-buffer
2322 (delete-region (line-beginning-position)
2323 (line-end-position))
2325 ;; Ensure buffer is fontified, keeping it
2326 ;; compatible with Emacs < 24.4.
2327 (if (fboundp 'font-lock-ensure)
2328 (funcall 'font-lock-ensure)
2329 (font-lock-default-fontify-buffer))
2330 ;; Replace FACE text properties with FONT-LOCK-FACE so
2331 ;; they are not overwritten by comint buffer's font lock.
2332 (python-util-text-properties-replace-name
2333 'face 'font-lock-face)
2334 (buffer-substring (line-beginning-position)
2335 (line-end-position))))
2336 (goto-char current-point))))))
2338 (defun python-shell-font-lock-turn-on (&optional msg)
2339 "Turn on shell font-lock.
2340 With argument MSG show activation message."
2342 (python-shell-with-shell-buffer
2343 (python-shell-font-lock-kill-buffer)
2344 (set (make-local-variable 'python-shell--font-lock-buffer) nil)
2345 (add-hook 'post-command-hook
2346 #'python-shell-font-lock-post-command-hook nil 'local)
2347 (add-hook 'kill-buffer-hook
2348 #'python-shell-font-lock-kill-buffer nil 'local)
2349 (add-hook 'comint-output-filter-functions
2350 #'python-shell-font-lock-comint-output-filter-function
2353 (message "Shell font-lock is enabled"))))
2355 (defun python-shell-font-lock-turn-off (&optional msg)
2356 "Turn off shell font-lock.
2357 With argument MSG show deactivation message."
2359 (python-shell-with-shell-buffer
2360 (python-shell-font-lock-kill-buffer)
2361 (when (python-util-comint-last-prompt)
2362 ;; Cleanup current fontification
2363 (remove-text-properties
2364 (cdr (python-util-comint-last-prompt))
2366 '(face nil font-lock-face nil)))
2367 (set (make-local-variable 'python-shell--font-lock-buffer) nil)
2368 (remove-hook 'post-command-hook
2369 #'python-shell-font-lock-post-command-hook'local)
2370 (remove-hook 'kill-buffer-hook
2371 #'python-shell-font-lock-kill-buffer 'local)
2372 (remove-hook 'comint-output-filter-functions
2373 #'python-shell-font-lock-comint-output-filter-function
2376 (message "Shell font-lock is disabled"))))
2378 (defun python-shell-font-lock-toggle (&optional msg)
2379 "Toggle font-lock for shell.
2380 With argument MSG show activation/deactivation message."
2382 (python-shell-with-shell-buffer
2383 (set (make-local-variable 'python-shell-font-lock-enable)
2384 (not python-shell-font-lock-enable))
2385 (if python-shell-font-lock-enable
2386 (python-shell-font-lock-turn-on msg)
2387 (python-shell-font-lock-turn-off msg))
2388 python-shell-font-lock-enable))
2390 (define-derived-mode inferior-python-mode comint-mode "Inferior Python"
2391 "Major mode for Python inferior process.
2392 Runs a Python interpreter as a subprocess of Emacs, with Python
2393 I/O through an Emacs buffer. Variables `python-shell-interpreter'
2394 and `python-shell-interpreter-args' control which Python
2395 interpreter is run. Variables
2396 `python-shell-prompt-regexp',
2397 `python-shell-prompt-output-regexp',
2398 `python-shell-prompt-block-regexp',
2399 `python-shell-font-lock-enable',
2400 `python-shell-completion-setup-code',
2401 `python-shell-completion-string-code',
2402 `python-eldoc-setup-code', `python-eldoc-string-code',
2403 `python-ffap-setup-code' and `python-ffap-string-code' can
2404 customize this mode for different Python interpreters.
2406 This mode resets `comint-output-filter-functions' locally, so you
2407 may want to re-add custom functions to it using the
2408 `inferior-python-mode-hook'.
2410 You can also add additional setup code to be run at
2411 initialization of the interpreter via `python-shell-setup-codes'
2414 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
2415 (let ((interpreter python-shell-interpreter)
2416 (args python-shell-interpreter-args))
2417 (when python-shell--parent-buffer
2418 (python-util-clone-local-variables python-shell--parent-buffer))
2419 ;; Users can override default values for these vars when calling
2420 ;; `run-python'. This ensures new values let-bound in
2421 ;; `python-shell-make-comint' are locally set.
2422 (set (make-local-variable 'python-shell-interpreter) interpreter)
2423 (set (make-local-variable 'python-shell-interpreter-args) args))
2424 (set (make-local-variable 'python-shell--prompt-calculated-input-regexp) nil)
2425 (set (make-local-variable 'python-shell--prompt-calculated-output-regexp) nil)
2426 (python-shell-prompt-set-calculated-regexps)
2427 (setq comint-prompt-regexp python-shell--prompt-calculated-input-regexp
2428 comint-prompt-read-only t)
2429 (setq mode-line-process '(":%s"))
2430 (set (make-local-variable 'comint-output-filter-functions)
2431 '(ansi-color-process-output
2432 python-pdbtrack-comint-output-filter-function
2433 python-comint-postoutput-scroll-to-bottom))
2434 (set (make-local-variable 'compilation-error-regexp-alist)
2435 python-shell-compilation-regexp-alist)
2436 (add-hook 'completion-at-point-functions
2437 #'python-shell-completion-at-point nil 'local)
2438 (define-key inferior-python-mode-map "\t"
2439 'python-shell-completion-complete-or-indent)
2440 (make-local-variable 'python-pdbtrack-buffers-to-kill)
2441 (make-local-variable 'python-pdbtrack-tracked-buffer)
2442 (make-local-variable 'python-shell-internal-last-output)
2443 (when python-shell-font-lock-enable
2444 (python-shell-font-lock-turn-on))
2445 (compilation-shell-minor-mode 1)
2446 (python-shell-accept-process-output
2447 (get-buffer-process (current-buffer))))
2449 (defun python-shell-make-comint (cmd proc-name &optional pop internal)
2450 "Create a Python shell comint buffer.
2451 CMD is the Python command to be executed and PROC-NAME is the
2452 process name the comint buffer will get. After the comint buffer
2453 is created the `inferior-python-mode' is activated. When
2454 optional argument POP is non-nil the buffer is shown. When
2455 optional argument INTERNAL is non-nil this process is run on a
2456 buffer with a name that starts with a space, following the Emacs
2457 convention for temporary/internal buffers, and also makes sure
2458 the user is not queried for confirmation when the process is
2461 (let* ((proc-buffer-name
2462 (format (if (not internal) "*%s*" " *%s*") proc-name))
2463 (process-environment (python-shell-calculate-process-environment))
2464 (exec-path (python-shell-calculate-exec-path)))
2465 (when (not (comint-check-proc proc-buffer-name))
2466 (let* ((cmdlist (split-string-and-unquote cmd))
2467 (interpreter (car cmdlist))
2468 (args (cdr cmdlist))
2469 (buffer (apply #'make-comint-in-buffer proc-name proc-buffer-name
2470 interpreter nil args))
2471 (python-shell--parent-buffer (current-buffer))
2472 (process (get-buffer-process buffer))
2473 ;; As the user may have overridden default values for
2474 ;; these vars on `run-python', let-binding them allows
2475 ;; to have the new right values in all setup code
2476 ;; that's is done in `inferior-python-mode', which is
2477 ;; important, especially for prompt detection.
2478 (python-shell-interpreter interpreter)
2479 (python-shell-interpreter-args
2480 (mapconcat #'identity args " ")))
2481 (with-current-buffer buffer
2482 (inferior-python-mode))
2483 (and pop (pop-to-buffer buffer t))
2484 (and internal (set-process-query-on-exit-flag process nil))))
2488 (defun run-python (&optional cmd dedicated show)
2489 "Run an inferior Python process.
2490 Input and output via buffer named after
2491 `python-shell-buffer-name'. If there is a process already
2492 running in that buffer, just switch to it.
2494 Argument CMD defaults to `python-shell-calculate-command' return
2495 value. When called interactively with `prefix-arg', it allows
2496 the user to edit such value and choose whether the interpreter
2497 should be DEDICATED for the current buffer. When numeric prefix
2498 arg is other than 0 or 4 do not SHOW.
2500 Runs the hook `inferior-python-mode-hook' after
2501 `comint-mode-hook' is run. (Type \\[describe-mode] in the
2502 process buffer for a list of commands.)"
2504 (if current-prefix-arg
2506 (read-shell-command "Run Python: " (python-shell-calculate-command))
2507 (y-or-n-p "Make dedicated process? ")
2508 (= (prefix-numeric-value current-prefix-arg) 4))
2509 (list (python-shell-calculate-command) nil t)))
2510 (python-shell-make-comint
2511 (or cmd (python-shell-calculate-command))
2512 (python-shell-get-process-name dedicated) show)
2515 (defun run-python-internal ()
2516 "Run an inferior Internal Python process.
2517 Input and output via buffer named after
2518 `python-shell-internal-buffer-name' and what
2519 `python-shell-internal-get-process-name' returns.
2521 This new kind of shell is intended to be used for generic
2522 communication related to defined configurations; the main
2523 difference with global or dedicated shells is that these ones are
2524 attached to a configuration, not a buffer. This means that can
2525 be used for example to retrieve the sys.path and other stuff,
2526 without messing with user shells. Note that
2527 `python-shell-font-lock-enable' and `inferior-python-mode-hook'
2528 are set to nil for these shells, so setup codes are not sent at
2530 (let ((python-shell-font-lock-enable nil)
2531 (inferior-python-mode-hook nil))
2533 (python-shell-make-comint
2534 (python-shell-calculate-command)
2535 (python-shell-internal-get-process-name) nil t))))
2537 (defun python-shell-get-buffer ()
2538 "Return inferior Python buffer for current buffer.
2539 If current buffer is in `inferior-python-mode', return it."
2540 (if (derived-mode-p 'inferior-python-mode)
2542 (let* ((dedicated-proc-name (python-shell-get-process-name t))
2543 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
2544 (global-proc-name (python-shell-get-process-name nil))
2545 (global-proc-buffer-name (format "*%s*" global-proc-name))
2546 (dedicated-running (comint-check-proc dedicated-proc-buffer-name))
2547 (global-running (comint-check-proc global-proc-buffer-name)))
2548 ;; Always prefer dedicated
2549 (or (and dedicated-running dedicated-proc-buffer-name)
2550 (and global-running global-proc-buffer-name)))))
2552 (defun python-shell-get-process ()
2553 "Return inferior Python process for current buffer."
2554 (get-buffer-process (python-shell-get-buffer)))
2556 (defun python-shell-get-or-create-process (&optional cmd dedicated show)
2557 "Get or create an inferior Python process for current buffer and return it.
2558 Arguments CMD, DEDICATED and SHOW are those of `run-python' and
2559 are used to start the shell. If those arguments are not
2560 provided, `run-python' is called interactively and the user will
2561 be asked for their values."
2562 (let ((shell-process (python-shell-get-process)))
2563 (when (not shell-process)
2565 ;; XXX: Refactor code such that calling `run-python'
2566 ;; interactively is not needed anymore.
2567 (call-interactively 'run-python)
2568 (run-python cmd dedicated show)))
2569 (or shell-process (python-shell-get-process))))
2571 (defvar python-shell-internal-buffer nil
2572 "Current internal shell buffer for the current buffer.
2573 This is really not necessary at all for the code to work but it's
2574 there for compatibility with CEDET.")
2576 (defvar python-shell-internal-last-output nil
2577 "Last output captured by the internal shell.
2578 This is really not necessary at all for the code to work but it's
2579 there for compatibility with CEDET.")
2581 (defun python-shell-internal-get-or-create-process ()
2582 "Get or create an inferior Internal Python process."
2583 (let* ((proc-name (python-shell-internal-get-process-name))
2584 (proc-buffer-name (format " *%s*" proc-name)))
2585 (when (not (process-live-p proc-name))
2586 (run-python-internal)
2587 (setq python-shell-internal-buffer proc-buffer-name))
2588 (get-buffer-process proc-buffer-name)))
2590 (define-obsolete-function-alias
2591 'python-proc 'python-shell-internal-get-or-create-process "24.3")
2593 (define-obsolete-variable-alias
2594 'python-buffer 'python-shell-internal-buffer "24.3")
2596 (define-obsolete-variable-alias
2597 'python-preoutput-result 'python-shell-internal-last-output "24.3")
2599 (defun python-shell--save-temp-file (string)
2600 (let* ((temporary-file-directory
2601 (if (file-remote-p default-directory)
2602 (concat (file-remote-p default-directory) "/tmp")
2603 temporary-file-directory))
2604 (temp-file-name (make-temp-file "py"))
2605 (coding-system-for-write 'utf-8))
2606 (with-temp-file temp-file-name
2607 (insert "# -*- coding: utf-8 -*-\n") ;Not needed for Python-3.
2609 (delete-trailing-whitespace))
2612 (defun python-shell-send-string (string &optional process)
2613 "Send STRING to inferior Python PROCESS."
2614 (interactive "sPython command: ")
2615 (let ((process (or process (python-shell-get-or-create-process))))
2616 (if (string-match ".\n+." string) ;Multiline.
2617 (let* ((temp-file-name (python-shell--save-temp-file string)))
2618 (python-shell-send-file temp-file-name process temp-file-name t))
2619 (comint-send-string process string)
2620 (when (or (not (string-match "\n\\'" string))
2621 (string-match "\n[ \t].*\n?\\'" string))
2622 (comint-send-string process "\n")))))
2624 (defvar python-shell-output-filter-in-progress nil)
2625 (defvar python-shell-output-filter-buffer nil)
2627 (defun python-shell-output-filter (string)
2628 "Filter used in `python-shell-send-string-no-output' to grab output.
2629 STRING is the output received to this point from the process.
2630 This filter saves received output from the process in
2631 `python-shell-output-filter-buffer' and stops receiving it after
2632 detecting a prompt at the end of the buffer."
2634 string (ansi-color-filter-apply string)
2635 python-shell-output-filter-buffer
2636 (concat python-shell-output-filter-buffer string))
2637 (when (python-shell-comint-end-of-output-p
2638 python-shell-output-filter-buffer)
2639 ;; Output ends when `python-shell-output-filter-buffer' contains
2640 ;; the prompt attached at the end of it.
2641 (setq python-shell-output-filter-in-progress nil
2642 python-shell-output-filter-buffer
2643 (substring python-shell-output-filter-buffer
2644 0 (match-beginning 0)))
2646 python-shell--prompt-calculated-output-regexp
2647 python-shell-output-filter-buffer)
2648 ;; Some shells, like IPython might append a prompt before the
2649 ;; output, clean that.
2650 (setq python-shell-output-filter-buffer
2651 (substring python-shell-output-filter-buffer (match-end 0)))))
2654 (defun python-shell-send-string-no-output (string &optional process)
2655 "Send STRING to PROCESS and inhibit output.
2657 (let ((process (or process (python-shell-get-or-create-process)))
2658 (comint-preoutput-filter-functions
2659 '(python-shell-output-filter))
2660 (python-shell-output-filter-in-progress t)
2664 (python-shell-send-string string process)
2665 (while python-shell-output-filter-in-progress
2666 ;; `python-shell-output-filter' takes care of setting
2667 ;; `python-shell-output-filter-in-progress' to NIL after it
2668 ;; detects end of output.
2669 (accept-process-output process))
2671 python-shell-output-filter-buffer
2672 (setq python-shell-output-filter-buffer nil)))
2673 (with-current-buffer (process-buffer process)
2674 (comint-interrupt-subjob)))))
2676 (defun python-shell-internal-send-string (string)
2677 "Send STRING to the Internal Python interpreter.
2678 Returns the output. See `python-shell-send-string-no-output'."
2679 ;; XXX Remove `python-shell-internal-last-output' once CEDET is
2680 ;; updated to support this new mode.
2681 (setq python-shell-internal-last-output
2682 (python-shell-send-string-no-output
2683 ;; Makes this function compatible with the old
2684 ;; python-send-receive. (At least for CEDET).
2685 (replace-regexp-in-string "_emacs_out +" "" string)
2686 (python-shell-internal-get-or-create-process))))
2688 (define-obsolete-function-alias
2689 'python-send-receive 'python-shell-internal-send-string "24.3")
2691 (define-obsolete-function-alias
2692 'python-send-string 'python-shell-internal-send-string "24.3")
2694 (defvar python--use-fake-loc nil
2695 "If non-nil, use `compilation-fake-loc' to trace errors back to the buffer.
2696 If nil, regions of text are prepended by the corresponding number of empty
2697 lines and Python is told to output error messages referring to the whole
2700 (defun python-shell-buffer-substring (start end &optional nomain)
2701 "Send buffer substring from START to END formatted for shell.
2702 This is a wrapper over `buffer-substring' that takes care of
2703 different transformations for the code sent to be evaluated in
2705 1. When optional argument NOMAIN is non-nil everything under an
2706 \"if __name__ == '__main__'\" block will be removed.
2707 2. When a subregion of the buffer is sent, it takes care of
2708 appending extra empty lines so tracebacks are correct.
2709 3. Wraps indented regions under an \"if True:\" block so the
2710 interpreter evaluates them correctly."
2711 (let ((substring (buffer-substring-no-properties start end))
2712 (fillstr (unless python--use-fake-loc
2713 (make-string (1- (line-number-at-pos start)) ?\n)))
2714 (toplevel-block-p (save-excursion
2716 (or (zerop (line-number-at-pos start))
2718 (python-util-forward-comment 1)
2719 (zerop (current-indentation)))))))
2722 (if fillstr (insert fillstr))
2724 (goto-char (point-min))
2725 (unless python--use-fake-loc
2726 ;; python-shell--save-temp-file adds an extra coding line, which would
2727 ;; throw off the line-counts, so let's try to compensate here.
2728 (if (looking-at "[ \t]*[#\n]")
2729 (delete-region (point) (line-beginning-position 2))))
2730 (when (not toplevel-block-p)
2732 (delete-region (point) (line-end-position)))
2734 (let* ((if-name-main-start-end
2737 (when (python-nav-if-name-main)
2739 (progn (python-nav-forward-sexp-safe)
2741 ;; Oh destructuring bind, how I miss you.
2742 (if-name-main-start (car if-name-main-start-end))
2743 (if-name-main-end (cdr if-name-main-start-end)))
2744 (when if-name-main-start-end
2745 (goto-char if-name-main-start)
2746 (delete-region if-name-main-start if-name-main-end)
2749 (- (line-number-at-pos if-name-main-end)
2750 (line-number-at-pos if-name-main-start)) ?\n)))))
2751 (buffer-substring-no-properties (point-min) (point-max)))))
2753 (declare-function compilation-fake-loc "compile"
2754 (marker file &optional line col))
2756 (defun python-shell-send-region (start end &optional nomain)
2757 "Send the region delimited by START and END to inferior Python process."
2759 (let* ((python--use-fake-loc
2760 (or python--use-fake-loc (not buffer-file-name)))
2761 (string (python-shell-buffer-substring start end nomain))
2762 (process (python-shell-get-or-create-process))
2763 (_ (string-match "\\`\n*\\(.*\\)" string)))
2764 (message "Sent: %s..." (match-string 1 string))
2765 (let* ((temp-file-name (python-shell--save-temp-file string))
2766 (file-name (or (buffer-file-name) temp-file-name)))
2767 (python-shell-send-file file-name process temp-file-name t)
2768 (unless python--use-fake-loc
2769 (with-current-buffer (process-buffer process)
2770 (compilation-fake-loc (copy-marker start) temp-file-name
2771 2)) ;; Not 1, because of the added coding line.
2774 (defun python-shell-send-buffer (&optional arg)
2775 "Send the entire buffer to inferior Python process.
2776 With prefix ARG allow execution of code inside blocks delimited
2777 by \"if __name__== '__main__':\"."
2781 (python-shell-send-region (point-min) (point-max) (not arg))))
2783 (defun python-shell-send-defun (arg)
2784 "Send the current defun to inferior Python process.
2785 When argument ARG is non-nil do not include decorators."
2788 (python-shell-send-region
2791 (while (and (or (python-nav-beginning-of-defun)
2792 (beginning-of-line 1))
2793 (> (current-indentation) 0)))
2795 (while (and (forward-line -1)
2796 (looking-at (python-rx decorator))))
2800 (or (python-nav-end-of-defun)
2804 (defun python-shell-send-file (file-name &optional process temp-file-name
2806 "Send FILE-NAME to inferior Python PROCESS.
2807 If TEMP-FILE-NAME is passed then that file is used for processing
2808 instead, while internally the shell will continue to use FILE-NAME.
2809 If DELETE is non-nil, delete the file afterwards."
2810 (interactive "fFile to send: ")
2811 (let* ((process (or process (python-shell-get-or-create-process)))
2812 (temp-file-name (when temp-file-name
2814 (or (file-remote-p temp-file-name 'localname)
2816 (file-name (or (when file-name
2818 (or (file-remote-p file-name 'localname)
2821 (when (not file-name)
2822 (error "If FILE-NAME is nil then TEMP-FILE-NAME must be non-nil"))
2823 (python-shell-send-string
2825 (concat "__pyfile = open('''%s''');"
2826 "exec(compile(__pyfile.read(), '''%s''', 'exec'));"
2827 "__pyfile.close()%s")
2828 (or temp-file-name file-name) file-name
2829 (if delete (format "; import os; os.remove('''%s''')"
2830 (or temp-file-name file-name))
2834 (defun python-shell-switch-to-shell ()
2835 "Switch to inferior Python process buffer."
2837 (process-buffer (python-shell-get-or-create-process)) t)
2839 (defun python-shell-send-setup-code ()
2840 "Send all setup code for shell.
2841 This function takes the list of setup code to send from the
2842 `python-shell-setup-codes' list."
2843 (let ((process (python-shell-get-process))
2847 (cond ((stringp elt) elt)
2848 ((symbolp elt) (symbol-value elt))
2850 python-shell-setup-codes
2852 "\n\nprint ('python.el: sent setup code')")))
2853 (python-shell-send-string code process)
2854 (python-shell-accept-process-output process)))
2856 (add-hook 'inferior-python-mode-hook
2857 #'python-shell-send-setup-code)
2860 ;;; Shell completion
2862 (defcustom python-shell-completion-setup-code
2867 import builtins as __builtin__
2869 import readline, rlcompleter
2871 def __PYTHON_EL_get_completions(text):
2874 def __PYTHON_EL_get_completions(text):
2875 builtins = dir(__builtin__)
2878 splits = text.split()
2879 is_module = splits and splits[0] in ('from', 'import')
2880 is_ipython = ('__IPYTHON__' in builtins or
2881 '__IPYTHON__active' in builtins)
2883 from IPython.core.completerlib import module_completion
2884 completions = module_completion(text.strip())
2885 elif is_ipython and '__IP' in builtins:
2886 completions = __IP.complete(text)
2887 elif is_ipython and 'get_ipython' in builtins:
2888 completions = get_ipython().Completer.all_completions(text)
2892 res = readline.get_completer()(text, i)
2896 completions.append(res)
2900 "Code used to setup completion in inferior Python processes."
2904 (defcustom python-shell-completion-string-code
2905 "';'.join(__PYTHON_EL_get_completions('''%s'''))\n"
2906 "Python code used to get a string of completions separated by semicolons.
2907 The string passed to the function is the current python name or
2908 the full statement in the case of imports."
2912 (define-obsolete-variable-alias
2913 'python-shell-completion-module-string-code
2914 'python-shell-completion-string-code
2916 "Completion string code must also autocomplete modules.")
2918 (define-obsolete-variable-alias
2919 'python-shell-completion-pdb-string-code
2920 'python-shell-completion-string-code
2922 "Completion string code must work for (i)pdb.")
2924 (defun python-shell-completion-get-completions (process import input)
2925 "Do completion at point using PROCESS for IMPORT or INPUT.
2926 When IMPORT is non-nil takes precedence over INPUT for
2928 (with-current-buffer (process-buffer process)
2930 (let ((prompt-boundaries (python-util-comint-last-prompt)))
2931 (buffer-substring-no-properties
2932 (car prompt-boundaries) (cdr prompt-boundaries))))
2934 ;; Check whether a prompt matches a pdb string, an import
2935 ;; statement or just the standard prompt and use the
2936 ;; correct python-shell-completion-*-code string
2937 (cond ((and (string-match
2938 (concat "^" python-shell-prompt-pdb-regexp) prompt))
2939 ;; Since there are no guarantees the user will remain
2940 ;; in the same context where completion code was sent
2941 ;; (e.g. user steps into a function), safeguard
2942 ;; resending completion setup continuously.
2943 (concat python-shell-completion-setup-code
2944 "\nprint (" python-shell-completion-string-code ")"))
2946 python-shell--prompt-calculated-input-regexp prompt)
2947 python-shell-completion-string-code)
2949 (subject (or import input)))
2950 (and completion-code
2951 (> (length input) 0)
2953 (python-util-strip-string
2954 (python-shell-send-string-no-output
2955 (format completion-code subject) process))))
2956 (and (> (length completions) 2)
2957 (split-string completions
2958 "^'\\|^\"\\|;\\|'$\\|\"$" t)))))))
2960 (defun python-shell-completion-at-point (&optional process)
2961 "Function for `completion-at-point-functions' in `inferior-python-mode'.
2962 Optional argument PROCESS forces completions to be retrieved
2963 using that one instead of current buffer's process."
2964 (setq process (or process (get-buffer-process (current-buffer))))
2965 (let* ((last-prompt-end (cdr (python-util-comint-last-prompt)))
2967 (when (string-match-p
2968 (rx (* space) word-start (or "from" "import") word-end space)
2969 (buffer-substring-no-properties last-prompt-end (point)))
2970 (buffer-substring-no-properties last-prompt-end (point))))
2973 (if (not (re-search-backward
2975 (or whitespace open-paren close-paren string-delimiter))
2979 (forward-char (length (match-string-no-properties 0)))
2983 (completion-table-dynamic
2985 #'python-shell-completion-get-completions
2986 process import-statement)))))
2988 (define-obsolete-function-alias
2989 'python-shell-completion-complete-at-point
2990 'python-shell-completion-at-point
2993 (defun python-shell-completion-complete-or-indent ()
2994 "Complete or indent depending on the context.
2995 If content before pointer is all whitespace, indent.
2996 If not try to complete."
2998 (if (string-match "^[[:space:]]*$"
2999 (buffer-substring (comint-line-beginning-position)
3001 (indent-for-tab-command)
3002 (completion-at-point)))
3005 ;;; PDB Track integration
3007 (defcustom python-pdbtrack-activate t
3008 "Non-nil makes Python shell enable pdbtracking."
3013 (defcustom python-pdbtrack-stacktrace-info-regexp
3014 "> \\([^\"(<]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()"
3015 "Regular expression matching stacktrace information.
3016 Used to extract the current line and module being inspected."
3021 (defvar python-pdbtrack-tracked-buffer nil
3022 "Variable containing the value of the current tracked buffer.
3023 Never set this variable directly, use
3024 `python-pdbtrack-set-tracked-buffer' instead.")
3026 (defvar python-pdbtrack-buffers-to-kill nil
3027 "List of buffers to be deleted after tracking finishes.")
3029 (defun python-pdbtrack-set-tracked-buffer (file-name)
3030 "Set the buffer for FILE-NAME as the tracked buffer.
3031 Internally it uses the `python-pdbtrack-tracked-buffer' variable.
3032 Returns the tracked buffer."
3033 (let ((file-buffer (get-file-buffer
3034 (concat (file-remote-p default-directory)
3037 (setq python-pdbtrack-tracked-buffer file-buffer)
3038 (setq file-buffer (find-file-noselect file-name))
3039 (when (not (member file-buffer python-pdbtrack-buffers-to-kill))
3040 (add-to-list 'python-pdbtrack-buffers-to-kill file-buffer)))
3043 (defun python-pdbtrack-comint-output-filter-function (output)
3044 "Move overlay arrow to current pdb line in tracked buffer.
3045 Argument OUTPUT is a string with the output from the comint process."
3046 (when (and python-pdbtrack-activate (not (string= output "")))
3047 (let* ((full-output (ansi-color-filter-apply
3048 (buffer-substring comint-last-input-end (point-max))))
3052 (insert full-output)
3053 ;; When the debugger encounters a pdb.set_trace()
3054 ;; command, it prints a single stack frame. Sometimes
3055 ;; it prints a bit of extra information about the
3056 ;; arguments of the present function. When ipdb
3057 ;; encounters an exception, it prints the _entire_ stack
3058 ;; trace. To handle all of these cases, we want to find
3059 ;; the _last_ stack frame printed in the most recent
3060 ;; batch of output, then jump to the corresponding
3061 ;; file/line number.
3062 (goto-char (point-max))
3063 (when (re-search-backward python-pdbtrack-stacktrace-info-regexp nil t)
3064 (setq line-number (string-to-number
3065 (match-string-no-properties 2)))
3066 (match-string-no-properties 1)))))
3067 (if (and file-name line-number)
3068 (let* ((tracked-buffer
3069 (python-pdbtrack-set-tracked-buffer file-name))
3070 (shell-buffer (current-buffer))
3071 (tracked-buffer-window (get-buffer-window tracked-buffer))
3072 (tracked-buffer-line-pos))
3073 (with-current-buffer tracked-buffer
3074 (set (make-local-variable 'overlay-arrow-string) "=>")
3075 (set (make-local-variable 'overlay-arrow-position) (make-marker))
3076 (setq tracked-buffer-line-pos (progn
3077 (goto-char (point-min))
3078 (forward-line (1- line-number))
3080 (when tracked-buffer-window
3082 tracked-buffer-window tracked-buffer-line-pos))
3083 (set-marker overlay-arrow-position tracked-buffer-line-pos))
3084 (pop-to-buffer tracked-buffer)
3085 (switch-to-buffer-other-window shell-buffer))
3086 (when python-pdbtrack-tracked-buffer
3087 (with-current-buffer python-pdbtrack-tracked-buffer
3088 (set-marker overlay-arrow-position nil))
3089 (mapc #'(lambda (buffer)
3090 (ignore-errors (kill-buffer buffer)))
3091 python-pdbtrack-buffers-to-kill)
3092 (setq python-pdbtrack-tracked-buffer nil
3093 python-pdbtrack-buffers-to-kill nil)))))
3097 ;;; Symbol completion
3099 (defun python-completion-at-point ()
3100 "Function for `completion-at-point-functions' in `python-mode'.
3101 For this to work as best as possible you should call
3102 `python-shell-send-buffer' from time to time so context in
3103 inferior Python process is updated properly."
3104 (let ((process (python-shell-get-process)))
3106 (python-shell-completion-at-point process))))
3108 (define-obsolete-function-alias
3109 'python-completion-complete-at-point
3110 'python-completion-at-point
3116 (defcustom python-fill-comment-function 'python-fill-comment
3117 "Function to fill comments.
3118 This is the function used by `python-fill-paragraph' to
3123 (defcustom python-fill-string-function 'python-fill-string
3124 "Function to fill strings.
3125 This is the function used by `python-fill-paragraph' to
3130 (defcustom python-fill-decorator-function 'python-fill-decorator
3131 "Function to fill decorators.
3132 This is the function used by `python-fill-paragraph' to
3137 (defcustom python-fill-paren-function 'python-fill-paren
3138 "Function to fill parens.
3139 This is the function used by `python-fill-paragraph' to
3144 (defcustom python-fill-docstring-style 'pep-257
3145 "Style used to fill docstrings.
3146 This affects `python-fill-string' behavior with regards to
3147 triple quotes positioning.
3149 Possible values are `django', `onetwo', `pep-257', `pep-257-nn',
3150 `symmetric', and nil. A value of nil won't care about quotes
3151 position and will treat docstrings a normal string, any other
3152 value may result in one of the following docstring styles:
3157 Process foo, return bar.
3161 Process foo, return bar.
3163 If processing fails throw ProcessingError.
3168 \"\"\"Process foo, return bar.\"\"\"
3171 Process foo, return bar.
3173 If processing fails throw ProcessingError.
3179 \"\"\"Process foo, return bar.\"\"\"
3181 \"\"\"Process foo, return bar.
3183 If processing fails throw ProcessingError.
3189 \"\"\"Process foo, return bar.\"\"\"
3191 \"\"\"Process foo, return bar.
3193 If processing fails throw ProcessingError.
3198 \"\"\"Process foo, return bar.\"\"\"
3201 Process foo, return bar.
3203 If processing fails throw ProcessingError.
3206 (const :tag "Don't format docstrings" nil)
3207 (const :tag "Django's coding standards style." django)
3208 (const :tag "One newline and start and Two at end style." onetwo)
3209 (const :tag "PEP-257 with 2 newlines at end of string." pep-257)
3210 (const :tag "PEP-257 with 1 newline at end of string." pep-257-nn)
3211 (const :tag "Symmetric style." symmetric))
3214 (memq val '(django onetwo pep-257 pep-257-nn symmetric nil))))
3216 (defun python-fill-paragraph (&optional justify)
3217 "`fill-paragraph-function' handling multi-line strings and possibly comments.
3218 If any of the current line is in or at the end of a multi-line string,
3219 fill the string or the paragraph of it that point is in, preserving
3220 the string's indentation.
3221 Optional argument JUSTIFY defines if the paragraph should be justified."
3226 ((python-syntax-context 'comment)
3227 (funcall python-fill-comment-function justify))
3228 ;; Strings/Docstrings
3229 ((save-excursion (or (python-syntax-context 'string)
3230 (equal (string-to-syntax "|")
3231 (syntax-after (point)))))
3232 (funcall python-fill-string-function justify))
3234 ((equal (char-after (save-excursion
3235 (python-nav-beginning-of-statement))) ?@)
3236 (funcall python-fill-decorator-function justify))
3238 ((or (python-syntax-context 'paren)
3239 (looking-at (python-rx open-paren))
3241 (skip-syntax-forward "^(" (line-end-position))
3242 (looking-at (python-rx open-paren))))
3243 (funcall python-fill-paren-function justify))
3246 (defun python-fill-comment (&optional justify)
3247 "Comment fill function for `python-fill-paragraph'.
3248 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3249 (fill-comment-paragraph justify))
3251 (defun python-fill-string (&optional justify)
3252 "String fill function for `python-fill-paragraph'.
3253 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3254 (let* ((str-start-pos
3257 (or (python-syntax-context 'string)
3258 (and (equal (string-to-syntax "|")
3259 (syntax-after (point)))
3261 (num-quotes (python-syntax-count-quotes
3262 (char-after str-start-pos) str-start-pos))
3265 (goto-char (+ str-start-pos num-quotes))
3266 (or (re-search-forward (rx (syntax string-delimiter)) nil t)
3267 (goto-char (point-max)))
3270 ;; Docstring styles may vary for oneliners and multi-liners.
3271 (> (count-matches "\n" str-start-pos str-end-pos) 0))
3273 (pcase python-fill-docstring-style
3274 ;; delimiters-style is a cons cell with the form
3275 ;; (START-NEWLINES . END-NEWLINES). When any of the sexps
3276 ;; is NIL means to not add any newlines for start or end
3277 ;; of docstring. See `python-fill-docstring-style' for a
3278 ;; graphic idea of each style.
3279 (`django (cons 1 1))
3280 (`onetwo (and multi-line-p (cons 1 2)))
3281 (`pep-257 (and multi-line-p (cons nil 2)))
3282 (`pep-257-nn (and multi-line-p (cons nil 1)))
3283 (`symmetric (and multi-line-p (cons 1 1)))))
3284 (docstring-p (save-excursion
3285 ;; Consider docstrings those strings which
3286 ;; start on a line by themselves.
3287 (python-nav-beginning-of-statement)
3288 (and (= (point) str-start-pos))))
3289 (fill-paragraph-function))
3291 (narrow-to-region str-start-pos str-end-pos)
3292 (fill-paragraph justify))
3294 (when (and docstring-p python-fill-docstring-style)
3295 ;; Add the number of newlines indicated by the selected style
3296 ;; at the start of the docstring.
3297 (goto-char (+ str-start-pos num-quotes))
3298 (delete-region (point) (progn
3299 (skip-syntax-forward "> ")
3301 (and (car delimiters-style)
3302 (or (newline (car delimiters-style)) t)
3303 ;; Indent only if a newline is added.
3304 (indent-according-to-mode))
3305 ;; Add the number of newlines indicated by the selected style
3306 ;; at the end of the docstring.
3307 (goto-char (if (not (= str-end-pos (point-max)))
3308 (- str-end-pos num-quotes)
3310 (delete-region (point) (progn
3311 (skip-syntax-backward "> ")
3313 (and (cdr delimiters-style)
3314 ;; Add newlines only if string ends.
3315 (not (= str-end-pos (point-max)))
3316 (or (newline (cdr delimiters-style)) t)
3317 ;; Again indent only if a newline is added.
3318 (indent-according-to-mode))))) t)
3320 (defun python-fill-decorator (&optional _justify)
3321 "Decorator fill function for `python-fill-paragraph'.
3322 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3325 (defun python-fill-paren (&optional justify)
3326 "Paren fill function for `python-fill-paragraph'.
3327 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3329 (narrow-to-region (progn
3330 (while (python-syntax-context 'paren)
3331 (goto-char (1- (point))))
3332 (line-beginning-position))
3334 (when (not (python-syntax-context 'paren))
3336 (when (not (python-syntax-context 'paren))
3337 (skip-syntax-backward "^)")))
3338 (while (and (python-syntax-context 'paren)
3340 (goto-char (1+ (point))))
3342 (let ((paragraph-start "\f\\|[ \t]*$")
3343 (paragraph-separate ",")
3344 (fill-paragraph-function))
3345 (goto-char (point-min))
3346 (fill-paragraph justify))
3349 (python-indent-line)
3350 (goto-char (line-end-position))))
3356 (defcustom python-skeleton-autoinsert nil
3357 "Non-nil means template skeletons will be automagically inserted.
3358 This happens when pressing \"if<SPACE>\", for example, to prompt for
3364 (define-obsolete-variable-alias
3365 'python-use-skeletons 'python-skeleton-autoinsert "24.3")
3367 (defvar python-skeleton-available '()
3368 "Internal list of available skeletons.")
3370 (define-abbrev-table 'python-mode-skeleton-abbrev-table ()
3371 "Abbrev table for Python mode skeletons."
3373 ;; Allow / inside abbrevs.
3374 :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
3375 ;; Only expand in code.
3376 :enable-function (lambda ()
3378 (not (python-syntax-comment-or-string-p))
3379 python-skeleton-autoinsert)))
3381 (defmacro python-skeleton-define (name doc &rest skel)
3382 "Define a `python-mode' skeleton using NAME DOC and SKEL.
3383 The skeleton will be bound to python-skeleton-NAME and will
3384 be added to `python-mode-skeleton-abbrev-table'."
3385 (declare (indent 2))
3386 (let* ((name (symbol-name name))
3387 (function-name (intern (concat "python-skeleton-" name))))
3389 (define-abbrev python-mode-skeleton-abbrev-table
3390 ,name "" ',function-name :system t)
3391 (setq python-skeleton-available
3392 (cons ',function-name python-skeleton-available))
3393 (define-skeleton ,function-name
3395 (format "Insert %s statement." name))
3398 (define-abbrev-table 'python-mode-abbrev-table ()
3399 "Abbrev table for Python mode."
3400 :parents (list python-mode-skeleton-abbrev-table))
3402 (defmacro python-define-auxiliary-skeleton (name doc &optional &rest skel)
3403 "Define a `python-mode' auxiliary skeleton using NAME DOC and SKEL.
3404 The skeleton will be bound to python-skeleton-NAME."
3405 (declare (indent 2))
3406 (let* ((name (symbol-name name))
3407 (function-name (intern (concat "python-skeleton--" name)))
3409 "Add '%s' clause? " name)))
3412 `(< ,(format "%s:" name) \n \n
3414 `(define-skeleton ,function-name
3416 (format "Auxiliary skeleton for %s statement." name))
3418 (unless (y-or-n-p ,msg)
3422 (python-define-auxiliary-skeleton else nil)
3424 (python-define-auxiliary-skeleton except nil)
3426 (python-define-auxiliary-skeleton finally nil)
3428 (python-skeleton-define if nil
3432 ("other condition, %s: "
3436 '(python-skeleton--else) | ^)
3438 (python-skeleton-define while nil
3442 '(python-skeleton--else) | ^)
3444 (python-skeleton-define for nil
3448 '(python-skeleton--else) | ^)
3450 (python-skeleton-define import nil
3451 "Import from module: "
3452 "from " str & " " | -5
3454 ("Identifier: " str ", ") -2 \n _)
3456 (python-skeleton-define try nil
3462 "except " str ":" \n
3465 '(python-skeleton--except)
3466 '(python-skeleton--else)
3467 '(python-skeleton--finally) | ^)
3469 (python-skeleton-define def nil
3471 "def " str "(" ("Parameter, %s: "
3472 (unless (equal ?\( (char-before)) ", ")
3474 "\"\"\"" - "\"\"\"" \n
3477 (python-skeleton-define class nil
3479 "class " str "(" ("Inheritance, %s: "
3480 (unless (equal ?\( (char-before)) ", ")
3484 "\"\"\"" - "\"\"\"" \n
3487 (defun python-skeleton-add-menu-items ()
3488 "Add menu items to Python->Skeletons menu."
3489 (let ((skeletons (sort python-skeleton-available 'string<)))
3490 (dolist (skeleton skeletons)
3492 nil '("Python" "Skeletons")
3494 "Insert %s" (nth 2 (split-string (symbol-name skeleton) "-")))
3499 (defcustom python-ffap-setup-code
3500 "def __FFAP_get_module_path(module):
3503 path = __import__(module).__file__
3504 if path[-4:] == '.pyc' and os.path.exists(path[0:-1]):
3509 "Python code to get a module path."
3513 (defcustom python-ffap-string-code
3514 "__FFAP_get_module_path('''%s''')\n"
3515 "Python code used to get a string with the path of a module."
3519 (defun python-ffap-module-path (module)
3520 "Function for `ffap-alist' to return path for MODULE."
3522 (and (derived-mode-p 'inferior-python-mode)
3523 (get-buffer-process (current-buffer)))
3524 (python-shell-get-process))))
3528 (python-shell-send-string-no-output
3529 (format python-ffap-string-code module) process)))
3531 (substring-no-properties module-file 1 -1))))))
3535 (eval-after-load "ffap"
3537 (push '(python-mode . python-ffap-module-path) ffap-alist)
3538 (push '(inferior-python-mode . python-ffap-module-path) ffap-alist)))
3543 (defcustom python-check-command
3545 "Command used to check a Python file."
3549 (defcustom python-check-buffer-name
3550 "*Python check: %s*"
3551 "Buffer name used for check commands."
3555 (defvar python-check-custom-command nil
3558 (defun python-check (command)
3559 "Check a Python file (default current buffer's file).
3560 Runs COMMAND, a shell command, as if by `compile'.
3561 See `python-check-command' for the default."
3563 (list (read-string "Check command: "
3564 (or python-check-custom-command
3565 (concat python-check-command " "
3566 (shell-quote-argument
3568 (let ((name (buffer-file-name)))
3570 (file-name-nondirectory name)))
3572 (setq python-check-custom-command command)
3573 (save-some-buffers (not compilation-ask-about-save) nil)
3574 (let ((process-environment (python-shell-calculate-process-environment))
3575 (exec-path (python-shell-calculate-exec-path)))
3576 (compilation-start command nil
3578 (format python-check-buffer-name command)))))
3583 (defcustom python-eldoc-setup-code
3584 "def __PYDOC_get_help(obj):
3588 str_type = basestring
3591 if isinstance(obj, str_type):
3592 obj = eval(obj, globals())
3593 doc = inspect.getdoc(obj)
3594 if not doc and callable(obj):
3596 if inspect.isclass(obj) and hasattr(obj, '__init__'):
3597 target = obj.__init__
3603 args = inspect.formatargspec(
3604 *inspect.getargspec(target)
3607 doc = '{objtype} {name}{args}'.format(
3608 objtype=objtype, name=name, args=args
3611 doc = doc.splitlines()[0]
3615 "Python code to setup documentation retrieval."
3619 (defcustom python-eldoc-string-code
3620 "__PYDOC_get_help('''%s''')\n"
3621 "Python code used to get a string with the documentation of an object."
3625 (defun python-eldoc--get-doc-at-point (&optional force-input force-process)
3626 "Internal implementation to get documentation at point.
3627 If not FORCE-INPUT is passed then what `python-info-current-symbol'
3628 returns will be used. If not FORCE-PROCESS is passed what
3629 `python-shell-get-process' returns is used."
3630 (let ((process (or force-process (python-shell-get-process))))
3632 (let ((input (or force-input
3633 (python-info-current-symbol t))))
3635 ;; Prevent resizing the echo area when iPython is
3636 ;; enabled. Bug#18794.
3637 (python-util-strip-string
3638 (python-shell-send-string-no-output
3639 (format python-eldoc-string-code input)
3642 (defun python-eldoc-function ()
3643 "`eldoc-documentation-function' for Python.
3644 For this to work as best as possible you should call
3645 `python-shell-send-buffer' from time to time so context in
3646 inferior Python process is updated properly."
3647 (python-eldoc--get-doc-at-point))
3649 (defun python-eldoc-at-point (symbol)
3650 "Get help on SYMBOL using `help'.
3651 Interactively, prompt for symbol."
3653 (let ((symbol (python-info-current-symbol t))
3654 (enable-recursive-minibuffers t))
3655 (list (read-string (if symbol
3656 (format "Describe symbol (default %s): " symbol)
3657 "Describe symbol: ")
3659 (message (python-eldoc--get-doc-at-point symbol)))
3664 (defvar python-imenu-format-item-label-function
3665 'python-imenu-format-item-label
3666 "Imenu function used to format an item label.
3667 It must be a function with two arguments: TYPE and NAME.")
3669 (defvar python-imenu-format-parent-item-label-function
3670 'python-imenu-format-parent-item-label
3671 "Imenu function used to format a parent item label.
3672 It must be a function with two arguments: TYPE and NAME.")
3674 (defvar python-imenu-format-parent-item-jump-label-function
3675 'python-imenu-format-parent-item-jump-label
3676 "Imenu function used to format a parent jump item label.
3677 It must be a function with two arguments: TYPE and NAME.")
3679 (defun python-imenu-format-item-label (type name)
3680 "Return Imenu label for single node using TYPE and NAME."
3681 (format "%s (%s)" name type))
3683 (defun python-imenu-format-parent-item-label (type name)
3684 "Return Imenu label for parent node using TYPE and NAME."
3685 (format "%s..." (python-imenu-format-item-label type name)))
3687 (defun python-imenu-format-parent-item-jump-label (type _name)
3688 "Return Imenu label for parent node jump using TYPE and NAME."
3689 (if (string= type "class")
3690 "*class definition*"
3691 "*function definition*"))
3693 (defun python-imenu--put-parent (type name pos tree)
3694 "Add the parent with TYPE, NAME and POS to TREE."
3696 (funcall python-imenu-format-item-label-function type name))
3698 (funcall python-imenu-format-parent-item-jump-label-function type name)))
3701 (cons label (cons (cons jump-label pos) tree)))))
3703 (defun python-imenu--build-tree (&optional min-indent prev-indent tree)
3704 "Recursively build the tree of nested definitions of a node.
3705 Arguments MIN-INDENT, PREV-INDENT and TREE are internal and should
3706 not be passed explicitly unless you know what you are doing."
3707 (setq min-indent (or min-indent 0)
3708 prev-indent (or prev-indent python-indent-offset))
3709 (let* ((pos (python-nav-backward-defun))
3711 (name (when (and pos (looking-at python-nav-beginning-of-defun-regexp))
3712 (let ((split (split-string (match-string-no-properties 0))))
3713 (setq type (car split))
3716 (funcall python-imenu-format-item-label-function type name)))
3717 (indent (current-indentation))
3718 (children-indent-limit (+ python-indent-offset min-indent)))
3720 ;; Nothing found, probably near to bobp.
3722 ((<= indent min-indent)
3723 ;; The current indentation points that this is a parent
3724 ;; node, add it to the tree and stop recursing.
3725 (python-imenu--put-parent type name pos tree))
3727 (python-imenu--build-tree
3730 (if (<= indent children-indent-limit)
3731 ;; This lies within the children indent offset range,
3732 ;; so it's a normal child of its parent (i.e., not
3733 ;; a child of a child).
3734 (cons (cons label pos) tree)
3735 ;; Oh no, a child of a child?! Fear not, we
3736 ;; know how to roll. We recursively parse these by
3737 ;; swapping prev-indent and min-indent plus adding this
3738 ;; newly found item to a fresh subtree. This works, I
3741 (python-imenu--build-tree
3742 prev-indent indent (list (cons label pos)))
3745 (defun python-imenu-create-index ()
3746 "Return tree Imenu alist for the current Python buffer.
3747 Change `python-imenu-format-item-label-function',
3748 `python-imenu-format-parent-item-label-function',
3749 `python-imenu-format-parent-item-jump-label-function' to
3750 customize how labels are formatted."
3751 (goto-char (point-max))
3754 (while (setq tree (python-imenu--build-tree))
3755 (setq index (cons tree index)))
3758 (defun python-imenu-create-flat-index (&optional alist prefix)
3759 "Return flat outline of the current Python buffer for Imenu.
3760 Optional argument ALIST is the tree to be flattened; when nil
3761 `python-imenu-build-index' is used with
3762 `python-imenu-format-parent-item-jump-label-function'
3763 `python-imenu-format-parent-item-label-function'
3764 `python-imenu-format-item-label-function' set to
3765 (lambda (type name) name)
3766 Optional argument PREFIX is used in recursive calls and should
3767 not be passed explicitly.
3774 (\"decorator\" . 173)
3777 (\"wrapped_f\" . 393))))
3783 (\"decorator\" . 173)
3784 (\"decorator.wrap\" . 353)
3785 (\"decorator.wrapped_f\" . 393))"
3786 ;; Inspired by imenu--flatten-index-alist removed in revno 21853.
3791 (let ((name (if prefix
3792 (concat prefix "." (car item))
3795 (cond ((or (numberp pos) (markerp pos))
3796 (list (cons name pos)))
3799 (cons name (cdar pos))
3800 (python-imenu-create-flat-index (cddr item) name))))))
3802 (let* ((fn (lambda (_type name) name))
3803 (python-imenu-format-item-label-function fn)
3804 (python-imenu-format-parent-item-label-function fn)
3805 (python-imenu-format-parent-item-jump-label-function fn))
3806 (python-imenu-create-index))))))
3811 (defun python-info-current-defun (&optional include-type)
3812 "Return name of surrounding function with Python compatible dotty syntax.
3813 Optional argument INCLUDE-TYPE indicates to include the type of the defun.
3814 This function can be used as the value of `add-log-current-defun-function'
3815 since it returns nil if point is not inside a defun."
3821 (starting-indentation (current-indentation))
3822 (starting-pos (point))
3827 (while (python-nav-beginning-of-defun 1)
3828 (when (save-match-data
3830 (or (not last-indent)
3831 (< (current-indentation) last-indent))
3835 ;; If this is the first run, we may add
3836 ;; the current defun at point.
3837 (setq first-run nil)
3838 (goto-char starting-pos)
3839 (python-nav-beginning-of-statement)
3840 (beginning-of-line 1)
3842 python-nav-beginning-of-defun-regexp)))
3846 (+ (current-indentation)
3847 python-indent-offset)))
3848 (if (< starting-indentation min-indent)
3849 ;; If the starting indentation is not
3850 ;; within the min defun indent make the
3853 ;; Else go to the end of defun and add
3854 ;; up the current indentation to the
3856 (python-nav-end-of-defun)
3858 (if (>= (current-indentation) min-indent)
3859 (1+ (current-indentation))
3861 (save-match-data (setq last-indent (current-indentation)))
3862 (if (or (not include-type) type)
3863 (setq names (cons (match-string-no-properties 1) names))
3864 (let ((match (split-string (match-string-no-properties 0))))
3865 (setq type (car match))
3866 (setq names (cons (cadr match) names)))))
3867 ;; Stop searching ASAP.
3868 (and (= (current-indentation) 0) (throw 'exit t))))
3870 (concat (and type (format "%s " type))
3871 (mapconcat 'identity names ".")))))))
3873 (defun python-info-current-symbol (&optional replace-self)
3874 "Return current symbol using dotty syntax.
3875 With optional argument REPLACE-SELF convert \"self\" to current
3878 (and (not (python-syntax-comment-or-string-p))
3879 (with-syntax-table python-dotty-syntax-table
3880 (let ((sym (symbol-at-point)))
3882 (substring-no-properties (symbol-name sym))))))))
3884 (if (not replace-self)
3886 (let ((current-defun (python-info-current-defun)))
3887 (if (not current-defun)
3889 (replace-regexp-in-string
3890 (python-rx line-start word-start "self" word-end ?.)
3892 (mapconcat 'identity
3893 (butlast (split-string current-defun "\\."))
3897 (defun python-info-statement-starts-block-p ()
3898 "Return non-nil if current statement opens a block."
3900 (python-nav-beginning-of-statement)
3901 (looking-at (python-rx block-start))))
3903 (defun python-info-statement-ends-block-p ()
3904 "Return non-nil if point is at end of block."
3905 (let ((end-of-block-pos (save-excursion
3906 (python-nav-end-of-block)))
3907 (end-of-statement-pos (save-excursion
3908 (python-nav-end-of-statement))))
3909 (and end-of-block-pos end-of-statement-pos
3910 (= end-of-block-pos end-of-statement-pos))))
3912 (defun python-info-beginning-of-statement-p ()
3913 "Return non-nil if point is at beginning of statement."
3914 (= (point) (save-excursion
3915 (python-nav-beginning-of-statement)
3918 (defun python-info-end-of-statement-p ()
3919 "Return non-nil if point is at end of statement."
3920 (= (point) (save-excursion
3921 (python-nav-end-of-statement)
3924 (defun python-info-beginning-of-block-p ()
3925 "Return non-nil if point is at beginning of block."
3926 (and (python-info-beginning-of-statement-p)
3927 (python-info-statement-starts-block-p)))
3929 (defun python-info-end-of-block-p ()
3930 "Return non-nil if point is at end of block."
3931 (and (python-info-end-of-statement-p)
3932 (python-info-statement-ends-block-p)))
3934 (define-obsolete-function-alias
3935 'python-info-closing-block
3936 'python-info-dedenter-opening-block-position "24.4")
3938 (defun python-info-dedenter-opening-block-position ()
3939 "Return the point of the closest block the current line closes.
3940 Returns nil if point is not on a dedenter statement or no opening
3941 block can be detected. The latter case meaning current file is
3942 likely an invalid python file."
3943 (let ((positions (python-info-dedenter-opening-block-positions))
3944 (indentation (current-indentation))
3946 (while (and (not position)
3949 (goto-char (car positions))
3950 (if (<= (current-indentation) indentation)
3951 (setq position (car positions))
3952 (setq positions (cdr positions)))))
3955 (defun python-info-dedenter-opening-block-positions ()
3956 "Return points of blocks the current line may close sorted by closer.
3957 Returns nil if point is not on a dedenter statement or no opening
3958 block can be detected. The latter case meaning current file is
3959 likely an invalid python file."
3961 (let ((dedenter-pos (python-info-dedenter-statement-p)))
3963 (goto-char dedenter-pos)
3964 (let* ((pairs '(("elif" "elif" "if")
3965 ("else" "if" "elif" "except" "for" "while")
3966 ("except" "except" "try")
3967 ("finally" "else" "except" "try")))
3968 (dedenter (match-string-no-properties 0))
3969 (possible-opening-blocks (cdr (assoc-string dedenter pairs)))
3970 (collected-indentations)
3973 (while (python-nav--syntactically
3975 (re-search-backward (python-rx block-start) nil t))
3977 (let ((indentation (current-indentation)))
3978 (when (and (not (memq indentation collected-indentations))
3979 (or (not collected-indentations)
3980 (< indentation (apply #'min collected-indentations))))
3981 (setq collected-indentations
3982 (cons indentation collected-indentations))
3983 (when (member (match-string-no-properties 0)
3984 possible-opening-blocks)
3985 (setq opening-blocks (cons (point) opening-blocks))))
3986 (when (zerop indentation)
3987 (throw 'exit nil)))))
3989 (nreverse opening-blocks))))))
3991 (define-obsolete-function-alias
3992 'python-info-closing-block-message
3993 'python-info-dedenter-opening-block-message "24.4")
3995 (defun python-info-dedenter-opening-block-message ()
3996 "Message the first line of the block the current statement closes."
3997 (let ((point (python-info-dedenter-opening-block-position)))
4001 (message "Closes %s" (save-excursion
4004 (point) (line-end-position))))))))
4006 (defun python-info-dedenter-statement-p ()
4007 "Return point if current statement is a dedenter.
4008 Sets `match-data' to the keyword that starts the dedenter
4011 (python-nav-beginning-of-statement)
4012 (when (and (not (python-syntax-context-type))
4013 (looking-at (python-rx dedenter)))
4016 (defun python-info-line-ends-backslash-p (&optional line-number)
4017 "Return non-nil if current line ends with backslash.
4018 With optional argument LINE-NUMBER, check that line instead."
4023 (python-util-goto-line line-number))
4024 (while (and (not (eobp))
4025 (goto-char (line-end-position))
4026 (python-syntax-context 'paren)
4027 (not (equal (char-before (point)) ?\\)))
4029 (when (equal (char-before) ?\\)
4032 (defun python-info-beginning-of-backslash (&optional line-number)
4033 "Return the point where the backslashed line start.
4034 Optional argument LINE-NUMBER forces the line number to check against."
4039 (python-util-goto-line line-number))
4040 (when (python-info-line-ends-backslash-p)
4041 (while (save-excursion
4042 (goto-char (line-beginning-position))
4043 (python-syntax-context 'paren))
4045 (back-to-indentation)
4048 (defun python-info-continuation-line-p ()
4049 "Check if current line is continuation of another.
4050 When current line is continuation of another return the point
4051 where the continued line ends."
4055 (let* ((context-type (progn
4056 (back-to-indentation)
4057 (python-syntax-context-type)))
4058 (line-start (line-number-at-pos))
4059 (context-start (when context-type
4060 (python-syntax-context context-type))))
4061 (cond ((equal context-type 'paren)
4062 ;; Lines inside a paren are always a continuation line
4063 ;; (except the first one).
4064 (python-util-forward-comment -1)
4066 ((member context-type '(string comment))
4067 ;; move forward an roll again
4068 (goto-char context-start)
4069 (python-util-forward-comment)
4070 (python-info-continuation-line-p))
4072 ;; Not within a paren, string or comment, the only way
4073 ;; we are dealing with a continuation line is that
4074 ;; previous line contains a backslash, and this can
4075 ;; only be the previous line from current
4076 (back-to-indentation)
4077 (python-util-forward-comment -1)
4078 (when (and (equal (1- line-start) (line-number-at-pos))
4079 (python-info-line-ends-backslash-p))
4080 (point-marker))))))))
4082 (defun python-info-block-continuation-line-p ()
4083 "Return non-nil if current line is a continuation of a block."
4085 (when (python-info-continuation-line-p)
4087 (back-to-indentation)
4088 (when (looking-at (python-rx block-start))
4091 (defun python-info-assignment-continuation-line-p ()
4092 "Check if current line is a continuation of an assignment.
4093 When current line is continuation of another with an assignment
4094 return the point of the first non-blank character after the
4097 (when (python-info-continuation-line-p)
4099 (back-to-indentation)
4100 (when (and (not (looking-at (python-rx block-start)))
4101 (and (re-search-forward (python-rx not-simple-operator
4103 not-simple-operator)
4104 (line-end-position) t)
4105 (not (python-syntax-context-type))))
4106 (skip-syntax-forward "\s")
4109 (defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss)
4110 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS."
4111 (and (not (python-syntax-context-type (or syntax-ppss (syntax-ppss))))
4113 (beginning-of-line 1)
4114 (looking-at python-nav-beginning-of-defun-regexp))))
4116 (defun python-info-current-line-comment-p ()
4117 "Return non-nil if current line is a comment line."
4119 (or (char-after (+ (line-beginning-position) (current-indentation))) ?_)
4122 (defun python-info-current-line-empty-p ()
4123 "Return non-nil if current line is empty, ignoring whitespace."
4125 (beginning-of-line 1)
4127 (python-rx line-start (* whitespace)
4128 (group (* not-newline))
4129 (* whitespace) line-end))
4130 (string-equal "" (match-string-no-properties 1))))
4133 ;;; Utility functions
4135 (defun python-util-goto-line (line-number)
4136 "Move point to LINE-NUMBER."
4137 (goto-char (point-min))
4138 (forward-line (1- line-number)))
4140 ;; Stolen from org-mode
4141 (defun python-util-clone-local-variables (from-buffer &optional regexp)
4142 "Clone local variables from FROM-BUFFER.
4143 Optional argument REGEXP selects variables to clone and defaults
4147 (and (symbolp (car pair))
4148 (string-match (or regexp "^python-")
4149 (symbol-name (car pair)))
4150 (set (make-local-variable (car pair))
4152 (buffer-local-variables from-buffer)))
4154 (defvar comint-last-prompt-overlay) ; Shut up, byte compiler.
4156 (defun python-util-comint-last-prompt ()
4157 "Return comint last prompt overlay start and end.
4158 This is for compatibility with Emacs < 24.4."
4159 (cond ((bound-and-true-p comint-last-prompt-overlay)
4160 (cons (overlay-start comint-last-prompt-overlay)
4161 (overlay-end comint-last-prompt-overlay)))
4162 ((bound-and-true-p comint-last-prompt)
4166 (defun python-util-forward-comment (&optional direction)
4167 "Python mode specific version of `forward-comment'.
4168 Optional argument DIRECTION defines the direction to move to."
4169 (let ((comment-start (python-syntax-context 'comment))
4170 (factor (if (< (or direction 0) 0)
4174 (goto-char comment-start))
4175 (forward-comment factor)))
4177 (defun python-util-list-directories (directory &optional predicate max-depth)
4178 "List DIRECTORY subdirs, filtered by PREDICATE and limited by MAX-DEPTH.
4179 Argument PREDICATE defaults to `identity' and must be a function
4180 that takes one argument (a full path) and returns non-nil for
4181 allowed files. When optional argument MAX-DEPTH is non-nil, stop
4182 searching when depth is reached, else don't limit."
4183 (let* ((dir (expand-file-name directory))
4184 (dir-length (length dir))
4185 (predicate (or predicate #'identity))
4186 (to-scan (list dir))
4189 (let ((current-dir (car to-scan)))
4190 (when (funcall predicate current-dir)
4191 (setq tally (cons current-dir tally)))
4192 (setq to-scan (append (cdr to-scan)
4193 (python-util-list-files
4194 current-dir #'file-directory-p)
4196 (when (and max-depth
4198 (length (split-string
4199 (substring current-dir dir-length)
4201 (setq to-scan nil))))
4204 (defun python-util-list-files (dir &optional predicate)
4205 "List files in DIR, filtering with PREDICATE.
4206 Argument PREDICATE defaults to `identity' and must be a function
4207 that takes one argument (a full path) and returns non-nil for
4209 (let ((dir-name (file-name-as-directory dir)))
4211 (mapcar (lambda (file-name)
4212 (let ((full-file-name (expand-file-name file-name dir-name)))
4214 (not (member file-name '("." "..")))
4215 (funcall (or predicate #'identity) full-file-name))
4216 (list full-file-name))))
4217 (directory-files dir-name)))))
4219 (defun python-util-list-packages (dir &optional max-depth)
4220 "List packages in DIR, limited by MAX-DEPTH.
4221 When optional argument MAX-DEPTH is non-nil, stop searching when
4222 depth is reached, else don't limit."
4223 (let* ((dir (expand-file-name dir))
4224 (parent-dir (file-name-directory
4225 (directory-file-name
4226 (file-name-directory
4227 (file-name-as-directory dir)))))
4228 (subpath-length (length parent-dir)))
4231 (replace-regexp-in-string
4232 (rx (or ?\\ ?/)) "." (substring file-name subpath-length)))
4233 (python-util-list-directories
4234 (directory-file-name dir)
4236 (file-exists-p (expand-file-name "__init__.py" dir)))
4239 (defun python-util-popn (lst n)
4240 "Return LST first N elements.
4241 N should be an integer, when negative its opposite is used.
4242 When N is bigger than the length of LST, the list is
4244 (let* ((n (min (abs n)))
4250 (setq acc (cons (car lst) acc)
4255 (defun python-util-text-properties-replace-name
4256 (from to &optional start end)
4257 "Replace properties named FROM to TO, keeping its value.
4258 Arguments START and END narrow the buffer region to work on."
4260 (goto-char (or start (point-min)))
4262 (let ((plist (text-properties-at (point)))
4263 (next-change (or (next-property-change (point) (current-buffer))
4264 (or end (point-max)))))
4265 (when (plist-get plist from)
4266 (let* ((face (plist-get plist from))
4267 (plist (plist-put plist from nil))
4268 (plist (plist-put plist to face)))
4269 (set-text-properties (point) next-change plist (current-buffer))))
4270 (goto-char next-change)))))
4272 (defun python-util-strip-string (string)
4273 "Strip STRING whitespace and newlines from end and beginning."
4274 (replace-regexp-in-string
4275 (rx (or (: string-start (* (any whitespace ?\r ?\n)))
4276 (: (* (any whitespace ?\r ?\n)) string-end)))
4280 (defun python-util-valid-regexp-p (regexp)
4281 "Return non-nil if REGEXP is valid."
4282 (ignore-errors (string-match regexp "") t))
4285 (defun python-electric-pair-string-delimiter ()
4286 (when (and electric-pair-mode
4287 (memq last-command-event '(?\" ?\'))
4289 (while (eq (char-before (- (point) count)) last-command-event)
4292 (eq (char-after) last-command-event))
4293 (save-excursion (insert (make-string 2 last-command-event)))))
4295 (defvar electric-indent-inhibit)
4298 (define-derived-mode python-mode prog-mode "Python"
4299 "Major mode for editing Python files.
4301 \\{python-mode-map}"
4302 (set (make-local-variable 'tab-width) 8)
4303 (set (make-local-variable 'indent-tabs-mode) nil)
4305 (set (make-local-variable 'comment-start) "# ")
4306 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
4308 (set (make-local-variable 'parse-sexp-lookup-properties) t)
4309 (set (make-local-variable 'parse-sexp-ignore-comments) t)
4311 (set (make-local-variable 'forward-sexp-function)
4312 'python-nav-forward-sexp)
4314 (set (make-local-variable 'font-lock-defaults)
4315 '(python-font-lock-keywords nil nil nil nil))
4317 (set (make-local-variable 'syntax-propertize-function)
4318 python-syntax-propertize-function)
4320 (set (make-local-variable 'indent-line-function)
4321 #'python-indent-line-function)
4322 (set (make-local-variable 'indent-region-function) #'python-indent-region)
4323 ;; Because indentation is not redundant, we cannot safely reindent code.
4324 (set (make-local-variable 'electric-indent-inhibit) t)
4325 (set (make-local-variable 'electric-indent-chars)
4326 (cons ?: electric-indent-chars))
4328 ;; Add """ ... """ pairing to electric-pair-mode.
4329 (add-hook 'post-self-insert-hook
4330 #'python-electric-pair-string-delimiter 'append t)
4332 (set (make-local-variable 'paragraph-start) "\\s-*$")
4333 (set (make-local-variable 'fill-paragraph-function)
4334 #'python-fill-paragraph)
4336 (set (make-local-variable 'beginning-of-defun-function)
4337 #'python-nav-beginning-of-defun)
4338 (set (make-local-variable 'end-of-defun-function)
4339 #'python-nav-end-of-defun)
4341 (add-hook 'completion-at-point-functions
4342 #'python-completion-at-point nil 'local)
4344 (add-hook 'post-self-insert-hook
4345 #'python-indent-post-self-insert-function 'append 'local)
4347 (set (make-local-variable 'imenu-create-index-function)
4348 #'python-imenu-create-index)
4350 (set (make-local-variable 'add-log-current-defun-function)
4351 #'python-info-current-defun)
4353 (add-hook 'which-func-functions #'python-info-current-defun nil t)
4355 (set (make-local-variable 'skeleton-further-elements)
4357 (< '(backward-delete-char-untabify (min python-indent-offset
4359 (^ '(- (1+ (current-indentation))))))
4361 (set (make-local-variable 'eldoc-documentation-function)
4362 #'python-eldoc-function)
4364 (add-to-list 'hs-special-modes-alist
4365 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
4367 (python-nav-end-of-defun))
4370 (set (make-local-variable 'outline-regexp)
4371 (python-rx (* space) block-start))
4372 (set (make-local-variable 'outline-heading-end-regexp) ":[^\n]*\n")
4373 (set (make-local-variable 'outline-level)
4375 "`outline-level' function for Python mode."
4376 (1+ (/ (current-indentation) python-indent-offset))))
4378 (python-skeleton-add-menu-items)
4380 (make-local-variable 'python-shell-internal-buffer)
4382 (when python-indent-guess-indent-offset
4383 (python-indent-guess-indent-offset)))
4390 ;; indent-tabs-mode: nil
4393 ;;; python.el ends here