* progmodes/python.el Fixed defsubst warning.
[bpt/emacs.git] / lisp / progmodes / python.el
1 ;;; python.el --- Python's flying circus support for Emacs
2
3 ;; Copyright (C) 2003-2012 Free Software Foundation, Inc.
4
5 ;; Author: Fabián E. Gallina <fabian@anue.biz>
6 ;; URL: https://github.com/fgallina/python.el
7 ;; Version: 0.24.2
8 ;; Maintainer: FSF
9 ;; Created: Jul 2010
10 ;; Keywords: languages
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published
16 ;; by the Free Software Foundation, either version 3 of the License,
17 ;; or (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; Major mode for editing Python files with some fontification and
30 ;; indentation bits extracted from original Dave Love's python.el
31 ;; found in GNU/Emacs.
32
33 ;; Implements Syntax highlighting, Indentation, Movement, Shell
34 ;; interaction, Shell completion, Shell virtualenv support, Pdb
35 ;; tracking, Symbol completion, Skeletons, FFAP, Code Check, Eldoc,
36 ;; imenu.
37
38 ;; Syntax highlighting: Fontification of code is provided and supports
39 ;; python's triple quoted strings properly.
40
41 ;; Indentation: Automatic indentation with indentation cycling is
42 ;; provided, it allows you to navigate different available levels of
43 ;; indentation by hitting <tab> several times. Also when inserting a
44 ;; colon the `python-indent-electric-colon' command is invoked and
45 ;; causes the current line to be dedented automatically if needed.
46
47 ;; Movement: `beginning-of-defun' and `end-of-defun' functions are
48 ;; properly implemented. There are also specialized
49 ;; `forward-sentence' and `backward-sentence' replacements called
50 ;; `python-nav-forward-block', `python-nav-backward-block'
51 ;; respectively which navigate between beginning of blocks of code.
52 ;; Extra functions `python-nav-forward-statement',
53 ;; `python-nav-backward-statement',
54 ;; `python-nav-beginning-of-statement', `python-nav-end-of-statement',
55 ;; `python-nav-beginning-of-block' and `python-nav-end-of-block' are
56 ;; included but no bound to any key. At last but not least the
57 ;; specialized `python-nav-forward-sexp-function' allows easy
58 ;; navigation between code blocks.
59
60 ;; Shell interaction: is provided and allows you to execute easily any
61 ;; block of code of your current buffer in an inferior Python process.
62
63 ;; Shell completion: hitting tab will try to complete the current
64 ;; word. Shell completion is implemented in a manner that if you
65 ;; change the `python-shell-interpreter' to any other (for example
66 ;; IPython) it should be easy to integrate another way to calculate
67 ;; completions. You just need to specify your custom
68 ;; `python-shell-completion-setup-code' and
69 ;; `python-shell-completion-string-code'.
70
71 ;; Here is a complete example of the settings you would use for
72 ;; iPython 0.11:
73
74 ;; (setq
75 ;; python-shell-interpreter "ipython"
76 ;; python-shell-interpreter-args ""
77 ;; python-shell-prompt-regexp "In \\[[0-9]+\\]: "
78 ;; python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
79 ;; python-shell-completion-setup-code
80 ;; "from IPython.core.completerlib import module_completion"
81 ;; python-shell-completion-module-string-code
82 ;; "';'.join(module_completion('''%s'''))\n"
83 ;; python-shell-completion-string-code
84 ;; "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
85
86 ;; For iPython 0.10 everything would be the same except for
87 ;; `python-shell-completion-string-code' and
88 ;; `python-shell-completion-module-string-code':
89
90 ;; (setq python-shell-completion-string-code
91 ;; "';'.join(__IP.complete('''%s'''))\n"
92 ;; python-shell-completion-module-string-code "")
93
94 ;; Unfortunately running iPython on Windows needs some more tweaking.
95 ;; The way you must set `python-shell-interpreter' and
96 ;; `python-shell-interpreter-args' is as follows:
97
98 ;; (setq
99 ;; python-shell-interpreter "C:\\Python27\\python.exe"
100 ;; python-shell-interpreter-args
101 ;; "-i C:\\Python27\\Scripts\\ipython-script.py")
102
103 ;; That will spawn the iPython process correctly (Of course you need
104 ;; to modify the paths according to your system).
105
106 ;; Please note that the default completion system depends on the
107 ;; readline module, so if you are using some Operating System that
108 ;; bundles Python without it (like Windows) just install the
109 ;; pyreadline from http://ipython.scipy.org/moin/PyReadline/Intro and
110 ;; you should be good to go.
111
112 ;; Shell virtualenv support: The shell also contains support for
113 ;; virtualenvs and other special environment modifications thanks to
114 ;; `python-shell-process-environment' and `python-shell-exec-path'.
115 ;; These two variables allows you to modify execution paths and
116 ;; environment variables to make easy for you to setup virtualenv rules
117 ;; or behavior modifications when running shells. Here is an example
118 ;; of how to make shell processes to be run using the /path/to/env/
119 ;; virtualenv:
120
121 ;; (setq python-shell-process-environment
122 ;; (list
123 ;; (format "PATH=%s" (mapconcat
124 ;; 'identity
125 ;; (reverse
126 ;; (cons (getenv "PATH")
127 ;; '("/path/to/env/bin/")))
128 ;; ":"))
129 ;; "VIRTUAL_ENV=/path/to/env/"))
130 ;; (python-shell-exec-path . ("/path/to/env/bin/"))
131
132 ;; Since the above is cumbersome and can be programmatically
133 ;; calculated, the variable `python-shell-virtualenv-path' is
134 ;; provided. When this variable is set with the path of the
135 ;; virtualenv to use, `process-environment' and `exec-path' get proper
136 ;; values in order to run shells inside the specified virtualenv. So
137 ;; the following will achieve the same as the previous example:
138
139 ;; (setq python-shell-virtualenv-path "/path/to/env/")
140
141 ;; Also the `python-shell-extra-pythonpaths' variable have been
142 ;; introduced as simple way of adding paths to the PYTHONPATH without
143 ;; affecting existing values.
144
145 ;; Pdb tracking: when you execute a block of code that contains some
146 ;; call to pdb (or ipdb) it will prompt the block of code and will
147 ;; follow the execution of pdb marking the current line with an arrow.
148
149 ;; Symbol completion: you can complete the symbol at point. It uses
150 ;; the shell completion in background so you should run
151 ;; `python-shell-send-buffer' from time to time to get better results.
152
153 ;; Skeletons: 6 skeletons are provided for simple inserting of class,
154 ;; def, for, if, try and while. These skeletons are integrated with
155 ;; dabbrev. If you have `dabbrev-mode' activated and
156 ;; `python-skeleton-autoinsert' is set to t, then whenever you type
157 ;; the name of any of those defined and hit SPC, they will be
158 ;; automatically expanded.
159
160 ;; FFAP: You can find the filename for a given module when using ffap
161 ;; out of the box. This feature needs an inferior python shell
162 ;; running.
163
164 ;; Code check: Check the current file for errors with `python-check'
165 ;; using the program defined in `python-check-command'.
166
167 ;; Eldoc: returns documentation for object at point by using the
168 ;; inferior python subprocess to inspect its documentation. As you
169 ;; might guessed you should run `python-shell-send-buffer' from time
170 ;; to time to get better results too.
171
172 ;; imenu: This mode supports imenu in its most basic form, letting it
173 ;; build the necessary alist via `imenu-default-create-index-function'
174 ;; by having set `imenu-extract-index-name-function' to
175 ;; `python-info-current-defun'.
176
177 ;; If you used python-mode.el you probably will miss auto-indentation
178 ;; when inserting newlines. To achieve the same behavior you have
179 ;; two options:
180
181 ;; 1) Use GNU/Emacs' standard binding for `newline-and-indent': C-j.
182
183 ;; 2) Add the following hook in your .emacs:
184
185 ;; (add-hook 'python-mode-hook
186 ;; #'(lambda ()
187 ;; (define-key python-mode-map "\C-m" 'newline-and-indent)))
188
189 ;; I'd recommend the first one since you'll get the same behavior for
190 ;; all modes out-of-the-box.
191
192 ;;; Installation:
193
194 ;; Add this to your .emacs:
195
196 ;; (add-to-list 'load-path "/folder/containing/file")
197 ;; (require 'python)
198
199 ;;; TODO:
200
201 ;;; Code:
202
203 (require 'ansi-color)
204 (require 'comint)
205
206 (eval-when-compile
207 (require 'cl)
208 ;; Avoid compiler warnings
209 (defvar view-return-to-alist)
210 (defvar compilation-error-regexp-alist)
211 (defvar outline-heading-end-regexp))
212
213 (autoload 'comint-mode "comint")
214
215 ;;;###autoload
216 (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode))
217 ;;;###autoload
218 (add-to-list 'interpreter-mode-alist (cons (purecopy "python") 'python-mode))
219
220 (defgroup python nil
221 "Python Language's flying circus support for Emacs."
222 :group 'languages
223 :version "23.2"
224 :link '(emacs-commentary-link "python"))
225
226 \f
227 ;;; Bindings
228
229 (defvar python-mode-map
230 (let ((map (make-sparse-keymap)))
231 ;; Movement
232 (substitute-key-definition 'backward-sentence
233 'python-nav-backward-block
234 map global-map)
235 (substitute-key-definition 'forward-sentence
236 'python-nav-forward-block
237 map global-map)
238 (define-key map "\C-c\C-j" 'imenu)
239 ;; Indent specific
240 (define-key map "\177" 'python-indent-dedent-line-backspace)
241 (define-key map (kbd "<backtab>") 'python-indent-dedent-line)
242 (define-key map "\C-c<" 'python-indent-shift-left)
243 (define-key map "\C-c>" 'python-indent-shift-right)
244 (define-key map ":" 'python-indent-electric-colon)
245 ;; Skeletons
246 (define-key map "\C-c\C-tc" 'python-skeleton-class)
247 (define-key map "\C-c\C-td" 'python-skeleton-def)
248 (define-key map "\C-c\C-tf" 'python-skeleton-for)
249 (define-key map "\C-c\C-ti" 'python-skeleton-if)
250 (define-key map "\C-c\C-tt" 'python-skeleton-try)
251 (define-key map "\C-c\C-tw" 'python-skeleton-while)
252 ;; Shell interaction
253 (define-key map "\C-c\C-p" 'run-python)
254 (define-key map "\C-c\C-s" 'python-shell-send-string)
255 (define-key map "\C-c\C-r" 'python-shell-send-region)
256 (define-key map "\C-\M-x" 'python-shell-send-defun)
257 (define-key map "\C-c\C-c" 'python-shell-send-buffer)
258 (define-key map "\C-c\C-l" 'python-shell-send-file)
259 (define-key map "\C-c\C-z" 'python-shell-switch-to-shell)
260 ;; Some util commands
261 (define-key map "\C-c\C-v" 'python-check)
262 (define-key map "\C-c\C-f" 'python-eldoc-at-point)
263 ;; Utilities
264 (substitute-key-definition 'complete-symbol 'completion-at-point
265 map global-map)
266 (easy-menu-define python-menu map "Python Mode menu"
267 `("Python"
268 :help "Python-specific Features"
269 ["Shift region left" python-indent-shift-left :active mark-active
270 :help "Shift region left by a single indentation step"]
271 ["Shift region right" python-indent-shift-right :active mark-active
272 :help "Shift region right by a single indentation step"]
273 "-"
274 ["Start of def/class" beginning-of-defun
275 :help "Go to start of outermost definition around point"]
276 ["End of def/class" end-of-defun
277 :help "Go to end of definition around point"]
278 ["Mark def/class" mark-defun
279 :help "Mark outermost definition around point"]
280 ["Jump to def/class" imenu
281 :help "Jump to a class or function definition"]
282 "--"
283 ("Skeletons")
284 "---"
285 ["Start interpreter" run-python
286 :help "Run inferior Python process in a separate buffer"]
287 ["Switch to shell" python-shell-switch-to-shell
288 :help "Switch to running inferior Python process"]
289 ["Eval string" python-shell-send-string
290 :help "Eval string in inferior Python session"]
291 ["Eval buffer" python-shell-send-buffer
292 :help "Eval buffer in inferior Python session"]
293 ["Eval region" python-shell-send-region
294 :help "Eval region in inferior Python session"]
295 ["Eval defun" python-shell-send-defun
296 :help "Eval defun in inferior Python session"]
297 ["Eval file" python-shell-send-file
298 :help "Eval file in inferior Python session"]
299 ["Debugger" pdb :help "Run pdb under GUD"]
300 "----"
301 ["Check file" python-check
302 :help "Check file for errors"]
303 ["Help on symbol" python-eldoc-at-point
304 :help "Get help on symbol at point"]
305 ["Complete symbol" completion-at-point
306 :help "Complete symbol before point"]))
307 map)
308 "Keymap for `python-mode'.")
309
310 \f
311 ;;; Python specialized rx
312
313 (eval-when-compile
314 (defconst python-rx-constituents
315 `((block-start . ,(rx symbol-start
316 (or "def" "class" "if" "elif" "else" "try"
317 "except" "finally" "for" "while" "with")
318 symbol-end))
319 (decorator . ,(rx line-start (* space) ?@ (any letter ?_)
320 (* (any word ?_))))
321 (defun . ,(rx symbol-start (or "def" "class") symbol-end))
322 (if-name-main . ,(rx line-start "if" (+ space) "__name__"
323 (+ space) "==" (+ space)
324 (any ?' ?\") "__main__" (any ?' ?\")
325 (* space) ?:))
326 (symbol-name . ,(rx (any letter ?_) (* (any word ?_))))
327 (open-paren . ,(rx (or "{" "[" "(")))
328 (close-paren . ,(rx (or "}" "]" ")")))
329 (simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))
330 ;; FIXME: rx should support (not simple-operator).
331 (not-simple-operator . ,(rx
332 (not
333 (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))))
334 ;; FIXME: Use regexp-opt.
335 (operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
336 "=" "%" "**" "//" "<<" ">>" "<=" "!="
337 "==" ">=" "is" "not")))
338 ;; FIXME: Use regexp-opt.
339 (assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**="
340 ">>=" "<<=" "&=" "^=" "|="))))
341 "Additional Python specific sexps for `python-rx'"))
342
343 (defmacro python-rx (&rest regexps)
344 "Python mode specialized rx macro.
345 This variant of `rx' supports common python named REGEXPS."
346 (let ((rx-constituents (append python-rx-constituents rx-constituents)))
347 (cond ((null regexps)
348 (error "No regexp"))
349 ((cdr regexps)
350 (rx-to-string `(and ,@regexps) t))
351 (t
352 (rx-to-string (car regexps) t)))))
353
354 \f
355 ;;; Font-lock and syntax
356
357 (defun python-syntax-context (type &optional syntax-ppss)
358 "Return non-nil if point is on TYPE using SYNTAX-PPSS.
359 TYPE can be `comment', `string' or `paren'. It returns the start
360 character address of the specified TYPE."
361 (let ((ppss (or syntax-ppss (syntax-ppss))))
362 (case type
363 (comment (and (nth 4 ppss) (nth 8 ppss)))
364 (string (and (not (nth 4 ppss)) (nth 8 ppss)))
365 (paren (nth 1 ppss))
366 (t nil))))
367
368 (defun python-syntax-context-type (&optional syntax-ppss)
369 "Return the context type using SYNTAX-PPSS.
370 The type returned can be `comment', `string' or `paren'."
371 (let ((ppss (or syntax-ppss (syntax-ppss))))
372 (cond
373 ((nth 8 ppss) (if (nth 4 ppss) 'comment 'string))
374 ((nth 1 ppss) 'paren))))
375
376 (defsubst python-syntax-comment-or-string-p ()
377 "Return non-nil if point is inside 'comment or 'string."
378 (nth 8 (syntax-ppss)))
379
380 (define-obsolete-function-alias
381 'python-info-ppss-context #'python-syntax-context "24.2")
382
383 (define-obsolete-function-alias
384 'python-info-ppss-context-type #'python-syntax-context-type "24.2")
385
386 (define-obsolete-function-alias
387 'python-info-ppss-comment-or-string-p
388 #'python-syntax-comment-or-string-p "24.2")
389
390 (defvar python-font-lock-keywords
391 ;; Keywords
392 `(,(rx symbol-start
393 (or
394 "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with"
395 "assert" "else" "if" "pass" "yield" "break" "except" "import" "class"
396 "in" "raise" "continue" "finally" "is" "return" "def" "for" "lambda"
397 "try"
398 ;; Python 2:
399 "print" "exec"
400 ;; Python 3:
401 ;; False, None, and True are listed as keywords on the Python 3
402 ;; documentation, but since they also qualify as constants they are
403 ;; fontified like that in order to keep font-lock consistent between
404 ;; Python versions.
405 "nonlocal"
406 ;; Extra:
407 "self")
408 symbol-end)
409 ;; functions
410 (,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_))))
411 (1 font-lock-function-name-face))
412 ;; classes
413 (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_))))
414 (1 font-lock-type-face))
415 ;; Constants
416 (,(rx symbol-start
417 (or
418 "Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
419 ;; copyright, license, credits, quit and exit are added by the site
420 ;; module and they are not intended to be used in programs
421 "copyright" "credits" "exit" "license" "quit")
422 symbol-end) . font-lock-constant-face)
423 ;; Decorators.
424 (,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_))
425 (0+ "." (1+ (or word ?_)))))
426 (1 font-lock-type-face))
427 ;; Builtin Exceptions
428 (,(rx symbol-start
429 (or
430 "ArithmeticError" "AssertionError" "AttributeError" "BaseException"
431 "DeprecationWarning" "EOFError" "EnvironmentError" "Exception"
432 "FloatingPointError" "FutureWarning" "GeneratorExit" "IOError"
433 "ImportError" "ImportWarning" "IndexError" "KeyError"
434 "KeyboardInterrupt" "LookupError" "MemoryError" "NameError"
435 "NotImplementedError" "OSError" "OverflowError"
436 "PendingDeprecationWarning" "ReferenceError" "RuntimeError"
437 "RuntimeWarning" "StopIteration" "SyntaxError" "SyntaxWarning"
438 "SystemError" "SystemExit" "TypeError" "UnboundLocalError"
439 "UnicodeDecodeError" "UnicodeEncodeError" "UnicodeError"
440 "UnicodeTranslateError" "UnicodeWarning" "UserWarning" "VMSError"
441 "ValueError" "Warning" "WindowsError" "ZeroDivisionError"
442 ;; Python 2:
443 "StandardError"
444 ;; Python 3:
445 "BufferError" "BytesWarning" "IndentationError" "ResourceWarning"
446 "TabError")
447 symbol-end) . font-lock-type-face)
448 ;; Builtins
449 (,(rx symbol-start
450 (or
451 "abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
452 "compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
453 "eval" "filter" "float" "format" "frozenset" "getattr" "globals"
454 "hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
455 "issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
456 "min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
457 "range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
458 "staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
459 "__import__"
460 ;; Python 2:
461 "basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
462 "reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
463 "intern"
464 ;; Python 3:
465 "ascii" "bytearray" "bytes" "exec"
466 ;; Extra:
467 "__all__" "__doc__" "__name__" "__package__")
468 symbol-end) . font-lock-builtin-face)
469 ;; assignments
470 ;; support for a = b = c = 5
471 (,(lambda (limit)
472 (let ((re (python-rx (group (+ (any word ?. ?_)))
473 (? ?\[ (+ (not (any ?\]))) ?\]) (* space)
474 assignment-operator)))
475 (when (re-search-forward re limit t)
476 (while (and (python-syntax-context 'paren)
477 (re-search-forward re limit t)))
478 (if (and (not (python-syntax-context 'paren))
479 (not (equal (char-after (point-marker)) ?=)))
480 t
481 (set-match-data nil)))))
482 (1 font-lock-variable-name-face nil nil))
483 ;; support for a, b, c = (1, 2, 3)
484 (,(lambda (limit)
485 (let ((re (python-rx (group (+ (any word ?. ?_))) (* space)
486 (* ?, (* space) (+ (any word ?. ?_)) (* space))
487 ?, (* space) (+ (any word ?. ?_)) (* space)
488 assignment-operator)))
489 (when (and (re-search-forward re limit t)
490 (goto-char (nth 3 (match-data))))
491 (while (and (python-syntax-context 'paren)
492 (re-search-forward re limit t))
493 (goto-char (nth 3 (match-data))))
494 (if (not (python-syntax-context 'paren))
495 t
496 (set-match-data nil)))))
497 (1 font-lock-variable-name-face nil nil))))
498
499 (defconst python-syntax-propertize-function
500 ;; Make outer chars of matching triple-quote sequences into generic
501 ;; string delimiters. Fixme: Is there a better way?
502 ;; First avoid a sequence preceded by an odd number of backslashes.
503 (syntax-propertize-rules
504 (;; ¡Backrefs don't work in syntax-propertize-rules!
505 (concat "\\(?:\\([RUru]\\)[Rr]?\\|^\\|[^\\]\\(?:\\\\.\\)*\\)" ;Prefix.
506 "\\(?:\\('\\)'\\('\\)\\|\\(?2:\"\\)\"\\(?3:\"\\)\\)")
507 (3 (ignore (python-quote-syntax))))))
508
509 (defun python-quote-syntax ()
510 "Put `syntax-table' property correctly on triple quote.
511 Used for syntactic keywords. N is the match number (1, 2 or 3)."
512 ;; Given a triple quote, we have to check the context to know
513 ;; whether this is an opening or closing triple or whether it's
514 ;; quoted anyhow, and should be ignored. (For that we need to do
515 ;; the same job as `syntax-ppss' to be correct and it seems to be OK
516 ;; to use it here despite initial worries.) We also have to sort
517 ;; out a possible prefix -- well, we don't _have_ to, but I think it
518 ;; should be treated as part of the string.
519
520 ;; Test cases:
521 ;; ur"""ar""" x='"' # """
522 ;; x = ''' """ ' a
523 ;; '''
524 ;; x '"""' x """ \"""" x
525 (save-excursion
526 (goto-char (match-beginning 0))
527 (let ((syntax (save-match-data (syntax-ppss))))
528 (cond
529 ((eq t (nth 3 syntax)) ; after unclosed fence
530 ;; Consider property for the last char if in a fenced string.
531 (goto-char (nth 8 syntax)) ; fence position
532 (skip-chars-forward "uUrR") ; skip any prefix
533 ;; Is it a matching sequence?
534 (if (eq (char-after) (char-after (match-beginning 2)))
535 (put-text-property (match-beginning 3) (match-end 3)
536 'syntax-table (string-to-syntax "|"))))
537 ((match-end 1)
538 ;; Consider property for initial char, accounting for prefixes.
539 (put-text-property (match-beginning 1) (match-end 1)
540 'syntax-table (string-to-syntax "|")))
541 (t
542 ;; Consider property for initial char, accounting for prefixes.
543 (put-text-property (match-beginning 2) (match-end 2)
544 'syntax-table (string-to-syntax "|"))))
545 )))
546
547 (defvar python-mode-syntax-table
548 (let ((table (make-syntax-table)))
549 ;; Give punctuation syntax to ASCII that normally has symbol
550 ;; syntax or has word syntax and isn't a letter.
551 (let ((symbol (string-to-syntax "_"))
552 (sst (standard-syntax-table)))
553 (dotimes (i 128)
554 (unless (= i ?_)
555 (if (equal symbol (aref sst i))
556 (modify-syntax-entry i "." table)))))
557 (modify-syntax-entry ?$ "." table)
558 (modify-syntax-entry ?% "." table)
559 ;; exceptions
560 (modify-syntax-entry ?# "<" table)
561 (modify-syntax-entry ?\n ">" table)
562 (modify-syntax-entry ?' "\"" table)
563 (modify-syntax-entry ?` "$" table)
564 table)
565 "Syntax table for Python files.")
566
567 (defvar python-dotty-syntax-table
568 (let ((table (make-syntax-table python-mode-syntax-table)))
569 (modify-syntax-entry ?. "w" table)
570 (modify-syntax-entry ?_ "w" table)
571 table)
572 "Dotty syntax table for Python files.
573 It makes underscores and dots word constituent chars.")
574
575 \f
576 ;;; Indentation
577
578 (defcustom python-indent-offset 4
579 "Default indentation offset for Python."
580 :group 'python
581 :type 'integer
582 :safe 'integerp)
583
584 (defcustom python-indent-guess-indent-offset t
585 "Non-nil tells Python mode to guess `python-indent-offset' value."
586 :type 'boolean
587 :group 'python
588 :safe 'booleanp)
589
590 (define-obsolete-variable-alias
591 'python-indent 'python-indent-offset "24.2")
592
593 (define-obsolete-variable-alias
594 'python-guess-indent 'python-indent-guess-indent-offset "24.2")
595
596 (defvar python-indent-current-level 0
597 "Current indentation level `python-indent-line-function' is using.")
598
599 (defvar python-indent-levels '(0)
600 "Levels of indentation available for `python-indent-line-function'.")
601
602 (defvar python-indent-dedenters '("else" "elif" "except" "finally")
603 "List of words that should be dedented.
604 These make `python-indent-calculate-indentation' subtract the value of
605 `python-indent-offset'.")
606
607 (defun python-indent-guess-indent-offset ()
608 "Guess and set `python-indent-offset' for the current buffer."
609 (interactive)
610 (save-excursion
611 (save-restriction
612 (widen)
613 (goto-char (point-min))
614 (let ((block-end))
615 (while (and (not block-end)
616 (re-search-forward
617 (python-rx line-start block-start) nil t))
618 (when (and
619 (not (python-syntax-context-type))
620 (progn
621 (goto-char (line-end-position))
622 (python-util-forward-comment -1)
623 (if (equal (char-before) ?:)
624 t
625 (forward-line 1)
626 (when (python-info-block-continuation-line-p)
627 (while (and (python-info-continuation-line-p)
628 (not (eobp)))
629 (forward-line 1))
630 (python-util-forward-comment -1)
631 (when (equal (char-before) ?:)
632 t)))))
633 (setq block-end (point-marker))))
634 (let ((indentation
635 (when block-end
636 (goto-char block-end)
637 (python-util-forward-comment)
638 (current-indentation))))
639 (if indentation
640 (setq python-indent-offset indentation)
641 (message "Can't guess python-indent-offset, using defaults: %s"
642 python-indent-offset)))))))
643
644 (defun python-indent-context ()
645 "Get information on indentation context.
646 Context information is returned with a cons with the form:
647 \(STATUS . START)
648
649 Where status can be any of the following symbols:
650 * inside-paren: If point in between (), {} or []
651 * inside-string: If point is inside a string
652 * after-backslash: Previous line ends in a backslash
653 * after-beginning-of-block: Point is after beginning of block
654 * after-line: Point is after normal line
655 * no-indent: Point is at beginning of buffer or other special case
656 START is the buffer position where the sexp starts."
657 (save-restriction
658 (widen)
659 (let ((ppss (save-excursion (beginning-of-line) (syntax-ppss)))
660 (start))
661 (cons
662 (cond
663 ;; Beginning of buffer
664 ((save-excursion
665 (goto-char (line-beginning-position))
666 (bobp))
667 'no-indent)
668 ;; Inside a paren
669 ((setq start (python-syntax-context 'paren ppss))
670 'inside-paren)
671 ;; Inside string
672 ((setq start (python-syntax-context 'string ppss))
673 'inside-string)
674 ;; After backslash
675 ((setq start (when (not (or (python-syntax-context 'string ppss)
676 (python-syntax-context 'comment ppss)))
677 (let ((line-beg-pos (line-beginning-position)))
678 (when (python-info-line-ends-backslash-p
679 (1- line-beg-pos))
680 (- line-beg-pos 2)))))
681 'after-backslash)
682 ;; After beginning of block
683 ((setq start (save-excursion
684 (when (progn
685 (back-to-indentation)
686 (python-util-forward-comment -1)
687 (equal (char-before) ?:))
688 ;; Move to the first block start that's not in within
689 ;; a string, comment or paren and that's not a
690 ;; continuation line.
691 (while (and (re-search-backward
692 (python-rx block-start) nil t)
693 (or
694 (python-syntax-context-type)
695 (python-info-continuation-line-p))))
696 (when (looking-at (python-rx block-start))
697 (point-marker)))))
698 'after-beginning-of-block)
699 ;; After normal line
700 ((setq start (save-excursion
701 (back-to-indentation)
702 (python-util-forward-comment -1)
703 (python-nav-beginning-of-statement)
704 (point-marker)))
705 'after-line)
706 ;; Do not indent
707 (t 'no-indent))
708 start))))
709
710 (defun python-indent-calculate-indentation ()
711 "Calculate correct indentation offset for the current line."
712 (let* ((indentation-context (python-indent-context))
713 (context-status (car indentation-context))
714 (context-start (cdr indentation-context)))
715 (save-restriction
716 (widen)
717 (save-excursion
718 (case context-status
719 ('no-indent 0)
720 ;; When point is after beginning of block just add one level
721 ;; of indentation relative to the context-start
722 ('after-beginning-of-block
723 (goto-char context-start)
724 (+ (current-indentation) python-indent-offset))
725 ;; When after a simple line just use previous line
726 ;; indentation, in the case current line starts with a
727 ;; `python-indent-dedenters' de-indent one level.
728 ('after-line
729 (-
730 (save-excursion
731 (goto-char context-start)
732 (current-indentation))
733 (if (progn
734 (back-to-indentation)
735 (looking-at (regexp-opt python-indent-dedenters)))
736 python-indent-offset
737 0)))
738 ;; When inside of a string, do nothing. just use the current
739 ;; indentation. XXX: perhaps it would be a good idea to
740 ;; invoke standard text indentation here
741 ('inside-string
742 (goto-char context-start)
743 (current-indentation))
744 ;; After backslash we have several possibilities.
745 ('after-backslash
746 (cond
747 ;; Check if current line is a dot continuation. For this
748 ;; the current line must start with a dot and previous
749 ;; line must contain a dot too.
750 ((save-excursion
751 (back-to-indentation)
752 (when (looking-at "\\.")
753 ;; If after moving one line back point is inside a paren it
754 ;; needs to move back until it's not anymore
755 (while (prog2
756 (forward-line -1)
757 (and (not (bobp))
758 (python-syntax-context 'paren))))
759 (goto-char (line-end-position))
760 (while (and (re-search-backward
761 "\\." (line-beginning-position) t)
762 (python-syntax-context-type)))
763 (if (and (looking-at "\\.")
764 (not (python-syntax-context-type)))
765 ;; The indentation is the same column of the
766 ;; first matching dot that's not inside a
767 ;; comment, a string or a paren
768 (current-column)
769 ;; No dot found on previous line, just add another
770 ;; indentation level.
771 (+ (current-indentation) python-indent-offset)))))
772 ;; Check if prev line is a block continuation
773 ((let ((block-continuation-start
774 (python-info-block-continuation-line-p)))
775 (when block-continuation-start
776 ;; If block-continuation-start is set jump to that
777 ;; marker and use first column after the block start
778 ;; as indentation value.
779 (goto-char block-continuation-start)
780 (re-search-forward
781 (python-rx block-start (* space))
782 (line-end-position) t)
783 (current-column))))
784 ;; Check if current line is an assignment continuation
785 ((let ((assignment-continuation-start
786 (python-info-assignment-continuation-line-p)))
787 (when assignment-continuation-start
788 ;; If assignment-continuation is set jump to that
789 ;; marker and use first column after the assignment
790 ;; operator as indentation value.
791 (goto-char assignment-continuation-start)
792 (current-column))))
793 (t
794 (forward-line -1)
795 (goto-char (python-info-beginning-of-backslash))
796 (if (save-excursion
797 (and
798 (forward-line -1)
799 (goto-char
800 (or (python-info-beginning-of-backslash) (point)))
801 (python-info-line-ends-backslash-p)))
802 ;; The two previous lines ended in a backslash so we must
803 ;; respect previous line indentation.
804 (current-indentation)
805 ;; What happens here is that we are dealing with the second
806 ;; line of a backslash continuation, in that case we just going
807 ;; to add one indentation level.
808 (+ (current-indentation) python-indent-offset)))))
809 ;; When inside a paren there's a need to handle nesting
810 ;; correctly
811 ('inside-paren
812 (cond
813 ;; If current line closes the outermost open paren use the
814 ;; current indentation of the context-start line.
815 ((save-excursion
816 (skip-syntax-forward "\s" (line-end-position))
817 (when (and (looking-at (regexp-opt '(")" "]" "}")))
818 (progn
819 (forward-char 1)
820 (not (python-syntax-context 'paren))))
821 (goto-char context-start)
822 (current-indentation))))
823 ;; If open paren is contained on a line by itself add another
824 ;; indentation level, else look for the first word after the
825 ;; opening paren and use it's column position as indentation
826 ;; level.
827 ((let* ((content-starts-in-newline)
828 (indent
829 (save-excursion
830 (if (setq content-starts-in-newline
831 (progn
832 (goto-char context-start)
833 (forward-char)
834 (save-restriction
835 (narrow-to-region
836 (line-beginning-position)
837 (line-end-position))
838 (python-util-forward-comment))
839 (looking-at "$")))
840 (+ (current-indentation) python-indent-offset)
841 (current-column)))))
842 ;; Adjustments
843 (cond
844 ;; If current line closes a nested open paren de-indent one
845 ;; level.
846 ((progn
847 (back-to-indentation)
848 (looking-at (regexp-opt '(")" "]" "}"))))
849 (- indent python-indent-offset))
850 ;; If the line of the opening paren that wraps the current
851 ;; line starts a block add another level of indentation to
852 ;; follow new pep8 recommendation. See: http://ur1.ca/5rojx
853 ((save-excursion
854 (when (and content-starts-in-newline
855 (progn
856 (goto-char context-start)
857 (back-to-indentation)
858 (looking-at (python-rx block-start))))
859 (+ indent python-indent-offset))))
860 (t indent)))))))))))
861
862 (defun python-indent-calculate-levels ()
863 "Calculate `python-indent-levels' and reset `python-indent-current-level'."
864 (let* ((indentation (python-indent-calculate-indentation))
865 (remainder (% indentation python-indent-offset))
866 (steps (/ (- indentation remainder) python-indent-offset)))
867 (setq python-indent-levels (list 0))
868 (dotimes (step steps)
869 (push (* python-indent-offset (1+ step)) python-indent-levels))
870 (when (not (eq 0 remainder))
871 (push (+ (* python-indent-offset steps) remainder) python-indent-levels))
872 (setq python-indent-levels (nreverse python-indent-levels))
873 (setq python-indent-current-level (1- (length python-indent-levels)))))
874
875 (defun python-indent-toggle-levels ()
876 "Toggle `python-indent-current-level' over `python-indent-levels'."
877 (setq python-indent-current-level (1- python-indent-current-level))
878 (when (< python-indent-current-level 0)
879 (setq python-indent-current-level (1- (length python-indent-levels)))))
880
881 (defun python-indent-line (&optional force-toggle)
882 "Internal implementation of `python-indent-line-function'.
883 Uses the offset calculated in
884 `python-indent-calculate-indentation' and available levels
885 indicated by the variable `python-indent-levels' to set the
886 current indentation.
887
888 When the variable `last-command' is equal to
889 `indent-for-tab-command' or FORCE-TOGGLE is non-nil it cycles
890 levels indicated in the variable `python-indent-levels' by
891 setting the current level in the variable
892 `python-indent-current-level'.
893
894 When the variable `last-command' is not equal to
895 `indent-for-tab-command' and FORCE-TOGGLE is nil it calculates
896 possible indentation levels and saves it in the variable
897 `python-indent-levels'. Afterwards it sets the variable
898 `python-indent-current-level' correctly so offset is equal
899 to (`nth' `python-indent-current-level' `python-indent-levels')"
900 (if (or (and (eq this-command 'indent-for-tab-command)
901 (eq last-command this-command))
902 force-toggle)
903 (if (not (equal python-indent-levels '(0)))
904 (python-indent-toggle-levels)
905 (python-indent-calculate-levels))
906 (python-indent-calculate-levels))
907 (beginning-of-line)
908 (delete-horizontal-space)
909 (indent-to (nth python-indent-current-level python-indent-levels))
910 (python-info-closing-block-message))
911
912 (defun python-indent-line-function ()
913 "`indent-line-function' for Python mode.
914 See `python-indent-line' for details."
915 (python-indent-line))
916
917 (defun python-indent-dedent-line ()
918 "De-indent current line."
919 (interactive "*")
920 (when (and (not (python-syntax-comment-or-string-p))
921 (<= (point-marker) (save-excursion
922 (back-to-indentation)
923 (point-marker)))
924 (> (current-column) 0))
925 (python-indent-line t)
926 t))
927
928 (defun python-indent-dedent-line-backspace (arg)
929 "De-indent current line.
930 Argument ARG is passed to `backward-delete-char-untabify' when
931 point is not in between the indentation."
932 (interactive "*p")
933 (when (not (python-indent-dedent-line))
934 (backward-delete-char-untabify arg)))
935 (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)
936
937 (defun python-indent-region (start end)
938 "Indent a python region automagically.
939
940 Called from a program, START and END specify the region to indent."
941 (let ((deactivate-mark nil))
942 (save-excursion
943 (goto-char end)
944 (setq end (point-marker))
945 (goto-char start)
946 (or (bolp) (forward-line 1))
947 (while (< (point) end)
948 (or (and (bolp) (eolp))
949 (let (word)
950 (forward-line -1)
951 (back-to-indentation)
952 (setq word (current-word))
953 (forward-line 1)
954 (when word
955 (beginning-of-line)
956 (delete-horizontal-space)
957 (indent-to (python-indent-calculate-indentation)))))
958 (forward-line 1))
959 (move-marker end nil))))
960
961 (defun python-indent-shift-left (start end &optional count)
962 "Shift lines contained in region START END by COUNT columns to the left.
963 COUNT defaults to `python-indent-offset'. If region isn't
964 active, the current line is shifted. The shifted region includes
965 the lines in which START and END lie. An error is signaled if
966 any lines in the region are indented less than COUNT columns."
967 (interactive
968 (if mark-active
969 (list (region-beginning) (region-end) current-prefix-arg)
970 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
971 (if count
972 (setq count (prefix-numeric-value count))
973 (setq count python-indent-offset))
974 (when (> count 0)
975 (let ((deactivate-mark nil))
976 (save-excursion
977 (goto-char start)
978 (while (< (point) end)
979 (if (and (< (current-indentation) count)
980 (not (looking-at "[ \t]*$")))
981 (error "Can't shift all lines enough"))
982 (forward-line))
983 (indent-rigidly start end (- count))))))
984
985 (add-to-list 'debug-ignored-errors "^Can't shift all lines enough")
986
987 (defun python-indent-shift-right (start end &optional count)
988 "Shift lines contained in region START END by COUNT columns to the left.
989 COUNT defaults to `python-indent-offset'. If region isn't
990 active, the current line is shifted. The shifted region includes
991 the lines in which START and END lie."
992 (interactive
993 (if mark-active
994 (list (region-beginning) (region-end) current-prefix-arg)
995 (list (line-beginning-position) (line-end-position) current-prefix-arg)))
996 (let ((deactivate-mark nil))
997 (if count
998 (setq count (prefix-numeric-value count))
999 (setq count python-indent-offset))
1000 (indent-rigidly start end count)))
1001
1002 (defun python-indent-electric-colon (arg)
1003 "Insert a colon and maybe de-indent the current line.
1004 With numeric ARG, just insert that many colons. With
1005 \\[universal-argument], just insert a single colon."
1006 (interactive "*P")
1007 (self-insert-command (if (not (integerp arg)) 1 arg))
1008 (when (and (not arg)
1009 (eolp)
1010 (not (equal ?: (char-after (- (point-marker) 2))))
1011 (not (python-syntax-comment-or-string-p)))
1012 (let ((indentation (current-indentation))
1013 (calculated-indentation (python-indent-calculate-indentation)))
1014 (python-info-closing-block-message)
1015 (when (> indentation calculated-indentation)
1016 (save-excursion
1017 (indent-line-to calculated-indentation)
1018 (when (not (python-info-closing-block-message))
1019 (indent-line-to indentation)))))))
1020 (put 'python-indent-electric-colon 'delete-selection t)
1021
1022 (defun python-indent-post-self-insert-function ()
1023 "Adjust closing paren line indentation after a char is added.
1024 This function is intended to be added to the
1025 `post-self-insert-hook.' If a line renders a paren alone, after
1026 adding a char before it, the line will be re-indented
1027 automatically if needed."
1028 (when (and (eq (char-before) last-command-event)
1029 (not (bolp))
1030 (memq (char-after) '(?\) ?\] ?\})))
1031 (save-excursion
1032 (goto-char (line-beginning-position))
1033 ;; If after going to the beginning of line the point
1034 ;; is still inside a paren it's ok to do the trick
1035 (when (python-syntax-context 'paren)
1036 (let ((indentation (python-indent-calculate-indentation)))
1037 (when (< (current-indentation) indentation)
1038 (indent-line-to indentation)))))))
1039
1040 \f
1041 ;;; Navigation
1042
1043 (defvar python-nav-beginning-of-defun-regexp
1044 (python-rx line-start (* space) defun (+ space) (group symbol-name))
1045 "Regexp matching class or function definition.
1046 The name of the defun should be grouped so it can be retrieved
1047 via `match-string'.")
1048
1049 (defun python-nav-beginning-of-defun (&optional arg)
1050 "Move point to `beginning-of-defun'.
1051 With positive ARG move search backwards. With negative do the
1052 same but forward. When ARG is nil or 0 defaults to 1. This is
1053 the main part of `python-beginning-of-defun-function'. Return
1054 non-nil if point is moved to `beginning-of-defun'."
1055 (when (or (null arg) (= arg 0)) (setq arg 1))
1056 (let* ((re-search-fn (if (> arg 0)
1057 #'re-search-backward
1058 #'re-search-forward))
1059 (line-beg-pos (line-beginning-position))
1060 (line-content-start (+ line-beg-pos (current-indentation)))
1061 (pos (point-marker))
1062 (found
1063 (progn
1064 (when (and (< arg 0)
1065 (python-info-looking-at-beginning-of-defun))
1066 (end-of-line 1))
1067 (while (and (funcall re-search-fn
1068 python-nav-beginning-of-defun-regexp nil t)
1069 (python-syntax-context-type)))
1070 (and (python-info-looking-at-beginning-of-defun)
1071 (or (not (= (line-number-at-pos pos)
1072 (line-number-at-pos)))
1073 (and (>= (point) line-beg-pos)
1074 (<= (point) line-content-start)
1075 (> pos line-content-start)))))))
1076 (if found
1077 (or (beginning-of-line 1) t)
1078 (and (goto-char pos) nil))))
1079
1080 (defun python-beginning-of-defun-function (&optional arg)
1081 "Move point to the beginning of def or class.
1082 With positive ARG move that number of functions backwards. With
1083 negative do the same but forward. When ARG is nil or 0 defaults
1084 to 1. Return non-nil if point is moved to `beginning-of-defun'."
1085 (when (or (null arg) (= arg 0)) (setq arg 1))
1086 (let ((found))
1087 (cond ((and (eq this-command 'mark-defun)
1088 (python-info-looking-at-beginning-of-defun)))
1089 (t
1090 (dotimes (i (if (> arg 0) arg (- arg)))
1091 (when (and (python-nav-beginning-of-defun arg)
1092 (not found))
1093 (setq found t)))))
1094 found))
1095
1096 (defun python-end-of-defun-function ()
1097 "Move point to the end of def or class.
1098 Returns nil if point is not in a def or class."
1099 (interactive)
1100 (let ((beg-defun-indent))
1101 (when (or (python-info-looking-at-beginning-of-defun)
1102 (python-beginning-of-defun-function 1)
1103 (python-beginning-of-defun-function -1))
1104 (setq beg-defun-indent (current-indentation))
1105 (forward-line 1)
1106 ;; Go as forward as possible
1107 (while (and (or
1108 (python-nav-beginning-of-defun -1)
1109 (and (goto-char (point-max)) nil))
1110 (> (current-indentation) beg-defun-indent)))
1111 (beginning-of-line 1)
1112 ;; Go as backwards as possible
1113 (while (and (forward-line -1)
1114 (not (bobp))
1115 (or (not (current-word))
1116 (equal (char-after (+ (point) (current-indentation))) ?#)
1117 (<= (current-indentation) beg-defun-indent)
1118 (looking-at (python-rx decorator))
1119 (python-syntax-context-type))))
1120 (forward-line 1)
1121 ;; If point falls inside a paren or string context the point is
1122 ;; forwarded at the end of it (or end of buffer if its not closed)
1123 (let ((context-type (python-syntax-context-type)))
1124 (when (memq context-type '(paren string))
1125 ;; Slow but safe.
1126 (while (and (not (eobp))
1127 (python-syntax-context-type))
1128 (forward-line 1)))))))
1129
1130 (defun python-nav-beginning-of-statement ()
1131 "Move to start of current statement."
1132 (interactive "^")
1133 (while (and (or (back-to-indentation) t)
1134 (not (bobp))
1135 (when (or
1136 (save-excursion
1137 (forward-line -1)
1138 (python-info-line-ends-backslash-p))
1139 (python-syntax-context 'string)
1140 (python-syntax-context 'paren))
1141 (forward-line -1)))))
1142
1143 (defun python-nav-end-of-statement ()
1144 "Move to end of current statement."
1145 (interactive "^")
1146 (while (and (goto-char (line-end-position))
1147 (not (eobp))
1148 (when (or
1149 (python-info-line-ends-backslash-p)
1150 (python-syntax-context 'string)
1151 (python-syntax-context 'paren))
1152 (forward-line 1)))))
1153
1154 (defun python-nav-backward-statement (&optional arg)
1155 "Move backward to previous statement.
1156 With ARG, repeat. See `python-nav-forward-statement'."
1157 (interactive "^p")
1158 (or arg (setq arg 1))
1159 (python-nav-forward-statement (- arg)))
1160
1161 (defun python-nav-forward-statement (&optional arg)
1162 "Move forward to next statement.
1163 With ARG, repeat. With negative argument, move ARG times
1164 backward to previous statement."
1165 (interactive "^p")
1166 (or arg (setq arg 1))
1167 (while (> arg 0)
1168 (python-nav-end-of-statement)
1169 (python-util-forward-comment)
1170 (python-nav-beginning-of-statement)
1171 (setq arg (1- arg)))
1172 (while (< arg 0)
1173 (python-nav-beginning-of-statement)
1174 (python-util-forward-comment -1)
1175 (python-nav-beginning-of-statement)
1176 (setq arg (1+ arg))))
1177
1178 (defun python-nav-beginning-of-block ()
1179 "Move to start of current block."
1180 (interactive "^")
1181 (let ((starting-pos (point))
1182 (block-regexp (python-rx
1183 line-start (* whitespace) block-start)))
1184 (if (progn
1185 (python-nav-beginning-of-statement)
1186 (looking-at (python-rx block-start)))
1187 (point-marker)
1188 ;; Go to first line beginning a statement
1189 (while (and (not (bobp))
1190 (or (and (python-nav-beginning-of-statement) nil)
1191 (python-info-current-line-comment-p)
1192 (python-info-current-line-empty-p)))
1193 (forward-line -1))
1194 (let ((block-matching-indent
1195 (- (current-indentation) python-indent-offset)))
1196 (while
1197 (and (python-nav-backward-block)
1198 (> (current-indentation) block-matching-indent)))
1199 (if (and (looking-at (python-rx block-start))
1200 (= (current-indentation) block-matching-indent))
1201 (point-marker)
1202 (and (goto-char starting-pos) nil))))))
1203
1204 (defun python-nav-end-of-block ()
1205 "Move to end of current block."
1206 (interactive "^")
1207 (when (python-nav-beginning-of-block)
1208 (let ((block-indentation (current-indentation)))
1209 (python-nav-end-of-statement)
1210 (while (and (forward-line 1)
1211 (not (eobp))
1212 (or (and (> (current-indentation) block-indentation)
1213 (or (python-nav-end-of-statement) t))
1214 (python-info-current-line-comment-p)
1215 (python-info-current-line-empty-p))))
1216 (python-util-forward-comment -1)
1217 (point-marker))))
1218
1219 (defun python-nav-backward-block (&optional arg)
1220 "Move backward to previous block of code.
1221 With ARG, repeat. See `python-nav-forward-block'."
1222 (interactive "^p")
1223 (or arg (setq arg 1))
1224 (python-nav-forward-block (- arg)))
1225
1226 (defun python-nav-forward-block (&optional arg)
1227 "Move forward to next block of code.
1228 With ARG, repeat. With negative argument, move ARG times
1229 backward to previous block."
1230 (interactive "^p")
1231 (or arg (setq arg 1))
1232 (let ((block-start-regexp
1233 (python-rx line-start (* whitespace) block-start))
1234 (starting-pos (point)))
1235 (while (> arg 0)
1236 (python-nav-end-of-statement)
1237 (while (and
1238 (re-search-forward block-start-regexp nil t)
1239 (python-syntax-context-type)))
1240 (setq arg (1- arg)))
1241 (while (< arg 0)
1242 (python-nav-beginning-of-statement)
1243 (while (and
1244 (re-search-backward block-start-regexp nil t)
1245 (python-syntax-context-type)))
1246 (setq arg (1+ arg)))
1247 (python-nav-beginning-of-statement)
1248 (if (not (looking-at (python-rx block-start)))
1249 (and (goto-char starting-pos) nil)
1250 (and (not (= (point) starting-pos)) (point-marker)))))
1251
1252 (defun python-nav-forward-sexp-function (&optional arg)
1253 "Move forward across one block of code.
1254 With ARG, do it that many times. Negative arg -N means
1255 move backward N times."
1256 (interactive "^p")
1257 (or arg (setq arg 1))
1258 (while (> arg 0)
1259 (let ((block-starting-pos
1260 (save-excursion (python-nav-beginning-of-block)))
1261 (block-ending-pos
1262 (save-excursion (python-nav-end-of-block)))
1263 (next-block-starting-pos
1264 (save-excursion (python-nav-forward-block))))
1265 (cond ((not block-starting-pos)
1266 (python-nav-forward-block))
1267 ((= (point) block-starting-pos)
1268 (if (or (not next-block-starting-pos)
1269 (< block-ending-pos next-block-starting-pos))
1270 (python-nav-end-of-block)
1271 (python-nav-forward-block)))
1272 ((= block-ending-pos (point))
1273 (let ((parent-block-end-pos
1274 (save-excursion
1275 (python-util-forward-comment)
1276 (python-nav-beginning-of-block)
1277 (python-nav-end-of-block))))
1278 (if (and parent-block-end-pos
1279 (or (not next-block-starting-pos)
1280 (> next-block-starting-pos parent-block-end-pos)))
1281 (goto-char parent-block-end-pos)
1282 (python-nav-forward-block))))
1283 (t (python-nav-end-of-block))))
1284 (setq arg (1- arg)))
1285 (while (< arg 0)
1286 (let* ((block-starting-pos
1287 (save-excursion (python-nav-beginning-of-block)))
1288 (block-ending-pos
1289 (save-excursion (python-nav-end-of-block)))
1290 (prev-block-ending-pos
1291 (save-excursion (when (python-nav-backward-block)
1292 (python-nav-end-of-block))))
1293 (prev-block-parent-ending-pos
1294 (save-excursion
1295 (when prev-block-ending-pos
1296 (goto-char prev-block-ending-pos)
1297 (python-util-forward-comment)
1298 (python-nav-beginning-of-block)
1299 (python-nav-end-of-block)))))
1300 (cond ((not block-ending-pos)
1301 (and (python-nav-backward-block)
1302 (python-nav-end-of-block)))
1303 ((= (point) block-ending-pos)
1304 (let ((candidates))
1305 (dolist (name
1306 '(prev-block-parent-ending-pos
1307 prev-block-ending-pos
1308 block-ending-pos
1309 block-starting-pos))
1310 (when (and (symbol-value name)
1311 (< (symbol-value name) (point)))
1312 (add-to-list 'candidates (symbol-value name))))
1313 (goto-char (apply 'max candidates))))
1314 ((> (point) block-ending-pos)
1315 (python-nav-end-of-block))
1316 ((= (point) block-starting-pos)
1317 (if (not (> (point) (or prev-block-ending-pos (point))))
1318 (python-nav-backward-block)
1319 (goto-char prev-block-ending-pos)
1320 (let ((parent-block-ending-pos
1321 (save-excursion
1322 (python-nav-forward-sexp-function)
1323 (and (not (looking-at (python-rx block-start)))
1324 (point)))))
1325 (when (and parent-block-ending-pos
1326 (> parent-block-ending-pos prev-block-ending-pos))
1327 (goto-char parent-block-ending-pos)))))
1328 (t (python-nav-beginning-of-block))))
1329 (setq arg (1+ arg))))
1330
1331 \f
1332 ;;; Shell integration
1333
1334 (defcustom python-shell-buffer-name "Python"
1335 "Default buffer name for Python interpreter."
1336 :type 'string
1337 :group 'python
1338 :safe 'stringp)
1339
1340 (defcustom python-shell-interpreter "python"
1341 "Default Python interpreter for shell."
1342 :type 'string
1343 :group 'python)
1344
1345 (defcustom python-shell-internal-buffer-name "Python Internal"
1346 "Default buffer name for the Internal Python interpreter."
1347 :type 'string
1348 :group 'python
1349 :safe 'stringp)
1350
1351 (defcustom python-shell-interpreter-args "-i"
1352 "Default arguments for the Python interpreter."
1353 :type 'string
1354 :group 'python)
1355
1356 (defcustom python-shell-prompt-regexp ">>> "
1357 "Regular Expression matching top\-level input prompt of python shell.
1358 It should not contain a caret (^) at the beginning."
1359 :type 'string
1360 :group 'python
1361 :safe 'stringp)
1362
1363 (defcustom python-shell-prompt-block-regexp "[.][.][.] "
1364 "Regular Expression matching block input prompt of python shell.
1365 It should not contain a caret (^) at the beginning."
1366 :type 'string
1367 :group 'python
1368 :safe 'stringp)
1369
1370 (defcustom python-shell-prompt-output-regexp ""
1371 "Regular Expression matching output prompt of python shell.
1372 It should not contain a caret (^) at the beginning."
1373 :type 'string
1374 :group 'python
1375 :safe 'stringp)
1376
1377 (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ "
1378 "Regular Expression matching pdb input prompt of python shell.
1379 It should not contain a caret (^) at the beginning."
1380 :type 'string
1381 :group 'python
1382 :safe 'stringp)
1383
1384 (defcustom python-shell-enable-font-lock t
1385 "Should syntax highlighting be enabled in the python shell buffer?
1386 Restart the python shell after changing this variable for it to take effect."
1387 :type 'boolean
1388 :group 'python
1389 :safe 'booleanp)
1390
1391 (defcustom python-shell-process-environment nil
1392 "List of environment variables for Python shell.
1393 This variable follows the same rules as `process-environment'
1394 since it merges with it before the process creation routines are
1395 called. When this variable is nil, the Python shell is run with
1396 the default `process-environment'."
1397 :type '(repeat string)
1398 :group 'python
1399 :safe 'listp)
1400
1401 (defcustom python-shell-extra-pythonpaths nil
1402 "List of extra pythonpaths for Python shell.
1403 The values of this variable are added to the existing value of
1404 PYTHONPATH in the `process-environment' variable."
1405 :type '(repeat string)
1406 :group 'python
1407 :safe 'listp)
1408
1409 (defcustom python-shell-exec-path nil
1410 "List of path to search for binaries.
1411 This variable follows the same rules as `exec-path' since it
1412 merges with it before the process creation routines are called.
1413 When this variable is nil, the Python shell is run with the
1414 default `exec-path'."
1415 :type '(repeat string)
1416 :group 'python
1417 :safe 'listp)
1418
1419 (defcustom python-shell-virtualenv-path nil
1420 "Path to virtualenv root.
1421 This variable, when set to a string, makes the values stored in
1422 `python-shell-process-environment' and `python-shell-exec-path'
1423 to be modified properly so shells are started with the specified
1424 virtualenv."
1425 :type 'string
1426 :group 'python
1427 :safe 'stringp)
1428
1429 (defcustom python-shell-setup-codes '(python-shell-completion-setup-code
1430 python-ffap-setup-code
1431 python-eldoc-setup-code)
1432 "List of code run by `python-shell-send-setup-codes'."
1433 :type '(repeat symbol)
1434 :group 'python
1435 :safe 'listp)
1436
1437 (defcustom python-shell-compilation-regexp-alist
1438 `((,(rx line-start (1+ (any " \t")) "File \""
1439 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
1440 "\", line " (group (1+ digit)))
1441 1 2)
1442 (,(rx " in file " (group (1+ not-newline)) " on line "
1443 (group (1+ digit)))
1444 1 2)
1445 (,(rx line-start "> " (group (1+ (not (any "(\"<"))))
1446 "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()")
1447 1 2))
1448 "`compilation-error-regexp-alist' for inferior Python."
1449 :type '(alist string)
1450 :group 'python)
1451
1452 (defun python-shell-get-process-name (dedicated)
1453 "Calculate the appropriate process name for inferior Python process.
1454 If DEDICATED is t and the variable `buffer-file-name' is non-nil
1455 returns a string with the form
1456 `python-shell-buffer-name'[variable `buffer-file-name'] else
1457 returns the value of `python-shell-buffer-name'. After
1458 calculating the process name adds the buffer name for the process
1459 in the `same-window-buffer-names' list."
1460 (let ((process-name
1461 (if (and dedicated
1462 buffer-file-name)
1463 (format "%s[%s]" python-shell-buffer-name buffer-file-name)
1464 (format "%s" python-shell-buffer-name))))
1465 (add-to-list 'same-window-buffer-names (purecopy
1466 (format "*%s*" process-name)))
1467 process-name))
1468
1469 (defun python-shell-internal-get-process-name ()
1470 "Calculate the appropriate process name for Internal Python process.
1471 The name is calculated from `python-shell-global-buffer-name' and
1472 a hash of all relevant global shell settings in order to ensure
1473 uniqueness for different types of configurations."
1474 (format "%s [%s]"
1475 python-shell-internal-buffer-name
1476 (md5
1477 (concat
1478 (python-shell-parse-command)
1479 python-shell-prompt-regexp
1480 python-shell-prompt-block-regexp
1481 python-shell-prompt-output-regexp
1482 (mapconcat #'symbol-value python-shell-setup-codes "")
1483 (mapconcat #'identity python-shell-process-environment "")
1484 (mapconcat #'identity python-shell-extra-pythonpaths "")
1485 (mapconcat #'identity python-shell-exec-path "")
1486 (or python-shell-virtualenv-path "")
1487 (mapconcat #'identity python-shell-exec-path "")))))
1488
1489 (defun python-shell-parse-command ()
1490 "Calculate the string used to execute the inferior Python process."
1491 (format "%s %s" python-shell-interpreter python-shell-interpreter-args))
1492
1493 (defun python-shell-calculate-process-environment ()
1494 "Calculate process environment given `python-shell-virtualenv-path'."
1495 (let ((process-environment (append
1496 python-shell-process-environment
1497 process-environment nil))
1498 (virtualenv (if python-shell-virtualenv-path
1499 (directory-file-name python-shell-virtualenv-path)
1500 nil)))
1501 (when python-shell-extra-pythonpaths
1502 (setenv "PYTHONPATH"
1503 (format "%s%s%s"
1504 (mapconcat 'identity
1505 python-shell-extra-pythonpaths
1506 path-separator)
1507 path-separator
1508 (or (getenv "PYTHONPATH") ""))))
1509 (if (not virtualenv)
1510 process-environment
1511 (setenv "PYTHONHOME" nil)
1512 (setenv "PATH" (format "%s/bin%s%s"
1513 virtualenv path-separator
1514 (or (getenv "PATH") "")))
1515 (setenv "VIRTUAL_ENV" virtualenv))
1516 process-environment))
1517
1518 (defun python-shell-calculate-exec-path ()
1519 "Calculate exec path given `python-shell-virtualenv-path'."
1520 (let ((path (append python-shell-exec-path
1521 exec-path nil)))
1522 (if (not python-shell-virtualenv-path)
1523 path
1524 (cons (format "%s/bin"
1525 (directory-file-name python-shell-virtualenv-path))
1526 path))))
1527
1528 (defun python-comint-output-filter-function (output)
1529 "Hook run after content is put into comint buffer.
1530 OUTPUT is a string with the contents of the buffer."
1531 (ansi-color-filter-apply output))
1532
1533 (define-derived-mode inferior-python-mode comint-mode "Inferior Python"
1534 "Major mode for Python inferior process.
1535 Runs a Python interpreter as a subprocess of Emacs, with Python
1536 I/O through an Emacs buffer. Variables
1537 `python-shell-interpreter' and `python-shell-interpreter-args'
1538 controls which Python interpreter is run. Variables
1539 `python-shell-prompt-regexp',
1540 `python-shell-prompt-output-regexp',
1541 `python-shell-prompt-block-regexp',
1542 `python-shell-enable-font-lock',
1543 `python-shell-completion-setup-code',
1544 `python-shell-completion-string-code',
1545 `python-shell-completion-module-string-code',
1546 `python-eldoc-setup-code', `python-eldoc-string-code',
1547 `python-ffap-setup-code' and `python-ffap-string-code' can
1548 customize this mode for different Python interpreters.
1549
1550 You can also add additional setup code to be run at
1551 initialization of the interpreter via `python-shell-setup-codes'
1552 variable.
1553
1554 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
1555 (set-syntax-table python-mode-syntax-table)
1556 (setq mode-line-process '(":%s"))
1557 (setq comint-prompt-regexp (format "^\\(?:%s\\|%s\\|%s\\)"
1558 python-shell-prompt-regexp
1559 python-shell-prompt-block-regexp
1560 python-shell-prompt-pdb-regexp))
1561 (make-local-variable 'comint-output-filter-functions)
1562 (add-hook 'comint-output-filter-functions
1563 'python-comint-output-filter-function)
1564 (add-hook 'comint-output-filter-functions
1565 'python-pdbtrack-comint-output-filter-function)
1566 (set (make-local-variable 'compilation-error-regexp-alist)
1567 python-shell-compilation-regexp-alist)
1568 (define-key inferior-python-mode-map [remap complete-symbol]
1569 'completion-at-point)
1570 (add-hook 'completion-at-point-functions
1571 'python-shell-completion-complete-at-point nil 'local)
1572 (add-to-list (make-local-variable 'comint-dynamic-complete-functions)
1573 'python-shell-completion-complete-at-point)
1574 (define-key inferior-python-mode-map "\t"
1575 'python-shell-completion-complete-or-indent)
1576 (when python-shell-enable-font-lock
1577 (set (make-local-variable 'font-lock-defaults)
1578 '(python-font-lock-keywords nil nil nil nil))
1579 (set (make-local-variable 'syntax-propertize-function)
1580 python-syntax-propertize-function))
1581 (compilation-shell-minor-mode 1))
1582
1583 (defun python-shell-make-comint (cmd proc-name &optional pop internal)
1584 "Create a python shell comint buffer.
1585 CMD is the python command to be executed and PROC-NAME is the
1586 process name the comint buffer will get. After the comint buffer
1587 is created the `inferior-python-mode' is activated. When
1588 optional argument POP is non-nil the buffer is shown. When
1589 optional argument INTERNAL is non-nil this process is run on a
1590 buffer with a name that starts with a space, following the Emacs
1591 convention for temporary/internal buffers, and also makes sure
1592 the user is not queried for confirmation when the process is
1593 killed."
1594 (save-excursion
1595 (let* ((proc-buffer-name
1596 (format (if (not internal) "*%s*" " *%s*") proc-name))
1597 (process-environment (python-shell-calculate-process-environment))
1598 (exec-path (python-shell-calculate-exec-path)))
1599 (when (not (comint-check-proc proc-buffer-name))
1600 (let* ((cmdlist (split-string-and-unquote cmd))
1601 (buffer (apply #'make-comint-in-buffer proc-name proc-buffer-name
1602 (car cmdlist) nil (cdr cmdlist)))
1603 (current-buffer (current-buffer))
1604 (process (get-buffer-process buffer)))
1605 (with-current-buffer buffer
1606 (inferior-python-mode)
1607 (python-util-clone-local-variables current-buffer))
1608 (accept-process-output process)
1609 (and pop (pop-to-buffer buffer t))
1610 (and internal (set-process-query-on-exit-flag process nil))))
1611 proc-buffer-name)))
1612
1613 ;;;###autoload
1614 (defun run-python (cmd &optional dedicated show)
1615 "Run an inferior Python process.
1616 Input and output via buffer named after
1617 `python-shell-buffer-name'. If there is a process already
1618 running in that buffer, just switch to it.
1619
1620 With argument, allows you to define CMD so you can edit the
1621 command used to call the interpreter and define DEDICATED, so a
1622 dedicated process for the current buffer is open. When numeric
1623 prefix arg is other than 0 or 4 do not SHOW.
1624
1625 Runs the hook `inferior-python-mode-hook' (after the
1626 `comint-mode-hook' is run). \(Type \\[describe-mode] in the
1627 process buffer for a list of commands.)"
1628 (interactive
1629 (if current-prefix-arg
1630 (list
1631 (read-string "Run Python: " (python-shell-parse-command))
1632 (y-or-n-p "Make dedicated process? ")
1633 (= (prefix-numeric-value current-prefix-arg) 4))
1634 (list (python-shell-parse-command) nil t)))
1635 (python-shell-make-comint
1636 cmd (python-shell-get-process-name dedicated) show)
1637 dedicated)
1638
1639 (defun run-python-internal ()
1640 "Run an inferior Internal Python process.
1641 Input and output via buffer named after
1642 `python-shell-internal-buffer-name' and what
1643 `python-shell-internal-get-process-name' returns.
1644
1645 This new kind of shell is intended to be used for generic
1646 communication related to defined configurations, the main
1647 difference with global or dedicated shells is that these ones are
1648 attached to a configuration, not a buffer. This means that can
1649 be used for example to retrieve the sys.path and other stuff,
1650 without messing with user shells. Note that
1651 `python-shell-enable-font-lock' and `inferior-python-mode-hook'
1652 are set to nil for these shells, so setup codes are not sent at
1653 startup."
1654 (let ((python-shell-enable-font-lock nil)
1655 (inferior-python-mode-hook nil))
1656 (get-buffer-process
1657 (python-shell-make-comint
1658 (python-shell-parse-command)
1659 (python-shell-internal-get-process-name) nil t))))
1660
1661 (defun python-shell-get-process ()
1662 "Get inferior Python process for current buffer and return it."
1663 (let* ((dedicated-proc-name (python-shell-get-process-name t))
1664 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
1665 (global-proc-name (python-shell-get-process-name nil))
1666 (global-proc-buffer-name (format "*%s*" global-proc-name))
1667 (dedicated-running (comint-check-proc dedicated-proc-buffer-name))
1668 (global-running (comint-check-proc global-proc-buffer-name)))
1669 ;; Always prefer dedicated
1670 (get-buffer-process (or (and dedicated-running dedicated-proc-buffer-name)
1671 (and global-running global-proc-buffer-name)))))
1672
1673 (defun python-shell-get-or-create-process ()
1674 "Get or create an inferior Python process for current buffer and return it."
1675 (let* ((dedicated-proc-name (python-shell-get-process-name t))
1676 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
1677 (global-proc-name (python-shell-get-process-name nil))
1678 (global-proc-buffer-name (format "*%s*" global-proc-name))
1679 (dedicated-running (comint-check-proc dedicated-proc-buffer-name))
1680 (global-running (comint-check-proc global-proc-buffer-name))
1681 (current-prefix-arg 16))
1682 (when (and (not dedicated-running) (not global-running))
1683 (if (call-interactively 'run-python)
1684 (setq dedicated-running t)
1685 (setq global-running t)))
1686 ;; Always prefer dedicated
1687 (get-buffer-process (if dedicated-running
1688 dedicated-proc-buffer-name
1689 global-proc-buffer-name))))
1690
1691 (defvar python-shell-internal-buffer nil
1692 "Current internal shell buffer for the current buffer.
1693 This is really not necessary at all for the code to work but it's
1694 there for compatibility with CEDET.")
1695 (make-variable-buffer-local 'python-shell-internal-buffer)
1696
1697 (defvar python-shell-internal-last-output nil
1698 "Last output captured by the internal shell.
1699 This is really not necessary at all for the code to work but it's
1700 there for compatibility with CEDET.")
1701 (make-variable-buffer-local 'python-shell-internal-last-output)
1702
1703 (defun python-shell-internal-get-or-create-process ()
1704 "Get or create an inferior Internal Python process."
1705 (let* ((proc-name (python-shell-internal-get-process-name))
1706 (proc-buffer-name (format " *%s*" proc-name)))
1707 (when (not (process-live-p proc-name))
1708 (run-python-internal)
1709 (setq python-shell-internal-buffer proc-buffer-name)
1710 ;; XXX: Why is this `sit-for' needed?
1711 ;; `python-shell-make-comint' calls `accept-process-output'
1712 ;; already but it is not helping to get proper output on
1713 ;; 'gnu/linux when the internal shell process is not running and
1714 ;; a call to `python-shell-internal-send-string' is issued.
1715 (sit-for 0.1 t))
1716 (get-buffer-process proc-buffer-name)))
1717
1718 (define-obsolete-function-alias
1719 'python-proc 'python-shell-internal-get-or-create-process "24.2")
1720
1721 (define-obsolete-variable-alias
1722 'python-buffer 'python-shell-internal-buffer "24.2")
1723
1724 (define-obsolete-variable-alias
1725 'python-preoutput-result 'python-shell-internal-last-output "24.2")
1726
1727 (defun python-shell-send-string (string &optional process msg)
1728 "Send STRING to inferior Python PROCESS.
1729 When MSG is non-nil messages the first line of STRING."
1730 (interactive "sPython command: ")
1731 (let ((process (or process (python-shell-get-or-create-process)))
1732 (lines (split-string string "\n" t)))
1733 (when msg
1734 (message (format "Sent: %s..." (nth 0 lines))))
1735 (if (> (length lines) 1)
1736 (let* ((temp-file-name (make-temp-file "py"))
1737 (file-name (or (buffer-file-name) temp-file-name)))
1738 (with-temp-file temp-file-name
1739 (insert string)
1740 (delete-trailing-whitespace))
1741 (python-shell-send-file file-name process temp-file-name))
1742 (comint-send-string process string)
1743 (when (or (not (string-match "\n$" string))
1744 (string-match "\n[ \t].*\n?$" string))
1745 (comint-send-string process "\n")))))
1746
1747 (defun python-shell-send-string-no-output (string &optional process msg)
1748 "Send STRING to PROCESS and inhibit output.
1749 When MSG is non-nil messages the first line of STRING. Return
1750 the output."
1751 (let* ((output-buffer "")
1752 (process (or process (python-shell-get-or-create-process)))
1753 (comint-preoutput-filter-functions
1754 (append comint-preoutput-filter-functions
1755 '(ansi-color-filter-apply
1756 (lambda (string)
1757 (setq output-buffer (concat output-buffer string))
1758 ""))))
1759 (inhibit-quit t))
1760 (or
1761 (with-local-quit
1762 (python-shell-send-string string process msg)
1763 (accept-process-output process)
1764 (replace-regexp-in-string
1765 (if (> (length python-shell-prompt-output-regexp) 0)
1766 (format "\n*%s$\\|^%s\\|\n$"
1767 python-shell-prompt-regexp
1768 (or python-shell-prompt-output-regexp ""))
1769 (format "\n*$\\|^%s\\|\n$"
1770 python-shell-prompt-regexp))
1771 "" output-buffer))
1772 (with-current-buffer (process-buffer process)
1773 (comint-interrupt-subjob)))))
1774
1775 (defun python-shell-internal-send-string (string)
1776 "Send STRING to the Internal Python interpreter.
1777 Returns the output. See `python-shell-send-string-no-output'."
1778 ;; XXX Remove `python-shell-internal-last-output' once CEDET is
1779 ;; updated to support this new mode.
1780 (setq python-shell-internal-last-output
1781 (python-shell-send-string-no-output
1782 ;; Makes this function compatible with the old
1783 ;; python-send-receive. (At least for CEDET).
1784 (replace-regexp-in-string "_emacs_out +" "" string)
1785 (python-shell-internal-get-or-create-process) nil)))
1786
1787 (define-obsolete-function-alias
1788 'python-send-receive 'python-shell-internal-send-string "24.2")
1789
1790 (define-obsolete-function-alias
1791 'python-send-string 'python-shell-internal-send-string "24.2")
1792
1793 (defun python-shell-send-region (start end)
1794 "Send the region delimited by START and END to inferior Python process."
1795 (interactive "r")
1796 (python-shell-send-string (buffer-substring start end) nil t))
1797
1798 (defun python-shell-send-buffer (&optional arg)
1799 "Send the entire buffer to inferior Python process.
1800
1801 With prefix ARG include lines surrounded by \"if __name__ == '__main__':\""
1802 (interactive "P")
1803 (save-restriction
1804 (widen)
1805 (python-shell-send-region
1806 (point-min)
1807 (or (and
1808 (not arg)
1809 (save-excursion
1810 (re-search-forward (python-rx if-name-main) nil t))
1811 (match-beginning 0))
1812 (point-max)))))
1813
1814 (defun python-shell-send-defun (arg)
1815 "Send the current defun to inferior Python process.
1816 When argument ARG is non-nil do not include decorators."
1817 (interactive "P")
1818 (save-excursion
1819 (python-shell-send-region
1820 (progn
1821 (end-of-line 1)
1822 (while (and (or (python-beginning-of-defun-function)
1823 (beginning-of-line 1))
1824 (> (current-indentation) 0)))
1825 (when (not arg)
1826 (while (and (forward-line -1)
1827 (looking-at (python-rx decorator))))
1828 (forward-line 1))
1829 (point-marker))
1830 (progn
1831 (or (python-end-of-defun-function)
1832 (end-of-line 1))
1833 (point-marker)))))
1834
1835 (defun python-shell-send-file (file-name &optional process temp-file-name)
1836 "Send FILE-NAME to inferior Python PROCESS.
1837 If TEMP-FILE-NAME is passed then that file is used for processing
1838 instead, while internally the shell will continue to use
1839 FILE-NAME."
1840 (interactive "fFile to send: ")
1841 (let* ((process (or process (python-shell-get-or-create-process)))
1842 (temp-file-name (when temp-file-name
1843 (expand-file-name temp-file-name)))
1844 (file-name (or (expand-file-name file-name) temp-file-name)))
1845 (when (not file-name)
1846 (error "If FILE-NAME is nil then TEMP-FILE-NAME must be non-nil"))
1847 (python-shell-send-string
1848 (format
1849 (concat "__pyfile = open('''%s''');"
1850 "exec(compile(__pyfile.read(), '''%s''', 'exec'));"
1851 "__pyfile.close()")
1852 (or temp-file-name file-name) file-name)
1853 process)))
1854
1855 (defun python-shell-switch-to-shell ()
1856 "Switch to inferior Python process buffer."
1857 (interactive)
1858 (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t))
1859
1860 (defun python-shell-send-setup-code ()
1861 "Send all setup code for shell.
1862 This function takes the list of setup code to send from the
1863 `python-shell-setup-codes' list."
1864 (let ((msg "Sent %s")
1865 (process (get-buffer-process (current-buffer))))
1866 (dolist (code python-shell-setup-codes)
1867 (when code
1868 (message (format msg code))
1869 (python-shell-send-string
1870 (symbol-value code) process)))))
1871
1872 (add-hook 'inferior-python-mode-hook
1873 #'python-shell-send-setup-code)
1874
1875 \f
1876 ;;; Shell completion
1877
1878 (defcustom python-shell-completion-setup-code
1879 "try:
1880 import readline
1881 except ImportError:
1882 def __COMPLETER_all_completions(text): []
1883 else:
1884 import rlcompleter
1885 readline.set_completer(rlcompleter.Completer().complete)
1886 def __COMPLETER_all_completions(text):
1887 import sys
1888 completions = []
1889 try:
1890 i = 0
1891 while True:
1892 res = readline.get_completer()(text, i)
1893 if not res: break
1894 i += 1
1895 completions.append(res)
1896 except NameError:
1897 pass
1898 return completions"
1899 "Code used to setup completion in inferior Python processes."
1900 :type 'string
1901 :group 'python)
1902
1903 (defcustom python-shell-completion-string-code
1904 "';'.join(__COMPLETER_all_completions('''%s'''))\n"
1905 "Python code used to get a string of completions separated by semicolons."
1906 :type 'string
1907 :group 'python)
1908
1909 (defcustom python-shell-completion-module-string-code ""
1910 "Python code used to get completions separated by semicolons for imports.
1911
1912 For IPython v0.11, add the following line to
1913 `python-shell-completion-setup-code':
1914
1915 from IPython.core.completerlib import module_completion
1916
1917 and use the following as the value of this variable:
1918
1919 ';'.join(module_completion('''%s'''))\n"
1920 :type 'string
1921 :group 'python)
1922
1923 (defcustom python-shell-completion-pdb-string-code
1924 "';'.join(globals().keys() + locals().keys())"
1925 "Python code used to get completions separated by semicolons for [i]pdb."
1926 :type 'string
1927 :group 'python)
1928
1929 (defun python-shell-completion--get-completions (input process completion-code)
1930 "Retrieve available completions for INPUT using PROCESS.
1931 Argument COMPLETION-CODE is the python code used to get
1932 completions on the current context."
1933 (with-current-buffer (process-buffer process)
1934 (let ((completions (python-shell-send-string-no-output
1935 (format completion-code input) process)))
1936 (when (> (length completions) 2)
1937 (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t)))))
1938
1939 (defun python-shell-completion--do-completion-at-point (process)
1940 "Do completion at point for PROCESS."
1941 (with-syntax-table python-dotty-syntax-table
1942 (let* ((beg
1943 (save-excursion
1944 (let* ((paren-depth (car (syntax-ppss)))
1945 (syntax-string "w_")
1946 (syntax-list (string-to-syntax syntax-string)))
1947 ;; Stop scanning for the beginning of the completion subject
1948 ;; after the char before point matches a delimiter
1949 (while (member (car (syntax-after (1- (point)))) syntax-list)
1950 (skip-syntax-backward syntax-string)
1951 (when (or (equal (char-before) ?\))
1952 (equal (char-before) ?\"))
1953 (forward-char -1))
1954 (while (or
1955 ;; honor initial paren depth
1956 (> (car (syntax-ppss)) paren-depth)
1957 (python-syntax-context 'string))
1958 (forward-char -1))))
1959 (point)))
1960 (end (point))
1961 (line (buffer-substring-no-properties (point-at-bol) end))
1962 (input (buffer-substring-no-properties beg end))
1963 ;; Get the last prompt for the inferior process buffer. This is
1964 ;; used for the completion code selection heuristic.
1965 (prompt
1966 (with-current-buffer (process-buffer process)
1967 (buffer-substring-no-properties
1968 (overlay-start comint-last-prompt-overlay)
1969 (overlay-end comint-last-prompt-overlay))))
1970 (completion-context
1971 ;; Check whether a prompt matches a pdb string, an import statement
1972 ;; or just the standard prompt and use the correct
1973 ;; python-shell-completion-*-code string
1974 (cond ((and (> (length python-shell-completion-pdb-string-code) 0)
1975 (string-match
1976 (concat "^" python-shell-prompt-pdb-regexp) prompt))
1977 'pdb)
1978 ((and (>
1979 (length python-shell-completion-module-string-code) 0)
1980 (string-match
1981 (concat "^" python-shell-prompt-regexp) prompt)
1982 (string-match "^[ \t]*\\(from\\|import\\)[ \t]" line))
1983 'import)
1984 ((string-match
1985 (concat "^" python-shell-prompt-regexp) prompt)
1986 'default)
1987 (t nil)))
1988 (completion-code
1989 (case completion-context
1990 ('pdb python-shell-completion-pdb-string-code)
1991 ('import python-shell-completion-module-string-code)
1992 ('default python-shell-completion-string-code)
1993 (t nil)))
1994 (input
1995 (if (eq completion-context 'import)
1996 (replace-regexp-in-string "^[ \t]+" "" line)
1997 input))
1998 (completions
1999 (and completion-code (> (length input) 0)
2000 (python-shell-completion--get-completions
2001 input process completion-code))))
2002 (list beg end completions))))
2003
2004 (defun python-shell-completion-complete-at-point ()
2005 "Perform completion at point in inferior Python process."
2006 (and comint-last-prompt-overlay
2007 (> (point-marker) (overlay-end comint-last-prompt-overlay))
2008 (python-shell-completion--do-completion-at-point
2009 (get-buffer-process (current-buffer)))))
2010
2011 (defun python-shell-completion-complete-or-indent ()
2012 "Complete or indent depending on the context.
2013 If content before pointer is all whitespace indent. If not try
2014 to complete."
2015 (interactive)
2016 (if (string-match "^[[:space:]]*$"
2017 (buffer-substring (comint-line-beginning-position)
2018 (point-marker)))
2019 (indent-for-tab-command)
2020 (completion-at-point)))
2021
2022 \f
2023 ;;; PDB Track integration
2024
2025 (defcustom python-pdbtrack-activate t
2026 "Non-nil makes python shell enable pdbtracking."
2027 :type 'boolean
2028 :group 'python
2029 :safe 'booleanp)
2030
2031 (defcustom python-pdbtrack-stacktrace-info-regexp
2032 "^> \\([^\"(<]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()"
2033 "Regular Expression matching stacktrace information.
2034 Used to extract the current line and module being inspected."
2035 :type 'string
2036 :group 'python
2037 :safe 'stringp)
2038
2039 (defvar python-pdbtrack-tracked-buffer nil
2040 "Variable containing the value of the current tracked buffer.
2041 Never set this variable directly, use
2042 `python-pdbtrack-set-tracked-buffer' instead.")
2043 (make-variable-buffer-local 'python-pdbtrack-tracked-buffer)
2044
2045 (defvar python-pdbtrack-buffers-to-kill nil
2046 "List of buffers to be deleted after tracking finishes.")
2047 (make-variable-buffer-local 'python-pdbtrack-buffers-to-kill)
2048
2049 (defun python-pdbtrack-set-tracked-buffer (file-name)
2050 "Set the buffer for FILE-NAME as the tracked buffer.
2051 Internally it uses the `python-pdbtrack-tracked-buffer' variable.
2052 Returns the tracked buffer."
2053 (let ((file-buffer (get-file-buffer file-name)))
2054 (if file-buffer
2055 (setq python-pdbtrack-tracked-buffer file-buffer)
2056 (setq file-buffer (find-file-noselect file-name))
2057 (when (not (member file-buffer python-pdbtrack-buffers-to-kill))
2058 (add-to-list 'python-pdbtrack-buffers-to-kill file-buffer)))
2059 file-buffer))
2060
2061 (defun python-pdbtrack-comint-output-filter-function (output)
2062 "Move overlay arrow to current pdb line in tracked buffer.
2063 Argument OUTPUT is a string with the output from the comint process."
2064 (when (and python-pdbtrack-activate (not (string= output "")))
2065 (let* ((full-output (ansi-color-filter-apply
2066 (buffer-substring comint-last-input-end (point-max))))
2067 (line-number)
2068 (file-name
2069 (with-temp-buffer
2070 (insert full-output)
2071 (goto-char (point-min))
2072 ;; OK, this sucked but now it became a cool hack. The
2073 ;; stacktrace information normally is on the first line
2074 ;; but in some cases (like when doing a step-in) it is
2075 ;; on the second.
2076 (when (or (looking-at python-pdbtrack-stacktrace-info-regexp)
2077 (and
2078 (forward-line)
2079 (looking-at python-pdbtrack-stacktrace-info-regexp)))
2080 (setq line-number (string-to-number
2081 (match-string-no-properties 2)))
2082 (match-string-no-properties 1)))))
2083 (if (and file-name line-number)
2084 (let* ((tracked-buffer
2085 (python-pdbtrack-set-tracked-buffer file-name))
2086 (shell-buffer (current-buffer))
2087 (tracked-buffer-window (get-buffer-window tracked-buffer))
2088 (tracked-buffer-line-pos))
2089 (with-current-buffer tracked-buffer
2090 (set (make-local-variable 'overlay-arrow-string) "=>")
2091 (set (make-local-variable 'overlay-arrow-position) (make-marker))
2092 (setq tracked-buffer-line-pos (progn
2093 (goto-char (point-min))
2094 (forward-line (1- line-number))
2095 (point-marker)))
2096 (when tracked-buffer-window
2097 (set-window-point
2098 tracked-buffer-window tracked-buffer-line-pos))
2099 (set-marker overlay-arrow-position tracked-buffer-line-pos))
2100 (pop-to-buffer tracked-buffer)
2101 (switch-to-buffer-other-window shell-buffer))
2102 (when python-pdbtrack-tracked-buffer
2103 (with-current-buffer python-pdbtrack-tracked-buffer
2104 (set-marker overlay-arrow-position nil))
2105 (mapc #'(lambda (buffer)
2106 (ignore-errors (kill-buffer buffer)))
2107 python-pdbtrack-buffers-to-kill)
2108 (setq python-pdbtrack-tracked-buffer nil
2109 python-pdbtrack-buffers-to-kill nil)))))
2110 output)
2111
2112 \f
2113 ;;; Symbol completion
2114
2115 (defun python-completion-complete-at-point ()
2116 "Complete current symbol at point.
2117 For this to work the best as possible you should call
2118 `python-shell-send-buffer' from time to time so context in
2119 inferior python process is updated properly."
2120 (let ((process (python-shell-get-process)))
2121 (if (not process)
2122 (error "Completion needs an inferior Python process running")
2123 (python-shell-completion--do-completion-at-point process))))
2124
2125 (add-to-list 'debug-ignored-errors
2126 "^Completion needs an inferior Python process running.")
2127
2128 \f
2129 ;;; Fill paragraph
2130
2131 (defcustom python-fill-comment-function 'python-fill-comment
2132 "Function to fill comments.
2133 This is the function used by `python-fill-paragraph-function' to
2134 fill comments."
2135 :type 'symbol
2136 :group 'python
2137 :safe 'symbolp)
2138
2139 (defcustom python-fill-string-function 'python-fill-string
2140 "Function to fill strings.
2141 This is the function used by `python-fill-paragraph-function' to
2142 fill strings."
2143 :type 'symbol
2144 :group 'python
2145 :safe 'symbolp)
2146
2147 (defcustom python-fill-decorator-function 'python-fill-decorator
2148 "Function to fill decorators.
2149 This is the function used by `python-fill-paragraph-function' to
2150 fill decorators."
2151 :type 'symbol
2152 :group 'python
2153 :safe 'symbolp)
2154
2155 (defcustom python-fill-paren-function 'python-fill-paren
2156 "Function to fill parens.
2157 This is the function used by `python-fill-paragraph-function' to
2158 fill parens."
2159 :type 'symbol
2160 :group 'python
2161 :safe 'symbolp)
2162
2163 (defun python-fill-paragraph-function (&optional justify)
2164 "`fill-paragraph-function' handling multi-line strings and possibly comments.
2165 If any of the current line is in or at the end of a multi-line string,
2166 fill the string or the paragraph of it that point is in, preserving
2167 the string's indentation.
2168 Optional argument JUSTIFY defines if the paragraph should be justified."
2169 (interactive "P")
2170 (save-excursion
2171 (back-to-indentation)
2172 (cond
2173 ;; Comments
2174 ((funcall python-fill-comment-function justify))
2175 ;; Strings/Docstrings
2176 ((save-excursion (skip-chars-forward "\"'uUrR")
2177 (python-syntax-context 'string))
2178 (funcall python-fill-string-function justify))
2179 ;; Decorators
2180 ((equal (char-after (save-excursion
2181 (back-to-indentation)
2182 (point-marker))) ?@)
2183 (funcall python-fill-decorator-function justify))
2184 ;; Parens
2185 ((or (python-syntax-context 'paren)
2186 (looking-at (python-rx open-paren))
2187 (save-excursion
2188 (skip-syntax-forward "^(" (line-end-position))
2189 (looking-at (python-rx open-paren))))
2190 (funcall python-fill-paren-function justify))
2191 (t t))))
2192
2193 (defun python-fill-comment (&optional justify)
2194 "Comment fill function for `python-fill-paragraph-function'.
2195 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
2196 (fill-comment-paragraph justify))
2197
2198 (defun python-fill-string (&optional justify)
2199 "String fill function for `python-fill-paragraph-function'.
2200 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
2201 (let ((marker (point-marker))
2202 (string-start-marker
2203 (progn
2204 (skip-chars-forward "\"'uUrR")
2205 (goto-char (python-syntax-context 'string))
2206 (skip-chars-forward "\"'uUrR")
2207 (point-marker)))
2208 (reg-start (line-beginning-position))
2209 (string-end-marker
2210 (progn
2211 (while (python-syntax-context 'string)
2212 (goto-char (1+ (point-marker))))
2213 (skip-chars-backward "\"'")
2214 (point-marker)))
2215 (reg-end (line-end-position))
2216 (fill-paragraph-function))
2217 (save-restriction
2218 (narrow-to-region reg-start reg-end)
2219 (save-excursion
2220 (goto-char string-start-marker)
2221 (delete-region (point-marker) (progn
2222 (skip-syntax-forward "> ")
2223 (point-marker)))
2224 (goto-char string-end-marker)
2225 (delete-region (point-marker) (progn
2226 (skip-syntax-backward "> ")
2227 (point-marker)))
2228 (save-excursion
2229 (goto-char marker)
2230 (fill-paragraph justify))
2231 ;; If there is a newline in the docstring lets put triple
2232 ;; quote in it's own line to follow pep 8
2233 (when (save-excursion
2234 (re-search-backward "\n" string-start-marker t))
2235 (newline)
2236 (newline-and-indent))
2237 (fill-paragraph justify)))) t)
2238
2239 (defun python-fill-decorator (&optional justify)
2240 "Decorator fill function for `python-fill-paragraph-function'.
2241 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
2242 t)
2243
2244 (defun python-fill-paren (&optional justify)
2245 "Paren fill function for `python-fill-paragraph-function'.
2246 JUSTIFY should be used (if applicable) as in `fill-paragraph'."
2247 (save-restriction
2248 (narrow-to-region (progn
2249 (while (python-syntax-context 'paren)
2250 (goto-char (1- (point-marker))))
2251 (point-marker)
2252 (line-beginning-position))
2253 (progn
2254 (when (not (python-syntax-context 'paren))
2255 (end-of-line)
2256 (when (not (python-syntax-context 'paren))
2257 (skip-syntax-backward "^)")))
2258 (while (python-syntax-context 'paren)
2259 (goto-char (1+ (point-marker))))
2260 (point-marker)))
2261 (let ((paragraph-start "\f\\|[ \t]*$")
2262 (paragraph-separate ",")
2263 (fill-paragraph-function))
2264 (goto-char (point-min))
2265 (fill-paragraph justify))
2266 (while (not (eobp))
2267 (forward-line 1)
2268 (python-indent-line)
2269 (goto-char (line-end-position)))) t)
2270
2271 \f
2272 ;;; Skeletons
2273
2274 (defcustom python-skeleton-autoinsert nil
2275 "Non-nil means template skeletons will be automagically inserted.
2276 This happens when pressing \"if<SPACE>\", for example, to prompt for
2277 the if condition."
2278 :type 'boolean
2279 :group 'python
2280 :safe 'booleanp)
2281
2282 (define-obsolete-variable-alias
2283 'python-use-skeletons 'python-skeleton-autoinsert "24.2")
2284
2285 (defvar python-skeleton-available '()
2286 "Internal list of available skeletons.")
2287
2288 (define-abbrev-table 'python-mode-abbrev-table ()
2289 "Abbrev table for Python mode."
2290 :case-fixed t
2291 ;; Allow / inside abbrevs.
2292 :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
2293 ;; Only expand in code.
2294 :enable-function (lambda ()
2295 (and
2296 (not (python-syntax-comment-or-string-p))
2297 python-skeleton-autoinsert)))
2298
2299 (defmacro python-skeleton-define (name doc &rest skel)
2300 "Define a `python-mode' skeleton using NAME DOC and SKEL.
2301 The skeleton will be bound to python-skeleton-NAME and will
2302 be added to `python-mode-abbrev-table'."
2303 (declare (indent 2))
2304 (let* ((name (symbol-name name))
2305 (function-name (intern (concat "python-skeleton-" name))))
2306 `(progn
2307 (define-abbrev python-mode-abbrev-table ,name "" ',function-name
2308 :system t)
2309 (setq python-skeleton-available
2310 (cons ',function-name python-skeleton-available))
2311 (define-skeleton ,function-name
2312 ,(or doc
2313 (format "Insert %s statement." name))
2314 ,@skel))))
2315
2316 (defmacro python-define-auxiliary-skeleton (name doc &optional &rest skel)
2317 "Define a `python-mode' auxiliary skeleton using NAME DOC and SKEL.
2318 The skeleton will be bound to python-skeleton-NAME."
2319 (declare (indent 2))
2320 (let* ((name (symbol-name name))
2321 (function-name (intern (concat "python-skeleton--" name)))
2322 (msg (format
2323 "Add '%s' clause? " name)))
2324 (when (not skel)
2325 (setq skel
2326 `(< ,(format "%s:" name) \n \n
2327 > _ \n)))
2328 `(define-skeleton ,function-name
2329 ,(or doc
2330 (format "Auxiliary skeleton for %s statement." name))
2331 nil
2332 (unless (y-or-n-p ,msg)
2333 (signal 'quit t))
2334 ,@skel)))
2335
2336 (python-define-auxiliary-skeleton else nil)
2337
2338 (python-define-auxiliary-skeleton except nil)
2339
2340 (python-define-auxiliary-skeleton finally nil)
2341
2342 (python-skeleton-define if nil
2343 "Condition: "
2344 "if " str ":" \n
2345 _ \n
2346 ("other condition, %s: "
2347 <
2348 "elif " str ":" \n
2349 > _ \n nil)
2350 '(python-skeleton--else) | ^)
2351
2352 (python-skeleton-define while nil
2353 "Condition: "
2354 "while " str ":" \n
2355 > _ \n
2356 '(python-skeleton--else) | ^)
2357
2358 (python-skeleton-define for nil
2359 "Iteration spec: "
2360 "for " str ":" \n
2361 > _ \n
2362 '(python-skeleton--else) | ^)
2363
2364 (python-skeleton-define try nil
2365 nil
2366 "try:" \n
2367 > _ \n
2368 ("Exception, %s: "
2369 <
2370 "except " str ":" \n
2371 > _ \n nil)
2372 resume:
2373 '(python-skeleton--except)
2374 '(python-skeleton--else)
2375 '(python-skeleton--finally) | ^)
2376
2377 (python-skeleton-define def nil
2378 "Function name: "
2379 "def " str " (" ("Parameter, %s: "
2380 (unless (equal ?\( (char-before)) ", ")
2381 str) "):" \n
2382 "\"\"\"" - "\"\"\"" \n
2383 > _ \n)
2384
2385 (python-skeleton-define class nil
2386 "Class name: "
2387 "class " str " (" ("Inheritance, %s: "
2388 (unless (equal ?\( (char-before)) ", ")
2389 str)
2390 & ")" | -2
2391 ":" \n
2392 "\"\"\"" - "\"\"\"" \n
2393 > _ \n)
2394
2395 (defun python-skeleton-add-menu-items ()
2396 "Add menu items to Python->Skeletons menu."
2397 (let ((skeletons (sort python-skeleton-available 'string<))
2398 (items))
2399 (dolist (skeleton skeletons)
2400 (easy-menu-add-item
2401 nil '("Python" "Skeletons")
2402 `[,(format
2403 "Insert %s" (caddr (split-string (symbol-name skeleton) "-")))
2404 ,skeleton t]))))
2405 \f
2406 ;;; FFAP
2407
2408 (defcustom python-ffap-setup-code
2409 "def __FFAP_get_module_path(module):
2410 try:
2411 import os
2412 path = __import__(module).__file__
2413 if path[-4:] == '.pyc' and os.path.exists(path[0:-1]):
2414 path = path[:-1]
2415 return path
2416 except:
2417 return ''"
2418 "Python code to get a module path."
2419 :type 'string
2420 :group 'python)
2421
2422 (defcustom python-ffap-string-code
2423 "__FFAP_get_module_path('''%s''')\n"
2424 "Python code used to get a string with the path of a module."
2425 :type 'string
2426 :group 'python)
2427
2428 (defun python-ffap-module-path (module)
2429 "Function for `ffap-alist' to return path for MODULE."
2430 (let ((process (or
2431 (and (eq major-mode 'inferior-python-mode)
2432 (get-buffer-process (current-buffer)))
2433 (python-shell-get-process))))
2434 (if (not process)
2435 nil
2436 (let ((module-file
2437 (python-shell-send-string-no-output
2438 (format python-ffap-string-code module) process)))
2439 (when module-file
2440 (substring-no-properties module-file 1 -1))))))
2441
2442 (eval-after-load "ffap"
2443 '(progn
2444 (push '(python-mode . python-ffap-module-path) ffap-alist)
2445 (push '(inferior-python-mode . python-ffap-module-path) ffap-alist)))
2446
2447 \f
2448 ;;; Code check
2449
2450 (defcustom python-check-command
2451 "pyflakes"
2452 "Command used to check a Python file."
2453 :type 'string
2454 :group 'python)
2455
2456 (defcustom python-check-buffer-name
2457 "*Python check: %s*"
2458 "Buffer name used for check commands."
2459 :type 'string
2460 :group 'python)
2461
2462 (defvar python-check-custom-command nil
2463 "Internal use.")
2464
2465 (defun python-check (command)
2466 "Check a Python file (default current buffer's file).
2467 Runs COMMAND, a shell command, as if by `compile'. See
2468 `python-check-command' for the default."
2469 (interactive
2470 (list (read-string "Check command: "
2471 (or python-check-custom-command
2472 (concat python-check-command " "
2473 (shell-quote-argument
2474 (or
2475 (let ((name (buffer-file-name)))
2476 (and name
2477 (file-name-nondirectory name)))
2478 "")))))))
2479 (setq python-check-custom-command command)
2480 (save-some-buffers (not compilation-ask-about-save) nil)
2481 (let ((process-environment (python-shell-calculate-process-environment))
2482 (exec-path (python-shell-calculate-exec-path)))
2483 (compilation-start command nil
2484 (lambda (mode-name)
2485 (format python-check-buffer-name command)))))
2486
2487 \f
2488 ;;; Eldoc
2489
2490 (defcustom python-eldoc-setup-code
2491 "def __PYDOC_get_help(obj):
2492 try:
2493 import inspect
2494 if hasattr(obj, 'startswith'):
2495 obj = eval(obj, globals())
2496 doc = inspect.getdoc(obj)
2497 if not doc and callable(obj):
2498 target = None
2499 if inspect.isclass(obj) and hasattr(obj, '__init__'):
2500 target = obj.__init__
2501 objtype = 'class'
2502 else:
2503 target = obj
2504 objtype = 'def'
2505 if target:
2506 args = inspect.formatargspec(
2507 *inspect.getargspec(target)
2508 )
2509 name = obj.__name__
2510 doc = '{objtype} {name}{args}'.format(
2511 objtype=objtype, name=name, args=args
2512 )
2513 else:
2514 doc = doc.splitlines()[0]
2515 except:
2516 doc = ''
2517 try:
2518 exec('print doc')
2519 except SyntaxError:
2520 print(doc)"
2521 "Python code to setup documentation retrieval."
2522 :type 'string
2523 :group 'python)
2524
2525 (defcustom python-eldoc-string-code
2526 "__PYDOC_get_help('''%s''')\n"
2527 "Python code used to get a string with the documentation of an object."
2528 :type 'string
2529 :group 'python)
2530
2531 (defun python-eldoc--get-doc-at-point (&optional force-input force-process)
2532 "Internal implementation to get documentation at point.
2533 If not FORCE-INPUT is passed then what
2534 `python-info-current-symbol' returns will be used. If not
2535 FORCE-PROCESS is passed what `python-shell-get-process' returns
2536 is used."
2537 (let ((process (or force-process (python-shell-get-process))))
2538 (if (not process)
2539 (error "Eldoc needs an inferior Python process running")
2540 (let ((input (or force-input
2541 (python-info-current-symbol t))))
2542 (and input
2543 (python-shell-send-string-no-output
2544 (format python-eldoc-string-code input)
2545 process))))))
2546
2547 (defun python-eldoc-function ()
2548 "`eldoc-documentation-function' for Python.
2549 For this to work the best as possible you should call
2550 `python-shell-send-buffer' from time to time so context in
2551 inferior python process is updated properly."
2552 (python-eldoc--get-doc-at-point))
2553
2554 (defun python-eldoc-at-point (symbol)
2555 "Get help on SYMBOL using `help'.
2556 Interactively, prompt for symbol."
2557 (interactive
2558 (let ((symbol (python-info-current-symbol t))
2559 (enable-recursive-minibuffers t))
2560 (list (read-string (if symbol
2561 (format "Describe symbol (default %s): " symbol)
2562 "Describe symbol: ")
2563 nil nil symbol))))
2564 (message (python-eldoc--get-doc-at-point symbol)))
2565
2566 (add-to-list 'debug-ignored-errors
2567 "^Eldoc needs an inferior Python process running.")
2568
2569 \f
2570 ;;; Misc helpers
2571
2572 (defun python-info-current-defun (&optional include-type)
2573 "Return name of surrounding function with Python compatible dotty syntax.
2574 Optional argument INCLUDE-TYPE indicates to include the type of the defun.
2575 This function is compatible to be used as
2576 `add-log-current-defun-function' since it returns nil if point is
2577 not inside a defun."
2578 (let ((names '())
2579 (starting-indentation)
2580 (starting-point)
2581 (first-run t))
2582 (save-restriction
2583 (widen)
2584 (save-excursion
2585 (setq starting-point (point-marker))
2586 (setq starting-indentation (save-excursion
2587 (python-nav-beginning-of-statement)
2588 (current-indentation)))
2589 (end-of-line 1)
2590 (while (python-beginning-of-defun-function 1)
2591 (when (or (< (current-indentation) starting-indentation)
2592 (and first-run
2593 (<
2594 starting-point
2595 (save-excursion
2596 (python-end-of-defun-function)
2597 (point-marker)))))
2598 (setq first-run nil)
2599 (setq starting-indentation (current-indentation))
2600 (looking-at python-nav-beginning-of-defun-regexp)
2601 (setq names (cons
2602 (if (not include-type)
2603 (match-string-no-properties 1)
2604 (mapconcat 'identity
2605 (split-string
2606 (match-string-no-properties 0)) " "))
2607 names))))))
2608 (when names
2609 (mapconcat (lambda (string) string) names "."))))
2610
2611 (defun python-info-current-symbol (&optional replace-self)
2612 "Return current symbol using dotty syntax.
2613 With optional argument REPLACE-SELF convert \"self\" to current
2614 parent defun name."
2615 (let ((name
2616 (and (not (python-syntax-comment-or-string-p))
2617 (with-syntax-table python-dotty-syntax-table
2618 (let ((sym (symbol-at-point)))
2619 (and sym
2620 (substring-no-properties (symbol-name sym))))))))
2621 (when name
2622 (if (not replace-self)
2623 name
2624 (let ((current-defun (python-info-current-defun)))
2625 (if (not current-defun)
2626 name
2627 (replace-regexp-in-string
2628 (python-rx line-start word-start "self" word-end ?.)
2629 (concat
2630 (mapconcat 'identity
2631 (butlast (split-string current-defun "\\."))
2632 ".") ".")
2633 name)))))))
2634
2635 (defsubst python-info-beginning-of-block-statement-p ()
2636 "Return non-nil if current statement opens a block."
2637 (save-excursion
2638 (python-nav-beginning-of-statement)
2639 (looking-at (python-rx block-start))))
2640
2641 (defun python-info-closing-block ()
2642 "Return the point of the block the current line closes."
2643 (let ((closing-word (save-excursion
2644 (back-to-indentation)
2645 (current-word)))
2646 (indentation (current-indentation)))
2647 (when (member closing-word python-indent-dedenters)
2648 (save-excursion
2649 (forward-line -1)
2650 (while (and (> (current-indentation) indentation)
2651 (not (bobp))
2652 (not (back-to-indentation))
2653 (forward-line -1)))
2654 (back-to-indentation)
2655 (cond
2656 ((not (equal indentation (current-indentation))) nil)
2657 ((string= closing-word "elif")
2658 (when (member (current-word) '("if" "elif"))
2659 (point-marker)))
2660 ((string= closing-word "else")
2661 (when (member (current-word) '("if" "elif" "except" "for" "while"))
2662 (point-marker)))
2663 ((string= closing-word "except")
2664 (when (member (current-word) '("try"))
2665 (point-marker)))
2666 ((string= closing-word "finally")
2667 (when (member (current-word) '("except" "else"))
2668 (point-marker))))))))
2669
2670 (defun python-info-closing-block-message (&optional closing-block-point)
2671 "Message the contents of the block the current line closes.
2672 With optional argument CLOSING-BLOCK-POINT use that instead of
2673 recalculating it calling `python-info-closing-block'."
2674 (let ((point (or closing-block-point (python-info-closing-block))))
2675 (when point
2676 (save-restriction
2677 (widen)
2678 (message "Closes %s" (save-excursion
2679 (goto-char point)
2680 (back-to-indentation)
2681 (buffer-substring
2682 (point) (line-end-position))))))))
2683
2684 (defun python-info-line-ends-backslash-p (&optional line-number)
2685 "Return non-nil if current line ends with backslash.
2686 With optional argument LINE-NUMBER, check that line instead."
2687 (save-excursion
2688 (save-restriction
2689 (widen)
2690 (when line-number
2691 (goto-char line-number))
2692 (while (and (not (eobp))
2693 (goto-char (line-end-position))
2694 (python-syntax-context 'paren)
2695 (not (equal (char-before (point)) ?\\)))
2696 (forward-line 1))
2697 (when (equal (char-before) ?\\)
2698 (point-marker)))))
2699
2700 (defun python-info-beginning-of-backslash (&optional line-number)
2701 "Return the point where the backslashed line start.
2702 Optional argument LINE-NUMBER forces the line number to check against."
2703 (save-excursion
2704 (save-restriction
2705 (widen)
2706 (when line-number
2707 (goto-char line-number))
2708 (when (python-info-line-ends-backslash-p)
2709 (while (save-excursion
2710 (goto-char (line-beginning-position))
2711 (python-syntax-context 'paren))
2712 (forward-line -1))
2713 (back-to-indentation)
2714 (point-marker)))))
2715
2716 (defun python-info-continuation-line-p ()
2717 "Check if current line is continuation of another.
2718 When current line is continuation of another return the point
2719 where the continued line ends."
2720 (save-excursion
2721 (save-restriction
2722 (widen)
2723 (let* ((context-type (progn
2724 (back-to-indentation)
2725 (python-syntax-context-type)))
2726 (line-start (line-number-at-pos))
2727 (context-start (when context-type
2728 (python-syntax-context context-type))))
2729 (cond ((equal context-type 'paren)
2730 ;; Lines inside a paren are always a continuation line
2731 ;; (except the first one).
2732 (python-util-forward-comment -1)
2733 (point-marker))
2734 ((member context-type '(string comment))
2735 ;; move forward an roll again
2736 (goto-char context-start)
2737 (python-util-forward-comment)
2738 (python-info-continuation-line-p))
2739 (t
2740 ;; Not within a paren, string or comment, the only way
2741 ;; we are dealing with a continuation line is that
2742 ;; previous line contains a backslash, and this can
2743 ;; only be the previous line from current
2744 (back-to-indentation)
2745 (python-util-forward-comment -1)
2746 (when (and (equal (1- line-start) (line-number-at-pos))
2747 (python-info-line-ends-backslash-p))
2748 (point-marker))))))))
2749
2750 (defun python-info-block-continuation-line-p ()
2751 "Return non-nil if current line is a continuation of a block."
2752 (save-excursion
2753 (when (python-info-continuation-line-p)
2754 (forward-line -1)
2755 (back-to-indentation)
2756 (when (looking-at (python-rx block-start))
2757 (point-marker)))))
2758
2759 (defun python-info-assignment-continuation-line-p ()
2760 "Check if current line is a continuation of an assignment.
2761 When current line is continuation of another with an assignment
2762 return the point of the first non-blank character after the
2763 operator."
2764 (save-excursion
2765 (when (python-info-continuation-line-p)
2766 (forward-line -1)
2767 (back-to-indentation)
2768 (when (and (not (looking-at (python-rx block-start)))
2769 (and (re-search-forward (python-rx not-simple-operator
2770 assignment-operator
2771 not-simple-operator)
2772 (line-end-position) t)
2773 (not (python-syntax-context-type))))
2774 (skip-syntax-forward "\s")
2775 (point-marker)))))
2776
2777 (defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss)
2778 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS."
2779 (and (not (python-syntax-context-type (or syntax-ppss (syntax-ppss))))
2780 (save-excursion
2781 (beginning-of-line 1)
2782 (looking-at python-nav-beginning-of-defun-regexp))))
2783
2784 (defun python-info-current-line-comment-p ()
2785 "Check if current line is a comment line."
2786 (char-equal (or (char-after (+ (point) (current-indentation))) ?_) ?#))
2787
2788 (defun python-info-current-line-empty-p ()
2789 "Check if current line is empty, ignoring whitespace."
2790 (save-excursion
2791 (beginning-of-line 1)
2792 (looking-at
2793 (python-rx line-start (* whitespace)
2794 (group (* not-newline))
2795 (* whitespace) line-end))
2796 (string-equal "" (match-string-no-properties 1))))
2797
2798 \f
2799 ;;; Utility functions
2800
2801 (defun python-util-position (item seq)
2802 "Find the first occurrence of ITEM in SEQ.
2803 Return the index of the matching item, or nil if not found."
2804 (let ((member-result (member item seq)))
2805 (when member-result
2806 (- (length seq) (length member-result)))))
2807
2808 ;; Stolen from org-mode
2809 (defun python-util-clone-local-variables (from-buffer &optional regexp)
2810 "Clone local variables from FROM-BUFFER.
2811 Optional argument REGEXP selects variables to clone and defaults
2812 to \"^python-\"."
2813 (mapc
2814 (lambda (pair)
2815 (and (symbolp (car pair))
2816 (string-match (or regexp "^python-")
2817 (symbol-name (car pair)))
2818 (set (make-local-variable (car pair))
2819 (cdr pair))))
2820 (buffer-local-variables from-buffer)))
2821
2822 (defun python-util-forward-comment (&optional direction)
2823 "Python mode specific version of `forward-comment'.
2824 Optional argument DIRECTION defines the direction to move to."
2825 (let ((comment-start (python-syntax-context 'comment))
2826 (factor (if (< (or direction 0) 0)
2827 -99999
2828 99999)))
2829 (when comment-start
2830 (goto-char comment-start))
2831 (forward-comment factor)))
2832
2833 \f
2834 ;;;###autoload
2835 (define-derived-mode python-mode prog-mode "Python"
2836 "Major mode for editing Python files.
2837
2838 \\{python-mode-map}
2839 Entry to this mode calls the value of `python-mode-hook'
2840 if that value is non-nil."
2841 (set (make-local-variable 'tab-width) 8)
2842 (set (make-local-variable 'indent-tabs-mode) nil)
2843
2844 (set (make-local-variable 'comment-start) "# ")
2845 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
2846
2847 (set (make-local-variable 'parse-sexp-lookup-properties) t)
2848 (set (make-local-variable 'parse-sexp-ignore-comments) t)
2849
2850 (set (make-local-variable 'forward-sexp-function)
2851 'python-nav-forward-sexp-function)
2852
2853 (set (make-local-variable 'font-lock-defaults)
2854 '(python-font-lock-keywords nil nil nil nil))
2855
2856 (set (make-local-variable 'syntax-propertize-function)
2857 python-syntax-propertize-function)
2858
2859 (set (make-local-variable 'indent-line-function)
2860 #'python-indent-line-function)
2861 (set (make-local-variable 'indent-region-function) #'python-indent-region)
2862
2863 (set (make-local-variable 'paragraph-start) "\\s-*$")
2864 (set (make-local-variable 'fill-paragraph-function)
2865 'python-fill-paragraph-function)
2866
2867 (set (make-local-variable 'beginning-of-defun-function)
2868 #'python-beginning-of-defun-function)
2869 (set (make-local-variable 'end-of-defun-function)
2870 #'python-end-of-defun-function)
2871
2872 (add-hook 'completion-at-point-functions
2873 'python-completion-complete-at-point nil 'local)
2874
2875 (add-hook 'post-self-insert-hook
2876 'python-indent-post-self-insert-function nil 'local)
2877
2878 (set (make-local-variable 'imenu-extract-index-name-function)
2879 #'python-info-current-defun)
2880
2881 (set (make-local-variable 'add-log-current-defun-function)
2882 #'python-info-current-defun)
2883
2884 (add-hook 'which-func-functions #'python-info-current-defun nil t)
2885
2886 (set (make-local-variable 'skeleton-further-elements)
2887 '((abbrev-mode nil)
2888 (< '(backward-delete-char-untabify (min python-indent-offset
2889 (current-column))))
2890 (^ '(- (1+ (current-indentation))))))
2891
2892 (set (make-local-variable 'eldoc-documentation-function)
2893 #'python-eldoc-function)
2894
2895 (add-to-list 'hs-special-modes-alist
2896 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
2897 ,(lambda (arg)
2898 (python-end-of-defun-function)) nil))
2899
2900 (set (make-local-variable 'mode-require-final-newline) t)
2901
2902 (set (make-local-variable 'outline-regexp)
2903 (python-rx (* space) block-start))
2904 (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n")
2905 (set (make-local-variable 'outline-level)
2906 #'(lambda ()
2907 "`outline-level' function for Python mode."
2908 (1+ (/ (current-indentation) python-indent-offset))))
2909
2910 (python-skeleton-add-menu-items)
2911
2912 (when python-indent-guess-indent-offset
2913 (python-indent-guess-indent-offset)))
2914
2915
2916 (provide 'python)
2917
2918 ;; Local Variables:
2919 ;; coding: utf-8
2920 ;; indent-tabs-mode: nil
2921 ;; End:
2922
2923 ;;; python.el ends here