Mercurial > emacs
annotate lisp/progmodes/python.el @ 54880:f23c0b924770
*** empty log message ***
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Wed, 14 Apr 2004 18:58:45 +0000 |
parents | 3768540a819c |
children | fb2f71e89f5f |
rev | line source |
---|---|
54789 | 1 ;;; python.el --- silly walks for Python |
2 | |
3 ;; Copyright (C) 2003, 04 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Dave Love <fx@gnu.org> | |
6 ;; Created: Nov 2003 | |
7 ;; Keywords: languages | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; This file is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; This file is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; Major mode for editing Python, with support for inferior processes. | |
29 | |
30 ;; There is another Python mode, python-mode.el, used by XEmacs and | |
31 ;; maintained with Python. That isn't covered by an FSF copyright | |
32 ;; assignment, unlike this code, and seems not to be well-maintained | |
33 ;; for Emacs (though I've submitted fixes). This mode is rather | |
34 ;; simpler and is, perhaps, better in other ways. In particular, | |
35 ;; using the syntax functions with text properties maintained by | |
36 ;; font-lock should make it more correct with arbitrary string and | |
37 ;; comment contents. | |
38 | |
39 ;; This doesn't implement all the facilities of python-mode.el. Some | |
40 ;; just need doing, e.g. catching exceptions in the inferior Python | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
41 ;; buffer (but see M-x pdb for debugging). [Actually, the use of |
54789 | 42 ;; `compilation-minor-mode' now is probably enough for that.] Others |
43 ;; don't seem appropriate. For instance, `forward-into-nomenclature' | |
44 ;; should be done separately, since it's not specific to Python, and | |
45 ;; I've installed a minor mode to do the job properly in Emacs 22. | |
46 ;; Other things seem more natural or canonical here, e.g. the | |
47 ;; {beginning,end}-of-defun implementation dealing with nested | |
48 ;; definitions, and the inferior mode following `cmuscheme'. (The | |
49 ;; inferior mode should be able to find the source of errors from | |
50 ;; `python-send-region' & al via `compilation-minor-mode', but I can't | |
51 ;; make that work with the current (March '04) compile.el.) | |
52 ;; Successive TABs cycle between possible indentations for the line. | |
53 | |
54 ;; Even where it has similar facilities, this is incompatible with | |
55 ;; python-mode.el in various respects. For instance, various key | |
56 ;; bindings are changed to obey Emacs conventions, and things like | |
57 ;; marking blocks and `beginning-of-defun' behave differently. | |
58 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
59 ;; TODO: See various Fixmes below. It should be possible to arrange |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
60 ;; some sort of completion using the inferior interpreter. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
61 |
54789 | 62 ;;; Code: |
63 | |
64 ;; It's messy to autoload the relevant comint functions so that comint | |
65 ;; is only required when inferior Python is used. | |
66 (require 'comint) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
67 (eval-when-compile |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
68 (require 'compile) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
69 (autoload 'Info-last "info") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
70 (autoload 'Info-exit "info") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
71 (autoload 'info-lookup-maybe-add-help "info-look")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
72 (autoload 'compilation-start "compile") ; spurious compiler warning anyway |
54789 | 73 |
74 (defgroup python nil | |
75 "Silly walks in the Python language" | |
76 :group 'languages | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
77 :version "21.4" |
54789 | 78 :link '(emacs-commentary-link "python")) |
79 | |
80 ;;;###autoload | |
81 (add-to-list 'interpreter-mode-alist '("jython" . jython-mode)) | |
82 ;;;###autoload | |
83 (add-to-list 'interpreter-mode-alist '("python" . python-mode)) | |
84 ;;;###autoload | |
85 (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode)) | |
86 | |
87 ;;;; Font lock | |
88 | |
89 (defvar python-font-lock-keywords | |
90 `(,(rx (and word-start | |
91 ;; From v 2.3 reference. | |
92 ;; def and class dealt with separately below | |
93 (or "and" "assert" "break" "continue" "del" "elif" "else" | |
94 "except" "exec" "finally" "for" "from" "global" "if" | |
95 "import" "in" "is" "lambda" "not" "or" "pass" "print" | |
96 "raise" "return" "try" "while" "yield" | |
97 ;; Future keywords | |
98 "as" "None") | |
99 word-end)) | |
100 (,(rx (and word-start (group "class") (1+ space) (group (1+ word)))) | |
101 (1 font-lock-keyword-face) (2 font-lock-type-face)) | |
102 (,(rx (and word-start (group "def") (1+ space) (group (1+ word)))) | |
103 (1 font-lock-keyword-face) (2 font-lock-function-name-face)))) | |
104 | |
105 (defconst python-font-lock-syntactic-keywords | |
106 ;; Make outer chars of matching triple-quote sequences into generic | |
107 ;; string delimiters. Fixme: Is there a better way? | |
108 `((,(rx (and (group (optional (any "uUrR"))) ; prefix gets syntax property | |
109 (optional (any "rR")) ; possible second prefix | |
110 (group (syntax string-quote)) ; maybe gets property | |
111 (backref 2) ; per first quote | |
112 (group (backref 2)))) ; maybe gets property | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
113 (1 (python-quote-syntax 1)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
114 (2 (python-quote-syntax 2)) |
54789 | 115 (3 (python-quote-syntax 3))) |
116 ;; This doesn't really help. | |
117 ;;; (,(rx (and ?\\ (group ?\n))) (1 " ")) | |
118 )) | |
119 | |
120 (defun python-quote-syntax (n) | |
121 "Put `syntax-table' property correctly on triple quote. | |
122 Used for syntactic keywords. N is the match number (1, 2 or 3)." | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
123 ;; Given a triple quote, we have to check the context to know |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
124 ;; whether this is an opening or closing triple or whether it's |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
125 ;; quoted anyhow, and should be ignored. (For that we need to do |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
126 ;; the same job as `syntax-ppss' to be correct and it seems to be OK |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
127 ;; to use it here despite initial worries.) We also have to sort |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
128 ;; out a possible prefix -- well, we don't _have_ to, but I think it |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
129 ;; should be treated as part of the string. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
130 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
131 ;; Test cases: |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
132 ;; ur"""ar""" x='"' # """ |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
133 ;; x = ''' """ ' a |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
134 ;; ''' |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
135 ;; x '"""' x |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
136 (save-excursion |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
137 (goto-char (match-beginning 0)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
138 (unless (eq ?\\ (char-before)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
139 (cond |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
140 ;; Consider property for the last char if in a fenced string. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
141 ((= n 3) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
142 (let ((syntax (syntax-ppss))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
143 (when (eq t (nth 3 syntax)) ; after unclosed fence |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
144 (goto-char (nth 8 syntax)) ; fence position |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
145 ;; Skip any prefix. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
146 (if (memq (char-after) '(?u ?U ?R ?r)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
147 (skip-chars-forward "uUrR")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
148 ;; Is it a matching sequence? |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
149 (if (eq (char-after) (char-after (match-beginning 2))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
150 (eval-when-compile (string-to-syntax "|")))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
151 ;; Consider property for initial char, accounting for prefixes. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
152 ((or (and (= n 2) ; not prefix |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
153 (= (match-beginning 1) (match-end 1))) ; prefix is null |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
154 (and (= n 1) ; prefix |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
155 (/= (match-beginning 1) (match-end 1)))) ; non-empty |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
156 (unless (eq 'string (syntax-ppss-context (syntax-ppss))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
157 (eval-when-compile (string-to-syntax "|"))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
158 ;; Otherwise (we're in a non-matching string) the property is |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
159 ;; nil, which is OK. |
54789 | 160 ))) |
161 | |
162 ;; This isn't currently in `font-lock-defaults' as probably not worth | |
163 ;; it -- we basically only mess with a few normally-symbol characters. | |
164 | |
165 ;; (defun python-font-lock-syntactic-face-function (state) | |
166 ;; "`font-lock-syntactic-face-function' for Python mode. | |
167 ;; Returns the string or comment face as usual, with side effect of putting | |
168 ;; a `syntax-table' property on the inside of the string or comment which is | |
169 ;; the standard syntax table." | |
170 ;; (if (nth 3 state) | |
171 ;; (save-excursion | |
172 ;; (goto-char (nth 8 state)) | |
173 ;; (condition-case nil | |
174 ;; (forward-sexp) | |
175 ;; (error nil)) | |
176 ;; (put-text-property (1+ (nth 8 state)) (1- (point)) | |
177 ;; 'syntax-table (standard-syntax-table)) | |
178 ;; 'font-lock-string-face) | |
179 ;; (put-text-property (1+ (nth 8 state)) (line-end-position) | |
180 ;; 'syntax-table (standard-syntax-table)) | |
181 ;; 'font-lock-comment-face)) | |
182 | |
183 ;;;; Keymap and syntax | |
184 | |
185 (defvar python-mode-map | |
186 (let ((map (make-sparse-keymap))) | |
187 ;; Mostly taken from python-mode.el. | |
188 (define-key map ":" 'python-electric-colon) | |
189 (define-key map "\177" 'python-backspace) | |
190 (define-key map "\C-c<" 'python-shift-left) | |
191 (define-key map "\C-c>" 'python-shift-right) | |
192 (define-key map "\C-c\C-k" 'python-mark-block) | |
193 (define-key map "\C-c\C-n" 'python-next-statement) | |
194 (define-key map "\C-c\C-p" 'python-previous-statement) | |
195 (define-key map "\C-c\C-u" 'python-beginning-of-block) | |
196 (define-key map "\C-c\C-f" 'python-describe-symbol) | |
197 (define-key map "\C-c\C-w" 'python-check) | |
198 (define-key map "\C-c\C-v" 'python-check) ; a la sgml-mode | |
199 (define-key map "\C-c\C-s" 'python-send-string) | |
200 (define-key map [?\C-\M-x] 'python-send-defun) | |
201 (define-key map "\C-c\C-r" 'python-send-region) | |
202 (define-key map "\C-c\M-r" 'python-send-region-and-go) | |
203 (define-key map "\C-c\C-c" 'python-send-buffer) | |
204 (define-key map "\C-c\C-z" 'python-switch-to-python) | |
205 (define-key map "\C-c\C-m" 'python-load-file) | |
206 (define-key map "\C-c\C-l" 'python-load-file) ; a la cmuscheme | |
207 ;; Fixme: Add :help to menu. | |
208 (easy-menu-define python-menu map "Python Mode menu" | |
209 '("Python" | |
210 ["Shift region left" python-shift-left :active mark-active] | |
211 ["Shift region right" python-shift-right :active mark-active] | |
212 "-" | |
213 ["Mark block" python-mark-block] | |
214 ["Mark def/class" mark-defun | |
215 :help "Mark innermost definition around point"] | |
216 "-" | |
217 ["Start of block" python-beginning-of-block] | |
218 ["End of block" python-end-of-block] | |
219 ["Start of def/class" beginning-of-defun | |
220 :help "Go to start of innermost definition around point"] | |
221 ["End of def/class" end-of-defun | |
222 :help "Go to end of innermost definition around point"] | |
223 "-" | |
224 ["Start interpreter" run-python | |
225 :help "Run `inferior' Python in separate buffer"] | |
226 ["Import/reload file" python-load-file | |
227 :help "Load into inferior Python session"] | |
228 ["Eval buffer" python-send-buffer | |
229 :help "Evaluate buffer en bloc in inferior Python session"] | |
230 ["Eval region" python-send-region :active mark-active | |
231 :help "Evaluate region en bloc in inferior Python session"] | |
232 ["Eval def/class" python-send-defun | |
233 :help "Evaluate current definition in inferior Python session"] | |
234 ["Switch to interpreter" python-switch-to-python | |
235 :help "Switch to inferior Python buffer"] | |
236 ["Check file" python-check :help "Run pychecker"] | |
237 ["Debugger" pdb :help "Run pdb under GUD"] | |
238 "-" | |
239 ["Help on symbol" python-describe-symbol | |
240 :help "Use pydoc on symbol at point"])) | |
241 map)) | |
242 | |
243 (defvar python-mode-syntax-table | |
244 (let ((table (make-syntax-table))) | |
245 ;; Give punctuation syntax to ASCII that normally has symbol | |
246 ;; syntax or has word syntax and isn't a letter. | |
247 (let ((symbol (string-to-syntax "_")) | |
248 (sst (standard-syntax-table))) | |
249 (dotimes (i 128) | |
250 (unless (= i ?_) | |
251 (if (equal symbol (aref sst i)) | |
252 (modify-syntax-entry i "." table))))) | |
253 (modify-syntax-entry ?$ "." table) | |
254 (modify-syntax-entry ?% "." table) | |
255 ;; exceptions | |
256 (modify-syntax-entry ?# "<" table) | |
257 (modify-syntax-entry ?\n ">" table) | |
258 (modify-syntax-entry ?' "\"" table) | |
259 (modify-syntax-entry ?` "$" table) | |
260 table)) | |
261 | |
262 ;;;; Utility stuff | |
263 | |
264 (defsubst python-in-string/comment () | |
265 "Return non-nil if point is in a Python literal (a comment or string). | |
266 Optional argument LIM indicates the beginning of the containing form, | |
267 i.e. the limit on how far back to scan." | |
268 (syntax-ppss-context (syntax-ppss))) | |
269 | |
270 (defconst python-space-backslash-table | |
271 (let ((table (copy-syntax-table python-mode-syntax-table))) | |
272 (modify-syntax-entry ?\\ " " table) | |
273 table) | |
274 "`python-mode-syntax-table' with backslash given whitespace syntax.") | |
275 | |
276 (defun python-skip-comments/blanks (&optional backward) | |
277 "Skip comments and blank lines. | |
278 BACKWARD non-nil means go backwards, otherwise go forwards. Backslash is | |
279 treated as whitespace so that continued blank lines are skipped. | |
280 Doesn't move out of comments -- should be outside or at end of line." | |
281 (with-syntax-table python-space-backslash-table | |
282 (forward-comment (if backward | |
283 most-negative-fixnum | |
284 most-positive-fixnum)))) | |
285 | |
286 (defun python-backslash-continuation-line-p () | |
287 "Non-nil if preceding line ends with backslash that is not in a comment." | |
288 (and (eq ?\\ (char-before (line-end-position 0))) | |
289 (not (syntax-ppss-context (syntax-ppss))))) | |
290 | |
291 (defun python-continuation-line-p () | |
292 "Return non-nil if current line continues a previous one. | |
293 The criteria are that the previous line ends in a backslash outside | |
294 comments and strings, or that the bracket/paren nesting depth is nonzero." | |
295 (or (and (eq ?\\ (char-before (line-end-position 0))) | |
296 (not (syntax-ppss-context (syntax-ppss)))) | |
297 (/= 0 (syntax-ppss-depth | |
298 (save-excursion ; syntax-ppss with arg changes point | |
299 (syntax-ppss (line-beginning-position))))))) | |
300 | |
301 (defun python-comment-line-p () | |
302 "Return non-nil if current line has only a comment or is blank." | |
303 (save-excursion | |
304 (back-to-indentation) | |
305 (looking-at (rx (or (syntax comment-start) line-end))))) | |
306 | |
307 (defun python-beginning-of-string () | |
308 "Go to beginning of string around point. | |
309 Do nothing if not in string." | |
310 (let ((state (syntax-ppss))) | |
311 (when (nth 3 state) | |
312 (goto-char (nth 8 state))))) | |
313 | |
314 (defun python-open-block-statement-p (&optional bos) | |
315 "Return non-nil if statement at point opens a block. | |
316 BOS non-nil means point is known to be at beginning of statement." | |
317 (save-excursion | |
318 (unless bos (python-beginning-of-statement)) | |
319 (and (not (python-comment-line-p)) | |
320 (re-search-forward (rx (and ?: (0+ space) | |
321 (optional (and (syntax comment-start) | |
322 (0+ not-newline))) | |
323 line-end)) | |
324 (save-excursion (python-end-of-statement)) | |
325 t) | |
326 (not (python-in-string/comment))))) | |
327 | |
328 (defun python-close-block-statement-p (&optional bos) | |
329 "Return non-nil if current line is a statement closing a block. | |
330 BOS non-nil means point is at beginning of statement. | |
331 The criteria are that the line isn't a comment or in string and starts with | |
332 keyword `raise', `break', `continue' or `pass'." | |
333 (save-excursion | |
334 (unless bos (python-beginning-of-statement)) | |
335 (back-to-indentation) | |
336 (looking-at (rx (and (or "return" "raise" "break" "continue" "pass") | |
337 word-end))))) | |
338 | |
339 (defun python-outdent-p () | |
340 "Return non-nil if current line should outdent a level." | |
341 (save-excursion | |
342 (back-to-indentation) | |
343 (and (looking-at (rx (and (or (and (or "else" "finally") word-end) | |
344 (and (or "except" "elif") word-end | |
345 (1+ (not (any ?:))))) | |
346 (optional space) ":" (optional space) | |
347 (or (syntax comment-start) line-end)))) | |
348 (progn (end-of-line) | |
349 (not (python-in-string/comment))) | |
350 ;; Ensure there's a previous statement and move to it. | |
351 (zerop (python-previous-statement)) | |
352 (not (python-close-block-statement-p t)) | |
353 ;; Fixme: check this | |
354 (not (looking-at (rx (and (or (and (or "if" "elif" "except" | |
355 "for" "while") | |
356 word-end (1+ (not (any ?:)))) | |
357 (and "try" word-end)) | |
358 (optional space) ":" (optional space) | |
359 (or (syntax comment-start) line-end))))) | |
360 (progn (end-of-line) | |
361 (not (python-in-string/comment)))))) | |
362 | |
363 ;;;; Indentation. | |
364 | |
365 (defcustom python-indent 4 | |
366 "*Number of columns for a unit of indentation in Python mode. | |
367 See also `\\[python-guess-indent]'" | |
368 :group 'python | |
369 :type 'integer) | |
370 | |
371 (defcustom python-guess-indent t | |
372 "*Non-nil means Python mode guesses `python-indent' for the buffer." | |
373 :type 'boolean | |
374 :group 'python) | |
375 | |
376 (defcustom python-indent-string-contents t | |
377 "*Non-nil means indent contents of multi-line strings together. | |
378 This means indent them the same as the preceding non-blank line. | |
379 Otherwise indent them to column zero." | |
380 :type '(choice (const :tag "Align with preceding" t) | |
381 (const :tag "Indent to column 0" nil)) | |
382 :group 'python) | |
383 | |
384 (defcustom python-honour-comment-indentation nil | |
385 "Non-nil means indent relative to preceding comment line. | |
386 Only do this for comments where the leading comment character is followed | |
387 by space." | |
388 :type 'boolean | |
389 :group 'python) | |
390 | |
391 (defcustom python-continuation-offset 4 | |
392 "*Number of columns of additional indentation for continuation lines. | |
393 Continuation lines follow a backslash-terminated line starting a statement." | |
394 :group 'python | |
395 :type 'integer) | |
396 | |
397 (defun python-guess-indent () | |
398 "Guess step for indentation of current buffer. | |
399 Set `python-indent' locally to the value guessed." | |
400 (interactive) | |
401 (save-excursion | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
402 (save-restriction |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
403 (widen) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
404 (goto-char (point-min)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
405 (let (done indent) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
406 (while (and (not done) (not (eobp))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
407 (when (and (re-search-forward (rx (and ?: (0+ space) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
408 (or (syntax comment-start) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
409 line-end))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
410 nil 'move) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
411 (python-open-block-statement-p)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
412 (save-excursion |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
413 (python-beginning-of-statement) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
414 (let ((initial (current-indentation))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
415 (if (zerop (python-next-statement)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
416 (setq indent (- (current-indentation) initial))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
417 (if (and (>= indent 2) (<= indent 8)) ; sanity check |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
418 (setq done t)))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
419 (when done |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
420 (when (/= indent (default-value 'python-indent)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
421 (set (make-local-variable 'python-indent) indent) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
422 (unless (= tab-width python-indent) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
423 (setq indent-tabs-mode nil))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
424 indent))))) |
54789 | 425 |
426 (defun python-calculate-indentation () | |
427 "Calculate Python indentation for line at point." | |
428 (save-excursion | |
429 (beginning-of-line) | |
430 (let ((syntax (syntax-ppss)) | |
431 start) | |
432 (cond | |
433 ((eq 'string (syntax-ppss-context syntax)) ; multi-line string | |
434 (if (not python-indent-string-contents) | |
435 0 | |
436 (save-excursion | |
437 ;; Find indentation of preceding non-blank line within string. | |
438 (setq start (nth 8 syntax)) | |
439 (forward-line -1) | |
440 (while (and (< start (point)) (looking-at "\\s-*$")) | |
441 (forward-line -1)) | |
442 (current-indentation)))) | |
443 ((python-continuation-line-p) | |
444 (let ((point (point)) | |
445 (open-start (cadr syntax))) | |
446 (if open-start | |
447 ;; Inside bracketed expression. | |
448 (progn | |
449 (goto-char (1+ open-start)) | |
450 ;; Look for first item in list (preceding point) and | |
451 ;; align with it, if found. | |
452 (if (with-syntax-table python-space-backslash-table | |
453 (let ((parse-sexp-ignore-comments t)) | |
454 (condition-case () | |
455 (progn (forward-sexp) | |
456 (backward-sexp) | |
457 (< (point) point)) | |
458 (error nil)))) | |
459 (current-column) | |
460 ;; Otherwise indent relative to statement start, one | |
461 ;; level per bracketing level. | |
462 (goto-char (1+ open-start)) | |
463 (python-beginning-of-statement) | |
464 (+ (current-indentation) (* (car syntax) python-indent)))) | |
465 ;; Otherwise backslash-continued. | |
466 (forward-line -1) | |
467 (if (python-continuation-line-p) | |
468 ;; We're past first continuation line. Align with | |
469 ;; previous line. | |
470 (current-indentation) | |
471 ;; First continuation line. Indent one step, with an | |
472 ;; extra one if statement opens a block. | |
473 (save-excursion | |
474 (python-beginning-of-statement) | |
475 (+ (current-indentation) python-continuation-offset | |
476 (if (python-open-block-statement-p t) | |
477 python-indent | |
478 0))))))) | |
479 ((bobp) 0) | |
480 ;; Fixme: Like python-mode.el; not convinced by this. | |
481 ((looking-at (rx (and (0+ space) (syntax comment-start) | |
482 (not (any " \t\n"))))) ; non-indentable comment | |
483 (current-indentation)) | |
484 (t (let ((point (point))) | |
485 (if python-honour-comment-indentation | |
486 ;; Back over whitespace, newlines, non-indentable comments. | |
487 (catch 'done | |
488 (while t | |
489 (if (cond ((bobp)) | |
490 ;; not at comment start | |
491 ((not (forward-comment -1)) | |
492 (python-beginning-of-statement) | |
493 t) | |
494 ;; trailing comment | |
495 ((/= (current-column) (current-indentation)) | |
496 (python-beginning-of-statement) | |
497 t) | |
498 ;; indentable comment like python-mode.el | |
499 ((and (looking-at (rx (and (syntax comment-start) | |
500 (or space line-end)))) | |
501 (/= 0 (current-column))))) | |
502 (throw 'done t)))) | |
503 ;; Else back over all comments. | |
504 (python-skip-comments/blanks t) | |
505 (python-beginning-of-statement)) | |
506 ;; don't lose on bogus outdent | |
507 (max 0 (+ (current-indentation) | |
508 (or (cond ((python-open-block-statement-p t) | |
509 python-indent) | |
510 ((python-close-block-statement-p t) | |
511 (- python-indent))) | |
512 (progn (goto-char point) | |
513 (if (python-outdent-p) | |
514 (- python-indent))) | |
515 0))))))))) | |
516 | |
517 ;;;; Cycling through the possible indentations with successive TABs. | |
518 | |
519 ;; These don't need to be buffer-local since they're only relevant | |
520 ;; during a cycle. | |
521 | |
522 ;; Alist of possible indentations and start of statement they would close. | |
523 (defvar python-indent-list nil | |
524 "Internal use.") | |
525 ;; Length of the above | |
526 (defvar python-indent-list-length nil | |
527 "Internal use.") | |
528 ;; Current index into the alist. | |
529 (defvar python-indent-index nil | |
530 "Internal use.") | |
531 | |
532 (defun python-initial-text () | |
533 "Text of line following indentation and ignoring any trailing comment." | |
534 (buffer-substring (+ (line-beginning-position) (current-indentation)) | |
535 (save-excursion | |
536 (end-of-line) | |
537 (forward-comment -1) | |
538 (point)))) | |
539 | |
540 (defun python-indentation-levels () | |
541 "Return a list of possible indentations for this statement. | |
542 Includes the default indentation and those which would close all | |
543 enclosing blocks." | |
544 (save-excursion | |
545 (let ((levels (list (cons (current-indentation) nil)))) | |
546 ;; Only one possibility if we immediately follow a block open or | |
547 ;; are in a continuation line. | |
548 (unless (or (python-continuation-line-p) | |
549 (save-excursion (and (python-previous-statement) | |
550 (python-open-block-statement-p t)))) | |
551 (while (python-beginning-of-block) | |
552 (push (cons (current-indentation) (python-initial-text)) | |
553 levels))) | |
554 levels))) | |
555 | |
556 ;; This is basically what `python-indent-line' would be if we didn't | |
557 ;; do the cycling. | |
558 (defun python-indent-line-1 () | |
559 "Subroutine of `python-indent-line'." | |
560 (let ((target (python-calculate-indentation)) | |
561 (pos (- (point-max) (point)))) | |
562 (if (= target (current-indentation)) | |
563 (if (< (current-column) (current-indentation)) | |
564 (back-to-indentation)) | |
565 (beginning-of-line) | |
566 (delete-horizontal-space) | |
567 (indent-to target) | |
568 (if (> (- (point-max) pos) (point)) | |
569 (goto-char (- (point-max) pos)))))) | |
570 | |
571 ;; Fixme: Is the arg necessary? | |
572 (defun python-indent-line (&optional arg) | |
573 "Indent current line as Python code. | |
574 When invoked via `indent-for-tab-command', cycle through possible | |
575 indentations for current line. The cycle is broken by a command different | |
576 from `indent-for-tab-command', i.e. successive TABs do the cycling." | |
577 (interactive) | |
578 ;; Don't do extra work if invoked via `indent-region', for instance. | |
579 (if (not (eq this-command 'indent-for-tab-command)) | |
580 (python-indent-line-1) | |
581 (if (eq last-command this-command) | |
582 (if (= 1 python-indent-list-length) | |
583 (message "Sole indentation") | |
584 (progn (setq python-indent-index (% (1+ python-indent-index) | |
585 python-indent-list-length)) | |
586 (beginning-of-line) | |
587 (delete-horizontal-space) | |
588 (indent-to (car (nth python-indent-index python-indent-list))) | |
589 (let ((text (cdr (nth python-indent-index | |
590 python-indent-list)))) | |
591 (if text (message "Closes: %s" text))))) | |
592 (python-indent-line-1) | |
593 (setq python-indent-list (python-indentation-levels) | |
594 python-indent-list-length (length python-indent-list) | |
595 python-indent-index (1- python-indent-list-length))))) | |
596 | |
597 ;;;; Movement. | |
598 | |
599 (defun python-beginning-of-defun () | |
600 "`beginning-of-defun-function' for Python. | |
601 Finds beginning of innermost nested class or method definition. | |
602 Returns the name of the definition found at the end, or nil if reached | |
603 start of buffer." | |
604 (let ((ci (current-indentation)) | |
605 (def-re (rx (and line-start (0+ space) (or "def" "class") | |
606 (1+ space) | |
607 (group (1+ (or word (syntax symbol))))))) | |
608 point found lep def-line) | |
609 (if (python-comment-line-p) | |
610 (setq ci most-positive-fixnum)) | |
611 (while (and (not (bobp)) (not found)) | |
612 ;; Treat bol at beginning of function as outside function so | |
613 ;; that successive C-M-a makes progress backwards. | |
614 (setq def-line (looking-at def-re)) | |
615 (unless (bolp) (end-of-line)) | |
616 (setq lep (line-end-position)) | |
617 (if (and (re-search-backward def-re nil 'move) | |
618 ;; Must be less indented or matching top level, or | |
619 ;; equally indented if we started on a definition line. | |
620 (let ((in (current-indentation))) | |
621 (or (and (zerop ci) (zerop in)) | |
622 (= lep (line-end-position)) ; on initial line | |
623 (and def-line (= in ci)) | |
624 (< in ci))) | |
625 (not (python-in-string/comment))) | |
626 (setq found t))))) | |
627 | |
628 (defun python-end-of-defun () | |
629 "`end-of-defun-function' for Python. | |
630 Finds end of innermost nested class or method definition." | |
631 (let ((orig (point)) | |
632 (pattern (rx (and line-start (0+ space) | |
633 (or "def" "class") space)))) | |
634 ;; Go to start of current block and check whether it's at top | |
635 ;; level. If it is, and not a block start, look forward for | |
636 ;; definition statement. | |
637 (when (python-comment-line-p) | |
638 (end-of-line) | |
639 (forward-comment most-positive-fixnum)) | |
640 (if (not (python-open-block-statement-p)) | |
641 (python-beginning-of-block)) | |
642 (if (zerop (current-indentation)) | |
643 (unless (python-open-block-statement-p) | |
644 (while (and (re-search-forward pattern nil 'move) | |
645 (python-in-string/comment))) ; just loop | |
646 (unless (eobp) | |
647 (beginning-of-line))) | |
648 ;; Don't move before top-level statement that would end defun. | |
649 (end-of-line) | |
650 (python-beginning-of-defun)) | |
651 ;; If we got to the start of buffer, look forward for | |
652 ;; definition statement. | |
653 (if (and (bobp) (not (looking-at "def\\|class"))) | |
654 (while (and (not (eobp)) | |
655 (re-search-forward pattern nil 'move) | |
656 (python-in-string/comment)))) ; just loop | |
657 ;; We're at a definition statement (or end-of-buffer). | |
658 (unless (eobp) | |
659 (python-end-of-block) | |
660 ;; Count trailing space in defun (but not trailing comments). | |
661 (skip-syntax-forward " >") | |
662 (beginning-of-line)) | |
663 ;; Catch pathological case like this, where the beginning-of-defun | |
664 ;; skips to a definition we're not in: | |
665 ;; if ...: | |
666 ;; ... | |
667 ;; else: | |
668 ;; ... # point here | |
669 ;; ... | |
670 ;; def ... | |
671 (if (< (point) orig) | |
672 (goto-char (point-max))))) | |
673 | |
674 (defun python-beginning-of-statement () | |
675 "Go to start of current statement. | |
676 Accounts for continuation lines, multi-line strings, and multi-line bracketed | |
677 expressions." | |
678 (beginning-of-line) | |
679 (python-beginning-of-string) | |
680 (while (python-continuation-line-p) | |
681 (beginning-of-line) | |
682 (if (python-backslash-continuation-line-p) | |
683 (while (python-backslash-continuation-line-p) | |
684 (forward-line -1)) | |
685 (python-beginning-of-string) | |
686 ;; Skip forward out of nested brackets. | |
687 (condition-case () ; beware invalid syntax | |
688 (progn (backward-up-list (syntax-ppss-depth (syntax-ppss))) t) | |
689 (error (end-of-line))))) | |
690 (back-to-indentation)) | |
691 | |
692 (defun python-end-of-statement () | |
693 "Go to the end of the current statement and return point. | |
694 Usually this is the start of the next line, but if this is a | |
695 multi-line statement we need to skip over the continuation lines. | |
696 On a comment line, go to end of line." | |
697 (end-of-line) | |
698 (while (let (comment) | |
699 ;; Move past any enclosing strings and sexps, or stop if | |
700 ;; we're in a comment. | |
701 (while (let ((s (syntax-ppss))) | |
702 (cond ((eq 'comment (syntax-ppss-context s)) | |
703 (setq comment t) | |
704 nil) | |
705 ((eq 'string (syntax-ppss-context s)) | |
706 ;; Go to start of string and skip it. | |
707 (goto-char (nth 8 s)) | |
708 (condition-case () ; beware invalid syntax | |
709 (progn (forward-sexp) t) | |
710 (error (end-of-line)))) | |
711 ((> (syntax-ppss-depth s) 0) | |
712 ;; Skip forward out of nested brackets. | |
713 (condition-case () ; beware invalid syntax | |
714 (progn (backward-up-list | |
715 (- (syntax-ppss-depth s))) | |
716 t) | |
717 (error (end-of-line)))))) | |
718 (end-of-line)) | |
719 (unless comment | |
720 (eq ?\\ (char-before)))) ; Line continued? | |
721 (end-of-line 2)) ; Try next line. | |
722 (point)) | |
723 | |
724 (defun python-previous-statement (&optional count) | |
725 "Go to start of previous statement. | |
726 With argument COUNT, do it COUNT times. Stop at beginning of buffer. | |
727 Return count of statements left to move." | |
728 (interactive "p") | |
729 (unless count (setq count 1)) | |
730 (if (< count 0) | |
731 (python-next-statement (- count)) | |
732 (python-beginning-of-statement) | |
733 (while (and (> count 0) (not (bobp))) | |
734 (python-skip-comments/blanks t) | |
735 (python-beginning-of-statement) | |
736 (unless (bobp) (setq count (1- count)))) | |
737 count)) | |
738 | |
739 (defun python-next-statement (&optional count) | |
740 "Go to start of next statement. | |
741 With argument COUNT, do it COUNT times. Stop at end of buffer. | |
742 Return count of statements left to move." | |
743 (interactive "p") | |
744 (unless count (setq count 1)) | |
745 (if (< count 0) | |
746 (python-previous-statement (- count)) | |
747 (beginning-of-line) | |
748 (while (and (> count 0) (not (eobp))) | |
749 (python-end-of-statement) | |
750 (python-skip-comments/blanks) | |
751 (setq count (1- count))) | |
752 count)) | |
753 | |
754 (defun python-beginning-of-block (&optional arg) | |
755 "Go to start of current block. | |
756 With numeric arg, do it that many times. If ARG is negative, call | |
757 `python-end-of-block' instead. | |
758 If point is on the first line of a block, use its outer block. | |
759 If current statement is in column zero, don't move and return nil. | |
760 Otherwise return non-nil." | |
761 (interactive "p") | |
762 (unless arg (setq arg 1)) | |
763 (cond | |
764 ((zerop arg)) | |
765 ((< arg 0) (python-end-of-block (- arg))) | |
766 (t | |
767 (let ((point (point))) | |
768 (if (python-comment-line-p) | |
769 (python-skip-comments/blanks t)) | |
770 (python-beginning-of-statement) | |
771 (let ((ci (current-indentation))) | |
772 (if (zerop ci) | |
773 (not (goto-char point)) ; return nil | |
774 ;; Look upwards for less indented statement. | |
775 (if (catch 'done | |
776 ;;; This is slower than the below. | |
777 ;;; (while (zerop (python-previous-statement)) | |
778 ;;; (when (and (< (current-indentation) ci) | |
779 ;;; (python-open-block-statement-p t)) | |
780 ;;; (beginning-of-line) | |
781 ;;; (throw 'done t))) | |
782 (while (and (zerop (forward-line -1))) | |
783 (when (and (< (current-indentation) ci) | |
784 (not (python-comment-line-p)) | |
785 ;; Move to beginning to save effort in case | |
786 ;; this is in string. | |
787 (progn (python-beginning-of-statement) t) | |
788 (python-open-block-statement-p t)) | |
789 (beginning-of-line) | |
790 (throw 'done t))) | |
791 (not (goto-char point))) ; Failed -- return nil | |
792 (python-beginning-of-block (1- arg))))))))) | |
793 | |
794 (defun python-end-of-block (&optional arg) | |
795 "Go to end of current block. | |
796 With numeric arg, do it that many times. If ARG is negative, call | |
797 `python-beginning-of-block' instead. | |
798 If current statement is in column zero and doesn't open a block, don't | |
799 move and return nil. Otherwise return t." | |
800 (interactive "p") | |
801 (unless arg (setq arg 1)) | |
802 (if (< arg 0) | |
803 (python-beginning-of-block (- arg))) | |
804 (while (and (> arg 0) | |
805 (let* ((point (point)) | |
806 (_ (if (python-comment-line-p) | |
807 (python-skip-comments/blanks t))) | |
808 (ci (current-indentation)) | |
809 (open (python-open-block-statement-p))) | |
810 (if (and (zerop ci) (not open)) | |
811 (not (goto-char point)) | |
812 (catch 'done | |
813 (while (zerop (python-next-statement)) | |
814 (when (or (and open (<= (current-indentation) ci)) | |
815 (< (current-indentation) ci)) | |
816 (python-skip-comments/blanks t) | |
817 (beginning-of-line 2) | |
818 (throw 'done t))) | |
819 (not (goto-char point)))))) | |
820 (setq arg (1- arg))) | |
821 (zerop arg)) | |
822 | |
823 ;;;; Imenu. | |
824 | |
825 (defun python-imenu-create-index () | |
826 "`imenu-create-index-function' for Python. | |
827 | |
828 Makes nested Imenu menus from nested `class' and `def' statements. | |
829 The nested menus are headed by an item referencing the outer | |
830 definition; it has a space prepended to the name so that it sorts | |
831 first with `imenu--sort-by-name'." | |
832 (unless (boundp 'recursing) ; dynamically bound below | |
833 (goto-char (point-min))) ; normal call from Imenu | |
834 (let (index-alist ; accumulated value to return | |
835 name is-class pos) | |
836 (while (re-search-forward | |
837 (rx (and line-start (0+ space) ; leading space | |
838 (or (group "def") (group "class")) ; type | |
839 (1+ space) (group (1+ (or word ?_))))) ; name | |
840 nil t) | |
841 (unless (python-in-string/comment) | |
842 (let ((pos (match-beginning 0)) | |
843 (name (match-string-no-properties 3))) | |
844 (if (match-beginning 2) ; def or class? | |
845 (setq name (concat "class " name))) | |
846 (save-restriction | |
847 (narrow-to-defun) | |
848 (let* ((recursing t) | |
849 (sublist (python-imenu-create-index))) | |
850 (if sublist | |
851 (progn (push (cons (concat " " name) pos) sublist) | |
852 (push (cons name sublist) index-alist)) | |
853 (push (cons name pos) index-alist))))))) | |
854 (nreverse index-alist))) | |
855 | |
856 ;;;; `Electric' commands. | |
857 | |
858 (defun python-electric-colon (arg) | |
859 "Insert a colon and maybe outdent the line if it is a statement like `else'. | |
860 With numeric ARG, just insert that many colons. With \\[universal-argument], | |
861 just insert a single colon." | |
862 (interactive "*P") | |
863 (self-insert-command (if (not (integerp arg)) 1 arg)) | |
864 (and (not arg) | |
865 (eolp) | |
866 (python-outdent-p) | |
867 (not (python-in-string/comment)) | |
868 (> (current-indentation) (python-calculate-indentation)) | |
869 (python-indent-line))) ; OK, do it | |
870 (put 'python-electric-colon 'delete-selection t) | |
871 | |
872 (defun python-backspace (arg) | |
873 "Maybe delete a level of indentation on the current line. | |
874 If not at the end of line's indentation, or on a comment line, just call | |
875 `backward-delete-char-untabify'. With ARG, repeat that many times." | |
876 (interactive "*p") | |
877 (if (or (/= (current-indentation) (current-column)) | |
878 (bolp) | |
879 (python-continuation-line-p)) | |
880 (backward-delete-char-untabify arg) | |
881 (let ((indent 0)) | |
882 (save-excursion | |
883 (while (and (> arg 0) (python-beginning-of-block)) | |
884 (setq arg (1- arg))) | |
885 (when (zerop arg) | |
886 (setq indent (current-indentation)) | |
887 (message "Closes %s" (python-initial-text)))) | |
888 (delete-horizontal-space) | |
889 (indent-to indent)))) | |
890 (put 'python-backspace 'delete-selection 'supersede) | |
891 | |
892 ;;;; pychecker | |
893 | |
894 (defcustom python-check-command "pychecker --stdlib" | |
895 "*Command used to check a Python file." | |
896 :type 'string | |
897 :group 'python) | |
898 | |
899 (defvar python-saved-check-command nil | |
900 "Internal use.") | |
901 | |
902 ;; After `sgml-validate-command'. | |
903 (defun python-check (command) | |
904 "Check a Python file (default current buffer's file). | |
905 Runs COMMAND, a shell command, as if by `compile'. | |
906 See `python-check-command' for the default." | |
907 (interactive | |
908 (list (read-string "Checker command: " | |
909 (or python-saved-check-command | |
910 (concat python-check-command " " | |
911 (let ((name (buffer-file-name))) | |
912 (if name | |
913 (file-name-nondirectory name)))))))) | |
914 (setq python-saved-check-command command) | |
915 (save-some-buffers (not compilation-ask-about-save) nil) | |
916 (compilation-start command)) | |
917 | |
918 ;;;; Inferior mode stuff (following cmuscheme). | |
919 | |
920 (defcustom python-python-command "python" | |
921 "*Shell command to run Python interpreter. | |
922 Any arguments can't contain whitespace." | |
923 :group 'python | |
924 :type 'string) | |
925 | |
926 (defcustom python-jython-command "jython" | |
927 "*Shell command to run Jython interpreter. | |
928 Any arguments can't contain whitespace." | |
929 :group 'python | |
930 :type 'string) | |
931 | |
932 (defvar python-command python-python-command | |
933 "Actual command used to run Python. | |
934 May be `python-python-command' or `python-jython-command'. | |
935 Additional arguments are added when the command is used by `run-python' | |
936 et al.") | |
937 | |
938 (defvar python-buffer nil | |
939 "*The current python process buffer. | |
940 To run multiple Python processes, start the first with \\[run-python]. | |
941 It will be in a buffer named *Python*. Rename that with | |
942 \\[rename-buffer]. Now start a new process with \\[run-python]. It | |
943 will be in a new buffer, named *Python*. Switch between the different | |
944 process buffers with \\[switch-to-buffer]. | |
945 | |
946 Commands that send text from source buffers to Python processes have | |
947 to choose a process to send to. This is determined by global variable | |
948 `python-buffer'. Suppose you have three inferior Pythons running: | |
949 Buffer Process | |
950 foo python | |
951 bar python<2> | |
952 *Python* python<3> | |
953 If you do a \\[python-send-region-and-go] command on some Python source | |
954 code, what process does it go to? | |
955 | |
956 - In a process buffer (foo, bar, or *Python*), send it to that process. | |
957 - In some other buffer (e.g. a source file), send it to the process | |
958 attached to `python-buffer'. | |
959 Process selection is done by function `python-proc'. | |
960 | |
961 Whenever \\[run-python] starts a new process, it resets `python-buffer' | |
962 to be the new process's buffer. If you only run one process, this will | |
963 do the right thing. If you run multiple processes, you can change | |
964 `python-buffer' to another process buffer with \\[set-variable].") | |
965 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
966 (defconst python-compilation-regexp-alist |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
967 `((,(rx (and line-start (1+ (any " \t")) "File \"" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
968 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
969 "\", line " (group (1+ digit)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
970 1 python-compilation-line-number)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
971 "`compilation-error-regexp-alist' for inferior Python.") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
972 |
54789 | 973 (define-derived-mode inferior-python-mode comint-mode "Inferior Python" |
974 "Major mode for interacting with an inferior Python process. | |
975 A Python process can be started with \\[run-python]. | |
976 | |
977 Hooks `comint-mode-hook' and `inferior-python-mode-hook' are run in | |
978 that order. | |
979 | |
980 You can send text to the inferior Python process from other buffers containing | |
981 Python source. | |
982 * `python-switch-to-python' switches the current buffer to the Python | |
983 process buffer. | |
984 * `python-send-region' sends the current region to the Python process. | |
985 * `python-send-region-and-go' switches to the Python process buffer | |
986 after sending the text. | |
987 For running multiple processes in multiple buffers, see `python-buffer'. | |
988 | |
989 \\{inferior-python-mode-map}" | |
990 :group 'python | |
991 (set-syntax-table python-mode-syntax-table) | |
992 (setq mode-line-process '(":%s")) | |
993 ;; Fixme: Maybe install some python-mode bindings too. | |
994 (define-key inferior-python-mode-map "\C-c\C-l" 'python-load-file) | |
995 (define-key inferior-python-mode-map "\C-c\C-z" 'python-switch-to-python) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
996 (add-hook 'comint-input-filter-functions 'python-input-filter nil t) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
997 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
998 nil t) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
999 ;; Still required by `comint-redirect-send-command', for instance: |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1000 (set (make-local-variable 'comint-prompt-regexp) "^\\([>.]\\{3\\} \\)+") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1001 (set (make-local-variable 'compilation-error-regexp-alist) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1002 python-compilation-regexp-alist) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1003 (compilation-shell-minor-mode 1)) |
54789 | 1004 |
1005 (defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'" | |
1006 "*Input matching this regexp is not saved on the history list. | |
1007 Default ignores all inputs of 0, 1, or 2 non-blank characters." | |
1008 :type 'regexp | |
1009 :group 'python) | |
1010 | |
1011 (defvar python-orig-start-line nil | |
1012 "Line number at start of region sent to inferior Python.") | |
1013 | |
1014 (defvar python-orig-file nil | |
1015 "File name to associate with errors found in inferior Python.") | |
1016 | |
1017 (defun python-input-filter (str) | |
1018 "`comint-input-filter' function for inferior Python. | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1019 Don't save anything for STR matching `inferior-python-filter-regexp'. |
54789 | 1020 Also resets variables for adjusting error messages." |
1021 (setq python-orig-file nil | |
1022 python-orig-start-line 1) | |
1023 (not (string-match inferior-python-filter-regexp str))) | |
1024 | |
1025 ;; Fixme: Loses with quoted whitespace. | |
1026 (defun python-args-to-list (string) | |
1027 (let ((where (string-match "[ \t]" string))) | |
1028 (cond ((null where) (list string)) | |
1029 ((not (= where 0)) | |
1030 (cons (substring string 0 where) | |
1031 (python-args-to-list (substring string (+ 1 where))))) | |
1032 (t (let ((pos (string-match "[^ \t]" string))) | |
1033 (if pos (python-args-to-list (substring string pos)))))))) | |
1034 | |
1035 (defun python-compilation-line-number (file col) | |
1036 "Return error descriptor of error found for FILE, column COL. | |
1037 Used as line-number hook function in `python-compilation-regexp-alist'." | |
1038 (let ((line (save-excursion | |
1039 (goto-char (match-beginning 2)) | |
1040 (read (current-buffer))))) | |
1041 (list (point-marker) (if python-orig-file | |
1042 (list python-orig-file default-directory) | |
1043 file) | |
1044 (+ line (1- python-orig-start-line)) | |
1045 nil))) | |
1046 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1047 (defvar python-preoutput-result nil |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1048 "Data from output line last `_emacs_out' line seen by the preoutput filter.") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1049 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1050 (defvar python-preoutput-continuation nil |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1051 "If non-nil, funcall this when `python-preoutput-filter' sees `_emacs_ok'.") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1052 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1053 ;; Using this stops us getting lines in the buffer like |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1054 ;; >>> ... ... >>> |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1055 ;; Also look for (and delete) an `_emacs_ok' string and call |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1056 ;; `python-preoutput-continuation' if we get it. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1057 (defun python-preoutput-filter (s) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1058 "`comint-preoutput-filter-functions' function: ignore prompts not at bol." |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1059 (cond ((and (string-match "\\`[.>]\\{3\\} \\'" s) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1060 (/= (let ((inhibit-field-text-motion t)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1061 (line-beginning-position)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1062 (point))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1063 "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1064 ((string= s "_emacs_ok\n") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1065 (when python-preoutput-continuation |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1066 (funcall python-preoutput-continuation) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1067 (setq python-preoutput-continuation nil)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1068 "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1069 ((string-match "_emacs_out \\(.*\\)\n" s) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1070 (setq python-preoutput-result (match-string 1 s)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1071 "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1072 (t s))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1073 |
54789 | 1074 ;;;###autoload |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1075 (defun run-python (&optional cmd noshow) |
54789 | 1076 "Run an inferior Python process, input and output via buffer *Python*. |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1077 CMD is the Python command to run. NOSHOW non-nil means don't show the |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1078 buffer automatically. |
54789 | 1079 If there is a process already running in `*Python*', switch to |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1080 that buffer. Interactively a prefix arg, allows you to edit the initial |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1081 command line (default is the value of `python-command'); `-i' etc. args |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1082 will be added to this as appropriate. Runs the hooks |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1083 `inferior-python-mode-hook' (after the `comint-mode-hook' is run). |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1084 \(Type \\[describe-mode] in the process buffer for a list of commands.)" |
54789 | 1085 (interactive (list (if current-prefix-arg |
1086 (read-string "Run Python: " python-command) | |
1087 python-command))) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1088 (unless cmd (setq cmd python-python-command)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1089 (setq python-command cmd) |
54789 | 1090 ;; Fixme: Consider making `python-buffer' buffer-local as a buffer |
1091 ;; (not a name) in Python buffers from which `run-python' &c is | |
1092 ;; invoked. Would support multiple processes better. | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1093 (unless (comint-check-proc "*Python*") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1094 (let ((cmdlist (append (python-args-to-list cmd) '("-i")))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1095 (set-buffer (apply 'make-comint "Python" (car cmdlist) nil |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1096 (cdr cmdlist)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1097 (inferior-python-mode) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1098 ;; Load function defintions we need. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1099 ;; Before the preoutput function was used, this was done via -c in |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1100 ;; cmdlist, but that loses the banner and doesn't run the startup |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1101 ;; file. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1102 (python-send-string "\ |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1103 def _emacs_execfile (file): # execute file and remove it |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1104 from os import remove |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1105 try: execfile (file, globals (), globals ()) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1106 finally: remove (file) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1107 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1108 def _emacs_args (name): # get arglist of name for eldoc &c |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1109 import inspect |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1110 parts = name.split ('.') |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1111 if len (parts) > 1: |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1112 try: exec 'import ' + parts[0] |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1113 except: return None |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1114 try: exec 'func='+name # lose if name is keyword or undefined |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1115 except: return None |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1116 if inspect.isbuiltin (func): |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1117 doc = func.__doc__ |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1118 if doc.find (' ->') != -1: |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1119 print '_emacs_out', doc.split (' ->')[0] |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1120 elif doc.find ('\n') != -1: |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1121 print '_emacs_out', doc.split ('\n')[0] |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1122 return None |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1123 if inspect.ismethod (func): func = func.im_func |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1124 if not inspect.isfunction (func): |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1125 return None |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1126 (args, varargs, varkw, defaults) = inspect.getargspec (func) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1127 print '_emacs_out', func.__name__+inspect.formatargspec (args, varargs, varkw, defaults) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1128 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1129 print '_emacs_ok'")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1130 (unless noshow (pop-to-buffer (setq python-buffer "*Python*")))) |
54789 | 1131 |
1132 (defun python-mouse-2-command (event) | |
1133 "Command bound to `mouse-2' in inferior Python buffer. | |
1134 Selects Comint or Compilation mode command as appropriate." | |
1135 (interactive "e") | |
1136 ;; This only works with the font-lock-based compilation mode. | |
1137 (call-interactively | |
1138 (lookup-key (if (save-window-excursion | |
1139 (save-excursion | |
1140 (mouse-set-point event) | |
1141 (consp (get-text-property (line-beginning-position) | |
1142 'message)))) | |
1143 compilation-mode-map | |
1144 comint-mode-map) | |
1145 [mouse-2]))) | |
1146 | |
1147 (defun python-RET-command () | |
1148 "Command bound to `RET' in inferior Python buffer. | |
1149 Selects Comint or Compilation mode command as appropriate." | |
1150 (interactive) | |
1151 ;; This only works with the font-lock-based compilation mode. | |
1152 (call-interactively | |
1153 (lookup-key (if (consp (get-text-property (line-beginning-position) | |
1154 'message)) | |
1155 compilation-mode-map | |
1156 comint-mode-map) | |
1157 "\C-m"))) | |
1158 | |
1159 (defun python-send-region (start end) | |
1160 "Send the region to the inferior Python process." | |
1161 ;; The region is evaluated from a temporary file. This avoids | |
1162 ;; problems with blank lines, which have different semantics | |
1163 ;; interactively and in files. It also saves the inferior process | |
1164 ;; buffer filling up with interpreter prompts. We need a function | |
1165 ;; to remove the temporary file when it has been evaluated, which | |
1166 ;; unfortunately means using a not-quite pristine interpreter | |
1167 ;; initially. Unfortunately we also get tracebacks which look like: | |
1168 ;; | |
1169 ;; >>> Traceback (most recent call last): | |
1170 ;; File "<stdin>", line 1, in ? | |
1171 ;; File "<string>", line 4, in _emacs_execfile | |
1172 ;; File "/tmp/py7734RSB", line 11 | |
1173 ;; | |
1174 ;; The compilation-minor-mode parsing takes care of relating the | |
1175 ;; reference to the temporary file to the source. Fixme: | |
1176 ;; comint-filter the first two lines of the traceback? | |
1177 (interactive "r") | |
1178 (let* ((f (make-temp-file "py")) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1179 (command (format "_emacs_execfile(%S)" f)) |
54789 | 1180 (orig-file (buffer-file-name)) |
1181 (orig-line (save-restriction | |
1182 (widen) | |
1183 (line-number-at-pos start)))) | |
1184 (if (save-excursion | |
1185 (goto-char start) | |
1186 (/= 0 (current-indentation))) ; need dummy block | |
1187 (write-region "if True:\n" nil f nil 'nomsg)) | |
1188 (write-region start end f t 'nomsg) | |
1189 (when python-buffer | |
1190 (with-current-buffer python-buffer | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1191 (let ((end (marker-position (process-mark (python-proc))))) |
54789 | 1192 (set (make-local-variable 'python-orig-file) orig-file) |
1193 (set (make-local-variable 'python-orig-start-line) orig-line) | |
1194 (set (make-local-variable 'compilation-error-list) nil) | |
1195 (let ((comint-input-filter-functions | |
1196 (delete 'python-input-filter comint-input-filter-functions))) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1197 (python-send-string command)) |
54789 | 1198 (set-marker compilation-parsing-end end) |
1199 (setq compilation-last-buffer (current-buffer))))))) | |
1200 | |
1201 (defun python-send-string (string) | |
1202 "Evaluate STRING in inferior Python process." | |
1203 (interactive "sPython command: ") | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1204 (comint-send-string (python-proc) string) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1205 (comint-send-string (python-proc) "\n\n")) |
54789 | 1206 |
1207 (defun python-send-buffer () | |
1208 "Send the current buffer to the inferior Python process." | |
1209 (interactive) | |
1210 (python-send-region (point-min) (point-max))) | |
1211 | |
1212 (defun python-send-defun () | |
1213 "Send the current defun (class or method) to the inferior Python process." | |
1214 (interactive) | |
1215 (save-excursion (python-send-region (progn (beginning-of-defun) (point)) | |
1216 (progn (end-of-defun) (point))))) | |
1217 | |
1218 (defun python-switch-to-python (eob-p) | |
1219 "Switch to the Python process buffer. | |
1220 With prefix arg, position cursor at end of buffer." | |
1221 (interactive "P") | |
1222 (if (get-buffer python-buffer) | |
1223 (pop-to-buffer python-buffer) | |
1224 (error "No current process buffer. See variable `python-buffer'")) | |
1225 (when eob-p | |
1226 (push-mark) | |
1227 (goto-char (point-max)))) | |
1228 | |
1229 (add-to-list 'debug-ignored-errors "^No current process buffer.") | |
1230 | |
1231 (defun python-send-region-and-go (start end) | |
1232 "Send the region to the inferior Python process. | |
1233 Then switch to the process buffer." | |
1234 (interactive "r") | |
1235 (python-send-region start end) | |
1236 (python-switch-to-python t)) | |
1237 | |
1238 (defcustom python-source-modes '(python-mode jython-mode) | |
1239 "*Used to determine if a buffer contains Python source code. | |
1240 If it's loaded into a buffer that is in one of these major modes, it's | |
1241 considered a Python source file by `python-load-file'. | |
1242 Used by these commands to determine defaults." | |
1243 :type '(repeat function) | |
1244 :group 'python) | |
1245 | |
1246 (defvar python-prev-dir/file nil | |
1247 "Caches (directory . file) pair used in the last `python-load-file' command. | |
1248 Used for determining the default in the next one.") | |
1249 | |
1250 (defun python-load-file (file-name) | |
1251 "Load a Python file FILE-NAME into the inferior Python process. | |
1252 If the file has extension `.py' import or reload it as a module. | |
1253 Treating it as a module keeps the global namespace clean, provides | |
1254 function location information for debugging, and supports users of | |
1255 module-qualified names." | |
1256 (interactive (comint-get-source "Load Python file: " python-prev-dir/file | |
1257 python-source-modes | |
1258 t)) ; because execfile needs exact name | |
1259 (comint-check-source file-name) ; Check to see if buffer needs saved. | |
1260 (setq python-prev-dir/file (cons (file-name-directory file-name) | |
1261 (file-name-nondirectory file-name))) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1262 (when python-buffer |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1263 (with-current-buffer python-buffer |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1264 (let ((end (marker-position (process-mark (python-proc))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1265 (set (make-local-variable 'compilation-error-list) nil) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1266 ;; (set (make-local-variable 'compilation-old-error-list) nil) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1267 (let ((comint-input-filter-functions |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1268 (delete 'python-input-filter comint-input-filter-functions))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1269 (python-send-string |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1270 (if (string-match "\\.py\\'" file-name) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1271 ;; Fixme: make sure the directory is in the path list |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1272 (let ((module (file-name-sans-extension |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1273 (file-name-nondirectory file-name)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1274 (set (make-local-variable 'python-orig-file) nil) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1275 (set (make-local-variable 'python-orig-start-line) nil) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1276 (format "\ |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1277 try: |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1278 if globals().has_key(%S): reload(%s) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1279 else: import %s |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1280 except: None |
54789 | 1281 " module module module)) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1282 (set (make-local-variable 'python-orig-file) file-name) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1283 (set (make-local-variable 'python-orig-start-line) 1) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1284 (format "execfile('%s')" file-name)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1285 (set-marker compilation-parsing-end end) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1286 (setq compilation-last-buffer (current-buffer)))))) |
54789 | 1287 |
1288 ;; Fixme: Should this start a process if there isn't one? (Unlike cmuscheme.) | |
1289 (defun python-proc () | |
1290 "Return the current Python process. See variable `python-buffer'." | |
1291 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-python-mode) | |
1292 (current-buffer) | |
1293 python-buffer)))) | |
1294 (or proc (error "No current process. See variable `python-buffer'")))) | |
1295 | |
1296 ;;;; Context-sensitive help. | |
1297 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1298 (defconst python-dotty-syntax-table |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1299 (let ((table (make-syntax-table))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1300 (set-char-table-parent table python-mode-syntax-table) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1301 (modify-syntax-entry ?. "_" table) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1302 table) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1303 "Syntax table giving `.' symbol syntax. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1304 Otherwise inherits from `python-mode-syntax-table'.") |
54789 | 1305 |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1306 ;; Fixme: Should this actually be used instead of info-look, i.e. be |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1307 ;; bound to C-h S? |
54789 | 1308 (defun python-describe-symbol (symbol) |
1309 "Get help on SYMBOL using `pydoc'. | |
1310 Interactively, prompt for symbol." | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1311 ;; Note that we do this in the inferior process, not a separate one to |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1312 ;; ensure the environment is appropriate. |
54789 | 1313 (interactive |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1314 (let ((symbol (with-syntax-table python-dotty-syntax-table |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1315 (current-word))) |
54789 | 1316 (enable-recursive-minibuffers t) |
1317 val) | |
1318 (setq val (read-string (if symbol | |
1319 (format "Describe variable (default %s): " | |
1320 symbol) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1321 "Describe symbol: ") |
54789 | 1322 nil nil symbol)) |
1323 (list (or val symbol)))) | |
1324 (if (equal symbol "") (error "No symbol")) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1325 (let* ((func `(lambda () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1326 (comint-redirect-send-command (format "help(%S)\n" ,symbol) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1327 "*Help*" nil)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1328 ;; Ensure we have a suitable help buffer. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1329 (let (temp-buffer-show-hook) ; avoid xref stuff |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1330 (with-output-to-temp-buffer "*Help*" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1331 (with-current-buffer standard-output |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1332 (set (make-local-variable 'comint-redirect-subvert-readonly) t)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1333 (if (and python-buffer (get-buffer python-buffer)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1334 (with-current-buffer python-buffer |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1335 (funcall func)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1336 (setq python-preoutput-continuation func) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1337 (run-python nil t)))) |
54789 | 1338 |
1339 (add-to-list 'debug-ignored-errors "^No symbol") | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1340 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1341 ;; Fixme: try to make it work with point in the arglist. Also, is |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1342 ;; there anything reasonable we can do with random methods? |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1343 ;; (Currently only works with functions.) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1344 (defun python-eldoc-function () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1345 "`eldoc-print-current-symbol-info' for Python. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1346 Only works when point is in a function name, not its arglist, for instance. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1347 Assumes an inferior Python is running." |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1348 (let ((symbol (with-syntax-table python-dotty-syntax-table |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1349 (current-word))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1350 (proc (and python-buffer (python-proc)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1351 (when (and proc symbol) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1352 (python-send-string |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1353 (format "_emacs_args(%S)" symbol)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1354 (setq python-preoutput-result nil) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1355 (accept-process-output proc 1) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1356 python-preoutput-result))) |
54789 | 1357 |
1358 ;;;; Info-look functionality. | |
1359 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1360 (defun python-after-info-look () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1361 "Set up info-look for Python. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1362 Used with `eval-after-load'." |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1363 (let* ((version (let ((s (shell-command-to-string (concat python-command |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1364 " -V")))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1365 (string-match "^Python \\([0-9]+\\.[0-9]+\\>\\)" s) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1366 (match-string 1 s))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1367 ;; Whether info files have a Python version suffix, e.g. in Debian. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1368 (versioned |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1369 (with-temp-buffer |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1370 (Info-mode) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1371 (condition-case () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1372 (Info-goto-node (format "(python%s-lib)Miscellaneous Index" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1373 version)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1374 (error nil))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1375 (info-lookup-maybe-add-help |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1376 :mode 'python-mode |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1377 :regexp "[[:alnum:]_]+" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1378 :doc-spec |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1379 ;; Fixme: Can this reasonably be made specific to indices with |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1380 ;; different rules? Is the order of indices optimal? |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1381 ;; (Miscellaneous in -ref first prefers lookup of keywords, for |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1382 ;; instance.) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1383 (if versioned |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1384 ;; The empty prefix just gets us highlighted terms. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1385 `((,(concat "(python" version "-ref)Miscellaneous Index") nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1386 (,(concat "(python" version "-ref)Module Index" nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1387 (,(concat "(python" version "-ref)Function-Method-Variable Index" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1388 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1389 (,(concat "(python" version "-ref)Class-Exception-Object Index" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1390 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1391 (,(concat "(python" version "-lib)Module Index" nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1392 (,(concat "(python" version "-lib)Class-Exception-Object Index" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1393 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1394 (,(concat "(python" version "-lib)Function-Method-Variable Index" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1395 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1396 (,(concat "(python" version "-lib)Miscellaneous Index" nil ""))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1397 '(("(python-ref)Miscellaneous Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1398 ("(python-ref)Module Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1399 ("(python-ref)Function-Method-Variable Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1400 ("(python-ref)Class-Exception-Object Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1401 ("(python-lib)Module Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1402 ("(python-lib)Class-Exception-Object Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1403 ("(python-lib)Function-Method-Variable Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1404 ("(python-lib)Miscellaneous Index" nil "")))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1405 (eval-after-load "info-look" '(python-after-info-look)) |
54789 | 1406 |
1407 ;;;; Miscellancy. | |
1408 | |
1409 (defcustom python-jython-packages '("java" "javax" "org" "com") | |
1410 "Packages implying `jython-mode'. | |
1411 If these are imported near the beginning of the buffer, `python-mode' | |
1412 actually punts to `jython-mode'." | |
1413 :type '(repeat string) | |
1414 :group 'python) | |
1415 | |
1416 ;; Called from `python-mode', this causes a recursive call of the | |
1417 ;; mode. See logic there to break out of the recursion. | |
1418 (defun python-maybe-jython () | |
1419 "Invoke `jython-mode' if the buffer appears to contain Jython code. | |
1420 The criterion is either a match for `jython-mode' via | |
1421 `interpreter-mode-alist' or an import of a module from the list | |
1422 `python-jython-packages'." | |
1423 ;; The logic is taken from python-mode.el. | |
1424 (save-excursion | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1425 (save-restriction |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1426 (widen) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1427 (goto-char (point-min)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1428 (let ((interpreter (if (looking-at auto-mode-interpreter-regexp) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1429 (match-string 2)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1430 (if (and interpreter (eq 'jython-mode |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1431 (cdr (assoc (file-name-nondirectory |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1432 interpreter) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1433 interpreter-mode-alist)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1434 (jython-mode) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1435 (if (catch 'done |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1436 (while (re-search-forward |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1437 (rx (and line-start (or "import" "from") (1+ space) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1438 (group (1+ (not (any " \t\n.")))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1439 10000 ; Probably not worth customizing. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1440 t) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1441 (if (member (match-string 1) python-jython-packages) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1442 (throw 'done t)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1443 (jython-mode))))))) |
54789 | 1444 |
1445 (defun python-fill-paragraph (&optional justify) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1446 "`fill-paragraph-function' handling comments and multi-line strings. |
54789 | 1447 If any of the current line is a comment, fill the comment or the |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1448 paragraph of it that point is in, preserving the comment's |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1449 indentation and initial comment characters. Similarly if the end |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1450 of the current line is in or at the end of a multi-line string. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1451 Otherwise, do nothing." |
54789 | 1452 (interactive "P") |
1453 (or (fill-comment-paragraph justify) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1454 ;; The `paragraph-start' and `paragraph-separate' variables |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1455 ;; don't allow us to delimit the last paragraph in a multi-line |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1456 ;; string properly, so narrow to the string and then fill around |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1457 ;; (the end of) the current line. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1458 (save-excursion |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1459 (end-of-line) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1460 (let* ((syntax (syntax-ppss)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1461 (orig (point)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1462 (start (nth 8 syntax)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1463 end) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1464 (cond ((eq t (nth 3 syntax)) ; in fenced string |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1465 (goto-char (nth 8 syntax)) ; string start |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1466 (condition-case () ; for unbalanced quotes |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1467 (progn (forward-sexp) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1468 (setq end (point))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1469 (error (setq end (point-max))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1470 ((re-search-backward "\\s|\\s-*\\=" nil t) ; end of fenced |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1471 ; string |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1472 (forward-char) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1473 (setq end (point)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1474 (condition-case () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1475 (progn (backward-sexp) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1476 (setq start (point))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1477 (error nil)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1478 (when end |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1479 (save-restriction |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1480 (narrow-to-region start end) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1481 (goto-char orig) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1482 (fill-paragraph justify)))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1483 t) |
54789 | 1484 |
1485 (defun python-shift-left (start end &optional count) | |
1486 "Shift lines in region COUNT (the prefix arg) columns to the left. | |
1487 COUNT defaults to `python-indent'. If region isn't active, just shift | |
1488 current line. The region shifted includes the lines in which START and | |
1489 END lie. It is an error if any lines in the region are indented less than | |
1490 COUNT columns." | |
1491 (interactive (if mark-active | |
1492 (list (region-beginning) (region-end) current-prefix-arg) | |
1493 (list (point) (point) current-prefix-arg))) | |
1494 (if count | |
1495 (setq count (prefix-numeric-value count)) | |
1496 (setq count python-indent)) | |
1497 (when (> count 0) | |
1498 (save-excursion | |
1499 (goto-char start) | |
1500 (while (< (point) end) | |
1501 (if (and (< (current-indentation) count) | |
1502 (not (looking-at "[ \t]*$"))) | |
1503 (error "Can't shift all lines enough")) | |
1504 (forward-line)) | |
1505 (indent-rigidly start end (- count))))) | |
1506 | |
1507 (add-to-list 'debug-ignored-errors "^Can't shift all lines enough") | |
1508 | |
1509 (defun python-shift-right (start end &optional count) | |
1510 "Shift lines in region COUNT (the prefix arg) columns to the right. | |
1511 COUNT defaults to `python-indent'. If region isn't active, just shift | |
1512 current line. The region shifted includes the lines in which START and | |
1513 END lie." | |
1514 (interactive (if mark-active | |
1515 (list (region-beginning) (region-end) current-prefix-arg) | |
1516 (list (point) (point) current-prefix-arg))) | |
1517 (if count | |
1518 (setq count (prefix-numeric-value count)) | |
1519 (setq count python-indent)) | |
1520 (indent-rigidly start end count)) | |
1521 | |
1522 (defun python-outline-level () | |
1523 "`outline-level' function for Python mode. | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1524 The level is the number of `python-indent' steps of indentation |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1525 of current line." |
54789 | 1526 (/ (current-indentation) python-indent)) |
1527 | |
1528 ;; Fixme: Consider top-level assignments, imports, &c. | |
1529 (defun python-current-defun () | |
1530 "`add-log-current-defun-function' for Python." | |
1531 (save-excursion | |
1532 ;; Move up the tree of nested `class' and `def' blocks until we | |
1533 ;; get to zero indentation, accumulating the defined names. | |
1534 (let ((start t) | |
1535 accum) | |
1536 (while (or start (> (current-indentation) 0)) | |
1537 (setq start nil) | |
1538 (python-beginning-of-block) | |
1539 (end-of-line) | |
1540 (beginning-of-defun) | |
1541 (if (looking-at (rx (and (0+ space) (or "def" "class") (1+ space) | |
1542 (group (1+ (or word (syntax symbol)))) | |
1543 word-end))) | |
1544 (push (match-string 1) accum))) | |
1545 (if accum (mapconcat 'identity accum "."))))) | |
1546 | |
1547 (defun python-mark-block () | |
1548 "Mark the block around point. | |
1549 Uses `python-beginning-of-block', `python-end-of-block'." | |
1550 (interactive) | |
1551 (push-mark) | |
1552 (python-beginning-of-block) | |
1553 (push-mark (point) nil t) | |
1554 (python-end-of-block) | |
1555 (exchange-point-and-mark)) | |
1556 | |
1557 ;;;; Modes. | |
1558 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1559 (defvar outline-heading-end-regexp) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1560 (defvar eldoc-print-current-symbol-info-function) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1561 |
54789 | 1562 ;;;###autoload |
1563 (define-derived-mode python-mode fundamental-mode "Python" | |
1564 "Major mode for editing Python files. | |
1565 Turns on Font Lock mode unconditionally since it is required for correct | |
1566 parsing of the source. | |
1567 See also `jython-mode', which is actually invoked if the buffer appears to | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1568 contain Jython code. See also `run-python' and associated Python mode |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1569 commands for running Python under Emacs. |
54789 | 1570 |
1571 The Emacs commands which work with `defun's, e.g. \\[beginning-of-defun], deal | |
1572 with nested `def' and `class' blocks. They take the innermost one as | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1573 current without distinguishing method and class definitions. Used multiple |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1574 times, they move over others at the same indentation level until they reach |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1575 the end of definitions at that level, when they move up a level. |
54789 | 1576 \\<python-mode-map> |
1577 Colon is electric: it outdents the line if appropriate, e.g. for | |
1578 an else statement. \\[python-backspace] at the beginning of an indented statement | |
1579 deletes a level of indentation to close the current block; otherwise it | |
1580 deletes a charcter backward. TAB indents the current line relative to | |
1581 the preceding code. Successive TABs, with no intervening command, cycle | |
1582 through the possibilities for indentation on the basis of enclosing blocks. | |
1583 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1584 \\[fill-paragraph] fills comments and multiline strings appropriately, but has no |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1585 effect outside them. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1586 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1587 Supports Eldoc mode (only for functions, using a Python process), |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1588 Info-Look and Imenu. In Outline minor mode, `class' and `def' |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1589 lines count as headers. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1590 |
54789 | 1591 \\{python-mode-map}" |
1592 :group 'python | |
1593 (set (make-local-variable 'font-lock-defaults) | |
1594 '(python-font-lock-keywords nil nil ((?_ . "w")) nil | |
1595 (font-lock-syntactic-keywords | |
1596 . python-font-lock-syntactic-keywords) | |
1597 ;;; This probably isn't worth it. | |
1598 ;;; (font-lock-syntactic-face-function | |
1599 ;;; . python-font-lock-syntactic-face-function) | |
1600 )) | |
1601 (set (make-local-variable 'parse-sexp-lookup-properties) t) | |
1602 (set (make-local-variable 'comment-start) "# ") | |
1603 ;; Fixme: define a comment-indent-function? | |
1604 (set (make-local-variable 'indent-line-function) #'python-indent-line) | |
1605 (set (make-local-variable 'paragraph-start) "\\s-*$") | |
1606 (set (make-local-variable 'fill-paragraph-function) | |
1607 'python-fill-paragraph) | |
1608 (set (make-local-variable 'require-final-newline) t) | |
1609 (set (make-local-variable 'add-log-current-defun-function) | |
1610 #'python-current-defun) | |
1611 ;; Fixme: Generalize to do all blocks? | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1612 (set (make-local-variable 'outline-regexp) "\\s-*\\(def\\|class\\)\\>") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1613 (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n") |
54789 | 1614 (set (make-local-variable 'outline-level) #'python-outline-level) |
1615 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil) | |
1616 (make-local-variable 'python-saved-check-command) | |
1617 (set (make-local-variable 'beginning-of-defun-function) | |
1618 'python-beginning-of-defun) | |
1619 (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun) | |
1620 (setq imenu-create-index-function #'python-imenu-create-index) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1621 (set (make-local-variable 'eldoc-print-current-symbol-info-function) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1622 #'python-eldoc-function) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1623 (add-hook 'eldoc-mode-hook |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1624 '(lambda () (run-python 0 t)) nil t) ; need it running |
54789 | 1625 (unless font-lock-mode (font-lock-mode 1)) |
1626 (when python-guess-indent (python-guess-indent)) | |
1627 (set (make-local-variable 'python-command) python-python-command) | |
1628 (unless (boundp 'python-mode-running) ; kill the recursion from jython-mode | |
1629 (let ((python-mode-running t)) | |
1630 (python-maybe-jython)))) | |
1631 | |
1632 (custom-add-option 'python-mode-hook 'imenu-add-menubar-index) | |
1633 (custom-add-option 'python-mode-hook | |
1634 '(lambda () | |
1635 "Turn on Indent Tabs mode." | |
1636 (set (make-local-variable 'indent-tabs-mode) t))) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1637 (custom-add-option 'python-mode-hook 'turn-on-eldoc-mode) |
54789 | 1638 |
1639 ;;;###autoload | |
1640 (define-derived-mode jython-mode python-mode "Jython" | |
1641 "Major mode for editing Jython files. | |
1642 Like `python-mode', but sets up parameters for Jython subprocesses. | |
1643 Runs `jython-mode-hook' after `python-mode-hook'." | |
1644 :group 'python | |
1645 (set (make-local-variable 'python-command) python-jython-command)) | |
1646 | |
1647 (provide 'python) | |
1648 ;; arch-tag: 6fce1d99-a704-4de9-ba19-c6e4912b0554 | |
1649 ;;; python.el ends here |