Commit | Line | Data |
---|---|---|
45c138ac FEG |
1 | ;;; python.el -- Python's flying circus support for Emacs |
2 | ||
3 | ;; Copyright (C) 2010 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Fabián E. Gallina <fabian@anue.biz> | |
6 | ;; Maintainer: FSF | |
7 | ;; Created: Jul 2010 | |
8 | ;; Keywords: languages | |
9 | ||
10 | ;; This file is NOT part of GNU Emacs. | |
11 | ||
12 | ;; python.el is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; python.el is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with python.el. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Major mode for editing Python files with some fontification and | |
28 | ;; indentation bits extracted from original Dave Love's python.el | |
29 | ;; found in GNU/Emacs. | |
30 | ||
31 | ;; While it probably has less features than Dave Love's python.el and | |
32 | ;; PSF's python-mode.el it provides the main stuff you'll need while | |
33 | ;; keeping it simple :) | |
34 | ||
35 | ;; Implements Syntax highlighting, Indentation, Movement, Shell | |
36 | ;; interaction, Shell completion, Pdb tracking, Symbol completion, | |
37 | ;; Eldoc. | |
38 | ||
39 | ;; Syntax highlighting: Fontification of code is provided and supports | |
40 | ;; python's triple quoted strings properly. | |
41 | ||
42 | ;; Indentation: Automatic indentation with indentation cycling is | |
43 | ;; provided, it allows you to navigate different available levels of | |
44 | ;; indentation by hitting <tab> several times. | |
45 | ||
46 | ;; Movement: `beginning-of-defun' and `end-of-defun' functions are | |
47 | ;; properly implemented. A `beginning-of-innermost-defun' is defined | |
48 | ;; to navigate nested defuns. | |
49 | ||
50 | ;; Shell interaction: is provided and allows you easily execute any | |
51 | ;; block of code of your current buffer in an inferior Python process. | |
52 | ||
53 | ;; Shell completion: hitting tab will try to complete the current | |
4e531f7a | 54 | ;; word. Shell completion is implemented in a manner that if you |
45c138ac FEG |
55 | ;; change the `python-shell-interpreter' to any other (for example |
56 | ;; IPython) it should be easy to integrate another way to calculate | |
57808175 | 57 | ;; completions. You just need to specify your custom |
45c138ac FEG |
58 | ;; `python-shell-completion-setup-code' and |
59 | ;; `python-shell-completion-strings-code' | |
60 | ||
61 | ;; Pdb tracking: when you execute a block of code that contains some | |
62 | ;; call to pdb (or ipdb) it will prompt the block of code and will | |
63 | ;; follow the execution of pdb marking the current line with an arrow. | |
64 | ||
4e531f7a | 65 | ;; Symbol completion: you can complete the symbol at point. It uses |
45c138ac FEG |
66 | ;; the shell completion in background so you should run |
67 | ;; `python-shell-send-buffer' from time to time to get better results. | |
68 | ||
2947016a FEG |
69 | ;; FFAP: You can find the filename for a given module when using ffap |
70 | ;; out of the box. This feature needs an inferior python shell | |
71 | ;; running. | |
72 | ||
8b3e0e76 FEG |
73 | ;; Code check: Check the current file for errors using |
74 | ;; `python-check-command' | |
75 | ||
45c138ac | 76 | ;; Eldoc: returns documentation for object at point by using the |
4e531f7a | 77 | ;; inferior python subprocess to inspect its documentation. As you |
45c138ac FEG |
78 | ;; might guessed you should run `python-shell-send-buffer' from time |
79 | ;; to time to get better results too. | |
80 | ||
57808175 FEG |
81 | ;; If you used python-mode.el you probably will miss auto-indentation |
82 | ;; when inserting newlines. To achieve the same behavior you have | |
83 | ;; two options: | |
84 | ||
85 | ;; 1) Use GNU/Emacs' standard binding for `newline-and-indent': C-j. | |
86 | ||
87 | ;; 2) Add the following hook in your .emacs: | |
88 | ||
89 | ;; (add-hook 'python-mode-hook | |
90 | ;; #'(lambda () | |
91 | ;; (define-key python-mode-map "\C-m" 'newline-and-indent))) | |
92 | ||
93 | ;; I'd recommend the first one since you'll get the same behavior for | |
94 | ;; all modes out-of-the-box. | |
95 | ||
45c138ac FEG |
96 | ;;; Installation: |
97 | ||
98 | ;; Add this to your .emacs: | |
99 | ||
100 | ;; (add-to-list 'load-path "/folder/containing/file") | |
101 | ;; (require 'python) | |
102 | ||
103 | ;;; TODO: | |
104 | ||
105 | ;; Ordered by priority: | |
106 | ||
107 | ;; Better decorator support for beginning of defun | |
108 | ||
db1497be | 109 | ;; Review code and cleanup |
45c138ac | 110 | |
db1497be | 111 | ;; (Perhaps) some skeletons (I never use them because of yasnippet) |
45c138ac FEG |
112 | |
113 | ;;; Code: | |
114 | ||
115 | (require 'comint) | |
116 | (require 'ansi-color) | |
117 | (require 'outline) | |
118 | ||
119 | (eval-when-compile | |
120 | (require 'cl)) | |
121 | ||
122 | (autoload 'comint-mode "comint") | |
123 | ||
124 | ;;;###autoload | |
125 | (add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode)) | |
126 | ;;;###autoload | |
127 | (add-to-list 'interpreter-mode-alist (cons (purecopy "python") 'python-mode)) | |
128 | ||
129 | (defgroup python nil | |
130 | "Python Language's flying circus support for Emacs." | |
131 | :group 'languages | |
132 | :version "23.2" | |
133 | :link '(emacs-commentary-link "python")) | |
134 | ||
135 | \f | |
136 | ;;; Bindings | |
137 | ||
138 | (defvar python-mode-map | |
139 | (let ((map (make-sparse-keymap))) | |
140 | ;; Indent specific | |
141 | (define-key map "\177" 'python-indent-dedent-line-backspace) | |
142 | (define-key map (kbd "<backtab>") 'python-indent-dedent-line) | |
143 | (define-key map "\C-c<" 'python-indent-shift-left) | |
144 | (define-key map "\C-c>" 'python-indent-shift-right) | |
145 | ;; Shell interaction | |
146 | (define-key map "\C-c\C-s" 'python-shell-send-string) | |
147 | (define-key map "\C-c\C-r" 'python-shell-send-region) | |
148 | (define-key map "\C-\M-x" 'python-shell-send-defun) | |
149 | (define-key map "\C-c\C-c" 'python-shell-send-buffer) | |
150 | (define-key map "\C-c\C-l" 'python-shell-send-file) | |
151 | (define-key map "\C-c\C-z" 'python-shell-switch-to-shell) | |
8b3e0e76 FEG |
152 | ;; Some util commands |
153 | (define-key map "\C-c\C-v" 'python-check) | |
78334b43 | 154 | (define-key map "\C-c\C-f" 'python-eldoc-at-point) |
45c138ac FEG |
155 | ;; Utilities |
156 | (substitute-key-definition 'complete-symbol 'completion-at-point | |
157 | map global-map) | |
158 | (easy-menu-define python-menu map "Python Mode menu" | |
159 | `("Python" | |
160 | :help "Python-specific Features" | |
161 | ["Shift region left" python-indent-shift-left :active mark-active | |
162 | :help "Shift region left by a single indentation step"] | |
163 | ["Shift region right" python-indent-shift-right :active mark-active | |
164 | :help "Shift region right by a single indentation step"] | |
165 | "-" | |
166 | ["Mark def/class" mark-defun | |
167 | :help "Mark outermost definition around point"] | |
168 | "-" | |
169 | ["Start of def/class" beginning-of-defun | |
170 | :help "Go to start of outermost definition around point"] | |
171 | ["Start of def/class" python-beginning-of-innermost-defun | |
172 | :help "Go to start of innermost definition around point"] | |
173 | ["End of def/class" end-of-defun | |
174 | :help "Go to end of definition around point"] | |
175 | "-" | |
176 | ["Start interpreter" run-python | |
177 | :help "Run inferior Python process in a separate buffer"] | |
178 | ["Switch to shell" python-shell-switch-to-shell | |
179 | :help "Switch to running inferior Python process"] | |
180 | ["Eval string" python-shell-send-string | |
181 | :help "Eval string in inferior Python session"] | |
182 | ["Eval buffer" python-shell-send-buffer | |
183 | :help "Eval buffer in inferior Python session"] | |
184 | ["Eval region" python-shell-send-region | |
185 | :help "Eval region in inferior Python session"] | |
186 | ["Eval defun" python-shell-send-defun | |
187 | :help "Eval defun in inferior Python session"] | |
188 | ["Eval file" python-shell-send-file | |
189 | :help "Eval file in inferior Python session"] | |
190 | ["Debugger" pdb :help "Run pdb under GUD"] | |
191 | "-" | |
8b3e0e76 FEG |
192 | ["Check file" python-check |
193 | :help "Check file for errors"] | |
78334b43 FEG |
194 | ["Help on symbol" python-eldoc-at-point |
195 | :help "Get help on symbol at point"] | |
45c138ac FEG |
196 | ["Complete symbol" completion-at-point |
197 | :help "Complete symbol before point"])) | |
198 | map) | |
199 | "Keymap for `python-mode'.") | |
200 | ||
201 | \f | |
202 | ;;; Python specialized rx | |
203 | ||
204 | (defconst python-rx-constituents | |
205 | (list | |
206 | `(block-start . ,(rx symbol-start | |
207 | (or "def" "class" "if" "elif" "else" "try" | |
208 | "except" "finally" "for" "while" "with") | |
209 | symbol-end)) | |
210 | `(defun . ,(rx symbol-start (or "def" "class") symbol-end)) | |
211 | `(open-paren . ,(rx (or "{" "[" "("))) | |
212 | `(close-paren . ,(rx (or "}" "]" ")"))) | |
213 | `(simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))) | |
214 | `(not-simple-operator . ,(rx (not (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))) | |
215 | `(operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">" | |
216 | "=" "%" "**" "//" "<<" ">>" "<=" "!=" | |
217 | "==" ">=" "is" "not"))) | |
218 | `(assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**=" | |
219 | ">>=" "<<=" "&=" "^=" "|="))))) | |
220 | ||
221 | (defmacro python-rx (&rest regexps) | |
4e531f7a | 222 | "Python mode especialized rx macro which supports common python named REGEXPS." |
45c138ac FEG |
223 | (let ((rx-constituents (append python-rx-constituents rx-constituents))) |
224 | (cond ((null regexps) | |
225 | (error "No regexp")) | |
226 | ((cdr regexps) | |
227 | (rx-to-string `(and ,@regexps) t)) | |
228 | (t | |
229 | (rx-to-string (car regexps) t))))) | |
230 | ||
231 | \f | |
232 | ;;; Font-lock and syntax | |
233 | ||
234 | (defvar python-font-lock-keywords | |
235 | ;; Keywords | |
236 | `(,(rx symbol-start | |
237 | (or "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with" | |
238 | "assert" "else" "if" "pass" "yield" "break" "except" "import" | |
239 | "print" "class" "exec" "in" "raise" "continue" "finally" "is" | |
240 | "return" "def" "for" "lambda" "try" "self") | |
241 | symbol-end) | |
242 | ;; functions | |
243 | (,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_)))) | |
244 | (1 font-lock-function-name-face)) | |
245 | ;; classes | |
246 | (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_)))) | |
247 | (1 font-lock-type-face)) | |
248 | ;; Constants | |
249 | (,(rx symbol-start (group "None" symbol-end)) | |
250 | (1 font-lock-constant-face)) | |
251 | ;; Decorators. | |
252 | (,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_)) | |
253 | (0+ "." (1+ (or word ?_))))) | |
254 | (1 font-lock-type-face)) | |
255 | ;; Builtin Exceptions | |
256 | (,(rx symbol-start | |
257 | (or "ArithmeticError" "AssertionError" "AttributeError" | |
258 | "BaseException" "BufferError" "BytesWarning" "DeprecationWarning" | |
259 | "EOFError" "EnvironmentError" "Exception" "FloatingPointError" | |
260 | "FutureWarning" "GeneratorExit" "IOError" "ImportError" | |
261 | "ImportWarning" "IndentationError" "IndexError" "KeyError" | |
262 | "KeyboardInterrupt" "LookupError" "MemoryError" "NameError" | |
263 | "NotImplemented" "NotImplementedError" "OSError" "OverflowError" | |
264 | "PendingDeprecationWarning" "ReferenceError" "RuntimeError" | |
265 | "RuntimeWarning" "StandardError" "StopIteration" "SyntaxError" | |
266 | "SyntaxWarning" "SystemError" "SystemExit" "TabError" "TypeError" | |
267 | "UnboundLocalError" "UnicodeDecodeError" "UnicodeEncodeError" | |
268 | "UnicodeError" "UnicodeTranslateError" "UnicodeWarning" | |
269 | "UserWarning" "ValueError" "Warning" "ZeroDivisionError") | |
270 | symbol-end) . font-lock-type-face) | |
271 | ;; Builtins | |
272 | (,(rx (or line-start (not (any ". \t"))) (* (any " \t")) symbol-start | |
273 | (group | |
274 | (or "_" "__debug__" "__doc__" "__import__" "__name__" "__package__" | |
275 | "abs" "all" "any" "apply" "basestring" "bin" "bool" "buffer" | |
276 | "bytearray" "bytes" "callable" "chr" "classmethod" "cmp" "coerce" | |
277 | "compile" "complex" "copyright" "credits" "delattr" "dict" "dir" | |
278 | "divmod" "enumerate" "eval" "execfile" "exit" "file" "filter" | |
279 | "float" "format" "frozenset" "getattr" "globals" "hasattr" "hash" | |
280 | "help" "hex" "id" "input" "int" "intern" "isinstance" "issubclass" | |
281 | "iter" "len" "license" "list" "locals" "long" "map" "max" "min" | |
282 | "next" "object" "oct" "open" "ord" "pow" "print" "property" "quit" | |
283 | "range" "raw_input" "reduce" "reload" "repr" "reversed" "round" | |
284 | "set" "setattr" "slice" "sorted" "staticmethod" "str" "sum" | |
285 | "super" "tuple" "type" "unichr" "unicode" "vars" "xrange" "zip" | |
286 | "True" "False" "Ellipsis")) symbol-end) | |
287 | (1 font-lock-builtin-face)) | |
288 | ;; asignations | |
289 | ;; support for a = b = c = 5 | |
290 | (,(lambda (limit) | |
d8e594db FEG |
291 | (let ((re (python-rx (group (+ (any word ?. ?_))) |
292 | (? ?\[ (+ (not (any ?\]))) ?\]) (* space) | |
45c138ac FEG |
293 | assignment-operator))) |
294 | (when (re-search-forward re limit t) | |
295 | (while (and (not (equal (nth 0 (syntax-ppss)) 0)) | |
296 | (re-search-forward re limit t))) | |
534e2438 FEG |
297 | (if (and (equal (nth 0 (syntax-ppss)) 0) |
298 | (not (equal (char-after (point-marker)) ?=))) | |
45c138ac FEG |
299 | t |
300 | (set-match-data nil))))) | |
301 | (1 font-lock-variable-name-face nil nil)) | |
302 | ;; support for a, b, c = (1, 2, 3) | |
303 | (,(lambda (limit) | |
304 | (let ((re (python-rx (group (+ (any word ?. ?_))) (* space) | |
305 | (* ?, (* space) (+ (any word ?. ?_)) (* space)) | |
306 | ?, (* space) (+ (any word ?. ?_)) (* space) | |
307 | assignment-operator))) | |
308 | (when (and (re-search-forward re limit t) | |
309 | (goto-char (nth 3 (match-data)))) | |
310 | (while (and (not (equal (nth 0 (syntax-ppss)) 0)) | |
311 | (re-search-forward re limit t)) | |
312 | (goto-char (nth 3 (match-data)))) | |
313 | (if (equal (nth 0 (syntax-ppss)) 0) | |
314 | t | |
315 | (set-match-data nil))))) | |
316 | (1 font-lock-variable-name-face nil nil)))) | |
317 | ||
318 | ;; Fixme: Is there a better way? | |
319 | (defconst python-font-lock-syntactic-keywords | |
320 | ;; First avoid a sequence preceded by an odd number of backslashes. | |
321 | `((,(rx (not (any ?\\)) | |
322 | ?\\ (* (and ?\\ ?\\)) | |
323 | (group (syntax string-quote)) | |
324 | (backref 1) | |
325 | (group (backref 1))) | |
326 | (2 ,(string-to-syntax "\""))) ; dummy | |
327 | (,(rx (group (optional (any "uUrR"))) ; prefix gets syntax property | |
328 | (optional (any "rR")) ; possible second prefix | |
329 | (group (syntax string-quote)) ; maybe gets property | |
330 | (backref 2) ; per first quote | |
331 | (group (backref 2))) ; maybe gets property | |
332 | (1 (python-quote-syntax 1)) | |
333 | (2 (python-quote-syntax 2)) | |
334 | (3 (python-quote-syntax 3)))) | |
335 | "Make outer chars of triple-quote strings into generic string delimiters.") | |
336 | ||
337 | (defun python-quote-syntax (n) | |
338 | "Put `syntax-table' property correctly on triple quote. | |
339 | Used for syntactic keywords. N is the match number (1, 2 or 3)." | |
340 | ;; Given a triple quote, we have to check the context to know | |
341 | ;; whether this is an opening or closing triple or whether it's | |
342 | ;; quoted anyhow, and should be ignored. (For that we need to do | |
343 | ;; the same job as `syntax-ppss' to be correct and it seems to be OK | |
344 | ;; to use it here despite initial worries.) We also have to sort | |
345 | ;; out a possible prefix -- well, we don't _have_ to, but I think it | |
346 | ;; should be treated as part of the string. | |
347 | ||
348 | ;; Test cases: | |
349 | ;; ur"""ar""" x='"' # """ | |
350 | ;; x = ''' """ ' a | |
351 | ;; ''' | |
352 | ;; x '"""' x """ \"""" x | |
353 | (save-excursion | |
354 | (goto-char (match-beginning 0)) | |
355 | (cond | |
356 | ;; Consider property for the last char if in a fenced string. | |
357 | ((= n 3) | |
358 | (let* ((font-lock-syntactic-keywords nil) | |
359 | (syntax (syntax-ppss))) | |
360 | (when (eq t (nth 3 syntax)) ; after unclosed fence | |
361 | (goto-char (nth 8 syntax)) ; fence position | |
362 | (skip-chars-forward "uUrR") ; skip any prefix | |
363 | ;; Is it a matching sequence? | |
364 | (if (eq (char-after) (char-after (match-beginning 2))) | |
365 | (eval-when-compile (string-to-syntax "|")))))) | |
366 | ;; Consider property for initial char, accounting for prefixes. | |
367 | ((or (and (= n 2) ; leading quote (not prefix) | |
368 | (= (match-beginning 1) (match-end 1))) ; prefix is null | |
369 | (and (= n 1) ; prefix | |
370 | (/= (match-beginning 1) (match-end 1)))) ; non-empty | |
371 | (let ((font-lock-syntactic-keywords nil)) | |
372 | (unless (eq 'string (syntax-ppss-context (syntax-ppss))) | |
373 | (eval-when-compile (string-to-syntax "|"))))) | |
374 | ;; Otherwise (we're in a non-matching string) the property is | |
375 | ;; nil, which is OK. | |
376 | ))) | |
377 | ||
378 | (defvar python-mode-syntax-table | |
379 | (let ((table (make-syntax-table))) | |
380 | ;; Give punctuation syntax to ASCII that normally has symbol | |
381 | ;; syntax or has word syntax and isn't a letter. | |
382 | (let ((symbol (string-to-syntax "_")) | |
383 | (sst (standard-syntax-table))) | |
384 | (dotimes (i 128) | |
385 | (unless (= i ?_) | |
386 | (if (equal symbol (aref sst i)) | |
387 | (modify-syntax-entry i "." table))))) | |
388 | (modify-syntax-entry ?$ "." table) | |
389 | (modify-syntax-entry ?% "." table) | |
390 | ;; exceptions | |
391 | (modify-syntax-entry ?# "<" table) | |
392 | (modify-syntax-entry ?\n ">" table) | |
393 | (modify-syntax-entry ?' "\"" table) | |
394 | (modify-syntax-entry ?` "$" table) | |
395 | table) | |
396 | "Syntax table for Python files.") | |
397 | ||
398 | (defvar python-dotty-syntax-table | |
399 | (let ((table (make-syntax-table python-mode-syntax-table))) | |
400 | (modify-syntax-entry ?. "w" table) | |
401 | (modify-syntax-entry ?_ "w" table) | |
402 | table) | |
403 | "Dotty syntax table for Python files. | |
404 | It makes underscores and dots word constituent chars.") | |
405 | ||
406 | \f | |
407 | ;;; Indentation | |
408 | ||
409 | (defcustom python-indent-offset 4 | |
410 | "Default indentation offset for Python." | |
411 | :group 'python | |
412 | :type 'integer | |
413 | :safe 'integerp) | |
414 | ||
415 | (defcustom python-indent-guess-indent-offset t | |
416 | "Non-nil tells Python mode to guess `python-indent-offset' value." | |
417 | :type 'boolean | |
418 | :group 'python) | |
419 | ||
420 | (defvar python-indent-current-level 0 | |
421 | "Current indentation level `python-indent-line-function' is using.") | |
422 | ||
423 | (defvar python-indent-levels '(0) | |
424 | "Levels of indentation available for `python-indent-line-function'.") | |
425 | ||
426 | (defvar python-indent-dedenters '("else" "elif" "except" "finally") | |
427 | "List of words that should be dedented. | |
428 | These make `python-indent-calculate-indentation' subtract the value of | |
429 | `python-indent-offset'.") | |
430 | ||
431 | (defun python-indent-guess-indent-offset () | |
954aa7bd | 432 | "Guess and set `python-indent-offset' for the current buffer." |
bbac1eb8 FEG |
433 | (save-excursion |
434 | (save-restriction | |
435 | (widen) | |
436 | (goto-char (point-min)) | |
437 | (let ((found-block)) | |
438 | (while (and (not found-block) | |
439 | (re-search-forward | |
440 | (python-rx line-start block-start) nil t)) | |
441 | (when (and (not (syntax-ppss-context (syntax-ppss))) | |
442 | (progn | |
443 | (goto-char (line-end-position)) | |
444 | (forward-comment -1) | |
445 | (eq ?: (char-before)))) | |
446 | (setq found-block t))) | |
447 | (if (not found-block) | |
448 | (message "Can't guess python-indent-offset, using defaults: %s" | |
449 | python-indent-offset) | |
450 | (while (and (progn | |
451 | (goto-char (line-end-position)) | |
452 | (python-info-continuation-line-p)) | |
453 | (not (eobp))) | |
454 | (forward-line 1)) | |
455 | (forward-line 1) | |
456 | (forward-comment 1) | |
14d9f80c FEG |
457 | (let ((indent-offset (current-indentation))) |
458 | (when (> indent-offset 0) | |
459 | (setq python-indent-offset indent-offset)))))))) | |
45c138ac FEG |
460 | |
461 | (defun python-indent-context (&optional stop) | |
462 | "Return information on indentation context. | |
463 | Optional argument STOP serves to stop recursive calls. | |
464 | ||
465 | Returns a cons with the form: | |
466 | ||
467 | \(STATUS . START) | |
468 | ||
469 | Where status can be any of the following symbols: | |
470 | ||
471 | * inside-paren: If point in between (), {} or [] | |
472 | * inside-string: If point is inside a string | |
473 | * after-backslash: Previous line ends in a backslash | |
474 | * after-beginning-of-block: Point is after beginning of block | |
475 | * after-line: Point is after normal line | |
476 | * no-indent: Point is at beginning of buffer or other special case | |
477 | ||
478 | START is the buffer position where the sexp starts." | |
479 | (save-restriction | |
480 | (widen) | |
481 | (let ((ppss (save-excursion (beginning-of-line) (syntax-ppss))) | |
482 | (start)) | |
483 | (cons | |
484 | (cond | |
69bab1de | 485 | ;; Beginning of buffer |
19b122e4 FEG |
486 | ((save-excursion |
487 | (goto-char (line-beginning-position)) | |
488 | (bobp)) | |
69bab1de | 489 | 'no-indent) |
45c138ac FEG |
490 | ;; Inside a paren |
491 | ((setq start (nth 1 ppss)) | |
492 | 'inside-paren) | |
493 | ;; Inside string | |
494 | ((setq start (when (and (nth 3 ppss)) | |
495 | (nth 8 ppss))) | |
496 | 'inside-string) | |
497 | ;; After backslash | |
498 | ((setq start (when (not (syntax-ppss-context ppss)) | |
499 | (let ((line-beg-pos (line-beginning-position))) | |
500 | (when (eq ?\\ (char-before (1- line-beg-pos))) | |
501 | (- line-beg-pos 2))))) | |
502 | 'after-backslash) | |
503 | ;; After beginning of block | |
504 | ((setq start (save-excursion | |
505 | (let ((block-regexp (python-rx block-start)) | |
506 | (block-start-line-end ":[[:space:]]*$")) | |
507 | (back-to-indentation) | |
508 | (while (and (forward-comment -1) (not (bobp)))) | |
509 | (back-to-indentation) | |
510 | (when (or (python-info-continuation-line-p) | |
511 | (and (not (looking-at block-regexp)) | |
512 | (save-excursion | |
513 | (re-search-forward | |
514 | block-start-line-end | |
515 | (line-end-position) t)))) | |
516 | (while (and (forward-line -1) | |
517 | (python-info-continuation-line-p) | |
518 | (not (bobp)))) | |
519 | (when (not (looking-at block-regexp)) | |
520 | (forward-line 1))) | |
521 | (back-to-indentation) | |
522 | (when (and (looking-at block-regexp) | |
523 | (or (re-search-forward | |
524 | block-start-line-end | |
525 | (line-end-position) t) | |
526 | (python-info-continuation-line-p))) | |
527 | (point-marker))))) | |
528 | 'after-beginning-of-block) | |
529 | ;; After normal line | |
530 | ((setq start (save-excursion | |
531 | (while (and (forward-comment -1) (not (bobp)))) | |
532 | (while (and (not (back-to-indentation)) | |
533 | (not (bobp)) | |
534 | (if (> (nth 0 (syntax-ppss)) 0) | |
535 | (forward-line -1) | |
536 | (if (save-excursion | |
537 | (forward-line -1) | |
538 | (python-info-line-ends-backslash-p)) | |
539 | (forward-line -1))))) | |
540 | (point-marker))) | |
541 | 'after-line) | |
542 | ;; Do not indent | |
543 | (t 'no-indent)) | |
544 | start)))) | |
545 | ||
546 | (defun python-indent-calculate-indentation () | |
547 | "Calculate correct indentation offset for the current line." | |
548 | (let* ((indentation-context (python-indent-context)) | |
549 | (context-status (car indentation-context)) | |
550 | (context-start (cdr indentation-context))) | |
551 | (save-restriction | |
552 | (widen) | |
553 | (save-excursion | |
554 | (case context-status | |
555 | ('no-indent 0) | |
556 | ('after-beginning-of-block | |
557 | (goto-char context-start) | |
558 | (+ (current-indentation) python-indent-offset)) | |
559 | ('after-line | |
560 | (- | |
561 | (save-excursion | |
562 | (goto-char context-start) | |
563 | (current-indentation)) | |
564 | (if (progn | |
565 | (back-to-indentation) | |
566 | (looking-at (regexp-opt python-indent-dedenters))) | |
567 | python-indent-offset | |
568 | 0))) | |
569 | ('inside-string | |
570 | (goto-char context-start) | |
571 | (current-indentation)) | |
572 | ('after-backslash | |
573 | (let* ((block-continuation | |
574 | (save-excursion | |
575 | (forward-line -1) | |
576 | (python-info-block-continuation-line-p))) | |
577 | (assignment-continuation | |
578 | (save-excursion | |
579 | (forward-line -1) | |
580 | (python-info-assignment-continuation-line-p))) | |
581 | (indentation (cond (block-continuation | |
582 | (goto-char block-continuation) | |
583 | (re-search-forward | |
584 | (python-rx block-start (* space)) | |
585 | (line-end-position) t) | |
586 | (current-column)) | |
587 | (assignment-continuation | |
588 | (goto-char assignment-continuation) | |
589 | (re-search-forward | |
590 | (python-rx simple-operator) | |
591 | (line-end-position) t) | |
592 | (forward-char 1) | |
593 | (re-search-forward | |
594 | (python-rx (* space)) | |
595 | (line-end-position) t) | |
596 | (current-column)) | |
597 | (t | |
598 | (goto-char context-start) | |
599 | (current-indentation))))) | |
600 | indentation)) | |
601 | ('inside-paren | |
602 | (- | |
603 | (save-excursion | |
604 | (goto-char context-start) | |
605 | (forward-char) | |
13d1a42e FEG |
606 | (save-restriction |
607 | (narrow-to-region | |
608 | (line-beginning-position) | |
609 | (line-end-position)) | |
610 | (forward-comment 1)) | |
611 | (if (looking-at "$") | |
45c138ac FEG |
612 | (+ (current-indentation) python-indent-offset) |
613 | (forward-comment 1) | |
614 | (current-column))) | |
615 | (if (progn | |
616 | (back-to-indentation) | |
617 | (looking-at (regexp-opt '(")" "]" "}")))) | |
618 | python-indent-offset | |
619 | 0)))))))) | |
620 | ||
621 | (defun python-indent-calculate-levels () | |
622 | "Calculate `python-indent-levels' and reset `python-indent-current-level'." | |
623 | (let* ((indentation (python-indent-calculate-indentation)) | |
624 | (remainder (% indentation python-indent-offset)) | |
625 | (steps (/ (- indentation remainder) python-indent-offset))) | |
626 | (setq python-indent-levels '()) | |
627 | (setq python-indent-levels (cons 0 python-indent-levels)) | |
628 | (dotimes (step steps) | |
629 | (setq python-indent-levels | |
630 | (cons (* python-indent-offset (1+ step)) python-indent-levels))) | |
631 | (when (not (eq 0 remainder)) | |
632 | (setq python-indent-levels | |
633 | (cons (+ (* python-indent-offset steps) remainder) | |
634 | python-indent-levels))) | |
635 | (setq python-indent-levels (nreverse python-indent-levels)) | |
636 | (setq python-indent-current-level (1- (length python-indent-levels))))) | |
637 | ||
638 | (defun python-indent-toggle-levels () | |
639 | "Toggle `python-indent-current-level' over `python-indent-levels'." | |
640 | (setq python-indent-current-level (1- python-indent-current-level)) | |
641 | (when (< python-indent-current-level 0) | |
642 | (setq python-indent-current-level (1- (length python-indent-levels))))) | |
643 | ||
644 | (defun python-indent-line (&optional force-toggle) | |
645 | "Internal implementation of `python-indent-line-function'. | |
646 | ||
647 | Uses the offset calculated in | |
648 | `python-indent-calculate-indentation' and available levels | |
649 | indicated by the variable `python-indent-levels'. | |
650 | ||
651 | When the variable `last-command' is equal to | |
652 | `indent-for-tab-command' or FORCE-TOGGLE is non-nil: | |
653 | ||
654 | * Cycles levels indicated in the variable `python-indent-levels' | |
655 | by setting the current level in the variable | |
656 | `python-indent-current-level'. | |
657 | ||
658 | When the variable `last-command' is not equal to | |
659 | `indent-for-tab-command' and FORCE-TOGGLE is nil: | |
660 | ||
661 | * calculates possible indentation levels and saves it in the | |
662 | variable `python-indent-levels'. | |
663 | ||
664 | * sets the variable `python-indent-current-level' correctly so | |
665 | offset is equal to (`nth' `python-indent-current-level' | |
666 | `python-indent-levels')" | |
667 | (if (or (and (eq this-command 'indent-for-tab-command) | |
668 | (eq last-command this-command)) | |
669 | force-toggle) | |
670 | (python-indent-toggle-levels) | |
671 | (python-indent-calculate-levels)) | |
672 | (beginning-of-line) | |
673 | (delete-horizontal-space) | |
674 | (indent-to (nth python-indent-current-level python-indent-levels)) | |
675 | (save-restriction | |
676 | (widen) | |
677 | (let ((closing-block-point (python-info-closing-block))) | |
678 | (when closing-block-point | |
679 | (message "Closes %s" (buffer-substring | |
680 | closing-block-point | |
681 | (save-excursion | |
682 | (goto-char closing-block-point) | |
683 | (line-end-position)))))))) | |
684 | ||
685 | (defun python-indent-line-function () | |
686 | "`indent-line-function' for Python mode. | |
687 | Internally just calls `python-indent-line'." | |
688 | (python-indent-line)) | |
689 | ||
690 | (defun python-indent-dedent-line () | |
691 | "Dedent current line." | |
692 | (interactive "*") | |
693 | (when (and (not (syntax-ppss-context (syntax-ppss))) | |
694 | (<= (point-marker) (save-excursion | |
695 | (back-to-indentation) | |
696 | (point-marker))) | |
697 | (> (current-column) 0)) | |
698 | (python-indent-line t) | |
699 | t)) | |
700 | ||
701 | (defun python-indent-dedent-line-backspace (arg) | |
702 | "Dedent current line. | |
703 | Argument ARG is passed to `backward-delete-char-untabify' when | |
704 | point is not in between the indentation." | |
705 | (interactive "*p") | |
706 | (when (not (python-indent-dedent-line)) | |
707 | (backward-delete-char-untabify arg))) | |
183f9296 | 708 | (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede) |
45c138ac FEG |
709 | |
710 | (defun python-indent-region (start end) | |
711 | "Indent a python region automagically. | |
712 | ||
713 | Called from a program, START and END specify the region to indent." | |
714 | (save-excursion | |
715 | (goto-char end) | |
716 | (setq end (point-marker)) | |
717 | (goto-char start) | |
718 | (or (bolp) (forward-line 1)) | |
719 | (while (< (point) end) | |
720 | (or (and (bolp) (eolp)) | |
721 | (let (word) | |
722 | (forward-line -1) | |
723 | (back-to-indentation) | |
724 | (setq word (current-word)) | |
725 | (forward-line 1) | |
726 | (when word | |
727 | (beginning-of-line) | |
728 | (delete-horizontal-space) | |
729 | (indent-to (python-indent-calculate-indentation))))) | |
730 | (forward-line 1)) | |
731 | (move-marker end nil))) | |
732 | ||
733 | (defun python-indent-shift-left (start end &optional count) | |
734 | "Shift lines contained in region START END by COUNT columns to the left. | |
735 | ||
736 | COUNT defaults to `python-indent-offset'. | |
737 | ||
738 | If region isn't active, the current line is shifted. | |
739 | ||
740 | The shifted region includes the lines in which START and END lie. | |
741 | ||
742 | An error is signaled if any lines in the region are indented less | |
743 | than COUNT columns." | |
744 | (interactive | |
745 | (if mark-active | |
746 | (list (region-beginning) (region-end) current-prefix-arg) | |
747 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | |
748 | (if count | |
749 | (setq count (prefix-numeric-value count)) | |
750 | (setq count python-indent-offset)) | |
751 | (when (> count 0) | |
752 | (save-excursion | |
753 | (goto-char start) | |
754 | (while (< (point) end) | |
755 | (if (and (< (current-indentation) count) | |
756 | (not (looking-at "[ \t]*$"))) | |
757 | (error "Can't shift all lines enough")) | |
758 | (forward-line)) | |
759 | (indent-rigidly start end (- count))))) | |
760 | ||
761 | (add-to-list 'debug-ignored-errors "^Can't shift all lines enough") | |
762 | ||
763 | (defun python-indent-shift-right (start end &optional count) | |
764 | "Shift lines contained in region START END by COUNT columns to the left. | |
765 | ||
766 | COUNT defaults to `python-indent-offset'. | |
767 | ||
768 | If region isn't active, the current line is shifted. | |
769 | ||
770 | The shifted region includes the lines in which START and END | |
771 | lie." | |
772 | (interactive | |
773 | (if mark-active | |
774 | (list (region-beginning) (region-end) current-prefix-arg) | |
775 | (list (line-beginning-position) (line-end-position) current-prefix-arg))) | |
776 | (if count | |
777 | (setq count (prefix-numeric-value count)) | |
778 | (setq count python-indent-offset)) | |
779 | (indent-rigidly start end count)) | |
780 | ||
781 | \f | |
782 | ;;; Navigation | |
783 | ||
784 | (defvar python-beginning-of-defun-regexp | |
785 | "^\\(def\\|class\\)[[:space:]]+[[:word:]]+" | |
786 | "Regular expresion matching beginning of outermost class or function.") | |
787 | ||
788 | (defvar python-beginning-of-innermost-defun-regexp | |
789 | "^[[:space:]]*\\(def\\|class\\)[[:space:]]+[[:word:]]+" | |
790 | "Regular expresion matching beginning of innermost class or function.") | |
791 | ||
792 | (defun python-beginning-of-defun (&optional innermost) | |
793 | "Move point to the beginning of innermost/outermost def or class. | |
794 | If INNERMOST is non-nil then move to the beginning of the | |
795 | innermost definition." | |
796 | (let ((starting-point (point-marker)) | |
797 | (nonblank-line-indent) | |
798 | (defun-indent) | |
799 | (defun-point) | |
800 | (regexp (if innermost | |
801 | python-beginning-of-innermost-defun-regexp | |
802 | python-beginning-of-defun-regexp))) | |
803 | (back-to-indentation) | |
804 | (if (and (not (looking-at "@")) | |
805 | (not (looking-at regexp))) | |
806 | (forward-comment -1) | |
807 | (while (and (not (eobp)) | |
808 | (forward-line 1) | |
809 | (not (back-to-indentation)) | |
810 | (looking-at "@")))) | |
811 | (when (not (looking-at regexp)) | |
812 | (re-search-backward regexp nil t)) | |
813 | (setq nonblank-line-indent (+ (current-indentation) python-indent-offset)) | |
814 | (setq defun-indent (current-indentation)) | |
815 | (setq defun-point (point-marker)) | |
816 | (if (> nonblank-line-indent defun-indent) | |
817 | (progn | |
818 | (goto-char defun-point) | |
819 | (forward-line -1) | |
820 | (while (and (looking-at "@") | |
821 | (forward-line -1) | |
822 | (not (bobp)) | |
823 | (not (back-to-indentation)))) | |
df700cc9 FEG |
824 | (unless (bobp) |
825 | (forward-line 1)) | |
45c138ac FEG |
826 | (point-marker)) |
827 | (if innermost | |
828 | (python-beginning-of-defun) | |
829 | (goto-char starting-point) | |
830 | nil)))) | |
831 | ||
832 | (defun python-beginning-of-defun-function () | |
833 | "Move point to the beginning of outermost def or class. | |
834 | Returns nil if point is not in a def or class." | |
835 | (python-beginning-of-defun nil)) | |
836 | ||
837 | (defun python-beginning-of-innermost-defun () | |
838 | "Move point to the beginning of innermost def or class. | |
839 | Returns nil if point is not in a def or class." | |
840 | (interactive) | |
841 | (python-beginning-of-defun t)) | |
842 | ||
843 | (defun python-end-of-defun-function () | |
844 | "Move point to the end of def or class. | |
845 | Returns nil if point is not in a def or class." | |
846 | (let ((starting-point (point-marker)) | |
847 | (defun-regexp (python-rx defun)) | |
848 | (beg-defun-indent)) | |
849 | (back-to-indentation) | |
850 | (if (looking-at "@") | |
851 | (while (and (not (eobp)) | |
852 | (forward-line 1) | |
853 | (not (back-to-indentation)) | |
854 | (looking-at "@"))) | |
855 | (while (and (not (bobp)) | |
856 | (not (progn (back-to-indentation) (current-word))) | |
857 | (forward-line -1)))) | |
858 | (when (or (not (equal (current-indentation) 0)) | |
859 | (string-match defun-regexp (current-word))) | |
860 | (setq beg-defun-indent (save-excursion | |
861 | (or (looking-at defun-regexp) | |
862 | (python-beginning-of-innermost-defun)) | |
863 | (current-indentation))) | |
864 | (while (and (forward-line 1) | |
865 | (not (eobp)) | |
866 | (or (not (current-word)) | |
867 | (> (current-indentation) beg-defun-indent)))) | |
868 | (while (and (forward-comment -1) | |
869 | (not (bobp)))) | |
870 | (forward-line 1) | |
871 | (point-marker)))) | |
872 | ||
873 | \f | |
874 | ;;; Shell integration | |
875 | ||
876 | (defvar python-shell-buffer-name "Python" | |
877 | "Default buffer name for Python interpreter.") | |
878 | ||
879 | (defcustom python-shell-interpreter "python" | |
880 | "Default Python interpreter for shell." | |
881 | :group 'python | |
882 | :type 'string | |
883 | :safe 'stringp) | |
884 | ||
885 | (defcustom python-shell-interpreter-args "-i" | |
886 | "Default arguments for the Python interpreter." | |
887 | :group 'python | |
888 | :type 'string | |
889 | :safe 'stringp) | |
890 | ||
891 | (defcustom python-shell-prompt-regexp ">>> " | |
892 | "Regex matching top\-level input prompt of python shell. | |
893 | The regex should not contain a caret (^) at the beginning." | |
894 | :type 'string | |
895 | :group 'python | |
896 | :safe 'stringp) | |
897 | ||
898 | (defcustom python-shell-prompt-block-regexp "[.][.][.] " | |
899 | "Regex matching block input prompt of python shell. | |
900 | The regex should not contain a caret (^) at the beginning." | |
901 | :type 'string | |
902 | :group 'python | |
903 | :safe 'stringp) | |
904 | ||
905 | (defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ " | |
906 | "Regex matching pdb input prompt of python shell. | |
907 | The regex should not contain a caret (^) at the beginning." | |
908 | :type 'string | |
909 | :group 'python | |
910 | :safe 'stringp) | |
911 | ||
912 | (defcustom python-shell-compilation-regexp-alist | |
913 | `((,(rx line-start (1+ (any " \t")) "File \"" | |
914 | (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c | |
915 | "\", line " (group (1+ digit))) | |
916 | 1 2) | |
917 | (,(rx " in file " (group (1+ not-newline)) " on line " | |
918 | (group (1+ digit))) | |
919 | 1 2) | |
920 | (,(rx line-start "> " (group (1+ (not (any "(\"<")))) | |
921 | "(" (group (1+ digit)) ")" (1+ (not (any "("))) "()") | |
922 | 1 2)) | |
923 | "`compilation-error-regexp-alist' for inferior Python." | |
924 | :type '(alist string) | |
925 | :group 'python) | |
926 | ||
927 | (defun python-shell-get-process-name (dedicated) | |
928 | "Calculate the appropiate process name for inferior Python process. | |
929 | ||
930 | If DEDICATED is t and the variable `buffer-file-name' is non-nil | |
931 | returns a string with the form | |
932 | `python-shell-buffer-name'[variable `buffer-file-name'] else | |
933 | returns the value of `python-shell-buffer-name'. | |
934 | ||
935 | After calculating the process name add the buffer name for the | |
936 | process in the `same-window-buffer-names' list" | |
937 | (let ((process-name | |
938 | (if (and dedicated | |
939 | buffer-file-name) | |
940 | (format "%s[%s]" python-shell-buffer-name buffer-file-name) | |
941 | (format "%s" python-shell-buffer-name)))) | |
942 | (add-to-list 'same-window-buffer-names (purecopy | |
943 | (format "*%s*" process-name))) | |
944 | process-name)) | |
945 | ||
946 | (defun python-shell-parse-command () | |
947 | "Calculates the string used to execute the inferior Python process." | |
948 | (format "%s %s" python-shell-interpreter python-shell-interpreter-args)) | |
949 | ||
950 | (defun python-comint-output-filter-function (output) | |
951 | "Hook run after content is put into comint buffer. | |
952 | OUTPUT is a string with the contents of the buffer." | |
953 | (ansi-color-filter-apply output)) | |
954 | ||
955 | (defvar inferior-python-mode-current-file nil | |
956 | "Current file from which a region was sent.") | |
957 | (make-variable-buffer-local 'inferior-python-mode-current-file) | |
958 | ||
959 | (defvar inferior-python-mode-current-temp-file nil | |
960 | "Current temp file sent to process.") | |
961 | (make-variable-buffer-local 'inferior-python-mode-current-file) | |
962 | ||
963 | (define-derived-mode inferior-python-mode comint-mode "Inferior Python" | |
964 | "Major mode for Python inferior process." | |
965 | (set-syntax-table python-mode-syntax-table) | |
966 | (setq mode-line-process '(":%s")) | |
967 | (setq comint-prompt-regexp (format "^\\(?:%s\\|%s\\|%s\\)" | |
968 | python-shell-prompt-regexp | |
969 | python-shell-prompt-block-regexp | |
970 | python-shell-prompt-pdb-regexp)) | |
971 | (make-local-variable 'comint-output-filter-functions) | |
972 | (add-hook 'comint-output-filter-functions | |
973 | 'python-comint-output-filter-function) | |
974 | (add-hook 'comint-output-filter-functions | |
975 | 'python-pdbtrack-comint-output-filter-function) | |
976 | (set (make-local-variable 'compilation-error-regexp-alist) | |
977 | python-shell-compilation-regexp-alist) | |
ed0eb594 FEG |
978 | (define-key inferior-python-mode-map [remap complete-symbol] |
979 | 'completion-at-point) | |
980 | (add-hook 'completion-at-point-functions | |
981 | 'python-shell-completion-complete-at-point nil 'local) | |
45c138ac FEG |
982 | (compilation-shell-minor-mode 1)) |
983 | ||
984 | (defun run-python (dedicated cmd) | |
985 | "Run an inferior Python process. | |
986 | ||
987 | Input and output via buffer *\\[python-shell-buffer-name]*. | |
988 | ||
989 | If there is a process already running in | |
990 | *\\[python-shell-buffer-name]*, switch to that buffer. | |
991 | ||
992 | With argument, allows you to: | |
993 | ||
994 | * Define DEDICATED so a dedicated process for the current buffer | |
995 | is open. | |
996 | ||
997 | * Define CMD so you can edit the command used to call the | |
998 | interpreter (default is value of `python-shell-interpreter' and | |
999 | arguments defined in `python-shell-interpreter-args'). | |
1000 | ||
1001 | Runs the hook `inferior-python-mode-hook' (after the | |
1002 | `comint-mode-hook' is run). | |
1003 | ||
1004 | \(Type \\[describe-mode] in the process buffer for a list of | |
1005 | commands.)" | |
1006 | (interactive | |
1007 | (if current-prefix-arg | |
1008 | (list | |
1009 | (y-or-n-p "Make dedicated process? ") | |
1010 | (read-string "Run Python: " (python-shell-parse-command))) | |
1011 | (list nil (python-shell-parse-command)))) | |
1012 | (let* ((proc-name (python-shell-get-process-name dedicated)) | |
1013 | (proc-buffer-name (format "*%s*" proc-name))) | |
1014 | (when (not (comint-check-proc proc-buffer-name)) | |
1015 | (let ((cmdlist (split-string-and-unquote cmd))) | |
1016 | (set-buffer | |
1017 | (apply 'make-comint proc-name (car cmdlist) nil | |
1018 | (cdr cmdlist))) | |
1019 | (inferior-python-mode))) | |
1020 | (pop-to-buffer proc-buffer-name)) | |
1021 | dedicated) | |
1022 | ||
1023 | (defun python-shell-get-process () | |
1024 | "Get inferior Python process for current buffer and return it." | |
1025 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) | |
1026 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) | |
1027 | (global-proc-name (python-shell-get-process-name nil)) | |
1028 | (global-proc-buffer-name (format "*%s*" global-proc-name)) | |
1029 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | |
1030 | (global-running (comint-check-proc global-proc-buffer-name))) | |
1031 | ;; Always prefer dedicated | |
1032 | (get-buffer-process (or (and dedicated-running dedicated-proc-buffer-name) | |
1033 | (and global-running global-proc-buffer-name))))) | |
1034 | ||
1035 | (defun python-shell-get-or-create-process () | |
1036 | "Get or create an inferior Python process for current buffer and return it." | |
79dafa51 FEG |
1037 | (let* ((old-buffer (current-buffer)) |
1038 | (dedicated-proc-name (python-shell-get-process-name t)) | |
45c138ac FEG |
1039 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) |
1040 | (global-proc-name (python-shell-get-process-name nil)) | |
1041 | (global-proc-buffer-name (format "*%s*" global-proc-name)) | |
1042 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | |
1043 | (global-running (comint-check-proc global-proc-buffer-name)) | |
1044 | (current-prefix-arg 4)) | |
1045 | (when (and (not dedicated-running) (not global-running)) | |
1046 | (if (call-interactively 'run-python) | |
1047 | (setq dedicated-running t) | |
1048 | (setq global-running t))) | |
1049 | ;; Always prefer dedicated | |
79dafa51 | 1050 | (switch-to-buffer old-buffer) |
45c138ac FEG |
1051 | (get-buffer-process (if dedicated-running |
1052 | dedicated-proc-buffer-name | |
1053 | global-proc-buffer-name)))) | |
1054 | ||
13d914ed | 1055 | (defun python-shell-send-string (string &optional process) |
138df813 | 1056 | "Send STRING to inferior Python PROCESS." |
45c138ac | 1057 | (interactive "sPython command: ") |
13d914ed | 1058 | (let ((process (or process (python-shell-get-or-create-process)))) |
fc87f759 | 1059 | (when (called-interactively-p 'interactive) |
13d914ed | 1060 | (message (format "Sent: %s..." string))) |
45c138ac FEG |
1061 | (comint-send-string process string) |
1062 | (when (or (not (string-match "\n$" string)) | |
1063 | (string-match "\n[ \t].*\n?$" string)) | |
1064 | (comint-send-string process "\n")))) | |
1065 | ||
1066 | (defun python-shell-send-region (start end) | |
1067 | "Send the region delimited by START and END to inferior Python process." | |
1068 | (interactive "r") | |
1069 | (let* ((contents (buffer-substring start end)) | |
1070 | (current-file (buffer-file-name)) | |
1071 | (process (python-shell-get-or-create-process)) | |
d818ffa8 | 1072 | (temp-file (convert-standard-filename (make-temp-file "py")))) |
45c138ac FEG |
1073 | (with-temp-file temp-file |
1074 | (insert contents) | |
1075 | (delete-trailing-whitespace) | |
1076 | (goto-char (point-min)) | |
1077 | (message (format "Sent: %s..." | |
1078 | (buffer-substring (point-min) | |
1079 | (line-end-position))))) | |
1080 | (with-current-buffer (process-buffer process) | |
1081 | (setq inferior-python-mode-current-file current-file) | |
66b0b492 FEG |
1082 | (setq inferior-python-mode-current-temp-file temp-file)) |
1083 | (python-shell-send-file temp-file process))) | |
45c138ac FEG |
1084 | |
1085 | (defun python-shell-send-buffer () | |
1086 | "Send the entire buffer to inferior Python process." | |
1087 | (interactive) | |
1088 | (save-restriction | |
1089 | (widen) | |
1090 | (python-shell-send-region (point-min) (point-max)))) | |
1091 | ||
1092 | (defun python-shell-send-defun (arg) | |
1093 | "Send the (inner|outer)most def or class to inferior Python process. | |
1094 | When argument ARG is non-nil sends the innermost defun." | |
1095 | (interactive "P") | |
1096 | (save-excursion | |
1097 | (python-shell-send-region (progn | |
1098 | (or (if arg | |
1099 | (python-beginning-of-innermost-defun) | |
1100 | (python-beginning-of-defun-function)) | |
1101 | (progn (beginning-of-line) (point-marker)))) | |
1102 | (progn | |
1103 | (or (python-end-of-defun-function) | |
1104 | (progn (end-of-line) (point-marker))))))) | |
1105 | ||
66b0b492 | 1106 | (defun python-shell-send-file (file-name &optional process) |
4e531f7a | 1107 | "Send FILE-NAME to inferior Python PROCESS." |
45c138ac | 1108 | (interactive "fFile to send: ") |
b962ebad FEG |
1109 | (let ((process (or process (python-shell-get-or-create-process))) |
1110 | (full-file-name (expand-file-name file-name))) | |
13d914ed | 1111 | (python-shell-send-string |
b962ebad | 1112 | (format |
13d914ed FEG |
1113 | "__pyfile = open('%s'); exec(compile(__pyfile.read(), '%s', 'exec')); __pyfile.close()" |
1114 | full-file-name full-file-name) | |
1115 | process))) | |
45c138ac | 1116 | |
138df813 FEG |
1117 | (defun python-shell-clear-latest-output () |
1118 | "Clear latest output from the Python shell. | |
1119 | Return the cleaned output." | |
1120 | (interactive) | |
1121 | (when (and comint-last-output-start | |
1122 | comint-last-prompt-overlay) | |
1123 | (save-excursion | |
1124 | (let* ((last-output-end | |
1125 | (save-excursion | |
1126 | (goto-char | |
1127 | (overlay-start comint-last-prompt-overlay)) | |
1128 | (forward-comment -1) | |
1129 | (point-marker))) | |
1130 | (last-output | |
1131 | (buffer-substring-no-properties | |
1132 | comint-last-output-start last-output-end))) | |
1133 | (when (< 0 (length last-output)) | |
1134 | (goto-char comint-last-output-start) | |
1135 | (delete-region comint-last-output-start last-output-end) | |
1136 | (delete-char -1) | |
1137 | last-output))))) | |
1138 | ||
1139 | (defun python-shell-send-and-clear-output (string process) | |
1140 | "Send STRING to PROCESS and clear the output. | |
1141 | Return the cleaned output." | |
1142 | (interactive) | |
1143 | (python-shell-send-string string process) | |
1144 | (accept-process-output process) | |
1145 | (with-current-buffer (process-buffer process) | |
1146 | (let ((output (python-shell-clear-latest-output))) | |
1147 | (forward-line -1) | |
1148 | (kill-whole-line) | |
1149 | (goto-char (overlay-end comint-last-prompt-overlay)) | |
1150 | output))) | |
1151 | ||
45c138ac FEG |
1152 | (defun python-shell-switch-to-shell () |
1153 | "Switch to inferior Python process buffer." | |
1154 | (interactive) | |
1155 | (pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t)) | |
1156 | ||
1157 | \f | |
1158 | ;;; Shell completion | |
1159 | ||
1160 | (defvar python-shell-completion-setup-code | |
1161 | "try: | |
1162 | import readline | |
1163 | except ImportError: | |
1164 | def __COMPLETER_all_completions(text): [] | |
1165 | else: | |
1166 | import rlcompleter | |
1167 | readline.set_completer(rlcompleter.Completer().complete) | |
1168 | def __COMPLETER_all_completions(text): | |
1169 | import sys | |
1170 | completions = [] | |
1171 | try: | |
1172 | i = 0 | |
1173 | while True: | |
1174 | res = readline.get_completer()(text, i) | |
1175 | if not res: break | |
1176 | i += 1 | |
1177 | completions.append(res) | |
1178 | except NameError: | |
1179 | pass | |
1180 | return completions" | |
1181 | "Code used to setup completion in inferior Python processes.") | |
1182 | ||
1183 | (defvar python-shell-completion-strings-code | |
1184 | "';'.join(__COMPLETER_all_completions('''%s'''))\n" | |
1185 | "Python code used to get a string of completions separated by semicolons.") | |
1186 | ||
1187 | (defun python-shell-completion-setup () | |
1188 | "Send `python-shell-completion-setup-code' to inferior Python process. | |
1189 | Also binds <tab> to `python-shell-complete-or-indent' in the | |
1190 | `inferior-python-mode-map' and adds | |
1191 | `python-shell-completion-complete-at-point' to the | |
1192 | `comint-dynamic-complete-functions' list. | |
1193 | It is specially designed to be added to the | |
1194 | `inferior-python-mode-hook'." | |
1195 | (when python-shell-completion-setup-code | |
1196 | (let ((temp-file (make-temp-file "py")) | |
1197 | (process (get-buffer-process (current-buffer)))) | |
1198 | (with-temp-file temp-file | |
1199 | (insert python-shell-completion-setup-code) | |
1200 | (delete-trailing-whitespace) | |
1201 | (goto-char (point-min))) | |
66b0b492 | 1202 | (python-shell-send-file temp-file process) |
45c138ac FEG |
1203 | (message (format "Completion setup code sent."))) |
1204 | (add-to-list (make-local-variable | |
1205 | 'comint-dynamic-complete-functions) | |
1206 | 'python-shell-completion-complete-at-point) | |
1207 | (define-key inferior-python-mode-map (kbd "<tab>") | |
1208 | 'python-shell-completion-complete-or-indent))) | |
1209 | ||
075a0f61 FEG |
1210 | (defun python-shell-completion--get-completions (input process) |
1211 | "Retrieve available completions for INPUT using PROCESS." | |
1212 | (with-current-buffer (process-buffer process) | |
138df813 FEG |
1213 | (split-string |
1214 | (or (python-shell-send-and-clear-output | |
1215 | (format python-shell-completion-strings-code input) | |
1216 | process) "") | |
1217 | ";\\|\"\\|'\\|(" t))) | |
075a0f61 FEG |
1218 | |
1219 | (defun python-shell-completion--get-completion (input completions) | |
1220 | "Get completion for INPUT using COMPLETIONS." | |
1221 | (let ((completion (when completions | |
1222 | (try-completion input completions)))) | |
1223 | (cond ((eq completion t) | |
1224 | input) | |
1225 | ((null completion) | |
1226 | (message "Can't find completion for \"%s\"" input) | |
1227 | (ding) | |
1228 | input) | |
1229 | ((not (string= input completion)) | |
1230 | completion) | |
1231 | (t | |
1232 | (message "Making completion list...") | |
1233 | (with-output-to-temp-buffer "*Python Completions*" | |
1234 | (display-completion-list | |
1235 | (all-completions input completions))) | |
1236 | input)))) | |
1237 | ||
45c138ac FEG |
1238 | (defun python-shell-completion-complete-at-point () |
1239 | "Perform completion at point in inferior Python process." | |
1240 | (interactive) | |
3d6913c7 FEG |
1241 | (with-syntax-table python-dotty-syntax-table |
1242 | (when (and comint-last-prompt-overlay | |
1243 | (> (point-marker) (overlay-end comint-last-prompt-overlay))) | |
1244 | (let* ((process (get-buffer-process (current-buffer))) | |
075a0f61 FEG |
1245 | (input (substring-no-properties |
1246 | (or (comint-word (current-word)) "") nil nil))) | |
1247 | (delete-char (- (length input))) | |
1248 | (insert | |
1249 | (python-shell-completion--get-completion | |
1250 | input (python-shell-completion--get-completions input process))))))) | |
45c138ac | 1251 | |
45c138ac FEG |
1252 | (defun python-shell-completion-complete-or-indent () |
1253 | "Complete or indent depending on the context. | |
1254 | If content before pointer is all whitespace indent. If not try to | |
1255 | complete." | |
1256 | (interactive) | |
1257 | (if (string-match "^[[:space:]]*$" | |
1258 | (buffer-substring (comint-line-beginning-position) | |
1259 | (point-marker))) | |
1260 | (indent-for-tab-command) | |
1261 | (comint-dynamic-complete))) | |
1262 | ||
1263 | (add-hook 'inferior-python-mode-hook | |
1264 | #'python-shell-completion-setup) | |
1265 | ||
1266 | \f | |
1267 | ;;; PDB Track integration | |
1268 | ||
1269 | (defvar python-pdbtrack-stacktrace-info-regexp | |
1270 | "> %s(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()" | |
1271 | "Regexp matching stacktrace information. | |
1272 | It is used to extract the current line and module beign | |
1273 | inspected. | |
1274 | The regexp should not start with a caret (^) and can contain a | |
1275 | string placeholder (\%s) which is replaced with the filename | |
1276 | beign inspected (so other files in the debugging process are not | |
1277 | opened)") | |
1278 | ||
1279 | (defvar python-pdbtrack-tracking-buffers '() | |
1280 | "Alist containing elements of form (#<buffer> . #<buffer>). | |
1281 | The car of each element of the alist is the tracking buffer and | |
1282 | the cdr is the tracked buffer.") | |
1283 | ||
1284 | (defun python-pdbtrack-get-or-add-tracking-buffers () | |
1285 | "Get/Add a tracked buffer for the current buffer. | |
1286 | Internally it uses the `python-pdbtrack-tracking-buffers' alist. | |
1287 | Returns a cons with the form: | |
1288 | * (#<tracking buffer> . #< tracked buffer>)." | |
1289 | (or | |
1290 | (assq (current-buffer) python-pdbtrack-tracking-buffers) | |
1291 | (let* ((file (with-current-buffer (current-buffer) | |
1292 | (or inferior-python-mode-current-file | |
1293 | inferior-python-mode-current-temp-file))) | |
1294 | (tracking-buffers | |
1295 | `(,(current-buffer) . | |
1296 | ,(or (get-file-buffer file) | |
1297 | (find-file-noselect file))))) | |
1298 | (set-buffer (cdr tracking-buffers)) | |
1299 | (python-mode) | |
1300 | (set-buffer (car tracking-buffers)) | |
1301 | (setq python-pdbtrack-tracking-buffers | |
1302 | (cons tracking-buffers python-pdbtrack-tracking-buffers)) | |
1303 | tracking-buffers))) | |
1304 | ||
1305 | (defun python-pdbtrack-comint-output-filter-function (output) | |
1306 | "Move overlay arrow to current pdb line in tracked buffer. | |
1307 | Argument OUTPUT is a string with the output from the comint process." | |
1308 | (when (not (string= output "")) | |
1309 | (let ((full-output (ansi-color-filter-apply | |
1310 | (buffer-substring comint-last-input-end | |
1311 | (point-max))))) | |
1312 | (if (string-match python-shell-prompt-pdb-regexp full-output) | |
1313 | (let* ((tracking-buffers (python-pdbtrack-get-or-add-tracking-buffers)) | |
1314 | (line-num | |
1315 | (save-excursion | |
1316 | (string-match | |
1317 | (format python-pdbtrack-stacktrace-info-regexp | |
1318 | (regexp-quote | |
1319 | inferior-python-mode-current-temp-file)) | |
1320 | full-output) | |
1321 | (string-to-number (or (match-string-no-properties 1 full-output) "")))) | |
1322 | (tracked-buffer-window (get-buffer-window (cdr tracking-buffers))) | |
1323 | (tracked-buffer-line-pos)) | |
1324 | (when line-num | |
1325 | (with-current-buffer (cdr tracking-buffers) | |
1326 | (set (make-local-variable 'overlay-arrow-string) "=>") | |
1327 | (set (make-local-variable 'overlay-arrow-position) (make-marker)) | |
1328 | (setq tracked-buffer-line-pos (progn | |
1329 | (goto-char (point-min)) | |
1330 | (forward-line (1- line-num)) | |
1331 | (point-marker))) | |
1332 | (when tracked-buffer-window | |
1333 | (set-window-point tracked-buffer-window tracked-buffer-line-pos)) | |
1334 | (set-marker overlay-arrow-position tracked-buffer-line-pos))) | |
1335 | (pop-to-buffer (cdr tracking-buffers)) | |
1336 | (switch-to-buffer-other-window (car tracking-buffers))) | |
1337 | (let ((tracking-buffers (assq (current-buffer) | |
1338 | python-pdbtrack-tracking-buffers))) | |
1339 | (when tracking-buffers | |
1340 | (if inferior-python-mode-current-file | |
1341 | (with-current-buffer (cdr tracking-buffers) | |
1342 | (set-marker overlay-arrow-position nil)) | |
1343 | (kill-buffer (cdr tracking-buffers))) | |
1344 | (setq python-pdbtrack-tracking-buffers | |
1345 | (assq-delete-all (current-buffer) | |
1346 | python-pdbtrack-tracking-buffers))))))) | |
1347 | output) | |
1348 | ||
1349 | \f | |
1350 | ;;; Symbol completion | |
1351 | ||
1352 | (defun python-completion-complete-at-point () | |
1353 | "Complete current symbol at point. | |
1354 | For this to work the best as possible you should call | |
1355 | `python-shell-send-buffer' from time to time so context in | |
1356 | inferior python process is updated properly." | |
1357 | (interactive) | |
1358 | (let ((process (python-shell-get-process))) | |
1359 | (if (not process) | |
4e531f7a | 1360 | (error "Completion needs an inferior Python process running") |
075a0f61 FEG |
1361 | (with-syntax-table python-dotty-syntax-table |
1362 | (let* ((input (substring-no-properties | |
1363 | (or (comint-word (current-word)) "") nil nil)) | |
1364 | (completions (python-shell-completion--get-completions | |
1365 | input process))) | |
1366 | (delete-char (- (length input))) | |
1367 | (insert | |
1368 | (python-shell-completion--get-completion | |
1369 | input completions))))))) | |
45c138ac FEG |
1370 | |
1371 | (add-to-list 'debug-ignored-errors "^Completion needs an inferior Python process running.") | |
1372 | ||
1373 | \f | |
1374 | ;;; Fill paragraph | |
1375 | ||
1376 | (defun python-fill-paragraph-function (&optional justify) | |
1377 | "`fill-paragraph-function' handling multi-line strings and possibly comments. | |
1378 | If any of the current line is in or at the end of a multi-line string, | |
1379 | fill the string or the paragraph of it that point is in, preserving | |
4e531f7a FEG |
1380 | the string's indentation. |
1381 | Optional argument JUSTIFY defines if the paragraph should be justified." | |
45c138ac FEG |
1382 | (interactive "P") |
1383 | (save-excursion | |
1384 | (back-to-indentation) | |
1385 | (cond | |
1386 | ;; Comments | |
1387 | ((fill-comment-paragraph justify)) | |
1388 | ;; Docstrings | |
1389 | ((save-excursion (skip-chars-forward "\"'uUrR") | |
1390 | (nth 3 (syntax-ppss))) | |
1391 | (let ((marker (point-marker)) | |
1392 | (string-start-marker | |
1393 | (progn | |
1394 | (skip-chars-forward "\"'uUrR") | |
1395 | (goto-char (nth 8 (syntax-ppss))) | |
1396 | (skip-chars-forward "\"'uUrR") | |
1397 | (point-marker))) | |
1398 | (reg-start (line-beginning-position)) | |
1399 | (string-end-marker | |
1400 | (progn | |
1401 | (while (nth 3 (syntax-ppss)) (goto-char (1+ (point-marker)))) | |
1402 | (skip-chars-backward "\"'") | |
1403 | (point-marker))) | |
1404 | (reg-end (line-end-position)) | |
1405 | (fill-paragraph-function)) | |
1406 | (save-restriction | |
1407 | (narrow-to-region reg-start reg-end) | |
1408 | (save-excursion | |
1409 | (goto-char string-start-marker) | |
1410 | (delete-region (point-marker) (progn | |
1411 | (skip-syntax-forward "> ") | |
1412 | (point-marker))) | |
1413 | (goto-char string-end-marker) | |
1414 | (delete-region (point-marker) (progn | |
1415 | (skip-syntax-backward "> ") | |
1416 | (point-marker))) | |
1417 | (save-excursion | |
1418 | (goto-char marker) | |
1419 | (fill-paragraph justify)) | |
1420 | ;; If there is a newline in the docstring lets put triple | |
1421 | ;; quote in it's own line to follow pep 8 | |
1422 | (when (save-excursion | |
1423 | (re-search-backward "\n" string-start-marker t)) | |
1424 | (newline) | |
1425 | (newline-and-indent)) | |
1426 | (fill-paragraph justify)))) t) | |
1427 | ;; Decorators | |
1428 | ((equal (char-after (save-excursion | |
1429 | (back-to-indentation) | |
1430 | (point-marker))) ?@) t) | |
1431 | ;; Parens | |
1432 | ((or (> (nth 0 (syntax-ppss)) 0) | |
1433 | (looking-at (python-rx open-paren)) | |
1434 | (save-excursion | |
1435 | (skip-syntax-forward "^(" (line-end-position)) | |
1436 | (looking-at (python-rx open-paren)))) | |
1437 | (save-restriction | |
1438 | (narrow-to-region (progn | |
1439 | (while (> (nth 0 (syntax-ppss)) 0) | |
1440 | (goto-char (1- (point-marker)))) | |
1441 | (point-marker) | |
1442 | (line-beginning-position)) | |
1443 | (progn | |
1444 | (when (not (> (nth 0 (syntax-ppss)) 0)) | |
1445 | (end-of-line) | |
1446 | (when (not (> (nth 0 (syntax-ppss)) 0)) | |
1447 | (skip-syntax-backward "^)"))) | |
1448 | (while (> (nth 0 (syntax-ppss)) 0) | |
1449 | (goto-char (1+ (point-marker)))) | |
1450 | (point-marker))) | |
1451 | (let ((paragraph-start "\f\\|[ \t]*$") | |
1452 | (paragraph-separate ",") | |
1453 | (fill-paragraph-function)) | |
1454 | (goto-char (point-min)) | |
1455 | (fill-paragraph justify)) | |
1456 | (while (not (eobp)) | |
1457 | (forward-line 1) | |
1458 | (python-indent-line) | |
1459 | (goto-char (line-end-position)))) t) | |
1460 | (t t)))) | |
1461 | ||
1462 | \f | |
046428d3 FEG |
1463 | ;;; FFAP |
1464 | ||
1465 | (defvar python-ffap-setup-code | |
1466 | "def __FFAP_get_module_path(module): | |
1467 | try: | |
1468 | import os | |
1469 | path = __import__(module).__file__ | |
1470 | if path[-4:] == '.pyc' and os.path.exists(path[0:-1]): | |
1471 | path = path[:-1] | |
1472 | return path | |
1473 | except: | |
1474 | return ''" | |
1475 | "Python code to get a module path.") | |
1476 | ||
1477 | (defvar python-ffap-string-code | |
1478 | "__FFAP_get_module_path('''%s''')\n" | |
1479 | "Python code used to get a string with the path of a module.") | |
1480 | ||
1481 | (defun python-ffap-setup () | |
1482 | "Send `python-ffap-setup-code' to inferior Python process. | |
1483 | It is specially designed to be added to the | |
1484 | `inferior-python-mode-hook'." | |
1485 | (when python-ffap-setup-code | |
1486 | (let ((temp-file (make-temp-file "py"))) | |
1487 | (with-temp-file temp-file | |
1488 | (insert python-ffap-setup-code) | |
1489 | (delete-trailing-whitespace) | |
1490 | (goto-char (point-min))) | |
1491 | (python-shell-send-file temp-file (get-buffer-process (current-buffer))) | |
1492 | (message (format "FFAP setup code sent."))))) | |
1493 | ||
1494 | (defun python-ffap-module-path (module) | |
1495 | "Function for `ffap-alist' to return path for MODULE." | |
1496 | (let ((process (or | |
1497 | (and (eq major-mode 'inferior-python-mode) | |
1498 | (get-buffer-process (current-buffer))) | |
1499 | (python-shell-get-process)))) | |
1500 | (if (not process) | |
1501 | nil | |
1502 | (let ((module-file | |
1503 | (python-shell-send-and-clear-output | |
1504 | (format python-ffap-string-code module) process))) | |
1505 | (when module-file | |
2947016a | 1506 | (substring-no-properties module-file 1 -1)))))) |
046428d3 FEG |
1507 | |
1508 | (eval-after-load "ffap" | |
1509 | '(progn | |
1510 | (push '(python-mode . python-ffap-module-path) ffap-alist) | |
1511 | (push '(inferior-python-mode . python-ffap-module-path) ffap-alist))) | |
1512 | ||
1513 | (add-hook 'inferior-python-mode-hook | |
1514 | #'python-ffap-setup) | |
1515 | ||
1516 | \f | |
8b3e0e76 FEG |
1517 | ;;; Code check |
1518 | ||
1519 | (defvar python-check-command | |
1520 | "pychecker --stdlib" | |
1521 | "Command used to check a Python file.") | |
1522 | ||
1523 | (defvar python-check-custom-command nil | |
1524 | "Internal use.") | |
1525 | ||
1526 | (defun python-check (command) | |
1527 | "Check a Python file (default current buffer's file). | |
1528 | Runs COMMAND, a shell command, as if by `compile'. See | |
1529 | `python-check-command' for the default." | |
1530 | (interactive | |
1531 | (list (read-string "Check command: " | |
1532 | (or python-check-custom-command | |
1533 | (concat python-check-command " " | |
1534 | (shell-quote-argument | |
1535 | (or | |
1536 | (let ((name (buffer-file-name))) | |
1537 | (and name | |
1538 | (file-name-nondirectory name))) | |
1539 | ""))))))) | |
1540 | (setq python-check-custom-command command) | |
1541 | (save-some-buffers (not compilation-ask-about-save) nil) | |
1542 | (compilation-start command)) | |
1543 | ||
1544 | \f | |
45c138ac FEG |
1545 | ;;; Eldoc |
1546 | ||
1547 | (defvar python-eldoc-setup-code | |
1548 | "def __PYDOC_get_help(obj): | |
1549 | try: | |
1550 | import pydoc | |
9e662938 FEG |
1551 | if hasattr(obj, 'startswith'): |
1552 | obj = eval(obj, globals()) | |
1553 | doc = pydoc.getdoc(obj) | |
45c138ac | 1554 | except: |
9e662938 FEG |
1555 | doc = '' |
1556 | try: | |
1557 | exec('print doc') | |
1558 | except SyntaxError: | |
1559 | print(doc)" | |
45c138ac FEG |
1560 | "Python code to setup documentation retrieval.") |
1561 | ||
1562 | (defvar python-eldoc-string-code | |
9e662938 | 1563 | "__PYDOC_get_help('''%s''')\n" |
45c138ac FEG |
1564 | "Python code used to get a string with the documentation of an object.") |
1565 | ||
1566 | (defun python-eldoc-setup () | |
1567 | "Send `python-eldoc-setup-code' to inferior Python process. | |
1568 | It is specially designed to be added to the | |
1569 | `inferior-python-mode-hook'." | |
1570 | (when python-eldoc-setup-code | |
1571 | (let ((temp-file (make-temp-file "py"))) | |
1572 | (with-temp-file temp-file | |
1573 | (insert python-eldoc-setup-code) | |
1574 | (delete-trailing-whitespace) | |
1575 | (goto-char (point-min))) | |
66b0b492 | 1576 | (python-shell-send-file temp-file (get-buffer-process (current-buffer))) |
046428d3 | 1577 | (message (format "Eldoc setup code sent."))))) |
45c138ac | 1578 | |
78334b43 FEG |
1579 | (defun python-eldoc--get-doc-at-point (&optional force-input force-process) |
1580 | (let ((process (or force-process (python-shell-get-process)))) | |
45c138ac FEG |
1581 | (if (not process) |
1582 | "Eldoc needs an inferior Python process running." | |
1583 | (let* ((current-defun (python-info-current-defun)) | |
78334b43 FEG |
1584 | (input (or force-input |
1585 | (with-syntax-table python-dotty-syntax-table | |
1586 | (if (not current-defun) | |
1587 | (current-word) | |
1588 | (concat current-defun "." (current-word)))))) | |
45c138ac FEG |
1589 | (ppss (syntax-ppss)) |
1590 | (help (when (and input | |
1591 | (not (string= input (concat current-defun "."))) | |
1592 | (eq nil (nth 3 ppss)) | |
1593 | (eq nil (nth 4 ppss))) | |
1594 | (when (string-match (concat | |
1595 | (regexp-quote (concat current-defun ".")) | |
1596 | "self\\.") input) | |
1597 | (with-temp-buffer | |
1598 | (insert input) | |
1599 | (goto-char (point-min)) | |
1600 | (forward-word) | |
1601 | (forward-char) | |
1602 | (delete-region (point-marker) (search-forward "self.")) | |
1603 | (setq input (buffer-substring (point-min) (point-max))))) | |
1066882c FEG |
1604 | (python-shell-send-and-clear-output |
1605 | (format python-eldoc-string-code input) process)))) | |
45c138ac FEG |
1606 | (with-current-buffer (process-buffer process) |
1607 | (when comint-last-prompt-overlay | |
1608 | (delete-region comint-last-input-end | |
1609 | (overlay-start comint-last-prompt-overlay)))) | |
1610 | (when (and help | |
1611 | (not (string= help "\n"))) | |
1612 | help))))) | |
1613 | ||
78334b43 FEG |
1614 | (defun python-eldoc-function () |
1615 | "`eldoc-documentation-function' for Python. | |
1616 | For this to work the best as possible you should call | |
1617 | `python-shell-send-buffer' from time to time so context in | |
1618 | inferior python process is updated properly." | |
1619 | (python-eldoc--get-doc-at-point)) | |
1620 | ||
1621 | (defun python-eldoc-at-point (symbol) | |
1622 | "Get help on SYMBOL using `help'. | |
1623 | Interactively, prompt for symbol." | |
1624 | (interactive | |
1625 | (let ((symbol (with-syntax-table python-dotty-syntax-table | |
1626 | (current-word))) | |
1627 | (enable-recursive-minibuffers t)) | |
1628 | (list (read-string (if symbol | |
1629 | (format "Describe symbol (default %s): " symbol) | |
1630 | "Describe symbol: ") | |
1631 | nil nil symbol)))) | |
1632 | (let ((process (python-shell-get-process))) | |
1633 | (if (not process) | |
1634 | (message "Eldoc needs an inferior Python process running.") | |
1635 | (let ((temp-buffer-show-hook | |
1636 | (lambda () | |
1637 | (toggle-read-only 1) | |
1638 | (setq view-return-to-alist | |
1639 | (list (cons (selected-window) help-return-method)))))) | |
1640 | (with-output-to-temp-buffer (help-buffer) | |
1641 | (with-current-buffer standard-output | |
1642 | (insert | |
1643 | (python-eldoc--get-doc-at-point symbol process)) | |
1644 | (help-print-return-message))))))) | |
1645 | ||
45c138ac FEG |
1646 | (add-hook 'inferior-python-mode-hook |
1647 | #'python-eldoc-setup) | |
1648 | ||
1649 | \f | |
1650 | ;;; Misc helpers | |
1651 | ||
1652 | (defun python-info-current-defun () | |
1653 | "Return name of surrounding function with Python compatible dotty syntax. | |
1654 | This function is compatible to be used as | |
1655 | `add-log-current-defun-function' since it returns nil if point is | |
1656 | not inside a defun." | |
1657 | (let ((names '())) | |
1658 | (save-restriction | |
1659 | (widen) | |
1660 | (save-excursion | |
1661 | (beginning-of-line) | |
1662 | (when (not (>= (current-indentation) python-indent-offset)) | |
1663 | (while (and (not (eobp)) (forward-comment 1)))) | |
1664 | (while (and (not (equal 0 (current-indentation))) | |
1665 | (python-beginning-of-innermost-defun)) | |
1666 | (back-to-indentation) | |
1667 | (looking-at "\\(?:def\\|class\\) +\\([^(]+\\)[^:]+:\\s-*\n") | |
1668 | (setq names (cons (match-string-no-properties 1) names))))) | |
1669 | (when names | |
1670 | (mapconcat (lambda (string) string) names ".")))) | |
1671 | ||
1672 | (defun python-info-closing-block () | |
1673 | "Return the point of the block that the current line closes." | |
1674 | (let ((closing-word (save-excursion | |
1675 | (back-to-indentation) | |
1676 | (current-word))) | |
1677 | (indentation (current-indentation))) | |
1678 | (when (member closing-word python-indent-dedenters) | |
1679 | (save-excursion | |
1680 | (forward-line -1) | |
1681 | (while (and (> (current-indentation) indentation) | |
1682 | (not (bobp)) | |
1683 | (not (back-to-indentation)) | |
1684 | (forward-line -1))) | |
1685 | (back-to-indentation) | |
1686 | (cond | |
1687 | ((not (equal indentation (current-indentation))) nil) | |
1688 | ((string= closing-word "elif") | |
1689 | (when (member (current-word) '("if" "elif")) | |
1690 | (point-marker))) | |
1691 | ((string= closing-word "else") | |
1692 | (when (member (current-word) '("if" "elif" "except" "for" "while")) | |
1693 | (point-marker))) | |
1694 | ((string= closing-word "except") | |
1695 | (when (member (current-word) '("try")) | |
1696 | (point-marker))) | |
1697 | ((string= closing-word "finally") | |
1698 | (when (member (current-word) '("except" "else")) | |
1699 | (point-marker)))))))) | |
1700 | ||
1701 | (defun python-info-line-ends-backslash-p () | |
1702 | "Return non-nil if current line ends with backslash." | |
1703 | (string= (or (ignore-errors | |
1704 | (buffer-substring | |
1705 | (line-end-position) | |
1706 | (- (line-end-position) 1))) "") "\\")) | |
1707 | ||
1708 | (defun python-info-continuation-line-p () | |
1709 | "Return non-nil if current line is continuation of another." | |
1710 | (or (python-info-line-ends-backslash-p) | |
1711 | (string-match ",[[:space:]]*$" (buffer-substring | |
1712 | (line-beginning-position) | |
1713 | (line-end-position))) | |
1714 | (save-excursion | |
1715 | (let ((innermost-paren (progn | |
1716 | (goto-char (line-end-position)) | |
1717 | (nth 1 (syntax-ppss))))) | |
1718 | (when (and innermost-paren | |
1719 | (and (<= (line-beginning-position) innermost-paren) | |
1720 | (>= (line-end-position) innermost-paren))) | |
1721 | (goto-char innermost-paren) | |
1722 | (looking-at (python-rx open-paren (* space) line-end))))) | |
1723 | (save-excursion | |
1724 | (back-to-indentation) | |
1725 | (nth 1 (syntax-ppss))))) | |
1726 | ||
1727 | (defun python-info-block-continuation-line-p () | |
1728 | "Return non-nil if current line is a continuation of a block." | |
1729 | (save-excursion | |
1730 | (while (and (not (bobp)) | |
1731 | (python-info-continuation-line-p)) | |
1732 | (forward-line -1)) | |
1733 | (forward-line 1) | |
1734 | (back-to-indentation) | |
1735 | (when (looking-at (python-rx block-start)) | |
1736 | (point-marker)))) | |
1737 | ||
1738 | (defun python-info-assignment-continuation-line-p () | |
1739 | "Return non-nil if current line is a continuation of an assignment." | |
1740 | (save-excursion | |
1741 | (while (and (not (bobp)) | |
1742 | (python-info-continuation-line-p)) | |
1743 | (forward-line -1)) | |
1744 | (forward-line 1) | |
1745 | (back-to-indentation) | |
1746 | (when (and (not (looking-at (python-rx block-start))) | |
1747 | (save-excursion | |
1748 | (and (re-search-forward (python-rx not-simple-operator | |
1749 | assignment-operator | |
1750 | not-simple-operator) | |
1751 | (line-end-position) t) | |
1752 | (not (syntax-ppss-context (syntax-ppss)))))) | |
1753 | (point-marker)))) | |
1754 | ||
1755 | \f | |
1756 | ;;;###autoload | |
1757 | (define-derived-mode python-mode fundamental-mode "Python" | |
1758 | "A major mode for editing Python files." | |
1759 | (set (make-local-variable 'tab-width) 8) | |
1760 | (set (make-local-variable 'indent-tabs-mode) nil) | |
1761 | ||
1762 | (set (make-local-variable 'comment-start) "# ") | |
1763 | (set (make-local-variable 'comment-start-skip) "#+\\s-*") | |
1764 | ||
1765 | (set (make-local-variable 'parse-sexp-lookup-properties) t) | |
1766 | (set (make-local-variable 'parse-sexp-ignore-comments) t) | |
1767 | ||
1768 | (set (make-local-variable 'font-lock-defaults) | |
1769 | '(python-font-lock-keywords | |
1770 | nil nil nil nil | |
1771 | (font-lock-syntactic-keywords . python-font-lock-syntactic-keywords))) | |
1772 | ||
1773 | (set (make-local-variable 'indent-line-function) #'python-indent-line-function) | |
1774 | (set (make-local-variable 'indent-region-function) #'python-indent-region) | |
1775 | ||
1776 | (set (make-local-variable 'paragraph-start) "\\s-*$") | |
1777 | (set (make-local-variable 'fill-paragraph-function) 'python-fill-paragraph-function) | |
1778 | ||
1779 | (set (make-local-variable 'beginning-of-defun-function) | |
1780 | #'python-beginning-of-defun-function) | |
1781 | (set (make-local-variable 'end-of-defun-function) | |
1782 | #'python-end-of-defun-function) | |
1783 | ||
1784 | (add-hook 'completion-at-point-functions | |
1785 | 'python-completion-complete-at-point nil 'local) | |
1786 | ||
1787 | (set (make-local-variable 'add-log-current-defun-function) | |
1788 | #'python-info-current-defun) | |
1789 | ||
1790 | (set (make-local-variable 'eldoc-documentation-function) | |
1791 | #'python-eldoc-function) | |
1792 | ||
1793 | (add-to-list 'hs-special-modes-alist | |
1794 | `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#" | |
1795 | ,(lambda (arg) | |
1796 | (python-end-of-defun-function)) nil)) | |
1797 | ||
1798 | (set (make-local-variable 'outline-regexp) | |
1799 | (python-rx (* space) block-start)) | |
1800 | (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n") | |
1801 | (set (make-local-variable 'outline-level) | |
1802 | #'(lambda () | |
1803 | "`outline-level' function for Python mode." | |
1804 | (1+ (/ (current-indentation) python-indent-offset)))) | |
1805 | ||
1806 | (when python-indent-guess-indent-offset | |
1807 | (python-indent-guess-indent-offset))) | |
1808 | ||
1809 | ||
1810 | (provide 'python) | |
1811 | ;;; python.el ends here |