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