blob: 42b112df3b6e9fbeae54eaba51b67521ebcb9031 [file] [log] [blame]
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001;;; python-mode.el --- Major mode for editing Python programs
2
3;; Copyright (C) 1992,1993,1994 Tim Peters
4
Barry Warsaw4669d7e1996-03-22 16:13:24 +00005;; Author: 1995-1996 Barry A. Warsaw
Barry Warsawfec75d61995-07-05 23:26:15 +00006;; 1992-1994 Tim Peters
7;; Maintainer: python-mode@python.org
Barry Warsawcfec3591995-03-10 15:58:16 +00008;; Created: Feb 1992
Barry Warsaw7b0f5681995-03-08 21:33:04 +00009;; Version: $Revision$
10;; Last Modified: $Date$
Barry Warsaw4669d7e1996-03-22 16:13:24 +000011;; Keywords: python languages oop
Barry Warsaw7b0f5681995-03-08 21:33:04 +000012
Barry Warsawcfec3591995-03-10 15:58:16 +000013;; 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.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000018
19;;; Commentary:
Barry Warsaw7ae77681994-12-12 20:38:05 +000020;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +000021;; This is a major mode for editing Python programs. It was developed
22;; by Tim Peters <tim@ksr.com> after an original idea by Michael
23;; A. Guravage. Tim doesn't appear to be on the 'net any longer so I
Barry Warsaw4669d7e1996-03-22 16:13:24 +000024;; (Barry) have undertaken maintenance of the mode.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000025
26;; At some point this mode will undergo a rewrite to bring it more in
27;; line with GNU Emacs Lisp coding standards. But all in all, the
Barry Warsaw03970731996-07-03 23:12:52 +000028;; mode works exceedingly well, and I've simply been tweaking it as I
Barry Warsaw4669d7e1996-03-22 16:13:24 +000029;; go along. Ain't it wonderful that Python has a much more sane
Barry Warsaw03970731996-07-03 23:12:52 +000030;; syntax than C? (or <shudder> C++?! :-). I can say that; I maintain
31;; cc-mode!
Barry Warsaw7b0f5681995-03-08 21:33:04 +000032
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):
Barry Warsaw7ae77681994-12-12 20:38:05 +000037;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +000038;; (autoload 'python-mode "python-mode" "Python editing mode." t)
Barry Warsaw7ae77681994-12-12 20:38:05 +000039;; (setq auto-mode-alist
40;; (cons '("\\.py$" . python-mode) auto-mode-alist))
Barry Warsaw44b72201996-07-05 20:11:35 +000041;;
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)
Barry Warsawc08a9491996-07-31 22:27:58 +000046;;
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.
Barry Warsaw7ae77681994-12-12 20:38:05 +000050
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000051;; Here's a brief list of recent additions/improvements:
52;;
53;; - Wrapping and indentation within triple quote strings should work
54;; properly now.
55;; - `Standard' bug reporting mechanism (use C-c C-b)
56;; - py-mark-block was moved to C-c C-m
57;; - C-c C-v shows you the python-mode version
58;; - a basic python-font-lock-keywords has been added for Emacs 19
59;; font-lock colorizations.
60;; - proper interaction with pending-del and del-sel modes.
61;; - New py-electric-colon (:) command for improved outdenting. Also
62;; py-indent-line (TAB) should handle outdented lines better.
Barry Warsaw03970731996-07-03 23:12:52 +000063;; - improved (I think) C-c > and C-c <
Barry Warsaw9e5a9c81996-07-24 18:26:53 +000064;; - py-(forward|backward)-into-nomenclature, not bound, but useful on
65;; M-f and M-b respectively.
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000066
Barry Warsaw7b0f5681995-03-08 21:33:04 +000067;; Here's a brief to do list:
68;;
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000069;; - Better integration with gud-mode for debugging.
70;; - Rewrite according to GNU Emacs Lisp standards.
71;; - py-delete-char should obey numeric arguments.
Barry Warsaw7a1f6f41995-05-08 21:36:20 +000072;; - de-electrify colon inside literals (e.g. comments and strings)
Barry Warsaw5c0d00f1996-07-31 21:30:21 +000073;; - possibly force indent-tabs-mode == nil, and add a
74;; write-file-hooks that runs untabify on the whole buffer (to work
75;; around potential tab/space mismatch problems). In practice this
76;; hasn't been a problem... yet.
Barry Warsaw9e277db1996-07-31 22:33:40 +000077;; - have py-execute-region on indented code act as if the region is
78;; left justified. Avoids syntax errors.
Barry Warsaw7ae77681994-12-12 20:38:05 +000079
Barry Warsaw7b0f5681995-03-08 21:33:04 +000080;; If you can think of more things you'd like to see, drop me a line.
81;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
82;;
Barry Warsaw4669d7e1996-03-22 16:13:24 +000083;; Note that I only test things on XEmacs. If you port stuff to FSF
84;; Emacs 19, or Emacs 18, please send me your patches. Byte compiler
85;; complaints can probably be safely ignored.
Barry Warsaw7ae77681994-12-12 20:38:05 +000086
Barry Warsaw7b0f5681995-03-08 21:33:04 +000087;; LCD Archive Entry:
Barry Warsawfec75d61995-07-05 23:26:15 +000088;; python-mode|Barry A. Warsaw|python-mode@python.org
Barry Warsaw7b0f5681995-03-08 21:33:04 +000089;; |Major mode for editing Python programs
90;; |$Date$|$Revision$|
Barry Warsaw7ae77681994-12-12 20:38:05 +000091
Barry Warsaw7b0f5681995-03-08 21:33:04 +000092;;; Code:
93
94
95;; user definable variables
96;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Barry Warsaw7ae77681994-12-12 20:38:05 +000097
98(defvar py-python-command "python"
99 "*Shell command used to start Python interpreter.")
100
Barry Warsaw17914f41995-11-03 18:25:15 +0000101(defvar py-indent-offset 4
Barry Warsaw7ae77681994-12-12 20:38:05 +0000102 "*Indentation increment.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000103Note that `\\[py-guess-indent-offset]' can usually guess a good value
104when you're editing someone else's Python code.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000105
Barry Warsaw095e9c61995-09-19 20:01:42 +0000106(defvar py-align-multiline-strings-p t
107 "*Flag describing how multiline triple quoted strings are aligned.
108When this flag is non-nil, continuation lines are lined up under the
109preceding line's indentation. When this flag is nil, continuation
110lines are aligned to column zero.")
111
Barry Warsaw7ae77681994-12-12 20:38:05 +0000112(defvar py-block-comment-prefix "##"
Barry Warsaw867a32a1996-03-07 18:30:26 +0000113 "*String used by \\[comment-region] to comment out a block of code.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000114This should follow the convention for non-indenting comment lines so
115that the indentation commands won't get confused (i.e., the string
116should be of the form `#x...' where `x' is not a blank or a tab, and
117`...' is arbitrary).")
118
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000119(defvar py-honor-comment-indentation t
Barry Warsaw6d627751996-03-06 18:41:38 +0000120 "*Controls how comment lines influence subsequent indentation.
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000121
Barry Warsaw6d627751996-03-06 18:41:38 +0000122When nil, all comment lines are skipped for indentation purposes, and
123in Emacs 19, a faster algorithm is used.
124
125When t, lines that begin with a single `#' are a hint to subsequent
126line indentation. If the previous line is such a comment line (as
127opposed to one that starts with `py-block-comment-prefix'), then it's
128indentation is used as a hint for this line's indentation. Lines that
129begin with `py-block-comment-prefix' are ignored for indentation
130purposes.
131
132When not nil or t, comment lines that begin with a `#' are used as
133indentation hints, unless the comment character is in column zero.")
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000134
Barry Warsaw7ae77681994-12-12 20:38:05 +0000135(defvar py-scroll-process-buffer t
136 "*Scroll Python process buffer as output arrives.
137If nil, the Python process buffer acts, with respect to scrolling, like
138Shell-mode buffers normally act. This is surprisingly complicated and
139so won't be explained here; in fact, you can't get the whole story
140without studying the Emacs C code.
141
142If non-nil, the behavior is different in two respects (which are
143slightly inaccurate in the interest of brevity):
144
145 - If the buffer is in a window, and you left point at its end, the
146 window will scroll as new output arrives, and point will move to the
147 buffer's end, even if the window is not the selected window (that
148 being the one the cursor is in). The usual behavior for shell-mode
149 windows is not to scroll, and to leave point where it was, if the
150 buffer is in a window other than the selected window.
151
152 - If the buffer is not visible in any window, and you left point at
153 its end, the buffer will be popped into a window as soon as more
154 output arrives. This is handy if you have a long-running
155 computation and don't want to tie up screen area waiting for the
156 output. The usual behavior for a shell-mode buffer is to stay
157 invisible until you explicitly visit it.
158
159Note the `and if you left point at its end' clauses in both of the
160above: you can `turn off' the special behaviors while output is in
161progress, by visiting the Python buffer and moving point to anywhere
162besides the end. Then the buffer won't scroll, point will remain where
163you leave it, and if you hide the buffer it will stay hidden until you
164visit it again. You can enable and disable the special behaviors as
165often as you like, while output is in progress, by (respectively) moving
166point to, or away from, the end of the buffer.
167
168Warning: If you expect a large amount of output, you'll probably be
169happier setting this option to nil.
170
171Obscure: `End of buffer' above should really say `at or beyond the
172process mark', but if you know what that means you didn't need to be
173told <grin>.")
174
175(defvar py-temp-directory
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000176 (let ((ok '(lambda (x)
177 (and x
178 (setq x (expand-file-name x)) ; always true
179 (file-directory-p x)
180 (file-writable-p x)
181 x))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000182 (or (funcall ok (getenv "TMPDIR"))
183 (funcall ok "/usr/tmp")
184 (funcall ok "/tmp")
185 (funcall ok ".")
186 (error
187 "Couldn't find a usable temp directory -- set py-temp-directory")))
188 "*Directory used for temp files created by a *Python* process.
189By default, the first directory from this list that exists and that you
190can write into: the value (if any) of the environment variable TMPDIR,
191/usr/tmp, /tmp, or the current directory.")
192
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000193(defvar py-beep-if-tab-change t
194 "*Ring the bell if tab-width is changed.
195If a comment of the form
196
197 \t# vi:set tabsize=<number>:
198
199is found before the first code line when the file is entered, and the
200current value of (the general Emacs variable) `tab-width' does not
201equal <number>, `tab-width' is set to <number>, a message saying so is
202displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
203the Emacs bell is also rung as a warning.")
204
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000205(defconst python-font-lock-keywords
Barry Warsaw44b72201996-07-05 20:11:35 +0000206 (let* ((keywords '("access" "and" "break" "class"
207 "continue" "def" "del" "elif"
208 "else:" "except" "except:" "exec"
209 "finally:" "for" "from" "global"
210 "if" "import" "in" "is"
211 "lambda" "not" "or" "pass"
212 "print" "raise" "return" "try:"
213 "while"
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000214 ))
215 (kwregex (mapconcat 'identity keywords "\\|")))
216 (list
217 ;; keywords not at beginning of line
218 (cons (concat "\\s-\\(" kwregex "\\)[ \n\t(]") 1)
219 ;; keywords at beginning of line. i don't think regexps are
220 ;; powerful enough to handle these two cases in one regexp.
221 ;; prove me wrong!
222 (cons (concat "^\\(" kwregex "\\)[ \n\t(]") 1)
223 ;; classes
224 '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
225 1 font-lock-type-face)
226 ;; functions
227 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
228 1 font-lock-function-name-face)
229 ))
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000230 "Additional expressions to highlight in Python mode.")
Barry Warsawb01b4fa1995-06-20 18:55:34 +0000231
Barry Warsaw81437461996-08-01 19:48:02 +0000232(defvar imenu-example--python-show-method-args-p nil
233 "*Controls echoing of arguments of functions & methods in the imenu buffer.
234When non-nil, arguments are printed.")
235
236
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000237
238;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
239;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
240
Barry Warsaw52bc17c1995-10-12 21:15:49 +0000241(make-variable-buffer-local 'py-indent-offset)
242
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000243;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
244;; seems to be the standard way of checking this.
245;; BAW - This is *not* the right solution. When at all possible,
246;; instead of testing for the version of Emacs, use feature tests.
247
248(setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
249(setq py-this-is-emacs-19-p
250 (and
251 (not py-this-is-lucid-emacs-p)
252 (string-match "^19\\." emacs-version)))
253
Barry Warsaw7ae77681994-12-12 20:38:05 +0000254;; have to bind py-file-queue before installing the kill-emacs hook
255(defvar py-file-queue nil
256 "Queue of Python temp files awaiting execution.
257Currently-active file is at the head of the list.")
258
259;; define a mode-specific abbrev table for those who use such things
260(defvar python-mode-abbrev-table nil
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000261 "Abbrev table in use in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000262(define-abbrev-table 'python-mode-abbrev-table nil)
263
Barry Warsaw7ae77681994-12-12 20:38:05 +0000264(defvar python-mode-hook nil
265 "*Hook called by `python-mode'.")
266
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000267;; in previous version of python-mode.el, the hook was incorrectly
268;; called py-mode-hook, and was not defvar'd. deprecate its use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000269(and (fboundp 'make-obsolete-variable)
270 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
271
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000272(defvar py-mode-map ()
273 "Keymap used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000274
Barry Warsaw7ae77681994-12-12 20:38:05 +0000275(if py-mode-map
276 ()
277 (setq py-mode-map (make-sparse-keymap))
278
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000279 ;; shadow global bindings for newline-and-indent w/ the py- version.
280 ;; BAW - this is extremely bad form, but I'm not going to change it
281 ;; for now.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000282 (mapcar (function (lambda (key)
283 (define-key
284 py-mode-map key 'py-newline-and-indent)))
285 (where-is-internal 'newline-and-indent))
286
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000287 ;; BAW - you could do it this way, but its not considered proper
288 ;; major-mode form.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000289 (mapcar (function
290 (lambda (x)
291 (define-key py-mode-map (car x) (cdr x))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000292 '((":" . py-electric-colon)
293 ("\C-c\C-c" . py-execute-buffer)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000294 ("\C-c|" . py-execute-region)
295 ("\C-c!" . py-shell)
296 ("\177" . py-delete-char)
297 ("\n" . py-newline-and-indent)
298 ("\C-c:" . py-guess-indent-offset)
299 ("\C-c\t" . py-indent-region)
Barry Warsawdea4a291996-07-03 22:59:12 +0000300 ("\C-c\C-l" . py-shift-region-left)
301 ("\C-c\C-r" . py-shift-region-right)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000302 ("\C-c<" . py-shift-region-left)
303 ("\C-c>" . py-shift-region-right)
304 ("\C-c\C-n" . py-next-statement)
305 ("\C-c\C-p" . py-previous-statement)
306 ("\C-c\C-u" . py-goto-block-up)
Barry Warsaw850437a1995-03-08 21:50:28 +0000307 ("\C-c\C-m" . py-mark-block)
Barry Warsawa7891711996-08-01 15:53:09 +0000308 ("\C-c#" . py-comment-region)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000309 ("\C-c?" . py-describe-mode)
310 ("\C-c\C-hm" . py-describe-mode)
311 ("\e\C-a" . beginning-of-python-def-or-class)
312 ("\e\C-e" . end-of-python-def-or-class)
Barry Warsaw850437a1995-03-08 21:50:28 +0000313 ( "\e\C-h" . mark-python-def-or-class)))
314 ;; should do all keybindings this way
315 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
316 (define-key py-mode-map "\C-c\C-v" 'py-version)
317 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000318
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000319(defvar py-mode-syntax-table nil
320 "Syntax table used in `python-mode' buffers.")
321
Barry Warsaw7ae77681994-12-12 20:38:05 +0000322(if py-mode-syntax-table
323 ()
324 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000325 ;; BAW - again, blech.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000326 (mapcar (function
327 (lambda (x) (modify-syntax-entry
328 (car x) (cdr x) py-mode-syntax-table)))
329 '(( ?\( . "()" ) ( ?\) . ")(" )
330 ( ?\[ . "(]" ) ( ?\] . ")[" )
331 ( ?\{ . "(}" ) ( ?\} . "){" )
332 ;; fix operator symbols misassigned in the std table
333 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
334 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
335 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
336 ( ?\> . "." ) ( ?\| . "." )
Barry Warsawfb349421996-07-24 18:32:08 +0000337 ;; for historical reasons, underscore is word class
338 ;; instead of symbol class. it should be symbol class,
339 ;; but if you're tempted to change it, try binding M-f and
340 ;; M-b to py-forward-into-nomenclature and
341 ;; py-backward-into-nomenclature instead. -baw
Barry Warsaw8e9d7d71996-07-03 23:15:51 +0000342 ( ?\_ . "w" ) ; underscore is legit in words
Barry Warsaw7ae77681994-12-12 20:38:05 +0000343 ( ?\' . "\"") ; single quote is string quote
344 ( ?\" . "\"" ) ; double quote is string quote too
345 ( ?\` . "$") ; backquote is open and close paren
346 ( ?\# . "<") ; hash starts comment
347 ( ?\n . ">")))) ; newline ends comment
348
349(defconst py-stringlit-re
350 (concat
351 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
352 "\\|" ; or
353 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000354 "Regexp matching a Python string literal.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000355
356;; this is tricky because a trailing backslash does not mean
357;; continuation if it's in a comment
358(defconst py-continued-re
359 (concat
360 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
361 "\\\\$")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000362 "Regexp matching Python lines that are continued via backslash.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000363
364(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000365 "Regexp matching blank or comment lines.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000366
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000367(defconst py-outdent-re
368 (concat "\\(" (mapconcat 'identity
369 '("else:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000370 "except\\(\\s +.*\\)?:"
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000371 "finally:"
372 "elif\\s +.*:")
373 "\\|")
374 "\\)")
375 "Regexp matching clauses to be outdented one level.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000376
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000377(defconst py-no-outdent-re
378 (concat "\\(" (mapconcat 'identity
Barry Warsaw464c94a1995-03-14 23:25:44 +0000379 '("try:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000380 "except\\(\\s +.*\\)?:"
381 "while\\s +.*:"
382 "for\\s +.*:"
383 "if\\s +.*:"
384 "elif\\s +.*:")
385 "\\|")
386 "\\)")
387 "Regexp matching lines to not outdent after.")
388
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000389
Barry Warsaw42f707f1996-07-29 21:05:05 +0000390;; Menu definitions, only relevent if you have the easymenu.el package
391;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
392(if (condition-case nil
393 (require 'easymenu)
394 (error nil))
395 (easy-menu-define
396 py-menu py-mode-map "Python Mode menu"
397 '("Python"
398 ["Comment Out Region" comment-region (mark)]
399 ["Uncomment Region" (comment-region (point) (mark) '(4)) (mark)]
400 "-"
401 ["Mark current block" py-mark-block t]
402 ["Mark current def" mark-python-def-or-class t]
403 ["Mark current class" (mark-python-def-or-class t) t]
404 "-"
405 ["Shift region left" py-shift-region-left (mark)]
406 ["Shift region right" py-shift-region-right (mark)]
407 "-"
408 ["Execute buffer" py-execute-buffer t]
409 ["Execute region" py-execute-region (mark)]
410 ["Start interpreter..." py-shell t]
411 "-"
412 ["Go to start of block" py-goto-block-up t]
413 ["Go to start of class" (beginning-of-python-def-or-class t) t]
414 ["Move to end of class" (end-of-python-def-or-class t) t]
415 ["Move to start of def" beginning-of-python-def-or-class t]
416 ["Move to end of def" end-of-python-def-or-class t]
417 "-"
418 ["Describe mode" py-describe-mode t]
419 )))
420
Barry Warsaw81437461996-08-01 19:48:02 +0000421
422
423;; imenu definitions, courtesy of Perry A. Stoll <stoll@atr-sw.atr.co.jp>
424(defvar imenu-example--python-class-regexp
425 (concat ; <<classes>>
426 "\\(" ;
427 "^[ \t]*" ; newline and maybe whitespace
428 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
429 ; possibly multiple superclasses
430 "\\([ \t]*\\((\\([a-zA-Z0-9_, \t\n]\\)*)\\)?\\)"
431 "[ \t]*:" ; and the final :
432 "\\)" ; >>classes<<
433 )
434 "Regexp for Python classes for use with the imenu package."
435 )
436
437(defvar imenu-example--python-method-regexp
438 (concat ; <<methods and functions>>
439 "\\(" ;
440 "^[ \t]*" ; new line and maybe whitespace
441 "\\(def[ \t]+" ; function definitions start with def
442 "\\([a-zA-Z0-9_]+\\)" ; name is here
443 ; function arguments...
444 "[ \t]*(\\([a-zA-Z0-9_=,\* \t\n]*\\))"
445 "\\)" ; end of def
446 "[ \t]*:" ; and then the :
447 "\\)" ; >>methods and functions<<
448 )
449 "Regexp for Python methods/functions for use with the imenu package."
450 )
451
452(defvar imenu-example--python-method-no-arg-parens '(2 8)
453 "Indicies into groups of the Python regexp for use with imenu.
454
455Using these values will result in smaller imenu lists, as arguments to
456functions are not listed.
457
458See the variable `imenu-example--python-show-method-args-p' for more
459information.")
460
461(defvar imenu-example--python-method-arg-parens '(2 7)
462 "Indicies into groups of the Python regexp for use with imenu.
463Using these values will result in large imenu lists, as arguments to
464functions are listed.
465
466See the variable `imenu-example--python-show-method-args-p' for more
467information.")
468
469;; Note that in this format, this variable can still be used with the
470;; imenu--generic-function. Otherwise, there is no real reason to have
471;; it.
472(defvar imenu-example--generic-python-expression
473 (cons
474 (concat
475 imenu-example--python-class-regexp
476 "\\|" ; or...
477 imenu-example--python-method-regexp
478 )
479 imenu-example--python-method-no-arg-parens)
480 "Generic Python expression which may be used directly with imenu.
481Used by setting the variable `imenu-generic-expression' to this value.
482Also, see the function \\[imenu-example--create-python-index] for a
483better alternative for finding the index.")
484
485;; These next two variables are used when searching for the python
486;; class/definitions. Just saving some time in accessing the
487;; generic-python-expression, really.
488(defvar imenu-example--python-generic-regexp)
489(defvar imenu-example--python-generic-parens)
490
491
492;;;###autoload
493(eval-when-compile
494 ;; Imenu isn't used in XEmacs, so just ignore load errors
495 (condition-case ()
496 (progn
497 (require 'cl)
498 (require 'imenu))
499 (error nil)))
500
501(defun imenu-example--create-python-index ()
502 "Python interface function for imenu package.
503Finds all python classes and functions/methods. Calls function
504\\[imenu-example--create-python-index-engine]. See that function for
505the details of how this works."
506 (setq imenu-example--python-generic-regexp
507 (car imenu-example--generic-python-expression))
508 (setq imenu-example--python-generic-parens
509 (if imenu-example--python-show-method-args-p
510 imenu-example--python-method-arg-parens
511 imenu-example--python-method-no-arg-parens))
512 (goto-char (point-min))
513 (imenu-example--create-python-index-engine nil))
514
515(defun imenu-example--create-python-index-engine (&optional start-indent)
516 "Function for finding imenu definitions in Python.
517
518Finds all definitions (classes, methods, or functions) in a Python
519file for the imenu package.
520
521Returns a possibly nested alist of the form
522
523 (INDEX-NAME . INDEX-POSITION)
524
525The second element of the alist may be an alist, producing a nested
526list as in
527
528 (INDEX-NAME . INDEX-ALIST)
529
530This function should not be called directly, as it calls itself
531recursively and requires some setup. Rather this is the engine for
532the function \\[imenu-example--create-python-index].
533
534It works recursively by looking for all definitions at the current
535indention level. When it finds one, it adds it to the alist. If it
536finds a definition at a greater indentation level, it removes the
537previous definition from the alist. In it's place it adds all
538definitions found at the next indentation level. When it finds a
539definition that is less indented then the current level, it retuns the
540alist it has created thus far.
541
542The optional argument START-INDENT indicates the starting indentation
543at which to continue looking for Python classes, methods, or
544functions. If this is not supplied, the function uses the indentation
545of the first definition found."
546 (let ((index-alist '())
547 (sub-method-alist '())
548 looking-p
549 def-name prev-name
550 cur-indent def-pos
551 (class-paren (first imenu-example--python-generic-parens))
552 (def-paren (second imenu-example--python-generic-parens)))
553 (setq looking-p
554 (re-search-forward imenu-example--python-generic-regexp
555 (point-max) t))
556 (while looking-p
557 (save-excursion
558 ;; used to set def-name to this value but generic-extract-name is
559 ;; new to imenu-1.14. this way it still works with imenu-1.11
560 ;;(imenu--generic-extract-name imenu-example--python-generic-parens))
561 (let ((cur-paren (if (match-beginning class-paren)
562 class-paren def-paren)))
563 (setq def-name
564 (buffer-substring (match-beginning cur-paren)
565 (match-end cur-paren))))
566 (beginning-of-line)
567 (setq cur-indent (current-indentation)))
568
569 ;; HACK: want to go to the next correct definition location. we
570 ;; explicitly list them here. would be better to have them in a
571 ;; list.
572 (setq def-pos
573 (or (match-beginning class-paren)
574 (match-beginning def-paren)))
575
576 ;; if we don't have a starting indent level, take this one
577 (or start-indent
578 (setq start-indent cur-indent))
579
580 ;; if we don't have class name yet, take this one
581 (or prev-name
582 (setq prev-name def-name))
583
584 ;; what level is the next definition on? must be same, deeper
585 ;; or shallower indentation
586 (cond
587 ;; at the same indent level, add it to the list...
588 ((= start-indent cur-indent)
589
590 ;; if we don't have push, use the following...
591 ;;(setf index-alist (cons (cons def-name def-pos) index-alist))
592 (push (cons def-name def-pos) index-alist))
593
594 ;; deeper indented expression, recur...
595 ((< start-indent cur-indent)
596
597 ;; the point is currently on the expression we're supposed to
598 ;; start on, so go back to the last expression. The recursive
599 ;; call will find this place again and add it to the correct
600 ;; list
601 (re-search-backward imenu-example--python-generic-regexp
602 (point-min) 'move)
603 (setq sub-method-alist (imenu-example--create-python-index-engine
604 cur-indent))
605
606 (if sub-method-alist
607 ;; we put the last element on the index-alist on the start
608 ;; of the submethod alist so the user can still get to it.
609 (let ((save-elmt (pop index-alist)))
610 (push (cons (imenu-create-submenu-name prev-name)
611 (cons save-elmt sub-method-alist))
612 index-alist))))
613
614 ;; found less indented expression, we're done.
615 (t
616 (setq looking-p nil)
617 (re-search-backward imenu-example--python-generic-regexp
618 (point-min) t)))
619 (setq prev-name def-name)
620 (and looking-p
621 (setq looking-p
622 (re-search-forward imenu-example--python-generic-regexp
623 (point-max) 'move))))
624 (nreverse index-alist)))
625
Barry Warsaw42f707f1996-07-29 21:05:05 +0000626
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000627;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +0000628(defun python-mode ()
629 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000630To submit a problem report, enter `\\[py-submit-bug-report]' from a
631`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
632documentation. To see what version of `python-mode' you are running,
633enter `\\[py-version]'.
634
635This mode knows about Python indentation, tokens, comments and
636continuation lines. Paragraphs are separated by blank lines only.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000637
638COMMANDS
639\\{py-mode-map}
640VARIABLES
641
Barry Warsaw42f707f1996-07-29 21:05:05 +0000642py-indent-offset\t\tindentation increment
643py-block-comment-prefix\t\tcomment string used by comment-region
644py-python-command\t\tshell command to invoke Python interpreter
645py-scroll-process-buffer\t\talways scroll Python process buffer
646py-temp-directory\t\tdirectory used for temp files (if needed)
647py-beep-if-tab-change\t\tring the bell if tab-width is changed"
Barry Warsaw7ae77681994-12-12 20:38:05 +0000648 (interactive)
649 (kill-all-local-variables)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000650 (set-syntax-table py-mode-syntax-table)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000651 (setq major-mode 'python-mode
652 mode-name "Python"
653 local-abbrev-table python-mode-abbrev-table)
654 (use-local-map py-mode-map)
Barry Warsaw42f707f1996-07-29 21:05:05 +0000655 ;; add the menu
656 (if py-menu
657 (easy-menu-add py-menu))
Barry Warsaw57697af1995-09-14 20:01:14 +0000658 ;; Emacs 19 requires this
659 (if (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p)
660 (setq comment-multi-line nil))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000661 ;; BAW -- style...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000662 (mapcar (function (lambda (x)
663 (make-local-variable (car x))
664 (set (car x) (cdr x))))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000665 '((paragraph-separate . "^[ \t]*$")
666 (paragraph-start . "^[ \t]*$")
667 (require-final-newline . t)
Barry Warsawa7891711996-08-01 15:53:09 +0000668 (comment-start . "# ")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000669 (comment-start-skip . "# *")
670 (comment-column . 40)
671 (indent-region-function . py-indent-region)
672 (indent-line-function . py-indent-line)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000673 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000674 ;;
675 ;; not sure where the magic comment has to be; to save time
676 ;; searching for a rarity, we give up if it's not found prior to the
677 ;; first executable statement.
678 ;;
679 ;; BAW - on first glance, this seems like complete hackery. Why was
680 ;; this necessary, and is it still necessary?
681 (let ((case-fold-search nil)
682 (start (point))
683 new-tab-width)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000684 (if (re-search-forward
685 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
686 (prog2 (py-next-statement 1) (point) (goto-char 1))
687 t)
688 (progn
689 (setq new-tab-width
690 (string-to-int
691 (buffer-substring (match-beginning 1) (match-end 1))))
692 (if (= tab-width new-tab-width)
693 nil
694 (setq tab-width new-tab-width)
695 (message "Caution: tab-width changed to %d" new-tab-width)
696 (if py-beep-if-tab-change (beep)))))
697 (goto-char start))
698
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000699 ;; run the mode hook. py-mode-hook use is deprecated
Barry Warsaw7ae77681994-12-12 20:38:05 +0000700 (if python-mode-hook
701 (run-hooks 'python-mode-hook)
702 (run-hooks 'py-mode-hook)))
703
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000704
Barry Warsaw826255b1996-03-22 16:09:34 +0000705(defun py-keep-region-active ()
Barry Warsawce60bc71996-08-01 18:17:14 +0000706 ;; do whatever is necessary to keep the region active in XEmacs.
707 ;; Ignore byte-compiler warnings you might see. Also note that
708 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
709 ;; require us to take explicit action.
Barry Warsaw826255b1996-03-22 16:09:34 +0000710 (and (boundp 'zmacs-region-stays)
711 (setq zmacs-region-stays t)))
712
Barry Warsawce60bc71996-08-01 18:17:14 +0000713
Barry Warsawb91b7431995-03-14 15:55:20 +0000714;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000715(defun py-outdent-p ()
716 ;; returns non-nil if the current line should outdent one level
717 (save-excursion
718 (and (progn (back-to-indentation)
719 (looking-at py-outdent-re))
720 (progn (backward-to-indentation 1)
721 (while (or (looking-at py-blank-or-comment-re)
722 (bobp))
723 (backward-to-indentation 1))
724 (not (looking-at py-no-outdent-re)))
725 )))
726
727
Barry Warsawb91b7431995-03-14 15:55:20 +0000728(defun py-electric-colon (arg)
729 "Insert a colon.
730In certain cases the line is outdented appropriately. If a numeric
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000731argument is provided, that many colons are inserted non-electrically.
732Electric behavior is inhibited inside a string or comment."
Barry Warsawb91b7431995-03-14 15:55:20 +0000733 (interactive "P")
734 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000735 ;; are we in a string or comment?
736 (if (save-excursion
737 (let ((pps (parse-partial-sexp (save-excursion
738 (beginning-of-python-def-or-class)
739 (point))
740 (point))))
741 (not (or (nth 3 pps) (nth 4 pps)))))
742 (save-excursion
743 (let ((here (point))
744 (outdent 0)
745 (indent (py-compute-indentation)))
746 (if (and (not arg)
747 (py-outdent-p)
748 (= indent (save-excursion
749 (forward-line -1)
750 (py-compute-indentation)))
751 )
752 (setq outdent py-indent-offset))
753 ;; Don't indent, only outdent. This assumes that any lines that
754 ;; are already outdented relative to py-compute-indentation were
755 ;; put there on purpose. Its highly annoying to have `:' indent
756 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
757 ;; there a better way to determine this???
758 (if (< (current-indentation) indent) nil
759 (goto-char here)
760 (beginning-of-line)
761 (delete-horizontal-space)
762 (indent-to (- indent outdent))
763 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000764
765
Barry Warsaw7ae77681994-12-12 20:38:05 +0000766;;; Functions that execute Python commands in a subprocess
Barry Warsaw7ae77681994-12-12 20:38:05 +0000767(defun py-shell ()
768 "Start an interactive Python interpreter in another window.
769This is like Shell mode, except that Python is running in the window
770instead of a shell. See the `Interactive Shell' and `Shell Mode'
771sections of the Emacs manual for details, especially for the key
772bindings active in the `*Python*' buffer.
773
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000774See the docs for variable `py-scroll-buffer' for info on scrolling
Barry Warsaw7ae77681994-12-12 20:38:05 +0000775behavior in the process window.
776
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000777Warning: Don't use an interactive Python if you change sys.ps1 or
778sys.ps2 from their default values, or if you're running code that
779prints `>>> ' or `... ' at the start of a line. `python-mode' can't
780distinguish your output from Python's output, and assumes that `>>> '
781at the start of a line is a prompt from Python. Similarly, the Emacs
782Shell mode code assumes that both `>>> ' and `... ' at the start of a
783line are Python prompts. Bad things can happen if you fool either
784mode.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000785
786Warning: If you do any editing *in* the process buffer *while* the
787buffer is accepting output from Python, do NOT attempt to `undo' the
788changes. Some of the output (nowhere near the parts you changed!) may
789be lost if you do. This appears to be an Emacs bug, an unfortunate
790interaction between undo and process filters; the same problem exists in
791non-Python process buffers using the default (Emacs-supplied) process
792filter."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000793 ;; BAW - should undo be disabled in the python process buffer, if
794 ;; this bug still exists?
Barry Warsaw7ae77681994-12-12 20:38:05 +0000795 (interactive)
796 (if py-this-is-emacs-19-p
797 (progn
798 (require 'comint)
799 (switch-to-buffer-other-window
800 (make-comint "Python" py-python-command)))
801 (progn
802 (require 'shell)
803 (switch-to-buffer-other-window
Barry Warsaw9fbcc6a1996-01-23 22:52:02 +0000804 (apply (if (fboundp 'make-shell) 'make-shell 'make-comint)
Barry Warsaw6e98f331995-07-05 22:06:50 +0000805 "Python" py-python-command nil))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000806 (make-local-variable 'shell-prompt-pattern)
807 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
808 (set-process-filter (get-buffer-process (current-buffer))
809 'py-process-filter)
810 (set-syntax-table py-mode-syntax-table))
811
812(defun py-execute-region (start end)
813 "Send the region between START and END to a Python interpreter.
814If there is a *Python* process it is used.
815
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000816Hint: If you want to execute part of a Python file several times
817\(e.g., perhaps you're developing a function and want to flesh it out
818a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
819the region of interest, and send the code to a *Python* process via
820`\\[py-execute-buffer]' instead.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000821
822Following are subtleties to note when using a *Python* process:
823
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000824If a *Python* process is used, the region is copied into a temporary
825file (in directory `py-temp-directory'), and an `execfile' command is
826sent to Python naming that file. If you send regions faster than
827Python can execute them, `python-mode' will save them into distinct
828temp files, and execute the next one in the queue the next time it
829sees a `>>> ' prompt from Python. Each time this happens, the process
830buffer is popped into a window (if it's not already in some window) so
831you can see it, and a comment of the form
Barry Warsaw7ae77681994-12-12 20:38:05 +0000832
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000833 \t## working on region in file <name> ...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000834
835is inserted at the end.
836
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000837Caution: No more than 26 regions can be pending at any given time.
838This limit is (indirectly) inherited from libc's mktemp(3).
839`python-mode' does not try to protect you from exceeding the limit.
840It's extremely unlikely that you'll get anywhere close to the limit in
841practice, unless you're trying to be a jerk <grin>.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000842
843See the `\\[py-shell]' docs for additional warnings."
844 (interactive "r")
845 (or (< start end) (error "Region is empty"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000846 (let ((pyproc (get-process "Python"))
847 fname)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000848 (if (null pyproc)
849 (shell-command-on-region start end py-python-command)
850 ;; else feed it thru a temp file
851 (setq fname (py-make-temp-name))
852 (write-region start end fname nil 'no-msg)
853 (setq py-file-queue (append py-file-queue (list fname)))
854 (if (cdr py-file-queue)
855 (message "File %s queued for execution" fname)
856 ;; else
857 (py-execute-file pyproc fname)))))
858
859(defun py-execute-file (pyproc fname)
860 (py-append-to-process-buffer
861 pyproc
862 (format "## working on region in file %s ...\n" fname))
863 (process-send-string pyproc (format "execfile('%s')\n" fname)))
864
865(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000866 (let ((curbuf (current-buffer))
867 (pbuf (process-buffer pyproc))
868 (pmark (process-mark pyproc))
869 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000870
871 ;; make sure we switch to a different buffer at least once. if we
872 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000873 ;; window, and point is before the end, and lots of output is
874 ;; coming at a fast pace, then (a) simple cursor-movement commands
875 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
876 ;; to have a visible effect (the window just doesn't get updated,
877 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
878 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000879 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000880 ;; #b makes no sense to me at all. #a almost makes sense: unless
881 ;; we actually change buffers, set_buffer_internal in buffer.c
882 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
883 ;; seems to make the Emacs command loop reluctant to update the
884 ;; display. Perhaps the default process filter in process.c's
885 ;; read_process_output has update_mode_lines++ for a similar
886 ;; reason? beats me ...
887
888 ;; BAW - we want to check to see if this still applies
Barry Warsaw7ae77681994-12-12 20:38:05 +0000889 (if (eq curbuf pbuf) ; mysterious ugly hack
890 (set-buffer (get-buffer-create "*scratch*")))
891
892 (set-buffer pbuf)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000893 (let* ((start (point))
894 (goback (< start pmark))
Barry Warsawe64bfee1995-07-05 22:27:23 +0000895 (goend (and (not goback) (= start (point-max))))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000896 (buffer-read-only nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000897 (goto-char pmark)
898 (insert string)
899 (move-marker pmark (point))
900 (setq file-finished
901 (and py-file-queue
902 (equal ">>> "
903 (buffer-substring
904 (prog2 (beginning-of-line) (point)
905 (goto-char pmark))
906 (point)))))
907 (if goback (goto-char start)
908 ;; else
909 (if py-scroll-process-buffer
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000910 (let* ((pop-up-windows t)
911 (pwin (display-buffer pbuf)))
Barry Warsawe64bfee1995-07-05 22:27:23 +0000912 (set-window-point pwin (point)))))
913 (set-buffer curbuf)
914 (if file-finished
915 (progn
916 (py-delete-file-silently (car py-file-queue))
917 (setq py-file-queue (cdr py-file-queue))
918 (if py-file-queue
919 (py-execute-file pyproc (car py-file-queue)))))
920 (and goend
921 (progn (set-buffer pbuf)
922 (goto-char (point-max))))
923 )))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000924
925(defun py-execute-buffer ()
926 "Send the contents of the buffer to a Python interpreter.
927If there is a *Python* process buffer it is used. If a clipping
928restriction is in effect, only the accessible portion of the buffer is
929sent. A trailing newline will be supplied if needed.
930
931See the `\\[py-execute-region]' docs for an account of some subtleties."
932 (interactive)
933 (py-execute-region (point-min) (point-max)))
934
935
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000936
937;; Functions for Python style indentation
Barry Warsaw03970731996-07-03 23:12:52 +0000938(defun py-delete-char (count)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000939 "Reduce indentation or delete character.
940If point is at the leftmost column, deletes the preceding newline.
941
942Else if point is at the leftmost non-blank character of a line that is
943neither a continuation line nor a non-indenting comment line, or if
944point is at the end of a blank line, reduces the indentation to match
945that of the line that opened the current block of code. The line that
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000946opened the block is displayed in the echo area to help you keep track
Barry Warsaw03970731996-07-03 23:12:52 +0000947of where you are. With numeric count, outdents that many blocks (but
948not past column zero).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000949
950Else the preceding character is deleted, converting a tab to spaces if
Barry Warsaw03970731996-07-03 23:12:52 +0000951needed so that only a single column position is deleted. Numeric
952argument delets that many characters."
953 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000954 (if (or (/= (current-indentation) (current-column))
955 (bolp)
956 (py-continuation-line-p)
Barry Warsaw6e527d21996-08-01 15:57:48 +0000957 (not py-honor-comment-indentation)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000958 (looking-at "#[^ \t\n]")) ; non-indenting #
Barry Warsaw03970731996-07-03 23:12:52 +0000959 (backward-delete-char-untabify count)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000960 ;; else indent the same as the colon line that opened the block
961
962 ;; force non-blank so py-goto-block-up doesn't ignore it
963 (insert-char ?* 1)
964 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000965 (let ((base-indent 0) ; indentation of base line
966 (base-text "") ; and text of base line
967 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +0000968 (save-excursion
969 (while (< 0 count)
970 (condition-case nil ; in case no enclosing block
971 (progn
972 (py-goto-block-up 'no-mark)
973 (setq base-indent (current-indentation)
974 base-text (py-suck-up-leading-text)
975 base-found-p t))
976 (error nil))
977 (setq count (1- count))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000978 (delete-char 1) ; toss the dummy character
979 (delete-horizontal-space)
980 (indent-to base-indent)
981 (if base-found-p
982 (message "Closes block: %s" base-text)))))
983
Barry Warsawfc8a01f1995-03-09 16:07:29 +0000984;; required for pending-del and delsel modes
985(put 'py-delete-char 'delete-selection 'supersede)
986(put 'py-delete-char 'pending-delete 'supersede)
987
Barry Warsaw7ae77681994-12-12 20:38:05 +0000988(defun py-indent-line ()
989 "Fix the indentation of the current line according to Python rules."
990 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000991 (let* ((ci (current-indentation))
992 (move-to-indentation-p (<= (current-column) ci))
Barry Warsawb86bbad1995-03-14 15:56:35 +0000993 (need (py-compute-indentation)))
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000994 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000995 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000996 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000997 (if (/= ci need)
998 (save-excursion
999 (beginning-of-line)
1000 (delete-horizontal-space)
1001 (indent-to need)))
1002 (if move-to-indentation-p (back-to-indentation))))
1003
1004(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001005 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001006This is just `strives to' because correct indentation can't be computed
1007from scratch for Python code. In general, deletes the whitespace before
1008point, inserts a newline, and takes an educated guess as to how you want
1009the new line indented."
1010 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001011 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001012 (if (< ci (current-column)) ; if point beyond indentation
1013 (newline-and-indent)
1014 ;; else try to act like newline-and-indent "normally" acts
1015 (beginning-of-line)
1016 (insert-char ?\n 1)
1017 (move-to-column ci))))
1018
1019(defun py-compute-indentation ()
1020 (save-excursion
Barry Warsaw095e9c61995-09-19 20:01:42 +00001021 (let ((pps (parse-partial-sexp (save-excursion
1022 (beginning-of-python-def-or-class)
1023 (point))
1024 (point))))
1025 (beginning-of-line)
1026 (cond
1027 ;; are we inside a string or comment?
1028 ((or (nth 3 pps) (nth 4 pps))
1029 (save-excursion
1030 (if (not py-align-multiline-strings-p) 0
1031 ;; skip back over blank & non-indenting comment lines
1032 ;; note: will skip a blank or non-indenting comment line
1033 ;; that happens to be a continuation line too
1034 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
1035 (back-to-indentation)
1036 (current-column))))
1037 ;; are we on a continuation line?
1038 ((py-continuation-line-p)
1039 (let ((startpos (point))
1040 (open-bracket-pos (py-nesting-level))
Barry Warsawce60bc71996-08-01 18:17:14 +00001041 endpos searching found state)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001042 (if open-bracket-pos
1043 (progn
1044 ;; align with first item in list; else a normal
1045 ;; indent beyond the line with the open bracket
1046 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
1047 ;; is the first list item on the same line?
1048 (skip-chars-forward " \t")
1049 (if (null (memq (following-char) '(?\n ?# ?\\)))
1050 ; yes, so line up with it
1051 (current-column)
1052 ;; first list item on another line, or doesn't exist yet
1053 (forward-line 1)
1054 (while (and (< (point) startpos)
1055 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
1056 (forward-line 1))
1057 (if (< (point) startpos)
1058 ;; again mimic the first list item
1059 (current-indentation)
1060 ;; else they're about to enter the first item
1061 (goto-char open-bracket-pos)
1062 (+ (current-indentation) py-indent-offset))))
1063
1064 ;; else on backslash continuation line
1065 (forward-line -1)
1066 (if (py-continuation-line-p) ; on at least 3rd line in block
1067 (current-indentation) ; so just continue the pattern
1068 ;; else started on 2nd line in block, so indent more.
1069 ;; if base line is an assignment with a start on a RHS,
1070 ;; indent to 2 beyond the leftmost "="; else skip first
1071 ;; chunk of non-whitespace characters on base line, + 1 more
1072 ;; column
1073 (end-of-line)
1074 (setq endpos (point) searching t)
1075 (back-to-indentation)
1076 (setq startpos (point))
1077 ;; look at all "=" from left to right, stopping at first
1078 ;; one not nested in a list or string
1079 (while searching
1080 (skip-chars-forward "^=" endpos)
1081 (if (= (point) endpos)
1082 (setq searching nil)
1083 (forward-char 1)
1084 (setq state (parse-partial-sexp startpos (point)))
1085 (if (and (zerop (car state)) ; not in a bracket
1086 (null (nth 3 state))) ; & not in a string
1087 (progn
1088 (setq searching nil) ; done searching in any case
1089 (setq found
1090 (not (or
1091 (eq (following-char) ?=)
1092 (memq (char-after (- (point) 2))
1093 '(?< ?> ?!)))))))))
1094 (if (or (not found) ; not an assignment
1095 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
1096 (progn
1097 (goto-char startpos)
1098 (skip-chars-forward "^ \t\n")))
1099 (1+ (current-column))))))
1100
1101 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00001102 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001103
Barry Warsawa7891711996-08-01 15:53:09 +00001104 ;; Dfn: "Indenting comment line". A line containing only a
1105 ;; comment, but which is treated like a statement for
1106 ;; indentation calculation purposes. Such lines are only
1107 ;; treated specially by the mode; they are not treated
1108 ;; specially by the Python interpreter.
1109
1110 ;; The rules for indenting comment lines are a line where:
1111 ;; - the first non-whitespace character is `#', and
1112 ;; - the character following the `#' is whitespace, and
1113 ;; - the line is outdented with respect to (i.e. to the left
1114 ;; of) the indentation of the preceding non-blank line.
1115
1116 ;; The first non-blank line following an indenting comment
1117 ;; line is given the same amount of indentation as the
1118 ;; indenting comment line.
1119
1120 ;; All other comment-only lines are ignored for indentation
1121 ;; purposes.
1122
1123 ;; Are we looking at a comment-only line which is *not* an
1124 ;; indenting comment line? If so, we assume that its been
1125 ;; placed at the desired indentation, so leave it alone.
1126 ;; Indenting comment lines are aligned as statements down
1127 ;; below.
1128 ((and (looking-at "[ \t]*#[^ \t\n]")
1129 ;; NOTE: this test will not be performed in older Emacsen
1130 (fboundp 'forward-comment)
1131 (<= (current-indentation)
1132 (save-excursion
1133 (forward-comment (- (point-max)))
1134 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001135 (current-indentation))
1136
1137 ;; else indentation based on that of the statement that
1138 ;; precedes us; use the first line of that statement to
1139 ;; establish the base, in case the user forced a non-std
1140 ;; indentation for the continuation lines (if any)
1141 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00001142 ;; skip back over blank & non-indenting comment lines note:
1143 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00001144 ;; happens to be a continuation line too. use fast Emacs 19
1145 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00001146 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00001147 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00001148 (forward-comment (- (point-max)))
Barry Warsaw6d627751996-03-06 18:41:38 +00001149 (let (done)
1150 (while (not done)
1151 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
1152 nil 'move)
1153 (setq done (or (eq py-honor-comment-indentation t)
1154 (bobp)
1155 (/= (following-char) ?#)
1156 (not (zerop (current-column)))))
1157 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001158 ;; if we landed inside a string, go to the beginning of that
1159 ;; string. this handles triple quoted, multi-line spanning
1160 ;; strings.
1161 (py-goto-initial-line)
Barry Warsawf831d811996-07-31 20:42:59 +00001162 (+ (current-indentation)
1163 (if (py-statement-opens-block-p)
1164 py-indent-offset
1165 (if (py-statement-closes-block-p)
1166 (- py-indent-offset)
1167 0)))
1168 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001169
1170(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001171 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001172By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001173`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +00001174Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001175`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +00001176their own buffer-local copy), both those currently existing and those
1177created later in the Emacs session.
1178
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001179Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001180There's no excuse for such foolishness, but sometimes you have to deal
1181with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001182`py-indent-offset' to what it thinks it was when they created the
1183mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001184
1185Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001186looking for a line that opens a block of code. `py-indent-offset' is
1187set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00001188statement following it. If the search doesn't succeed going forward,
1189it's tried again going backward."
1190 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001191 (let (new-value
1192 (start (point))
1193 restart
1194 (found nil)
1195 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001196 (py-goto-initial-line)
1197 (while (not (or found (eobp)))
1198 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1199 (progn
1200 (setq restart (point))
1201 (py-goto-initial-line)
1202 (if (py-statement-opens-block-p)
1203 (setq found t)
1204 (goto-char restart)))))
1205 (if found
1206 ()
1207 (goto-char start)
1208 (py-goto-initial-line)
1209 (while (not (or found (bobp)))
1210 (setq found
1211 (and
1212 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1213 (or (py-goto-initial-line) t) ; always true -- side effect
1214 (py-statement-opens-block-p)))))
1215 (setq colon-indent (current-indentation)
1216 found (and found (zerop (py-next-statement 1)))
1217 new-value (- (current-indentation) colon-indent))
1218 (goto-char start)
1219 (if found
1220 (progn
1221 (funcall (if global 'kill-local-variable 'make-local-variable)
1222 'py-indent-offset)
1223 (setq py-indent-offset new-value)
1224 (message "%s value of py-indent-offset set to %d"
1225 (if global "Global" "Local")
1226 py-indent-offset))
1227 (error "Sorry, couldn't guess a value for py-indent-offset"))))
1228
1229(defun py-shift-region (start end count)
1230 (save-excursion
1231 (goto-char end) (beginning-of-line) (setq end (point))
1232 (goto-char start) (beginning-of-line) (setq start (point))
1233 (indent-rigidly start end count)))
1234
1235(defun py-shift-region-left (start end &optional count)
1236 "Shift region of Python code to the left.
1237The lines from the line containing the start of the current region up
1238to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001239shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001240
1241If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001242many columns. With no active region, outdent only the current line.
1243You cannot outdent the region if any line is already at column zero."
1244 (interactive
1245 (let ((p (point))
1246 (m (mark))
1247 (arg current-prefix-arg))
1248 (if m
1249 (list (min p m) (max p m) arg)
1250 (list p (save-excursion (forward-line 1) (point)) arg))))
1251 ;; if any line is at column zero, don't shift the region
1252 (save-excursion
1253 (goto-char start)
1254 (while (< (point) end)
1255 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00001256 (if (and (zerop (current-column))
1257 (not (looking-at "\\s *$")))
Barry Warsawdea4a291996-07-03 22:59:12 +00001258 (error "Region is at left edge."))
1259 (forward-line 1)))
1260 (py-shift-region start end (- (prefix-numeric-value
1261 (or count py-indent-offset))))
1262 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001263
1264(defun py-shift-region-right (start end &optional count)
1265 "Shift region of Python code to the right.
1266The lines from the line containing the start of the current region up
1267to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001268shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001269
1270If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001271many columns. With no active region, indent only the current line."
1272 (interactive
1273 (let ((p (point))
1274 (m (mark))
1275 (arg current-prefix-arg))
1276 (if m
1277 (list (min p m) (max p m) arg)
1278 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001279 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00001280 (or count py-indent-offset)))
1281 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001282
1283(defun py-indent-region (start end &optional indent-offset)
1284 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00001285
Barry Warsaw7ae77681994-12-12 20:38:05 +00001286The lines from the line containing the start of the current region up
1287to (but not including) the line containing the end of the region are
1288reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001289character in the first column, the first line is left alone and the
1290rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00001291region is reindented with respect to the (closest code or indenting
1292comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001293
1294This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001295control structures are introduced or removed, or to reformat code
1296using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001297
1298If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001299the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00001300used.
1301
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001302Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00001303is called! This function does not compute proper indentation from
1304scratch (that's impossible in Python), it merely adjusts the existing
1305indentation to be correct in context.
1306
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001307Warning: This function really has no idea what to do with
1308non-indenting comment lines, and shifts them as if they were indenting
1309comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001310
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001311Special cases: whitespace is deleted from blank lines; continuation
1312lines are shifted by the same amount their initial line was shifted,
1313in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00001314initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001315 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001316 (save-excursion
1317 (goto-char end) (beginning-of-line) (setq end (point-marker))
1318 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001319 (let ((py-indent-offset (prefix-numeric-value
1320 (or indent-offset py-indent-offset)))
1321 (indents '(-1)) ; stack of active indent levels
1322 (target-column 0) ; column to which to indent
1323 (base-shifted-by 0) ; amount last base line was shifted
1324 (indent-base (if (looking-at "[ \t\n]")
1325 (py-compute-indentation)
1326 0))
1327 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001328 (while (< (point) end)
1329 (setq ci (current-indentation))
1330 ;; figure out appropriate target column
1331 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001332 ((or (eq (following-char) ?#) ; comment in column 1
1333 (looking-at "[ \t]*$")) ; entirely blank
1334 (setq target-column 0))
1335 ((py-continuation-line-p) ; shift relative to base line
1336 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001337 (t ; new base line
1338 (if (> ci (car indents)) ; going deeper; push it
1339 (setq indents (cons ci indents))
1340 ;; else we should have seen this indent before
1341 (setq indents (memq ci indents)) ; pop deeper indents
1342 (if (null indents)
1343 (error "Bad indentation in region, at line %d"
1344 (save-restriction
1345 (widen)
1346 (1+ (count-lines 1 (point)))))))
1347 (setq target-column (+ indent-base
1348 (* py-indent-offset
1349 (- (length indents) 2))))
1350 (setq base-shifted-by (- target-column ci))))
1351 ;; shift as needed
1352 (if (/= ci target-column)
1353 (progn
1354 (delete-horizontal-space)
1355 (indent-to target-column)))
1356 (forward-line 1))))
1357 (set-marker end nil))
1358
Barry Warsawa7891711996-08-01 15:53:09 +00001359(defun py-comment-region (beg end &optional arg)
1360 "Like `comment-region' but uses double hash (`#') comment starter."
1361 (interactive "r\nP")
1362 (let ((comment-start "## "))
1363 (comment-region beg end arg)))
1364
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001365
1366;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00001367(defun py-previous-statement (count)
1368 "Go to the start of previous Python statement.
1369If the statement at point is the i'th Python statement, goes to the
1370start of statement i-COUNT. If there is no such statement, goes to the
1371first statement. Returns count of statements left to move.
1372`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001373 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001374 (if (< count 0) (py-next-statement (- count))
1375 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001376 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001377 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001378 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001379 (> count 0)
1380 (zerop (forward-line -1))
1381 (py-goto-statement-at-or-above))
1382 (setq count (1- count)))
1383 (if (> count 0) (goto-char start)))
1384 count))
1385
1386(defun py-next-statement (count)
1387 "Go to the start of next Python statement.
1388If the statement at point is the i'th Python statement, goes to the
1389start of statement i+COUNT. If there is no such statement, goes to the
1390last statement. Returns count of statements left to move. `Statements'
1391do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001392 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001393 (if (< count 0) (py-previous-statement (- count))
1394 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001395 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001396 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001397 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001398 (> count 0)
1399 (py-goto-statement-below))
1400 (setq count (1- count)))
1401 (if (> count 0) (goto-char start)))
1402 count))
1403
1404(defun py-goto-block-up (&optional nomark)
1405 "Move up to start of current block.
1406Go to the statement that starts the smallest enclosing block; roughly
1407speaking, this will be the closest preceding statement that ends with a
1408colon and is indented less than the statement you started on. If
1409successful, also sets the mark to the starting point.
1410
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001411`\\[py-mark-block]' can be used afterward to mark the whole code
1412block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001413
1414If called from a program, the mark will not be set if optional argument
1415NOMARK is not nil."
1416 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001417 (let ((start (point))
1418 (found nil)
1419 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001420 (py-goto-initial-line)
1421 ;; if on blank or non-indenting comment line, use the preceding stmt
1422 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1423 (progn
1424 (py-goto-statement-at-or-above)
1425 (setq found (py-statement-opens-block-p))))
1426 ;; search back for colon line indented less
1427 (setq initial-indent (current-indentation))
1428 (if (zerop initial-indent)
1429 ;; force fast exit
1430 (goto-char (point-min)))
1431 (while (not (or found (bobp)))
1432 (setq found
1433 (and
1434 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1435 (or (py-goto-initial-line) t) ; always true -- side effect
1436 (< (current-indentation) initial-indent)
1437 (py-statement-opens-block-p))))
1438 (if found
1439 (progn
1440 (or nomark (push-mark start))
1441 (back-to-indentation))
1442 (goto-char start)
1443 (error "Enclosing block not found"))))
1444
1445(defun beginning-of-python-def-or-class (&optional class)
1446 "Move point to start of def (or class, with prefix arg).
1447
1448Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001449arg, looks for a `class' instead. The docs assume the `def' case;
1450just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001451
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001452If point is in a def statement already, and after the `d', simply
1453moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001454
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001455Else (point is not in a def statement, or at or before the `d' of a
1456def statement), searches for the closest preceding def statement, and
1457leaves point at its start. If no such statement can be found, leaves
1458point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001459
1460Returns t iff a def statement is found by these rules.
1461
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001462Note that doing this command repeatedly will take you closer to the
1463start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001464
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001465If you want to mark the current def/class, see
1466`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001467 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001468 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1469 (start-of-line (progn (beginning-of-line) (point)))
1470 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001471 (if (or (/= start-of-stmt start-of-line)
1472 (not at-or-before-p))
1473 (end-of-line)) ; OK to match on this line
1474 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001475 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001476
1477(defun end-of-python-def-or-class (&optional class)
1478 "Move point beyond end of def (or class, with prefix arg) body.
1479
1480By default, looks for an appropriate `def'. If you supply a prefix arg,
1481looks for a `class' instead. The docs assume the `def' case; just
1482substitute `class' for `def' for the other case.
1483
1484If point is in a def statement already, this is the def we use.
1485
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001486Else if the def found by `\\[beginning-of-python-def-or-class]'
1487contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001488
1489Else we search forward for the closest following def, and use that.
1490
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001491If a def can be found by these rules, point is moved to the start of
1492the line immediately following the def block, and the position of the
1493start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001494
1495Else point is moved to the end of the buffer, and nil is returned.
1496
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001497Note that doing this command repeatedly will take you closer to the
1498end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001499
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001500If you want to mark the current def/class, see
1501`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001502 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001503 (let ((start (progn (py-goto-initial-line) (point)))
1504 (which (if class "class" "def"))
1505 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001506 ;; move point to start of appropriate def/class
1507 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1508 (setq state 'at-beginning)
1509 ;; else see if beginning-of-python-def-or-class hits container
1510 (if (and (beginning-of-python-def-or-class class)
1511 (progn (py-goto-beyond-block)
1512 (> (point) start)))
1513 (setq state 'at-end)
1514 ;; else search forward
1515 (goto-char start)
1516 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1517 (progn (setq state 'at-beginning)
1518 (beginning-of-line)))))
1519 (cond
1520 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1521 ((eq state 'at-end) t)
1522 ((eq state 'not-found) nil)
1523 (t (error "internal error in end-of-python-def-or-class")))))
1524
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001525
1526;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001527(defun py-mark-block (&optional extend just-move)
1528 "Mark following block of lines. With prefix arg, mark structure.
1529Easier to use than explain. It sets the region to an `interesting'
1530block of succeeding lines. If point is on a blank line, it goes down to
1531the next non-blank line. That will be the start of the region. The end
1532of the region depends on the kind of line at the start:
1533
1534 - If a comment, the region will include all succeeding comment lines up
1535 to (but not including) the next non-comment line (if any).
1536
1537 - Else if a prefix arg is given, and the line begins one of these
1538 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001539
1540 if elif else try except finally for while def class
1541
Barry Warsaw7ae77681994-12-12 20:38:05 +00001542 the region will be set to the body of the structure, including
1543 following blocks that `belong' to it, but excluding trailing blank
1544 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001545 and all (if any) of the following `except' and `finally' blocks
1546 that belong to the `try' structure will be in the region. Ditto
1547 for if/elif/else, for/else and while/else structures, and (a bit
1548 degenerate, since they're always one-block structures) def and
1549 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001550
1551 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001552 block (see list above), and the block is not a `one-liner' (i.e.,
1553 the statement ends with a colon, not with code), the region will
1554 include all succeeding lines up to (but not including) the next
1555 code statement (if any) that's indented no more than the starting
1556 line, except that trailing blank and comment lines are excluded.
1557 E.g., if the starting line begins a multi-statement `def'
1558 structure, the region will be set to the full function definition,
1559 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001560
1561 - Else the region will include all succeeding lines up to (but not
1562 including) the next blank line, or code or indenting-comment line
1563 indented strictly less than the starting line. Trailing indenting
1564 comment lines are included in this case, but not trailing blank
1565 lines.
1566
1567A msg identifying the location of the mark is displayed in the echo
1568area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1569
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001570If called from a program, optional argument EXTEND plays the role of
1571the prefix arg, and if optional argument JUST-MOVE is not nil, just
1572moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001573 (interactive "P") ; raw prefix arg
1574 (py-goto-initial-line)
1575 ;; skip over blank lines
1576 (while (and
1577 (looking-at "[ \t]*$") ; while blank line
1578 (not (eobp))) ; & somewhere to go
1579 (forward-line 1))
1580 (if (eobp)
1581 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001582 (let ((initial-pos (point))
1583 (initial-indent (current-indentation))
1584 last-pos ; position of last stmt in region
1585 (followers
1586 '((if elif else) (elif elif else) (else)
1587 (try except finally) (except except) (finally)
1588 (for else) (while else)
1589 (def) (class) ) )
1590 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001591
1592 (cond
1593 ;; if comment line, suck up the following comment lines
1594 ((looking-at "[ \t]*#")
1595 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1596 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1597 (setq last-pos (point)))
1598
1599 ;; else if line is a block line and EXTEND given, suck up
1600 ;; the whole structure
1601 ((and extend
1602 (setq first-symbol (py-suck-up-first-keyword) )
1603 (assq first-symbol followers))
1604 (while (and
1605 (or (py-goto-beyond-block) t) ; side effect
1606 (forward-line -1) ; side effect
1607 (setq last-pos (point)) ; side effect
1608 (py-goto-statement-below)
1609 (= (current-indentation) initial-indent)
1610 (setq next-symbol (py-suck-up-first-keyword))
1611 (memq next-symbol (cdr (assq first-symbol followers))))
1612 (setq first-symbol next-symbol)))
1613
1614 ;; else if line *opens* a block, search for next stmt indented <=
1615 ((py-statement-opens-block-p)
1616 (while (and
1617 (setq last-pos (point)) ; always true -- side effect
1618 (py-goto-statement-below)
1619 (> (current-indentation) initial-indent))
1620 nil))
1621
1622 ;; else plain code line; stop at next blank line, or stmt or
1623 ;; indenting comment line indented <
1624 (t
1625 (while (and
1626 (setq last-pos (point)) ; always true -- side effect
1627 (or (py-goto-beyond-final-line) t)
1628 (not (looking-at "[ \t]*$")) ; stop at blank line
1629 (or
1630 (>= (current-indentation) initial-indent)
1631 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1632 nil)))
1633
1634 ;; skip to end of last stmt
1635 (goto-char last-pos)
1636 (py-goto-beyond-final-line)
1637
1638 ;; set mark & display
1639 (if just-move
1640 () ; just return
1641 (push-mark (point) 'no-msg)
1642 (forward-line -1)
1643 (message "Mark set after: %s" (py-suck-up-leading-text))
1644 (goto-char initial-pos))))
1645
1646(defun mark-python-def-or-class (&optional class)
1647 "Set region to body of def (or class, with prefix arg) enclosing point.
1648Pushes the current mark, then point, on the mark ring (all language
1649modes do this, but although it's handy it's never documented ...).
1650
1651In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001652hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1653`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001654
1655And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001656Turned out that was more confusing than useful: the `goto start' and
1657`goto end' commands are usually used to search through a file, and
1658people expect them to act a lot like `search backward' and `search
1659forward' string-search commands. But because Python `def' and `class'
1660can nest to arbitrary levels, finding the smallest def containing
1661point cannot be done via a simple backward search: the def containing
1662point may not be the closest preceding def, or even the closest
1663preceding def that's indented less. The fancy algorithm required is
1664appropriate for the usual uses of this `mark' command, but not for the
1665`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001666
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001667So the def marked by this command may not be the one either of the
1668`goto' commands find: If point is on a blank or non-indenting comment
1669line, moves back to start of the closest preceding code statement or
1670indenting comment line. If this is a `def' statement, that's the def
1671we use. Else searches for the smallest enclosing `def' block and uses
1672that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001673
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001674When an enclosing def is found: The mark is left immediately beyond
1675the last line of the def block. Point is left at the start of the
1676def, except that: if the def is preceded by a number of comment lines
1677followed by (at most) one optional blank line, point is left at the
1678start of the comments; else if the def is preceded by a blank line,
1679point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001680
1681The intent is to mark the containing def/class and its associated
1682documentation, to make moving and duplicating functions and classes
1683pleasant."
1684 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001685 (let ((start (point))
1686 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001687 (push-mark start)
1688 (if (not (py-go-up-tree-to-keyword which))
1689 (progn (goto-char start)
1690 (error "Enclosing %s not found" which))
1691 ;; else enclosing def/class found
1692 (setq start (point))
1693 (py-goto-beyond-block)
1694 (push-mark (point))
1695 (goto-char start)
1696 (if (zerop (forward-line -1)) ; if there is a preceding line
1697 (progn
1698 (if (looking-at "[ \t]*$") ; it's blank
1699 (setq start (point)) ; so reset start point
1700 (goto-char start)) ; else try again
1701 (if (zerop (forward-line -1))
1702 (if (looking-at "[ \t]*#") ; a comment
1703 ;; look back for non-comment line
1704 ;; tricky: note that the regexp matches a blank
1705 ;; line, cuz \n is in the 2nd character class
1706 (and
1707 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1708 (forward-line 1))
1709 ;; no comment, so go back
1710 (goto-char start))))))))
1711
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00001712;; ripped from cc-mode
1713(defun py-forward-into-nomenclature (&optional arg)
1714 "Move forward to end of a nomenclature section or word.
1715With arg, to it arg times.
1716
1717A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
1718 (interactive "p")
1719 (let ((case-fold-search nil))
1720 (if (> arg 0)
Barry Warsawc846f461996-07-25 18:53:17 +00001721 (re-search-forward "\\W*\\([A-Z_]*[a-z0-9]*\\)" (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00001722 (while (and (< arg 0)
1723 (re-search-backward
1724 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
1725 (point-min) 0))
1726 (forward-char 1)
1727 (setq arg (1+ arg)))))
1728 (py-keep-region-active))
1729
1730(defun py-backward-into-nomenclature (&optional arg)
1731 "Move backward to beginning of a nomenclature section or word.
1732With optional ARG, move that many times. If ARG is negative, move
1733forward.
1734
1735A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
1736 (interactive "p")
1737 (py-forward-into-nomenclature (- arg))
1738 (py-keep-region-active))
1739
1740
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001741
1742;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001743
1744;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001745;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1746;; out of the right places, along with the keys they're on & current
1747;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00001748(defun py-dump-help-string (str)
1749 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001750 (let ((locals (buffer-local-variables))
1751 funckind funcname func funcdoc
1752 (start 0) mstart end
1753 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001754 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1755 (setq mstart (match-beginning 0) end (match-end 0)
1756 funckind (substring str (match-beginning 1) (match-end 1))
1757 funcname (substring str (match-beginning 2) (match-end 2))
1758 func (intern funcname))
1759 (princ (substitute-command-keys (substring str start mstart)))
1760 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001761 ((equal funckind "c") ; command
1762 (setq funcdoc (documentation func)
1763 keys (concat
1764 "Key(s): "
1765 (mapconcat 'key-description
1766 (where-is-internal func py-mode-map)
1767 ", "))))
1768 ((equal funckind "v") ; variable
1769 (setq funcdoc (substitute-command-keys
1770 (get func 'variable-documentation))
1771 keys (if (assq func locals)
1772 (concat
1773 "Local/Global values: "
1774 (prin1-to-string (symbol-value func))
1775 " / "
1776 (prin1-to-string (default-value func)))
1777 (concat
1778 "Value: "
1779 (prin1-to-string (symbol-value func))))))
1780 (t ; unexpected
1781 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001782 (princ (format "\n-> %s:\t%s\t%s\n\n"
1783 (if (equal funckind "c") "Command" "Variable")
1784 funcname keys))
1785 (princ funcdoc)
1786 (terpri)
1787 (setq start end))
1788 (princ (substitute-command-keys (substring str start))))
1789 (print-help-return-message)))
1790
1791(defun py-describe-mode ()
1792 "Dump long form of Python-mode docs."
1793 (interactive)
1794 (py-dump-help-string "Major mode for editing Python files.
1795Knows about Python indentation, tokens, comments and continuation lines.
1796Paragraphs are separated by blank lines only.
1797
1798Major sections below begin with the string `@'; specific function and
1799variable docs begin with `->'.
1800
1801@EXECUTING PYTHON CODE
1802
1803\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1804\\[py-execute-region]\tsends the current region
1805\\[py-shell]\tstarts a Python interpreter window; this will be used by
1806\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1807%c:py-execute-buffer
1808%c:py-execute-region
1809%c:py-shell
1810
1811@VARIABLES
1812
1813py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00001814py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00001815
1816py-python-command\tshell command to invoke Python interpreter
1817py-scroll-process-buffer\talways scroll Python process buffer
1818py-temp-directory\tdirectory used for temp files (if needed)
1819
1820py-beep-if-tab-change\tring the bell if tab-width is changed
1821%v:py-indent-offset
1822%v:py-block-comment-prefix
1823%v:py-python-command
1824%v:py-scroll-process-buffer
1825%v:py-temp-directory
1826%v:py-beep-if-tab-change
1827
1828@KINDS OF LINES
1829
1830Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001831preceding line ends with a backslash that's not part of a comment, or
1832the paren/bracket/brace nesting level at the start of the line is
1833non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001834
1835An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001836possibly blanks or tabs), a `comment line' (leftmost non-blank
1837character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001838
1839Comment Lines
1840
1841Although all comment lines are treated alike by Python, Python mode
1842recognizes two kinds that act differently with respect to indentation.
1843
1844An `indenting comment line' is a comment line with a blank, tab or
1845nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001846treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00001847indenting comment line will be indented like the comment line. All
1848other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001849following the initial `#') are `non-indenting comment lines', and
1850their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001851
1852Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001853whenever possible. Non-indenting comment lines are useful in cases
1854like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00001855
1856\ta = b # a very wordy single-line comment that ends up being
1857\t #... continued onto another line
1858
1859\tif a == b:
1860##\t\tprint 'panic!' # old code we've `commented out'
1861\t\treturn a
1862
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001863Since the `#...' and `##' comment lines have a non-whitespace
1864character following the initial `#', Python mode ignores them when
1865computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001866
1867Continuation Lines and Statements
1868
1869The Python-mode commands generally work on statements instead of on
1870individual lines, where a `statement' is a comment or blank line, or a
1871code line and all of its following continuation lines (if any)
1872considered as a single logical unit. The commands in this mode
1873generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001874statement containing point, even if point happens to be in the middle
1875of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001876
1877
1878@INDENTATION
1879
1880Primarily for entering new code:
1881\t\\[indent-for-tab-command]\t indent line appropriately
1882\t\\[py-newline-and-indent]\t insert newline, then indent
1883\t\\[py-delete-char]\t reduce indentation, or delete single character
1884
1885Primarily for reindenting existing code:
1886\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1887\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1888
1889\t\\[py-indent-region]\t reindent region to match its context
1890\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1891\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1892
1893Unlike most programming languages, Python uses indentation, and only
1894indentation, to specify block structure. Hence the indentation supplied
1895automatically by Python-mode is just an educated guess: only you know
1896the block structure you intend, so only you can supply correct
1897indentation.
1898
1899The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1900the indentation of preceding statements. E.g., assuming
1901py-indent-offset is 4, after you enter
1902\tif a > 0: \\[py-newline-and-indent]
1903the cursor will be moved to the position of the `_' (_ is not a
1904character in the file, it's just used here to indicate the location of
1905the cursor):
1906\tif a > 0:
1907\t _
1908If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1909to
1910\tif a > 0:
1911\t c = d
1912\t _
1913Python-mode cannot know whether that's what you intended, or whether
1914\tif a > 0:
1915\t c = d
1916\t_
1917was your intent. In general, Python-mode either reproduces the
1918indentation of the (closest code or indenting-comment) preceding
1919statement, or adds an extra py-indent-offset blanks if the preceding
1920statement has `:' as its last significant (non-whitespace and non-
1921comment) character. If the suggested indentation is too much, use
1922\\[py-delete-char] to reduce it.
1923
1924Continuation lines are given extra indentation. If you don't like the
1925suggested indentation, change it to something you do like, and Python-
1926mode will strive to indent later lines of the statement in the same way.
1927
1928If a line is a continuation line by virtue of being in an unclosed
1929paren/bracket/brace structure (`list', for short), the suggested
1930indentation depends on whether the current line contains the first item
1931in the list. If it does, it's indented py-indent-offset columns beyond
1932the indentation of the line containing the open bracket. If you don't
1933like that, change it by hand. The remaining items in the list will mimic
1934whatever indentation you give to the first item.
1935
1936If a line is a continuation line because the line preceding it ends with
1937a backslash, the third and following lines of the statement inherit their
1938indentation from the line preceding them. The indentation of the second
1939line in the statement depends on the form of the first (base) line: if
1940the base line is an assignment statement with anything more interesting
1941than the backslash following the leftmost assigning `=', the second line
1942is indented two columns beyond that `='. Else it's indented to two
1943columns beyond the leftmost solid chunk of non-whitespace characters on
1944the base line.
1945
1946Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1947repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1948structure you intend.
1949%c:indent-for-tab-command
1950%c:py-newline-and-indent
1951%c:py-delete-char
1952
1953
1954The next function may be handy when editing code you didn't write:
1955%c:py-guess-indent-offset
1956
1957
1958The remaining `indent' functions apply to a region of Python code. They
1959assume the block structure (equals indentation, in Python) of the region
1960is correct, and alter the indentation in various ways while preserving
1961the block structure:
1962%c:py-indent-region
1963%c:py-shift-region-left
1964%c:py-shift-region-right
1965
1966@MARKING & MANIPULATING REGIONS OF CODE
1967
1968\\[py-mark-block]\t mark block of lines
1969\\[mark-python-def-or-class]\t mark smallest enclosing def
1970\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00001971\\[comment-region]\t comment out region of code
1972\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00001973%c:py-mark-block
1974%c:mark-python-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00001975%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00001976
1977@MOVING POINT
1978
1979\\[py-previous-statement]\t move to statement preceding point
1980\\[py-next-statement]\t move to statement following point
1981\\[py-goto-block-up]\t move up to start of current block
1982\\[beginning-of-python-def-or-class]\t move to start of def
1983\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
1984\\[end-of-python-def-or-class]\t move to end of def
1985\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
1986
1987The first two move to one statement beyond the statement that contains
1988point. A numeric prefix argument tells them to move that many
1989statements instead. Blank lines, comment lines, and continuation lines
1990do not count as `statements' for these commands. So, e.g., you can go
1991to the first code statement in a file by entering
1992\t\\[beginning-of-buffer]\t to move to the top of the file
1993\t\\[py-next-statement]\t to skip over initial comments and blank lines
1994Or do `\\[py-previous-statement]' with a huge prefix argument.
1995%c:py-previous-statement
1996%c:py-next-statement
1997%c:py-goto-block-up
1998%c:beginning-of-python-def-or-class
1999%c:end-of-python-def-or-class
2000
2001@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
2002
2003`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
2004
2005`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
2006overall class and def structure of a module.
2007
2008`\\[back-to-indentation]' moves point to a line's first non-blank character.
2009
2010`\\[indent-relative]' is handy for creating odd indentation.
2011
2012@OTHER EMACS HINTS
2013
2014If you don't like the default value of a variable, change its value to
2015whatever you do like by putting a `setq' line in your .emacs file.
2016E.g., to set the indentation increment to 4, put this line in your
2017.emacs:
2018\t(setq py-indent-offset 4)
2019To see the value of a variable, do `\\[describe-variable]' and enter the variable
2020name at the prompt.
2021
2022When entering a key sequence like `C-c C-n', it is not necessary to
2023release the CONTROL key after doing the `C-c' part -- it suffices to
2024press the CONTROL key, press and release `c' (while still holding down
2025CONTROL), press and release `n' (while still holding down CONTROL), &
2026then release CONTROL.
2027
2028Entering Python mode calls with no arguments the value of the variable
2029`python-mode-hook', if that value exists and is not nil; for backward
2030compatibility it also tries `py-mode-hook'; see the `Hooks' section of
2031the Elisp manual for details.
2032
2033Obscure: When python-mode is first loaded, it looks for all bindings
2034to newline-and-indent in the global keymap, and shadows them with
2035local bindings to py-newline-and-indent."))
2036
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002037
2038;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002039(defvar py-parse-state-re
2040 (concat
2041 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
2042 "\\|"
2043 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002044
Barry Warsaw7ae77681994-12-12 20:38:05 +00002045;; returns the parse state at point (see parse-partial-sexp docs)
2046(defun py-parse-state ()
2047 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002048 (let ((here (point))
Barry Warsaw170ffa71996-07-31 20:57:22 +00002049 pps done ci)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002050 (while (not done)
2051 ;; back up to the first preceding line (if any; else start of
2052 ;; buffer) that begins with a popular Python keyword, or a
2053 ;; non- whitespace and non-comment character. These are good
2054 ;; places to start parsing to see whether where we started is
2055 ;; at a non-zero nesting level. It may be slow for people who
2056 ;; write huge code blocks or huge lists ... tough beans.
2057 (re-search-backward py-parse-state-re nil 'move)
Barry Warsaw170ffa71996-07-31 20:57:22 +00002058 (setq ci (current-indentation))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002059 (beginning-of-line)
2060 (save-excursion
2061 (setq pps (parse-partial-sexp (point) here)))
2062 ;; make sure we don't land inside a triple-quoted string
Barry Warsaw170ffa71996-07-31 20:57:22 +00002063 (setq done (or (zerop ci)
2064 (not (nth 3 pps))
2065 (bobp)))
2066 )
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002067 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002068
2069;; if point is at a non-zero nesting level, returns the number of the
2070;; character that opens the smallest enclosing unclosed list; else
2071;; returns nil.
2072(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002073 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002074 (if (zerop (car status))
2075 nil ; not in a nest
2076 (car (cdr status))))) ; char# of open bracket
2077
2078;; t iff preceding line ends with backslash that's not in a comment
2079(defun py-backslash-continuation-line-p ()
2080 (save-excursion
2081 (beginning-of-line)
2082 (and
2083 ;; use a cheap test first to avoid the regexp if possible
2084 ;; use 'eq' because char-after may return nil
2085 (eq (char-after (- (point) 2)) ?\\ )
2086 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002087 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002088 (looking-at py-continued-re))))
2089
2090;; t iff current line is a continuation line
2091(defun py-continuation-line-p ()
2092 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002093 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002094 (or (py-backslash-continuation-line-p)
2095 (py-nesting-level))))
2096
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002097;; go to initial line of current statement; usually this is the line
2098;; we're on, but if we're on the 2nd or following lines of a
2099;; continuation block, we need to go up to the first line of the
2100;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002101;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002102;; Tricky: We want to avoid quadratic-time behavior for long continued
2103;; blocks, whether of the backslash or open-bracket varieties, or a
2104;; mix of the two. The following manages to do that in the usual
2105;; cases.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002106(defun py-goto-initial-line ()
2107 (let ( open-bracket-pos )
2108 (while (py-continuation-line-p)
2109 (beginning-of-line)
2110 (if (py-backslash-continuation-line-p)
2111 (while (py-backslash-continuation-line-p)
2112 (forward-line -1))
2113 ;; else zip out of nested brackets/braces/parens
2114 (while (setq open-bracket-pos (py-nesting-level))
2115 (goto-char open-bracket-pos)))))
2116 (beginning-of-line))
2117
2118;; go to point right beyond final line of current statement; usually
2119;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002120;; statement we need to skip over the continuation lines. Tricky:
2121;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002122(defun py-goto-beyond-final-line ()
2123 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002124 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002125 (while (and (py-continuation-line-p)
2126 (not (eobp)))
2127 ;; skip over the backslash flavor
2128 (while (and (py-backslash-continuation-line-p)
2129 (not (eobp)))
2130 (forward-line 1))
2131 ;; if in nest, zip to the end of the nest
2132 (setq state (py-parse-state))
2133 (if (and (not (zerop (car state)))
2134 (not (eobp)))
2135 (progn
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002136 ;; BUG ALERT: I could swear, from reading the docs, that
Barry Warsaw7ae77681994-12-12 20:38:05 +00002137 ;; the 3rd argument should be plain 0
2138 (parse-partial-sexp (point) (point-max) (- 0 (car state))
2139 nil state)
2140 (forward-line 1))))))
2141
2142;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002143;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00002144(defun py-statement-opens-block-p ()
2145 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002146 (let ((start (point))
2147 (finish (progn (py-goto-beyond-final-line) (1- (point))))
2148 (searching t)
2149 (answer nil)
2150 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002151 (goto-char start)
2152 (while searching
2153 ;; look for a colon with nothing after it except whitespace, and
2154 ;; maybe a comment
2155 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
2156 finish t)
2157 (if (eq (point) finish) ; note: no `else' clause; just
2158 ; keep searching if we're not at
2159 ; the end yet
2160 ;; sure looks like it opens a block -- but it might
2161 ;; be in a comment
2162 (progn
2163 (setq searching nil) ; search is done either way
2164 (setq state (parse-partial-sexp start
2165 (match-beginning 0)))
2166 (setq answer (not (nth 4 state)))))
2167 ;; search failed: couldn't find another interesting colon
2168 (setq searching nil)))
2169 answer)))
2170
Barry Warsawf831d811996-07-31 20:42:59 +00002171(defun py-statement-closes-block-p ()
2172 ;; true iff the current statement `closes' a block == the line
2173 ;; starts with `return', `raise', `break' or `continue'. doesn't
2174 ;; catch embedded statements
2175 (let ((here (point)))
2176 (back-to-indentation)
2177 (prog1
2178 (looking-at "\\(return\\|raise\\|break\\|continue\\)\\>")
2179 (goto-char here))))
2180
Barry Warsaw7ae77681994-12-12 20:38:05 +00002181;; go to point right beyond final line of block begun by the current
2182;; line. This is the same as where py-goto-beyond-final-line goes
2183;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002184;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00002185(defun py-goto-beyond-block ()
2186 (if (py-statement-opens-block-p)
2187 (py-mark-block nil 'just-move)
2188 (py-goto-beyond-final-line)))
2189
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002190;; go to start of first statement (not blank or comment or
2191;; continuation line) at or preceding point. returns t if there is
2192;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002193(defun py-goto-statement-at-or-above ()
2194 (py-goto-initial-line)
2195 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002196 ;; skip back over blank & comment lines
2197 ;; note: will skip a blank or comment line that happens to be
2198 ;; a continuation line too
2199 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
2200 (progn (py-goto-initial-line) t)
2201 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002202 t))
2203
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002204;; go to start of first statement (not blank or comment or
2205;; continuation line) following the statement containing point returns
2206;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002207(defun py-goto-statement-below ()
2208 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002209 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002210 (py-goto-beyond-final-line)
2211 (while (and
2212 (looking-at py-blank-or-comment-re)
2213 (not (eobp)))
2214 (forward-line 1))
2215 (if (eobp)
2216 (progn (goto-char start) nil)
2217 t)))
2218
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002219;; go to start of statement, at or preceding point, starting with
2220;; keyword KEY. Skips blank lines and non-indenting comments upward
2221;; first. If that statement starts with KEY, done, else go back to
2222;; first enclosing block starting with KEY. If successful, leaves
2223;; point at the start of the KEY line & returns t. Else leaves point
2224;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002225(defun py-go-up-tree-to-keyword (key)
2226 ;; skip blanks and non-indenting #
2227 (py-goto-initial-line)
2228 (while (and
2229 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2230 (zerop (forward-line -1))) ; go back
2231 nil)
2232 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002233 (let* ((re (concat "[ \t]*" key "\\b"))
2234 (case-fold-search nil) ; let* so looking-at sees this
2235 (found (looking-at re))
2236 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002237 (while (not (or found dead))
2238 (condition-case nil ; in case no enclosing block
2239 (py-goto-block-up 'no-mark)
2240 (error (setq dead t)))
2241 (or dead (setq found (looking-at re))))
2242 (beginning-of-line)
2243 found))
2244
2245;; return string in buffer from start of indentation to end of line;
2246;; prefix "..." if leading whitespace was skipped
2247(defun py-suck-up-leading-text ()
2248 (save-excursion
2249 (back-to-indentation)
2250 (concat
2251 (if (bolp) "" "...")
2252 (buffer-substring (point) (progn (end-of-line) (point))))))
2253
2254;; assuming point at bolp, return first keyword ([a-z]+) on the line,
2255;; as a Lisp symbol; return nil if none
2256(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002257 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002258 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
2259 (intern (buffer-substring (match-beginning 1) (match-end 1)))
2260 nil)))
2261
2262(defun py-make-temp-name ()
2263 (make-temp-name
2264 (concat (file-name-as-directory py-temp-directory) "python")))
2265
2266(defun py-delete-file-silently (fname)
2267 (condition-case nil
2268 (delete-file fname)
2269 (error nil)))
2270
2271(defun py-kill-emacs-hook ()
2272 ;; delete our temp files
2273 (while py-file-queue
2274 (py-delete-file-silently (car py-file-queue))
2275 (setq py-file-queue (cdr py-file-queue)))
2276 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
2277 ;; run the hook we inherited, if any
2278 (and py-inherited-kill-emacs-hook
2279 (funcall py-inherited-kill-emacs-hook))))
2280
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002281;; make PROCESS's buffer visible, append STRING to it, and force
2282;; display; also make shell-mode believe the user typed this string,
2283;; so that kill-output-from-shell and show-output-from-shell work
2284;; "right"
Barry Warsaw7ae77681994-12-12 20:38:05 +00002285(defun py-append-to-process-buffer (process string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002286 (let ((cbuf (current-buffer))
2287 (pbuf (process-buffer process))
2288 (py-scroll-process-buffer t))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002289 (set-buffer pbuf)
2290 (goto-char (point-max))
2291 (move-marker (process-mark process) (point))
Barry Warsaw4dba7e21995-07-05 23:01:43 +00002292 (if (not (or py-this-is-emacs-19-p
2293 py-this-is-lucid-emacs-p))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002294 (move-marker last-input-start (point))) ; muck w/ shell-mode
2295 (funcall (process-filter process) process string)
Barry Warsaw4dba7e21995-07-05 23:01:43 +00002296 (if (not (or py-this-is-emacs-19-p
2297 py-this-is-lucid-emacs-p))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002298 (move-marker last-input-end (point))) ; muck w/ shell-mode
2299 (set-buffer cbuf))
2300 (sit-for 0))
2301
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002302
2303
Barry Warsaw850437a1995-03-08 21:50:28 +00002304(defconst py-version "$Revision$"
2305 "`python-mode' version number.")
Barry Warsawfec75d61995-07-05 23:26:15 +00002306(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00002307 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002308
Barry Warsaw850437a1995-03-08 21:50:28 +00002309(defun py-version ()
2310 "Echo the current version of `python-mode' in the minibuffer."
2311 (interactive)
2312 (message "Using `python-mode' version %s" py-version)
2313 (py-keep-region-active))
2314
2315;; only works under Emacs 19
2316;(eval-when-compile
2317; (require 'reporter))
2318
2319(defun py-submit-bug-report (enhancement-p)
2320 "Submit via mail a bug report on `python-mode'.
2321With \\[universal-argument] just submit an enhancement request."
2322 (interactive
2323 (list (not (y-or-n-p
2324 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002325 (let ((reporter-prompt-for-summary-p (if enhancement-p
2326 "(Very) brief summary: "
2327 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00002328 (require 'reporter)
2329 (reporter-submit-bug-report
2330 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002331 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00002332 ;; varlist
2333 (if enhancement-p nil
2334 '(py-python-command
2335 py-indent-offset
2336 py-block-comment-prefix
2337 py-scroll-process-buffer
2338 py-temp-directory
2339 py-beep-if-tab-change))
2340 nil ;pre-hooks
2341 nil ;post-hooks
2342 "Dear Barry,") ;salutation
2343 (if enhancement-p nil
2344 (set-mark (point))
2345 (insert
2346"Please replace this text with a sufficiently large code sample\n\
2347and an exact recipe so that I can reproduce your problem. Failure\n\
2348to do so may mean a greater delay in fixing your bug.\n\n")
2349 (exchange-point-and-mark)
2350 (py-keep-region-active))))
2351
2352
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002353;; arrange to kill temp files when Emacs exists
2354(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
2355 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
2356 ;; have to trust that other people are as respectful of our hook
2357 ;; fiddling as we are of theirs
2358 (if (boundp 'py-inherited-kill-emacs-hook)
2359 ;; we were loaded before -- trust others not to have screwed us
2360 ;; in the meantime (no choice, really)
2361 nil
2362 ;; else arrange for our hook to run theirs
2363 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
2364 (setq kill-emacs-hook 'py-kill-emacs-hook)))
2365
2366
2367
2368(provide 'python-mode)
2369;;; python-mode.el ends here