1 ;;; python.el -- Python's flying circus support for Emacs
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Fabián E. Gallina <fabian@anue.biz>
10 ;; This file is NOT part of GNU Emacs.
12 ;; python.el is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; python.el is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with python.el. If not, see <http://www.gnu.org/licenses/>.
27 ;; Major mode for editing Python files with some fontification and
28 ;; indentation bits extracted from original Dave Love's python.el
29 ;; found in GNU/Emacs.
31 ;; While it probably has less features than Dave Love's python.el and
32 ;; PSF's python-mode.el it provides the main stuff you'll need while
33 ;; keeping it simple :)
35 ;; Implements Syntax highlighting, Indentation, Movement, Shell
36 ;; interaction, Shell completion, Pdb tracking, Symbol completion,
39 ;; Syntax highlighting: Fontification of code is provided and supports
40 ;; python's triple quoted strings properly.
42 ;; Indentation: Automatic indentation with indentation cycling is
43 ;; provided, it allows you to navigate different available levels of
44 ;; indentation by hitting <tab> several times.
46 ;; Movement: `beginning-of-defun' and `end-of-defun' functions are
47 ;; properly implemented. A `beginning-of-innermost-defun' is defined
48 ;; to navigate nested defuns.
50 ;; Shell interaction: is provided and allows you easily execute any
51 ;; block of code of your current buffer in an inferior Python process.
53 ;; Shell completion: hitting tab will try to complete the current
54 ;; word. Shell completion is implemented in a manner that if you
55 ;; change the `python-shell-interpreter' to any other (for example
56 ;; IPython) it should be easy to integrate another way to calculate
57 ;; completions. You just need to specify your custom
58 ;; `python-shell-completion-setup-code' and
59 ;; `python-shell-completion-strings-code'
61 ;; Pdb tracking: when you execute a block of code that contains some
62 ;; call to pdb (or ipdb) it will prompt the block of code and will
63 ;; follow the execution of pdb marking the current line with an arrow.
65 ;; Symbol completion: you can complete the symbol at point. It uses
66 ;; the shell completion in background so you should run
67 ;; `python-shell-send-buffer' from time to time to get better results.
69 ;; FFAP: You can find the filename for a given module when using ffap
70 ;; out of the box. This feature needs an inferior python shell
73 ;; Code check: Check the current file for errors using
74 ;; `python-check-command'
76 ;; Eldoc: returns documentation for object at point by using the
77 ;; inferior python subprocess to inspect its documentation. As you
78 ;; might guessed you should run `python-shell-send-buffer' from time
79 ;; to time to get better results too.
81 ;; If you used python-mode.el you probably will miss auto-indentation
82 ;; when inserting newlines. To achieve the same behavior you have
85 ;; 1) Use GNU/Emacs' standard binding for `newline-and-indent': C-j.
87 ;; 2) Add the following hook in your .emacs:
89 ;; (add-hook 'python-mode-hook
91 ;; (define-key python-mode-map "\C-m" 'newline-and-indent)))
93 ;; I'd recommend the first one since you'll get the same behavior for
94 ;; all modes out-of-the-box.
98 ;; Add this to your .emacs:
100 ;; (add-to-list 'load-path "/folder/containing/file")
105 ;; Ordered by priority:
107 ;; Better decorator support for beginning of defun
109 ;; Review code and cleanup
111 ;; (Perhaps) some skeletons (I never use them because of yasnippet)
116 (require 'ansi-color
)
122 (autoload 'comint-mode
"comint")
125 (add-to-list 'auto-mode-alist
(cons (purecopy "\\.py\\'") 'python-mode
))
127 (add-to-list 'interpreter-mode-alist
(cons (purecopy "python") 'python-mode
))
130 "Python Language's flying circus support for Emacs."
133 :link
'(emacs-commentary-link "python"))
138 (defvar python-mode-map
139 (let ((map (make-sparse-keymap)))
141 (define-key map
"\177" 'python-indent-dedent-line-backspace
)
142 (define-key map
(kbd "<backtab>") 'python-indent-dedent-line
)
143 (define-key map
"\C-c<" 'python-indent-shift-left
)
144 (define-key map
"\C-c>" 'python-indent-shift-right
)
146 (define-key map
"\C-c\C-s" 'python-shell-send-string
)
147 (define-key map
"\C-c\C-r" 'python-shell-send-region
)
148 (define-key map
"\C-\M-x" 'python-shell-send-defun
)
149 (define-key map
"\C-c\C-c" 'python-shell-send-buffer
)
150 (define-key map
"\C-c\C-l" 'python-shell-send-file
)
151 (define-key map
"\C-c\C-z" 'python-shell-switch-to-shell
)
152 ;; Some util commands
153 (define-key map
"\C-c\C-v" 'python-check
)
154 (define-key map
"\C-c\C-f" 'python-eldoc-at-point
)
156 (substitute-key-definition 'complete-symbol
'completion-at-point
158 (easy-menu-define python-menu map
"Python Mode menu"
160 :help
"Python-specific Features"
161 ["Shift region left" python-indent-shift-left
:active mark-active
162 :help
"Shift region left by a single indentation step"]
163 ["Shift region right" python-indent-shift-right
:active mark-active
164 :help
"Shift region right by a single indentation step"]
166 ["Mark def/class" mark-defun
167 :help
"Mark outermost definition around point"]
169 ["Start of def/class" beginning-of-defun
170 :help
"Go to start of outermost definition around point"]
171 ["Start of def/class" python-beginning-of-innermost-defun
172 :help
"Go to start of innermost definition around point"]
173 ["End of def/class" end-of-defun
174 :help
"Go to end of definition around point"]
176 ["Start interpreter" run-python
177 :help
"Run inferior Python process in a separate buffer"]
178 ["Switch to shell" python-shell-switch-to-shell
179 :help
"Switch to running inferior Python process"]
180 ["Eval string" python-shell-send-string
181 :help
"Eval string in inferior Python session"]
182 ["Eval buffer" python-shell-send-buffer
183 :help
"Eval buffer in inferior Python session"]
184 ["Eval region" python-shell-send-region
185 :help
"Eval region in inferior Python session"]
186 ["Eval defun" python-shell-send-defun
187 :help
"Eval defun in inferior Python session"]
188 ["Eval file" python-shell-send-file
189 :help
"Eval file in inferior Python session"]
190 ["Debugger" pdb
:help
"Run pdb under GUD"]
192 ["Check file" python-check
193 :help
"Check file for errors"]
194 ["Help on symbol" python-eldoc-at-point
195 :help
"Get help on symbol at point"]
196 ["Complete symbol" completion-at-point
197 :help
"Complete symbol before point"]))
199 "Keymap for `python-mode'.")
202 ;;; Python specialized rx
204 (defconst python-rx-constituents
206 `(block-start .
,(rx symbol-start
207 (or "def" "class" "if" "elif" "else" "try"
208 "except" "finally" "for" "while" "with")
210 `(defun .
,(rx symbol-start
(or "def" "class") symbol-end
))
211 `(open-paren .
,(rx (or "{" "[" "(")))
212 `(close-paren .
,(rx (or "}" "]" ")")))
213 `(simple-operator .
,(rx (any ?
+ ?- ?
/ ?
& ?^ ?~ ?| ?
* ?
< ?
> ?
= ?%
)))
214 `(not-simple-operator .
,(rx (not (any ?
+ ?- ?
/ ?
& ?^ ?~ ?| ?
* ?
< ?
> ?
= ?%
))))
215 `(operator .
,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
216 "=" "%" "**" "//" "<<" ">>" "<=" "!="
217 "==" ">=" "is" "not")))
218 `(assignment-operator .
,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**="
219 ">>=" "<<=" "&=" "^=" "|=")))))
221 (defmacro python-rx
(&rest regexps
)
222 "Python mode especialized rx macro which supports common python named REGEXPS."
223 (let ((rx-constituents (append python-rx-constituents rx-constituents
)))
224 (cond ((null regexps
)
227 (rx-to-string `(and ,@regexps
) t
))
229 (rx-to-string (car regexps
) t
)))))
232 ;;; Font-lock and syntax
234 (defvar python-font-lock-keywords
237 (or "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with"
238 "assert" "else" "if" "pass" "yield" "break" "except" "import"
239 "print" "class" "exec" "in" "raise" "continue" "finally" "is"
240 "return" "def" "for" "lambda" "try" "self")
243 (,(rx symbol-start
"def" (1+ space
) (group (1+ (or word ?_
))))
244 (1 font-lock-function-name-face
))
246 (,(rx symbol-start
"class" (1+ space
) (group (1+ (or word ?_
))))
247 (1 font-lock-type-face
))
249 (,(rx symbol-start
(group "None" symbol-end
))
250 (1 font-lock-constant-face
))
252 (,(rx line-start
(* (any " \t")) (group "@" (1+ (or word ?_
))
253 (0+ "." (1+ (or word ?_
)))))
254 (1 font-lock-type-face
))
255 ;; Builtin Exceptions
257 (or "ArithmeticError" "AssertionError" "AttributeError"
258 "BaseException" "BufferError" "BytesWarning" "DeprecationWarning"
259 "EOFError" "EnvironmentError" "Exception" "FloatingPointError"
260 "FutureWarning" "GeneratorExit" "IOError" "ImportError"
261 "ImportWarning" "IndentationError" "IndexError" "KeyError"
262 "KeyboardInterrupt" "LookupError" "MemoryError" "NameError"
263 "NotImplemented" "NotImplementedError" "OSError" "OverflowError"
264 "PendingDeprecationWarning" "ReferenceError" "RuntimeError"
265 "RuntimeWarning" "StandardError" "StopIteration" "SyntaxError"
266 "SyntaxWarning" "SystemError" "SystemExit" "TabError" "TypeError"
267 "UnboundLocalError" "UnicodeDecodeError" "UnicodeEncodeError"
268 "UnicodeError" "UnicodeTranslateError" "UnicodeWarning"
269 "UserWarning" "ValueError" "Warning" "ZeroDivisionError")
270 symbol-end
) . font-lock-type-face
)
272 (,(rx (or line-start
(not (any ". \t"))) (* (any " \t")) symbol-start
274 (or "_" "__debug__" "__doc__" "__import__" "__name__" "__package__"
275 "abs" "all" "any" "apply" "basestring" "bin" "bool" "buffer"
276 "bytearray" "bytes" "callable" "chr" "classmethod" "cmp" "coerce"
277 "compile" "complex" "copyright" "credits" "delattr" "dict" "dir"
278 "divmod" "enumerate" "eval" "execfile" "exit" "file" "filter"
279 "float" "format" "frozenset" "getattr" "globals" "hasattr" "hash"
280 "help" "hex" "id" "input" "int" "intern" "isinstance" "issubclass"
281 "iter" "len" "license" "list" "locals" "long" "map" "max" "min"
282 "next" "object" "oct" "open" "ord" "pow" "print" "property" "quit"
283 "range" "raw_input" "reduce" "reload" "repr" "reversed" "round"
284 "set" "setattr" "slice" "sorted" "staticmethod" "str" "sum"
285 "super" "tuple" "type" "unichr" "unicode" "vars" "xrange" "zip"
286 "True" "False" "Ellipsis")) symbol-end
)
287 (1 font-lock-builtin-face
))
289 ;; support for a = b = c = 5
291 (let ((re (python-rx (group (+ (any word ?. ?_
)))
292 (? ?\
[ (+ (not (any ?\
]))) ?\
]) (* space
)
293 assignment-operator
)))
294 (when (re-search-forward re limit t
)
295 (while (and (not (equal (nth 0 (syntax-ppss)) 0))
296 (re-search-forward re limit t
)))
297 (if (and (equal (nth 0 (syntax-ppss)) 0)
298 (not (equal (char-after (point-marker)) ?
=)))
300 (set-match-data nil
)))))
301 (1 font-lock-variable-name-face nil nil
))
302 ;; support for a, b, c = (1, 2, 3)
304 (let ((re (python-rx (group (+ (any word ?. ?_
))) (* space
)
305 (* ?
, (* space
) (+ (any word ?. ?_
)) (* space
))
306 ?
, (* space
) (+ (any word ?. ?_
)) (* space
)
307 assignment-operator
)))
308 (when (and (re-search-forward re limit t
)
309 (goto-char (nth 3 (match-data))))
310 (while (and (not (equal (nth 0 (syntax-ppss)) 0))
311 (re-search-forward re limit t
))
312 (goto-char (nth 3 (match-data))))
313 (if (equal (nth 0 (syntax-ppss)) 0)
315 (set-match-data nil
)))))
316 (1 font-lock-variable-name-face nil nil
))))
318 ;; Fixme: Is there a better way?
319 (defconst python-font-lock-syntactic-keywords
320 ;; First avoid a sequence preceded by an odd number of backslashes.
321 `((,(rx (not (any ?
\\))
322 ?
\\ (* (and ?
\\ ?
\\))
323 (group (syntax string-quote
))
326 (2 ,(string-to-syntax "\""))) ; dummy
327 (,(rx (group (optional (any "uUrR"))) ; prefix gets syntax property
328 (optional (any "rR")) ; possible second prefix
329 (group (syntax string-quote
)) ; maybe gets property
330 (backref 2) ; per first quote
331 (group (backref 2))) ; maybe gets property
332 (1 (python-quote-syntax 1))
333 (2 (python-quote-syntax 2))
334 (3 (python-quote-syntax 3))))
335 "Make outer chars of triple-quote strings into generic string delimiters.")
337 (defun python-quote-syntax (n)
338 "Put `syntax-table' property correctly on triple quote.
339 Used for syntactic keywords. N is the match number (1, 2 or 3)."
340 ;; Given a triple quote, we have to check the context to know
341 ;; whether this is an opening or closing triple or whether it's
342 ;; quoted anyhow, and should be ignored. (For that we need to do
343 ;; the same job as `syntax-ppss' to be correct and it seems to be OK
344 ;; to use it here despite initial worries.) We also have to sort
345 ;; out a possible prefix -- well, we don't _have_ to, but I think it
346 ;; should be treated as part of the string.
349 ;; ur"""ar""" x='"' # """
352 ;; x '"""' x """ \"""" x
354 (goto-char (match-beginning 0))
356 ;; Consider property for the last char if in a fenced string.
358 (let* ((font-lock-syntactic-keywords nil
)
359 (syntax (syntax-ppss)))
360 (when (eq t
(nth 3 syntax
)) ; after unclosed fence
361 (goto-char (nth 8 syntax
)) ; fence position
362 (skip-chars-forward "uUrR") ; skip any prefix
363 ;; Is it a matching sequence?
364 (if (eq (char-after) (char-after (match-beginning 2)))
365 (eval-when-compile (string-to-syntax "|"))))))
366 ;; Consider property for initial char, accounting for prefixes.
367 ((or (and (= n
2) ; leading quote (not prefix)
368 (= (match-beginning 1) (match-end 1))) ; prefix is null
369 (and (= n
1) ; prefix
370 (/= (match-beginning 1) (match-end 1)))) ; non-empty
371 (let ((font-lock-syntactic-keywords nil
))
372 (unless (eq 'string
(syntax-ppss-context (syntax-ppss)))
373 (eval-when-compile (string-to-syntax "|")))))
374 ;; Otherwise (we're in a non-matching string) the property is
378 (defvar python-mode-syntax-table
379 (let ((table (make-syntax-table)))
380 ;; Give punctuation syntax to ASCII that normally has symbol
381 ;; syntax or has word syntax and isn't a letter.
382 (let ((symbol (string-to-syntax "_"))
383 (sst (standard-syntax-table)))
386 (if (equal symbol
(aref sst i
))
387 (modify-syntax-entry i
"." table
)))))
388 (modify-syntax-entry ?$
"." table
)
389 (modify-syntax-entry ?%
"." table
)
391 (modify-syntax-entry ?
# "<" table
)
392 (modify-syntax-entry ?
\n ">" table
)
393 (modify-syntax-entry ?
' "\"" table
)
394 (modify-syntax-entry ?
` "$" table
)
396 "Syntax table for Python files.")
398 (defvar python-dotty-syntax-table
399 (let ((table (make-syntax-table python-mode-syntax-table
)))
400 (modify-syntax-entry ?.
"w" table
)
401 (modify-syntax-entry ?_
"w" table
)
403 "Dotty syntax table for Python files.
404 It makes underscores and dots word constituent chars.")
409 (defcustom python-indent-offset
4
410 "Default indentation offset for Python."
415 (defcustom python-indent-guess-indent-offset t
416 "Non-nil tells Python mode to guess `python-indent-offset' value."
420 (defvar python-indent-current-level
0
421 "Current indentation level `python-indent-line-function' is using.")
423 (defvar python-indent-levels
'(0)
424 "Levels of indentation available for `python-indent-line-function'.")
426 (defvar python-indent-dedenters
'("else" "elif" "except" "finally")
427 "List of words that should be dedented.
428 These make `python-indent-calculate-indentation' subtract the value of
429 `python-indent-offset'.")
431 (defun python-indent-guess-indent-offset ()
432 "Guess and set `python-indent-offset' for the current buffer."
436 (goto-char (point-min))
438 (while (and (not found-block
)
440 (python-rx line-start block-start
) nil t
))
441 (when (and (not (syntax-ppss-context (syntax-ppss)))
443 (goto-char (line-end-position))
445 (eq ?
: (char-before))))
446 (setq found-block t
)))
447 (if (not found-block
)
448 (message "Can't guess python-indent-offset, using defaults: %s"
449 python-indent-offset
)
451 (goto-char (line-end-position))
452 (python-info-continuation-line-p))
457 (let ((indent-offset (current-indentation)))
458 (when (> indent-offset
0)
459 (setq python-indent-offset indent-offset
))))))))
461 (defun python-indent-context (&optional stop
)
462 "Return information on indentation context.
463 Optional argument STOP serves to stop recursive calls.
465 Returns a cons with the form:
469 Where status can be any of the following symbols:
471 * inside-paren: If point in between (), {} or []
472 * inside-string: If point is inside a string
473 * after-backslash: Previous line ends in a backslash
474 * after-beginning-of-block: Point is after beginning of block
475 * after-line: Point is after normal line
476 * no-indent: Point is at beginning of buffer or other special case
478 START is the buffer position where the sexp starts."
481 (let ((ppss (save-excursion (beginning-of-line) (syntax-ppss)))
485 ;; Beginning of buffer
487 (goto-char (line-beginning-position))
491 ((setq start
(nth 1 ppss
))
494 ((setq start
(when (and (nth 3 ppss
))
498 ((setq start
(when (not (syntax-ppss-context ppss
))
499 (let ((line-beg-pos (line-beginning-position)))
500 (when (eq ?
\\ (char-before (1- line-beg-pos
)))
501 (- line-beg-pos
2)))))
503 ;; After beginning of block
504 ((setq start
(save-excursion
505 (let ((block-regexp (python-rx block-start
))
506 (block-start-line-end ":[[:space:]]*$"))
507 (back-to-indentation)
508 (while (and (forward-comment -
1) (not (bobp))))
509 (back-to-indentation)
510 (when (or (python-info-continuation-line-p)
511 (and (not (looking-at block-regexp
))
515 (line-end-position) t
))))
516 (while (and (forward-line -
1)
517 (python-info-continuation-line-p)
519 (when (not (looking-at block-regexp
))
521 (back-to-indentation)
522 (when (and (looking-at block-regexp
)
523 (or (re-search-forward
525 (line-end-position) t
)
526 (python-info-continuation-line-p)))
528 'after-beginning-of-block
)
530 ((setq start
(save-excursion
531 (while (and (forward-comment -
1) (not (bobp))))
532 (while (and (not (back-to-indentation))
534 (if (> (nth 0 (syntax-ppss)) 0)
538 (python-info-line-ends-backslash-p))
539 (forward-line -
1)))))
546 (defun python-indent-calculate-indentation ()
547 "Calculate correct indentation offset for the current line."
548 (let* ((indentation-context (python-indent-context))
549 (context-status (car indentation-context
))
550 (context-start (cdr indentation-context
)))
556 ('after-beginning-of-block
557 (goto-char context-start
)
558 (+ (current-indentation) python-indent-offset
))
562 (goto-char context-start
)
563 (current-indentation))
565 (back-to-indentation)
566 (looking-at (regexp-opt python-indent-dedenters
)))
570 (goto-char context-start
)
571 (current-indentation))
573 (let* ((block-continuation
576 (python-info-block-continuation-line-p)))
577 (assignment-continuation
580 (python-info-assignment-continuation-line-p)))
581 (indentation (cond (block-continuation
582 (goto-char block-continuation
)
584 (python-rx block-start
(* space
))
585 (line-end-position) t
)
587 (assignment-continuation
588 (goto-char assignment-continuation
)
590 (python-rx simple-operator
)
591 (line-end-position) t
)
594 (python-rx (* space
))
595 (line-end-position) t
)
598 (goto-char context-start
)
599 (current-indentation)))))
604 (looking-at (regexp-opt '(")" "]" "}")))
606 (when (not (nth 1 (syntax-ppss)))
607 (goto-char context-start
)
608 (back-to-indentation)
612 (goto-char context-start
)
616 (line-beginning-position)
620 (+ (current-indentation) python-indent-offset
)
624 (back-to-indentation)
625 (looking-at (regexp-opt '(")" "]" "}"))))
629 (defun python-indent-calculate-levels ()
630 "Calculate `python-indent-levels' and reset `python-indent-current-level'."
631 (let* ((indentation (python-indent-calculate-indentation))
632 (remainder (% indentation python-indent-offset
))
633 (steps (/ (- indentation remainder
) python-indent-offset
)))
634 (setq python-indent-levels
'())
635 (setq python-indent-levels
(cons 0 python-indent-levels
))
636 (dotimes (step steps
)
637 (setq python-indent-levels
638 (cons (* python-indent-offset
(1+ step
)) python-indent-levels
)))
639 (when (not (eq 0 remainder
))
640 (setq python-indent-levels
641 (cons (+ (* python-indent-offset steps
) remainder
)
642 python-indent-levels
)))
643 (setq python-indent-levels
(nreverse python-indent-levels
))
644 (setq python-indent-current-level
(1- (length python-indent-levels
)))))
646 (defun python-indent-toggle-levels ()
647 "Toggle `python-indent-current-level' over `python-indent-levels'."
648 (setq python-indent-current-level
(1- python-indent-current-level
))
649 (when (< python-indent-current-level
0)
650 (setq python-indent-current-level
(1- (length python-indent-levels
)))))
652 (defun python-indent-line (&optional force-toggle
)
653 "Internal implementation of `python-indent-line-function'.
655 Uses the offset calculated in
656 `python-indent-calculate-indentation' and available levels
657 indicated by the variable `python-indent-levels'.
659 When the variable `last-command' is equal to
660 `indent-for-tab-command' or FORCE-TOGGLE is non-nil:
662 * Cycles levels indicated in the variable `python-indent-levels'
663 by setting the current level in the variable
664 `python-indent-current-level'.
666 When the variable `last-command' is not equal to
667 `indent-for-tab-command' and FORCE-TOGGLE is nil:
669 * calculates possible indentation levels and saves it in the
670 variable `python-indent-levels'.
672 * sets the variable `python-indent-current-level' correctly so
673 offset is equal to (`nth' `python-indent-current-level'
674 `python-indent-levels')"
675 (if (or (and (eq this-command
'indent-for-tab-command
)
676 (eq last-command this-command
))
678 (python-indent-toggle-levels)
679 (python-indent-calculate-levels))
681 (delete-horizontal-space)
682 (indent-to (nth python-indent-current-level python-indent-levels
))
685 (let ((closing-block-point (python-info-closing-block)))
686 (when closing-block-point
687 (message "Closes %s" (buffer-substring
690 (goto-char closing-block-point
)
691 (line-end-position))))))))
693 (defun python-indent-line-function ()
694 "`indent-line-function' for Python mode.
695 Internally just calls `python-indent-line'."
696 (python-indent-line))
698 (defun python-indent-dedent-line ()
699 "Dedent current line."
701 (when (and (not (syntax-ppss-context (syntax-ppss)))
702 (<= (point-marker) (save-excursion
703 (back-to-indentation)
705 (> (current-column) 0))
706 (python-indent-line t
)
709 (defun python-indent-dedent-line-backspace (arg)
710 "Dedent current line.
711 Argument ARG is passed to `backward-delete-char-untabify' when
712 point is not in between the indentation."
714 (when (not (python-indent-dedent-line))
715 (backward-delete-char-untabify arg
)))
716 (put 'python-indent-dedent-line-backspace
'delete-selection
'supersede
)
718 (defun python-indent-region (start end
)
719 "Indent a python region automagically.
721 Called from a program, START and END specify the region to indent."
724 (setq end
(point-marker))
726 (or (bolp) (forward-line 1))
727 (while (< (point) end
)
728 (or (and (bolp) (eolp))
731 (back-to-indentation)
732 (setq word
(current-word))
736 (delete-horizontal-space)
737 (indent-to (python-indent-calculate-indentation)))))
739 (move-marker end nil
)))
741 (defun python-indent-shift-left (start end
&optional count
)
742 "Shift lines contained in region START END by COUNT columns to the left.
744 COUNT defaults to `python-indent-offset'.
746 If region isn't active, the current line is shifted.
748 The shifted region includes the lines in which START and END lie.
750 An error is signaled if any lines in the region are indented less
754 (list (region-beginning) (region-end) current-prefix-arg
)
755 (list (line-beginning-position) (line-end-position) current-prefix-arg
)))
757 (setq count
(prefix-numeric-value count
))
758 (setq count python-indent-offset
))
762 (while (< (point) end
)
763 (if (and (< (current-indentation) count
)
764 (not (looking-at "[ \t]*$")))
765 (error "Can't shift all lines enough"))
767 (indent-rigidly start end
(- count
)))))
769 (add-to-list 'debug-ignored-errors
"^Can't shift all lines enough")
771 (defun python-indent-shift-right (start end
&optional count
)
772 "Shift lines contained in region START END by COUNT columns to the left.
774 COUNT defaults to `python-indent-offset'.
776 If region isn't active, the current line is shifted.
778 The shifted region includes the lines in which START and END
782 (list (region-beginning) (region-end) current-prefix-arg
)
783 (list (line-beginning-position) (line-end-position) current-prefix-arg
)))
785 (setq count
(prefix-numeric-value count
))
786 (setq count python-indent-offset
))
787 (indent-rigidly start end count
))
792 (defvar python-beginning-of-defun-regexp
793 "^\\(def\\|class\\)[[:space:]]+[[:word:]]+"
794 "Regular expresion matching beginning of outermost class or function.")
796 (defvar python-beginning-of-innermost-defun-regexp
797 "^[[:space:]]*\\(def\\|class\\)[[:space:]]+[[:word:]]+"
798 "Regular expresion matching beginning of innermost class or function.")
800 (defun python-beginning-of-defun (&optional innermost
)
801 "Move point to the beginning of innermost/outermost def or class.
802 If INNERMOST is non-nil then move to the beginning of the
803 innermost definition."
804 (let ((starting-point (point-marker))
805 (nonblank-line-indent)
808 (regexp (if innermost
809 python-beginning-of-innermost-defun-regexp
810 python-beginning-of-defun-regexp
)))
811 (back-to-indentation)
812 (if (and (not (looking-at "@"))
813 (not (looking-at regexp
)))
815 (while (and (not (eobp))
817 (not (back-to-indentation))
819 (when (not (looking-at regexp
))
820 (re-search-backward regexp nil t
))
821 (setq nonblank-line-indent
(+ (current-indentation) python-indent-offset
))
822 (setq defun-indent
(current-indentation))
823 (setq defun-point
(point-marker))
824 (if (> nonblank-line-indent defun-indent
)
826 (goto-char defun-point
)
828 (while (and (looking-at "@")
831 (not (back-to-indentation))))
836 (python-beginning-of-defun)
837 (goto-char starting-point
)
840 (defun python-beginning-of-defun-function ()
841 "Move point to the beginning of outermost def or class.
842 Returns nil if point is not in a def or class."
843 (python-beginning-of-defun nil))
845 (defun python-beginning-of-innermost-defun ()
846 "Move point to the beginning of innermost def or class.
847 Returns nil if point is not in a def or class."
849 (python-beginning-of-defun t))
851 (defun python-end-of-defun-function ()
852 "Move point to the end of def or class.
853 Returns nil if point is not in a def or class."
854 (let ((starting-point (point-marker))
855 (defun-regexp (python-rx defun
))
857 (back-to-indentation)
859 (while (and (not (eobp))
861 (not (back-to-indentation))
863 (while (and (not (bobp))
864 (not (progn (back-to-indentation) (current-word)))
866 (when (or (not (equal (current-indentation) 0))
867 (string-match defun-regexp
(current-word)))
868 (setq beg-defun-indent
(save-excursion
869 (or (looking-at defun-regexp
)
870 (python-beginning-of-innermost-defun))
871 (current-indentation)))
872 (while (and (forward-line 1)
874 (or (not (current-word))
875 (> (current-indentation) beg-defun-indent
))))
876 (while (and (forward-comment -
1)
882 ;;; Shell integration
884 (defvar python-shell-buffer-name
"Python"
885 "Default buffer name for Python interpreter.")
887 (defcustom python-shell-interpreter
"python"
888 "Default Python interpreter for shell."
893 (defcustom python-shell-interpreter-args
"-i"
894 "Default arguments for the Python interpreter."
899 (defcustom python-shell-prompt-regexp
">>> "
900 "Regex matching top\-level input prompt of python shell.
901 The regex should not contain a caret (^) at the beginning."
906 (defcustom python-shell-prompt-block-regexp
"[.][.][.] "
907 "Regex matching block input prompt of python shell.
908 The regex should not contain a caret (^) at the beginning."
913 (defcustom python-shell-prompt-pdb-regexp
"[(<]*[Ii]?[Pp]db[>)]+ "
914 "Regex matching pdb input prompt of python shell.
915 The regex should not contain a caret (^) at the beginning."
920 (defcustom python-shell-compilation-regexp-alist
921 `((,(rx line-start
(1+ (any " \t")) "File \""
922 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
923 "\", line " (group (1+ digit
)))
925 (,(rx " in file " (group (1+ not-newline
)) " on line "
928 (,(rx line-start
"> " (group (1+ (not (any "(\"<"))))
929 "(" (group (1+ digit
)) ")" (1+ (not (any "("))) "()")
931 "`compilation-error-regexp-alist' for inferior Python."
932 :type
'(alist string
)
935 (defun python-shell-get-process-name (dedicated)
936 "Calculate the appropiate process name for inferior Python process.
938 If DEDICATED is t and the variable `buffer-file-name' is non-nil
939 returns a string with the form
940 `python-shell-buffer-name'[variable `buffer-file-name'] else
941 returns the value of `python-shell-buffer-name'.
943 After calculating the process name add the buffer name for the
944 process in the `same-window-buffer-names' list"
948 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)
949 (format "%s" python-shell-buffer-name
))))
950 (add-to-list 'same-window-buffer-names
(purecopy
951 (format "*%s*" process-name
)))
954 (defun python-shell-parse-command ()
955 "Calculates the string used to execute the inferior Python process."
956 (format "%s %s" python-shell-interpreter python-shell-interpreter-args
))
958 (defun python-comint-output-filter-function (output)
959 "Hook run after content is put into comint buffer.
960 OUTPUT is a string with the contents of the buffer."
961 (ansi-color-filter-apply output
))
963 (defvar inferior-python-mode-current-file nil
964 "Current file from which a region was sent.")
965 (make-variable-buffer-local 'inferior-python-mode-current-file
)
967 (define-derived-mode inferior-python-mode comint-mode
"Inferior Python"
968 "Major mode for Python inferior process."
969 (set-syntax-table python-mode-syntax-table
)
970 (setq mode-line-process
'(":%s"))
971 (setq comint-prompt-regexp
(format "^\\(?:%s\\|%s\\|%s\\)"
972 python-shell-prompt-regexp
973 python-shell-prompt-block-regexp
974 python-shell-prompt-pdb-regexp
))
975 (make-local-variable 'comint-output-filter-functions
)
976 (add-hook 'comint-output-filter-functions
977 'python-comint-output-filter-function
)
978 (add-hook 'comint-output-filter-functions
979 'python-pdbtrack-comint-output-filter-function
)
980 (set (make-local-variable 'compilation-error-regexp-alist
)
981 python-shell-compilation-regexp-alist
)
982 (define-key inferior-python-mode-map
[remap complete-symbol
]
983 'completion-at-point
)
984 (add-hook 'completion-at-point-functions
985 'python-shell-completion-complete-at-point nil
'local
)
986 (compilation-shell-minor-mode 1))
988 (defun run-python (dedicated cmd
)
989 "Run an inferior Python process.
991 Input and output via buffer *\\[python-shell-buffer-name]*.
993 If there is a process already running in
994 *\\[python-shell-buffer-name]*, switch to that buffer.
996 With argument, allows you to:
998 * Define DEDICATED so a dedicated process for the current buffer
1001 * Define CMD so you can edit the command used to call the
1002 interpreter (default is value of `python-shell-interpreter' and
1003 arguments defined in `python-shell-interpreter-args').
1005 Runs the hook `inferior-python-mode-hook' (after the
1006 `comint-mode-hook' is run).
1008 \(Type \\[describe-mode] in the process buffer for a list of
1011 (if current-prefix-arg
1013 (y-or-n-p "Make dedicated process? ")
1014 (read-string "Run Python: " (python-shell-parse-command)))
1015 (list nil
(python-shell-parse-command))))
1016 (let* ((proc-name (python-shell-get-process-name dedicated
))
1017 (proc-buffer-name (format "*%s*" proc-name
)))
1018 (when (not (comint-check-proc proc-buffer-name
))
1019 (let ((cmdlist (split-string-and-unquote cmd
)))
1021 (apply 'make-comint proc-name
(car cmdlist
) nil
1023 (inferior-python-mode)))
1024 (pop-to-buffer proc-buffer-name
))
1027 (defun python-shell-get-process ()
1028 "Get inferior Python process for current buffer and return it."
1029 (let* ((dedicated-proc-name (python-shell-get-process-name t
))
1030 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name
))
1031 (global-proc-name (python-shell-get-process-name nil
))
1032 (global-proc-buffer-name (format "*%s*" global-proc-name
))
1033 (dedicated-running (comint-check-proc dedicated-proc-buffer-name
))
1034 (global-running (comint-check-proc global-proc-buffer-name
)))
1035 ;; Always prefer dedicated
1036 (get-buffer-process (or (and dedicated-running dedicated-proc-buffer-name
)
1037 (and global-running global-proc-buffer-name
)))))
1039 (defun python-shell-get-or-create-process ()
1040 "Get or create an inferior Python process for current buffer and return it."
1041 (let* ((old-buffer (current-buffer))
1042 (dedicated-proc-name (python-shell-get-process-name t
))
1043 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name
))
1044 (global-proc-name (python-shell-get-process-name nil
))
1045 (global-proc-buffer-name (format "*%s*" global-proc-name
))
1046 (dedicated-running (comint-check-proc dedicated-proc-buffer-name
))
1047 (global-running (comint-check-proc global-proc-buffer-name
))
1048 (current-prefix-arg 4))
1049 (when (and (not dedicated-running
) (not global-running
))
1050 (if (call-interactively 'run-python
)
1051 (setq dedicated-running t
)
1052 (setq global-running t
)))
1053 ;; Always prefer dedicated
1054 (switch-to-buffer old-buffer
)
1055 (get-buffer-process (if dedicated-running
1056 dedicated-proc-buffer-name
1057 global-proc-buffer-name
))))
1059 (defun python-shell-send-string (string &optional process
)
1060 "Send STRING to inferior Python PROCESS."
1061 (interactive "sPython command: ")
1062 (let ((process (or process
(python-shell-get-or-create-process))))
1063 (when (called-interactively-p 'interactive
)
1064 (message (format "Sent: %s..." string
)))
1065 (comint-send-string process string
)
1066 (when (or (not (string-match "\n$" string
))
1067 (string-match "\n[ \t].*\n?$" string
))
1068 (comint-send-string process
"\n"))))
1070 (defun python-shell-send-region (start end
)
1071 "Send the region delimited by START and END to inferior Python process."
1073 (let* ((contents (buffer-substring start end
))
1074 (current-file (buffer-file-name))
1075 (process (python-shell-get-or-create-process))
1076 (temp-file (make-temp-file "py")))
1077 (with-temp-file temp-file
1079 (delete-trailing-whitespace)
1080 (goto-char (point-min))
1081 (message (format "Sent: %s..."
1082 (buffer-substring (point-min)
1083 (line-end-position)))))
1084 (python-shell-send-file current-file process temp-file
)))
1086 (defun python-shell-send-buffer ()
1087 "Send the entire buffer to inferior Python process."
1091 (python-shell-send-region (point-min) (point-max))))
1093 (defun python-shell-send-defun (arg)
1094 "Send the (inner|outer)most def or class to inferior Python process.
1095 When argument ARG is non-nil sends the innermost defun."
1098 (python-shell-send-region (progn
1100 (python-beginning-of-innermost-defun)
1101 (python-beginning-of-defun-function))
1102 (progn (beginning-of-line) (point-marker))))
1104 (or (python-end-of-defun-function)
1105 (progn (end-of-line) (point-marker)))))))
1107 (defun python-shell-send-file (file-name &optional process temp-file-name
)
1108 "Send FILE-NAME to inferior Python PROCESS.
1109 If TEMP-FILE-NAME is passed then that file is used for processing
1110 instead, while internally the shell will continue to use
1112 (interactive "fFile to send: ")
1113 (let ((process (or process
(python-shell-get-or-create-process)))
1114 (file-name (expand-file-name file-name
))
1115 (temp-file-name (when temp-file-name
1116 (expand-file-name temp-file-name
))))
1117 (find-file-noselect file-name
)
1118 (with-current-buffer (process-buffer process
)
1119 (setq inferior-python-mode-current-file
1120 (convert-standard-filename file-name
)))
1121 (python-shell-send-string
1123 (concat "__pyfile = open('''%s''');"
1124 "exec(compile(__pyfile.read(), '''%s''', 'exec'));"
1126 (or temp-file-name file-name
) file-name
)
1129 (defun python-shell-clear-latest-output ()
1130 "Clear latest output from the Python shell.
1131 Return the cleaned output."
1133 (when (and comint-last-output-start
1134 comint-last-prompt-overlay
)
1136 (let* ((last-output-end
1139 (overlay-start comint-last-prompt-overlay
))
1140 (forward-comment -
1)
1143 (buffer-substring-no-properties
1144 comint-last-output-start last-output-end
)))
1145 (when (< 0 (length last-output
))
1146 (goto-char comint-last-output-start
)
1147 (delete-region comint-last-output-start last-output-end
)
1151 (defun python-shell-send-and-clear-output (string process
)
1152 "Send STRING to PROCESS and clear the output.
1153 Return the cleaned output."
1155 (python-shell-send-string string process
)
1156 (accept-process-output process
)
1157 (with-current-buffer (process-buffer process
)
1158 (let ((output (python-shell-clear-latest-output)))
1161 (goto-char (overlay-end comint-last-prompt-overlay
))
1164 (defun python-shell-switch-to-shell ()
1165 "Switch to inferior Python process buffer."
1167 (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t
))
1170 ;;; Shell completion
1172 (defvar python-shell-completion-setup-code
1176 def __COMPLETER_all_completions(text): []
1179 readline.set_completer(rlcompleter.Completer().complete)
1180 def __COMPLETER_all_completions(text):
1186 res = readline.get_completer()(text, i)
1189 completions.append(res)
1193 "Code used to setup completion in inferior Python processes.")
1195 (defvar python-shell-completion-strings-code
1196 "';'.join(__COMPLETER_all_completions('''%s'''))\n"
1197 "Python code used to get a string of completions separated by semicolons.")
1199 (defun python-shell-completion-setup ()
1200 "Send `python-shell-completion-setup-code' to inferior Python process.
1201 Also binds <tab> to `python-shell-complete-or-indent' in the
1202 `inferior-python-mode-map' and adds
1203 `python-shell-completion-complete-at-point' to the
1204 `comint-dynamic-complete-functions' list.
1205 It is specially designed to be added to the
1206 `inferior-python-mode-hook'."
1207 (when python-shell-completion-setup-code
1208 (let ((temp-file (make-temp-file "py"))
1209 (process (get-buffer-process (current-buffer))))
1210 (with-temp-file temp-file
1211 (insert python-shell-completion-setup-code
)
1212 (delete-trailing-whitespace)
1213 (goto-char (point-min)))
1214 (python-shell-send-file temp-file process
)
1215 (message (format "Completion setup code sent.")))
1216 (add-to-list (make-local-variable
1217 'comint-dynamic-complete-functions
)
1218 'python-shell-completion-complete-at-point
)
1219 (define-key inferior-python-mode-map
(kbd "<tab>")
1220 'python-shell-completion-complete-or-indent
)))
1222 (defun python-shell-completion--get-completions (input process
)
1223 "Retrieve available completions for INPUT using PROCESS."
1224 (with-current-buffer (process-buffer process
)
1226 (or (python-shell-send-and-clear-output
1227 (format python-shell-completion-strings-code input
)
1229 ";\\|\"\\|'\\|(" t
)))
1231 (defun python-shell-completion--get-completion (input completions
)
1232 "Get completion for INPUT using COMPLETIONS."
1233 (let ((completion (when completions
1234 (try-completion input completions
))))
1235 (cond ((eq completion t
)
1238 (message "Can't find completion for \"%s\"" input
)
1241 ((not (string= input completion
))
1244 (message "Making completion list...")
1245 (with-output-to-temp-buffer "*Python Completions*"
1246 (display-completion-list
1247 (all-completions input completions
)))
1250 (defun python-shell-completion-complete-at-point ()
1251 "Perform completion at point in inferior Python process."
1253 (with-syntax-table python-dotty-syntax-table
1254 (when (and comint-last-prompt-overlay
1255 (> (point-marker) (overlay-end comint-last-prompt-overlay
)))
1256 (let* ((process (get-buffer-process (current-buffer)))
1257 (input (substring-no-properties
1258 (or (comint-word (current-word)) "") nil nil
)))
1259 (delete-char (- (length input
)))
1261 (python-shell-completion--get-completion
1262 input
(python-shell-completion--get-completions input process
)))))))
1264 (defun python-shell-completion-complete-or-indent ()
1265 "Complete or indent depending on the context.
1266 If content before pointer is all whitespace indent. If not try to
1269 (if (string-match "^[[:space:]]*$"
1270 (buffer-substring (comint-line-beginning-position)
1272 (indent-for-tab-command)
1273 (comint-dynamic-complete)))
1275 (add-hook 'inferior-python-mode-hook
1276 #'python-shell-completion-setup
)
1279 ;;; PDB Track integration
1281 (defvar python-pdbtrack-stacktrace-info-regexp
1282 "> %s(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()"
1283 "Regexp matching stacktrace information.
1284 It is used to extract the current line and module beign
1286 The regexp should not start with a caret (^) and can contain a
1287 string placeholder (\%s) which is replaced with the filename
1288 beign inspected (so other files in the debugging process are not
1291 (defvar python-pdbtrack-tracking-buffers
'()
1292 "Alist containing elements of form (#<buffer> . #<buffer>).
1293 The car of each element of the alist is the tracking buffer and
1294 the cdr is the tracked buffer.")
1296 (defun python-pdbtrack-get-or-add-tracking-buffers ()
1297 "Get/Add a tracked buffer for the current buffer.
1298 Internally it uses the `python-pdbtrack-tracking-buffers' alist.
1299 Returns a cons with the form:
1300 * (#<tracking buffer> . #< tracked buffer>)."
1302 (assq (current-buffer) python-pdbtrack-tracking-buffers
)
1303 (let* ((file (with-current-buffer (current-buffer)
1304 inferior-python-mode-current-file
))
1306 `(,(current-buffer) .
1307 ,(or (get-file-buffer file
)
1308 (find-file-noselect file
)))))
1309 (set-buffer (cdr tracking-buffers
))
1311 (set-buffer (car tracking-buffers
))
1312 (setq python-pdbtrack-tracking-buffers
1313 (cons tracking-buffers python-pdbtrack-tracking-buffers
))
1316 (defun python-pdbtrack-comint-output-filter-function (output)
1317 "Move overlay arrow to current pdb line in tracked buffer.
1318 Argument OUTPUT is a string with the output from the comint process."
1319 (when (not (string= output
""))
1320 (let ((full-output (ansi-color-filter-apply
1321 (buffer-substring comint-last-input-end
1323 (if (string-match python-shell-prompt-pdb-regexp full-output
)
1324 (let* ((tracking-buffers (python-pdbtrack-get-or-add-tracking-buffers))
1328 (format python-pdbtrack-stacktrace-info-regexp
1330 inferior-python-mode-current-file
))
1332 (string-to-number (or (match-string-no-properties 1 full-output
) ""))))
1333 (tracked-buffer-window (get-buffer-window (cdr tracking-buffers
)))
1334 (tracked-buffer-line-pos))
1336 (with-current-buffer (cdr tracking-buffers
)
1337 (set (make-local-variable 'overlay-arrow-string
) "=>")
1338 (set (make-local-variable 'overlay-arrow-position
) (make-marker))
1339 (setq tracked-buffer-line-pos
(progn
1340 (goto-char (point-min))
1341 (forward-line (1- line-num
))
1343 (when tracked-buffer-window
1344 (set-window-point tracked-buffer-window tracked-buffer-line-pos
))
1345 (set-marker overlay-arrow-position tracked-buffer-line-pos
)))
1346 (pop-to-buffer (cdr tracking-buffers
))
1347 (switch-to-buffer-other-window (car tracking-buffers
)))
1348 (let ((tracking-buffers (assq (current-buffer)
1349 python-pdbtrack-tracking-buffers
)))
1350 (when tracking-buffers
1351 (if inferior-python-mode-current-file
1352 (with-current-buffer (cdr tracking-buffers
)
1353 (set-marker overlay-arrow-position nil
))
1354 (kill-buffer (cdr tracking-buffers
)))
1355 (setq python-pdbtrack-tracking-buffers
1356 (assq-delete-all (current-buffer)
1357 python-pdbtrack-tracking-buffers
)))))))
1361 ;;; Symbol completion
1363 (defun python-completion-complete-at-point ()
1364 "Complete current symbol at point.
1365 For this to work the best as possible you should call
1366 `python-shell-send-buffer' from time to time so context in
1367 inferior python process is updated properly."
1369 (let ((process (python-shell-get-process)))
1371 (error "Completion needs an inferior Python process running")
1372 (with-syntax-table python-dotty-syntax-table
1373 (let* ((input (substring-no-properties
1374 (or (comint-word (current-word)) "") nil nil
))
1375 (completions (python-shell-completion--get-completions
1377 (delete-char (- (length input
)))
1379 (python-shell-completion--get-completion
1380 input completions
)))))))
1382 (add-to-list 'debug-ignored-errors
"^Completion needs an inferior Python process running.")
1387 (defun python-fill-paragraph-function (&optional justify
)
1388 "`fill-paragraph-function' handling multi-line strings and possibly comments.
1389 If any of the current line is in or at the end of a multi-line string,
1390 fill the string or the paragraph of it that point is in, preserving
1391 the string's indentation.
1392 Optional argument JUSTIFY defines if the paragraph should be justified."
1395 (back-to-indentation)
1398 ((fill-comment-paragraph justify
))
1400 ((save-excursion (skip-chars-forward "\"'uUrR")
1401 (nth 3 (syntax-ppss)))
1402 (let ((marker (point-marker))
1403 (string-start-marker
1405 (skip-chars-forward "\"'uUrR")
1406 (goto-char (nth 8 (syntax-ppss)))
1407 (skip-chars-forward "\"'uUrR")
1409 (reg-start (line-beginning-position))
1412 (while (nth 3 (syntax-ppss)) (goto-char (1+ (point-marker))))
1413 (skip-chars-backward "\"'")
1415 (reg-end (line-end-position))
1416 (fill-paragraph-function))
1418 (narrow-to-region reg-start reg-end
)
1420 (goto-char string-start-marker
)
1421 (delete-region (point-marker) (progn
1422 (skip-syntax-forward "> ")
1424 (goto-char string-end-marker
)
1425 (delete-region (point-marker) (progn
1426 (skip-syntax-backward "> ")
1430 (fill-paragraph justify
))
1431 ;; If there is a newline in the docstring lets put triple
1432 ;; quote in it's own line to follow pep 8
1433 (when (save-excursion
1434 (re-search-backward "\n" string-start-marker t
))
1436 (newline-and-indent))
1437 (fill-paragraph justify
)))) t
)
1439 ((equal (char-after (save-excursion
1440 (back-to-indentation)
1441 (point-marker))) ?
@) t
)
1443 ((or (> (nth 0 (syntax-ppss)) 0)
1444 (looking-at (python-rx open-paren
))
1446 (skip-syntax-forward "^(" (line-end-position))
1447 (looking-at (python-rx open-paren
))))
1449 (narrow-to-region (progn
1450 (while (> (nth 0 (syntax-ppss)) 0)
1451 (goto-char (1- (point-marker))))
1453 (line-beginning-position))
1455 (when (not (> (nth 0 (syntax-ppss)) 0))
1457 (when (not (> (nth 0 (syntax-ppss)) 0))
1458 (skip-syntax-backward "^)")))
1459 (while (> (nth 0 (syntax-ppss)) 0)
1460 (goto-char (1+ (point-marker))))
1462 (let ((paragraph-start "\f\\|[ \t]*$")
1463 (paragraph-separate ",")
1464 (fill-paragraph-function))
1465 (goto-char (point-min))
1466 (fill-paragraph justify
))
1469 (python-indent-line)
1470 (goto-char (line-end-position)))) t
)
1476 (defvar python-ffap-setup-code
1477 "def __FFAP_get_module_path(module):
1480 path = __import__(module).__file__
1481 if path[-4:] == '.pyc' and os.path.exists(path[0:-1]):
1486 "Python code to get a module path.")
1488 (defvar python-ffap-string-code
1489 "__FFAP_get_module_path('''%s''')\n"
1490 "Python code used to get a string with the path of a module.")
1492 (defun python-ffap-setup ()
1493 "Send `python-ffap-setup-code' to inferior Python process.
1494 It is specially designed to be added to the
1495 `inferior-python-mode-hook'."
1496 (when python-ffap-setup-code
1497 (let ((temp-file (make-temp-file "py")))
1498 (with-temp-file temp-file
1499 (insert python-ffap-setup-code
)
1500 (delete-trailing-whitespace)
1501 (goto-char (point-min)))
1502 (python-shell-send-file temp-file
(get-buffer-process (current-buffer)))
1503 (message (format "FFAP setup code sent.")))))
1505 (defun python-ffap-module-path (module)
1506 "Function for `ffap-alist' to return path for MODULE."
1508 (and (eq major-mode
'inferior-python-mode
)
1509 (get-buffer-process (current-buffer)))
1510 (python-shell-get-process))))
1514 (python-shell-send-and-clear-output
1515 (format python-ffap-string-code module
) process
)))
1517 (substring-no-properties module-file
1 -
1))))))
1519 (eval-after-load "ffap"
1521 (push '(python-mode . python-ffap-module-path
) ffap-alist
)
1522 (push '(inferior-python-mode . python-ffap-module-path
) ffap-alist
)))
1524 (add-hook 'inferior-python-mode-hook
1525 #'python-ffap-setup
)
1530 (defvar python-check-command
1531 "pychecker --stdlib"
1532 "Command used to check a Python file.")
1534 (defvar python-check-custom-command nil
1537 (defun python-check (command)
1538 "Check a Python file (default current buffer's file).
1539 Runs COMMAND, a shell command, as if by `compile'. See
1540 `python-check-command' for the default."
1542 (list (read-string "Check command: "
1543 (or python-check-custom-command
1544 (concat python-check-command
" "
1545 (shell-quote-argument
1547 (let ((name (buffer-file-name)))
1549 (file-name-nondirectory name
)))
1551 (setq python-check-custom-command command
)
1552 (save-some-buffers (not compilation-ask-about-save
) nil
)
1553 (compilation-start command
))
1558 (defvar python-eldoc-setup-code
1559 "def __PYDOC_get_help(obj):
1562 if hasattr(obj, 'startswith'):
1563 obj = eval(obj, globals())
1564 doc = pydoc.getdoc(obj)
1571 "Python code to setup documentation retrieval.")
1573 (defvar python-eldoc-string-code
1574 "__PYDOC_get_help('''%s''')\n"
1575 "Python code used to get a string with the documentation of an object.")
1577 (defun python-eldoc-setup ()
1578 "Send `python-eldoc-setup-code' to inferior Python process.
1579 It is specially designed to be added to the
1580 `inferior-python-mode-hook'."
1581 (when python-eldoc-setup-code
1582 (let ((temp-file (make-temp-file "py")))
1583 (with-temp-file temp-file
1584 (insert python-eldoc-setup-code
)
1585 (delete-trailing-whitespace)
1586 (goto-char (point-min)))
1587 (python-shell-send-file temp-file
(get-buffer-process (current-buffer)))
1588 (message (format "Eldoc setup code sent.")))))
1590 (defun python-eldoc--get-doc-at-point (&optional force-input force-process
)
1591 "Internal implementation to get documentation at point.
1592 If not FORCE-INPUT is passed then what `current-word' returns
1593 will be used. If not FORCE-PROCESS is passed what
1594 `python-shell-get-process' returns is used."
1595 (let ((process (or force-process
(python-shell-get-process))))
1597 "Eldoc needs an inferior Python process running."
1598 (let* ((current-defun (python-info-current-defun))
1599 (input (or force-input
1600 (with-syntax-table python-dotty-syntax-table
1601 (if (not current-defun
)
1603 (concat current-defun
"." (current-word))))))
1604 (ppss (syntax-ppss))
1605 (help (when (and input
1606 (not (string= input
(concat current-defun
".")))
1607 (eq nil
(nth 3 ppss
))
1608 (eq nil
(nth 4 ppss
)))
1609 (when (string-match (concat
1610 (regexp-quote (concat current-defun
"."))
1614 (goto-char (point-min))
1617 (delete-region (point-marker) (search-forward "self."))
1618 (setq input
(buffer-substring (point-min) (point-max)))))
1619 (python-shell-send-and-clear-output
1620 (format python-eldoc-string-code input
) process
))))
1621 (with-current-buffer (process-buffer process
)
1622 (when comint-last-prompt-overlay
1623 (delete-region comint-last-input-end
1624 (overlay-start comint-last-prompt-overlay
))))
1626 (not (string= help
"\n")))
1629 (defun python-eldoc-function ()
1630 "`eldoc-documentation-function' for Python.
1631 For this to work the best as possible you should call
1632 `python-shell-send-buffer' from time to time so context in
1633 inferior python process is updated properly."
1634 (python-eldoc--get-doc-at-point))
1636 (defun python-eldoc-at-point (symbol)
1637 "Get help on SYMBOL using `help'.
1638 Interactively, prompt for symbol."
1640 (let ((symbol (with-syntax-table python-dotty-syntax-table
1642 (enable-recursive-minibuffers t
))
1643 (list (read-string (if symbol
1644 (format "Describe symbol (default %s): " symbol
)
1645 "Describe symbol: ")
1647 (let ((process (python-shell-get-process)))
1649 (message "Eldoc needs an inferior Python process running.")
1650 (let ((temp-buffer-show-hook
1652 (toggle-read-only 1)
1653 (setq view-return-to-alist
1654 (list (cons (selected-window) help-return-method
))))))
1655 (with-output-to-temp-buffer (help-buffer)
1656 (with-current-buffer standard-output
1658 (python-eldoc--get-doc-at-point symbol process
))
1659 (help-print-return-message)))))))
1661 (add-hook 'inferior-python-mode-hook
1662 #'python-eldoc-setup
)
1667 (defun python-info-current-defun ()
1668 "Return name of surrounding function with Python compatible dotty syntax.
1669 This function is compatible to be used as
1670 `add-log-current-defun-function' since it returns nil if point is
1671 not inside a defun."
1677 (when (not (>= (current-indentation) python-indent-offset
))
1678 (while (and (not (eobp)) (forward-comment 1))))
1679 (while (and (not (equal 0 (current-indentation)))
1680 (python-beginning-of-innermost-defun))
1681 (back-to-indentation)
1682 (looking-at "\\(?:def\\|class\\) +\\([^(]+\\)[^:]+:\\s-*\n")
1683 (setq names
(cons (match-string-no-properties 1) names
)))))
1685 (mapconcat (lambda (string) string
) names
"."))))
1687 (defun python-info-closing-block ()
1688 "Return the point of the block that the current line closes."
1689 (let ((closing-word (save-excursion
1690 (back-to-indentation)
1692 (indentation (current-indentation)))
1693 (when (member closing-word python-indent-dedenters
)
1696 (while (and (> (current-indentation) indentation
)
1698 (not (back-to-indentation))
1700 (back-to-indentation)
1702 ((not (equal indentation
(current-indentation))) nil
)
1703 ((string= closing-word
"elif")
1704 (when (member (current-word) '("if" "elif"))
1706 ((string= closing-word
"else")
1707 (when (member (current-word) '("if" "elif" "except" "for" "while"))
1709 ((string= closing-word
"except")
1710 (when (member (current-word) '("try"))
1712 ((string= closing-word
"finally")
1713 (when (member (current-word) '("except" "else"))
1714 (point-marker))))))))
1716 (defun python-info-line-ends-backslash-p ()
1717 "Return non-nil if current line ends with backslash."
1718 (string= (or (ignore-errors
1721 (- (line-end-position) 1))) "") "\\"))
1723 (defun python-info-continuation-line-p ()
1724 "Return non-nil if current line is continuation of another."
1725 (or (python-info-line-ends-backslash-p)
1726 (string-match ",[[:space:]]*$" (buffer-substring
1727 (line-beginning-position)
1728 (line-end-position)))
1730 (let ((innermost-paren (progn
1731 (goto-char (line-end-position))
1732 (nth 1 (syntax-ppss)))))
1733 (when (and innermost-paren
1734 (and (<= (line-beginning-position) innermost-paren
)
1735 (>= (line-end-position) innermost-paren
)))
1736 (goto-char innermost-paren
)
1737 (looking-at (python-rx open-paren
(* space
) line-end
)))))
1739 (back-to-indentation)
1740 (nth 1 (syntax-ppss)))))
1742 (defun python-info-block-continuation-line-p ()
1743 "Return non-nil if current line is a continuation of a block."
1745 (while (and (not (bobp))
1746 (python-info-continuation-line-p))
1749 (back-to-indentation)
1750 (when (looking-at (python-rx block-start
))
1753 (defun python-info-assignment-continuation-line-p ()
1754 "Return non-nil if current line is a continuation of an assignment."
1756 (while (and (not (bobp))
1757 (python-info-continuation-line-p))
1760 (back-to-indentation)
1761 (when (and (not (looking-at (python-rx block-start
)))
1763 (and (re-search-forward (python-rx not-simple-operator
1765 not-simple-operator
)
1766 (line-end-position) t
)
1767 (not (syntax-ppss-context (syntax-ppss))))))
1772 (define-derived-mode python-mode fundamental-mode
"Python"
1773 "A major mode for editing Python files."
1774 (set (make-local-variable 'tab-width
) 8)
1775 (set (make-local-variable 'indent-tabs-mode
) nil
)
1777 (set (make-local-variable 'comment-start
) "# ")
1778 (set (make-local-variable 'comment-start-skip
) "#+\\s-*")
1780 (set (make-local-variable 'parse-sexp-lookup-properties
) t
)
1781 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
1783 (set (make-local-variable 'font-lock-defaults
)
1784 '(python-font-lock-keywords
1786 (font-lock-syntactic-keywords . python-font-lock-syntactic-keywords
)))
1788 (set (make-local-variable 'indent-line-function
) #'python-indent-line-function
)
1789 (set (make-local-variable 'indent-region-function
) #'python-indent-region
)
1791 (set (make-local-variable 'paragraph-start
) "\\s-*$")
1792 (set (make-local-variable 'fill-paragraph-function
) 'python-fill-paragraph-function
)
1794 (set (make-local-variable 'beginning-of-defun-function
)
1795 #'python-beginning-of-defun-function
)
1796 (set (make-local-variable 'end-of-defun-function
)
1797 #'python-end-of-defun-function
)
1799 (add-hook 'completion-at-point-functions
1800 'python-completion-complete-at-point nil
'local
)
1802 (set (make-local-variable 'add-log-current-defun-function
)
1803 #'python-info-current-defun
)
1805 (set (make-local-variable 'eldoc-documentation-function
)
1806 #'python-eldoc-function
)
1808 (add-to-list 'hs-special-modes-alist
1809 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil
"#"
1811 (python-end-of-defun-function)) nil
))
1813 (set (make-local-variable 'outline-regexp
)
1814 (python-rx (* space
) block-start
))
1815 (set (make-local-variable 'outline-heading-end-regexp
) ":\\s-*\n")
1816 (set (make-local-variable 'outline-level
)
1818 "`outline-level' function for Python mode."
1819 (1+ (/ (current-indentation) python-indent-offset
))))
1821 (when python-indent-guess-indent-offset
1822 (python-indent-guess-indent-offset)))
1826 ;;; python.el ends here