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