Commit | Line | Data |
---|---|---|
45c138ac FEG |
1 | ;;; python.el -- Python's flying circus support for Emacs |
2 | ||
e2d8d479 | 3 | ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc. |
45c138ac FEG |
4 | |
5 | ;; Author: Fabián E. Gallina <fabian@anue.biz> | |
6 | ;; Maintainer: FSF | |
7 | ;; Created: Jul 2010 | |
8 | ;; Keywords: languages | |
9 | ||
10 | ;; This file is NOT part of GNU Emacs. | |
11 | ||
12 | ;; python.el is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; python.el is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with python.el. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Major mode for editing Python files with some fontification and | |
28 | ;; indentation bits extracted from original Dave Love's python.el | |
29 | ;; found in GNU/Emacs. | |
30 | ||
31 | ;; While it probably has less features than Dave Love's python.el and | |
32 | ;; PSF's python-mode.el it provides the main stuff you'll need while | |
33 | ;; keeping it simple :) | |
34 | ||
35 | ;; Implements Syntax highlighting, Indentation, Movement, Shell | |
36 | ;; interaction, Shell completion, Pdb tracking, Symbol completion, | |
fc2dc7df | 37 | ;; Skeletons, FFAP, Code Check, Eldoc, imenu. |
45c138ac FEG |
38 | |
39 | ;; Syntax highlighting: Fontification of code is provided and supports | |
40 | ;; python's triple quoted strings properly. | |
41 | ||
42 | ;; Indentation: Automatic indentation with indentation cycling is | |
43 | ;; provided, it allows you to navigate different available levels of | |
67845102 FEG |
44 | ;; indentation by hitting <tab> several times. Also when inserting a |
45 | ;; colon the `python-indent-electric-colon' command is invoked and | |
46 | ;; causes the current line to be dedented automatically if needed. | |
45c138ac FEG |
47 | |
48 | ;; Movement: `beginning-of-defun' and `end-of-defun' functions are | |
0567effb | 49 | ;; properly implemented. |
45c138ac FEG |
50 | |
51 | ;; Shell interaction: is provided and allows you easily execute any | |
52 | ;; block of code of your current buffer in an inferior Python process. | |
53 | ||
54 | ;; Shell completion: hitting tab will try to complete the current | |
4e531f7a | 55 | ;; word. Shell completion is implemented in a manner that if you |
45c138ac FEG |
56 | ;; change the `python-shell-interpreter' to any other (for example |
57 | ;; IPython) it should be easy to integrate another way to calculate | |
57808175 | 58 | ;; completions. You just need to specify your custom |
45c138ac | 59 | ;; `python-shell-completion-setup-code' and |
62feb915 FEG |
60 | ;; `python-shell-completion-string-code' |
61 | ||
62 | ;; Here is a complete example of the settings you would use for | |
63 | ;; iPython | |
64 | ||
65 | ;; (setq | |
66 | ;; python-shell-interpreter "ipython" | |
67 | ;; python-shell-interpreter-args "" | |
68 | ;; python-shell-prompt-regexp "In \\[[0-9]+\\]: " | |
69 | ;; python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: " | |
70 | ;; python-shell-completion-setup-code "" | |
71 | ;; python-shell-completion-string-code | |
72 | ;; "';'.join(__IP.complete('''%s'''))\n") | |
45c138ac | 73 | |
099bf010 FEG |
74 | ;; Please note that the default completion system depends on the |
75 | ;; readline module, so if you are using some Operating System that | |
76 | ;; bundles Python without it (like Windows) just install the | |
77 | ;; pyreadline from http://ipython.scipy.org/moin/PyReadline/Intro and | |
78 | ;; you should be good to go. | |
79 | ||
66bbb27f FEG |
80 | ;; The shell also contains support for virtualenvs and other special |
81 | ;; environment modification thanks to | |
82 | ;; `python-shell-process-environment' and `python-shell-exec-path'. | |
83 | ;; These two variables allows you to modify execution paths and | |
84 | ;; enviroment variables to make easy for you to setup virtualenv rules | |
85 | ;; or behaviors modifications when running shells. Here is an example | |
86 | ;; of how to make shell processes to be run using the /path/to/env/ | |
87 | ;; virtualenv: | |
88 | ||
89 | ;; (setq python-shell-process-environment | |
90 | ;; (list | |
91 | ;; (format "PATH=%s" (mapconcat | |
92 | ;; 'identity | |
93 | ;; (reverse | |
94 | ;; (cons (getenv "PATH") | |
95 | ;; '("/path/to/env/bin/"))) | |
96 | ;; ":")) | |
97 | ;; "VIRTUAL_ENV=/path/to/env/")) | |
98 | ;; (python-shell-exec-path . ("/path/to/env/bin/")) | |
99 | ||
45c138ac FEG |
100 | ;; Pdb tracking: when you execute a block of code that contains some |
101 | ;; call to pdb (or ipdb) it will prompt the block of code and will | |
102 | ;; follow the execution of pdb marking the current line with an arrow. | |
103 | ||
4e531f7a | 104 | ;; Symbol completion: you can complete the symbol at point. It uses |
45c138ac FEG |
105 | ;; the shell completion in background so you should run |
106 | ;; `python-shell-send-buffer' from time to time to get better results. | |
107 | ||
e2803784 FEG |
108 | ;; Skeletons: 6 skeletons are provided for simple inserting of class, |
109 | ;; def, for, if, try and while. These skeletons are integrated with | |
110 | ;; dabbrev. If you have `dabbrev-mode' activated and | |
111 | ;; `python-skeleton-autoinsert' is set to t, then whenever you type | |
112 | ;; the name of any of those defined and hit SPC, they will be | |
113 | ;; automatically expanded. | |
114 | ||
2947016a FEG |
115 | ;; FFAP: You can find the filename for a given module when using ffap |
116 | ;; out of the box. This feature needs an inferior python shell | |
117 | ;; running. | |
118 | ||
2d63ad56 FEG |
119 | ;; Code check: Check the current file for errors with `python-check' |
120 | ;; using the program defined in `python-check-command'. | |
8b3e0e76 | 121 | |
45c138ac | 122 | ;; Eldoc: returns documentation for object at point by using the |
4e531f7a | 123 | ;; inferior python subprocess to inspect its documentation. As you |
45c138ac FEG |
124 | ;; might guessed you should run `python-shell-send-buffer' from time |
125 | ;; to time to get better results too. | |
126 | ||
fc2dc7df FEG |
127 | ;; imenu: This mode supports imenu. It builds a plain or tree menu |
128 | ;; depending on the value of `python-imenu-make-tree'. Also you can | |
129 | ;; customize if menu items should include its type using | |
130 | ;; `python-imenu-include-defun-type'. | |
131 | ||
57808175 FEG |
132 | ;; If you used python-mode.el you probably will miss auto-indentation |
133 | ;; when inserting newlines. To achieve the same behavior you have | |
134 | ;; two options: | |
135 | ||
136 | ;; 1) Use GNU/Emacs' standard binding for `newline-and-indent': C-j. | |
137 | ||
138 | ;; 2) Add the following hook in your .emacs: | |
139 | ||
140 | ;; (add-hook 'python-mode-hook | |
141 | ;; #'(lambda () | |
142 | ;; (define-key python-mode-map "\C-m" 'newline-and-indent))) | |
143 | ||
144 | ;; I'd recommend the first one since you'll get the same behavior for | |
145 | ;; all modes out-of-the-box. | |
146 | ||
45c138ac FEG |
147 | ;;; Installation: |
148 | ||
149 | ;; Add this to your .emacs: | |
150 | ||
151 | ;; (add-to-list 'load-path "/folder/containing/file") | |
152 | ;; (require 'python) | |
153 | ||
154 | ;;; TODO: | |
155 | ||
156 | ;; Ordered by priority: | |
157 | ||
158 | ;; Better decorator support for beginning of defun | |
159 | ||
db1497be | 160 | ;; Review code and cleanup |
45c138ac | 161 | |
45c138ac FEG |
162 | ;;; Code: |
163 | ||
45c138ac | 164 | (require 'ansi-color) |
73ed6836 | 165 | (require 'comint) |
45c138ac FEG |
166 | |
167 | (eval-when-compile | |
73ed6836 FEG |
168 | (require 'cl) |
169 | ;; Avoid compiler warnings | |
170 | (defvar view-return-to-alist) | |
171 | (defvar compilation-error-regexp-alist) | |
172 | (defvar outline-heading-end-regexp)) | |
45c138ac FEG |
173 | |
174 | (autoload 'comint-mode "comint") | |
175 | ||
176 | ;;;###autoload | |
177 | (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode)) | |
178 | ;;;###autoload | |
179 | (add-to-list 'interpreter-mode-alist (cons (purecopy "python") 'python-mode)) | |
180 | ||
181 | (defgroup python nil | |
182 | "Python Language's flying circus support for Emacs." | |
183 | :group 'languages | |
184 | :version "23.2" | |
185 | :link '(emacs-commentary-link "python")) | |
186 | ||
187 | \f | |
188 | ;;; Bindings | |
189 | ||
190 | (defvar python-mode-map | |
191 | (let ((map (make-sparse-keymap))) | |
192 | ;; Indent specific | |
193 | (define-key map "\177" 'python-indent-dedent-line-backspace) | |
194 | (define-key map (kbd "<backtab>") 'python-indent-dedent-line) | |
195 | (define-key map "\C-c<" 'python-indent-shift-left) | |
196 | (define-key map "\C-c>" 'python-indent-shift-right) | |
ffdb56c3 | 197 | (define-key map ":" 'python-indent-electric-colon) |
e2803784 FEG |
198 | ;; Skeletons |
199 | (define-key map "\C-c\C-tc" 'python-skeleton-class) | |
200 | (define-key map "\C-c\C-td" 'python-skeleton-def) | |
201 | (define-key map "\C-c\C-tf" 'python-skeleton-for) | |
202 | (define-key map "\C-c\C-ti" 'python-skeleton-if) | |
203 | (define-key map "\C-c\C-tt" 'python-skeleton-try) | |
204 | (define-key map "\C-c\C-tw" 'python-skeleton-while) | |
45c138ac FEG |
205 | ;; Shell interaction |
206 | (define-key map "\C-c\C-s" 'python-shell-send-string) | |
207 | (define-key map "\C-c\C-r" 'python-shell-send-region) | |
208 | (define-key map "\C-\M-x" 'python-shell-send-defun) | |
209 | (define-key map "\C-c\C-c" 'python-shell-send-buffer) | |
210 | (define-key map "\C-c\C-l" 'python-shell-send-file) | |
211 | (define-key map "\C-c\C-z" 'python-shell-switch-to-shell) | |
8b3e0e76 FEG |
212 | ;; Some util commands |
213 | (define-key map "\C-c\C-v" 'python-check) | |
78334b43 | 214 | (define-key map "\C-c\C-f" 'python-eldoc-at-point) |
45c138ac FEG |
215 | ;; Utilities |
216 | (substitute-key-definition 'complete-symbol 'completion-at-point | |
217 | map global-map) | |
218 | (easy-menu-define python-menu map "Python Mode menu" | |
219 | `("Python" | |
220 | :help "Python-specific Features" | |
221 | ["Shift region left" python-indent-shift-left :active mark-active | |
222 | :help "Shift region left by a single indentation step"] | |
223 | ["Shift region right" python-indent-shift-right :active mark-active | |
224 | :help "Shift region right by a single indentation step"] | |
225 | "-" | |
226 | ["Mark def/class" mark-defun | |
227 | :help "Mark outermost definition around point"] | |
228 | "-" | |
229 | ["Start of def/class" beginning-of-defun | |
230 | :help "Go to start of outermost definition around point"] | |
45c138ac FEG |
231 | ["End of def/class" end-of-defun |
232 | :help "Go to end of definition around point"] | |
233 | "-" | |
e2803784 FEG |
234 | ("Skeletons") |
235 | "-" | |
45c138ac FEG |
236 | ["Start interpreter" run-python |
237 | :help "Run inferior Python process in a separate buffer"] | |
238 | ["Switch to shell" python-shell-switch-to-shell | |
239 | :help "Switch to running inferior Python process"] | |
240 | ["Eval string" python-shell-send-string | |
241 | :help "Eval string in inferior Python session"] | |
242 | ["Eval buffer" python-shell-send-buffer | |
243 | :help "Eval buffer in inferior Python session"] | |
244 | ["Eval region" python-shell-send-region | |
245 | :help "Eval region in inferior Python session"] | |
246 | ["Eval defun" python-shell-send-defun | |
247 | :help "Eval defun in inferior Python session"] | |
248 | ["Eval file" python-shell-send-file | |
249 | :help "Eval file in inferior Python session"] | |
250 | ["Debugger" pdb :help "Run pdb under GUD"] | |
251 | "-" | |
8b3e0e76 FEG |
252 | ["Check file" python-check |
253 | :help "Check file for errors"] | |
78334b43 FEG |
254 | ["Help on symbol" python-eldoc-at-point |
255 | :help "Get help on symbol at point"] | |
45c138ac FEG |
256 | ["Complete symbol" completion-at-point |
257 | :help "Complete symbol before point"])) | |
258 | map) | |
259 | "Keymap for `python-mode'.") | |
260 | ||
261 | \f | |
262 | ;;; Python specialized rx | |
263 | ||
73ed6836 FEG |
264 | (eval-when-compile |
265 | (defconst python-rx-constituents | |
266 | (list | |
267 | `(block-start . ,(rx symbol-start | |
268 | (or "def" "class" "if" "elif" "else" "try" | |
269 | "except" "finally" "for" "while" "with") | |
270 | symbol-end)) | |
0567effb FEG |
271 | `(decorator . ,(rx line-start (* space) ?@ (any letter ?_) |
272 | (* (any word ?_)))) | |
73ed6836 | 273 | `(defun . ,(rx symbol-start (or "def" "class") symbol-end)) |
0567effb | 274 | `(symbol-name . ,(rx (any letter ?_) (* (any word ?_)))) |
73ed6836 FEG |
275 | `(open-paren . ,(rx (or "{" "[" "("))) |
276 | `(close-paren . ,(rx (or "}" "]" ")"))) | |
277 | `(simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))) | |
278 | `(not-simple-operator . ,(rx (not (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))) | |
279 | `(operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">" | |
280 | "=" "%" "**" "//" "<<" ">>" "<=" "!=" | |
281 | "==" ">=" "is" "not"))) | |
282 | `(assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**=" | |
e2d8d479 FEG |
283 | ">>=" "<<=" "&=" "^=" "|=")))) |
284 | "Additional Python specific sexps for `python-rx'")) | |
45c138ac FEG |
285 | |
286 | (defmacro python-rx (&rest regexps) | |
4e531f7a | 287 | "Python mode especialized rx macro which supports common python named REGEXPS." |
45c138ac FEG |
288 | (let ((rx-constituents (append python-rx-constituents rx-constituents))) |
289 | (cond ((null regexps) | |
290 | (error "No regexp")) | |
291 | ((cdr regexps) | |
292 | (rx-to-string `(and ,@regexps) t)) | |
293 | (t | |
294 | (rx-to-string (car regexps) t))))) | |
295 | ||
296 | \f | |
297 | ;;; Font-lock and syntax | |
298 | ||
299 | (defvar python-font-lock-keywords | |
300 | ;; Keywords | |
301 | `(,(rx symbol-start | |
302 | (or "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with" | |
303 | "assert" "else" "if" "pass" "yield" "break" "except" "import" | |
304 | "print" "class" "exec" "in" "raise" "continue" "finally" "is" | |
305 | "return" "def" "for" "lambda" "try" "self") | |
306 | symbol-end) | |
307 | ;; functions | |
308 | (,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_)))) | |
309 | (1 font-lock-function-name-face)) | |
310 | ;; classes | |
311 | (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_)))) | |
312 | (1 font-lock-type-face)) | |
313 | ;; Constants | |
314 | (,(rx symbol-start (group "None" symbol-end)) | |
315 | (1 font-lock-constant-face)) | |
316 | ;; Decorators. | |
317 | (,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_)) | |
318 | (0+ "." (1+ (or word ?_))))) | |
319 | (1 font-lock-type-face)) | |
320 | ;; Builtin Exceptions | |
321 | (,(rx symbol-start | |
322 | (or "ArithmeticError" "AssertionError" "AttributeError" | |
323 | "BaseException" "BufferError" "BytesWarning" "DeprecationWarning" | |
324 | "EOFError" "EnvironmentError" "Exception" "FloatingPointError" | |
325 | "FutureWarning" "GeneratorExit" "IOError" "ImportError" | |
326 | "ImportWarning" "IndentationError" "IndexError" "KeyError" | |
327 | "KeyboardInterrupt" "LookupError" "MemoryError" "NameError" | |
328 | "NotImplemented" "NotImplementedError" "OSError" "OverflowError" | |
329 | "PendingDeprecationWarning" "ReferenceError" "RuntimeError" | |
330 | "RuntimeWarning" "StandardError" "StopIteration" "SyntaxError" | |
331 | "SyntaxWarning" "SystemError" "SystemExit" "TabError" "TypeError" | |
332 | "UnboundLocalError" "UnicodeDecodeError" "UnicodeEncodeError" | |
333 | "UnicodeError" "UnicodeTranslateError" "UnicodeWarning" | |
334 | "UserWarning" "ValueError" "Warning" "ZeroDivisionError") | |
335 | symbol-end) . font-lock-type-face) | |
336 | ;; Builtins | |
337 | (,(rx (or line-start (not (any ". \t"))) (* (any " \t")) symbol-start | |
338 | (group | |
339 | (or "_" "__debug__" "__doc__" "__import__" "__name__" "__package__" | |
340 | "abs" "all" "any" "apply" "basestring" "bin" "bool" "buffer" | |
341 | "bytearray" "bytes" "callable" "chr" "classmethod" "cmp" "coerce" | |
342 | "compile" "complex" "copyright" "credits" "delattr" "dict" "dir" | |
343 | "divmod" "enumerate" "eval" "execfile" "exit" "file" "filter" | |
344 | "float" "format" "frozenset" "getattr" "globals" "hasattr" "hash" | |
345 | "help" "hex" "id" "input" "int" "intern" "isinstance" "issubclass" | |
346 | "iter" "len" "license" "list" "locals" "long" "map" "max" "min" | |
347 | "next" "object" "oct" "open" "ord" "pow" "print" "property" "quit" | |
348 | "range" "raw_input" "reduce" "reload" "repr" "reversed" "round" | |
349 | "set" "setattr" "slice" "sorted" "staticmethod" "str" "sum" | |
350 | "super" "tuple" "type" "unichr" "unicode" "vars" "xrange" "zip" | |
351 | "True" "False" "Ellipsis")) symbol-end) | |
352 | (1 font-lock-builtin-face)) | |
353 | ;; asignations | |
354 | ;; support for a = b = c = 5 | |
355 | (,(lambda (limit) | |
d8e594db FEG |
356 | (let ((re (python-rx (group (+ (any word ?. ?_))) |
357 | (? ?\[ (+ (not (any ?\]))) ?\]) (* space) | |
45c138ac FEG |
358 | assignment-operator))) |
359 | (when (re-search-forward re limit t) | |
14a78495 | 360 | (while (and (python-info-ppss-context 'paren) |
45c138ac | 361 | (re-search-forward re limit t))) |
14a78495 | 362 | (if (and (not (python-info-ppss-context 'paren)) |
534e2438 | 363 | (not (equal (char-after (point-marker)) ?=))) |
45c138ac FEG |
364 | t |
365 | (set-match-data nil))))) | |
366 | (1 font-lock-variable-name-face nil nil)) | |
367 | ;; support for a, b, c = (1, 2, 3) | |
368 | (,(lambda (limit) | |
369 | (let ((re (python-rx (group (+ (any word ?. ?_))) (* space) | |
370 | (* ?, (* space) (+ (any word ?. ?_)) (* space)) | |
371 | ?, (* space) (+ (any word ?. ?_)) (* space) | |
372 | assignment-operator))) | |
373 | (when (and (re-search-forward re limit t) | |
374 | (goto-char (nth 3 (match-data)))) | |
14a78495 | 375 | (while (and (python-info-ppss-context 'paren) |
45c138ac FEG |
376 | (re-search-forward re limit t)) |
377 | (goto-char (nth 3 (match-data)))) | |
14a78495 | 378 | (if (not (python-info-ppss-context 'paren)) |
45c138ac FEG |
379 | t |
380 | (set-match-data nil))))) | |
381 | (1 font-lock-variable-name-face nil nil)))) | |
382 | ||
383 | ;; Fixme: Is there a better way? | |
384 | (defconst python-font-lock-syntactic-keywords | |
385 | ;; First avoid a sequence preceded by an odd number of backslashes. | |
386 | `((,(rx (not (any ?\\)) | |
387 | ?\\ (* (and ?\\ ?\\)) | |
388 | (group (syntax string-quote)) | |
389 | (backref 1) | |
390 | (group (backref 1))) | |
391 | (2 ,(string-to-syntax "\""))) ; dummy | |
392 | (,(rx (group (optional (any "uUrR"))) ; prefix gets syntax property | |
393 | (optional (any "rR")) ; possible second prefix | |
394 | (group (syntax string-quote)) ; maybe gets property | |
395 | (backref 2) ; per first quote | |
396 | (group (backref 2))) ; maybe gets property | |
397 | (1 (python-quote-syntax 1)) | |
398 | (2 (python-quote-syntax 2)) | |
399 | (3 (python-quote-syntax 3)))) | |
400 | "Make outer chars of triple-quote strings into generic string delimiters.") | |
401 | ||
402 | (defun python-quote-syntax (n) | |
403 | "Put `syntax-table' property correctly on triple quote. | |
404 | Used for syntactic keywords. N is the match number (1, 2 or 3)." | |
405 | ;; Given a triple quote, we have to check the context to know | |
406 | ;; whether this is an opening or closing triple or whether it's | |
407 | ;; quoted anyhow, and should be ignored. (For that we need to do | |
408 | ;; the same job as `syntax-ppss' to be correct and it seems to be OK | |
409 | ;; to use it here despite initial worries.) We also have to sort | |
410 | ;; out a possible prefix -- well, we don't _have_ to, but I think it | |
411 | ;; should be treated as part of the string. | |
412 | ||
413 | ;; Test cases: | |
414 | ;; ur"""ar""" x='"' # """ | |
415 | ;; x = ''' """ ' a | |
416 | ;; ''' | |
417 | ;; x '"""' x """ \"""" x | |
418 | (save-excursion | |
419 | (goto-char (match-beginning 0)) | |
420 | (cond | |
421 | ;; Consider property for the last char if in a fenced string. | |
422 | ((= n 3) | |
423 | (let* ((font-lock-syntactic-keywords nil) | |
424 | (syntax (syntax-ppss))) | |
425 | (when (eq t (nth 3 syntax)) ; after unclosed fence | |
426 | (goto-char (nth 8 syntax)) ; fence position | |
427 | (skip-chars-forward "uUrR") ; skip any prefix | |
428 | ;; Is it a matching sequence? | |
429 | (if (eq (char-after) (char-after (match-beginning 2))) | |
430 | (eval-when-compile (string-to-syntax "|")))))) | |
431 | ;; Consider property for initial char, accounting for prefixes. | |
432 | ((or (and (= n 2) ; leading quote (not prefix) | |
433 | (= (match-beginning 1) (match-end 1))) ; prefix is null | |
434 | (and (= n 1) ; prefix | |
435 | (/= (match-beginning 1) (match-end 1)))) ; non-empty | |
436 | (let ((font-lock-syntactic-keywords nil)) | |
437 | (unless (eq 'string (syntax-ppss-context (syntax-ppss))) | |
438 | (eval-when-compile (string-to-syntax "|"))))) | |
439 | ;; Otherwise (we're in a non-matching string) the property is | |
440 | ;; nil, which is OK. | |
441 | ))) | |
442 | ||
443 | (defvar python-mode-syntax-table | |
444 | (let ((table (make-syntax-table))) | |
445 | ;; Give punctuation syntax to ASCII that normally has symbol | |
446 | ;; syntax or has word syntax and isn't a letter. | |
447 | (let ((symbol (string-to-syntax "_")) | |
448 | (sst (standard-syntax-table))) | |
449 | (dotimes (i 128) | |
450 | (unless (= i ?_) | |
451 | (if (equal symbol (aref sst i)) | |
452 | (modify-syntax-entry i "." table))))) | |
453 | (modify-syntax-entry ?$ "." table) | |
454 | (modify-syntax-entry ?% "." table) | |
455 | ;; exceptions | |
456 | (modify-syntax-entry ?# "<" table) | |
457 | (modify-syntax-entry ?\n ">" table) | |
458 | (modify-syntax-entry ?' "\"" table) | |
459 | (modify-syntax-entry ?` "$" table) | |
460 | table) | |
461 | "Syntax table for Python files.") | |
462 | ||
463 | (defvar python-dotty-syntax-table | |
464 | (let ((table (make-syntax-table python-mode-syntax-table))) | |
465 | (modify-syntax-entry ?. "w" table) | |
466 | (modify-syntax-entry ?_ "w" table) | |
467 | table) | |
468 | "Dotty syntax table for Python files. | |
469 | It makes underscores and dots word constituent chars.") | |
470 | ||
471 | \f | |
472 | ;;; Indentation | |
473 | ||
474 | (defcustom python-indent-offset 4 | |
475 | "Default indentation offset for Python." | |
476 | :group 'python | |
477 | :type 'integer | |
478 | :safe 'integerp) | |
479 | ||
480 | (defcustom python-indent-guess-indent-offset t | |
481 | "Non-nil tells Python mode to guess `python-indent-offset' value." | |
482 | :type 'boolean | |
483 | :group 'python) | |
484 | ||
485 | (defvar python-indent-current-level 0 | |
486 | "Current indentation level `python-indent-line-function' is using.") | |
487 | ||
488 | (defvar python-indent-levels '(0) | |
489 | "Levels of indentation available for `python-indent-line-function'.") | |
490 | ||
491 | (defvar python-indent-dedenters '("else" "elif" "except" "finally") | |
492 | "List of words that should be dedented. | |
493 | These make `python-indent-calculate-indentation' subtract the value of | |
494 | `python-indent-offset'.") | |
495 | ||
496 | (defun python-indent-guess-indent-offset () | |
954aa7bd | 497 | "Guess and set `python-indent-offset' for the current buffer." |
bbac1eb8 FEG |
498 | (save-excursion |
499 | (save-restriction | |
500 | (widen) | |
501 | (goto-char (point-min)) | |
502 | (let ((found-block)) | |
503 | (while (and (not found-block) | |
504 | (re-search-forward | |
505 | (python-rx line-start block-start) nil t)) | |
14a78495 FEG |
506 | (when (and (not (python-info-ppss-context 'string)) |
507 | (not (python-info-ppss-context 'comment)) | |
bbac1eb8 FEG |
508 | (progn |
509 | (goto-char (line-end-position)) | |
510 | (forward-comment -1) | |
511 | (eq ?: (char-before)))) | |
512 | (setq found-block t))) | |
513 | (if (not found-block) | |
514 | (message "Can't guess python-indent-offset, using defaults: %s" | |
515 | python-indent-offset) | |
516 | (while (and (progn | |
517 | (goto-char (line-end-position)) | |
518 | (python-info-continuation-line-p)) | |
519 | (not (eobp))) | |
520 | (forward-line 1)) | |
521 | (forward-line 1) | |
522 | (forward-comment 1) | |
14d9f80c FEG |
523 | (let ((indent-offset (current-indentation))) |
524 | (when (> indent-offset 0) | |
525 | (setq python-indent-offset indent-offset)))))))) | |
45c138ac | 526 | |
e2d8d479 FEG |
527 | (defun python-indent-context () |
528 | "Get information on indentation context. | |
529 | Context information is returned with a cons with the form: | |
530 | \(STATUS . START) | |
45c138ac FEG |
531 | |
532 | Where status can be any of the following symbols: | |
45c138ac FEG |
533 | * inside-paren: If point in between (), {} or [] |
534 | * inside-string: If point is inside a string | |
535 | * after-backslash: Previous line ends in a backslash | |
536 | * after-beginning-of-block: Point is after beginning of block | |
537 | * after-line: Point is after normal line | |
538 | * no-indent: Point is at beginning of buffer or other special case | |
45c138ac FEG |
539 | START is the buffer position where the sexp starts." |
540 | (save-restriction | |
541 | (widen) | |
542 | (let ((ppss (save-excursion (beginning-of-line) (syntax-ppss))) | |
543 | (start)) | |
544 | (cons | |
545 | (cond | |
69bab1de | 546 | ;; Beginning of buffer |
19b122e4 FEG |
547 | ((save-excursion |
548 | (goto-char (line-beginning-position)) | |
549 | (bobp)) | |
69bab1de | 550 | 'no-indent) |
45c138ac | 551 | ;; Inside a paren |
14a78495 | 552 | ((setq start (python-info-ppss-context 'paren ppss)) |
45c138ac FEG |
553 | 'inside-paren) |
554 | ;; Inside string | |
14a78495 | 555 | ((setq start (python-info-ppss-context 'string ppss)) |
45c138ac FEG |
556 | 'inside-string) |
557 | ;; After backslash | |
14a78495 FEG |
558 | ((setq start (when (not (or (python-info-ppss-context 'string ppss) |
559 | (python-info-ppss-context 'comment ppss))) | |
45c138ac FEG |
560 | (let ((line-beg-pos (line-beginning-position))) |
561 | (when (eq ?\\ (char-before (1- line-beg-pos))) | |
562 | (- line-beg-pos 2))))) | |
563 | 'after-backslash) | |
564 | ;; After beginning of block | |
565 | ((setq start (save-excursion | |
566 | (let ((block-regexp (python-rx block-start)) | |
567 | (block-start-line-end ":[[:space:]]*$")) | |
568 | (back-to-indentation) | |
569 | (while (and (forward-comment -1) (not (bobp)))) | |
570 | (back-to-indentation) | |
571 | (when (or (python-info-continuation-line-p) | |
572 | (and (not (looking-at block-regexp)) | |
573 | (save-excursion | |
574 | (re-search-forward | |
575 | block-start-line-end | |
576 | (line-end-position) t)))) | |
577 | (while (and (forward-line -1) | |
578 | (python-info-continuation-line-p) | |
579 | (not (bobp)))) | |
580 | (when (not (looking-at block-regexp)) | |
581 | (forward-line 1))) | |
582 | (back-to-indentation) | |
583 | (when (and (looking-at block-regexp) | |
584 | (or (re-search-forward | |
585 | block-start-line-end | |
586 | (line-end-position) t) | |
587 | (python-info-continuation-line-p))) | |
588 | (point-marker))))) | |
589 | 'after-beginning-of-block) | |
590 | ;; After normal line | |
591 | ((setq start (save-excursion | |
592 | (while (and (forward-comment -1) (not (bobp)))) | |
3697b531 | 593 | (python-nav-sentence-start) |
45c138ac FEG |
594 | (point-marker))) |
595 | 'after-line) | |
596 | ;; Do not indent | |
597 | (t 'no-indent)) | |
598 | start)))) | |
599 | ||
600 | (defun python-indent-calculate-indentation () | |
601 | "Calculate correct indentation offset for the current line." | |
602 | (let* ((indentation-context (python-indent-context)) | |
603 | (context-status (car indentation-context)) | |
604 | (context-start (cdr indentation-context))) | |
605 | (save-restriction | |
606 | (widen) | |
607 | (save-excursion | |
608 | (case context-status | |
609 | ('no-indent 0) | |
610 | ('after-beginning-of-block | |
611 | (goto-char context-start) | |
612 | (+ (current-indentation) python-indent-offset)) | |
613 | ('after-line | |
614 | (- | |
615 | (save-excursion | |
616 | (goto-char context-start) | |
617 | (current-indentation)) | |
618 | (if (progn | |
619 | (back-to-indentation) | |
620 | (looking-at (regexp-opt python-indent-dedenters))) | |
621 | python-indent-offset | |
622 | 0))) | |
623 | ('inside-string | |
624 | (goto-char context-start) | |
625 | (current-indentation)) | |
626 | ('after-backslash | |
627 | (let* ((block-continuation | |
628 | (save-excursion | |
629 | (forward-line -1) | |
630 | (python-info-block-continuation-line-p))) | |
631 | (assignment-continuation | |
632 | (save-excursion | |
633 | (forward-line -1) | |
634 | (python-info-assignment-continuation-line-p))) | |
9f1537ef FEG |
635 | (dot-continuation |
636 | (save-excursion | |
637 | (back-to-indentation) | |
638 | (when (looking-at "\\.") | |
639 | (forward-line -1) | |
640 | (back-to-indentation) | |
641 | (forward-char (length | |
642 | (with-syntax-table python-dotty-syntax-table | |
643 | (current-word)))) | |
644 | (re-search-backward "\\." (line-beginning-position) t 1) | |
645 | (current-column)))) | |
45c138ac FEG |
646 | (indentation (cond (block-continuation |
647 | (goto-char block-continuation) | |
648 | (re-search-forward | |
649 | (python-rx block-start (* space)) | |
650 | (line-end-position) t) | |
651 | (current-column)) | |
652 | (assignment-continuation | |
653 | (goto-char assignment-continuation) | |
654 | (re-search-forward | |
655 | (python-rx simple-operator) | |
656 | (line-end-position) t) | |
657 | (forward-char 1) | |
658 | (re-search-forward | |
659 | (python-rx (* space)) | |
660 | (line-end-position) t) | |
661 | (current-column)) | |
9f1537ef FEG |
662 | (dot-continuation |
663 | dot-continuation) | |
45c138ac FEG |
664 | (t |
665 | (goto-char context-start) | |
666 | (current-indentation))))) | |
667 | indentation)) | |
668 | ('inside-paren | |
17d13b85 FEG |
669 | (or (save-excursion |
670 | (forward-comment 1) | |
f9471190 FEG |
671 | (when (and (looking-at (regexp-opt '(")" "]" "}"))) |
672 | (not (forward-char 1)) | |
673 | (not (python-info-ppss-context 'paren))) | |
17d13b85 FEG |
674 | (goto-char context-start) |
675 | (back-to-indentation) | |
676 | (current-column))) | |
677 | (- | |
678 | (save-excursion | |
679 | (goto-char context-start) | |
680 | (forward-char) | |
681 | (save-restriction | |
682 | (narrow-to-region | |
683 | (line-beginning-position) | |
684 | (line-end-position)) | |
685 | (forward-comment 1)) | |
686 | (if (looking-at "$") | |
687 | (+ (current-indentation) python-indent-offset) | |
688 | (forward-comment 1) | |
689 | (current-column))) | |
690 | (if (progn | |
691 | (back-to-indentation) | |
692 | (looking-at (regexp-opt '(")" "]" "}")))) | |
693 | python-indent-offset | |
694 | 0))))))))) | |
45c138ac FEG |
695 | |
696 | (defun python-indent-calculate-levels () | |
697 | "Calculate `python-indent-levels' and reset `python-indent-current-level'." | |
698 | (let* ((indentation (python-indent-calculate-indentation)) | |
699 | (remainder (% indentation python-indent-offset)) | |
700 | (steps (/ (- indentation remainder) python-indent-offset))) | |
86f1889a | 701 | (setq python-indent-levels '(0)) |
45c138ac FEG |
702 | (dotimes (step steps) |
703 | (setq python-indent-levels | |
704 | (cons (* python-indent-offset (1+ step)) python-indent-levels))) | |
705 | (when (not (eq 0 remainder)) | |
706 | (setq python-indent-levels | |
707 | (cons (+ (* python-indent-offset steps) remainder) | |
708 | python-indent-levels))) | |
709 | (setq python-indent-levels (nreverse python-indent-levels)) | |
710 | (setq python-indent-current-level (1- (length python-indent-levels))))) | |
711 | ||
712 | (defun python-indent-toggle-levels () | |
713 | "Toggle `python-indent-current-level' over `python-indent-levels'." | |
714 | (setq python-indent-current-level (1- python-indent-current-level)) | |
715 | (when (< python-indent-current-level 0) | |
716 | (setq python-indent-current-level (1- (length python-indent-levels))))) | |
717 | ||
718 | (defun python-indent-line (&optional force-toggle) | |
719 | "Internal implementation of `python-indent-line-function'. | |
45c138ac FEG |
720 | Uses the offset calculated in |
721 | `python-indent-calculate-indentation' and available levels | |
e2d8d479 FEG |
722 | indicated by the variable `python-indent-levels' to set the |
723 | current indentation. | |
45c138ac FEG |
724 | |
725 | When the variable `last-command' is equal to | |
e2d8d479 FEG |
726 | `indent-for-tab-command' or FORCE-TOGGLE is non-nil it cycles |
727 | levels indicated in the variable `python-indent-levels' by | |
728 | setting the current level in the variable | |
729 | `python-indent-current-level'. | |
45c138ac FEG |
730 | |
731 | When the variable `last-command' is not equal to | |
e2d8d479 FEG |
732 | `indent-for-tab-command' and FORCE-TOGGLE is nil it calculates |
733 | possible indentation levels and saves it in the variable | |
734 | `python-indent-levels'. Afterwards it sets the variable | |
735 | `python-indent-current-level' correctly so offset is equal | |
736 | to (`nth' `python-indent-current-level' `python-indent-levels')" | |
45c138ac FEG |
737 | (if (or (and (eq this-command 'indent-for-tab-command) |
738 | (eq last-command this-command)) | |
739 | force-toggle) | |
86f1889a FEG |
740 | (if (not (equal python-indent-levels '(0))) |
741 | (python-indent-toggle-levels) | |
742 | (python-indent-calculate-levels)) | |
45c138ac FEG |
743 | (python-indent-calculate-levels)) |
744 | (beginning-of-line) | |
745 | (delete-horizontal-space) | |
746 | (indent-to (nth python-indent-current-level python-indent-levels)) | |
747 | (save-restriction | |
748 | (widen) | |
749 | (let ((closing-block-point (python-info-closing-block))) | |
750 | (when closing-block-point | |
751 | (message "Closes %s" (buffer-substring | |
752 | closing-block-point | |
753 | (save-excursion | |
754 | (goto-char closing-block-point) | |
755 | (line-end-position)))))))) | |
756 | ||
757 | (defun python-indent-line-function () | |
758 | "`indent-line-function' for Python mode. | |
e2d8d479 | 759 | See `python-indent-line' for details." |
45c138ac FEG |
760 | (python-indent-line)) |
761 | ||
762 | (defun python-indent-dedent-line () | |
e2d8d479 | 763 | "De-indent current line." |
45c138ac | 764 | (interactive "*") |
14a78495 FEG |
765 | (when (and (not (or (python-info-ppss-context 'string) |
766 | (python-info-ppss-context 'comment))) | |
45c138ac FEG |
767 | (<= (point-marker) (save-excursion |
768 | (back-to-indentation) | |
769 | (point-marker))) | |
770 | (> (current-column) 0)) | |
771 | (python-indent-line t) | |
772 | t)) | |
773 | ||
774 | (defun python-indent-dedent-line-backspace (arg) | |
e2d8d479 | 775 | "De-indent current line. |
45c138ac | 776 | Argument ARG is passed to `backward-delete-char-untabify' when |
e2d8d479 | 777 | point is not in between the indentation." |
45c138ac FEG |
778 | (interactive "*p") |
779 | (when (not (python-indent-dedent-line)) | |
780 | (backward-delete-char-untabify arg))) | |
183f9296 | 781 | (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede) |
45c138ac FEG |
782 | |
783 | (defun python-indent-region (start end) | |
784 | "Indent a python region automagically. | |
785 | ||
786 | Called from a program, START and END specify the region to indent." | |
cb42456f FEG |
787 | (let ((deactivate-mark nil)) |
788 | (save-excursion | |
789 | (goto-char end) | |
790 | (setq end (point-marker)) | |
791 | (goto-char start) | |
792 | (or (bolp) (forward-line 1)) | |
793 | (while (< (point) end) | |
794 | (or (and (bolp) (eolp)) | |
795 | (let (word) | |
796 | (forward-line -1) | |
797 | (back-to-indentation) | |
798 | (setq word (current-word)) | |
799 | (forward-line 1) | |
800 | (when word | |
801 | (beginning-of-line) | |
802 | (delete-horizontal-space) | |
803 | (indent-to (python-indent-calculate-indentation))))) | |
804 | (forward-line 1)) | |
805 | (move-marker end nil)))) | |
45c138ac FEG |
806 | |
807 | (defun python-indent-shift-left (start end &optional count) | |
808 | "Shift lines contained in region START END by COUNT columns to the left. | |
e2d8d479 FEG |
809 | COUNT defaults to `python-indent-offset'. If region isn't |
810 | active, the current line is shifted. The shifted region includes | |
811 | the lines in which START and END lie. An error is signaled if | |
812 | any lines in the region are indented less than COUNT columns." | |
45c138ac FEG |
813 | (interactive |
814 | (if mark-active | |
815 | (list (region-beginning) (region-end) current-prefix-arg) | |
816 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | |
817 | (if count | |
818 | (setq count (prefix-numeric-value count)) | |
819 | (setq count python-indent-offset)) | |
820 | (when (> count 0) | |
cb42456f FEG |
821 | (let ((deactivate-mark nil)) |
822 | (save-excursion | |
823 | (goto-char start) | |
824 | (while (< (point) end) | |
825 | (if (and (< (current-indentation) count) | |
826 | (not (looking-at "[ \t]*$"))) | |
827 | (error "Can't shift all lines enough")) | |
828 | (forward-line)) | |
829 | (indent-rigidly start end (- count)))))) | |
45c138ac FEG |
830 | |
831 | (add-to-list 'debug-ignored-errors "^Can't shift all lines enough") | |
832 | ||
833 | (defun python-indent-shift-right (start end &optional count) | |
834 | "Shift lines contained in region START END by COUNT columns to the left. | |
e2d8d479 FEG |
835 | COUNT defaults to `python-indent-offset'. If region isn't |
836 | active, the current line is shifted. The shifted region includes | |
837 | the lines in which START and END lie." | |
45c138ac FEG |
838 | (interactive |
839 | (if mark-active | |
840 | (list (region-beginning) (region-end) current-prefix-arg) | |
841 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | |
cb42456f FEG |
842 | (let ((deactivate-mark nil)) |
843 | (if count | |
844 | (setq count (prefix-numeric-value count)) | |
845 | (setq count python-indent-offset)) | |
846 | (indent-rigidly start end count))) | |
45c138ac | 847 | |
ffdb56c3 | 848 | (defun python-indent-electric-colon (arg) |
e2d8d479 FEG |
849 | "Insert a colon and maybe de-indent the current line. |
850 | With numeric ARG, just insert that many colons. With | |
851 | \\[universal-argument], just insert a single colon." | |
ffdb56c3 FEG |
852 | (interactive "*P") |
853 | (self-insert-command (if (not (integerp arg)) 1 arg)) | |
c43cd8b1 FEG |
854 | (when (and (not arg) |
855 | (eolp) | |
856 | (not (equal ?: (char-after (- (point-marker) 2)))) | |
857 | (not (or (python-info-ppss-context 'string) | |
858 | (python-info-ppss-context 'comment)))) | |
859 | (let ((indentation (current-indentation)) | |
860 | (calculated-indentation (python-indent-calculate-indentation))) | |
861 | (when (> indentation calculated-indentation) | |
862 | (save-excursion | |
863 | (indent-line-to calculated-indentation) | |
864 | (when (not (python-info-closing-block)) | |
865 | (indent-line-to indentation))))))) | |
ffdb56c3 FEG |
866 | (put 'python-indent-electric-colon 'delete-selection t) |
867 | ||
45c138ac FEG |
868 | \f |
869 | ;;; Navigation | |
870 | ||
0567effb | 871 | (defvar python-nav-beginning-of-defun-regexp |
af5c1beb FEG |
872 | (python-rx line-start (* space) defun (+ space) (group symbol-name)) |
873 | "Regular expresion matching beginning of class or function. | |
fc2dc7df FEG |
874 | The name of the defun should be grouped so it can be retrieved |
875 | via `match-string'.") | |
45c138ac | 876 | |
6b432853 | 877 | (defun python-nav-beginning-of-defun (&optional nodecorators) |
053a6c72 | 878 | "Move point to `beginning-of-defun'. |
6b432853 FEG |
879 | When NODECORATORS is non-nil decorators are not included. This |
880 | is the main part of`python-beginning-of-defun-function' | |
74d7b605 | 881 | implementation. Return non-nil if point is moved to the |
fc2dc7df | 882 | `beginning-of-defun'." |
0567effb FEG |
883 | (let ((indent-pos (save-excursion |
884 | (back-to-indentation) | |
885 | (point-marker))) | |
74d7b605 | 886 | (found) |
0567effb FEG |
887 | (include-decorators |
888 | (lambda () | |
6b432853 FEG |
889 | (when (not nodecorators) |
890 | (when (save-excursion | |
891 | (forward-line -1) | |
892 | (looking-at (python-rx decorator))) | |
893 | (while (and (not (bobp)) | |
894 | (forward-line -1) | |
895 | (looking-at (python-rx decorator)))) | |
896 | (when (not (bobp)) (forward-line 1))))))) | |
0567effb FEG |
897 | (if (and (> (point) indent-pos) |
898 | (save-excursion | |
899 | (goto-char (line-beginning-position)) | |
900 | (looking-at python-nav-beginning-of-defun-regexp))) | |
45c138ac | 901 | (progn |
0567effb | 902 | (goto-char (line-beginning-position)) |
74d7b605 FEG |
903 | (funcall include-decorators) |
904 | (setq found t)) | |
0567effb | 905 | (goto-char (line-beginning-position)) |
74d7b605 FEG |
906 | (when (re-search-backward python-nav-beginning-of-defun-regexp nil t) |
907 | (setq found t)) | |
0567effb | 908 | (goto-char (or (python-info-ppss-context 'string) (point))) |
74d7b605 FEG |
909 | (funcall include-decorators)) |
910 | found)) | |
0567effb | 911 | |
6b432853 | 912 | (defun python-beginning-of-defun-function (&optional arg nodecorators) |
0567effb FEG |
913 | "Move point to the beginning of def or class. |
914 | With positive ARG move that number of functions forward. With | |
6b432853 | 915 | negative do the same but backwards. When NODECORATORS is non-nil |
74d7b605 | 916 | decorators are not included. Return non-nil if point is moved to the |
fc2dc7df | 917 | `beginning-of-defun'." |
0567effb FEG |
918 | (when (or (null arg) (= arg 0)) (setq arg 1)) |
919 | (if (> arg 0) | |
74d7b605 FEG |
920 | (dotimes (i arg (python-nav-beginning-of-defun nodecorators))) |
921 | (let ((found)) | |
922 | (dotimes (i (- arg) found) | |
923 | (python-end-of-defun-function) | |
924 | (forward-comment 1) | |
925 | (goto-char (line-end-position)) | |
926 | (when (not (eobp)) | |
927 | (setq found | |
928 | (python-nav-beginning-of-defun nodecorators))))))) | |
45c138ac FEG |
929 | |
930 | (defun python-end-of-defun-function () | |
931 | "Move point to the end of def or class. | |
932 | Returns nil if point is not in a def or class." | |
0567effb FEG |
933 | (interactive) |
934 | (let ((beg-defun-indent) | |
935 | (decorator-regexp "[[:space:]]*@")) | |
936 | (when (looking-at decorator-regexp) | |
937 | (while (and (not (eobp)) | |
938 | (forward-line 1) | |
939 | (looking-at decorator-regexp)))) | |
940 | (when (not (looking-at python-nav-beginning-of-defun-regexp)) | |
941 | (python-beginning-of-defun-function)) | |
942 | (setq beg-defun-indent (current-indentation)) | |
943 | (forward-line 1) | |
944 | (while (and (forward-line 1) | |
945 | (not (eobp)) | |
946 | (or (not (current-word)) | |
947 | (> (current-indentation) beg-defun-indent)))) | |
948 | (forward-comment 1) | |
949 | (goto-char (line-beginning-position)))) | |
45c138ac | 950 | |
3697b531 FEG |
951 | (defun python-nav-sentence-start () |
952 | "Move to start of current sentence." | |
953 | (interactive "^") | |
954 | (while (and (not (back-to-indentation)) | |
955 | (not (bobp)) | |
956 | (when (or | |
957 | (save-excursion | |
958 | (forward-line -1) | |
959 | (python-info-line-ends-backslash-p)) | |
960 | (python-info-ppss-context 'paren)) | |
961 | (forward-line -1))))) | |
962 | ||
963 | (defun python-nav-sentence-end () | |
964 | "Move to end of current sentence." | |
965 | (interactive "^") | |
966 | (while (and (goto-char (line-end-position)) | |
967 | (not (eobp)) | |
968 | (when (or | |
969 | (python-info-line-ends-backslash-p) | |
970 | (python-info-ppss-context 'paren)) | |
971 | (forward-line 1))))) | |
972 | ||
45c138ac FEG |
973 | \f |
974 | ;;; Shell integration | |
975 | ||
976 | (defvar python-shell-buffer-name "Python" | |
977 | "Default buffer name for Python interpreter.") | |
978 | ||
979 | (defcustom python-shell-interpreter "python" | |
980 | "Default Python interpreter for shell." | |
45c138ac | 981 | :type 'string |
c0428ba0 | 982 | :group 'python |
45c138ac FEG |
983 | :safe 'stringp) |
984 | ||
985 | (defcustom python-shell-interpreter-args "-i" | |
986 | "Default arguments for the Python interpreter." | |
45c138ac | 987 | :type 'string |
c0428ba0 | 988 | :group 'python |
45c138ac FEG |
989 | :safe 'stringp) |
990 | ||
991 | (defcustom python-shell-prompt-regexp ">>> " | |
e2d8d479 FEG |
992 | "Regular Expression matching top\-level input prompt of python shell. |
993 | It should not contain a caret (^) at the beginning." | |
45c138ac FEG |
994 | :type 'string |
995 | :group 'python | |
996 | :safe 'stringp) | |
997 | ||
998 | (defcustom python-shell-prompt-block-regexp "[.][.][.] " | |
e2d8d479 FEG |
999 | "Regular Expression matching block input prompt of python shell. |
1000 | It should not contain a caret (^) at the beginning." | |
45c138ac FEG |
1001 | :type 'string |
1002 | :group 'python | |
1003 | :safe 'stringp) | |
1004 | ||
62feb915 | 1005 | (defcustom python-shell-prompt-output-regexp nil |
e2d8d479 FEG |
1006 | "Regular Expression matching output prompt of python shell. |
1007 | It should not contain a caret (^) at the beginning." | |
62feb915 FEG |
1008 | :type 'string |
1009 | :group 'python | |
1010 | :safe 'stringp) | |
1011 | ||
45c138ac | 1012 | (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ " |
e2d8d479 FEG |
1013 | "Regular Expression matching pdb input prompt of python shell. |
1014 | It should not contain a caret (^) at the beginning." | |
45c138ac FEG |
1015 | :type 'string |
1016 | :group 'python | |
1017 | :safe 'stringp) | |
1018 | ||
30e429dd FEG |
1019 | (defcustom python-shell-send-setup-max-wait 5 |
1020 | "Seconds to wait for process output before code setup. | |
1021 | If output is received before the especified time then control is | |
1022 | returned in that moment and not after waiting." | |
1023 | :type 'number | |
1024 | :group 'python | |
1025 | :safe 'numberp) | |
1026 | ||
66bbb27f FEG |
1027 | (defcustom python-shell-process-environment nil |
1028 | "List of enviroment variables for Python shell. | |
1029 | This variable follows the same rules as `process-enviroment' | |
1030 | since it merges with it before the process creation routines are | |
1031 | called. When this variable is nil, the Python shell is run with | |
1032 | the default `process-enviroment'." | |
1033 | :type '(repeat string) | |
1034 | :group 'python | |
1035 | :safe 'listp) | |
1036 | ||
1037 | (defcustom python-shell-exec-path nil | |
1038 | "List of path to search for binaries. | |
1039 | This variable follows the same rules as `exec-path' since it | |
1040 | merges with it before the process creation routines are called. | |
1041 | When this variable is nil, the Python shell is run with the | |
1042 | default `exec-path'." | |
1043 | :type '(repeat string) | |
1044 | :group 'python | |
1045 | :safe 'listp) | |
1046 | ||
c0428ba0 FEG |
1047 | (defcustom python-shell-setup-codes '(python-shell-completion-setup-code |
1048 | python-ffap-setup-code | |
1049 | python-eldoc-setup-code) | |
1050 | "List of code run by `python-shell-send-setup-codes'. | |
e2d8d479 | 1051 | Each variable can contain either a simple string with the code to |
c0428ba0 FEG |
1052 | execute or a cons with the form (CODE . DESCRIPTION), where CODE |
1053 | is a string with the code to execute and DESCRIPTION is the | |
1054 | description of it." | |
1055 | :type '(repeat symbol) | |
1056 | :group 'python | |
1057 | :safe 'listp) | |
1058 | ||
45c138ac FEG |
1059 | (defcustom python-shell-compilation-regexp-alist |
1060 | `((,(rx line-start (1+ (any " \t")) "File \"" | |
1061 | (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c | |
1062 | "\", line " (group (1+ digit))) | |
1063 | 1 2) | |
1064 | (,(rx " in file " (group (1+ not-newline)) " on line " | |
1065 | (group (1+ digit))) | |
1066 | 1 2) | |
1067 | (,(rx line-start "> " (group (1+ (not (any "(\"<")))) | |
1068 | "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()") | |
1069 | 1 2)) | |
1070 | "`compilation-error-regexp-alist' for inferior Python." | |
1071 | :type '(alist string) | |
1072 | :group 'python) | |
1073 | ||
1074 | (defun python-shell-get-process-name (dedicated) | |
1075 | "Calculate the appropiate process name for inferior Python process. | |
45c138ac FEG |
1076 | If DEDICATED is t and the variable `buffer-file-name' is non-nil |
1077 | returns a string with the form | |
1078 | `python-shell-buffer-name'[variable `buffer-file-name'] else | |
e2d8d479 FEG |
1079 | returns the value of `python-shell-buffer-name'. After |
1080 | calculating the process name adds the buffer name for the process | |
1081 | in the `same-window-buffer-names' list." | |
45c138ac FEG |
1082 | (let ((process-name |
1083 | (if (and dedicated | |
1084 | buffer-file-name) | |
1085 | (format "%s[%s]" python-shell-buffer-name buffer-file-name) | |
1086 | (format "%s" python-shell-buffer-name)))) | |
1087 | (add-to-list 'same-window-buffer-names (purecopy | |
1088 | (format "*%s*" process-name))) | |
1089 | process-name)) | |
1090 | ||
1091 | (defun python-shell-parse-command () | |
e2d8d479 | 1092 | "Calculate the string used to execute the inferior Python process." |
45c138ac FEG |
1093 | (format "%s %s" python-shell-interpreter python-shell-interpreter-args)) |
1094 | ||
1095 | (defun python-comint-output-filter-function (output) | |
1096 | "Hook run after content is put into comint buffer. | |
1097 | OUTPUT is a string with the contents of the buffer." | |
1098 | (ansi-color-filter-apply output)) | |
1099 | ||
1100 | (defvar inferior-python-mode-current-file nil | |
1101 | "Current file from which a region was sent.") | |
1102 | (make-variable-buffer-local 'inferior-python-mode-current-file) | |
1103 | ||
45c138ac | 1104 | (define-derived-mode inferior-python-mode comint-mode "Inferior Python" |
62feb915 | 1105 | "Major mode for Python inferior process. |
e2d8d479 FEG |
1106 | Runs a Python interpreter as a subprocess of Emacs, with Python |
1107 | I/O through an Emacs buffer. Variables | |
1108 | `python-shell-interpreter' and `python-shell-interpreter-args' | |
1109 | controls which Python interpreter is run. Variables | |
1110 | `python-shell-prompt-regexp', | |
1111 | `python-shell-prompt-output-regexp', | |
1112 | `python-shell-prompt-block-regexp', | |
1113 | `python-shell-completion-setup-code', | |
1114 | `python-shell-completion-string-code', `python-eldoc-setup-code', | |
1115 | `python-eldoc-string-code', `python-ffap-setup-code' and | |
1116 | `python-ffap-string-code' can customize this mode for different | |
1117 | Python interpreters. | |
1118 | ||
1119 | You can also add additional setup code to be run at | |
1120 | initialization of the interpreter via `python-shell-setup-codes' | |
1121 | variable. | |
1122 | ||
1123 | \(Type \\[describe-mode] in the process buffer for a list of commands.)" | |
45c138ac FEG |
1124 | (set-syntax-table python-mode-syntax-table) |
1125 | (setq mode-line-process '(":%s")) | |
1126 | (setq comint-prompt-regexp (format "^\\(?:%s\\|%s\\|%s\\)" | |
1127 | python-shell-prompt-regexp | |
1128 | python-shell-prompt-block-regexp | |
1129 | python-shell-prompt-pdb-regexp)) | |
1130 | (make-local-variable 'comint-output-filter-functions) | |
1131 | (add-hook 'comint-output-filter-functions | |
1132 | 'python-comint-output-filter-function) | |
1133 | (add-hook 'comint-output-filter-functions | |
1134 | 'python-pdbtrack-comint-output-filter-function) | |
1135 | (set (make-local-variable 'compilation-error-regexp-alist) | |
1136 | python-shell-compilation-regexp-alist) | |
ed0eb594 FEG |
1137 | (define-key inferior-python-mode-map [remap complete-symbol] |
1138 | 'completion-at-point) | |
1139 | (add-hook 'completion-at-point-functions | |
1140 | 'python-shell-completion-complete-at-point nil 'local) | |
62feb915 FEG |
1141 | (add-to-list (make-local-variable 'comint-dynamic-complete-functions) |
1142 | 'python-shell-completion-complete-at-point) | |
1143 | (define-key inferior-python-mode-map (kbd "<tab>") | |
1144 | 'python-shell-completion-complete-or-indent) | |
45c138ac FEG |
1145 | (compilation-shell-minor-mode 1)) |
1146 | ||
1147 | (defun run-python (dedicated cmd) | |
1148 | "Run an inferior Python process. | |
e2d8d479 FEG |
1149 | Input and output via buffer named after |
1150 | `python-shell-buffer-name'. If there is a process already | |
1151 | running in that buffer, just switch to it. | |
1152 | With argument, allows you to define DEDICATED, so a dedicated | |
1153 | process for the current buffer is open, and define CMD so you can | |
1154 | edit the command used to call the interpreter (default is value | |
1155 | of `python-shell-interpreter' and arguments defined in | |
1156 | `python-shell-interpreter-args'). Runs the hook | |
1157 | `inferior-python-mode-hook' (after the `comint-mode-hook' is | |
1158 | run). | |
1159 | \(Type \\[describe-mode] in the process buffer for a list of commands.)" | |
45c138ac FEG |
1160 | (interactive |
1161 | (if current-prefix-arg | |
1162 | (list | |
1163 | (y-or-n-p "Make dedicated process? ") | |
1164 | (read-string "Run Python: " (python-shell-parse-command))) | |
1165 | (list nil (python-shell-parse-command)))) | |
1166 | (let* ((proc-name (python-shell-get-process-name dedicated)) | |
66bbb27f FEG |
1167 | (proc-buffer-name (format "*%s*" proc-name)) |
1168 | (process-environment | |
1169 | (if python-shell-process-environment | |
c942de99 FEG |
1170 | (python-util-merge 'list python-shell-process-environment |
1171 | process-environment 'string=) | |
66bbb27f FEG |
1172 | process-environment)) |
1173 | (exec-path | |
1174 | (if python-shell-exec-path | |
c942de99 FEG |
1175 | (python-util-merge 'list python-shell-exec-path |
1176 | exec-path 'string=) | |
66bbb27f | 1177 | exec-path))) |
45c138ac FEG |
1178 | (when (not (comint-check-proc proc-buffer-name)) |
1179 | (let ((cmdlist (split-string-and-unquote cmd))) | |
1180 | (set-buffer | |
1181 | (apply 'make-comint proc-name (car cmdlist) nil | |
1182 | (cdr cmdlist))) | |
1183 | (inferior-python-mode))) | |
1184 | (pop-to-buffer proc-buffer-name)) | |
1185 | dedicated) | |
1186 | ||
1187 | (defun python-shell-get-process () | |
1188 | "Get inferior Python process for current buffer and return it." | |
1189 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) | |
1190 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) | |
1191 | (global-proc-name (python-shell-get-process-name nil)) | |
1192 | (global-proc-buffer-name (format "*%s*" global-proc-name)) | |
1193 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | |
1194 | (global-running (comint-check-proc global-proc-buffer-name))) | |
1195 | ;; Always prefer dedicated | |
1196 | (get-buffer-process (or (and dedicated-running dedicated-proc-buffer-name) | |
1197 | (and global-running global-proc-buffer-name))))) | |
1198 | ||
1199 | (defun python-shell-get-or-create-process () | |
1200 | "Get or create an inferior Python process for current buffer and return it." | |
79dafa51 FEG |
1201 | (let* ((old-buffer (current-buffer)) |
1202 | (dedicated-proc-name (python-shell-get-process-name t)) | |
45c138ac FEG |
1203 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) |
1204 | (global-proc-name (python-shell-get-process-name nil)) | |
1205 | (global-proc-buffer-name (format "*%s*" global-proc-name)) | |
1206 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | |
1207 | (global-running (comint-check-proc global-proc-buffer-name)) | |
1208 | (current-prefix-arg 4)) | |
1209 | (when (and (not dedicated-running) (not global-running)) | |
1210 | (if (call-interactively 'run-python) | |
1211 | (setq dedicated-running t) | |
1212 | (setq global-running t))) | |
1213 | ;; Always prefer dedicated | |
79dafa51 | 1214 | (switch-to-buffer old-buffer) |
45c138ac FEG |
1215 | (get-buffer-process (if dedicated-running |
1216 | dedicated-proc-buffer-name | |
1217 | global-proc-buffer-name)))) | |
1218 | ||
9ce938be FEG |
1219 | (defun python-shell-send-string (string &optional process msg) |
1220 | "Send STRING to inferior Python PROCESS. | |
1221 | When MSG is non-nil messages the first line of STRING." | |
45c138ac | 1222 | (interactive "sPython command: ") |
9ce938be FEG |
1223 | (let ((process (or process (python-shell-get-or-create-process))) |
1224 | (lines (split-string string "\n" t))) | |
1225 | (when msg | |
1226 | (message (format "Sent: %s..." (nth 0 lines)))) | |
1227 | (if (> (length lines) 1) | |
1228 | (let* ((temp-file-name (make-temp-file "py")) | |
1229 | (file-name (or (buffer-file-name) temp-file-name))) | |
1230 | (with-temp-file temp-file-name | |
1231 | (insert string) | |
1232 | (delete-trailing-whitespace)) | |
1233 | (python-shell-send-file file-name process temp-file-name)) | |
1234 | (comint-send-string process string) | |
1235 | (when (or (not (string-match "\n$" string)) | |
1236 | (string-match "\n[ \t].*\n?$" string)) | |
1237 | (comint-send-string process "\n"))))) | |
1238 | ||
1239 | (defun python-shell-send-string-no-output (string &optional process msg) | |
1240 | "Send STRING to PROCESS and inhibit output. | |
e2d8d479 FEG |
1241 | When MSG is non-nil messages the first line of STRING. Return |
1242 | the output." | |
9ce938be FEG |
1243 | (let* ((output-buffer) |
1244 | (process (or process (python-shell-get-or-create-process))) | |
1245 | (comint-preoutput-filter-functions | |
1246 | (append comint-preoutput-filter-functions | |
1247 | '(ansi-color-filter-apply | |
1248 | (lambda (string) | |
1249 | (setq output-buffer (concat output-buffer string)) | |
1250 | ""))))) | |
1251 | (python-shell-send-string string process msg) | |
1252 | (accept-process-output process) | |
62feb915 FEG |
1253 | ;; Cleanup output prompt regexp |
1254 | (when (and (not (string= "" output-buffer)) | |
1255 | (> (length python-shell-prompt-output-regexp) 0)) | |
1256 | (setq output-buffer | |
1257 | (with-temp-buffer | |
2db30ac5 | 1258 | (insert output-buffer) |
62feb915 FEG |
1259 | (goto-char (point-min)) |
1260 | (forward-comment 1) | |
1261 | (buffer-substring-no-properties | |
1262 | (or | |
1263 | (and (looking-at python-shell-prompt-output-regexp) | |
1264 | (re-search-forward | |
1265 | python-shell-prompt-output-regexp nil t 1)) | |
1266 | (point-marker)) | |
1267 | (point-max))))) | |
9ce938be FEG |
1268 | (mapconcat |
1269 | (lambda (string) string) | |
1270 | (butlast (split-string output-buffer "\n")) "\n"))) | |
45c138ac FEG |
1271 | |
1272 | (defun python-shell-send-region (start end) | |
1273 | "Send the region delimited by START and END to inferior Python process." | |
1274 | (interactive "r") | |
9ce938be FEG |
1275 | (let ((deactivate-mark nil)) |
1276 | (python-shell-send-string (buffer-substring start end) nil t))) | |
45c138ac FEG |
1277 | |
1278 | (defun python-shell-send-buffer () | |
1279 | "Send the entire buffer to inferior Python process." | |
1280 | (interactive) | |
1281 | (save-restriction | |
1282 | (widen) | |
1283 | (python-shell-send-region (point-min) (point-max)))) | |
1284 | ||
1285 | (defun python-shell-send-defun (arg) | |
2ed294c5 | 1286 | "Send the current defun to inferior Python process. |
45c138ac FEG |
1287 | When argument ARG is non-nil sends the innermost defun." |
1288 | (interactive "P") | |
1289 | (save-excursion | |
2ed294c5 FEG |
1290 | (python-shell-send-region |
1291 | (progn | |
1292 | (or (python-beginning-of-defun-function) | |
1293 | (progn (beginning-of-line) (point-marker)))) | |
1294 | (progn | |
1295 | (or (python-end-of-defun-function) | |
1296 | (progn (end-of-line) (point-marker))))))) | |
45c138ac | 1297 | |
d439cda5 FEG |
1298 | (defun python-shell-send-file (file-name &optional process temp-file-name) |
1299 | "Send FILE-NAME to inferior Python PROCESS. | |
1300 | If TEMP-FILE-NAME is passed then that file is used for processing | |
1301 | instead, while internally the shell will continue to use | |
1302 | FILE-NAME." | |
45c138ac | 1303 | (interactive "fFile to send: ") |
9ce938be FEG |
1304 | (let* ((process (or process (python-shell-get-or-create-process))) |
1305 | (temp-file-name (when temp-file-name | |
1306 | (expand-file-name temp-file-name))) | |
1307 | (file-name (or (expand-file-name file-name) temp-file-name))) | |
1308 | (when (not file-name) | |
1309 | (error "If FILE-NAME is nil then TEMP-FILE-NAME must be non-nil")) | |
d439cda5 | 1310 | (with-current-buffer (process-buffer process) |
24b68537 FEG |
1311 | (setq inferior-python-mode-current-file |
1312 | (convert-standard-filename file-name))) | |
13d914ed | 1313 | (python-shell-send-string |
b962ebad | 1314 | (format |
d439cda5 FEG |
1315 | (concat "__pyfile = open('''%s''');" |
1316 | "exec(compile(__pyfile.read(), '''%s''', 'exec'));" | |
1317 | "__pyfile.close()") | |
1318 | (or temp-file-name file-name) file-name) | |
13d914ed | 1319 | process))) |
45c138ac FEG |
1320 | |
1321 | (defun python-shell-switch-to-shell () | |
1322 | "Switch to inferior Python process buffer." | |
1323 | (interactive) | |
1324 | (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t)) | |
1325 | ||
c0428ba0 FEG |
1326 | (defun python-shell-send-setup-code () |
1327 | "Send all setup code for shell. | |
1328 | This function takes the list of setup code to send from the | |
1329 | `python-shell-setup-codes' list." | |
1330 | (let ((msg "Sent %s") | |
1331 | (process (get-buffer-process (current-buffer)))) | |
30e429dd | 1332 | (accept-process-output process python-shell-send-setup-max-wait) |
c0428ba0 FEG |
1333 | (dolist (code python-shell-setup-codes) |
1334 | (when code | |
1335 | (when (consp code) | |
1336 | (setq msg (cdr code))) | |
1337 | (message (format msg code)) | |
1338 | (python-shell-send-string-no-output | |
1339 | (symbol-value code) process))))) | |
1340 | ||
1341 | (add-hook 'inferior-python-mode-hook | |
1342 | #'python-shell-send-setup-code) | |
1343 | ||
45c138ac FEG |
1344 | \f |
1345 | ;;; Shell completion | |
1346 | ||
1347 | (defvar python-shell-completion-setup-code | |
1348 | "try: | |
1349 | import readline | |
1350 | except ImportError: | |
1351 | def __COMPLETER_all_completions(text): [] | |
1352 | else: | |
1353 | import rlcompleter | |
1354 | readline.set_completer(rlcompleter.Completer().complete) | |
1355 | def __COMPLETER_all_completions(text): | |
1356 | import sys | |
1357 | completions = [] | |
1358 | try: | |
1359 | i = 0 | |
1360 | while True: | |
1361 | res = readline.get_completer()(text, i) | |
1362 | if not res: break | |
1363 | i += 1 | |
1364 | completions.append(res) | |
1365 | except NameError: | |
1366 | pass | |
1367 | return completions" | |
1368 | "Code used to setup completion in inferior Python processes.") | |
1369 | ||
62feb915 | 1370 | (defvar python-shell-completion-string-code |
45c138ac FEG |
1371 | "';'.join(__COMPLETER_all_completions('''%s'''))\n" |
1372 | "Python code used to get a string of completions separated by semicolons.") | |
1373 | ||
075a0f61 FEG |
1374 | (defun python-shell-completion--get-completions (input process) |
1375 | "Retrieve available completions for INPUT using PROCESS." | |
1376 | (with-current-buffer (process-buffer process) | |
62feb915 FEG |
1377 | (let ((completions (python-shell-send-string-no-output |
1378 | (format python-shell-completion-string-code input) | |
1379 | process))) | |
1380 | (when (> (length completions) 2) | |
1381 | (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t))))) | |
075a0f61 FEG |
1382 | |
1383 | (defun python-shell-completion--get-completion (input completions) | |
1384 | "Get completion for INPUT using COMPLETIONS." | |
1385 | (let ((completion (when completions | |
1386 | (try-completion input completions)))) | |
1387 | (cond ((eq completion t) | |
1388 | input) | |
1389 | ((null completion) | |
1390 | (message "Can't find completion for \"%s\"" input) | |
1391 | (ding) | |
1392 | input) | |
1393 | ((not (string= input completion)) | |
1394 | completion) | |
1395 | (t | |
1396 | (message "Making completion list...") | |
1397 | (with-output-to-temp-buffer "*Python Completions*" | |
1398 | (display-completion-list | |
1399 | (all-completions input completions))) | |
1400 | input)))) | |
1401 | ||
45c138ac FEG |
1402 | (defun python-shell-completion-complete-at-point () |
1403 | "Perform completion at point in inferior Python process." | |
1404 | (interactive) | |
3d6913c7 FEG |
1405 | (with-syntax-table python-dotty-syntax-table |
1406 | (when (and comint-last-prompt-overlay | |
1407 | (> (point-marker) (overlay-end comint-last-prompt-overlay))) | |
1408 | (let* ((process (get-buffer-process (current-buffer))) | |
075a0f61 FEG |
1409 | (input (substring-no-properties |
1410 | (or (comint-word (current-word)) "") nil nil))) | |
1411 | (delete-char (- (length input))) | |
1412 | (insert | |
1413 | (python-shell-completion--get-completion | |
1414 | input (python-shell-completion--get-completions input process))))))) | |
45c138ac | 1415 | |
45c138ac FEG |
1416 | (defun python-shell-completion-complete-or-indent () |
1417 | "Complete or indent depending on the context. | |
e2d8d479 FEG |
1418 | If content before pointer is all whitespace indent. If not try |
1419 | to complete." | |
45c138ac FEG |
1420 | (interactive) |
1421 | (if (string-match "^[[:space:]]*$" | |
1422 | (buffer-substring (comint-line-beginning-position) | |
1423 | (point-marker))) | |
1424 | (indent-for-tab-command) | |
1425 | (comint-dynamic-complete))) | |
1426 | ||
45c138ac FEG |
1427 | \f |
1428 | ;;; PDB Track integration | |
1429 | ||
1430 | (defvar python-pdbtrack-stacktrace-info-regexp | |
1431 | "> %s(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()" | |
e2d8d479 FEG |
1432 | "Regular Expression matching stacktrace information. |
1433 | Used to extract the current line and module beign inspected. The | |
1434 | regexp should not start with a caret (^) and can contain a string | |
1435 | placeholder (\%s) which is replaced with the filename beign | |
1436 | inspected (so other files in the debugging process are not | |
45c138ac FEG |
1437 | opened)") |
1438 | ||
1439 | (defvar python-pdbtrack-tracking-buffers '() | |
1440 | "Alist containing elements of form (#<buffer> . #<buffer>). | |
1441 | The car of each element of the alist is the tracking buffer and | |
1442 | the cdr is the tracked buffer.") | |
1443 | ||
1444 | (defun python-pdbtrack-get-or-add-tracking-buffers () | |
1445 | "Get/Add a tracked buffer for the current buffer. | |
1446 | Internally it uses the `python-pdbtrack-tracking-buffers' alist. | |
1447 | Returns a cons with the form: | |
1448 | * (#<tracking buffer> . #< tracked buffer>)." | |
1449 | (or | |
1450 | (assq (current-buffer) python-pdbtrack-tracking-buffers) | |
1451 | (let* ((file (with-current-buffer (current-buffer) | |
d439cda5 | 1452 | inferior-python-mode-current-file)) |
45c138ac FEG |
1453 | (tracking-buffers |
1454 | `(,(current-buffer) . | |
1455 | ,(or (get-file-buffer file) | |
1456 | (find-file-noselect file))))) | |
1457 | (set-buffer (cdr tracking-buffers)) | |
1458 | (python-mode) | |
1459 | (set-buffer (car tracking-buffers)) | |
1460 | (setq python-pdbtrack-tracking-buffers | |
1461 | (cons tracking-buffers python-pdbtrack-tracking-buffers)) | |
1462 | tracking-buffers))) | |
1463 | ||
1464 | (defun python-pdbtrack-comint-output-filter-function (output) | |
1465 | "Move overlay arrow to current pdb line in tracked buffer. | |
1466 | Argument OUTPUT is a string with the output from the comint process." | |
1467 | (when (not (string= output "")) | |
1468 | (let ((full-output (ansi-color-filter-apply | |
1469 | (buffer-substring comint-last-input-end | |
1470 | (point-max))))) | |
1471 | (if (string-match python-shell-prompt-pdb-regexp full-output) | |
1472 | (let* ((tracking-buffers (python-pdbtrack-get-or-add-tracking-buffers)) | |
1473 | (line-num | |
1474 | (save-excursion | |
1475 | (string-match | |
1476 | (format python-pdbtrack-stacktrace-info-regexp | |
1477 | (regexp-quote | |
d439cda5 | 1478 | inferior-python-mode-current-file)) |
45c138ac FEG |
1479 | full-output) |
1480 | (string-to-number (or (match-string-no-properties 1 full-output) "")))) | |
1481 | (tracked-buffer-window (get-buffer-window (cdr tracking-buffers))) | |
1482 | (tracked-buffer-line-pos)) | |
1483 | (when line-num | |
1484 | (with-current-buffer (cdr tracking-buffers) | |
1485 | (set (make-local-variable 'overlay-arrow-string) "=>") | |
1486 | (set (make-local-variable 'overlay-arrow-position) (make-marker)) | |
1487 | (setq tracked-buffer-line-pos (progn | |
1488 | (goto-char (point-min)) | |
1489 | (forward-line (1- line-num)) | |
1490 | (point-marker))) | |
1491 | (when tracked-buffer-window | |
1492 | (set-window-point tracked-buffer-window tracked-buffer-line-pos)) | |
1493 | (set-marker overlay-arrow-position tracked-buffer-line-pos))) | |
1494 | (pop-to-buffer (cdr tracking-buffers)) | |
1495 | (switch-to-buffer-other-window (car tracking-buffers))) | |
1496 | (let ((tracking-buffers (assq (current-buffer) | |
1497 | python-pdbtrack-tracking-buffers))) | |
1498 | (when tracking-buffers | |
1499 | (if inferior-python-mode-current-file | |
1500 | (with-current-buffer (cdr tracking-buffers) | |
1501 | (set-marker overlay-arrow-position nil)) | |
1502 | (kill-buffer (cdr tracking-buffers))) | |
1503 | (setq python-pdbtrack-tracking-buffers | |
1504 | (assq-delete-all (current-buffer) | |
1505 | python-pdbtrack-tracking-buffers))))))) | |
1506 | output) | |
1507 | ||
1508 | \f | |
1509 | ;;; Symbol completion | |
1510 | ||
1511 | (defun python-completion-complete-at-point () | |
1512 | "Complete current symbol at point. | |
1513 | For this to work the best as possible you should call | |
1514 | `python-shell-send-buffer' from time to time so context in | |
1515 | inferior python process is updated properly." | |
1516 | (interactive) | |
1517 | (let ((process (python-shell-get-process))) | |
1518 | (if (not process) | |
4e531f7a | 1519 | (error "Completion needs an inferior Python process running") |
075a0f61 FEG |
1520 | (with-syntax-table python-dotty-syntax-table |
1521 | (let* ((input (substring-no-properties | |
1522 | (or (comint-word (current-word)) "") nil nil)) | |
1523 | (completions (python-shell-completion--get-completions | |
1524 | input process))) | |
1525 | (delete-char (- (length input))) | |
1526 | (insert | |
1527 | (python-shell-completion--get-completion | |
1528 | input completions))))))) | |
45c138ac FEG |
1529 | |
1530 | (add-to-list 'debug-ignored-errors "^Completion needs an inferior Python process running.") | |
1531 | ||
1532 | \f | |
1533 | ;;; Fill paragraph | |
1534 | ||
c2cb97ae FEG |
1535 | (defcustom python-fill-comment-function 'python-fill-comment |
1536 | "Function to fill comments. | |
1537 | This is the function used by `python-fill-paragraph-function' to | |
1538 | fill comments." | |
1539 | :type 'symbol | |
1540 | :group 'python | |
1541 | :safe 'symbolp) | |
1542 | ||
1543 | (defcustom python-fill-string-function 'python-fill-string | |
1544 | "Function to fill strings. | |
1545 | This is the function used by `python-fill-paragraph-function' to | |
1546 | fill strings." | |
1547 | :type 'symbol | |
1548 | :group 'python | |
1549 | :safe 'symbolp) | |
1550 | ||
1551 | (defcustom python-fill-decorator-function 'python-fill-decorator | |
1552 | "Function to fill decorators. | |
1553 | This is the function used by `python-fill-paragraph-function' to | |
1554 | fill decorators." | |
1555 | :type 'symbol | |
1556 | :group 'python | |
1557 | :safe 'symbolp) | |
1558 | ||
1559 | (defcustom python-fill-paren-function 'python-fill-paren | |
1560 | "Function to fill parens. | |
1561 | This is the function used by `python-fill-paragraph-function' to | |
1562 | fill parens." | |
1563 | :type 'symbol | |
1564 | :group 'python | |
1565 | :safe 'symbolp) | |
1566 | ||
45c138ac FEG |
1567 | (defun python-fill-paragraph-function (&optional justify) |
1568 | "`fill-paragraph-function' handling multi-line strings and possibly comments. | |
1569 | If any of the current line is in or at the end of a multi-line string, | |
1570 | fill the string or the paragraph of it that point is in, preserving | |
4e531f7a FEG |
1571 | the string's indentation. |
1572 | Optional argument JUSTIFY defines if the paragraph should be justified." | |
45c138ac FEG |
1573 | (interactive "P") |
1574 | (save-excursion | |
1575 | (back-to-indentation) | |
1576 | (cond | |
1577 | ;; Comments | |
c2cb97ae FEG |
1578 | ((funcall python-fill-comment-function justify)) |
1579 | ;; Strings/Docstrings | |
45c138ac | 1580 | ((save-excursion (skip-chars-forward "\"'uUrR") |
14a78495 | 1581 | (python-info-ppss-context 'string)) |
c2cb97ae | 1582 | (funcall python-fill-string-function justify)) |
45c138ac FEG |
1583 | ;; Decorators |
1584 | ((equal (char-after (save-excursion | |
1585 | (back-to-indentation) | |
c2cb97ae FEG |
1586 | (point-marker))) ?@) |
1587 | (funcall python-fill-decorator-function justify)) | |
45c138ac | 1588 | ;; Parens |
14a78495 | 1589 | ((or (python-info-ppss-context 'paren) |
45c138ac FEG |
1590 | (looking-at (python-rx open-paren)) |
1591 | (save-excursion | |
1592 | (skip-syntax-forward "^(" (line-end-position)) | |
1593 | (looking-at (python-rx open-paren)))) | |
c2cb97ae | 1594 | (funcall python-fill-paren-function justify)) |
45c138ac FEG |
1595 | (t t)))) |
1596 | ||
c2cb97ae | 1597 | (defun python-fill-comment (&optional justify) |
053a6c72 FEG |
1598 | "Comment fill function for `python-fill-paragraph-function'. |
1599 | JUSTIFY should be used (if applicable) as in `fill-paragraph'." | |
c2cb97ae FEG |
1600 | (fill-comment-paragraph justify)) |
1601 | ||
1602 | (defun python-fill-string (&optional justify) | |
053a6c72 FEG |
1603 | "String fill function for `python-fill-paragraph-function'. |
1604 | JUSTIFY should be used (if applicable) as in `fill-paragraph'." | |
c2cb97ae FEG |
1605 | (let ((marker (point-marker)) |
1606 | (string-start-marker | |
1607 | (progn | |
1608 | (skip-chars-forward "\"'uUrR") | |
1609 | (goto-char (python-info-ppss-context 'string)) | |
1610 | (skip-chars-forward "\"'uUrR") | |
1611 | (point-marker))) | |
1612 | (reg-start (line-beginning-position)) | |
1613 | (string-end-marker | |
1614 | (progn | |
1615 | (while (python-info-ppss-context 'string) | |
1616 | (goto-char (1+ (point-marker)))) | |
1617 | (skip-chars-backward "\"'") | |
1618 | (point-marker))) | |
1619 | (reg-end (line-end-position)) | |
1620 | (fill-paragraph-function)) | |
1621 | (save-restriction | |
1622 | (narrow-to-region reg-start reg-end) | |
1623 | (save-excursion | |
1624 | (goto-char string-start-marker) | |
1625 | (delete-region (point-marker) (progn | |
1626 | (skip-syntax-forward "> ") | |
1627 | (point-marker))) | |
1628 | (goto-char string-end-marker) | |
1629 | (delete-region (point-marker) (progn | |
1630 | (skip-syntax-backward "> ") | |
1631 | (point-marker))) | |
1632 | (save-excursion | |
1633 | (goto-char marker) | |
1634 | (fill-paragraph justify)) | |
1635 | ;; If there is a newline in the docstring lets put triple | |
1636 | ;; quote in it's own line to follow pep 8 | |
1637 | (when (save-excursion | |
1638 | (re-search-backward "\n" string-start-marker t)) | |
1639 | (newline) | |
1640 | (newline-and-indent)) | |
1641 | (fill-paragraph justify)))) t) | |
1642 | ||
1643 | (defun python-fill-decorator (&optional justify) | |
053a6c72 FEG |
1644 | "Decorator fill function for `python-fill-paragraph-function'. |
1645 | JUSTIFY should be used (if applicable) as in `fill-paragraph'." | |
c2cb97ae FEG |
1646 | t) |
1647 | ||
1648 | (defun python-fill-paren (&optional justify) | |
053a6c72 FEG |
1649 | "Paren fill function for `python-fill-paragraph-function'. |
1650 | JUSTIFY should be used (if applicable) as in `fill-paragraph'." | |
c2cb97ae FEG |
1651 | (save-restriction |
1652 | (narrow-to-region (progn | |
1653 | (while (python-info-ppss-context 'paren) | |
1654 | (goto-char (1- (point-marker)))) | |
1655 | (point-marker) | |
1656 | (line-beginning-position)) | |
1657 | (progn | |
1658 | (when (not (python-info-ppss-context 'paren)) | |
1659 | (end-of-line) | |
1660 | (when (not (python-info-ppss-context 'paren)) | |
1661 | (skip-syntax-backward "^)"))) | |
1662 | (while (python-info-ppss-context 'paren) | |
1663 | (goto-char (1+ (point-marker)))) | |
1664 | (point-marker))) | |
1665 | (let ((paragraph-start "\f\\|[ \t]*$") | |
1666 | (paragraph-separate ",") | |
1667 | (fill-paragraph-function)) | |
1668 | (goto-char (point-min)) | |
1669 | (fill-paragraph justify)) | |
1670 | (while (not (eobp)) | |
1671 | (forward-line 1) | |
1672 | (python-indent-line) | |
1673 | (goto-char (line-end-position)))) t) | |
1674 | ||
45c138ac | 1675 | \f |
e2803784 FEG |
1676 | ;;; Skeletons |
1677 | ||
1678 | (defcustom python-skeleton-autoinsert nil | |
1679 | "Non-nil means template skeletons will be automagically inserted. | |
1680 | This happens when pressing \"if<SPACE>\", for example, to prompt for | |
1681 | the if condition." | |
1682 | :type 'boolean | |
1683 | :group 'python) | |
1684 | ||
1685 | (defvar python-skeleton-available '() | |
1686 | "Internal list of available skeletons.") | |
1687 | (make-variable-buffer-local 'inferior-python-mode-current-file) | |
1688 | ||
1689 | (define-abbrev-table 'python-mode-abbrev-table () | |
1690 | "Abbrev table for Python mode." | |
1691 | :case-fixed t | |
1692 | ;; Allow / inside abbrevs. | |
1693 | :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*" | |
1694 | ;; Only expand in code. | |
1695 | :enable-function (lambda () | |
e2803784 | 1696 | (and |
14a78495 FEG |
1697 | (not (or (python-info-ppss-context 'string) |
1698 | (python-info-ppss-context 'comment))) | |
e2803784 FEG |
1699 | python-skeleton-autoinsert))) |
1700 | ||
1701 | (defmacro python-skeleton-define (name doc &rest skel) | |
1702 | "Define a `python-mode' skeleton using NAME DOC and SKEL. | |
1703 | The skeleton will be bound to python-skeleton-NAME and will | |
1704 | be added to `python-mode-abbrev-table'." | |
1705 | (let* ((name (symbol-name name)) | |
1706 | (function-name (intern (concat "python-skeleton-" name)))) | |
73ed6836 FEG |
1707 | `(progn |
1708 | (define-abbrev python-mode-abbrev-table ,name "" ',function-name) | |
1709 | (setq python-skeleton-available | |
1710 | (cons ',function-name python-skeleton-available)) | |
1711 | (define-skeleton ,function-name | |
1712 | ,(or doc | |
1713 | (format "Insert %s statement." name)) | |
1714 | ,@skel)))) | |
e2803784 FEG |
1715 | (put 'python-skeleton-define 'lisp-indent-function 2) |
1716 | ||
1717 | (defmacro python-define-auxiliary-skeleton (name doc &optional &rest skel) | |
1718 | "Define a `python-mode' auxiliary skeleton using NAME DOC and SKEL. | |
1719 | The skeleton will be bound to python-skeleton-NAME." | |
1720 | (let* ((name (symbol-name name)) | |
1721 | (function-name (intern (concat "python-skeleton--" name))) | |
1722 | (msg (format | |
1723 | "Add '%s' clause? " name))) | |
1724 | (when (not skel) | |
1725 | (setq skel | |
1726 | `(< ,(format "%s:" name) \n \n | |
1727 | > _ \n))) | |
1728 | `(define-skeleton ,function-name | |
1729 | ,(or doc | |
1730 | (format "Auxiliary skeleton for %s statement." name)) | |
1731 | nil | |
1732 | (unless (y-or-n-p ,msg) | |
1733 | (signal 'quit t)) | |
1734 | ,@skel))) | |
1735 | (put 'python-define-auxiliary-skeleton 'lisp-indent-function 2) | |
1736 | ||
1737 | (python-define-auxiliary-skeleton else nil) | |
1738 | ||
1739 | (python-define-auxiliary-skeleton except nil) | |
1740 | ||
1741 | (python-define-auxiliary-skeleton finally nil) | |
1742 | ||
1743 | (python-skeleton-define if nil | |
1744 | "Condition: " | |
1745 | "if " str ":" \n | |
1746 | _ \n | |
1747 | ("other condition, %s: " | |
1748 | < | |
1749 | "elif " str ":" \n | |
1750 | > _ \n nil) | |
1751 | '(python-skeleton--else) | ^) | |
1752 | ||
1753 | (python-skeleton-define while nil | |
1754 | "Condition: " | |
1755 | "while " str ":" \n | |
1756 | > _ \n | |
1757 | '(python-skeleton--else) | ^) | |
1758 | ||
1759 | (python-skeleton-define for nil | |
1760 | "Iteration spec: " | |
1761 | "for " str ":" \n | |
1762 | > _ \n | |
1763 | '(python-skeleton--else) | ^) | |
1764 | ||
1765 | (python-skeleton-define try nil | |
1766 | nil | |
1767 | "try:" \n | |
1768 | > _ \n | |
1769 | ("Exception, %s: " | |
1770 | < | |
1771 | "except " str ":" \n | |
1772 | > _ \n nil) | |
1773 | resume: | |
1774 | '(python-skeleton--except) | |
1775 | '(python-skeleton--else) | |
1776 | '(python-skeleton--finally) | ^) | |
1777 | ||
1778 | (python-skeleton-define def nil | |
1779 | "Function name: " | |
1780 | "def " str " (" ("Parameter, %s: " | |
1781 | (unless (equal ?\( (char-before)) ", ") | |
1782 | str) "):" \n | |
1783 | "\"\"\"" - "\"\"\"" \n | |
1784 | > _ \n) | |
1785 | ||
1786 | (python-skeleton-define class nil | |
1787 | "Class name: " | |
1788 | "class " str " (" ("Inheritance, %s: " | |
1789 | (unless (equal ?\( (char-before)) ", ") | |
1790 | str) | |
1791 | & ")" | -2 | |
1792 | ":" \n | |
1793 | "\"\"\"" - "\"\"\"" \n | |
1794 | > _ \n) | |
1795 | ||
1796 | (defun python-skeleton-add-menu-items () | |
1797 | "Add menu items to Python->Skeletons menu." | |
1798 | (let ((skeletons (sort python-skeleton-available 'string<)) | |
1799 | (items)) | |
1800 | (dolist (skeleton skeletons) | |
1801 | (easy-menu-add-item | |
1802 | nil '("Python" "Skeletons") | |
1803 | `[,(format | |
1804 | "Insert %s" (caddr (split-string (symbol-name skeleton) "-"))) | |
1805 | ,skeleton t])))) | |
1806 | \f | |
046428d3 FEG |
1807 | ;;; FFAP |
1808 | ||
1809 | (defvar python-ffap-setup-code | |
1810 | "def __FFAP_get_module_path(module): | |
1811 | try: | |
1812 | import os | |
1813 | path = __import__(module).__file__ | |
1814 | if path[-4:] == '.pyc' and os.path.exists(path[0:-1]): | |
1815 | path = path[:-1] | |
1816 | return path | |
1817 | except: | |
1818 | return ''" | |
1819 | "Python code to get a module path.") | |
1820 | ||
1821 | (defvar python-ffap-string-code | |
1822 | "__FFAP_get_module_path('''%s''')\n" | |
1823 | "Python code used to get a string with the path of a module.") | |
1824 | ||
046428d3 FEG |
1825 | (defun python-ffap-module-path (module) |
1826 | "Function for `ffap-alist' to return path for MODULE." | |
1827 | (let ((process (or | |
1828 | (and (eq major-mode 'inferior-python-mode) | |
1829 | (get-buffer-process (current-buffer))) | |
1830 | (python-shell-get-process)))) | |
1831 | (if (not process) | |
1832 | nil | |
1833 | (let ((module-file | |
9ce938be | 1834 | (python-shell-send-string-no-output |
046428d3 FEG |
1835 | (format python-ffap-string-code module) process))) |
1836 | (when module-file | |
2947016a | 1837 | (substring-no-properties module-file 1 -1)))))) |
046428d3 FEG |
1838 | |
1839 | (eval-after-load "ffap" | |
1840 | '(progn | |
1841 | (push '(python-mode . python-ffap-module-path) ffap-alist) | |
1842 | (push '(inferior-python-mode . python-ffap-module-path) ffap-alist))) | |
1843 | ||
046428d3 | 1844 | \f |
8b3e0e76 FEG |
1845 | ;;; Code check |
1846 | ||
1847 | (defvar python-check-command | |
1848 | "pychecker --stdlib" | |
1849 | "Command used to check a Python file.") | |
1850 | ||
1851 | (defvar python-check-custom-command nil | |
1852 | "Internal use.") | |
1853 | ||
1854 | (defun python-check (command) | |
1855 | "Check a Python file (default current buffer's file). | |
1856 | Runs COMMAND, a shell command, as if by `compile'. See | |
1857 | `python-check-command' for the default." | |
1858 | (interactive | |
1859 | (list (read-string "Check command: " | |
1860 | (or python-check-custom-command | |
1861 | (concat python-check-command " " | |
1862 | (shell-quote-argument | |
1863 | (or | |
1864 | (let ((name (buffer-file-name))) | |
1865 | (and name | |
1866 | (file-name-nondirectory name))) | |
1867 | ""))))))) | |
1868 | (setq python-check-custom-command command) | |
1869 | (save-some-buffers (not compilation-ask-about-save) nil) | |
1870 | (compilation-start command)) | |
1871 | ||
1872 | \f | |
45c138ac FEG |
1873 | ;;; Eldoc |
1874 | ||
1875 | (defvar python-eldoc-setup-code | |
1876 | "def __PYDOC_get_help(obj): | |
1877 | try: | |
1878 | import pydoc | |
9e662938 FEG |
1879 | if hasattr(obj, 'startswith'): |
1880 | obj = eval(obj, globals()) | |
1881 | doc = pydoc.getdoc(obj) | |
45c138ac | 1882 | except: |
9e662938 FEG |
1883 | doc = '' |
1884 | try: | |
1885 | exec('print doc') | |
1886 | except SyntaxError: | |
1887 | print(doc)" | |
45c138ac FEG |
1888 | "Python code to setup documentation retrieval.") |
1889 | ||
1890 | (defvar python-eldoc-string-code | |
9e662938 | 1891 | "__PYDOC_get_help('''%s''')\n" |
45c138ac FEG |
1892 | "Python code used to get a string with the documentation of an object.") |
1893 | ||
78334b43 | 1894 | (defun python-eldoc--get-doc-at-point (&optional force-input force-process) |
d439cda5 FEG |
1895 | "Internal implementation to get documentation at point. |
1896 | If not FORCE-INPUT is passed then what `current-word' returns | |
1897 | will be used. If not FORCE-PROCESS is passed what | |
1898 | `python-shell-get-process' returns is used." | |
78334b43 | 1899 | (let ((process (or force-process (python-shell-get-process)))) |
45c138ac FEG |
1900 | (if (not process) |
1901 | "Eldoc needs an inferior Python process running." | |
1902 | (let* ((current-defun (python-info-current-defun)) | |
78334b43 FEG |
1903 | (input (or force-input |
1904 | (with-syntax-table python-dotty-syntax-table | |
1905 | (if (not current-defun) | |
1906 | (current-word) | |
1907 | (concat current-defun "." (current-word)))))) | |
45c138ac FEG |
1908 | (ppss (syntax-ppss)) |
1909 | (help (when (and input | |
1910 | (not (string= input (concat current-defun "."))) | |
14a78495 FEG |
1911 | (not (or (python-info-ppss-context 'string ppss) |
1912 | (python-info-ppss-context 'comment ppss)))) | |
45c138ac FEG |
1913 | (when (string-match (concat |
1914 | (regexp-quote (concat current-defun ".")) | |
1915 | "self\\.") input) | |
1916 | (with-temp-buffer | |
1917 | (insert input) | |
1918 | (goto-char (point-min)) | |
1919 | (forward-word) | |
1920 | (forward-char) | |
1921 | (delete-region (point-marker) (search-forward "self.")) | |
1922 | (setq input (buffer-substring (point-min) (point-max))))) | |
9ce938be | 1923 | (python-shell-send-string-no-output |
1066882c | 1924 | (format python-eldoc-string-code input) process)))) |
45c138ac FEG |
1925 | (with-current-buffer (process-buffer process) |
1926 | (when comint-last-prompt-overlay | |
1927 | (delete-region comint-last-input-end | |
1928 | (overlay-start comint-last-prompt-overlay)))) | |
1929 | (when (and help | |
1930 | (not (string= help "\n"))) | |
1931 | help))))) | |
1932 | ||
78334b43 FEG |
1933 | (defun python-eldoc-function () |
1934 | "`eldoc-documentation-function' for Python. | |
1935 | For this to work the best as possible you should call | |
1936 | `python-shell-send-buffer' from time to time so context in | |
1937 | inferior python process is updated properly." | |
1938 | (python-eldoc--get-doc-at-point)) | |
1939 | ||
1940 | (defun python-eldoc-at-point (symbol) | |
1941 | "Get help on SYMBOL using `help'. | |
1942 | Interactively, prompt for symbol." | |
1943 | (interactive | |
1944 | (let ((symbol (with-syntax-table python-dotty-syntax-table | |
1945 | (current-word))) | |
1946 | (enable-recursive-minibuffers t)) | |
1947 | (list (read-string (if symbol | |
1948 | (format "Describe symbol (default %s): " symbol) | |
1949 | "Describe symbol: ") | |
1950 | nil nil symbol)))) | |
1951 | (let ((process (python-shell-get-process))) | |
1952 | (if (not process) | |
1953 | (message "Eldoc needs an inferior Python process running.") | |
1954 | (let ((temp-buffer-show-hook | |
1955 | (lambda () | |
1956 | (toggle-read-only 1) | |
1957 | (setq view-return-to-alist | |
1958 | (list (cons (selected-window) help-return-method)))))) | |
1959 | (with-output-to-temp-buffer (help-buffer) | |
1960 | (with-current-buffer standard-output | |
1961 | (insert | |
1962 | (python-eldoc--get-doc-at-point symbol process)) | |
1963 | (help-print-return-message))))))) | |
1964 | ||
45c138ac | 1965 | \f |
fc2dc7df FEG |
1966 | ;;; Imenu |
1967 | ||
1968 | (defcustom python-imenu-include-defun-type t | |
1969 | "Non-nil make imenu items to include its type." | |
1970 | :type 'boolean | |
1971 | :group 'python | |
1972 | :safe 'booleanp) | |
1973 | ||
c942de99 | 1974 | (defcustom python-imenu-make-tree t |
fc2dc7df FEG |
1975 | "Non-nil make imenu to build a tree menu. |
1976 | Set to nil for speed." | |
1977 | :type 'boolean | |
1978 | :group 'python | |
1979 | :safe 'booleanp) | |
1980 | ||
1981 | (defcustom python-imenu-subtree-root-label "<Jump to %s>" | |
1982 | "Label displayed to navigate to root from a subtree. | |
1983 | It can contain a \"%s\" which will be replaced with the root name." | |
1984 | :type 'string | |
1985 | :group 'python | |
1986 | :safe 'stringp) | |
1987 | ||
1988 | (defvar python-imenu-index-alist nil | |
1989 | "Calculated index tree for imenu.") | |
1990 | ||
1991 | (defun python-imenu-tree-assoc (keylist tree) | |
1992 | "Using KEYLIST traverse TREE." | |
1993 | (if keylist | |
1994 | (python-imenu-tree-assoc (cdr keylist) | |
1995 | (ignore-errors (assoc (car keylist) tree))) | |
1996 | tree)) | |
1997 | ||
1998 | (defun python-imenu-make-element-tree (element-list full-element plain-index) | |
1999 | "Make a tree from plain alist of module names. | |
2000 | ELEMENT-LIST is the defun name splitted by \".\" and FULL-ELEMENT | |
2001 | is the same thing, the difference is that FULL-ELEMENT remains | |
2002 | untouched in all recursive calls. | |
2003 | Argument PLAIN-INDEX is the calculated plain index used to build the tree." | |
2004 | (when (not (python-imenu-tree-assoc full-element python-imenu-index-alist)) | |
2005 | (when element-list | |
2006 | (let* ((subelement-point (cdr (assoc | |
2007 | (mapconcat #'identity full-element ".") | |
2008 | plain-index))) | |
2009 | (subelement-name (car element-list)) | |
c942de99 FEG |
2010 | (subelement-position (python-util-position |
2011 | subelement-name full-element)) | |
fc2dc7df FEG |
2012 | (subelement-path (when subelement-position |
2013 | (butlast | |
2014 | full-element | |
2015 | (- (length full-element) | |
2016 | subelement-position))))) | |
2017 | (let ((path-ref (python-imenu-tree-assoc subelement-path | |
2018 | python-imenu-index-alist))) | |
2019 | (if (not path-ref) | |
2020 | (push (cons subelement-name subelement-point) | |
2021 | python-imenu-index-alist) | |
2022 | (when (not (listp (cdr path-ref))) | |
2023 | ;; Modifiy root cdr to be a list | |
2024 | (setcdr path-ref | |
2025 | (list (cons (format python-imenu-subtree-root-label | |
2026 | (car path-ref)) | |
2027 | (cdr (assoc | |
2028 | (mapconcat #'identity | |
2029 | subelement-path ".") | |
2030 | plain-index)))))) | |
2031 | (when (not (assoc subelement-name path-ref)) | |
2032 | (push (cons subelement-name subelement-point) (cdr path-ref)))))) | |
2033 | (python-imenu-make-element-tree (cdr element-list) | |
2034 | full-element plain-index)))) | |
2035 | ||
2036 | (defun python-imenu-make-tree (index) | |
2037 | "Build the imenu alist tree from plain INDEX. | |
2038 | ||
2039 | The idea of this function is that given the alist: | |
2040 | ||
2041 | '((\"Test\" . 100) | |
2042 | (\"Test.__init__\" . 200) | |
2043 | (\"Test.some_method\" . 300) | |
2044 | (\"Test.some_method.another\" . 400) | |
2045 | (\"Test.something_else\" . 500) | |
2046 | (\"test\" . 600) | |
2047 | (\"test.reprint\" . 700) | |
2048 | (\"test.reprint\" . 800)) | |
2049 | ||
2050 | This tree gets built: | |
2051 | ||
2052 | '((\"Test\" . ((\"jump to...\" . 100) | |
2053 | (\"__init__\" . 200) | |
2054 | (\"some_method\" . ((\"jump to...\" . 300) | |
2055 | (\"another\" . 400))) | |
2056 | (\"something_else\" . 500))) | |
2057 | (\"test\" . ((\"jump to...\" . 600) | |
2058 | (\"reprint\" . 700) | |
2059 | (\"reprint\" . 800)))) | |
2060 | ||
2061 | Internally it uses `python-imenu-make-element-tree' to create all | |
2062 | branches for each element." | |
2063 | (setq python-imenu-index-alist nil) | |
c942de99 FEG |
2064 | (mapc (lambda (element) |
2065 | (python-imenu-make-element-tree element element index)) | |
2066 | (mapcar (lambda (element) | |
2067 | (split-string (car element) "\\." t)) index)) | |
fc2dc7df FEG |
2068 | python-imenu-index-alist) |
2069 | ||
2070 | (defun python-imenu-create-index () | |
2071 | "`imenu-create-index-function' for Python." | |
2072 | (let ((index)) | |
2073 | (goto-char (point-max)) | |
2074 | (while (python-beginning-of-defun-function 1 t) | |
2075 | (let ((defun-dotted-name | |
2076 | (python-info-current-defun python-imenu-include-defun-type))) | |
2077 | (push (cons defun-dotted-name (point)) index))) | |
2078 | (if python-imenu-make-tree | |
2079 | (python-imenu-make-tree index) | |
2080 | index))) | |
2081 | ||
2082 | \f | |
45c138ac FEG |
2083 | ;;; Misc helpers |
2084 | ||
fc2dc7df | 2085 | (defun python-info-current-defun (&optional include-type) |
45c138ac | 2086 | "Return name of surrounding function with Python compatible dotty syntax. |
fc2dc7df | 2087 | Optional argument INCLUDE-TYPE indicates to include the type of the defun. |
45c138ac FEG |
2088 | This function is compatible to be used as |
2089 | `add-log-current-defun-function' since it returns nil if point is | |
2090 | not inside a defun." | |
6b432853 FEG |
2091 | (let ((names '()) |
2092 | (min-indent)) | |
45c138ac FEG |
2093 | (save-restriction |
2094 | (widen) | |
2095 | (save-excursion | |
6b432853 | 2096 | (goto-char (line-end-position)) |
327f0e84 | 2097 | (forward-comment -1) |
fc2dc7df | 2098 | (while (python-beginning-of-defun-function 1 t) |
6b432853 FEG |
2099 | (when (or (not min-indent) |
2100 | (< (current-indentation) min-indent)) | |
2101 | (setq min-indent (current-indentation)) | |
af5c1beb | 2102 | (looking-at python-nav-beginning-of-defun-regexp) |
fc2dc7df FEG |
2103 | (setq names (cons |
2104 | (if (not include-type) | |
2105 | (match-string-no-properties 1) | |
2106 | (mapconcat 'identity | |
2107 | (split-string | |
2108 | (match-string-no-properties 0)) " ")) | |
2109 | names)))))) | |
45c138ac FEG |
2110 | (when names |
2111 | (mapconcat (lambda (string) string) names ".")))) | |
2112 | ||
2113 | (defun python-info-closing-block () | |
e2d8d479 | 2114 | "Return the point of the block the current line closes." |
45c138ac FEG |
2115 | (let ((closing-word (save-excursion |
2116 | (back-to-indentation) | |
2117 | (current-word))) | |
2118 | (indentation (current-indentation))) | |
2119 | (when (member closing-word python-indent-dedenters) | |
2120 | (save-excursion | |
2121 | (forward-line -1) | |
2122 | (while (and (> (current-indentation) indentation) | |
2123 | (not (bobp)) | |
2124 | (not (back-to-indentation)) | |
2125 | (forward-line -1))) | |
2126 | (back-to-indentation) | |
2127 | (cond | |
2128 | ((not (equal indentation (current-indentation))) nil) | |
2129 | ((string= closing-word "elif") | |
2130 | (when (member (current-word) '("if" "elif")) | |
2131 | (point-marker))) | |
2132 | ((string= closing-word "else") | |
2133 | (when (member (current-word) '("if" "elif" "except" "for" "while")) | |
2134 | (point-marker))) | |
2135 | ((string= closing-word "except") | |
2136 | (when (member (current-word) '("try")) | |
2137 | (point-marker))) | |
2138 | ((string= closing-word "finally") | |
2139 | (when (member (current-word) '("except" "else")) | |
2140 | (point-marker)))))))) | |
2141 | ||
2142 | (defun python-info-line-ends-backslash-p () | |
2143 | "Return non-nil if current line ends with backslash." | |
2144 | (string= (or (ignore-errors | |
2145 | (buffer-substring | |
2146 | (line-end-position) | |
2147 | (- (line-end-position) 1))) "") "\\")) | |
2148 | ||
2149 | (defun python-info-continuation-line-p () | |
2150 | "Return non-nil if current line is continuation of another." | |
2151 | (or (python-info-line-ends-backslash-p) | |
2152 | (string-match ",[[:space:]]*$" (buffer-substring | |
2153 | (line-beginning-position) | |
2154 | (line-end-position))) | |
2155 | (save-excursion | |
2156 | (let ((innermost-paren (progn | |
2157 | (goto-char (line-end-position)) | |
14a78495 | 2158 | (python-info-ppss-context 'paren)))) |
45c138ac FEG |
2159 | (when (and innermost-paren |
2160 | (and (<= (line-beginning-position) innermost-paren) | |
2161 | (>= (line-end-position) innermost-paren))) | |
2162 | (goto-char innermost-paren) | |
2163 | (looking-at (python-rx open-paren (* space) line-end))))) | |
2164 | (save-excursion | |
2165 | (back-to-indentation) | |
14a78495 | 2166 | (python-info-ppss-context 'paren)))) |
45c138ac FEG |
2167 | |
2168 | (defun python-info-block-continuation-line-p () | |
2169 | "Return non-nil if current line is a continuation of a block." | |
2170 | (save-excursion | |
2171 | (while (and (not (bobp)) | |
2172 | (python-info-continuation-line-p)) | |
2173 | (forward-line -1)) | |
2174 | (forward-line 1) | |
2175 | (back-to-indentation) | |
2176 | (when (looking-at (python-rx block-start)) | |
2177 | (point-marker)))) | |
2178 | ||
2179 | (defun python-info-assignment-continuation-line-p () | |
2180 | "Return non-nil if current line is a continuation of an assignment." | |
2181 | (save-excursion | |
2182 | (while (and (not (bobp)) | |
2183 | (python-info-continuation-line-p)) | |
2184 | (forward-line -1)) | |
2185 | (forward-line 1) | |
2186 | (back-to-indentation) | |
2187 | (when (and (not (looking-at (python-rx block-start))) | |
2188 | (save-excursion | |
2189 | (and (re-search-forward (python-rx not-simple-operator | |
2190 | assignment-operator | |
2191 | not-simple-operator) | |
2192 | (line-end-position) t) | |
14a78495 | 2193 | (not (or (python-info-ppss-context 'string) |
9f1537ef | 2194 | (python-info-ppss-context 'paren) |
14a78495 | 2195 | (python-info-ppss-context 'comment)))))) |
45c138ac FEG |
2196 | (point-marker)))) |
2197 | ||
14a78495 FEG |
2198 | (defun python-info-ppss-context (type &optional syntax-ppss) |
2199 | "Return non-nil if point is on TYPE using SYNTAX-PPSS. | |
2200 | TYPE can be 'comment, 'string or 'parent. It returns the start | |
2201 | character address of the specified TYPE." | |
2202 | (let ((ppss (or syntax-ppss (syntax-ppss)))) | |
2203 | (case type | |
2204 | ('comment | |
2205 | (and (nth 4 ppss) | |
2206 | (nth 8 ppss))) | |
2207 | ('string | |
2208 | (nth 8 ppss)) | |
2209 | ('paren | |
2210 | (nth 1 ppss)) | |
2211 | (t nil)))) | |
2212 | ||
45c138ac | 2213 | \f |
c942de99 FEG |
2214 | ;;; Utility functions |
2215 | ||
2216 | ;; Stolen from GNUS | |
2217 | (defun python-util-merge (type list1 list2 pred) | |
2218 | "Destructively merge lists LIST1 and LIST2 to produce a new list. | |
2219 | Argument TYPE is for compatibility and ignored. | |
2220 | Ordering of the elements is preserved according to PRED, a `less-than' | |
2221 | predicate on the elements." | |
2222 | (let ((res nil)) | |
2223 | (while (and list1 list2) | |
2224 | (if (funcall pred (car list2) (car list1)) | |
2225 | (push (pop list2) res) | |
2226 | (push (pop list1) res))) | |
2227 | (nconc (nreverse res) list1 list2))) | |
2228 | ||
2229 | (defun python-util-position (item seq) | |
2230 | "Find the first occurrence of ITEM in SEQ. | |
2231 | Return the index of the matching item, or nil if not found." | |
2232 | (let ((member-result (member item seq))) | |
2233 | (when member-result | |
2234 | (- (length seq) (length member-result))))) | |
2235 | ||
2236 | \f | |
45c138ac FEG |
2237 | ;;;###autoload |
2238 | (define-derived-mode python-mode fundamental-mode "Python" | |
e2d8d479 FEG |
2239 | "Major mode for editing Python files. |
2240 | ||
2241 | \\{python-mode-map} | |
2242 | Entry to this mode calls the value of `python-mode-hook' | |
2243 | if that value is non-nil." | |
45c138ac FEG |
2244 | (set (make-local-variable 'tab-width) 8) |
2245 | (set (make-local-variable 'indent-tabs-mode) nil) | |
2246 | ||
2247 | (set (make-local-variable 'comment-start) "# ") | |
2248 | (set (make-local-variable 'comment-start-skip) "#+\\s-*") | |
2249 | ||
2250 | (set (make-local-variable 'parse-sexp-lookup-properties) t) | |
2251 | (set (make-local-variable 'parse-sexp-ignore-comments) t) | |
2252 | ||
2253 | (set (make-local-variable 'font-lock-defaults) | |
2254 | '(python-font-lock-keywords | |
2255 | nil nil nil nil | |
2256 | (font-lock-syntactic-keywords . python-font-lock-syntactic-keywords))) | |
2257 | ||
2258 | (set (make-local-variable 'indent-line-function) #'python-indent-line-function) | |
2259 | (set (make-local-variable 'indent-region-function) #'python-indent-region) | |
2260 | ||
2261 | (set (make-local-variable 'paragraph-start) "\\s-*$") | |
2262 | (set (make-local-variable 'fill-paragraph-function) 'python-fill-paragraph-function) | |
2263 | ||
2264 | (set (make-local-variable 'beginning-of-defun-function) | |
2265 | #'python-beginning-of-defun-function) | |
2266 | (set (make-local-variable 'end-of-defun-function) | |
2267 | #'python-end-of-defun-function) | |
2268 | ||
2269 | (add-hook 'completion-at-point-functions | |
2270 | 'python-completion-complete-at-point nil 'local) | |
2271 | ||
fc2dc7df FEG |
2272 | (setq imenu-create-index-function #'python-imenu-create-index) |
2273 | ||
45c138ac FEG |
2274 | (set (make-local-variable 'add-log-current-defun-function) |
2275 | #'python-info-current-defun) | |
2276 | ||
e2803784 FEG |
2277 | (set (make-local-variable 'skeleton-further-elements) |
2278 | '((abbrev-mode nil) | |
2279 | (< '(backward-delete-char-untabify (min python-indent-offset | |
2280 | (current-column)))) | |
2281 | (^ '(- (1+ (current-indentation)))))) | |
2282 | ||
45c138ac FEG |
2283 | (set (make-local-variable 'eldoc-documentation-function) |
2284 | #'python-eldoc-function) | |
2285 | ||
2286 | (add-to-list 'hs-special-modes-alist | |
2287 | `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#" | |
2288 | ,(lambda (arg) | |
2289 | (python-end-of-defun-function)) nil)) | |
2290 | ||
2291 | (set (make-local-variable 'outline-regexp) | |
2292 | (python-rx (* space) block-start)) | |
2293 | (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n") | |
2294 | (set (make-local-variable 'outline-level) | |
2295 | #'(lambda () | |
2296 | "`outline-level' function for Python mode." | |
2297 | (1+ (/ (current-indentation) python-indent-offset)))) | |
2298 | ||
e2803784 FEG |
2299 | (python-skeleton-add-menu-items) |
2300 | ||
45c138ac FEG |
2301 | (when python-indent-guess-indent-offset |
2302 | (python-indent-guess-indent-offset))) | |
2303 | ||
2304 | ||
2305 | (provide 'python) | |
2306 | ;;; python.el ends here |