2
|
1 ;;; python-mode.el --- Major mode for editing Python programs
|
|
2
|
|
3 ;; Copyright (C) 1992,1993,1994 Tim Peters
|
|
4
|
98
|
5 ;; Author: 1995-1997 Barry A. Warsaw
|
2
|
6 ;; 1992-1994 Tim Peters
|
|
7 ;; Maintainer: python-mode@python.org
|
|
8 ;; Created: Feb 1992
|
98
|
9 ;; Version: 2.89
|
|
10 ;; Last Modified: 1997/01/30 20:16:18
|
2
|
11 ;; Keywords: python languages oop
|
|
12
|
|
13 ;; This software is provided as-is, without express or implied
|
|
14 ;; warranty. Permission to use, copy, modify, distribute or sell this
|
|
15 ;; software, without fee, for any purpose and by any individual or
|
|
16 ;; organization, is hereby granted, provided that the above copyright
|
|
17 ;; notice and this paragraph appear in all copies.
|
|
18
|
|
19 ;;; Commentary:
|
|
20
|
|
21 ;; This is a major mode for editing Python programs. It was developed
|
|
22 ;; by Tim Peters after an original idea by Michael A. Guravage. Tim
|
4
|
23 ;; subsequently left the net; in 1995, Barry Warsaw inherited the
|
|
24 ;; mode and is the current maintainer.
|
2
|
25
|
|
26 ;; At some point this mode will undergo a rewrite to bring it more in
|
|
27 ;; line with GNU Emacs Lisp coding standards, and to wax all the Emacs
|
|
28 ;; 18 support. But all in all, the mode works exceedingly well, and
|
|
29 ;; I've simply been tweaking it as I go along. Ain't it wonderful
|
|
30 ;; that Python has a much more sane syntax than C? (or <shudder> C++?!
|
|
31 ;; :-). I can say that; I maintain cc-mode!
|
|
32
|
|
33 ;; The following statements, placed in your .emacs file or
|
|
34 ;; site-init.el, will cause this file to be autoloaded, and
|
|
35 ;; python-mode invoked, when visiting .py files (assuming this file is
|
|
36 ;; in your load-path):
|
|
37 ;;
|
|
38 ;; (autoload 'python-mode "python-mode" "Python editing mode." t)
|
|
39 ;; (setq auto-mode-alist
|
|
40 ;; (cons '("\\.py$" . python-mode) auto-mode-alist))
|
|
41 ;;
|
|
42 ;; If you want font-lock support for Python source code (a.k.a. syntax
|
|
43 ;; coloring, highlighting), add this to your .emacs file:
|
|
44 ;;
|
|
45 ;; (add-hook 'python-mode-hook 'turn-on-font-lock)
|
|
46 ;;
|
|
47 ;; But you better be sure you're version of Emacs supports
|
|
48 ;; font-lock-mode! As of this writing, the latest Emacs and XEmacs
|
|
49 ;; 19's do.
|
|
50
|
|
51 ;; Here's a brief list of recent additions/improvements/changes:
|
|
52 ;;
|
|
53 ;; - Wrapping and indentation within triple quote strings now works.
|
|
54 ;; - `Standard' bug reporting mechanism (use C-c C-b)
|
|
55 ;; - py-mark-block was moved to C-c C-m
|
|
56 ;; - C-c C-v shows you the python-mode version
|
|
57 ;; - a basic python-font-lock-keywords has been added for (X)Emacs 19
|
|
58 ;; - proper interaction with pending-del and del-sel modes.
|
|
59 ;; - Better support for outdenting: py-electric-colon (:) and
|
|
60 ;; py-indent-line (TAB) improvements; one level of outdentation
|
4
|
61 ;; added after a return, raise, break, pass, or continue statement.
|
|
62 ;; Defeated by prefixing command with C-u.
|
2
|
63 ;; - New py-electric-colon (:) command for improved outdenting Also
|
|
64 ;; py-indent-line (TAB) should handle outdented lines better
|
|
65 ;; - improved (I think) C-c > and C-c <
|
|
66 ;; - py-(forward|backward)-into-nomenclature, not bound, but useful on
|
|
67 ;; M-f and M-b respectively.
|
|
68 ;; - integration with imenu by Perry A. Stoll <stoll@atr-sw.atr.co.jp>
|
|
69 ;; - py-indent-offset now defaults to 4
|
|
70 ;; - new variable py-honor-comment-indentation
|
|
71 ;; - comment-region bound to C-c #
|
|
72 ;; - py-delete-char obeys numeric arguments
|
|
73 ;; - Small modification to rule for "indenting comment lines", such
|
|
74 ;; lines must now also be indented less than or equal to the
|
|
75 ;; indentation of the previous statement.
|
|
76
|
|
77 ;; Here's a brief to do list:
|
|
78 ;;
|
|
79 ;; - Better integration with gud-mode for debugging.
|
|
80 ;; - Rewrite according to GNU Emacs Lisp standards.
|
|
81 ;; - possibly force indent-tabs-mode == nil, and add a
|
|
82 ;; write-file-hooks that runs untabify on the whole buffer (to work
|
|
83 ;; around potential tab/space mismatch problems). In practice this
|
|
84 ;; hasn't been a problem... yet.
|
|
85 ;; - have py-execute-region on indented code act as if the region is
|
|
86 ;; left justified. Avoids syntax errors.
|
4
|
87 ;; - Add a py-goto-error or some such that would scan an exception in
|
|
88 ;; the py-shell buffer, and pop you to that line in the file.
|
2
|
89
|
|
90 ;; If you can think of more things you'd like to see, drop me a line.
|
|
91 ;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
|
|
92 ;;
|
|
93 ;; Note that I only test things on XEmacs 19 and to some degree on
|
|
94 ;; Emacs 19. If you port stuff to FSF Emacs 19, or Emacs 18, please
|
|
95 ;; send me your patches. Byte compiler complaints can probably be
|
|
96 ;; safely ignored.
|
|
97
|
|
98 ;;; Code:
|
|
99
|
|
100
|
|
101 ;; user definable variables
|
|
102 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
103
|
|
104 (defvar py-python-command "python"
|
|
105 "*Shell command used to start Python interpreter.")
|
|
106
|
|
107 (defvar py-indent-offset 4
|
|
108 "*Indentation increment.
|
|
109 Note that `\\[py-guess-indent-offset]' can usually guess a good value
|
|
110 when you're editing someone else's Python code.")
|
|
111
|
|
112 (defvar py-align-multiline-strings-p t
|
|
113 "*Flag describing how multiline triple quoted strings are aligned.
|
|
114 When this flag is non-nil, continuation lines are lined up under the
|
|
115 preceding line's indentation. When this flag is nil, continuation
|
|
116 lines are aligned to column zero.")
|
|
117
|
|
118 (defvar py-block-comment-prefix "## "
|
|
119 "*String used by \\[comment-region] to comment out a block of code.
|
|
120 This should follow the convention for non-indenting comment lines so
|
|
121 that the indentation commands won't get confused (i.e., the string
|
|
122 should be of the form `#x...' where `x' is not a blank or a tab, and
|
|
123 `...' is arbitrary).")
|
|
124
|
|
125 (defvar py-honor-comment-indentation t
|
|
126 "*Controls how comment lines influence subsequent indentation.
|
|
127
|
|
128 When nil, all comment lines are skipped for indentation purposes, and
|
|
129 in Emacs 19, a faster algorithm is used.
|
|
130
|
|
131 When t, lines that begin with a single `#' are a hint to subsequent
|
|
132 line indentation. If the previous line is such a comment line (as
|
|
133 opposed to one that starts with `py-block-comment-prefix'), then it's
|
|
134 indentation is used as a hint for this line's indentation. Lines that
|
|
135 begin with `py-block-comment-prefix' are ignored for indentation
|
|
136 purposes.
|
|
137
|
|
138 When not nil or t, comment lines that begin with a `#' are used as
|
|
139 indentation hints, unless the comment character is in column zero.")
|
|
140
|
|
141 (defvar py-scroll-process-buffer t
|
|
142 "*Scroll Python process buffer as output arrives.
|
|
143 If nil, the Python process buffer acts, with respect to scrolling, like
|
|
144 Shell-mode buffers normally act. This is surprisingly complicated and
|
|
145 so won't be explained here; in fact, you can't get the whole story
|
|
146 without studying the Emacs C code.
|
|
147
|
|
148 If non-nil, the behavior is different in two respects (which are
|
|
149 slightly inaccurate in the interest of brevity):
|
|
150
|
|
151 - If the buffer is in a window, and you left point at its end, the
|
|
152 window will scroll as new output arrives, and point will move to the
|
|
153 buffer's end, even if the window is not the selected window (that
|
|
154 being the one the cursor is in). The usual behavior for shell-mode
|
|
155 windows is not to scroll, and to leave point where it was, if the
|
|
156 buffer is in a window other than the selected window.
|
|
157
|
|
158 - If the buffer is not visible in any window, and you left point at
|
|
159 its end, the buffer will be popped into a window as soon as more
|
|
160 output arrives. This is handy if you have a long-running
|
|
161 computation and don't want to tie up screen area waiting for the
|
|
162 output. The usual behavior for a shell-mode buffer is to stay
|
|
163 invisible until you explicitly visit it.
|
|
164
|
|
165 Note the `and if you left point at its end' clauses in both of the
|
|
166 above: you can `turn off' the special behaviors while output is in
|
|
167 progress, by visiting the Python buffer and moving point to anywhere
|
|
168 besides the end. Then the buffer won't scroll, point will remain where
|
|
169 you leave it, and if you hide the buffer it will stay hidden until you
|
|
170 visit it again. You can enable and disable the special behaviors as
|
|
171 often as you like, while output is in progress, by (respectively) moving
|
|
172 point to, or away from, the end of the buffer.
|
|
173
|
|
174 Warning: If you expect a large amount of output, you'll probably be
|
|
175 happier setting this option to nil.
|
|
176
|
|
177 Obscure: `End of buffer' above should really say `at or beyond the
|
|
178 process mark', but if you know what that means you didn't need to be
|
|
179 told <grin>.")
|
|
180
|
|
181 (defvar py-temp-directory
|
|
182 (let ((ok '(lambda (x)
|
|
183 (and x
|
|
184 (setq x (expand-file-name x)) ; always true
|
|
185 (file-directory-p x)
|
|
186 (file-writable-p x)
|
|
187 x))))
|
|
188 (or (funcall ok (getenv "TMPDIR"))
|
|
189 (funcall ok "/usr/tmp")
|
|
190 (funcall ok "/tmp")
|
|
191 (funcall ok ".")
|
|
192 (error
|
|
193 "Couldn't find a usable temp directory -- set py-temp-directory")))
|
|
194 "*Directory used for temp files created by a *Python* process.
|
|
195 By default, the first directory from this list that exists and that you
|
|
196 can write into: the value (if any) of the environment variable TMPDIR,
|
|
197 /usr/tmp, /tmp, or the current directory.")
|
|
198
|
|
199 (defvar py-beep-if-tab-change t
|
|
200 "*Ring the bell if tab-width is changed.
|
|
201 If a comment of the form
|
|
202
|
|
203 \t# vi:set tabsize=<number>:
|
|
204
|
|
205 is found before the first code line when the file is entered, and the
|
|
206 current value of (the general Emacs variable) `tab-width' does not
|
|
207 equal <number>, `tab-width' is set to <number>, a message saying so is
|
|
208 displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
|
|
209 the Emacs bell is also rung as a warning.")
|
|
210
|
70
|
211 (defconst python-font-lock-keywords
|
4
|
212 (let* ((keywords '("and" "break" "class"
|
2
|
213 "continue" "def" "del" "elif"
|
|
214 "else:" "except" "except:" "exec"
|
|
215 "finally:" "for" "from" "global"
|
|
216 "if" "import" "in" "is"
|
|
217 "lambda" "not" "or" "pass"
|
|
218 "print" "raise" "return" "try:"
|
|
219 "while"
|
|
220 ))
|
|
221 (kwregex (mapconcat 'identity keywords "\\|")))
|
|
222 (list
|
|
223 ;; keywords not at beginning of line
|
|
224 (cons (concat "\\s-\\(" kwregex "\\)[ \n\t(]") 1)
|
|
225 ;; keywords at beginning of line. i don't think regexps are
|
|
226 ;; powerful enough to handle these two cases in one regexp.
|
|
227 ;; prove me wrong!
|
|
228 (cons (concat "^\\(" kwregex "\\)[ \n\t(]") 1)
|
|
229 ;; classes
|
|
230 '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
|
|
231 1 font-lock-type-face)
|
|
232 ;; functions
|
|
233 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
|
|
234 1 font-lock-function-name-face)
|
|
235 ))
|
|
236 "Additional expressions to highlight in Python mode.")
|
|
237
|
|
238 (defvar imenu-example--python-show-method-args-p nil
|
|
239 "*Controls echoing of arguments of functions & methods in the imenu buffer.
|
|
240 When non-nil, arguments are printed.")
|
|
241
|
|
242
|
|
243
|
|
244 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
245 ;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
|
|
246
|
|
247 (make-variable-buffer-local 'py-indent-offset)
|
|
248
|
|
249 ;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
|
|
250 ;; seems to be the standard way of checking this.
|
|
251 ;; BAW - This is *not* the right solution. When at all possible,
|
|
252 ;; instead of testing for the version of Emacs, use feature tests.
|
|
253
|
|
254 (setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
|
|
255 (setq py-this-is-emacs-19-p
|
|
256 (and
|
|
257 (not py-this-is-lucid-emacs-p)
|
|
258 (string-match "^19\\." emacs-version)))
|
|
259
|
|
260 ;; have to bind py-file-queue before installing the kill-emacs hook
|
|
261 (defvar py-file-queue nil
|
|
262 "Queue of Python temp files awaiting execution.
|
|
263 Currently-active file is at the head of the list.")
|
|
264
|
|
265 ;; define a mode-specific abbrev table for those who use such things
|
|
266 (defvar python-mode-abbrev-table nil
|
|
267 "Abbrev table in use in `python-mode' buffers.")
|
|
268 (define-abbrev-table 'python-mode-abbrev-table nil)
|
|
269
|
|
270 (defvar python-mode-hook nil
|
|
271 "*Hook called by `python-mode'.")
|
|
272
|
|
273 ;; in previous version of python-mode.el, the hook was incorrectly
|
|
274 ;; called py-mode-hook, and was not defvar'd. deprecate its use.
|
|
275 (and (fboundp 'make-obsolete-variable)
|
|
276 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
|
|
277
|
98
|
278 (defvar py-delete-function 'backward-delete-char-untabify
|
|
279 "*Function called by `py-delete-char' when deleting characters.")
|
|
280
|
2
|
281 (defvar py-mode-map ()
|
|
282 "Keymap used in `python-mode' buffers.")
|
|
283
|
|
284 (if py-mode-map
|
|
285 ()
|
|
286 (setq py-mode-map (make-sparse-keymap))
|
|
287
|
|
288 ;; shadow global bindings for newline-and-indent w/ the py- version.
|
|
289 ;; BAW - this is extremely bad form, but I'm not going to change it
|
|
290 ;; for now.
|
|
291 (mapcar (function (lambda (key)
|
|
292 (define-key
|
|
293 py-mode-map key 'py-newline-and-indent)))
|
|
294 (where-is-internal 'newline-and-indent))
|
|
295
|
|
296 ;; BAW - you could do it this way, but its not considered proper
|
|
297 ;; major-mode form.
|
|
298 (mapcar (function
|
|
299 (lambda (x)
|
|
300 (define-key py-mode-map (car x) (cdr x))))
|
|
301 '((":" . py-electric-colon)
|
|
302 ("\C-c\C-c" . py-execute-buffer)
|
|
303 ("\C-c|" . py-execute-region)
|
|
304 ("\C-c!" . py-shell)
|
|
305 ("\177" . py-delete-char)
|
|
306 ("\n" . py-newline-and-indent)
|
|
307 ("\C-c:" . py-guess-indent-offset)
|
|
308 ("\C-c\t" . py-indent-region)
|
|
309 ("\C-c\C-l" . py-shift-region-left)
|
|
310 ("\C-c\C-r" . py-shift-region-right)
|
|
311 ("\C-c<" . py-shift-region-left)
|
|
312 ("\C-c>" . py-shift-region-right)
|
|
313 ("\C-c\C-n" . py-next-statement)
|
|
314 ("\C-c\C-p" . py-previous-statement)
|
|
315 ("\C-c\C-u" . py-goto-block-up)
|
|
316 ("\C-c\C-m" . py-mark-block)
|
|
317 ("\C-c#" . py-comment-region)
|
|
318 ("\C-c?" . py-describe-mode)
|
|
319 ("\C-c\C-hm" . py-describe-mode)
|
|
320 ("\e\C-a" . beginning-of-python-def-or-class)
|
|
321 ("\e\C-e" . end-of-python-def-or-class)
|
|
322 ( "\e\C-h" . mark-python-def-or-class)))
|
|
323 ;; should do all keybindings this way
|
|
324 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
|
|
325 (define-key py-mode-map "\C-c\C-v" 'py-version)
|
|
326 )
|
|
327
|
|
328 (defvar py-mode-syntax-table nil
|
|
329 "Syntax table used in `python-mode' buffers.")
|
|
330
|
|
331 (if py-mode-syntax-table
|
|
332 ()
|
|
333 (setq py-mode-syntax-table (make-syntax-table))
|
|
334 ;; BAW - again, blech.
|
|
335 (mapcar (function
|
|
336 (lambda (x) (modify-syntax-entry
|
|
337 (car x) (cdr x) py-mode-syntax-table)))
|
|
338 '(( ?\( . "()" ) ( ?\) . ")(" )
|
|
339 ( ?\[ . "(]" ) ( ?\] . ")[" )
|
|
340 ( ?\{ . "(}" ) ( ?\} . "){" )
|
|
341 ;; fix operator symbols misassigned in the std table
|
|
342 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
|
|
343 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
|
|
344 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
|
|
345 ( ?\> . "." ) ( ?\| . "." )
|
|
346 ;; for historical reasons, underscore is word class
|
|
347 ;; instead of symbol class. it should be symbol class,
|
|
348 ;; but if you're tempted to change it, try binding M-f and
|
|
349 ;; M-b to py-forward-into-nomenclature and
|
|
350 ;; py-backward-into-nomenclature instead. -baw
|
|
351 ( ?\_ . "w" ) ; underscore is legit in words
|
|
352 ( ?\' . "\"") ; single quote is string quote
|
|
353 ( ?\" . "\"" ) ; double quote is string quote too
|
|
354 ( ?\` . "$") ; backquote is open and close paren
|
|
355 ( ?\# . "<") ; hash starts comment
|
|
356 ( ?\n . ">")))) ; newline ends comment
|
|
357
|
|
358 (defconst py-stringlit-re
|
|
359 (concat
|
|
360 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
|
|
361 "\\|" ; or
|
|
362 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
|
|
363 "Regexp matching a Python string literal.")
|
|
364
|
|
365 ;; this is tricky because a trailing backslash does not mean
|
|
366 ;; continuation if it's in a comment
|
|
367 (defconst py-continued-re
|
|
368 (concat
|
|
369 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
|
|
370 "\\\\$")
|
|
371 "Regexp matching Python lines that are continued via backslash.")
|
|
372
|
|
373 (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
|
|
374 "Regexp matching blank or comment lines.")
|
|
375
|
|
376 (defconst py-outdent-re
|
|
377 (concat "\\(" (mapconcat 'identity
|
|
378 '("else:"
|
|
379 "except\\(\\s +.*\\)?:"
|
|
380 "finally:"
|
|
381 "elif\\s +.*:")
|
|
382 "\\|")
|
|
383 "\\)")
|
|
384 "Regexp matching clauses to be outdented one level.")
|
|
385
|
|
386 (defconst py-no-outdent-re
|
|
387 (concat "\\(" (mapconcat 'identity
|
|
388 '("try:"
|
|
389 "except\\(\\s +.*\\)?:"
|
|
390 "while\\s +.*:"
|
|
391 "for\\s +.*:"
|
|
392 "if\\s +.*:"
|
4
|
393 "elif\\s +.*:"
|
|
394 "\\(return\\|break\\|raise\\|continue\\)[ \t\n]"
|
|
395 )
|
2
|
396 "\\|")
|
|
397 "\\)")
|
|
398 "Regexp matching lines to not outdent after.")
|
|
399
|
4
|
400 (defvar py-defun-start-re
|
|
401 "^\\([ \t]*\\)def[ \t]+\\([a-zA-Z_0-9]+\\)\\|\\(^[a-zA-Z_0-9]+\\)[ \t]*="
|
|
402 "Regexp matching a function, method or variable assignment.
|
|
403
|
|
404 If you change this, you probably have to change `py-current-defun' as well.
|
|
405 This is only used by `py-current-defun' to find the name for add-log.el.")
|
|
406
|
|
407 (defvar py-class-start-re "^class[ \t]*\\([a-zA-Z_0-9]+\\)"
|
|
408 "Regexp for finding a class name.
|
|
409
|
|
410 If you change this, you probably have to change `py-current-defun' as well.
|
|
411 This is only used by `py-current-defun' to find the name for add-log.el.")
|
|
412
|
98
|
413 ;; As of 30-Jan-1997, Emacs 19.34 works but XEmacs 19.15b90 and
|
|
414 ;; previous does not. It is suspected that Emacsen before 19.34 are
|
|
415 ;; also broken.
|
|
416 (defvar py-parse-partial-sexp-works-p
|
|
417 (let ((buf (get-buffer-create " ---*---pps---*---"))
|
|
418 state status)
|
|
419 (save-excursion
|
|
420 (set-buffer buf)
|
|
421 (erase-buffer)
|
|
422 (insert "(line1\n line2)\nline3")
|
|
423 (lisp-mode)
|
|
424 (goto-char (point-min))
|
|
425 (setq state (parse-partial-sexp (point) (save-excursion
|
|
426 (forward-line 1)
|
|
427 (point))))
|
|
428 (parse-partial-sexp (point) (point-max) 0 nil state)
|
|
429 (setq status (not (= (point) (point-max))))
|
|
430 (kill-buffer buf)
|
|
431 status))
|
|
432 "Does `parse-partial-sexp' work in this Emacs?")
|
4
|
433
|
2
|
434
|
|
435 ;; Menu definitions, only relevent if you have the easymenu.el package
|
|
436 ;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
|
4
|
437 (defvar py-menu nil
|
|
438 "Menu for Python Mode.
|
|
439
|
|
440 This menu will get created automatically if you have the easymenu
|
|
441 package. Note that the latest XEmacs 19 and Emacs 19 versions contain
|
|
442 this package.")
|
|
443
|
2
|
444 (if (condition-case nil
|
|
445 (require 'easymenu)
|
|
446 (error nil))
|
|
447 (easy-menu-define
|
|
448 py-menu py-mode-map "Python Mode menu"
|
|
449 '("Python"
|
98
|
450 ["Comment Out Region" py-comment-region (mark)]
|
|
451 ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)]
|
2
|
452 "-"
|
|
453 ["Mark current block" py-mark-block t]
|
|
454 ["Mark current def" mark-python-def-or-class t]
|
|
455 ["Mark current class" (mark-python-def-or-class t) t]
|
|
456 "-"
|
|
457 ["Shift region left" py-shift-region-left (mark)]
|
|
458 ["Shift region right" py-shift-region-right (mark)]
|
|
459 "-"
|
|
460 ["Execute buffer" py-execute-buffer t]
|
|
461 ["Execute region" py-execute-region (mark)]
|
|
462 ["Start interpreter..." py-shell t]
|
|
463 "-"
|
|
464 ["Go to start of block" py-goto-block-up t]
|
|
465 ["Go to start of class" (beginning-of-python-def-or-class t) t]
|
|
466 ["Move to end of class" (end-of-python-def-or-class t) t]
|
|
467 ["Move to start of def" beginning-of-python-def-or-class t]
|
|
468 ["Move to end of def" end-of-python-def-or-class t]
|
|
469 "-"
|
|
470 ["Describe mode" py-describe-mode t]
|
|
471 )))
|
|
472
|
|
473
|
|
474
|
|
475 ;; imenu definitions, courtesy of Perry A. Stoll <stoll@atr-sw.atr.co.jp>
|
|
476 (defvar imenu-example--python-class-regexp
|
|
477 (concat ; <<classes>>
|
|
478 "\\(" ;
|
|
479 "^[ \t]*" ; newline and maybe whitespace
|
|
480 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
|
|
481 ; possibly multiple superclasses
|
|
482 "\\([ \t]*\\((\\([a-zA-Z0-9_, \t\n]\\)*)\\)?\\)"
|
|
483 "[ \t]*:" ; and the final :
|
|
484 "\\)" ; >>classes<<
|
|
485 )
|
|
486 "Regexp for Python classes for use with the imenu package."
|
|
487 )
|
|
488
|
|
489 (defvar imenu-example--python-method-regexp
|
|
490 (concat ; <<methods and functions>>
|
|
491 "\\(" ;
|
|
492 "^[ \t]*" ; new line and maybe whitespace
|
|
493 "\\(def[ \t]+" ; function definitions start with def
|
|
494 "\\([a-zA-Z0-9_]+\\)" ; name is here
|
|
495 ; function arguments...
|
|
496 "[ \t]*(\\([a-zA-Z0-9_=,\* \t\n]*\\))"
|
|
497 "\\)" ; end of def
|
|
498 "[ \t]*:" ; and then the :
|
|
499 "\\)" ; >>methods and functions<<
|
|
500 )
|
|
501 "Regexp for Python methods/functions for use with the imenu package."
|
|
502 )
|
|
503
|
|
504 (defvar imenu-example--python-method-no-arg-parens '(2 8)
|
|
505 "Indicies into groups of the Python regexp for use with imenu.
|
|
506
|
|
507 Using these values will result in smaller imenu lists, as arguments to
|
|
508 functions are not listed.
|
|
509
|
|
510 See the variable `imenu-example--python-show-method-args-p' for more
|
|
511 information.")
|
|
512
|
|
513 (defvar imenu-example--python-method-arg-parens '(2 7)
|
|
514 "Indicies into groups of the Python regexp for use with imenu.
|
|
515 Using these values will result in large imenu lists, as arguments to
|
|
516 functions are listed.
|
|
517
|
|
518 See the variable `imenu-example--python-show-method-args-p' for more
|
|
519 information.")
|
|
520
|
|
521 ;; Note that in this format, this variable can still be used with the
|
|
522 ;; imenu--generic-function. Otherwise, there is no real reason to have
|
|
523 ;; it.
|
|
524 (defvar imenu-example--generic-python-expression
|
|
525 (cons
|
|
526 (concat
|
|
527 imenu-example--python-class-regexp
|
|
528 "\\|" ; or...
|
|
529 imenu-example--python-method-regexp
|
|
530 )
|
|
531 imenu-example--python-method-no-arg-parens)
|
|
532 "Generic Python expression which may be used directly with imenu.
|
|
533 Used by setting the variable `imenu-generic-expression' to this value.
|
|
534 Also, see the function \\[imenu-example--create-python-index] for a
|
|
535 better alternative for finding the index.")
|
|
536
|
|
537 ;; These next two variables are used when searching for the python
|
|
538 ;; class/definitions. Just saving some time in accessing the
|
|
539 ;; generic-python-expression, really.
|
4
|
540 (defvar imenu-example--python-generic-regexp nil)
|
|
541 (defvar imenu-example--python-generic-parens nil)
|
2
|
542
|
|
543
|
|
544 ;;;###autoload
|
|
545 (eval-when-compile
|
|
546 ;; Imenu isn't used in XEmacs, so just ignore load errors
|
|
547 (condition-case ()
|
|
548 (progn
|
|
549 (require 'cl)
|
|
550 (require 'imenu))
|
|
551 (error nil)))
|
|
552
|
|
553 (defun imenu-example--create-python-index ()
|
|
554 "Python interface function for imenu package.
|
|
555 Finds all python classes and functions/methods. Calls function
|
|
556 \\[imenu-example--create-python-index-engine]. See that function for
|
|
557 the details of how this works."
|
|
558 (setq imenu-example--python-generic-regexp
|
|
559 (car imenu-example--generic-python-expression))
|
|
560 (setq imenu-example--python-generic-parens
|
|
561 (if imenu-example--python-show-method-args-p
|
|
562 imenu-example--python-method-arg-parens
|
|
563 imenu-example--python-method-no-arg-parens))
|
|
564 (goto-char (point-min))
|
|
565 (imenu-example--create-python-index-engine nil))
|
|
566
|
|
567 (defun imenu-example--create-python-index-engine (&optional start-indent)
|
|
568 "Function for finding imenu definitions in Python.
|
|
569
|
|
570 Finds all definitions (classes, methods, or functions) in a Python
|
|
571 file for the imenu package.
|
|
572
|
|
573 Returns a possibly nested alist of the form
|
|
574
|
|
575 (INDEX-NAME . INDEX-POSITION)
|
|
576
|
|
577 The second element of the alist may be an alist, producing a nested
|
|
578 list as in
|
|
579
|
|
580 (INDEX-NAME . INDEX-ALIST)
|
|
581
|
|
582 This function should not be called directly, as it calls itself
|
|
583 recursively and requires some setup. Rather this is the engine for
|
|
584 the function \\[imenu-example--create-python-index].
|
|
585
|
|
586 It works recursively by looking for all definitions at the current
|
|
587 indention level. When it finds one, it adds it to the alist. If it
|
|
588 finds a definition at a greater indentation level, it removes the
|
|
589 previous definition from the alist. In it's place it adds all
|
|
590 definitions found at the next indentation level. When it finds a
|
|
591 definition that is less indented then the current level, it retuns the
|
|
592 alist it has created thus far.
|
|
593
|
|
594 The optional argument START-INDENT indicates the starting indentation
|
|
595 at which to continue looking for Python classes, methods, or
|
|
596 functions. If this is not supplied, the function uses the indentation
|
|
597 of the first definition found."
|
|
598 (let ((index-alist '())
|
|
599 (sub-method-alist '())
|
|
600 looking-p
|
|
601 def-name prev-name
|
|
602 cur-indent def-pos
|
|
603 (class-paren (first imenu-example--python-generic-parens))
|
|
604 (def-paren (second imenu-example--python-generic-parens)))
|
|
605 (setq looking-p
|
|
606 (re-search-forward imenu-example--python-generic-regexp
|
|
607 (point-max) t))
|
|
608 (while looking-p
|
|
609 (save-excursion
|
|
610 ;; used to set def-name to this value but generic-extract-name is
|
|
611 ;; new to imenu-1.14. this way it still works with imenu-1.11
|
|
612 ;;(imenu--generic-extract-name imenu-example--python-generic-parens))
|
|
613 (let ((cur-paren (if (match-beginning class-paren)
|
|
614 class-paren def-paren)))
|
|
615 (setq def-name
|
|
616 (buffer-substring (match-beginning cur-paren)
|
|
617 (match-end cur-paren))))
|
|
618 (beginning-of-line)
|
|
619 (setq cur-indent (current-indentation)))
|
|
620
|
|
621 ;; HACK: want to go to the next correct definition location. we
|
|
622 ;; explicitly list them here. would be better to have them in a
|
|
623 ;; list.
|
|
624 (setq def-pos
|
|
625 (or (match-beginning class-paren)
|
|
626 (match-beginning def-paren)))
|
|
627
|
|
628 ;; if we don't have a starting indent level, take this one
|
|
629 (or start-indent
|
|
630 (setq start-indent cur-indent))
|
|
631
|
|
632 ;; if we don't have class name yet, take this one
|
|
633 (or prev-name
|
|
634 (setq prev-name def-name))
|
|
635
|
|
636 ;; what level is the next definition on? must be same, deeper
|
|
637 ;; or shallower indentation
|
|
638 (cond
|
|
639 ;; at the same indent level, add it to the list...
|
|
640 ((= start-indent cur-indent)
|
|
641
|
|
642 ;; if we don't have push, use the following...
|
|
643 ;;(setf index-alist (cons (cons def-name def-pos) index-alist))
|
|
644 (push (cons def-name def-pos) index-alist))
|
|
645
|
|
646 ;; deeper indented expression, recur...
|
|
647 ((< start-indent cur-indent)
|
|
648
|
|
649 ;; the point is currently on the expression we're supposed to
|
|
650 ;; start on, so go back to the last expression. The recursive
|
|
651 ;; call will find this place again and add it to the correct
|
|
652 ;; list
|
|
653 (re-search-backward imenu-example--python-generic-regexp
|
|
654 (point-min) 'move)
|
|
655 (setq sub-method-alist (imenu-example--create-python-index-engine
|
|
656 cur-indent))
|
|
657
|
|
658 (if sub-method-alist
|
|
659 ;; we put the last element on the index-alist on the start
|
|
660 ;; of the submethod alist so the user can still get to it.
|
|
661 (let ((save-elmt (pop index-alist)))
|
|
662 (push (cons (imenu-create-submenu-name prev-name)
|
|
663 (cons save-elmt sub-method-alist))
|
|
664 index-alist))))
|
|
665
|
|
666 ;; found less indented expression, we're done.
|
|
667 (t
|
|
668 (setq looking-p nil)
|
|
669 (re-search-backward imenu-example--python-generic-regexp
|
|
670 (point-min) t)))
|
|
671 (setq prev-name def-name)
|
|
672 (and looking-p
|
|
673 (setq looking-p
|
|
674 (re-search-forward imenu-example--python-generic-regexp
|
|
675 (point-max) 'move))))
|
|
676 (nreverse index-alist)))
|
|
677
|
|
678
|
|
679 ;;;###autoload
|
|
680 (defun python-mode ()
|
|
681 "Major mode for editing Python files.
|
|
682 To submit a problem report, enter `\\[py-submit-bug-report]' from a
|
|
683 `python-mode' buffer. Do `\\[py-describe-mode]' for detailed
|
|
684 documentation. To see what version of `python-mode' you are running,
|
|
685 enter `\\[py-version]'.
|
|
686
|
|
687 This mode knows about Python indentation, tokens, comments and
|
|
688 continuation lines. Paragraphs are separated by blank lines only.
|
|
689
|
|
690 COMMANDS
|
|
691 \\{py-mode-map}
|
|
692 VARIABLES
|
|
693
|
|
694 py-indent-offset\t\tindentation increment
|
|
695 py-block-comment-prefix\t\tcomment string used by comment-region
|
|
696 py-python-command\t\tshell command to invoke Python interpreter
|
|
697 py-scroll-process-buffer\t\talways scroll Python process buffer
|
|
698 py-temp-directory\t\tdirectory used for temp files (if needed)
|
|
699 py-beep-if-tab-change\t\tring the bell if tab-width is changed"
|
|
700 (interactive)
|
4
|
701 ;; set up local variables
|
2
|
702 (kill-all-local-variables)
|
4
|
703 (make-local-variable 'font-lock-defaults)
|
|
704 (make-local-variable 'paragraph-separate)
|
|
705 (make-local-variable 'paragraph-start)
|
|
706 (make-local-variable 'require-final-newline)
|
|
707 (make-local-variable 'comment-start)
|
98
|
708 (make-local-variable 'comment-end)
|
4
|
709 (make-local-variable 'comment-start-skip)
|
|
710 (make-local-variable 'comment-column)
|
|
711 (make-local-variable 'indent-region-function)
|
|
712 (make-local-variable 'indent-line-function)
|
|
713 (make-local-variable 'add-log-current-defun-function)
|
|
714 ;;
|
2
|
715 (set-syntax-table py-mode-syntax-table)
|
4
|
716 (setq major-mode 'python-mode
|
|
717 mode-name "Python"
|
|
718 local-abbrev-table python-mode-abbrev-table
|
70
|
719 font-lock-defaults '(python-font-lock-keywords)
|
4
|
720 paragraph-separate "^[ \t]*$"
|
|
721 paragraph-start "^[ \t]*$"
|
|
722 require-final-newline t
|
|
723 comment-start "# "
|
98
|
724 comment-end ""
|
4
|
725 comment-start-skip "# *"
|
|
726 comment-column 40
|
|
727 indent-region-function 'py-indent-region
|
|
728 indent-line-function 'py-indent-line
|
|
729 ;; tell add-log.el how to find the current function/method/variable
|
|
730 add-log-current-defun-function 'py-current-defun
|
|
731 )
|
2
|
732 (use-local-map py-mode-map)
|
|
733 ;; add the menu
|
|
734 (if py-menu
|
|
735 (easy-menu-add py-menu))
|
|
736 ;; Emacs 19 requires this
|
|
737 (if (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p)
|
|
738 (setq comment-multi-line nil))
|
|
739 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
|
|
740 ;;
|
|
741 ;; not sure where the magic comment has to be; to save time
|
|
742 ;; searching for a rarity, we give up if it's not found prior to the
|
|
743 ;; first executable statement.
|
|
744 ;;
|
|
745 ;; BAW - on first glance, this seems like complete hackery. Why was
|
|
746 ;; this necessary, and is it still necessary?
|
|
747 (let ((case-fold-search nil)
|
|
748 (start (point))
|
|
749 new-tab-width)
|
|
750 (if (re-search-forward
|
|
751 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
|
|
752 (prog2 (py-next-statement 1) (point) (goto-char 1))
|
|
753 t)
|
|
754 (progn
|
|
755 (setq new-tab-width
|
|
756 (string-to-int
|
|
757 (buffer-substring (match-beginning 1) (match-end 1))))
|
|
758 (if (= tab-width new-tab-width)
|
|
759 nil
|
|
760 (setq tab-width new-tab-width)
|
|
761 (message "Caution: tab-width changed to %d" new-tab-width)
|
|
762 (if py-beep-if-tab-change (beep)))))
|
|
763 (goto-char start))
|
|
764
|
|
765 ;; install imenu
|
|
766 (setq imenu-create-index-function
|
|
767 (function imenu-example--create-python-index))
|
|
768 (if (fboundp 'imenu-add-to-menubar)
|
|
769 (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
|
|
770
|
|
771 ;; run the mode hook. py-mode-hook use is deprecated
|
|
772 (if python-mode-hook
|
|
773 (run-hooks 'python-mode-hook)
|
|
774 (run-hooks 'py-mode-hook)))
|
|
775
|
|
776
|
|
777 (defun py-keep-region-active ()
|
|
778 ;; do whatever is necessary to keep the region active in XEmacs.
|
|
779 ;; Ignore byte-compiler warnings you might see. Also note that
|
|
780 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
|
|
781 ;; require us to take explicit action.
|
|
782 (and (boundp 'zmacs-region-stays)
|
|
783 (setq zmacs-region-stays t)))
|
|
784
|
|
785
|
|
786 ;; electric characters
|
|
787 (defun py-outdent-p ()
|
|
788 ;; returns non-nil if the current line should outdent one level
|
|
789 (save-excursion
|
|
790 (and (progn (back-to-indentation)
|
|
791 (looking-at py-outdent-re))
|
|
792 (progn (backward-to-indentation 1)
|
|
793 (while (or (looking-at py-blank-or-comment-re)
|
|
794 (bobp))
|
|
795 (backward-to-indentation 1))
|
|
796 (not (looking-at py-no-outdent-re)))
|
|
797 )))
|
|
798
|
|
799
|
|
800 (defun py-electric-colon (arg)
|
|
801 "Insert a colon.
|
|
802 In certain cases the line is outdented appropriately. If a numeric
|
|
803 argument is provided, that many colons are inserted non-electrically.
|
|
804 Electric behavior is inhibited inside a string or comment."
|
|
805 (interactive "P")
|
|
806 (self-insert-command (prefix-numeric-value arg))
|
|
807 ;; are we in a string or comment?
|
|
808 (if (save-excursion
|
|
809 (let ((pps (parse-partial-sexp (save-excursion
|
|
810 (beginning-of-python-def-or-class)
|
|
811 (point))
|
|
812 (point))))
|
|
813 (not (or (nth 3 pps) (nth 4 pps)))))
|
|
814 (save-excursion
|
|
815 (let ((here (point))
|
|
816 (outdent 0)
|
4
|
817 (indent (py-compute-indentation t)))
|
2
|
818 (if (and (not arg)
|
|
819 (py-outdent-p)
|
|
820 (= indent (save-excursion
|
4
|
821 (py-next-statement -1)
|
|
822 (py-compute-indentation t)))
|
2
|
823 )
|
|
824 (setq outdent py-indent-offset))
|
|
825 ;; Don't indent, only outdent. This assumes that any lines that
|
|
826 ;; are already outdented relative to py-compute-indentation were
|
|
827 ;; put there on purpose. Its highly annoying to have `:' indent
|
|
828 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
|
|
829 ;; there a better way to determine this???
|
|
830 (if (< (current-indentation) indent) nil
|
|
831 (goto-char here)
|
|
832 (beginning-of-line)
|
|
833 (delete-horizontal-space)
|
|
834 (indent-to (- indent outdent))
|
|
835 )))))
|
|
836
|
|
837
|
|
838 ;;; Functions that execute Python commands in a subprocess
|
4
|
839 ;;;###autoload
|
2
|
840 (defun py-shell ()
|
|
841 "Start an interactive Python interpreter in another window.
|
|
842 This is like Shell mode, except that Python is running in the window
|
|
843 instead of a shell. See the `Interactive Shell' and `Shell Mode'
|
|
844 sections of the Emacs manual for details, especially for the key
|
|
845 bindings active in the `*Python*' buffer.
|
|
846
|
|
847 See the docs for variable `py-scroll-buffer' for info on scrolling
|
|
848 behavior in the process window.
|
|
849
|
|
850 Warning: Don't use an interactive Python if you change sys.ps1 or
|
|
851 sys.ps2 from their default values, or if you're running code that
|
|
852 prints `>>> ' or `... ' at the start of a line. `python-mode' can't
|
|
853 distinguish your output from Python's output, and assumes that `>>> '
|
|
854 at the start of a line is a prompt from Python. Similarly, the Emacs
|
|
855 Shell mode code assumes that both `>>> ' and `... ' at the start of a
|
|
856 line are Python prompts. Bad things can happen if you fool either
|
|
857 mode.
|
|
858
|
|
859 Warning: If you do any editing *in* the process buffer *while* the
|
|
860 buffer is accepting output from Python, do NOT attempt to `undo' the
|
|
861 changes. Some of the output (nowhere near the parts you changed!) may
|
|
862 be lost if you do. This appears to be an Emacs bug, an unfortunate
|
|
863 interaction between undo and process filters; the same problem exists in
|
|
864 non-Python process buffers using the default (Emacs-supplied) process
|
|
865 filter."
|
|
866 ;; BAW - should undo be disabled in the python process buffer, if
|
|
867 ;; this bug still exists?
|
|
868 (interactive)
|
|
869 (if py-this-is-emacs-19-p
|
|
870 (progn
|
|
871 (require 'comint)
|
|
872 (switch-to-buffer-other-window
|
|
873 (make-comint "Python" py-python-command)))
|
|
874 (progn
|
|
875 (require 'shell)
|
|
876 (switch-to-buffer-other-window
|
|
877 (apply (if (fboundp 'make-shell) 'make-shell 'make-comint)
|
|
878 "Python" py-python-command nil))))
|
|
879 (make-local-variable 'shell-prompt-pattern)
|
|
880 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
|
|
881 (set-process-filter (get-buffer-process (current-buffer))
|
|
882 'py-process-filter)
|
|
883 (set-syntax-table py-mode-syntax-table))
|
|
884
|
|
885 (defun py-execute-region (start end)
|
|
886 "Send the region between START and END to a Python interpreter.
|
|
887 If there is a *Python* process it is used.
|
|
888
|
|
889 Hint: If you want to execute part of a Python file several times
|
|
890 \(e.g., perhaps you're developing a function and want to flesh it out
|
|
891 a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
|
|
892 the region of interest, and send the code to a *Python* process via
|
|
893 `\\[py-execute-buffer]' instead.
|
|
894
|
|
895 Following are subtleties to note when using a *Python* process:
|
|
896
|
|
897 If a *Python* process is used, the region is copied into a temporary
|
|
898 file (in directory `py-temp-directory'), and an `execfile' command is
|
|
899 sent to Python naming that file. If you send regions faster than
|
|
900 Python can execute them, `python-mode' will save them into distinct
|
|
901 temp files, and execute the next one in the queue the next time it
|
|
902 sees a `>>> ' prompt from Python. Each time this happens, the process
|
|
903 buffer is popped into a window (if it's not already in some window) so
|
|
904 you can see it, and a comment of the form
|
|
905
|
|
906 \t## working on region in file <name> ...
|
|
907
|
|
908 is inserted at the end.
|
|
909
|
|
910 Caution: No more than 26 regions can be pending at any given time.
|
|
911 This limit is (indirectly) inherited from libc's mktemp(3).
|
|
912 `python-mode' does not try to protect you from exceeding the limit.
|
|
913 It's extremely unlikely that you'll get anywhere close to the limit in
|
|
914 practice, unless you're trying to be a jerk <grin>.
|
|
915
|
|
916 See the `\\[py-shell]' docs for additional warnings."
|
|
917 (interactive "r")
|
|
918 (or (< start end) (error "Region is empty"))
|
|
919 (let ((pyproc (get-process "Python"))
|
|
920 fname)
|
|
921 (if (null pyproc)
|
|
922 (shell-command-on-region start end py-python-command)
|
|
923 ;; else feed it thru a temp file
|
|
924 (setq fname (py-make-temp-name))
|
|
925 (write-region start end fname nil 'no-msg)
|
|
926 (setq py-file-queue (append py-file-queue (list fname)))
|
|
927 (if (cdr py-file-queue)
|
|
928 (message "File %s queued for execution" fname)
|
|
929 ;; else
|
|
930 (py-execute-file pyproc fname)))))
|
|
931
|
|
932 (defun py-execute-file (pyproc fname)
|
|
933 (py-append-to-process-buffer
|
|
934 pyproc
|
|
935 (format "## working on region in file %s ...\n" fname))
|
|
936 (process-send-string pyproc (format "execfile('%s')\n" fname)))
|
|
937
|
|
938 (defun py-process-filter (pyproc string)
|
|
939 (let ((curbuf (current-buffer))
|
|
940 (pbuf (process-buffer pyproc))
|
|
941 (pmark (process-mark pyproc))
|
|
942 file-finished)
|
|
943
|
|
944 ;; make sure we switch to a different buffer at least once. if we
|
|
945 ;; *don't* do this, then if the process buffer is in the selected
|
|
946 ;; window, and point is before the end, and lots of output is
|
|
947 ;; coming at a fast pace, then (a) simple cursor-movement commands
|
|
948 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
|
|
949 ;; to have a visible effect (the window just doesn't get updated,
|
|
950 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
|
|
951 ;; get all the process output (until the next python prompt).
|
|
952 ;;
|
|
953 ;; #b makes no sense to me at all. #a almost makes sense: unless
|
|
954 ;; we actually change buffers, set_buffer_internal in buffer.c
|
|
955 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
|
|
956 ;; seems to make the Emacs command loop reluctant to update the
|
|
957 ;; display. Perhaps the default process filter in process.c's
|
|
958 ;; read_process_output has update_mode_lines++ for a similar
|
|
959 ;; reason? beats me ...
|
|
960
|
4
|
961 (unwind-protect
|
|
962 ;; make sure current buffer is restored
|
|
963 ;; BAW - we want to check to see if this still applies
|
|
964 (progn
|
|
965 ;; mysterious ugly hack
|
|
966 (if (eq curbuf pbuf)
|
|
967 (set-buffer (get-buffer-create "*scratch*")))
|
2
|
968
|
4
|
969 (set-buffer pbuf)
|
|
970 (let* ((start (point))
|
|
971 (goback (< start pmark))
|
|
972 (goend (and (not goback) (= start (point-max))))
|
|
973 (buffer-read-only nil))
|
|
974 (goto-char pmark)
|
|
975 (insert string)
|
|
976 (move-marker pmark (point))
|
|
977 (setq file-finished
|
|
978 (and py-file-queue
|
|
979 (equal ">>> "
|
|
980 (buffer-substring
|
|
981 (prog2 (beginning-of-line) (point)
|
|
982 (goto-char pmark))
|
|
983 (point)))))
|
|
984 (if goback (goto-char start)
|
|
985 ;; else
|
|
986 (if py-scroll-process-buffer
|
|
987 (let* ((pop-up-windows t)
|
|
988 (pwin (display-buffer pbuf)))
|
|
989 (set-window-point pwin (point)))))
|
|
990 (set-buffer curbuf)
|
|
991 (if file-finished
|
|
992 (progn
|
|
993 (py-delete-file-silently (car py-file-queue))
|
|
994 (setq py-file-queue (cdr py-file-queue))
|
|
995 (if py-file-queue
|
|
996 (py-execute-file pyproc (car py-file-queue)))))
|
|
997 (and goend
|
|
998 (progn (set-buffer pbuf)
|
|
999 (goto-char (point-max))))
|
|
1000 ))
|
|
1001 (set-buffer curbuf))))
|
2
|
1002
|
|
1003 (defun py-execute-buffer ()
|
|
1004 "Send the contents of the buffer to a Python interpreter.
|
|
1005 If there is a *Python* process buffer it is used. If a clipping
|
|
1006 restriction is in effect, only the accessible portion of the buffer is
|
|
1007 sent. A trailing newline will be supplied if needed.
|
|
1008
|
|
1009 See the `\\[py-execute-region]' docs for an account of some subtleties."
|
|
1010 (interactive)
|
|
1011 (py-execute-region (point-min) (point-max)))
|
|
1012
|
|
1013
|
|
1014
|
|
1015 ;; Functions for Python style indentation
|
|
1016 (defun py-delete-char (count)
|
|
1017 "Reduce indentation or delete character.
|
98
|
1018
|
2
|
1019 If point is at the leftmost column, deletes the preceding newline.
|
98
|
1020 Deletion is performed by calling the function in `py-delete-function'
|
|
1021 with a single argument (the number of characters to delete).
|
2
|
1022
|
|
1023 Else if point is at the leftmost non-blank character of a line that is
|
|
1024 neither a continuation line nor a non-indenting comment line, or if
|
|
1025 point is at the end of a blank line, reduces the indentation to match
|
|
1026 that of the line that opened the current block of code. The line that
|
|
1027 opened the block is displayed in the echo area to help you keep track
|
|
1028 of where you are. With numeric count, outdents that many blocks (but
|
|
1029 not past column zero).
|
|
1030
|
|
1031 Else the preceding character is deleted, converting a tab to spaces if
|
|
1032 needed so that only a single column position is deleted. Numeric
|
|
1033 argument delets that many characters."
|
|
1034 (interactive "*p")
|
|
1035 (if (or (/= (current-indentation) (current-column))
|
|
1036 (bolp)
|
|
1037 (py-continuation-line-p)
|
|
1038 (not py-honor-comment-indentation)
|
|
1039 (looking-at "#[^ \t\n]")) ; non-indenting #
|
98
|
1040 (funcall py-delete-function count)
|
2
|
1041 ;; else indent the same as the colon line that opened the block
|
|
1042
|
|
1043 ;; force non-blank so py-goto-block-up doesn't ignore it
|
|
1044 (insert-char ?* 1)
|
|
1045 (backward-char)
|
|
1046 (let ((base-indent 0) ; indentation of base line
|
|
1047 (base-text "") ; and text of base line
|
|
1048 (base-found-p nil))
|
|
1049 (save-excursion
|
|
1050 (while (< 0 count)
|
|
1051 (condition-case nil ; in case no enclosing block
|
|
1052 (progn
|
|
1053 (py-goto-block-up 'no-mark)
|
|
1054 (setq base-indent (current-indentation)
|
|
1055 base-text (py-suck-up-leading-text)
|
|
1056 base-found-p t))
|
|
1057 (error nil))
|
|
1058 (setq count (1- count))))
|
|
1059 (delete-char 1) ; toss the dummy character
|
|
1060 (delete-horizontal-space)
|
|
1061 (indent-to base-indent)
|
|
1062 (if base-found-p
|
|
1063 (message "Closes block: %s" base-text)))))
|
|
1064
|
|
1065 ;; required for pending-del and delsel modes
|
|
1066 (put 'py-delete-char 'delete-selection 'supersede)
|
|
1067 (put 'py-delete-char 'pending-delete 'supersede)
|
|
1068
|
4
|
1069 (defun py-indent-line (&optional arg)
|
|
1070 "Fix the indentation of the current line according to Python rules.
|
|
1071 With \\[universal-argument], ignore outdenting rules for block
|
|
1072 closing statements (e.g. return, raise, break, continue, pass)
|
|
1073
|
|
1074 This function is normally bound to `indent-line-function' so
|
|
1075 \\[indent-for-tab-command] will call it."
|
|
1076 (interactive "P")
|
2
|
1077 (let* ((ci (current-indentation))
|
|
1078 (move-to-indentation-p (<= (current-column) ci))
|
4
|
1079 (need (py-compute-indentation (not arg))))
|
2
|
1080 ;; see if we need to outdent
|
|
1081 (if (py-outdent-p)
|
|
1082 (setq need (- need py-indent-offset)))
|
|
1083 (if (/= ci need)
|
|
1084 (save-excursion
|
|
1085 (beginning-of-line)
|
|
1086 (delete-horizontal-space)
|
|
1087 (indent-to need)))
|
|
1088 (if move-to-indentation-p (back-to-indentation))))
|
|
1089
|
|
1090 (defun py-newline-and-indent ()
|
|
1091 "Strives to act like the Emacs `newline-and-indent'.
|
|
1092 This is just `strives to' because correct indentation can't be computed
|
|
1093 from scratch for Python code. In general, deletes the whitespace before
|
|
1094 point, inserts a newline, and takes an educated guess as to how you want
|
|
1095 the new line indented."
|
|
1096 (interactive)
|
|
1097 (let ((ci (current-indentation)))
|
|
1098 (if (< ci (current-column)) ; if point beyond indentation
|
|
1099 (newline-and-indent)
|
|
1100 ;; else try to act like newline-and-indent "normally" acts
|
|
1101 (beginning-of-line)
|
|
1102 (insert-char ?\n 1)
|
|
1103 (move-to-column ci))))
|
|
1104
|
4
|
1105 (defun py-compute-indentation (honor-block-close-p)
|
|
1106 ;; implements all the rules for indentation computation. when
|
|
1107 ;; honor-block-close-p is non-nil, statements such as return, raise,
|
|
1108 ;; break, continue, and pass force one level of outdenting.
|
2
|
1109 (save-excursion
|
|
1110 (let ((pps (parse-partial-sexp (save-excursion
|
|
1111 (beginning-of-python-def-or-class)
|
|
1112 (point))
|
|
1113 (point))))
|
|
1114 (beginning-of-line)
|
|
1115 (cond
|
|
1116 ;; are we inside a string or comment?
|
|
1117 ((or (nth 3 pps) (nth 4 pps))
|
|
1118 (save-excursion
|
|
1119 (if (not py-align-multiline-strings-p) 0
|
|
1120 ;; skip back over blank & non-indenting comment lines
|
|
1121 ;; note: will skip a blank or non-indenting comment line
|
|
1122 ;; that happens to be a continuation line too
|
|
1123 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
|
|
1124 (back-to-indentation)
|
|
1125 (current-column))))
|
|
1126 ;; are we on a continuation line?
|
|
1127 ((py-continuation-line-p)
|
|
1128 (let ((startpos (point))
|
|
1129 (open-bracket-pos (py-nesting-level))
|
|
1130 endpos searching found state)
|
|
1131 (if open-bracket-pos
|
|
1132 (progn
|
|
1133 ;; align with first item in list; else a normal
|
|
1134 ;; indent beyond the line with the open bracket
|
|
1135 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
|
|
1136 ;; is the first list item on the same line?
|
|
1137 (skip-chars-forward " \t")
|
|
1138 (if (null (memq (following-char) '(?\n ?# ?\\)))
|
|
1139 ; yes, so line up with it
|
|
1140 (current-column)
|
|
1141 ;; first list item on another line, or doesn't exist yet
|
|
1142 (forward-line 1)
|
|
1143 (while (and (< (point) startpos)
|
|
1144 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
|
|
1145 (forward-line 1))
|
|
1146 (if (< (point) startpos)
|
|
1147 ;; again mimic the first list item
|
|
1148 (current-indentation)
|
|
1149 ;; else they're about to enter the first item
|
|
1150 (goto-char open-bracket-pos)
|
|
1151 (+ (current-indentation) py-indent-offset))))
|
|
1152
|
|
1153 ;; else on backslash continuation line
|
|
1154 (forward-line -1)
|
|
1155 (if (py-continuation-line-p) ; on at least 3rd line in block
|
|
1156 (current-indentation) ; so just continue the pattern
|
|
1157 ;; else started on 2nd line in block, so indent more.
|
|
1158 ;; if base line is an assignment with a start on a RHS,
|
|
1159 ;; indent to 2 beyond the leftmost "="; else skip first
|
|
1160 ;; chunk of non-whitespace characters on base line, + 1 more
|
|
1161 ;; column
|
|
1162 (end-of-line)
|
|
1163 (setq endpos (point) searching t)
|
|
1164 (back-to-indentation)
|
|
1165 (setq startpos (point))
|
|
1166 ;; look at all "=" from left to right, stopping at first
|
|
1167 ;; one not nested in a list or string
|
|
1168 (while searching
|
|
1169 (skip-chars-forward "^=" endpos)
|
|
1170 (if (= (point) endpos)
|
|
1171 (setq searching nil)
|
|
1172 (forward-char 1)
|
|
1173 (setq state (parse-partial-sexp startpos (point)))
|
|
1174 (if (and (zerop (car state)) ; not in a bracket
|
|
1175 (null (nth 3 state))) ; & not in a string
|
|
1176 (progn
|
|
1177 (setq searching nil) ; done searching in any case
|
|
1178 (setq found
|
|
1179 (not (or
|
|
1180 (eq (following-char) ?=)
|
|
1181 (memq (char-after (- (point) 2))
|
|
1182 '(?< ?> ?!)))))))))
|
|
1183 (if (or (not found) ; not an assignment
|
|
1184 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
|
|
1185 (progn
|
|
1186 (goto-char startpos)
|
|
1187 (skip-chars-forward "^ \t\n")))
|
|
1188 (1+ (current-column))))))
|
|
1189
|
|
1190 ;; not on a continuation line
|
|
1191 ((bobp) (current-indentation))
|
|
1192
|
|
1193 ;; Dfn: "Indenting comment line". A line containing only a
|
|
1194 ;; comment, but which is treated like a statement for
|
|
1195 ;; indentation calculation purposes. Such lines are only
|
|
1196 ;; treated specially by the mode; they are not treated
|
|
1197 ;; specially by the Python interpreter.
|
|
1198
|
|
1199 ;; The rules for indenting comment lines are a line where:
|
|
1200 ;; - the first non-whitespace character is `#', and
|
|
1201 ;; - the character following the `#' is whitespace, and
|
|
1202 ;; - the line is outdented with respect to (i.e. to the left
|
|
1203 ;; of) the indentation of the preceding non-blank line.
|
|
1204
|
|
1205 ;; The first non-blank line following an indenting comment
|
|
1206 ;; line is given the same amount of indentation as the
|
|
1207 ;; indenting comment line.
|
|
1208
|
|
1209 ;; All other comment-only lines are ignored for indentation
|
|
1210 ;; purposes.
|
|
1211
|
|
1212 ;; Are we looking at a comment-only line which is *not* an
|
|
1213 ;; indenting comment line? If so, we assume that its been
|
|
1214 ;; placed at the desired indentation, so leave it alone.
|
|
1215 ;; Indenting comment lines are aligned as statements down
|
|
1216 ;; below.
|
|
1217 ((and (looking-at "[ \t]*#[^ \t\n]")
|
|
1218 ;; NOTE: this test will not be performed in older Emacsen
|
|
1219 (fboundp 'forward-comment)
|
|
1220 (<= (current-indentation)
|
|
1221 (save-excursion
|
|
1222 (forward-comment (- (point-max)))
|
|
1223 (current-indentation))))
|
|
1224 (current-indentation))
|
|
1225
|
|
1226 ;; else indentation based on that of the statement that
|
|
1227 ;; precedes us; use the first line of that statement to
|
|
1228 ;; establish the base, in case the user forced a non-std
|
|
1229 ;; indentation for the continuation lines (if any)
|
|
1230 (t
|
|
1231 ;; skip back over blank & non-indenting comment lines note:
|
|
1232 ;; will skip a blank or non-indenting comment line that
|
|
1233 ;; happens to be a continuation line too. use fast Emacs 19
|
|
1234 ;; function if it's there.
|
|
1235 (if (and (eq py-honor-comment-indentation nil)
|
|
1236 (fboundp 'forward-comment))
|
|
1237 (forward-comment (- (point-max)))
|
|
1238 (let (done)
|
|
1239 (while (not done)
|
|
1240 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
|
|
1241 nil 'move)
|
|
1242 (setq done (or (eq py-honor-comment-indentation t)
|
|
1243 (bobp)
|
|
1244 (/= (following-char) ?#)
|
|
1245 (not (zerop (current-column)))))
|
|
1246 )))
|
|
1247 ;; if we landed inside a string, go to the beginning of that
|
|
1248 ;; string. this handles triple quoted, multi-line spanning
|
|
1249 ;; strings.
|
|
1250 (py-goto-initial-line)
|
|
1251 (+ (current-indentation)
|
|
1252 (if (py-statement-opens-block-p)
|
|
1253 py-indent-offset
|
4
|
1254 (if (and honor-block-close-p (py-statement-closes-block-p))
|
2
|
1255 (- py-indent-offset)
|
|
1256 0)))
|
|
1257 )))))
|
|
1258
|
|
1259 (defun py-guess-indent-offset (&optional global)
|
|
1260 "Guess a good value for, and change, `py-indent-offset'.
|
|
1261 By default (without a prefix arg), makes a buffer-local copy of
|
|
1262 `py-indent-offset' with the new value. This will not affect any other
|
|
1263 Python buffers. With a prefix arg, changes the global value of
|
|
1264 `py-indent-offset'. This affects all Python buffers (that don't have
|
|
1265 their own buffer-local copy), both those currently existing and those
|
|
1266 created later in the Emacs session.
|
|
1267
|
|
1268 Some people use a different value for `py-indent-offset' than you use.
|
|
1269 There's no excuse for such foolishness, but sometimes you have to deal
|
|
1270 with their ugly code anyway. This function examines the file and sets
|
|
1271 `py-indent-offset' to what it thinks it was when they created the
|
|
1272 mess.
|
|
1273
|
|
1274 Specifically, it searches forward from the statement containing point,
|
|
1275 looking for a line that opens a block of code. `py-indent-offset' is
|
|
1276 set to the difference in indentation between that line and the Python
|
|
1277 statement following it. If the search doesn't succeed going forward,
|
|
1278 it's tried again going backward."
|
|
1279 (interactive "P") ; raw prefix arg
|
|
1280 (let (new-value
|
|
1281 (start (point))
|
|
1282 restart
|
|
1283 (found nil)
|
|
1284 colon-indent)
|
|
1285 (py-goto-initial-line)
|
|
1286 (while (not (or found (eobp)))
|
|
1287 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
|
|
1288 (progn
|
|
1289 (setq restart (point))
|
|
1290 (py-goto-initial-line)
|
|
1291 (if (py-statement-opens-block-p)
|
|
1292 (setq found t)
|
|
1293 (goto-char restart)))))
|
|
1294 (if found
|
|
1295 ()
|
|
1296 (goto-char start)
|
|
1297 (py-goto-initial-line)
|
|
1298 (while (not (or found (bobp)))
|
|
1299 (setq found
|
|
1300 (and
|
|
1301 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
|
|
1302 (or (py-goto-initial-line) t) ; always true -- side effect
|
|
1303 (py-statement-opens-block-p)))))
|
|
1304 (setq colon-indent (current-indentation)
|
|
1305 found (and found (zerop (py-next-statement 1)))
|
|
1306 new-value (- (current-indentation) colon-indent))
|
|
1307 (goto-char start)
|
|
1308 (if found
|
|
1309 (progn
|
|
1310 (funcall (if global 'kill-local-variable 'make-local-variable)
|
|
1311 'py-indent-offset)
|
|
1312 (setq py-indent-offset new-value)
|
|
1313 (message "%s value of py-indent-offset set to %d"
|
|
1314 (if global "Global" "Local")
|
|
1315 py-indent-offset))
|
|
1316 (error "Sorry, couldn't guess a value for py-indent-offset"))))
|
|
1317
|
|
1318 (defun py-shift-region (start end count)
|
|
1319 (save-excursion
|
|
1320 (goto-char end) (beginning-of-line) (setq end (point))
|
|
1321 (goto-char start) (beginning-of-line) (setq start (point))
|
|
1322 (indent-rigidly start end count)))
|
|
1323
|
|
1324 (defun py-shift-region-left (start end &optional count)
|
|
1325 "Shift region of Python code to the left.
|
|
1326 The lines from the line containing the start of the current region up
|
|
1327 to (but not including) the line containing the end of the region are
|
|
1328 shifted to the left, by `py-indent-offset' columns.
|
|
1329
|
|
1330 If a prefix argument is given, the region is instead shifted by that
|
|
1331 many columns. With no active region, outdent only the current line.
|
|
1332 You cannot outdent the region if any line is already at column zero."
|
|
1333 (interactive
|
|
1334 (let ((p (point))
|
|
1335 (m (mark))
|
|
1336 (arg current-prefix-arg))
|
|
1337 (if m
|
|
1338 (list (min p m) (max p m) arg)
|
|
1339 (list p (save-excursion (forward-line 1) (point)) arg))))
|
|
1340 ;; if any line is at column zero, don't shift the region
|
|
1341 (save-excursion
|
|
1342 (goto-char start)
|
|
1343 (while (< (point) end)
|
|
1344 (back-to-indentation)
|
|
1345 (if (and (zerop (current-column))
|
|
1346 (not (looking-at "\\s *$")))
|
|
1347 (error "Region is at left edge."))
|
|
1348 (forward-line 1)))
|
|
1349 (py-shift-region start end (- (prefix-numeric-value
|
|
1350 (or count py-indent-offset))))
|
|
1351 (py-keep-region-active))
|
|
1352
|
|
1353 (defun py-shift-region-right (start end &optional count)
|
|
1354 "Shift region of Python code to the right.
|
|
1355 The lines from the line containing the start of the current region up
|
|
1356 to (but not including) the line containing the end of the region are
|
|
1357 shifted to the right, by `py-indent-offset' columns.
|
|
1358
|
|
1359 If a prefix argument is given, the region is instead shifted by that
|
|
1360 many columns. With no active region, indent only the current line."
|
|
1361 (interactive
|
|
1362 (let ((p (point))
|
|
1363 (m (mark))
|
|
1364 (arg current-prefix-arg))
|
|
1365 (if m
|
|
1366 (list (min p m) (max p m) arg)
|
|
1367 (list p (save-excursion (forward-line 1) (point)) arg))))
|
|
1368 (py-shift-region start end (prefix-numeric-value
|
|
1369 (or count py-indent-offset)))
|
|
1370 (py-keep-region-active))
|
|
1371
|
|
1372 (defun py-indent-region (start end &optional indent-offset)
|
|
1373 "Reindent a region of Python code.
|
|
1374
|
|
1375 The lines from the line containing the start of the current region up
|
|
1376 to (but not including) the line containing the end of the region are
|
|
1377 reindented. If the first line of the region has a non-whitespace
|
|
1378 character in the first column, the first line is left alone and the
|
|
1379 rest of the region is reindented with respect to it. Else the entire
|
|
1380 region is reindented with respect to the (closest code or indenting
|
|
1381 comment) statement immediately preceding the region.
|
|
1382
|
|
1383 This is useful when code blocks are moved or yanked, when enclosing
|
|
1384 control structures are introduced or removed, or to reformat code
|
|
1385 using a new value for the indentation offset.
|
|
1386
|
|
1387 If a numeric prefix argument is given, it will be used as the value of
|
|
1388 the indentation offset. Else the value of `py-indent-offset' will be
|
|
1389 used.
|
|
1390
|
|
1391 Warning: The region must be consistently indented before this function
|
|
1392 is called! This function does not compute proper indentation from
|
|
1393 scratch (that's impossible in Python), it merely adjusts the existing
|
|
1394 indentation to be correct in context.
|
|
1395
|
|
1396 Warning: This function really has no idea what to do with
|
|
1397 non-indenting comment lines, and shifts them as if they were indenting
|
|
1398 comment lines. Fixing this appears to require telepathy.
|
|
1399
|
|
1400 Special cases: whitespace is deleted from blank lines; continuation
|
|
1401 lines are shifted by the same amount their initial line was shifted,
|
|
1402 in order to preserve their relative indentation with respect to their
|
|
1403 initial line; and comment lines beginning in column 1 are ignored."
|
|
1404 (interactive "*r\nP") ; region; raw prefix arg
|
|
1405 (save-excursion
|
|
1406 (goto-char end) (beginning-of-line) (setq end (point-marker))
|
|
1407 (goto-char start) (beginning-of-line)
|
|
1408 (let ((py-indent-offset (prefix-numeric-value
|
|
1409 (or indent-offset py-indent-offset)))
|
|
1410 (indents '(-1)) ; stack of active indent levels
|
|
1411 (target-column 0) ; column to which to indent
|
|
1412 (base-shifted-by 0) ; amount last base line was shifted
|
|
1413 (indent-base (if (looking-at "[ \t\n]")
|
4
|
1414 (py-compute-indentation t)
|
2
|
1415 0))
|
|
1416 ci)
|
|
1417 (while (< (point) end)
|
|
1418 (setq ci (current-indentation))
|
|
1419 ;; figure out appropriate target column
|
|
1420 (cond
|
|
1421 ((or (eq (following-char) ?#) ; comment in column 1
|
|
1422 (looking-at "[ \t]*$")) ; entirely blank
|
|
1423 (setq target-column 0))
|
|
1424 ((py-continuation-line-p) ; shift relative to base line
|
|
1425 (setq target-column (+ ci base-shifted-by)))
|
|
1426 (t ; new base line
|
|
1427 (if (> ci (car indents)) ; going deeper; push it
|
|
1428 (setq indents (cons ci indents))
|
|
1429 ;; else we should have seen this indent before
|
|
1430 (setq indents (memq ci indents)) ; pop deeper indents
|
|
1431 (if (null indents)
|
|
1432 (error "Bad indentation in region, at line %d"
|
|
1433 (save-restriction
|
|
1434 (widen)
|
|
1435 (1+ (count-lines 1 (point)))))))
|
|
1436 (setq target-column (+ indent-base
|
|
1437 (* py-indent-offset
|
|
1438 (- (length indents) 2))))
|
|
1439 (setq base-shifted-by (- target-column ci))))
|
|
1440 ;; shift as needed
|
|
1441 (if (/= ci target-column)
|
|
1442 (progn
|
|
1443 (delete-horizontal-space)
|
|
1444 (indent-to target-column)))
|
|
1445 (forward-line 1))))
|
|
1446 (set-marker end nil))
|
|
1447
|
|
1448 (defun py-comment-region (beg end &optional arg)
|
|
1449 "Like `comment-region' but uses double hash (`#') comment starter."
|
|
1450 (interactive "r\nP")
|
|
1451 (let ((comment-start py-block-comment-prefix))
|
|
1452 (comment-region beg end arg)))
|
|
1453
|
|
1454
|
|
1455 ;; Functions for moving point
|
|
1456 (defun py-previous-statement (count)
|
|
1457 "Go to the start of previous Python statement.
|
|
1458 If the statement at point is the i'th Python statement, goes to the
|
|
1459 start of statement i-COUNT. If there is no such statement, goes to the
|
|
1460 first statement. Returns count of statements left to move.
|
|
1461 `Statements' do not include blank, comment, or continuation lines."
|
|
1462 (interactive "p") ; numeric prefix arg
|
|
1463 (if (< count 0) (py-next-statement (- count))
|
|
1464 (py-goto-initial-line)
|
|
1465 (let (start)
|
|
1466 (while (and
|
|
1467 (setq start (point)) ; always true -- side effect
|
|
1468 (> count 0)
|
|
1469 (zerop (forward-line -1))
|
|
1470 (py-goto-statement-at-or-above))
|
|
1471 (setq count (1- count)))
|
|
1472 (if (> count 0) (goto-char start)))
|
|
1473 count))
|
|
1474
|
|
1475 (defun py-next-statement (count)
|
|
1476 "Go to the start of next Python statement.
|
|
1477 If the statement at point is the i'th Python statement, goes to the
|
|
1478 start of statement i+COUNT. If there is no such statement, goes to the
|
|
1479 last statement. Returns count of statements left to move. `Statements'
|
|
1480 do not include blank, comment, or continuation lines."
|
|
1481 (interactive "p") ; numeric prefix arg
|
|
1482 (if (< count 0) (py-previous-statement (- count))
|
|
1483 (beginning-of-line)
|
|
1484 (let (start)
|
|
1485 (while (and
|
|
1486 (setq start (point)) ; always true -- side effect
|
|
1487 (> count 0)
|
|
1488 (py-goto-statement-below))
|
|
1489 (setq count (1- count)))
|
|
1490 (if (> count 0) (goto-char start)))
|
|
1491 count))
|
|
1492
|
|
1493 (defun py-goto-block-up (&optional nomark)
|
|
1494 "Move up to start of current block.
|
|
1495 Go to the statement that starts the smallest enclosing block; roughly
|
|
1496 speaking, this will be the closest preceding statement that ends with a
|
|
1497 colon and is indented less than the statement you started on. If
|
|
1498 successful, also sets the mark to the starting point.
|
|
1499
|
|
1500 `\\[py-mark-block]' can be used afterward to mark the whole code
|
|
1501 block, if desired.
|
|
1502
|
|
1503 If called from a program, the mark will not be set if optional argument
|
|
1504 NOMARK is not nil."
|
|
1505 (interactive)
|
|
1506 (let ((start (point))
|
|
1507 (found nil)
|
|
1508 initial-indent)
|
|
1509 (py-goto-initial-line)
|
|
1510 ;; if on blank or non-indenting comment line, use the preceding stmt
|
|
1511 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
|
|
1512 (progn
|
|
1513 (py-goto-statement-at-or-above)
|
|
1514 (setq found (py-statement-opens-block-p))))
|
|
1515 ;; search back for colon line indented less
|
|
1516 (setq initial-indent (current-indentation))
|
|
1517 (if (zerop initial-indent)
|
|
1518 ;; force fast exit
|
|
1519 (goto-char (point-min)))
|
|
1520 (while (not (or found (bobp)))
|
|
1521 (setq found
|
|
1522 (and
|
|
1523 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
|
|
1524 (or (py-goto-initial-line) t) ; always true -- side effect
|
|
1525 (< (current-indentation) initial-indent)
|
|
1526 (py-statement-opens-block-p))))
|
|
1527 (if found
|
|
1528 (progn
|
|
1529 (or nomark (push-mark start))
|
|
1530 (back-to-indentation))
|
|
1531 (goto-char start)
|
|
1532 (error "Enclosing block not found"))))
|
|
1533
|
|
1534 (defun beginning-of-python-def-or-class (&optional class)
|
|
1535 "Move point to start of def (or class, with prefix arg).
|
|
1536
|
|
1537 Searches back for the closest preceding `def'. If you supply a prefix
|
|
1538 arg, looks for a `class' instead. The docs assume the `def' case;
|
|
1539 just substitute `class' for `def' for the other case.
|
|
1540
|
|
1541 If point is in a def statement already, and after the `d', simply
|
|
1542 moves point to the start of the statement.
|
|
1543
|
|
1544 Else (point is not in a def statement, or at or before the `d' of a
|
|
1545 def statement), searches for the closest preceding def statement, and
|
|
1546 leaves point at its start. If no such statement can be found, leaves
|
|
1547 point at the start of the buffer.
|
|
1548
|
|
1549 Returns t iff a def statement is found by these rules.
|
|
1550
|
|
1551 Note that doing this command repeatedly will take you closer to the
|
|
1552 start of the buffer each time.
|
|
1553
|
|
1554 If you want to mark the current def/class, see
|
|
1555 `\\[mark-python-def-or-class]'."
|
|
1556 (interactive "P") ; raw prefix arg
|
|
1557 (let ((at-or-before-p (<= (current-column) (current-indentation)))
|
|
1558 (start-of-line (progn (beginning-of-line) (point)))
|
|
1559 (start-of-stmt (progn (py-goto-initial-line) (point))))
|
|
1560 (if (or (/= start-of-stmt start-of-line)
|
|
1561 (not at-or-before-p))
|
|
1562 (end-of-line)) ; OK to match on this line
|
|
1563 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
|
|
1564 nil 'move)))
|
|
1565
|
|
1566 (defun end-of-python-def-or-class (&optional class)
|
|
1567 "Move point beyond end of def (or class, with prefix arg) body.
|
|
1568
|
|
1569 By default, looks for an appropriate `def'. If you supply a prefix arg,
|
|
1570 looks for a `class' instead. The docs assume the `def' case; just
|
|
1571 substitute `class' for `def' for the other case.
|
|
1572
|
|
1573 If point is in a def statement already, this is the def we use.
|
|
1574
|
|
1575 Else if the def found by `\\[beginning-of-python-def-or-class]'
|
|
1576 contains the statement you started on, that's the def we use.
|
|
1577
|
|
1578 Else we search forward for the closest following def, and use that.
|
|
1579
|
|
1580 If a def can be found by these rules, point is moved to the start of
|
|
1581 the line immediately following the def block, and the position of the
|
|
1582 start of the def is returned.
|
|
1583
|
|
1584 Else point is moved to the end of the buffer, and nil is returned.
|
|
1585
|
|
1586 Note that doing this command repeatedly will take you closer to the
|
|
1587 end of the buffer each time.
|
|
1588
|
|
1589 If you want to mark the current def/class, see
|
|
1590 `\\[mark-python-def-or-class]'."
|
|
1591 (interactive "P") ; raw prefix arg
|
|
1592 (let ((start (progn (py-goto-initial-line) (point)))
|
|
1593 (which (if class "class" "def"))
|
|
1594 (state 'not-found))
|
|
1595 ;; move point to start of appropriate def/class
|
|
1596 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
|
|
1597 (setq state 'at-beginning)
|
|
1598 ;; else see if beginning-of-python-def-or-class hits container
|
|
1599 (if (and (beginning-of-python-def-or-class class)
|
|
1600 (progn (py-goto-beyond-block)
|
|
1601 (> (point) start)))
|
|
1602 (setq state 'at-end)
|
|
1603 ;; else search forward
|
|
1604 (goto-char start)
|
|
1605 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
|
|
1606 (progn (setq state 'at-beginning)
|
|
1607 (beginning-of-line)))))
|
|
1608 (cond
|
|
1609 ((eq state 'at-beginning) (py-goto-beyond-block) t)
|
|
1610 ((eq state 'at-end) t)
|
|
1611 ((eq state 'not-found) nil)
|
|
1612 (t (error "internal error in end-of-python-def-or-class")))))
|
|
1613
|
|
1614
|
|
1615 ;; Functions for marking regions
|
|
1616 (defun py-mark-block (&optional extend just-move)
|
|
1617 "Mark following block of lines. With prefix arg, mark structure.
|
|
1618 Easier to use than explain. It sets the region to an `interesting'
|
|
1619 block of succeeding lines. If point is on a blank line, it goes down to
|
|
1620 the next non-blank line. That will be the start of the region. The end
|
|
1621 of the region depends on the kind of line at the start:
|
|
1622
|
|
1623 - If a comment, the region will include all succeeding comment lines up
|
|
1624 to (but not including) the next non-comment line (if any).
|
|
1625
|
|
1626 - Else if a prefix arg is given, and the line begins one of these
|
|
1627 structures:
|
|
1628
|
|
1629 if elif else try except finally for while def class
|
|
1630
|
|
1631 the region will be set to the body of the structure, including
|
|
1632 following blocks that `belong' to it, but excluding trailing blank
|
|
1633 and comment lines. E.g., if on a `try' statement, the `try' block
|
|
1634 and all (if any) of the following `except' and `finally' blocks
|
|
1635 that belong to the `try' structure will be in the region. Ditto
|
|
1636 for if/elif/else, for/else and while/else structures, and (a bit
|
|
1637 degenerate, since they're always one-block structures) def and
|
|
1638 class blocks.
|
|
1639
|
|
1640 - Else if no prefix argument is given, and the line begins a Python
|
|
1641 block (see list above), and the block is not a `one-liner' (i.e.,
|
|
1642 the statement ends with a colon, not with code), the region will
|
|
1643 include all succeeding lines up to (but not including) the next
|
|
1644 code statement (if any) that's indented no more than the starting
|
|
1645 line, except that trailing blank and comment lines are excluded.
|
|
1646 E.g., if the starting line begins a multi-statement `def'
|
|
1647 structure, the region will be set to the full function definition,
|
|
1648 but without any trailing `noise' lines.
|
|
1649
|
|
1650 - Else the region will include all succeeding lines up to (but not
|
|
1651 including) the next blank line, or code or indenting-comment line
|
|
1652 indented strictly less than the starting line. Trailing indenting
|
|
1653 comment lines are included in this case, but not trailing blank
|
|
1654 lines.
|
|
1655
|
|
1656 A msg identifying the location of the mark is displayed in the echo
|
|
1657 area; or do `\\[exchange-point-and-mark]' to flip down to the end.
|
|
1658
|
|
1659 If called from a program, optional argument EXTEND plays the role of
|
|
1660 the prefix arg, and if optional argument JUST-MOVE is not nil, just
|
|
1661 moves to the end of the block (& does not set mark or display a msg)."
|
|
1662 (interactive "P") ; raw prefix arg
|
|
1663 (py-goto-initial-line)
|
|
1664 ;; skip over blank lines
|
|
1665 (while (and
|
|
1666 (looking-at "[ \t]*$") ; while blank line
|
|
1667 (not (eobp))) ; & somewhere to go
|
|
1668 (forward-line 1))
|
|
1669 (if (eobp)
|
|
1670 (error "Hit end of buffer without finding a non-blank stmt"))
|
|
1671 (let ((initial-pos (point))
|
|
1672 (initial-indent (current-indentation))
|
|
1673 last-pos ; position of last stmt in region
|
|
1674 (followers
|
|
1675 '((if elif else) (elif elif else) (else)
|
|
1676 (try except finally) (except except) (finally)
|
|
1677 (for else) (while else)
|
|
1678 (def) (class) ) )
|
|
1679 first-symbol next-symbol)
|
|
1680
|
|
1681 (cond
|
|
1682 ;; if comment line, suck up the following comment lines
|
|
1683 ((looking-at "[ \t]*#")
|
|
1684 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
|
|
1685 (re-search-backward "^[ \t]*#") ; and back to last comment in block
|
|
1686 (setq last-pos (point)))
|
|
1687
|
|
1688 ;; else if line is a block line and EXTEND given, suck up
|
|
1689 ;; the whole structure
|
|
1690 ((and extend
|
|
1691 (setq first-symbol (py-suck-up-first-keyword) )
|
|
1692 (assq first-symbol followers))
|
|
1693 (while (and
|
|
1694 (or (py-goto-beyond-block) t) ; side effect
|
|
1695 (forward-line -1) ; side effect
|
|
1696 (setq last-pos (point)) ; side effect
|
|
1697 (py-goto-statement-below)
|
|
1698 (= (current-indentation) initial-indent)
|
|
1699 (setq next-symbol (py-suck-up-first-keyword))
|
|
1700 (memq next-symbol (cdr (assq first-symbol followers))))
|
|
1701 (setq first-symbol next-symbol)))
|
|
1702
|
|
1703 ;; else if line *opens* a block, search for next stmt indented <=
|
|
1704 ((py-statement-opens-block-p)
|
|
1705 (while (and
|
|
1706 (setq last-pos (point)) ; always true -- side effect
|
|
1707 (py-goto-statement-below)
|
|
1708 (> (current-indentation) initial-indent))
|
|
1709 nil))
|
|
1710
|
|
1711 ;; else plain code line; stop at next blank line, or stmt or
|
|
1712 ;; indenting comment line indented <
|
|
1713 (t
|
|
1714 (while (and
|
|
1715 (setq last-pos (point)) ; always true -- side effect
|
|
1716 (or (py-goto-beyond-final-line) t)
|
|
1717 (not (looking-at "[ \t]*$")) ; stop at blank line
|
|
1718 (or
|
|
1719 (>= (current-indentation) initial-indent)
|
|
1720 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
|
|
1721 nil)))
|
|
1722
|
|
1723 ;; skip to end of last stmt
|
|
1724 (goto-char last-pos)
|
|
1725 (py-goto-beyond-final-line)
|
|
1726
|
|
1727 ;; set mark & display
|
|
1728 (if just-move
|
|
1729 () ; just return
|
|
1730 (push-mark (point) 'no-msg)
|
|
1731 (forward-line -1)
|
|
1732 (message "Mark set after: %s" (py-suck-up-leading-text))
|
|
1733 (goto-char initial-pos))))
|
|
1734
|
|
1735 (defun mark-python-def-or-class (&optional class)
|
|
1736 "Set region to body of def (or class, with prefix arg) enclosing point.
|
|
1737 Pushes the current mark, then point, on the mark ring (all language
|
|
1738 modes do this, but although it's handy it's never documented ...).
|
|
1739
|
|
1740 In most Emacs language modes, this function bears at least a
|
|
1741 hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
|
|
1742 `\\[beginning-of-python-def-or-class]'.
|
|
1743
|
|
1744 And in earlier versions of Python mode, all 3 were tightly connected.
|
|
1745 Turned out that was more confusing than useful: the `goto start' and
|
|
1746 `goto end' commands are usually used to search through a file, and
|
|
1747 people expect them to act a lot like `search backward' and `search
|
|
1748 forward' string-search commands. But because Python `def' and `class'
|
|
1749 can nest to arbitrary levels, finding the smallest def containing
|
|
1750 point cannot be done via a simple backward search: the def containing
|
|
1751 point may not be the closest preceding def, or even the closest
|
|
1752 preceding def that's indented less. The fancy algorithm required is
|
|
1753 appropriate for the usual uses of this `mark' command, but not for the
|
|
1754 `goto' variations.
|
|
1755
|
|
1756 So the def marked by this command may not be the one either of the
|
|
1757 `goto' commands find: If point is on a blank or non-indenting comment
|
|
1758 line, moves back to start of the closest preceding code statement or
|
|
1759 indenting comment line. If this is a `def' statement, that's the def
|
|
1760 we use. Else searches for the smallest enclosing `def' block and uses
|
|
1761 that. Else signals an error.
|
|
1762
|
|
1763 When an enclosing def is found: The mark is left immediately beyond
|
|
1764 the last line of the def block. Point is left at the start of the
|
|
1765 def, except that: if the def is preceded by a number of comment lines
|
|
1766 followed by (at most) one optional blank line, point is left at the
|
|
1767 start of the comments; else if the def is preceded by a blank line,
|
|
1768 point is left at its start.
|
|
1769
|
|
1770 The intent is to mark the containing def/class and its associated
|
|
1771 documentation, to make moving and duplicating functions and classes
|
|
1772 pleasant."
|
|
1773 (interactive "P") ; raw prefix arg
|
|
1774 (let ((start (point))
|
|
1775 (which (if class "class" "def")))
|
|
1776 (push-mark start)
|
|
1777 (if (not (py-go-up-tree-to-keyword which))
|
|
1778 (progn (goto-char start)
|
|
1779 (error "Enclosing %s not found" which))
|
|
1780 ;; else enclosing def/class found
|
|
1781 (setq start (point))
|
|
1782 (py-goto-beyond-block)
|
|
1783 (push-mark (point))
|
|
1784 (goto-char start)
|
|
1785 (if (zerop (forward-line -1)) ; if there is a preceding line
|
|
1786 (progn
|
|
1787 (if (looking-at "[ \t]*$") ; it's blank
|
|
1788 (setq start (point)) ; so reset start point
|
|
1789 (goto-char start)) ; else try again
|
|
1790 (if (zerop (forward-line -1))
|
|
1791 (if (looking-at "[ \t]*#") ; a comment
|
|
1792 ;; look back for non-comment line
|
|
1793 ;; tricky: note that the regexp matches a blank
|
|
1794 ;; line, cuz \n is in the 2nd character class
|
|
1795 (and
|
|
1796 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
|
|
1797 (forward-line 1))
|
|
1798 ;; no comment, so go back
|
|
1799 (goto-char start))))))))
|
|
1800
|
|
1801 ;; ripped from cc-mode
|
|
1802 (defun py-forward-into-nomenclature (&optional arg)
|
|
1803 "Move forward to end of a nomenclature section or word.
|
|
1804 With arg, to it arg times.
|
|
1805
|
|
1806 A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
|
|
1807 (interactive "p")
|
|
1808 (let ((case-fold-search nil))
|
|
1809 (if (> arg 0)
|
4
|
1810 (re-search-forward
|
|
1811 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
|
|
1812 (point-max) t arg)
|
2
|
1813 (while (and (< arg 0)
|
|
1814 (re-search-backward
|
4
|
1815 "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
|
2
|
1816 (point-min) 0))
|
|
1817 (forward-char 1)
|
|
1818 (setq arg (1+ arg)))))
|
|
1819 (py-keep-region-active))
|
|
1820
|
|
1821 (defun py-backward-into-nomenclature (&optional arg)
|
|
1822 "Move backward to beginning of a nomenclature section or word.
|
|
1823 With optional ARG, move that many times. If ARG is negative, move
|
|
1824 forward.
|
|
1825
|
|
1826 A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
|
|
1827 (interactive "p")
|
|
1828 (py-forward-into-nomenclature (- arg))
|
|
1829 (py-keep-region-active))
|
|
1830
|
|
1831
|
|
1832
|
|
1833 ;; Documentation functions
|
|
1834
|
|
1835 ;; dump the long form of the mode blurb; does the usual doc escapes,
|
|
1836 ;; plus lines of the form ^[vc]:name$ to suck variable & command docs
|
|
1837 ;; out of the right places, along with the keys they're on & current
|
|
1838 ;; values
|
|
1839 (defun py-dump-help-string (str)
|
|
1840 (with-output-to-temp-buffer "*Help*"
|
|
1841 (let ((locals (buffer-local-variables))
|
|
1842 funckind funcname func funcdoc
|
|
1843 (start 0) mstart end
|
|
1844 keys )
|
|
1845 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
|
|
1846 (setq mstart (match-beginning 0) end (match-end 0)
|
|
1847 funckind (substring str (match-beginning 1) (match-end 1))
|
|
1848 funcname (substring str (match-beginning 2) (match-end 2))
|
|
1849 func (intern funcname))
|
|
1850 (princ (substitute-command-keys (substring str start mstart)))
|
|
1851 (cond
|
|
1852 ((equal funckind "c") ; command
|
|
1853 (setq funcdoc (documentation func)
|
|
1854 keys (concat
|
|
1855 "Key(s): "
|
|
1856 (mapconcat 'key-description
|
|
1857 (where-is-internal func py-mode-map)
|
|
1858 ", "))))
|
|
1859 ((equal funckind "v") ; variable
|
4
|
1860 (setq funcdoc (documentation-property func 'variable-documentation)
|
2
|
1861 keys (if (assq func locals)
|
|
1862 (concat
|
|
1863 "Local/Global values: "
|
|
1864 (prin1-to-string (symbol-value func))
|
|
1865 " / "
|
|
1866 (prin1-to-string (default-value func)))
|
|
1867 (concat
|
|
1868 "Value: "
|
|
1869 (prin1-to-string (symbol-value func))))))
|
|
1870 (t ; unexpected
|
|
1871 (error "Error in py-dump-help-string, tag `%s'" funckind)))
|
|
1872 (princ (format "\n-> %s:\t%s\t%s\n\n"
|
|
1873 (if (equal funckind "c") "Command" "Variable")
|
|
1874 funcname keys))
|
|
1875 (princ funcdoc)
|
|
1876 (terpri)
|
|
1877 (setq start end))
|
|
1878 (princ (substitute-command-keys (substring str start))))
|
|
1879 (print-help-return-message)))
|
|
1880
|
|
1881 (defun py-describe-mode ()
|
|
1882 "Dump long form of Python-mode docs."
|
|
1883 (interactive)
|
|
1884 (py-dump-help-string "Major mode for editing Python files.
|
|
1885 Knows about Python indentation, tokens, comments and continuation lines.
|
|
1886 Paragraphs are separated by blank lines only.
|
|
1887
|
|
1888 Major sections below begin with the string `@'; specific function and
|
|
1889 variable docs begin with `->'.
|
|
1890
|
|
1891 @EXECUTING PYTHON CODE
|
|
1892
|
|
1893 \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
|
|
1894 \\[py-execute-region]\tsends the current region
|
|
1895 \\[py-shell]\tstarts a Python interpreter window; this will be used by
|
|
1896 \tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
|
|
1897 %c:py-execute-buffer
|
|
1898 %c:py-execute-region
|
|
1899 %c:py-shell
|
|
1900
|
|
1901 @VARIABLES
|
|
1902
|
|
1903 py-indent-offset\tindentation increment
|
|
1904 py-block-comment-prefix\tcomment string used by comment-region
|
|
1905
|
|
1906 py-python-command\tshell command to invoke Python interpreter
|
|
1907 py-scroll-process-buffer\talways scroll Python process buffer
|
|
1908 py-temp-directory\tdirectory used for temp files (if needed)
|
|
1909
|
|
1910 py-beep-if-tab-change\tring the bell if tab-width is changed
|
|
1911 %v:py-indent-offset
|
|
1912 %v:py-block-comment-prefix
|
|
1913 %v:py-python-command
|
|
1914 %v:py-scroll-process-buffer
|
|
1915 %v:py-temp-directory
|
|
1916 %v:py-beep-if-tab-change
|
|
1917
|
|
1918 @KINDS OF LINES
|
|
1919
|
|
1920 Each physical line in the file is either a `continuation line' (the
|
|
1921 preceding line ends with a backslash that's not part of a comment, or
|
|
1922 the paren/bracket/brace nesting level at the start of the line is
|
|
1923 non-zero, or both) or an `initial line' (everything else).
|
|
1924
|
|
1925 An initial line is in turn a `blank line' (contains nothing except
|
|
1926 possibly blanks or tabs), a `comment line' (leftmost non-blank
|
|
1927 character is `#'), or a `code line' (everything else).
|
|
1928
|
|
1929 Comment Lines
|
|
1930
|
|
1931 Although all comment lines are treated alike by Python, Python mode
|
|
1932 recognizes two kinds that act differently with respect to indentation.
|
|
1933
|
|
1934 An `indenting comment line' is a comment line with a blank, tab or
|
|
1935 nothing after the initial `#'. The indentation commands (see below)
|
|
1936 treat these exactly as if they were code lines: a line following an
|
|
1937 indenting comment line will be indented like the comment line. All
|
|
1938 other comment lines (those with a non-whitespace character immediately
|
|
1939 following the initial `#') are `non-indenting comment lines', and
|
|
1940 their indentation is ignored by the indentation commands.
|
|
1941
|
|
1942 Indenting comment lines are by far the usual case, and should be used
|
|
1943 whenever possible. Non-indenting comment lines are useful in cases
|
|
1944 like these:
|
|
1945
|
|
1946 \ta = b # a very wordy single-line comment that ends up being
|
|
1947 \t #... continued onto another line
|
|
1948
|
|
1949 \tif a == b:
|
|
1950 ##\t\tprint 'panic!' # old code we've `commented out'
|
|
1951 \t\treturn a
|
|
1952
|
|
1953 Since the `#...' and `##' comment lines have a non-whitespace
|
|
1954 character following the initial `#', Python mode ignores them when
|
|
1955 computing the proper indentation for the next line.
|
|
1956
|
|
1957 Continuation Lines and Statements
|
|
1958
|
|
1959 The Python-mode commands generally work on statements instead of on
|
|
1960 individual lines, where a `statement' is a comment or blank line, or a
|
|
1961 code line and all of its following continuation lines (if any)
|
|
1962 considered as a single logical unit. The commands in this mode
|
|
1963 generally (when it makes sense) automatically move to the start of the
|
|
1964 statement containing point, even if point happens to be in the middle
|
|
1965 of some continuation line.
|
|
1966
|
|
1967
|
|
1968 @INDENTATION
|
|
1969
|
|
1970 Primarily for entering new code:
|
|
1971 \t\\[indent-for-tab-command]\t indent line appropriately
|
|
1972 \t\\[py-newline-and-indent]\t insert newline, then indent
|
|
1973 \t\\[py-delete-char]\t reduce indentation, or delete single character
|
|
1974
|
|
1975 Primarily for reindenting existing code:
|
|
1976 \t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
|
|
1977 \t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
|
|
1978
|
|
1979 \t\\[py-indent-region]\t reindent region to match its context
|
|
1980 \t\\[py-shift-region-left]\t shift region left by py-indent-offset
|
|
1981 \t\\[py-shift-region-right]\t shift region right by py-indent-offset
|
|
1982
|
|
1983 Unlike most programming languages, Python uses indentation, and only
|
|
1984 indentation, to specify block structure. Hence the indentation supplied
|
|
1985 automatically by Python-mode is just an educated guess: only you know
|
|
1986 the block structure you intend, so only you can supply correct
|
|
1987 indentation.
|
|
1988
|
|
1989 The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
|
|
1990 the indentation of preceding statements. E.g., assuming
|
|
1991 py-indent-offset is 4, after you enter
|
|
1992 \tif a > 0: \\[py-newline-and-indent]
|
|
1993 the cursor will be moved to the position of the `_' (_ is not a
|
|
1994 character in the file, it's just used here to indicate the location of
|
|
1995 the cursor):
|
|
1996 \tif a > 0:
|
|
1997 \t _
|
|
1998 If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
|
|
1999 to
|
|
2000 \tif a > 0:
|
|
2001 \t c = d
|
|
2002 \t _
|
|
2003 Python-mode cannot know whether that's what you intended, or whether
|
|
2004 \tif a > 0:
|
|
2005 \t c = d
|
|
2006 \t_
|
|
2007 was your intent. In general, Python-mode either reproduces the
|
|
2008 indentation of the (closest code or indenting-comment) preceding
|
|
2009 statement, or adds an extra py-indent-offset blanks if the preceding
|
|
2010 statement has `:' as its last significant (non-whitespace and non-
|
|
2011 comment) character. If the suggested indentation is too much, use
|
|
2012 \\[py-delete-char] to reduce it.
|
|
2013
|
|
2014 Continuation lines are given extra indentation. If you don't like the
|
|
2015 suggested indentation, change it to something you do like, and Python-
|
|
2016 mode will strive to indent later lines of the statement in the same way.
|
|
2017
|
|
2018 If a line is a continuation line by virtue of being in an unclosed
|
|
2019 paren/bracket/brace structure (`list', for short), the suggested
|
|
2020 indentation depends on whether the current line contains the first item
|
|
2021 in the list. If it does, it's indented py-indent-offset columns beyond
|
|
2022 the indentation of the line containing the open bracket. If you don't
|
|
2023 like that, change it by hand. The remaining items in the list will mimic
|
|
2024 whatever indentation you give to the first item.
|
|
2025
|
|
2026 If a line is a continuation line because the line preceding it ends with
|
|
2027 a backslash, the third and following lines of the statement inherit their
|
|
2028 indentation from the line preceding them. The indentation of the second
|
|
2029 line in the statement depends on the form of the first (base) line: if
|
|
2030 the base line is an assignment statement with anything more interesting
|
|
2031 than the backslash following the leftmost assigning `=', the second line
|
|
2032 is indented two columns beyond that `='. Else it's indented to two
|
|
2033 columns beyond the leftmost solid chunk of non-whitespace characters on
|
|
2034 the base line.
|
|
2035
|
|
2036 Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
|
|
2037 repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
|
|
2038 structure you intend.
|
|
2039 %c:indent-for-tab-command
|
|
2040 %c:py-newline-and-indent
|
|
2041 %c:py-delete-char
|
|
2042
|
|
2043
|
|
2044 The next function may be handy when editing code you didn't write:
|
|
2045 %c:py-guess-indent-offset
|
|
2046
|
|
2047
|
|
2048 The remaining `indent' functions apply to a region of Python code. They
|
|
2049 assume the block structure (equals indentation, in Python) of the region
|
|
2050 is correct, and alter the indentation in various ways while preserving
|
|
2051 the block structure:
|
|
2052 %c:py-indent-region
|
|
2053 %c:py-shift-region-left
|
|
2054 %c:py-shift-region-right
|
|
2055
|
|
2056 @MARKING & MANIPULATING REGIONS OF CODE
|
|
2057
|
|
2058 \\[py-mark-block]\t mark block of lines
|
|
2059 \\[mark-python-def-or-class]\t mark smallest enclosing def
|
|
2060 \\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
|
|
2061 \\[comment-region]\t comment out region of code
|
|
2062 \\[universal-argument] \\[comment-region]\t uncomment region of code
|
|
2063 %c:py-mark-block
|
|
2064 %c:mark-python-def-or-class
|
|
2065 %c:comment-region
|
|
2066
|
|
2067 @MOVING POINT
|
|
2068
|
|
2069 \\[py-previous-statement]\t move to statement preceding point
|
|
2070 \\[py-next-statement]\t move to statement following point
|
|
2071 \\[py-goto-block-up]\t move up to start of current block
|
|
2072 \\[beginning-of-python-def-or-class]\t move to start of def
|
|
2073 \\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
|
|
2074 \\[end-of-python-def-or-class]\t move to end of def
|
|
2075 \\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
|
|
2076
|
|
2077 The first two move to one statement beyond the statement that contains
|
|
2078 point. A numeric prefix argument tells them to move that many
|
|
2079 statements instead. Blank lines, comment lines, and continuation lines
|
|
2080 do not count as `statements' for these commands. So, e.g., you can go
|
|
2081 to the first code statement in a file by entering
|
|
2082 \t\\[beginning-of-buffer]\t to move to the top of the file
|
|
2083 \t\\[py-next-statement]\t to skip over initial comments and blank lines
|
|
2084 Or do `\\[py-previous-statement]' with a huge prefix argument.
|
|
2085 %c:py-previous-statement
|
|
2086 %c:py-next-statement
|
|
2087 %c:py-goto-block-up
|
|
2088 %c:beginning-of-python-def-or-class
|
|
2089 %c:end-of-python-def-or-class
|
|
2090
|
|
2091 @LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
|
|
2092
|
|
2093 `\\[indent-new-comment-line]' is handy for entering a multi-line comment.
|
|
2094
|
|
2095 `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
|
|
2096 overall class and def structure of a module.
|
|
2097
|
|
2098 `\\[back-to-indentation]' moves point to a line's first non-blank character.
|
|
2099
|
|
2100 `\\[indent-relative]' is handy for creating odd indentation.
|
|
2101
|
|
2102 @OTHER EMACS HINTS
|
|
2103
|
|
2104 If you don't like the default value of a variable, change its value to
|
|
2105 whatever you do like by putting a `setq' line in your .emacs file.
|
|
2106 E.g., to set the indentation increment to 4, put this line in your
|
|
2107 .emacs:
|
|
2108 \t(setq py-indent-offset 4)
|
|
2109 To see the value of a variable, do `\\[describe-variable]' and enter the variable
|
|
2110 name at the prompt.
|
|
2111
|
|
2112 When entering a key sequence like `C-c C-n', it is not necessary to
|
|
2113 release the CONTROL key after doing the `C-c' part -- it suffices to
|
|
2114 press the CONTROL key, press and release `c' (while still holding down
|
|
2115 CONTROL), press and release `n' (while still holding down CONTROL), &
|
|
2116 then release CONTROL.
|
|
2117
|
|
2118 Entering Python mode calls with no arguments the value of the variable
|
|
2119 `python-mode-hook', if that value exists and is not nil; for backward
|
|
2120 compatibility it also tries `py-mode-hook'; see the `Hooks' section of
|
|
2121 the Elisp manual for details.
|
|
2122
|
|
2123 Obscure: When python-mode is first loaded, it looks for all bindings
|
|
2124 to newline-and-indent in the global keymap, and shadows them with
|
|
2125 local bindings to py-newline-and-indent."))
|
|
2126
|
|
2127
|
|
2128 ;; Helper functions
|
|
2129 (defvar py-parse-state-re
|
|
2130 (concat
|
|
2131 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
|
|
2132 "\\|"
|
|
2133 "^[^ #\t\n]"))
|
|
2134
|
|
2135 ;; returns the parse state at point (see parse-partial-sexp docs)
|
|
2136 (defun py-parse-state ()
|
|
2137 (save-excursion
|
|
2138 (let ((here (point))
|
|
2139 pps done ci)
|
|
2140 (while (not done)
|
|
2141 ;; back up to the first preceding line (if any; else start of
|
|
2142 ;; buffer) that begins with a popular Python keyword, or a
|
|
2143 ;; non- whitespace and non-comment character. These are good
|
|
2144 ;; places to start parsing to see whether where we started is
|
|
2145 ;; at a non-zero nesting level. It may be slow for people who
|
|
2146 ;; write huge code blocks or huge lists ... tough beans.
|
|
2147 (re-search-backward py-parse-state-re nil 'move)
|
|
2148 (setq ci (current-indentation))
|
|
2149 (beginning-of-line)
|
|
2150 (save-excursion
|
|
2151 (setq pps (parse-partial-sexp (point) here)))
|
|
2152 ;; make sure we don't land inside a triple-quoted string
|
|
2153 (setq done (or (zerop ci)
|
|
2154 (not (nth 3 pps))
|
|
2155 (bobp)))
|
|
2156 )
|
|
2157 pps)))
|
|
2158
|
|
2159 ;; if point is at a non-zero nesting level, returns the number of the
|
|
2160 ;; character that opens the smallest enclosing unclosed list; else
|
|
2161 ;; returns nil.
|
|
2162 (defun py-nesting-level ()
|
|
2163 (let ((status (py-parse-state)) )
|
|
2164 (if (zerop (car status))
|
|
2165 nil ; not in a nest
|
|
2166 (car (cdr status))))) ; char# of open bracket
|
|
2167
|
|
2168 ;; t iff preceding line ends with backslash that's not in a comment
|
|
2169 (defun py-backslash-continuation-line-p ()
|
|
2170 (save-excursion
|
|
2171 (beginning-of-line)
|
|
2172 (and
|
|
2173 ;; use a cheap test first to avoid the regexp if possible
|
|
2174 ;; use 'eq' because char-after may return nil
|
|
2175 (eq (char-after (- (point) 2)) ?\\ )
|
|
2176 ;; make sure; since eq test passed, there is a preceding line
|
|
2177 (forward-line -1) ; always true -- side effect
|
|
2178 (looking-at py-continued-re))))
|
|
2179
|
|
2180 ;; t iff current line is a continuation line
|
|
2181 (defun py-continuation-line-p ()
|
|
2182 (save-excursion
|
|
2183 (beginning-of-line)
|
|
2184 (or (py-backslash-continuation-line-p)
|
|
2185 (py-nesting-level))))
|
|
2186
|
|
2187 ;; go to initial line of current statement; usually this is the line
|
|
2188 ;; we're on, but if we're on the 2nd or following lines of a
|
|
2189 ;; continuation block, we need to go up to the first line of the
|
|
2190 ;; block.
|
|
2191 ;;
|
|
2192 ;; Tricky: We want to avoid quadratic-time behavior for long continued
|
|
2193 ;; blocks, whether of the backslash or open-bracket varieties, or a
|
|
2194 ;; mix of the two. The following manages to do that in the usual
|
|
2195 ;; cases.
|
|
2196 (defun py-goto-initial-line ()
|
|
2197 (let ( open-bracket-pos )
|
|
2198 (while (py-continuation-line-p)
|
|
2199 (beginning-of-line)
|
|
2200 (if (py-backslash-continuation-line-p)
|
|
2201 (while (py-backslash-continuation-line-p)
|
|
2202 (forward-line -1))
|
|
2203 ;; else zip out of nested brackets/braces/parens
|
|
2204 (while (setq open-bracket-pos (py-nesting-level))
|
|
2205 (goto-char open-bracket-pos)))))
|
|
2206 (beginning-of-line))
|
|
2207
|
|
2208 ;; go to point right beyond final line of current statement; usually
|
|
2209 ;; this is the start of the next line, but if this is a multi-line
|
|
2210 ;; statement we need to skip over the continuation lines. Tricky:
|
|
2211 ;; Again we need to be clever to avoid quadratic time behavior.
|
|
2212 (defun py-goto-beyond-final-line ()
|
|
2213 (forward-line 1)
|
|
2214 (let (state)
|
|
2215 (while (and (py-continuation-line-p)
|
|
2216 (not (eobp)))
|
|
2217 ;; skip over the backslash flavor
|
|
2218 (while (and (py-backslash-continuation-line-p)
|
|
2219 (not (eobp)))
|
|
2220 (forward-line 1))
|
|
2221 ;; if in nest, zip to the end of the nest
|
|
2222 (setq state (py-parse-state))
|
|
2223 (if (and (not (zerop (car state)))
|
|
2224 (not (eobp)))
|
|
2225 (progn
|
98
|
2226 (parse-partial-sexp (point) (point-max)
|
|
2227 (if py-parse-partial-sexp-works-p
|
|
2228 0 (- 0 (car state)))
|
2
|
2229 nil state)
|
|
2230 (forward-line 1))))))
|
|
2231
|
|
2232 ;; t iff statement opens a block == iff it ends with a colon that's
|
|
2233 ;; not in a comment. point should be at the start of a statement
|
|
2234 (defun py-statement-opens-block-p ()
|
|
2235 (save-excursion
|
|
2236 (let ((start (point))
|
|
2237 (finish (progn (py-goto-beyond-final-line) (1- (point))))
|
|
2238 (searching t)
|
|
2239 (answer nil)
|
|
2240 state)
|
|
2241 (goto-char start)
|
|
2242 (while searching
|
|
2243 ;; look for a colon with nothing after it except whitespace, and
|
|
2244 ;; maybe a comment
|
|
2245 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
|
|
2246 finish t)
|
|
2247 (if (eq (point) finish) ; note: no `else' clause; just
|
|
2248 ; keep searching if we're not at
|
|
2249 ; the end yet
|
|
2250 ;; sure looks like it opens a block -- but it might
|
|
2251 ;; be in a comment
|
|
2252 (progn
|
|
2253 (setq searching nil) ; search is done either way
|
|
2254 (setq state (parse-partial-sexp start
|
|
2255 (match-beginning 0)))
|
|
2256 (setq answer (not (nth 4 state)))))
|
|
2257 ;; search failed: couldn't find another interesting colon
|
|
2258 (setq searching nil)))
|
|
2259 answer)))
|
|
2260
|
|
2261 (defun py-statement-closes-block-p ()
|
|
2262 ;; true iff the current statement `closes' a block == the line
|
4
|
2263 ;; starts with `return', `raise', `break', `continue', and `pass'.
|
|
2264 ;; doesn't catch embedded statements
|
2
|
2265 (let ((here (point)))
|
|
2266 (back-to-indentation)
|
|
2267 (prog1
|
4
|
2268 (looking-at "\\(return\\|raise\\|break\\|continue\\|pass\\)\\>")
|
2
|
2269 (goto-char here))))
|
|
2270
|
|
2271 ;; go to point right beyond final line of block begun by the current
|
|
2272 ;; line. This is the same as where py-goto-beyond-final-line goes
|
|
2273 ;; unless we're on colon line, in which case we go to the end of the
|
|
2274 ;; block. assumes point is at bolp
|
|
2275 (defun py-goto-beyond-block ()
|
|
2276 (if (py-statement-opens-block-p)
|
|
2277 (py-mark-block nil 'just-move)
|
|
2278 (py-goto-beyond-final-line)))
|
|
2279
|
|
2280 ;; go to start of first statement (not blank or comment or
|
|
2281 ;; continuation line) at or preceding point. returns t if there is
|
|
2282 ;; one, else nil
|
|
2283 (defun py-goto-statement-at-or-above ()
|
|
2284 (py-goto-initial-line)
|
|
2285 (if (looking-at py-blank-or-comment-re)
|
|
2286 ;; skip back over blank & comment lines
|
|
2287 ;; note: will skip a blank or comment line that happens to be
|
|
2288 ;; a continuation line too
|
|
2289 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
|
|
2290 (progn (py-goto-initial-line) t)
|
|
2291 nil)
|
|
2292 t))
|
|
2293
|
|
2294 ;; go to start of first statement (not blank or comment or
|
|
2295 ;; continuation line) following the statement containing point returns
|
|
2296 ;; t if there is one, else nil
|
|
2297 (defun py-goto-statement-below ()
|
|
2298 (beginning-of-line)
|
|
2299 (let ((start (point)))
|
|
2300 (py-goto-beyond-final-line)
|
|
2301 (while (and
|
|
2302 (looking-at py-blank-or-comment-re)
|
|
2303 (not (eobp)))
|
|
2304 (forward-line 1))
|
|
2305 (if (eobp)
|
|
2306 (progn (goto-char start) nil)
|
|
2307 t)))
|
|
2308
|
|
2309 ;; go to start of statement, at or preceding point, starting with
|
|
2310 ;; keyword KEY. Skips blank lines and non-indenting comments upward
|
|
2311 ;; first. If that statement starts with KEY, done, else go back to
|
|
2312 ;; first enclosing block starting with KEY. If successful, leaves
|
|
2313 ;; point at the start of the KEY line & returns t. Else leaves point
|
|
2314 ;; at an undefined place & returns nil.
|
|
2315 (defun py-go-up-tree-to-keyword (key)
|
|
2316 ;; skip blanks and non-indenting #
|
|
2317 (py-goto-initial-line)
|
|
2318 (while (and
|
|
2319 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
|
|
2320 (zerop (forward-line -1))) ; go back
|
|
2321 nil)
|
|
2322 (py-goto-initial-line)
|
|
2323 (let* ((re (concat "[ \t]*" key "\\b"))
|
|
2324 (case-fold-search nil) ; let* so looking-at sees this
|
|
2325 (found (looking-at re))
|
|
2326 (dead nil))
|
|
2327 (while (not (or found dead))
|
|
2328 (condition-case nil ; in case no enclosing block
|
|
2329 (py-goto-block-up 'no-mark)
|
|
2330 (error (setq dead t)))
|
|
2331 (or dead (setq found (looking-at re))))
|
|
2332 (beginning-of-line)
|
|
2333 found))
|
|
2334
|
|
2335 ;; return string in buffer from start of indentation to end of line;
|
|
2336 ;; prefix "..." if leading whitespace was skipped
|
|
2337 (defun py-suck-up-leading-text ()
|
|
2338 (save-excursion
|
|
2339 (back-to-indentation)
|
|
2340 (concat
|
|
2341 (if (bolp) "" "...")
|
|
2342 (buffer-substring (point) (progn (end-of-line) (point))))))
|
|
2343
|
|
2344 ;; assuming point at bolp, return first keyword ([a-z]+) on the line,
|
|
2345 ;; as a Lisp symbol; return nil if none
|
|
2346 (defun py-suck-up-first-keyword ()
|
|
2347 (let ((case-fold-search nil))
|
|
2348 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
|
|
2349 (intern (buffer-substring (match-beginning 1) (match-end 1)))
|
|
2350 nil)))
|
|
2351
|
|
2352 (defun py-make-temp-name ()
|
|
2353 (make-temp-name
|
|
2354 (concat (file-name-as-directory py-temp-directory) "python")))
|
|
2355
|
|
2356 (defun py-delete-file-silently (fname)
|
|
2357 (condition-case nil
|
|
2358 (delete-file fname)
|
|
2359 (error nil)))
|
|
2360
|
|
2361 (defun py-kill-emacs-hook ()
|
|
2362 ;; delete our temp files
|
|
2363 (while py-file-queue
|
|
2364 (py-delete-file-silently (car py-file-queue))
|
|
2365 (setq py-file-queue (cdr py-file-queue)))
|
|
2366 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
|
|
2367 ;; run the hook we inherited, if any
|
|
2368 (and py-inherited-kill-emacs-hook
|
|
2369 (funcall py-inherited-kill-emacs-hook))))
|
|
2370
|
|
2371 ;; make PROCESS's buffer visible, append STRING to it, and force
|
|
2372 ;; display; also make shell-mode believe the user typed this string,
|
|
2373 ;; so that kill-output-from-shell and show-output-from-shell work
|
|
2374 ;; "right"
|
|
2375 (defun py-append-to-process-buffer (process string)
|
|
2376 (let ((cbuf (current-buffer))
|
|
2377 (pbuf (process-buffer process))
|
|
2378 (py-scroll-process-buffer t))
|
|
2379 (set-buffer pbuf)
|
|
2380 (goto-char (point-max))
|
|
2381 (move-marker (process-mark process) (point))
|
|
2382 (if (not (or py-this-is-emacs-19-p
|
|
2383 py-this-is-lucid-emacs-p))
|
|
2384 (move-marker last-input-start (point))) ; muck w/ shell-mode
|
|
2385 (funcall (process-filter process) process string)
|
|
2386 (if (not (or py-this-is-emacs-19-p
|
|
2387 py-this-is-lucid-emacs-p))
|
|
2388 (move-marker last-input-end (point))) ; muck w/ shell-mode
|
|
2389 (set-buffer cbuf))
|
|
2390 (sit-for 0))
|
|
2391
|
98
|
2392 ;; older Emacsen don't have this function
|
|
2393 (if (not (fboundp 'match-string))
|
|
2394 (defun match-string (n)
|
|
2395 (let ((beg (match-beginning n))
|
|
2396 (end (match-end n)))
|
|
2397 (if (and beg end)
|
|
2398 (buffer-substring beg end)
|
|
2399 nil))))
|
|
2400
|
4
|
2401 (defun py-current-defun ()
|
|
2402 ;; tell add-log.el how to find the current function/method/variable
|
|
2403 (save-excursion
|
|
2404 (if (re-search-backward py-defun-start-re nil t)
|
|
2405 (or (match-string 3)
|
|
2406 (let ((method (match-string 2)))
|
|
2407 (if (and (not (zerop (length (match-string 1))))
|
|
2408 (re-search-backward py-class-start-re nil t))
|
|
2409 (concat (match-string 1) "." method)
|
|
2410 method)))
|
|
2411 nil)))
|
2
|
2412
|
|
2413
|
98
|
2414 (defconst py-version "2.89"
|
2
|
2415 "`python-mode' version number.")
|
|
2416 (defconst py-help-address "python-mode@python.org"
|
|
2417 "Address accepting submission of bug reports.")
|
|
2418
|
|
2419 (defun py-version ()
|
|
2420 "Echo the current version of `python-mode' in the minibuffer."
|
|
2421 (interactive)
|
|
2422 (message "Using `python-mode' version %s" py-version)
|
|
2423 (py-keep-region-active))
|
|
2424
|
|
2425 ;; only works under Emacs 19
|
|
2426 ;(eval-when-compile
|
|
2427 ; (require 'reporter))
|
|
2428
|
|
2429 (defun py-submit-bug-report (enhancement-p)
|
|
2430 "Submit via mail a bug report on `python-mode'.
|
|
2431 With \\[universal-argument] just submit an enhancement request."
|
|
2432 (interactive
|
|
2433 (list (not (y-or-n-p
|
|
2434 "Is this a bug report? (hit `n' to send other comments) "))))
|
|
2435 (let ((reporter-prompt-for-summary-p (if enhancement-p
|
|
2436 "(Very) brief summary: "
|
|
2437 t)))
|
|
2438 (require 'reporter)
|
|
2439 (reporter-submit-bug-report
|
|
2440 py-help-address ;address
|
|
2441 (concat "python-mode " py-version) ;pkgname
|
|
2442 ;; varlist
|
|
2443 (if enhancement-p nil
|
|
2444 '(py-python-command
|
|
2445 py-indent-offset
|
|
2446 py-block-comment-prefix
|
|
2447 py-scroll-process-buffer
|
|
2448 py-temp-directory
|
|
2449 py-beep-if-tab-change))
|
|
2450 nil ;pre-hooks
|
|
2451 nil ;post-hooks
|
|
2452 "Dear Barry,") ;salutation
|
|
2453 (if enhancement-p nil
|
|
2454 (set-mark (point))
|
|
2455 (insert
|
|
2456 "Please replace this text with a sufficiently large code sample\n\
|
|
2457 and an exact recipe so that I can reproduce your problem. Failure\n\
|
|
2458 to do so may mean a greater delay in fixing your bug.\n\n")
|
|
2459 (exchange-point-and-mark)
|
|
2460 (py-keep-region-active))))
|
|
2461
|
|
2462
|
|
2463 ;; arrange to kill temp files when Emacs exists
|
|
2464 (if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
|
|
2465 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
|
|
2466 ;; have to trust that other people are as respectful of our hook
|
|
2467 ;; fiddling as we are of theirs
|
|
2468 (if (boundp 'py-inherited-kill-emacs-hook)
|
|
2469 ;; we were loaded before -- trust others not to have screwed us
|
|
2470 ;; in the meantime (no choice, really)
|
|
2471 nil
|
|
2472 ;; else arrange for our hook to run theirs
|
|
2473 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
|
|
2474 (setq kill-emacs-hook 'py-kill-emacs-hook)))
|
|
2475
|
|
2476
|
|
2477
|
|
2478 (provide 'python-mode)
|
|
2479 ;;; python-mode.el ends here
|