Fixed indentation of multi-line function call's closing parenthesis
[bpt/emacs.git] / lisp / progmodes / python.el
1 ;;; python.el -- Python's flying circus support for Emacs
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Fabián E. Gallina <fabian@anue.biz>
6 ;; Maintainer: FSF
7 ;; Created: Jul 2010
8 ;; Keywords: languages
9
10 ;; This file is NOT part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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.
30
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 :)
34
35 ;; Implements Syntax highlighting, Indentation, Movement, Shell
36 ;; interaction, Shell completion, Pdb tracking, Symbol completion,
37 ;; Eldoc.
38
39 ;; Syntax highlighting: Fontification of code is provided and supports
40 ;; python's triple quoted strings properly.
41
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.
45
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.
49
50 ;; Shell interaction: is provided and allows you easily execute any
51 ;; block of code of your current buffer in an inferior Python process.
52
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'
60
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.
64
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.
68
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
71 ;; running.
72
73 ;; Code check: Check the current file for errors using
74 ;; `python-check-command'
75
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.
80
81 ;; If you used python-mode.el you probably will miss auto-indentation
82 ;; when inserting newlines. To achieve the same behavior you have
83 ;; two options:
84
85 ;; 1) Use GNU/Emacs' standard binding for `newline-and-indent': C-j.
86
87 ;; 2) Add the following hook in your .emacs:
88
89 ;; (add-hook 'python-mode-hook
90 ;; #'(lambda ()
91 ;; (define-key python-mode-map "\C-m" 'newline-and-indent)))
92
93 ;; I'd recommend the first one since you'll get the same behavior for
94 ;; all modes out-of-the-box.
95
96 ;;; Installation:
97
98 ;; Add this to your .emacs:
99
100 ;; (add-to-list 'load-path "/folder/containing/file")
101 ;; (require 'python)
102
103 ;;; TODO:
104
105 ;; Ordered by priority:
106
107 ;; Better decorator support for beginning of defun
108
109 ;; Review code and cleanup
110
111 ;; (Perhaps) some skeletons (I never use them because of yasnippet)
112
113 ;;; Code:
114
115 (require 'comint)
116 (require 'ansi-color)
117 (require 'outline)
118
119 (eval-when-compile
120 (require 'cl))
121
122 (autoload 'comint-mode "comint")
123
124 ;;;###autoload
125 (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode))
126 ;;;###autoload
127 (add-to-list 'interpreter-mode-alist (cons (purecopy "python") 'python-mode))
128
129 (defgroup python nil
130 "Python Language's flying circus support for Emacs."
131 :group 'languages
132 :version "23.2"
133 :link '(emacs-commentary-link "python"))
134
135 \f
136 ;;; Bindings
137
138 (defvar python-mode-map
139 (let ((map (make-sparse-keymap)))
140 ;; Indent specific
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)
145 ;; Shell interaction
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)
155 ;; Utilities
156 (substitute-key-definition 'complete-symbol 'completion-at-point
157 map global-map)
158 (easy-menu-define python-menu map "Python Mode menu"
159 `("Python"
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"]
165 "-"
166 ["Mark def/class" mark-defun
167 :help "Mark outermost definition around point"]
168 "-"
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"]
175 "-"
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"]
191 "-"
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"]))
198 map)
199 "Keymap for `python-mode'.")
200
201 \f
202 ;;; Python specialized rx
203
204 (defconst python-rx-constituents
205 (list
206 `(block-start . ,(rx symbol-start
207 (or "def" "class" "if" "elif" "else" "try"
208 "except" "finally" "for" "while" "with")
209 symbol-end))
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 ">>=" "<<=" "&=" "^=" "|=")))))
220
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)
225 (error "No regexp"))
226 ((cdr regexps)
227 (rx-to-string `(and ,@regexps) t))
228 (t
229 (rx-to-string (car regexps) t)))))
230
231 \f
232 ;;; Font-lock and syntax
233
234 (defvar python-font-lock-keywords
235 ;; Keywords
236 `(,(rx symbol-start
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")
241 symbol-end)
242 ;; functions
243 (,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_))))
244 (1 font-lock-function-name-face))
245 ;; classes
246 (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_))))
247 (1 font-lock-type-face))
248 ;; Constants
249 (,(rx symbol-start (group "None" symbol-end))
250 (1 font-lock-constant-face))
251 ;; Decorators.
252 (,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_))
253 (0+ "." (1+ (or word ?_)))))
254 (1 font-lock-type-face))
255 ;; Builtin Exceptions
256 (,(rx symbol-start
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)
271 ;; Builtins
272 (,(rx (or line-start (not (any ". \t"))) (* (any " \t")) symbol-start
273 (group
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))
288 ;; asignations
289 ;; support for a = b = c = 5
290 (,(lambda (limit)
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)) ?=)))
299 t
300 (set-match-data nil)))))
301 (1 font-lock-variable-name-face nil nil))
302 ;; support for a, b, c = (1, 2, 3)
303 (,(lambda (limit)
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)
314 t
315 (set-match-data nil)))))
316 (1 font-lock-variable-name-face nil nil))))
317
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))
324 (backref 1)
325 (group (backref 1)))
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.")
336
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.
347
348 ;; Test cases:
349 ;; ur"""ar""" x='"' # """
350 ;; x = ''' """ ' a
351 ;; '''
352 ;; x '"""' x """ \"""" x
353 (save-excursion
354 (goto-char (match-beginning 0))
355 (cond
356 ;; Consider property for the last char if in a fenced string.
357 ((= n 3)
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
375 ;; nil, which is OK.
376 )))
377
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)))
384 (dotimes (i 128)
385 (unless (= i ?_)
386 (if (equal symbol (aref sst i))
387 (modify-syntax-entry i "." table)))))
388 (modify-syntax-entry ?$ "." table)
389 (modify-syntax-entry ?% "." table)
390 ;; exceptions
391 (modify-syntax-entry ?# "<" table)
392 (modify-syntax-entry ?\n ">" table)
393 (modify-syntax-entry ?' "\"" table)
394 (modify-syntax-entry ?` "$" table)
395 table)
396 "Syntax table for Python files.")
397
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)
402 table)
403 "Dotty syntax table for Python files.
404 It makes underscores and dots word constituent chars.")
405
406 \f
407 ;;; Indentation
408
409 (defcustom python-indent-offset 4
410 "Default indentation offset for Python."
411 :group 'python
412 :type 'integer
413 :safe 'integerp)
414
415 (defcustom python-indent-guess-indent-offset t
416 "Non-nil tells Python mode to guess `python-indent-offset' value."
417 :type 'boolean
418 :group 'python)
419
420 (defvar python-indent-current-level 0
421 "Current indentation level `python-indent-line-function' is using.")
422
423 (defvar python-indent-levels '(0)
424 "Levels of indentation available for `python-indent-line-function'.")
425
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'.")
430
431 (defun python-indent-guess-indent-offset ()
432 "Guess and set `python-indent-offset' for the current buffer."
433 (save-excursion
434 (save-restriction
435 (widen)
436 (goto-char (point-min))
437 (let ((found-block))
438 (while (and (not found-block)
439 (re-search-forward
440 (python-rx line-start block-start) nil t))
441 (when (and (not (syntax-ppss-context (syntax-ppss)))
442 (progn
443 (goto-char (line-end-position))
444 (forward-comment -1)
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)
450 (while (and (progn
451 (goto-char (line-end-position))
452 (python-info-continuation-line-p))
453 (not (eobp)))
454 (forward-line 1))
455 (forward-line 1)
456 (forward-comment 1)
457 (let ((indent-offset (current-indentation)))
458 (when (> indent-offset 0)
459 (setq python-indent-offset indent-offset))))))))
460
461 (defun python-indent-context (&optional stop)
462 "Return information on indentation context.
463 Optional argument STOP serves to stop recursive calls.
464
465 Returns a cons with the form:
466
467 \(STATUS . START)
468
469 Where status can be any of the following symbols:
470
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
477
478 START is the buffer position where the sexp starts."
479 (save-restriction
480 (widen)
481 (let ((ppss (save-excursion (beginning-of-line) (syntax-ppss)))
482 (start))
483 (cons
484 (cond
485 ;; Beginning of buffer
486 ((save-excursion
487 (goto-char (line-beginning-position))
488 (bobp))
489 'no-indent)
490 ;; Inside a paren
491 ((setq start (nth 1 ppss))
492 'inside-paren)
493 ;; Inside string
494 ((setq start (when (and (nth 3 ppss))
495 (nth 8 ppss)))
496 'inside-string)
497 ;; After backslash
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)))))
502 'after-backslash)
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))
512 (save-excursion
513 (re-search-forward
514 block-start-line-end
515 (line-end-position) t))))
516 (while (and (forward-line -1)
517 (python-info-continuation-line-p)
518 (not (bobp))))
519 (when (not (looking-at block-regexp))
520 (forward-line 1)))
521 (back-to-indentation)
522 (when (and (looking-at block-regexp)
523 (or (re-search-forward
524 block-start-line-end
525 (line-end-position) t)
526 (python-info-continuation-line-p)))
527 (point-marker)))))
528 'after-beginning-of-block)
529 ;; After normal line
530 ((setq start (save-excursion
531 (while (and (forward-comment -1) (not (bobp))))
532 (while (and (not (back-to-indentation))
533 (not (bobp))
534 (if (> (nth 0 (syntax-ppss)) 0)
535 (forward-line -1)
536 (if (save-excursion
537 (forward-line -1)
538 (python-info-line-ends-backslash-p))
539 (forward-line -1)))))
540 (point-marker)))
541 'after-line)
542 ;; Do not indent
543 (t 'no-indent))
544 start))))
545
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)))
551 (save-restriction
552 (widen)
553 (save-excursion
554 (case context-status
555 ('no-indent 0)
556 ('after-beginning-of-block
557 (goto-char context-start)
558 (+ (current-indentation) python-indent-offset))
559 ('after-line
560 (-
561 (save-excursion
562 (goto-char context-start)
563 (current-indentation))
564 (if (progn
565 (back-to-indentation)
566 (looking-at (regexp-opt python-indent-dedenters)))
567 python-indent-offset
568 0)))
569 ('inside-string
570 (goto-char context-start)
571 (current-indentation))
572 ('after-backslash
573 (let* ((block-continuation
574 (save-excursion
575 (forward-line -1)
576 (python-info-block-continuation-line-p)))
577 (assignment-continuation
578 (save-excursion
579 (forward-line -1)
580 (python-info-assignment-continuation-line-p)))
581 (indentation (cond (block-continuation
582 (goto-char block-continuation)
583 (re-search-forward
584 (python-rx block-start (* space))
585 (line-end-position) t)
586 (current-column))
587 (assignment-continuation
588 (goto-char assignment-continuation)
589 (re-search-forward
590 (python-rx simple-operator)
591 (line-end-position) t)
592 (forward-char 1)
593 (re-search-forward
594 (python-rx (* space))
595 (line-end-position) t)
596 (current-column))
597 (t
598 (goto-char context-start)
599 (current-indentation)))))
600 indentation))
601 ('inside-paren
602 (or (save-excursion
603 (forward-comment 1)
604 (looking-at (regexp-opt '(")" "]" "}")))
605 (forward-char 1)
606 (when (not (nth 1 (syntax-ppss)))
607 (goto-char context-start)
608 (back-to-indentation)
609 (current-column)))
610 (-
611 (save-excursion
612 (goto-char context-start)
613 (forward-char)
614 (save-restriction
615 (narrow-to-region
616 (line-beginning-position)
617 (line-end-position))
618 (forward-comment 1))
619 (if (looking-at "$")
620 (+ (current-indentation) python-indent-offset)
621 (forward-comment 1)
622 (current-column)))
623 (if (progn
624 (back-to-indentation)
625 (looking-at (regexp-opt '(")" "]" "}"))))
626 python-indent-offset
627 0)))))))))
628
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)))))
645
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)))))
651
652 (defun python-indent-line (&optional force-toggle)
653 "Internal implementation of `python-indent-line-function'.
654
655 Uses the offset calculated in
656 `python-indent-calculate-indentation' and available levels
657 indicated by the variable `python-indent-levels'.
658
659 When the variable `last-command' is equal to
660 `indent-for-tab-command' or FORCE-TOGGLE is non-nil:
661
662 * Cycles levels indicated in the variable `python-indent-levels'
663 by setting the current level in the variable
664 `python-indent-current-level'.
665
666 When the variable `last-command' is not equal to
667 `indent-for-tab-command' and FORCE-TOGGLE is nil:
668
669 * calculates possible indentation levels and saves it in the
670 variable `python-indent-levels'.
671
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))
677 force-toggle)
678 (python-indent-toggle-levels)
679 (python-indent-calculate-levels))
680 (beginning-of-line)
681 (delete-horizontal-space)
682 (indent-to (nth python-indent-current-level python-indent-levels))
683 (save-restriction
684 (widen)
685 (let ((closing-block-point (python-info-closing-block)))
686 (when closing-block-point
687 (message "Closes %s" (buffer-substring
688 closing-block-point
689 (save-excursion
690 (goto-char closing-block-point)
691 (line-end-position))))))))
692
693 (defun python-indent-line-function ()
694 "`indent-line-function' for Python mode.
695 Internally just calls `python-indent-line'."
696 (python-indent-line))
697
698 (defun python-indent-dedent-line ()
699 "Dedent current line."
700 (interactive "*")
701 (when (and (not (syntax-ppss-context (syntax-ppss)))
702 (<= (point-marker) (save-excursion
703 (back-to-indentation)
704 (point-marker)))
705 (> (current-column) 0))
706 (python-indent-line t)
707 t))
708
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."
713 (interactive "*p")
714 (when (not (python-indent-dedent-line))
715 (backward-delete-char-untabify arg)))
716 (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)
717
718 (defun python-indent-region (start end)
719 "Indent a python region automagically.
720
721 Called from a program, START and END specify the region to indent."
722 (save-excursion
723 (goto-char end)
724 (setq end (point-marker))
725 (goto-char start)
726 (or (bolp) (forward-line 1))
727 (while (< (point) end)
728 (or (and (bolp) (eolp))
729 (let (word)
730 (forward-line -1)
731 (back-to-indentation)
732 (setq word (current-word))
733 (forward-line 1)
734 (when word
735 (beginning-of-line)
736 (delete-horizontal-space)
737 (indent-to (python-indent-calculate-indentation)))))
738 (forward-line 1))
739 (move-marker end nil)))
740
741 (defun python-indent-shift-left (start end &optional count)
742 "Shift lines contained in region START END by COUNT columns to the left.
743
744 COUNT defaults to `python-indent-offset'.
745
746 If region isn't active, the current line is shifted.
747
748 The shifted region includes the lines in which START and END lie.
749
750 An error is signaled if any lines in the region are indented less
751 than COUNT columns."
752 (interactive
753 (if mark-active
754 (list (region-beginning) (region-end) current-prefix-arg)
755 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
756 (if count
757 (setq count (prefix-numeric-value count))
758 (setq count python-indent-offset))
759 (when (> count 0)
760 (save-excursion
761 (goto-char start)
762 (while (< (point) end)
763 (if (and (< (current-indentation) count)
764 (not (looking-at "[ \t]*$")))
765 (error "Can't shift all lines enough"))
766 (forward-line))
767 (indent-rigidly start end (- count)))))
768
769 (add-to-list 'debug-ignored-errors "^Can't shift all lines enough")
770
771 (defun python-indent-shift-right (start end &optional count)
772 "Shift lines contained in region START END by COUNT columns to the left.
773
774 COUNT defaults to `python-indent-offset'.
775
776 If region isn't active, the current line is shifted.
777
778 The shifted region includes the lines in which START and END
779 lie."
780 (interactive
781 (if mark-active
782 (list (region-beginning) (region-end) current-prefix-arg)
783 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
784 (if count
785 (setq count (prefix-numeric-value count))
786 (setq count python-indent-offset))
787 (indent-rigidly start end count))
788
789 \f
790 ;;; Navigation
791
792 (defvar python-beginning-of-defun-regexp
793 "^\\(def\\|class\\)[[:space:]]+[[:word:]]+"
794 "Regular expresion matching beginning of outermost class or function.")
795
796 (defvar python-beginning-of-innermost-defun-regexp
797 "^[[:space:]]*\\(def\\|class\\)[[:space:]]+[[:word:]]+"
798 "Regular expresion matching beginning of innermost class or function.")
799
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)
806 (defun-indent)
807 (defun-point)
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)))
814 (forward-comment -1)
815 (while (and (not (eobp))
816 (forward-line 1)
817 (not (back-to-indentation))
818 (looking-at "@"))))
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)
825 (progn
826 (goto-char defun-point)
827 (forward-line -1)
828 (while (and (looking-at "@")
829 (forward-line -1)
830 (not (bobp))
831 (not (back-to-indentation))))
832 (unless (bobp)
833 (forward-line 1))
834 (point-marker))
835 (if innermost
836 (python-beginning-of-defun)
837 (goto-char starting-point)
838 nil))))
839
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))
844
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."
848 (interactive)
849 (python-beginning-of-defun t))
850
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))
856 (beg-defun-indent))
857 (back-to-indentation)
858 (if (looking-at "@")
859 (while (and (not (eobp))
860 (forward-line 1)
861 (not (back-to-indentation))
862 (looking-at "@")))
863 (while (and (not (bobp))
864 (not (progn (back-to-indentation) (current-word)))
865 (forward-line -1))))
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)
873 (not (eobp))
874 (or (not (current-word))
875 (> (current-indentation) beg-defun-indent))))
876 (while (and (forward-comment -1)
877 (not (bobp))))
878 (forward-line 1)
879 (point-marker))))
880
881 \f
882 ;;; Shell integration
883
884 (defvar python-shell-buffer-name "Python"
885 "Default buffer name for Python interpreter.")
886
887 (defcustom python-shell-interpreter "python"
888 "Default Python interpreter for shell."
889 :group 'python
890 :type 'string
891 :safe 'stringp)
892
893 (defcustom python-shell-interpreter-args "-i"
894 "Default arguments for the Python interpreter."
895 :group 'python
896 :type 'string
897 :safe 'stringp)
898
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."
902 :type 'string
903 :group 'python
904 :safe 'stringp)
905
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."
909 :type 'string
910 :group 'python
911 :safe 'stringp)
912
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."
916 :type 'string
917 :group 'python
918 :safe 'stringp)
919
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)))
924 1 2)
925 (,(rx " in file " (group (1+ not-newline)) " on line "
926 (group (1+ digit)))
927 1 2)
928 (,(rx line-start "> " (group (1+ (not (any "(\"<"))))
929 "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()")
930 1 2))
931 "`compilation-error-regexp-alist' for inferior Python."
932 :type '(alist string)
933 :group 'python)
934
935 (defun python-shell-get-process-name (dedicated)
936 "Calculate the appropiate process name for inferior Python process.
937
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'.
942
943 After calculating the process name add the buffer name for the
944 process in the `same-window-buffer-names' list"
945 (let ((process-name
946 (if (and dedicated
947 buffer-file-name)
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)))
952 process-name))
953
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))
957
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))
962
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)
966
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))
987
988 (defun run-python (dedicated cmd)
989 "Run an inferior Python process.
990
991 Input and output via buffer *\\[python-shell-buffer-name]*.
992
993 If there is a process already running in
994 *\\[python-shell-buffer-name]*, switch to that buffer.
995
996 With argument, allows you to:
997
998 * Define DEDICATED so a dedicated process for the current buffer
999 is open.
1000
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').
1004
1005 Runs the hook `inferior-python-mode-hook' (after the
1006 `comint-mode-hook' is run).
1007
1008 \(Type \\[describe-mode] in the process buffer for a list of
1009 commands.)"
1010 (interactive
1011 (if current-prefix-arg
1012 (list
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)))
1020 (set-buffer
1021 (apply 'make-comint proc-name (car cmdlist) nil
1022 (cdr cmdlist)))
1023 (inferior-python-mode)))
1024 (pop-to-buffer proc-buffer-name))
1025 dedicated)
1026
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)))))
1038
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))))
1058
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"))))
1069
1070 (defun python-shell-send-region (start end)
1071 "Send the region delimited by START and END to inferior Python process."
1072 (interactive "r")
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
1078 (insert contents)
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)))
1085
1086 (defun python-shell-send-buffer ()
1087 "Send the entire buffer to inferior Python process."
1088 (interactive)
1089 (save-restriction
1090 (widen)
1091 (python-shell-send-region (point-min) (point-max))))
1092
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."
1096 (interactive "P")
1097 (save-excursion
1098 (python-shell-send-region (progn
1099 (or (if arg
1100 (python-beginning-of-innermost-defun)
1101 (python-beginning-of-defun-function))
1102 (progn (beginning-of-line) (point-marker))))
1103 (progn
1104 (or (python-end-of-defun-function)
1105 (progn (end-of-line) (point-marker)))))))
1106
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
1111 FILE-NAME."
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
1122 (format
1123 (concat "__pyfile = open('''%s''');"
1124 "exec(compile(__pyfile.read(), '''%s''', 'exec'));"
1125 "__pyfile.close()")
1126 (or temp-file-name file-name) file-name)
1127 process)))
1128
1129 (defun python-shell-clear-latest-output ()
1130 "Clear latest output from the Python shell.
1131 Return the cleaned output."
1132 (interactive)
1133 (when (and comint-last-output-start
1134 comint-last-prompt-overlay)
1135 (save-excursion
1136 (let* ((last-output-end
1137 (save-excursion
1138 (goto-char
1139 (overlay-start comint-last-prompt-overlay))
1140 (forward-comment -1)
1141 (point-marker)))
1142 (last-output
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)
1148 (delete-char -1)
1149 last-output)))))
1150
1151 (defun python-shell-send-and-clear-output (string process)
1152 "Send STRING to PROCESS and clear the output.
1153 Return the cleaned output."
1154 (interactive)
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)))
1159 (forward-line -1)
1160 (kill-whole-line)
1161 (goto-char (overlay-end comint-last-prompt-overlay))
1162 output)))
1163
1164 (defun python-shell-switch-to-shell ()
1165 "Switch to inferior Python process buffer."
1166 (interactive)
1167 (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t))
1168
1169 \f
1170 ;;; Shell completion
1171
1172 (defvar python-shell-completion-setup-code
1173 "try:
1174 import readline
1175 except ImportError:
1176 def __COMPLETER_all_completions(text): []
1177 else:
1178 import rlcompleter
1179 readline.set_completer(rlcompleter.Completer().complete)
1180 def __COMPLETER_all_completions(text):
1181 import sys
1182 completions = []
1183 try:
1184 i = 0
1185 while True:
1186 res = readline.get_completer()(text, i)
1187 if not res: break
1188 i += 1
1189 completions.append(res)
1190 except NameError:
1191 pass
1192 return completions"
1193 "Code used to setup completion in inferior Python processes.")
1194
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.")
1198
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)))
1221
1222 (defun python-shell-completion--get-completions (input process)
1223 "Retrieve available completions for INPUT using PROCESS."
1224 (with-current-buffer (process-buffer process)
1225 (split-string
1226 (or (python-shell-send-and-clear-output
1227 (format python-shell-completion-strings-code input)
1228 process) "")
1229 ";\\|\"\\|'\\|(" t)))
1230
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)
1236 input)
1237 ((null completion)
1238 (message "Can't find completion for \"%s\"" input)
1239 (ding)
1240 input)
1241 ((not (string= input completion))
1242 completion)
1243 (t
1244 (message "Making completion list...")
1245 (with-output-to-temp-buffer "*Python Completions*"
1246 (display-completion-list
1247 (all-completions input completions)))
1248 input))))
1249
1250 (defun python-shell-completion-complete-at-point ()
1251 "Perform completion at point in inferior Python process."
1252 (interactive)
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)))
1260 (insert
1261 (python-shell-completion--get-completion
1262 input (python-shell-completion--get-completions input process)))))))
1263
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
1267 complete."
1268 (interactive)
1269 (if (string-match "^[[:space:]]*$"
1270 (buffer-substring (comint-line-beginning-position)
1271 (point-marker)))
1272 (indent-for-tab-command)
1273 (comint-dynamic-complete)))
1274
1275 (add-hook 'inferior-python-mode-hook
1276 #'python-shell-completion-setup)
1277
1278 \f
1279 ;;; PDB Track integration
1280
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
1285 inspected.
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
1289 opened)")
1290
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.")
1295
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>)."
1301 (or
1302 (assq (current-buffer) python-pdbtrack-tracking-buffers)
1303 (let* ((file (with-current-buffer (current-buffer)
1304 inferior-python-mode-current-file))
1305 (tracking-buffers
1306 `(,(current-buffer) .
1307 ,(or (get-file-buffer file)
1308 (find-file-noselect file)))))
1309 (set-buffer (cdr tracking-buffers))
1310 (python-mode)
1311 (set-buffer (car tracking-buffers))
1312 (setq python-pdbtrack-tracking-buffers
1313 (cons tracking-buffers python-pdbtrack-tracking-buffers))
1314 tracking-buffers)))
1315
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
1322 (point-max)))))
1323 (if (string-match python-shell-prompt-pdb-regexp full-output)
1324 (let* ((tracking-buffers (python-pdbtrack-get-or-add-tracking-buffers))
1325 (line-num
1326 (save-excursion
1327 (string-match
1328 (format python-pdbtrack-stacktrace-info-regexp
1329 (regexp-quote
1330 inferior-python-mode-current-file))
1331 full-output)
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))
1335 (when line-num
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))
1342 (point-marker)))
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)))))))
1358 output)
1359
1360 \f
1361 ;;; Symbol completion
1362
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."
1368 (interactive)
1369 (let ((process (python-shell-get-process)))
1370 (if (not 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
1376 input process)))
1377 (delete-char (- (length input)))
1378 (insert
1379 (python-shell-completion--get-completion
1380 input completions)))))))
1381
1382 (add-to-list 'debug-ignored-errors "^Completion needs an inferior Python process running.")
1383
1384 \f
1385 ;;; Fill paragraph
1386
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."
1393 (interactive "P")
1394 (save-excursion
1395 (back-to-indentation)
1396 (cond
1397 ;; Comments
1398 ((fill-comment-paragraph justify))
1399 ;; Docstrings
1400 ((save-excursion (skip-chars-forward "\"'uUrR")
1401 (nth 3 (syntax-ppss)))
1402 (let ((marker (point-marker))
1403 (string-start-marker
1404 (progn
1405 (skip-chars-forward "\"'uUrR")
1406 (goto-char (nth 8 (syntax-ppss)))
1407 (skip-chars-forward "\"'uUrR")
1408 (point-marker)))
1409 (reg-start (line-beginning-position))
1410 (string-end-marker
1411 (progn
1412 (while (nth 3 (syntax-ppss)) (goto-char (1+ (point-marker))))
1413 (skip-chars-backward "\"'")
1414 (point-marker)))
1415 (reg-end (line-end-position))
1416 (fill-paragraph-function))
1417 (save-restriction
1418 (narrow-to-region reg-start reg-end)
1419 (save-excursion
1420 (goto-char string-start-marker)
1421 (delete-region (point-marker) (progn
1422 (skip-syntax-forward "> ")
1423 (point-marker)))
1424 (goto-char string-end-marker)
1425 (delete-region (point-marker) (progn
1426 (skip-syntax-backward "> ")
1427 (point-marker)))
1428 (save-excursion
1429 (goto-char marker)
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))
1435 (newline)
1436 (newline-and-indent))
1437 (fill-paragraph justify)))) t)
1438 ;; Decorators
1439 ((equal (char-after (save-excursion
1440 (back-to-indentation)
1441 (point-marker))) ?@) t)
1442 ;; Parens
1443 ((or (> (nth 0 (syntax-ppss)) 0)
1444 (looking-at (python-rx open-paren))
1445 (save-excursion
1446 (skip-syntax-forward "^(" (line-end-position))
1447 (looking-at (python-rx open-paren))))
1448 (save-restriction
1449 (narrow-to-region (progn
1450 (while (> (nth 0 (syntax-ppss)) 0)
1451 (goto-char (1- (point-marker))))
1452 (point-marker)
1453 (line-beginning-position))
1454 (progn
1455 (when (not (> (nth 0 (syntax-ppss)) 0))
1456 (end-of-line)
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))))
1461 (point-marker)))
1462 (let ((paragraph-start "\f\\|[ \t]*$")
1463 (paragraph-separate ",")
1464 (fill-paragraph-function))
1465 (goto-char (point-min))
1466 (fill-paragraph justify))
1467 (while (not (eobp))
1468 (forward-line 1)
1469 (python-indent-line)
1470 (goto-char (line-end-position)))) t)
1471 (t t))))
1472
1473 \f
1474 ;;; FFAP
1475
1476 (defvar python-ffap-setup-code
1477 "def __FFAP_get_module_path(module):
1478 try:
1479 import os
1480 path = __import__(module).__file__
1481 if path[-4:] == '.pyc' and os.path.exists(path[0:-1]):
1482 path = path[:-1]
1483 return path
1484 except:
1485 return ''"
1486 "Python code to get a module path.")
1487
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.")
1491
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.")))))
1504
1505 (defun python-ffap-module-path (module)
1506 "Function for `ffap-alist' to return path for MODULE."
1507 (let ((process (or
1508 (and (eq major-mode 'inferior-python-mode)
1509 (get-buffer-process (current-buffer)))
1510 (python-shell-get-process))))
1511 (if (not process)
1512 nil
1513 (let ((module-file
1514 (python-shell-send-and-clear-output
1515 (format python-ffap-string-code module) process)))
1516 (when module-file
1517 (substring-no-properties module-file 1 -1))))))
1518
1519 (eval-after-load "ffap"
1520 '(progn
1521 (push '(python-mode . python-ffap-module-path) ffap-alist)
1522 (push '(inferior-python-mode . python-ffap-module-path) ffap-alist)))
1523
1524 (add-hook 'inferior-python-mode-hook
1525 #'python-ffap-setup)
1526
1527 \f
1528 ;;; Code check
1529
1530 (defvar python-check-command
1531 "pychecker --stdlib"
1532 "Command used to check a Python file.")
1533
1534 (defvar python-check-custom-command nil
1535 "Internal use.")
1536
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."
1541 (interactive
1542 (list (read-string "Check command: "
1543 (or python-check-custom-command
1544 (concat python-check-command " "
1545 (shell-quote-argument
1546 (or
1547 (let ((name (buffer-file-name)))
1548 (and name
1549 (file-name-nondirectory name)))
1550 "")))))))
1551 (setq python-check-custom-command command)
1552 (save-some-buffers (not compilation-ask-about-save) nil)
1553 (compilation-start command))
1554
1555 \f
1556 ;;; Eldoc
1557
1558 (defvar python-eldoc-setup-code
1559 "def __PYDOC_get_help(obj):
1560 try:
1561 import pydoc
1562 if hasattr(obj, 'startswith'):
1563 obj = eval(obj, globals())
1564 doc = pydoc.getdoc(obj)
1565 except:
1566 doc = ''
1567 try:
1568 exec('print doc')
1569 except SyntaxError:
1570 print(doc)"
1571 "Python code to setup documentation retrieval.")
1572
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.")
1576
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.")))))
1589
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))))
1596 (if (not 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)
1602 (current-word)
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 "."))
1611 "self\\.") input)
1612 (with-temp-buffer
1613 (insert input)
1614 (goto-char (point-min))
1615 (forward-word)
1616 (forward-char)
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))))
1625 (when (and help
1626 (not (string= help "\n")))
1627 help)))))
1628
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))
1635
1636 (defun python-eldoc-at-point (symbol)
1637 "Get help on SYMBOL using `help'.
1638 Interactively, prompt for symbol."
1639 (interactive
1640 (let ((symbol (with-syntax-table python-dotty-syntax-table
1641 (current-word)))
1642 (enable-recursive-minibuffers t))
1643 (list (read-string (if symbol
1644 (format "Describe symbol (default %s): " symbol)
1645 "Describe symbol: ")
1646 nil nil symbol))))
1647 (let ((process (python-shell-get-process)))
1648 (if (not process)
1649 (message "Eldoc needs an inferior Python process running.")
1650 (let ((temp-buffer-show-hook
1651 (lambda ()
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
1657 (insert
1658 (python-eldoc--get-doc-at-point symbol process))
1659 (help-print-return-message)))))))
1660
1661 (add-hook 'inferior-python-mode-hook
1662 #'python-eldoc-setup)
1663
1664 \f
1665 ;;; Misc helpers
1666
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."
1672 (let ((names '()))
1673 (save-restriction
1674 (widen)
1675 (save-excursion
1676 (beginning-of-line)
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)))))
1684 (when names
1685 (mapconcat (lambda (string) string) names "."))))
1686
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)
1691 (current-word)))
1692 (indentation (current-indentation)))
1693 (when (member closing-word python-indent-dedenters)
1694 (save-excursion
1695 (forward-line -1)
1696 (while (and (> (current-indentation) indentation)
1697 (not (bobp))
1698 (not (back-to-indentation))
1699 (forward-line -1)))
1700 (back-to-indentation)
1701 (cond
1702 ((not (equal indentation (current-indentation))) nil)
1703 ((string= closing-word "elif")
1704 (when (member (current-word) '("if" "elif"))
1705 (point-marker)))
1706 ((string= closing-word "else")
1707 (when (member (current-word) '("if" "elif" "except" "for" "while"))
1708 (point-marker)))
1709 ((string= closing-word "except")
1710 (when (member (current-word) '("try"))
1711 (point-marker)))
1712 ((string= closing-word "finally")
1713 (when (member (current-word) '("except" "else"))
1714 (point-marker))))))))
1715
1716 (defun python-info-line-ends-backslash-p ()
1717 "Return non-nil if current line ends with backslash."
1718 (string= (or (ignore-errors
1719 (buffer-substring
1720 (line-end-position)
1721 (- (line-end-position) 1))) "") "\\"))
1722
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)))
1729 (save-excursion
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)))))
1738 (save-excursion
1739 (back-to-indentation)
1740 (nth 1 (syntax-ppss)))))
1741
1742 (defun python-info-block-continuation-line-p ()
1743 "Return non-nil if current line is a continuation of a block."
1744 (save-excursion
1745 (while (and (not (bobp))
1746 (python-info-continuation-line-p))
1747 (forward-line -1))
1748 (forward-line 1)
1749 (back-to-indentation)
1750 (when (looking-at (python-rx block-start))
1751 (point-marker))))
1752
1753 (defun python-info-assignment-continuation-line-p ()
1754 "Return non-nil if current line is a continuation of an assignment."
1755 (save-excursion
1756 (while (and (not (bobp))
1757 (python-info-continuation-line-p))
1758 (forward-line -1))
1759 (forward-line 1)
1760 (back-to-indentation)
1761 (when (and (not (looking-at (python-rx block-start)))
1762 (save-excursion
1763 (and (re-search-forward (python-rx not-simple-operator
1764 assignment-operator
1765 not-simple-operator)
1766 (line-end-position) t)
1767 (not (syntax-ppss-context (syntax-ppss))))))
1768 (point-marker))))
1769
1770 \f
1771 ;;;###autoload
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)
1776
1777 (set (make-local-variable 'comment-start) "# ")
1778 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
1779
1780 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1781 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1782
1783 (set (make-local-variable 'font-lock-defaults)
1784 '(python-font-lock-keywords
1785 nil nil nil nil
1786 (font-lock-syntactic-keywords . python-font-lock-syntactic-keywords)))
1787
1788 (set (make-local-variable 'indent-line-function) #'python-indent-line-function)
1789 (set (make-local-variable 'indent-region-function) #'python-indent-region)
1790
1791 (set (make-local-variable 'paragraph-start) "\\s-*$")
1792 (set (make-local-variable 'fill-paragraph-function) 'python-fill-paragraph-function)
1793
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)
1798
1799 (add-hook 'completion-at-point-functions
1800 'python-completion-complete-at-point nil 'local)
1801
1802 (set (make-local-variable 'add-log-current-defun-function)
1803 #'python-info-current-defun)
1804
1805 (set (make-local-variable 'eldoc-documentation-function)
1806 #'python-eldoc-function)
1807
1808 (add-to-list 'hs-special-modes-alist
1809 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
1810 ,(lambda (arg)
1811 (python-end-of-defun-function)) nil))
1812
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)
1817 #'(lambda ()
1818 "`outline-level' function for Python mode."
1819 (1+ (/ (current-indentation) python-indent-offset))))
1820
1821 (when python-indent-guess-indent-offset
1822 (python-indent-guess-indent-offset)))
1823
1824
1825 (provide 'python)
1826 ;;; python.el ends here